mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Codex declares each MCP server's tools as one Responses `namespace`. The bridge sent that namespace to the chat model as a single function with no schema, so the model saw `mcp__hanzo` and none of the tools inside it, and Codex, Hanzo Dev and Open Interpreter refused every call it made with "unsupported call: mcp__hanzo". Each tool inside a namespace is now a chat function of its own, named `<namespace>__<name>` within 64 characters; a call comes back with the `namespace` and `name` the client routes by, and a namespaced call in the history is named the same way. Claude-Session: https://claude.ai/code/session_01M8RoubcPN71KgJuZ8LdBUL
445 lines
18 KiB
Go
445 lines
18 KiB
Go
package controllers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
func TestResponsesZstdPureGoRoundTrip(t *testing.T) {
|
|
w, err := zstd.NewWriter(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
compressed := w.EncodeAll([]byte(`{"model":"zen4-coder","input":"hello"}`), nil)
|
|
w.Close()
|
|
|
|
decoded, err := decodeResponsesZstd(compressed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, want := string(decoded), `{"model":"zen4-coder","input":"hello"}`; got != want {
|
|
t.Fatalf("decoded = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestResponsesInputToMessagesToolRoundTrip(t *testing.T) {
|
|
input := json.RawMessage(`[
|
|
{"type":"message","role":"user","content":[{"type":"input_text","text":"run it"}]},
|
|
{"type":"function_call","id":"fc_1","call_id":"call_1","name":"shell_command","arguments":"{\"command\":\"pwd\"}"},
|
|
{"type":"function_call_output","call_id":"call_1","output":"/tmp"},
|
|
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}
|
|
]`)
|
|
messages, err := responsesInputToMessages("be precise", input)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 5 {
|
|
t.Fatalf("messages = %#v, want system + user + assistant tool call + tool result + assistant", messages)
|
|
}
|
|
if messages[0].Role != "system" || messages[0].Content != "be precise" {
|
|
t.Fatalf("system message = %#v", messages[0])
|
|
}
|
|
if messages[2].Role != "assistant" || len(messages[2].ToolCalls) != 1 {
|
|
t.Fatalf("tool call history = %#v", messages[2])
|
|
}
|
|
if got := messages[2].ToolCalls[0].Function.Name; got != "shell_command" {
|
|
t.Fatalf("tool name = %q", got)
|
|
}
|
|
if messages[3].Role != "tool" || messages[3].ToolCallID != "call_1" || messages[3].Content != "/tmp" {
|
|
t.Fatalf("tool result = %#v", messages[3])
|
|
}
|
|
}
|
|
|
|
// A user message carrying a screenshot beside its text — what a coding agent's
|
|
// browser tool sends after every capture. go-openai refuses to marshal a message
|
|
// that sets both Content and MultiContent, so the parts must travel alone.
|
|
func TestResponsesInputToMessagesImageMarshals(t *testing.T) {
|
|
input := json.RawMessage(`[
|
|
{"type":"message","role":"user","content":[
|
|
{"type":"input_text","text":"what is on screen?"},
|
|
{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgo="}
|
|
]}
|
|
]`)
|
|
messages, err := responsesInputToMessages("", input)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 1 || len(messages[0].MultiContent) != 2 || messages[0].Content != "" {
|
|
t.Fatalf("message = %#v, want two parts and no joined text", messages[0])
|
|
}
|
|
if _, err := json.Marshal(messages[0]); err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResponsesToChatRequestTools(t *testing.T) {
|
|
parallel := true
|
|
req := &OpenAIResponsesRequest{
|
|
Model: "zen4-coder", Input: json.RawMessage(`"hello"`), Stream: true,
|
|
ParallelToolCalls: ¶llel,
|
|
ToolChoice: json.RawMessage(`{"type":"function","name":"shell_command"}`),
|
|
Tools: []responsesTool{{
|
|
Type: "function", Name: "shell_command", Description: "run shell",
|
|
Parameters: json.RawMessage(`{"type":"object","properties":{"command":{"type":"string"}}}`),
|
|
}},
|
|
}
|
|
chat, kinds, err := responsesToChatRequest(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(chat.Tools) != 1 || chat.Tools[0].Function.Name != "shell_command" {
|
|
t.Fatalf("tools = %#v", chat.Tools)
|
|
}
|
|
if kinds["shell_command"] != "function" {
|
|
t.Fatalf("tool kinds = %#v", kinds)
|
|
}
|
|
choice, ok := chat.ToolChoice.(map[string]any)
|
|
if !ok || choice["type"] != "function" {
|
|
t.Fatalf("tool choice = %#v", chat.ToolChoice)
|
|
}
|
|
if enabled, ok := chat.ParallelToolCalls.(bool); !ok || !enabled {
|
|
t.Fatalf("parallel_tool_calls = %#v", chat.ParallelToolCalls)
|
|
}
|
|
if chat.StreamOptions == nil || !chat.StreamOptions.IncludeUsage {
|
|
t.Fatalf("stream_options must request the final usage chunk: %#v", chat.StreamOptions)
|
|
}
|
|
}
|
|
|
|
func TestResponsesStreamTextWire(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{Model: "zen4-coder", Stream: true}
|
|
out := &bytes.Buffer{}
|
|
bridge := newResponsesBridge(out, req, nil)
|
|
|
|
upstream := strings.Join([]string{
|
|
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{"role":"assistant","content":"HANZO_"},"finish_reason":null}]}`,
|
|
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{"content":"OK"},"finish_reason":null}]}`,
|
|
`data: {"id":"chat_1","model":"qwen","choices":[{"delta":{},"finish_reason":"stop"}]}`,
|
|
`data: {"id":"chat_1","model":"qwen","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":2,"total_tokens":14}}`,
|
|
`data: [DONE]`,
|
|
"",
|
|
}, "\n\n")
|
|
// Split in the middle of a frame to prove arbitrary network writes work.
|
|
cut := len(upstream) / 2
|
|
if _, err := bridge.Write([]byte(upstream[:cut])); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := bridge.Write([]byte(upstream[cut:])); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := bridge.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := out.String()
|
|
|
|
assertInOrder(
|
|
t, wire,
|
|
`event: response.created`,
|
|
`event: response.output_item.added`,
|
|
`event: response.content_part.added`,
|
|
`"delta":"HANZO_"`,
|
|
`"delta":"OK"`,
|
|
`event: response.output_item.done`,
|
|
`event: response.completed`,
|
|
)
|
|
for _, forbidden := range []string{`chat.completion.chunk`, `"choices"`, `data: [DONE]`} {
|
|
if strings.Contains(wire, forbidden) {
|
|
t.Fatalf("raw Chat Completions token %q leaked:\n%s", forbidden, wire)
|
|
}
|
|
}
|
|
if !strings.Contains(wire, `"text":"HANZO_OK"`) || !strings.Contains(wire, `"total_tokens":14`) {
|
|
t.Fatalf("missing final text/usage:\n%s", wire)
|
|
}
|
|
if got := strings.Count(wire, `event: response.completed`); got != 1 {
|
|
t.Fatalf("response.completed count = %d, want exactly one after the usage chunk:\n%s", got, wire)
|
|
}
|
|
}
|
|
|
|
func TestResponsesStreamFunctionCallWire(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{Model: "zen4-coder", Stream: true}
|
|
out := &bytes.Buffer{}
|
|
bridge := newResponsesBridge(out, req, map[string]string{"shell_command": "function"})
|
|
|
|
upstream := strings.Join([]string{
|
|
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"shell_command","arguments":""}}]},"finish_reason":null}]}`,
|
|
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"command\":"}}]},"finish_reason":null}]}`,
|
|
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"pwd\"}"}}]},"finish_reason":null}]}`,
|
|
`data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":20,"completion_tokens":5,"total_tokens":25}}`,
|
|
`data: [DONE]`,
|
|
"",
|
|
}, "\n\n")
|
|
if _, err := bridge.Write([]byte(upstream)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := bridge.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := out.String()
|
|
|
|
assertInOrder(
|
|
t, wire,
|
|
`event: response.created`,
|
|
`event: response.output_item.added`,
|
|
`event: response.function_call_arguments.delta`,
|
|
`event: response.function_call_arguments.done`,
|
|
`event: response.output_item.done`,
|
|
`event: response.completed`,
|
|
)
|
|
for _, required := range []string{`"type":"function_call"`, `"call_id":"call_1"`, `"name":"shell_command"`, `"arguments":"{\"command\":\"pwd\"}"`} {
|
|
if !strings.Contains(wire, required) {
|
|
t.Fatalf("missing %q:\n%s", required, wire)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A non-streaming answer is ONE body, so it is translated as one — by the function
|
|
// the sink in Responses hands it to, not through the stream bridge. It used to go
|
|
// through the bridge because the bridge was standing in for the response writer and
|
|
// saw everything; it now sees only the stream, so this asks the translation
|
|
// directly.
|
|
func TestResponsesNonStreamingTranslation(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{Model: "zen4-coder"}
|
|
chat := `{"id":"chat_1","choices":[{"message":{"role":"assistant","content":"OK"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`
|
|
|
|
body, err := openAIChatResponseToResponses([]byte(chat), req, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got map[string]any
|
|
if err := json.Unmarshal(body, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got["object"] != "response" || got["status"] != "completed" {
|
|
t.Fatalf("response = %#v", got)
|
|
}
|
|
if strings.Contains(string(body), `"choices"`) {
|
|
t.Fatalf("chat response leaked: %s", body)
|
|
}
|
|
}
|
|
|
|
func assertInOrder(t *testing.T, text string, needles ...string) {
|
|
t.Helper()
|
|
pos := 0
|
|
for _, needle := range needles {
|
|
next := strings.Index(text[pos:], needle)
|
|
if next < 0 {
|
|
t.Fatalf("missing/out-of-order %q after byte %d:\n%s", needle, pos, text)
|
|
}
|
|
pos += next + len(needle)
|
|
}
|
|
}
|
|
|
|
// A provider is free to end its stream without [DONE], and Hanzo's does. When
|
|
// that happened the response never closed: every delta arrived, none of the
|
|
// terminal events did, and a client reading the Responses stream to spec waited
|
|
// for response.completed, retried, and reported the stream as truncated.
|
|
//
|
|
// The usage chunk still has to land in it, which is why finishing on
|
|
// finish_reason alone was wrong — whichever signal arrives second finishes.
|
|
func TestResponsesStreamCompletesWithoutDone(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{Model: "enso-auto", Stream: true}
|
|
out := &bytes.Buffer{}
|
|
bridge := newResponsesBridge(out, req, nil)
|
|
|
|
upstream := strings.Join([]string{
|
|
`data: {"id":"chat_1","model":"enso","choices":[{"delta":{"role":"assistant","content":"hi"},"finish_reason":null}]}`,
|
|
`data: {"id":"chat_1","model":"enso","choices":[{"delta":{},"finish_reason":"stop"}]}`,
|
|
`data: {"id":"chat_1","model":"enso","choices":[],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`,
|
|
"",
|
|
}, "\n\n")
|
|
if _, err := bridge.Write([]byte(upstream)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := out.String()
|
|
|
|
// Before Close, because a client must not have to wait for the connection
|
|
// to drop to learn the answer is complete.
|
|
assertInOrder(
|
|
t, wire,
|
|
`event: response.created`,
|
|
`"delta":"hi"`,
|
|
`event: response.output_text.done`,
|
|
`event: response.content_part.done`,
|
|
`event: response.output_item.done`,
|
|
`event: response.completed`,
|
|
)
|
|
if !strings.Contains(wire, `"total_tokens":4`) {
|
|
t.Fatalf("usage from the post-finish_reason chunk was lost:\n%s", wire)
|
|
}
|
|
if err := bridge.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Count(out.String(), `event: response.completed`); got != 1 {
|
|
t.Fatalf("response.completed count = %d, want exactly one; Close must not repeat it", got)
|
|
}
|
|
}
|
|
|
|
// The usage chunk may also arrive first. Whichever signal is second finishes.
|
|
func TestResponsesStreamCompletesWhenUsagePrecedesFinishReason(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{Model: "enso-auto", Stream: true}
|
|
out := &bytes.Buffer{}
|
|
bridge := newResponsesBridge(out, req, nil)
|
|
|
|
upstream := strings.Join([]string{
|
|
`data: {"id":"c","model":"enso","choices":[{"delta":{"role":"assistant","content":"yo"},"finish_reason":null}]}`,
|
|
`data: {"id":"c","model":"enso","choices":[],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`,
|
|
`data: {"id":"c","model":"enso","choices":[{"delta":{},"finish_reason":"stop"}]}`,
|
|
"",
|
|
}, "\n\n")
|
|
if _, err := bridge.Write([]byte(upstream)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := out.String()
|
|
if !strings.Contains(wire, `event: response.completed`) {
|
|
t.Fatalf("no response.completed when usage preceded finish_reason:\n%s", wire)
|
|
}
|
|
if !strings.Contains(wire, `"total_tokens":2`) {
|
|
t.Fatalf("usage missing:\n%s", wire)
|
|
}
|
|
}
|
|
|
|
// A Responses request reaches the chat body with what go-openai's struct cannot hold:
|
|
// greedy sampling (zero, which omitempty drops), the structured-output format in
|
|
// chat's shape, and the reasoning effort.
|
|
func TestAResponsesRequestKeepsZeroSamplingItsFormatAndItsEffort(t *testing.T) {
|
|
call, err := ReadResponses([]byte(`{"model":"m","input":"hi","temperature":0,"top_p":0,`+
|
|
`"text":{"format":{"type":"json_schema","name":"answer","schema":{"type":"object"},"strict":true}},`+
|
|
`"reasoning":{"effort":"high"}}`), "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var chat map[string]json.RawMessage
|
|
if err := json.Unmarshal(call.Chat, &chat); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(chat["temperature"]) != "0" || string(chat["top_p"]) != "0" {
|
|
t.Errorf("temperature=%s top_p=%s, want 0 and 0: %s", chat["temperature"], chat["top_p"], call.Chat)
|
|
}
|
|
var format struct {
|
|
Type string `json:"type"`
|
|
JSONSchema struct {
|
|
Name string `json:"name"`
|
|
Schema json.RawMessage `json:"schema"`
|
|
Strict bool `json:"strict"`
|
|
} `json:"json_schema"`
|
|
}
|
|
if err := json.Unmarshal(chat["response_format"], &format); err != nil || format.Type != "json_schema" ||
|
|
format.JSONSchema.Name != "answer" || !format.JSONSchema.Strict || string(format.JSONSchema.Schema) != `{"type":"object"}` {
|
|
t.Errorf("response_format = %s", chat["response_format"])
|
|
}
|
|
if string(chat["reasoning_effort"]) != `"high"` {
|
|
t.Errorf("reasoning_effort = %s", chat["reasoning_effort"])
|
|
}
|
|
}
|
|
|
|
// A stored conversation is asked for by id, and none is stored here: the request is
|
|
// refused rather than answered without the conversation it named.
|
|
func TestAPreviousResponseIsRefusedNotIgnored(t *testing.T) {
|
|
if _, err := ReadResponses([]byte(`{"model":"m","input":"and then?","previous_response_id":"resp_1"}`), ""); err == nil ||
|
|
!strings.Contains(err.Error(), "previous_response_id") {
|
|
t.Fatalf("err = %v, want a refusal naming previous_response_id", err)
|
|
}
|
|
}
|
|
|
|
// A Responses field that would buy something the SKU's price does not cover is
|
|
// refused here as on /v1/chat/completions, not dropped by the conversion.
|
|
func TestAResponsesRequestNamingAnUnpricedFieldIsRefused(t *testing.T) {
|
|
if _, err := ReadResponses([]byte(`{"model":"m","input":"hi","service_tier":"priority"}`), ""); err == nil ||
|
|
!strings.Contains(err.Error(), "service_tier") {
|
|
t.Fatalf("err = %v, want a refusal naming service_tier", err)
|
|
}
|
|
}
|
|
|
|
// Codex declares each MCP server's tools as one `namespace`. Chat Completions has
|
|
// none, so every tool inside is a function of its own, and a call coming back is
|
|
// answered with the namespace and name the client routes by. Sending the
|
|
// namespace as one empty function left the model one tool per server to call,
|
|
// with no schema, and Codex refused every call it made ("unsupported call").
|
|
func TestResponsesNamespaceToolsReachTheModelOneByOne(t *testing.T) {
|
|
req := &OpenAIResponsesRequest{
|
|
Model: "enso",
|
|
Stream: true,
|
|
Input: json.RawMessage(`[
|
|
{"type":"message","role":"user","content":"decide"},
|
|
{"type":"function_call","call_id":"c0","namespace":"mcp__hanzo","name":"kai_decide","arguments":"{}"},
|
|
{"type":"function_call_output","call_id":"c0","output":"0.99"}
|
|
]`),
|
|
Tools: []responsesTool{
|
|
{Type: "function", Name: "exec_command", Parameters: json.RawMessage(`{"type":"object","properties":{}}`)},
|
|
{Type: "namespace", Name: "mcp__hanzo", Description: "Hanzo", Tools: []responsesTool{
|
|
{Type: "function", Name: "kai_decide", Description: "decide", Parameters: json.RawMessage(`{"type":"object","properties":{"questions":{"type":"object"}}}`)},
|
|
{Type: "function", Name: "exec", Parameters: json.RawMessage(`{"type":"object","properties":{}}`)},
|
|
}},
|
|
},
|
|
}
|
|
chat, kinds, err := responsesToChatRequest(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var names []string
|
|
for _, tool := range chat.Tools {
|
|
names = append(names, tool.Function.Name)
|
|
}
|
|
if strings.Join(names, ",") != "exec_command,mcp__hanzo__kai_decide,mcp__hanzo__exec" {
|
|
t.Fatalf("chat tools = %v", names)
|
|
}
|
|
if params, _ := json.Marshal(chat.Tools[1].Function.Parameters); !strings.Contains(string(params), `"questions"`) {
|
|
t.Fatalf("the tool inside the namespace lost its schema: %s", params)
|
|
}
|
|
// The history names the same call the same way.
|
|
var history string
|
|
for _, m := range chat.Messages {
|
|
for _, tc := range m.ToolCalls {
|
|
history += tc.Function.Name
|
|
}
|
|
}
|
|
if history != "mcp__hanzo__kai_decide" {
|
|
t.Fatalf("history tool call = %q", history)
|
|
}
|
|
|
|
out := &bytes.Buffer{}
|
|
bridge := newResponsesBridge(out, req, kinds)
|
|
upstream := strings.Join([]string{
|
|
`data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"mcp__hanzo__kai_decide","arguments":"{}"}}]},"finish_reason":null}]}`,
|
|
`data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":20,"completion_tokens":5,"total_tokens":25}}`,
|
|
`data: [DONE]`,
|
|
"",
|
|
}, "\n\n")
|
|
if _, err := bridge.Write([]byte(upstream)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := bridge.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := out.String()
|
|
if !strings.Contains(wire, `"name":"kai_decide","namespace":"mcp__hanzo"`) {
|
|
t.Fatalf("the call did not come back namespaced:\n%s", wire)
|
|
}
|
|
if strings.Contains(wire, `"name":"mcp__hanzo__kai_decide"`) {
|
|
t.Fatalf("the flat name leaked to the client:\n%s", wire)
|
|
}
|
|
|
|
// A whole answer comes back the same way.
|
|
whole := `{"choices":[{"message":{"role":"assistant","tool_calls":[{"id":"call_2","function":{"name":"mcp__hanzo__exec","arguments":"{}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`
|
|
body, err := openAIChatResponseToResponses([]byte(whole), req, kinds)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(body), `"namespace":"mcp__hanzo"`) || !strings.Contains(string(body), `"name":"exec"`) {
|
|
t.Fatalf("whole answer = %s", body)
|
|
}
|
|
}
|
|
|
|
func TestFlatNamesStayWithinSixtyFourCharacters(t *testing.T) {
|
|
long := flatName("mcp__codex_apps__safety_settings", "_prepare_parental_control_update_with_a_longer_name")
|
|
if len(long) > 64 || long != flatName("mcp__codex_apps__safety_settings", "_prepare_parental_control_update_with_a_longer_name") {
|
|
t.Fatalf("flat name %q (%d)", long, len(long))
|
|
}
|
|
if got := flatName("mcp__codex_apps__github", "_add_comment_to_issue"); got != "mcp__codex_apps__github__add_comment_to_issue" {
|
|
t.Fatalf("flat name %q", got)
|
|
}
|
|
}
|