fix json extraction
Browse files
src/lib/components/PicletGenerator/PicletGenerator.svelte
CHANGED
|
@@ -752,41 +752,32 @@ Write your response within \`\`\`json\`\`\``;
|
|
| 752 |
}
|
| 753 |
|
| 754 |
try {
|
| 755 |
-
//
|
| 756 |
-
|
| 757 |
-
if (
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
// If no complete JSON found, try to find the start and attempt to complete it
|
| 761 |
-
console.warn('No complete JSON found, attempting recovery...');
|
| 762 |
|
| 763 |
-
// Find the
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
cleanJson
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
let lastValidIndex = -1;
|
| 772 |
-
for (let i = 0; i < cleanJson.length; i++) {
|
| 773 |
-
if (cleanJson[i] === '{') braceCount++;
|
| 774 |
-
if (cleanJson[i] === '}') {
|
| 775 |
-
braceCount--;
|
| 776 |
-
if (braceCount === 0) {
|
| 777 |
-
lastValidIndex = i;
|
| 778 |
-
break;
|
| 779 |
-
}
|
| 780 |
}
|
| 781 |
}
|
| 782 |
-
|
| 783 |
-
if (lastValidIndex !== -1) {
|
| 784 |
-
cleanJson = cleanJson.substring(0, lastValidIndex + 1);
|
| 785 |
-
console.log('Balanced JSON extracted');
|
| 786 |
-
} else {
|
| 787 |
-
throw new Error('JSON appears to be truncated - unable to balance braces');
|
| 788 |
-
}
|
| 789 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
}
|
| 791 |
|
| 792 |
console.log('Final JSON to parse (length: ' + cleanJson.length + '):', cleanJson.substring(0, 500) + '...');
|
|
@@ -926,7 +917,7 @@ Write your response within \`\`\`json\`\`\``;
|
|
| 926 |
{#if state.currentStep === 'captioning'}
|
| 927 |
Analyzing your image...
|
| 928 |
{:else if state.currentStep === 'conceptualizing'}
|
| 929 |
-
Creating
|
| 930 |
{:else if state.currentStep === 'statsGenerating'}
|
| 931 |
Generating battle stats...
|
| 932 |
{:else if state.currentStep === 'promptCrafting'}
|
|
|
|
| 752 |
}
|
| 753 |
|
| 754 |
try {
|
| 755 |
+
// Extract JSON by properly balancing braces instead of using regex
|
| 756 |
+
const startIndex = cleanJson.indexOf('{');
|
| 757 |
+
if (startIndex !== -1) {
|
| 758 |
+
let braceCount = 0;
|
| 759 |
+
let endIndex = -1;
|
|
|
|
|
|
|
| 760 |
|
| 761 |
+
// Find the matching closing brace by counting
|
| 762 |
+
for (let i = startIndex; i < cleanJson.length; i++) {
|
| 763 |
+
if (cleanJson[i] === '{') braceCount++;
|
| 764 |
+
if (cleanJson[i] === '}') {
|
| 765 |
+
braceCount--;
|
| 766 |
+
if (braceCount === 0) {
|
| 767 |
+
endIndex = i;
|
| 768 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
}
|
| 770 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
}
|
| 772 |
+
|
| 773 |
+
if (endIndex !== -1) {
|
| 774 |
+
cleanJson = cleanJson.substring(startIndex, endIndex + 1);
|
| 775 |
+
console.log('Balanced JSON extracted, length:', cleanJson.length);
|
| 776 |
+
} else {
|
| 777 |
+
throw new Error('JSON appears to be truncated - unable to balance braces');
|
| 778 |
+
}
|
| 779 |
+
} else {
|
| 780 |
+
throw new Error('No JSON object found in response');
|
| 781 |
}
|
| 782 |
|
| 783 |
console.log('Final JSON to parse (length: ' + cleanJson.length + '):', cleanJson.substring(0, 500) + '...');
|
|
|
|
| 917 |
{#if state.currentStep === 'captioning'}
|
| 918 |
Analyzing your image...
|
| 919 |
{:else if state.currentStep === 'conceptualizing'}
|
| 920 |
+
Creating Piclet concept...
|
| 921 |
{:else if state.currentStep === 'statsGenerating'}
|
| 922 |
Generating battle stats...
|
| 923 |
{:else if state.currentStep === 'promptCrafting'}
|