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>
129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package internal
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cloudwego/eino/adk"
|
|
"github.com/cloudwego/eino/components/model"
|
|
"github.com/cloudwego/eino/components/tool"
|
|
"github.com/cloudwego/eino/compose"
|
|
"github.com/cloudwego/eino/schema"
|
|
)
|
|
|
|
type traceTestModel struct {
|
|
model.ToolCallingChatModel
|
|
calls int
|
|
err error
|
|
}
|
|
|
|
func (m *traceTestModel) WithTools([]*schema.ToolInfo) (model.ToolCallingChatModel, error) {
|
|
return m, nil
|
|
}
|
|
|
|
func (m *traceTestModel) Stream(context.Context, []*schema.Message, ...model.Option) (*schema.StreamReader[*schema.Message], error) {
|
|
if m.err != nil {
|
|
return nil, m.err
|
|
}
|
|
m.calls++
|
|
if m.calls == 1 {
|
|
return schema.StreamReaderFromArray([]*schema.Message{
|
|
schema.AssistantMessage("", []schema.ToolCall{{ID: "weather-1", Type: "function", Function: schema.FunctionCall{Name: "get_weather", Arguments: `{"location":"北京"}`}}}),
|
|
}), nil
|
|
}
|
|
return schema.StreamReaderFromArray([]*schema.Message{
|
|
schema.AssistantMessage("北京", nil), schema.AssistantMessage("晴。", nil),
|
|
}), nil
|
|
}
|
|
|
|
type traceTestWeather struct{}
|
|
|
|
func (*traceTestWeather) Info(context.Context) (*schema.ToolInfo, error) {
|
|
return &schema.ToolInfo{Name: "get_weather", Desc: "查询天气"}, nil
|
|
}
|
|
|
|
func (*traceTestWeather) InvokableRun(context.Context, string, ...tool.Option) (string, error) {
|
|
return `{"condition":"晴"}`, nil
|
|
}
|
|
|
|
func TestTraceHandler(t *testing.T) {
|
|
for _, fail := range []bool{false, true} {
|
|
name := "streaming weather"
|
|
if fail {
|
|
name = "model error"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
var logs bytes.Buffer
|
|
previous := log.Writer()
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(previous)
|
|
chat := &traceTestModel{}
|
|
if fail {
|
|
chat.err = errors.New("model unavailable")
|
|
}
|
|
agent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
|
|
Name: "weather_test", Model: chat, MaxIterations: 3,
|
|
ToolsConfig: adk.ToolsConfig{ToolsNodeConfig: compose.ToolsNodeConfig{Tools: []tool.BaseTool{&traceTestWeather{}}}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
trace := NewTraceHandler()
|
|
runner := adk.NewRunner(ctx, adk.RunnerConfig{Agent: agent, EnableStreaming: true})
|
|
iter := runner.Query(ctx, "北京天气", adk.WithCallbacks(trace))
|
|
var reply strings.Builder
|
|
var runErr error
|
|
for {
|
|
event, ok := iter.Next()
|
|
if !ok {
|
|
break
|
|
}
|
|
if event.Err != nil {
|
|
runErr = event.Err
|
|
}
|
|
if event.Output == nil || event.Output.MessageOutput == nil {
|
|
continue
|
|
}
|
|
output := event.Output.MessageOutput
|
|
if !output.IsStreaming {
|
|
continue
|
|
}
|
|
for {
|
|
chunk, err := output.MessageStream.Recv()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reply.WriteString(chunk.Content)
|
|
}
|
|
output.MessageStream.Close()
|
|
}
|
|
trace.Wait()
|
|
if fail {
|
|
if runErr == nil || !strings.Contains(logs.String(), "model unavailable") {
|
|
t.Fatalf("error=%v trace=%s", runErr, logs.String())
|
|
}
|
|
return
|
|
}
|
|
if runErr != nil || reply.String() != "北京晴。" {
|
|
t.Fatalf("reply=%q error=%v trace=%s", reply.String(), runErr, logs.String())
|
|
}
|
|
for _, want := range []string{"parent=0 Agent/weather_test", "parent=1 ChatModel/", "Tool/get_weather", `tool arguments={"location":"北京"}`, `tool output={"condition":"晴"}`, "model output chunks=2", "end span=1 Agent/weather_test", "duration="} {
|
|
if !strings.Contains(logs.String(), want) {
|
|
t.Errorf("missing %q in trace:\n%s", want, logs.String())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|