zenstory-ai/oh-story-claudecode / skills/story-long-write
story-long-write
Unverified长篇网文规划与写作。支持只讨论结构、只写大纲或指定细纲,明确要求正文后再写章节。触发方式:/story-long-write、/写长篇、「帮我开书」「定设定」「出卷纲」「规划剧情」「写大纲」「补细纲」「日更」「续写」「继续写」「修改第X章」「回炉」「重写第X章」。
scripts/build_architect_brief.js
View on GitHub#!/usr/bin/env node
'use strict';
/**
* build_architect_brief.js — 把定设定、出卷纲、出一批细纲要用的流程、模板与卷纲段拼成 story-architect 任务包。
*
* 用法:
* node build_architect_brief.js --project <书目录> --task world|volume|outline [--volume N] [--chapters A-B] [--rules-only]
* Exit: 0 = 已写出;2 = 参数或资料缺失。
*/
const fs = require('node:fs');
const path = require('node:path');
const { parse: parseVolume, sliceArc, stripRetired, unitChapterRange } = require('./outline_view.js');
const SKILL_ROOT = path.resolve(__dirname, '..');
const REFERENCES = path.join(SKILL_ROOT, 'references');
const WORK_DIR = ['.story', 'work', '排纲'];
const FENCE_OPEN_RE = /^[ ]{0,3}(`{3,}|~{3,})([^\n]*)$/;
const FENCE_TAG_RE = /(?:^|\s)template=(\S+)/;
const CROSS_BOOK = ['cross-book-recall.md', null, '`设定/题材定位.md`「对标登记」里有 2 本及以上对标书'];
const ROMANCE = ['character-relations.md', '感情线阶段', '本书有感情线'];
const CONDITIONAL = {
world: [['female-audience-writing.md', null, '本书是女频'], ROMANCE, CROSS_BOOK],
volume: [
CROSS_BOOK,
['outline-methods.md', '常见的八步结构', '没有主对标'],
['outline-methods.md', '情绪模块公式', '卷纲里有自创的剧情段'],
['outline-methods.md', '换地图', '本卷换大地图或开新地图'],
['long-reversal.md', '反转类型', '本卷规划了反转'],
ROMANCE,
],
outline: [ROMANCE, CROSS_BOOK],
};
const TEMPLATE_TARGETS = {
题材定位: '设定/题材定位.md',
全书梗概: '设定/全书梗概.md',
关系: '设定/关系.md',
主角卡: '设定/角色/{名}.md(主角)',
配角卡: '设定/角色/{名}.md(配角)',
};
const PLOT_CORE_AFTER_OPENING = [
'目标情绪与主角选择',
'爽点:六类与倒推',
'高潮构建公式',
'情节细化:情节点怎么排',
'留悬念与章尾',
'过渡章与衔接',
];
const NEW_ADDITION_TIERS =
'**新增物三级**(缺东西时三种处理;只看下一章需不需要知道它存在过):\n' +
'- **直接写**:微连接、路人、器物、地名细部、一次性对话、现场细节;不申报。\n' +
'- **补上并记下**:具名配角、势力、复用地点属性、能力形态、规矩、刻度、新伏笔、新关系或承诺;' +
'排纲先落设定档再引用,写正文照写并进申报表。\n' +
'- **先问作者**:新主线事件或反转、金手指规则与力量档位、真相或伏笔结算、经济锚点、读者期待、' +
'提前写后续章、改变细纲已定结果或人物决定、卷纲新增主线或故事线、碰这卷主打的线或后面才揭开的东西、' +
'与既有规矩或承诺冲突;写正文时不写,排纲时挂起列候选,由主会话问作者。\n' +
'拿不准:写时按补上并记下,处置时按先问作者。';
class BriefError extends Error {}
function fail(message) {
throw new BriefError(message);
}
function readReference(name) {
const filePath = path.join(REFERENCES, name);
let stat;
try {
stat = fs.statSync(filePath);
} catch {
fail(`ERROR: 缺少 reference:${filePath}`);
}
if (!stat.isFile()) {
fail(`ERROR: 缺少 reference:${filePath}`);
}
return fs.readFileSync(filePath, 'utf8').replace(/^\ufeff/, '').replace(/\r\n/g, '\n');
}
function headings(text) {
const found = [];
let fence = null;
const lines = text.split('\n');
for (let index = 0; index < lines.length; index += 1) {
const line = lines[index];
const marker = /^\s{0,3}(`{3,}|~{3,})/.exec(line);
if (marker) {
const value = marker[1];
if (fence === null) {
fence = value;
} else if (value[0] === fence[0] && value.length >= fence.length) {
fence = null;
}
continue;
}
if (!fence) {
const match = /^(#{1,6})\s+(.*\S)\s*$/.exec(line);
if (match) {
found.push([index, match[1].length, match[2]]);
}
}
}
return found;
}
function rstrip(str) {
return str.replace(/\s+$/, '');
}
function section(text, ...names) {
const lines = text.split('\n');
const marks = headings(text);
const parts = [];
for (const name of names) {
const hit = marks.findIndex(([, , title]) => title.startsWith(name));
if (hit < 0) {
fail(`ERROR: reference 里找不到小节「${name}」`);
}
const [start, level] = marks[hit];
const nextMark = marks.slice(hit + 1).find(([, lv]) => lv <= level);
const end = nextMark ? nextMark[0] : lines.length;
parts.push(rstrip(lines.slice(start, end).join('\n')));
}
return parts.join('\n\n');
}
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function templateBlock(text, tag, source = '参考文件') {
const lines = text.replace(/^\ufeff/, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n');
const hits = [];
let index = 0;
while (index < lines.length) {
const opened = FENCE_OPEN_RE.exec(lines[index]);
if (!opened || (opened[1][0] === '`' && opened[2].includes('`'))) {
index += 1;
continue;
}
const marker = opened[1];
const found = FENCE_TAG_RE.exec(opened[2]);
const closeRe = new RegExp(`^[ ]{0,3}${escapeRegExp(marker[0])}{${marker.length},}[ \\t]*$`);
let end = lines.length;
for (let row = index + 1; row < lines.length; row += 1) {
if (closeRe.test(lines[row])) {
end = row;
break;
}
}
if (found && found[1] === tag) {
hits.push(lines.slice(index + 1, end).join('\n'));
}
index = end + 1;
}
if (hits.length !== 1) {
const reason = !hits.length ? `找不到模板「${tag}」` : `模板「${tag}」出现了 ${hits.length} 次`;
fail(`ERROR: ${source} 里${reason}`);
}
return hits[0];
}
function templates(name, ...tags) {
const text = readReference(name);
const blocks = ['模板里的小节名、字段名照抄,不改名:写正文的脚本和别的 skill 按名读取。'];
for (const tag of tags) {
const body = templateBlock(text, tag, name);
blocks.push(`### ${TEMPLATE_TARGETS[tag] || tag}\n\n\`\`\`markdown\n${body.trim()}\n\`\`\``);
}
return blocks.join('\n\n');
}
function dropLastColumn(text) {
return text
.split(/\r?\n/)
.map((line) => (line.startsWith('|') ? `${line.split('|').slice(0, -2).join('|')}|` : line))
.join('\n');
}
function conditionalReads(task) {
const lines = [
'下面几份在包外,只在条件满足时读,只读点名的那一节(没点名读整份);条件不满足不读。' +
'路径按你的参考文件路径规则解析;流程提到而这里没列的参考文件不用找,对标资料缺了就停下,说清缺哪份:',
];
for (const [name, part, when] of CONDITIONAL[task]) {
const text = readReference(name);
if (part) {
section(text, part);
}
lines.push(`- ${when}:读 \`agent-references/${name}\` ${part ? `「${part}」一节` : '整份'}`);
}
if (CONDITIONAL[task].includes(CROSS_BOOK)) {
lines.push(`读了 ${CROSS_BOOK[0]} 时:\`gaps.*\` 写进「要作者定的事」,「副对标召回摘要」不单独回。`);
}
return lines.join('\n');
}
function render(sections, chosen, span, preamble) {
const out = preamble && preamble.length ? [rstrip(stripRetired(preamble, false).join('\n'))] : [];
for (const sec of sections) {
if (!chosen.has(sec)) continue;
const body =
sec.title.includes('情绪弧线') || sec.title.includes('排班')
? sliceArc(sec, span)
: sec.lines;
out.push(rstrip(stripRetired(body, false).join('\n')));
}
return `${out.join('\n\n')}\n`;
}
function compress(numbers) {
const spans = [];
let start = numbers[0];
for (let i = 0; i < numbers.length; i += 1) {
const prev = numbers[i];
const cur = i + 1 < numbers.length ? numbers[i + 1] : null;
if (cur !== prev + 1) {
spans.push(start !== prev ? `${start}-${prev}` : `${start}`);
start = cur;
}
}
return spans.join('、');
}
function listVolumeFiles(project) {
const dir = path.join(project, '大纲');
let entries;
try {
entries = fs.readdirSync(dir);
} catch {
return [];
}
return entries
.filter((name) => /^卷纲_第.*卷\.md$/.test(name))
.sort()
.map((name) => path.join(dir, name));
}
function unitClosures(project, first, last) {
const found = [];
const covered = new Set();
const undeclared = [];
for (const volumePath of listVolumeFiles(project)) {
const volumeName = path.basename(volumePath);
const { lines, sections } = parseVolume(fs.readFileSync(volumePath, 'utf8').replace(/^\ufeff/, ''));
for (const s of sections) {
if (s.scope === null && s.unit && (s.title.includes('剧情单元') || s.title.includes('段剧情'))) {
undeclared.push(`${volumeName}「${s.title.trim()}」`);
}
}
if (sections.some((s) => s.scope && (s.kind === null || (s.kind !== '卷级常任' && s.unit === null)))) {
fail(`ERROR: ${volumeName} 标题注释(或旧的作用域行)写得不对,先跑 outline_view.js --check 修正`);
}
const active = sections.filter((s) => s.status !== '已退役');
const shared = active.filter((s) => s.kind === '卷级常任' || s.scope === null);
const units = [...new Set(active.filter((s) => s.unit && s.kind === '单元级').map((s) => s.unit))].sort();
let given = false;
for (const unit of units) {
const span = unitChapterRange(active, unit);
if (!(span && span[0] <= last && span[1] >= first)) continue;
for (let ch = span[0]; ch <= span[1]; ch += 1) covered.add(ch);
const live = active.filter((s) => s.unit === unit && s.kind === '单元级');
let text;
if (given) {
text = `(全卷段同上一段,不重复。)\n\n${render(sections, new Set(live), span, null)}`;
} else {
text = render(
sections,
new Set([...shared, ...live]),
span,
sections.length ? lines.slice(0, sections[0].start) : null
);
}
given = true;
found.push([
`${volumeName} · 剧情段 ${unit}(第${span[0]}-${span[1]}章;排纲笔记写 .story/排纲笔记/${unit}.md)`,
text,
]);
}
}
const missing = [];
for (let n = first; n <= last; n += 1) {
if (!covered.has(n)) missing.push(n);
}
if (missing.length) {
const hint = undeclared.length
? `;这些剧情段的标题末尾缺单元编号注释(如 <!-- L1-01 -->):${undeclared.join('、')}`
: '(每段剧情的标题末尾写单元编号注释,如「### 第一段剧情:退婚风波 <!-- L1-01 -->」,卡里写「章节范围:第A-B章」)';
const where = !found.length ? '找不到覆盖' : '剧情段只覆盖了一部分,缺覆盖';
fail(
`ERROR: 卷纲里${where}第${compress(missing)}章的剧情段${hint},先补卷纲` +
'(`outline_view.js --check --strict` 列出全部问题)'
);
}
return found;
}
function pad3(n) {
return String(n).padStart(3, '0');
}
function build(task, volume, chapters, project = null, rulesOnly = false) {
const outside = '写作技法只读本包,外加包末「按条件可读」里条件满足的那几节。';
let head;
let parts;
if (task === 'world') {
head =
'# 任务包:出核心设定提案\n\n' +
'按下面 Phase 2 的流程,把核心设定写成文件:`设定/题材定位.md`(在定方向时已写的部分上补全,「题材框架」留到出卷纲补,不删作者已定的内容)、' +
'`设定/全书梗概.md`(故事话写主角最终要什么、最大阻碍、前几次大转折;结局一节没想好可空)、' +
'`设定/关系.md`、主角与关键角色的 `设定/角色/{名}.md`、影响全书的 `设定/世界观/{主题}.md`。' +
'作者在定方向时说过的话都在 `设定/题材定位.md`,先读它;拿不准的写成二选一的候选留给作者定,不替作者拍板,' +
'物价、收入、力量档位这类全书刻度按下附「新增物三级」属问作者,只给候选。' +
`不出卷纲、不写正文。${outside}交付后只回:写了哪些文件、主角与核心冲突各一句、要作者定的事(没有写「无」)。`;
const setup = readReference('workflow-setup.md');
parts = [
['workflow-setup.md(Phase 2)', section(setup, 'Phase 2:核心设定')],
['workflow-setup.md(题材定位、全书梗概、关系模板)', templates('workflow-setup.md', '题材定位', '全书梗概', '关系')],
['character-basics.md', readReference('character-basics.md')],
[
'long-genre-mechanics.md(核心梗三层递进设计、金手指)',
section(readReference('long-genre-mechanics.md'), '核心梗三层递进设计', '金手指'),
],
['character-relations.md(人物关系类型)', section(readReference('character-relations.md'), '人物关系类型')],
['reader-contract-and-progression.md', readReference('reader-contract-and-progression.md')],
];
} else if (task === 'volume') {
const label = volume ? `第${volume}卷` : '本卷';
head =
`# 任务包:出${label}卷纲\n\n` +
`只交付 \`大纲/大纲.md\` 的分卷表(已有就只补本卷那一行)、\`大纲/卷纲_第${volume || 'X'}卷.md\`,` +
'并补齐 `设定/题材定位.md`「题材框架」的「关键转折节点」(标题写 `## 题材框架`,去掉「(出卷纲时补)」,其余小节不动);' +
'不出细纲、不写正文、不建 `追踪/`。本书方向、设定、对标登记与作者已定的事读 `设定/`(先读 `设定/题材定位.md`)。' +
`${outside}交付后只回:写了哪些文件、排纲自查四项的结论各一句、要作者定的事(没有写「无」)。`;
parts = [
['workflow-volume.md(排纲方法)', section(readReference('workflow-volume.md'), '排纲方法')],
['artifact-protocols.md', readReference('artifact-protocols.md')],
['outline-methods.md(卷纲排法)', section(readReference('outline-methods.md'), '卷纲排法')],
['reader-contract-and-progression.md', readReference('reader-contract-and-progression.md')],
];
} else {
const [first, last] = chapters;
head =
`# 任务包:出第${first}-${last}章细纲\n\n` +
`只交付 \`大纲/细纲_第${pad3(first)}章\` 到第${pad3(last)}章,以及排纲笔记(\`.story/排纲笔记/{剧情段编号}.md\`)与必要的设定补全,` +
'不写正文、不建 `追踪/`。本批涉及的卷纲剧情段附在包末(已按段取好),卷纲本身不整读;设定从 `设定/` 定点读;' +
`${outside}交付后只回:写了哪些文件、每章一句核心事件、要作者定的事(没有写「无」)。`;
const plot = readReference('plot-core-methods.md');
parts = [
['workflow-outline.md(排纲方法)', section(readReference('workflow-outline.md'), '排纲方法')],
['outline-template.md', readReference('outline-template.md')],
[
'long-chapter-hooks.md(章尾钩子 13 式、章首钩子 7 式)',
dropLastColumn(section(readReference('long-chapter-hooks.md'), '章尾钩子 13 式', '章首钩子 7 式')),
],
['character-basics.md(主角卡、配角卡模板)', templates('character-basics.md', '主角卡', '配角卡')],
];
if (first <= 3) {
parts.push(['plot-core-methods.md', plot]);
parts.push(['opening-design.md(开篇规划)', section(readReference('opening-design.md'), '开篇规划')]);
} else {
parts.push(['plot-core-methods.md(不含开篇噱头)', section(plot, ...PLOT_CORE_AFTER_OPENING)]);
}
if (!rulesOnly) {
for (const [name, text] of unitClosures(project, first, last)) {
parts.push([`卷纲取段:${name}`, text]);
}
}
}
parts.push(['SKILL.md(新增物三级)', NEW_ADDITION_TIERS]);
parts.push(['按条件可读(包外)', conditionalReads(task)]);
const body = [
head +
'\n\n包里流程写到的脚本命令(取段、`--check`、`check-outline-contract.js`)由主会话在你交付后跑,你不执行命令;需要的取段结果已放进包里。',
];
for (const [name, text] of parts) {
body.push(`\n---\n\n<!-- 资料:${name} -->\n\n${text.trim()}\n`);
}
return { text: body.join('\n'), included: parts.map(([name]) => name) };
}
function parseRange(value) {
const match = /^\s*(\d+)\s*(?:-\s*(\d+))?\s*$/.exec(value || '');
if (!match) {
fail('ERROR: --chapters 写成 A-B,如 1-10');
}
const first = Number(match[1]);
const last = Number(match[2] || match[1]);
if (first < 1 || last < first) {
fail('ERROR: --chapters 范围无效');
}
return [first, last];
}
function parseCliArgs(argv) {
const args = { project: null, task: null, volume: null, chapters: null, rulesOnly: false };
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (token === '--project') {
if (i + 1 >= argv.length) return { error: '--project requires a value' };
args.project = argv[++i];
} else if (token.startsWith('--project=')) {
args.project = token.slice('--project='.length);
} else if (token === '--task') {
if (i + 1 >= argv.length) return { error: '--task requires a value' };
args.task = argv[++i];
} else if (token.startsWith('--task=')) {
args.task = token.slice('--task='.length);
} else if (token === '--volume') {
if (i + 1 >= argv.length) return { error: '--volume requires an integer' };
const raw = argv[++i];
if (!/^-?\d+$/.test(raw)) return { error: `invalid --volume: ${raw}` };
args.volume = Number(raw);
} else if (token.startsWith('--volume=')) {
const raw = token.slice('--volume='.length);
if (!/^-?\d+$/.test(raw)) return { error: `invalid --volume: ${raw}` };
args.volume = Number(raw);
} else if (token === '--chapters') {
if (i + 1 >= argv.length) return { error: '--chapters requires a value' };
args.chapters = argv[++i];
} else if (token.startsWith('--chapters=')) {
args.chapters = token.slice('--chapters='.length);
} else if (token === '--rules-only') {
args.rulesOnly = true;
} else {
return { error: `unrecognized argument: ${token}` };
}
}
if (!args.project) return { error: 'missing required --project' };
if (!['world', 'volume', 'outline'].includes(args.task)) {
return { error: 'missing or invalid --task (expected world|volume|outline)' };
}
return args;
}
function main(argv = process.argv.slice(2)) {
const args = parseCliArgs(argv);
if (args.error) {
process.stderr.write(`ERROR: ${args.error}\n`);
return 2;
}
let text;
let included;
let chapters = null;
try {
let isDir = false;
try {
isDir = fs.statSync(args.project).isDirectory();
} catch {
isDir = false;
}
if (!isDir) {
fail(`ERROR: 书目录不存在:${args.project}`);
}
chapters = args.task === 'outline' ? parseRange(args.chapters) : null;
if (args.task === 'outline' && chapters[1] - chapters[0] >= 10) {
fail('ERROR: 一批细纲最多 10 章,拆成两批');
}
({ text, included } = build(args.task, args.volume, chapters, args.project, args.rulesOnly));
} catch (err) {
if (err instanceof BriefError) {
process.stderr.write(`${err.message}\n`);
return 2;
}
throw err;
}
const outDir = path.join(args.project, ...WORK_DIR);
fs.mkdirSync(outDir, { recursive: true });
const name =
args.task === 'world'
? '核心设定.md'
: args.task === 'volume'
? args.volume
? `卷纲_第${args.volume}卷.md`
: '卷纲.md'
: `细纲_第${pad3(chapters[0])}-${pad3(chapters[1])}章.md`;
const out = path.join(outDir, name);
fs.writeFileSync(out, text, 'utf8');
process.stdout.write(
`${JSON.stringify({ brief: out, chars: text.replace(/\s/g, '').length, includes: included })}\n`
);
return 0;
}
if (require.main === module) {
process.exitCode = main();
}
module.exports = { build, templateBlock, parseRange, main };
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |