@mcoda/codali 0.1.97 → 0.1.99

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.
@@ -1 +1 @@
1
- {"version":3,"file":"GatewayPlanner.d.ts","sourceRoot":"","sources":["../../src/gateway/GatewayPlanner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EAEhB,MAAM,+BAA+B,CAAC;AAIvC,OAAO,KAAK,EACV,6BAA6B,EAC7B,0BAA0B,EAC1B,oBAAoB,EAEpB,4BAA4B,EAE7B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,4BAA4B,CAAC;AAGpC,MAAM,WAAW,kCAAkC;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,oBAAoB,CAAC;IAC9B,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;IAC7C,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,kCAAkC,CAAC,CAAC;CAChF;AAED,MAAM,WAAW,2BAA2B;IAC1C,iBAAiB,EAAE,wBAAwB,CAAC;IAC5C,UAAU,EAAE,6BAA6B,CAAC;IAC1C,OAAO,EAAE,0BAA0B,CAAC;IACpC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,wBAAwB,EAAE,MAAM,CAAC;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,2BAA2B;IAC1C,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAA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: request?.mode === "image" && request?.policy.allowImageWorker === true,
238
238
  };
239
239
  };
240
+ /**
241
+ * Returns every complete, brace-balanced span in the text, in order, ignoring
242
+ * braces inside string literals. Prose can contain braces of its own, so the
243
+ * caller tries each span and keeps the first that is actually valid JSON.
244
+ */
245
+ const extractBalancedJsonObjects = (text) => {
246
+ const found = [];
247
+ let depth = 0;
248
+ let start = -1;
249
+ let inString = false;
250
+ let escaped = false;
251
+ for (let index = 0; index < text.length; index += 1) {
252
+ const char = text[index];
253
+ if (inString) {
254
+ if (escaped) {
255
+ escaped = false;
256
+ }
257
+ else if (char === "\\") {
258
+ escaped = true;
259
+ }
260
+ else if (char === '"') {
261
+ inString = false;
262
+ }
263
+ continue;
264
+ }
265
+ if (char === '"') {
266
+ inString = true;
267
+ }
268
+ else if (char === "{") {
269
+ if (depth === 0) {
270
+ start = index;
271
+ }
272
+ depth += 1;
273
+ }
274
+ else if (char === "}" && depth > 0) {
275
+ depth -= 1;
276
+ if (depth === 0 && start >= 0) {
277
+ found.push(text.slice(start, index + 1));
278
+ }
279
+ }
280
+ }
281
+ return found;
282
+ };
283
+ const summarizeRawStageContent = (content) => {
284
+ const collapsed = content.replace(/\s+/g, " ").trim();
285
+ if (!collapsed) {
286
+ return "<empty response>";
287
+ }
288
+ return collapsed.length > 400 ? `${collapsed.slice(0, 400)}… (${collapsed.length} chars)` : collapsed;
289
+ };
240
290
  const parseJsonObject = (content) => {
241
291
  const trimmed = content.trim();
242
292
  const fenced = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i)?.[1]?.trim();
@@ -245,12 +295,24 @@ const parseJsonObject = (content) => {
245
295
  return JSON.parse(candidate);
246
296
  }
247
297
  catch {
248
- const start = candidate.indexOf("{");
249
- const end = candidate.lastIndexOf("}");
250
- if (start >= 0 && end > start) {
251
- return JSON.parse(candidate.slice(start, end + 1));
298
+ // Models wrap the object in prose, emit the fence mid-message, or add
299
+ // trailing commentary. Taking everything between the first '{' and the last
300
+ // '}' is not valid JSON whenever that surrounding text has braces of its
301
+ // own, and the resulting SyntaxError escaped unwrapped: it is not a
302
+ // CodaliGatewayPlannerError, so generateValidated fell through to its
303
+ // generic "could not be parsed or validated" and the model's actual output
304
+ // was discarded. A caller that degrades on a gateway stage failure then
305
+ // silently loses its whole tool layer for the turn, with nothing in the
306
+ // logs to diagnose it.
307
+ for (const balanced of extractBalancedJsonObjects(candidate)) {
308
+ try {
309
+ return JSON.parse(balanced);
310
+ }
311
+ catch {
312
+ // Not this span - prose braces look balanced too. Try the next one.
313
+ }
252
314
  }
253
- throw new CodaliGatewayPlannerError("GATEWAY_JSON_PARSE_FAILED", "Planner stage returned non-JSON content.");
315
+ throw new CodaliGatewayPlannerError("GATEWAY_JSON_PARSE_FAILED", `Planner stage returned non-JSON content: ${summarizeRawStageContent(candidate)}`);
254
316
  }
255
317
  };
256
318
  const validateClassifierOutput = (value, input, policyCompilation) => {
@@ -726,7 +788,7 @@ export class CodaliGatewayPlanner {
726
788
  if (error instanceof CodaliGatewayPlannerError) {
727
789
  throw error;
728
790
  }
729
- throw new CodaliGatewayPlannerError("GATEWAY_STAGE_SCHEMA_INVALID", `${stage} output could not be parsed or validated.`);
791
+ throw new CodaliGatewayPlannerError("GATEWAY_STAGE_SCHEMA_INVALID", `${stage} output could not be parsed or validated (${lastError instanceof Error ? lastError.message : String(lastError)}): ${summarizeRawStageContent(lastRaw)}`);
730
792
  }
731
793
  repairAttempts += 1;
732
794
  currentMessages = buildRepairMessages(stage, messages, lastRaw, lastError);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcoda/codali",
3
- "version": "0.1.97",
3
+ "version": "0.1.99",
4
4
  "description": "Standalone tool-runner adapter for mcoda.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -34,8 +34,8 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@mcoda/db": "0.1.97",
38
- "@mcoda/shared": "0.1.97"
37
+ "@mcoda/db": "0.1.99",
38
+ "@mcoda/shared": "0.1.99"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsc -p tsconfig.json",