-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathtoolCallProcessor.ts
More file actions
726 lines (651 loc) · 23.5 KB
/
toolCallProcessor.ts
File metadata and controls
726 lines (651 loc) · 23.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import {
ChatMessage,
LLMAgentEvent,
MCPToolCall,
MCPToolDefinition,
MCPToolResponse,
ModelConfig
} from '@shared/presenter'
import fs from 'fs/promises'
import path from 'path'
import { isNonRetryableError } from './errorClassification'
import { resolveToolOffloadPath } from '../../sessionPresenter/sessionPaths'
import { parseQuestionToolArgs, QUESTION_TOOL_NAME } from '../tools/questionTool'
interface ToolCallProcessorOptions {
getAllToolDefinitions: (context: ToolCallExecutionContext) => Promise<MCPToolDefinition[]>
callTool: (request: MCPToolCall) => Promise<{ content: unknown; rawData: MCPToolResponse }>
preCheckToolPermission?: (request: MCPToolCall) => Promise<PermissionRequestPayload | null>
onToolCallFinished?: (info: {
toolName: string
toolCallId: string
toolServerName?: string
conversationId?: string
status: 'success' | 'error' | 'permission'
}) => void
}
interface ToolCallExecutionContext {
eventId: string
toolCalls: Array<{ id: string; name: string; arguments: string }>
enabledMcpTools?: string[]
conversationMessages: ChatMessage[]
modelConfig: ModelConfig
providerId?: string
abortSignal: AbortSignal
currentToolCallCount: number
maxToolCalls: number
conversationId?: string
}
interface ToolCallProcessResult {
toolCallCount: number
needContinueConversation: boolean
}
interface ToolCall {
id: string
name: string
arguments: string
}
type PermissionType = 'read' | 'write' | 'all' | 'command'
interface CommandInfoPayload {
command: string
riskLevel: 'low' | 'medium' | 'high' | 'critical'
suggestion: string
signature?: string
baseCommand?: string
}
interface PermissionRequestPayload {
needsPermission: true
toolName: string
serverName: string
permissionType: PermissionType
description: string
command?: string
commandSignature?: string
commandInfo?: CommandInfoPayload
paths?: string[]
providerId?: string
requestId?: string
sessionId?: string
agentId?: string
agentName?: string
conversationId?: string
rememberable?: boolean
[key: string]: unknown
}
interface PermissionRequestInfo {
toolCall: ToolCall
serverName: string
serverIcons?: string
serverDescription?: string
payload: PermissionRequestPayload
}
const TOOL_OUTPUT_OFFLOAD_THRESHOLD = 5000
const TOOL_OUTPUT_PREVIEW_LENGTH = 1024
const QUESTION_ERROR_KEY = 'common.error.invalidQuestionRequest'
// Tools that require offload when output exceeds threshold
// Tools not in this list will never trigger offload (e.g., read has its own pagination)
const TOOLS_REQUIRING_OFFLOAD = new Set(['exec', 'ls', 'find', 'grep', 'yo_browser_cdp_send'])
export class ToolCallProcessor {
constructor(private readonly options: ToolCallProcessorOptions) {}
async *process(
context: ToolCallExecutionContext
): AsyncGenerator<LLMAgentEvent, ToolCallProcessResult, void> {
let toolCallCount = context.currentToolCallCount
let needContinueConversation = context.toolCalls.length > 0
let toolDefinitions = await this.options.getAllToolDefinitions(context)
// Step 1: Pre-check all tool permissions in batch
// If any tool requires permission, we pause and request permission for all at once
const permissionCheckResult = await this.batchPreCheckPermissions(context, toolDefinitions)
if (permissionCheckResult.hasPendingPermissions) {
// Yield permission request event for all tools that need permission
for (const permissionRequest of permissionCheckResult.permissionRequests) {
const permissionPayload = {
...permissionRequest.payload,
toolName: permissionRequest.toolCall.name,
serverName: permissionRequest.serverName,
permissionType: permissionRequest.payload.permissionType,
description: permissionRequest.payload.description,
conversationId: permissionRequest.payload.conversationId ?? context.conversationId,
// Mark this as part of a batch
isBatchPermission: true,
totalInBatch: permissionCheckResult.permissionRequests.length
}
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'permission-required',
tool_call_id: permissionRequest.toolCall.id,
tool_call_name: permissionRequest.toolCall.name,
tool_call_params: permissionRequest.toolCall.arguments,
tool_call_server_name: permissionRequest.serverName,
tool_call_server_icons: permissionRequest.serverIcons,
tool_call_server_description: permissionRequest.serverDescription,
tool_call_response: permissionRequest.payload.description,
permission_request: permissionPayload
}
}
}
// Stop here and wait for user to grant permissions
// The loop will be restarted after permissions are granted
needContinueConversation = false
return {
toolCallCount,
needContinueConversation
}
}
const resolveToolDefinition = async (
toolName: string
): Promise<MCPToolDefinition | undefined> => {
const match = toolDefinitions.find((tool) => tool.function.name === toolName)
if (match) return match
toolDefinitions = await this.options.getAllToolDefinitions(context)
return toolDefinitions.find((tool) => tool.function.name === toolName)
}
for (const [index, toolCall] of context.toolCalls.entries()) {
if (context.abortSignal.aborted) break
if (toolCallCount >= context.maxToolCalls) {
console.warn('Max tool calls reached during execution phase for event:', context.eventId)
yield {
type: 'response',
data: {
eventId: context.eventId,
maximum_tool_calls_reached: true,
tool_call_id: toolCall.id,
tool_call_name: toolCall.name
}
}
needContinueConversation = false
break
}
toolCallCount++
const toolDef = await resolveToolDefinition(toolCall.name)
if (!toolDef) {
console.error(`Tool definition not found for ${toolCall.name}. Skipping execution.`)
const errorMsg = `Tool definition for ${toolCall.name} not found.`
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'error',
tool_call_id: toolCall.id,
tool_call_name: toolCall.name,
tool_call_response: errorMsg
}
}
context.conversationMessages.push({
role: 'user',
content: `Error: ${errorMsg}`
})
continue
}
const notifyToolCallFinished = (status: 'success' | 'error' | 'permission') => {
if (!this.options.onToolCallFinished) return
try {
this.options.onToolCallFinished({
toolName: toolCall.name,
toolCallId: toolCall.id,
toolServerName: toolDef.server?.name,
conversationId: context.conversationId,
status
})
} catch (error) {
console.warn('[ToolCallProcessor] onToolCallFinished handler failed:', error)
}
}
const mcpToolInput: MCPToolCall = {
id: toolCall.id,
type: 'function',
function: {
name: toolCall.name,
arguments: toolCall.arguments
},
server: toolDef.server,
conversationId: context.conversationId
}
if (toolCall.name === QUESTION_TOOL_NAME) {
const isStandalone = context.toolCalls.length === 1
const isLast = index === context.toolCalls.length - 1
if (!isStandalone || !isLast) {
notifyToolCallFinished('error')
this.appendToolError(
context.conversationMessages,
context.modelConfig,
toolCall,
'Question tool must be the only tool call in a turn.'
)
yield {
type: 'response',
data: {
eventId: context.eventId,
question_error: QUESTION_ERROR_KEY,
tool_call_id: toolCall.id,
tool_call_name: toolCall.name
}
}
continue
}
const parsedQuestion = parseQuestionToolArgs(toolCall.arguments || '')
if (!parsedQuestion.success) {
notifyToolCallFinished('error')
this.appendToolError(
context.conversationMessages,
context.modelConfig,
toolCall,
`Invalid question tool arguments: ${parsedQuestion.error}`
)
yield {
type: 'response',
data: {
eventId: context.eventId,
question_error: QUESTION_ERROR_KEY,
tool_call_id: toolCall.id,
tool_call_name: toolCall.name
}
}
continue
}
notifyToolCallFinished('success')
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'question-required',
tool_call_id: toolCall.id,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_server_name: toolDef.server.name,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description,
question_request: parsedQuestion.data
}
}
needContinueConversation = false
break
}
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'running',
tool_call_id: toolCall.id,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_server_name: toolDef.server.name,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description
}
}
try {
const toolResponse = await this.options.callTool(mcpToolInput)
const requiresPermission = Boolean(toolResponse.rawData?.requiresPermission)
if (requiresPermission) {
notifyToolCallFinished('permission')
console.log(
`[Agent Loop] Permission required for tool ${toolCall.name}, creating permission request`
)
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'permission-required',
tool_call_id: toolCall.id,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_server_name: toolResponse.rawData.permissionRequest?.serverName,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description,
tool_call_response: toolResponse.content,
permission_request: toolResponse.rawData.permissionRequest
}
}
needContinueConversation = false
break
}
notifyToolCallFinished('success')
if (context.abortSignal.aborted) break
const supportsFunctionCall = context.modelConfig?.functionCall || false
const toolContent = this.stringifyToolContent(toolResponse.content)
const toolContentForModel = await this.offloadToolContentIfNeeded(
toolContent,
toolCall.id,
context.conversationId,
toolCall.name
)
if (supportsFunctionCall) {
this.appendNativeFunctionCallMessages(context.conversationMessages, toolCall, {
content: toolContentForModel
})
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'end',
tool_call_id: toolCall.id,
tool_call_response: toolContentForModel,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_server_name: toolDef.server.name,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description,
tool_call_response_raw: toolResponse.rawData
}
}
} else {
this.appendLegacyFunctionCallMessages(context.conversationMessages, toolCall, {
content: toolContentForModel
})
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'end',
tool_call_id: toolCall.id,
tool_call_response: toolContentForModel,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_server_name: toolDef.server.name,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description,
tool_call_response_raw: toolResponse.rawData
}
}
}
} catch (toolError) {
notifyToolCallFinished('error')
if (context.abortSignal.aborted) break
console.error(
`Tool execution error for ${toolCall.name} (event ${context.eventId}):`,
toolError
)
const errorMessage = toolError instanceof Error ? toolError.message : String(toolError)
// Check if error is non-retryable (should stop the loop)
const errorForClassification: Error | string =
toolError instanceof Error ? toolError : String(toolError)
const isNonRetryable = isNonRetryableError(errorForClassification)
this.appendToolError(
context.conversationMessages,
context.modelConfig,
toolCall,
errorMessage
)
yield {
type: 'response',
data: {
eventId: context.eventId,
tool_call: 'error',
tool_call_id: toolCall.id,
tool_call_name: toolCall.name,
tool_call_params: toolCall.arguments,
tool_call_response: errorMessage,
tool_call_server_name: toolDef.server.name,
tool_call_server_icons: toolDef.server.icons,
tool_call_server_description: toolDef.server.description
}
}
// If error is non-retryable, stop the loop
// Otherwise, keep needContinueConversation = true (default) to let LLM decide
if (isNonRetryable) {
needContinueConversation = false
break
}
// For retryable errors, continue the loop (needContinueConversation remains true)
}
}
return {
toolCallCount,
needContinueConversation
}
}
private appendNativeFunctionCallMessages(
conversationMessages: ChatMessage[],
toolCall: { id: string; name: string; arguments: string },
toolResponse: { content: unknown }
): void {
const lastAssistantMsg = conversationMessages.findLast(
(message) => message.role === 'assistant'
)
if (lastAssistantMsg) {
if (!lastAssistantMsg.tool_calls) lastAssistantMsg.tool_calls = []
lastAssistantMsg.tool_calls.push({
function: {
arguments: toolCall.arguments,
name: toolCall.name
},
id: toolCall.id,
type: 'function'
})
} else {
conversationMessages.push({
role: 'assistant',
tool_calls: [
{
function: {
arguments: toolCall.arguments,
name: toolCall.name
},
id: toolCall.id,
type: 'function'
}
]
})
}
const toolContent = this.stringifyToolContent(toolResponse.content)
conversationMessages.push({
role: 'tool',
content: toolContent,
tool_call_id: toolCall.id
})
}
private appendLegacyFunctionCallMessages(
conversationMessages: ChatMessage[],
toolCall: { id: string; name: string; arguments: string },
toolResponse: { content: unknown; rawData?: unknown }
): void {
const formattedToolRecordText =
'<function_call>' +
JSON.stringify({
function_call_record: {
name: toolCall.name,
arguments: toolCall.arguments,
response: toolResponse.content
}
}) +
'</function_call>'
let lastAssistantMessage = conversationMessages.findLast(
(message) => message.role === 'assistant'
)
if (lastAssistantMessage) {
if (typeof lastAssistantMessage.content === 'string') {
lastAssistantMessage.content += formattedToolRecordText + '\n'
} else if (Array.isArray(lastAssistantMessage.content)) {
lastAssistantMessage.content.push({
type: 'text',
text: formattedToolRecordText + '\n'
})
} else {
lastAssistantMessage.content = [{ type: 'text', text: formattedToolRecordText + '\n' }]
}
} else {
conversationMessages.push({
role: 'assistant',
content: [{ type: 'text', text: formattedToolRecordText + '\n' }]
})
}
const userPromptText =
'以上是你刚执行的工具调用及其响应信息,已帮你插入,请仔细阅读工具响应,并继续你的回答。'
conversationMessages.push({
role: 'user',
content: [{ type: 'text', text: userPromptText }]
})
}
private async offloadToolContentIfNeeded(
content: string,
toolCallId: string,
conversationId?: string,
toolName?: string
): Promise<string> {
// Only offload tools in the whitelist
if (toolName && !TOOLS_REQUIRING_OFFLOAD.has(toolName)) {
return content
}
if (content.length <= TOOL_OUTPUT_OFFLOAD_THRESHOLD) return content
if (!conversationId) return content
const filePath = resolveToolOffloadPath(conversationId, toolCallId)
if (!filePath) return content
const sessionDir = path.dirname(filePath)
try {
await fs.mkdir(sessionDir, { recursive: true })
await fs.writeFile(filePath, content, 'utf-8')
} catch (error) {
console.warn('[ToolCallProcessor] Failed to offload tool output:', error)
return content
}
const preview = content.slice(0, TOOL_OUTPUT_PREVIEW_LENGTH)
return this.buildToolOutputStub(content.length, preview, filePath)
}
private buildToolOutputStub(totalLength: number, preview: string, filePath: string): string {
return [
'[Tool output offloaded]',
`Total characters: ${totalLength}`,
`Full output saved to: ${filePath}`,
`first ${preview.length} chars:`,
preview
].join('\n')
}
private appendToolError(
conversationMessages: ChatMessage[],
modelConfig: ModelConfig,
toolCall: { id: string; name: string; arguments: string },
errorMessage: string
): void {
if (modelConfig?.functionCall) {
// For native function-calling models, ensure every tool error is still paired
// with a preceding assistant message that declares the tool_call in tool_calls.
const toolCallEntry = {
id: toolCall.id,
type: 'function' as const,
function: {
name: toolCall.name,
arguments: toolCall.arguments
}
}
let lastAssistantMessage = conversationMessages.findLast(
(message) => message.role === 'assistant'
)
if (lastAssistantMessage) {
if (!lastAssistantMessage.tool_calls) {
lastAssistantMessage.tool_calls = []
}
lastAssistantMessage.tool_calls.push(toolCallEntry)
} else {
// Extremely defensive fallback – create a synthetic assistant message
// so the OpenAI API still sees a valid tool_calls declaration.
lastAssistantMessage = {
role: 'assistant',
tool_calls: [toolCallEntry]
}
conversationMessages.push(lastAssistantMessage)
}
conversationMessages.push({
role: 'tool',
content: `The tool call with ID ${toolCall.id} and name ${toolCall.name} failed to execute: ${errorMessage}`,
tool_call_id: toolCall.id
})
return
}
const formattedErrorText = `编号为 ${toolCall.id} 的工具 ${toolCall.name} 调用执行失败: ${errorMessage}`
let lastAssistantMessage = conversationMessages.findLast(
(message) => message.role === 'assistant'
)
if (lastAssistantMessage) {
if (typeof lastAssistantMessage.content === 'string') {
lastAssistantMessage.content += '\n' + formattedErrorText + '\n'
} else if (Array.isArray(lastAssistantMessage.content)) {
lastAssistantMessage.content.push({
type: 'text',
text: '\n' + formattedErrorText + '\n'
})
} else {
lastAssistantMessage.content = [{ type: 'text', text: '\n' + formattedErrorText + '\n' }]
}
} else {
conversationMessages.push({
role: 'assistant',
content: [{ type: 'text', text: formattedErrorText + '\n' }]
})
}
const userPromptText =
'以上是你刚调用的工具及其执行的错误信息,已帮你插入,请根据情况继续回答或重新尝试。'
conversationMessages.push({
role: 'user',
content: [{ type: 'text', text: userPromptText }]
})
}
private stringifyToolContent(content: unknown): string {
return typeof content === 'string' ? content : JSON.stringify(content)
}
/**
* Batch pre-check permissions for all tool calls
* Returns info about tools that need permission, or empty if all have permission
*/
private async batchPreCheckPermissions(
context: ToolCallExecutionContext,
toolDefinitions: MCPToolDefinition[]
): Promise<{
hasPendingPermissions: boolean
permissionRequests: PermissionRequestInfo[]
}> {
// If no permission pre-check function provided, skip batch check
if (!this.options.preCheckToolPermission) {
return { hasPendingPermissions: false, permissionRequests: [] }
}
const permissionRequests: PermissionRequestInfo[] = []
const toolNameToDefMap = new Map(toolDefinitions.map((t) => [t.function.name, t]))
for (const toolCall of context.toolCalls) {
const toolDef = toolNameToDefMap.get(toolCall.name)
if (!toolDef) continue
// Skip question tool for permission check
if (toolCall.name === QUESTION_TOOL_NAME) continue
const mcpToolInput: MCPToolCall = {
id: toolCall.id,
type: 'function',
function: {
name: toolCall.name,
arguments: toolCall.arguments
},
server: toolDef.server,
conversationId: context.conversationId
}
try {
const permissionResult = await this.options.preCheckToolPermission(mcpToolInput)
if (permissionResult) {
const permissionPayload: PermissionRequestPayload = {
...permissionResult,
toolName: permissionResult.toolName || toolCall.name,
serverName: permissionResult.serverName || toolDef.server.name,
permissionType: permissionResult.permissionType,
description: permissionResult.description
}
// Preserve the full permission payload (paths and custom fields included)
permissionRequests.push({
toolCall,
serverName: permissionPayload.serverName,
serverIcons: toolDef.server?.icons,
serverDescription: toolDef.server?.description,
payload: permissionPayload
})
}
} catch (error) {
console.warn(
`[ToolCallProcessor] Failed to pre-check permission for ${toolCall.name}:`,
error
)
// If pre-check fails, we'll let the actual execution handle it
}
}
return {
hasPendingPermissions: permissionRequests.length > 0,
permissionRequests
}
}
}