@gui-chat-plugin/sequence 0.1.0 → 0.3.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.
@@ -0,0 +1,706 @@
1
+ import { createSerialLock as e } from "gui-chat-protocol";
2
+ //#region src/core/slideHtml.ts
3
+ var t = 1280, n = 720, r = 6e4, i = "https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.3.3/dist/index.global.js", a = "sha384-2ql948lIdLcGEE0/qxNiudyTjgauA3RDJERu5xW75kFCvSl5a9odyQYCb6tEjnmB", o = [
4
+ "fade-in",
5
+ "fade-up",
6
+ "fade-down",
7
+ "slide-in-left",
8
+ "slide-in-right",
9
+ "zoom-in",
10
+ "pop",
11
+ "draw"
12
+ ], s = "\n@theme {\n --animate-fade-in: fade-in 0.8s ease-out both;\n --animate-fade-up: fade-up 0.8s ease-out both;\n --animate-fade-down: fade-down 0.8s ease-out both;\n --animate-slide-in-left: slide-in-left 0.8s ease-out both;\n --animate-slide-in-right: slide-in-right 0.8s ease-out both;\n --animate-zoom-in: zoom-in 0.8s ease-out both;\n --animate-pop: pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;\n --animate-draw: draw 1.5s ease-in-out both;\n @keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }\n @keyframes fade-up { from { opacity: 0; transform: translateY(40px) } to { opacity: 1; transform: none } }\n @keyframes fade-down { from { opacity: 0; transform: translateY(-40px) } to { opacity: 1; transform: none } }\n @keyframes slide-in-left { from { opacity: 0; transform: translateX(-80px) } to { opacity: 1; transform: none } }\n @keyframes slide-in-right { from { opacity: 0; transform: translateX(80px) } to { opacity: 1; transform: none } }\n @keyframes zoom-in { from { opacity: 0; transform: scale(0.8) } to { opacity: 1; transform: none } }\n @keyframes pop { from { opacity: 0; transform: scale(0.5) } to { opacity: 1; transform: none } }\n @keyframes draw { from { stroke-dashoffset: var(--draw-length, 1000) } to { stroke-dashoffset: 0 } }\n}", c = "\n(() => {\n const show = () => document.documentElement.classList.add(\"ready\");\n const ready = () => requestAnimationFrame(() => requestAnimationFrame(show));\n const check = () =>\n document.querySelector(\"style:not([type]):not(#slide-base)\") ? ready() : setTimeout(check, 30);\n addEventListener(\"DOMContentLoaded\", check);\n setTimeout(show, 1000);\n addEventListener(\"click\", (event) => {\n if (event.target instanceof Element && event.target.closest(\"a, area\")) event.preventDefault();\n }, true);\n})();", l = [
13
+ "default-src 'none'",
14
+ `script-src 'sha256-MZyL5rvWmIVxl3WUuDU5BqzPJGR2/fV/YPh01MNyUoM=' ${i}`,
15
+ "style-src 'unsafe-inline' https://fonts.googleapis.com",
16
+ "font-src https://fonts.gstatic.com data:",
17
+ "img-src data: blob:",
18
+ "media-src data: blob:",
19
+ "connect-src 'none'",
20
+ "form-action 'none'",
21
+ "base-uri 'none'"
22
+ ].join("; "), u = (e) => e.replace(/<(?=(meta|base|link)\b)/gi, "&lt;").replace(/shadowroot/gi, "data-no-shadow-root"), d = `
23
+ html, body { margin: 0; width: ${t}px; height: 720px; overflow: hidden; }
24
+ html:not(.ready) body { visibility: hidden; }
25
+ html:not(.ready) body * { animation-play-state: paused !important; }`;
26
+ function f(e) {
27
+ return `<!doctype html>
28
+ <html lang="en">
29
+ <head>
30
+ <meta charset="utf-8">
31
+ <meta http-equiv="Content-Security-Policy" content="${l}">
32
+ <style id="slide-base">${d}</style>
33
+ <script>${c}<\/script>
34
+ <script defer src="${i}" integrity="${a}" crossorigin="anonymous"><\/script>
35
+ <style type="text/tailwindcss">${s}</style>
36
+ </head>
37
+ <body class="bg-white text-slate-900 font-sans antialiased">
38
+ ${u(e)}
39
+ </body>
40
+ </html>`;
41
+ }
42
+ //#endregion
43
+ //#region src/core/definitions.ts
44
+ var p = "presentSlide", m = "defineStoryboard", h = "presentPanel", ee = 4, te = 12, ne = 3, g = /^[0-9a-f]{12}$/, _ = (e) => typeof e == "object" && !!e && !Array.isArray(e), v = (e) => typeof e == "string" ? e.trim() : "", y = (e, t) => e.toLowerCase() === t.toLowerCase(), b = (e) => typeof e == "number" && Number.isInteger(e), x = "presentSlide needs slide and totalSlides (whole numbers, slide from 1 to totalSlides), and either an imagePrompt or html", re = `presentSlide's html is too long: keep a slide under ${r} characters`;
45
+ function ie(e) {
46
+ let { slide: t, totalSlides: n, title: r, imagePrompt: i } = e, a = v(e.html);
47
+ return !b(t) || !b(n) || t < 1 || n < t || !a && !v(i) ? x : a.length > 6e4 ? re : {
48
+ slide: t,
49
+ totalSlides: n,
50
+ title: typeof r == "string" ? r : "",
51
+ imagePrompt: a ? "" : String(i),
52
+ ...a && { html: a },
53
+ mode: e.mode === "steps" ? "steps" : "presentation"
54
+ };
55
+ }
56
+ var S = ({ slide: e, totalSlides: t, title: n, mode: r }) => {
57
+ let i = n ? ` ("${n}")` : "";
58
+ if (r === "steps") {
59
+ let n = `Step ${e} of ${t}${i}`;
60
+ return e < t ? `${n} is now on the screen. Explain what to do in this step, then stop and wait while the user does it. When they say they're ready (next, done, go on), call presentSlide for step ${e + 1} with mode "steps". If they ask to go back or to see a step again, call presentSlide for that step.` : `${n}, the last one, is now on the screen. Explain what to do, then wrap up the guide once the user is done.`;
61
+ }
62
+ let a = `Slide ${e} of ${t}${i}`;
63
+ return e < t ? `${a} is now on the screen. Explain it, then, in this same reply and without waiting for the user, call presentSlide for slide ${e + 1}. If the user has asked you to stop, or asked something else, since the slideshow began, answer them instead of going on.` : `${a}, the last one, is now on the screen. Explain it, then wrap up the slideshow.`;
64
+ }, ae = [`For an HTML slide, instead of imagePrompt: the slide's content, the inside of <body> only (no <html>, <head> or <body> tags), for a ${t}x720 px canvas, which it should fill (start with a <div class="w-full h-full ...">). Style it with Tailwind CSS v4 classes, which are loaded for you: a clear layout, a large title (text-6xl or so), few words in large type, a color scheme, and inline SVG or emoji for icons and diagrams. There is no network: no <img> from URLs, no links to follow.`, `Animate it so it builds up as you explain it: these classes are defined, ${o.map((e) => `animate-${e}`).join(", ")} (an SVG path drawing itself: set stroke-dasharray and --draw-length to its length), plus Tailwind's animate-pulse, animate-bounce, animate-spin and animate-ping, and transitions. Stagger the parts with a delay, [animation-delay:600ms], so they appear one after another over a few seconds. A <style> with your own @keyframes is allowed too (and Google Fonts by @import). Scripts don't run: animate with CSS only.`].join(" "), C = "When the user asks for a slideshow (or to explain something with slides), plan four to six slides, then show them one at a time with presentSlide: slide 1 first, and each next slide only after you have explained the one on the screen. Go on to the last slide without asking whether to continue. A slide is a generated picture (imagePrompt) or a designed slide in HTML (html): use a picture for a scene, an object or a place, and HTML for words, numbers, lists, comparisons, timelines and diagrams, which a picture model draws badly; a slideshow can mix them. When the user wants to be shown how to do something they will do along with you (cooking, folding, fixing, an exercise), make it a step-by-step guide instead: mode \"steps\", one slide per step, and after each step wait for the user to say they are ready. Use generateImage for a single picture, not for slides.", w = {
65
+ type: "function",
66
+ name: p,
67
+ prompt: C,
68
+ description: "Show one slide of a slideshow, full screen: a picture generated from imagePrompt, or a slide written in HTML (html). Call it once per slide, in order, starting with slide 1. With mode \"steps\" it is a step-by-step guide: one slide per step, and the next step waits for the user.",
69
+ parameters: {
70
+ type: "object",
71
+ properties: {
72
+ slide: {
73
+ type: "integer",
74
+ description: "This slide's number, starting at 1."
75
+ },
76
+ totalSlides: {
77
+ type: "integer",
78
+ description: "How many slides the slideshow has. Keep it the same for every slide."
79
+ },
80
+ title: {
81
+ type: "string",
82
+ description: "The slide's title."
83
+ },
84
+ imagePrompt: {
85
+ type: "string",
86
+ description: "For a picture slide: the picture, a clear illustration of its point, with the title as its only large text. Be concrete. For a step, show the action: hands, tools and the object. Leave it out for an HTML slide."
87
+ },
88
+ html: {
89
+ type: "string",
90
+ description: ae
91
+ },
92
+ mode: {
93
+ type: "string",
94
+ enum: ["presentation", "steps"],
95
+ description: "\"presentation\" (the default) goes on by itself; \"steps\" is a how-to the user does along with you, and waits for them after each step. Keep it the same for every slide."
96
+ }
97
+ },
98
+ required: [
99
+ "slide",
100
+ "totalSlides",
101
+ "title"
102
+ ]
103
+ }
104
+ };
105
+ function T(e) {
106
+ let { totalPanels: t } = e;
107
+ if (!b(t) || t < 1 || t > 12) return "totalPanels must be a whole number from 1 to 12";
108
+ let n = [];
109
+ for (let t of Array.isArray(e.characters) ? e.characters : []) {
110
+ let e = _(t) ? v(t.name) : "", r = _(t) ? v(t.description) : "";
111
+ if (!e || !r) return "every character needs a name and a description";
112
+ if (n.some((t) => y(t.name, e))) return `two characters are named ${e}`;
113
+ n.push({
114
+ name: e,
115
+ description: r
116
+ });
117
+ }
118
+ return n.length < 1 || n.length > 4 ? "a storyboard has 1 to 4 characters" : {
119
+ title: v(e.title),
120
+ style: v(e.style),
121
+ totalPanels: t,
122
+ characters: n,
123
+ interactive: e.interactive === !0
124
+ };
125
+ }
126
+ var E = ({ title: e, id: t }) => `The cast of "${e}" is now on the screen. Introduce the characters briefly, then, in this same reply and without waiting for the user, call presentPanel for panel 1 with storyboardId "${t}". If the user has asked you to stop, or asked something else, answer them instead of going on.`, D = "presentPanel needs a storyboardId, a panel number (from 1) and an imagePrompt";
127
+ function O(e) {
128
+ let { panel: t } = e, n = v(e.storyboardId), r = v(e.imagePrompt);
129
+ if (!n || !r || !b(t) || t < 1) return null;
130
+ let i = (e) => (Array.isArray(e) ? e : []).map(v).filter(Boolean);
131
+ return {
132
+ storyboardId: n,
133
+ panel: t,
134
+ characters: i(e.characters),
135
+ imagePrompt: r,
136
+ caption: v(e.caption),
137
+ choices: i(e.choices).slice(0, 3)
138
+ };
139
+ }
140
+ function k(e, t, n) {
141
+ let { id: r, totalPanels: i } = e;
142
+ return n.length ? `Panel ${t} of ${i} is now on the screen, with the user's choices: ${n.map((e, t) => `${t + 1}. ${e}`).join(" ")} Tell this part of the story, then read the choices out and ask the user which one they pick, and wait for their answer. Then call presentPanel for panel ${t + 1} with storyboardId "${r}", going on the way they chose.` : t < i ? `Panel ${t} of ${i} is now on the screen. Tell this part of the story, then, in this same reply and without waiting for the user, call presentPanel for panel ${t + 1} with storyboardId "${r}". If the user has asked you to stop, or asked something else, since the story began, answer them instead of going on.` : `Panel ${t} of ${i}, the last one, is now on the screen. Tell this part of the story, then bring it to an end.`;
143
+ }
144
+ var A = "When the user asks for a story, a picture book, a comic or a storyboard, tell it in pictures: first call defineStoryboard with the art style and the characters who appear more than once (it draws a reference sheet for each, so they look the same in every panel), then show the panels one at a time with presentPanel, naming the characters in each, and tell each part of the story when its panel appears. Go on to the last panel without asking whether to continue, unless the story is interactive. When the user wants to decide what happens (an adventure, a story for a child who wants to choose), set interactive, and give two or three choices on two or three of the panels: the story waits there for the user's pick and goes on their way; it still ends at the last panel whichever way it goes. Use presentSlide to explain a topic, not to tell a story.", j = {
145
+ type: "function",
146
+ name: m,
147
+ prompt: A,
148
+ description: "Start a story told in pictures: set its art style and draw a reference sheet for each recurring character, shown as the cast. Then call presentPanel for each panel, in order.",
149
+ parameters: {
150
+ type: "object",
151
+ properties: {
152
+ title: {
153
+ type: "string",
154
+ description: "The story's title."
155
+ },
156
+ style: {
157
+ type: "string",
158
+ description: "The art style of every picture, in one sentence (for example: soft watercolor, children's picture book)."
159
+ },
160
+ totalPanels: {
161
+ type: "integer",
162
+ description: "How many panels the story has, at most 12; four to eight is usual."
163
+ },
164
+ interactive: {
165
+ type: "boolean",
166
+ description: "The user chooses what happens: panels can offer choices, and the story waits for the pick."
167
+ },
168
+ characters: {
169
+ type: "array",
170
+ description: "The characters who appear in more than one panel, 1 to 4.",
171
+ items: {
172
+ type: "object",
173
+ properties: {
174
+ name: {
175
+ type: "string",
176
+ description: "A short name, used in presentPanel."
177
+ },
178
+ description: {
179
+ type: "string",
180
+ description: "How they look: age, build, face, hair, clothes and colors. Be concrete: it is drawn once and reused in every panel."
181
+ }
182
+ },
183
+ required: ["name", "description"]
184
+ }
185
+ }
186
+ },
187
+ required: [
188
+ "title",
189
+ "style",
190
+ "totalPanels",
191
+ "characters"
192
+ ]
193
+ }
194
+ }, M = {
195
+ type: "function",
196
+ name: h,
197
+ description: "Show one panel of a storyboard made with defineStoryboard: a picture of the scene with its characters drawn as in their reference sheets, and a caption. Call it once per panel, in order, starting with panel 1.",
198
+ parameters: {
199
+ type: "object",
200
+ properties: {
201
+ storyboardId: {
202
+ type: "string",
203
+ description: "The storyboardId defineStoryboard returned."
204
+ },
205
+ panel: {
206
+ type: "integer",
207
+ description: "This panel's number, starting at 1."
208
+ },
209
+ characters: {
210
+ type: "array",
211
+ items: { type: "string" },
212
+ description: "The names (as in defineStoryboard) of the characters in this panel."
213
+ },
214
+ imagePrompt: {
215
+ type: "string",
216
+ description: "What the panel shows: the setting, what happens, expressions and the camera angle. Refer to characters by name; don't describe their looks again."
217
+ },
218
+ caption: {
219
+ type: "string",
220
+ description: "One short line shown under the picture."
221
+ },
222
+ choices: {
223
+ type: "array",
224
+ items: { type: "string" },
225
+ description: "Only in an interactive story, and not on the last panel: two or three short options for what happens next. The story waits for the user's pick."
226
+ }
227
+ },
228
+ required: [
229
+ "storyboardId",
230
+ "panel",
231
+ "characters",
232
+ "imagePrompt"
233
+ ]
234
+ }
235
+ }, N = (e) => typeof e == "object" && !!e && !Array.isArray(e), oe = (e) => N(e) && typeof e.message == "string";
236
+ function P(e) {
237
+ let t = N(e?.data) ? e.data : {};
238
+ return {
239
+ ...typeof t.imageData == "string" && { imageData: t.imageData },
240
+ ...typeof t.imagePath == "string" && { imagePath: t.imagePath }
241
+ };
242
+ }
243
+ function F(e) {
244
+ let t = N(e?.data) ? e.data : {};
245
+ return typeof t.html == "string" ? t.html : "";
246
+ }
247
+ var se = (e) => !!P(e).imageData || !!F(e);
248
+ function ce(e, t) {
249
+ let n = P(e), r = P(t);
250
+ if (n.imagePath && r.imagePath) return n.imagePath === r.imagePath;
251
+ if (n.imageData) return n.imageData === r.imageData;
252
+ let i = F(e);
253
+ return !!i && i === F(t);
254
+ }
255
+ var I = (e, t) => e.userSpokeAt === void 0 || e.userSpokeAt > t, L = () => crypto.randomUUID().replace(/-/g, "").slice(0, 12), le = "artifacts/";
256
+ async function R(e, t) {
257
+ let n = e.files?.artifacts;
258
+ if (!e.app?.editImages || !n) return [];
259
+ let r = [];
260
+ for (let e of t) if (e.startsWith(le)) try {
261
+ await n.exists(e.slice(10)) && r.push(e);
262
+ } catch {}
263
+ return r;
264
+ }
265
+ async function z(e, t, n = []) {
266
+ let r = e.app, i = r?.editImages, a = r?.generateImage;
267
+ try {
268
+ let e;
269
+ if (n.length && i) e = await i(t, [...n]);
270
+ else if (a) e = await a(t);
271
+ else return { message: "image generation isn't available" };
272
+ return oe(e) ? e : { message: "image generation returned an unrecognized result" };
273
+ } catch (e) {
274
+ return { message: `image generation failed: ${e instanceof Error ? e.message : String(e)}` };
275
+ }
276
+ }
277
+ var ue = 50;
278
+ function B(e) {
279
+ let t = /* @__PURE__ */ new Map();
280
+ return (n) => {
281
+ let r = n.conversationId ?? "", i = t.get(r);
282
+ if (i === void 0 ? i = e() : t.delete(r), t.set(r, i), t.size > ue) {
283
+ let e = t.keys().next().value;
284
+ e !== void 0 && t.delete(e);
285
+ }
286
+ return i;
287
+ };
288
+ }
289
+ //#endregion
290
+ //#region src/core/records.ts
291
+ var V = "slideshows", H = "storyboards", U = {}, W = /* @__PURE__ */ new WeakMap(), G = /* @__PURE__ */ new WeakMap();
292
+ function K(e) {
293
+ let t = W.get(e);
294
+ return t || W.set(e, t = /* @__PURE__ */ new Map()), t;
295
+ }
296
+ function de(t, n) {
297
+ let r = G.get(t);
298
+ r || G.set(t, r = /* @__PURE__ */ new Map());
299
+ let i = r.get(n);
300
+ return i || r.set(n, i = e()), i;
301
+ }
302
+ var q = (e, t) => `${e}/${t}.json`;
303
+ async function J(e, t) {
304
+ let n = K(e ?? U), r = n.get(t);
305
+ if (r) return r;
306
+ if (!e) return null;
307
+ try {
308
+ let r = JSON.parse(await e.read(t));
309
+ return n.set(t, r), r;
310
+ } catch {
311
+ return null;
312
+ }
313
+ }
314
+ async function Y(e, t, n) {
315
+ return g.test(n) ? J(e, q(t, n)) : null;
316
+ }
317
+ async function X(e, t, n, r) {
318
+ let i = q(t, n), a = e ?? U;
319
+ return de(a, i)(async () => {
320
+ let t = r(await J(e, i));
321
+ if (K(a).set(i, t), !e) return !1;
322
+ try {
323
+ return await e.write(i, JSON.stringify(t, null, 2)), !0;
324
+ } catch (e) {
325
+ return console.warn("[sequence] could not save", i, e), !1;
326
+ }
327
+ });
328
+ }
329
+ var Z = (e) => `the ${e} could not be saved, so it lasts only until the app or server restarts`, fe = 6e4;
330
+ function pe() {
331
+ let e = /* @__PURE__ */ new Map();
332
+ return { claim: (t) => {
333
+ let n = Date.now();
334
+ for (let [t, r] of e) n - r.at > fe && e.delete(t);
335
+ let r = e.get(t);
336
+ if (r) return { earlier: r.shown };
337
+ let i = () => void 0, a = new Promise((e) => i = e);
338
+ return e.set(t, {
339
+ at: n,
340
+ shown: a
341
+ }), { settle: (n) => {
342
+ !n && e.get(t)?.shown === a && e.delete(t), i(n);
343
+ } };
344
+ } };
345
+ }
346
+ //#endregion
347
+ //#region src/core/presentSlide.ts
348
+ function me({ slide: e, totalSlides: t, mode: n }) {
349
+ return n === "steps" ? {
350
+ step: e,
351
+ total: t,
352
+ kind: "guide",
353
+ label: `Step ${e} of ${t}`,
354
+ onShown: "explain it",
355
+ nextCall: `call presentSlide for step ${e + 1} of ${t} with mode "steps"`,
356
+ waitsForUser: !0
357
+ } : {
358
+ step: e,
359
+ total: t,
360
+ kind: "slideshow",
361
+ label: `Slide ${e} of ${t}`,
362
+ onShown: "explain it",
363
+ nextCall: `call presentSlide for slide ${e + 1} of ${t}`
364
+ };
365
+ }
366
+ var he = (e, { slide: t, totalSlides: n, title: r, imagePrompt: i, html: a }) => JSON.stringify([
367
+ e,
368
+ t,
369
+ n,
370
+ r,
371
+ i,
372
+ a
373
+ ]), ge = B(() => ({
374
+ latest: {
375
+ presentation: null,
376
+ steps: null
377
+ },
378
+ lastShown: null,
379
+ repeats: pe()
380
+ }));
381
+ function _e(e, t) {
382
+ let n = e.latest[t.mode], r = n?.totalSlides === t.totalSlides && (t.slide !== 1 || n.firstTitle === t.title);
383
+ if (n && r) return n;
384
+ let i = {
385
+ id: L(),
386
+ mode: t.mode,
387
+ totalSlides: t.totalSlides,
388
+ firstTitle: t.slide === 1 ? t.title : "",
389
+ slides: /* @__PURE__ */ new Map(),
390
+ current: 0,
391
+ shownAt: -Infinity
392
+ };
393
+ return e.latest[t.mode] = i, i;
394
+ }
395
+ function ve(e, t, n, r) {
396
+ t.current = n.slide, t.shownAt = Date.now(), e.lastShown = t;
397
+ let i = n.mode === "steps" ? "step" : "slide", { uuid: a, ...o } = r;
398
+ return {
399
+ ...o,
400
+ message: `${i} ${n.slide} of ${n.totalSlides} is on the screen again, as it was`,
401
+ instructions: S(n),
402
+ instructionsRequired: !0,
403
+ sequence: me(n)
404
+ };
405
+ }
406
+ function ye(e, t) {
407
+ if (e.mode !== "steps") return e.title ? `A presentation slide titled "${e.title}". ${e.imagePrompt}` : e.imagePrompt;
408
+ let n = e.title ? `, "${e.title}"` : "";
409
+ return [
410
+ `An instructional illustration of step ${e.slide} of ${e.totalSlides}${n}. ${e.imagePrompt}`,
411
+ t ? "The reference image is the previous step: keep the same objects, hands, tools and setting, and show what this step changes." : "",
412
+ "Show the action clearly, with little or no text."
413
+ ].filter(Boolean).join(" ");
414
+ }
415
+ async function be(e, t, n) {
416
+ if (n.html) return {
417
+ html: n.html,
418
+ prompt: ""
419
+ };
420
+ let r = t.slides.get(n.slide - 1), i = n.mode === "steps" ? P(r).imagePath : void 0, a = i ? await R(e, [i]) : [], o = ye(n, a.length > 0), s = await z(e, o, a), { imageData: c, imagePath: l } = P(s);
421
+ return c ? {
422
+ imageData: c,
423
+ ...l && { imagePath: l },
424
+ prompt: o
425
+ } : { failed: {
426
+ ...s,
427
+ sequence: null
428
+ } };
429
+ }
430
+ async function xe(e, t, n) {
431
+ let r = await be(e, t, n);
432
+ if ("failed" in r) return r.failed;
433
+ let { imageData: i, imagePath: a, html: o, prompt: s } = r, c = t.id, l = await X(e.files?.artifacts, V, c, (e) => {
434
+ let t = e ?? {
435
+ id: c,
436
+ title: "",
437
+ mode: n.mode,
438
+ totalSlides: n.totalSlides,
439
+ slides: {}
440
+ };
441
+ return n.slide === 1 && (t.title = n.title), t.slides[String(n.slide)] = {
442
+ title: n.title,
443
+ imagePrompt: n.imagePrompt,
444
+ ...a && { imagePath: a },
445
+ ...o && { html: o }
446
+ }, t;
447
+ }), u = {
448
+ ...i && { imageData: i },
449
+ ...a && { imagePath: a },
450
+ ...o && { html: o },
451
+ prompt: s,
452
+ slideshowId: c,
453
+ slide: n.slide,
454
+ totalSlides: n.totalSlides,
455
+ title: n.title,
456
+ mode: n.mode
457
+ }, d = n.mode === "steps" ? "step" : "slide";
458
+ return {
459
+ data: u,
460
+ title: n.title || `Slide ${n.slide}`,
461
+ message: [
462
+ `${d} ${n.slide} of ${n.totalSlides} of slideshow "${c}" is on the screen`,
463
+ a ? `saved to ${a}` : "",
464
+ l ? "" : Z("slideshow")
465
+ ].filter(Boolean).join("; "),
466
+ instructions: S(n),
467
+ instructionsRequired: !0,
468
+ sequence: me(n)
469
+ };
470
+ }
471
+ async function Q(e, t) {
472
+ let n = ie(t);
473
+ if (typeof n == "string") return {
474
+ message: n,
475
+ sequence: null
476
+ };
477
+ let r = ge(e), i = _e(r, n), a = n.mode === "steps";
478
+ if (a && n.slide > i.current && i.current > 0 && !I(e, i.shownAt)) {
479
+ console.info(`[sequence] holding step ${n.slide} for the user`);
480
+ let e = i.shownAt === Infinity ? "is still being drawn" : "is still on the screen";
481
+ return {
482
+ message: `step ${n.slide} was not shown: the user hasn't said they're ready yet, and step ${i.current} ${e}. Wait for them before calling presentSlide for step ${n.slide}.`,
483
+ cancelled: !0
484
+ };
485
+ }
486
+ let o = a ? i.slides.get(n.slide) : void 0;
487
+ if (o) return ce(e.currentResult, o) ? {
488
+ message: `step ${n.slide} of ${n.totalSlides} is already on the screen`,
489
+ cancelled: !0
490
+ } : ve(r, i, n, o);
491
+ let s = r.repeats.claim(he(i.id, n));
492
+ if ("earlier" in s) {
493
+ if (!await s.earlier) return Q(e, t);
494
+ let a = i.slides.get(n.slide);
495
+ return a && r.lastShown !== i ? ve(r, i, n, a) : {
496
+ message: `slide ${n.slide} of ${n.totalSlides} is already on the screen`,
497
+ cancelled: !0
498
+ };
499
+ }
500
+ let { settle: c } = s, l = {
501
+ current: i.current,
502
+ shownAt: i.shownAt
503
+ };
504
+ i.current = n.slide, i.shownAt = Infinity;
505
+ let u = !1;
506
+ try {
507
+ let t = await xe(e, i, n);
508
+ return u = se(t), u && (i.slides.set(n.slide, t), i.current = n.slide, i.shownAt = Date.now(), r.lastShown = i), t;
509
+ } finally {
510
+ c(u), !u && i.current === n.slide && (i.current = l.current, i.shownAt = l.shownAt);
511
+ }
512
+ }
513
+ //#endregion
514
+ //#region src/core/storyboard.ts
515
+ var Se = ({ id: e, title: t, totalPanels: n }) => ({
516
+ step: 0,
517
+ total: n,
518
+ kind: "story",
519
+ label: `The cast of "${t}"`,
520
+ onShown: "introduce the characters",
521
+ nextCall: `call presentPanel for panel 1 of ${n} with storyboardId "${e}"`
522
+ }), Ce = ({ id: e, totalPanels: t }, n, r) => ({
523
+ step: n,
524
+ total: t,
525
+ kind: "story",
526
+ label: `Panel ${n} of ${t}`,
527
+ onShown: "tell that part of the story",
528
+ nextCall: `call presentPanel for panel ${n + 1} of ${t} with storyboardId "${e}"`,
529
+ ...r.length > 0 && { waitsForUser: !0 }
530
+ }), we = (e, t) => e ? `${e}. ${t}` : t, Te = (e, { name: t, description: n }) => we(e, `Character reference sheet of ${t}: ${n}. Full body, front, side and back views, on a plain white background. No text or labels.`);
531
+ async function Ee(e, t) {
532
+ let n = T(t);
533
+ if (typeof n == "string") return {
534
+ message: n,
535
+ sequence: null
536
+ };
537
+ let r = await Promise.all(n.characters.map((t) => z(e, Te(n.style, t)))), i = r.map(P), a = r.find((e, t) => !i[t]?.imageData);
538
+ if (i.every(({ imageData: e }) => !e) && a) return {
539
+ ...a,
540
+ message: `the character sheets couldn't be made: ${a.message}`,
541
+ sequence: null
542
+ };
543
+ let o = {
544
+ id: L(),
545
+ ...n,
546
+ characters: n.characters.map((e, t) => {
547
+ let n = i[t]?.imagePath;
548
+ return {
549
+ ...e,
550
+ ...n && { imagePath: n }
551
+ };
552
+ }),
553
+ panels: {}
554
+ }, s = await X(e.files?.artifacts, H, o.id, () => o), c = o.characters.filter((e) => e.imagePath).map((e) => `${e.name}: ${e.imagePath}`), l = n.characters.map(({ name: e }, t) => i[t]?.imageData ? null : `${e} (${r[t]?.message ?? ""})`).filter((e) => !!e), u = i.find((e) => e.imageData)?.imageData;
555
+ return {
556
+ data: {
557
+ storyboardId: o.id,
558
+ title: o.title,
559
+ totalPanels: o.totalPanels,
560
+ ...u && { imageData: u },
561
+ characters: o.characters.map(({ name: e, imagePath: t }, n) => {
562
+ let r = i[n]?.imageData;
563
+ return {
564
+ name: e,
565
+ ...r && { imageData: r },
566
+ ...t && { imagePath: t }
567
+ };
568
+ })
569
+ },
570
+ title: o.title,
571
+ message: [
572
+ `storyboard "${o.id}" is ready with ${o.totalPanels} panels; the cast is on the screen`,
573
+ c.length ? `character sheets saved to ${c.join(", ")}` : "",
574
+ l.length ? `no sheet for ${l.join("; ")}: they will be drawn from their description` : "",
575
+ s ? "" : Z("storyboard")
576
+ ].filter(Boolean).join("; "),
577
+ instructions: E(o),
578
+ instructionsRequired: !0,
579
+ sequence: Se(o)
580
+ };
581
+ }
582
+ function De(e, t, n) {
583
+ let r = [we(e.style, `Panel ${t.panel} of ${e.totalPanels} of the illustrated story "${e.title}". ${t.imagePrompt}`)], i = 0, a = n.map(({ character: e, hasSheet: t }) => t ? `${e.name} is the character in reference image ${++i}` : `${e.name}: ${e.description}`);
584
+ return a.length && r.push(`${a.join(". ")}.`), i && r.push("Draw each character exactly as in their reference image: the same face, hair, clothes and colors. Use the reference images only for how the characters look, not for their pose or the background."), r.push("No captions, speech bubbles or other text in the picture."), r.join(" ");
585
+ }
586
+ async function Oe(e, t, n) {
587
+ let r = [], i = [];
588
+ for (let a of n.characters) {
589
+ let n = t.characters.find((e) => y(e.name, a));
590
+ if (!n) r.push(a);
591
+ else if (!i.some((e) => e.character === n)) {
592
+ let [t] = n.imagePath ? await R(e, [n.imagePath]) : [];
593
+ i.push({
594
+ character: n,
595
+ sheet: t ?? null
596
+ });
597
+ }
598
+ }
599
+ let a = i.map(({ sheet: e }) => e).filter((e) => !!e), o = De(t, n, i.map(({ character: e, sheet: t }) => ({
600
+ character: e,
601
+ hasSheet: !!t
602
+ }))), s = await z(e, o, a), { imageData: c, imagePath: l } = P(s);
603
+ if (!c) return {
604
+ ...s,
605
+ sequence: null
606
+ };
607
+ let u = n.panel < t.totalPanels && n.choices.length >= 2 ? n.choices : [], d = await X(e.files?.artifacts, H, t.id, (e) => {
608
+ let r = e ?? t;
609
+ return u.length && (r.interactive = !0), r.panels[String(n.panel)] = {
610
+ caption: n.caption,
611
+ characters: i.map(({ character: e }) => e.name),
612
+ imagePrompt: n.imagePrompt,
613
+ ...l && { imagePath: l },
614
+ ...u.length && { choices: u }
615
+ }, r;
616
+ });
617
+ return {
618
+ data: {
619
+ imageData: c,
620
+ ...l && { imagePath: l },
621
+ prompt: o,
622
+ storyboardId: t.id,
623
+ panel: n.panel,
624
+ totalPanels: t.totalPanels,
625
+ caption: n.caption,
626
+ ...u.length && { choices: u }
627
+ },
628
+ title: n.caption || `Panel ${n.panel}`,
629
+ message: [
630
+ `panel ${n.panel} of ${t.totalPanels} is on the screen`,
631
+ l ? `saved to ${l}` : "",
632
+ r.length ? `not in the cast, so drawn from the prompt alone: ${r.join(", ")}` : "",
633
+ d ? "" : Z("storyboard")
634
+ ].filter(Boolean).join("; "),
635
+ instructions: k(t, n.panel, u),
636
+ instructionsRequired: !0,
637
+ sequence: Ce(t, n.panel, u)
638
+ };
639
+ }
640
+ var ke = B(() => ({
641
+ repeats: pe(),
642
+ awaitingChoice: /* @__PURE__ */ new Map()
643
+ }));
644
+ async function $(e, t) {
645
+ let n = O(t);
646
+ if (!n) return {
647
+ message: D,
648
+ sequence: null
649
+ };
650
+ let { storyboardId: r, panel: i } = n, { repeats: a, awaitingChoice: o } = ke(e), s = o.get(r);
651
+ if (s && i > s.panel && !I(e, s.shownAt)) return console.info(`[sequence] holding panel ${i} for the user`), {
652
+ message: s.shownAt === Infinity ? `panel ${i} was not shown: panel ${s.panel} is still being drawn. Wait for it before going on.` : `panel ${i} was not shown: the user hasn't picked a choice yet, and panel ${s.panel} is still on the screen. Wait for their answer.`,
653
+ cancelled: !0
654
+ };
655
+ let c = a.claim(JSON.stringify(n));
656
+ if ("earlier" in c) return await c.earlier ? {
657
+ message: `panel ${i} is already on the screen`,
658
+ cancelled: !0
659
+ } : $(e, t);
660
+ let { settle: l } = c, u = {
661
+ panel: i,
662
+ shownAt: Infinity
663
+ }, d = o.get(r);
664
+ o.set(r, u);
665
+ let f = () => o.get(r) === u, p = !1;
666
+ try {
667
+ let t = await Y(e.files?.artifacts, H, r);
668
+ if (!t) return {
669
+ message: `there is no storyboard "${r}"; call defineStoryboard first`,
670
+ sequence: null
671
+ };
672
+ if (i > t.totalPanels) return {
673
+ message: `storyboard "${t.id}" has ${t.totalPanels} panels`,
674
+ sequence: null
675
+ };
676
+ let a = await Oe(e, t, n);
677
+ p = !!P(a).imageData;
678
+ let s = a.data;
679
+ return p && f() && (s?.choices?.length ? o.set(r, {
680
+ panel: i,
681
+ shownAt: Date.now()
682
+ }) : o.delete(r)), a;
683
+ } finally {
684
+ l(p), !p && f() && (d ? o.set(r, d) : o.delete(r));
685
+ }
686
+ }
687
+ //#endregion
688
+ //#region src/core/index.ts
689
+ var Ae = {
690
+ toolDefinition: w,
691
+ generatingMessage: "Making the slide...",
692
+ isEnabled: () => !0,
693
+ execute: (e, t) => Q(e, t)
694
+ }, je = {
695
+ toolDefinition: j,
696
+ generatingMessage: "Drawing the characters...",
697
+ isEnabled: () => !0,
698
+ execute: (e, t) => Ee(e, t)
699
+ }, Me = {
700
+ toolDefinition: M,
701
+ generatingMessage: "Drawing the panel...",
702
+ isEnabled: () => !0,
703
+ execute: (e, t) => $(e, t)
704
+ };
705
+ //#endregion
706
+ export { o as A, k as C, y as D, T as E, t as M, f as N, S as O, E as S, ie as T, C as _, H as a, re as b, j as c, te as d, D as f, w as g, p as h, V as i, n as j, r as k, ee as l, M as m, Me as n, Y as o, h as p, Ae as r, m as s, je as t, ne as u, g as v, O as w, A as x, x as y };