60fcb494a7
按 jingtingxiang 拍板将 prompt 回到 v4 基线:模板正文恢复内嵌的 格式/语气/句式/词汇/标点全量约束与 11 组正误例(量词规则按术语表 现行裁定 package→包 写作「由三个包构成的 seam」),不再注入 translation-rules.md——该文件约束人和 agent,不进模板;占位符收敛 为 source_lang/target_lang/terminology 三个,切换行由模型按文档 自身拼写。渲染器、解析器、conformance 门禁与单测同步回 v4 契约: 三段裸 XML(translation/review/final 顺序唯一),容忍整体 ```xml 围栏回显;saxes 依赖随 CDATA 协议一并移除。
47 lines
2.3 KiB
TypeScript
47 lines
2.3 KiB
TypeScript
/** Verify that the committed translation prompt renders and parses as documented. */
|
|
|
|
import { readFileSync } from 'node:fs'
|
|
import { join, resolve } from 'node:path'
|
|
import {
|
|
documentedTranslationPromptPlaceholders,
|
|
parseTranslationResponse,
|
|
renderTranslationPrompt,
|
|
renderTranslationResponse,
|
|
TRANSLATION_PROMPT_PLACEHOLDERS,
|
|
} from './translation-prompt.ts'
|
|
|
|
const root = resolve(import.meta.dirname, '..')
|
|
|
|
function read(path: string): string {
|
|
return readFileSync(join(root, path), 'utf8')
|
|
}
|
|
|
|
try {
|
|
const document = read('docs/i18n/translation-prompt.md')
|
|
const terminology = read('docs/i18n/terminology.md')
|
|
const documented = documentedTranslationPromptPlaceholders(document)
|
|
if (documented.join('\n') !== TRANSLATION_PROMPT_PLACEHOLDERS.join('\n')) {
|
|
throw new Error(`placeholder table must list exactly: ${TRANSLATION_PROMPT_PLACEHOLDERS.join(', ')}`)
|
|
}
|
|
|
|
const englishSource = renderTranslationPrompt(document, { sourceLanguage: 'English', terminology })
|
|
const chineseSource = renderTranslationPrompt(document, { sourceLanguage: 'Chinese', terminology })
|
|
if (englishSource.includes('{{') || chineseSource.includes('{{')) throw new Error('rendered prompt contains an unresolved placeholder')
|
|
if (!englishSource.includes('from English to Chinese')) throw new Error('English-source render does not translate into Chinese')
|
|
if (!chineseSource.includes('from Chinese to English')) throw new Error('Chinese-source render does not translate into English')
|
|
|
|
const example = /```xml\n([\s\S]*?)\n```/.exec(englishSource)?.[1]
|
|
if (example === undefined) throw new Error('rendered prompt has no three-section response example')
|
|
parseTranslationResponse(example)
|
|
|
|
const roundTrip = { translation: 'first pass\n\nwith **markdown**', review: '- 无修正', final: 'final text' }
|
|
const parsed = parseTranslationResponse(renderTranslationResponse(roundTrip))
|
|
if (JSON.stringify(parsed) !== JSON.stringify(roundTrip)) throw new Error('three-section response does not round-trip')
|
|
|
|
console.log('verify-translation-prompt: both directions render and the three-section response contract parses.')
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
console.error(`verify-translation-prompt: ${message}`)
|
|
process.exit(1)
|
|
}
|