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:
@@ -1,5 +1,7 @@
|
|||||||
// Gemini helper functions for translator
|
// Gemini helper functions for translator
|
||||||
|
|
||||||
|
import { safeParseJSON } from "./jsonUtil.js";
|
||||||
|
|
||||||
// Unsupported JSON Schema constraints that should be removed for Antigravity
|
// Unsupported JSON Schema constraints that should be removed for Antigravity
|
||||||
export const UNSUPPORTED_SCHEMA_CONSTRAINTS = [
|
export const UNSUPPORTED_SCHEMA_CONSTRAINTS = [
|
||||||
// Basic constraints (not supported by Gemini API)
|
// Basic constraints (not supported by Gemini API)
|
||||||
@@ -90,14 +92,9 @@ export function extractTextContent(content) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try parse JSON safely
|
// Try parse JSON safely (null fallback on parse error; re-export keeps legacy API)
|
||||||
export function tryParseJSON(str) {
|
export function tryParseJSON(str) {
|
||||||
if (typeof str !== "string") return str;
|
return safeParseJSON(str, null);
|
||||||
try {
|
|
||||||
return JSON.parse(str);
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate request ID
|
// Generate request ID
|
||||||
|
|||||||
5
open-sse/translator/helpers/jsonUtil.js
Normal file
5
open-sse/translator/helpers/jsonUtil.js
Normal 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; }
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import { register } from "../index.js";
|
|||||||
import { FORMATS } from "../formats.js";
|
import { FORMATS } from "../formats.js";
|
||||||
import { CLAUDE_SYSTEM_PROMPT } from "../../config/appConstants.js";
|
import { CLAUDE_SYSTEM_PROMPT } from "../../config/appConstants.js";
|
||||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||||
|
import { safeParseJSON } from "../helpers/jsonUtil.js";
|
||||||
|
|
||||||
// Empty prefix matches real Claude Code behavior (no tool name prefix).
|
// Empty prefix matches real Claude Code behavior (no tool name prefix).
|
||||||
// Previously "proxy_" was used but this is a detectable fingerprint difference.
|
// Previously "proxy_" was used but this is a detectable fingerprint difference.
|
||||||
@@ -281,7 +282,7 @@ function getContentBlocksFromMessage(msg, toolNameMap = new Map()) {
|
|||||||
type: "tool_use",
|
type: "tool_use",
|
||||||
id: tc.id,
|
id: tc.id,
|
||||||
name: toolName,
|
name: toolName,
|
||||||
input: tryParseJSON(tc.function.arguments)
|
input: safeParseJSON(tc.function.arguments, tc.function.arguments)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -332,16 +333,6 @@ function extractTextContent(content) {
|
|||||||
return "";
|
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)
|
// OpenAI -> Claude format for Antigravity (without system prompt modifications)
|
||||||
function openaiToClaudeRequestForAntigravity(model, body, stream) {
|
function openaiToClaudeRequestForAntigravity(model, body, stream) {
|
||||||
const result = openaiToClaudeRequest(model, body, stream);
|
const result = openaiToClaudeRequest(model, body, stream);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user