Harden pasted Scattermind JSON handoff

This commit is contained in:
OpenClaw Bot
2026-05-27 00:31:53 +02:00
parent c2f61bc959
commit fd0bb3857e
2 changed files with 64 additions and 4 deletions
+37 -3
View File
@@ -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;