@lemonriver/core 0.5.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.
- package/dist/{build-CS1goZxt.js → build-RlcGG-Ve.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-BpbcCbUv.js → config-BJT87wE9.js} +1155 -19
- package/dist/{dev-BpYnfgLL.js → dev-DR-j_xda.js} +1 -1
- package/dist/{preview-HSzPPkSs.js → preview-BTAhTb5B.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/lemonriver/board/blocks/classify.test.ts +5 -0
- package/src/app/lemonriver/board/blocks/classify.ts +29 -2
- package/src/app/lemonriver/board/blocks/snapshot.ts +34 -4
- package/src/app/lemonriver/board/board-page.tsx +15 -0
- package/src/app/lemonriver/board/model/agent-tasks.ts +33 -1
- package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
- package/src/app/lemonriver/board/model/self-check.ts +489 -0
- package/src/app/lemonriver/board/page-node.tsx +23 -1
- package/src/app/lemonriver/board/self-check-jump.ts +23 -0
- package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
- package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
- package/src/app/lemonriver/board/use-self-check.ts +117 -0
- package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
- package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
- package/src/app/lemonriver/features.test.ts +5 -0
- package/src/app/lemonriver/features.ts +2 -1
- package/src/app/lemonriver/home/deck-card.tsx +10 -0
- package/src/app/lemonriver/home/home-page.tsx +55 -6
- package/src/app/lemonriver/home/use-deck-times.ts +4 -1
- package/src/app/lemonriver/outline/build-team.tsx +42 -0
- package/src/app/lemonriver/outline/describe-page.tsx +128 -27
- package/src/app/lemonriver/outline/draft.ts +38 -0
- package/src/app/lemonriver/outline/model.test.ts +79 -0
- package/src/app/lemonriver/outline/model.ts +80 -7
- package/src/app/lemonriver/outline/outline-page.tsx +56 -6
- package/src/app/lemonriver/outline/pick-style.tsx +282 -0
- package/src/app/lemonriver/routes.test.tsx +17 -5
- package/src/app/lemonriver/routes.tsx +28 -0
- package/src/app/lemonriver/templates/client.ts +60 -0
- package/src/app/lemonriver/templates/demo-page.tsx +79 -0
- package/src/app/lemonriver/templates/knobs.test.ts +62 -0
- package/src/app/lemonriver/templates/knobs.ts +62 -0
- package/src/app/lemonriver/templates/library-page.tsx +292 -0
- package/src/app/lemonriver/templates/model.test.ts +175 -0
- package/src/app/lemonriver/templates/model.ts +339 -0
- package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
- package/src/app/lemonriver/templates/preview-content.ts +149 -0
- package/src/app/lemonriver/templates/recommend.test.ts +71 -0
- package/src/app/lemonriver/templates/recommend.ts +72 -0
- package/src/app/lemonriver/templates/use-library.ts +58 -0
- package/src/app/lemonriver/templates/virtual.d.ts +28 -0
- package/src/app/lemonriver/text/en.ts +57 -0
- package/src/app/lemonriver/text/es.ts +57 -0
- package/src/app/lemonriver/text/ja.ts +57 -0
- package/src/app/lemonriver/text/types.ts +73 -0
- package/src/app/lemonriver/text/zh-cn.ts +57 -0
- package/src/app/lemonriver/text/zh-tw.ts +57 -0
- package/src/app/lemonriver/theme/pages.test.ts +4 -0
- package/src/app/lemonriver/theme/pages.ts +6 -1
- package/src/app/lemonriver/update/prompt.ts +1 -1
|
@@ -3,7 +3,7 @@ import { A as reorderNotesArrayInSource, B as CONFIG_FILES, C as duplicatePageIn
|
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
-
import { existsSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
6
|
+
import { existsSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
7
7
|
import { execFile, spawn } from "node:child_process";
|
|
8
8
|
import fs from "node:fs/promises";
|
|
9
9
|
import { parse } from "@babel/parser";
|
|
@@ -11,17 +11,679 @@ import { randomBytes, randomUUID } from "node:crypto";
|
|
|
11
11
|
import { promisify } from "node:util";
|
|
12
12
|
import tailwindcss from "@tailwindcss/vite";
|
|
13
13
|
import react from "@vitejs/plugin-react";
|
|
14
|
+
import { loadConfigFromFile, normalizePath } from "vite";
|
|
14
15
|
import os from "node:os";
|
|
15
16
|
import * as t from "@babel/types";
|
|
16
17
|
import { isJSXElement, isJSXFragment } from "@babel/types";
|
|
17
18
|
import fg from "fast-glob";
|
|
18
|
-
|
|
19
|
+
//#region src/app/lemonriver/templates/knobs.ts
|
|
20
|
+
const KNOB_KEYS$1 = [
|
|
21
|
+
"formal",
|
|
22
|
+
"bright",
|
|
23
|
+
"motion"
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* Values under a third lean left, over two thirds lean right; the middle keeps
|
|
27
|
+
* the template's own design. Discrete ends keep a deck's colours reproducible.
|
|
28
|
+
*/
|
|
29
|
+
const LEFT_UNTIL = .34;
|
|
30
|
+
const RIGHT_FROM = .66;
|
|
31
|
+
function knobEnd(knob, value) {
|
|
32
|
+
if (!knob || value === void 0) return null;
|
|
33
|
+
if (value <= LEFT_UNTIL) return knob.left;
|
|
34
|
+
if (value >= RIGHT_FROM) return knob.right;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function mergeOverrides(design, overrides) {
|
|
38
|
+
return {
|
|
39
|
+
palette: {
|
|
40
|
+
...design.palette,
|
|
41
|
+
...overrides.palette
|
|
42
|
+
},
|
|
43
|
+
fonts: {
|
|
44
|
+
...design.fonts,
|
|
45
|
+
...overrides.fonts
|
|
46
|
+
},
|
|
47
|
+
typeScale: {
|
|
48
|
+
...design.typeScale,
|
|
49
|
+
...overrides.typeScale
|
|
50
|
+
},
|
|
51
|
+
radius: overrides.radius ?? design.radius
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** A template that does not define a group keeps its own design for that group. */
|
|
55
|
+
function applyKnobs(design, knobs, values = {}) {
|
|
56
|
+
let out = design;
|
|
57
|
+
for (const key of KNOB_KEYS$1) {
|
|
58
|
+
const end = knobEnd(knobs[key], values[key]);
|
|
59
|
+
if (end?.design) out = mergeOverrides(out, end.design);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function knobFlags(knobs, values = {}) {
|
|
64
|
+
const flags = {};
|
|
65
|
+
for (const key of KNOB_KEYS$1) {
|
|
66
|
+
const end = knobEnd(knobs[key], values[key]);
|
|
67
|
+
if (end?.flags) Object.assign(flags, end.flags);
|
|
68
|
+
}
|
|
69
|
+
return flags;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/app/lemonriver/templates/model.ts
|
|
73
|
+
const OCCASIONS = [
|
|
74
|
+
{
|
|
75
|
+
id: "pitch",
|
|
76
|
+
name: "融资路演"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "report",
|
|
80
|
+
name: "经营汇报"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: "launch",
|
|
84
|
+
name: "产品发布"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
id: "review",
|
|
88
|
+
name: "专业评审"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "event",
|
|
92
|
+
name: "活动招商与品牌"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
id: "training",
|
|
96
|
+
name: "培训课程"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: "retro",
|
|
100
|
+
name: "复盘分享"
|
|
101
|
+
}
|
|
102
|
+
];
|
|
103
|
+
const OCCASION_IDS = new Set(OCCASIONS.map((occasion) => occasion.id));
|
|
104
|
+
const SKELETONS = [
|
|
105
|
+
{
|
|
106
|
+
id: "cover",
|
|
107
|
+
name: "封面",
|
|
108
|
+
component: "CoverSkeleton"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: "overview",
|
|
112
|
+
name: "一页总览",
|
|
113
|
+
component: "OverviewSkeleton"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: "bignum",
|
|
117
|
+
name: "超大单数字",
|
|
118
|
+
component: "BignumSkeleton"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: "figure",
|
|
122
|
+
name: "主图加右栏",
|
|
123
|
+
component: "FigureSkeleton"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: "compare",
|
|
127
|
+
name: "前后对比",
|
|
128
|
+
component: "CompareSkeleton"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: "cards",
|
|
132
|
+
name: "三卡或四卡并列",
|
|
133
|
+
component: "CardsSkeleton"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: "process",
|
|
137
|
+
name: "原理或流程",
|
|
138
|
+
component: "ProcessSkeleton"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: "timeline",
|
|
142
|
+
name: "时间线",
|
|
143
|
+
component: "TimelineSkeleton"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: "risk",
|
|
147
|
+
name: "风险",
|
|
148
|
+
component: "RiskSkeleton"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: "ask",
|
|
152
|
+
name: "请求",
|
|
153
|
+
component: "AskSkeleton"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
id: "section",
|
|
157
|
+
name: "章节页",
|
|
158
|
+
component: "SectionSkeleton"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: "quote",
|
|
162
|
+
name: "引用",
|
|
163
|
+
component: "QuoteSkeleton"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: "end",
|
|
167
|
+
name: "结尾",
|
|
168
|
+
component: "EndSkeleton"
|
|
169
|
+
}
|
|
170
|
+
];
|
|
171
|
+
const SKELETON_IDS$1 = new Set(SKELETONS.map((skeleton) => skeleton.id));
|
|
172
|
+
/** A template may lower the small-type floor, never lift it. */
|
|
173
|
+
const MIN_TYPE_FLOOR = {
|
|
174
|
+
min: 16,
|
|
175
|
+
max: 20
|
|
176
|
+
};
|
|
177
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
178
|
+
const text$2 = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : null;
|
|
179
|
+
const KNOB_KEYS = [
|
|
180
|
+
"formal",
|
|
181
|
+
"bright",
|
|
182
|
+
"motion"
|
|
183
|
+
];
|
|
184
|
+
function readDesign(raw) {
|
|
185
|
+
if (!isRecord(raw)) return null;
|
|
186
|
+
const palette = raw.palette;
|
|
187
|
+
const fonts = raw.fonts;
|
|
188
|
+
const typeScale = raw.typeScale;
|
|
189
|
+
if (!isRecord(palette) || !isRecord(fonts) || !isRecord(typeScale)) return null;
|
|
190
|
+
const bg = text$2(palette.bg);
|
|
191
|
+
const ink = text$2(palette.text);
|
|
192
|
+
const accent = text$2(palette.accent);
|
|
193
|
+
const display = text$2(fonts.display);
|
|
194
|
+
const body = text$2(fonts.body);
|
|
195
|
+
const hero = typeScale.hero;
|
|
196
|
+
const bodySize = typeScale.body;
|
|
197
|
+
const radius = raw.radius;
|
|
198
|
+
if (!bg || !ink || !accent || !display || !body) return null;
|
|
199
|
+
if (typeof hero !== "number" || !Number.isFinite(hero) || hero <= 0) return null;
|
|
200
|
+
if (typeof bodySize !== "number" || !Number.isFinite(bodySize) || bodySize <= 0) return null;
|
|
201
|
+
if (typeof radius !== "number" || !Number.isFinite(radius) || radius < 0) return null;
|
|
202
|
+
return {
|
|
203
|
+
palette: {
|
|
204
|
+
bg,
|
|
205
|
+
text: ink,
|
|
206
|
+
accent
|
|
207
|
+
},
|
|
208
|
+
fonts: {
|
|
209
|
+
display,
|
|
210
|
+
body
|
|
211
|
+
},
|
|
212
|
+
typeScale: {
|
|
213
|
+
hero,
|
|
214
|
+
body: bodySize
|
|
215
|
+
},
|
|
216
|
+
radius
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function readOverrides(raw) {
|
|
220
|
+
if (!isRecord(raw)) return void 0;
|
|
221
|
+
const out = {};
|
|
222
|
+
if (isRecord(raw.palette)) {
|
|
223
|
+
const palette = {};
|
|
224
|
+
for (const key of [
|
|
225
|
+
"bg",
|
|
226
|
+
"text",
|
|
227
|
+
"accent"
|
|
228
|
+
]) {
|
|
229
|
+
const value = text$2(raw.palette[key]);
|
|
230
|
+
if (value) palette[key] = value;
|
|
231
|
+
}
|
|
232
|
+
if (Object.keys(palette).length > 0) out.palette = palette;
|
|
233
|
+
}
|
|
234
|
+
if (isRecord(raw.fonts)) {
|
|
235
|
+
const fonts = {};
|
|
236
|
+
for (const key of ["display", "body"]) {
|
|
237
|
+
const value = text$2(raw.fonts[key]);
|
|
238
|
+
if (value) fonts[key] = value;
|
|
239
|
+
}
|
|
240
|
+
if (Object.keys(fonts).length > 0) out.fonts = fonts;
|
|
241
|
+
}
|
|
242
|
+
if (isRecord(raw.typeScale)) {
|
|
243
|
+
const typeScale = {};
|
|
244
|
+
for (const key of ["hero", "body"]) {
|
|
245
|
+
const value = raw.typeScale[key];
|
|
246
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) typeScale[key] = value;
|
|
247
|
+
}
|
|
248
|
+
if (Object.keys(typeScale).length > 0) out.typeScale = typeScale;
|
|
249
|
+
}
|
|
250
|
+
const radius = raw.radius;
|
|
251
|
+
if (typeof radius === "number" && Number.isFinite(radius) && radius >= 0) out.radius = radius;
|
|
252
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
253
|
+
}
|
|
254
|
+
function readKnobEnd(raw) {
|
|
255
|
+
if (!isRecord(raw)) return null;
|
|
256
|
+
const label = text$2(raw.label);
|
|
257
|
+
if (!label) return null;
|
|
258
|
+
const design = readOverrides(raw.design);
|
|
259
|
+
const flags = {};
|
|
260
|
+
if (isRecord(raw.flags)) {
|
|
261
|
+
for (const [key, value] of Object.entries(raw.flags)) if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") flags[key] = value;
|
|
262
|
+
}
|
|
263
|
+
const end = { label };
|
|
264
|
+
if (design) end.design = design;
|
|
265
|
+
if (Object.keys(flags).length > 0) end.flags = flags;
|
|
266
|
+
return end;
|
|
267
|
+
}
|
|
268
|
+
function readKnobs(raw) {
|
|
269
|
+
if (!isRecord(raw)) return null;
|
|
270
|
+
const knobs = {};
|
|
271
|
+
for (const key of KNOB_KEYS) {
|
|
272
|
+
const knob = raw[key];
|
|
273
|
+
if (!isRecord(knob)) return null;
|
|
274
|
+
const left = readKnobEnd(knob.left);
|
|
275
|
+
const right = readKnobEnd(knob.right);
|
|
276
|
+
if (!left || !right) return null;
|
|
277
|
+
knobs[key] = {
|
|
278
|
+
left,
|
|
279
|
+
right
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return knobs;
|
|
283
|
+
}
|
|
284
|
+
function readFonts(raw) {
|
|
285
|
+
if (!Array.isArray(raw)) return null;
|
|
286
|
+
const fonts = [];
|
|
287
|
+
for (const entry of raw) {
|
|
288
|
+
if (!isRecord(entry)) return null;
|
|
289
|
+
const file = text$2(entry.file);
|
|
290
|
+
const license = text$2(entry.license);
|
|
291
|
+
if (!file || !license) return null;
|
|
292
|
+
fonts.push({
|
|
293
|
+
file,
|
|
294
|
+
license
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return fonts;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Turns a `template.json` into a template, or names the first thing that is
|
|
301
|
+
* wrong with it. A template the library cannot trust is dropped whole rather
|
|
302
|
+
* than half-read.
|
|
303
|
+
*/
|
|
304
|
+
function sanitizeTemplate(raw, expectedId) {
|
|
305
|
+
if (!isRecord(raw)) return {
|
|
306
|
+
ok: false,
|
|
307
|
+
reason: "not a JSON object"
|
|
308
|
+
};
|
|
309
|
+
const id = text$2(raw.id);
|
|
310
|
+
if (!id || !/^[a-z0-9][a-z0-9-]*$/.test(id)) return {
|
|
311
|
+
ok: false,
|
|
312
|
+
reason: "id must be lowercase letters, digits and dashes"
|
|
313
|
+
};
|
|
314
|
+
if (expectedId !== void 0 && id !== expectedId) return {
|
|
315
|
+
ok: false,
|
|
316
|
+
reason: `id "${id}" does not match the folder "${expectedId}"`
|
|
317
|
+
};
|
|
318
|
+
const name = text$2(raw.name);
|
|
319
|
+
if (!name) return {
|
|
320
|
+
ok: false,
|
|
321
|
+
reason: "missing name"
|
|
322
|
+
};
|
|
323
|
+
const audience = text$2(raw.audience);
|
|
324
|
+
if (!audience) return {
|
|
325
|
+
ok: false,
|
|
326
|
+
reason: "missing audience"
|
|
327
|
+
};
|
|
328
|
+
const object = text$2(raw.object);
|
|
329
|
+
if (!object) return {
|
|
330
|
+
ok: false,
|
|
331
|
+
reason: "missing object"
|
|
332
|
+
};
|
|
333
|
+
const reason = text$2(raw.reason);
|
|
334
|
+
if (!reason) return {
|
|
335
|
+
ok: false,
|
|
336
|
+
reason: "missing reason"
|
|
337
|
+
};
|
|
338
|
+
const accent = text$2(raw.accent);
|
|
339
|
+
if (!accent) return {
|
|
340
|
+
ok: false,
|
|
341
|
+
reason: "missing accent"
|
|
342
|
+
};
|
|
343
|
+
const occasionsRaw = raw.occasions;
|
|
344
|
+
if (!Array.isArray(occasionsRaw) || occasionsRaw.length === 0) return {
|
|
345
|
+
ok: false,
|
|
346
|
+
reason: "missing occasions"
|
|
347
|
+
};
|
|
348
|
+
const occasions = [];
|
|
349
|
+
for (const entry of occasionsRaw) {
|
|
350
|
+
if (typeof entry !== "string" || !OCCASION_IDS.has(entry)) return {
|
|
351
|
+
ok: false,
|
|
352
|
+
reason: `unknown occasion "${String(entry)}"`
|
|
353
|
+
};
|
|
354
|
+
if (!occasions.includes(entry)) occasions.push(entry);
|
|
355
|
+
}
|
|
356
|
+
const design = readDesign(raw.design);
|
|
357
|
+
if (!design) return {
|
|
358
|
+
ok: false,
|
|
359
|
+
reason: "missing or malformed design"
|
|
360
|
+
};
|
|
361
|
+
const knobs = readKnobs(raw.knobs);
|
|
362
|
+
if (!knobs) return {
|
|
363
|
+
ok: false,
|
|
364
|
+
reason: "missing or malformed knobs"
|
|
365
|
+
};
|
|
366
|
+
const skeletonsRaw = raw.skeletons;
|
|
367
|
+
if (!Array.isArray(skeletonsRaw) || skeletonsRaw.length === 0) return {
|
|
368
|
+
ok: false,
|
|
369
|
+
reason: "missing skeletons"
|
|
370
|
+
};
|
|
371
|
+
const skeletons = [];
|
|
372
|
+
for (const entry of skeletonsRaw) {
|
|
373
|
+
if (typeof entry !== "string" || !SKELETON_IDS$1.has(entry)) return {
|
|
374
|
+
ok: false,
|
|
375
|
+
reason: `unknown skeleton "${String(entry)}"`
|
|
376
|
+
};
|
|
377
|
+
if (!skeletons.includes(entry)) skeletons.push(entry);
|
|
378
|
+
}
|
|
379
|
+
const fonts = readFonts(raw.fonts);
|
|
380
|
+
if (!fonts) return {
|
|
381
|
+
ok: false,
|
|
382
|
+
reason: "missing or malformed fonts"
|
|
383
|
+
};
|
|
384
|
+
const source = raw.source;
|
|
385
|
+
if (source !== null && source !== void 0 && typeof source !== "string") return {
|
|
386
|
+
ok: false,
|
|
387
|
+
reason: "source must be a string or null"
|
|
388
|
+
};
|
|
389
|
+
const minTypeRaw = raw.minType;
|
|
390
|
+
if (minTypeRaw !== void 0 && minTypeRaw !== null) {
|
|
391
|
+
if (typeof minTypeRaw !== "number" || !Number.isInteger(minTypeRaw) || minTypeRaw < MIN_TYPE_FLOOR.min || minTypeRaw > MIN_TYPE_FLOOR.max) return {
|
|
392
|
+
ok: false,
|
|
393
|
+
reason: `minType must be an integer from ${MIN_TYPE_FLOOR.min} to ${MIN_TYPE_FLOOR.max}`
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
const previewPages = Array.isArray(raw.previewPages) ? raw.previewPages.filter((n) => typeof n === "number" && Number.isInteger(n) && n >= 1).slice(0, 8) : [];
|
|
397
|
+
return {
|
|
398
|
+
ok: true,
|
|
399
|
+
template: {
|
|
400
|
+
id,
|
|
401
|
+
name,
|
|
402
|
+
occasions,
|
|
403
|
+
audience,
|
|
404
|
+
object,
|
|
405
|
+
reason,
|
|
406
|
+
accent,
|
|
407
|
+
design,
|
|
408
|
+
knobs,
|
|
409
|
+
skeletons,
|
|
410
|
+
previewPages: previewPages.length > 0 ? previewPages : null,
|
|
411
|
+
minType: typeof minTypeRaw === "number" ? minTypeRaw : void 0,
|
|
412
|
+
fonts,
|
|
413
|
+
source: typeof source === "string" && source.trim() !== "" ? source.trim() : null
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/** The board record written when a template is applied; anything else is ignored. */
|
|
418
|
+
function sanitizeAppliedTemplate(raw) {
|
|
419
|
+
if (!isRecord(raw)) return null;
|
|
420
|
+
const id = text$2(raw.id);
|
|
421
|
+
const appliedAt = raw.appliedAt;
|
|
422
|
+
if (!id || typeof appliedAt !== "number" || !Number.isFinite(appliedAt)) return null;
|
|
423
|
+
return {
|
|
424
|
+
id,
|
|
425
|
+
knobs: sanitizeKnobValues(raw.knobs),
|
|
426
|
+
appliedAt
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
function sanitizeKnobValues(raw) {
|
|
430
|
+
if (!isRecord(raw)) return {};
|
|
431
|
+
const values = {};
|
|
432
|
+
for (const key of KNOB_KEYS) {
|
|
433
|
+
const value = raw[key];
|
|
434
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1) values[key] = value;
|
|
435
|
+
}
|
|
436
|
+
return values;
|
|
437
|
+
}
|
|
438
|
+
function designSnippet(design) {
|
|
439
|
+
return `export const design: DesignSystem = ${JSON.stringify(design, null, 2)};`;
|
|
440
|
+
}
|
|
441
|
+
//#endregion
|
|
442
|
+
//#region src/lemonriver/templates/library.ts
|
|
443
|
+
const TEMPLATES_ENV = "LEMONRIVER_TEMPLATES_DIR";
|
|
444
|
+
const SKIP_DIR = /^(?:[._]|node_modules$)/;
|
|
445
|
+
function corePackageRoot(from = import.meta.url) {
|
|
446
|
+
let dir = path.dirname(fileURLToPath(from));
|
|
447
|
+
while (dir !== path.dirname(dir)) {
|
|
448
|
+
if (existsSync(path.join(dir, "package.json"))) return dir;
|
|
449
|
+
dir = path.dirname(dir);
|
|
450
|
+
}
|
|
451
|
+
return path.dirname(fileURLToPath(from));
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* A folder is the template library when it carries `_shared/` or at least one
|
|
455
|
+
* `<id>/template.json`. That keeps an unrelated `templates/` folder in a user's
|
|
456
|
+
* project from being mistaken for one.
|
|
457
|
+
*/
|
|
458
|
+
function looksLikeLibrary(dir) {
|
|
459
|
+
try {
|
|
460
|
+
if (statSync(path.join(dir, "_shared")).isDirectory()) return true;
|
|
461
|
+
} catch {}
|
|
462
|
+
try {
|
|
463
|
+
return readdirSync(dir, { withFileTypes: true }).some((entry) => entry.isDirectory() && !SKIP_DIR.test(entry.name) && existsSync(path.join(dir, entry.name, "template.json")));
|
|
464
|
+
} catch {
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* `LEMONRIVER_TEMPLATES_DIR` wins when it is set — a set-but-missing folder
|
|
470
|
+
* means "no library", not "fall back". With it unset, walk up from core's own
|
|
471
|
+
* package until a library folder appears; only a dev checkout has one, because
|
|
472
|
+
* the library never ships in the package.
|
|
473
|
+
*/
|
|
474
|
+
function resolveTemplatesRoot(opts) {
|
|
475
|
+
const fromEnv = (opts.env ?? process.env)[TEMPLATES_ENV];
|
|
476
|
+
if (fromEnv !== void 0) {
|
|
477
|
+
const trimmed = fromEnv.trim();
|
|
478
|
+
if (!trimmed) return null;
|
|
479
|
+
const abs = path.resolve(opts.cwd ?? process.cwd(), trimmed);
|
|
480
|
+
return looksLikeLibrary(abs) ? abs : null;
|
|
481
|
+
}
|
|
482
|
+
let dir = path.resolve(opts.coreRoot);
|
|
483
|
+
while (true) {
|
|
484
|
+
const candidate = path.join(dir, "templates");
|
|
485
|
+
if (looksLikeLibrary(candidate)) return candidate;
|
|
486
|
+
const parent = path.dirname(dir);
|
|
487
|
+
if (parent === dir) return null;
|
|
488
|
+
dir = parent;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const TEMPLATE_ID_RE$1 = /^[a-z0-9][a-z0-9-]*$/;
|
|
492
|
+
/** The sample deck's page count, read from its default export without running it. */
|
|
493
|
+
async function demoPageCount(dir) {
|
|
494
|
+
try {
|
|
495
|
+
const source = await fs.readFile(path.join(dir, "demo", "index.tsx"), "utf8");
|
|
496
|
+
let decl = (parse(source, {
|
|
497
|
+
sourceType: "module",
|
|
498
|
+
plugins: ["typescript", "jsx"]
|
|
499
|
+
}).program?.body ?? []).find((item) => item.type === "ExportDefaultDeclaration")?.declaration;
|
|
500
|
+
while (decl && (decl.type === "TSAsExpression" || decl.type === "TSSatisfiesExpression")) decl = decl.expression;
|
|
501
|
+
if (decl?.type !== "ArrayExpression") return 0;
|
|
502
|
+
return (decl.elements ?? []).length;
|
|
503
|
+
} catch {
|
|
504
|
+
return 0;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
async function loadTemplates(root, log = (line) => console.warn(line)) {
|
|
508
|
+
if (!root) return [];
|
|
509
|
+
let dirents;
|
|
510
|
+
try {
|
|
511
|
+
dirents = await fs.readdir(root, { withFileTypes: true });
|
|
512
|
+
} catch {
|
|
513
|
+
return [];
|
|
514
|
+
}
|
|
515
|
+
const entries = [];
|
|
516
|
+
for (const dirent of dirents.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
517
|
+
if (!dirent.isDirectory() || SKIP_DIR.test(dirent.name)) continue;
|
|
518
|
+
const id = dirent.name;
|
|
519
|
+
if (!TEMPLATE_ID_RE$1.test(id)) {
|
|
520
|
+
log(`template ${id}: folder name must be lowercase letters, digits and dashes`);
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
let raw;
|
|
524
|
+
try {
|
|
525
|
+
raw = JSON.parse(await fs.readFile(path.join(root, id, "template.json"), "utf8"));
|
|
526
|
+
} catch (err) {
|
|
527
|
+
log(`template ${id}: ${err.message}`);
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
const read = sanitizeTemplate(raw, id);
|
|
531
|
+
if (!read.ok) {
|
|
532
|
+
log(`template ${id}: ${read.reason}`);
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
const dir = path.join(root, id);
|
|
536
|
+
const hasDemo = existsSync(path.join(dir, "demo", "index.tsx"));
|
|
537
|
+
entries.push({
|
|
538
|
+
template: read.template,
|
|
539
|
+
dir,
|
|
540
|
+
hasDemo,
|
|
541
|
+
pages: hasDemo ? await demoPageCount(dir) : 0
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return entries;
|
|
545
|
+
}
|
|
546
|
+
function summarize(entry) {
|
|
547
|
+
const { id, name, occasions, audience, object, reason, accent, design, knobs, skeletons, previewPages } = entry.template;
|
|
548
|
+
return {
|
|
549
|
+
id,
|
|
550
|
+
name,
|
|
551
|
+
occasions,
|
|
552
|
+
audience,
|
|
553
|
+
object,
|
|
554
|
+
reason,
|
|
555
|
+
accent,
|
|
556
|
+
design,
|
|
557
|
+
knobs,
|
|
558
|
+
skeletons,
|
|
559
|
+
previewPages,
|
|
560
|
+
minType: entry.template.minType,
|
|
561
|
+
hasDemo: entry.hasDemo,
|
|
562
|
+
pages: entry.pages
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
async function readTemplateListing(root) {
|
|
566
|
+
const entries = await loadTemplates(root);
|
|
567
|
+
return {
|
|
568
|
+
available: root !== null,
|
|
569
|
+
occasions: OCCASIONS,
|
|
570
|
+
templates: entries.map(summarize)
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
/** Only an id the library already accepted can name a file; nothing is joined blindly. */
|
|
574
|
+
async function readTemplateGuide(root, id) {
|
|
575
|
+
if (!root || !TEMPLATE_ID_RE$1.test(id)) return null;
|
|
576
|
+
if (!(await loadTemplates(root, () => {})).some((entry) => entry.template.id === id)) return null;
|
|
577
|
+
try {
|
|
578
|
+
return await fs.readFile(path.join(root, id, "GUIDE.md"), "utf8");
|
|
579
|
+
} catch {
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region src/lemonriver/templates/vite-plugin.ts
|
|
585
|
+
const TEMPLATES_VMOD = "virtual:open-slide/templates";
|
|
586
|
+
function resolved$2(id) {
|
|
587
|
+
return `\0${id}`;
|
|
588
|
+
}
|
|
589
|
+
function generateModule(templates) {
|
|
590
|
+
const meta = templates.map(summarize);
|
|
591
|
+
const casesFor = (file, ok) => templates.flatMap((entry) => {
|
|
592
|
+
if (!ok(entry)) return [];
|
|
593
|
+
const abs = path.join(entry.dir, file);
|
|
594
|
+
const importPath = `@fs/${normalizePath(abs).replace(/^\/+/, "")}`;
|
|
595
|
+
return [` case ${JSON.stringify(entry.template.id)}: return import(/* @vite-ignore */ import.meta.env.BASE_URL + ${JSON.stringify(importPath)});`];
|
|
596
|
+
}).join("\n");
|
|
597
|
+
const demoCases = casesFor(path.join("demo", "index.tsx"), (entry) => entry.hasDemo);
|
|
598
|
+
const previewCases = casesFor(path.join("kit", "preview.tsx"), (entry) => existsSync(path.join(entry.dir, "kit", "preview.tsx")));
|
|
599
|
+
return `// virtual:open-slide/templates — generated
|
|
600
|
+
export const templates = ${JSON.stringify(meta)};
|
|
601
|
+
|
|
602
|
+
export async function loadTemplateDemo(id) {
|
|
603
|
+
switch (id) {
|
|
604
|
+
${demoCases}
|
|
605
|
+
default: throw new Error('Template demo not found: ' + id);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export async function loadTemplatePreview(id) {
|
|
610
|
+
switch (id) {
|
|
611
|
+
${previewCases}
|
|
612
|
+
default: throw new Error('Template preview not found: ' + id);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
`;
|
|
616
|
+
}
|
|
617
|
+
const EMPTY_MODULE = `// virtual:open-slide/templates — the library is dev-only
|
|
618
|
+
export const templates = [];
|
|
619
|
+
|
|
620
|
+
export async function loadTemplateDemo(id) {
|
|
621
|
+
throw new Error('Template demos are only available in dev: ' + id);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export async function loadTemplatePreview(id) {
|
|
625
|
+
throw new Error('Template previews are only available in dev: ' + id);
|
|
626
|
+
}
|
|
627
|
+
`;
|
|
628
|
+
function templatesPlugin(opts) {
|
|
629
|
+
let isDev = false;
|
|
630
|
+
const currentRoot = () => resolveTemplatesRoot({
|
|
631
|
+
coreRoot: opts.coreRoot,
|
|
632
|
+
cwd: opts.userCwd
|
|
633
|
+
});
|
|
634
|
+
return {
|
|
635
|
+
name: "lemonriver:templates",
|
|
636
|
+
config(_config, env) {
|
|
637
|
+
isDev = env.command === "serve";
|
|
638
|
+
},
|
|
639
|
+
configResolved(config) {
|
|
640
|
+
const root = currentRoot();
|
|
641
|
+
if (root) config.server.fs.allow.push(root);
|
|
642
|
+
},
|
|
643
|
+
resolveId(id) {
|
|
644
|
+
if (id === TEMPLATES_VMOD) return resolved$2(TEMPLATES_VMOD);
|
|
645
|
+
return null;
|
|
646
|
+
},
|
|
647
|
+
async load(id) {
|
|
648
|
+
if (id !== resolved$2(TEMPLATES_VMOD)) return null;
|
|
649
|
+
if (!isDev) return EMPTY_MODULE;
|
|
650
|
+
return generateModule(await loadTemplates(currentRoot()));
|
|
651
|
+
},
|
|
652
|
+
configureServer(server) {
|
|
653
|
+
const root = currentRoot();
|
|
654
|
+
if (!root) return;
|
|
655
|
+
const under = (file) => {
|
|
656
|
+
const rel = path.relative(root, file);
|
|
657
|
+
return rel !== "" && !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
658
|
+
};
|
|
659
|
+
let reloadTimer = null;
|
|
660
|
+
const reload = () => {
|
|
661
|
+
if (reloadTimer) clearTimeout(reloadTimer);
|
|
662
|
+
reloadTimer = setTimeout(() => {
|
|
663
|
+
reloadTimer = null;
|
|
664
|
+
const mod = server.moduleGraph.getModuleById(resolved$2(TEMPLATES_VMOD));
|
|
665
|
+
if (mod) server.moduleGraph.invalidateModule(mod);
|
|
666
|
+
server.ws.send({ type: "full-reload" });
|
|
667
|
+
}, 150);
|
|
668
|
+
};
|
|
669
|
+
if (existsSync(root)) server.watcher.add(root);
|
|
670
|
+
for (const event of [
|
|
671
|
+
"add",
|
|
672
|
+
"change",
|
|
673
|
+
"unlink"
|
|
674
|
+
]) server.watcher.on(event, (file) => {
|
|
675
|
+
if (under(file)) reload();
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
//#endregion
|
|
19
681
|
//#region src/app/lemonriver/theme/pages.ts
|
|
20
682
|
/**
|
|
21
683
|
* BRAND_PAGES as a path pattern, for the pre-paint script, which runs before
|
|
22
684
|
* any module loads. pages.test.ts keeps the two in step.
|
|
23
685
|
*/
|
|
24
|
-
const BRAND_PATH_SOURCE = "^/(themes(/.*)?|assets|settings|trash|new)?/*$";
|
|
686
|
+
const BRAND_PATH_SOURCE = "^/(themes(/.*)?|assets|settings|trash|new|templates)?/*$";
|
|
25
687
|
/** Agent screenshots and thumbnails render through this route; nothing may cover it. */
|
|
26
688
|
const PREVIEW_PATH_SOURCE = "^/s/[^/]+/preview/*$";
|
|
27
689
|
const REDUCE_MOTION_KEY = "lemonriver:reduce-motion";
|
|
@@ -1370,6 +2032,17 @@ const OUTLINE_PAGE_SIZES = [
|
|
|
1370
2032
|
12
|
|
1371
2033
|
];
|
|
1372
2034
|
const MAX_BRIEF = 2e3;
|
|
2035
|
+
const MAX_TEMPLATE_PROMPT = 8e3;
|
|
2036
|
+
const SKELETON_IDS = new Set(SKELETONS.map((skeleton) => skeleton.id));
|
|
2037
|
+
function sanitizeSkeletons(raw) {
|
|
2038
|
+
if (!Array.isArray(raw)) return [];
|
|
2039
|
+
const out = [];
|
|
2040
|
+
for (const entry of raw) {
|
|
2041
|
+
if (typeof entry !== "string" || !SKELETON_IDS.has(entry)) continue;
|
|
2042
|
+
if (!out.includes(entry)) out.push(entry);
|
|
2043
|
+
}
|
|
2044
|
+
return out;
|
|
2045
|
+
}
|
|
1373
2046
|
const ID_RE = /^[a-z0-9-]{1,40}$/;
|
|
1374
2047
|
const BLOCK_RE = /<lemonriver-outline>([\s\S]*?)<\/lemonriver-outline>/gi;
|
|
1375
2048
|
let idSeq = 0;
|
|
@@ -1383,6 +2056,12 @@ function isPageKind(value) {
|
|
|
1383
2056
|
function isOutlineState(value) {
|
|
1384
2057
|
return typeof value === "string" && OUTLINE_STATES.includes(value);
|
|
1385
2058
|
}
|
|
2059
|
+
function isOccasionId(value) {
|
|
2060
|
+
return typeof value === "string" && OCCASIONS.some((occasion) => occasion.id === value);
|
|
2061
|
+
}
|
|
2062
|
+
function cleanAudience(value) {
|
|
2063
|
+
return typeof value === "string" ? value.trim().slice(0, 40) : "";
|
|
2064
|
+
}
|
|
1386
2065
|
function cleanTitle(value) {
|
|
1387
2066
|
return typeof value === "string" ? value.trim().slice(0, 120) : "";
|
|
1388
2067
|
}
|
|
@@ -1487,7 +2166,12 @@ function sanitizeOutline(raw) {
|
|
|
1487
2166
|
dispatched: cleanStep(r.dispatched, items.length),
|
|
1488
2167
|
lastTurn: cleanTurn(r.lastTurn, items.length),
|
|
1489
2168
|
prefs: sanitizePrefs(r.prefs),
|
|
1490
|
-
decisions: sanitizeDecisions(r.decisions)
|
|
2169
|
+
decisions: sanitizeDecisions(r.decisions),
|
|
2170
|
+
occasion: isOccasionId(r.occasion) ? r.occasion : null,
|
|
2171
|
+
audience: cleanAudience(r.audience),
|
|
2172
|
+
templatePrompt: typeof r.templatePrompt === "string" ? r.templatePrompt.slice(0, MAX_TEMPLATE_PROMPT) : "",
|
|
2173
|
+
templateSkeletons: sanitizeSkeletons(r.templateSkeletons),
|
|
2174
|
+
templateId: typeof r.templateId === "string" && /^[a-z0-9][a-z0-9-]*$/.test(r.templateId) ? r.templateId : null
|
|
1491
2175
|
};
|
|
1492
2176
|
}
|
|
1493
2177
|
/** The last outline block wins: a retried answer may carry an earlier draft. */
|
|
@@ -1515,22 +2199,47 @@ const KIND_GUIDE = {
|
|
|
1515
2199
|
page: "body — a normal content page",
|
|
1516
2200
|
end: "closing — the last page and what should happen next"
|
|
1517
2201
|
};
|
|
2202
|
+
const TEMPLATE_PAGE_LINE = "Keep using the template in ./kit/ — follow kit/docs/GUIDE.md and pick one skeleton per page.";
|
|
2203
|
+
const TEMPLATE_REVIEW_LINE = "Before you finish, take a screenshot of every page (see the slide-authoring skill's preview section) and go through kit/docs/质量清单.md item by item; fix what fails. Reply with the items you could not satisfy, if any.";
|
|
2204
|
+
const FRAMEWORK_REVIEW_LINE = "Before you finish, take a screenshot of every page (see the slide-authoring skill's preview section) and go through the pre-delivery checklist at the end of the slide-authoring skill item by item; fix what fails. Reply with the items you could not satisfy, if any.";
|
|
2205
|
+
const SKELETON_FOR_KIND = {
|
|
2206
|
+
cover: ["cover"],
|
|
2207
|
+
kpi: ["overview", "bignum"],
|
|
2208
|
+
chart: ["figure"],
|
|
2209
|
+
list: ["cards"],
|
|
2210
|
+
page: ["compare", "process"],
|
|
2211
|
+
end: ["end"]
|
|
2212
|
+
};
|
|
2213
|
+
/** The skeleton the template offers for a page of this kind, if it has one. */
|
|
2214
|
+
function skeletonFor(kind, available) {
|
|
2215
|
+
return SKELETON_FOR_KIND[kind].find((id) => available.includes(id)) ?? null;
|
|
2216
|
+
}
|
|
1518
2217
|
function pagePrompt(outline, slideId, k) {
|
|
1519
2218
|
const file = `slides/${slideId}/index.tsx`;
|
|
1520
2219
|
const page = outline.items[k - 1];
|
|
1521
2220
|
if (k >= 2 && page) {
|
|
1522
2221
|
const title = page.title || "(untitled)";
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
2222
|
+
const skeleton = skeletonFor(page.kind, outline.templateSkeletons);
|
|
2223
|
+
const lines = [`Page ${k} of ${outline.items.length}: [${page.kind}] ${title}${skeleton ? ` → ${skeleton}` : ""}. Write only this page; leave the others as they are.`];
|
|
2224
|
+
if (outline.templatePrompt) lines.push(TEMPLATE_PAGE_LINE);
|
|
2225
|
+
lines.push(...prefsLines(outline.prefs, "page"));
|
|
2226
|
+
return lines.join("\n");
|
|
2227
|
+
}
|
|
2228
|
+
const list = outline.items.map((item, i) => {
|
|
2229
|
+
const base = `${i + 1}. [${item.kind}] ${item.title || "(untitled)"}`;
|
|
2230
|
+
const skeleton = skeletonFor(item.kind, outline.templateSkeletons);
|
|
2231
|
+
return skeleton ? `${base} → ${skeleton}` : base;
|
|
2232
|
+
}).join("\n");
|
|
2233
|
+
const lines = [
|
|
1527
2234
|
`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:`,
|
|
1528
2235
|
list,
|
|
1529
2236
|
"Page kinds and how to write them:",
|
|
1530
2237
|
...PAGE_KINDS.map((kind) => `- ${KIND_GUIDE[kind]}`),
|
|
1531
2238
|
"Numbers from the reference files stay exactly as written there.",
|
|
1532
2239
|
...prefsLines(outline.prefs, "page")
|
|
1533
|
-
]
|
|
2240
|
+
];
|
|
2241
|
+
if (outline.templatePrompt) return `${outline.templatePrompt}\n\n${lines.join("\n")}`;
|
|
2242
|
+
return lines.join("\n");
|
|
1534
2243
|
}
|
|
1535
2244
|
function reviewPrompt(outline, slideId) {
|
|
1536
2245
|
const lines = [
|
|
@@ -1538,6 +2247,7 @@ function reviewPrompt(outline, slideId) {
|
|
|
1538
2247
|
`Check each page of slides/${slideId}/index.tsx against the outline and the reference files in slides/${slideId}/assets/:`,
|
|
1539
2248
|
"fix typos and any number that differs from the files."
|
|
1540
2249
|
];
|
|
2250
|
+
lines.push(outline.templatePrompt ? TEMPLATE_REVIEW_LINE : FRAMEWORK_REVIEW_LINE);
|
|
1541
2251
|
if (outline.prefs.askOnMismatch) lines.push("If two files give different numbers for the same thing, ask the user which to use before changing it.");
|
|
1542
2252
|
lines.push("Do not add or remove pages.", ...prefsLines(outline.prefs, "review"));
|
|
1543
2253
|
return lines.join(" ");
|
|
@@ -1608,6 +2318,79 @@ function lastErrorText(items, userItemId) {
|
|
|
1608
2318
|
return null;
|
|
1609
2319
|
}
|
|
1610
2320
|
//#endregion
|
|
2321
|
+
//#region src/app/lemonriver/board/model/self-check.ts
|
|
2322
|
+
const FINDING_KINDS = [
|
|
2323
|
+
"tiny",
|
|
2324
|
+
"small-body",
|
|
2325
|
+
"overflow",
|
|
2326
|
+
"overlap",
|
|
2327
|
+
"no-notes",
|
|
2328
|
+
"long-title",
|
|
2329
|
+
"accent"
|
|
2330
|
+
];
|
|
2331
|
+
const BOX_KEYS = [
|
|
2332
|
+
"x",
|
|
2333
|
+
"y",
|
|
2334
|
+
"w",
|
|
2335
|
+
"h"
|
|
2336
|
+
];
|
|
2337
|
+
function readBox(raw) {
|
|
2338
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return void 0;
|
|
2339
|
+
const record = raw;
|
|
2340
|
+
const box = {};
|
|
2341
|
+
for (const key of BOX_KEYS) {
|
|
2342
|
+
const value = record[key];
|
|
2343
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return void 0;
|
|
2344
|
+
box[key] = value;
|
|
2345
|
+
}
|
|
2346
|
+
return box;
|
|
2347
|
+
}
|
|
2348
|
+
function readCount(raw) {
|
|
2349
|
+
return typeof raw === "number" && Number.isFinite(raw) ? Math.round(raw) : void 0;
|
|
2350
|
+
}
|
|
2351
|
+
function sanitizeFindings(raw) {
|
|
2352
|
+
if (!Array.isArray(raw)) return [];
|
|
2353
|
+
return raw.flatMap((entry) => {
|
|
2354
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) return [];
|
|
2355
|
+
const record = entry;
|
|
2356
|
+
const page = record.page;
|
|
2357
|
+
const kind = record.kind;
|
|
2358
|
+
if (typeof page !== "number" || !Number.isInteger(page) || page < 1 || page > 999) return [];
|
|
2359
|
+
if (typeof kind !== "string" || !FINDING_KINDS.includes(kind)) return [];
|
|
2360
|
+
const boxes = Array.isArray(record.boxes) ? record.boxes.flatMap((box) => readBox(box) ?? []) : (() => {
|
|
2361
|
+
const box = readBox(record.box);
|
|
2362
|
+
return box ? [box] : [];
|
|
2363
|
+
})();
|
|
2364
|
+
const finding = {
|
|
2365
|
+
page,
|
|
2366
|
+
kind,
|
|
2367
|
+
detail: typeof record.detail === "string" ? record.detail.slice(0, 160) : "",
|
|
2368
|
+
boxes,
|
|
2369
|
+
count: Math.max(1, readCount(record.count) ?? boxes.length ?? 1)
|
|
2370
|
+
};
|
|
2371
|
+
const size = readCount(record.size);
|
|
2372
|
+
if (size !== void 0) finding.size = size;
|
|
2373
|
+
const measure = readCount(record.measure);
|
|
2374
|
+
if (measure !== void 0) finding.measure = measure;
|
|
2375
|
+
return [finding];
|
|
2376
|
+
});
|
|
2377
|
+
}
|
|
2378
|
+
function readPages(raw) {
|
|
2379
|
+
if (!Array.isArray(raw)) return [];
|
|
2380
|
+
return raw.flatMap((value) => typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 999 ? [value] : []);
|
|
2381
|
+
}
|
|
2382
|
+
function sanitizeSelfCheck(raw) {
|
|
2383
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
2384
|
+
const record = raw;
|
|
2385
|
+
const at = record.at;
|
|
2386
|
+
if (typeof at !== "number" || !Number.isFinite(at)) return null;
|
|
2387
|
+
return {
|
|
2388
|
+
at,
|
|
2389
|
+
findings: sanitizeFindings(record.findings),
|
|
2390
|
+
unread: readPages(record.unread)
|
|
2391
|
+
};
|
|
2392
|
+
}
|
|
2393
|
+
//#endregion
|
|
1611
2394
|
//#region src/app/lemonriver/board/model/agent-tasks.ts
|
|
1612
2395
|
const PAGE_ACTIONS = [
|
|
1613
2396
|
"rewrite",
|
|
@@ -1656,6 +2439,13 @@ function sanitizeTask(raw) {
|
|
|
1656
2439
|
title: typeof task.title === "string" ? task.title.slice(0, 120) : ""
|
|
1657
2440
|
};
|
|
1658
2441
|
}
|
|
2442
|
+
case "self-check": {
|
|
2443
|
+
const findings = sanitizeFindings(task.findings).slice(0, 40);
|
|
2444
|
+
return findings.length > 0 ? {
|
|
2445
|
+
kind: "self-check",
|
|
2446
|
+
findings
|
|
2447
|
+
} : null;
|
|
2448
|
+
}
|
|
1659
2449
|
default: return null;
|
|
1660
2450
|
}
|
|
1661
2451
|
}
|
|
@@ -1667,6 +2457,22 @@ const ACTION_LINE = {
|
|
|
1667
2457
|
chart: "Turn the main numbers on this page into a chart. Keep the title and the point it makes.",
|
|
1668
2458
|
expand: "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."
|
|
1669
2459
|
};
|
|
2460
|
+
/** The problems a finished deck's self-check found, one line each. */
|
|
2461
|
+
function selfCheckPrompt({ slideId, findings, lang }) {
|
|
2462
|
+
const lines = findings.map((finding) => {
|
|
2463
|
+
const box = finding.boxes[0];
|
|
2464
|
+
const at = box ? ` at (${Math.round(box.x)}, ${Math.round(box.y)})` : "";
|
|
2465
|
+
const times = finding.count > 1 ? ` (${finding.count} places)` : "";
|
|
2466
|
+
return `- page ${finding.page}: ${finding.detail}${times}${at}`;
|
|
2467
|
+
});
|
|
2468
|
+
return [
|
|
2469
|
+
`A finished deck at slides/${slideId}/index.tsx was checked automatically and these problems were found:`,
|
|
2470
|
+
...lines,
|
|
2471
|
+
"Fix only what is listed above, and only on those pages. Change nothing else, and do not add or remove pages.",
|
|
2472
|
+
LANGUAGE(lang),
|
|
2473
|
+
DONE
|
|
2474
|
+
].join("\n");
|
|
2475
|
+
}
|
|
1670
2476
|
/** The message for one page. A note action without a note has nothing to say. */
|
|
1671
2477
|
function pageTaskPrompt({ slideId, page, action, lang }) {
|
|
1672
2478
|
const memo = page.memo?.trim() ?? "";
|
|
@@ -2472,6 +3278,26 @@ async function startApply(ctx, slideId, task = { kind: "changes" }, lang = "en")
|
|
|
2472
3278
|
prompt
|
|
2473
3279
|
};
|
|
2474
3280
|
}
|
|
3281
|
+
if (task.kind === "self-check") {
|
|
3282
|
+
if (task.findings.length === 0) return {
|
|
3283
|
+
ok: false,
|
|
3284
|
+
status: 409,
|
|
3285
|
+
error: "nothing to fix"
|
|
3286
|
+
};
|
|
3287
|
+
const prompt = selfCheckPrompt({
|
|
3288
|
+
slideId,
|
|
3289
|
+
findings: task.findings,
|
|
3290
|
+
lang
|
|
3291
|
+
});
|
|
3292
|
+
await updateBoardFile(file, {
|
|
3293
|
+
...applying({}),
|
|
3294
|
+
chapterPlan: void 0
|
|
3295
|
+
});
|
|
3296
|
+
return {
|
|
3297
|
+
ok: true,
|
|
3298
|
+
prompt
|
|
3299
|
+
};
|
|
3300
|
+
}
|
|
2475
3301
|
const changes = sanitizeChanges(stored.changes);
|
|
2476
3302
|
const restructures = sanitizeRestructures(stored.restructures);
|
|
2477
3303
|
if (changes.length === 0 && restructures.length === 0) return {
|
|
@@ -2605,6 +3431,7 @@ async function collectApply(ctx, store, sessionId, userItemId) {
|
|
|
2605
3431
|
fields.changes = [];
|
|
2606
3432
|
fields.restructures = [];
|
|
2607
3433
|
} else if (task.kind === "page") pages = [.../* @__PURE__ */ new Set([task.page, ...created])].filter((key) => newKeys.includes(key));
|
|
3434
|
+
else if (task.kind === "self-check") pages = [...new Set(task.findings.map((finding) => finding.page))].map((n) => newKeys[n - 1]).filter((key) => typeof key === "string");
|
|
2608
3435
|
else {
|
|
2609
3436
|
pages = created;
|
|
2610
3437
|
const plan = sanitizeChapterPlan(stored.chapterPlan);
|
|
@@ -3009,7 +3836,8 @@ function registerBoardRoutes(server, ctx) {
|
|
|
3009
3836
|
applying: sanitizeApplying(raw.applying),
|
|
3010
3837
|
lastApply: sanitizeLastApply(raw.lastApply),
|
|
3011
3838
|
chapterPlan: sanitizeChapterPlan(raw.chapterPlan),
|
|
3012
|
-
targetMinutes: sanitizeTargetMinutes(raw.targetMinutes)
|
|
3839
|
+
targetMinutes: sanitizeTargetMinutes(raw.targetMinutes),
|
|
3840
|
+
template: sanitizeAppliedTemplate(raw.template)
|
|
3013
3841
|
});
|
|
3014
3842
|
}
|
|
3015
3843
|
if (method === "PUT" && url.pathname === "/") {
|
|
@@ -3041,6 +3869,26 @@ function registerBoardRoutes(server, ctx) {
|
|
|
3041
3869
|
await updateBoardFile(boardStructurePath(ctx.userCwd, slideId), { targetMinutes: sanitizeTargetMinutes(body?.targetMinutes) });
|
|
3042
3870
|
return json(res, 200, { ok: true });
|
|
3043
3871
|
}
|
|
3872
|
+
if (method === "GET" && url.pathname === "/self-check") {
|
|
3873
|
+
const slideId = url.searchParams.get("slideId") ?? "";
|
|
3874
|
+
if (!resolveSlideEntryPath(ctx, slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
3875
|
+
const stored = await readBoardFile(boardStructurePath(ctx.userCwd, slideId));
|
|
3876
|
+
return json(res, 200, { selfCheck: sanitizeSelfCheck(stored.selfCheck) });
|
|
3877
|
+
}
|
|
3878
|
+
if (method === "PUT" && url.pathname === "/self-check") {
|
|
3879
|
+
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
3880
|
+
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3881
|
+
const body = await readBody(req);
|
|
3882
|
+
const slideId = typeof body?.slideId === "string" ? body.slideId : "";
|
|
3883
|
+
if (!resolveSlideEntryPath(ctx, slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
3884
|
+
const selfCheck = sanitizeSelfCheck(body?.selfCheck);
|
|
3885
|
+
if (!selfCheck) return json(res, 400, { error: "invalid selfCheck" });
|
|
3886
|
+
await updateBoardFile(boardStructurePath(ctx.userCwd, slideId), { selfCheck });
|
|
3887
|
+
return json(res, 200, {
|
|
3888
|
+
ok: true,
|
|
3889
|
+
selfCheck
|
|
3890
|
+
});
|
|
3891
|
+
}
|
|
3044
3892
|
if (method === "GET" && url.pathname === "/cards") {
|
|
3045
3893
|
const slideId = url.searchParams.get("slideId") ?? "";
|
|
3046
3894
|
if (!resolveSlideEntryPath(ctx, slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
@@ -3252,19 +4100,212 @@ async function readDeckTimes(slidesRoot) {
|
|
|
3252
4100
|
}
|
|
3253
4101
|
return decks;
|
|
3254
4102
|
}
|
|
4103
|
+
async function deckFacts(ctx, slideId) {
|
|
4104
|
+
const stored = await readBoardFile(boardStructurePath(ctx.userCwd, slideId));
|
|
4105
|
+
const applied = sanitizeAppliedTemplate(stored.template);
|
|
4106
|
+
const template = applied?.id ?? null;
|
|
4107
|
+
const outline = sanitizeOutline(stored.outline);
|
|
4108
|
+
if (outline?.occasion) return {
|
|
4109
|
+
occasion: outline.occasion,
|
|
4110
|
+
template
|
|
4111
|
+
};
|
|
4112
|
+
if (!applied) return {
|
|
4113
|
+
occasion: null,
|
|
4114
|
+
template
|
|
4115
|
+
};
|
|
4116
|
+
const root = resolveTemplatesRoot({
|
|
4117
|
+
coreRoot: corePackageRoot(),
|
|
4118
|
+
cwd: ctx.userCwd
|
|
4119
|
+
});
|
|
4120
|
+
if (!root) return {
|
|
4121
|
+
occasion: null,
|
|
4122
|
+
template
|
|
4123
|
+
};
|
|
4124
|
+
try {
|
|
4125
|
+
const read = sanitizeTemplate(JSON.parse(await fs.readFile(path.join(root, applied.id, "template.json"), "utf8")), applied.id);
|
|
4126
|
+
return {
|
|
4127
|
+
occasion: read.ok ? read.template.occasions[0] ?? null : null,
|
|
4128
|
+
template
|
|
4129
|
+
};
|
|
4130
|
+
} catch {
|
|
4131
|
+
return {
|
|
4132
|
+
occasion: null,
|
|
4133
|
+
template
|
|
4134
|
+
};
|
|
4135
|
+
}
|
|
4136
|
+
}
|
|
3255
4137
|
function registerDeckRoutes(server, ctx) {
|
|
3256
4138
|
server.middlewares.use("/__lr/decks", async (req, res, next) => {
|
|
3257
4139
|
const url = new URL(req.url ?? "/", "http://local");
|
|
3258
4140
|
if ((req.method ?? "GET") !== "GET" || url.pathname !== "/") return next();
|
|
3259
4141
|
try {
|
|
3260
|
-
|
|
4142
|
+
const times = await readDeckTimes(ctx.slidesRoot);
|
|
4143
|
+
const decks = {};
|
|
4144
|
+
for (const [id, info] of Object.entries(times)) decks[id] = {
|
|
4145
|
+
modifiedAt: info.modifiedAt,
|
|
4146
|
+
...await deckFacts(ctx, id)
|
|
4147
|
+
};
|
|
4148
|
+
json(res, 200, { decks });
|
|
3261
4149
|
} catch (err) {
|
|
3262
4150
|
json(res, 500, { error: String(err.message ?? err) });
|
|
3263
4151
|
}
|
|
3264
4152
|
});
|
|
3265
4153
|
}
|
|
3266
4154
|
//#endregion
|
|
4155
|
+
//#region src/lemonriver/templates/apply.ts
|
|
4156
|
+
const SHARED_DOCS = [
|
|
4157
|
+
"讲法.md",
|
|
4158
|
+
"中文排版.md",
|
|
4159
|
+
"质量清单.md",
|
|
4160
|
+
"插图画法.md"
|
|
4161
|
+
];
|
|
4162
|
+
/** A kit's tests describe the template; the deck that copies it has no one to run them. */
|
|
4163
|
+
const isTestFile = (file) => /\.test\.tsx?$/.test(file);
|
|
4164
|
+
const docList = (slideId) => [`GUIDE.md`, ...SHARED_DOCS].map((doc) => `- slides/${slideId}/kit/docs/${doc}`).join("\n");
|
|
4165
|
+
function promptFor(input) {
|
|
4166
|
+
const { slideId, template, design, flags, knobs } = input;
|
|
4167
|
+
const skeletons = template.skeletons.map((id) => SKELETONS.find((skeleton) => skeleton.id === id)).filter((skeleton) => skeleton !== void 0).map((skeleton) => `- ${skeleton.id} (${skeleton.name}): ${skeleton.component}`).join("\n");
|
|
4168
|
+
const knobLine = `formal=${knobs.formal ?? .5}, bright=${knobs.bright ?? .5}, motion=${knobs.motion ?? .5}`;
|
|
4169
|
+
const flagEntries = Object.entries(flags);
|
|
4170
|
+
const flagLine = flagEntries.length > 0 ? `\nThe knobs also switch: ${flagEntries.map(([key, value]) => `${key}=${value}`).join(", ")}.` : "";
|
|
4171
|
+
return `This deck uses the "${template.name}" template (${template.id}). Everything below is relative to slides/${slideId}/index.tsx.
|
|
4172
|
+
|
|
4173
|
+
Before writing any page, read:
|
|
4174
|
+
${docList(slideId)}
|
|
4175
|
+
|
|
4176
|
+
The shell, the fixed parts and the skeleton components live in slides/${slideId}/kit/. Import them from './kit/...' and do not reach outside this deck folder. The template describes the borrowed object as: ${template.object}. The accent colour stands for: ${template.accent}.
|
|
4177
|
+
|
|
4178
|
+
Replace any existing \`design\` export with this constant. It is the template's design with the knobs (${knobLine}) already applied; do not hand-edit its colours:
|
|
4179
|
+
${designSnippet(design)}${flagLine}
|
|
4180
|
+
|
|
4181
|
+
Pick one skeleton per page from slides/${slideId}/kit/skeletons.tsx:
|
|
4182
|
+
${skeletons}
|
|
4183
|
+
|
|
4184
|
+
When you are done, reply with one short sentence saying which skeletons you used.`;
|
|
4185
|
+
}
|
|
4186
|
+
/**
|
|
4187
|
+
* Copies a template into one deck's own folder. Everything the deck needs to
|
|
4188
|
+
* keep its style later — kit, fonts, guides — travels with it, so a later
|
|
4189
|
+
* template update never rewrites a finished deck.
|
|
4190
|
+
*/
|
|
4191
|
+
async function applyTemplateToDeck(ctx, slideId, templateId, knobs) {
|
|
4192
|
+
const entry = resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
4193
|
+
if (!entry) return {
|
|
4194
|
+
ok: false,
|
|
4195
|
+
status: 400,
|
|
4196
|
+
error: "invalid slideId"
|
|
4197
|
+
};
|
|
4198
|
+
try {
|
|
4199
|
+
await fs.access(entry);
|
|
4200
|
+
} catch {
|
|
4201
|
+
return {
|
|
4202
|
+
ok: false,
|
|
4203
|
+
status: 404,
|
|
4204
|
+
error: "slide not found"
|
|
4205
|
+
};
|
|
4206
|
+
}
|
|
4207
|
+
const root = resolveTemplatesRoot({
|
|
4208
|
+
coreRoot: corePackageRoot(),
|
|
4209
|
+
cwd: ctx.userCwd
|
|
4210
|
+
});
|
|
4211
|
+
if (!root) return {
|
|
4212
|
+
ok: false,
|
|
4213
|
+
status: 404,
|
|
4214
|
+
error: "no template library"
|
|
4215
|
+
};
|
|
4216
|
+
const found = (await loadTemplates(root, () => {})).find((candidate) => candidate.template.id === templateId);
|
|
4217
|
+
if (!found) return {
|
|
4218
|
+
ok: false,
|
|
4219
|
+
status: 404,
|
|
4220
|
+
error: "template not found"
|
|
4221
|
+
};
|
|
4222
|
+
const missing = [];
|
|
4223
|
+
for (const doc of SHARED_DOCS) try {
|
|
4224
|
+
await fs.access(path.join(root, "_shared", doc));
|
|
4225
|
+
} catch {
|
|
4226
|
+
missing.push(`_shared/${doc}`);
|
|
4227
|
+
}
|
|
4228
|
+
try {
|
|
4229
|
+
await fs.access(path.join(found.dir, "GUIDE.md"));
|
|
4230
|
+
} catch {
|
|
4231
|
+
missing.push(`${templateId}/GUIDE.md`);
|
|
4232
|
+
}
|
|
4233
|
+
try {
|
|
4234
|
+
await fs.access(path.join(found.dir, "kit"));
|
|
4235
|
+
} catch {
|
|
4236
|
+
missing.push(`${templateId}/kit`);
|
|
4237
|
+
}
|
|
4238
|
+
if (missing.length > 0) return {
|
|
4239
|
+
ok: false,
|
|
4240
|
+
status: 500,
|
|
4241
|
+
error: `template library is incomplete: ${missing.join(", ")}`
|
|
4242
|
+
};
|
|
4243
|
+
const deckDir = path.join(ctx.slidesRoot, slideId);
|
|
4244
|
+
const destKit = path.join(deckDir, "kit");
|
|
4245
|
+
await fs.rm(destKit, {
|
|
4246
|
+
recursive: true,
|
|
4247
|
+
force: true
|
|
4248
|
+
});
|
|
4249
|
+
await fs.cp(path.join(found.dir, "kit"), destKit, {
|
|
4250
|
+
recursive: true,
|
|
4251
|
+
filter: (src) => !isTestFile(src)
|
|
4252
|
+
});
|
|
4253
|
+
const docsDir = path.join(destKit, "docs");
|
|
4254
|
+
await fs.mkdir(docsDir, { recursive: true });
|
|
4255
|
+
await fs.copyFile(path.join(found.dir, "GUIDE.md"), path.join(docsDir, "GUIDE.md"));
|
|
4256
|
+
for (const doc of SHARED_DOCS) await fs.copyFile(path.join(root, "_shared", doc), path.join(docsDir, doc));
|
|
4257
|
+
const values = sanitizeKnobValues(knobs);
|
|
4258
|
+
await updateBoardFile(boardStructurePath(ctx.userCwd, slideId), { template: {
|
|
4259
|
+
id: found.template.id,
|
|
4260
|
+
knobs: values,
|
|
4261
|
+
appliedAt: Date.now()
|
|
4262
|
+
} });
|
|
4263
|
+
return {
|
|
4264
|
+
ok: true,
|
|
4265
|
+
prompt: promptFor({
|
|
4266
|
+
slideId,
|
|
4267
|
+
template: found.template,
|
|
4268
|
+
design: applyKnobs(found.template.design, found.template.knobs, values),
|
|
4269
|
+
flags: knobFlags(found.template.knobs, values),
|
|
4270
|
+
knobs: values
|
|
4271
|
+
}),
|
|
4272
|
+
skeletons: found.template.skeletons
|
|
4273
|
+
};
|
|
4274
|
+
}
|
|
4275
|
+
//#endregion
|
|
4276
|
+
//#region src/lemonriver/outline/occasion.ts
|
|
4277
|
+
const text$1 = (value) => typeof value === "string" ? value.trim() : "";
|
|
4278
|
+
/**
|
|
4279
|
+
* The two lines an outline prompt gains when the user named an occasion: who
|
|
4280
|
+
* it is for, and the page order that occasion usually wants.
|
|
4281
|
+
*/
|
|
4282
|
+
async function occasionLines(root, occasion, audience) {
|
|
4283
|
+
const id = text$1(occasion);
|
|
4284
|
+
if (!root || !id) return [];
|
|
4285
|
+
let parsed;
|
|
4286
|
+
try {
|
|
4287
|
+
parsed = JSON.parse(await fs.readFile(path.join(root, "_shared", "场合.json"), "utf8"));
|
|
4288
|
+
} catch {
|
|
4289
|
+
return [];
|
|
4290
|
+
}
|
|
4291
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return [];
|
|
4292
|
+
const entry = parsed[id];
|
|
4293
|
+
if (!entry || typeof entry !== "object") return [];
|
|
4294
|
+
const name = text$1(entry.name);
|
|
4295
|
+
if (!name) return [];
|
|
4296
|
+
const lines = [];
|
|
4297
|
+
const who = text$1(audience) || text$1(entry.audience);
|
|
4298
|
+
if (who) lines.push(`The deck is for: ${who}.`);
|
|
4299
|
+
const order = Array.isArray(entry.order) ? entry.order.filter((kind) => typeof kind === "string") : [];
|
|
4300
|
+
if (order.length > 0) lines.push(`For a ${name} deck, this page order is recommended: ${order.join(" → ")}.`);
|
|
4301
|
+
return lines;
|
|
4302
|
+
}
|
|
4303
|
+
//#endregion
|
|
3267
4304
|
//#region src/lemonriver/outline/routes.ts
|
|
4305
|
+
const TEMPLATE_ID_RE = /^[a-z0-9][a-z0-9-]*$/;
|
|
4306
|
+
function isTemplateId(value) {
|
|
4307
|
+
return typeof value === "string" && TEMPLATE_ID_RE.test(value);
|
|
4308
|
+
}
|
|
3268
4309
|
function startOutline(input, now) {
|
|
3269
4310
|
const brief = typeof input.brief === "string" ? input.brief.trim().slice(0, MAX_BRIEF) : "";
|
|
3270
4311
|
const files = Array.isArray(input.files) ? input.files.filter((file) => typeof file === "string" && file.trim() !== "") : [];
|
|
@@ -3281,7 +4322,12 @@ function startOutline(input, now) {
|
|
|
3281
4322
|
dispatched: null,
|
|
3282
4323
|
lastTurn: null,
|
|
3283
4324
|
prefs: sanitizePrefs(input.prefs),
|
|
3284
|
-
decisions: []
|
|
4325
|
+
decisions: [],
|
|
4326
|
+
occasion: isOccasionId(input.occasion) ? input.occasion : null,
|
|
4327
|
+
audience: typeof input.audience === "string" ? input.audience.trim().slice(0, 40) : "",
|
|
4328
|
+
templatePrompt: "",
|
|
4329
|
+
templateSkeletons: [],
|
|
4330
|
+
templateId: isTemplateId(input.template) ? input.template : null
|
|
3285
4331
|
};
|
|
3286
4332
|
}
|
|
3287
4333
|
/** Only a ready outline may be edited: while drafting the agent owns the list. */
|
|
@@ -3335,7 +4381,20 @@ function undoBuild(outline, now) {
|
|
|
3335
4381
|
stage: "pages",
|
|
3336
4382
|
dispatched: null,
|
|
3337
4383
|
lastTurn: null,
|
|
3338
|
-
updatedAt: now
|
|
4384
|
+
updatedAt: now,
|
|
4385
|
+
templatePrompt: "",
|
|
4386
|
+
templateSkeletons: []
|
|
4387
|
+
};
|
|
4388
|
+
}
|
|
4389
|
+
/** Only an id shaped like a template folder is passed on; the library re-checks it. */
|
|
4390
|
+
function readTemplateChoice(raw) {
|
|
4391
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
4392
|
+
const record = raw;
|
|
4393
|
+
const id = record.id;
|
|
4394
|
+
if (!isTemplateId(id)) return null;
|
|
4395
|
+
return {
|
|
4396
|
+
id,
|
|
4397
|
+
knobs: record.knobs
|
|
3339
4398
|
};
|
|
3340
4399
|
}
|
|
3341
4400
|
async function readOutline(ctx, slideId) {
|
|
@@ -3372,8 +4431,16 @@ function registerOutlineRoutes(server, ctx) {
|
|
|
3372
4431
|
const body = await readBody(req);
|
|
3373
4432
|
const slideId = typeof body.slideId === "string" ? body.slideId : "";
|
|
3374
4433
|
if (!resolveSlideEntryPath(ctx, slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
3375
|
-
|
|
3376
|
-
|
|
4434
|
+
const outline = startOutline(body, Date.now());
|
|
4435
|
+
await updateBoardFile(boardStructurePath(ctx.userCwd, slideId), { outline });
|
|
4436
|
+
const lines = await occasionLines(resolveTemplatesRoot({
|
|
4437
|
+
coreRoot: corePackageRoot(),
|
|
4438
|
+
cwd: ctx.userCwd
|
|
4439
|
+
}), outline.occasion, outline.audience);
|
|
4440
|
+
return json(res, 200, {
|
|
4441
|
+
ok: true,
|
|
4442
|
+
prompt: lines.join("\n")
|
|
4443
|
+
});
|
|
3377
4444
|
}
|
|
3378
4445
|
if (method === "POST" && url.pathname === "/build") {
|
|
3379
4446
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
@@ -3390,8 +4457,21 @@ function registerOutlineRoutes(server, ctx) {
|
|
|
3390
4457
|
prefs: sanitizePrefs(body.prefs)
|
|
3391
4458
|
};
|
|
3392
4459
|
const buildBackup = await readSlideSource(entry) ?? "";
|
|
4460
|
+
const choice = readTemplateChoice(body.template);
|
|
4461
|
+
let templatePrompt = "";
|
|
4462
|
+
let templateSkeletons = [];
|
|
4463
|
+
if (choice) {
|
|
4464
|
+
const applied = await applyTemplateToDeck(ctx, slideId, choice.id, choice.knobs);
|
|
4465
|
+
if (!applied.ok) return json(res, applied.status, { error: applied.error });
|
|
4466
|
+
templatePrompt = applied.prompt;
|
|
4467
|
+
templateSkeletons = applied.skeletons;
|
|
4468
|
+
}
|
|
3393
4469
|
await updateBoardFile(file, {
|
|
3394
|
-
outline:
|
|
4470
|
+
outline: {
|
|
4471
|
+
...beginBuild(withPrefs, Date.now()),
|
|
4472
|
+
templatePrompt,
|
|
4473
|
+
templateSkeletons
|
|
4474
|
+
},
|
|
3395
4475
|
buildBackup
|
|
3396
4476
|
});
|
|
3397
4477
|
return json(res, 200, { ok: true });
|
|
@@ -3409,8 +4489,17 @@ function registerOutlineRoutes(server, ctx) {
|
|
|
3409
4489
|
now: Date.now()
|
|
3410
4490
|
});
|
|
3411
4491
|
if (!claimed) return json(res, 409, { error: "no step to send" });
|
|
3412
|
-
|
|
3413
|
-
|
|
4492
|
+
let next = claimed.outline;
|
|
4493
|
+
if (claimed.step.kind === "page" && claimed.step.n === 1 && next.templateId && !next.templatePrompt) {
|
|
4494
|
+
const applied = await applyTemplateToDeck(ctx, slideId, next.templateId, {});
|
|
4495
|
+
if (applied.ok) next = {
|
|
4496
|
+
...next,
|
|
4497
|
+
templatePrompt: applied.prompt,
|
|
4498
|
+
templateSkeletons: applied.skeletons
|
|
4499
|
+
};
|
|
4500
|
+
}
|
|
4501
|
+
await updateBoardFile(boardStructurePath(ctx.userCwd, slideId), { outline: next });
|
|
4502
|
+
const prompt = claimed.step.kind === "review" ? reviewPrompt(next, slideId) : pagePrompt(next, slideId, claimed.step.n);
|
|
3414
4503
|
return json(res, 200, {
|
|
3415
4504
|
prompt,
|
|
3416
4505
|
step: claimed.step
|
|
@@ -3446,9 +4535,14 @@ function registerOutlineRoutes(server, ctx) {
|
|
|
3446
4535
|
const next = undoBuild(outline, Date.now());
|
|
3447
4536
|
if (!next) return json(res, 409, { error: "nothing to undo" });
|
|
3448
4537
|
await fs.writeFile(entry, backup, "utf8");
|
|
4538
|
+
if (sanitizeAppliedTemplate(stored.template)) await fs.rm(path.join(ctx.slidesRoot, slideId, "kit"), {
|
|
4539
|
+
recursive: true,
|
|
4540
|
+
force: true
|
|
4541
|
+
});
|
|
3449
4542
|
await updateBoardFile(file, {
|
|
3450
4543
|
outline: next,
|
|
3451
|
-
buildBackup: void 0
|
|
4544
|
+
buildBackup: void 0,
|
|
4545
|
+
template: void 0
|
|
3452
4546
|
});
|
|
3453
4547
|
return json(res, 200, { ok: true });
|
|
3454
4548
|
}
|
|
@@ -3459,6 +4553,43 @@ function registerOutlineRoutes(server, ctx) {
|
|
|
3459
4553
|
});
|
|
3460
4554
|
}
|
|
3461
4555
|
//#endregion
|
|
4556
|
+
//#region src/lemonriver/templates/routes.ts
|
|
4557
|
+
const GUIDE_PATH = /^\/([A-Za-z0-9._-]+)\/guide$/;
|
|
4558
|
+
function text(res, status, body, type) {
|
|
4559
|
+
res.statusCode = status;
|
|
4560
|
+
res.setHeader("content-type", `${type}; charset=utf-8`);
|
|
4561
|
+
res.end(body);
|
|
4562
|
+
}
|
|
4563
|
+
function createTemplatesMiddleware(deps) {
|
|
4564
|
+
return async (req, res, next) => {
|
|
4565
|
+
if ((req.method ?? "GET") !== "GET") return next();
|
|
4566
|
+
const url = new URL(req.url ?? "/", "http://local");
|
|
4567
|
+
try {
|
|
4568
|
+
const root = resolveTemplatesRoot({
|
|
4569
|
+
coreRoot: deps.coreRoot,
|
|
4570
|
+
cwd: deps.cwd,
|
|
4571
|
+
env: deps.env
|
|
4572
|
+
});
|
|
4573
|
+
if (url.pathname === "/" || url.pathname === "") return json(res, 200, await readTemplateListing(root));
|
|
4574
|
+
const guide = GUIDE_PATH.exec(url.pathname);
|
|
4575
|
+
if (guide) {
|
|
4576
|
+
const body = await readTemplateGuide(root, guide[1]);
|
|
4577
|
+
if (body === null) return json(res, 404, { error: "template not found" });
|
|
4578
|
+
return text(res, 200, body, "text/markdown");
|
|
4579
|
+
}
|
|
4580
|
+
return next();
|
|
4581
|
+
} catch (err) {
|
|
4582
|
+
return json(res, 500, { error: String(err.message ?? err) });
|
|
4583
|
+
}
|
|
4584
|
+
};
|
|
4585
|
+
}
|
|
4586
|
+
function registerTemplateRoutes(server, ctx) {
|
|
4587
|
+
server.middlewares.use("/__lr/templates", createTemplatesMiddleware({
|
|
4588
|
+
coreRoot: corePackageRoot(),
|
|
4589
|
+
cwd: ctx.userCwd
|
|
4590
|
+
}));
|
|
4591
|
+
}
|
|
4592
|
+
//#endregion
|
|
3462
4593
|
//#region src/vite/recent-writes.ts
|
|
3463
4594
|
const recentWrites = /* @__PURE__ */ new Map();
|
|
3464
4595
|
/** Record that `file` was just written by us. */
|
|
@@ -4023,6 +5154,7 @@ function registerLemonRiverRoutes(server, ctx) {
|
|
|
4023
5154
|
registerTrashRoutes(server, ctx);
|
|
4024
5155
|
registerDeckRoutes(server, ctx);
|
|
4025
5156
|
registerOutlineRoutes(server, ctx);
|
|
5157
|
+
registerTemplateRoutes(server, ctx);
|
|
4026
5158
|
registerAgentProviderRoutes(server, ctx);
|
|
4027
5159
|
}
|
|
4028
5160
|
//#endregion
|
|
@@ -8377,6 +9509,10 @@ async function createViteConfig(opts) {
|
|
|
8377
9509
|
userCwd,
|
|
8378
9510
|
config
|
|
8379
9511
|
}),
|
|
9512
|
+
templatesPlugin({
|
|
9513
|
+
coreRoot: PKG_ROOT,
|
|
9514
|
+
userCwd
|
|
9515
|
+
}),
|
|
8380
9516
|
designPlugin({ userCwd }),
|
|
8381
9517
|
apiPlugin({
|
|
8382
9518
|
userCwd,
|