fix(codex): add default instructions for /responses/compact
Unified · Split
| @@ -219,6 +219,10 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A · 218 unmodified lines | |||
| 219 | body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel) | 219 | body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel) |
| 220 | body, _ = sjson.SetBytes(body, "model", baseModel) | 220 | body, _ = sjson.SetBytes(body, "model", baseModel) |
| 221 | body, _ = sjson.DeleteBytes(body, "stream") | 221 | body, _ = sjson.DeleteBytes(body, "stream") |
| 222 | instructions := gjson.GetBytes(body, "instructions") | ||
| 223 | if !instructions.Exists() || instructions.Type == gjson.Null { | ||
| 224 | body, _ = sjson.SetBytes(body, "instructions", "") | ||
| 225 | } | ||
| 222 | 226 | ||
| 223 | url := strings.TrimSuffix(baseURL, "/") + "/responses/compact" | 227 | url := strings.TrimSuffix(baseURL, "/") + "/responses/compact" |
| 224 | httpReq, err := e.cacheHelper(ctx, from, url, req, body) | 228 | httpReq, err := e.cacheHelper(ctx, from, url, req, body) |
| @@ -0,0 +1,79 @@ | |||
| 1 | package executor | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "io" | ||
| 6 | "net/http" | ||
| 7 | "net/http/httptest" | ||
| 8 | "testing" | ||
| 9 | |||
| 10 | "github.com/router-for-me/CLIProxyAPI/v6/internal/config" | ||
| 11 | cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" | ||
| 12 | cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" | ||
| 13 | sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" | ||
| 14 | "github.com/tidwall/gjson" | ||
| 15 | ) | ||
| 16 | |||
| 17 | func TestCodexExecutorCompactAddsDefaultInstructions(t *testing.T) { | ||
| 18 | cases := []struct { | ||
| 19 | name string | ||
| 20 | payload string | ||
| 21 | }{ | ||
| 22 | { | ||
| 23 | name: "missing instructions", | ||
| 24 | payload: `{"model":"gpt-5.4","input":"hello"}`, | ||
| 25 | }, | ||
| 26 | { | ||
| 27 | name: "null instructions", | ||
| 28 | payload: `{"model":"gpt-5.4","instructions":null,"input":"hello"}`, | ||
| 29 | }, | ||
| 30 | } | ||
| 31 | |||
| 32 | for _, tc := range cases { | ||
| 33 | t.Run(tc.name, func(t *testing.T) { | ||
| 34 | var gotPath string | ||
| 35 | var gotBody []byte | ||
| 36 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| 37 | gotPath = r.URL.Path | ||
| 38 | body, _ := io.ReadAll(r.Body) | ||
| 39 | gotBody = body | ||
| 40 | w.Header().Set("Content-Type", "application/json") | ||
| 41 | _, _ = w.Write([]byte(`{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}`)) | ||
| 42 | })) | ||
| 43 | defer server.Close() | ||
| 44 | |||
| 45 | executor := NewCodexExecutor(&config.Config{}) | ||
| 46 | auth := &cliproxyauth.Auth{Attributes: map[string]string{ | ||
| 47 | "base_url": server.URL, | ||
| 48 | "api_key": "test", | ||
| 49 | }} | ||
| 50 | |||
| 51 | resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ | ||
| 52 | Model: "gpt-5.4", | ||
| 53 | Payload: []byte(tc.payload), | ||
| 54 | }, cliproxyexecutor.Options{ | ||
| 55 | SourceFormat: sdktranslator.FromString("openai-response"), | ||
| 56 | Alt: "responses/compact", | ||
| 57 | Stream: false, | ||
| 58 | }) | ||
| 59 | if err != nil { | ||
| 60 | t.Fatalf("Execute error: %v", err) | ||
| 61 | } | ||
| 62 | if gotPath != "/responses/compact" { | ||
| 63 | t.Fatalf("path = %q, want %q", gotPath, "/responses/compact") | ||
| 64 | } | ||
| 65 | if !gjson.GetBytes(gotBody, "instructions").Exists() { | ||
| 66 | t.Fatalf("expected instructions in compact request body, got %s", string(gotBody)) | ||
| 67 | } | ||
| 68 | if gjson.GetBytes(gotBody, "instructions").Type != gjson.String { | ||
| 69 | t.Fatalf("instructions type = %v, want string", gjson.GetBytes(gotBody, "instructions").Type) | ||
| 70 | } | ||
| 71 | if gjson.GetBytes(gotBody, "instructions").String() != "" { | ||
| 72 | t.Fatalf("instructions = %q, want empty string", gjson.GetBytes(gotBody, "instructions").String()) | ||
| 73 | } | ||
| 74 | if string(resp.Payload) != `{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}` { | ||
| 75 | t.Fatalf("payload = %s", string(resp.Payload)) | ||
| 76 | } | ||
| 77 | }) | ||
| 78 | } | ||
| 79 | } | ||