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 parent
87fe069e9e
commit
b87cf0c96a
4 files changed
+12
-19
No files matched your search
@@ -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
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{"numTotalTestSuites":219,"numPassedTestSuites":202,"numFailedTestSuites":17,"numPendingTestSuites":0,"numTotalTests":823,"numPassedTests":777,"numFailedTests":26,"numPendingTests":20,"numTodoTests":0,"snapshot":{"added":0,"failure":false,"filesAdded":0,"filesRemoved":0,"filesRemovedList":[],"filesUnmatched":0,"filesUpdated":0,"matched":149,"total":149,"unchecked":0,"uncheckedKeysByFile":[],"unmatched":0,"updated":0,"didUpdate":false},"startTime":1781343132874,"success":false,"testResults":[{"aLine truncated
|
{"numTotalTestSuites":219,"numPassedTestSuites":202,"numFailedTestSuites":17,"numPendingTestSuites":0,"numTotalTests":823,"numPassedTests":777,"numFailedTests":26,"numPendingTests":20,"numTodoTests":0,"snapshot":{"added":0,"failure":false,"filesAdded":0,"filesRemoved":0,"filesRemovedList":[],"filesUnmatched":0,"filesUpdated":0,"matched":149,"total":149,"unchecked":0,"uncheckedKeysByFile":[],"unmatched":0,"updated":0,"didUpdate":false},"startTime":1781343752363,"success":false,"testResults":[{"aLine truncated
|
||||||
Reference in new issue
Block a user