import type { TranslationPayload } from "../types.js"; export const BLOCKED_JSON_KEYS = new Set(["__proto__", "constructor", "prototype"]); export function toCleanString(val: unknown): string { if (typeof val === "string") return val.trim(); if (typeof val === "number" || typeof val === "boolean") return String(val); if (Array.isArray(val)) return val.map(toCleanString).filter(Boolean).join(" · "); return ""; } /** * Featherweight JSON repair & object extractor (protected against prototype pollution & >32KB payloads). */ export function repairAndParseJsonObject(raw: string): Record | null { try { if (!raw || typeof raw !== "string" || raw.length > 32_768) return null; const stripped = raw.replace(/<(?:think|thought)>[\s\S]*?<\/(?:think|thought)>/gi, "").trim(); const tryParseCandidate = (candidate: string): Record | null => { const first = candidate.indexOf("{"); const last = candidate.lastIndexOf("}"); if (first === -1 || last <= first) return null; const jsonSubstr = candidate.slice(first, last + 1); let parsed: any; try { parsed = JSON.parse(jsonSubstr); } catch { const repaired = jsonSubstr .replace(/,\s*([}\]])/g, "$1") .replace( /("[a-zA-Z0-9_-]+"\s*:\s*")([\s\S]*?)("(?=\s*,\s*"|\s*\}))/g, (_m, prefix, content, suffix) => prefix + content.replace(/(?[\s\S]*?<\/(?:think|thought)>/gi, "") .replace(/```(?:json)?|```/gi, "") .trim(); const regexFallback = cleaned.match(/"(?:prompt|written|spoken)"\s*:\s*"([\s\S]*?)"\s*\}/); if (regexFallback && regexFallback[1].trim()) { return regexFallback[1].replace(/\\"/g, '"').replace(/\\n/g, "\n").trim(); } if (cleaned.startsWith("{") && cleaned.endsWith("}")) { return ""; } return cleaned.replace(/^["']|["']$/g, "").trim(); } /** * Parse LLM JSON responses safely (shared across translateCore and translatePrompt). */ export function parseLlmResponse(raw: string): TranslationPayload | null { const parsed = repairAndParseJsonObject(raw); if (!parsed) return null; const spoken = toCleanString(parsed.spoken ?? parsed.casual ?? parsed.slot1); const spokenMeaning = toCleanString(parsed.spoken_meaning ?? parsed.spokenMeaning); const written = toCleanString(parsed.written ?? parsed.academic ?? parsed.slot2); const writtenMeaning = toCleanString(parsed.written_meaning ?? parsed.writtenMeaning); const vocab = toCleanString(parsed.vocab); const summary = toCleanString(parsed.summary ?? parsed.core_intent ?? parsed.coreIntent); const slotOutputs: Record = Object.create(null); for (const [k, v] of Object.entries(parsed)) { if (BLOCKED_JSON_KEYS.has(k) || k.endsWith("_meaning") || k === "summary" || k === "core_intent" || k === "coreIntent") continue; const textVal = toCleanString(v); if (textVal) { const meaningVal = toCleanString(parsed[k + "_meaning"]); slotOutputs[k] = { content: textVal, meaning: meaningVal || undefined, }; } } const nonVocabOutputs = Object.entries(slotOutputs) .filter(([k]) => k !== "vocab") .map(([, val]) => val); const effectiveSpoken = spoken || nonVocabOutputs[0]?.content || vocab || ""; const effectiveSpokenMeaning = spokenMeaning || nonVocabOutputs[0]?.meaning || ""; if (!effectiveSpoken) return null; return { spoken: effectiveSpoken, spokenMeaning: effectiveSpokenMeaning || undefined, written: written || undefined, writtenMeaning: writtenMeaning || undefined, vocab: vocab || undefined, summary: summary || undefined, slotOutputs: Object.keys(slotOutputs).length > 0 ? slotOutputs : undefined, }; } /** * Strip annotations and recover purely clean text to prevent LLM prompt pollution. */ export function stripLingualAnnotation(annotatedText: string): { raw: string; spoken?: string; written?: string; vocab?: string; } { const lines = annotatedText.split("\n"); const rawLines: string[] = []; let spoken: string | undefined; let written: string | undefined; let vocab: string | undefined; for (const line of lines) { const trimmed = line.trim(); const sourceMatch = trimmed.match(/^·\s*\[?(?:原文|source|original|quelle)\]?\s+(.*)$/i); if (sourceMatch) { rawLines.push(sourceMatch[1].trim()); continue; } if (/^(?:[│\s]*↳\s*\(.*\)|↳\s*\(.*\))/.test(trimmed)) { continue; } const slotMatch = trimmed.match(/^(?:[┌├└│]\s*|↳\s*)\[([^\]]+)\]\s*(.*)$/); if (slotMatch) { const tag = slotMatch[1].trim(); let text = slotMatch[2].trim().replace(/\x1b\[[0-9;]*[a-zA-Z]/g, ""); const parenIdx = text.lastIndexOf(" ("); if (parenIdx !== -1 && text.endsWith(")")) { text = text.slice(0, parenIdx).trim(); } if (/^(?:口语|spoken|口語|slack|slot1)$/i.test(tag)) { spoken = text; } else if (/^(?:写作|written|文面|rfc|slot2|敬語)$/i.test(tag)) { written = text; } else if (/^(?:重点|vocab|単語|词汇)$/i.test(tag)) { vocab = text; } } else { rawLines.push(line); } } return { raw: rawLines.join("\n").trim(), spoken, written, vocab }; }