@cccarv82/freya 3.8.3 → 3.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -117,18 +117,29 @@ function extractFirstJsonObject(text) {
117
117
  const start = text.indexOf('{');
118
118
  if (start === -1) return null;
119
119
  let depth = 0;
120
+ let inString = false;
121
+ let escape = false;
120
122
  for (let i = start; i < text.length; i++) {
121
- if (text[i] === '{') depth++;
122
- else if (text[i] === '}') { depth--; if (depth === 0) return text.slice(start, i + 1); }
123
+ const ch = text[i];
124
+ if (escape) { escape = false; continue; }
125
+ if (ch === '\\' && inString) { escape = true; continue; }
126
+ if (ch === '"') { inString = !inString; continue; }
127
+ if (inString) continue;
128
+ if (ch === '{') depth++;
129
+ else if (ch === '}') { depth--; if (depth === 0) return text.slice(start, i + 1); }
123
130
  }
131
+ // If brace matching failed, try a greedy approach — find the last }
132
+ const lastBrace = text.lastIndexOf('}');
133
+ if (lastBrace > start) return text.slice(start, lastBrace + 1);
124
134
  return null;
125
135
  }
126
136
 
127
- function escapeJsonControlChars(jsonText) {
128
- return jsonText.replace(/[\x00-\x1F\x7F]/g, (ch) => {
129
- if (ch === '\n' || ch === '\r' || ch === '\t') return ch;
130
- return '\\u' + ch.charCodeAt(0).toString(16).padStart(4, '0');
131
- });
137
+ function sanitizeJsonText(jsonText) {
138
+ // Copilot often returns pretty-printed JSON with literal newlines INSIDE
139
+ // string values (e.g. multi-line descriptions). These are invalid JSON.
140
+ // Replace ALL control chars (including \n, \r, \t) with spaces — JSON
141
+ // doesn't use newlines as syntax, they're just whitespace between tokens.
142
+ return jsonText.replace(/[\x00-\x1F\x7F]/g, ' ');
132
143
  }
133
144
 
134
145
  function readProjectSlugMap(wsDir) {
@@ -328,12 +339,18 @@ IMPORTANTE: Extraia APENAS informações explícitas do log. NÃO invente dados.
328
339
  try {
329
340
  plan = JSON.parse(jsonText);
330
341
  } catch {
331
- try { plan = JSON.parse(escapeJsonControlChars(jsonText)); } catch {
342
+ try { plan = JSON.parse(sanitizeJsonText(jsonText)); } catch {
332
343
  totalErrors++;
333
344
  if (totalErrors <= 2) {
334
345
  console.log(`\n ⚠ JSON parse diagnostic for ${date}:`);
335
- console.log(` Raw output (first 500 chars): ${out.slice(0, 500)}`);
336
- console.log(` Extracted JSON attempt: ${(jsonText || '').slice(0, 300)}`);
346
+ console.log(` Raw output length: ${out.length} chars`);
347
+ console.log(` Raw output (first 300 chars): ${out.slice(0, 300)}`);
348
+ console.log(` Raw output (last 200 chars): ${out.slice(-200)}`);
349
+ console.log(` Extracted JSON length: ${(jsonText || '').length}`);
350
+ console.log(` Extracted JSON (last 200 chars): ${(jsonText || '').slice(-200)}`);
351
+ try { JSON.parse(jsonText); } catch (parseErr) {
352
+ console.log(` Parse error: ${parseErr.message}`);
353
+ }
337
354
  }
338
355
  process.stdout.write(`\r [${i + 1}/${files.length}] ${date} — ❌ invalid JSON `);
339
356
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cccarv82/freya",
3
- "version": "3.8.3",
3
+ "version": "3.8.5",
4
4
  "description": "Personal AI Assistant with local-first persistence",
5
5
  "scripts": {
6
6
  "health": "node scripts/validate-data.js && node scripts/validate-structure.js",