package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"unicode/utf8"
"github.com/yetone/magpie/internal/provider"
)
// packinggoodsllc_07668 on Discord (#general, Claude Code v2.1.316 through
// the routing group "Hard to strong, easy to cheap"): the reply, word for
// word, as Claude Code showed it. The model wrote its calls as invokes in
// its text, with the results it made up after each, before anything ran.
const invokeReported = "\n" +
"select:mcp__magpie__magpie-browser__browser_navigate\n" +
"\n" +
"*Result: Tool loaded.*\n" +
"\n" +
"https://www.google.com\n" +
"\n" +
"*Result: Navigated to https://www.google.com — page title: \"Google\"*\n" +
"已用 magpie 的浏览器打开了谷歌 ..."
const invokeQuery = `{"query":"select:mcp__magpie__magpie-browser__browser_navigate"}`
// Claude Code's tools, the browser's deferred: ToolSearch loads it.
const invokeAnthropicTools = `"tools":[{"name":"ToolSearch","input_schema":{"type":"object"}},{"name":"Bash","input_schema":{"type":"object"}}]`
// cutInto is s in pieces of n bytes at most, cut on rune boundaries.
func cutInto(s string, n int) []string {
var out []string
for s != "" {
i := min(n, len(s))
for i < len(s) && !utf8.RuneStart(s[i]) {
i++
}
out = append(out, s[:i])
s = s[i:]
}
return out
}
// The reported reply is the ToolSearch call, and what the model made up
// after it goes: the browser call after it depends on a result that
// didn't exist yet, and names a tool not offered.
func TestInvokeReportedParsed(t *testing.T) {
names := map[string]bool{"ToolSearch": true, "Bash": true}
calls, rest, ok := parseTextCalls(invokeReported, names)
if !ok || len(calls) != 1 || calls[0].Name != "ToolSearch" || string(calls[0].Args) != invokeQuery || rest != "" {
t.Fatalf("calls %+v rest %q ok %v", calls, rest, ok)
}
// the browser offered too: still the first call only, the second
// came after a made-up result
names["mcp__magpie__magpie-browser__browser_navigate"] = true
calls, rest, ok = parseTextCalls(invokeReported, names)
if !ok || len(calls) != 1 || calls[0].Name != "ToolSearch" || rest != "" {
t.Fatalf("browser offered: calls %+v rest %q ok %v", calls, rest, ok)
}
// calls back to back are all made
two := "\nls\n\n" +
"\npwd\n"
calls, rest, ok = parseTextCalls(two, names)
if !ok || len(calls) != 2 || string(calls[1].Args) != `{"command":"pwd"}` || rest != "" {
t.Fatalf("two: calls %+v rest %q ok %v", calls, rest, ok)
}
// MiniMax's wrapper and Claude's
for _, w := range [][2]string{{"", ""}, {"", ""}} {
s := w[0] + "\n\nls -la\n\n" + w[1]
if at, _ := textCallAt("先看看。\n" + s); at != len("先看看。\n") {
t.Fatalf("%s: not seen (%d)", w[0], at)
}
calls, rest, ok = parseTextCalls(s, names)
if !ok || len(calls) != 1 || string(calls[0].Args) != `{"command":"ls -la"}` || rest != "" {
t.Fatalf("%s: calls %+v rest %q ok %v", w[0], calls, rest, ok)
}
}
// an invoke of a tool not offered stays text
if _, _, ok := parseTextCalls("\n/\n", names); ok {
t.Fatal("unoffered tool read as a call")
}
}
// Through a Chat relay, streamed in pieces: the call, and none of the
// made-up text.
func TestInvokeReportedRelayed(t *testing.T) {
var up []string
for _, p := range cutInto(invokeReported, 7) {
up = append(up, chunkOf(p))
}
up = append(up, stopChunk)
tools := `"tools":[{"type":"function","function":{"name":"ToolSearch","parameters":{"type":"object"}}}]`
text, calls, finish, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"帮我使用magpie的browser打开谷歌"}],`+tools+`}`, up...)
if text != "" || calls["ToolSearch"] != invokeQuery || len(calls) != 1 || finish != "tool_calls" || strings.Contains(raw, "Result") || strings.Contains(raw, "invoke") {
t.Fatalf("text %q calls %v finish %q\n%s", text, calls, finish, raw)
}
}
// anthropicSSE is a Messages stream whose one text block says text, in
// pieces.
func anthropicSSE(text string) string {
ev := func(typ string, v any) string {
b, _ := json.Marshal(v)
return "event: " + typ + "\ndata: " + string(b)
}
lines := []string{
ev("message_start", map[string]any{"type": "message_start", "message": map[string]any{"id": "msg_1", "type": "message", "role": "assistant", "model": "m", "content": []any{}, "usage": map[string]any{"input_tokens": 9, "output_tokens": 1}}}),
ev("content_block_start", map[string]any{"type": "content_block_start", "index": 0, "content_block": map[string]any{"type": "text", "text": ""}}),
}
for _, p := range cutInto(text, 9) {
lines = append(lines, ev("content_block_delta", map[string]any{"type": "content_block_delta", "index": 0, "delta": map[string]any{"type": "text_delta", "text": p}}))
}
lines = append(lines,
ev("content_block_stop", map[string]any{"type": "content_block_stop", "index": 0}),
ev("message_delta", map[string]any{"type": "message_delta", "delta": map[string]any{"stop_reason": "end_turn"}, "usage": map[string]any{"output_tokens": 40}}),
ev("message_stop", map[string]any{"type": "message_stop"}))
return sse(lines...)
}
// anthropicRead is what a Messages client reads of a stream: the text,
// each tool_use's name and input, and the stop reason. Blocks must open
// and close in order, each index once.
func anthropicRead(t *testing.T, raw string) (text string, names []string, inputs []string, stop string) {
t.Helper()
open, next := -1, 0
for _, line := range strings.Split(raw, "\n") {
data, ok := strings.CutPrefix(line, "data: ")
if !ok {
continue
}
var ev struct {
Type string `json:"type"`
Index int `json:"index"`
ContentBlock struct {
Type string `json:"type"`
Name string `json:"name"`
} `json:"content_block"`
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
PartialJSON string `json:"partial_json"`
StopReason string `json:"stop_reason"`
} `json:"delta"`
}
if err := json.Unmarshal([]byte(data), &ev); err != nil {
t.Fatalf("unreadable event %q: %v", data, err)
}
switch ev.Type {
case "content_block_start":
if open >= 0 || ev.Index != next {
t.Fatalf("block %d opened (open %d, next %d)\n%s", ev.Index, open, next, raw)
}
open, next = ev.Index, next+1
if ev.ContentBlock.Type == "tool_use" {
names, inputs = append(names, ev.ContentBlock.Name), append(inputs, "")
}
case "content_block_delta":
if ev.Index != open {
t.Fatalf("delta for %d, %d open\n%s", ev.Index, open, raw)
}
text += ev.Delta.Text
if ev.Delta.Type == "input_json_delta" {
inputs[len(inputs)-1] += ev.Delta.PartialJSON
}
case "content_block_stop":
if ev.Index != open {
t.Fatalf("stop for %d, %d open\n%s", ev.Index, open, raw)
}
open = -1
case "message_delta":
stop = ev.Delta.StopReason
}
}
return text, names, inputs, stop
}
// The reported case on its own path: Claude Code's Messages request to a
// routing group whose model speaks Messages itself, relayed as it came.
// It reaches Claude Code as the ToolSearch call, and the turn stops for
// it; a reply that came whole the same.
func TestInvokeReportedAnthropicPassthrough(t *testing.T) {
fresh(t)
a := &scripted{}
scriptedOn(t, "a", provider.Anthropic, a)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
ask := func(stream bool) string {
rec := httptest.NewRecorder()
s := "false"
if stream {
s = "true"
}
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(
`{"model":"group/g","max_tokens":100,"stream":`+s+`,"messages":[{"role":"user","content":"帮我使用magpie的browser打开谷歌"}],`+invokeAnthropicTools+`}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
return rec.Body.String()
}
a.replies, a.n = []reply{{200, "text/event-stream", anthropicSSE(invokeReported)}}, 0
raw := ask(true)
text, names, inputs, stop := anthropicRead(t, raw)
if text != "" || len(names) != 1 || names[0] != "ToolSearch" || inputs[0] != invokeQuery || stop != "tool_use" || strings.Contains(raw, "Result") {
t.Fatalf("text %q names %v inputs %v stop %q\n%s", text, names, inputs, stop, raw)
}
// text before the call stays, in its block, and the call follows it
a.replies, a.n = []reply{{200, "text/event-stream", anthropicSSE("我来打开谷歌。\n\n" + invokeReported)}}, 0
raw = ask(true)
text, names, inputs, stop = anthropicRead(t, raw)
if text != "我来打开谷歌。\n\n" || len(names) != 1 || inputs[0] != invokeQuery || stop != "tool_use" {
t.Fatalf("lead: text %q names %v inputs %v stop %q\n%s", text, names, inputs, stop, raw)
}
// text that only names an invoke of no offered tool goes as it came
plain := "Write it as /, then stop."
a.replies, a.n = []reply{{200, "text/event-stream", anthropicSSE(plain)}}, 0
raw = ask(true)
text, names, _, stop = anthropicRead(t, raw)
if text != plain || len(names) != 0 || stop != "end_turn" {
t.Fatalf("plain: text %q names %v stop %q\n%s", text, names, stop, raw)
}
whole, _ := json.Marshal(map[string]any{"id": "msg_1", "type": "message", "role": "assistant", "model": "m",
"content": []any{map[string]any{"type": "text", "text": "好的。\n" + invokeReported}},
"stop_reason": "end_turn", "usage": map[string]any{"input_tokens": 9, "output_tokens": 40}})
a.replies, a.n = []reply{{200, "application/json", string(whole)}}, 0
raw = ask(false)
var got struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
Name string `json:"name"`
ID string `json:"id"`
Input json.RawMessage `json:"input"`
} `json:"content"`
StopReason string `json:"stop_reason"`
}
if err := json.Unmarshal([]byte(raw), &got); err != nil {
t.Fatalf("%v: %s", err, raw)
}
if len(got.Content) != 2 || got.Content[0].Text != "好的。" || got.Content[1].Type != "tool_use" || got.Content[1].Name != "ToolSearch" ||
string(got.Content[1].Input) != invokeQuery || got.Content[1].ID == "" || got.StopReason != "tool_use" {
t.Fatalf("whole: %s", raw)
}
}
// responsesSSE is a Responses stream whose one message item's output_text
// says text, in pieces, as OpenAI's own send one.
func responsesSSE(text string) string {
ev := func(typ string, v any) string {
b, _ := json.Marshal(v)
return "event: " + typ + "\ndata: " + string(b)
}
part := map[string]any{"type": "output_text", "text": text, "annotations": []any{}}
lines := []string{
ev("response.created", map[string]any{"type": "response.created", "response": map[string]any{"id": "r1", "model": "m"}}),
ev("response.output_item.added", map[string]any{"type": "response.output_item.added", "output_index": 0,
"item": map[string]any{"type": "message", "role": "assistant", "id": "msg_1", "status": "in_progress", "content": []any{}}}),
ev("response.content_part.added", map[string]any{"type": "response.content_part.added", "item_id": "msg_1",
"output_index": 0, "content_index": 0, "part": map[string]any{"type": "output_text", "text": "", "annotations": []any{}}}),
}
for i, p := range cutInto(text, 9) {
lines = append(lines, ev("response.output_text.delta", map[string]any{"type": "response.output_text.delta",
"item_id": "msg_1", "output_index": 0, "content_index": 0, "delta": p, "sequence_number": i}))
}
lines = append(lines,
ev("response.output_text.done", map[string]any{"type": "response.output_text.done", "item_id": "msg_1",
"output_index": 0, "content_index": 0, "text": text}),
ev("response.content_part.done", map[string]any{"type": "response.content_part.done", "item_id": "msg_1",
"output_index": 0, "content_index": 0, "part": part}),
ev("response.output_item.done", map[string]any{"type": "response.output_item.done", "output_index": 0,
"item": map[string]any{"type": "message", "role": "assistant", "id": "msg_1", "status": "completed", "content": []any{part}}}),
ev("response.completed", map[string]any{"type": "response.completed",
"response": map[string]any{"id": "r1", "model": "m", "status": "completed", "output": []any{
map[string]any{"type": "message", "role": "assistant", "id": "msg_1", "status": "completed", "content": []any{part}}}}}))
return sse(lines...)
}
// responsesRead is what a Responses client reads of a stream: the text the
// deltas carried, each function_call item's name and arguments as its
// output_item.done said them, and the items the completed reply's output
// named.
func responsesStreamRead(t *testing.T, raw string) (text string, names, args []string, done, output []map[string]any) {
t.Helper()
for _, e := range events(raw) {
switch e["type"] {
case "response.output_text.delta":
text += e["delta"].(string)
case "response.output_item.done":
it := e["item"].(map[string]any)
done = append(done, it)
if it["type"] == "function_call" {
names = append(names, it["name"].(string))
args = append(args, it["arguments"].(string))
}
case "response.completed":
out, _ := e["response"].(map[string]any)["output"].([]any)
for _, it := range out {
output = append(output, it.(map[string]any))
}
}
}
return text, names, args, done, output
}
// The reported case on the Responses path: Codex to a provider that speaks
// Responses itself, relayed as it came. It reaches Codex as the ToolSearch
// call, and the turn stops for it; the completed reply's output says so
// too. A reply that came whole the same.
func TestInvokeReportedResponsesPassthrough(t *testing.T) {
fresh(t)
a := &scripted{}
scriptedOn(t, "a", provider.Responses, a)
ask := func(stream bool) string {
rec := httptest.NewRecorder()
s := "false"
if stream {
s = "true"
}
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(
`{"model":"a/m","stream":`+s+`,"store":false,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"帮我使用magpie的browser打开谷歌"}]}],
"tools":[{"type":"function","name":"ToolSearch","parameters":{"type":"object"}},{"type":"function","name":"Bash","parameters":{"type":"object"}}]}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
return rec.Body.String()
}
a.replies, a.n = []reply{{200, "text/event-stream", responsesSSE(invokeReported)}}, 0
raw := ask(true)
text, names, args, done, output := responsesStreamRead(t, raw)
if text != "" || len(names) != 1 || names[0] != "ToolSearch" || args[0] != invokeQuery ||
strings.Contains(raw, "Result") || strings.Contains(raw, "invoke") {
t.Fatalf("text %q names %v args %v\n%s", text, names, args, raw)
}
if len(output) != 2 || output[0]["type"] != "message" {
t.Fatalf("completed's output %v", output)
}
// the completed output's call is the one the stream ended with
last, _ := json.Marshal(done[len(done)-1])
matched, _ := json.Marshal(output[1])
if string(last) != string(matched) {
t.Fatalf("completed's output[1] %s, the stream's item %s", matched, last)
}
// text before the call stays, and the call follows it
a.replies, a.n = []reply{{200, "text/event-stream", responsesSSE("我来打开谷歌。\n\n" + invokeReported)}}, 0
raw = ask(true)
text, names, args, _, _ = responsesStreamRead(t, raw)
if text != "我来打开谷歌。\n\n" || len(names) != 1 || args[0] != invokeQuery {
t.Fatalf("lead: text %q names %v args %v\n%s", text, names, args, raw)
}
// text that only names an invoke of no offered tool goes as it came
plain := "Write it as /, then stop."
a.replies, a.n = []reply{{200, "text/event-stream", responsesSSE(plain)}}, 0
raw = ask(true)
text, names, _, _, _ = responsesStreamRead(t, raw)
if text != plain || len(names) != 0 {
t.Fatalf("plain: text %q names %v\n%s", text, names, raw)
}
whole, _ := json.Marshal(map[string]any{"id": "r1", "object": "response", "model": "m", "status": "completed",
"output": []any{map[string]any{"type": "message", "role": "assistant", "id": "msg_1", "status": "completed",
"content": []any{map[string]any{"type": "output_text", "text": "好的。\n" + invokeReported}}}}})
a.replies, a.n = []reply{{200, "application/json", string(whole)}}, 0
raw = ask(false)
var got struct {
Output []struct {
Type string `json:"type"`
Name string `json:"name"`
Args string `json:"arguments"`
} `json:"output"`
}
if err := json.Unmarshal([]byte(raw), &got); err != nil {
t.Fatalf("%v: %s", err, raw)
}
if len(got.Output) != 2 || got.Output[0].Type != "message" || got.Output[1].Type != "function_call" ||
got.Output[1].Name != "ToolSearch" || got.Output[1].Args != invokeQuery {
t.Fatalf("whole: %s", raw)
}
}
// geminiTextChunk is one chunk of a Gemini stream: the model's parts, and
// finish for the last one's finishReason.
func geminiTextChunk(parts []map[string]any, finish string) string {
cand := map[string]any{"content": map[string]any{"role": "model", "parts": parts}}
if finish != "" {
cand["finishReason"] = "STOP"
}
c := map[string]any{"candidates": []any{cand}}
if finish != "" {
c["usageMetadata"] = map[string]any{"promptTokenCount": 9, "candidatesTokenCount": 40}
}
b, _ := json.Marshal(c)
return "data: " + string(b)
}
// geminiSSE is a Gemini stream whose answer parts say text, in pieces.
func geminiSSE(text string) string {
var chunks []string
for _, p := range cutInto(text, 9) {
b, _ := json.Marshal(p)
chunks = append(chunks, geminiTextChunk([]map[string]any{{"text": json.RawMessage(b)}}, ""))
}
chunks = append(chunks, geminiTextChunk([]map[string]any{{"text": ""}}, "STOP"))
return sse(chunks...)
}
// geminiRead is what a Gemini client reads of a stream: the answer text
// and each functionCall's name and args.
func geminiRead(t *testing.T, raw string) (text string, names []string, args []json.RawMessage) {
t.Helper()
for _, line := range strings.Split(raw, "\n") {
data, ok := strings.CutPrefix(line, "data: ")
if !ok {
continue
}
var ch struct {
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text"`
Thought bool `json:"thought"`
Call struct {
Name string `json:"name"`
Args json.RawMessage `json:"args"`
} `json:"functionCall"`
} `json:"parts"`
} `json:"content"`
} `json:"candidates"`
}
if err := json.Unmarshal([]byte(data), &ch); err != nil {
t.Fatalf("unreadable chunk %q: %v", data, err)
}
for _, c := range ch.Candidates {
for _, p := range c.Content.Parts {
if !p.Thought {
text += p.Text
}
if p.Call.Name != "" {
names = append(names, p.Call.Name)
args = append(args, p.Call.Args)
}
}
}
}
return text, names, args
}
// The reported case on the Gemini path: Gemini CLI to a custom provider's
// Gemini URL, relayed as it came, and Vertex AI's whole generateContent
// reply the same.
func TestInvokeReportedGeminiPassthrough(t *testing.T) {
fresh(t)
geminiTools := `"tools":[{"functionDeclarations":[{"name":"ToolSearch","parameters":{"type":"object"}},{"name":"Bash","parameters":{"type":"object"}}]}]`
ask := func(stream bool) string {
rec := httptest.NewRecorder()
path := "/v1beta/models/a/m:generateContent"
if stream {
path = "/v1beta/models/a/m:streamGenerateContent?alt=sse"
}
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(
`{"contents":[{"role":"user","parts":[{"text":"帮我使用magpie的browser打开谷歌"}]}],`+geminiTools+`}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
return rec.Body.String()
}
stream := func(sse string) string {
srv := &scripted{replies: []reply{{200, "text/event-stream", sse}}}
upSSE := httptest.NewServer(srv)
t.Cleanup(upSSE.Close)
if err := provider.Save(provider.Provider{ID: "a", Name: "A", Key: "k", Models: []string{"m"}, Gemini: upSSE.URL + "/v1beta"}); err != nil {
t.Fatal(err)
}
return ask(true)
}
raw := stream(geminiSSE(invokeReported))
text, names, args := geminiRead(t, raw)
var texts string
if text != "" || len(names) != 1 || names[0] != "ToolSearch" || string(args[0]) != invokeQuery ||
strings.Contains(raw, "Result") || strings.Contains(raw, "invoke") {
t.Fatalf("text %q names %v args %v\n%s", text, names, args, raw)
}
// text before the call stays, and the call follows it
raw = stream(geminiSSE("我来打开谷歌。\n\n" + invokeReported))
text, names, args = geminiRead(t, raw)
if text != "我来打开谷歌。\n\n" || len(names) != 1 || string(args[0]) != invokeQuery {
t.Fatalf("lead: text %q names %v args %v\n%s", text, names, args, raw)
}
// text that only names an invoke of no offered tool goes as it came
plain := "Write it as /, then stop."
raw = stream(geminiSSE(plain))
text, names, _ = geminiRead(t, raw)
if text != plain || len(names) != 0 {
t.Fatalf("plain: text %q names %v\n%s", text, names, raw)
}
// Vertex AI's whole generateContent reply
v := &vertexUp{reply: func(n int, body map[string]any) (int, string) {
cand := map[string]any{
"content": map[string]any{"role": "model", "parts": []any{map[string]any{"text": "好的。\n" + invokeReported}}},
"finishReason": "STOP",
}
b, _ := json.Marshal(map[string]any{"candidates": []any{cand}})
return 200, string(b)
}}
vs := vertexServe(t, v, vertexLevels)
rec := httptest.NewRecorder()
vs.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1beta/models/google-vertex/gemini-3.8-flash:generateContent", strings.NewReader(
`{"contents":[{"role":"user","parts":[{"text":"帮我使用magpie的browser打开谷歌"}]}],`+geminiTools+`}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
raw = rec.Body.String()
var gots struct {
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text"`
Call struct {
Name string `json:"name"`
Args json.RawMessage `json:"args"`
} `json:"functionCall"`
} `json:"parts"`
} `json:"content"`
} `json:"candidates"`
}
if err := json.Unmarshal([]byte(raw), &gots); err != nil {
t.Fatalf("%v: %s", err, raw)
}
names, args, texts = nil, nil, ""
for _, c := range gots.Candidates {
for _, p := range c.Content.Parts {
if p.Call.Name != "" {
names, args = append(names, p.Call.Name), append(args, p.Call.Args)
} else {
texts += p.Text
}
}
}
if texts != "好的。\n" || len(names) != 1 || names[0] != "ToolSearch" || string(args[0]) != invokeQuery {
t.Fatalf("whole: texts %q names %v args %v\n%s", texts, names, args, raw)
}
}
// Translated, the reported reply from a Chat model reaches a Messages
// client (Claude Code) as the call, streamed and whole.
func TestInvokeReportedTranslated(t *testing.T) {
fresh(t)
var up []string
for _, p := range cutInto(invokeReported, 5) {
up = append(up, chunkOf(p))
}
body := sse(append(up, stopChunk, `data: [DONE]`)...)
a := &scripted{}
scriptedOn(t, "a", provider.Chat, a)
for _, stream := range []bool{true, false} {
a.replies, a.n = []reply{{200, "text/event-stream", body}}, 0
s := "false"
if stream {
s = "true"
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(
`{"model":"a/m","max_tokens":100,"stream":`+s+`,"messages":[{"role":"user","content":"hi"}],`+invokeAnthropicTools+`}`)))
raw := rec.Body.String()
if rec.Code != 200 || strings.Contains(raw, "invoke") || strings.Contains(raw, "Result") || !strings.Contains(raw, `"stop_reason":"tool_use"`) || !strings.Contains(raw, `"name":"ToolSearch"`) {
t.Fatalf("stream=%v %d %s", stream, rec.Code, raw)
}
}
}
// A Responses stream that numbers every event, as OpenAI's does, still
// counts up after the text's calls are made items: the events made for
// them, and the done events held while the text was read, go after the
// last one sent, never back nor twice.
func TestResponsesTextCallsNumberOn(t *testing.T) {
var lines []string
for i, e := range events(responsesSSE("我来打开谷歌。\n\n" + invokeReported)) {
e["sequence_number"] = i
b, _ := json.Marshal(e)
lines = append(lines, "event: "+e["type"].(string)+"\ndata: "+string(b))
}
stream := sse(lines...)
tc := &responsesTextCalls{names: map[string]bool{"ToolSearch": true}, sse: true, seq: -1}
var out []byte
for _, p := range cutInto(stream, 37) {
out = append(out, tc.write([]byte(p))...)
}
out = append(out, tc.flush()...)
_, names, _, _, _ := responsesStreamRead(t, string(out))
if len(names) != 1 || names[0] != "ToolSearch" {
t.Fatalf("names %v\n%s", names, out)
}
evs := events(string(out))
for i, e := range evs {
n, ok := e["sequence_number"].(float64)
if !ok {
t.Fatalf("event %d (%v) has no sequence_number\n%s", i, e["type"], out)
}
if i > 0 && n <= evs[i-1]["sequence_number"].(float64) {
t.Fatalf("event %d (%v) numbered %v after %v\n%s", i, e["type"], n, evs[i-1]["sequence_number"], out)
}
}
}