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
@@ -35,7 +35,7 @@ import {
35
35
  type TaskRuntime,
36
36
  type Tx,
37
37
  } from "@earendil-works/pi-durable";
38
- import { type PendingReport, REPORT_PREFIX, SubagentsDoc } from "../docs.ts";
38
+ import { type PendingReport, PlanDoc, REPORT_PREFIX, SubagentsDoc } from "../docs.ts";
39
39
  import { describe } from "../errors.ts";
40
40
  import type { PocketHost } from "../host.ts";
41
41
  import { levelFor, sameModel, THINKING_LEVELS } from "../models.ts";
@@ -1028,6 +1028,12 @@ export default function createSubagents(host: PocketHost) {
1028
1028
  ...(thinking === undefined ? {} : { thinkingLevel: thinking }),
1029
1029
  ...(tools === undefined ? {} : { tools }),
1030
1030
  });
1031
+ const plan = await tx.doc(PlanDoc, api.conversationId);
1032
+
1033
+ if (plan.on) {
1034
+ Object.assign(await tx.doc(PlanDoc, child.id), plan);
1035
+ }
1036
+
1031
1037
  state.agents[name] = { conversationId: child.id, reported: [] };
1032
1038
  }
1033
1039
 
@@ -1,5 +1,5 @@
1
1
  // Updates' browser module, for the owner: a menu row and a launcher action that open the Update Pi Pocket sheet (the running
2
- // version, the newest release and its notes, what stands in the way, and Update and restart), a notice once for each
2
+ // version and how it was installed, the newest release and its notes, what stands in the way, and Update and restart), a notice once for each
3
3
  // new version, and one for how an update ended, after its restart. It uses only `pocket`.
4
4
  export const pocketApi = 1;
5
5
 
@@ -27,6 +27,13 @@ const remember = (key, value) => {
27
27
  }
28
28
  };
29
29
 
30
+ /** How the running copy was installed (`installKind` in the server's status), in words for the sheet. */
31
+ const INSTALLED = {
32
+ "npm-global": "installed with npm (global)",
33
+ "npm-local": "installed with npm (local)",
34
+ npx: "run with npx",
35
+ };
36
+
30
37
  /** "5 minutes ago", "3 hours ago", "2 days ago". */
31
38
  function ago(at) {
32
39
  const minutes = Math.max(0, Math.round((Date.now() - at) / 60_000));
@@ -256,11 +263,14 @@ export default function setup(pocket) {
256
263
  const careful = latest !== undefined && (latest.warning || latest.unread === true);
257
264
  const step = current.step ?? working;
258
265
  // While an update moves the checkout, the server does not read it: no commit then is no word on it.
259
- const where = current.commit
260
- ? `${current.branch ?? "detached"} · ${current.commit.slice(0, 7)}`
261
- : current.step === undefined
262
- ? "not a git checkout"
263
- : null;
266
+ // A server older than this field, or one that cannot tell, sends no kind: the checkout words then.
267
+ const where = Object.hasOwn(INSTALLED, current.installKind)
268
+ ? INSTALLED[current.installKind]
269
+ : current.commit
270
+ ? `${current.branch ?? "detached"} · ${current.commit.slice(0, 7)}`
271
+ : current.step === undefined
272
+ ? "not a git checkout"
273
+ : null;
264
274
 
265
275
  return html`<div class="updates-sheet">
266
276
  <div class="updates-card">
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Updates of Pi Pocket itself: it looks for a new release every 6 hours, tells the owner, shows its notes, and updates
3
- * the git checkout to it with one tap, then restarts; it can also install new releases by itself while no session is
3
+ * the git checkout or the global npm install to it with one tap, then restarts; it can also install new releases by itself while no session is
4
4
  * working. Its routes and screens are the owner's alone (`updates.ts` does the work).
5
5
  */
6
6
  import { defineExtension } from "@earendil-works/pi-durable";
@@ -51,7 +51,7 @@ export default function createUpdates(host: PocketHost, web: ExtensionWeb) {
51
51
  name: "check",
52
52
  label: "Look for new versions",
53
53
  description:
54
- "Ask GitHub for the newest release every 6 hours, and say when there is one.",
54
+ "Ask npm (for an npm install) or GitHub for the newest release every 6 hours, and say when there is one.",
55
55
  type: "boolean",
56
56
  default: true,
57
57
  },
@@ -0,0 +1,216 @@
1
+ /** Bundle path validation, safe MIME types, and sandboxed asset responses. */
2
+ import { createHmac, timingSafeEqual } from "node:crypto";
3
+ import type { IncomingMessage, ServerResponse } from "node:http";
4
+ import type { User } from "../config.ts";
5
+ import { origin } from "../auth.ts";
6
+ import { conversationId } from "./io.ts";
7
+ import { posix } from "node:path";
8
+ import type { PocketApp } from "../app.ts";
9
+ import type { ConversationId } from "@earendil-works/pi-durable";
10
+ import { HttpError } from "../errors.ts";
11
+ import { send } from "./io.ts";
12
+ import { artifactResponse } from "./artifacts.ts";
13
+
14
+ /** Artifacts must keep an opaque origin, including assets opened in their own tabs. */
15
+ export const ARTIFACT_CSP =
16
+ "sandbox allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-pointer-lock allow-downloads; frame-ancestors 'self'";
17
+
18
+ function signature(
19
+ app: PocketApp,
20
+ conversation: string,
21
+ artifact: string,
22
+ version: string,
23
+ user: User,
24
+ until: string,
25
+ ): string {
26
+ return createHmac("sha256", app.config.ownerToken)
27
+ .update(
28
+ JSON.stringify([
29
+ "artifact-assets-v1",
30
+ conversation,
31
+ artifact,
32
+ version,
33
+ user.id,
34
+ user.tokenHash,
35
+ until,
36
+ ]),
37
+ )
38
+ .digest("hex");
39
+ }
40
+
41
+ /** Opaque-origin subrequests have no SameSite cookie. This capability grants only one version's assets for 12 hours. */
42
+ export function assetRoot(
43
+ app: PocketApp,
44
+ conversation: string,
45
+ artifact: string,
46
+ version: number,
47
+ user: User,
48
+ ): string {
49
+ const until = String(app.now() + 12 * 60 * 60 * 1000);
50
+ const signed = `${version}~${user.id}~${until}~${signature(app, conversation, artifact, String(version), user, until)}`;
51
+
52
+ return `/a/${encodeURIComponent(conversation)}/${encodeURIComponent(artifact)}/${encodeURIComponent(signed)}/`;
53
+ }
54
+
55
+ /** Recheck the person's current access. Removing the person or rotating either token revokes this capability. */
56
+ export function assetUser(
57
+ app: PocketApp,
58
+ conversation: string,
59
+ artifact: string,
60
+ signed: string,
61
+ ): { user: User; version: number } | undefined {
62
+ const [version, id, until, mac, extra] = signed.split("~");
63
+ const user = app.config.users.find((each) => each.id === id);
64
+
65
+ if (
66
+ extra !== undefined ||
67
+ user === undefined ||
68
+ !/^[1-9][0-9]*$/.test(version ?? "") ||
69
+ !/^[0-9]+$/.test(until ?? "") ||
70
+ Number(until) <= app.now() ||
71
+ !/^[0-9a-f]{64}$/.test(mac ?? "")
72
+ ) {
73
+ return undefined;
74
+ }
75
+
76
+ const expected = signature(app, conversation, artifact, version!, user, until!);
77
+
78
+ if (!timingSafeEqual(Buffer.from(mac!, "hex"), Buffer.from(expected, "hex"))) {
79
+ return undefined;
80
+ }
81
+
82
+ return { user, version: Number(version) };
83
+ }
84
+
85
+ export function assetName(name: string): boolean {
86
+ return (
87
+ name.length > 0 &&
88
+ name.length <= 200 &&
89
+ name
90
+ .split("/")
91
+ .every((part) => /^[A-Za-z0-9._-]+$/.test(part) && part !== "." && part !== "..")
92
+ );
93
+ }
94
+
95
+ /** Safe bundle MIME types, shared with isolated design rendering. Unknown extensions must remain binary. */
96
+ export const ASSET_TYPES: Readonly<Record<string, string>> = Object.freeze({
97
+ ".html": "text/html; charset=utf-8",
98
+ ".htm": "text/html; charset=utf-8",
99
+ ".css": "text/css; charset=utf-8",
100
+ ".js": "text/javascript; charset=utf-8",
101
+ ".mjs": "text/javascript; charset=utf-8",
102
+ ".json": "application/json",
103
+ ".txt": "text/plain; charset=utf-8",
104
+ ".csv": "text/csv; charset=utf-8",
105
+ ".svg": "image/svg+xml",
106
+ ".png": "image/png",
107
+ ".jpg": "image/jpeg",
108
+ ".jpeg": "image/jpeg",
109
+ ".gif": "image/gif",
110
+ ".webp": "image/webp",
111
+ ".avif": "image/avif",
112
+ ".ico": "image/x-icon",
113
+ ".woff": "font/woff",
114
+ ".woff2": "font/woff2",
115
+ ".ttf": "font/ttf",
116
+ ".otf": "font/otf",
117
+ ".mp3": "audio/mpeg",
118
+ ".wav": "audio/wav",
119
+ ".mp4": "video/mp4",
120
+ ".webm": "video/webm",
121
+ ".pdf": "application/pdf",
122
+ ".wasm": "application/wasm",
123
+ });
124
+
125
+ /** The extension, never file bytes or model input, decides the response type. */
126
+ export function assetType(name: string): string {
127
+ return ASSET_TYPES[posix.extname(name).toLowerCase()] ?? "application/octet-stream";
128
+ }
129
+
130
+ /** A published bundle keeps its existing share token as the sole access capability. */
131
+ export async function publicArtifactBundle(
132
+ app: PocketApp,
133
+ request: IncomingMessage,
134
+ response: ServerResponse,
135
+ token: string | undefined,
136
+ name = "",
137
+ query = new URLSearchParams(),
138
+ ): Promise<void> {
139
+ if (request.method !== "GET" && request.method !== "HEAD") {
140
+ response.setHeader("allow", "GET, HEAD");
141
+
142
+ throw new HttpError(405, "Published artifacts are only read");
143
+ }
144
+
145
+ const published = token === undefined ? undefined : await app.shares.open(token);
146
+
147
+ if (published === undefined) {
148
+ throw new HttpError(404, "No such page");
149
+ }
150
+
151
+ const root = `${origin(request)}/pub/${encodeURIComponent(token!)}/`;
152
+ const { found, share } = published;
153
+
154
+ if (name !== "") {
155
+ return artifactAssetResponse(
156
+ app,
157
+ response,
158
+ conversationId(share.conversation),
159
+ share.id,
160
+ found.version,
161
+ name,
162
+ root + name.slice(0, name.lastIndexOf("/") + 1),
163
+ false,
164
+ );
165
+ }
166
+
167
+ const manifest = found.meta.versions.find((each) => each.version === found.version)!.assets;
168
+
169
+ return artifactResponse(
170
+ response,
171
+ found,
172
+ query,
173
+ false,
174
+ Object.keys(manifest ?? {}).length > 0 ? root : undefined,
175
+ );
176
+ }
177
+
178
+ /** Resolve an asset only through the chosen version's manifest, never directly by a global hash. */
179
+ export async function artifactAssetResponse(
180
+ app: PocketApp,
181
+ response: ServerResponse,
182
+ conversation: ConversationId,
183
+ artifact: string,
184
+ version: number,
185
+ name: string,
186
+ base: string,
187
+ bridge = true,
188
+ ): Promise<void> {
189
+ if (!assetName(name)) {
190
+ throw new HttpError(404, "No such asset");
191
+ }
192
+
193
+ const asset = await app.artifactAsset(conversation, artifact, version, name);
194
+ const type = assetType(name);
195
+
196
+ if (type.startsWith("text/html")) {
197
+ return artifactResponse(
198
+ response,
199
+ {
200
+ meta: { type: "html", title: asset.title },
201
+ content: asset.bytes.toString("utf8"),
202
+ },
203
+ new URLSearchParams(),
204
+ bridge,
205
+ base,
206
+ );
207
+ }
208
+
209
+ return send(response, 200, asset.bytes, type, {
210
+ "content-security-policy": ARTIFACT_CSP,
211
+ "x-content-type-options": "nosniff",
212
+ "referrer-policy": "no-referrer",
213
+ // Sandboxed pages send an opaque Origin, including module and font requests.
214
+ "access-control-allow-origin": "*",
215
+ });
216
+ }