mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-22 17:57:46 +00:00
fix: OpenAI tools (#328)
- MCP tool params schema lost causing all MCP not working well - Compatible with occasional llm return tool call parameters that are invalid json
This commit is contained in:
45
packages/core/src/utils/safeJsonParse.ts
Normal file
45
packages/core/src/utils/safeJsonParse.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { jsonrepair } from 'jsonrepair';
|
||||
|
||||
/**
|
||||
* Safely parse JSON string with jsonrepair fallback for malformed JSON.
|
||||
* This function attempts to parse JSON normally first, and if that fails,
|
||||
* it uses jsonrepair to fix common JSON formatting issues before parsing.
|
||||
*
|
||||
* @param jsonString - The JSON string to parse
|
||||
* @param fallbackValue - The value to return if parsing fails completely
|
||||
* @returns The parsed object or the fallback value
|
||||
*/
|
||||
export function safeJsonParse<T = Record<string, unknown>>(
|
||||
jsonString: string,
|
||||
fallbackValue: T = {} as T,
|
||||
): T {
|
||||
if (!jsonString || typeof jsonString !== 'string') {
|
||||
return fallbackValue;
|
||||
}
|
||||
|
||||
try {
|
||||
// First attempt: try normal JSON.parse
|
||||
return JSON.parse(jsonString) as T;
|
||||
} catch (error) {
|
||||
try {
|
||||
// Second attempt: use jsonrepair to fix common JSON issues
|
||||
const repairedJson = jsonrepair(jsonString);
|
||||
|
||||
// jsonrepair always returns a string, so we need to parse it
|
||||
return JSON.parse(repairedJson) as T;
|
||||
} catch (repairError) {
|
||||
console.error('Failed to parse JSON even with jsonrepair:', {
|
||||
originalError: error,
|
||||
repairError,
|
||||
jsonString,
|
||||
});
|
||||
return fallbackValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user