|
| 1 | +package responses |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/tidwall/gjson" |
| 9 | +) |
| 10 | + |
| 11 | +func prettyJSONForTest(raw []byte) string { |
| 12 | + if !gjson.ValidBytes(raw) { |
| 13 | + return string(raw) |
| 14 | + } |
| 15 | + var out bytes.Buffer |
| 16 | + if err := json.Indent(&out, raw, "", " "); err != nil { |
| 17 | + return string(raw) |
| 18 | + } |
| 19 | + return out.String() |
| 20 | +} |
| 21 | + |
| 22 | +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_MergeConsecutiveFunctionCalls(t *testing.T) { |
| 23 | + raw := []byte(`{ |
| 24 | + "input": [ |
| 25 | + {"type":"function_call","call_id":"exec_command:0","name":"exec_command","arguments":"{\"cmd\":\"ls\"}"}, |
| 26 | + {"type":"function_call","call_id":"exec_command:1","name":"exec_command","arguments":"{\"cmd\":\"pwd\"}"}, |
| 27 | + {"type":"function_call_output","call_id":"exec_command:0","output":"ok0"}, |
| 28 | + {"type":"function_call_output","call_id":"exec_command:1","output":"ok1"} |
| 29 | + ] |
| 30 | + }`) |
| 31 | + t.Logf("input json:\n%s", prettyJSONForTest(raw)) |
| 32 | + |
| 33 | + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, true) |
| 34 | + t.Logf("output json:\n%s", prettyJSONForTest(out)) |
| 35 | + |
| 36 | + msgs := gjson.GetBytes(out, "messages") |
| 37 | + if !msgs.Exists() || !msgs.IsArray() { |
| 38 | + t.Fatalf("messages should be an array") |
| 39 | + } |
| 40 | + if got := len(msgs.Array()); got != 3 { |
| 41 | + t.Fatalf("messages count = %d, want %d", got, 3) |
| 42 | + } |
| 43 | + |
| 44 | + if got := gjson.GetBytes(out, "messages.0.role").String(); got != "assistant" { |
| 45 | + t.Fatalf("messages.0.role = %q, want %q", got, "assistant") |
| 46 | + } |
| 47 | + if got := len(gjson.GetBytes(out, "messages.0.tool_calls").Array()); got != 2 { |
| 48 | + t.Fatalf("messages.0.tool_calls length = %d, want %d", got, 2) |
| 49 | + } |
| 50 | + if got := gjson.GetBytes(out, "messages.0.tool_calls.0.id").String(); got != "exec_command:0" { |
| 51 | + t.Fatalf("messages.0.tool_calls.0.id = %q, want %q", got, "exec_command:0") |
| 52 | + } |
| 53 | + if got := gjson.GetBytes(out, "messages.0.tool_calls.1.id").String(); got != "exec_command:1" { |
| 54 | + t.Fatalf("messages.0.tool_calls.1.id = %q, want %q", got, "exec_command:1") |
| 55 | + } |
| 56 | + |
| 57 | + if got := gjson.GetBytes(out, "messages.1.tool_call_id").String(); got != "exec_command:0" { |
| 58 | + t.Fatalf("messages.1.tool_call_id = %q, want %q", got, "exec_command:0") |
| 59 | + } |
| 60 | + if got := gjson.GetBytes(out, "messages.2.tool_call_id").String(); got != "exec_command:1" { |
| 61 | + t.Fatalf("messages.2.tool_call_id = %q, want %q", got, "exec_command:1") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_SplitFunctionCallsWhenInterrupted(t *testing.T) { |
| 66 | + raw := []byte(`{ |
| 67 | + "input": [ |
| 68 | + {"type":"function_call","call_id":"call_a","name":"tool_a","arguments":"{}"}, |
| 69 | + {"type":"message","role":"user","content":"next"}, |
| 70 | + {"type":"function_call","call_id":"call_b","name":"tool_b","arguments":"{}"} |
| 71 | + ] |
| 72 | + }`) |
| 73 | + t.Logf("input json:\n%s", prettyJSONForTest(raw)) |
| 74 | + |
| 75 | + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, false) |
| 76 | + t.Logf("output json:\n%s", prettyJSONForTest(out)) |
| 77 | + |
| 78 | + if got := len(gjson.GetBytes(out, "messages").Array()); got != 3 { |
| 79 | + t.Fatalf("messages count = %d, want %d", got, 3) |
| 80 | + } |
| 81 | + if got := gjson.GetBytes(out, "messages.0.tool_calls.0.id").String(); got != "call_a" { |
| 82 | + t.Fatalf("messages.0.tool_calls.0.id = %q, want %q", got, "call_a") |
| 83 | + } |
| 84 | + if got := gjson.GetBytes(out, "messages.2.tool_calls.0.id").String(); got != "call_b" { |
| 85 | + t.Fatalf("messages.2.tool_calls.0.id = %q, want %q", got, "call_b") |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_DefersMessageUntilToolOutput(t *testing.T) { |
| 90 | + raw := []byte(`{ |
| 91 | + "input": [ |
| 92 | + {"type":"function_call","call_id":"call_x","name":"exec_command","arguments":"{\"cmd\":\"echo hi\"}"}, |
| 93 | + {"type":"message","role":"user","content":"Approved command prefix saved"}, |
| 94 | + {"type":"function_call_output","call_id":"call_x","output":"ok"}, |
| 95 | + {"type":"message","role":"user","content":"next"} |
| 96 | + ] |
| 97 | + }`) |
| 98 | + t.Logf("input json:\n%s", prettyJSONForTest(raw)) |
| 99 | + |
| 100 | + out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, true) |
| 101 | + t.Logf("output json:\n%s", prettyJSONForTest(out)) |
| 102 | + |
| 103 | + if got := len(gjson.GetBytes(out, "messages").Array()); got != 4 { |
| 104 | + t.Fatalf("messages count = %d, want %d", got, 4) |
| 105 | + } |
| 106 | + if got := gjson.GetBytes(out, "messages.0.role").String(); got != "assistant" { |
| 107 | + t.Fatalf("messages.0.role = %q, want %q", got, "assistant") |
| 108 | + } |
| 109 | + if got := gjson.GetBytes(out, "messages.1.role").String(); got != "tool" { |
| 110 | + t.Fatalf("messages.1.role = %q, want %q", got, "tool") |
| 111 | + } |
| 112 | + if got := gjson.GetBytes(out, "messages.1.tool_call_id").String(); got != "call_x" { |
| 113 | + t.Fatalf("messages.1.tool_call_id = %q, want %q", got, "call_x") |
| 114 | + } |
| 115 | + if got := gjson.GetBytes(out, "messages.2.role").String(); got != "user" { |
| 116 | + t.Fatalf("messages.2.role = %q, want %q", got, "user") |
| 117 | + } |
| 118 | + if got := gjson.GetBytes(out, "messages.2.content").String(); got != "Approved command prefix saved" { |
| 119 | + t.Fatalf("messages.2.content = %q, want %q", got, "Approved command prefix saved") |
| 120 | + } |
| 121 | + if got := gjson.GetBytes(out, "messages.3.content").String(); got != "next" { |
| 122 | + t.Fatalf("messages.3.content = %q, want %q", got, "next") |
| 123 | + } |
| 124 | +} |
0 commit comments