narrarium-engine 0.1.20 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +41 -16
  2. package/dist/ai/router.d.ts +7 -5
  3. package/dist/ai/router.d.ts.map +1 -1
  4. package/dist/ai/router.js +35 -11
  5. package/dist/ai/router.js.map +1 -1
  6. package/dist/ai/types.d.ts +15 -0
  7. package/dist/ai/types.d.ts.map +1 -1
  8. package/dist/ai/types.js +15 -0
  9. package/dist/ai/types.js.map +1 -1
  10. package/dist/autopilot/campaign.d.ts +101 -0
  11. package/dist/autopilot/campaign.d.ts.map +1 -0
  12. package/dist/autopilot/campaign.js +260 -0
  13. package/dist/autopilot/campaign.js.map +1 -0
  14. package/dist/autopilot/estimate-texts.js +4 -4
  15. package/dist/autopilot/estimate-texts.js.map +1 -1
  16. package/dist/autopilot/frozen.d.ts +27 -2
  17. package/dist/autopilot/frozen.d.ts.map +1 -1
  18. package/dist/autopilot/frozen.js +105 -34
  19. package/dist/autopilot/frozen.js.map +1 -1
  20. package/dist/autopilot/outline-check.js +4 -4
  21. package/dist/autopilot/outline-check.js.map +1 -1
  22. package/dist/autopilot/planner.d.ts.map +1 -1
  23. package/dist/autopilot/planner.js +9 -6
  24. package/dist/autopilot/planner.js.map +1 -1
  25. package/dist/autopilot/runner.d.ts +5 -3
  26. package/dist/autopilot/runner.d.ts.map +1 -1
  27. package/dist/autopilot/runner.js +27 -8
  28. package/dist/autopilot/runner.js.map +1 -1
  29. package/dist/autopilot/session.d.ts +11 -0
  30. package/dist/autopilot/session.d.ts.map +1 -1
  31. package/dist/autopilot/session.js +32 -0
  32. package/dist/autopilot/session.js.map +1 -1
  33. package/dist/autopilot/state.d.ts +8 -1
  34. package/dist/autopilot/state.d.ts.map +1 -1
  35. package/dist/autopilot/state.js +69 -1
  36. package/dist/autopilot/state.js.map +1 -1
  37. package/dist/autopilot/tasks.d.ts.map +1 -1
  38. package/dist/autopilot/tasks.js +31 -10
  39. package/dist/autopilot/tasks.js.map +1 -1
  40. package/dist/autopilot/types.d.ts +68 -1
  41. package/dist/autopilot/types.d.ts.map +1 -1
  42. package/dist/autopilot/types.js +3 -0
  43. package/dist/autopilot/types.js.map +1 -1
  44. package/dist/book/outline-journal.d.ts +149 -0
  45. package/dist/book/outline-journal.d.ts.map +1 -0
  46. package/dist/book/outline-journal.js +321 -0
  47. package/dist/book/outline-journal.js.map +1 -0
  48. package/dist/book/outline.d.ts.map +1 -1
  49. package/dist/book/outline.js +7 -6
  50. package/dist/book/outline.js.map +1 -1
  51. package/dist/book/paths.d.ts +12 -0
  52. package/dist/book/paths.d.ts.map +1 -1
  53. package/dist/book/paths.js +12 -0
  54. package/dist/book/paths.js.map +1 -1
  55. package/dist/index.d.ts +2 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +2 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/server/client.d.ts +10 -1
  60. package/dist/server/client.d.ts.map +1 -1
  61. package/dist/server/client.js +21 -2
  62. package/dist/server/client.js.map +1 -1
  63. package/dist/server/protocol.d.ts +16 -0
  64. package/dist/server/protocol.d.ts.map +1 -1
  65. package/dist/server/protocol.js +12 -0
  66. package/dist/server/protocol.js.map +1 -1
  67. package/dist/sync/files.d.ts +12 -0
  68. package/dist/sync/files.d.ts.map +1 -1
  69. package/dist/sync/files.js +40 -2
  70. package/dist/sync/files.js.map +1 -1
  71. package/dist/version.d.ts +1 -1
  72. package/dist/version.js +1 -1
  73. package/package.json +1 -1
@@ -1,9 +1,10 @@
1
1
  import { normalizeScore, requireStructuredFields, splitReasoningBlock } from "../ai/structured.js";
2
2
  import { AI_CAPABILITIES, AiReplyRejectedError } from "../ai/types.js";
3
3
  import { canonDetails, canonManifest, ghostwriterBlock, isThinDocument, loadChapterParagraphs, loadGhostwriter, markedChapterText, mentionedCanon, readBody, readDocument, secretRules, } from "../book/context.js";
4
- import { AUTOPILOT_PLAN_FILE, BOOK_FILE, CONTEXT_FILE, OUTLINE_BOOK_FILE, PLOT_FILE, STORY_DESIGN_FILE, TOTAL_RESUME_FILE, canonPath, canonSlug, chapterFilePath, chapterResumePath, chapterSlug, directorOpinionPath, formatOrdinal, isValidGhostwriterSlug, outlineChapterPath, paragraphFilePath, paragraphId, paragraphSlug, readerEvaluationPath, readerSummaryPath, } from "../book/paths.js";
4
+ import { AUTOPILOT_PLAN_FILE, BOOK_FILE, CONTEXT_FILE, OUTLINE_BOOK_FILE, OUTLINE_CHANGES_FILE, PLOT_FILE, STORY_DESIGN_FILE, TOTAL_RESUME_FILE, canonPath, canonSlug, chapterFilePath, chapterResumePath, chapterSlug, directorOpinionPath, formatOrdinal, isValidGhostwriterSlug, outlineChapterPath, paragraphFilePath, paragraphId, paragraphSlug, readerEvaluationPath, readerSummaryPath, } from "../book/paths.js";
5
5
  import { chapterFreezeRecord, freezeChapterChanges, frozenChanges, markReviewedChanges } from "../book/freeze.js";
6
6
  import { OUTLINE_TITLE_MAX_CHARS, OutlineReader, outlineBookDigest, outlineEntryDigest, serializeOutlineBook, serializeOutlineChapter } from "../book/outline.js";
7
+ import { appendOutlineJournal, outlineJournalChaptersOf } from "../book/outline-journal.js";
7
8
  import { askDirector, directorSystemPrompt, parseDirectorVerdict, renderDirectorOpinion, withheldApprovalNote } from "../director/opinion.js";
8
9
  import { assertFrontmatterReadable, frontmatterString, parseMarkdown, renderMarkdown } from "../markdown/frontmatter.js";
9
10
  import { evaluationStyleBlock, evaluationStyleInstruction } from "../profiles/evaluation-style.js";
@@ -179,7 +180,7 @@ function outlineGhostwriter(ctx, entry) {
179
180
  if (isValidGhostwriterSlug(slug) && ctx.snapshot.ghostwriterSlugs.includes(slug))
180
181
  return slug;
181
182
  const italian = isItalian(ctx.language);
182
- ctx.insight("warning", italian ? `La Scaletta del capitolo ${entry.number} indica il ghostwriter «${slug}», che non esiste: lo scrive quello del libro` : `The outline of chapter ${entry.number} names the ghostwriter «${slug}», which does not exist: the book's ghostwriter writes it`, italian ? `Crea ghostwriters/${slug}.md, oppure correggi la voce della Scaletta.` : `Create ghostwriters/${slug}.md, or correct the entry of the outline.`, { code: "outlineGhostwriter", chapterNumber: entry.number, ghostwriter: slug });
183
+ ctx.insight("warning", italian ? `La Sinossi del capitolo ${entry.number} indica il ghostwriter «${slug}», che non esiste: lo scrive quello del libro` : `The synopsis of chapter ${entry.number} names the ghostwriter «${slug}», which does not exist: the book's ghostwriter writes it`, italian ? `Crea ghostwriters/${slug}.md, oppure correggi la voce della Sinossi.` : `Create ghostwriters/${slug}.md, or correct the entry of the synopsis.`, { code: "outlineGhostwriter", chapterNumber: entry.number, ghostwriter: slug });
183
184
  return undefined;
184
185
  }
185
186
  /** What the autopilot writes in the outline for a chapter: its title, then its summary and its key events as a list. */
@@ -191,14 +192,18 @@ function outlineContentOf(chapter) {
191
192
  * The outline after a plan: a planned chapter without an entry gets one, as the autopilot's (`origin: autopilot`, with the `digest`
192
193
  * of what it wrote); a chapter the author wrote gets one only when it has none, with its summary; an entry of the autopilot that
193
194
  * nobody touched follows the new plan of its chapter; an entry of the author is never touched. Every chapter remembers the digest
194
- * of its entry as it is after this step, so that a later change of the author is recognized. With `fillOutline` off nothing is
195
- * written. `outline/book.md` is written once, when it is missing, with the logline of the concept.
195
+ * of its entry as it is after this step, so that a later change of the author is recognized; a written chapter the plan knew keeps
196
+ * the digest it followed, so that a change nobody decided about yet is not taken for followed. With `fillOutline` off nothing is
197
+ * written. `outline/book.md` is written once, when it is missing, with the logline of the concept. The entries it writes are a series
198
+ * of the journal of the Synopsis (`outline/changes.yml`), by the autopilot, in the same commit.
196
199
  */
197
200
  async function fillOutline(ctx, planned, imported) {
198
201
  const fill = ctx.state.options.fillOutline !== false;
199
202
  const outline = outlineOf(ctx);
200
203
  const entries = new Map((await outline.chapters()).map((entry) => [entry.number, entry]));
201
204
  const changes = [];
205
+ const was = new Map();
206
+ const now = new Map();
202
207
  const write = async (chapter, existing) => {
203
208
  const content = outlineContentOf(chapter);
204
209
  const digest = await outlineEntryDigest(content);
@@ -208,13 +213,16 @@ async function fillOutline(ctx, planned, imported) {
208
213
  content: serializeOutlineChapter({ number: chapter.number, ...content, origin: "autopilot", digest, frontmatter: existing?.frontmatter }),
209
214
  expectedHash: existing ? existing.hash : null,
210
215
  });
216
+ if (existing)
217
+ was.set(chapter.number, { title: existing.title, digest: existing.contentDigest });
218
+ now.set(chapter.number, { title: content.title, digest });
211
219
  }
212
220
  chapter.outlineDigest = digest;
213
221
  };
214
222
  for (const chapter of imported) {
215
223
  const entry = entries.get(chapter.number);
216
224
  if (entry)
217
- chapter.outlineDigest = entry.contentDigest;
225
+ chapter.outlineDigest ??= entry.contentDigest;
218
226
  else if (fill && chapter.summary.trim())
219
227
  await write(chapter);
220
228
  else
@@ -234,6 +242,17 @@ async function fillOutline(ctx, planned, imported) {
234
242
  if (body)
235
243
  changes.push({ path: OUTLINE_BOOK_FILE, content: serializeOutlineBook({ body, origin: "autopilot", digest: await outlineBookDigest({ body }) }), expectedHash: null });
236
244
  }
245
+ const { chapters, more } = outlineJournalChaptersOf(was, now, ctx.language);
246
+ if (chapters.length) {
247
+ const journal = await ctx.store.read(OUTLINE_CHANGES_FILE);
248
+ const at = ctx.now();
249
+ changes.push({
250
+ path: OUTLINE_CHANGES_FILE,
251
+ // The id comes from the moment of the step: one runner writes at a time, and a run with a fixed clock writes the same file.
252
+ content: appendOutlineJournal(journal?.content, { id: `autopilot-${at.getTime().toString(36)}`, at: at.toISOString(), by: "autopilot", chapters, ...(more ? { more } : {}) }, at),
253
+ expectedHash: journal?.hash ?? null,
254
+ });
255
+ }
237
256
  return changes;
238
257
  }
239
258
  /** The style a step that judges is given, among the data, and the instruction to judge with it; nothing without a style. */
@@ -660,8 +679,10 @@ async function runPlan(ctx) {
660
679
  const imported = importedChapters(ctx.snapshot).map((chapter) => {
661
680
  const earlier = known.get(chapter.slug);
662
681
  const kept = earlier?.storySoFarAtClose ? { ...chapter, storySoFarAtClose: earlier.storySoFarAtClose, ...(earlier.score !== undefined ? { score: earlier.score } : {}) } : chapter;
663
- // The history of the rounds of a chapter that was reviewed outlives the plan being made again around it (the book is being extended).
664
- return earlier?.rounds?.length ? { ...kept, rounds: earlier.rounds } : kept;
682
+ // The history of the rounds of a chapter that was reviewed outlives the plan being made again around it (the book is being extended), and so does the
683
+ // entry of the outline it follows: a change of its entry that nobody decided about yet (asked, left for later) is still one after the plan.
684
+ const followed = earlier?.outlineDigest ? { ...kept, outlineDigest: earlier.outlineDigest } : kept;
685
+ return earlier?.rounds?.length ? { ...followed, rounds: earlier.rounds } : followed;
665
686
  });
666
687
  // The author's outline binds the plan: its chapters, its order, its titles, its events, and as many chapters as it has.
667
688
  const view = await authorOutline(ctx);
@@ -1026,9 +1047,9 @@ async function runReplan(ctx) {
1026
1047
  plan.removedChapters = [...new Set([...(plan.removedChapters ?? []), number])].sort((left, right) => left - right);
1027
1048
  if (stubHash && !written)
1028
1049
  changes.push({ path: stubPath, content: null, expectedHash: stubHash });
1029
- ctx.insight("warning", italian ? `Il capitolo ${number} non è più nella Scaletta: esce dal piano` : `Chapter ${number} is no longer in the outline: it leaves the plan`, italian ? "I capitoli successivi mantengono il loro numero: rinumera la Scaletta se vuoi chiudere il vuoto." : "The following chapters keep their number: renumber the outline if you want to close the gap.", { code: "outlineRemoved", chapterNumber: number });
1050
+ ctx.insight("warning", italian ? `Il capitolo ${number} non è più nella Sinossi: esce dal piano` : `Chapter ${number} is no longer in the synopsis: it leaves the plan`, italian ? "I capitoli successivi mantengono il loro numero: rinumera la Sinossi se vuoi chiudere il vuoto." : "The following chapters keep their number: renumber the synopsis if you want to close the gap.", { code: "outlineRemoved", chapterNumber: number });
1030
1051
  changes.push(await planFileChange(ctx));
1031
- return { changes: compact(changes), summary: italian ? `Il capitolo ${number} è uscito dal piano: la sua voce della Scaletta è stata cancellata.` : `Chapter ${number} left the plan: its entry of the outline was deleted.` };
1052
+ return { changes: compact(changes), summary: italian ? `Il capitolo ${number} è uscito dal piano: la sua voce della Sinossi è stata cancellata.` : `Chapter ${number} left the plan: its entry of the synopsis was deleted.` };
1032
1053
  }
1033
1054
  const [view, design, resume] = await Promise.all([authorOutline(ctx), readBody(ctx.store, STORY_DESIGN_FILE, 2500), storySoFar(ctx, number, 4000)]);
1034
1055
  const secretsPlan = ctx.state.concept.secrets.filter((secret) => secret.revealChapter).map((secret) => `- ${secret.title}: reveal in chapter ${secret.revealChapter}`).join("\n");
@@ -1088,7 +1109,7 @@ async function runReplan(ctx) {
1088
1109
  delete chapter.skipReaders;
1089
1110
  changes.push(await chapterStubChange(ctx, chapter, before));
1090
1111
  changes.push(await planFileChange(ctx));
1091
- ctx.insight("info", italian ? `Hai cambiato la Scaletta del capitolo ${number}: lo ripianifico` : `You changed the outline of chapter ${number}: I am planning it again`, `${number}. ${title}${chapter.summary ? ` — ${chapter.summary}` : ""}`, { code: "outlineChanged", chapterNumber: number });
1112
+ ctx.insight("info", italian ? `Hai cambiato la Sinossi del capitolo ${number}: lo ripianifico` : `You changed the synopsis of chapter ${number}: I am planning it again`, `${number}. ${title}${chapter.summary ? ` — ${chapter.summary}` : ""}`, { code: "outlineChanged", chapterNumber: number });
1092
1113
  return { changes: compact(changes), summary: `${number}. ${title}${chapter.summary ? ` — ${truncate(chapter.summary, 240)}` : ""}` };
1093
1114
  }
1094
1115
  // ─── Scenes and prose ───────────────────────────────────────────────────────