import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import assert from 'node:assert/strict'; import { pathToFileURL } from 'node:url'; export const manualFile = 'docs/workshop-quality-r13-20261110/final-康师傅工作坊操作手册.html'; export const bankFile = 'skills/ksf-workshop-runtime/references/workshop-dialogue-prompts.json'; const evidenceDir = 'docs/workshop-dialogue-guide-20261111'; const sha = text => crypto.createHash('sha256').update(text).digest('hex'); export function compileDialogue(bank, steps) { assert.equal(bank.schema, 'ksf.workshop-dialogue-prompts.v1'); assert.deepEqual(Object.keys(bank.steps).sort(), steps.map(step => step.id).sort(), '提示词与手册环节必须一一对应'); return Object.fromEntries(steps.map(step => { const section = bank.steps[step.id]; assert.equal(section.title, step.title); assert.ok(section.rounds.length >= 2 && section.rounds.length <= 4); return [step.id, section.rounds.map((round, index) => { assert.ok(round.title && round.task && round.feedback); const researchStart = section.rounds.findIndex(item => item.purpose !== 'account-binding'); const topicStart = step.id === 'topic' && index === researchStart; const topicEnd = step.id === 'topic' && index === section.rounds.length - 1; return { title: round.title, feedback: round.feedback, prerequisite: round.prerequisite || null, prompt: [topicStart ? bank.shared.opening : topicEnd ? bank.shared.continuation : null, `当前环节:${step.title};第${index + 1}轮:${round.title}。`, round.task, topicStart ? '本次工作坊按多轮对话推进:' + bank.shared.stop : null, topicEnd ? '以下约定适用于本课题后续各轮,无需每次重述:\n' + bank.shared.evidence + '\n\n' + bank.shared.delivery : null ].filter(Boolean).join('\n\n') }; })]; })); } export function updateManual() { let html = fs.readFileSync(manualFile, 'utf8'); const dataMatch = html.match(/window\.WORKSHOP_MANUAL_DATA = (\{[\s\S]*?\n\});\r?\n<\/script>/); assert.ok(dataMatch, '手册数据结构变化,停止修改'); const data = JSON.parse(dataMatch[1]); const bank = JSON.parse(fs.readFileSync(bankFile, 'utf8')); const map = compileDialogue(bank, data.steps); const oldMap = html.match(/ const promptMap = (\{[^\r\n]+\});/); const currentMap = html.match(/ const dialogueMap = (\{[^\r\n]+\});/); assert.ok(oldMap || currentMap, '未找到手册实际使用的提示词'); fs.mkdirSync(evidenceDir, { recursive: true }); const beforeFile = path.join(evidenceDir, 'manual-before.html'); if (!fs.existsSync(beforeFile)) { fs.writeFileSync(beforeFile, html); fs.writeFileSync(path.join(evidenceDir, 'before.json'), JSON.stringify({ manualSha256: sha(html), contentVersion: data.release.contentVersion, steps: data.steps.length, promptCount: oldMap ? Object.keys(JSON.parse(oldMap[1])).length : null, mode: oldMap ? 'single-prompt-per-step' : 'multi-round' }, null, 2) + '\n'); } data.release.contentVersion = bank.revision; data.release.deploymentStatus = 'local-pending-deployment'; data.release.productionCutoverConfirmed = false; data.common = '01首轮确认称呼与账号,随后确定课题;研究通用约定集中在课题研究的开场与收尾,后续各轮沿用Skill规则。'; const topic = data.steps.find(step => step.id === 'topic'); if (bank.steps.topic.rounds[0]?.purpose === 'account-binding') { topic.action = '先复制第一轮确认称呼与登录账号,再依次复制第二、三轮确定本组课题;每轮发送后先看结果再继续。'; topic.prompt = map.topic[0].prompt; } html = html.replace(dataMatch[0], 'window.WORKSHOP_MANUAL_DATA = ' + JSON.stringify(data, null, 2) + ';\n'); const serialized = JSON.stringify(map).replace(/ promptMap[step.id];', ` function renderPrompts(step) { const rounds = dialogueMap[step.id]; return '
每次只复制一轮,发送后先看结果,再反馈或发送下一轮。也可直接回复“继续”;需要业务选择时先回答,不必重复粘贴前文。
' + rounds.map((round, index) => { const promptId = 'prompt-' + step.id + '-' + index; return \`' + escape(round.prerequisite) + '
' : ''}\${escape(round.prompt)}看完怎样反馈:\${escape(round.feedback)}
${escape(fullPrompt(step))}