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:
parent
3bfd40b701
commit
78494f8622
4 changed files with 64 additions and 37 deletions
|
|
@ -256,8 +256,8 @@ func (ast *Assistant) Stream(ctx *context.Context, inputMessages []context.Messa
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the output of the agent node
|
// Create completion node to report final output
|
||||||
ast.traceAgentOutput(agentNode, createResponse, nextResponse, completionResponse)
|
ast.traceAgentCompletion(ctx, createResponse, nextResponse, completionResponse, finalResponse)
|
||||||
|
|
||||||
// Only close output and send stream_end if this is the root call (entry point)
|
// 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
|
// Nested calls (from MCP, hooks, etc.) should not close the output or send stream_end
|
||||||
|
|
|
||||||
|
|
@ -97,17 +97,6 @@ func (ast *Assistant) buildCompletionOptions(ctx *context.Context, createRespons
|
||||||
return nil, "", fmt.Errorf("failed to apply MCP tools: %w", err)
|
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
|
return options, mcpSamplesPrompt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,39 @@ func (ast *Assistant) traceLLMComplete(ctx *context.Context, completionResponse
|
||||||
trace.Complete(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
|
// 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) {
|
func (ast *Assistant) traceAgentOutput(agentNode types.Node, createResponse *context.HookCreateResponse, nextResponse interface{}, completionResponse *context.CompletionResponse) {
|
||||||
if agentNode == nil {
|
if agentNode == nil {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,16 @@ func init() {
|
||||||
Locale: "en",
|
Locale: "en",
|
||||||
Messages: map[string]any{
|
Messages: map[string]any{
|
||||||
// Assistant: agent.go Stream() function
|
// Assistant: agent.go Stream() function
|
||||||
"assistant.agent.stream.label": "{{name}}",
|
"assistant.agent.stream.label": "{{name}}",
|
||||||
"assistant.agent.stream.description": "{{name}} is processing the request",
|
"assistant.agent.stream.description": "{{name}} is processing the request",
|
||||||
"assistant.agent.stream.history": "Get Chat History",
|
"assistant.agent.stream.history": "Get Chat History",
|
||||||
"assistant.agent.stream.capabilities": "Get Connector Capabilities",
|
"assistant.agent.stream.capabilities": "Get Connector Capabilities",
|
||||||
"assistant.agent.stream.create_hook": "Call Create Hook",
|
"assistant.agent.stream.create_hook": "Call Create Hook",
|
||||||
"assistant.agent.stream.closing": "Closing output (root call)",
|
"assistant.agent.stream.closing": "Closing output (root call)",
|
||||||
"assistant.agent.stream.skipping": "Skipping output close (nested call)",
|
"assistant.agent.stream.skipping": "Skipping output close (nested call)",
|
||||||
"assistant.agent.stream.close_error": "Failed to close output",
|
"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: providers/openai/openai.go Stream() function
|
||||||
"llm.openai.stream.label": "LLM %s",
|
"llm.openai.stream.label": "LLM %s",
|
||||||
|
|
@ -100,14 +102,16 @@ func init() {
|
||||||
Locale: "zh-cn",
|
Locale: "zh-cn",
|
||||||
Messages: map[string]any{
|
Messages: map[string]any{
|
||||||
// Assistant: agent.go Stream() function
|
// Assistant: agent.go Stream() function
|
||||||
"assistant.agent.stream.label": "{{name}}",
|
"assistant.agent.stream.label": "{{name}}",
|
||||||
"assistant.agent.stream.description": "{{name}} 正在处理请求",
|
"assistant.agent.stream.description": "{{name}} 正在处理请求",
|
||||||
"assistant.agent.stream.history": "获取聊天历史",
|
"assistant.agent.stream.history": "获取聊天历史",
|
||||||
"assistant.agent.stream.capabilities": "获取连接器能力",
|
"assistant.agent.stream.capabilities": "获取连接器能力",
|
||||||
"assistant.agent.stream.create_hook": "调用 Create Hook",
|
"assistant.agent.stream.create_hook": "调用 Create Hook",
|
||||||
"assistant.agent.stream.closing": "关闭输出(根调用)",
|
"assistant.agent.stream.closing": "关闭输出(根调用)",
|
||||||
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
|
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
|
||||||
"assistant.agent.stream.close_error": "关闭输出失败",
|
"assistant.agent.stream.close_error": "关闭输出失败",
|
||||||
|
"assistant.agent.completion.label": "智能体完成",
|
||||||
|
"assistant.agent.completion.description": "智能体最终输出",
|
||||||
|
|
||||||
// LLM: providers/openai/openai.go Stream() function
|
// LLM: providers/openai/openai.go Stream() function
|
||||||
"llm.openai.stream.label": "LLM %s",
|
"llm.openai.stream.label": "LLM %s",
|
||||||
|
|
@ -160,14 +164,16 @@ func init() {
|
||||||
Locale: "zh",
|
Locale: "zh",
|
||||||
Messages: map[string]any{
|
Messages: map[string]any{
|
||||||
// Assistant: agent.go Stream() function
|
// Assistant: agent.go Stream() function
|
||||||
"assistant.agent.stream.label": "{{name}}",
|
"assistant.agent.stream.label": "{{name}}",
|
||||||
"assistant.agent.stream.description": "{{name}} 正在处理请求",
|
"assistant.agent.stream.description": "{{name}} 正在处理请求",
|
||||||
"assistant.agent.stream.history": "获取聊天历史",
|
"assistant.agent.stream.history": "获取聊天历史",
|
||||||
"assistant.agent.stream.capabilities": "获取连接器能力",
|
"assistant.agent.stream.capabilities": "获取连接器能力",
|
||||||
"assistant.agent.stream.create_hook": "调用 Create Hook",
|
"assistant.agent.stream.create_hook": "调用 Create Hook",
|
||||||
"assistant.agent.stream.closing": "关闭输出(根调用)",
|
"assistant.agent.stream.closing": "关闭输出(根调用)",
|
||||||
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
|
"assistant.agent.stream.skipping": "跳过输出关闭(嵌套调用)",
|
||||||
"assistant.agent.stream.close_error": "关闭输出失败",
|
"assistant.agent.stream.close_error": "关闭输出失败",
|
||||||
|
"assistant.agent.completion.label": "智能体完成",
|
||||||
|
"assistant.agent.completion.description": "智能体最终输出",
|
||||||
|
|
||||||
// LLM: providers/openai/openai.go Stream() function
|
// LLM: providers/openai/openai.go Stream() function
|
||||||
"llm.openai.stream.label": "LLM %s",
|
"llm.openai.stream.label": "LLM %s",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue