@lemonriver/core 0.3.0 → 0.4.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 (86) hide show
  1. package/dist/{build-Bqo69DWP.js → build-Nl7EeJ6x.js} +1 -1
  2. package/dist/cli/bin.js +3 -3
  3. package/dist/{config-teo3kWz7.d.ts → config-CYNOR93W.d.ts} +1 -1
  4. package/dist/{config-DJ5rkMMg.js → config-CpC0nM3r.js} +697 -2
  5. package/dist/{dev-B2FJD5bJ.js → dev-BB3THd5I.js} +1 -1
  6. package/dist/index.d.ts +2 -2
  7. package/dist/index.js +1 -1
  8. package/dist/locale/index.d.ts +1 -1
  9. package/dist/locale/index.js +1 -1
  10. package/dist/{preview-C93BOZL1.js → preview-BGjgTg7H.js} +1 -1
  11. package/dist/{types-DFK02XVv.d.ts → types-tdaXSyah.d.ts} +1 -0
  12. package/dist/vite/index.d.ts +1 -1
  13. package/dist/vite/index.js +1 -1
  14. package/dist/{zh-tw-DwTAZKa6.js → zh-tw-DcrDeKxr.js} +5 -0
  15. package/package.json +1 -1
  16. package/src/agent/prompt.ts +2 -0
  17. package/src/app/components/agent/agent-panel.tsx +2 -0
  18. package/src/app/components/player.tsx +105 -23
  19. package/src/app/components/present/curtain.tsx +52 -0
  20. package/src/app/components/present/last-slide-hint.tsx +36 -0
  21. package/src/app/components/sidebar/sidebar-footer.tsx +6 -1
  22. package/src/app/components/thumbnail-rail.tsx +1 -0
  23. package/src/app/lemonriver/agent/lemon.tsx +16 -1
  24. package/src/app/lemonriver/agent-prefs/prefs.test.ts +173 -0
  25. package/src/app/lemonriver/agent-prefs/prefs.ts +137 -0
  26. package/src/app/lemonriver/agent-prefs/use-prefs.ts +36 -0
  27. package/src/app/lemonriver/brand/mascot.tsx +86 -0
  28. package/src/app/lemonriver/brand/mascots/design-done.png +0 -0
  29. package/src/app/lemonriver/brand/mascots/design-idle.png +0 -0
  30. package/src/app/lemonriver/brand/mascots/design-needs-you.png +0 -0
  31. package/src/app/lemonriver/brand/mascots/design-working.png +0 -0
  32. package/src/app/lemonriver/brand/mascots/outline-done.png +0 -0
  33. package/src/app/lemonriver/brand/mascots/outline-idle.png +0 -0
  34. package/src/app/lemonriver/brand/mascots/outline-needs-you.png +0 -0
  35. package/src/app/lemonriver/brand/mascots/outline-working.png +0 -0
  36. package/src/app/lemonriver/brand/mascots/research-done.png +0 -0
  37. package/src/app/lemonriver/brand/mascots/research-idle.png +0 -0
  38. package/src/app/lemonriver/brand/mascots/research-needs-you.png +0 -0
  39. package/src/app/lemonriver/brand/mascots/research-working.png +0 -0
  40. package/src/app/lemonriver/brand/mascots/review-done.png +0 -0
  41. package/src/app/lemonriver/brand/mascots/review-idle.png +0 -0
  42. package/src/app/lemonriver/brand/mascots/review-needs-you.png +0 -0
  43. package/src/app/lemonriver/brand/mascots/review-working.png +0 -0
  44. package/src/app/lemonriver/chrome/app-chrome.tsx +2 -0
  45. package/src/app/lemonriver/features.test.ts +5 -0
  46. package/src/app/lemonriver/features.ts +2 -1
  47. package/src/app/lemonriver/home/brief-card.tsx +8 -1
  48. package/src/app/lemonriver/home/home-page.tsx +11 -7
  49. package/src/app/lemonriver/onboarding/onboarding.tsx +182 -0
  50. package/src/app/lemonriver/outline/build-team.tsx +278 -0
  51. package/src/app/lemonriver/outline/describe-page.tsx +380 -0
  52. package/src/app/lemonriver/outline/files.test.ts +90 -0
  53. package/src/app/lemonriver/outline/files.ts +69 -0
  54. package/src/app/lemonriver/outline/model.test.ts +633 -0
  55. package/src/app/lemonriver/outline/model.ts +443 -0
  56. package/src/app/lemonriver/outline/outline-page.tsx +504 -0
  57. package/src/app/lemonriver/outline/pending-pages.tsx +39 -0
  58. package/src/app/lemonriver/outline/rule.ts +3 -0
  59. package/src/app/lemonriver/outline/steps.tsx +43 -0
  60. package/src/app/lemonriver/outline/use-build-driver.ts +258 -0
  61. package/src/app/lemonriver/routes.test.tsx +6 -6
  62. package/src/app/lemonriver/routes.tsx +28 -0
  63. package/src/app/lemonriver/settings/settings-page.tsx +211 -0
  64. package/src/app/lemonriver/slide-extensions.tsx +31 -24
  65. package/src/app/lemonriver/text/en.ts +149 -0
  66. package/src/app/lemonriver/text/es.ts +148 -0
  67. package/src/app/lemonriver/text/ja.ts +150 -0
  68. package/src/app/lemonriver/text/text.test.ts +33 -0
  69. package/src/app/lemonriver/text/types.ts +169 -0
  70. package/src/app/lemonriver/text/zh-cn.ts +146 -0
  71. package/src/app/lemonriver/text/zh-tw.ts +146 -0
  72. package/src/app/lemonriver/theme/app.css +12 -1
  73. package/src/app/lemonriver/theme/pages.test.ts +4 -0
  74. package/src/app/lemonriver/theme/pages.ts +11 -2
  75. package/src/app/lemonriver/update/beta-badge.tsx +26 -0
  76. package/src/app/lemonriver/update/prompt.test.ts +84 -0
  77. package/src/app/lemonriver/update/prompt.ts +48 -0
  78. package/src/app/lemonriver/update/update-prompt.tsx +113 -0
  79. package/src/app/routes/home.tsx +13 -5
  80. package/src/app/routes/slide.tsx +70 -13
  81. package/src/locale/en.ts +1 -0
  82. package/src/locale/es.ts +1 -0
  83. package/src/locale/ja.ts +1 -0
  84. package/src/locale/types.ts +1 -0
  85. package/src/locale/zh-cn.ts +1 -0
  86. package/src/locale/zh-tw.ts +1 -0
@@ -0,0 +1,443 @@
1
+ import { type AgentPrefs, prefsLines, sanitizePrefs } from '../agent-prefs/prefs';
2
+
3
+ export const PAGE_KINDS = ['cover', 'kpi', 'chart', 'list', 'page', 'end'] as const;
4
+ export type PageKind = (typeof PAGE_KINDS)[number];
5
+
6
+ export const OUTLINE_STATES = ['drafting', 'ready', 'failed', 'building', 'built'] as const;
7
+ export type OutlineState = (typeof OUTLINE_STATES)[number];
8
+
9
+ export type OutlinePage = { id: string; title: string; kind: PageKind };
10
+
11
+ export type BuildStage = 'pages' | 'review';
12
+
13
+ export type BuildStep = { kind: 'page' | 'review'; n: number; at: number };
14
+
15
+ export type BuildTurn = { kind: 'page' | 'review'; n: number; ok: boolean; at: number };
16
+
17
+ export type Decision = { id: string; question: string; answer: string };
18
+
19
+ export type Outline = {
20
+ state: OutlineState;
21
+ brief: string;
22
+ pages: number;
23
+ files: string[];
24
+ items: OutlinePage[];
25
+ updatedAt: number;
26
+ built: number;
27
+ paused: boolean;
28
+ stage: BuildStage;
29
+ dispatched: BuildStep | null;
30
+ lastTurn: BuildTurn | null;
31
+ prefs: AgentPrefs;
32
+ decisions: Decision[];
33
+ };
34
+
35
+ export const OUTLINE_PAGE_SIZES = [5, 8, 12] as const;
36
+ export const MAX_ITEMS = 30;
37
+ export const MIN_ITEMS = 2;
38
+ export const MAX_TITLE = 120;
39
+ export const MAX_BRIEF = 2000;
40
+ export const MAX_DECISIONS = 20;
41
+ export const MAX_DECISION_TEXT = 120;
42
+
43
+ const ID_RE = /^[a-z0-9-]{1,40}$/;
44
+ const BLOCK_RE = /<lemonriver-outline>([\s\S]*?)<\/lemonriver-outline>/gi;
45
+
46
+ let idSeq = 0;
47
+
48
+ export function newPageId(): string {
49
+ idSeq += 1;
50
+ return `p-${Date.now().toString(36)}-${idSeq.toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
51
+ }
52
+
53
+ export function isPageKind(value: unknown): value is PageKind {
54
+ return typeof value === 'string' && (PAGE_KINDS as readonly string[]).includes(value);
55
+ }
56
+
57
+ export function isOutlineState(value: unknown): value is OutlineState {
58
+ return typeof value === 'string' && (OUTLINE_STATES as readonly string[]).includes(value);
59
+ }
60
+
61
+ function cleanTitle(value: unknown): string {
62
+ return typeof value === 'string' ? value.trim().slice(0, MAX_TITLE) : '';
63
+ }
64
+
65
+ function cleanItem(raw: unknown): OutlinePage | null {
66
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
67
+ const r = raw as Record<string, unknown>;
68
+ const id = typeof r.id === 'string' && ID_RE.test(r.id) ? r.id : newPageId();
69
+ return { id, title: cleanTitle(r.title), kind: isPageKind(r.kind) ? r.kind : 'page' };
70
+ }
71
+
72
+ export function sanitizeItems(raw: unknown): OutlinePage[] {
73
+ if (!Array.isArray(raw)) return [];
74
+ return raw.slice(0, MAX_ITEMS).flatMap((item) => {
75
+ const cleaned = cleanItem(item);
76
+ return cleaned ? [cleaned] : [];
77
+ });
78
+ }
79
+
80
+ function cleanStep(raw: unknown, maxPage: number): BuildStep | null {
81
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
82
+ const r = raw as Record<string, unknown>;
83
+ const kind = r.kind === 'page' || r.kind === 'review' ? r.kind : null;
84
+ if (!kind) return null;
85
+ const n = typeof r.n === 'number' && Number.isFinite(r.n) ? Math.trunc(r.n) : -1;
86
+ const at = typeof r.at === 'number' && Number.isFinite(r.at) ? r.at : -1;
87
+ if (n < 0 || at < 0) return null;
88
+ return { kind, n: Math.min(n, maxPage), at };
89
+ }
90
+
91
+ function cleanTurn(raw: unknown, maxPage: number): BuildTurn | null {
92
+ const step = cleanStep(raw, maxPage);
93
+ if (!step || !raw || typeof raw !== 'object') return null;
94
+ return { ...step, ok: (raw as Record<string, unknown>).ok === true };
95
+ }
96
+
97
+ export function sanitizeDecisions(raw: unknown): Decision[] {
98
+ if (!Array.isArray(raw)) return [];
99
+ return raw.slice(0, MAX_DECISIONS).flatMap((entry) => {
100
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return [];
101
+ const r = entry as Record<string, unknown>;
102
+ if (typeof r.id !== 'string' || r.id === '') return [];
103
+ return [
104
+ {
105
+ id: r.id.slice(0, 80),
106
+ question: typeof r.question === 'string' ? r.question.slice(0, MAX_DECISION_TEXT) : '',
107
+ answer: typeof r.answer === 'string' ? r.answer.slice(0, MAX_DECISION_TEXT) : '',
108
+ },
109
+ ];
110
+ });
111
+ }
112
+
113
+ export type AnsweredQuestion = {
114
+ id: string;
115
+ kind: string;
116
+ status?: string;
117
+ questions?: readonly { question: string; header?: string }[];
118
+ answers?: Record<string, string> | null;
119
+ };
120
+
121
+ /**
122
+ * Every answered question in a review session becomes one decision. Ids are
123
+ * the question item plus its index, so a resumed turn that reports the same
124
+ * item again does not add a duplicate.
125
+ */
126
+ export function collectDecisions(
127
+ existing: readonly Decision[],
128
+ items: readonly AnsweredQuestion[],
129
+ ): Decision[] {
130
+ const seen = new Set(existing.map((decision) => decision.id));
131
+ const next = existing.slice();
132
+ for (const item of items) {
133
+ if (item.kind !== 'question' || item.status !== 'answered') continue;
134
+ const questions = item.questions ?? [];
135
+ for (let index = 0; index < questions.length; index += 1) {
136
+ if (next.length >= MAX_DECISIONS) return next;
137
+ const id = `${item.id}:${index}`;
138
+ if (seen.has(id)) continue;
139
+ seen.add(id);
140
+ const question = questions[index];
141
+ const answer = item.answers?.[question.question] ?? '';
142
+ next.push({
143
+ id,
144
+ question: (question.header || question.question).slice(0, MAX_DECISION_TEXT),
145
+ answer: answer.slice(0, MAX_DECISION_TEXT),
146
+ });
147
+ }
148
+ }
149
+ return next;
150
+ }
151
+
152
+ export function sanitizeOutline(raw: unknown): Outline | null {
153
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
154
+ const r = raw as Record<string, unknown>;
155
+ if (!isOutlineState(r.state)) return null;
156
+ const pages = OUTLINE_PAGE_SIZES.find((size) => size === r.pages) ?? 8;
157
+ const brief = typeof r.brief === 'string' ? r.brief.slice(0, MAX_BRIEF) : '';
158
+ const files = Array.isArray(r.files)
159
+ ? r.files.filter((file): file is string => typeof file === 'string' && file.trim() !== '')
160
+ : [];
161
+ const updatedAt =
162
+ typeof r.updatedAt === 'number' && Number.isFinite(r.updatedAt) ? r.updatedAt : 0;
163
+ const items = sanitizeItems(r.items);
164
+ const builtRaw =
165
+ typeof r.built === 'number' && Number.isFinite(r.built) ? Math.trunc(r.built) : 0;
166
+ return {
167
+ state: r.state,
168
+ brief,
169
+ pages,
170
+ files,
171
+ items,
172
+ updatedAt,
173
+ built: Math.min(Math.max(builtRaw, 0), items.length),
174
+ paused: r.paused === true,
175
+ stage: r.stage === 'review' ? 'review' : 'pages',
176
+ dispatched: cleanStep(r.dispatched, items.length),
177
+ lastTurn: cleanTurn(r.lastTurn, items.length),
178
+ prefs: sanitizePrefs(r.prefs),
179
+ decisions: sanitizeDecisions(r.decisions),
180
+ };
181
+ }
182
+
183
+ /** The last outline block wins: a retried answer may carry an earlier draft. */
184
+ export function parseOutlineBlock(text: string): OutlinePage[] | null {
185
+ let last: string | null = null;
186
+ for (const match of text.matchAll(BLOCK_RE)) last = match[1] ?? '';
187
+ if (last === null) return null;
188
+ let parsed: unknown;
189
+ try {
190
+ parsed = JSON.parse(last.trim());
191
+ } catch {
192
+ return null;
193
+ }
194
+ const items = sanitizeItems(parsed);
195
+ return items.length >= MIN_ITEMS ? items : null;
196
+ }
197
+
198
+ // Also drops a block that is still streaming in, so the JSON never flashes
199
+ // in the transcript.
200
+ export function stripOutlineBlock(text: string): string {
201
+ return text
202
+ .replace(BLOCK_RE, '')
203
+ .replace(/<lemonriver-outline>[\s\S]*$/i, '')
204
+ .trim();
205
+ }
206
+
207
+ function validIndex(items: OutlinePage[], index: number): boolean {
208
+ return Number.isInteger(index) && index >= 0 && index < items.length;
209
+ }
210
+
211
+ export function movePage(items: OutlinePage[], from: number, to: number): OutlinePage[] {
212
+ if (!validIndex(items, from) || !Number.isInteger(to) || to < 0 || to >= items.length) {
213
+ return items;
214
+ }
215
+ if (from === to) return items;
216
+ const next = items.slice();
217
+ const [moved] = next.splice(from, 1);
218
+ next.splice(to, 0, moved);
219
+ return next;
220
+ }
221
+
222
+ export function insertPage(
223
+ items: OutlinePage[],
224
+ at: number,
225
+ kind: PageKind = 'page',
226
+ ): OutlinePage[] {
227
+ if (items.length >= MAX_ITEMS) return items;
228
+ const index = Math.min(Math.max(Math.trunc(at), 0), items.length);
229
+ const next = items.slice();
230
+ next.splice(index, 0, { id: newPageId(), title: '', kind });
231
+ return next;
232
+ }
233
+
234
+ export function removePage(items: OutlinePage[], index: number): OutlinePage[] {
235
+ if (items.length <= MIN_ITEMS || !validIndex(items, index)) return items;
236
+ return items.filter((_, i) => i !== index);
237
+ }
238
+
239
+ export function renamePage(items: OutlinePage[], index: number, title: string): OutlinePage[] {
240
+ if (!validIndex(items, index)) return items;
241
+ // Not trimmed here: the title is typed into an input, and trailing spaces are
242
+ // what the next word needs. The server trims when it stores the outline.
243
+ const clean = typeof title === 'string' ? title.slice(0, MAX_TITLE) : '';
244
+ if (items[index].title === clean) return items;
245
+ return items.map((item, i) => (i === index ? { ...item, title: clean } : item));
246
+ }
247
+
248
+ export function setKind(items: OutlinePage[], index: number, kind: PageKind): OutlinePage[] {
249
+ if (!validIndex(items, index) || items[index].kind === kind) return items;
250
+ return items.map((item, i) => (i === index ? { ...item, kind } : item));
251
+ }
252
+
253
+ const KIND_GUIDE: Record<PageKind, string> = {
254
+ cover: 'cover — the deck title and subtitle',
255
+ kpi: 'key figure — one large number with a one-line takeaway',
256
+ chart: 'chart — a labelled chart with the point it makes',
257
+ list: 'bullet points — a short heading and a few concise points',
258
+ page: 'body — a normal content page',
259
+ end: 'closing — the last page and what should happen next',
260
+ };
261
+
262
+ export function outlinePrompt(outline: Outline, slideId: string, lang: string): string {
263
+ const fileList =
264
+ outline.files.length > 0
265
+ ? `Read these reference files first, from slides/${slideId}/assets/: ${outline.files.join(', ')}.`
266
+ : 'The request has no reference files.';
267
+ const lines = [
268
+ 'Plan the outline for a new deck. This turn is outline only: do not write, edit or create any slide file.',
269
+ `The user asked for: ${outline.brief}`,
270
+ `The deck should have about ${outline.pages} pages.`,
271
+ fileList,
272
+ `Write the page titles in the language "${lang}".`,
273
+ 'If a file cannot be read, say which file it is and why, then plan from what you have.',
274
+ 'Finish with one line of its own holding a JSON array between the tags <lemonriver-outline> and </lemonriver-outline>,',
275
+ 'each entry shaped like {"title": "…", "kind": "cover"}.',
276
+ `"kind" is one of: ${PAGE_KINDS.join(', ')}. The first page is "cover" and the last is "end".`,
277
+ `Give ${MIN_ITEMS} to ${MAX_ITEMS} entries.`,
278
+ ...prefsLines(outline.prefs, 'outline'),
279
+ ];
280
+ return lines.join('\n');
281
+ }
282
+
283
+ export function pagePrompt(outline: Outline, slideId: string, k: number): string {
284
+ const file = `slides/${slideId}/index.tsx`;
285
+ const page = outline.items[k - 1];
286
+ if (k >= 2 && page) {
287
+ const title = page.title || '(untitled)';
288
+ return [
289
+ `Page ${k} of ${outline.items.length}: [${page.kind}] ${title}. Write only this page; leave the others as they are.`,
290
+ ...prefsLines(outline.prefs, 'page'),
291
+ ].join('\n');
292
+ }
293
+ const list = outline.items
294
+ .map((item, i) => `${i + 1}. [${item.kind}] ${item.title || '(untitled)'}`)
295
+ .join('\n');
296
+ return [
297
+ `Build ${file} from this outline, one page per turn. This turn: write page 1 only, replacing the placeholder page, and set meta.title to the deck title. Pages:`,
298
+ list,
299
+ 'Page kinds and how to write them:',
300
+ ...PAGE_KINDS.map((kind) => `- ${KIND_GUIDE[kind]}`),
301
+ 'Numbers from the reference files stay exactly as written there.',
302
+ ...prefsLines(outline.prefs, 'page'),
303
+ ].join('\n');
304
+ }
305
+
306
+ export function reviewPrompt(outline: Outline, slideId: string): string {
307
+ const lines = [
308
+ `All ${outline.items.length} pages are written.`,
309
+ `Check each page of slides/${slideId}/index.tsx against the outline and the reference files in slides/${slideId}/assets/:`,
310
+ 'fix typos and any number that differs from the files.',
311
+ ];
312
+ if (outline.prefs.askOnMismatch) {
313
+ lines.push(
314
+ 'If two files give different numbers for the same thing, ask the user which to use before changing it.',
315
+ );
316
+ }
317
+ lines.push('Do not add or remove pages.', ...prefsLines(outline.prefs, 'review'));
318
+ return lines.join(' ');
319
+ }
320
+
321
+ export const DISPATCH_STALE_MS = 30_000;
322
+ export const RESUME_WINDOW_MS = 120_000;
323
+
324
+ /** Both the build button and a "direct" outline start from a ready record. */
325
+ export function beginBuild(outline: Outline, now: number): Outline {
326
+ return {
327
+ ...outline,
328
+ state: 'building',
329
+ built: 0,
330
+ paused: false,
331
+ stage: 'pages',
332
+ dispatched: null,
333
+ lastTurn: null,
334
+ updatedAt: now,
335
+ };
336
+ }
337
+
338
+ export type BuildNext = { kind: 'page'; n: number } | { kind: 'review' } | null;
339
+
340
+ export function nextStep(outline: Outline): BuildNext {
341
+ if (outline.state !== 'building') return null;
342
+ if (outline.stage === 'review') return { kind: 'review' };
343
+ if (outline.built >= outline.items.length) return null;
344
+ return { kind: 'page', n: outline.built + 1 };
345
+ }
346
+
347
+ export function afterTurn(
348
+ outline: Outline,
349
+ turn: { ok: boolean; waitingForAnswer: boolean },
350
+ now: number,
351
+ ): Outline {
352
+ const dispatched = outline.dispatched;
353
+ if (outline.state !== 'building' || !dispatched) return outline;
354
+ if (turn.waitingForAnswer) return outline;
355
+ const lastTurn: BuildTurn = {
356
+ kind: dispatched.kind,
357
+ n: dispatched.n,
358
+ ok: turn.ok,
359
+ at: now,
360
+ };
361
+ if (!turn.ok) {
362
+ return { ...outline, dispatched: null, lastTurn, updatedAt: now };
363
+ }
364
+ if (dispatched.kind === 'review') {
365
+ return { ...outline, state: 'built', dispatched: null, lastTurn, updatedAt: now };
366
+ }
367
+ const last = dispatched.n >= outline.items.length;
368
+ return {
369
+ ...outline,
370
+ built: dispatched.n,
371
+ stage: last ? 'review' : outline.stage,
372
+ paused: last ? false : outline.prefs.confirmEachPage || outline.paused,
373
+ dispatched: null,
374
+ lastTurn,
375
+ updatedAt: now,
376
+ };
377
+ }
378
+
379
+ /** The page the build is waiting on after a confirm-each-page stop, or null. */
380
+ export function pageAwaitingCheck(outline: Outline): number | null {
381
+ if (outline.state !== 'building' || !outline.paused) return null;
382
+ if (!outline.prefs.confirmEachPage) return null;
383
+ const turn = outline.lastTurn;
384
+ if (turn?.kind !== 'page' || !turn.ok) return null;
385
+ if (turn.n >= outline.items.length) return null;
386
+ return turn.n;
387
+ }
388
+
389
+ export type DriverAction = 'send' | 'wait' | 'paused' | 'interrupted' | 'idle';
390
+
391
+ export function driverAction(
392
+ outline: Outline,
393
+ now: { running: boolean; now: number },
394
+ ): DriverAction {
395
+ if (outline.state !== 'building') return 'idle';
396
+ if (now.running) return 'wait';
397
+ if (outline.paused) return 'paused';
398
+ if (outline.dispatched) {
399
+ return now.now - outline.dispatched.at < DISPATCH_STALE_MS ? 'wait' : 'interrupted';
400
+ }
401
+ if (outline.lastTurn && !outline.lastTurn.ok) return 'interrupted';
402
+ if (!outline.lastTurn) return 'send';
403
+ return now.now - outline.lastTurn.at < RESUME_WINDOW_MS ? 'send' : 'interrupted';
404
+ }
405
+
406
+ export type TeamLeadState = 'idle' | 'working' | 'done' | 'needs-you' | 'thinking';
407
+ export type TeamHelperState = 'idle' | 'working' | 'done' | 'needs-you' | 'none';
408
+ export type TeamState = {
409
+ lead: TeamLeadState;
410
+ research: TeamHelperState;
411
+ outline: TeamHelperState;
412
+ design: TeamHelperState;
413
+ review: TeamHelperState;
414
+ };
415
+
416
+ export function teamState(
417
+ outline: Outline,
418
+ seen: { running: boolean; waitingForAnswer: boolean; action: DriverAction },
419
+ ): TeamState {
420
+ const inReview = outline.stage === 'review';
421
+ const done = outline.state === 'built';
422
+ return {
423
+ lead: done
424
+ ? 'done'
425
+ : seen.action === 'interrupted'
426
+ ? 'needs-you'
427
+ : outline.paused
428
+ ? 'idle'
429
+ : seen.running
430
+ ? 'working'
431
+ : 'thinking',
432
+ research: outline.files.length > 0 ? 'done' : 'none',
433
+ outline: 'done',
434
+ design: done || inReview ? 'done' : seen.running ? 'working' : 'idle',
435
+ review: done
436
+ ? 'done'
437
+ : inReview && seen.waitingForAnswer
438
+ ? 'needs-you'
439
+ : inReview && seen.running
440
+ ? 'working'
441
+ : 'idle',
442
+ };
443
+ }