Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

recover code-section answers that a local model closed with stray or missing JSON

[._.]/ Adam Eivyac40900dfef6
diff --git a/server/services/codeAnimation/prompt.js b/server/services/codeAnimation/prompt.js
index 3f3d491b13..ad21d36a9c 100644
--- a/server/services/codeAnimation/prompt.js
+++ b/server/services/codeAnimation/prompt.js
@@ -430,15 +430,28 @@ export function extractCodeSections(text) {
const start = candidate.indexOf('{');
const end = candidate.lastIndexOf('}');
if (start < 0 || end <= start) continue;
- try {
- const parsed = JSON.parse(candidate.slice(start, end + 1));
- if (!Array.isArray(parsed?.sections)) continue;
- return parsed.sections.filter((section) => section && typeof section.id === 'string' && typeof section.source === 'string');
- } catch { /* the next fence may be the document */ }
+ const parsed = parseSectionsJson(candidate.slice(start, end + 1));
+ if (!Array.isArray(parsed?.sections)) continue;
+ return parsed.sections.filter((section) => section && typeof section.id === 'string' && typeof section.source === 'string');
}
return [];
}
+// Local models often close the JSON slightly wrong: stray characters after the final
+// `]}` (`..."}]}"}`) or a section object left unclosed (`..."]}`). Try the text as given,
+// then a few bounded repairs, and return the first that parses (or null).
+function parseSectionsJson(json) {
+ const attempts = [json];
+ for (let end = json.length - 1, tries = 0; end > 0 && tries < 8; end = json.lastIndexOf('}', end - 1), tries += 1) {
+ if (json[end] === '}' && end < json.length - 1) attempts.push(json.slice(0, end + 1));
+ }
+ if (/"\s*\]\s*\}\s*$/.test(json)) attempts.push(json.replace(/"\s*\]\s*\}\s*$/, '"}]}'));
+ for (const attempt of attempts) {
+ try { return JSON.parse(attempt); } catch { /* try the next repair */ }
+ }
+ return null;
+}
+
/** Author only bounded drawing functions; the host owns the page and media. */
export function buildMixedMediaDocumentPrompt({ title, song, palette, treatment, visualSpec, scenes, styleLines = [], onlySectionId = null, sharedStyle = null, directionContext = '', renderer = 'canvas', mediaMode = 'code-images-video' }) {
const sections = (song.sections || []).filter((section) => !onlySectionId || section.id === onlySectionId);
diff --git a/server/services/codeAnimation/prompt.test.js b/server/services/codeAnimation/prompt.test.js
index e0c2f31f78..f523f00cc4 100644
--- a/server/services/codeAnimation/prompt.test.js
+++ b/server/services/codeAnimation/prompt.test.js
@@ -5,6 +5,7 @@ import {
buildMixedMediaDocumentPrompt,
PACING_RULE,
extractAnimationHtml,
+ extractCodeSections,
resolveFrameSize,
CODE_ANIMATION_AUDIO_GLOBAL,
CODE_ANIMATION_MESSAGES,
@@ -252,3 +253,22 @@ describe('music-video craft and measured choreography', () => {
}
});
});
+
+describe('extractCodeSections', () => {
+ const fn = 'function render(ctx, env) { ctx.fillRect(0, 0, 1, 1); }';
+ const valid = JSON.stringify({ sections: [{ id: 'beat-3', source: fn }] });
+ it('parses a fenced answer and ignores sections without an id or source', () => {
+ expect(extractCodeSections(`\`\`\`json\n${JSON.stringify({ sections: [{ id: 'a', source: fn }, { id: 'b' }] })}\n\`\`\``)).toEqual([{ id: 'a', source: fn }]);
+ });
+ it('recovers an answer with stray characters after the closing brackets', () => {
+ expect(extractCodeSections(`\`\`\`json\n${valid}"}\n\`\`\``)).toEqual([{ id: 'beat-3', source: fn }]);
+ });
+ it('recovers an answer that left its last section object unclosed', () => {
+ const unclosed = `{"sections":[{"id":"beat-3","source":${JSON.stringify(fn)}]}`;
+ expect(extractCodeSections(`\`\`\`json\n${unclosed}\n\`\`\``)).toEqual([{ id: 'beat-3', source: fn }]);
+ });
+ it('returns nothing for text with no usable JSON', () => {
+ expect(extractCodeSections('no json here')).toEqual([]);
+ expect(extractCodeSections('```json\n{"sections": [ {"id": \n```')).toEqual([]);
+ });
+});