UNPKG

@visactor/vmind

Version:

<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu

53 lines (45 loc) 1.8 kB
"use strict"; var __importDefault = this && this.__importDefault || function(mod) { return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.parseLLMJson = exports.revisedJSONStr = exports.matchJSONStr = void 0; const json5_1 = __importDefault(require("json5")), jsonrepair_1 = require("jsonrepair"), text_1 = require("./text"), matchJSONStr = str => { const first = str.indexOf("{"), last = str.lastIndexOf("}"), result = str.substring(first, last + 1); return result && result.length > 0 ? (0, text_1.replaceAll)(result, "\n", " ") : str; }; exports.matchJSONStr = matchJSONStr; const revisedJSONStr = str => { let res = str; return str.includes('"unit":"%""') && (res = res.replaceAll('"unit":"%""', "unit:'%'")), str.includes('"unit": "%""') && (res = res.replaceAll('"unit": "%""', "unit:'%'")), res; }; exports.revisedJSONStr = revisedJSONStr; const parseLLMJson = (JsonStr, prefix) => { const parseNoPrefixStr = str => { try { return json5_1.default.parse(str); } catch (err) { try { return json5_1.default.parse((0, jsonrepair_1.jsonrepair)(str)); } catch (err) { return { error: `LLM Result is not Valid JSON; llm res: ${JsonStr}`, errorInfo: err }; } } }; if (prefix) { const splitArr = JsonStr.split(prefix), res = parseNoPrefixStr(splitArr[splitArr.length - 2]); if (!res.error) return res; } const res2 = parseNoPrefixStr(JsonStr); return res2.error && console.error(res2.errorInfo), res2; }; exports.parseLLMJson = parseLLMJson; //# sourceMappingURL=json.js.map