diff --git a/internal/gateway/pi_responses_test.go b/internal/gateway/pi_responses_test.go index 1830a6d27..176909c41 100644 --- a/internal/gateway/pi_responses_test.go +++ b/internal/gateway/pi_responses_test.go @@ -92,7 +92,7 @@ func TestPiResponsesRelayedToChatGPT(t *testing.T) { auth := fmt.Sprintf(`{"auth_mode":"chatgpt","tokens":{"id_token":%q,"access_token":%q,"refresh_token":"r","account_id":"acct-1"}}`, claims(map[string]any{"email": "me@example.com"}), claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()})) os.WriteFile(filepath.Join(home, ".codex", "auth.json"), []byte(auth), 0o600) - f := &fake{t: t, ctype: "none", reply: piReply} + f := &fake{t: t, ctype: "none", reply: piReply, refuse: codexBackendRefuses} up := httptest.NewServer(f) defer up.Close() old := provider.CodexBase @@ -112,6 +112,29 @@ func TestPiResponsesRelayedToChatGPT(t *testing.T) { } } +// codexBackendRefuses answers as the ChatGPT backend does an input item +// with a parameter its kind doesn't have: a message without its id is an +// input message, and an input message has no status (Doby on Discord, Pi +// 1.4.0 going on with a conversation: 400 "Unknown parameter: +// 'input[8].status'"). +func codexBackendRefuses(body []byte) (int, string) { + var q struct { + Input []map[string]any `json:"input"` + } + if json.Unmarshal(body, &q) != nil { + return 400, `{"error":{"message":"bad json","type":"invalid_request_error"}}` + } + for i, it := range q.Input { + _, id := it["id"] + _, status := it["status"] + if typ := it["type"]; status && !id && (typ == nil || typ == "message") { + p := fmt.Sprintf("input[%d].status", i) + return 400, fmt.Sprintf(`{"error":{"message":"Unknown parameter: '%s'.","type":"invalid_request_error","param":"%s","code":"unknown_parameter"}}`, p, p) + } + } + return 0, "" +} + // DeepSeek Harness on magpie's route switched to OpenAI Responses (its // llm-pi-ai plugin is pi-ai's openai-responses, so it asks as Pi does) // reaches a model served on Chat Completions alone, a DeepSeek one: the diff --git a/internal/provider/codex_request.go b/internal/provider/codex_request.go index 0f9e67299..89e15e164 100644 --- a/internal/provider/codex_request.go +++ b/internal/provider/codex_request.go @@ -380,7 +380,8 @@ func BoundCallID(id string) string { } // codexInput makes input items fit a request that keeps nothing: no -// references to stored items, no ids, a tool's output without its call +// references to stored items, no ids (nor a message's status, which goes +// with its id), a tool's output without its call // kept as a message saying what it was, and no system role, which the // backend rejects (developer keeps what it means in its place). func codexInput(input []any) []any { @@ -422,6 +423,14 @@ func codexInput(input []any) []any { continue } delete(it, "id") + // A message's status goes only with its id: without one, the + // backend reads it as an input message, which has no status, and + // turns the request away 400 "Unknown parameter: 'input[N].status'" + // (Doby on Discord: Pi gives each assistant message it sends again + // status "completed"). Codex's own reasoning items carry none. + if t := it["type"]; t == nil || t == "message" || t == "reasoning" { + delete(it, "status") + } switch t, _ := it["type"].(string); t { case "function_call_output", "custom_tool_call_output", "local_shell_call_output": id, _ := it["call_id"].(string) diff --git a/internal/provider/codex_request_test.go b/internal/provider/codex_request_test.go index 8567a7ebf..880088d44 100644 --- a/internal/provider/codex_request_test.go +++ b/internal/provider/codex_request_test.go @@ -375,3 +375,31 @@ func TestCodexBodyDropsPromptCacheOptions(t *testing.T) { t.Errorf("cache key lost: %v", m) } } + +// A message's status goes with its id, and Codex's reasoning items carry +// none, so both lose it with the id (Doby on Discord: Pi's assistant +// message sent again with status "completed" was refused 400 "Unknown +// parameter: 'input[8].status'"). A tool call keeps its status, as Codex +// sends it. +func TestCodexInputDropsStatusWithID(t *testing.T) { + in := []any{} + json.Unmarshal([]byte(`[ + {"role":"user","content":"hi","status":"completed"}, + {"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"e","status":"completed"}, + {"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok","annotations":[]}],"status":"completed","id":"msg_1"}, + {"type":"function_call","id":"fc_1","call_id":"c1","name":"ls","arguments":"{}","status":"completed"}, + {"type":"local_shell_call","id":"lsh_1","call_id":"c2","status":"completed","action":{"type":"exec","command":["ls"]}}, + {"type":"function_call_output","call_id":"c1","output":"a"}, + {"type":"local_shell_call_output","call_id":"c2","output":"b"}]`), &in) + out := codexInput(in) + want := []bool{false, false, false, true, true, false, false} + if len(out) != len(want) { + t.Fatalf("items: %v", out) + } + for i, item := range out { + _, has := item.(map[string]any)["status"] + if has != want[i] { + t.Errorf("input[%d] status kept %v, want %v: %v", i, has, want[i], item) + } + } +}