narrarium-engine 0.0.0-stage → 0.1.0

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 (142) hide show
  1. package/README.md +104 -2
  2. package/dist/ai/config.d.ts +69 -0
  3. package/dist/ai/config.d.ts.map +1 -0
  4. package/dist/ai/config.js +100 -0
  5. package/dist/ai/config.js.map +1 -0
  6. package/dist/ai/fetch-client.d.ts +34 -0
  7. package/dist/ai/fetch-client.d.ts.map +1 -0
  8. package/dist/ai/fetch-client.js +322 -0
  9. package/dist/ai/fetch-client.js.map +1 -0
  10. package/dist/ai/router.d.ts +20 -0
  11. package/dist/ai/router.d.ts.map +1 -0
  12. package/dist/ai/router.js +95 -0
  13. package/dist/ai/router.js.map +1 -0
  14. package/dist/ai/structured.d.ts +59 -0
  15. package/dist/ai/structured.d.ts.map +1 -0
  16. package/dist/ai/structured.js +210 -0
  17. package/dist/ai/structured.js.map +1 -0
  18. package/dist/ai/token-limit.d.ts +5 -0
  19. package/dist/ai/token-limit.d.ts.map +1 -0
  20. package/dist/ai/token-limit.js +16 -0
  21. package/dist/ai/token-limit.js.map +1 -0
  22. package/dist/ai/types.d.ts +86 -0
  23. package/dist/ai/types.d.ts.map +1 -0
  24. package/dist/ai/types.js +46 -0
  25. package/dist/ai/types.js.map +1 -0
  26. package/dist/autopilot/planner.d.ts +56 -0
  27. package/dist/autopilot/planner.d.ts.map +1 -0
  28. package/dist/autopilot/planner.js +357 -0
  29. package/dist/autopilot/planner.js.map +1 -0
  30. package/dist/autopilot/prompts.d.ts +16 -0
  31. package/dist/autopilot/prompts.d.ts.map +1 -0
  32. package/dist/autopilot/prompts.js +284 -0
  33. package/dist/autopilot/prompts.js.map +1 -0
  34. package/dist/autopilot/runner.d.ts +92 -0
  35. package/dist/autopilot/runner.d.ts.map +1 -0
  36. package/dist/autopilot/runner.js +531 -0
  37. package/dist/autopilot/runner.js.map +1 -0
  38. package/dist/autopilot/session.d.ts +84 -0
  39. package/dist/autopilot/session.d.ts.map +1 -0
  40. package/dist/autopilot/session.js +248 -0
  41. package/dist/autopilot/session.js.map +1 -0
  42. package/dist/autopilot/state.d.ts +26 -0
  43. package/dist/autopilot/state.d.ts.map +1 -0
  44. package/dist/autopilot/state.js +398 -0
  45. package/dist/autopilot/state.js.map +1 -0
  46. package/dist/autopilot/story-so-far.d.ts +51 -0
  47. package/dist/autopilot/story-so-far.d.ts.map +1 -0
  48. package/dist/autopilot/story-so-far.js +137 -0
  49. package/dist/autopilot/story-so-far.js.map +1 -0
  50. package/dist/autopilot/tasks.d.ts +42 -0
  51. package/dist/autopilot/tasks.d.ts.map +1 -0
  52. package/dist/autopilot/tasks.js +1354 -0
  53. package/dist/autopilot/tasks.js.map +1 -0
  54. package/dist/autopilot/types.d.ts +373 -0
  55. package/dist/autopilot/types.d.ts.map +1 -0
  56. package/dist/autopilot/types.js +2 -0
  57. package/dist/autopilot/types.js.map +1 -0
  58. package/dist/book/context.d.ts +55 -0
  59. package/dist/book/context.d.ts.map +1 -0
  60. package/dist/book/context.js +163 -0
  61. package/dist/book/context.js.map +1 -0
  62. package/dist/book/paths.d.ts +57 -0
  63. package/dist/book/paths.d.ts.map +1 -0
  64. package/dist/book/paths.js +92 -0
  65. package/dist/book/paths.js.map +1 -0
  66. package/dist/book/snapshot.d.ts +69 -0
  67. package/dist/book/snapshot.d.ts.map +1 -0
  68. package/dist/book/snapshot.js +139 -0
  69. package/dist/book/snapshot.js.map +1 -0
  70. package/dist/book/store.d.ts +39 -0
  71. package/dist/book/store.d.ts.map +1 -0
  72. package/dist/book/store.js +28 -0
  73. package/dist/book/store.js.map +1 -0
  74. package/dist/director/opinion.d.ts +80 -0
  75. package/dist/director/opinion.d.ts.map +1 -0
  76. package/dist/director/opinion.js +302 -0
  77. package/dist/director/opinion.js.map +1 -0
  78. package/dist/index.d.ts +33 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.js +33 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/markdown/frontmatter.d.ts +17 -0
  83. package/dist/markdown/frontmatter.d.ts.map +1 -0
  84. package/dist/markdown/frontmatter.js +67 -0
  85. package/dist/markdown/frontmatter.js.map +1 -0
  86. package/dist/profiles/director.d.ts +35 -0
  87. package/dist/profiles/director.d.ts.map +1 -0
  88. package/dist/profiles/director.js +169 -0
  89. package/dist/profiles/director.js.map +1 -0
  90. package/dist/profiles/ghostwriter.d.ts +30 -0
  91. package/dist/profiles/ghostwriter.d.ts.map +1 -0
  92. package/dist/profiles/ghostwriter.js +140 -0
  93. package/dist/profiles/ghostwriter.js.map +1 -0
  94. package/dist/profiles/readers.d.ts +35 -0
  95. package/dist/profiles/readers.d.ts.map +1 -0
  96. package/dist/profiles/readers.js +189 -0
  97. package/dist/profiles/readers.js.map +1 -0
  98. package/dist/prompt/trust.d.ts +6 -0
  99. package/dist/prompt/trust.d.ts.map +1 -0
  100. package/dist/prompt/trust.js +12 -0
  101. package/dist/prompt/trust.js.map +1 -0
  102. package/dist/readers/evaluation.d.ts +26 -0
  103. package/dist/readers/evaluation.d.ts.map +1 -0
  104. package/dist/readers/evaluation.js +92 -0
  105. package/dist/readers/evaluation.js.map +1 -0
  106. package/dist/server/client.d.ts +90 -0
  107. package/dist/server/client.d.ts.map +1 -0
  108. package/dist/server/client.js +231 -0
  109. package/dist/server/client.js.map +1 -0
  110. package/dist/server/protocol.d.ts +225 -0
  111. package/dist/server/protocol.d.ts.map +1 -0
  112. package/dist/server/protocol.js +120 -0
  113. package/dist/server/protocol.js.map +1 -0
  114. package/dist/sync/files.d.ts +43 -0
  115. package/dist/sync/files.d.ts.map +1 -0
  116. package/dist/sync/files.js +88 -0
  117. package/dist/sync/files.js.map +1 -0
  118. package/dist/testing/index.d.ts +46 -0
  119. package/dist/testing/index.d.ts.map +1 -0
  120. package/dist/testing/index.js +202 -0
  121. package/dist/testing/index.js.map +1 -0
  122. package/dist/util/abort.d.ts +11 -0
  123. package/dist/util/abort.d.ts.map +1 -0
  124. package/dist/util/abort.js +53 -0
  125. package/dist/util/abort.js.map +1 -0
  126. package/dist/util/hash.d.ts +4 -0
  127. package/dist/util/hash.d.ts.map +1 -0
  128. package/dist/util/hash.js +11 -0
  129. package/dist/util/hash.js.map +1 -0
  130. package/dist/util/json.d.ts +20 -0
  131. package/dist/util/json.d.ts.map +1 -0
  132. package/dist/util/json.js +133 -0
  133. package/dist/util/json.js.map +1 -0
  134. package/dist/util/text.d.ts +17 -0
  135. package/dist/util/text.d.ts.map +1 -0
  136. package/dist/util/text.js +99 -0
  137. package/dist/util/text.js.map +1 -0
  138. package/dist/version.d.ts +3 -0
  139. package/dist/version.d.ts.map +1 -0
  140. package/dist/version.js +3 -0
  141. package/dist/version.js.map +1 -0
  142. package/package.json +51 -4
@@ -0,0 +1,84 @@
1
+ import type { AiPort } from "../ai/types.js";
2
+ import { type EvaluationTarget } from "../book/paths.js";
3
+ import { type BookStore } from "../book/store.js";
4
+ import { type AskDirectorResult } from "../director/opinion.js";
5
+ import { type AutopilotHooks } from "./runner.js";
6
+ import type { AutopilotEvent, AutopilotSnapshot, AutopilotState, InterventionInput, QuestionAnswer, StartOptions } from "./types.js";
7
+ /** Minutes after which a `running` state without heartbeat is treated as interrupted. */
8
+ export declare const RUNNER_STALE_MINUTES = 20;
9
+ export declare class AutopilotBusyError extends Error {
10
+ readonly runnerLabel: string;
11
+ constructor(runnerLabel: string);
12
+ }
13
+ /** Whether a stored runner is still alive according to its heartbeat. */
14
+ export declare function runnerActivity(state: AutopilotState | null, now?: Date): "none" | "active" | "stale";
15
+ export interface AutopilotSessionConfig {
16
+ bookId: string;
17
+ store: BookStore;
18
+ ai: AiPort;
19
+ host: "browser" | "server";
20
+ hostLabel: string;
21
+ /** Stable identity of this host (device or server id). Without it the label identifies the host. */
22
+ hostId?: string;
23
+ /**
24
+ * Whether a run recorded by this same host is alive outside this session (for example in
25
+ * another tab of the same browser). Without it, such a run is treated as interrupted.
26
+ */
27
+ ownRunnerAlive?: () => boolean | Promise<boolean>;
28
+ now?: () => Date;
29
+ hooks?: AutopilotHooks;
30
+ /** Live events kept in memory for late subscribers (default 400). */
31
+ maxEvents?: number;
32
+ retryDelayMs?: number;
33
+ }
34
+ export type AutopilotListener = (event: AutopilotEvent) => void;
35
+ /**
36
+ * Control surface of one book's autopilot. The browser and the personal server both use
37
+ * this class, so start, pause, interventions, questions and live events behave identically.
38
+ */
39
+ export declare class AutopilotSession {
40
+ private readonly config;
41
+ private run;
42
+ private runPromise;
43
+ private seq;
44
+ private events;
45
+ private readonly listeners;
46
+ private live;
47
+ private readonly now;
48
+ constructor(config: AutopilotSessionConfig);
49
+ get bookId(): string;
50
+ get running(): boolean;
51
+ get lastSeq(): number;
52
+ subscribe(listener: AutopilotListener): () => void;
53
+ /** Live events after `since` (exclusive), oldest first. */
54
+ recentEvents(since?: number): AutopilotEvent[];
55
+ private emit;
56
+ /** A runner recorded by this host (this session, another tab, or a previous process). */
57
+ isOwnRunner(runner: AutopilotState["runner"]): boolean;
58
+ private ownRunnerAliveElsewhere;
59
+ snapshot(): Promise<AutopilotSnapshot>;
60
+ /** Start or resume. Resolves once the run has saved its start, rejects if it could not start. */
61
+ start(options?: StartOptions & {
62
+ force?: boolean;
63
+ }): Promise<void>;
64
+ /** `now` abandons the request in flight; `after-step` lets the current step finish and save. */
65
+ pause(mode?: "now" | "after-step"): boolean;
66
+ /** Resolves when no run is active. */
67
+ waitForIdle(): Promise<void>;
68
+ private mutate;
69
+ /** Record an author intervention. While running it reaches the next step (or interrupts the current one). */
70
+ intervene(input: InterventionInput): Promise<AutopilotState | null>;
71
+ answer(answer: QuestionAnswer): Promise<AutopilotState>;
72
+ /** Change the brief or the options while the autopilot is not running. */
73
+ configure(input: StartOptions): Promise<AutopilotState>;
74
+ /** Forget the autopilot state (plan, history, rules). The book's chapters are never touched. */
75
+ reset(): Promise<void>;
76
+ /** "What does the director think?" on the book, a chapter or a scene. */
77
+ askDirector(input: {
78
+ target: EvaluationTarget;
79
+ question?: string;
80
+ save?: boolean;
81
+ signal?: AbortSignal;
82
+ }): Promise<AskDirectorResult>;
83
+ }
84
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/autopilot/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAsE,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC7H,OAAO,EAAuB,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAe,KAAK,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC7E,OAAO,EAOL,KAAK,cAAc,EACpB,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAEV,cAAc,EAEd,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACb,MAAM,YAAY,CAAC;AAEpB,yFAAyF;AACzF,eAAO,MAAM,oBAAoB,KAAK,CAAC;AAEvC,qBAAa,kBAAmB,SAAQ,KAAK;IAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM;gBAAnB,WAAW,EAAE,MAAM;CAIzC;AAED,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,EAAE,GAAG,OAAa,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAK1G;AAED,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,SAAS,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,SAAS,GAAG,QAAQ,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,oGAAoG;IACpG,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClD,GAAG,CAAC,EAAE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I,CAAC;YAIpB,MAAM;IAepB,6GAA6G;IACvG,SAAS,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;IAUnE,MAAM,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;IAK7D,0EAA0E;IACpE,SAAS,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC;IAS7D,gGAAgG;IAC1F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAW5B,yEAAyE;IACzE,WAAW,CAAC,KAAK,EAAE;QAAE,MAAM,EAAE,gBAAgB,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAGtI"}
@@ -0,0 +1,248 @@
1
+ import { AUTOPILOT_ACTIVITY_FILE, AUTOPILOT_PLAN_FILE, AUTOPILOT_STATE_FILE } from "../book/paths.js";
2
+ import { isBookConflictError } from "../book/store.js";
3
+ import { askDirector } from "../director/opinion.js";
4
+ import { AutopilotRun, applyAnswer, applyIntervention, readStoredAutopilot, stampState, stateChange, } from "./runner.js";
5
+ import { createAutopilotState, normalizeBrief, normalizeOptions, summarizeAutopilotState } from "./state.js";
6
+ /** Minutes after which a `running` state without heartbeat is treated as interrupted. */
7
+ export const RUNNER_STALE_MINUTES = 20;
8
+ export class AutopilotBusyError extends Error {
9
+ runnerLabel;
10
+ constructor(runnerLabel) {
11
+ super(`The autopilot of this book is already running on ${runnerLabel}.`);
12
+ this.runnerLabel = runnerLabel;
13
+ this.name = "AutopilotBusyError";
14
+ }
15
+ }
16
+ /** Whether a stored runner is still alive according to its heartbeat. */
17
+ export function runnerActivity(state, now = new Date()) {
18
+ if (!state || state.status !== "running" || !state.runner)
19
+ return "none";
20
+ const heartbeat = Date.parse(state.runner.heartbeatAt);
21
+ if (!Number.isFinite(heartbeat))
22
+ return "stale";
23
+ return now.getTime() - heartbeat > RUNNER_STALE_MINUTES * 60_000 ? "stale" : "active";
24
+ }
25
+ /**
26
+ * Control surface of one book's autopilot. The browser and the personal server both use
27
+ * this class, so start, pause, interventions, questions and live events behave identically.
28
+ */
29
+ export class AutopilotSession {
30
+ config;
31
+ run = null;
32
+ runPromise = null;
33
+ seq = 0;
34
+ events = [];
35
+ listeners = new Set();
36
+ live = null;
37
+ now;
38
+ constructor(config) {
39
+ this.config = config;
40
+ this.now = config.now ?? (() => new Date());
41
+ }
42
+ get bookId() {
43
+ return this.config.bookId;
44
+ }
45
+ get running() {
46
+ return this.run !== null;
47
+ }
48
+ get lastSeq() {
49
+ return this.seq;
50
+ }
51
+ subscribe(listener) {
52
+ this.listeners.add(listener);
53
+ return () => this.listeners.delete(listener);
54
+ }
55
+ /** Live events after `since` (exclusive), oldest first. */
56
+ recentEvents(since = 0) {
57
+ return this.events.filter((event) => event.seq > since);
58
+ }
59
+ emit(body) {
60
+ const event = { ...body, bookId: this.config.bookId, at: this.now().toISOString(), seq: ++this.seq };
61
+ if (body.type === "task-started")
62
+ this.live = { activity: body.activity, characters: 0, preview: "" };
63
+ else if (body.type === "task-progress" && this.live?.activity.id === body.activityId)
64
+ this.live = { ...this.live, characters: body.characters, preview: body.preview };
65
+ else if (body.type === "task-finished" && this.live?.activity.id === body.activity.id)
66
+ this.live = null;
67
+ else if (body.type === "run-ended")
68
+ this.live = null;
69
+ // Streaming previews are only useful live; keep the replay buffer for meaningful events.
70
+ if (body.type !== "task-progress") {
71
+ this.events.push(event);
72
+ const max = this.config.maxEvents ?? 400;
73
+ if (this.events.length > max)
74
+ this.events = this.events.slice(-max);
75
+ }
76
+ for (const listener of this.listeners) {
77
+ try {
78
+ listener(event);
79
+ }
80
+ catch {
81
+ // A broken listener must not stop the others.
82
+ }
83
+ }
84
+ }
85
+ /** A runner recorded by this host (this session, another tab, or a previous process). */
86
+ isOwnRunner(runner) {
87
+ if (!runner || runner.host !== this.config.host)
88
+ return false;
89
+ return runner.hostId && this.config.hostId ? runner.hostId === this.config.hostId : runner.label === this.config.hostLabel;
90
+ }
91
+ async ownRunnerAliveElsewhere() {
92
+ if (this.running || !this.config.ownRunnerAlive)
93
+ return false;
94
+ try {
95
+ return Boolean(await this.config.ownRunnerAlive());
96
+ }
97
+ catch {
98
+ return false;
99
+ }
100
+ }
101
+ async snapshot() {
102
+ const stored = await readStoredAutopilot(this.config.store);
103
+ let state = stored.state;
104
+ const interrupted = "Interrupted: resume to continue from the last saved step.";
105
+ if (state && !this.running && state.status === "running" && this.isOwnRunner(state.runner) && !(await this.ownRunnerAliveElsewhere())) {
106
+ state = { ...state, status: "paused", runner: null, message: state.message ?? interrupted };
107
+ }
108
+ if (state && !this.running && runnerActivity(state, this.now()) === "stale") {
109
+ state = { ...state, status: "paused", runner: null, message: interrupted };
110
+ }
111
+ const activities = [...stored.activities];
112
+ const current = this.run?.activity;
113
+ if (current && !activities.some((activity) => activity.id === current.id))
114
+ activities.push(current);
115
+ return {
116
+ bookId: this.config.bookId,
117
+ state,
118
+ activities,
119
+ live: this.live,
120
+ running: this.running,
121
+ host: this.running ? this.config.host : state?.status === "running" ? state.runner?.host ?? null : null,
122
+ };
123
+ }
124
+ /** Start or resume. Resolves once the run has saved its start, rejects if it could not start. */
125
+ async start(options = {}) {
126
+ if (this.run)
127
+ throw new AutopilotBusyError(this.config.hostLabel);
128
+ const stored = await readStoredAutopilot(this.config.store);
129
+ const runner = stored.state?.runner;
130
+ if (!options.force && runner && runnerActivity(stored.state, this.now()) === "active" && (!this.isOwnRunner(runner) || await this.ownRunnerAliveElsewhere())) {
131
+ throw new AutopilotBusyError(runner.label || runner.host);
132
+ }
133
+ let resolveStarted = () => undefined;
134
+ let rejectStarted = () => undefined;
135
+ const started = new Promise((resolve, reject) => {
136
+ resolveStarted = resolve;
137
+ rejectStarted = reject;
138
+ });
139
+ let didStart = false;
140
+ const run = new AutopilotRun({
141
+ bookId: this.config.bookId,
142
+ store: this.config.store,
143
+ ai: this.config.ai,
144
+ host: this.config.host,
145
+ hostLabel: this.config.hostLabel,
146
+ hostId: this.config.hostId,
147
+ now: this.config.now,
148
+ hooks: this.config.hooks,
149
+ retryDelayMs: this.config.retryDelayMs,
150
+ emit: (event) => {
151
+ if (event.type === "run-started" && !didStart) {
152
+ didStart = true;
153
+ resolveStarted();
154
+ }
155
+ this.emit(event);
156
+ },
157
+ });
158
+ this.run = run;
159
+ this.runPromise = run.execute(options).catch((error) => {
160
+ if (!didStart)
161
+ rejectStarted(error);
162
+ else
163
+ this.emit({ type: "insight", tone: "critical", code: "unexpectedStop", title: "The autopilot stopped unexpectedly.", detail: error instanceof Error ? error.message : String(error) });
164
+ throw error;
165
+ }).finally(() => {
166
+ if (this.run === run) {
167
+ this.run = null;
168
+ this.runPromise = null;
169
+ }
170
+ });
171
+ this.runPromise.catch(() => undefined);
172
+ await started;
173
+ }
174
+ /** `now` abandons the request in flight; `after-step` lets the current step finish and save. */
175
+ pause(mode = "after-step") {
176
+ if (!this.run)
177
+ return false;
178
+ if (mode === "now")
179
+ this.run.stop();
180
+ else
181
+ this.run.stopAfterStep();
182
+ return true;
183
+ }
184
+ /** Resolves when no run is active. */
185
+ async waitForIdle() {
186
+ await this.runPromise?.catch(() => undefined);
187
+ }
188
+ async mutate(message, change) {
189
+ for (let attempt = 0; attempt < 3; attempt += 1) {
190
+ const stored = await readStoredAutopilot(this.config.store);
191
+ const next = stampState(change(stored.state ?? createAutopilotState(this.now())), this.now());
192
+ try {
193
+ await this.config.store.commit([stateChange(next, stored.stateHash)], message);
194
+ this.emit({ type: "state", state: summarizeAutopilotState(next) });
195
+ return next;
196
+ }
197
+ catch (error) {
198
+ if (!isBookConflictError(error) || attempt === 2)
199
+ throw error;
200
+ }
201
+ }
202
+ throw new Error("Could not save the autopilot state.");
203
+ }
204
+ /** Record an author intervention. While running it reaches the next step (or interrupts the current one). */
205
+ async intervene(input) {
206
+ if (!input.text.trim())
207
+ throw new Error("The intervention is empty.");
208
+ if (this.run) {
209
+ this.run.intervene(input);
210
+ this.emit({ type: "insight", tone: "info", code: input.interrupt ? "interventionInterrupt" : "interventionQueued", title: input.interrupt ? "Intervention recorded: the current step restarts with it." : "Intervention recorded: it applies from the next step." });
211
+ return null;
212
+ }
213
+ return this.mutate("Autopilot: author intervention", (state) => applyIntervention(state, input, this.now(), "paused"));
214
+ }
215
+ async answer(answer) {
216
+ if (this.run)
217
+ throw new AutopilotBusyError(this.config.hostLabel);
218
+ return this.mutate("Autopilot: author answer", (state) => applyAnswer(state, answer, this.now()));
219
+ }
220
+ /** Change the brief or the options while the autopilot is not running. */
221
+ async configure(input) {
222
+ if (this.run)
223
+ throw new AutopilotBusyError(this.config.hostLabel);
224
+ return this.mutate("Autopilot: settings", (state) => ({
225
+ ...state,
226
+ brief: normalizeBrief(input.brief ?? {}, state.brief),
227
+ options: normalizeOptions(input.options ?? {}, state.options),
228
+ }));
229
+ }
230
+ /** Forget the autopilot state (plan, history, rules). The book's chapters are never touched. */
231
+ async reset() {
232
+ if (this.run)
233
+ throw new AutopilotBusyError(this.config.hostLabel);
234
+ const changes = (await Promise.all([AUTOPILOT_STATE_FILE, AUTOPILOT_ACTIVITY_FILE, AUTOPILOT_PLAN_FILE].map(async (path) => {
235
+ const file = await this.config.store.read(path);
236
+ return file ? { path, content: null, expectedHash: file.hash } : null;
237
+ }))).filter((change) => Boolean(change));
238
+ if (changes.length)
239
+ await this.config.store.commit(changes, "Autopilot: reset");
240
+ this.events = [];
241
+ this.emit({ type: "insight", tone: "info", code: "reset", title: "Autopilot reset." });
242
+ }
243
+ /** "What does the director think?" on the book, a chapter or a scene. */
244
+ askDirector(input) {
245
+ return askDirector({ ...input, store: this.config.store, ai: this.config.ai, now: this.now });
246
+ }
247
+ }
248
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/autopilot/session.ts"],"names":[],"mappings":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AA4F;QACtG,OAAO,WAAW,CAAC,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAChG,CAAC;CACF"}
@@ -0,0 +1,26 @@
1
+ import type { AutopilotActivity, AutopilotBrief, AutopilotOptions, AutopilotProgress, AutopilotState, AutopilotStateSummary, AuthorIntervention } from "./types.js";
2
+ export declare const MAX_ACTIVITIES = 200;
3
+ export declare const MAX_INTERVENTIONS = 100;
4
+ export declare const MAX_DIRECTOR_NOTES = 12;
5
+ export declare const MAX_CONSTRAINTS = 80;
6
+ export declare const MAX_REVISION_NOTES = 40;
7
+ /** Permanent author rules, newest kept when the cap is reached. */
8
+ export declare function capConstraints(constraints: string[]): string[];
9
+ export declare const DEFAULT_BRIEF: AutopilotBrief;
10
+ export declare const DEFAULT_OPTIONS: AutopilotOptions;
11
+ export declare function normalizeBrief(value: unknown, base?: AutopilotBrief): AutopilotBrief;
12
+ export declare function normalizeOptions(value: unknown, base?: AutopilotOptions): AutopilotOptions;
13
+ export declare function createAutopilotState(now: Date, brief?: Partial<AutopilotBrief>, options?: Partial<AutopilotOptions>): AutopilotState;
14
+ /** Tolerant reader for `autopilot/state.json`, including hand edits and older files. */
15
+ export declare function normalizeAutopilotState(value: unknown, now?: Date): AutopilotState;
16
+ export declare function parseAutopilotState(raw: string | null | undefined, now?: Date): AutopilotState | null;
17
+ export declare function serializeAutopilotState(state: AutopilotState): string;
18
+ export declare function parseActivityLog(raw: string | null | undefined): AutopilotActivity[];
19
+ export declare function serializeActivityLog(activities: AutopilotActivity[]): string;
20
+ /** A state saved as `running` by a runner that no longer exists is shown and resumed as paused. */
21
+ export declare function recoverInterruptedState(state: AutopilotState, message: string): AutopilotState;
22
+ export declare function autopilotProgress(state: AutopilotState | null): AutopilotProgress;
23
+ export declare function summarizeAutopilotState(state: AutopilotState): AutopilotStateSummary;
24
+ export declare function pendingNotes(state: AutopilotState): AuthorIntervention[];
25
+ export declare function wordsOf(text: string): number;
26
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/autopilot/state.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAGhB,iBAAiB,EACjB,cAAc,EACd,qBAAqB,EAGrB,kBAAkB,EAOnB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,cAAc,MAAM,CAAC;AAClC,eAAO,MAAM,iBAAiB,MAAM,CAAC;AACrC,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,eAAe,KAAK,CAAC;AAClC,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAOrC,mEAAmE;AACnE,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAE9D;AAED,eAAO,MAAM,aAAa,EAAE,cAQ3B,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,gBAU7B,CAAC;AAKF,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,cAA8B,GAAG,cAAc,CAWnG;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,gBAAkC,GAAG,gBAAgB,CAa3G;AAkMD,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,cAAc,CAyBpI;AAED,wFAAwF;AACxF,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,OAAa,GAAG,cAAc,CAiDxF;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GA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