fix(codex): normalize null instructions for compact requests

This commit is contained in:
MonsterQiu
2026-03-30 22:58:05 +08:00
parent d11936f292
commit d3b94c9241
2 changed files with 60 additions and 38 deletions
+2 -1
View File
@@ -220,7 +220,8 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A
body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel) body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel)
body, _ = sjson.SetBytes(body, "model", baseModel) body, _ = sjson.SetBytes(body, "model", baseModel)
body, _ = sjson.DeleteBytes(body, "stream") body, _ = sjson.DeleteBytes(body, "stream")
if !gjson.GetBytes(body, "instructions").Exists() { instructions := gjson.GetBytes(body, "instructions")
if !instructions.Exists() || instructions.Type == gjson.Null {
body, _ = sjson.SetBytes(body, "instructions", "") body, _ = sjson.SetBytes(body, "instructions", "")
} }
@@ -15,44 +15,65 @@ import (
) )
func TestCodexExecutorCompactAddsDefaultInstructions(t *testing.T) { func TestCodexExecutorCompactAddsDefaultInstructions(t *testing.T) {
var gotPath string cases := []struct {
var gotBody []byte name string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { payload string
gotPath = r.URL.Path }{
body, _ := io.ReadAll(r.Body) {
gotBody = body name: "missing instructions",
w.Header().Set("Content-Type", "application/json") payload: `{"model":"gpt-5.4","input":"hello"}`,
_, _ = w.Write([]byte(`{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}`)) },
})) {
defer server.Close() name: "null instructions",
payload: `{"model":"gpt-5.4","instructions":null,"input":"hello"}`,
},
}
executor := NewCodexExecutor(&config.Config{}) for _, tc := range cases {
auth := &cliproxyauth.Auth{Attributes: map[string]string{ t.Run(tc.name, func(t *testing.T) {
"base_url": server.URL, var gotPath string
"api_key": "test", var gotBody []byte
}} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}`))
}))
defer server.Close()
resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ executor := NewCodexExecutor(&config.Config{})
Model: "gpt-5.4", auth := &cliproxyauth.Auth{Attributes: map[string]string{
Payload: []byte(`{"model":"gpt-5.4","input":"hello"}`), "base_url": server.URL,
}, cliproxyexecutor.Options{ "api_key": "test",
SourceFormat: sdktranslator.FromString("openai-response"), }}
Alt: "responses/compact",
Stream: false, resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
}) Model: "gpt-5.4",
if err != nil { Payload: []byte(tc.payload),
t.Fatalf("Execute error: %v", err) }, cliproxyexecutor.Options{
} SourceFormat: sdktranslator.FromString("openai-response"),
if gotPath != "/responses/compact" { Alt: "responses/compact",
t.Fatalf("path = %q, want %q", gotPath, "/responses/compact") Stream: false,
} })
if !gjson.GetBytes(gotBody, "instructions").Exists() { if err != nil {
t.Fatalf("expected instructions in compact request body, got %s", string(gotBody)) t.Fatalf("Execute error: %v", err)
} }
if gjson.GetBytes(gotBody, "instructions").String() != "" { if gotPath != "/responses/compact" {
t.Fatalf("instructions = %q, want empty string", gjson.GetBytes(gotBody, "instructions").String()) t.Fatalf("path = %q, want %q", gotPath, "/responses/compact")
} }
if string(resp.Payload) != `{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}` { if !gjson.GetBytes(gotBody, "instructions").Exists() {
t.Fatalf("payload = %s", string(resp.Payload)) t.Fatalf("expected instructions in compact request body, got %s", string(gotBody))
}
if gjson.GetBytes(gotBody, "instructions").Type != gjson.String {
t.Fatalf("instructions type = %v, want string", gjson.GetBytes(gotBody, "instructions").Type)
}
if gjson.GetBytes(gotBody, "instructions").String() != "" {
t.Fatalf("instructions = %q, want empty string", gjson.GetBytes(gotBody, "instructions").String())
}
if string(resp.Payload) != `{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}` {
t.Fatalf("payload = %s", string(resp.Payload))
}
})
} }
} }