pi-pocket 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/AGENTS.md +2 -0
  2. package/README.md +5 -3
  3. package/docs/architecture.md +21 -0
  4. package/docs/design/design-mode.md +108 -0
  5. package/docs/design/notes-a.md +106 -0
  6. package/docs/design/notes-b.md +123 -0
  7. package/docs/design/notes-c.md +92 -0
  8. package/docs/design/notes-d.md +78 -0
  9. package/docs/design/release-notes-design.md +46 -0
  10. package/docs/extensions/browser-modules.md +2 -0
  11. package/docs/extensions/policies/api-stability.md +2 -0
  12. package/docs/extensions/reference/api-versions.md +1 -0
  13. package/docs/extensions/reference/host.md +1 -1
  14. package/docs/extensions/reference/limits.md +4 -0
  15. package/docs/extensions/reference/names.md +2 -2
  16. package/docs/extensions/reference/pocket.md +38 -0
  17. package/docs/features.md +19 -5
  18. package/docs/map.md +75 -54
  19. package/docs/release-notes.md +1 -1
  20. package/docs/self-editing.md +1 -1
  21. package/docs/systemd.md +1 -1
  22. package/npm-shrinkwrap.json +2 -2
  23. package/package.json +1 -1
  24. package/src/launcher/main.ts +20 -9
  25. package/src/server/app.ts +113 -4
  26. package/src/server/background.ts +3 -3
  27. package/src/server/cache-keeper.ts +605 -0
  28. package/src/server/compaction.ts +68 -15
  29. package/src/server/design/bundle.ts +65 -0
  30. package/src/server/design/export.ts +118 -0
  31. package/src/server/design/files.ts +49 -0
  32. package/src/server/design/guide.ts +56 -0
  33. package/src/server/design/index.ts +232 -0
  34. package/src/server/design/pptx.ts +304 -0
  35. package/src/server/design/render.ts +593 -0
  36. package/src/server/design/system.ts +248 -0
  37. package/src/server/docs.ts +55 -0
  38. package/src/server/extensions/artifacts.ts +202 -18
  39. package/src/server/extensions/design/canvas.js +304 -0
  40. package/src/server/extensions/design/comments.js +15 -0
  41. package/src/server/extensions/design/data.js +263 -0
  42. package/src/server/extensions/design/devices.js +70 -0
  43. package/src/server/extensions/design/docks.js +647 -0
  44. package/src/server/extensions/design/edits.js +37 -0
  45. package/src/server/extensions/design/index.js +326 -0
  46. package/src/server/extensions/design/messages.js +92 -0
  47. package/src/server/extensions/design/parts.js +160 -0
  48. package/src/server/extensions/design/state.js +146 -0
  49. package/src/server/extensions/design/style.css +1332 -0
  50. package/src/server/extensions/design/tweaks.js +161 -0
  51. package/src/server/extensions/design/views.js +375 -0
  52. package/src/server/extensions/design/workspace.js +723 -0
  53. package/src/server/extensions/design.ts +620 -0
  54. package/src/server/extensions/plan.ts +11 -9
  55. package/src/server/extensions/prompt.ts +11 -3
  56. package/src/server/extensions/subagents.ts +7 -1
  57. package/src/server/extensions/updates/index.js +16 -6
  58. package/src/server/extensions/updates.ts +2 -2
  59. package/src/server/http/artifact-assets.ts +216 -0
  60. package/src/server/http/artifact-inspect.ts +1281 -0
  61. package/src/server/http/artifacts.ts +89 -14
  62. package/src/server/http/conversation-routes.ts +46 -1
  63. package/src/server/http.ts +12 -4
  64. package/src/server/main.ts +4 -0
  65. package/src/server/npm-install.ts +82 -0
  66. package/src/server/npm-update.ts +100 -0
  67. package/src/server/reload.ts +5 -0
  68. package/src/server/room.ts +2 -0
  69. package/src/server/spend.ts +73 -9
  70. package/src/server/update-state.ts +257 -8
  71. package/src/server/updates.ts +268 -33
  72. package/src/server/zip.ts +163 -0
  73. package/types/pocket.d.ts +32 -0
  74. package/web/activity-fold.js +28 -0
  75. package/web/artifact-bridge.js +506 -82
  76. package/web/extension-ui.js +21 -1
  77. package/web/pip.js +46 -9
  78. package/web/sessions.js +1 -1
  79. package/web/sheets/appearance.js +10 -0
  80. package/web/sheets/viewers.js +26 -1
  81. package/web/style.css +11 -0
  82. package/web/taps.js +26 -0
  83. package/web/theme.js +6 -0
  84. package/web/transcript.js +19 -10
  85. package/src/server/http/public.ts +0 -30
@@ -0,0 +1,620 @@
1
+ /**
2
+ * Design: a canvas beside the chat for interactive designs. It shows an HTML artifact in a frame at phone, tablet, and
3
+ * desktop sizes, keeps its versions to compare and go back to, holds shared comments pinned to elements, edit and tweak
4
+ * modes, a grid for variations, and a present mode for decks, and hands each change to Pi as a message for the person to
5
+ * send. The agent's side (tools, prompt guide, export) is in `../design/`; this module is the workspace.
6
+ */
7
+ import { randomUUID } from "node:crypto";
8
+ import { defineExtension } from "@earendil-works/pi-durable";
9
+ import { designGuide, designRoutes, designTools } from "../design/index.ts";
10
+ import {
11
+ type ArtifactMeta,
12
+ ArtifactBodyDoc,
13
+ ArtifactsDoc,
14
+ type DesignComment,
15
+ DesignCommentsDoc,
16
+ type DesignItem,
17
+ } from "../docs.ts";
18
+ import type { ConversationWebRequest, ExtensionWeb, PocketHost } from "../host.ts";
19
+
20
+ /** An artifact as the index keeps it, with the fields of bundles (`group`, `kind`) that an older index lacks. */
21
+ type DesignArtifact = ArtifactMeta & { group?: string; kind?: string };
22
+
23
+ const MAX_COMMENT = 2000;
24
+ const MAX_COMMENTS = 200;
25
+ const MAX_ITEMS = 100;
26
+ const MAX_KNOBS = 40;
27
+ const MAX_PEOPLE = 50;
28
+ const MAX_TARGET = 4000;
29
+
30
+ /** Text on one line or more, with no control characters but line breaks and tabs, cut to `max`. */
31
+ function body(value: unknown, max: number): string {
32
+ return String(value ?? "")
33
+ .replace(/\r\n?/g, "\n")
34
+ .replace(/[\p{Bidi_Control}\u2028\u2029]|[^\P{Cc}\n\t]/gu, "")
35
+ .trim()
36
+ .slice(0, max);
37
+ }
38
+
39
+ const text = (value: unknown, max: number): string =>
40
+ typeof value === "string" ? value.replace(/[\p{Cc}\p{Bidi_Control}]/gu, " ").slice(0, max) : "";
41
+
42
+ /** A size or a place on the page: finite, within what a page can be, to a tenth. */
43
+ const number = (value: unknown): number =>
44
+ typeof value === "number" && Number.isFinite(value)
45
+ ? Math.round(Math.max(-1e7, Math.min(1e7, value)) * 10) / 10
46
+ : 0;
47
+
48
+ /**
49
+ * The element a comment points at, as the page's inspector described it (the page is untrusted data): a fixed shape,
50
+ * every text cut, no field the app does not know. Throws a 400 for what is no element at all.
51
+ */
52
+ export function cleanTarget(web: Pick<ExtensionWeb, "error">, value: unknown) {
53
+ const target = value as Record<string, unknown> | null;
54
+
55
+ if (typeof target !== "object" || target === null || Array.isArray(target)) {
56
+ throw web.error(400, "A comment points at an element.");
57
+ }
58
+
59
+ const selector = text(target.selector, 500).trim();
60
+
61
+ if (selector === "") {
62
+ throw web.error(400, "A comment points at an element.");
63
+ }
64
+
65
+ const rect = (
66
+ typeof target.rect === "object" && target.rect !== null ? target.rect : {}
67
+ ) as Record<string, unknown>;
68
+ const styles: Record<string, string> = {};
69
+
70
+ if (typeof target.styles === "object" && target.styles !== null) {
71
+ for (const [name, style] of Object.entries(target.styles).slice(0, 12)) {
72
+ if (/^[a-z-]{1,40}$/.test(name) && typeof style === "string") {
73
+ styles[name] = text(style, 200);
74
+ }
75
+ }
76
+ }
77
+
78
+ const clean = {
79
+ ...(typeof target.pid === "string" && target.pid !== ""
80
+ ? { pid: text(target.pid, 100) }
81
+ : {}),
82
+ selector,
83
+ tag: text(target.tag, 40).toLowerCase(),
84
+ text: text(target.text, 200),
85
+ html: text(target.html, 1000),
86
+ rect: {
87
+ x: number(rect.x),
88
+ y: number(rect.y),
89
+ width: number(rect.width),
90
+ height: number(rect.height),
91
+ },
92
+ styles,
93
+ };
94
+
95
+ if (JSON.stringify(clean).length > MAX_TARGET) {
96
+ throw web.error(400, "That element is too large to point at.");
97
+ }
98
+
99
+ return clean;
100
+ }
101
+
102
+ /**
103
+ * Tweak values as kept: at most `MAX_KNOBS`, a name that is a CSS custom property or a plain word (any other name is
104
+ * left out), a short value.
105
+ */
106
+ export function cleanTweaks(web: Pick<ExtensionWeb, "error">, value: unknown) {
107
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
108
+ throw web.error(400, "Tweaks are a set of names and values.");
109
+ }
110
+
111
+ const entries = Object.entries(value);
112
+
113
+ if (entries.length > MAX_KNOBS) {
114
+ throw web.error(400, `A design has up to ${MAX_KNOBS} tweaks.`);
115
+ }
116
+
117
+ const values: Record<string, string | number | boolean> = {};
118
+
119
+ for (const [name, each] of entries) {
120
+ if (!/^(?:--)?[A-Za-z][\w.-]{0,99}$/.test(name) && !/^--[\w.-]{1,98}$/.test(name)) {
121
+ // A name the server does not keep (a page may name an event "dark mode") is left out, not the whole set.
122
+ continue;
123
+ }
124
+
125
+ if (typeof each === "string") {
126
+ values[name] = text(each, 500);
127
+ } else if (
128
+ typeof each === "boolean" ||
129
+ (typeof each === "number" && Number.isFinite(each))
130
+ ) {
131
+ values[name] = each;
132
+ } else {
133
+ throw web.error(400, "A tweak is text, a number, or on and off.");
134
+ }
135
+ }
136
+
137
+ return values;
138
+ }
139
+
140
+ export default async function createDesign(host: PocketHost, web?: ExtensionWeb) {
141
+ const extension = defineExtension({
142
+ name: "pocket-design",
143
+ tools: designTools(host),
144
+ sections: [designGuide(host)],
145
+ });
146
+
147
+ if (web === undefined) {
148
+ return extension;
149
+ }
150
+
151
+ // Everyone who sees every session sees the canvas, the versions, and the comments; steering is checked per route.
152
+ web.client("design", { access: "view" });
153
+
154
+ /** The artifacts a design can show: HTML pages, newest version last, with their group and kind. */
155
+ const artifacts = async (request: ConversationWebRequest) => {
156
+ const index = await request.snapshot(
157
+ ArtifactsDoc,
158
+ request.conversation.id,
159
+ request.context,
160
+ );
161
+ const notes = await request.snapshot(
162
+ DesignCommentsDoc,
163
+ request.conversation.id,
164
+ request.context,
165
+ );
166
+
167
+ return Object.entries(index?.items ?? {})
168
+ .filter(([, meta]) => meta.type === "html")
169
+ .map(([id, meta]) => {
170
+ const design = meta as DesignArtifact;
171
+ const restores = notes?.items[id]?.restores ?? {};
172
+
173
+ return {
174
+ id,
175
+ title: meta.title,
176
+ ...(typeof design.group === "string" ? { group: design.group } : {}),
177
+ kind: design.kind === "deck" ? "deck" : "page",
178
+ versions: meta.versions.map((version) => ({
179
+ version: version.version,
180
+ size: version.size,
181
+ createdAt: version.createdAt,
182
+ ...(restores[String(version.version)] === undefined
183
+ ? {}
184
+ : { restoredFrom: restores[String(version.version)] }),
185
+ })),
186
+ };
187
+ });
188
+ };
189
+
190
+ /** The artifact must be one of the session's own HTML pages: an id names no other session's, nor `__proto__`. */
191
+ const requireArtifact = async (request: ConversationWebRequest, id: string) => {
192
+ const index = await request.snapshot(
193
+ ArtifactsDoc,
194
+ request.conversation.id,
195
+ request.context,
196
+ );
197
+
198
+ if (
199
+ index === undefined ||
200
+ !Object.hasOwn(index.items, id) ||
201
+ index.items[id]!.type !== "html"
202
+ ) {
203
+ throw web.error(404, "No such design.");
204
+ }
205
+
206
+ return index.items[id]!;
207
+ };
208
+
209
+ /**
210
+ * The item for a design, made if it is not there yet. The document tracks what is assigned and what is changed through
211
+ * it, not what is changed through the object that was assigned: so it is read back after it is made.
212
+ */
213
+ const itemOf = (doc: { items: Record<string, DesignItem> }, id: string): DesignItem => {
214
+ if (!Object.hasOwn(doc.items, id)) {
215
+ if (Object.keys(doc.items).length >= MAX_ITEMS) {
216
+ throw web.error(409, `This session keeps notes on up to ${MAX_ITEMS} designs.`);
217
+ }
218
+
219
+ doc.items[id] = { comments: [] };
220
+ }
221
+
222
+ return doc.items[id]!;
223
+ };
224
+
225
+ /** An item with no comments, tweaks, or restores keeps nothing: it goes, and its place with it. */
226
+ const dropIfEmpty = (doc: { items: Record<string, DesignItem> }, id: string) => {
227
+ const item = doc.items[id];
228
+
229
+ if (
230
+ item !== undefined &&
231
+ item.comments.length === 0 &&
232
+ Object.keys(item.tweaks ?? {}).length === 0 &&
233
+ Object.keys(item.restores ?? {}).length === 0
234
+ ) {
235
+ delete doc.items[id];
236
+ }
237
+ };
238
+
239
+ /** The comments are everyone's; one person's tweaks are theirs alone. */
240
+ const view = (item: DesignItem | undefined, person: string) => ({
241
+ comments: item?.comments ?? [],
242
+ tweaks: item?.tweaks?.[person]?.values ?? null,
243
+ });
244
+
245
+ const changed = (conversation: number, artifact: string) =>
246
+ web.send("comments", { conversation, artifact });
247
+
248
+ const options = { conversation: "conversation", access: "view" } as const;
249
+ const steer = { conversation: "conversation", access: "steer" } as const;
250
+
251
+ // The session's designs, for the picker.
252
+ web.conversationRoute(
253
+ "GET",
254
+ "c/:conversation/artifacts",
255
+ async (request) => ({ artifacts: await artifacts(request) }),
256
+ options,
257
+ );
258
+
259
+ // One design's comments (all of them) and this person's tweaks.
260
+ web.conversationRoute(
261
+ "GET",
262
+ "c/:conversation/designs/:artifact",
263
+ async (request) => {
264
+ const id = request.params.artifact!;
265
+
266
+ await requireArtifact(request, id);
267
+
268
+ const doc = await request.snapshot(
269
+ DesignCommentsDoc,
270
+ request.conversation.id,
271
+ request.context,
272
+ );
273
+
274
+ return view(
275
+ Object.hasOwn(doc?.items ?? {}, id) ? doc!.items[id] : undefined,
276
+ request.user.id,
277
+ );
278
+ },
279
+ options,
280
+ );
281
+
282
+ web.conversationRoute(
283
+ "POST",
284
+ "c/:conversation/designs/:artifact/comments",
285
+ async (request) => {
286
+ const id = request.params.artifact!;
287
+ const meta = await requireArtifact(request, id);
288
+ const version = request.body.version;
289
+ const words = body(request.body.text, MAX_COMMENT);
290
+
291
+ if (
292
+ typeof version !== "number" ||
293
+ !meta.versions.some((each) => each.version === version)
294
+ ) {
295
+ throw web.error(400, "That version is not there.");
296
+ }
297
+
298
+ if (words === "") {
299
+ throw web.error(400, "Write what to change.");
300
+ }
301
+
302
+ const target = cleanTarget(web, request.body.target);
303
+ const comment: DesignComment = {
304
+ id: randomUUID().slice(0, 8),
305
+ version,
306
+ target,
307
+ text: words,
308
+ by: request.user.id,
309
+ byName: text(request.user.name, 80),
310
+ at: Date.now(),
311
+ };
312
+
313
+ await request.commit(async (tx) => {
314
+ const doc = await tx.doc(DesignCommentsDoc, request.conversation.id);
315
+
316
+ const item = itemOf(doc, id);
317
+
318
+ if (item.comments.length >= MAX_COMMENTS) {
319
+ throw web.error(
320
+ 409,
321
+ `A design has up to ${MAX_COMMENTS} comments: delete some.`,
322
+ );
323
+ }
324
+
325
+ item.comments.push(comment);
326
+ });
327
+ changed(request.conversation.id, id);
328
+
329
+ return { comment };
330
+ },
331
+ steer,
332
+ );
333
+
334
+ // Edit a comment's words or mark it done (or open again). Anyone who steers may resolve; the words are the author's.
335
+ web.conversationRoute(
336
+ "POST",
337
+ "c/:conversation/designs/:artifact/comments/:comment",
338
+ async (request) => {
339
+ const id = request.params.artifact!;
340
+ const { resolved } = request.body;
341
+ const words =
342
+ request.body.text === undefined ? undefined : body(request.body.text, MAX_COMMENT);
343
+
344
+ if (resolved !== undefined && typeof resolved !== "boolean") {
345
+ throw web.error(400, "Resolved is true or false.");
346
+ }
347
+
348
+ if (words === "") {
349
+ throw web.error(400, "Write what to change.");
350
+ }
351
+
352
+ const comment = await request.commit(async (tx) => {
353
+ const doc = await tx.doc(DesignCommentsDoc, request.conversation.id);
354
+ const found = Object.hasOwn(doc.items, id)
355
+ ? doc.items[id]!.comments.find((each) => each.id === request.params.comment)
356
+ : undefined;
357
+
358
+ if (found === undefined) {
359
+ throw web.error(404, "No such comment.");
360
+ }
361
+
362
+ if (words !== undefined && words !== found.text) {
363
+ if (found.by !== request.user.id && request.user.role !== "owner") {
364
+ throw web.error(403, "Only its author changes a comment's words.");
365
+ }
366
+
367
+ found.text = words;
368
+ }
369
+
370
+ if (resolved === true && found.resolved === undefined) {
371
+ found.resolved = { by: request.user.id, at: Date.now() };
372
+ } else if (resolved === false && found.resolved !== undefined) {
373
+ delete found.resolved;
374
+ }
375
+
376
+ // The document hands out tracked objects, which cannot be cloned: a plain copy of what the answer needs.
377
+ return JSON.parse(JSON.stringify(found)) as DesignComment;
378
+ });
379
+
380
+ changed(request.conversation.id, id);
381
+
382
+ return { comment };
383
+ },
384
+ steer,
385
+ );
386
+
387
+ web.conversationRoute(
388
+ "DELETE",
389
+ "c/:conversation/designs/:artifact/comments/:comment",
390
+ async (request) => {
391
+ const id = request.params.artifact!;
392
+
393
+ await request.commit(async (tx) => {
394
+ const doc = await tx.doc(DesignCommentsDoc, request.conversation.id);
395
+ const item = Object.hasOwn(doc.items, id) ? doc.items[id]! : undefined;
396
+ const at =
397
+ item?.comments.findIndex((each) => each.id === request.params.comment) ?? -1;
398
+
399
+ if (item === undefined || at === -1) {
400
+ throw web.error(404, "No such comment.");
401
+ }
402
+
403
+ if (item.comments[at]!.by !== request.user.id && request.user.role !== "owner") {
404
+ throw web.error(403, "Only its author deletes a comment.");
405
+ }
406
+
407
+ item.comments.splice(at, 1);
408
+ dropIfEmpty(doc, id);
409
+ });
410
+ changed(request.conversation.id, id);
411
+
412
+ return {};
413
+ },
414
+ steer,
415
+ );
416
+
417
+ // Resolve every open comment of a design at once, for after "Send to Pi".
418
+ web.conversationRoute(
419
+ "POST",
420
+ "c/:conversation/designs/:artifact/resolve",
421
+ async (request) => {
422
+ const id = request.params.artifact!;
423
+ const ids = request.body.ids;
424
+
425
+ if (
426
+ !Array.isArray(ids) ||
427
+ ids.length > MAX_COMMENTS ||
428
+ !ids.every((each) => typeof each === "string")
429
+ ) {
430
+ throw web.error(400, "Choose the comments to resolve.");
431
+ }
432
+
433
+ const count = await request.commit(async (tx) => {
434
+ const doc = await tx.doc(DesignCommentsDoc, request.conversation.id);
435
+ const item = Object.hasOwn(doc.items, id) ? doc.items[id]! : undefined;
436
+ let done = 0;
437
+
438
+ for (const comment of item?.comments ?? []) {
439
+ if ((ids as string[]).includes(comment.id) && comment.resolved === undefined) {
440
+ comment.resolved = { by: request.user.id, at: Date.now() };
441
+ done += 1;
442
+ }
443
+ }
444
+
445
+ return done;
446
+ });
447
+
448
+ changed(request.conversation.id, id);
449
+
450
+ return { resolved: count };
451
+ },
452
+ steer,
453
+ );
454
+
455
+ // This person's tweak values for the design: replaced as a whole, or cleared with null.
456
+ web.conversationRoute(
457
+ "POST",
458
+ "c/:conversation/designs/:artifact/tweaks",
459
+ async (request) => {
460
+ const id = request.params.artifact!;
461
+
462
+ await requireArtifact(request, id);
463
+
464
+ const values =
465
+ request.body.values === null ? null : cleanTweaks(web, request.body.values);
466
+
467
+ await request.commit(async (tx) => {
468
+ const doc = await tx.doc(DesignCommentsDoc, request.conversation.id);
469
+
470
+ const clearing = values === null || Object.keys(values).length === 0;
471
+
472
+ // Nothing to clear, and no item to make for it: an empty item would keep a place for nothing.
473
+ if (clearing && !Object.hasOwn(doc.items, id)) {
474
+ return;
475
+ }
476
+
477
+ const item = itemOf(doc, id);
478
+
479
+ if (values === null || clearing) {
480
+ if (item.tweaks !== undefined) {
481
+ delete item.tweaks[request.user.id];
482
+ }
483
+
484
+ dropIfEmpty(doc, id);
485
+
486
+ return;
487
+ }
488
+
489
+ item.tweaks ??= {};
490
+
491
+ if (
492
+ item.tweaks[request.user.id] === undefined &&
493
+ Object.keys(item.tweaks).length >= MAX_PEOPLE
494
+ ) {
495
+ throw web.error(409, "Too many people keep tweaks for this design.");
496
+ }
497
+
498
+ item.tweaks[request.user.id] = { values, at: Date.now() };
499
+ });
500
+
501
+ return {};
502
+ },
503
+ steer,
504
+ );
505
+
506
+ // "Go back to this one": a new version that is a copy of an older one (its assets too), written in one commit. The
507
+ // key makes a repeat of the same tap find the version it made.
508
+ web.conversationRoute(
509
+ "POST",
510
+ "c/:conversation/designs/:artifact/restore",
511
+ async (request) => {
512
+ const id = request.params.artifact!;
513
+ const from = request.body.version;
514
+ const key = request.body.key;
515
+
516
+ if (typeof from !== "number" || !Number.isSafeInteger(from)) {
517
+ throw web.error(400, "Choose the version to go back to.");
518
+ }
519
+
520
+ if (typeof key !== "string" || !/^[\w-]{8,64}$/.test(key)) {
521
+ throw web.error(400, "A restore needs a key.");
522
+ }
523
+
524
+ const meta = await requireArtifact(request, id);
525
+
526
+ if (!meta.versions.some((each) => each.version === from)) {
527
+ throw web.error(404, "That version is not there.");
528
+ }
529
+
530
+ const source = await request.snapshot(
531
+ ArtifactBodyDoc,
532
+ request.conversation.id,
533
+ `${id}@${from}`,
534
+ request.context,
535
+ );
536
+
537
+ if (source === undefined) {
538
+ throw web.error(404, "That version has no content.");
539
+ }
540
+
541
+ const callId = `design-restore:${key}`;
542
+
543
+ const made = await request.commit(async (tx) => {
544
+ const doc = await tx.doc(ArtifactsDoc, request.conversation.id);
545
+ const current = Object.hasOwn(doc.items, id) ? doc.items[id] : undefined;
546
+
547
+ if (current === undefined || current.type !== "html") {
548
+ throw web.error(404, "No such design.");
549
+ }
550
+
551
+ const again = current.versions.find((each) => each.callId === callId);
552
+
553
+ if (again !== undefined) {
554
+ return again.version;
555
+ }
556
+
557
+ const original = current.versions.find((each) => each.version === from);
558
+
559
+ if (original === undefined) {
560
+ throw web.error(404, "That version is not there.");
561
+ }
562
+
563
+ if (current.versions.at(-1)!.version === from) {
564
+ throw web.error(409, "That is already the newest version.");
565
+ }
566
+
567
+ const next = current.versions.at(-1)!.version + 1;
568
+ const { inline: _inline, ...copy } = original;
569
+
570
+ // The task is the original's, so the record stays valid; the call id is new, so no real call matches it.
571
+ current.versions.push({ ...copy, version: next, callId, createdAt: Date.now() });
572
+
573
+ const stored = await tx.doc(
574
+ ArtifactBodyDoc,
575
+ request.conversation.id,
576
+ `${id}@${next}`,
577
+ {
578
+ content: source.content,
579
+ },
580
+ );
581
+
582
+ if (stored.content !== source.content) {
583
+ stored.content = source.content;
584
+ }
585
+
586
+ const notes = await tx.doc(DesignCommentsDoc, request.conversation.id);
587
+ const item = itemOf(notes, id);
588
+
589
+ item.restores = { ...item.restores, [String(next)]: from };
590
+
591
+ return next;
592
+ });
593
+
594
+ return { version: made };
595
+ },
596
+ steer,
597
+ );
598
+
599
+ // `/design-open [artifact]`: opens the workspace in the tab of the person who ran it. A command's name needs a dash,
600
+ // so it is not `/design`.
601
+ web.command("design-open", {
602
+ description: "Open the Design canvas",
603
+ args: "[artifact]",
604
+ scope: "conversation",
605
+ handler: (args, context) => {
606
+ web.send(
607
+ "open",
608
+ { conversation: context.conversationId, artifact: args.trim().slice(0, 100) },
609
+ { to: context.user.id },
610
+ );
611
+
612
+ return { type: "toast", level: "info", message: "Opening Design" };
613
+ },
614
+ });
615
+
616
+ // Export (`POST export`) is the agent side's; the file comes through core's download route.
617
+ designRoutes(web, host);
618
+
619
+ return extension;
620
+ }
@@ -18,10 +18,17 @@ import { PlanDoc } from "../docs.ts";
18
18
  import type { PocketHost } from "../host.ts";
19
19
  import { BUILT_IN_TOOLS } from "../reload.ts";
20
20
 
21
- const PLANNING = `Plan mode is on. Read and explore, then propose a plan; change nothing yet. You can use read, artifact, artifact_inline, the browser to look at pages (not to click, type, or run scripts in them), and bash for simple read-only commands such as ls, cat, grep, find, and git status/log/diff/show, without redirects, globs, variables, or command substitution, and tools marked read-only. Writing, editing, other commands, and subagents are blocked until a person approves the plan. End your answer with the plan as a short numbered list.`;
22
-
23
- /** Tools that change nothing, or whose own calls are checked one by one (codemode). */
24
- const ALWAYS_ALLOWED = new Set(["read", "artifact", "artifact_inline", "codemode"]);
21
+ const PLANNING = `Plan mode is on. Read and explore, then propose a plan; change nothing yet. You can use read, artifact, artifact_inline, the browser to look at pages (not to click, type, or run scripts in them), and bash for simple read-only commands such as ls, cat, grep, find, and git status/log/diff/show, without redirects, globs, variables, or command substitution, and tools marked read-only. Subagents inherit plan mode and can explore under the same restrictions. Writing, editing, and other commands are blocked until a person approves the plan. End your answer with the plan as a short numbered list.`;
22
+
23
+ /** Tools that change nothing, or whose own calls are checked in plan mode (codemode's, and a subagent's in its own plan mode). */
24
+ const ALWAYS_ALLOWED = new Set([
25
+ "read",
26
+ "artifact",
27
+ "artifact_inline",
28
+ "design_preview",
29
+ "codemode",
30
+ "subagent",
31
+ ]);
25
32
  /** What the browser may do in plan mode: look at pages, not act on them. */
26
33
  const BROWSER_LOOKS = new Set([
27
34
  "navigate",
@@ -271,11 +278,6 @@ export function blockedInPlanMode(
271
278
  return undefined;
272
279
  }
273
280
 
274
- // Asking how a subagent does, or stopping it, changes nothing.
275
- if (tool === "subagent" && (args.action === "status" || args.action === "stop")) {
276
- return undefined;
277
- }
278
-
279
281
  // Nor does reading what a background task printed, or stopping it; starting one runs a command.
280
282
  if (tool === "background" && args.action !== "start") {
281
283
  return undefined;
@@ -116,10 +116,18 @@ export default function createPrompt(host: PocketHost) {
116
116
  section("project_context", (input) => load(cwdOf(input)).context),
117
117
  section("skills", (input) => load(cwdOf(input)).skills, { tag: false }),
118
118
  section("environment", (input) => {
119
- // The date only, so the prompt stays cache-friendly through the day.
120
- const today = new Date().toISOString().slice(0, 10);
119
+ const base = `Working directory: ${cwdOf(input)}\nPlatform: ${process.platform}`;
121
120
 
122
- return `Working directory: ${cwdOf(input)}\nPlatform: ${process.platform}\nToday: ${today}`;
121
+ // Off by default: a date in the system prompt changes once a day and rewrites the whole prompt cache.
122
+ // PI_POCKET_PROMPT_DATE=1 adds the local date back.
123
+ if (process.env.PI_POCKET_PROMPT_DATE !== "1") {
124
+ return base;
125
+ }
126
+
127
+ const now = new Date();
128
+ const pad = (n: number) => String(n).padStart(2, "0");
129
+
130
+ return `${base}\nToday: ${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
123
131
  }),
124
132
  ],
125
133
  });