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
@@ -33,6 +33,7 @@ import {
33
33
  panelsBeside,
34
34
  store,
35
35
  } from "./store.js";
36
+ import { askPipFrom } from "./pip.js";
36
37
  import { fromExtensions, SLOTS, slotsChanged } from "./slots.js";
37
38
  import { setCallSummary } from "./turns.js";
38
39
  import {
@@ -79,7 +80,7 @@ const KINDS = [
79
80
  const OPENS = ["page", "sheet", "panel"];
80
81
  /** The event core sends a module's tabs when the owner changes its settings (`app.setExtensionSettings`). */
81
82
  const SETTINGS_EVENT = "pocket:settings";
82
- /** The most text `pocket.insertText` puts into the message box at once. */
83
+ /** The most text `pocket.insertText` puts into the message box at once, and `pocket.pip.ask` gives PiP. */
83
84
  const INSERT_MAX = 100_000;
84
85
  /** The most of a message's text `pocket.messages` gives, and the most messages in a page. */
85
86
  const MESSAGE_TEXT = 1000;
@@ -1148,6 +1149,25 @@ function pocketFor(record) {
1148
1149
  insertIntoComposer(text, [], { inline: options?.inline === true });
1149
1150
  }
1150
1151
  },
1152
+ pip: Object.freeze({
1153
+ /** Give PiP text as the person's message: in its box to review, or sent after a tap (`askPipFrom`). */
1154
+ ask(text, options) {
1155
+ tick(record);
1156
+
1157
+ if (typeof text !== "string" || text.trim() === "" || text.length > INSERT_MAX) {
1158
+ throw new Error(`pocket.pip.ask: give it text, up to ${INSERT_MAX} characters`);
1159
+ }
1160
+
1161
+ if (!live()) {
1162
+ return Promise.resolve({
1163
+ ok: false,
1164
+ reason: "This version of the module is not loaded",
1165
+ });
1166
+ }
1167
+
1168
+ return askPipFrom(`the extension “${record.title}”`, text, options?.send === true);
1169
+ },
1170
+ }),
1151
1171
  /**
1152
1172
  * Hide or reorder a slot's items, the app's own too, for the people who load this module: what a person chooses
1153
1173
  * in Appearance → Layout wins. Again for the same slot replaces it; `remove` takes it back.
package/web/pip.js CHANGED
@@ -3,12 +3,14 @@
3
3
  // while PiP works, or has something new. The panel watches PiP's conversation through a view of its own (`pipview`
4
4
  // events), so the session behind it stays as it was.
5
5
  import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
6
+ import { followUpText } from "./artifact-bridge.js";
6
7
  import { useBack } from "./back.js";
7
8
  import { boxKey, keyBox } from "./box-keys.js";
8
9
  import { useDragToClose } from "./gestures.js";
9
10
  import { createPipDraft } from "./pip-attachments.js";
10
11
  import { usePipResize } from "./pip-window.js";
11
12
  import { api, attempt, isRow, navigate, notify, openSheet, store, uid } from "./store.js";
13
+ import { spendTap } from "./taps.js";
12
14
  import { memoryAvailable, useMemory } from "./sheets/memory.js";
13
15
  import { itemsOf, says, summarize } from "./turns.js";
14
16
  import {
@@ -220,12 +222,19 @@ export function closePip() {
220
222
 
221
223
  export const togglePip = () => (store.state.pipOpen ? closePip() : openPip());
222
224
 
223
- /** Send PiP a message from anywhere (`/pip …`), and open the panel to show what it says. */
225
+ /** Put `text` in PiP's box, after what is there, and open the panel. */
226
+ function draftPip(text) {
227
+ const draft = localStorage.getItem(DRAFT_KEY)?.trim();
228
+
229
+ openPip(draft ? `${draft}\n\n${text}` : text);
230
+ }
231
+
232
+ /** Send PiP a message from anywhere (`/pip …`), and open the panel to show what it says; true when it went. */
224
233
  export async function askPip(text) {
225
234
  if (!pipAvailable()) {
226
235
  notify("error", "PiP is not on here.");
227
236
 
228
- return;
237
+ return false;
229
238
  }
230
239
 
231
240
  const account = store.state.me?.id;
@@ -234,7 +243,7 @@ export async function askPip(text) {
234
243
  const pip = store.state.pip ?? (await opening);
235
244
 
236
245
  if (account !== store.state.me?.id) {
237
- return;
246
+ return false;
238
247
  }
239
248
 
240
249
  if (!pip) {
@@ -242,15 +251,43 @@ export async function askPip(text) {
242
251
  }
243
252
 
244
253
  // Not sent: the words wait in PiP's box rather than be lost (`send` said why), after what was there already.
245
- if (!pip || !(await send(pip.id, text, false))) {
246
- if (account !== store.state.me?.id || (pip && store.state.pip?.id !== pip.id)) {
247
- return;
248
- }
254
+ if (pip && (await send(pip.id, text, false))) {
255
+ return true;
256
+ }
257
+
258
+ if (account === store.state.me?.id && (!pip || store.state.pip?.id === pip.id)) {
259
+ draftPip(text);
260
+ }
249
261
 
250
- const draft = localStorage.getItem(DRAFT_KEY)?.trim();
262
+ return false;
263
+ }
264
+
265
+ const PIP_UNAVAILABLE = "PiP is not available";
266
+
267
+ /**
268
+ * What `pocket.pip.ask` does for the browser module `source` names: put its text in PiP's box for the person to read,
269
+ * edit, and send; or, with `send` and only just after a tap or a key press, send it as the person's own message. The
270
+ * text starts with where it came from, as an artifact's follow-up does, so it never reads as a command. The answer says
271
+ * why nothing happened, or whether the message went (`sent` is false when it waits in the box).
272
+ */
273
+ export async function askPipFrom(source, text, send) {
274
+ if (!pipAvailable()) {
275
+ return { ok: false, reason: PIP_UNAVAILABLE };
276
+ }
277
+
278
+ const words = followUpText(text.trim(), undefined, source);
279
+
280
+ if (!send) {
281
+ draftPip(words);
251
282
 
252
- openPip(draft ? `${draft}\n\n${text}` : text);
283
+ return { ok: true, sent: false };
253
284
  }
285
+
286
+ if (!spendTap()) {
287
+ return { ok: false, reason: "Sending needs a click or a key press just before" };
288
+ }
289
+
290
+ return { ok: true, sent: await askPip(words) };
254
291
  }
255
292
 
256
293
  /** Send PiP a message; false when it did not go (a notice says why). PiP made just now is waited for. */
package/web/sessions.js CHANGED
@@ -1605,7 +1605,7 @@ function RailTiles() {
1605
1605
  ${order.map(
1606
1606
  (session, index) => html`<button
1607
1607
  key=${session.id}
1608
- class=${`ws ${session.id === conversationId ? "active" : ""} ${session.busy ? "busy" : ""} ${session.waiting ? "waiting" : ""}`}
1608
+ class=${`ws ${session.id === conversationId ? "active" : ""} ${session.busy ? "busy" : ""} ${session.waiting ? "waiting" : ""} ${session.background > 0 ? "has-background" : ""}`}
1609
1609
  style=${`--i:${index}`}
1610
1610
  title=${`${session.title ?? "New session"}${index < 9 ? ` (Alt+${index + 1})` : ""}`}
1611
1611
  aria-label=${`${session.title ?? "New session"}${session.waiting ? ", needs you" : session.busy ? ", working" : session.background > 0 ? ", running commands in the background" : ""}`}
@@ -782,6 +782,16 @@ export function AppearanceSheet() {
782
782
  onChange=${() => setPrefs({ showThinking: !p.showThinking })}
783
783
  />
784
784
  <//>
785
+ <${SettingRow}
786
+ title="Keep activity folded"
787
+ detail="Pi's thinking and tool calls stay on their one line until you open them, also while it works and in the newest turn"
788
+ >
789
+ <${Switch}
790
+ on=${p.foldActivity}
791
+ label="Keep activity folded"
792
+ onChange=${() => setPrefs({ foldActivity: !p.foldActivity })}
793
+ />
794
+ <//>
785
795
  </div>
786
796
  <div class="label">Status line</div>
787
797
  <div class="sheet-list">
@@ -1,6 +1,7 @@
1
1
  // Artifacts and images, each in a viewer of its own; and sharing an artifact to anyone with a link.
2
2
  import { useEffect, useRef, useState } from "preact/hooks";
3
3
  import { useArtifactBridge } from "../artifact-bridge.js";
4
+ import { browserModuleState, openExtPanel } from "../extension-ui.js";
4
5
  import { api, attempt, closeSheet, notify, openSheet, store } from "../store.js";
5
6
  import { modeOf, paletteOf, themeVars } from "../theme.js";
6
7
  import { copyText, formatBytes, formatWhen, html, Icon, Loader, Sheet, timeAgo } from "../ui.js";
@@ -81,6 +82,18 @@ export function ArtifactViewer({ id, version }) {
81
82
  const src = `/a/${view.conversation.id}/${encodeURIComponent(id)}/${shown ?? "latest"}${theme}`;
82
83
  // A published page is out of the reach of an invite to one session, so only those who see every session publish.
83
84
  const shareable = me?.role !== "viewer" && me?.sessions === undefined && me?.role !== undefined;
85
+ // The Design canvas (a built-in module) shows an HTML page with its versions, comments, and tweaks.
86
+ const designable =
87
+ artifact?.type === "html" && browserModuleState("design")?.status === "ready";
88
+
89
+ const openInDesign = () => {
90
+ dispatchEvent(
91
+ new CustomEvent("pocket:design-open", {
92
+ detail: { artifact: id, version: shown === latest ? null : shown },
93
+ }),
94
+ );
95
+ openExtPanel("design", "design");
96
+ };
84
97
 
85
98
  return html`<div class="viewer">
86
99
  <header class="viewer-head">
@@ -109,6 +122,16 @@ export function ArtifactViewer({ id, version }) {
109
122
  <button class="icon-button" title="Reload" onClick=${() => setNonce(nonce + 1)}>
110
123
  ↻
111
124
  </button>
125
+ ${
126
+ designable &&
127
+ html`<button
128
+ class="button small"
129
+ title="Open it in the Design canvas"
130
+ onClick=${openInDesign}
131
+ >
132
+ Open in Design
133
+ </button>`
134
+ }
112
135
  ${
113
136
  shareable &&
114
137
  artifact &&
@@ -182,6 +205,8 @@ function ShareRow({ share, base, fresh, canEnd, onRevoke }) {
182
205
  export function ArtifactShareSheet({ id, version }) {
183
206
  const { view, me } = store.state;
184
207
  const artifact = view.artifacts.find((each) => each.id === id);
208
+ // Opened from the Design canvas, closing it goes back there, not to the viewer.
209
+ const fromDesign = store.state.sheet?.back === "design";
185
210
  const [list, setList] = useState(null);
186
211
  const [base, setBase] = useState(null);
187
212
  const [life, setLife] = useState(0);
@@ -240,7 +265,7 @@ export function ArtifactShareSheet({ id, version }) {
240
265
 
241
266
  return html`<${Sheet}
242
267
  title=${`Share ${artifact?.title ?? id}`}
243
- onClose=${() => openSheet({ type: "viewer", id, version })}
268
+ onClose=${() => (fromDesign ? closeSheet() : openSheet({ type: "viewer", id, version }))}
244
269
  wide=${true}
245
270
  >
246
271
  <p class="muted small">
package/web/style.css CHANGED
@@ -4364,6 +4364,17 @@ pre.diff .del {
4364
4364
  background: linear-gradient(90deg, var(--accent), var(--spark));
4365
4365
  }
4366
4366
 
4367
+ /* Commands Pi runs in the background, with the session not working: the same underline, dashed and quiet. */
4368
+ .ws.has-background:not(.busy)::after {
4369
+ content: "";
4370
+ position: absolute;
4371
+ left: 10px;
4372
+ right: 10px;
4373
+ bottom: 3px;
4374
+ height: 2px;
4375
+ background: repeating-linear-gradient(90deg, var(--muted) 0 3px, transparent 3px 6px);
4376
+ }
4377
+
4367
4378
  .ws .unread-dot {
4368
4379
  position: absolute;
4369
4380
  top: 4px;
package/web/taps.js ADDED
@@ -0,0 +1,26 @@
1
+ // Whether a person just tapped the page or pressed a key. A script cannot make such an event (`isTrusted`), so an action
2
+ // that must come from a person can ask for one. This is the rule that the frame of an artifact keeps for its follow-ups
3
+ // (`ARTIFACT_BRIDGE` in `src/server/http/artifacts.ts`): a tap serves for 5 seconds, and for one action.
4
+
5
+ const TAP_MS = 5000;
6
+ let tapped = -Infinity;
7
+
8
+ const tap = (event) => {
9
+ if (event.isTrusted) {
10
+ tapped = performance.now();
11
+ }
12
+ };
13
+
14
+ // One event for each tap: a mouse's press, a finger's or pen's lift, a key's press but not its repeats.
15
+ addEventListener("keydown", (event) => event.key !== "Escape" && !event.repeat && tap(event), true);
16
+ addEventListener("pointerdown", (event) => event.pointerType === "mouse" && tap(event), true);
17
+ addEventListener("pointerup", (event) => event.pointerType !== "mouse" && tap(event), true);
18
+
19
+ /** True when a person tapped or pressed a key in the last 5 seconds, and not for an action already; uses that tap. */
20
+ export function spendTap() {
21
+ const fresh = performance.now() - tapped <= TAP_MS;
22
+
23
+ tapped = -Infinity;
24
+
25
+ return fresh;
26
+ }
package/web/theme.js CHANGED
@@ -69,6 +69,12 @@ const DEFAULTS = {
69
69
  speed: false,
70
70
  /** Pi's current thought above the live line, and its finished thoughts open. Off until turned on. */
71
71
  showThinking: false,
72
+ /**
73
+ * Pi's thinking and tool calls stay folded into their one line until opened, also while they run and in the newest
74
+ * turn; off, the newest turn's groups open as they come. A group opened or closed by hand keeps that choice, and a
75
+ * thought alone in its group still follows Show thinking. Off until turned on.
76
+ */
77
+ foldActivity: false,
72
78
  /** This browser's own layout of each slot (`slots.js`): `{ [slot]: { order: [ids], hidden: { [id]: boolean } } }`. */
73
79
  layout: {},
74
80
  };
package/web/transcript.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // into one line that opens into a timeline), then Pi's reply, any approval, and the live line while Pi works.
3
3
  import { Component } from "preact";
4
4
  import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
5
+ import { activityOpen } from "./activity-fold.js";
5
6
  import { initials, personColor } from "./avatar.js";
6
7
  import { routeSliding } from "./back.js";
7
8
  import { browserAvailable, setBrowserOpen } from "./browser.js";
@@ -69,7 +70,7 @@ const NEXT_REPORT = /\n\n(?=\[subagent \S+ (?:answered|failed))/;
69
70
 
70
71
  /**
71
72
  * A background task's message to Pi (`background.ts`): its head line, its command, and the end of its output fenced.
72
- * Its time is short ("2m 4s"), which keeps a long line that is not one quick to turn down.
73
+ * Its time is short ("2m 04s"), which keeps a long line that is not one quick to turn down.
73
74
  */
74
75
  const BACKGROUND =
75
76
  /^\[background\] Task (\S+) (.+?) after ([^.\n]{1,20})\.\nCommand: (.*)\n([\s\S]*)$/;
@@ -1249,7 +1250,8 @@ function GroupTime({ id, start, end, live }) {
1249
1250
  /**
1250
1251
  * Pi's thinking and tool calls between two of its words, as one line: what they did, and how long they took. It opens
1251
1252
  * into a timeline, a line per call, each drawn again only when what it shows changes. Open while it is live or in the
1252
- * newest turn, closed before, unless opened or closed by hand.
1253
+ * newest turn, closed before, unless opened or closed by hand; Appearance's Keep activity folded starts every group
1254
+ * closed.
1253
1255
  */
1254
1256
  function ActivityGroup({
1255
1257
  item,
@@ -1277,20 +1279,25 @@ function ActivityGroup({
1277
1279
  keep(wholeGroups, id, true);
1278
1280
  }
1279
1281
 
1280
- const chosen = openedGroups.get(id);
1281
1282
  const all = item.before.length === 0 ? item.steps : [...item.before, ...item.steps];
1282
1283
  // A thought alone is the group's own line, not a step under a line that says the same: it opens to its words,
1283
1284
  // which stay folded until asked for, as a thought on the timeline does.
1284
1285
  const lone = all.length === 1 && all[0].block.type === "thinking" ? all[0] : null;
1285
1286
  // Show thinking on: thoughts show their words once written.
1286
1287
  const reading = prefs().showThinking === true;
1287
- // A thought whose words have not come yet (`partial`, as a session opens) opens once they have.
1288
- const open =
1289
- !lone?.block.partial &&
1290
- (chosen ??
1291
- (lone
1292
- ? reading && !lone.streaming && Boolean(lone.block.text?.trim())
1293
- : live || newest));
1288
+ const open = activityOpen({
1289
+ chosen: openedGroups.get(id),
1290
+ lone: lone && {
1291
+ partial: lone.block.partial,
1292
+ streaming: lone.streaming,
1293
+ text: lone.block.text,
1294
+ },
1295
+ live,
1296
+ newest,
1297
+ reading,
1298
+ // Appearance's Keep activity folded.
1299
+ fold: prefs().foldActivity === true,
1300
+ });
1294
1301
  // Calls that failed, said on the line too: folded, "edited 1" alone would read as done. A call someone denied did
1295
1302
  // not fail, nor a script stopped by a denial: its line says who denied it.
1296
1303
  const failed = all.filter((step) => {
@@ -2368,6 +2375,8 @@ function Items({ items, results, slots, approvals, busy, past = false, conversat
2368
2375
  conversationId,
2369
2376
  after,
2370
2377
  prefs().showThinking === true,
2378
+ // Keep activity folded: a group drawn before the switch moved opens or folds with it.
2379
+ prefs().foldActivity === true,
2371
2380
  ];
2372
2381
 
2373
2382
  // The calls before the rows shown count in the group's line: their results, for its time.
@@ -1,30 +0,0 @@
1
- /**
2
- * `/pub/:token`: an artifact published to anyone with the address, with no sign-in. The token is the whole of the access;
3
- * what it shows is a version pinned when it was published, or the artifact's newest (`share.ts`).
4
- */
5
- import type { IncomingMessage, ServerResponse } from "node:http";
6
- import type { PocketApp } from "../app.ts";
7
- import { HttpError } from "../errors.ts";
8
- import { artifactResponse } from "./artifacts.ts";
9
-
10
- export async function publicArtifact(
11
- app: PocketApp,
12
- request: IncomingMessage,
13
- response: ServerResponse,
14
- token: string | undefined,
15
- query = new URLSearchParams(),
16
- ): Promise<void> {
17
- if (request.method !== "GET" && request.method !== "HEAD") {
18
- response.setHeader("allow", "GET, HEAD");
19
-
20
- throw new HttpError(405, "Published artifacts are only read");
21
- }
22
-
23
- const published = token === undefined ? undefined : await app.shares.open(token);
24
-
25
- if (published === undefined) {
26
- throw new HttpError(404, "No such page");
27
- }
28
-
29
- await artifactResponse(response, published.found, query, false);
30
- }