package loop import ( "context" "io" "net/http" "net/http/httptest" "strings" "sync" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/config" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model" ) // anthropicToolRoundSSE is a native Anthropic stream that thinks, then emits a // tool_use block with an empty text part — the exact shape that starved the old // loop (which only looked for a JSON envelope in Content). const anthropicToolRoundSSE = `data: {"type":"message_start","message":{"model":"m","usage":{"input_tokens":10}}} data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}} data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"checking the clock"}} data: {"type":"content_block_stop","index":0} data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_1","name":"datetime","input":{}}} data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"timezone\":\"Asia/Shanghai\"}"}} data: {"type":"content_block_stop","index":1} data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":20}} data: {"type":"message_stop"} ` // anthropicTextRoundSSE is the follow-up stream: a plain text answer after the // tool result was fed back. const anthropicTextRoundSSE = `data: {"type":"message_start","message":{"model":"m","usage":{"input_tokens":30}}} data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"现在是 10:34。"}} data: {"type":"content_block_stop","index":0} data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":8}} data: {"type":"message_stop"} ` // TestRun_NativeToolCall_AnthropicAdapter_HTTP exercises the real loop path end // to end through the real AnthropicChatAdapter against a stub /v1/messages // server: round 1 streams a thinking + tool_use block with no text, the loop // must execute it and re-ask, and round 2 must carry the assistant tool_use turn // plus a correlated tool_result and return the model's text (not an empty reply). func TestRun_NativeToolCall_AnthropicAdapter_HTTP(t *testing.T) { t.Parallel() var mu sync.Mutex round := 0 var secondBody string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(r.Body) mu.Lock() round++ n := round if n == 2 { secondBody = string(body) } mu.Unlock() w.Header().Set("Content-Type", "text/event-stream") if n == 1 { _, _ = io.WriteString(w, anthropicToolRoundSSE) return } _, _ = io.WriteString(w, anthropicTextRoundSSE) })) t.Cleanup(srv.Close) ad := model.NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "m"}) td := dispatcherWith(t, &mockTool{name: "datetime", result: map[string]any{"now": "2026-09-08 22:34:00"}}) ag, err := agent.New("你是昔涟。", td, nil, agent.WithModel(ad, nil)) if err != nil { t.Fatalf("agent.New: %v", err) } l := New(Options{Agent: ag, MaxRounds: 8, OnStream: func(model.StreamEvent) {}}) res, err := l.Run(context.Background(), "sess", []model.Message{model.TextMessage(model.RoleUser, "瞅瞅当前系统时间是多少")}) if err != nil { t.Fatalf("Run: %v", err) } if res.Text != "现在是 10:34。" { t.Fatalf("Text = %q, want the follow-up answer (empty means the native tool call was dropped)", res.Text) } if res.ToolCallsExecuted != 1 { t.Fatalf("ToolCallsExecuted = %d, want 1", res.ToolCallsExecuted) } if secondBody == "" { t.Fatal("adapter never made a second request (tool result was not fed back)") } for _, want := range []string{`"type":"tool_use"`, `"id":"toolu_1"`, `"type":"tool_result"`, `"tool_use_id":"toolu_1"`} { if !strings.Contains(secondBody, want) { t.Fatalf("second request missing %s:\n%s", want, secondBody) } } }