mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-29 05:12:44 +08:00
Copilot Chat can ignore assistant history sent as plain text content blocks, so flatten it to a string even for native OpenAI requests. Leave annotated blocks and native user content untouched, and keep payload overrides as final barrier.
255 lines
13 KiB
Go
255 lines
13 KiB
Go
package helps
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/auth/copilot"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
|
)
|
|
|
|
func TestCopilotTurnInitiator(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
body string
|
|
format translator.Format
|
|
want string
|
|
}{
|
|
{`{"contents":[{"role":"user","parts":[{"functionResponse":{"name":"lookup","response":{}}}]}]}`, translator.FormatGemini, "agent"},
|
|
{`{"contents":[{"role":"user","parts":[{"functionResponse":{}},{"text":"New user input"}]}]}`, translator.FormatGemini, "user"},
|
|
{`{"input":[{"type":"function_result","result":{}}]}`, translator.FormatInteractions, "agent"},
|
|
} {
|
|
if got := ParseCopilotTurnInput([]byte(tc.body), tc.format).Initiator; got != tc.want {
|
|
t.Errorf("%s: got %q, want %q", tc.format, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCopilotStreamedToolTurns(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
format translator.Format
|
|
event, continuation string
|
|
}{
|
|
{translator.FormatOpenAI, `{"choices":[{"delta":{"tool_calls":[{"id":"call-1"}]}}]}`, `{"messages":[{"role":"tool","tool_call_id":"call-1","content":"ok"}]}`},
|
|
{translator.FormatClaude, `{"type":"content_block_start","content_block":{"type":"tool_use","id":"call-1"}}`, `{"messages":[{"role":"user","content":[{"type":"tool_result","tool_use_id":"call-1"}]}]}`},
|
|
{translator.FormatOpenAIResponse, `{"type":"response.output_item.added","item":{"type":"function_call","call_id":"call-1"}}`, `{"input":[{"type":"function_call_output","call_id":"call-1"}]}`},
|
|
{translator.FormatOpenAIResponse, `{"type":"response.completed","response":{"id":"resp-1"}}`, `{"previous_response_id":"resp-1","input":[{"type":"function_call_output","call_id":"unknown"}]}`},
|
|
} {
|
|
t.Run(tc.format.String(), func(t *testing.T) {
|
|
links := NewCopilotTurnLinks()
|
|
opts := cliproxyexecutor.Options{Metadata: map[string]any{cliproxyexecutor.CanonicalSessionIDMetadataKey: "session", cliproxyexecutor.CallerScopeMetadataKey: "caller"}}
|
|
first := links.Resolve("account", CopilotTurnInput{Initiator: "user"}, opts, tc.format, ojson.Result{})
|
|
links.Observe(first, tc.format, ojson.Parse(tc.event))
|
|
replay := ojson.Parse(tc.continuation)
|
|
// Starting another user turn must neither reuse nor invalidate the first one.
|
|
next := links.Resolve("account", CopilotTurnInput{Initiator: "user"}, opts, tc.format, replay)
|
|
if first.session != next.session || first.interaction == next.interaction || first.task == next.task {
|
|
t.Fatal("user turns must share only the session ID")
|
|
}
|
|
if got := links.Resolve("account", CopilotTurnInput{Initiator: "agent"}, opts, tc.format, replay); got != first {
|
|
t.Fatal("continuation did not resolve its own originating turn")
|
|
}
|
|
if got := links.Resolve("another-account", CopilotTurnInput{Initiator: "agent"}, opts, tc.format, replay); got.session == first.session || got.interaction == first.interaction {
|
|
t.Fatal("account scopes collided")
|
|
}
|
|
links.Observe(next, tc.format, ojson.Parse(tc.event))
|
|
if got := links.Resolve("account", CopilotTurnInput{Initiator: "agent"}, opts, tc.format, replay); got == first || got == next {
|
|
t.Fatal("an ambiguous upstream ID was linked to the wrong turn")
|
|
}
|
|
opts.Metadata[cliproxyexecutor.CallerScopeMetadataKey] = "another-caller"
|
|
if got := links.Resolve("account", CopilotTurnInput{Initiator: "agent"}, opts, tc.format, replay); got.session == first.session || got.interaction == first.interaction {
|
|
t.Fatal("caller scopes collided")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCopilotClaudeImageRelayRequiresMatchingToolResult(t *testing.T) {
|
|
const result = `{"type":"tool_result","tool_use_id":"call-a","content":[{"type":"image"}]}`
|
|
const tool = `{"role":"tool","tool_call_id":"call-a","content":"ok"}`
|
|
var generated CopilotTurnInput
|
|
media := ojson.NewDocString(`{"messages":[{"role":"tool","tool_call_id":"call-a","content":[{"type":"image_url"}]}]}`)
|
|
generated.RelayChatToolMedia(media)
|
|
relay := media.Get("messages.1").Raw
|
|
links := NewCopilotTurnLinks()
|
|
opts := cliproxyexecutor.Options{}
|
|
first := links.Resolve("account", CopilotTurnInput{Initiator: "user"}, opts, translator.FormatOpenAI, ojson.Result{})
|
|
links.Observe(first, translator.FormatOpenAI, ojson.Parse(`{"choices":[{"message":{"tool_calls":[{"id":"call-a"},{"id":"call-b"}]}}]}`))
|
|
other := links.Resolve("account", CopilotTurnInput{Initiator: "user"}, opts, translator.FormatOpenAI, ojson.Result{})
|
|
links.Observe(other, translator.FormatOpenAI, ojson.Parse(`{"choices":[{"message":{"tool_calls":[{"id":"call-c"}]}}]}`))
|
|
const resultB = `{"type":"tool_result","tool_use_id":"call-b","content":"ok"}`
|
|
const toolB = `{"role":"tool","tool_call_id":"call-b"}`
|
|
for _, tc := range []struct {
|
|
name, content, messages string
|
|
reuse bool
|
|
}{
|
|
{"matching result", result, tool + "," + relay, true},
|
|
{"reordered exact batch", resultB + "," + result, tool + "," + toolB + "," + relay, true},
|
|
{"incomplete batch", resultB + "," + result, tool + "," + relay, false},
|
|
{"extra wire result", result, toolB + "," + tool + "," + relay, false},
|
|
{"duplicate wire ID", resultB + "," + result, tool + "," + tool + "," + relay, false},
|
|
{"duplicate source ID", result + "," + result, tool + "," + tool + "," + relay, false},
|
|
{"different parents", `{"type":"tool_result","tool_use_id":"call-c"},` + result, `{"role":"tool","tool_call_id":"call-c"},` + tool + "," + relay, false},
|
|
{"unknown parent", `{"type":"tool_result","tool_use_id":"unknown"},` + result, `{"role":"tool","tool_call_id":"unknown"},` + tool + "," + relay, false},
|
|
{"unverified trailing reminder", result, tool + "," + relay + `,{"role":"user","content":[{"type":"text","text":"<system-reminder>\nunrelated\n</system-reminder>"}]}`, false},
|
|
{"wire result changed", result, `{"role":"tool","tool_call_id":"call-b"},` + relay, false},
|
|
{"wire result removed", result, relay, false},
|
|
{"old matching result not adjacent", result, tool + `,{"role":"assistant","content":"done"},` + relay, false},
|
|
{"tool result with fresh user image", result + `,{"type":"image"}`, tool + "," + relay, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
input := ParseCopilotTurnInput([]byte(`{"messages":[{"role":"user","content":[`+tc.content+`]}]}`), translator.FormatClaude)
|
|
// Record the relay from an actual tool result, not from the candidate wire history.
|
|
input.chatRelays = generated.chatRelays
|
|
got := links.Resolve("account", input, opts, translator.FormatOpenAI, ojson.Parse(`{"messages":[`+tc.messages+`]}`))
|
|
if (got == first) != tc.reuse || got == other {
|
|
t.Errorf("turn reuse = %t, want %t", got == first, tc.reuse)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCopilotRecycledToolIDsUseUserHistory(t *testing.T) {
|
|
links := NewCopilotTurnLinks()
|
|
opts := cliproxyexecutor.Options{Metadata: map[string]any{cliproxyexecutor.CanonicalSessionIDMetadataKey: "session"}}
|
|
format := translator.FormatOpenAI
|
|
call := `{"choices":[{"message":{"tool_calls":[{"id":"recycled"}]}}]}`
|
|
first := `{"role":"user","content":"again"}`
|
|
result := `{"role":"tool","tool_call_id":"recycled","content":"ok"}`
|
|
assistant := `{"role":"assistant","tool_calls":[{"id":"recycled"}]}`
|
|
second := first + "," + assistant + "," + result + "," + first
|
|
resolve := func(history string) CopilotTurn {
|
|
body := `{"messages":[` + history + `]}`
|
|
return links.Resolve("account", ParseCopilotTurnInput([]byte(body), format), opts, format, ojson.Parse(body))
|
|
}
|
|
a := resolve(first)
|
|
links.Observe(a, format, ojson.Parse(call))
|
|
b := resolve(second)
|
|
links.Observe(b, format, ojson.Parse(call))
|
|
if a.interaction == b.interaction {
|
|
t.Fatal("user turns were merged")
|
|
}
|
|
for _, tc := range []struct {
|
|
history string
|
|
want CopilotTurn
|
|
}{{second, b}, {first, a}} {
|
|
if got := resolve(tc.history + "," + assistant + "," + result); got != tc.want {
|
|
t.Fatal("reused call ID was not linked to its own user turn")
|
|
}
|
|
}
|
|
if got := resolve(result); got == a || got == b {
|
|
t.Fatal("an ambiguous history-free continuation was attributed to another turn")
|
|
}
|
|
}
|
|
|
|
func TestCopilotCLIHeadersUseAccountScopedSessionsAndFeatureBetas(t *testing.T) {
|
|
opts := cliproxyexecutor.Options{Metadata: map[string]any{cliproxyexecutor.CanonicalSessionIDMetadataKey: "incoming-session"}}
|
|
route := copilot.Route{Format: translator.FormatClaude, AnthropicBetas: []string{"thinking-binding-controls-2026-08-01"}}
|
|
links := NewCopilotTurnLinks()
|
|
headers := make(http.Header)
|
|
SetCopilotInferenceHeaders(headers, route, ojson.Parse(`{"thinking":{"type":"adaptive","block_binding":{"prefix_mismatch_behavior":"drop_block"}}}`), "agent", links.Resolve("account-a", CopilotTurnInput{Initiator: "agent"}, opts, route.Format, ojson.Result{}))
|
|
if headers.Get("Anthropic-Beta") != "thinking-binding-controls-2026-08-01" || headers.Get("X-Interaction-Type") != "conversation-agent" || headers.Get("Openai-Intent") != "" {
|
|
t.Fatal("incorrect CLI Messages headers")
|
|
}
|
|
same := make(http.Header)
|
|
SetCopilotInferenceHeaders(same, route, ojson.Parse(`{"thinking":{"type":"adaptive"}}`), "user", links.Resolve("account-a", CopilotTurnInput{Initiator: "user"}, opts, route.Format, ojson.Result{}))
|
|
other := make(http.Header)
|
|
SetCopilotInferenceHeaders(other, route, ojson.Parse(`{"thinking":{"type":"enabled"}}`), "user", links.Resolve("account-b", CopilotTurnInput{Initiator: "user"}, opts, route.Format, ojson.Result{}))
|
|
if same.Get("Anthropic-Beta") != "" || other.Get("Anthropic-Beta") != "" || same.Get("X-Client-Session-Id") != headers.Get("X-Client-Session-Id") || other.Get("X-Client-Session-Id") == same.Get("X-Client-Session-Id") || same.Get("X-Client-Session-Id") == "incoming-session" {
|
|
t.Fatal("unexpected message beta or session identity")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeCopilotRequestPlainTextContent(t *testing.T) {
|
|
route := copilot.Route{Format: translator.FormatOpenAI, Info: ®istry.ModelInfo{}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
from translator.Format
|
|
messages string
|
|
wantType ojson.Type
|
|
wantText string
|
|
}{
|
|
{
|
|
name: "responses single plain text assistant",
|
|
from: translator.FormatOpenAIResponse,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"triage summary"}]}]`,
|
|
wantType: ojson.String,
|
|
wantText: "triage summary",
|
|
},
|
|
{
|
|
name: "responses multi-part plain text assistant",
|
|
from: translator.FormatOpenAIResponse,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"part 1 "},{"type":"text","text":"part 2"}]}]`,
|
|
wantType: ojson.String,
|
|
wantText: "part 1 part 2",
|
|
},
|
|
{
|
|
name: "claude single plain text assistant with tool calls",
|
|
from: translator.FormatClaude,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"checking"}],"tool_calls":[{"id":"call_1","type":"function","function":{"name":"ask","arguments":"{}"}}]}]`,
|
|
wantType: ojson.String,
|
|
wantText: "checking",
|
|
},
|
|
{
|
|
name: "claude empty plain text assistant with tool calls becomes null",
|
|
from: translator.FormatClaude,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":""}],"tool_calls":[{"id":"call_1","type":"function","function":{"name":"ask","arguments":"{}"}}]}]`,
|
|
wantType: ojson.Null,
|
|
},
|
|
{
|
|
name: "assistant block with extra metadata stays array",
|
|
from: translator.FormatOpenAIResponse,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"annotated","annotations":[{"type":"url_citation"}]}]}]`,
|
|
wantType: ojson.JSON,
|
|
},
|
|
{
|
|
name: "assistant multimodal array stays array",
|
|
from: translator.FormatOpenAIResponse,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"look"},{"type":"image_url","image_url":{"url":"https://example.com/a.png"}}]}]`,
|
|
wantType: ojson.JSON,
|
|
},
|
|
{
|
|
name: "native chat single plain text assistant",
|
|
from: translator.FormatOpenAI,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"native"}]}]`,
|
|
wantType: ojson.String,
|
|
wantText: "native",
|
|
},
|
|
{
|
|
name: "native chat assistant with annotated text stays array",
|
|
from: translator.FormatOpenAI,
|
|
messages: `[{"role":"assistant","content":[{"type":"text","text":"a"},{"type":"text","text":"b","annotations":[]}]}]`,
|
|
wantType: ojson.JSON,
|
|
},
|
|
{
|
|
name: "native chat user single text block stays array",
|
|
from: translator.FormatOpenAI,
|
|
messages: `[{"role":"user","content":[{"type":"text","text":"native"}]}]`,
|
|
wantType: ojson.JSON,
|
|
},
|
|
{
|
|
name: "user multi-part array stays array",
|
|
from: translator.FormatOpenAIResponse,
|
|
messages: `[{"role":"user","content":[{"type":"text","text":"a"},{"type":"text","text":"b"}]}]`,
|
|
wantType: ojson.JSON,
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
doc := ojson.NewDocString(`{"model":"gemini-4-neon","messages":` + tc.messages + `}`)
|
|
NormalizeCopilotRequest(doc, route, tc.from, ojson.Result{}, false)
|
|
got := doc.Get("messages.0.content")
|
|
if got.Type != tc.wantType {
|
|
t.Fatalf("messages.0.content type = %v, want %v (raw: %s)", got.Type, tc.wantType, got.Raw)
|
|
}
|
|
if tc.wantType == ojson.String && got.String() != tc.wantText {
|
|
t.Fatalf("messages.0.content = %q, want %q", got.String(), tc.wantText)
|
|
}
|
|
if tc.wantType == ojson.JSON && got.Raw != ojson.Get(tc.messages, "0.content").Raw {
|
|
t.Fatalf("expected original content array, got %s", got.Raw)
|
|
}
|
|
})
|
|
}
|
|
}
|