/** A literal in a code example is public only once its delimiter * actually closes. Stream ordinary text immediately; bound the ambiguous * suffix and prefer redaction if it is malformed or over the budget. */ const MAX_AMBIGUOUS = 32 * 1024; const OPEN = '', CLOSE = ''; export function createPublicTextStream() { let depth = 0, tail = '', held = '', heldDepth = 0, run = '', size = 0; let code: { mark: string; size: number; fence: boolean } | null = null; let fenceEnd = false, indent = 0, blank = true, slash = false; let first = true, jsonDepth = 0, quoted = false; function reset() { depth = heldDepth = size = indent = jsonDepth = 0; tail = held = run = ''; fenceEnd = slash = quoted = false; first = blank = true; code = null; } function read(chunk: string, final: boolean): string { let out = ''; function abandonLiteral() { // Only complete tag tokens enter held; a split delimiter lives in tail. let pos = 0; for (const m of held.matchAll(/<\/?think>/gi)) { if (!depth) out += held.slice(pos, m.index); if (m[0].length === OPEN.length) depth++; else if (depth) depth--; else out += m[0]; pos = m.index! + m[0].length; } if (!depth) out += held.slice(pos); held = ''; heldDepth = 0; code = null; quoted = fenceEnd = false; } function confirmLiteral() { // A backtick/quote inside private prose is not proof of a public // example. Still allow documenting the opening tag itself, e.g. ``. if (heldDepth && !/^\s*[`~"]+\s*$/i.test(held)) { abandonLiteral(); return; } out += held; held = ''; heldDepth = 0; code = null; quoted = fenceEnd = false; } function token(raw: string, mark = '') { const start = raw.length === OPEN.length && raw.toLowerCase() === OPEN; if (depth) { if (start) depth++; else if (raw.length === CLOSE.length && raw.toLowerCase() === CLOSE) depth--; return; } if (first && raw.trim()) { first = false; if (raw === '[' || raw === '{') jsonDepth = 1; } else if (!code && jsonDepth && !quoted) { if (raw === '[' || raw === '{') jsonDepth++; if (raw === ']' || raw === '}') jsonDepth--; } if (start && !code && !quoted) { depth++; return; } if (held || start) { held += raw; if (start) heldDepth++; else if (heldDepth && raw.length === CLOSE.length && raw.toLowerCase() === CLOSE) heldDepth--; } else out += raw; if (quoted) { if (raw === '"' && !slash) confirmLiteral(); else if (raw === '\n') { abandonLiteral(); jsonDepth = 0; } } else if (code) { if (code.fence) { if (mark === code.mark && raw.length >= code.size && indent <= 3) fenceEnd = true; else if (fenceEnd && raw === '\n') confirmLiteral(); else if (!/^[ \t\r]$/.test(raw)) fenceEnd = false; } else if (mark === code.mark && raw.length === code.size) confirmLiteral(); else if (raw === '\n' && blank) abandonLiteral(); } else if (jsonDepth && raw === '"') quoted = true; else if (mark && !slash) { const fence = raw.length >= 3 && indent <= 3; if (fence || mark === '`') code = {mark, size: raw.length, fence}; } slash = raw === '\\' && !slash; if (raw === '\n') { indent = 0; blank = true; } else { indent = raw === ' ' ? Math.min(4, indent + 1) : 4; if (!/^[ \t\r]$/.test(raw)) blank = false; } if (held.length > MAX_AMBIGUOUS) abandonLiteral(); } function endRun() { if (size) token(run.repeat(size), run); run = ''; size = 0; } const text = tail + chunk; tail = ''; for (let i = 0; i < text.length;) { const c = text[i]!; if (size && c !== run) endRun(); if (c === '`' || c === '~') { run = c; size++; i++; continue; } if (c === '<') { const tag = text[i+1] === '/' ? CLOSE : OPEN; const prefix = text.slice(i, i + tag.length).toLowerCase(); if (prefix === tag) { token(text.slice(i, i+tag.length)); i += tag.length; continue; } if (!final && tag.startsWith(prefix)) { tail = text.slice(i); break; } } token(c); i++; } if (final) { endRun(); if (fenceEnd) confirmLiteral(); else if (held) abandonLiteral(); reset(); } return out; } return { append: (s: string) => read(s, false), finish: () => read('', true), reset }; }