-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathtoolTimelineFormatting.ts
More file actions
191 lines (169 loc) · 6.07 KB
/
toolTimelineFormatting.ts
File metadata and controls
191 lines (169 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import type { ToolTimelineEntry } from '../store/chatRuntimeSlice';
interface ParsedToolArgs {
agent_id?: string;
prompt?: string;
toolkit?: string;
}
export function formatTimelineEntry(entry: ToolTimelineEntry): { title: string; detail?: string } {
const parsedArgs = parseToolArgs(entry.argsBuffer);
if (entry.name === 'spawn_subagent' && parsedArgs?.agent_id === 'integrations_agent') {
const provider =
inferIntegrationName(parsedArgs.toolkit) ?? inferIntegrationNameFromPrompt(parsedArgs.prompt);
return {
title: provider ? integrationActivityTitle(provider) : 'Checking your connected app',
detail: parsedArgs.prompt?.trim() || entry.detail,
};
}
if (entry.name === 'integrations_agent' || entry.name === 'subagent:integrations_agent') {
const provider =
inferIntegrationName(entry.sourceToolName) ??
inferIntegrationName(parsedArgs?.toolkit) ??
inferIntegrationNameFromPrompt(entry.detail) ??
inferIntegrationNameFromPrompt(parsedArgs?.prompt);
return {
title: provider ? integrationActivityTitle(provider) : 'Checking your connected app',
detail: entry.detail,
};
}
if (entry.name === 'subagent:researcher' || entry.name === 'researcher') {
return { title: 'Researching', detail: entry.detail };
}
if (entry.name === 'composio_list_connections') {
return { title: 'Viewing your Connections', detail: entry.detail };
}
if (entry.name === 'subagent:orchestrator' || entry.name === 'orchestrator') {
return { title: 'Planning next steps', detail: entry.detail };
}
if (entry.name === 'subagent:critic' || entry.name === 'critic') {
return { title: 'Reviewing the work', detail: entry.detail };
}
if (entry.name === 'subagent:tools_agent' || entry.name === 'tools_agent') {
return { title: 'Using tools', detail: entry.detail };
}
if (entry.name === 'subagent:code_executor' || entry.name === 'code_executor') {
return { title: 'Running code', detail: entry.detail };
}
if (entry.name.startsWith('delegate_')) {
const provider =
inferIntegrationName(parsedArgs?.toolkit) ??
inferIntegrationNameFromPrompt(parsedArgs?.prompt) ??
inferIntegrationName(entry.name);
// The collapsed `delegate_to_integrations_agent` tool has no toolkit
// baked into its name; if we couldn't infer a provider from args or
// prompt, surface a generic connected-app label (matches the
// `spawn_subagent → integrations_agent` fallback above) instead of
// humanising the tool name into "To Integrations Agent". Unknown
// toolkit slugs from args fall back to a humanised toolkit label so
// composio integrations outside KNOWN_TOOLKIT_RE still render
// meaningfully (e.g. `slack_bot` → "Slack Bot") rather than the
// generic copy.
let title: string;
if (provider) {
title = integrationActivityTitle(provider);
} else if (entry.name === 'delegate_to_integrations_agent') {
const rawToolkit = parsedArgs?.toolkit?.trim();
title = rawToolkit
? integrationActivityTitle(humanizeIdentifier(rawToolkit))
: 'Checking your connected app';
} else {
title = humanizeIdentifier(entry.name);
}
return {
title,
detail: entry.detail ?? parsedArgs?.prompt,
};
}
return {
title: entry.displayName ?? humanizeIdentifier(entry.name),
detail: entry.detail ?? parsedArgs?.prompt,
};
}
export function promptFromArgsBuffer(argsBuffer?: string): string | undefined {
return parseToolArgs(argsBuffer)?.prompt?.trim() || undefined;
}
/**
* Recognise the small set of known integration toolkit slugs. Used to
* gate `inferIntegrationName` so unknown `delegate_<x>` names (e.g.
* `delegate_summarize`, `delegate_router`) don't get fake-humanised
* into bogus "integration" labels in the tool timeline.
*/
const KNOWN_TOOLKIT_RE =
/^(gmail|notion|github|slack|discord|linear|jira|google_calendar|google_drive|calendar)$/i;
export function inferIntegrationName(input?: string): string | undefined {
if (!input) return undefined;
const delegateMatch = input.match(/^delegate_(.+)$/);
if (delegateMatch && KNOWN_TOOLKIT_RE.test(delegateMatch[1])) {
return normalizeIntegrationName(delegateMatch[1]);
}
if (KNOWN_TOOLKIT_RE.test(input)) {
return normalizeIntegrationName(input);
}
return undefined;
}
function integrationActivityTitle(provider: string): string {
switch (provider) {
case 'GitHub':
case 'Gmail':
case 'Linear':
case 'Jira':
return `Making requests to your ${provider} account`;
case 'Notion':
return 'Working in your Notion workspace';
case 'Slack':
case 'Discord':
return `Working in your ${provider} workspace`;
case 'Google Calendar':
return 'Updating your Google Calendar';
case 'Google Drive':
return 'Working in your Google Drive';
default:
return `Checking your ${provider}`;
}
}
function inferIntegrationNameFromPrompt(prompt?: string): string | undefined {
if (!prompt) return undefined;
const known = [
'Notion',
'Gmail',
'GitHub',
'Slack',
'Discord',
'Linear',
'Jira',
'Google Calendar',
'Google Drive',
];
const lower = prompt.toLowerCase();
return known.find(name => lower.includes(name.toLowerCase()));
}
function parseToolArgs(argsBuffer?: string): ParsedToolArgs | null {
if (!argsBuffer) return null;
try {
const parsed = JSON.parse(argsBuffer) as ParsedToolArgs;
return parsed && typeof parsed === 'object' ? parsed : null;
} catch {
return null;
}
}
function normalizeIntegrationName(value: string): string {
switch (value.toLowerCase()) {
case 'github':
return 'GitHub';
case 'gmail':
return 'Gmail';
case 'google_calendar':
case 'calendar':
return 'Google Calendar';
case 'google_drive':
return 'Google Drive';
default:
return humanizeIdentifier(value);
}
}
function humanizeIdentifier(value: string): string {
return value
.replace(/^subagent:/, '')
.replace(/^delegate_/, '')
.replace(/_/g, ' ')
.replace(/\b\w/g, char => char.toUpperCase());
}