Files
example-eino/legacy/round/round.go
T
zhaoyupeng.elqbandTRAE CLI 1512f0516f feat: add weather tracing and unify example system prompts
Move shared prompt constants into pkg/opt and use them across examples.
Add agent, model, and tool tracing to the weather example, with tests,
and update weather argument descriptions and parse-error logging.

Strip timestamp and speaker markers from final ADK voice transcripts,
keeping normal bracketed text and treating marker-only results as empty.

Co-authored-by: TRAE CLI <traecli@bytedance.com>
2026-11-19 10:44:56 +08:00

63 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package round
import (
"context"
"fmt"
"log"
"example.com/ueino/pkg/model"
"example.com/ueino/pkg/opt"
"example.com/ueino/pkg/tool"
)
// Client 只负责模型请求,不执行工具或保存记忆。
type Client interface {
ChatCompletion(context.Context, []model.ChatMessage, []model.ToolDefinition, func(string)) (*model.ChatMessage, error)
}
// Complete 完成当前一轮的模型和工具交互,仅返回最终回答。
func Complete(ctx context.Context, chat Client, messages []model.ChatMessage, tools []tool.Tool, onChunk func(string)) (string, error) {
var definitions []model.ToolDefinition
available := make(map[string]tool.Tool)
for _, t := range tools {
definition := t.Definition()
definitions = append(definitions, definition)
available[definition.Function.Name] = t
}
for step := 0; step < opt.MAX_TOOL_ROUND; step++ {
assistant, err := chat.ChatCompletion(ctx, messages, definitions, onChunk)
if err != nil {
return "", err
}
if len(assistant.ToolCalls) == 0 {
fmt.Println()
log.Printf("[DEBUG] 无需工具调用")
return assistant.Content.(string), nil
}
if step == opt.MAX_TOOL_ROUND-1 {
return "", fmt.Errorf("本轮已达到 %d 次模型请求上限", opt.MAX_TOOL_ROUND)
}
messages = append(messages, *assistant)
for _, call := range assistant.ToolCalls {
fmt.Println()
log.Printf("[DEBUG] 调用工具 %s,ID=%s,参数=%s", call.Function.Name, call.ID, call.Function.Arguments)
result := ""
if t, ok := available[call.Function.Name]; ok {
result, err = t.Execute(ctx, call.Function.Arguments)
if err != nil {
result = "工具执行失败: " + err.Error()
}
} else {
result = "工具执行失败: 未知工具 " + call.Function.Name
}
if err := ctx.Err(); err != nil {
return "", err
}
messages = append(messages, model.ChatMessage{Role: "tool", Content: result, ToolCallID: call.ID})
}
}
return "", fmt.Errorf("本轮已达到模型请求上限")
}