UNPKG

tinyagent-ts

Version:

Modern TypeScript framework for building AI agents with pluggable tools and ReAct reasoning

66 lines 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findFirstJson = findFirstJson; exports.safeJsonParse = safeJsonParse; exports.extractJson = extractJson; function findFirstJson(text) { const start = text.indexOf('{'); if (start === -1) return null; let depth = 0; for (let i = start; i < text.length; i++) { const ch = text[i]; if (ch === '{') depth++; if (ch === '}') { depth--; if (depth === 0) { return text.slice(start, i + 1); } } } return null; } /** Parse JSON safely, returning null on failure. */ function safeJsonParse(str) { try { return JSON.parse(str); } catch { return null; } } function findJsonBlocks(text) { const blocks = []; for (let i = 0; i < text.length; i++) { if (text[i] !== '{') continue; let depth = 0; for (let j = i; j < text.length; j++) { if (text[j] === '{') depth++; if (text[j] === '}') { depth--; if (depth === 0) { blocks.push(text.slice(i, j + 1)); i = j; break; } } } } return blocks; } function extractJson(text) { const chunks = Array.from(text.matchAll(/```(?:json)?\s*\n([\s\S]*?)```/gi)).map((m) => m[1]); chunks.push(text); for (const chunk of chunks) { const blocks = findJsonBlocks(chunk); for (const candidate of blocks) { if (safeJsonParse(candidate)) return candidate; } } return null; } //# sourceMappingURL=json.js.map