Skip to content

Commit 0fc19d9

Browse files
authored
feat(chatengine): 兼容toolcall并发模式 (#352)
1 parent ea8d1c6 commit 0fc19d9

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/chat-engine/adapters/agui/utils.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,26 @@ export function createThinkingContent(
206206
* 创建 toolcall 类型的 AIMessageContent
207207
* @param toolCall 工具调用数据
208208
* @param status 状态
209-
* @param strategy 策略(可选,会根据 toolCallName 自动判断
209+
* @param strategy 策略(可选)
210210
* @returns toolcall 类型的 AIMessageContent
211+
*
212+
* type 格式: toolcall-${toolCallName}-${toolCallId}
213+
* - toolCallName: 用于查找对应的渲染组件
214+
* - toolCallId: 用于区分同名工具的不同调用实例,支持并行工具调用
211215
*/
212216
export function createToolCallContent(
213217
toolCall: any,
214218
status: 'pending' | 'streaming' | 'complete' = 'pending',
215-
strategy: 'append' | 'merge' = 'append',
219+
strategy?: 'append' | 'merge',
216220
): any {
217-
// 根据 toolCallName 生成唯一的 type
218-
const type = `toolcall-${toolCall.toolCallName}`;
219-
return createAIMessageContent(type, toolCall, status, strategy);
221+
// 根据 toolCallName 和 toolCallId 生成唯一的 type
222+
// 这样同名工具的不同调用实例会有不同的 type,支持并行工具调用场景
223+
const type = `toolcall-${toolCall.toolCallName}-${toolCall.toolCallId}`;
224+
225+
// 如果没有指定 strategy,默认使用 'append'
226+
const finalStrategy = strategy || 'append';
227+
228+
return createAIMessageContent(type, toolCall, status, finalStrategy);
220229
}
221230

222231
/**
@@ -402,7 +411,8 @@ export function convertReasoningMessages(reasoningMessages: any[]): any[] {
402411
result: toolResult,
403412
};
404413
reasoningData.push({
405-
type: `toolcall-${toolCall.function.name}`,
414+
// 使用 toolCallName-toolCallId 格式,与 createToolCallContent 保持一致
415+
type: `toolcall-${toolCall.function.name}-${toolCall.id}`,
406416
data: toolCallData,
407417
status: 'complete',
408418
});
@@ -467,7 +477,8 @@ export function processToolCalls(toolCalls: any[], toolCallMap: Map<string, any>
467477
};
468478

469479
return {
470-
type: `toolcall-${toolCall.function.name}` as const,
480+
// 使用 toolCallName-toolCallId 格式,支持并行工具调用
481+
type: `toolcall-${toolCall.function.name}-${toolCall.id}` as const,
471482
data: toolCallData,
472483
};
473484
});

0 commit comments

Comments
 (0)