This change makes the Claude Code SDK package optional, preventing installation failures for users who don't need Claude Code functionality. Changes: - Added @anthropic-ai/claude-code to optionalDependencies in package.json - Implemented lazy loading in language-model.js to only import the SDK when actually used - Updated documentation to explain the optional installation requirement - Applied formatting fixes to ensure code consistency Benefits: - Users without Claude Code subscriptions don't need to install the dependency - Reduces package size for users who don't use Claude Code - Prevents installation failures if the package is unavailable - Provides clear error messages when the package is needed but not installed The implementation uses dynamic imports to load the SDK only when doGenerate() or doStream() is called, ensuring the provider can be instantiated without the package present.
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
/**
|
|
* @fileoverview Extract JSON from Claude's response, handling markdown blocks and other formatting
|
|
*/
|
|
|
|
/**
|
|
* Extract JSON from Claude's response
|
|
* @param {string} text - The text to extract JSON from
|
|
* @returns {string} - The extracted JSON string
|
|
*/
|
|
export function extractJson(text) {
|
|
// Remove markdown code blocks if present
|
|
let jsonText = text.trim();
|
|
|
|
// Remove ```json blocks
|
|
jsonText = jsonText.replace(/^```json\s*/gm, '');
|
|
jsonText = jsonText.replace(/^```\s*/gm, '');
|
|
jsonText = jsonText.replace(/```\s*$/gm, '');
|
|
|
|
// Remove common TypeScript/JavaScript patterns
|
|
jsonText = jsonText.replace(/^const\s+\w+\s*=\s*/, ''); // Remove "const varName = "
|
|
jsonText = jsonText.replace(/^let\s+\w+\s*=\s*/, ''); // Remove "let varName = "
|
|
jsonText = jsonText.replace(/^var\s+\w+\s*=\s*/, ''); // Remove "var varName = "
|
|
jsonText = jsonText.replace(/;?\s*$/, ''); // Remove trailing semicolons
|
|
|
|
// Try to extract JSON object or array
|
|
const objectMatch = jsonText.match(/{[\s\S]*}/);
|
|
const arrayMatch = jsonText.match(/\[[\s\S]*\]/);
|
|
|
|
if (objectMatch) {
|
|
jsonText = objectMatch[0];
|
|
} else if (arrayMatch) {
|
|
jsonText = arrayMatch[0];
|
|
}
|
|
|
|
// First try to parse as valid JSON
|
|
try {
|
|
JSON.parse(jsonText);
|
|
return jsonText;
|
|
} catch {
|
|
// If it's not valid JSON, it might be a JavaScript object literal
|
|
// Try to convert it to valid JSON
|
|
try {
|
|
// This is a simple conversion that handles basic cases
|
|
// Replace unquoted keys with quoted keys
|
|
const converted = jsonText
|
|
.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":')
|
|
// Replace single quotes with double quotes
|
|
.replace(/'/g, '"');
|
|
|
|
// Validate the converted JSON
|
|
JSON.parse(converted);
|
|
return converted;
|
|
} catch {
|
|
// If all else fails, return the original text
|
|
// The AI SDK will handle the error appropriately
|
|
return text;
|
|
}
|
|
}
|
|
}
|