@axlsdk/axl 0.13.1 → 0.13.3

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.
package/dist/index.cjs CHANGED
@@ -141,6 +141,7 @@ __export(index_exports, {
141
141
  agent: () => agent,
142
142
  createSpanManager: () => createSpanManager,
143
143
  defineConfig: () => defineConfig,
144
+ extractJson: () => extractJson,
144
145
  resolveThinkingOptions: () => resolveThinkingOptions,
145
146
  tool: () => tool,
146
147
  workflow: () => workflow,
@@ -2255,15 +2256,57 @@ function zodToJsonSchema(schema) {
2255
2256
  function estimateTokens(text) {
2256
2257
  return Math.ceil(text.length / 4);
2257
2258
  }
2258
- function stripMarkdownFences(text) {
2259
- const trimmed = text.trim();
2260
- if (trimmed.startsWith("```")) {
2261
- const withoutOpening = trimmed.replace(/^```\w*\s*\n?/, "");
2262
- const withoutClosing = withoutOpening.replace(/\n?```\s*$/, "");
2263
- return withoutClosing.trim();
2259
+ function extractJson(content) {
2260
+ const trimmed = content.trim();
2261
+ if (trimmed.startsWith("{")) {
2262
+ return extractBalanced(trimmed, 0, "{", "}") ?? trimmed;
2263
+ }
2264
+ if (trimmed.startsWith("[")) {
2265
+ return extractBalanced(trimmed, 0, "[", "]") ?? trimmed;
2266
+ }
2267
+ const fenceMatch = trimmed.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/);
2268
+ if (fenceMatch) {
2269
+ return fenceMatch[1].trim();
2270
+ }
2271
+ const open = trimmed.indexOf("{");
2272
+ if (open >= 0) {
2273
+ const extracted = extractBalanced(trimmed, open, "{", "}");
2274
+ if (extracted) return extracted;
2275
+ }
2276
+ const openBracket = trimmed.indexOf("[");
2277
+ if (openBracket >= 0) {
2278
+ const extracted = extractBalanced(trimmed, openBracket, "[", "]");
2279
+ if (extracted) return extracted;
2264
2280
  }
2265
2281
  return trimmed;
2266
2282
  }
2283
+ function extractBalanced(str, start, openChar, closeChar) {
2284
+ let depth = 0;
2285
+ let inString = false;
2286
+ let escape = false;
2287
+ for (let i = start; i < str.length; i++) {
2288
+ const ch = str[i];
2289
+ if (escape) {
2290
+ escape = false;
2291
+ continue;
2292
+ }
2293
+ if (ch === "\\" && inString) {
2294
+ escape = true;
2295
+ continue;
2296
+ }
2297
+ if (ch === '"') {
2298
+ inString = !inString;
2299
+ continue;
2300
+ }
2301
+ if (inString) continue;
2302
+ if (ch === openChar) depth++;
2303
+ else if (ch === closeChar) {
2304
+ depth--;
2305
+ if (depth === 0) return str.slice(start, i + 1);
2306
+ }
2307
+ }
2308
+ return null;
2309
+ }
2267
2310
  function estimateMessagesTokens(messages) {
2268
2311
  let total = 0;
2269
2312
  for (const msg of messages) {
@@ -3044,7 +3087,7 @@ ${JSON.stringify(jsonSchema, null, 2)}`;
3044
3087
  let validated = void 0;
3045
3088
  if (options?.schema) {
3046
3089
  try {
3047
- const parsed = JSON.parse(stripMarkdownFences(content));
3090
+ const parsed = JSON.parse(extractJson(content));
3048
3091
  validated = options.schema.parse(parsed);
3049
3092
  } catch (err) {
3050
3093
  const maxSchemaRetries = options.retries ?? 3;
@@ -6595,6 +6638,7 @@ function cosineSimilarity2(a, b) {
6595
6638
  agent,
6596
6639
  createSpanManager,
6597
6640
  defineConfig,
6641
+ extractJson,
6598
6642
  resolveThinkingOptions,
6599
6643
  tool,
6600
6644
  workflow,