@lemonriver/core 0.4.0 → 0.6.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 (115) hide show
  1. package/README.md +14 -4
  2. package/dist/{build-Nl7EeJ6x.js → build-RlcGG-Ve.js} +1 -1
  3. package/dist/cli/bin.js +4 -4
  4. package/dist/{config-CpC0nM3r.js → config-BJT87wE9.js} +4756 -2012
  5. package/dist/{dev-BB3THd5I.js → dev-DR-j_xda.js} +2 -2
  6. package/dist/{preview-BGjgTg7H.js → preview-BTAhTb5B.js} +1 -1
  7. package/dist/{update-DsxZBtwv.js → update-CMRxtTc9.js} +18 -8
  8. package/dist/vite/index.js +1 -1
  9. package/package.json +2 -2
  10. package/src/agent/types.ts +14 -2
  11. package/src/app/components/agent/agent-panel.tsx +17 -5
  12. package/src/app/components/agent/connect-view.tsx +1 -1
  13. package/src/app/components/agent/provider-logo.tsx +10 -1
  14. package/src/app/lemonriver/agent/provider-marks.tsx +53 -0
  15. package/src/app/lemonriver/agent-providers/section.tsx +75 -0
  16. package/src/app/lemonriver/agent-providers/use-providers.ts +61 -0
  17. package/src/app/lemonriver/board/blocks/block-panel.tsx +666 -0
  18. package/src/app/lemonriver/board/blocks/cache.ts +28 -0
  19. package/src/app/lemonriver/board/blocks/change-list.tsx +189 -0
  20. package/src/app/lemonriver/board/blocks/classify.test.ts +209 -0
  21. package/src/app/lemonriver/board/blocks/classify.ts +152 -0
  22. package/src/app/lemonriver/board/blocks/snapshot.ts +116 -0
  23. package/src/app/lemonriver/board/board-canvas.tsx +627 -104
  24. package/src/app/lemonriver/board/board-page.tsx +1702 -91
  25. package/src/app/lemonriver/board/board.css +58 -7
  26. package/src/app/lemonriver/board/layout.test.ts +84 -0
  27. package/src/app/lemonriver/board/layout.ts +112 -11
  28. package/src/app/lemonriver/board/model/agent-tasks.test.ts +200 -0
  29. package/src/app/lemonriver/board/model/agent-tasks.ts +223 -0
  30. package/src/app/lemonriver/board/model/apply-prompt.test.ts +121 -0
  31. package/src/app/lemonriver/board/model/apply-prompt.ts +115 -0
  32. package/src/app/lemonriver/board/model/apply.ts +71 -0
  33. package/src/app/lemonriver/board/model/cards.test.ts +95 -2
  34. package/src/app/lemonriver/board/model/cards.ts +88 -20
  35. package/src/app/lemonriver/board/model/changes.test.ts +244 -0
  36. package/src/app/lemonriver/board/model/changes.ts +268 -0
  37. package/src/app/lemonriver/board/model/filter.test.ts +50 -0
  38. package/src/app/lemonriver/board/model/filter.ts +42 -0
  39. package/src/app/lemonriver/board/model/history.test.ts +71 -0
  40. package/src/app/lemonriver/board/model/history.ts +51 -0
  41. package/src/app/lemonriver/board/model/restructure.test.ts +205 -0
  42. package/src/app/lemonriver/board/model/restructure.ts +169 -0
  43. package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
  44. package/src/app/lemonriver/board/model/self-check.ts +489 -0
  45. package/src/app/lemonriver/board/model/structure.test.ts +249 -0
  46. package/src/app/lemonriver/board/model/structure.ts +209 -0
  47. package/src/app/lemonriver/board/model/timing.test.ts +113 -0
  48. package/src/app/lemonriver/board/model/timing.ts +117 -0
  49. package/src/app/lemonriver/board/page-node.tsx +258 -55
  50. package/src/app/lemonriver/board/self-check-jump.ts +23 -0
  51. package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
  52. package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
  53. package/src/app/lemonriver/board/status-style.ts +25 -0
  54. package/src/app/lemonriver/board/use-board-apply.ts +206 -0
  55. package/src/app/lemonriver/board/use-board-cards.ts +108 -5
  56. package/src/app/lemonriver/board/use-board-changes.ts +108 -0
  57. package/src/app/lemonriver/board/use-board-columns.ts +37 -56
  58. package/src/app/lemonriver/board/use-board-comments.ts +36 -0
  59. package/src/app/lemonriver/board/use-board-restructures.ts +107 -0
  60. package/src/app/lemonriver/board/use-history.ts +86 -0
  61. package/src/app/lemonriver/board/use-page-order.ts +65 -38
  62. package/src/app/lemonriver/board/use-self-check.ts +117 -0
  63. package/src/app/lemonriver/board/use-target-minutes.ts +47 -0
  64. package/src/app/lemonriver/board/views/block-readings.tsx +46 -0
  65. package/src/app/lemonriver/board/views/list-view.tsx +356 -0
  66. package/src/app/lemonriver/board/views/progress.tsx +32 -0
  67. package/src/app/lemonriver/board/views/timeline-view.tsx +341 -0
  68. package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
  69. package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
  70. package/src/app/lemonriver/features.test.ts +5 -0
  71. package/src/app/lemonriver/features.ts +2 -1
  72. package/src/app/lemonriver/home/deck-card.tsx +10 -0
  73. package/src/app/lemonriver/home/home-page.tsx +55 -6
  74. package/src/app/lemonriver/home/use-deck-times.ts +4 -1
  75. package/src/app/lemonriver/outline/build-team.tsx +42 -0
  76. package/src/app/lemonriver/outline/describe-page.tsx +128 -27
  77. package/src/app/lemonriver/outline/draft.ts +38 -0
  78. package/src/app/lemonriver/outline/model.test.ts +118 -0
  79. package/src/app/lemonriver/outline/model.ts +92 -7
  80. package/src/app/lemonriver/outline/outline-page.tsx +90 -7
  81. package/src/app/lemonriver/outline/pick-style.tsx +282 -0
  82. package/src/app/lemonriver/routes.test.tsx +17 -5
  83. package/src/app/lemonriver/routes.tsx +28 -0
  84. package/src/app/lemonriver/settings/settings-page.tsx +3 -0
  85. package/src/app/lemonriver/slide-extensions.tsx +14 -2
  86. package/src/app/lemonriver/templates/client.ts +60 -0
  87. package/src/app/lemonriver/templates/demo-page.tsx +79 -0
  88. package/src/app/lemonriver/templates/knobs.test.ts +62 -0
  89. package/src/app/lemonriver/templates/knobs.ts +62 -0
  90. package/src/app/lemonriver/templates/library-page.tsx +292 -0
  91. package/src/app/lemonriver/templates/model.test.ts +175 -0
  92. package/src/app/lemonriver/templates/model.ts +339 -0
  93. package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
  94. package/src/app/lemonriver/templates/preview-content.ts +149 -0
  95. package/src/app/lemonriver/templates/recommend.test.ts +71 -0
  96. package/src/app/lemonriver/templates/recommend.ts +72 -0
  97. package/src/app/lemonriver/templates/use-library.ts +58 -0
  98. package/src/app/lemonriver/templates/virtual.d.ts +28 -0
  99. package/src/app/lemonriver/text/en.ts +228 -0
  100. package/src/app/lemonriver/text/es.ts +220 -0
  101. package/src/app/lemonriver/text/ja.ts +220 -0
  102. package/src/app/lemonriver/text/types.ts +243 -0
  103. package/src/app/lemonriver/text/zh-cn.ts +218 -0
  104. package/src/app/lemonriver/text/zh-tw.ts +218 -0
  105. package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
  106. package/src/app/lemonriver/theme/pages.test.ts +4 -0
  107. package/src/app/lemonriver/theme/pages.ts +6 -1
  108. package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
  109. package/src/app/lemonriver/theme/use-theme.tsx +2 -2
  110. package/src/app/lemonriver/update/prompt.ts +1 -1
  111. package/src/app/lib/agent/use-agent.ts +4 -3
  112. package/src/app/routes/slide.tsx +1 -1
  113. package/src/app/assets/open-slide.svg +0 -39
  114. package/src/app/favicon.ico +0 -0
  115. package/src/app/favicon.svg +0 -39
@@ -75,16 +75,33 @@
75
75
  color: color-mix(in oklab, var(--color-muted-foreground) 60%, transparent);
76
76
  }
77
77
 
78
- /* Links are loose associations, so they stay thin, dashed and quiet next to the
79
- solid main line that carries the deck's order. */
78
+ /* Links are loose associations, so they stay thin and quiet next to the solid
79
+ main line that carries the deck's order. Each relation has its own line, so
80
+ the two pages' relation reads from the canvas without opening anything. */
80
81
  .osd-board .osd-board-link {
81
82
  stroke: var(--osd-board-link);
82
83
  stroke-width: 1.25;
84
+ }
85
+
86
+ .osd-board .osd-board-link--plain {
83
87
  stroke-dasharray: 5 4;
84
88
  }
85
89
 
86
- .osd-board-link-remove {
87
- position: absolute;
90
+ .osd-board .osd-board-link--cause {
91
+ stroke-width: 1.5;
92
+ }
93
+
94
+ .osd-board .osd-board-link--contrast {
95
+ stroke-width: 0.9;
96
+ }
97
+
98
+ .osd-board .osd-board-link--echo {
99
+ stroke-width: 1.75;
100
+ stroke-linecap: round;
101
+ stroke-dasharray: 1 4;
102
+ }
103
+
104
+ .osd-board-link-menu {
88
105
  display: flex;
89
106
  align-items: center;
90
107
  justify-content: center;
@@ -101,10 +118,23 @@
101
118
  color 120ms ease;
102
119
  }
103
120
 
104
- .osd-board-link-remove:hover,
105
- .osd-board-link-remove:focus-visible {
121
+ .osd-board-link-menu:hover,
122
+ .osd-board-link-menu:focus-visible,
123
+ .osd-board-link-menu[data-state="open"] {
106
124
  opacity: 1;
107
- color: var(--color-destructive);
125
+ color: var(--color-foreground);
126
+ }
127
+
128
+ .osd-board-link-tag {
129
+ border: 1px solid var(--color-border);
130
+ border-radius: 999px;
131
+ background: var(--color-card);
132
+ padding: 1px 6px;
133
+ color: var(--color-muted-foreground);
134
+ font-size: 10.5px;
135
+ line-height: 1.4;
136
+ white-space: nowrap;
137
+ pointer-events: none;
108
138
  }
109
139
 
110
140
  /* The dot a link is dragged from. It only shows on the card under the pointer,
@@ -124,3 +154,24 @@
124
154
  .osd-board .osd-board-link-handle.connectingfrom {
125
155
  opacity: 1;
126
156
  }
157
+
158
+ /* A page the last apply touched keeps a brand outline for a moment. Reduced
159
+ motion keeps the outline and drops the fade. */
160
+ .osd-board-card.osd-board-applied {
161
+ box-shadow: 0 0 0 2px var(--lr-accent);
162
+ }
163
+
164
+ @media (prefers-reduced-motion: no-preference) {
165
+ .osd-board-card.osd-board-applied {
166
+ animation: osd-board-applied 1.6s ease-out forwards;
167
+ }
168
+ }
169
+
170
+ @keyframes osd-board-applied {
171
+ from {
172
+ box-shadow: 0 0 0 2px var(--lr-accent);
173
+ }
174
+ to {
175
+ box-shadow: 0 0 0 2px transparent;
176
+ }
177
+ }
@@ -6,9 +6,13 @@ import {
6
6
  CARD_H,
7
7
  CARD_W,
8
8
  cardAtPoint,
9
+ cardMetrics,
10
+ gapAtPoint,
9
11
  layoutBoard,
10
12
  linkCurve,
13
+ NOTE_H,
11
14
  orderAfterMove,
15
+ orderAfterMoveMany,
12
16
  seedColumns,
13
17
  slotAtPoint,
14
18
  } from './layout';
@@ -159,3 +163,83 @@ describe('linkCurve', () => {
159
163
  expect(forward.path.endsWith('300,218')).toBe(true);
160
164
  });
161
165
  });
166
+
167
+ describe('card sizes', () => {
168
+ it('keeps the large card at the preview size it has always had', () => {
169
+ expect(cardMetrics('large')).toMatchObject({ cardW: CARD_W, cardH: CARD_H, noteH: NOTE_H });
170
+ });
171
+
172
+ it('halves the preview at small, and drops the strips that need the room', () => {
173
+ expect(cardMetrics('small').cardW).toBe(CARD_W / 2);
174
+ expect(cardMetrics('small').cardH).toBeCloseTo(CARD_H / 2);
175
+ expect(cardMetrics('small').memoH).toBe(0);
176
+ });
177
+
178
+ it('draws no preview at all when only the title is shown', () => {
179
+ expect(cardMetrics('title').cardH).toBe(0);
180
+ expect(cardMetrics('title').boxH).toBeLessThan(cardMetrics('small').boxH);
181
+ });
182
+
183
+ it('lays the same structure out with the metrics of the chosen size', () => {
184
+ const columns = [{ title: 'A', size: 2 }];
185
+ expect(layoutBoard(columns, { size: 'small' }).metrics.cardW).toBe(CARD_W / 2);
186
+ expect(layoutBoard(columns).metrics.cardW).toBe(CARD_W);
187
+ });
188
+ });
189
+
190
+ describe('folded chapters', () => {
191
+ const columns = [
192
+ { title: 'A', size: 2 },
193
+ { title: 'B', size: 3 },
194
+ ];
195
+
196
+ it('draws no cards for a folded chapter but keeps its pages numbered', () => {
197
+ const layout = layoutBoard(columns, { collapsed: [false, true] });
198
+ expect(layout.slots.map((s) => s.index)).toEqual([0, 1]);
199
+ expect(layout.columns[1].collapsed).toBe(true);
200
+ });
201
+
202
+ it('keeps the folded chapter label in place while its cards are gone', () => {
203
+ const open = layoutBoard(columns);
204
+ const folded = layoutBoard(columns, { collapsed: [true, false] });
205
+ expect(folded.columns[0].x).toBe(open.columns[0].x);
206
+ expect(folded.columns[0].size).toBe(2);
207
+ expect(folded.slots.map((s) => s.index)).toEqual([2, 3, 4]);
208
+ });
209
+ });
210
+
211
+ describe('gapAtPoint', () => {
212
+ const layout = layoutBoard([
213
+ { title: 'A', size: 2 },
214
+ { title: 'B', size: 2 },
215
+ ]);
216
+
217
+ it('names the boundary a point between two chapters sits on', () => {
218
+ const right = layout.columns[0].x + layout.columns[0].boxW;
219
+ expect(gapAtPoint(layout, right + 10, 100)).toBe(1);
220
+ });
221
+
222
+ it('reports no boundary over a column or past the last one', () => {
223
+ expect(gapAtPoint(layout, layout.columns[0].x + 20, 100)).toBeNull();
224
+ expect(gapAtPoint(layout, layout.columns[1].x + 400, 100)).toBeNull();
225
+ });
226
+ });
227
+
228
+ describe('orderAfterMoveMany', () => {
229
+ it('carries the picked cards as one block, in deck order', () => {
230
+ expect(orderAfterMoveMany(5, [3, 1], 0)).toEqual([1, 3, 0, 2, 4]);
231
+ });
232
+
233
+ it('lands the block on the slot it was dropped on', () => {
234
+ expect(orderAfterMoveMany(6, [0, 1], 4)).toEqual([2, 3, 4, 5, 0, 1]);
235
+ expect(orderAfterMoveMany(6, [4, 5], 1)).toEqual([0, 4, 5, 1, 2, 3]);
236
+ // The same slot a single card takes, so "move to the end" still ends there.
237
+ expect(orderAfterMoveMany(3, [0], 2)).toEqual(orderAfterMove(3, 0, 2));
238
+ expect(orderAfterMoveMany(5, [4], 1)).toEqual(orderAfterMove(5, 4, 1));
239
+ });
240
+
241
+ it('keeps every page exactly once', () => {
242
+ const order = orderAfterMoveMany(7, [2, 5, 6], 1);
243
+ expect([...order].sort((a, b) => a - b)).toEqual([...Array(7).keys()]);
244
+ });
245
+ });
@@ -12,6 +12,46 @@ export const MEMO_H = 40;
12
12
  /** Card box = preview plus the card's padding, title row, note strip and memo strip. */
13
13
  export const CARD_BOX_W = CARD_W + 12;
14
14
  export const CARD_BOX_H = CARD_H + 60 + NOTE_H + MEMO_H;
15
+
16
+ /** How much of a page a card shows. `title` is a single row, for reading a long deck. */
17
+ export type CardSize = 'large' | 'small' | 'title';
18
+ export const CARD_SIZES: readonly CardSize[] = ['large', 'small', 'title'];
19
+
20
+ export type CardMetrics = {
21
+ cardW: number;
22
+ cardH: number;
23
+ /** Height of the speaker-note strip; 0 hides it. */
24
+ noteH: number;
25
+ /** Height of the work-memo strip; 0 hides it. */
26
+ memoH: number;
27
+ boxW: number;
28
+ boxH: number;
29
+ };
30
+
31
+ const METRICS: Record<CardSize, CardMetrics> = {
32
+ large: {
33
+ cardW: CARD_W,
34
+ cardH: CARD_H,
35
+ noteH: NOTE_H,
36
+ memoH: MEMO_H,
37
+ boxW: CARD_BOX_W,
38
+ boxH: CARD_BOX_H,
39
+ },
40
+ small: {
41
+ cardW: 240,
42
+ cardH: (240 * CANVAS_HEIGHT) / CANVAS_WIDTH,
43
+ noteH: 0,
44
+ memoH: 0,
45
+ boxW: 252,
46
+ boxH: 179,
47
+ },
48
+ title: { cardW: 340, cardH: 0, noteH: 0, memoH: 0, boxW: 352, boxH: 40 },
49
+ };
50
+
51
+ export function cardMetrics(size: CardSize = 'large'): CardMetrics {
52
+ return METRICS[size] ?? METRICS.large;
53
+ }
54
+
15
55
  /** Gap between columns, and between cards inside a column. */
16
56
  const COL_GAP = 88;
17
57
  const ROW_GAP = 52;
@@ -28,12 +68,24 @@ export type LaidColumn = {
28
68
  y: number;
29
69
  from: number;
30
70
  to: number;
71
+ /** Column width at the current card size; the gap between columns starts after it. */
72
+ boxW: number;
73
+ collapsed: boolean;
31
74
  };
32
- export type BoardLayout = { slots: Slot[]; columns: LaidColumn[]; width: number; height: number };
75
+ export type BoardLayout = {
76
+ slots: Slot[];
77
+ columns: LaidColumn[];
78
+ width: number;
79
+ height: number;
80
+ metrics: CardMetrics;
81
+ };
82
+
83
+ export type BoardView = { size?: CardSize; collapsed?: boolean[] };
33
84
 
34
85
  const CHAPTER_NAME = /chapter|section|章/i;
35
86
 
36
- function pageName(page: Page): string {
87
+ /** The name a page was written as, for anywhere the board shows a page by name. */
88
+ export function pageName(page: Page): string {
37
89
  const fn = page as unknown as { displayName?: string; name?: string };
38
90
  return fn.displayName ?? fn.name ?? '';
39
91
  }
@@ -76,14 +128,17 @@ export function seedColumns(pages: Page[], columnTitle: (index: number) => strin
76
128
  }
77
129
 
78
130
  /** Geometry for a structure: columns left to right, pages top to bottom. */
79
- export function layoutBoard(columns: BoardColumn[]): BoardLayout {
131
+ export function layoutBoard(columns: BoardColumn[], view: BoardView = {}): BoardLayout {
132
+ const metrics = cardMetrics(view.size);
133
+ const collapsed = view.collapsed ?? [];
80
134
  const slots: Slot[] = [];
81
135
  const laid: LaidColumn[] = [];
82
136
  let position = 0;
83
137
  let tallest = 0;
138
+ let x = 0;
84
139
 
85
140
  columns.forEach((column, index) => {
86
- const x = index * (CARD_BOX_W + COL_GAP);
141
+ const folded = collapsed[index] === true;
87
142
  laid.push({
88
143
  title: column.title,
89
144
  size: column.size,
@@ -91,20 +146,33 @@ export function layoutBoard(columns: BoardColumn[]): BoardLayout {
91
146
  y: 0,
92
147
  from: position,
93
148
  to: position + column.size - 1,
149
+ boxW: metrics.boxW,
150
+ collapsed: folded,
94
151
  });
95
- for (let row = 0; row < column.size; row++) {
96
- slots.push({ index: position, x, y: HEADER_H + row * (CARD_BOX_H + ROW_GAP), column: index });
97
- position += 1;
152
+ // A folded chapter keeps its pages' positions but draws no cards, so the
153
+ // columns after it close up instead of leaving a long empty lane.
154
+ if (!folded) {
155
+ for (let row = 0; row < column.size; row++) {
156
+ slots.push({
157
+ index: position + row,
158
+ x,
159
+ y: HEADER_H + row * (metrics.boxH + ROW_GAP),
160
+ column: index,
161
+ });
162
+ }
163
+ const height = HEADER_H + column.size * (metrics.boxH + ROW_GAP);
164
+ if (height > tallest) tallest = height;
98
165
  }
99
- const height = HEADER_H + column.size * (CARD_BOX_H + ROW_GAP);
100
- if (height > tallest) tallest = height;
166
+ position += column.size;
167
+ x += metrics.boxW + COL_GAP;
101
168
  });
102
169
 
103
170
  return {
104
171
  slots,
105
172
  columns: laid,
106
- width: columns.length * (CARD_BOX_W + COL_GAP),
173
+ width: x,
107
174
  height: tallest,
175
+ metrics,
108
176
  };
109
177
  }
110
178
 
@@ -124,10 +192,29 @@ export function slotAtPoint(layout: BoardLayout, x: number, y: number, dragged:
124
192
  return best;
125
193
  }
126
194
 
195
+ /**
196
+ * The chapter boundary a point falls on, expressed as the number of chapters to
197
+ * its left, or null when the point is over a column rather than between two.
198
+ */
199
+ export function gapAtPoint(layout: BoardLayout, x: number, y: number): number | null {
200
+ if (y < -HEADER_H || y > Math.max(layout.height, HEADER_H) + 80) return null;
201
+ for (let i = 0; i + 1 < layout.columns.length; i++) {
202
+ const right = layout.columns[i].x + layout.columns[i].boxW;
203
+ const left = layout.columns[i + 1].x;
204
+ if (x > right && x < left) return i + 1;
205
+ }
206
+ return null;
207
+ }
208
+
127
209
  /** Deck position of the card under a point, or null over empty canvas. */
128
210
  export function cardAtPoint(layout: BoardLayout, x: number, y: number): number | null {
129
211
  for (const slot of layout.slots) {
130
- if (x >= slot.x && x <= slot.x + CARD_BOX_W && y >= slot.y && y <= slot.y + CARD_BOX_H) {
212
+ if (
213
+ x >= slot.x &&
214
+ x <= slot.x + layout.metrics.boxW &&
215
+ y >= slot.y &&
216
+ y <= slot.y + layout.metrics.boxH
217
+ ) {
131
218
  return slot.index;
132
219
  }
133
220
  }
@@ -179,3 +266,17 @@ export function orderAfterMove(total: number, from: number, to: number): number[
179
266
  order.splice(to, 0, moved);
180
267
  return order;
181
268
  }
269
+
270
+ /**
271
+ * The deck's order once several cards move together: they travel as one block
272
+ * and keep their order among themselves, whatever order they were picked in.
273
+ * The block starts at the slot they were dropped on, the same slot a single
274
+ * card would take.
275
+ */
276
+ export function orderAfterMoveMany(total: number, moving: number[], to: number): number[] {
277
+ const lifted = [...new Set(moving)].sort((a, b) => a - b);
278
+ if (lifted.length === 0) return identityOrder(total);
279
+ const staying = identityOrder(total).filter((index) => !lifted.includes(index));
280
+ const at = Math.max(0, Math.min(staying.length, to));
281
+ return [...staying.slice(0, at), ...lifted, ...staying.slice(at)];
282
+ }
@@ -0,0 +1,200 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ chapterPlanPrompt,
4
+ chapterWritePrompt,
5
+ pageTaskPrompt,
6
+ parseChapterBlock,
7
+ sanitizeTask,
8
+ stripChapterBlock,
9
+ } from './agent-tasks.ts';
10
+
11
+ const page = { n: 3, title: 'Revenue by quarter' };
12
+
13
+ describe('pageTaskPrompt', () => {
14
+ const prompt = (action: 'rewrite' | 'chart' | 'memo' | 'expand', memo?: string) =>
15
+ pageTaskPrompt({ slideId: 'deck', page: { ...page, memo }, action, lang: 'en' });
16
+
17
+ it('names only the page it works on', () => {
18
+ expect(prompt('rewrite')).toContain(
19
+ 'Work on page 3 "Revenue by quarter" of slides/deck/index.tsx only. Leave every other page, the theme and the speaker notes of other pages as they are.',
20
+ );
21
+ expect(prompt('rewrite')).toContain(
22
+ 'Rewrite this page: keep what it says, make it clearer and better laid out.',
23
+ );
24
+ });
25
+
26
+ it('asks for a chart that keeps the point', () => {
27
+ expect(prompt('chart')).toContain(
28
+ 'Turn the main numbers on this page into a chart. Keep the title and the point it makes.',
29
+ );
30
+ });
31
+
32
+ it('follows the card note', () => {
33
+ expect(prompt('memo', 'lead with the number')).toContain(
34
+ 'Change this page as the note says: "lead with the number"',
35
+ );
36
+ });
37
+
38
+ it('keeps a note’s quotes and newlines as one quoted string', () => {
39
+ const memo = 'Say "first"\nthen stop';
40
+ expect(prompt('memo', memo)).toContain(
41
+ `Change this page as the note says: ${JSON.stringify(memo)}`,
42
+ );
43
+ });
44
+
45
+ it('has nothing to say for a note action without a note', () => {
46
+ expect(prompt('memo')).toBeNull();
47
+ expect(prompt('memo', ' ')).toBeNull();
48
+ });
49
+
50
+ it('asks for a second page right after this one', () => {
51
+ expect(prompt('expand')).toContain(
52
+ 'Expand this page into two pages: keep this one, and add one more right after it that goes deeper. Add a speaker note for the new page.',
53
+ );
54
+ });
55
+
56
+ it('ends with the language and one sentence back', () => {
57
+ expect(
58
+ prompt('rewrite')?.endsWith(
59
+ 'Write in the language "en". When you are done, reply with one short sentence saying what changed.',
60
+ ),
61
+ ).toBe(true);
62
+ });
63
+
64
+ it('leaves out the quotes when the page has no title', () => {
65
+ expect(
66
+ pageTaskPrompt({ slideId: 'd', page: { n: 2, title: '' }, action: 'rewrite', lang: 'en' }),
67
+ ).toContain('Work on page 2 of slides/d/index.tsx only.');
68
+ });
69
+ });
70
+
71
+ describe('chapterPlanPrompt', () => {
72
+ it('plans without writing, and says what the chapter has', () => {
73
+ const prompt = chapterPlanPrompt({
74
+ slideId: 'deck',
75
+ chapter: 1,
76
+ title: 'Why now',
77
+ brief: 'the market moved',
78
+ count: 3,
79
+ pages: [
80
+ { n: 4, title: 'The shift' },
81
+ { n: 5, title: '' },
82
+ ],
83
+ lang: 'zh-CN',
84
+ });
85
+ expect(prompt).toContain(
86
+ 'Plan new pages for chapter 2 "Why now" of slides/deck/index.tsx. This turn is planning only: do not write, edit or create any file.',
87
+ );
88
+ expect(prompt).toContain('The chapter should cover: the market moved');
89
+ expect(prompt).toContain('It has these pages now: page 4 "The shift", page 5.');
90
+ expect(prompt).toContain('Plan 3 new pages that come after them.');
91
+ expect(prompt).toContain('<lemonriver-chapter> and </lemonriver-chapter>');
92
+ expect(prompt).toContain('Write in the language "zh-CN".');
93
+ });
94
+
95
+ it('says so when the chapter is empty', () => {
96
+ const prompt = chapterPlanPrompt({
97
+ slideId: 'deck',
98
+ chapter: 0,
99
+ title: '',
100
+ brief: 'open',
101
+ count: 1,
102
+ pages: [],
103
+ lang: 'en',
104
+ });
105
+ expect(prompt).toContain('It has no pages yet.');
106
+ });
107
+ });
108
+
109
+ describe('chapterWritePrompt', () => {
110
+ const items = [
111
+ { id: 'a', title: 'First', kind: 'page' as const },
112
+ { id: 'b', title: 'Second', kind: 'chart' as const },
113
+ ];
114
+
115
+ it('places the pages after one page', () => {
116
+ const prompt = chapterWritePrompt({
117
+ slideId: 'deck',
118
+ chapter: 1,
119
+ title: 'Why now',
120
+ items,
121
+ anchor: { n: 4, title: 'The shift' },
122
+ lang: 'en',
123
+ });
124
+ expect(prompt).toContain(
125
+ 'Add these pages to chapter 2 "Why now" of slides/deck/index.tsx, right after page 4 "The shift".',
126
+ );
127
+ expect(prompt).toContain('1. "First" (page)');
128
+ expect(prompt).toContain('2. "Second" (chart)');
129
+ expect(prompt).toContain(
130
+ "Follow the deck's existing theme and layout. Add a speaker note for each new page. Do not change any other page.",
131
+ );
132
+ });
133
+
134
+ it('places them at the very start when there is nothing in front', () => {
135
+ const prompt = chapterWritePrompt({
136
+ slideId: 'deck',
137
+ chapter: 0,
138
+ title: '',
139
+ items,
140
+ anchor: null,
141
+ lang: 'en',
142
+ });
143
+ expect(prompt).toContain('of slides/deck/index.tsx, at the start of the deck.');
144
+ });
145
+ });
146
+
147
+ describe('chapter block', () => {
148
+ const block = (items: unknown) =>
149
+ `Planned.\n<lemonriver-chapter>${JSON.stringify(items)}</lemonriver-chapter>`;
150
+
151
+ it('reads the titles the agent planned', () => {
152
+ expect(parseChapterBlock(block([{ title: 'A', kind: 'page' }]))?.map((i) => i.title)).toEqual([
153
+ 'A',
154
+ ]);
155
+ });
156
+
157
+ it('takes the last block, so a retried answer wins', () => {
158
+ const text = `${block([{ title: 'Old', kind: 'page' }])}\n${block([
159
+ { title: 'New', kind: 'page' },
160
+ ])}`;
161
+ expect(parseChapterBlock(text)?.map((i) => i.title)).toEqual(['New']);
162
+ });
163
+
164
+ it('keeps no more than eight', () => {
165
+ const items = Array.from({ length: 10 }, (_, i) => ({ title: `P${i}`, kind: 'page' }));
166
+ expect(parseChapterBlock(block(items))).toHaveLength(8);
167
+ });
168
+
169
+ it('reads nothing when there is no block, or the block is still streaming', () => {
170
+ expect(parseChapterBlock('no block here')).toBeNull();
171
+ expect(parseChapterBlock('Planning… <lemonriver-chapter>[{"title":')).toBeNull();
172
+ });
173
+
174
+ it('takes the block out of what the transcript shows', () => {
175
+ expect(stripChapterBlock(block([{ title: 'A', kind: 'page' }]))).toBe('Planned.');
176
+ expect(stripChapterBlock('Planning <lemonriver-chapter>[{"ti')).toBe('Planning');
177
+ });
178
+ });
179
+
180
+ describe('sanitizeTask', () => {
181
+ it('reads each kind of task', () => {
182
+ expect(sanitizeTask({ kind: 'changes' })).toEqual({ kind: 'changes' });
183
+ expect(sanitizeTask({ kind: 'page', page: 'Two', action: 'expand' })).toEqual({
184
+ kind: 'page',
185
+ page: 'Two',
186
+ action: 'expand',
187
+ });
188
+ expect(sanitizeTask({ kind: 'chapter-write', chapter: 1, title: 'Why now' })).toMatchObject({
189
+ kind: 'chapter-write',
190
+ chapter: 1,
191
+ });
192
+ });
193
+
194
+ it('refuses what it cannot run', () => {
195
+ expect(sanitizeTask(null)).toBeNull();
196
+ expect(sanitizeTask({ kind: 'page', page: 'Two', action: 'delete' })).toBeNull();
197
+ expect(sanitizeTask({ kind: 'chapter-plan', chapter: 0, count: 9 })).toBeNull();
198
+ expect(sanitizeTask({ kind: 'chapter-write', chapter: -1 })).toBeNull();
199
+ });
200
+ });