samagotchi 0.100.0 → 0.101.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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -1
  3. data/bin/chi +1 -0
  4. data/docs/architecture.md +10 -1
  5. data/docs/cli.md +55 -0
  6. data/docs/configuration.md +1 -0
  7. data/docs/hooks.md +2 -0
  8. data/docs/internals/engine.md +1 -0
  9. data/docs/internals/working-set.md +130 -0
  10. data/docs/sessions.md +1 -1
  11. data/docs/testing.md +1 -1
  12. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  13. data/lib/samagotchi/bundles/work-cap/manifest.yml +2 -2
  14. data/lib/samagotchi/bundles/work-cap/plugin.rb +26 -16
  15. data/lib/samagotchi/config.rb +3 -0
  16. data/lib/samagotchi/engine.rb +30 -0
  17. data/lib/samagotchi/hooks/loader.rb +17 -2
  18. data/lib/samagotchi/host_keys.rb +35 -18
  19. data/lib/samagotchi/testing/fake_executable.rb +6 -16
  20. data/lib/samagotchi/testing/fake_executable_launcher +6 -0
  21. data/lib/samagotchi/tool_response.rb +10 -6
  22. data/lib/samagotchi/tool_runner.rb +25 -6
  23. data/lib/samagotchi/version.rb +1 -1
  24. data/lib/samagotchi/web/app.rb +49 -1
  25. data/lib/samagotchi/web/public/app.js +77 -11
  26. data/lib/samagotchi/web/public/chains.js +24 -0
  27. data/lib/samagotchi/web/public/chip_popover.js +56 -17
  28. data/lib/samagotchi/web/public/data.js +6 -0
  29. data/lib/samagotchi/web/public/index.html +134 -2
  30. data/lib/samagotchi/web/public/info_bar.js +16 -4
  31. data/lib/samagotchi/web/public/info_bar_model.js +7 -3
  32. data/lib/samagotchi/web/public/markdown_place.js +47 -1
  33. data/lib/samagotchi/web/public/refs.js +10 -1
  34. data/lib/samagotchi/web/public/send_turn.js +6 -2
  35. data/lib/samagotchi/web/public/session_actions.js +10 -5
  36. data/lib/samagotchi/web/public/stage_model.js +6 -5
  37. data/lib/samagotchi/web/public/stage_view.js +34 -2
  38. data/lib/samagotchi/web/public/stream_handlers.js +6 -1
  39. data/lib/samagotchi/web/public/turn_view.js +37 -11
  40. data/lib/samagotchi/web/public/working_set.js +520 -0
  41. data/lib/samagotchi/web/public/working_set_rail.js +278 -0
  42. data/lib/samagotchi/web/public/working_set_view.js +426 -0
  43. data/lib/samagotchi/web/server.rb +6 -4
  44. data/lib/samagotchi/working_set.rb +200 -0
  45. metadata +7 -1
@@ -1,14 +1,19 @@
1
1
  // The popover shell a session-bar chip opens (llm_context_chip.js,
2
- // level_chip.js): a div.ctx-popover on the body, placed at the chip,
3
- // opened by a click on it (delegated on +el+), closed by a second click,
4
- // its × (data-act="close"), a click outside or Esc. Another session's
5
- // summary (refresh) closes it. What it draws, and what a Set or another
6
- // data-act does, is the caller's.
2
+ // level_chip.js, the working set's): a div.ctx-popover on the body, placed
3
+ // at the chip, opened by a click on it (delegated on +el+) or by open()
4
+ // (another opener: the working set's rail and footprint), closed by a second
5
+ // click, its × (data-act="close"), a click outside or Esc; focus goes back
6
+ // to the opener. Another session's summary (refresh) closes it. What it
7
+ // draws, and what a Set or another data-act does, is the caller's. With
8
+ // +sheet+, a window narrower than SHEET_MAX gets it as a bottom sheet.
7
9
 
8
10
  import { nothing, render } from "./vendor/lit-html.js";
9
11
 
10
12
  const EDGE = 16;
11
13
  const GAP = 8;
14
+ // Under this window width a +sheet+ popover is a bottom sheet.
15
+ export const SHEET_MAX = 600;
16
+ const SHEET_EDGE = 8;
12
17
 
13
18
  // Where the popover goes for the chip's +rect+: above it, or below when
14
19
  // the room above is short of its height and below has more; capped to the
@@ -24,26 +29,48 @@ export function popoverPlace(rect, { width, height, innerWidth, innerHeight }) {
24
29
  return height > room ? { ...place, maxHeight: room } : place;
25
30
  }
26
31
 
32
+ // A bottom sheet's place in a phone's window: the window's width less its
33
+ // edges, at its bottom, at most 60 % of its height (the rest scrolls).
34
+ // @return {left, bottom, width, maxHeight}
35
+ export function sheetPlace({ innerWidth, innerHeight }) {
36
+ return { left: SHEET_EDGE, bottom: SHEET_EDGE, width: innerWidth - 2 * SHEET_EDGE, maxHeight: Math.round(innerHeight * 0.6) };
37
+ }
38
+
27
39
  // id, className: the popover's; width: its widest (px); el: where the chip
28
- // is drawn; anchor: the chip's selector; summary(): what the form is drawn
29
- // from (null: nothing opens); session(): the open session's id.
40
+ // is drawn; anchor: the chip's selector; openers: the selector of every
41
+ // element that opens it (a click there is not a click outside; default the
42
+ // anchor's); summary(anchor): what the form is drawn from (null: nothing
43
+ // opens); session(): the open session's id.
30
44
  // show(data, opening): draws the form into the popover (opening: a fresh
31
45
  // open, else a refresh with a newer summary); onClose(): the caller forgets
32
46
  // its open state; onSubmit(form); onAct(name): a data-act other than close.
33
- // @return {pop, close, refresh, isOpen}
34
- export function createChipPopover({ id, className, width: widest, el, anchor: anchorSel, summary,
47
+ // sheet: a bottom sheet in a narrow window (sheetPlace).
48
+ // @return {pop, open, close, refresh, isOpen, opener}
49
+ export function createChipPopover({ id, className, width: widest, el, anchor: anchorSel, openers = anchorSel, summary,
35
50
  session = () => null, show, onClose = () => {}, onSubmit, onAct = () => {},
36
- doc = document, win = window }) {
51
+ sheet = false, doc = document, win = window }) {
37
52
  const pop = doc.createElement("div");
38
53
  pop.id = id;
39
54
  pop.className = className;
40
55
  pop.setAttribute("role", "dialog");
41
56
  pop.hidden = true;
42
57
  doc.body.appendChild(pop);
43
- // The session the open form is for.
58
+ // The session the open form is for, and the element that opened it.
44
59
  let openFor = null;
60
+ let opener = null;
45
61
 
46
62
  function place(anchor) {
63
+ const narrow = sheet && win.innerWidth < SHEET_MAX;
64
+ pop.classList.toggle("sheet", narrow);
65
+ if (narrow) {
66
+ const at = sheetPlace({ innerWidth: win.innerWidth, innerHeight: win.innerHeight });
67
+ pop.style.width = `${at.width}px`;
68
+ pop.style.left = `${at.left}px`;
69
+ pop.style.top = "";
70
+ pop.style.bottom = `${at.bottom}px`;
71
+ pop.style.maxHeight = `${at.maxHeight}px`;
72
+ return;
73
+ }
47
74
  const width = Math.min(widest, win.innerWidth - 32);
48
75
  pop.style.width = `${width}px`;
49
76
  pop.style.maxHeight = "";
@@ -55,22 +82,29 @@ export function createChipPopover({ id, className, width: widest, el, anchor: an
55
82
  if (at.maxHeight !== undefined) pop.style.maxHeight = `${at.maxHeight}px`;
56
83
  }
57
84
 
85
+ // Opens at +anchor+ (open already: drawn anew there).
58
86
  function open(anchor) {
59
- const data = summary();
87
+ const data = summary(anchor);
60
88
  if (!data) return;
61
89
  openFor = session();
90
+ opener = anchor;
62
91
  show(data, true);
63
92
  pop.hidden = false;
64
93
  place(anchor);
65
94
  }
66
95
 
67
96
  // A closed popover is empty, so each open draws a fresh form (unsent
68
- // edits are gone).
69
- function close() {
97
+ // edits are gone). Focus that was in it (or +refocus+: Esc) goes back to
98
+ // its opener.
99
+ function close({ refocus = false } = {}) {
100
+ const had = refocus || pop.contains(doc.activeElement);
101
+ const back = opener;
70
102
  pop.hidden = true;
71
103
  openFor = null;
104
+ opener = null;
72
105
  onClose();
73
106
  render(nothing, pop);
107
+ if (had && back?.isConnected) back.focus?.();
74
108
  }
75
109
 
76
110
  function isOpen() { return !pop.hidden; }
@@ -84,6 +118,11 @@ export function createChipPopover({ id, className, width: widest, el, anchor: an
84
118
  show(data, false);
85
119
  }
86
120
 
121
+ // A popover's own re-placement (its content grew or shrank).
122
+ function replace() {
123
+ if (isOpen() && opener?.isConnected) place(opener);
124
+ }
125
+
87
126
  // The click goes on to the document: another chip's popover (or the
88
127
  // context popover) open meanwhile closes there. This one's own closer
89
128
  // leaves its chip's click alone.
@@ -103,11 +142,11 @@ export function createChipPopover({ id, className, width: widest, el, anchor: an
103
142
  else if (act) onAct(act);
104
143
  });
105
144
  doc.addEventListener("click", (e) => {
106
- if (isOpen() && !pop.contains(e.target) && !e.target.closest?.(anchorSel)) close();
145
+ if (isOpen() && !pop.contains(e.target) && !e.target.closest?.(openers)) close();
107
146
  });
108
147
  doc.addEventListener("keydown", (e) => {
109
- if (e.key === "Escape" && isOpen()) close();
148
+ if (e.key === "Escape" && isOpen()) close({ refocus: true });
110
149
  });
111
150
 
112
- return { pop, close, refresh, isOpen };
151
+ return { pop, open, close, refresh, isOpen, replace, opener: () => opener };
113
152
  }
@@ -298,6 +298,12 @@ export function openEvents(handlers = {}, opts = {}) {
298
298
  };
299
299
  }
300
300
 
301
+ // A session's working set as the model has it (WorkingSet: {items, at}),
302
+ // what the stage view's working set overlays on its rows (web.working_set).
303
+ export function getWorkingSet(id, opts = {}) {
304
+ return api(`/api/sessions/${encodeURIComponent(id)}/working_set`, opts);
305
+ }
306
+
301
307
  // A session's attached context (chi context): the sources it sees, one
302
308
  // with its text, and detach (a session's) or mute here (a project's).
303
309
  export function listContext(id, opts = {}) {
@@ -345,7 +345,13 @@ button.task-stop:hover:not(:disabled){background:var(--danger-bg-dim);filter:non
345
345
  --md-strong-color:var(--text-bright);--md-blockquote-color:var(--text-muted);--md-code-color:var(--text);
346
346
  --md-pre-size:12.5px;--md-cell-size:13px
347
347
  }
348
- .md-compact:is(.ts-asking,.question-said-full){white-space:normal}
348
+ .gen-text.md-compact{
349
+ --md-heading-size:14.5px;--md-heading-color:var(--text);
350
+ --md-subheading-size:14px;--md-subheading-color:var(--text-2);
351
+ --md-strong-color:var(--text);--md-blockquote-color:var(--text-muted);--md-code-color:var(--text);
352
+ --md-pre-size:12.5px;--md-cell-size:13px
353
+ }
354
+ .md-compact:is(.ts-asking,.question-said-full,.gen-text){white-space:normal}
349
355
  .md-compact :is(h1,h2,h3,h4,h5,h6){font-size:var(--md-heading-size);font-weight:600;line-height:1.4;margin:8px 0 2px;color:var(--md-heading-color)}
350
356
  .md-compact :is(h4,h5,h6){font-size:var(--md-subheading-size);color:var(--md-subheading-color)}
351
357
  .md-compact :is(p,ul,ol,blockquote,table,pre){margin:0 0 6px}
@@ -861,6 +867,132 @@ body.stage-view .turn-stage{position:absolute;left:0;right:0;bottom:100%;margin-
861
867
  .turn-stage .outline-fold{display:none}
862
868
  .turn-stage .turn-work.outline>.gen.folded{display:block}
863
869
  .turn-stage .ts-cloud-body .gen:first-of-type{margin-top:0;border-top:0}
870
+ /* The working set (web.working_set, working_set_rail.js): a rail on the right of the live slots, out of their flow (absolute,
871
+ the live block's full height), so it never sets their height; its lines are fixed 18 px slots, each at its place by a
872
+ transform (a fold fades the absorbed lines out in place and slides the ones below up once). A stage narrower than 760 px
873
+ has it as a row under the trail (a fixed line, part of the live block from the turn's start); a phone has neither (the
874
+ session bar's chip is its entry). body.working-set: the flag is on (app.js). */
875
+ body.working-set .turn-stage{container-type:inline-size}
876
+ body.working-set .ts-live{position:relative;padding-right:264px}
877
+ .ts-ws{--ws-line:18px;position:absolute;top:0;right:0;bottom:0;width:248px;box-sizing:border-box;padding:0 4px 0 12px;border-left:1px solid var(--border);border-radius:0 6px 6px 0;display:flex;flex-direction:column;overflow:hidden;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;line-height:var(--ws-line);color:var(--text-2);cursor:pointer}
878
+ .ts-ws:hover{background:var(--bg-hover)}
879
+ .ts-ws:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
880
+ .ws-head{flex:none;display:flex;align-items:center;gap:6px;min-width:0;height:var(--ws-line);font-family:ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;font-size:11px;font-weight:600;letter-spacing:.04em;text-transform:uppercase;color:var(--text-muted);white-space:nowrap}
881
+ .ws-count{margin-right:auto;font-weight:400;letter-spacing:0;color:var(--text-faint);font-variant-numeric:tabular-nums}
882
+ .ws-tick{display:none}
883
+ .ws-fold{flex:none;width:18px;height:18px;padding:0;border:0;border-radius:4px;background:transparent;color:var(--text-muted);font-size:12px;line-height:18px;opacity:0;transition:opacity .15s}
884
+ .ws-fold:hover:not(:disabled){filter:none;background:var(--bg-raised);color:var(--text)}
885
+ .ts-ws:hover .ws-fold,.ts-ws:focus-within .ws-fold,.ts-ws.collapsed .ws-fold{opacity:1}
886
+ @media (hover:none){.ws-fold{opacity:1}}
887
+ .ws-ring{flex:none;width:14px;height:14px}
888
+ .ws-ring circle{fill:none;stroke-width:2.4}
889
+ .ws-ring .track{stroke:var(--border-strong)}
890
+ .ws-ring .fill{stroke:var(--accent);stroke-linecap:round;transform:rotate(-90deg);transform-origin:50% 50%;transition:stroke-dasharray .4s ease}
891
+ .ws-ring.warn .fill{stroke:var(--warn)}
892
+ .ws-ring.danger .fill{stroke:var(--danger)}
893
+ .ws-list{position:relative;flex:1;min-height:0;overflow:hidden}
894
+ .ts-ws.empty .ws-list::before{content:"nothing read yet";font-family:ui-sans-serif,system-ui,sans-serif;font-style:italic;color:var(--text-faint)}
895
+ .ws-item,.ws-more{position:absolute;top:0;left:0;right:0;height:var(--ws-line);transition:transform .18s cubic-bezier(.3,.7,.2,1),opacity .15s ease,background-color .8s ease}
896
+ .ws-item{display:flex;align-items:center;gap:5px;white-space:nowrap;overflow:hidden;border-radius:4px}
897
+ .ws-item .k{flex:none;width:12px;text-align:center;color:var(--text-faint)}
898
+ .ws-item.memory .k{color:var(--think-text)}
899
+ .ws-item.url .k{color:var(--accent)}
900
+ .ws-item .t{min-width:0;overflow:hidden;text-overflow:ellipsis}
901
+ .ws-item .n{flex:none;color:var(--text-faint);font-variant-numeric:tabular-nums}
902
+ .ws-item .e{flex:none;color:var(--accent);font-weight:600;font-variant-numeric:tabular-nums}
903
+ .ws-item .c{flex:none;color:var(--text-faint);font-variant-numeric:tabular-nums}
904
+ .ws-item .n:empty,.ws-item .e:empty,.ws-item .c:empty{display:none}
905
+ /* The server's states (WorkingSet): stale faded; forgotten struck, faint and faded (a group only when all its members are). The words are the popover's and the titles' (colour is never alone). */
906
+ .ws-item.stale{opacity:.55}
907
+ .ws-item.forgotten{opacity:.5}
908
+ .ws-item.forgotten .t,.ws-row.forgotten .t,.ws-foot .l.forgotten{text-decoration:line-through;color:var(--text-faint)}
909
+ .ws-row.stale .k,.ws-row.stale .t,.ws-foot .l.stale{opacity:.55}
910
+ .ws-row.forgotten .k,.ws-row.forgotten .t,.ws-foot .l.forgotten{opacity:.5}
911
+ .ws-item.enter{animation:ws-in .2s ease}
912
+ @keyframes ws-in{from{opacity:0}}
913
+ .ws-item.leave,.ws-more.leave{opacity:0;pointer-events:none}
914
+ /* Just entered (FLASH_MS): an accent dot for the glyph and a tint; it settles (the tint fades 800 ms). */
915
+ .ws-item.fresh{background:var(--accent-tint)}
916
+ .ws-item.fresh .k{font-size:0;display:flex;align-items:center;justify-content:center;height:var(--ws-line)}
917
+ .ws-item.fresh .k::before{content:"";display:inline-block;width:6px;height:6px;border-radius:50%;background:var(--accent);box-shadow:0 0 0 2px var(--accent-ring)}
918
+ .ws-more{padding-left:17px;font-family:ui-sans-serif,system-ui,sans-serif;font-size:11px;color:var(--text-faint);font-variant-numeric:tabular-nums}
919
+ /* Folded by hand (kept per browser): the head line alone at the right, the newest entry ticking through it. */
920
+ .ts-ws.collapsed{bottom:auto;width:auto;max-width:248px;height:var(--ws-line);border-left:0;padding-left:6px}
921
+ .ts-ws.collapsed .ws-list{display:none}
922
+ .ts-ws.collapsed .ws-count{margin-right:0}
923
+ .ts-ws.collapsed .ws-tick{display:block;min-width:0;overflow:hidden;text-overflow:ellipsis;margin-right:auto;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;font-weight:400;letter-spacing:0;text-transform:none;color:var(--text-2)}
924
+ .ts-ws.collapsed .ws-tick:not(:empty)::before{content:"+";margin-right:5px;color:var(--accent);font-weight:700}
925
+ .ws-tick.fresh{animation:ts-fade .3s ease}
926
+ /* The row form, in a stage under 760 px: one line under the trail (its room, 18 px and a gap, kept while a card waits). */
927
+ @container (max-width:759px){
928
+ body.working-set .ts-live{padding-right:0;--ws-row-h:26px}
929
+ .ts-ws{position:static;width:auto;height:var(--ws-line);flex-direction:row;align-items:center;gap:10px;padding:0;border-left:0;border-radius:6px}
930
+ .ts-ws .ws-head{flex:none}
931
+ .ts-ws .ws-count{margin-right:0}
932
+ .ts-ws .ws-list{display:flex;align-items:center;gap:12px;height:var(--ws-line)}
933
+ .ws-item,.ws-more{position:static;flex:none;max-width:30ch;transform:none !important;transition:none}
934
+ .ws-more{padding-left:0}
935
+ .ws-item.leave,.ws-more.leave{display:none}
936
+ .turn-stage.waiting .ts-ws{display:none}
937
+ }
938
+ body.working-set .turn-stage.waiting .ts-live{min-height:calc(var(--ts-live-h) + var(--ws-row-h,0px))}
939
+ body.working-set .turn-stage.held-wait:not(.waiting) .ts-live{min-height:max(calc(var(--ts-live-h) + var(--ws-row-h,0px)),var(--live-held,0px))}
940
+ /* A finished turn's footprint, under its closed block (working_set_view.js): one faint line; a click opens its list. */
941
+ .ws-foot{display:block;width:fit-content;max-width:calc(100% - 13px);box-sizing:border-box;margin:-4px 0 0 13px;padding:0 6px;border-radius:6px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:11.5px;line-height:18px;color:var(--text-faint);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;cursor:pointer}
942
+ .ws-foot:hover{color:var(--text-muted);background:var(--bg-hover)}
943
+ .ws-foot:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
944
+ .ws-foot .w{font-family:ui-sans-serif,system-ui,sans-serif}
945
+ .ws-foot .sep{color:var(--border-strong)}
946
+ .ws-foot .e{color:var(--accent)}
947
+ .ws-foot .k{color:var(--think-text)}
948
+ .ws-foot .url .k{color:var(--accent)}
949
+ .ws-foot .f{text-decoration:line-through}
950
+ /* A step tag (s3, t2·s3, prompt): where an item entered; a button jumps to its call. */
951
+ .ws-step{display:inline-block;padding:0 4px;border:0;border-radius:4px;background:var(--badge-bg);font-family:ui-sans-serif,system-ui,sans-serif;font-size:10.5px;line-height:14px;font-weight:400;color:var(--text-faint);font-variant-numeric:tabular-nums;vertical-align:1px}
952
+ .ws-step.p{color:var(--think-text)}
953
+ button.ws-step{cursor:pointer}
954
+ button.ws-step:hover:not(:disabled){filter:none;background:var(--accent-bg);color:var(--accent)}
955
+ /* The expanded view (a chip popover; a bottom sheet on a phone). */
956
+ .ws-popover{gap:0}
957
+ .ws-popover .ctx-pop-head{align-items:center;flex-wrap:wrap;margin-bottom:4px}
958
+ .ws-scope{display:inline-flex;gap:2px}
959
+ .ws-scope button{padding:1px 8px;border:1px solid transparent;border-radius:999px;background:transparent;color:var(--text-muted);font-size:11px;line-height:16px}
960
+ .ws-scope button.on{color:var(--text-2);border-color:var(--border-strong);background:var(--bg-raised)}
961
+ .ws-scope button:hover:not(:disabled){filter:none;color:var(--text)}
962
+ .ws-popover .ctx-pop-meta{margin-top:6px}
963
+ .ws-popover .ws-hint{margin-top:10px;color:var(--text-faint)}
964
+ .ws-row{display:flex;align-items:center;gap:8px;width:100%;min-width:0;height:28px;box-sizing:border-box;padding:0 6px;border:0;border-radius:6px;background:transparent;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12.5px;line-height:28px;color:var(--text-2);text-align:left}
965
+ .ws-row:hover{background:var(--bg-hover)}
966
+ button.ws-row:hover:not(:disabled){filter:none;background:var(--bg-hover)}
967
+ .ws-row .k{flex:none;width:12px;text-align:center;color:var(--text-faint)}
968
+ .ws-row.memory .k{color:var(--think-text)}
969
+ .ws-row.url .k{color:var(--accent)}
970
+ .ws-row .t{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:inherit}
971
+ .ws-row a.t{text-decoration:none}
972
+ .ws-row a.t:hover{text-decoration:underline}
973
+ .ws-row .e{flex:none;color:var(--accent);font-weight:600}
974
+ .ws-row .n{flex:none;color:var(--text-faint)}
975
+ .ws-row .st{flex:none;margin-left:auto;font-family:ui-sans-serif,system-ui,sans-serif;font-size:11px;color:var(--text-faint)}
976
+ .ws-row .chev{flex:none;margin-left:auto;font-family:ui-sans-serif,system-ui,sans-serif;font-size:11px;color:var(--text-faint)}
977
+ .ws-row .ws-step{flex:none}
978
+ .ws-row .tk,.ws-row .c{flex:none;font-family:ui-sans-serif,system-ui,sans-serif;font-size:11px;color:var(--text-faint);font-variant-numeric:tabular-nums}
979
+ .ws-row.member{padding-left:26px}
980
+ .ws-row.fresh{background:var(--accent-tint)}
981
+ .ws-row.fresh .k{color:var(--accent)}
982
+ .ws-row.muted{opacity:.6}
983
+ .ws-popover.sheet{border-radius:14px}
984
+ /* A phone's sheet: the name, the scope and × on the first line, the count and the ring under them. */
985
+ .ws-popover.sheet .ctx-pop-close{order:2}
986
+ .ws-popover.sheet .ctx-pop-head>.ctx-pop-kind{order:3;flex:1 0 calc(100% - 30px)}
987
+ .ws-popover.sheet .ctx-pop-head>.ws-ring{order:4}
988
+ .ws-popover.sheet .ws-row{height:30px;line-height:30px}
989
+ /* The session bar's chip, in the mem chip's place: a dot (the accent: changed since its list was last opened) and the count. */
990
+ #infoBar .meta .ws-chip{display:inline-flex;align-items:center;gap:5px;padding:2px 7px;border:1px solid transparent;cursor:pointer;font-family:inherit;font-weight:400;white-space:nowrap;font-variant-numeric:tabular-nums}
991
+ #infoBar .meta .ws-chip:hover:not(:disabled){filter:none;color:var(--text);border-color:var(--accent-border);background:var(--accent-bg)}
992
+ .ws-dot{width:6px;height:6px;border-radius:50%;background:var(--text-faint);opacity:.6}
993
+ .ws-chip.changed .ws-dot{background:var(--accent);opacity:1;box-shadow:0 0 0 2px var(--accent-ring)}
994
+ @media(max-width:600px){body.working-set .ts-live{padding-right:0;--ws-row-h:0px}.ts-ws{display:none}}
995
+ @media (prefers-reduced-motion:reduce){.ws-item,.ws-more,.ws-ring .fill{transition:none}.ws-item.enter,.ws-tick.fresh{animation:none}}
864
996
  @media(max-width:600px){
865
997
  .bubble.user.queued[data-delivery="queue"]{flex-wrap:wrap}
866
998
  .bubble.user.queued[data-delivery="queue"] .origin-label,.bubble.user.queued[data-delivery="queue"] .state-badge{order:0}
@@ -968,7 +1100,7 @@ body.has-session.composer-folded #infoBar{border-top-color:transparent}
968
1100
  #infoBar .meta .id{order:-2}
969
1101
  #infoBar .meta .status{order:-1}
970
1102
  /* The memory chip right after the status (same order, later in the DOM): the meta scrolls, and only its start is in sight. */
971
- #infoBar .meta .mem-chip{order:-1}
1103
+ #infoBar .meta .mem-chip,#infoBar .meta .ws-chip{order:-1}
972
1104
  /* A parent's children chip too: its waiting part is what to see. */
973
1105
  #infoBar .meta .children-chip{order:-1}
974
1106
  /* A chain's ← → links too: the way to the other days. */
@@ -57,6 +57,15 @@ function kidsTemplate(kids) {
57
57
  return html`<a class=${`model children-chip${kids.waitingText ? " waiting" : ""}`} href=${ALL_SESSIONS_HASH} title=${`${kids.title} (all sessions)`}>${kids.text}${waiting}</a>`;
58
58
  }
59
59
 
60
+ // The working set's chip (web.working_set), in the mem chip's place: a dot
61
+ // (the accent when the set changed since its list was last opened) and
62
+ // "set N"; nothing while the set is empty. +chip+: {n, changed} or null.
63
+ export function workingSetChipTemplate(chip) {
64
+ if (!chip) return nothing;
65
+ const title = `Working set: ${chip.n} item${chip.n === 1 ? "" : "s"} this session brought into context (files, memories, URLs)${chip.changed ? ", changed since you last looked" : ""}. Open the list.`;
66
+ return html`<button type="button" class=${`model ws-chip${chip.changed ? " changed" : ""}`} title=${title} aria-label=${title} aria-haspopup="dialog"><span class="ws-dot" aria-hidden="true"></span>set ${chip.n}</button>`;
67
+ }
68
+
60
69
  // The info line for +bar+, nothing without a session (the start page hides
61
70
  // the bar). +bar+:
62
71
  // id, copied (the copy button just copied), status, model ({text, title,
@@ -65,18 +74,21 @@ function kidsTemplate(kids) {
65
74
  // childrenSummary), llmContext, thinking and reasoningHistory (the chips'
66
75
  // summaries; reasoningHistory null on a native host: no chip), toolkit
67
76
  // (the tools chip's summary, null on full: no chip), notes
68
- // ({text, title} or null), memories (the used names), dir, ctx ({text,
77
+ // ({text, title} or null), memories (the used names), workingSet
78
+ // (web.working_set on: {chip}, the working set's chip in place of the
79
+ // mem chip; null: off), dir, ctx ({text,
69
80
  // tip}), speed ({text, tip} or null), clock (the session time's text, ""
70
81
  // for none), worker ({badge, busy} or null).
71
82
  export function infoBarHtml(bar) {
72
83
  if (!bar) return nothing;
73
84
  const attach = `chi --attach ${bar.id}`;
74
85
  const memories = bar.memories || [];
75
- return html`<div class="meta"><span class="id" title=${bar.id}>${String(bar.id).slice(0, 8)}</span><button type="button" class="copy-attach" title=${`Copy "${attach}" to attach a terminal`}>${bar.copied ? "copied ✓" : "copy chi --attach"}</button><span class=${bar.status ? `status ${bar.status}` : "status"}>${bar.status || ""}</span><span class=${`model${bar.model.mismatch ? " served-mismatch" : ""}`} title=${bar.model.title}>${bar.model.text}</span>${bar.chain?.prev ? chainLinkTemplate(bar.chain.prev, PREV, "The previous link of this chain") : nothing}${bar.chain?.next ? chainLinkTemplate(bar.chain.next, NEXT, "The next link of this chain") : nothing}${bar.parent ? html`<a class="model parent" href=${sessionHash(bar.parent.id)} title=${bar.parent.title}>delegated by ${String(bar.parent.id).slice(0, 8)}</a>` : nothing}${kidsTemplate(bar.kids)}${thinkingChip(bar.thinking)}${reasoningHistoryChip(bar.reasoningHistory)}${llmContextChip(bar.llmContext)}${toolkitChip(bar.toolkit)}${bar.notes ? html`<span class="model notes-chip" title=${bar.notes.title}>${bar.notes.text}</span>` : nothing}${memories.length ? html`<span class="model mem-chip" title=${memChipTitle(memories)}>${memChipText(memories)}</span>` : nothing}<span class="dir" title=${bar.dir || ""}><bdi>${bar.dir || ""}</bdi></span><span class="model ctx" title=${bar.ctx.tip}>${bar.ctx.text}</span>${bar.speed ? html`<span class="model speed" title=${bar.speed.tip}>${bar.speed.text}</span>` : nothing}${bar.clock ? html`<span class="model session-time">${bar.clock}</span>` : nothing}${workerBadgeTemplate(bar.worker)}</div>`;
86
+ return html`<div class="meta"><span class="id" title=${bar.id}>${String(bar.id).slice(0, 8)}</span><button type="button" class="copy-attach" title=${`Copy "${attach}" to attach a terminal`}>${bar.copied ? "copied ✓" : "copy chi --attach"}</button><span class=${bar.status ? `status ${bar.status}` : "status"}>${bar.status || ""}</span><span class=${`model${bar.model.mismatch ? " served-mismatch" : ""}`} title=${bar.model.title}>${bar.model.text}</span>${bar.chain?.prev ? chainLinkTemplate(bar.chain.prev, PREV, "The previous link of this chain") : nothing}${bar.chain?.next ? chainLinkTemplate(bar.chain.next, NEXT, "The next link of this chain") : nothing}${bar.parent ? html`<a class="model parent" href=${sessionHash(bar.parent.id)} title=${bar.parent.title}>delegated by ${String(bar.parent.id).slice(0, 8)}</a>` : nothing}${kidsTemplate(bar.kids)}${thinkingChip(bar.thinking)}${reasoningHistoryChip(bar.reasoningHistory)}${llmContextChip(bar.llmContext)}${toolkitChip(bar.toolkit)}${bar.notes ? html`<span class="model notes-chip" title=${bar.notes.title}>${bar.notes.text}</span>` : nothing}${bar.workingSet ? workingSetChipTemplate(bar.workingSet.chip) : memories.length ? html`<span class="model mem-chip" title=${memChipTitle(memories)}>${memChipText(memories)}</span>` : nothing}<span class="dir" title=${bar.dir || ""}><bdi>${bar.dir || ""}</bdi></span><span class="model ctx" title=${bar.ctx.tip}>${bar.ctx.text}</span>${bar.speed ? html`<span class="model speed" title=${bar.speed.tip}>${bar.speed.text}</span>` : nothing}${bar.clock ? html`<span class="model session-time">${bar.clock}</span>` : nothing}${workerBadgeTemplate(bar.worker)}</div>`;
76
87
  }
77
88
 
78
- // Draws +bar+ into +el+ (#infoText), with the memories in its tooltip.
89
+ // Draws +bar+ into +el+ (#infoText), with the memories in its tooltip
90
+ // (with the working set on, its list says them instead).
79
91
  export function renderInfoBar(el, bar) {
80
92
  render(infoBarHtml(bar), el);
81
- el.title = bar ? memoriesTooltip(bar.memories, bar.preloaded, bar.muted) : "";
93
+ el.title = bar && !bar.workingSet ? memoriesTooltip(bar.memories, bar.preloaded, bar.muted) : "";
82
94
  }
@@ -63,7 +63,9 @@ export function infoButtons({ id, state, chain, archived, continuing, streaming
63
63
  // @param restarting whether this session's Restart is under way
64
64
  // @param running whether its turn runs (the session clock ticks)
65
65
  // @param now the clock (ms)
66
- export function infoBarModel({ id, state, sessions, chain, cardTitle, badge, restarting, running, now }) {
66
+ // @param workingSet web.working_set on: the chip's {n, changed}, or null
67
+ // while the set is empty; undefined: off (the mem chip shows)
68
+ export function infoBarModel({ id, state, sessions, chain, cardTitle, badge, restarting, running, now, workingSet = undefined }) {
67
69
  const tokens = state.timing.tokens;
68
70
  const ctxTip = ctxBarTip(tokens, state.timing.memoryIndex,
69
71
  { tokens: state.ctxWindow, source: state.ctxWindowSource, pct: state.ctxPct,
@@ -96,6 +98,8 @@ export function infoBarModel({ id, state, sessions, chain, cardTitle, badge, res
96
98
  // The model notes the session's prompt carried.
97
99
  notes: promptNotesChip(state.promptNotes),
98
100
  memories: state.usedMemories,
101
+ // On, the working set's chip stands in the mem chip's place.
102
+ workingSet: workingSet === undefined ? null : { chip: workingSet },
99
103
  preloaded: state.preloadedMemories,
100
104
  muted: state.mutedMemories,
101
105
  dir: state.dir,
@@ -109,12 +113,12 @@ export function infoBarModel({ id, state, sessions, chain, cardTitle, badge, res
109
113
  // The whole bar for the open session +id+ (null: the start page, no line).
110
114
  // @param sessions the page's list; +continuing+, +restarting+: Sets of ids
111
115
  // @return {buttons, bar}
112
- export function sessionBar({ id, state, sessions, versions, cardTitle, continuing, restarting, streaming, running, now }) {
116
+ export function sessionBar({ id, state, sessions, versions, cardTitle, continuing, restarting, streaming, running, now, workingSet = undefined }) {
113
117
  const chain = id ? chainNeighbours(sessions, id, { continues: state.continues }) : { prev: null, next: null };
114
118
  const archived = !!sessions.find((s) => s.id === id)?.archived;
115
119
  const buttons = infoButtons({ id, state, chain, archived, continuing: continuing.has(id), streaming });
116
120
  if (!id) return { buttons, bar: null };
117
121
  const badge = workerBadgeFor(id, state, versions);
118
- const bar = infoBarModel({ id, state, sessions, chain, cardTitle, badge, restarting: restarting.has(id), running, now });
122
+ const bar = infoBarModel({ id, state, sessions, chain, cardTitle, badge, restarting: restarting.has(id), running, now, workingSet });
119
123
  return { buttons, bar };
120
124
  }
@@ -7,11 +7,17 @@
7
7
  // The texts whose refs linkTextRefs links: the answers and the closed
8
8
  // steps' texts.
9
9
  const TEXTS = ".bubble.output, .gen:not(.live) > .gen-text";
10
+ // The closed steps' texts still shown raw: a live turn's (a reload's come
11
+ // rendered, turn_view.js textTemplate).
12
+ const RAW_STEPS = ".gen:not(.live) > .gen-text:not(.markdown)";
10
13
 
11
14
  export function createMarkdownPlace({
12
15
  capabilities, renderText, roots, touchedFiles, currentDir, linkRefs, applyFinalAnswer, findInTurnRoots, normalize,
13
- pairAnswerRefs, setAnswerRefs = () => {},
16
+ pairAnswerRefs, setAnswerRefs = () => {}, answerRefsOf = () => null,
14
17
  }) {
18
+ // The raw step texts already waiting for their step's open.
19
+ const waiting = new WeakSet();
20
+
15
21
  // A text the page already shows as its markdown HTML (POST /api/render,
16
22
  // sanitised there), or null: markdown off here (no request), or a failed
17
23
  // read (the raw text stays).
@@ -55,6 +61,7 @@ export function createMarkdownPlace({
55
61
  // turn's end and after a reload's render. A ref already made stays as it
56
62
  // is.
57
63
  function linkTextRefs() {
64
+ renderStepTexts();
58
65
  const pageRoots = roots();
59
66
  const touched = touchedFiles(pageRoots);
60
67
  for (const root of pageRoots) {
@@ -66,5 +73,44 @@ export function createMarkdownPlace({
66
73
  }
67
74
  }
68
75
 
76
+ // The closed steps' raw texts as markdown, as the answers' (markdown on
77
+ // only): each from its step's first open (a turn's end would ask once per
78
+ // step, folded or not), as a resolved question card's kept reply. The
79
+ // text stays as its source (stepText); its refs, a question's, pair again
80
+ // on the swap, then the `path:line` ones (linkRefsIn). A null render
81
+ // keeps the text, and the next open asks again.
82
+ function renderStepTexts() {
83
+ if (capabilities().display !== true) return;
84
+ for (const root of roots()) {
85
+ for (const el of root.querySelectorAll(RAW_STEPS)) {
86
+ const step = el.parentElement;
87
+ if (waiting.has(el) || !step?.addEventListener) continue;
88
+ waiting.add(el);
89
+ renderOnOpen(step, el, el.textContent);
90
+ }
91
+ }
92
+ }
93
+
94
+ function renderOnOpen(step, el, text) {
95
+ let state = "raw";
96
+ const draw = () => {
97
+ if (!step.open || state !== "raw") return;
98
+ state = "asking";
99
+ renderMarkdown(text).then((html) => {
100
+ if (!html || el.textContent !== text) { state = html ? "done" : "raw"; return; }
101
+ state = "done";
102
+ step.removeEventListener("toggle", draw);
103
+ const refs = answerRefsOf(el);
104
+ el.setAttribute("data-copy-source", text);
105
+ // Sanitised on the server (POST /api/render).
106
+ el.innerHTML = html;
107
+ el.classList.add("markdown", "md-compact");
108
+ linkRefsIn(el, refs);
109
+ });
110
+ };
111
+ step.addEventListener("toggle", draw);
112
+ draw();
113
+ }
114
+
69
115
  return { renderMarkdown, pairRefsIn, linkRefsIn, applyFinalMarkdown, linkTextRefs };
70
116
  }
@@ -413,10 +413,19 @@ function lineIn(row, line) {
413
413
  export function showInTurn(target, { behavior = smoothScroll() } = {}) {
414
414
  const row = turnRowFor(target);
415
415
  if (!row) return null;
416
+ return showRow(row, { line: target.line, behavior });
417
+ }
418
+
419
+ // Shows a tool row: what hides it opened (its step, its turn's block; a
420
+ // view that hides its rows its own way opens on the row's ref:reveal
421
+ // event), its diff (or the read showing +line+) opened, scrolled to the
422
+ // line (else the row) and both flashed. The working set's step tags jump
423
+ // here. The row.
424
+ export function showRow(row, { line: lineNo = null, behavior = smoothScroll() } = {}) {
416
425
  const view = row.ownerDocument.defaultView || globalThis;
417
426
  const reveal = new view.CustomEvent("ref:reveal", { bubbles: true, detail: {} });
418
427
  row.dispatchEvent(reveal);
419
- const line = target.line ? lineIn(row, target.line) : null;
428
+ const line = lineNo ? lineIn(row, lineNo) : null;
420
429
  const fold = line?.closest("details.activity-diff, details.activity-read") || row.querySelector("details.activity-diff");
421
430
  for (let details = (line || fold || row).closest("details"); details; details = details.parentElement?.closest("details")) {
422
431
  details.open = true;
@@ -19,7 +19,8 @@ import { composerAfterAck, dropEarlyRestores, restoreOnAck } from "./restore.js"
19
19
  // (start_page.js: pendingPick(), chosenModel(), newChoices(),
20
20
  // clearChoices()); +takeDelivery()+ the delivery line's armed choice;
21
21
  // +composer+: {el (the text box), button (Send/Start), chips(),
22
- // setChips(list), fit(), clear(), openForVisit(), focus()}; +historyEl+,
22
+ // setChips(list), fit(), clear(), openForVisit(), focus(), noteDraft(id) (the
23
+ // text was written in session id)}; +historyEl+,
23
24
  // +turnDom+; +makeUserBubble(prompt, opts)+, +bubbleById(id)+,
24
25
  // +addCommandBubble(view)+; +addCreatedSession(created)+, +select(id)+,
25
26
  // +showNotice(text)+, +updateInfoBar()+, +updateComposerMode()+,
@@ -128,7 +129,10 @@ export function createSendTurn({
128
129
  const restored = restoreOnAck(ack?.enqueued_id, earlyRestores, { myId: clientId, sentIds });
129
130
  // A restored line must be seen: the composer opens for this visit.
130
131
  if (restored?.refill && sentTo === session()) composer.openForVisit();
131
- promptEl.value = composerAfterAck({ sent: line, current: promptEl.value, refill: restored?.refill || "" });
132
+ const before = promptEl.value;
133
+ promptEl.value = composerAfterAck({ sent: line, current: before, refill: restored?.refill || "" });
134
+ // A line back from sentTo is a draft of that session, wherever the page is now.
135
+ if (promptEl.value.trim() && promptEl.value !== before) composer.noteDraft(sentTo);
132
136
  composer.fit();
133
137
  // Only the sent chips go: images added since stay (they go up at their own send).
134
138
  const kept = chipsAfterSend(composer.chips(), sending);
@@ -1,4 +1,4 @@
1
- import { continueRefusal, continuedText, openChildrenTemplate } from "./chains.js";
1
+ import { continueRefusal, continuedText, openChildrenTemplate, openerText } from "./chains.js";
2
2
  import { cantText, deleteConfirmText, goneSessionNotice } from "./format.js";
3
3
  import { sessionHash } from "./route.js";
4
4
  import { inTree } from "./sessions_list.js";
@@ -14,12 +14,14 @@ import { inTree } from "./sessions_list.js";
14
14
  // +refresh()+ fetches the list); +lists+ (lists_view.js: render);
15
15
  // +select(id)+, +clearSelection()+, +showNotice(text)+, +updateInfoBar()+,
16
16
  // +toast(text, action, options)+, +addCreatedSession(created)+,
17
- // +fillOpener(created)+ (the new link's composer), +closeLiveStream()+ (the live
17
+ // +fillOpener(created, continuedId)+ (the new link's composer; returns a draft
18
+ // from another session it displaced, or null), +restoreDraft(draft, opener)+
19
+ // (that draft back, never over newer text), +closeLiveStream()+ (the live
18
20
  // stream's close, the stream left as it is) and +workerGone(id)+ the page's.
19
21
  // +alert+ / +confirm+: the browser's.
20
22
  export function createSessionActions({
21
23
  selected, state, sessions, setSessions, api, eventsLive, refresh, lists,
22
- select, clearSelection, showNotice, updateInfoBar, toast, addCreatedSession, fillOpener, closeLiveStream, workerGone,
24
+ select, clearSelection, showNotice, updateInfoBar, toast, addCreatedSession, fillOpener, restoreDraft, closeLiveStream, workerGone,
23
25
  alert = (text) => globalThis.alert(text), confirm = (text) => globalThis.confirm(text),
24
26
  }) {
25
27
  // The session this tab is stopping: its stream may close before the stop
@@ -105,8 +107,11 @@ export function createSessionActions({
105
107
  return;
106
108
  }
107
109
  await select(next);
108
- if (selected() === next) fillOpener(created);
109
- toast(continuedText(id, created), null, { sticky });
110
+ // A draft carried over from elsewhere gave way to the opener: the
111
+ // toast brings it back, and stays until closed so it can't be lost.
112
+ const displaced = selected() === next ? fillOpener(created, id) : null;
113
+ const restore = displaced ? { label: "Restore draft", run: () => restoreDraft(displaced, openerText(created)) } : null;
114
+ toast(continuedText(id, created), restore, { sticky: sticky || !!displaced });
110
115
  } catch (e) {
111
116
  const refusal = continueRefusal(e, sessions());
112
117
  if (refusal.kind === "open") {
@@ -225,11 +225,12 @@ export function handOffDue({ ended, pending, inUse, quietSinceMs, nowMs }) {
225
225
  }
226
226
 
227
227
  // A handed-off turn's nodes in the order the turn view leaves them: the
228
- // prompt bubble (none for a reminder turn), the block, the extras in
229
- // arrival order, the answer's pieces, the timing line, the end line, the
230
- // kept cards, then the lines queued for after the turn (they run next).
231
- export function handOffOrder({ prompt = null, block = null, extras = [], answer = [], timing = null, end = null, kept = [], queued = [] }) {
232
- return [prompt, block, ...extras, ...answer, timing, end, ...kept, ...queued].filter(Boolean);
228
+ // prompt bubble (none for a reminder turn), the block, its working set's
229
+ // footprint line (web.working_set), the extras in arrival order, the
230
+ // answer's pieces, the timing line, the end line, the kept cards, then the
231
+ // lines queued for after the turn (they run next).
232
+ export function handOffOrder({ prompt = null, block = null, footprint = null, extras = [], answer = [], timing = null, end = null, kept = [], queued = [] }) {
233
+ return [prompt, block, footprint, ...extras, ...answer, timing, end, ...kept, ...queued].filter(Boolean);
233
234
  }
234
235
 
235
236
  // The rows that wait for the running turn's end to run as their own: a