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.
- package/AGENTS.md +2 -0
- package/README.md +5 -3
- package/docs/architecture.md +21 -0
- package/docs/design/design-mode.md +108 -0
- package/docs/design/notes-a.md +106 -0
- package/docs/design/notes-b.md +123 -0
- package/docs/design/notes-c.md +92 -0
- package/docs/design/notes-d.md +78 -0
- package/docs/design/release-notes-design.md +46 -0
- package/docs/extensions/browser-modules.md +2 -0
- package/docs/extensions/policies/api-stability.md +2 -0
- package/docs/extensions/reference/api-versions.md +1 -0
- package/docs/extensions/reference/host.md +1 -1
- package/docs/extensions/reference/limits.md +4 -0
- package/docs/extensions/reference/names.md +2 -2
- package/docs/extensions/reference/pocket.md +38 -0
- package/docs/features.md +19 -5
- package/docs/map.md +75 -54
- package/docs/release-notes.md +1 -1
- package/docs/self-editing.md +1 -1
- package/docs/systemd.md +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/launcher/main.ts +20 -9
- package/src/server/app.ts +113 -4
- package/src/server/background.ts +3 -3
- package/src/server/cache-keeper.ts +605 -0
- package/src/server/compaction.ts +68 -15
- package/src/server/design/bundle.ts +65 -0
- package/src/server/design/export.ts +118 -0
- package/src/server/design/files.ts +49 -0
- package/src/server/design/guide.ts +56 -0
- package/src/server/design/index.ts +232 -0
- package/src/server/design/pptx.ts +304 -0
- package/src/server/design/render.ts +593 -0
- package/src/server/design/system.ts +248 -0
- package/src/server/docs.ts +55 -0
- package/src/server/extensions/artifacts.ts +202 -18
- package/src/server/extensions/design/canvas.js +304 -0
- package/src/server/extensions/design/comments.js +15 -0
- package/src/server/extensions/design/data.js +263 -0
- package/src/server/extensions/design/devices.js +70 -0
- package/src/server/extensions/design/docks.js +647 -0
- package/src/server/extensions/design/edits.js +37 -0
- package/src/server/extensions/design/index.js +326 -0
- package/src/server/extensions/design/messages.js +92 -0
- package/src/server/extensions/design/parts.js +160 -0
- package/src/server/extensions/design/state.js +146 -0
- package/src/server/extensions/design/style.css +1332 -0
- package/src/server/extensions/design/tweaks.js +161 -0
- package/src/server/extensions/design/views.js +375 -0
- package/src/server/extensions/design/workspace.js +723 -0
- package/src/server/extensions/design.ts +620 -0
- package/src/server/extensions/plan.ts +11 -9
- package/src/server/extensions/prompt.ts +11 -3
- package/src/server/extensions/subagents.ts +7 -1
- package/src/server/extensions/updates/index.js +16 -6
- package/src/server/extensions/updates.ts +2 -2
- package/src/server/http/artifact-assets.ts +216 -0
- package/src/server/http/artifact-inspect.ts +1281 -0
- package/src/server/http/artifacts.ts +89 -14
- package/src/server/http/conversation-routes.ts +46 -1
- package/src/server/http.ts +12 -4
- package/src/server/main.ts +4 -0
- package/src/server/npm-install.ts +82 -0
- package/src/server/npm-update.ts +100 -0
- package/src/server/reload.ts +5 -0
- package/src/server/room.ts +2 -0
- package/src/server/spend.ts +73 -9
- package/src/server/update-state.ts +257 -8
- package/src/server/updates.ts +268 -33
- package/src/server/zip.ts +163 -0
- package/types/pocket.d.ts +32 -0
- package/web/activity-fold.js +28 -0
- package/web/artifact-bridge.js +506 -82
- package/web/extension-ui.js +21 -1
- package/web/pip.js +46 -9
- package/web/sessions.js +1 -1
- package/web/sheets/appearance.js +10 -0
- package/web/sheets/viewers.js +26 -1
- package/web/style.css +11 -0
- package/web/taps.js +26 -0
- package/web/theme.js +6 -0
- package/web/transcript.js +19 -10
- package/src/server/http/public.ts +0 -30
|
@@ -6,14 +6,13 @@
|
|
|
6
6
|
import type { ServerResponse } from "node:http";
|
|
7
7
|
import { marked } from "marked";
|
|
8
8
|
import type { PocketApp } from "../app.ts";
|
|
9
|
+
import { origin } from "../auth.ts";
|
|
9
10
|
import type { User } from "../config.ts";
|
|
10
11
|
import type { ArtifactType } from "../docs.ts";
|
|
11
12
|
import { HttpError } from "../errors.ts";
|
|
13
|
+
import { ARTIFACT_INSPECT, withDeck } from "./artifact-inspect.ts";
|
|
12
14
|
import { conversationId, escapeHtml, send } from "./io.ts";
|
|
13
|
-
|
|
14
|
-
/** Artifacts get an opaque origin even when opened in their own tab: no cookies, no access to the app. */
|
|
15
|
-
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'";
|
|
15
|
+
import { ARTIFACT_CSP, artifactAssetResponse, assetRoot, assetUser } from "./artifact-assets.ts";
|
|
17
16
|
|
|
18
17
|
/**
|
|
19
18
|
* A Markdown artifact's look, from a few colors: the browser's light or dark by default, the app's theme when the
|
|
@@ -76,7 +75,12 @@ function markdownTheme(query: URLSearchParams): string {
|
|
|
76
75
|
* press, a finger's or pen's lift, a key's press but not its repeats), or a page could spend the tap in one event and
|
|
77
76
|
* get it back from the next of the same tap (mousedown after pointerdown, touchend after pointerup). The message is copied before it is read,
|
|
78
77
|
* so a getter cannot show one type to this check and another to the app. Runs outside a frame or a sandbox: nothing.
|
|
79
|
-
* `rearm` puts the tap listeners back, for a page that wipes them with `document.open()`.
|
|
78
|
+
* `rearm` puts the tap listeners back, and design mode's too, for a page that wipes them with `document.open()`.
|
|
79
|
+
*
|
|
80
|
+
* Design mode's page side (`ARTIFACT_INSPECT`, `artifact-inspect.ts`) starts here too, with the line to the app: the
|
|
81
|
+
* app's design messages (inspect, text edit, tweaks, pins, goto) go to it and not to the page, and it sends the app
|
|
82
|
+
* what a person picks or edits, the page's tweaks manifest, and a deck's slides. Only it can send them: the stand-in
|
|
83
|
+
* `parent` passes the page's own messages for follow-ups and saved state, and nothing more down the line.
|
|
80
84
|
*/
|
|
81
85
|
const ARTIFACT_BRIDGE = `
|
|
82
86
|
const realParent = window.parent;
|
|
@@ -113,7 +117,13 @@ const ARTIFACT_BRIDGE = `
|
|
|
113
117
|
|
|
114
118
|
const tell = (data) => fire(new Heard("message", { data, source: stand, origin: address }));
|
|
115
119
|
|
|
116
|
-
|
|
120
|
+
const design = (${ARTIFACT_INSPECT})(toApp, false);
|
|
121
|
+
|
|
122
|
+
line.onmessage = (event) => {
|
|
123
|
+
if (!design.message(event.data)) {
|
|
124
|
+
tell(event.data);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
117
127
|
Object.defineProperty(stand, "postMessage", {
|
|
118
128
|
value: (message, ...rest) => {
|
|
119
129
|
const copy = clone(message);
|
|
@@ -139,7 +149,14 @@ const ARTIFACT_BRIDGE = `
|
|
|
139
149
|
rearm();
|
|
140
150
|
realPost({ type: "pocket-bridge" }, "*", [channel.port2]);
|
|
141
151
|
|
|
142
|
-
return {
|
|
152
|
+
return {
|
|
153
|
+
realParent,
|
|
154
|
+
realPost,
|
|
155
|
+
rearm: () => {
|
|
156
|
+
rearm();
|
|
157
|
+
design.restart();
|
|
158
|
+
},
|
|
159
|
+
};
|
|
143
160
|
`;
|
|
144
161
|
|
|
145
162
|
/** The bridge as a script tag, for the start of an HTML artifact. */
|
|
@@ -204,20 +221,33 @@ export async function artifactResponse(
|
|
|
204
221
|
query = new URLSearchParams(),
|
|
205
222
|
/** False for a public link: it shows the page as it was written, and the app frames it nowhere. */
|
|
206
223
|
bridge = true,
|
|
224
|
+
/** A bundle root (or an HTML asset's directory), also used when the inline card fetches this page. */
|
|
225
|
+
base?: string,
|
|
207
226
|
): Promise<void> {
|
|
208
227
|
const headers = {
|
|
209
228
|
"content-security-policy": ARTIFACT_CSP,
|
|
210
229
|
"x-content-type-options": "nosniff",
|
|
211
230
|
"referrer-policy": "no-referrer",
|
|
231
|
+
"access-control-allow-origin": "*",
|
|
212
232
|
};
|
|
213
233
|
|
|
234
|
+
if (base !== undefined && found.meta.type === "html") {
|
|
235
|
+
const start = /^\uFEFF?[\t\n\f\r ]*(?:<!doctype[^>]*>)?/i.exec(found.content)![0];
|
|
236
|
+
|
|
237
|
+
found = {
|
|
238
|
+
...found,
|
|
239
|
+
content:
|
|
240
|
+
start + `<base href="${escapeHtml(base)}">` + found.content.slice(start.length),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
214
244
|
if (found.meta.type === "svg") {
|
|
215
245
|
return send(response, 200, found.content, "image/svg+xml", headers);
|
|
216
246
|
}
|
|
217
247
|
|
|
218
248
|
if (found.meta.type === "markdown") {
|
|
219
249
|
const rendered = await marked.parse(found.content);
|
|
220
|
-
const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(found.meta.title)}</title
|
|
250
|
+
const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(found.meta.title)}</title>${base === undefined ? "" : `<base href="${escapeHtml(base)}">`}<style>${MARKDOWN_STYLE}${markdownTheme(query)}</style></head><body>${rendered}</body></html>`;
|
|
221
251
|
|
|
222
252
|
return send(response, 200, html, "text/html; charset=utf-8", headers);
|
|
223
253
|
}
|
|
@@ -227,10 +257,14 @@ export async function artifactResponse(
|
|
|
227
257
|
// a proxy that drops it), it goes in too, or a page could say hello itself and send follow-ups with no tap. Outside
|
|
228
258
|
// a frame, it does nothing.
|
|
229
259
|
const dest = response.req?.headers["sec-fetch-dest"];
|
|
260
|
+
// A deck as a page of its own still shows one slide at a time (`withDeck`). A script's fetch (the inline card) gets
|
|
261
|
+
// the page as it was written: the card shows it as code, and runs it in the run frame, which has the bridge.
|
|
230
262
|
const content =
|
|
231
|
-
|
|
263
|
+
dest === "empty"
|
|
232
264
|
? found.content
|
|
233
|
-
:
|
|
265
|
+
: !bridge || dest === "document"
|
|
266
|
+
? withDeck(found.content)
|
|
267
|
+
: withBridge(found.content);
|
|
234
268
|
|
|
235
269
|
return send(response, 200, content, "text/html; charset=utf-8", headers);
|
|
236
270
|
}
|
|
@@ -242,6 +276,22 @@ export async function artifact(
|
|
|
242
276
|
user: User | undefined,
|
|
243
277
|
query = new URLSearchParams(),
|
|
244
278
|
): Promise<void> {
|
|
279
|
+
if (response.req.method !== "GET" && response.req.method !== "HEAD") {
|
|
280
|
+
response.setHeader("allow", "GET, HEAD");
|
|
281
|
+
|
|
282
|
+
throw new HttpError(405, "Artifacts are only read");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const [conv, id, version] = parts;
|
|
286
|
+
const name = parts.slice(3).join("/");
|
|
287
|
+
const capability =
|
|
288
|
+
conv !== undefined && id !== undefined && version !== undefined && name !== ""
|
|
289
|
+
? assetUser(app, conv, id, version)
|
|
290
|
+
: undefined;
|
|
291
|
+
|
|
292
|
+
// A capability is accepted only for manifest assets, never the main page or /a/frame.
|
|
293
|
+
user = capability?.user ?? user;
|
|
294
|
+
|
|
245
295
|
if (user === undefined) {
|
|
246
296
|
throw new HttpError(401, "Sign in first");
|
|
247
297
|
}
|
|
@@ -255,8 +305,6 @@ export async function artifact(
|
|
|
255
305
|
});
|
|
256
306
|
}
|
|
257
307
|
|
|
258
|
-
const [conv, id, version] = parts;
|
|
259
|
-
|
|
260
308
|
if (id === undefined) {
|
|
261
309
|
throw new HttpError(404, "No artifact");
|
|
262
310
|
}
|
|
@@ -265,8 +313,35 @@ export async function artifact(
|
|
|
265
313
|
const found = await app.artifactBody(
|
|
266
314
|
conversationId(conv),
|
|
267
315
|
id,
|
|
268
|
-
version
|
|
316
|
+
capability?.version ??
|
|
317
|
+
(version === undefined || version === "latest" ? undefined : Number(version)),
|
|
269
318
|
);
|
|
270
319
|
|
|
271
|
-
|
|
320
|
+
// An asset capability must never mint a fresh lifetime for itself.
|
|
321
|
+
const root =
|
|
322
|
+
capability === undefined
|
|
323
|
+
? assetRoot(app, conv!, id, found.version, user)
|
|
324
|
+
: `/a/${encodeURIComponent(conv!)}/${encodeURIComponent(id)}/${encodeURIComponent(version!)}/`;
|
|
325
|
+
|
|
326
|
+
if (name !== "") {
|
|
327
|
+
return artifactAssetResponse(
|
|
328
|
+
app,
|
|
329
|
+
response,
|
|
330
|
+
conversationId(conv),
|
|
331
|
+
id,
|
|
332
|
+
found.version,
|
|
333
|
+
name,
|
|
334
|
+
origin(response.req) + root + name.slice(0, name.lastIndexOf("/") + 1),
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const manifest = found.meta.versions.find((each) => each.version === found.version)!.assets;
|
|
339
|
+
|
|
340
|
+
await artifactResponse(
|
|
341
|
+
response,
|
|
342
|
+
found,
|
|
343
|
+
query,
|
|
344
|
+
true,
|
|
345
|
+
Object.keys(manifest ?? {}).length > 0 ? origin(response.req) + root : undefined,
|
|
346
|
+
);
|
|
272
347
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** `/api/c/:id`: one conversation's routes, for people the API has checked may see it. */
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { createWriteStream, rmSync } from "node:fs";
|
|
3
|
+
import { constants, createWriteStream, rmSync } from "node:fs";
|
|
4
|
+
import { type FileHandle, open } from "node:fs/promises";
|
|
4
5
|
import { basename, extname, join, resolve, sep } from "node:path";
|
|
5
6
|
import { pipeline } from "node:stream/promises";
|
|
6
7
|
import type { ConversationId } from "@earendil-works/pi-durable";
|
|
@@ -410,6 +411,50 @@ export async function conversationRoutes(
|
|
|
410
411
|
return serveImage(request, response, app.workspace.conversationFile(user, id, requested));
|
|
411
412
|
}
|
|
412
413
|
|
|
414
|
+
// A file of the session's folder as a download (a design's export): whom the file viewer lets read it, as it is.
|
|
415
|
+
if (third === "download" && method === "GET") {
|
|
416
|
+
const requested = url.searchParams.get("path") ?? "";
|
|
417
|
+
|
|
418
|
+
if (requested.trim() === "") {
|
|
419
|
+
throw new HttpError(400, "path is required");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const file = app.workspace.readableFile(user, id, requested);
|
|
423
|
+
let handle: FileHandle;
|
|
424
|
+
|
|
425
|
+
try {
|
|
426
|
+
handle = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
427
|
+
} catch {
|
|
428
|
+
throw new HttpError(404, "File not found");
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
try {
|
|
432
|
+
const stats = await handle.stat();
|
|
433
|
+
|
|
434
|
+
if (!stats.isFile()) {
|
|
435
|
+
throw new HttpError(404, "File not found");
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// An ASCII fallback, then the whole name (RFC 6266): no quote or line break gets into the header.
|
|
439
|
+
const name = basename(file);
|
|
440
|
+
const ascii = name.replace(/[^\x20-\x7e]|["\\]/g, "_");
|
|
441
|
+
|
|
442
|
+
response.writeHead(200, {
|
|
443
|
+
"content-type": "application/octet-stream",
|
|
444
|
+
"content-length": stats.size,
|
|
445
|
+
"content-disposition": `attachment; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(name)}`,
|
|
446
|
+
"cache-control": "private, no-store",
|
|
447
|
+
"x-content-type-options": "nosniff",
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
await pipeline(handle.createReadStream({ autoClose: false }), response);
|
|
451
|
+
} finally {
|
|
452
|
+
await handle.close();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
413
458
|
if (third === "entry" && fourth !== undefined && method === "GET") {
|
|
414
459
|
const entry = await app.transcripts.fullEntry(id, Number(fourth));
|
|
415
460
|
|
package/src/server/http.ts
CHANGED
|
@@ -9,12 +9,12 @@ import { Auth } from "./auth.ts";
|
|
|
9
9
|
import { describe, HttpError } from "./errors.ts";
|
|
10
10
|
import { createApi } from "./http/api.ts";
|
|
11
11
|
import { artifact } from "./http/artifacts.ts";
|
|
12
|
+
import { publicArtifactBundle } from "./http/artifact-assets.ts";
|
|
12
13
|
import { extensionFile } from "./http/extension-routes.ts";
|
|
13
14
|
import { hookRoute } from "./http/hook-routes.ts";
|
|
14
15
|
import { linkRoute } from "./http/link-routes.ts";
|
|
15
16
|
import { serveApp, serveFile, VENDOR, WEB } from "./http/assets.ts";
|
|
16
17
|
import { type HttpOptions, json } from "./http/io.ts";
|
|
17
|
-
import { publicArtifact } from "./http/public.ts";
|
|
18
18
|
import { join as joinPage, login, sharePage } from "./http/sign-in.ts";
|
|
19
19
|
import { themeFile } from "./http/theme-routes.ts";
|
|
20
20
|
|
|
@@ -49,11 +49,19 @@ export function createHandler(options: HttpOptions) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// An artifact published to anyone with its address: the token is the whole of the access, no sign-in.
|
|
52
|
-
//
|
|
52
|
+
// `/pub/<token>` is the page, and `/pub/<token>/<name>` one of its bundle's files (`http/artifact-assets.ts`).
|
|
53
53
|
if (parts[0] === "pub") {
|
|
54
|
-
const token = parts.length
|
|
54
|
+
const token = parts.length >= 2 ? parts[1] : undefined;
|
|
55
|
+
const name = parts.slice(2).join("/");
|
|
55
56
|
|
|
56
|
-
return await
|
|
57
|
+
return await publicArtifactBundle(
|
|
58
|
+
app,
|
|
59
|
+
request,
|
|
60
|
+
response,
|
|
61
|
+
token,
|
|
62
|
+
name,
|
|
63
|
+
url.searchParams,
|
|
64
|
+
);
|
|
57
65
|
}
|
|
58
66
|
|
|
59
67
|
// A page an extension module shows to anyone with its link: the token is the whole of the access, no sign-in.
|
package/src/server/main.ts
CHANGED
|
@@ -15,6 +15,10 @@ import { join, resolve } from "node:path";
|
|
|
15
15
|
import type { AccessInfo, PocketApp } from "./app.ts";
|
|
16
16
|
import { APP_ROOT, dataDir } from "./config.ts";
|
|
17
17
|
import type { createHandler } from "./http.ts";
|
|
18
|
+
import { restoreMissingNpmPackage } from "./update-state.ts";
|
|
19
|
+
|
|
20
|
+
// A resident process can reach recovery even when a stopped package switch left no live folder.
|
|
21
|
+
restoreMissingNpmPackage(APP_ROOT);
|
|
18
22
|
|
|
19
23
|
const RESTART_CODE = 75;
|
|
20
24
|
/**
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Identify npm installs without treating a local project's hoisted packages or an npx cache as a global install. */
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { constants, existsSync } from "node:fs";
|
|
4
|
+
import { access, realpath } from "node:fs/promises";
|
|
5
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
6
|
+
import { updatePaths } from "./update-state.ts";
|
|
7
|
+
|
|
8
|
+
export type InstallKind = "git" | "npm-global" | "npm-local" | "npx" | "unknown";
|
|
9
|
+
|
|
10
|
+
/** Use the npm beside the Node.js that runs Pi Pocket, else the one on PATH. */
|
|
11
|
+
export function npmCommand(): { file: string; shell: boolean } {
|
|
12
|
+
const windows = process.platform === "win32";
|
|
13
|
+
const beside = join(dirname(process.execPath), windows ? "npm.cmd" : "npm");
|
|
14
|
+
|
|
15
|
+
return { file: existsSync(beside) ? beside : windows ? "npm.cmd" : "npm", shell: windows };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The global package folder for this Node.js, not another npm on PATH. */
|
|
19
|
+
export function globalNpmRoot(): Promise<string> {
|
|
20
|
+
const { file, shell } = npmCommand();
|
|
21
|
+
|
|
22
|
+
return new Promise((done, fail) => {
|
|
23
|
+
execFile(
|
|
24
|
+
shell ? `"${file}"` : file,
|
|
25
|
+
["root", "-g"],
|
|
26
|
+
{
|
|
27
|
+
encoding: "utf8",
|
|
28
|
+
shell,
|
|
29
|
+
timeout: 20_000,
|
|
30
|
+
maxBuffer: 1024 * 1024,
|
|
31
|
+
env: { ...process.env, npm_config_update_notifier: "false" },
|
|
32
|
+
},
|
|
33
|
+
(error, stdout) => {
|
|
34
|
+
if (error !== null) {
|
|
35
|
+
fail(error);
|
|
36
|
+
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
done(stdout.trim());
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Git detection belongs to Updates. Only an exact global root match permits a package swap. */
|
|
47
|
+
export async function npmInstallKind(appRoot: string): Promise<InstallKind> {
|
|
48
|
+
const root = await realpath(appRoot).catch(() => resolve(appRoot));
|
|
49
|
+
const parts = [...resolve(appRoot).split(sep), ...root.split(sep)];
|
|
50
|
+
|
|
51
|
+
if (parts.includes("_npx") && parts.includes("node_modules")) {
|
|
52
|
+
return "npx";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const global = await globalNpmRoot()
|
|
56
|
+
.then((path) => realpath(path))
|
|
57
|
+
.catch(() => undefined);
|
|
58
|
+
const parent = await realpath(dirname(root)).catch(() => dirname(root));
|
|
59
|
+
|
|
60
|
+
if (global !== undefined && parent === global) {
|
|
61
|
+
return "npm-global";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return parts.includes("node_modules") ? "npm-local" : "unknown";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Both renames need the parent, and an install owned by another user must not be replaced. The update's state folder
|
|
69
|
+
* beside the package must take its files too, when an earlier update left it.
|
|
70
|
+
*/
|
|
71
|
+
export async function npmWritable(appRoot: string): Promise<boolean> {
|
|
72
|
+
const state = updatePaths(appRoot).dir;
|
|
73
|
+
|
|
74
|
+
return Promise.all([
|
|
75
|
+
access(appRoot, constants.W_OK),
|
|
76
|
+
access(dirname(appRoot), constants.W_OK),
|
|
77
|
+
...(existsSync(state) ? [access(state, constants.W_OK)] : []),
|
|
78
|
+
]).then(
|
|
79
|
+
() => true,
|
|
80
|
+
() => false,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/** Install a release in an isolated global prefix while the live npm package keeps running. */
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { mkdir, readFile, rm } from "node:fs/promises";
|
|
4
|
+
import { npmCommand } from "./npm-install.ts";
|
|
5
|
+
import { readPending, stagedPackagePath, updatePaths } from "./update-state.ts";
|
|
6
|
+
|
|
7
|
+
export type NpmStageOptions = {
|
|
8
|
+
/** An explicit local tarball for offline tests. Production uses the exact registry version. */
|
|
9
|
+
spec?: string;
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Pending state must be durable before npm can write anything in staging. */
|
|
14
|
+
export async function stageNpmUpdate(
|
|
15
|
+
appRoot: string,
|
|
16
|
+
version: string,
|
|
17
|
+
options: NpmStageOptions = {},
|
|
18
|
+
): Promise<void> {
|
|
19
|
+
const pending = readPending(appRoot);
|
|
20
|
+
|
|
21
|
+
if (
|
|
22
|
+
pending?.kind !== "npm-global" ||
|
|
23
|
+
pending.toVersion !== version ||
|
|
24
|
+
pending.phase !== "packages" ||
|
|
25
|
+
pending.modules
|
|
26
|
+
) {
|
|
27
|
+
throw new Error("Write an npm global pending update before staging its exact version.");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(version)) {
|
|
31
|
+
throw new Error("An npm update needs an exact version.");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const { staging } = updatePaths(appRoot);
|
|
35
|
+
const command = npmCommand();
|
|
36
|
+
|
|
37
|
+
await rm(staging, { recursive: true, force: true });
|
|
38
|
+
await mkdir(staging, { recursive: true });
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
await new Promise<void>((resolve, reject) => {
|
|
42
|
+
const args = [
|
|
43
|
+
"install",
|
|
44
|
+
"--global",
|
|
45
|
+
"--prefix",
|
|
46
|
+
staging,
|
|
47
|
+
"--no-audit",
|
|
48
|
+
"--no-fund",
|
|
49
|
+
"--",
|
|
50
|
+
options.spec ?? `pi-pocket@${version}`,
|
|
51
|
+
];
|
|
52
|
+
const child = spawn(
|
|
53
|
+
command.shell ? `"${command.file}"` : command.file,
|
|
54
|
+
command.shell ? args.map((arg) => `"${arg}"`) : args,
|
|
55
|
+
{
|
|
56
|
+
shell: command.shell,
|
|
57
|
+
timeout: 20 * 60_000,
|
|
58
|
+
cwd: staging,
|
|
59
|
+
env: {
|
|
60
|
+
...process.env,
|
|
61
|
+
...options.env,
|
|
62
|
+
npm_config_update_notifier: "false",
|
|
63
|
+
npm_config_fund: "false",
|
|
64
|
+
},
|
|
65
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
let output = "";
|
|
69
|
+
|
|
70
|
+
const collect = (chunk: Buffer) => {
|
|
71
|
+
output = (output + chunk.toString()).slice(-16_384);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
child.stdout.on("data", collect);
|
|
75
|
+
child.stderr.on("data", collect);
|
|
76
|
+
child.once("error", reject);
|
|
77
|
+
child.once("close", (code) => {
|
|
78
|
+
if (code === 0) {
|
|
79
|
+
resolve();
|
|
80
|
+
} else {
|
|
81
|
+
reject(new Error(`npm staging failed (${code}): ${output.trim()}`));
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
const manifest = JSON.parse(
|
|
86
|
+
await readFile(`${stagedPackagePath(appRoot)}/package.json`, "utf8"),
|
|
87
|
+
) as { name?: unknown; version?: unknown };
|
|
88
|
+
|
|
89
|
+
if (manifest.name !== "pi-pocket" || manifest.version !== version) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"The staged npm package does not match the requested name and version.",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
// No live files moved. Keep the pending record until the caller records the outcome.
|
|
96
|
+
await rm(staging, { recursive: true, force: true });
|
|
97
|
+
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/server/reload.ts
CHANGED
|
@@ -62,6 +62,7 @@ const ORDER = [
|
|
|
62
62
|
"session-tags.ts",
|
|
63
63
|
"ask-someone.ts",
|
|
64
64
|
"updates.ts",
|
|
65
|
+
"design.ts",
|
|
65
66
|
];
|
|
66
67
|
|
|
67
68
|
/** The built-in modules' names (their files' without `.ts`): a drop-in with one of them is never loaded. */
|
|
@@ -98,6 +99,7 @@ const TITLES: Record<string, string> = {
|
|
|
98
99
|
"session-tags.ts": "Session tags",
|
|
99
100
|
"ask-someone.ts": "Ask someone",
|
|
100
101
|
"updates.ts": "Pi Pocket updates",
|
|
102
|
+
"design.ts": "Design",
|
|
101
103
|
};
|
|
102
104
|
|
|
103
105
|
/**
|
|
@@ -135,6 +137,9 @@ export const BUILT_IN_TOOLS: ReadonlySet<string> = new Set([
|
|
|
135
137
|
"repo_watch",
|
|
136
138
|
"session_watch_pr",
|
|
137
139
|
"ask_someone",
|
|
140
|
+
"design_preview",
|
|
141
|
+
"design_export",
|
|
142
|
+
"design_system",
|
|
138
143
|
]);
|
|
139
144
|
|
|
140
145
|
/** Pi Pocket's own modules, or the owner's from the drop-in folder. */
|
package/src/server/room.ts
CHANGED
|
@@ -602,6 +602,8 @@ export class Room {
|
|
|
602
602
|
id,
|
|
603
603
|
title: meta.title,
|
|
604
604
|
type: meta.type,
|
|
605
|
+
...(meta.group === undefined ? {} : { group: meta.group }),
|
|
606
|
+
...(meta.kind === undefined ? {} : { kind: meta.kind }),
|
|
605
607
|
versions: meta.versions.map((version) => ({
|
|
606
608
|
version: version.version,
|
|
607
609
|
size: version.size,
|
package/src/server/spend.ts
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* the work's usage is committed. The owner can limit a session or a person. Past a limit, new messages are refused,
|
|
5
5
|
* and a run that crosses it is stopped; the model request that crossed it still counts.
|
|
6
6
|
*/
|
|
7
|
+
import { copyJson } from "@earendil-works/chord";
|
|
7
8
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
9
|
+
import type { AssistantMessage, Usage } from "@earendil-works/pi-ai";
|
|
8
10
|
import { type ConversationId, UsageDoc, type UsageState } from "@earendil-works/pi-durable";
|
|
9
11
|
import type { PocketApp } from "./app.ts";
|
|
10
12
|
import type { User } from "./config.ts";
|
|
@@ -76,11 +78,15 @@ export class Spend {
|
|
|
76
78
|
const added = (this.#costs.get(id) ?? 0) - (stored?.counted[String(id)] ?? 0);
|
|
77
79
|
|
|
78
80
|
if (added > 0) {
|
|
79
|
-
this.#owe(id, this
|
|
81
|
+
this.#owe(id, this.payer(id), added);
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
await this.#count();
|
|
86
|
+
const counted = await harness.snapshot(SpendDoc, context);
|
|
87
|
+
|
|
88
|
+
this.#people = { ...counted?.people };
|
|
89
|
+
this.#stopOverLimit();
|
|
84
90
|
}
|
|
85
91
|
|
|
86
92
|
/** Add spend in a conversation to a person, to be stored with the next count. */
|
|
@@ -95,14 +101,15 @@ export class Spend {
|
|
|
95
101
|
* A conversation's usage changed: called from the commit listener, when whom Pi works for is who asked for this
|
|
96
102
|
* work. Storing it waits for a moment.
|
|
97
103
|
*/
|
|
98
|
-
usageChanged(id: ConversationId, usage: UsageState | null): void {
|
|
104
|
+
usageChanged(id: ConversationId, usage: UsageState | null, alreadyPaid = false): void {
|
|
99
105
|
const cost = usageCost(usage ?? undefined);
|
|
100
|
-
|
|
106
|
+
// An auxiliary request pays all its usage in this commit. Do not infer another debt from rounded totals.
|
|
107
|
+
const added = alreadyPaid ? 0 : cost - (this.#costs.get(id) ?? 0);
|
|
101
108
|
|
|
102
109
|
this.#costs.set(id, cost);
|
|
103
110
|
|
|
104
111
|
if (added > 0) {
|
|
105
|
-
this.#owe(id, this
|
|
112
|
+
this.#owe(id, this.payer(id), added);
|
|
106
113
|
}
|
|
107
114
|
|
|
108
115
|
this.#timer ??= setTimeout(() => {
|
|
@@ -113,8 +120,67 @@ export class Spend {
|
|
|
113
120
|
}, COUNT_AFTER_MS);
|
|
114
121
|
}
|
|
115
122
|
|
|
123
|
+
/** Follow stored spend, including auxiliary requests that pay their captured person atomically. */
|
|
124
|
+
countedChanged(stored: { people: Record<string, number> }): void {
|
|
125
|
+
this.#people = { ...stored.people };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Record an auxiliary model request with its captured payer, even if another run has started since. */
|
|
129
|
+
async recordModelUsage(
|
|
130
|
+
id: ConversationId,
|
|
131
|
+
payer: string,
|
|
132
|
+
message: AssistantMessage,
|
|
133
|
+
): Promise<void> {
|
|
134
|
+
await this.#app.harness.commit(async (tx) => {
|
|
135
|
+
const models = (await tx.doc(UsageDoc, id)).models as Record<string, Usage>;
|
|
136
|
+
const key = `${message.provider}/${message.model}`;
|
|
137
|
+
const usage = message.usage;
|
|
138
|
+
const total = Object.hasOwn(models, key) ? models[key] : undefined;
|
|
139
|
+
|
|
140
|
+
if (total === undefined) {
|
|
141
|
+
// The provider/model key contains a slash, so it cannot be a prototype setter.
|
|
142
|
+
models[key] = copyJson(usage, {
|
|
143
|
+
omitUndefinedProperties: true,
|
|
144
|
+
}) as unknown as Usage;
|
|
145
|
+
} else {
|
|
146
|
+
for (const field of [
|
|
147
|
+
"input",
|
|
148
|
+
"output",
|
|
149
|
+
"cacheRead",
|
|
150
|
+
"cacheWrite",
|
|
151
|
+
"totalTokens",
|
|
152
|
+
] as const) {
|
|
153
|
+
total[field] += usage[field];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
for (const field of ["cacheWrite1h", "reasoning"] as const) {
|
|
157
|
+
if (usage[field] !== undefined) {
|
|
158
|
+
total[field] = (total[field] ?? 0) + usage[field];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const field of [
|
|
163
|
+
"input",
|
|
164
|
+
"output",
|
|
165
|
+
"cacheRead",
|
|
166
|
+
"cacheWrite",
|
|
167
|
+
"total",
|
|
168
|
+
] as const) {
|
|
169
|
+
total.cost[field] += usage.cost[field];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Atomic with usage: a restart cannot lose the payer or charge this request twice.
|
|
174
|
+
const spend = await tx.doc(SpendDoc);
|
|
175
|
+
|
|
176
|
+
spend.counted[String(id)] = (spend.counted[String(id)] ?? 0) + usage.cost.total;
|
|
177
|
+
spend.people[payer] = (spend.people[payer] ?? 0) + usage.cost.total;
|
|
178
|
+
}, context);
|
|
179
|
+
this.#stopOverLimit();
|
|
180
|
+
}
|
|
181
|
+
|
|
116
182
|
/** Who pays for a conversation's work: whoever asked for it, else whoever started the session, else the owner. */
|
|
117
|
-
|
|
183
|
+
payer(id: ConversationId): string {
|
|
118
184
|
const app = this.#app;
|
|
119
185
|
|
|
120
186
|
return (
|
|
@@ -135,7 +201,7 @@ export class Spend {
|
|
|
135
201
|
}
|
|
136
202
|
|
|
137
203
|
try {
|
|
138
|
-
|
|
204
|
+
await this.#app.harness.commit(async (tx) => {
|
|
139
205
|
const doc = await tx.doc(SpendDoc);
|
|
140
206
|
|
|
141
207
|
for (const [id, payers] of owed) {
|
|
@@ -144,8 +210,6 @@ export class Spend {
|
|
|
144
210
|
doc.people[payer] = (doc.people[payer] ?? 0) + added;
|
|
145
211
|
}
|
|
146
212
|
}
|
|
147
|
-
|
|
148
|
-
return { ...doc.people };
|
|
149
213
|
}, context);
|
|
150
214
|
} catch (error) {
|
|
151
215
|
// Still owed: the next count stores it.
|
|
@@ -275,7 +339,7 @@ export class Spend {
|
|
|
275
339
|
* Why work Pi starts with nobody asking (a scheduled message, the next round toward a goal) may not start: a limit
|
|
276
340
|
* the session or its payer reached. `userId` pays when given, else whoever asked last.
|
|
277
341
|
*/
|
|
278
|
-
heldBack(id: ConversationId, userId = this
|
|
342
|
+
heldBack(id: ConversationId, userId = this.payer(id)): string | undefined {
|
|
279
343
|
const reached = this.#reached(id, userId);
|
|
280
344
|
|
|
281
345
|
if (reached === undefined) {
|