refactor(open-sse): extract safeParseJSON util, dedup tryParseJSON (B5)

Consolidate two tryParseJSON variants into helpers/jsonUtil.js with explicit
fallback param. Preserve exact per-call semantics: openai-to-claude passthrough
(fallback=str), geminiHelper null. geminiHelper keeps tryParseJSON re-export.
No behavior change; gate: no regression.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decoluaandCursor committed 2026-06-13 16:45:10 +07:00
1 parent 87fe069e9e
commit b87cf0c96a
4 files changed
+12 -19

No files matched your search

+4 -7
View File
@@ -1,5 +1,7 @@
// Gemini helper functions for translator
import { safeParseJSON } from "./jsonUtil.js";
// Unsupported JSON Schema constraints that should be removed for Antigravity
export const UNSUPPORTED_SCHEMA_CONSTRAINTS = [
// Basic constraints (not supported by Gemini API)
@@ -90,14 +92,9 @@ export function extractTextContent(content) {
return "";
}
// Try parse JSON safely
// Try parse JSON safely (null fallback on parse error; re-export keeps legacy API)
export function tryParseJSON(str) {
if (typeof str !== "string") return str;
try {
return JSON.parse(str);
} catch {
return null;
}
return safeParseJSON(str, null);
}
// Generate request ID
+5
View File
@@ -0,0 +1,5 @@
// Safe JSON.parse: non-string passthrough; on parse error return caller-chosen `fallback`.
export function safeParseJSON(str, fallback) {
if (typeof str !== "string") return str;
try { return JSON.parse(str); } catch { return fallback; }
}