From a66ed5474c91e6bab4851fd7dbf3c18b742dd71b Mon Sep 17 00:00:00 2001 From: Tianyi Cui <53024+tianyicui@users.noreply.github.com> Date: Thu, 23 Jul 2026 21:25:02 +0800 Subject: [PATCH] fix(i18n): avoid prompt parser assertions --- scripts/translation-prompt.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/translation-prompt.ts b/scripts/translation-prompt.ts index 25f1d9ae09..9f093d3e47 100644 --- a/scripts/translation-prompt.ts +++ b/scripts/translation-prompt.ts @@ -92,16 +92,17 @@ export function parseTranslationResponse(text: string): TranslationResponse { if (fenced?.[1] !== undefined) body = fenced[1].trim() const values: Partial> = {} + let previousSectionStart = -1 for (const section of RESPONSE_SECTIONS) { const pattern = new RegExp(`^<${section}>\\n?([\\s\\S]*?)\\n?^$`, 'gm') const first = pattern.exec(body) if (first?.[1] === undefined) throw new Error(`translation response: missing or unterminated <${section}> section`) if (pattern.exec(body) !== null) throw new Error(`translation response: duplicate <${section}> section`) + if (first.index <= previousSectionStart) { + throw new Error('translation response: sections must appear in translation, review, final order') + } + previousSectionStart = first.index values[section] = first[1] } - const order = RESPONSE_SECTIONS.map(section => body.search(new RegExp(`^<${section}>`, 'm'))) - if (!(order[0]! < order[1]! && order[1]! < order[2]!)) { - throw new Error('translation response: sections must appear in translation, review, final order') - } return values as TranslationResponse }