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:
decolua
2026-06-13 16:45:10 +07:00
parent 87fe069e9e
commit b87cf0c96a
4 changed files with 12 additions and 19 deletions

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

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; }
}

View File

@@ -2,6 +2,7 @@ import { register } from "../index.js";
import { FORMATS } from "../formats.js";
import { CLAUDE_SYSTEM_PROMPT } from "../../config/appConstants.js";
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
import { safeParseJSON } from "../helpers/jsonUtil.js";
// Empty prefix matches real Claude Code behavior (no tool name prefix).
// Previously "proxy_" was used but this is a detectable fingerprint difference.
@@ -281,7 +282,7 @@ function getContentBlocksFromMessage(msg, toolNameMap = new Map()) {
type: "tool_use",
id: tc.id,
name: toolName,
input: tryParseJSON(tc.function.arguments)
input: safeParseJSON(tc.function.arguments, tc.function.arguments)
});
}
}
@@ -332,16 +333,6 @@ function extractTextContent(content) {
return "";
}
// Try parse JSON
function tryParseJSON(str) {
if (typeof str !== "string") return str;
try {
return JSON.parse(str);
} catch {
return str;
}
}
// OpenAI -> Claude format for Antigravity (without system prompt modifications)
function openaiToClaudeRequestForAntigravity(model, body, stream) {
const result = openaiToClaudeRequest(model, body, stream);

File diff suppressed because one or more lines are too long