/** A conservative view for host intent heuristics, NOT an authorization parser. * Keep the original request in model history. Only explicitly marked evidence * is omitted here; prose is not guessed away because it resembles a log. * Unfenced evidence lasts until an explicit request/resume heading. */ export function requestIntentText(input: string): string { const out: string[] = []; let fence = ''; let evidence = false; let bracketDepth = 0; let remainder: string | undefined; const consumeQuote = (line: string, start = 0) => { for (let i = start; i < line.length; i++) { if (line[i] === '【') bracketDepth++; if (line[i] === '】' && --bracketDepth === 0) { remainder = line.slice(i + 1); return; } } }; const lines = input.replace(/\r\n?/g, '\n').split('\n'); let index = 0; while (index < lines.length || remainder !== undefined) { const line = remainder ?? lines[index++]!; remainder = undefined; const mark = /^\s*(`{3,}|~{3,})/.exec(line)?.[1]; if (fence) { if (mark?.[0] === fence[0] && mark.length >= fence.length) { fence = ''; evidence = false; } continue; } if (mark) { fence = mark; continue; } if (/^\s*>/.test(line)) continue; if (!bracketDepth && /^\s*(?:#{1,6}\s*)?(?:我的(?:请求|问题|要求)|本轮(?:请求|问题|要求)|以上(?:是|为)(?:日志|记录|引用|材料)|引用结束|日志结束|my request|end (?:log|quote|transcript))\s*[::]?/i.test(line)) { evidence = false; out.push(line.replace(/^\s*(?:#{1,6}\s*)?(?:我的(?:请求|问题|要求)|本轮(?:请求|问题|要求)|以上(?:是|为)(?:日志|记录|引用|材料)|引用结束|日志结束|my request|end (?:log|quote|transcript))\s*[::]?/i, '')); continue; } if (bracketDepth) { consumeQuote(line); continue; } if (evidence) { if (line.trimStart().startsWith('【')) { evidence = false; consumeQuote(line.trimStart()); } continue; } if (/^\s*(?:#{1,6}\s*)?(?:下面|以下)(?:是|为)?[^::【\n]{0,40}(?:日志|对话记录|聊天记录|回复|输出|引用|材料)\s*[::]?\s*$/.test(line)) { evidence = true; continue; } // A labelled paste may follow the user's question on the same line. const label = /(?:(?:下面|以下)(?:是|为)?(?:[^\n::【]{0,20})?(?:日志|对话记录|聊天记录|回复|输出|引用|材料)|(?= 0 && /(?:说法|日志|记录|回复|输出|引用|材料|如下|下面|以下)/.test(line.slice(0, start))) { out.push(line.slice(0, start)); consumeQuote(line, start); continue; } out.push(line); } return out.join('\n').trim(); }