Refactor agent tracing and completion handling

- Updated the traceAgentOutput method to be deprecated in favor of the new traceAgentCompletion method, which creates a dedicated completion node for reporting final outputs.
- Enhanced the i18n messages to include labels and descriptions for the new agent completion functionality, improving localization support.
- Removed debug logging related to MCP tools from the buildCompletionOptions method to streamline the code and improve clarity.
This commit is contained in:
Max 2025-11-30 08:31:31 +08:00
parent 3bfd40b701
commit 78494f8622
4 changed files with 64 additions and 37 deletions

View file

@ -256,8 +256,8 @@ func (ast *Assistant) Stream(ctx *context.Context, inputMessages []context.Messa
})
}
// Set the output of the agent node
ast.traceAgentOutput(agentNode, createResponse, nextResponse, completionResponse)
// Create completion node to report final output
ast.traceAgentCompletion(ctx, createResponse, nextResponse, completionResponse, finalResponse)
// Only close output and send stream_end if this is the root call (entry point)
// Nested calls (from MCP, hooks, etc.) should not close the output or send stream_end

View file

@ -97,17 +97,6 @@ func (ast *Assistant) buildCompletionOptions(ctx *context.Context, createRespons
return nil, "", fmt.Errorf("failed to apply MCP tools: %w", err)
}
// === Debug MCP Tools ===
fmt.Println("--- Debug MCP Tools after applyMCPTools ---------------")
fmt.Printf("options.Tools count: %d\n", len(options.Tools))
if len(options.Tools) > 0 {
for i, tool := range options.Tools {
fmt.Printf("Tool %d: %+v\n", i, tool)
}
}
fmt.Println("-------------------------------------------------------")
// === End Debug ===
return options, mcpSamplesPrompt, nil
}

View file

@ -83,7 +83,39 @@ func (ast *Assistant) traceLLMComplete(ctx *context.Context, completionResponse
trace.Complete(completionResponse)
}
// traceAgentCompletion creates a completion node to report the final output
func (ast *Assistant) traceAgentCompletion(ctx *context.Context, createResponse *context.HookCreateResponse, nextResponse *context.NextHookResponse, completionResponse *context.CompletionResponse, finalResponse interface{}) {
trace, _ := ctx.Trace()
if trace == nil {
return
}
// Prepare the input data (the raw responses before processing)
input := map[string]interface{}{
"create": createResponse,
"next": nextResponse,
"completion": completionResponse,
}
// Create a dedicated completion node
completionNode, _ := trace.Add(
input,
types.TraceNodeOption{
Label: i18n.Tr(ast.ID, ctx.Locale, "assistant.agent.completion.label"), // "Agent Completion"
Type: "agent_completion",
Icon: "check_circle",
Description: i18n.Tr(ast.ID, ctx.Locale, "assistant.agent.completion.description"), // "Final output from assistant"
},
)
// Immediately mark it as complete with the final response
if completionNode != nil {
completionNode.Complete(finalResponse)
}
}
// traceAgentOutput sets the output of the agent trace node
// Deprecated: Use traceAgentCompletion instead for better trace structure
func (ast *Assistant) traceAgentOutput(agentNode types.Node, createResponse *context.HookCreateResponse, nextResponse interface{}, completionResponse *context.CompletionResponse) {
if agentNode == nil {
return

View file

@ -12,14 +12,16 @@ func init() {
Locale: "en",
Messages: map[string]any{
// Assistant: agent.go Stream() function
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} is processing the request",
"assistant.agent.stream.history": "Get Chat History",
"assistant.agent.stream.capabilities": "Get Connector Capabilities",
"assistant.agent.stream.create_hook": "Call Create Hook",
"assistant.agent.stream.closing": "Closing output (root call)",
"assistant.agent.stream.skipping": "Skipping output close (nested call)",
"assistant.agent.stream.close_error": "Failed to close output",
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} is processing the request",
"assistant.agent.stream.history": "Get Chat History",
"assistant.agent.stream.capabilities": "Get Connector Capabilities",
"assistant.agent.stream.create_hook": "Call Create Hook",
"assistant.agent.stream.closing": "Closing output (root call)",
"assistant.agent.stream.skipping": "Skipping output close (nested call)",
"assistant.agent.stream.close_error": "Failed to close output",
"assistant.agent.completion.label": "Agent Completion",
"assistant.agent.completion.description": "Final output from assistant",
// LLM: providers/openai/openai.go Stream() function
"llm.openai.stream.label": "LLM %s",
@ -100,14 +102,16 @@ func init() {
Locale: "zh-cn",
Messages: map[string]any{
// Assistant: agent.go Stream() function
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} 正在处理请求",
"assistant.agent.stream.history": "获取聊天历史",
"assistant.agent.stream.capabilities": "获取连接器能力",
"assistant.agent.stream.create_hook": "调用 Create Hook",
"assistant.agent.stream.closing": "关闭输出(根调用)",
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
"assistant.agent.stream.close_error": "关闭输出失败",
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} 正在处理请求",
"assistant.agent.stream.history": "获取聊天历史",
"assistant.agent.stream.capabilities": "获取连接器能力",
"assistant.agent.stream.create_hook": "调用 Create Hook",
"assistant.agent.stream.closing": "关闭输出(根调用)",
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
"assistant.agent.stream.close_error": "关闭输出失败",
"assistant.agent.completion.label": "智能体完成",
"assistant.agent.completion.description": "智能体最终输出",
// LLM: providers/openai/openai.go Stream() function
"llm.openai.stream.label": "LLM %s",
@ -160,14 +164,16 @@ func init() {
Locale: "zh",
Messages: map[string]any{
// Assistant: agent.go Stream() function
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} 正在处理请求",
"assistant.agent.stream.history": "获取聊天历史",
"assistant.agent.stream.capabilities": "获取连接器能力",
"assistant.agent.stream.create_hook": "调用 Create Hook",
"assistant.agent.stream.closing": "关闭输出(根调用)",
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
"assistant.agent.stream.close_error": "关闭输出失败",
"assistant.agent.stream.label": "{{name}}",
"assistant.agent.stream.description": "{{name}} 正在处理请求",
"assistant.agent.stream.history": "获取聊天历史",
"assistant.agent.stream.capabilities": "获取连接器能力",
"assistant.agent.stream.create_hook": "调用 Create Hook",
"assistant.agent.stream.closing": "关闭输出(根调用)",
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
"assistant.agent.stream.close_error": "关闭输出失败",
"assistant.agent.completion.label": "智能体完成",
"assistant.agent.completion.description": "智能体最终输出",
// LLM: providers/openai/openai.go Stream() function
"llm.openai.stream.label": "LLM %s",