mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
- Update Codex request translators for OpenAI, Claude, Gemini, Interactions, and OpenAI Responses to correctly extract and preserve `max_output_tokens` or equivalent token limit fields. - Remove automatic deletion of `max_output_tokens` during request cleanup to ensure token limits are respected across execution paths. Closes: #7230
592 lines
29 KiB
Go
592 lines
29 KiB
Go
package interactions
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/MoeForge/moego/base64"
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
|
)
|
|
|
|
func TestConvertInteractionsRequestToCodexMaxOutputTokens(t *testing.T) {
|
|
for _, path := range []string{
|
|
"generation_config.max_output_tokens",
|
|
"generation_config.maxOutputTokens",
|
|
"generation_config.max_completion_tokens",
|
|
"generation_config.maxCompletionTokens",
|
|
"generation_config.max_tokens",
|
|
"generationConfig.max_output_tokens",
|
|
"generationConfig.maxOutputTokens",
|
|
"generationConfig.max_completion_tokens",
|
|
"generationConfig.maxCompletionTokens",
|
|
"generationConfig.max_tokens",
|
|
"max_output_tokens",
|
|
"maxOutputTokens",
|
|
"max_completion_tokens",
|
|
"maxCompletionTokens",
|
|
"max_tokens",
|
|
"agent_config.max_total_tokens",
|
|
"agentConfig.maxTotalTokens",
|
|
} {
|
|
t.Run(path, func(t *testing.T) {
|
|
inputDoc := ojson.NewDocString(`{"input":"hi"}`)
|
|
_ = inputDoc.Set(path, 1024)
|
|
out, errConvert := ConvertInteractionsRequestToCodex("codex-test", inputDoc.Bytes(), false)
|
|
if errConvert != nil {
|
|
t.Fatalf("ConvertInteractionsRequestToCodex() error = %v", errConvert)
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "max_output_tokens").Raw; got != "1024" {
|
|
t.Fatalf("max_output_tokens = %q, want 1024; output=%s", got, out)
|
|
}
|
|
for _, sourcePath := range []string{"generation_config", "generationConfig", "maxOutputTokens", "max_completion_tokens", "maxCompletionTokens", "max_tokens", "agent_config", "agentConfig"} {
|
|
if ojson.GetBytesNoCopy(out, sourcePath).Exists() {
|
|
t.Fatalf("%s should not be forwarded; output=%s", sourcePath, out)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexMaxOutputTokensPriority(t *testing.T) {
|
|
for _, prefix := range []string{"generation_config.", "generationConfig.", ""} {
|
|
paths := []string{"max_output_tokens", "maxOutputTokens", "max_completion_tokens", "maxCompletionTokens", "max_tokens"}
|
|
if prefix == "" {
|
|
paths = append(paths, "agent_config.max_total_tokens", "agentConfig.maxTotalTokens")
|
|
}
|
|
for index, path := range paths {
|
|
t.Run(prefix+path, func(t *testing.T) {
|
|
inputDoc := ojson.NewDocString(`{"input":"hi"}`)
|
|
for offset, sourcePath := range paths[index:] {
|
|
_ = inputDoc.Set(prefix+sourcePath, 1024+offset)
|
|
}
|
|
if prefix != "" {
|
|
_ = inputDoc.Set("max_output_tokens", 4096)
|
|
_ = inputDoc.Set("agent_config.max_total_tokens", 8192)
|
|
}
|
|
out, errConvert := ConvertInteractionsRequestToCodex("codex-test", inputDoc.Bytes(), false)
|
|
if errConvert != nil {
|
|
t.Fatalf("ConvertInteractionsRequestToCodex() error = %v", errConvert)
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "max_output_tokens").Raw; got != "1024" {
|
|
t.Fatalf("max_output_tokens = %q, want first matching limit 1024; output=%s", got, out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexMaxOutputTokensFallbacks(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want string
|
|
}{
|
|
{"no limit", `{"input":"hi"}`, ""},
|
|
{"empty generation config", `{"input":"hi","generation_config":{},"max_tokens":1024}`, "1024"},
|
|
{"generation config without token limit", `{"input":"hi","generationConfig":{"thinking_level":"high"},"agentConfig":{"maxTotalTokens":1024}}`, "1024"},
|
|
{"zero config limit takes precedence", `{"input":"hi","generation_config":{"max_tokens":0},"max_output_tokens":1024}`, "0"},
|
|
{"zero top-level limit takes precedence", `{"input":"hi","max_output_tokens":0,"max_tokens":1024}`, "0"},
|
|
{"snake config takes precedence", `{"input":"hi","generation_config":{"max_tokens":1024},"generationConfig":{"max_output_tokens":2048}}`, "1024"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
out, errConvert := ConvertInteractionsRequestToCodex("codex-test", []byte(tt.input), false)
|
|
if errConvert != nil {
|
|
t.Fatalf("ConvertInteractionsRequestToCodex() error = %v", errConvert)
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "max_output_tokens").Raw; got != tt.want {
|
|
t.Fatalf("max_output_tokens = %q, want %q; output=%s", got, tt.want, out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexGenerationConfigAliasPriority(t *testing.T) {
|
|
out, errConvert := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi","generation_config":{"top_p":0.5,"topP":0.9,"parallel_tool_calls":false,"parallelToolCalls":true,"tool_choice":"none","toolChoice":"auto","text":{"verbosity":"low"},"verbosity":"high"}}`), false)
|
|
if errConvert != nil {
|
|
t.Fatalf("ConvertInteractionsRequestToCodex() error = %v", errConvert)
|
|
}
|
|
for path, want := range map[string]string{
|
|
"top_p": "0.5",
|
|
"parallel_tool_calls": "false",
|
|
"tool_choice": `"none"`,
|
|
"text.verbosity": `"low"`,
|
|
} {
|
|
if got := ojson.GetBytesNoCopy(out, path).Raw; got != want {
|
|
t.Fatalf("%s = %q, want %q; output=%s", path, got, want, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexWithToolMessagesDirect(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"model":"codex-test","system_instruction":"be brief","input":[{"type":"user_input","content":[{"type":"text","text":"hi"}]},{"type":"thought","content":[{"type":"text","text":"thinking"}]},{"type":"function_call","name":"lookup","call_id":"call_1","arguments":{"q":"x"}},{"type":"function_result","name":"lookup","call_id":"call_1","result":{"ok":true}}],"tools":[{"type":"function","name":"lookup","parameters":{"type":"object","properties":{"q":{"type":"string"}}}}]}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "instructions").String(); got != "be brief" {
|
|
t.Fatalf("instructions = %q, want be brief. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.content.0.text").String(); got != "hi" {
|
|
t.Fatalf("input.0.content.0.text = %q, want hi. Output: %s", got, string(out))
|
|
}
|
|
// The signatureless thought step is dropped entirely: a reasoning item
|
|
// without replayable encrypted_content is rejected by both upstreams.
|
|
if got := ojson.GetBytesNoCopy(out, "input.1.type").String(); got != "function_call" {
|
|
t.Fatalf("input.1.type = %q, want function_call. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.1.call_id").String(); got != "call_1" {
|
|
t.Fatalf("function_call call_id = %q, want call_1. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.2.type").String(); got != "function_call_output" {
|
|
t.Fatalf("input.2.type = %q, want function_call_output. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "tools.0.name").String(); got != "lookup" {
|
|
t.Fatalf("tools.0.name = %q, want lookup. Output: %s", got, string(out))
|
|
}
|
|
if ojson.GetBytesNoCopy(out, "contents").Exists() || ojson.GetBytesNoCopy(out, "systemInstruction").Exists() {
|
|
t.Fatalf("Codex request must not use foreign request shape. Output: %s", string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexPreservesNonImageMediaContent(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"model":"codex-test","input":[{"type":"model_output","content":[{"type":"audio","mime_type":"audio/wav","data":"UklGRg=="},{"type":"video","mime_type":"video/mp4","data":"AAAAIGZ0eXA="},{"type":"document","mime_type":"application/pdf","data":"JVBERi0="}]}]}`), false)
|
|
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.role").String(); got != "assistant" {
|
|
t.Fatalf("input.0.role = %q, want assistant. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.content.0.type").String(); got != "input_audio" {
|
|
t.Fatalf("audio content type = %q, want input_audio. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.1.content.0.type").String(); got != "input_file" {
|
|
t.Fatalf("video content type = %q, want input_file. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.2.content.0.type").String(); got != "input_file" {
|
|
t.Fatalf("document content type = %q, want input_file. Output: %s", got, string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexPreservesTopLevelThinkingLevel(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"model":"codex-test","generation_config":{"thinking_level":"high"},"input":"hi"}`), true)
|
|
if got := ojson.GetBytesNoCopy(out, "reasoning.effort").String(); got != "high" {
|
|
t.Fatalf("reasoning.effort = %q, want high. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "stream").Bool(); !got {
|
|
t.Fatalf("stream = %v, want true. Output: %s", got, string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexUsesBodyStream(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"model":"codex-test","stream":true,"input":"hi"}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "stream").Bool(); !got {
|
|
t.Fatalf("stream = %v, want true. Output: %s", got, string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodexFunctionDeclarations(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"model":"codex-test","input":"hi","tools":[{"function_declarations":[{"name":"lookup","description":"Lookup data","parameters":{"type":"object","$schema":"http://json-schema.org/draft-07/schema#","properties":{"q":{"type":"string"}}}}]}]}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "tools.0.type").String(); got != "function" {
|
|
t.Fatalf("tools.0.type = %q, want function. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "tools.0.name").String(); got != "lookup" {
|
|
t.Fatalf("tools.0.name = %q, want lookup. Output: %s", got, string(out))
|
|
}
|
|
if ojson.GetBytesNoCopy(out, "tools.0.parameters.$schema").Exists() {
|
|
t.Fatalf("tool parameters should not keep $schema. Output: %s", string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsIncompleteTerminal(t *testing.T) {
|
|
raw := []byte(`{"type":"response.incomplete","response":{"id":"resp_1","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[],"usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}}`)
|
|
nonStreamOut := ConvertCodexResponseToInteractionsNonStream(context.Background(), "codex-test", nil, nil, raw, nil)
|
|
if got := ojson.GetBytesNoCopy(nonStreamOut, "status").String(); got != "incomplete" {
|
|
t.Fatalf("non-stream status = %q, want incomplete. Output: %s", got, nonStreamOut)
|
|
}
|
|
|
|
var param any
|
|
streamOut := ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, append([]byte("data: "), raw...), ¶m)
|
|
payload := findCodexInteractionsEventPayload(streamOut, "interaction.completed")
|
|
if len(payload) == 0 {
|
|
t.Fatalf("stream incomplete event did not terminate interaction: %q", streamOut)
|
|
}
|
|
if got := ojson.GetBytesNoCopy(payload, "interaction.status").String(); got != "incomplete" {
|
|
t.Fatalf("stream status = %q, want incomplete. Payload: %s", got, payload)
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsNonStream(t *testing.T) {
|
|
raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","created_at":1700000000,"usage":{"input_tokens":3,"output_tokens":2},"output":[{"type":"message","content":[{"type":"output_text","text":"ok"}]},{"type":"reasoning","content":"thinking"},{"type":"function_call","call_id":"call_1","name":"lookup","arguments":"{\"q\":\"x\"}"}]}}`)
|
|
out := ConvertCodexResponseToInteractionsNonStream(context.Background(), "codex-test", nil, nil, raw, nil)
|
|
if got := ojson.GetBytesNoCopy(out, "steps.0.content.0.text").String(); got != "ok" {
|
|
t.Fatalf("steps.0.content.0.text = %q, want ok. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "steps.1.type").String(); got != "thought" {
|
|
t.Fatalf("steps.1.type = %q, want thought. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "steps.2.type").String(); got != "function_call" {
|
|
t.Fatalf("steps.2.type = %q, want function_call. Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "usage.total_tokens").Int(); got != 5 {
|
|
t.Fatalf("usage.total_tokens = %d, want 5. Output: %s", got, string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsStream(t *testing.T) {
|
|
var param any
|
|
events := ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, []byte(`data: {"type":"response.output_text.delta","delta":"ok"}`), ¶m)
|
|
payload := findCodexInteractionsEventPayload(events, "step.delta")
|
|
if len(payload) == 0 {
|
|
t.Fatalf("step.delta event not found: %q", events)
|
|
}
|
|
if got := ojson.GetBytesNoCopy(payload, "delta.text").String(); got != "ok" {
|
|
t.Fatalf("delta.text = %q, want ok. Payload: %s", got, string(payload))
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsStreamFunctionCallStartHasCallID(t *testing.T) {
|
|
var param any
|
|
events := ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, []byte(`data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_1","name":"lookup","arguments":"{\"q\":\"x\"}"}}`), ¶m)
|
|
payload := findCodexInteractionsEventPayload(events, "step.start")
|
|
if got := ojson.GetBytesNoCopy(payload, "step.call_id").String(); got != "call_1" {
|
|
t.Fatalf("step.call_id = %q, want call_1. Payload: %s", got, string(payload))
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsStreamCompletesAfterSteps(t *testing.T) {
|
|
var param any
|
|
var events [][]byte
|
|
for _, chunk := range [][]byte{
|
|
[]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"codex-test"}}`),
|
|
[]byte(`data: {"type":"response.output_text.delta","delta":"我将调用工具。"}`),
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_1","name":"lookup","arguments":"{\"q\":\"weather\"}"},"output_index":1}`),
|
|
[]byte(`data: {"type":"response.completed","response":{"id":"resp_1","output":[],"usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}}`),
|
|
} {
|
|
events = append(events, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m)...)
|
|
}
|
|
|
|
got := strings.Join(codexInteractionsEventNames(events), ",")
|
|
want := "interaction.created,interaction.status_update,step.start,step.delta,step.stop,step.start,step.delta,step.stop,interaction.completed,done"
|
|
if got != want {
|
|
t.Fatalf("events = %s, want %s", got, want)
|
|
}
|
|
completed := findCodexInteractionsEventPayload(events, "interaction.completed")
|
|
if gotTokens := ojson.GetBytesNoCopy(completed, "interaction.usage.total_tokens").Int(); gotTokens != 3 {
|
|
t.Fatalf("total_tokens = %d, want 3. Payload: %s", gotTokens, string(completed))
|
|
}
|
|
}
|
|
|
|
// testInteractionsMetaSignature builds a shape-valid synthetic Meta envelope;
|
|
// no captured signature is embedded.
|
|
func testInteractionsMetaSignature() string {
|
|
return "Q-PaDgE" + strings.Repeat("A", 1419)
|
|
}
|
|
|
|
func testInteractionsGPTSignature() string {
|
|
decoded := make([]byte, 89)
|
|
decoded[0] = 0x80
|
|
for i := 9; i < len(decoded); i++ {
|
|
decoded[i] = byte(i)
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString(decoded)
|
|
}
|
|
|
|
func findCodexInteractionsEventPayloads(events [][]byte, eventType string) []ojson.Result {
|
|
var out []ojson.Result
|
|
for _, event := range events {
|
|
for _, line := range bytes.Split(event, []byte("\n")) {
|
|
line = bytes.TrimSpace(line)
|
|
if !bytes.HasPrefix(line, []byte("data:")) {
|
|
continue
|
|
}
|
|
payload := bytes.TrimSpace(line[len("data:"):])
|
|
if codexInteractionsEventName(codexInteractionsFrameEventName(event), payload) == eventType {
|
|
out = append(out, util.ParseGJSONBytesNoCopy(payload))
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsNonStream_ReasoningSignature(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
routedSig := metaSig + "." + strings.Repeat("Ab3", 23)[:67]
|
|
raw := []byte(`{"type":"response.completed","response":{"id":"resp_1","output":[` +
|
|
`{"type":"reasoning","summary":[{"type":"summary_text","text":"thinking hard"}],"encrypted_content":"` + metaSig + `"},` +
|
|
`{"type":"reasoning","summary":[],"encrypted_content":"` + routedSig + `"},` +
|
|
`{"type":"reasoning","summary":[]}` +
|
|
`]}}`)
|
|
|
|
out := ConvertCodexResponseToInteractionsNonStream(context.Background(), "codex-test", nil, nil, raw, nil)
|
|
steps := ojson.GetBytesNoCopy(out, "steps").Array()
|
|
if len(steps) != 2 {
|
|
t.Fatalf("steps len = %d, want 2 (empty reasoning emits no step). Output: %s", len(steps), string(out))
|
|
}
|
|
if got := steps[0].Get("type").String(); got != "thought" {
|
|
t.Fatalf("steps.0.type = %q, want thought. Output: %s", got, string(out))
|
|
}
|
|
if got := steps[0].Get("signature").String(); got != metaSig {
|
|
t.Fatalf("steps.0.signature = %q, want Meta signature verbatim. Output: %s", got, string(out))
|
|
}
|
|
if got := steps[0].Get("content.0.text").String(); got != "thinking hard" {
|
|
t.Fatalf("steps.0.content.0.text = %q, want thinking hard. Output: %s", got, string(out))
|
|
}
|
|
if got := steps[1].Get("signature").String(); got != routedSig {
|
|
t.Fatalf("steps.1.signature = %q, want routed Meta signature verbatim. Output: %s", got, string(out))
|
|
}
|
|
if steps[1].Get("content").Exists() {
|
|
t.Fatalf("steps.1 should carry no content when there is no text. Output: %s", string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertCodexResponseToInteractionsStream_ReasoningSignature(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
var param any
|
|
var events [][]byte
|
|
for _, chunk := range [][]byte{
|
|
[]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"codex-test"}}`),
|
|
[]byte(`data: {"type":"response.reasoning_summary_text.delta","delta":"streamed thinking"}`),
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"streamed thinking"}],"encrypted_content":"` + metaSig + `"}}`),
|
|
// A duplicated done event for the same item must not re-emit the signature or summary.
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"id":"rs_1","type":"reasoning","summary":[{"type":"summary_text","text":"streamed thinking"}],"encrypted_content":"` + metaSig + `"}}`),
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"type":"message","content":[{"type":"output_text","text":"answer"}]}}`),
|
|
} {
|
|
events = append(events, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m)...)
|
|
}
|
|
|
|
starts := findCodexInteractionsEventPayloads(events, "step.start")
|
|
if len(starts) != 2 {
|
|
t.Fatalf("step.start count = %d, want thought + model_output. Events: %q", len(starts), events)
|
|
}
|
|
if got := starts[0].Get("step.type").String(); got != "thought" {
|
|
t.Fatalf("step.start[0] type = %q, want thought", got)
|
|
}
|
|
if got := starts[1].Get("step.type").String(); got != "model_output" {
|
|
t.Fatalf("step.start[1] type = %q, want model_output", got)
|
|
}
|
|
|
|
signatureDeltas := 0
|
|
for _, delta := range findCodexInteractionsEventPayloads(events, "step.delta") {
|
|
if delta.Get("delta.type").String() != "thought_signature" {
|
|
continue
|
|
}
|
|
signatureDeltas++
|
|
if got := delta.Get("delta.signature").String(); got != metaSig {
|
|
t.Fatalf("thought_signature delta signature = %q, want Meta signature verbatim", got)
|
|
}
|
|
}
|
|
if signatureDeltas != 1 {
|
|
t.Fatalf("thought_signature delta count = %d, want 1 (duplicate suppressed)", signatureDeltas)
|
|
}
|
|
summaryDeltas := 0
|
|
for _, delta := range findCodexInteractionsEventPayloads(events, "step.delta") {
|
|
if delta.Get("delta.type").String() == "thought_summary" {
|
|
summaryDeltas++
|
|
}
|
|
}
|
|
if summaryDeltas != 1 {
|
|
t.Fatalf("thought_summary delta count = %d, want 1 (output_item.done must not duplicate streamed summary)", summaryDeltas)
|
|
}
|
|
|
|
names := strings.Join(codexInteractionsEventNames(events), ",")
|
|
want := "interaction.created,interaction.status_update,step.start,step.delta,step.delta,step.stop,step.start,step.delta,step.stop"
|
|
if names != want {
|
|
t.Fatalf("events = %s, want %s", names, want)
|
|
}
|
|
|
|
// A second done-only reasoning item after a streamed-delta reasoning item must still emit its summary.
|
|
var param2 any
|
|
var events2 [][]byte
|
|
for _, chunk := range [][]byte{
|
|
[]byte(`data: {"type":"response.created","response":{"id":"resp_2","model":"codex-test"}}`),
|
|
[]byte(`data: {"type":"response.reasoning_summary_text.delta","delta":"first summary"}`),
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"id":"rs_a","type":"reasoning","summary":[{"type":"summary_text","text":"first summary"}],"encrypted_content":"` + metaSig + `"}}`),
|
|
[]byte(`data: {"type":"response.output_item.done","item":{"id":"rs_b","type":"reasoning","summary":[{"type":"summary_text","text":"second summary only in done"}]}}`),
|
|
} {
|
|
events2 = append(events2, ConvertCodexResponseToInteractions(context.Background(), "codex-test", nil, nil, chunk, ¶m2)...)
|
|
}
|
|
var summaries2 []string
|
|
for _, delta := range findCodexInteractionsEventPayloads(events2, "step.delta") {
|
|
if delta.Get("delta.type").String() == "thought_summary" {
|
|
summaries2 = append(summaries2, delta.Get("delta.content.text").String())
|
|
}
|
|
}
|
|
if len(summaries2) != 2 || summaries2[0] != "first summary" || summaries2[1] != "second summary only in done" {
|
|
t.Fatalf("consecutive reasoning summaries = %v, want [first summary, second summary only in done]", summaries2)
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodex_RestoresThoughtSignature(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
routedSig := metaSig + "." + strings.Repeat("Ab3", 23)[:67]
|
|
for _, tc := range []struct {
|
|
name string
|
|
model string
|
|
sig string
|
|
}{
|
|
{"native meta", "muse-spark-1.3", metaSig},
|
|
{"namespaced router model", "meta/muse-wonder", metaSig},
|
|
{"routed meta verbatim", "meta/muse-wonder", routedSig},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
payload := []byte(`{"model":"` + tc.model + `","input":[{"type":"thought","signature":"` + tc.sig + `","content":[{"type":"text","text":"thinking"}]}]}`)
|
|
out, _ := ConvertInteractionsRequestToCodex(tc.model, payload, false)
|
|
item := ojson.GetBytesNoCopy(out, "input.0")
|
|
if got := item.Get("type").String(); got != "reasoning" {
|
|
t.Fatalf("input.0.type = %q, want reasoning. Output: %s", got, string(out))
|
|
}
|
|
if got := item.Get("encrypted_content").String(); got != tc.sig {
|
|
t.Fatalf("encrypted_content = %q, want signature verbatim. Output: %s", got, string(out))
|
|
}
|
|
if got := item.Get("summary.0.text").String(); got != "thinking" {
|
|
t.Fatalf("summary.0.text = %q, want thinking. Output: %s", got, string(out))
|
|
}
|
|
if item.Get("content").Exists() {
|
|
t.Fatalf("replay item must not carry content. Output: %s", string(out))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodex_ThoughtSignatureTargetMismatch(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
payload := []byte(`{"model":"gpt-5.4","input":[{"type":"thought","signature":"` + metaSig + `","content":[{"type":"text","text":"thinking"}],"id":"step_1"}]}`)
|
|
|
|
// A foreign signature is never replayed and the thought step is dropped
|
|
// entirely rather than sent as an unsigned reasoning item.
|
|
out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", payload, false)
|
|
if got := len(ojson.GetBytesNoCopy(out, "input").Array()); got != 0 {
|
|
t.Fatalf("input len = %d, want 0 (mismatched thought dropped). Output: %s", got, string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodex_DropsEmptyThoughtSteps(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
|
|
// A rejected signature with no text emits no item at all: a bare
|
|
// reasoning item carries nothing replayable and is rejected upstream.
|
|
out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", []byte(`{"input":[{"type":"user_input","content":[{"type":"text","text":"hi"}]},{"type":"thought","signature":"`+metaSig+`"}]}`), false)
|
|
if got := len(ojson.GetBytesNoCopy(out, "input").Array()); got != 1 {
|
|
t.Fatalf("input len = %d, want 1 (thought step dropped). Output: %s", got, string(out))
|
|
}
|
|
|
|
// A signatureless, textless thought step emits no item.
|
|
out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought"}]}`), false)
|
|
if got := len(ojson.GetBytesNoCopy(out, "input").Array()); got != 0 {
|
|
t.Fatalf("input len = %d, want 0. Output: %s", got, string(out))
|
|
}
|
|
|
|
// A duplicate accepted signature with no text emits nothing the second
|
|
// time either.
|
|
out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","signature":"`+metaSig+`"},{"type":"thought","signature":"`+metaSig+`"}]}`), false)
|
|
if got := len(ojson.GetBytesNoCopy(out, "input").Array()); got != 1 {
|
|
t.Fatalf("input len = %d, want 1 (duplicate without text dropped). Output: %s", got, string(out))
|
|
}
|
|
|
|
// A signatureless thought with text is dropped too, and the message/tool
|
|
// items around it keep their order.
|
|
out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"user_input","content":[{"type":"text","text":"hi"}]},{"type":"thought","content":[{"type":"text","text":"thinking"}]},{"type":"function_call","name":"lookup","call_id":"call_1","arguments":{"q":"x"}}]}`), false)
|
|
items := ojson.GetBytesNoCopy(out, "input").Array()
|
|
if len(items) != 2 || items[0].Get("type").String() != "message" || items[1].Get("type").String() != "function_call" {
|
|
t.Fatalf("input = %s, want [message, function_call] with the thought dropped", items)
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodex_ThoughtSignatureVariants(t *testing.T) {
|
|
metaSig := testInteractionsMetaSignature()
|
|
gptSig := testInteractionsGPTSignature()
|
|
|
|
// A cache-prefixed signature normalizes to its native payload.
|
|
out, _ := ConvertInteractionsRequestToCodex("gpt-5.4", []byte(`{"input":[{"type":"thought","signature":"gpt#`+gptSig+`"}]}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.encrypted_content").String(); got != gptSig {
|
|
t.Fatalf("prefixed GPT signature should normalize to native payload. Output: %s", string(out))
|
|
}
|
|
|
|
// The snake_case thought_signature spelling is accepted too.
|
|
out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","thought_signature":"`+metaSig+`"}]}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.encrypted_content").String(); got != metaSig {
|
|
t.Fatalf("thought_signature field should be accepted. Output: %s", string(out))
|
|
}
|
|
|
|
// An identical signature replays once; the duplicate thought is dropped.
|
|
out, _ = ConvertInteractionsRequestToCodex("muse-spark-1.3", []byte(`{"input":[{"type":"thought","signature":"`+metaSig+`","content":[{"type":"text","text":"one"}]},{"type":"thought","signature":"`+metaSig+`","content":[{"type":"text","text":"two"}]}]}`), false)
|
|
if got := len(ojson.GetBytesNoCopy(out, "input").Array()); got != 1 {
|
|
t.Fatalf("input len = %d, want 1 (duplicate dropped). Output: %s", got, string(out))
|
|
}
|
|
if got := ojson.GetBytesNoCopy(out, "input.0.encrypted_content").String(); got != metaSig {
|
|
t.Fatalf("first thought should carry the signature. Output: %s", string(out))
|
|
}
|
|
}
|
|
|
|
func TestConvertInteractionsRequestToCodex_IncludeEncryptedContent(t *testing.T) {
|
|
out, _ := ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi"}`), false)
|
|
if got := ojson.GetBytesNoCopy(out, "include.0").String(); got != "reasoning.encrypted_content" {
|
|
t.Fatalf("include.0 = %q, want reasoning.encrypted_content. Output: %s", got, string(out))
|
|
}
|
|
|
|
out, _ = ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi","include":["web_search_call.action.sources"]}`), false)
|
|
include := ojson.GetBytesNoCopy(out, "include").Array()
|
|
if len(include) != 2 || include[0].String() != "web_search_call.action.sources" || include[1].String() != "reasoning.encrypted_content" {
|
|
t.Fatalf("include = %s, want sources + reasoning.encrypted_content. Output: %s", include, string(out))
|
|
}
|
|
|
|
out, _ = ConvertInteractionsRequestToCodex("codex-test", []byte(`{"input":"hi","include":["reasoning.encrypted_content"]}`), false)
|
|
if include := ojson.GetBytesNoCopy(out, "include").Array(); len(include) != 1 {
|
|
t.Fatalf("include = %s, want unchanged single entry. Output: %s", include, string(out))
|
|
}
|
|
}
|
|
|
|
func findCodexInteractionsEventPayload(events [][]byte, eventType string) []byte {
|
|
prefix := []byte("data:")
|
|
for _, event := range events {
|
|
eventName := codexInteractionsFrameEventName(event)
|
|
for _, line := range bytes.Split(event, []byte("\n")) {
|
|
line = bytes.TrimSpace(line)
|
|
if !bytes.HasPrefix(line, prefix) {
|
|
continue
|
|
}
|
|
payload := bytes.TrimSpace(line[len(prefix):])
|
|
if codexInteractionsEventName(eventName, payload) == eventType {
|
|
return payload
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func codexInteractionsEventNames(events [][]byte) []string {
|
|
names := make([]string, 0, len(events))
|
|
for _, event := range events {
|
|
eventName := codexInteractionsFrameEventName(event)
|
|
for _, line := range bytes.Split(event, []byte("\n")) {
|
|
line = bytes.TrimSpace(line)
|
|
if !bytes.HasPrefix(line, []byte("data:")) {
|
|
continue
|
|
}
|
|
payload := bytes.TrimSpace(line[len("data:"):])
|
|
if name := codexInteractionsEventName(eventName, payload); name != "" {
|
|
names = append(names, name)
|
|
}
|
|
}
|
|
}
|
|
return names
|
|
}
|
|
|
|
func codexInteractionsEventName(eventName string, payload []byte) string {
|
|
if eventType := ojson.GetBytesNoCopy(payload, "event_type").String(); eventType != "" {
|
|
return eventType
|
|
}
|
|
if eventType := ojson.GetBytesNoCopy(payload, "type").String(); eventType != "" {
|
|
return eventType
|
|
}
|
|
return eventName
|
|
}
|
|
|
|
func codexInteractionsFrameEventName(event []byte) string {
|
|
for _, line := range bytes.Split(event, []byte("\n")) {
|
|
line = bytes.TrimSpace(line)
|
|
if bytes.HasPrefix(line, []byte("event:")) {
|
|
return strings.TrimSpace(string(line[len("event:"):]))
|
|
}
|
|
}
|
|
return ""
|
|
}
|