try help json
Browse files
src/lib/components/PicletGenerator/PicletGenerator.svelte
CHANGED
@@ -707,13 +707,23 @@ Write your response within \`\`\`json\`\`\``;
|
|
707 |
// Get the last message (assistant's response)
|
708 |
const lastMessage = chatHistory[chatHistory.length - 1];
|
709 |
|
|
|
|
|
710 |
if (lastMessage && lastMessage.content && Array.isArray(lastMessage.content)) {
|
711 |
-
// Extract text content from the message
|
712 |
const textContents = lastMessage.content
|
713 |
.filter((item: any) => item.type === "text")
|
714 |
-
.map((item: any) =>
|
715 |
-
|
|
|
|
|
|
|
716 |
responseText = textContents || "Response received but no text content found";
|
|
|
|
|
|
|
|
|
|
|
717 |
} else if (lastMessage && lastMessage.role === "assistant") {
|
718 |
// Fallback - if content structure is different
|
719 |
responseText = JSON.stringify(lastMessage, null, 2);
|
@@ -742,12 +752,45 @@ Write your response within \`\`\`json\`\`\``;
|
|
742 |
}
|
743 |
|
744 |
try {
|
745 |
-
// Remove any trailing text after the JSON object
|
746 |
-
|
747 |
if (jsonMatch) {
|
748 |
cleanJson = jsonMatch[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
749 |
}
|
750 |
|
|
|
|
|
751 |
const parsedStats = JSON.parse(cleanJson.trim());
|
752 |
|
753 |
// Validate the battle-ready monster structure
|
|
|
707 |
// Get the last message (assistant's response)
|
708 |
const lastMessage = chatHistory[chatHistory.length - 1];
|
709 |
|
710 |
+
console.log('Full message structure:', JSON.stringify(lastMessage, null, 2));
|
711 |
+
|
712 |
if (lastMessage && lastMessage.content && Array.isArray(lastMessage.content)) {
|
713 |
+
// Extract ALL text content from the message more robustly
|
714 |
const textContents = lastMessage.content
|
715 |
.filter((item: any) => item.type === "text")
|
716 |
+
.map((item: any) => {
|
717 |
+
console.log('Content item:', item);
|
718 |
+
return item.content || '';
|
719 |
+
})
|
720 |
+
.join(""); // Join without separator to avoid breaking JSON
|
721 |
responseText = textContents || "Response received but no text content found";
|
722 |
+
console.log('Extracted text length:', responseText.length);
|
723 |
+
console.log('Extracted text preview:', responseText.substring(0, 200) + '...');
|
724 |
+
} else if (lastMessage && typeof lastMessage === 'string') {
|
725 |
+
// Handle case where the message is a plain string
|
726 |
+
responseText = lastMessage;
|
727 |
} else if (lastMessage && lastMessage.role === "assistant") {
|
728 |
// Fallback - if content structure is different
|
729 |
responseText = JSON.stringify(lastMessage, null, 2);
|
|
|
752 |
}
|
753 |
|
754 |
try {
|
755 |
+
// Remove any trailing text after the JSON object
|
756 |
+
let jsonMatch = cleanJson.match(/^\s*\{[\s\S]*?\}\s*/);
|
757 |
if (jsonMatch) {
|
758 |
cleanJson = jsonMatch[0];
|
759 |
+
} else {
|
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 opening brace
|
764 |
+
const startIndex = cleanJson.indexOf('{');
|
765 |
+
if (startIndex !== -1) {
|
766 |
+
cleanJson = cleanJson.substring(startIndex);
|
767 |
+
console.log('Partial JSON found:', cleanJson.substring(0, 300) + '...');
|
768 |
+
|
769 |
+
// Try to balance braces if the JSON is incomplete
|
770 |
+
let braceCount = 0;
|
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) + '...');
|
793 |
+
|
794 |
const parsedStats = JSON.parse(cleanJson.trim());
|
795 |
|
796 |
// Validate the battle-ready monster structure
|
src/lib/components/PicletGenerator/WorkflowProgress.svelte
CHANGED
@@ -22,6 +22,11 @@
|
|
22 |
},
|
23 |
{
|
24 |
id: 'captioning',
|
|
|
|
|
|
|
|
|
|
|
25 |
label: 'Piclet Design',
|
26 |
description: 'Creating concept & lore'
|
27 |
},
|
@@ -30,6 +35,11 @@
|
|
30 |
label: 'Battle Stats',
|
31 |
description: 'Generating abilities'
|
32 |
},
|
|
|
|
|
|
|
|
|
|
|
33 |
{
|
34 |
id: 'generating',
|
35 |
label: 'Image Generation',
|
|
|
22 |
},
|
23 |
{
|
24 |
id: 'captioning',
|
25 |
+
label: 'Image Analysis',
|
26 |
+
description: 'Analyzing your photo'
|
27 |
+
},
|
28 |
+
{
|
29 |
+
id: 'conceptualizing',
|
30 |
label: 'Piclet Design',
|
31 |
description: 'Creating concept & lore'
|
32 |
},
|
|
|
35 |
label: 'Battle Stats',
|
36 |
description: 'Generating abilities'
|
37 |
},
|
38 |
+
{
|
39 |
+
id: 'promptCrafting',
|
40 |
+
label: 'Art Planning',
|
41 |
+
description: 'Preparing visual prompt'
|
42 |
+
},
|
43 |
{
|
44 |
id: 'generating',
|
45 |
label: 'Image Generation',
|