feat: 工具调用全部异步,不再阻塞主会话
- 去掉8s超时同步执行,工具一律走 executeAsyncAndStore - LLM立即收到[后台执行中]提示,继续生成回复 - 工具结果在下一轮对话中返回 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -87,32 +87,16 @@ func (s *Synthesizer) Synthesize(ctx context.Context, params SynthesizeParams, e
|
|||||||
args = make(map[string]interface{})
|
args = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.emitToolProgress(eventCh, tc.Name, "started", 0, "正在执行 "+tc.Name)
|
||||||
s.emitToolProgress(eventCh, tc.Name, "started", 0, "正在执行 "+tc.Name)
|
s.emitToolProgress(eventCh, tc.Name, "started", 0, "正在执行 "+tc.Name)
|
||||||
|
|
||||||
toolCtx, cancel := context.WithTimeout(ctx, toolDeadline)
|
// 工具调用全部异步执行,不阻塞主会话
|
||||||
result, execErr := s.toolRegistry.Execute(toolCtx, tc.Name, args)
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
if execErr != nil {
|
|
||||||
logger.Printf("[synthesizer] 工具 %s 执行失败: %v", tc.Name, execErr)
|
|
||||||
}
|
|
||||||
if result == nil {
|
|
||||||
result = &plgSDK.ToolResult{ToolName: tc.Name, Success: false, Error: execErr.Error()}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Async fallback: if tool timed out, store for next turn
|
|
||||||
if toolCtx.Err() == context.DeadlineExceeded {
|
|
||||||
s.emitToolProgress(eventCh, tc.Name, "running", 0.5, tc.Name+" 执行时间较长,转入后台继续...")
|
|
||||||
go s.executeAsyncAndStore(tc, args, params.SessionID, eventCh)
|
go s.executeAsyncAndStore(tc, args, params.SessionID, eventCh)
|
||||||
result = &plgSDK.ToolResult{
|
result := &plgSDK.ToolResult{
|
||||||
ToolName: tc.Name,
|
ToolName: tc.Name,
|
||||||
Success: true,
|
Success: true,
|
||||||
Output: fmt.Sprintf("[后台执行中] %s 正在后台运行,结果将在下一轮对话中返回。你可以继续聊天。", tc.Name),
|
Output: fmt.Sprintf("[后台执行中] %s 正在后台运行,结果稍后返回。", tc.Name),
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
s.emitToolProgress(eventCh, tc.Name, "completed", 1.0, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
resultJSON, _ := json.Marshal(result)
|
resultJSON, _ := json.Marshal(result)
|
||||||
messages = append(messages, model.LLMMessage{
|
messages = append(messages, model.LLMMessage{
|
||||||
Role: model.RoleTool,
|
Role: model.RoleTool,
|
||||||
|
|||||||
Reference in New Issue
Block a user