Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
// Safe JSON.parse helper shared by the translator layer.
//
// Behavior contract (must be preserved by all callers):
// - non-string input is returned unchanged (passthrough)
// - a valid JSON string is parsed and returned
// - on parse error the caller-chosen `fallback` is returned
//
// The `fallback` is explicit so the two historical `tryParseJSON` variants
// can keep their distinct semantics: geminiHelper returns `null` on error,
// while openai-to-claude returns the raw input string (passthrough).
export function safeParseJSON<TFallback>(str: unknown, fallback: TFallback): unknown {
if (typeof str !== "string") return str;
try {
return JSON.parse(str);
} catch {
return fallback;
}
}