samagotchi 0.179.0 → 0.181.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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +76 -1
  3. data/bin/chi +10 -0
  4. data/docs/architecture.md +1 -1
  5. data/docs/bundles.md +39 -3
  6. data/docs/cli.md +16 -9
  7. data/docs/configuration.md +25 -5
  8. data/docs/context.md +7 -0
  9. data/docs/internals/anthropic.md +4 -3
  10. data/docs/internals/background-tasks.md +4 -1
  11. data/docs/internals/block-renderers.md +11 -7
  12. data/docs/internals/bundles.md +12 -10
  13. data/docs/internals/objectives.md +9 -4
  14. data/docs/internals/web-frontend.md +127 -6
  15. data/docs/internals/working-set.md +24 -5
  16. data/docs/memory.md +3 -0
  17. data/docs/plugins.md +17 -6
  18. data/docs/sessions.md +1 -1
  19. data/docs/testing.md +5 -1
  20. data/lib/samagotchi/block_renderers.rb +22 -3
  21. data/lib/samagotchi/bundle_command.rb +9 -2
  22. data/lib/samagotchi/bundle_needs.rb +1 -1
  23. data/lib/samagotchi/bundle_settings_check.rb +5 -2
  24. data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
  25. data/lib/samagotchi/bundles/known-names/plugin.rb +4 -3
  26. data/lib/samagotchi/bundles/system/config_modification_protocol.md +3 -2
  27. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  28. data/lib/samagotchi/client.rb +8 -1
  29. data/lib/samagotchi/config.rb +6 -1
  30. data/lib/samagotchi/context_providers.rb +6 -8
  31. data/lib/samagotchi/core_notice.rb +44 -0
  32. data/lib/samagotchi/host_address.rb +7 -0
  33. data/lib/samagotchi/host_config.rb +8 -5
  34. data/lib/samagotchi/host_registry.rb +26 -8
  35. data/lib/samagotchi/installed_bundles/dir.rb +17 -55
  36. data/lib/samagotchi/installed_bundles/standing.rb +23 -3
  37. data/lib/samagotchi/installed_bundles.rb +26 -11
  38. data/lib/samagotchi/kernel_loop.rb +3 -2
  39. data/lib/samagotchi/llm/adapter_common.rb +3 -2
  40. data/lib/samagotchi/llm/adapters.rb +2 -1
  41. data/lib/samagotchi/llm/anthropic_assembly.rb +5 -0
  42. data/lib/samagotchi/llm/anthropic_messages.rb +6 -2
  43. data/lib/samagotchi/llm/chat_loop.rb +6 -5
  44. data/lib/samagotchi/llm/errors.rb +41 -3
  45. data/lib/samagotchi/llm/http.rb +85 -22
  46. data/lib/samagotchi/llm/openai_chat.rb +20 -3
  47. data/lib/samagotchi/memory_bundle/status.rb +10 -6
  48. data/lib/samagotchi/objectives.rb +7 -2
  49. data/lib/samagotchi/owner_lock.rb +2 -0
  50. data/lib/samagotchi/plugin/api.rb +4 -1
  51. data/lib/samagotchi/plugin/context.rb +9 -0
  52. data/lib/samagotchi/plugin/peer.rb +32 -15
  53. data/lib/samagotchi/plugin/tool_spec.rb +13 -2
  54. data/lib/samagotchi/row_refs.rb +14 -3
  55. data/lib/samagotchi/session_chain/delete.rb +6 -6
  56. data/lib/samagotchi/session_config.rb +15 -9
  57. data/lib/samagotchi/session_manager/removal.rb +2 -2
  58. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  59. data/lib/samagotchi/terminal_ui/formatting.rb +13 -3
  60. data/lib/samagotchi/terminal_ui/join_replay.rb +1 -1
  61. data/lib/samagotchi/tool_ids.rb +19 -0
  62. data/lib/samagotchi/tool_runner.rb +8 -4
  63. data/lib/samagotchi/tools/registry.rb +3 -2
  64. data/lib/samagotchi/tools/task_runtime.rb +46 -3
  65. data/lib/samagotchi/turn_stages/objectives.rb +10 -5
  66. data/lib/samagotchi/version.rb +1 -1
  67. data/lib/samagotchi/web/public/app.js +22 -14
  68. data/lib/samagotchi/web/public/block_host.js +20 -20
  69. data/lib/samagotchi/web/public/block_renderers.css +5 -5
  70. data/lib/samagotchi/web/public/context_chips.js +183 -49
  71. data/lib/samagotchi/web/public/history_shape.js +5 -3
  72. data/lib/samagotchi/web/public/images.js +1 -1
  73. data/lib/samagotchi/web/public/index.html +90 -5
  74. data/lib/samagotchi/web/public/objectives_strip.js +16 -6
  75. data/lib/samagotchi/web/public/pan_zoom.js +121 -0
  76. data/lib/samagotchi/web/public/peer_chip.js +171 -0
  77. data/lib/samagotchi/web/public/peer_data.js +131 -0
  78. data/lib/samagotchi/web/public/peer_exchange.js +83 -0
  79. data/lib/samagotchi/web/public/peer_links.js +161 -0
  80. data/lib/samagotchi/web/public/peer_note.js +49 -0
  81. data/lib/samagotchi/web/public/picture_viewer.js +304 -0
  82. data/lib/samagotchi/web/public/popover_modal.js +3 -2
  83. data/lib/samagotchi/web/public/session_view.js +10 -1
  84. data/lib/samagotchi/web/public/stream_handlers.js +17 -4
  85. data/lib/samagotchi/web/public/turn_html.js +11 -2
  86. data/lib/samagotchi/web/public/working_set.js +9 -3
  87. data/lib/samagotchi/web/public/working_set_view.js +28 -11
  88. data/lib/samagotchi/web/public/ws_cards.js +36 -10
  89. data/lib/samagotchi/web/public/ws_markdown.js +129 -0
  90. data/lib/samagotchi/web/public/ws_order.js +48 -0
  91. data/lib/samagotchi/web/public/ws_peek.js +22 -5
  92. data/lib/samagotchi/web/public/ws_preview.css +41 -1
  93. data/lib/samagotchi/web/public/ws_reader.js +35 -6
  94. data/lib/samagotchi/web/public/ws_sections.js +24 -15
  95. metadata +11 -1
@@ -37,9 +37,9 @@ const NOT_AN_IMAGE = "the renderer's SVG can't be shown as an image";
37
37
  // this tall and scrolls sideways instead.
38
38
  const MIN_SHOWN_HEIGHT = 120;
39
39
 
40
- // The zoom button's words: the picture fitted to the column, or at its natural size.
41
- const ZOOM_IN = "Show at full size";
42
- const ZOOM_OUT = "Fit to the column";
40
+ // The zoom button's words: it opens the picture in the viewer (picture_viewer.js),
41
+ // as a click on the picture does.
42
+ const OPEN_IN_VIEWER = "Open in the viewer";
43
43
 
44
44
  const ZOOM_ICON = html`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true"><circle cx="7" cy="7" r="4.25" fill="none" stroke="currentColor" stroke-width="1.4"/><path d="m10.2 10.2 3.3 3.3" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/></svg>`;
45
45
  const ICON = html`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true"><path d="M5.5 4 2 8l3.5 4M10.5 4 14 8l-3.5 4" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
@@ -133,10 +133,12 @@ function guardedStorage(get) {
133
133
 
134
134
  // The drawer of a bundle's renderers (block_watch.js has the contract): one
135
135
  // frame per language, waiting for /api/block_renderers to say who draws what.
136
- // @param roots the turn roots, for the view held and the zoom's listeners
136
+ // @param roots the turn roots, for the view held and the viewer's click
137
+ // @param openPicture (picture, {opener}) => shows a picture full screen (app.js's
138
+ // picture viewer); null (an older app.js): a click on a picture does nothing
137
139
  // The rest are createBlockHost's.
138
140
  export function createFrameDrawer({
139
- roots, listRenderers = fetchRendererList, fetchFile = fetchRendererFile, makeFrame = createBlockFrame,
141
+ roots, openPicture = null, listRenderers = fetchRendererList, fetchFile = fetchRendererFile, makeFrame = createBlockFrame,
140
142
  decodeImage = async (url) => { const img = new win.Image(); img.src = url; await img.decode(); return { width: img.naturalWidth, height: img.naturalHeight }; },
141
143
  decodeTimeoutMs = DECODE_TIMEOUT_MS, theme = currentTheme,
142
144
  doc = document, win = window, storage = guardedStorage(() => globalThis.sessionStorage),
@@ -224,8 +226,9 @@ export function createFrameDrawer({
224
226
  }
225
227
 
226
228
  // The picture's name says where its words are: the source <pre> is hidden, so a
227
- // screen reader reaches it through the toggle. The zoom is a button too, for the
228
- // keyboard (a click on the picture does the same); it shows on keyboard focus only.
229
+ // screen reader reaches it through the toggle. Opening it in the viewer is a
230
+ // button too, for the keyboard (a click on the picture does the same); it shows
231
+ // on keyboard focus only.
229
232
  function makeFigure(picture, themeName, lang) {
230
233
  const figure = doc.createElement("figure");
231
234
  figure.className = DRAWN;
@@ -239,8 +242,9 @@ export function createFrameDrawer({
239
242
  scroll.append(img);
240
243
  const zoom = button("block-zoom", ZOOM_ICON);
241
244
  const toggle = button("block-toggle", ICON);
245
+ zoom.setAttribute("aria-label", OPEN_IN_VIEWER);
246
+ zoom.title = OPEN_IN_VIEWER;
242
247
  figure.append(scroll, zoom, toggle);
243
- setZoomed(figure, false);
244
248
  return figure;
245
249
  }
246
250
 
@@ -252,15 +256,6 @@ export function createFrameDrawer({
252
256
  return el;
253
257
  }
254
258
 
255
- function setZoomed(figure, on) {
256
- figure.classList.toggle("natural", on);
257
- const zoom = figure.querySelector(":scope > .block-zoom");
258
- if (!zoom) return;
259
- zoom.setAttribute("aria-pressed", on ? "true" : "false");
260
- zoom.setAttribute("aria-label", on ? ZOOM_OUT : ZOOM_IN);
261
- zoom.title = on ? ZOOM_OUT : ZOOM_IN;
262
- }
263
-
264
259
  function showPicture(img, { url, width, height }) {
265
260
  if (width && height) {
266
261
  img.setAttribute("width", width);
@@ -368,11 +363,15 @@ export function createFrameDrawer({
368
363
  root.addEventListener("mousedown", (e) => {
369
364
  if (e.target.closest?.(".block-zoom")) e.preventDefault();
370
365
  });
371
- // Delegated, so a Revise clone's work too.
366
+ // Delegated, so a Revise clone's work too: the picture in the viewer, at its
367
+ // size from the viewBox (showPicture's attributes), the focus back on the button.
372
368
  root.addEventListener("click", (e) => {
373
- if (!e.target.closest?.(".block-zoom, .block-img")) return;
369
+ if (!openPicture || !e.target.closest?.(".block-zoom, .block-img")) return;
374
370
  const figure = e.target.closest(`.${DRAWN}`);
375
- setZoomed(figure, !figure.classList.contains("natural"));
371
+ const img = figure?.querySelector(".block-img");
372
+ if (!img) return;
373
+ const size = (name) => Number(img.getAttribute(name)) || null;
374
+ openPicture({ url: img.src, width: size("width"), height: size("height"), alt: img.alt }, { opener: figure.querySelector(":scope > .block-zoom") });
376
375
  });
377
376
  }
378
377
 
@@ -392,6 +391,7 @@ export function createFrameDrawer({
392
391
  // @param sessionId () => the open session's id: the frames go on a change, to
393
392
  // limit what they hold (docs/internals/block-renderers.md, residual risk)
394
393
  // @param drawers core drawers, asked before the bundle renderers
394
+ // +openPicture+: shows a drawn picture full screen (createFrameDrawer's).
395
395
  // The rest are seams for the specs.
396
396
  // +renderers+: false for a viewer without the bundles' renderers (viewer.js):
397
397
  // the core +drawers+ only, and GET /api/block_renderers is never asked.
@@ -1,12 +1,11 @@
1
- /* A drawn fenced block (block_host.js): the picture scales to the column, a click shows its natural size in a scroller; the toggle shows the source, in the block's place. In a .code-wrap the copy button sits top-right, so the toggle sits left of it. The zoom button, the keyboard's way to the natural size, shows on keyboard focus only, left of the toggle. */
1
+ /* A drawn fenced block (block_host.js): the picture scales to the column, a click opens it in the picture viewer (picture_viewer.js); the toggle shows the source, in the block's place. In a .code-wrap the copy button sits top-right, so the toggle sits left of it. The zoom button, the keyboard's way to the viewer, shows on keyboard focus only, left of the toggle. */
2
2
  .block-drawn{position:relative;margin:0 0 12px;white-space:normal}
3
3
  .code-wrap>.block-drawn{margin:0}
4
4
  .code-wrap>.block-drawn.source{height:0}
5
5
  .block-drawn:last-child{margin-bottom:0}
6
6
  .block-scroll{overflow-x:auto;padding:4px 0}
7
- /* max-height and the natural max-width are block_host.js MAX_PICTURE_SIDE: a backstop, so no parsing or decoding surprise reaches the browser's 33M px. */
7
+ /* max-height is block_host.js MAX_PICTURE_SIDE: a backstop, so no parsing or decoding surprise reaches the browser's 33M px. A very wide picture keeps its min-width (block_host.js minShownWidth) and scrolls sideways. */
8
8
  .block-img{display:block;max-width:100%;max-height:50000px;object-fit:contain;height:auto;margin:0 auto;cursor:zoom-in}
9
- .block-drawn.natural .block-img{max-width:50000px;margin:0;cursor:zoom-out}
10
9
  .block-drawn.source .block-scroll{display:none}
11
10
  /* A chi-ui widget (inline_widgets.js): its host holds the shadow root, whose own sheet is widgets.css; the source hides it as the picture. */
12
11
  .block-drawn.source>.widget-host{display:none}
@@ -17,8 +16,9 @@
17
16
  .block-drawn.source>.block-zoom{display:none}
18
17
  .block-zoom:not(:focus-visible){pointer-events:none}
19
18
  .block-toggle svg,.block-zoom svg{display:block}
20
- .block-drawn:hover>.block-toggle,.code-wrap:hover>.block-drawn>.block-toggle,.block-toggle:focus-visible,.block-zoom:focus-visible,.block-drawn.source>.block-toggle{opacity:1}
21
- .block-toggle:hover:not(:disabled),.block-zoom:hover:not(:disabled){filter:none}@media(hover:hover){.block-toggle:hover:not(:disabled),.block-zoom:hover:not(:disabled){color:var(--text-bright)}}
19
+ .block-toggle:focus-visible,.block-zoom:focus-visible,.block-drawn.source>.block-toggle{opacity:1}
20
+ @media(hover:hover){.block-drawn:hover>.block-toggle,.code-wrap:hover>.block-drawn>.block-toggle{opacity:1}.block-toggle:hover:not(:disabled),.block-zoom:hover:not(:disabled){filter:none;color:var(--text-bright)}}
21
+ @media(hover:none){.block-drawn>.block-toggle{opacity:.5}.block-drawn>.block-toggle:active,.block-drawn>.block-toggle:focus-visible{opacity:1}}
22
22
  .block-drawn.source:not(:last-child){margin:0}
23
23
  /* Its words are the attribute's, so a bubble's text stays the answer's (block_host.js fail). */
24
24
  .block-note{margin:4px 0 12px;font-size:12px;color:var(--text-3);white-space:normal}
@@ -8,10 +8,11 @@
8
8
 
9
9
  import { html, nothing, render } from "./vendor/lit-html.js";
10
10
  import { keepFocus } from "./keep_focus.js";
11
- import { popoverPlace } from "./chip_popover.js";
11
+ import { popoverPlace, SHEET_MAX, sheetPlace } from "./chip_popover.js";
12
12
  import { focusBack, modalDialog } from "./popover_modal.js";
13
13
  import { cantText } from "./format.js";
14
14
  import { createLatest } from "./latest.js";
15
+ import { peerChipModel, peerGroups, peerKey, peerPopoverTemplate, peerShown } from "./peer_chip.js";
15
16
 
16
17
  // "now", "3m", "2h", "4d" since an ISO time; "" for none.
17
18
  export function ageText(iso, now = Date.now()) {
@@ -45,12 +46,14 @@ export function chipModel(rows, now = Date.now()) {
45
46
  }
46
47
 
47
48
  // The chips (a lit-html template; attributes after a bound one are bound
48
- // too, so they keep their order). open: the chip whose popover is open (a
49
- // source's name).
49
+ // too, so they keep their order). open: the chip whose popover is open (its
50
+ // key: a source's name, or a peer chip's "peer:<name>", peer_chip.js). A
51
+ // peer chip (peerChipModel) is drawn as one: class "peer", its dot when it
52
+ // says answered.
50
53
  export function chipsTemplate(chips, { open = null } = {}) {
51
- return html`${chips.map((chip) => html`<button type="button" class=${`ctx-chip ${chip.state}${chip.name === open ? " open" : ""}`}
54
+ return html`${chips.map((chip) => html`<button type="button" class=${`ctx-chip${chip.peer ? " peer" : ""} ${chip.state}${chip.name === open ? " open" : ""}`}
52
55
  data-name=${chip.name} title=${chip.title} aria-haspopup=${"dialog"} aria-expanded=${chip.name === open ? "true" : "false"}>${
53
- chip.state === "unread" ? html`<span class="ctx-dot" aria-hidden="true"></span>` : nothing}${chip.label}</button>`)}`;
56
+ chip.state === "unread" || chip.dot ? html`<span class="ctx-dot" aria-hidden="true"></span>` : nothing}${chip.label}</button>`)}`;
54
57
  }
55
58
 
56
59
  // The popover's inside for one row (GET …/context's); +text+ the row's
@@ -87,35 +90,67 @@ export function popoverTemplate(row, { text = null, now = Date.now() } = {}) {
87
90
  // so focus stays. The
88
91
  // popover behaves as every popover does (popover_modal.js): a modal dialog, its chip's
89
92
  // aria-expanded, Esc closes it and focus goes back to the chip.
90
- export function createContextChips({ el, api, toast = () => {}, doc = document, win = window }) {
93
+ // A paired peer's two sources are one chip (peer_chip.js), drawn first:
94
+ // +peers+() the peers model (peer_data.js createPeers; null: none, the
95
+ // chips then read no exchanges), read again when a session's chips load or
96
+ // a peer appears among them, and the chips redrawn on its change
97
+ // (peersChanged); +peerActions+: {status(name): the status card's command
98
+ // line ("/claude") when the session has it, else null; run(line); drawn(id):
99
+ // whether that send's row is in the conversation; jump(id): to it}.
100
+ // +focusAway+(): where focus goes when a detach or Unpair took the last chip
101
+ // (the composer).
102
+ export function createContextChips({ el, api, toast = () => {}, peers = () => null, peerActions = {},
103
+ focusAway = () => doc.getElementById("prompt")?.focus?.({ preventScroll: true }),
104
+ doc = document, win = window }) {
91
105
  let sessionId = null;
92
106
  let rows = [];
93
- let openName = null;
107
+ // The chip whose popover is open: its key (a source's name, or "peer:<name>").
108
+ let openKey = null;
94
109
  // Only the newest list read for the open session is drawn (latest.js).
95
110
  const latest = createLatest(() => sessionId);
96
- // The open row's text once read (view text).
111
+ // The open popover's text once read (view text; a peer's: its outbox's).
97
112
  let text = null;
113
+ // The peers the last read named (a new one reads the peers model again).
114
+ let peerNames = "";
98
115
  const pop = doc.createElement("div");
99
116
  pop.id = "contextPopover";
100
117
  pop.className = "ctx-popover";
101
118
  pop.hidden = true;
102
119
  doc.body.appendChild(pop);
103
- modalDialog(pop, { isOpen: () => openName !== null, doc });
120
+ modalDialog(pop, { isOpen: () => openKey !== null, doc });
121
+
122
+ const groups = () => peerGroups(rows).peers.filter(peerShown);
123
+ const groupOf = (key) => groups().find((group) => group.key === key) || null;
124
+ const exchangesOf = (name) => peers()?.peer?.(name)?.exchanges || [];
125
+ const rowOf = (name) => rows.find((r) => r.name === name) || null;
126
+
127
+ function chips(now = Date.now()) {
128
+ const peerChips = groups().map((group) => peerChipModel(group, exchangesOf(group.name), now));
129
+ return [...peerChips, ...chipModel(peerGroups(rows).rest, now)];
130
+ }
104
131
 
105
132
  function drawChips() {
106
- const chips = chipModel(rows);
133
+ const drawn = chips();
107
134
  // A source inserted before the focused chip: the focus stays on its source.
108
- keepFocus(el, "data-name", () => render(chipsTemplate(chips, { open: openName }), el));
109
- el.hidden = chips.length === 0;
135
+ keepFocus(el, "data-name", () => render(chipsTemplate(drawn, { open: openKey }), el));
136
+ el.hidden = drawn.length === 0;
110
137
  }
111
138
 
112
- // The chips, and the open popover closed when what it is for has gone.
139
+ // The chips, and the open popover closed when its chip isn't drawn any more.
113
140
  function draw() {
114
- if (openName && !chipModel(rows).some((chip) => chip.name === openName)) close();
141
+ if (openKey && !chips().some((chip) => chip.name === openKey)) close();
115
142
  else drawChips();
116
143
  }
117
144
 
118
- async function refresh() {
145
+ // The open popover drawn again from what is known (not while it shows a text).
146
+ function follow() {
147
+ if (openKey === null || text !== null) return;
148
+ const group = groupOf(openKey);
149
+ const row = group ? null : rowOf(openKey);
150
+ if (group || row) fill(group, row);
151
+ }
152
+
153
+ async function refresh({ loading = false } = {}) {
119
154
  const id = sessionId;
120
155
  if (!id) return;
121
156
  const fresh = latest.begin();
@@ -130,34 +165,56 @@ export function createContextChips({ el, api, toast = () => {}, doc = document,
130
165
  }
131
166
  draw();
132
167
  // An open popover follows its row, but not while it shows the text.
133
- if (openName && text === null) {
134
- const row = rows.find((r) => r.name === openName);
135
- if (row) fill(row);
136
- }
168
+ follow();
169
+ // The peers' exchanges for the chips: at a session's open (reuse the
170
+ // conversation's read if loaded or in flight), and when a peer comes.
171
+ const names = groups().map((group) => group.name).join(" ");
172
+ if (names && (loading || names !== peerNames)) peers()?.refresh?.({ reuse: loading });
173
+ peerNames = names;
137
174
  }
138
175
 
139
176
  function load(id) {
140
177
  sessionId = id;
141
178
  rows = [];
179
+ peerNames = "";
142
180
  close();
143
- return refresh();
181
+ return refresh({ loading: true });
144
182
  }
145
183
 
146
184
  function clear() {
147
185
  sessionId = null;
148
186
  rows = [];
187
+ peerNames = "";
149
188
  close();
150
189
  }
151
190
 
152
- function fill(row) {
153
- render(popoverTemplate(row, { text }), pop);
191
+ function fill(group, row) {
192
+ if (group) {
193
+ const actions = peerActions;
194
+ render(peerPopoverTemplate(group, exchangesOf(group.name), {
195
+ text, status: actions.status?.(group.name) || null, drawn: (id) => !!actions.drawn?.(id),
196
+ }), pop);
197
+ } else render(popoverTemplate(row, { text }), pop);
154
198
  }
155
199
 
156
200
  // At the chip as the session bar's chips place theirs (popoverPlace):
157
201
  // above it, or below when the room above is short; capped to the room
158
- // (it scrolls), inside the window with a 16px gutter.
202
+ // (it scrolls), inside the window with a 16px gutter. A peer's popover in
203
+ // a phone's window is a bottom sheet (sheetPlace), as scene 2 has it.
159
204
  function place(chip) {
160
- const width = Math.min(380, win.innerWidth - 32);
205
+ const sheet = !!groupOf(openKey) && win.innerWidth < SHEET_MAX;
206
+ pop.classList.toggle("sheet", sheet);
207
+ if (sheet) {
208
+ const at = sheetPlace({ innerWidth: win.innerWidth, innerHeight: win.innerHeight });
209
+ pop.style.width = `${at.width}px`;
210
+ pop.style.left = `${at.left}px`;
211
+ pop.style.top = "";
212
+ pop.style.bottom = `calc(${at.bottom}px + var(--safe-b, 0px))`;
213
+ pop.style.maxHeight = `${at.maxHeight}px`;
214
+ return;
215
+ }
216
+ // A peer's is wider: its actions on one line (scene 2).
217
+ const width = Math.min(groupOf(openKey) ? 440 : 380, win.innerWidth - 32);
161
218
  pop.style.width = `${width}px`;
162
219
  pop.style.maxHeight = "";
163
220
  const at = popoverPlace(chip.getBoundingClientRect(), { width, height: pop.offsetHeight,
@@ -169,19 +226,21 @@ export function createContextChips({ el, api, toast = () => {}, doc = document,
169
226
  }
170
227
 
171
228
  function placeAtOpenChip() {
172
- const chip = openName && el.querySelector(".ctx-chip.open");
229
+ const chip = openKey && el.querySelector(".ctx-chip.open");
173
230
  if (chip) place(chip);
174
231
  }
175
232
 
176
- function open(name, chip) {
177
- const row = rows.find((r) => r.name === name);
178
- if (!row) return;
233
+ function open(key, chip) {
234
+ const group = groupOf(key);
235
+ const row = group ? null : rowOf(key);
236
+ if (!group && !row) return;
179
237
  // Another popover's nodes go, so this one starts fresh.
180
238
  render(nothing, pop);
181
- openName = name;
239
+ openKey = key;
182
240
  text = null;
183
- pop.setAttribute("aria-label", name);
184
- fill(row);
241
+ pop.setAttribute("aria-label", group ? group.label : key);
242
+ pop.classList.toggle("peer-pop", !!group);
243
+ fill(group, row);
185
244
  pop.hidden = false;
186
245
  place(chip);
187
246
  drawChips();
@@ -190,25 +249,29 @@ export function createContextChips({ el, api, toast = () => {}, doc = document,
190
249
  // Focus that was in it (or +refocus+: Esc) goes back to its chip.
191
250
  function close({ refocus = false } = {}) {
192
251
  const back = focusBack(pop, { refocus, doc });
193
- const name = openName;
194
- openName = null;
252
+ const key = openKey;
253
+ openKey = null;
195
254
  text = null;
196
255
  pop.hidden = true;
256
+ pop.classList.remove("sheet", "peer-pop");
197
257
  render(nothing, pop);
198
258
  drawChips();
199
- if (name !== null) back([...el.querySelectorAll(".ctx-chip[data-name]")].find((chip) => chip.dataset.name === name));
259
+ if (key !== null) back([...el.querySelectorAll(".ctx-chip[data-name]")].find((chip) => chip.dataset.name === key));
200
260
  }
201
261
 
202
262
  async function viewText() {
203
- const name = openName;
204
- if (!name) return;
263
+ const key = openKey;
264
+ if (!key) return;
265
+ // A peer's text is its outbox's: the latest answer.
266
+ const name = groupOf(key)?.outbox?.name || key;
205
267
  try {
206
268
  const data = await api.show(sessionId, name);
207
- if (name !== openName) return;
208
- const row = rows.find((r) => r.name === name);
209
- if (!row) return;
269
+ if (key !== openKey) return;
270
+ const group = groupOf(key);
271
+ const row = group ? null : rowOf(key);
272
+ if (!group && !row) return;
210
273
  text = data.text || "";
211
- fill(row);
274
+ fill(group, row);
212
275
  // The text makes it taller: placed again.
213
276
  placeAtOpenChip();
214
277
  } catch (e) {
@@ -216,7 +279,7 @@ export function createContextChips({ el, api, toast = () => {}, doc = document,
216
279
  }
217
280
  }
218
281
  async function detach() {
219
- const row = rows.find((r) => r.name === openName);
282
+ const row = rowOf(openKey);
220
283
  if (!row) return;
221
284
  const ask = row.scope === "project"
222
285
  ? `Mute ${row.name} for this session? It is the project's: other sessions keep it.`
@@ -226,35 +289,106 @@ export function createContextChips({ el, api, toast = () => {}, doc = document,
226
289
  await api.detach(sessionId, row.name);
227
290
  close();
228
291
  await refresh();
292
+ focusAfterRemoval();
229
293
  } catch (e) {
230
294
  toast(cantText(`detach ${row.name}`, e));
231
295
  }
232
296
  }
233
297
 
298
+ // Unpair: one confirm, then both legs detached, the outbox first; a leg
299
+ // that fails is named and stops it (the inbox stays: the chip doesn't turn
300
+ // into "answers only" over a half unpair).
301
+ async function unpair() {
302
+ const group = groupOf(openKey);
303
+ if (!group) return;
304
+ const legs = [group.outbox, group.inbox].filter(Boolean).map((leg) => leg.name);
305
+ if (!win.confirm(`Unpair ${group.label}? This detaches ${legs.join(" and ")} from this session.`)) return;
306
+ const id = sessionId;
307
+ close();
308
+ for (const name of legs) {
309
+ try {
310
+ await api.detach(id, name);
311
+ } catch (e) {
312
+ toast(cantText(`detach ${name}`, e));
313
+ break;
314
+ }
315
+ }
316
+ await refresh();
317
+ focusAfterRemoval();
318
+ }
319
+
320
+ // A chip the focus was given back to went with its source: the focus goes
321
+ // to the strip's first chip, else to +focusAway+ (the composer).
322
+ function focusAfterRemoval() {
323
+ const at = doc.activeElement;
324
+ if (at && at !== doc.body && at.isConnected) return;
325
+ const chip = el.querySelector(".ctx-chip");
326
+ if (chip) chip.focus();
327
+ else focusAway();
328
+ }
329
+
330
+ function act(name, target) {
331
+ const group = groupOf(openKey);
332
+ if (name === "close") close();
333
+ else if (name === "view") viewText();
334
+ else if (name === "detach") detach();
335
+ else if (name === "unpair") unpair();
336
+ else if (name === "status" && group) {
337
+ const line = peerActions.status?.(group.name);
338
+ close();
339
+ if (line) peerActions.run?.(line);
340
+ } else if (name === "jump") {
341
+ const id = target.dataset.peerId;
342
+ close();
343
+ if (id) peerActions.jump?.(id);
344
+ }
345
+ }
346
+
234
347
  el.addEventListener("click", (e) => {
235
348
  const chip = e.target.closest(".ctx-chip");
236
349
  if (!chip) return;
237
350
  // On to the document: a session-bar chip's popover closes (the close
238
351
  // below leaves clicks in +el+ alone).
239
- if (openName === chip.dataset.name) close();
352
+ if (openKey === chip.dataset.name) close();
240
353
  else open(chip.dataset.name, chip);
241
354
  });
242
355
  pop.addEventListener("click", (e) => {
243
- const act = e.target.closest("[data-act]")?.dataset.act;
244
- if (act === "close") close();
245
- else if (act === "view") viewText();
246
- else if (act === "detach") detach();
356
+ const target = e.target.closest("[data-act]");
357
+ if (target) act(target.dataset.act, target);
247
358
  });
248
359
  doc.addEventListener("click", (e) => {
249
- if (openName && !pop.contains(e.target) && !el.contains(e.target)) close();
360
+ if (openKey && !pop.contains(e.target) && !el.contains(e.target)) close();
250
361
  });
251
362
  doc.addEventListener("keydown", (e) => {
252
- if (e.key === "Escape" && openName) close({ refocus: true });
363
+ if (e.key === "Escape" && openKey) close({ refocus: true });
253
364
  });
254
365
  win.addEventListener("resize", placeAtOpenChip);
255
366
  win.addEventListener("focus", () => refresh());
256
367
  // The ages move on by themselves.
257
368
  win.setInterval(() => { if (rows.length) drawChips(); }, 60_000);
258
369
 
259
- return { load, clear, refresh, close, get rows() { return rows; } };
370
+ // +name+'s popover on its text (a peer answer's "Read the full answer",
371
+ // peer_links.js), at its chip: a peer's leg opens its peer's chip. In a
372
+ // task of its own: the click that asks would close it on its way to the
373
+ // document.
374
+ function showText(name) {
375
+ setTimeout(() => {
376
+ const leg = rowOf(name);
377
+ const key = leg?.peer && groupOf(peerKey(leg.peer)) ? peerKey(leg.peer) : name;
378
+ const chip = [...el.querySelectorAll(".ctx-chip[data-name]")].find((c) => c.dataset.name === key);
379
+ if (!chip) return;
380
+ chip.scrollIntoView?.({ block: "nearest", inline: "nearest" });
381
+ if (openKey !== key) open(key, chip);
382
+ if (openKey === key && text === null) viewText();
383
+ }, 0);
384
+ }
385
+
386
+ // The peers model read again: the chips and an open peer's popover follow it.
387
+ function peersChanged() {
388
+ if (!sessionId) return;
389
+ drawChips();
390
+ if (groupOf(openKey)) follow();
391
+ }
392
+
393
+ return { load, clear, refresh, close, showText, peersChanged, get rows() { return rows; } };
260
394
  }
@@ -55,12 +55,14 @@ function sameBut(a, b, key) {
55
55
  return JSON.stringify(strip(a)) === JSON.stringify(strip(b));
56
56
  }
57
57
 
58
- // What a reader reads of +el+: its text, without the copy button and the
59
- // time dividers, whitespace collapsed.
58
+ // What a reader reads of +el+: its text, without the copy button, the
59
+ // time dividers and a peer answer's late slots (peer_links.js fills them
60
+ // from GET …/peers, which an offscreen reload doesn't read), whitespace
61
+ // collapsed.
60
62
  function textOf(el) {
61
63
  if (!el) return "";
62
64
  const copy = el.cloneNode(true);
63
- copy.querySelectorAll(".copy-btn, .time-gap").forEach((node) => node.remove());
65
+ copy.querySelectorAll(".copy-btn, .time-gap, [data-peer-slot]").forEach((node) => node.remove());
64
66
  return copy.textContent.replace(/\s+/g, " ").trim();
65
67
  }
66
68
 
@@ -1,6 +1,6 @@
1
1
  // Images in the web UI: pasted or dropped into the composer as chips, sent
2
2
  // with a turn as refs (uploaded first), shown as thumbnails in the bubbles
3
- // and tool rows, opened full size in a lightbox. No DOM here beyond what the
3
+ // and tool rows, opened in the picture viewer (picture_viewer.js). No DOM here beyond what the
4
4
  // callers pass (the templates are lit-html's), so it runs under `node --test`.
5
5
  import { html, nothing } from "./vendor/lit-html.js";
6
6