mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-16 15:35:31 +01:00
29 lines
571 B
TypeScript
29 lines
571 B
TypeScript
|
|
export function formatJSON(json: string) {
|
||
|
|
try {
|
||
|
|
return JSON.stringify(JSON.parse(json), null, 2);
|
||
|
|
} catch (e) {
|
||
|
|
return json;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function extractJson(text: string) {
|
||
|
|
let openBraces = 0;
|
||
|
|
let startIndex = -1;
|
||
|
|
|
||
|
|
for (let i = 0; i < text.length; i++) {
|
||
|
|
if (text[i] === '{') {
|
||
|
|
if (openBraces === 0) {
|
||
|
|
startIndex = i;
|
||
|
|
}
|
||
|
|
openBraces++;
|
||
|
|
} else if (text[i] === '}') {
|
||
|
|
openBraces--;
|
||
|
|
if (openBraces === 0 && startIndex !== -1) {
|
||
|
|
return text.slice(startIndex, i + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return '';
|
||
|
|
}
|