UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

113 lines • 4.88 kB
const BARE_RUN = /(?<![\\A-Za-z0-9_])((?:u[0-9a-fA-F]{4})+)(?![A-Za-z0-9_])/g; const LITERAL = /\\u([0-9a-fA-F]{4})/g; // Order matters: a fence holds backticks and newlines, and a double-backtick span may hold a single backtick. const QUOTED_CODE = [/(```|~~~)[\s\S]*?\1/g, /``[^\n]*?``/g, /`[^`\n]*`/g]; const NON_ASCII_LETTER = /(?!\p{ASCII})\p{L}/gu; const PRINTABLE = /^[\p{L}\p{M}\p{N}\p{P}\p{S}]$/u; const CJK_FAMILY = "CJK"; const SCRIPTS = [ [CJK_FAMILY, /[\p{Script=Hangul}\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Bopomofo}]/u], ["Latin", /\p{Script=Latin}/u], ["Cyrillic", /\p{Script=Cyrillic}/u], ["Greek", /\p{Script=Greek}/u], ["Arabic", /\p{Script=Arabic}/u], ["Hebrew", /\p{Script=Hebrew}/u], ["Thai", /\p{Script=Thai}/u], ["Devanagari", /\p{Script=Devanagari}/u], ]; function scriptOf(char) { return SCRIPTS.find(([, pattern]) => pattern.test(char))?.[0]; } function decode(run) { const units = []; for (let index = 1; index < run.length; index += 5) units.push(Number.parseInt(run.slice(index, index + 4), 16)); return String.fromCharCode(...units); } function isResidueRun(run, scripts) { const decoded = decode(run); const chars = Array.from(decoded); if (chars.some((char) => (char.codePointAt(0) ?? 0) < 0x80)) return false; if (run.length > 5) return chars.every((char) => PRINTABLE.test(char)); const char = chars[0] ?? ""; if (!/\p{L}/u.test(char)) return false; const script = scriptOf(char); return script !== undefined && scripts.has(script); } function textFields(questions) { const fields = []; questions.forEach((question, index) => { const at = `questions[${index}]`; fields.push([`${at}.header`, question.header], [`${at}.question`, question.question]); question.options.forEach((option, optionIndex) => { fields.push([`${at}.options[${optionIndex}].label`, option.label]); if (option.description !== undefined) fields.push([`${at}.options[${optionIndex}].description`, option.description]); }); }); return fields; } export function findEscapeResidue(questions) { const fields = textFields(questions).map(([field, text]) => [ field, QUOTED_CODE.reduce((masked, pattern) => masked.replace(pattern, (span) => " ".repeat(span.length)), text), ]); const scripts = new Set(); for (const [, text] of fields) { for (const [char] of text.matchAll(NON_ASCII_LETTER)) { const script = scriptOf(char); if (script !== undefined) scripts.add(script); } } const residue = []; for (const [field, text] of fields) { for (const match of text.matchAll(LITERAL)) { if (Number.parseInt(match[1] ?? "0", 16) >= 0x80) residue.push({ field, token: match[0] }); } for (const match of text.matchAll(BARE_RUN)) { const run = match[1] ?? ""; if (isResidueRun(run, scripts)) residue.push({ field, token: run }); } } return residue; } export function freshEscapeResidueRetry() { return { refusedThisTurn: false, retryOpen: false, retriedThisTurn: false }; } /** * Records any call; true means refuse it. A call made while the retry is open uses it up, clean or not, so a clean * re-call closes the retry and a later residue set is refused once again. A residue call with no open retry is refused. */ export function refuseEscapeResidue(retry, hasResidue) { if (retry.retryOpen) { retry.retriedThisTurn = true; return false; } if (hasResidue) retry.refusedThisTurn = true; return hasResidue; } export function endEscapeResidueTurn(retry) { retry.retryOpen = retry.refusedThisTurn || (retry.retryOpen && !retry.retriedThisTurn); retry.refusedThisTurn = false; retry.retriedThisTurn = false; } export class AskUserEscapeResidueError extends Error { constructor(residue) { const found = residue.map(({ field, token }) => `${field} has "${token}"`).join("; "); super(`Escape residue in the question text: ${found}. Non-ASCII text was written as a \\uXXXX escape that lost its backslash or kept a literal one, so the user would see the code instead of the character. Call this tool again with the same questions, writing every non-ASCII character directly as UTF-8 text, never as a \\uXXXX escape.`); this.name = "AskUserEscapeResidueError"; this.residue = residue; } } export function escapeResidueNote(residue) { const tokens = [...new Set(residue.map(({ token }) => token))].join(", "); return `This question may show undecoded character codes (${tokens}); it is shown as the model wrote it.`; } //# sourceMappingURL=escape-residue.js.map