diff --git a/agent/assistant/agent.go b/agent/assistant/agent.go index e32b02e4..c6cdb31b 100644 --- a/agent/assistant/agent.go +++ b/agent/assistant/agent.go @@ -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 diff --git a/agent/assistant/build.go b/agent/assistant/build.go index 3a7fd700..d7feef05 100644 --- a/agent/assistant/build.go +++ b/agent/assistant/build.go @@ -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 } diff --git a/agent/assistant/trace.go b/agent/assistant/trace.go index 934f4972..074c11d5 100644 --- a/agent/assistant/trace.go +++ b/agent/assistant/trace.go @@ -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 diff --git a/agent/i18n/builtin.go b/agent/i18n/builtin.go index b5bf69bb..9e93770c 100644 --- a/agent/i18n/builtin.go +++ b/agent/i18n/builtin.go @@ -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",