Harden pasted Scattermind JSON handoff
This commit is contained in:
@@ -486,11 +486,45 @@ function looksLikeRankPayload(value = {}) {
|
||||
);
|
||||
}
|
||||
|
||||
function extractFirstJsonObject(text = '') {
|
||||
const start = text.indexOf('{');
|
||||
if (start < 0) return '';
|
||||
let depth = 0;
|
||||
let inString = false;
|
||||
let escaped = false;
|
||||
for (let index = start; index < text.length; index += 1) {
|
||||
const char = text[index];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
if (char === '\\' && inString) {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
if (char === '"') {
|
||||
inString = !inString;
|
||||
continue;
|
||||
}
|
||||
if (inString) continue;
|
||||
if (char === '{') depth += 1;
|
||||
if (char === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) return text.slice(start, index + 1);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function parseEmbeddedRankPayload(value = '') {
|
||||
const text = cleanMultiline(value, 12000);
|
||||
if (!text.startsWith('{') || !text.endsWith('}')) return null;
|
||||
const text = cleanMultiline(value, 12000)
|
||||
.replace(/^```(?:json)?\s*/i, '')
|
||||
.replace(/```$/i, '')
|
||||
.trim();
|
||||
const jsonText = text.startsWith('{') && text.endsWith('}') ? text : extractFirstJsonObject(text);
|
||||
if (!jsonText) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
const parsed = JSON.parse(jsonText);
|
||||
return looksLikeRankPayload(parsed) ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user