From b37b257e63168d1b7b72ec8bddab75d17c89db25 Mon Sep 17 00:00:00 2001 From: Suyash-K <114049661+Suyash-K@users.noreply.github.com> Date: Sat, 19 Apr 2025 19:54:29 +0530 Subject: [PATCH] gracefully handle SSE parse errors and suppress raw parser code (#367) Closes #187 Closes #358 --------- Co-authored-by: Thibault Sottiaux --- codex-cli/src/utils/agent/agent-loop.ts | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/codex-cli/src/utils/agent/agent-loop.ts b/codex-cli/src/utils/agent/agent-loop.ts index 044715df9..cbe3c5696 100644 --- a/codex-cli/src/utils/agent/agent-loop.ts +++ b/codex-cli/src/utils/agent/agent-loop.ts @@ -791,6 +791,41 @@ export class AgentLoop { this.onLoading(false); return; } + // Suppress internal stack on JSON parse failures + if (err instanceof SyntaxError) { + this.onItem({ + id: `error-${Date.now()}`, + type: "message", + role: "system", + content: [ + { + type: "input_text", + text: "⚠️ Failed to parse streaming response (invalid JSON). Please `/clear` to reset.", + }, + ], + }); + this.onLoading(false); + return; + } + // Handle OpenAI API quota errors + if ( + err instanceof Error && + (err as { code?: string }).code === "insufficient_quota" + ) { + this.onItem({ + id: `error-${Date.now()}`, + type: "message", + role: "system", + content: [ + { + type: "input_text", + text: "⚠️ Insufficient quota. Please check your billing details and retry.", + }, + ], + }); + this.onLoading(false); + return; + } throw err; } finally { this.currentStream = null;