@axlsdk/axl 0.13.1 → 0.13.2

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,54 @@ 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("{") || trimmed.startsWith("[")) {
2262
+ return trimmed;
2263
+ }
2264
+ const fenceMatch = trimmed.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/);
2265
+ if (fenceMatch) {
2266
+ return fenceMatch[1].trim();
2267
+ }
2268
+ const open = trimmed.indexOf("{");
2269
+ if (open >= 0) {
2270
+ const extracted = extractBalanced(trimmed, open, "{", "}");
2271
+ if (extracted) return extracted;
2272
+ }
2273
+ const openBracket = trimmed.indexOf("[");
2274
+ if (openBracket >= 0) {
2275
+ const extracted = extractBalanced(trimmed, openBracket, "[", "]");
2276
+ if (extracted) return extracted;
2264
2277
  }
2265
2278
  return trimmed;
2266
2279
  }
2280
+ function extractBalanced(str, start, openChar, closeChar) {
2281
+ let depth = 0;
2282
+ let inString = false;
2283
+ let escape = false;
2284
+ for (let i = start; i < str.length; i++) {
2285
+ const ch = str[i];
2286
+ if (escape) {
2287
+ escape = false;
2288
+ continue;
2289
+ }
2290
+ if (ch === "\\" && inString) {
2291
+ escape = true;
2292
+ continue;
2293
+ }
2294
+ if (ch === '"') {
2295
+ inString = !inString;
2296
+ continue;
2297
+ }
2298
+ if (inString) continue;
2299
+ if (ch === openChar) depth++;
2300
+ else if (ch === closeChar) {
2301
+ depth--;
2302
+ if (depth === 0) return str.slice(start, i + 1);
2303
+ }
2304
+ }
2305
+ return null;
2306
+ }
2267
2307
  function estimateMessagesTokens(messages) {
2268
2308
  let total = 0;
2269
2309
  for (const msg of messages) {
@@ -3044,7 +3084,7 @@ ${JSON.stringify(jsonSchema, null, 2)}`;
3044
3084
  let validated = void 0;
3045
3085
  if (options?.schema) {
3046
3086
  try {
3047
- const parsed = JSON.parse(stripMarkdownFences(content));
3087
+ const parsed = JSON.parse(extractJson(content));
3048
3088
  validated = options.schema.parse(parsed);
3049
3089
  } catch (err) {
3050
3090
  const maxSchemaRetries = options.retries ?? 3;
@@ -6595,6 +6635,7 @@ function cosineSimilarity2(a, b) {
6595
6635
  agent,
6596
6636
  createSpanManager,
6597
6637
  defineConfig,
6638
+ extractJson,
6598
6639
  resolveThinkingOptions,
6599
6640
  tool,
6600
6641
  workflow,