-
Notifications
You must be signed in to change notification settings - Fork 16.5k
Expand file tree
/
Copy pathgroupToolUses.ts
More file actions
211 lines (190 loc) · 6.11 KB
/
Copy pathgroupToolUses.ts
File metadata and controls
211 lines (190 loc) · 6.11 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type { BetaToolUseBlock } from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs'
import type {
ContentBlockParam,
ToolResultBlockParam,
} from '@anthropic-ai/sdk/resources/messages/messages.mjs'
import type { Tools } from '../Tool.js'
import type {
GroupedToolUseMessage,
NormalizedAssistantMessage,
NormalizedMessage,
NormalizedUserMessage,
ProgressMessage,
RenderableMessage,
} from '../types/message.js'
export type MessageWithoutProgress = Exclude<NormalizedMessage, ProgressMessage>
export type GroupingResult = {
messages: RenderableMessage[]
}
// Cache the set of tool names that support grouped rendering, keyed by the
// tools array reference. The tools array is stable across renders (only
// replaced on MCP connect/disconnect), so this avoids rebuilding the set on
// every call. WeakMap lets old entries be GC'd when the array is replaced.
const GROUPING_CACHE = new WeakMap<Tools, Set<string>>()
function getToolsWithGrouping(tools: Tools): Set<string> {
let cached = GROUPING_CACHE.get(tools)
if (!cached) {
cached = new Set(tools.reduce((acc: string[], t) => {
if (t.renderGroupedToolUse) acc.push(t.name);
return acc;
}, []))
GROUPING_CACHE.set(tools, cached)
}
return cached
}
function getToolUseInfo(
msg: MessageWithoutProgress,
): { messageId: string; toolUseId: string; toolName: string } | null {
if (
msg.type === 'assistant' &&
msg.message?.content &&
Array.isArray(msg.message.content) &&
(msg.message.content[0] as { type?: string })?.type === 'tool_use'
) {
const content = msg.message.content[0] as unknown as {
type: 'tool_use'
id: string
name: string
[key: string]: unknown
}
return {
messageId: msg.message.id as string,
toolUseId: content.id,
toolName: content.name,
}
}
return null
}
/**
* Groups tool uses by message.id (same API response) if the tool supports grouped rendering.
* Only groups 2+ tools of the same type from the same message.
* Also collects corresponding tool_results and attaches them to the grouped message.
* When verbose is true, skips grouping so messages render at original positions.
*/
export function applyGrouping(
messages: MessageWithoutProgress[],
tools: Tools,
verbose: boolean = false,
): GroupingResult {
// In verbose mode, don't group - each message renders at its original position
if (verbose) {
return {
messages: messages as RenderableMessage[],
}
}
const toolsWithGrouping = getToolsWithGrouping(tools)
// First pass: group tool uses by message.id + tool name
const groups = new Map<
string,
NormalizedAssistantMessage<BetaToolUseBlock>[]
>()
for (const msg of messages) {
const info = getToolUseInfo(msg)
if (info && toolsWithGrouping.has(info.toolName)) {
const key = `${info.messageId}:${info.toolName}`
const group = groups.get(key) ?? []
group.push(msg as NormalizedAssistantMessage<BetaToolUseBlock>)
groups.set(key, group)
}
}
// Identify valid groups (2+ items) and collect their tool use IDs
const validGroups = new Map<
string,
NormalizedAssistantMessage<BetaToolUseBlock>[]
>()
const groupedToolUseIds = new Set<string>()
for (const [key, group] of groups) {
if (group.length >= 2) {
validGroups.set(key, group)
for (const msg of group) {
const info = getToolUseInfo(msg)
if (info) {
groupedToolUseIds.add(info.toolUseId)
}
}
}
}
// Collect result messages for grouped tool_uses
// Map from tool_use_id to the user message containing that result
const resultsByToolUseId = new Map<string, NormalizedUserMessage>()
for (const msg of messages) {
if (
msg.type === 'user' &&
msg.message?.content &&
Array.isArray(msg.message.content)
) {
for (const content of msg.message.content) {
if (
(content as { type?: string }).type === 'tool_result' &&
groupedToolUseIds.has(
(content as { tool_use_id: string }).tool_use_id,
)
) {
resultsByToolUseId.set(
(content as { tool_use_id: string }).tool_use_id,
msg as NormalizedUserMessage,
)
}
}
}
}
// Second pass: build output, emitting each group only once
const result: RenderableMessage[] = []
const emittedGroups = new Set<string>()
for (const msg of messages) {
const info = getToolUseInfo(msg)
if (info) {
const key = `${info.messageId}:${info.toolName}`
const group = validGroups.get(key)
if (group) {
if (!emittedGroups.has(key)) {
emittedGroups.add(key)
const firstMsg = group[0]!
// Collect results for this group
const results: NormalizedUserMessage[] = []
for (const assistantMsg of group) {
const toolUseId = (
assistantMsg.message!.content![0] as { id: string }
).id
const resultMsg = resultsByToolUseId.get(toolUseId)
if (resultMsg) {
results.push(resultMsg)
}
}
const groupedMessage: GroupedToolUseMessage = {
type: 'grouped_tool_use',
toolName: info.toolName,
messages: group,
results,
displayMessage: firstMsg,
uuid: `grouped-${firstMsg.uuid}`,
timestamp: firstMsg.timestamp,
messageId: info.messageId,
}
result.push(groupedMessage)
}
continue
}
}
// Skip user messages whose tool_results are all grouped
if (
msg.type === 'user' &&
msg.message?.content &&
Array.isArray(msg.message.content)
) {
const toolResults = (
msg.message.content as Array<ContentBlockParam>
).filter((c): c is ToolResultBlockParam => c.type === 'tool_result')
if (toolResults.length > 0) {
const allGrouped = toolResults.every(tr =>
groupedToolUseIds.has(tr.tool_use_id),
)
if (allGrouped) {
continue
}
}
}
result.push(msg as RenderableMessage)
}
return { messages: result }
}