samagotchi 0.190.1 → 0.192.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 (106) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/docs/architecture.md +13 -11
  4. data/docs/bundles.md +1 -1
  5. data/docs/cli.md +17 -6
  6. data/docs/configuration.md +6 -1
  7. data/docs/context.md +8 -1
  8. data/docs/internals/attached-context.md +3 -1
  9. data/docs/internals/inline-widgets.md +2 -1
  10. data/docs/internals/participants.md +1 -5
  11. data/docs/internals/peers.md +15 -3
  12. data/docs/internals/remote-sessions.md +5 -2
  13. data/docs/internals/session-lifecycle.md +233 -18
  14. data/docs/internals/web-frontend.md +107 -9
  15. data/docs/peers.md +3 -1
  16. data/docs/plugins.md +2 -1
  17. data/docs/scheduling.md +31 -7
  18. data/docs/sessions.md +12 -12
  19. data/docs/sub-agent.md +1 -1
  20. data/docs/testing.md +1 -2
  21. data/docs/thin-clients.md +98 -12
  22. data/lib/samagotchi/bridge.rb +3 -4
  23. data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
  24. data/lib/samagotchi/bundles/mcp/plugin.rb +7 -398
  25. data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
  26. data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
  27. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  28. data/lib/samagotchi/client_id.rb +2 -19
  29. data/lib/samagotchi/compat.rb +1 -7
  30. data/lib/samagotchi/config.rb +3 -0
  31. data/lib/samagotchi/context_command.rb +5 -1
  32. data/lib/samagotchi/context_sources.rb +28 -0
  33. data/lib/samagotchi/continue_intent.rb +23 -43
  34. data/lib/samagotchi/delivery.rb +1 -5
  35. data/lib/samagotchi/hold_stop.rb +5 -2
  36. data/lib/samagotchi/participants.rb +13 -5
  37. data/lib/samagotchi/peer_say.rb +29 -14
  38. data/lib/samagotchi/plugin/sessions.rb +9 -5
  39. data/lib/samagotchi/schedule_command.rb +6 -1
  40. data/lib/samagotchi/schedules/prune_part.rb +162 -41
  41. data/lib/samagotchi/schedules.rb +32 -15
  42. data/lib/samagotchi/send_command.rb +6 -6
  43. data/lib/samagotchi/session.rb +7 -0
  44. data/lib/samagotchi/session_access/ask.rb +9 -3
  45. data/lib/samagotchi/session_access/exchange.rb +6 -2
  46. data/lib/samagotchi/session_access/local.rb +103 -32
  47. data/lib/samagotchi/session_access/remote.rb +10 -9
  48. data/lib/samagotchi/session_access/remote_peers.rb +2 -2
  49. data/lib/samagotchi/session_access.rb +17 -7
  50. data/lib/samagotchi/session_chain/delete.rb +100 -47
  51. data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
  52. data/lib/samagotchi/session_chain/successor.rb +78 -0
  53. data/lib/samagotchi/session_continue.rb +112 -42
  54. data/lib/samagotchi/session_inbox.rb +1 -1
  55. data/lib/samagotchi/session_inbox_delivery.rb +111 -25
  56. data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
  57. data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
  58. data/lib/samagotchi/session_lifecycle.rb +17 -2
  59. data/lib/samagotchi/session_manager/delivery.rb +61 -29
  60. data/lib/samagotchi/session_manager/emptiness.rb +5 -2
  61. data/lib/samagotchi/session_manager/lifecycle.rb +68 -19
  62. data/lib/samagotchi/session_manager/listing.rb +2 -1
  63. data/lib/samagotchi/session_manager/removal.rb +67 -13
  64. data/lib/samagotchi/session_manager/spawn.rb +16 -9
  65. data/lib/samagotchi/session_manager.rb +4 -0
  66. data/lib/samagotchi/session_retention.rb +28 -7
  67. data/lib/samagotchi/session_row.rb +2 -0
  68. data/lib/samagotchi/session_transition.rb +361 -0
  69. data/lib/samagotchi/sessions_command.rb +1 -1
  70. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
  71. data/lib/samagotchi/tools/cancel_schedule.rb +10 -6
  72. data/lib/samagotchi/version.rb +1 -1
  73. data/lib/samagotchi/web/app.rb +13 -3
  74. data/lib/samagotchi/web/ask_create.rb +138 -0
  75. data/lib/samagotchi/web/ask_routes.rb +56 -9
  76. data/lib/samagotchi/web/last_ask.rb +78 -0
  77. data/lib/samagotchi/web/peer_routes.rb +5 -2
  78. data/lib/samagotchi/web/public/answer_refs.js +3 -9
  79. data/lib/samagotchi/web/public/answer_write.js +28 -0
  80. data/lib/samagotchi/web/public/app.js +72 -96
  81. data/lib/samagotchi/web/public/block_fix.js +30 -12
  82. data/lib/samagotchi/web/public/chains.js +3 -1
  83. data/lib/samagotchi/web/public/command_list.js +5 -3
  84. data/lib/samagotchi/web/public/composer.js +9 -6
  85. data/lib/samagotchi/web/public/composer_mount.js +18 -11
  86. data/lib/samagotchi/web/public/draft_keys.js +10 -0
  87. data/lib/samagotchi/web/public/drafts.js +278 -0
  88. data/lib/samagotchi/web/public/owner_claim.js +7 -12
  89. data/lib/samagotchi/web/public/prompt_history.js +6 -0
  90. data/lib/samagotchi/web/public/send_turn.js +51 -20
  91. data/lib/samagotchi/web/public/session_actions.js +80 -16
  92. data/lib/samagotchi/web/public/session_prefs.js +9 -3
  93. data/lib/samagotchi/web/public/session_view.js +7 -2
  94. data/lib/samagotchi/web/public/turn_events.js +0 -2
  95. data/lib/samagotchi/web/public/unqueue.js +22 -9
  96. data/lib/samagotchi/web/public/viewer_self.js +1 -6
  97. data/lib/samagotchi/web/public/views.js +12 -1
  98. data/lib/samagotchi/web/session_access_routes.rb +11 -5
  99. data/lib/samagotchi/web/session_control.rb +8 -1
  100. data/lib/samagotchi/web/session_create.rb +51 -34
  101. data/lib/samagotchi/web/session_hub.rb +52 -36
  102. data/lib/samagotchi/web/session_summary.rb +45 -7
  103. data/lib/samagotchi/web/start_dir.rb +6 -4
  104. data/lib/samagotchi/worker_boot.rb +28 -6
  105. data/lib/samagotchi/worker_presence.rb +22 -6
  106. metadata +10 -1
@@ -0,0 +1,278 @@
1
+ // The composer's drafts: one per session window ("" the start page, else
2
+ // the session's id), and the one writer of the message box (#prompt) and its
3
+ // image chips. Every write of text or chips into the composer comes through
4
+ // here, by the key of the draft it is for: a pick, a quote, a failed turn's
5
+ // line back, the send's ack, a recalled history entry, a / pick. The ESLint
6
+ // block samagotchi/prompt-writes (eslint.config.js) keeps the other modules
7
+ // off #prompt's value and the mount's setChips.
8
+ //
9
+ // The box and the mount's chips are the draft on view (+painted+): the
10
+ // truth for it, read and written in place. The other drafts are kept here
11
+ // ({text, chips}, per tab). show(key) swaps them (session_view.js
12
+ // forgetSession, and app.js at boot): the outgoing draft is taken from the
13
+ // box and saved, the next one painted. A draft's text is saved in this
14
+ // browser on every write and input (session_prefs.js, COMPOSER_DRAFT:
15
+ // <key>:<session>, swept with the view settings; the start page's under
16
+ // START_DRAFT, one per browser): a reload or the Home Screen app's
17
+ // relaunch finds it. Chips live in memory only (a reload loses them); a
18
+ // chip's preview is freed on its ×, at its send, or when its draft is
19
+ // dropped, never on a swap.
20
+ //
21
+ // While a line is in flight (sending(key, line)), the saved copy leaves it
22
+ // out (restore.js composerAfterAck): a reload or an iOS kill right after
23
+ // Send doesn't bring back a line already sent.
24
+ //
25
+ // deps: +promptEl+ the message box; +view+ the mounted composer
26
+ // (composer_mount.js: chips(), setChips(list, {revoke}), resetHistory());
27
+ // +fit()+ its height; +shownKey()+ the key of the window on view; +reveal+:
28
+ // {openForVisit(), focusPrompt()} the composer's (composer_grip.js), for a
29
+ // write that must be seen; +revoke(src)+ frees a chip's preview.
30
+ //
31
+ // @return {text(key), chips(key), has(key), setText(key, text, opts),
32
+ // putBack(key, text), clear(key), putChips(key, list), move(from, to),
33
+ // handOver(from, to, opener), drop(key), show(key),
34
+ // syncFromView(), sending(key, line), settle(key, line)}
35
+ // A module of its own: new exports go in new modules (web-frontend.md).
36
+ import { COMPOSER_DRAFT, START_DRAFT } from "./draft_keys.js";
37
+ import { forget as forgetPref, read as readPref, write as writePref } from "./session_prefs.js";
38
+ import { readStored, removeStored, writeStored } from "./storage.js";
39
+ import { composerAfterAck, restoreInto } from "./restore.js";
40
+
41
+ export function createDrafts({
42
+ promptEl, view, fit = () => {}, shownKey = () => "", reveal = {},
43
+ revoke = (src) => URL.revokeObjectURL(src),
44
+ }) {
45
+ // The drafts not on view, by key: {text, chips, sent, base} (+sent+: the
46
+ // line in flight, left out of the saved copy; +base+: the text this tab
47
+ // last read, painted or saved for the key, so a switch saves only what this
48
+ // tab changed: another tab may have saved a newer text since).
49
+ const kept = new Map();
50
+ // The draft the box and the chips show; null until the first show (boot).
51
+ let painted = null;
52
+
53
+ function stored(key) {
54
+ return (key ? readPref(COMPOSER_DRAFT, key, "") : readStored(START_DRAFT, "")) ?? "";
55
+ }
56
+
57
+ // +key+'s entry, its text read from storage the first time this tab sees it.
58
+ function entry(key) {
59
+ let e = kept.get(key);
60
+ if (!e) {
61
+ e = { text: stored(key), chips: [], sent: null };
62
+ e.base = e.text;
63
+ kept.set(key, e);
64
+ }
65
+ return e;
66
+ }
67
+
68
+ // An older cached session_view.js never calls show(): the box stays the one
69
+ // for every window, as before, and is the draft of the window on view (an
70
+ // empty box over a draft kept for it paints that draft).
71
+ function heal() {
72
+ const key = shownKey();
73
+ if (painted === null || key === painted) return;
74
+ painted = key;
75
+ if (!promptEl.value.trim() && !view.chips().length && hasKept(entry(key))) paint(key);
76
+ }
77
+ const hasKept = (e) => !!(e.text.trim() || e.chips.length);
78
+
79
+ function paint(key) {
80
+ const e = entry(key);
81
+ promptEl.value = e.text;
82
+ e.base = e.text;
83
+ view.setChips(e.chips.slice(), { revoke: false });
84
+ fit();
85
+ view.resetHistory?.();
86
+ }
87
+
88
+ // The box's draft back into its entry (a copy: the mount changes its list in place).
89
+ function flush() {
90
+ const e = entry(painted);
91
+ e.text = promptEl.value;
92
+ e.chips = view.chips().slice();
93
+ }
94
+
95
+ // +key+'s text as saved: empty (white space only) is no key at all.
96
+ function save(key) {
97
+ const e = entry(key);
98
+ const current = key === painted ? promptEl.value : e.text;
99
+ e.base = current;
100
+ const text = e.sent ? composerAfterAck({ sent: e.sent, current }) : current;
101
+ if (!text.trim()) {
102
+ if (key) forgetPref(COMPOSER_DRAFT, key);
103
+ else removeStored(START_DRAFT);
104
+ } else if (key) writePref(COMPOSER_DRAFT, key, text);
105
+ else writeStored(START_DRAFT, text);
106
+ }
107
+
108
+ function caretToEnd() {
109
+ const end = promptEl.value.length;
110
+ promptEl.setSelectionRange?.(end, end);
111
+ promptEl.scrollTop = promptEl.scrollHeight;
112
+ }
113
+
114
+ function freePreviews(list, keep = []) {
115
+ for (const c of list) if (c.file && c.src && !keep.includes(c)) revoke(c.src);
116
+ }
117
+
118
+ function text(key = shownKey()) {
119
+ heal();
120
+ return key === painted ? promptEl.value : entry(key).text;
121
+ }
122
+
123
+ function chips(key = shownKey()) {
124
+ heal();
125
+ return key === painted ? view.chips() : entry(key).chips;
126
+ }
127
+
128
+ // Whether +key+'s draft holds anything: text or a chip.
129
+ function has(key = shownKey()) {
130
+ return !!(text(key).trim() || chips(key).length);
131
+ }
132
+
133
+ // +key+'s text becomes +value+, saved. On view only: +reveal+ opens the
134
+ // composer for this visit first, +focus+ gives the box the focus,
135
+ // +caretEnd+ puts the caret at the end.
136
+ function setText(key, value, { reveal: open = false, focus = false, caretEnd = false } = {}) {
137
+ heal();
138
+ const next = String(value ?? "");
139
+ if (key === painted) {
140
+ if (open) reveal.openForVisit?.();
141
+ promptEl.value = next;
142
+ fit();
143
+ if (focus) reveal.focusPrompt?.();
144
+ if (caretEnd) caretToEnd();
145
+ } else {
146
+ entry(key).text = next;
147
+ }
148
+ save(key);
149
+ }
150
+
151
+ // +text+ into +key+'s draft when it is empty, never over one: "shown" (on
152
+ // view: the composer opens, the box takes the focus), "stored", or false.
153
+ function putBack(key, text) {
154
+ if (has(key)) return false;
155
+ const shown = key === painted;
156
+ setText(key, text, { reveal: shown, focus: shown });
157
+ return shown ? "shown" : "stored";
158
+ }
159
+
160
+ // +key+'s text emptied; its chips stay.
161
+ function clear(key) {
162
+ setText(key, "");
163
+ }
164
+
165
+ // +key+'s chips become +list+ (a chip left out frees its preview).
166
+ function putChips(key, list) {
167
+ heal();
168
+ if (key === painted) {
169
+ view.setChips(list);
170
+ return;
171
+ }
172
+ const e = entry(key);
173
+ freePreviews(e.chips, list);
174
+ e.chips = list.slice();
175
+ }
176
+
177
+ // Empties +key+'s draft in place (its previews kept: they moved).
178
+ function empty(key) {
179
+ const e = entry(key);
180
+ e.sent = null;
181
+ if (key === painted) {
182
+ promptEl.value = "";
183
+ view.setChips([], { revoke: false });
184
+ fit();
185
+ } else {
186
+ e.text = "";
187
+ e.chips = [];
188
+ }
189
+ save(key);
190
+ }
191
+
192
+ // +from+'s draft goes to +to+: into an empty one as it is, else its text
193
+ // under +to+'s and its chips after them. +from+ is emptied, its saved copy
194
+ // gone.
195
+ function move(from, to) {
196
+ heal();
197
+ if (from === to) return;
198
+ const movedText = text(from);
199
+ const movedChips = chips(from).slice();
200
+ const sent = entry(from).sent;
201
+ empty(from);
202
+ if (!movedText.trim() && !movedChips.length) return;
203
+ const had = has(to);
204
+ const nextText = had ? restoreInto(movedText, text(to)) : movedText;
205
+ const nextChips = had ? chips(to).concat(movedChips) : movedChips;
206
+ if (!had) entry(to).sent = sent;
207
+ if (to === painted) {
208
+ promptEl.value = nextText;
209
+ view.setChips(nextChips, { revoke: false });
210
+ fit();
211
+ } else {
212
+ const e = entry(to);
213
+ e.text = nextText;
214
+ e.chips = nextChips;
215
+ }
216
+ save(to);
217
+ }
218
+
219
+ // Continue →: +from+'s draft goes to the next link +to+ (text or chips:
220
+ // the whole draft, from emptied); an empty one leaves +to+ the +opener+
221
+ // (none: nothing). A link with a draft of its own (one continued already)
222
+ // keeps it.
223
+ function handOver(from, to, opener = null) {
224
+ if (has(to)) return;
225
+ if (has(from)) move(from, to);
226
+ else if (opener) setText(to, opener);
227
+ }
228
+
229
+ // +key+'s draft forgotten, here and in storage (its session was deleted):
230
+ // its previews freed.
231
+ function drop(key) {
232
+ heal();
233
+ freePreviews(chips(key));
234
+ if (key === painted) {
235
+ promptEl.value = "";
236
+ view.setChips([], { revoke: false });
237
+ fit();
238
+ }
239
+ kept.delete(key);
240
+ if (key) forgetPref(COMPOSER_DRAFT, key);
241
+ else removeStored(START_DRAFT);
242
+ }
243
+
244
+ // The window +key+ on view: the box and the chips swap to its draft. The
245
+ // same key again changes nothing.
246
+ function show(key) {
247
+ if (key === painted) return;
248
+ if (painted !== null) {
249
+ flush();
250
+ // A pure switch writes nothing: only text this tab changed is saved.
251
+ if (entry(painted).text !== entry(painted).base) save(painted);
252
+ }
253
+ painted = key;
254
+ paint(key);
255
+ }
256
+
257
+ // The box's input: saved as the draft on view.
258
+ function syncFromView() {
259
+ heal();
260
+ if (painted !== null) save(painted);
261
+ }
262
+
263
+ // +line+ is in flight from +key+'s draft: the saved copy leaves it out.
264
+ function sending(key, line) {
265
+ entry(key).sent = line;
266
+ save(key);
267
+ }
268
+
269
+ // The send of +line+ is over: the saved copy is the draft as it is again.
270
+ function settle(key, line) {
271
+ const e = entry(key);
272
+ if (e.sent !== line) return;
273
+ e.sent = null;
274
+ save(key);
275
+ }
276
+
277
+ return { text, chips, has, setText, putBack, clear, putChips, move, handOver, drop, show, syncFromView, sending, settle };
278
+ }
@@ -1,5 +1,4 @@
1
1
  import { ownerClaimHash, participantClaimLink, selectionClaim, showClaim, turnIdOf } from "./claim_link.js";
2
- import { sessionHash } from "./route.js";
3
2
 
4
3
  // Where a claim's text can be on the owner's page: answers, own messages, steps.
5
4
  export const CLAIM_ROOTS = ".bubble.output, .bubble.user .user-message, .gen-text";
@@ -13,12 +12,12 @@ export const CLAIM_ROOTS = ".bubble.output, .bubble.user .user-message, .gen-tex
13
12
  // +historyEl+: #history. +store+: the page's session store (selected,
14
13
  // current.dir, sessions; read only). +claimTarget()+: participants_panel.js's
15
14
  // (the people a link would open for, or null). +toast(text, action,
16
- // options)+, +copyText(text)+ (copy.js), +allViewOpen()+: the page's.
17
- // +pagesAreaOpen()+: the page's pages check (views.js's: a page at #/p/NAME
18
- // or the Pages list at #/pages open). +home+: the home folder the page shows
19
- // as ~ (body data-home). +win+: the window (location, history).
15
+ // options)+, +copyText(text)+ (copy.js), +keepSessionHash(id)+: the page's
16
+ // rule for keeping the session's hash in the URL (views.js's: a no-op while
17
+ // All sessions or a pages view owns it). +home+: the home folder the page
18
+ // shows as ~ (body data-home). +win+: the window (location, history).
20
19
  export function createOwnerClaim({
21
- historyEl, store, claimTarget, toast, copyText, allViewOpen, pagesAreaOpen = () => false, home = "", win = globalThis,
20
+ historyEl, store, claimTarget, toast, copyText, keepSessionHash = () => {}, home = "", win = globalThis,
22
21
  }) {
23
22
  // A claim link waiting for its session to be drawn (selectFromHash sets it).
24
23
  let pendingClaim = null;
@@ -36,16 +35,12 @@ export function createOwnerClaim({
36
35
  // participant page shows it (the session's folder relative, home ~); a
37
36
  // quote not found marks its turn; neither found: the top, and a toast.
38
37
  // The hash is the session's again (select() already set it when it
39
- // switched), so the same link opened again is a hashchange. Not while a
40
- // pages view is open either: #/pages or #/p/NAME stays, as All sessions does.
38
+ // switched), so the same link opened again is a hashchange.
41
39
  function showPending() {
42
40
  const claim = pendingClaim;
43
41
  if (!claim || claim.sessionId !== store.selected) return;
44
42
  pendingClaim = null;
45
- const { location, history } = win;
46
- if (!allViewOpen() && !pagesAreaOpen() && location.hash !== sessionHash(store.selected)) {
47
- history.replaceState(null, "", location.pathname + location.search + sessionHash(store.selected));
48
- }
43
+ keepSessionHash(store.selected);
49
44
  if (showClaim(historyEl, claim, { roots: CLAIM_ROOTS, opts: opts() })) return;
50
45
  historyEl.scrollTop = 0;
51
46
  toast("The linked turn isn't in this session.");
@@ -32,6 +32,12 @@ export function createPromptHistory() {
32
32
  }
33
33
 
34
34
  return {
35
+ // Browsing ends: the next ↑ starts from the newest entry again.
36
+ reset() {
37
+ index = -1;
38
+ lastSet = null;
39
+ },
40
+
35
41
  // The list as fetched; a place in it survives while its entry does.
36
42
  setEntries(list) {
37
43
  entries = Array.isArray(list) ? list.map(String) : [];
@@ -19,9 +19,14 @@ import { composerAfterAck, dropEarlyRestores, restoreOnAck } from "./restore.js"
19
19
  // page's scope folder (new sessions start there); +startPage+
20
20
  // (start_page.js: pendingPick(), chosenModel(), newChoices(),
21
21
  // clearChoices()); +takeDelivery()+ the delivery line's armed choice;
22
- // +composer+: {el (the text box), button (Send/Start), chips(),
23
- // setChips(list), fit(), clear(), openForVisit(), focus(), noteDraft(id) (the
24
- // text was written in session id)}; +historyEl+,
22
+ // +composer+: the drafts by key ("" the start page, else the session's id:
23
+ // drafts.js): {text(key), chips(key), setText(key, text), putChips(key,
24
+ // list), clear(key), move(from, to), sending(key, line) and settle(key,
25
+ // line) (the line in flight, left out of the draft's saved copy until the
26
+ // send is over)}, and button (Send/Start), fit(), openForVisit(), focus().
27
+ // An older cached app.js passes the one box instead: {el (the text box),
28
+ // chips(), setChips(list), clear()}; read and written as it is then.
29
+ // +historyEl+,
25
30
  // +turnDom+; +makeUserBubble(prompt, opts)+, +bubbleById(id)+,
26
31
  // +addCommandBubble(view)+; +addCreatedSession(created)+, +select(id)+,
27
32
  // +showNotice(text)+,
@@ -43,8 +48,20 @@ export function createSendTurn({
43
48
  addCreatedSession, select, showNotice, followWokenWorker, refreshHistory,
44
49
  leaveGoneSession, runCommand, toast, closesOn = null, alert = (text) => globalThis.alert(text),
45
50
  }) {
46
- const promptEl = composer.el;
47
51
  const actionBtn = composer.button;
52
+ // The draft of +key+, read and written by that key (drafts.js); an older
53
+ // cached app.js's one box (no text()) for every key.
54
+ const drafted = typeof composer.text === "function";
55
+ const textOf = (key) => (drafted ? composer.text(key) : composer.el.value);
56
+ const chipsOf = (key) => composer.chips(key);
57
+ function setTextOf(key, text) {
58
+ if (drafted) composer.setText(key, text);
59
+ else composer.el.value = text; // eslint-disable-line no-restricted-syntax -- an older cached app.js: its one box, no drafts to write through
60
+ }
61
+ function putChipsOf(key, list) {
62
+ if (drafted) composer.putChips(key, list);
63
+ else composer.setChips(list);
64
+ }
48
65
 
49
66
  // A create's refusal: a picked kit's in the 🧰 list, else an alert.
50
67
  function refused(e) {
@@ -61,21 +78,25 @@ export function createSendTurn({
61
78
  // /queue wins over both.
62
79
  async function send({ delivery: chosen = null } = {}) {
63
80
  if (store.current.owner === "tui") return;
81
+ // The draft the line is sent from ("" the start page): every read and
82
+ // write below is by this key, never the session open by then (the page
83
+ // may switch while the pick, the create, an upload or the /turn waits).
84
+ let key = session() ?? "";
64
85
  // A fresh link's pick first: its /model is queued in the worker once
65
86
  // acked, and the worker runs queued commands before a turn's input.
66
87
  const pick = startPage.pendingPick();
67
88
  if (pick) await pick;
68
- let prompt = turnText(promptEl.value, composer.chips());
89
+ let prompt = turnText(textOf(key), chipsOf(key));
69
90
  if (!prompt) return;
70
91
  const armed = takeDelivery();
71
92
  // An image-only send's text is its placeholders: not for ↑.
72
- const typed = promptEl.value.trim() !== "";
93
+ const typed = textOf(key).trim() !== "";
73
94
  // The composer holds the line as typed until the ack (composerAfterAck).
74
95
  const line = prompt;
75
96
  // Where the line goes (composer.js): the page's own reply, a session to
76
97
  // make first (the start page), a command, or a turn.
77
98
  let route = routeComposerLine(prompt, {
78
- session: !!session(), running: running(), commands: store.current.commands, images: composer.chips().length,
99
+ session: !!key, running: running(), commands: store.current.commands, images: chipsOf(key).length,
79
100
  });
80
101
  if (route.create) {
81
102
  actionBtn.disabled = true;
@@ -85,6 +106,10 @@ export function createSendTurn({
85
106
  // The choice was for this chat only.
86
107
  startPage.clearChoices();
87
108
  addCreatedSession(s);
109
+ // The start page's draft is the new session's now, before the page
110
+ // opens it (a failed create moves nothing).
111
+ composer.move?.("", s.id);
112
+ key = s.id;
88
113
  await select(s.id);
89
114
  } catch (e) {
90
115
  refused(e);
@@ -93,19 +118,19 @@ export function createSendTurn({
93
118
  actionBtn.disabled = false;
94
119
  }
95
120
  // In the new session, by its command list (select read it).
96
- route = routeSessionLine(route.send, { commands: store.current.commands, images: composer.chips().length, delivery: route.delivery });
121
+ route = routeSessionLine(route.send, { commands: store.current.commands, images: chipsOf(key).length, delivery: route.delivery });
97
122
  }
98
123
  if (route.reply) {
99
124
  // The start page's reply is a notice: no session is made for it (2.29).
100
- if (session()) addCommandBubble({ label: null, line: route.line, text: route.reply });
125
+ if (key) addCommandBubble({ label: null, line: route.line, text: route.reply });
101
126
  else showNotice(route.reply);
102
- composer.clear();
127
+ composer.clear(key);
103
128
  return;
104
129
  }
105
130
  if (route.command && runCommand) return runCommand(route.command);
106
131
  // A viewer without session commands (viewer.js): the line goes as a turn, and the worker decides.
107
132
  prompt = route.command ?? route.send;
108
- if (inFlight && inFlight.to === session() && inFlight.line === line) return;
133
+ if (inFlight && inFlight.to === key && inFlight.line === line) return;
109
134
  const isRunning = running() && !!session();
110
135
  const delivery = route.delivery || (isRunning ? chosen || armed : null);
111
136
  const hint = historyEl.querySelector(".hint");
@@ -113,7 +138,7 @@ export function createSendTurn({
113
138
  // Sending moves a finished turn out of the stage at once (else the echo
114
139
  // would land among its rows); into a running one it is a queued row there.
115
140
  turnDom.handOff({ immediate: true });
116
- const sending = composer.chips().slice();
141
+ const sending = chipsOf(key).slice();
117
142
  // Into a running turn the echo says what it waits for (its delivery); a
118
143
  // line with images, or a Verify or a Revise, always waits for the turn's
119
144
  // end (the worker queues it).
@@ -127,9 +152,12 @@ export function createSendTurn({
127
152
  actionBtn.disabled = true;
128
153
  // The session the line goes to: the page may switch to another while an
129
154
  // image uploads or the turn is sent, and what follows stays with this one.
130
- const sentTo = session();
155
+ const sentTo = key;
131
156
  const flight = { to: sentTo, line };
132
157
  inFlight = flight;
158
+ // Until the ack, the draft's saved copy leaves the line out: a reload
159
+ // (or an iOS kill) right after Send doesn't bring it back to send again.
160
+ composer.sending?.(sentTo, line);
133
161
  try {
134
162
  await uploadChips(sending, sentTo, { upload: api.uploadImage });
135
163
  const verifyAnchor = verify();
@@ -146,7 +174,7 @@ export function createSendTurn({
146
174
  // The worker knew it as a command (its list was newer than ours):
147
175
  // its command_ran shows it, not a prompt bubble.
148
176
  echo.remove();
149
- composer.clear();
177
+ composer.clear(sentTo);
150
178
  if (sentTo === session()) followWokenWorker();
151
179
  return;
152
180
  }
@@ -169,14 +197,13 @@ export function createSendTurn({
169
197
  if (restored?.refill && restored.verify && sentTo === session()) restoreVerify(restored.verify);
170
198
  // And its Revise draft, so the resend still stacks.
171
199
  if (restored?.refill && restored.revises && sentTo === session()) restoreRevise(restored.revises);
172
- const before = promptEl.value;
173
- promptEl.value = composerAfterAck({ sent: line, current: before, refill: restored?.refill || "" });
174
- // A line back from sentTo is a draft of that session, wherever the page is now.
175
- if (promptEl.value.trim() && promptEl.value !== before) composer.noteDraft(sentTo);
200
+ const before = textOf(sentTo);
201
+ const after = composerAfterAck({ sent: line, current: before, refill: restored?.refill || "" });
202
+ setTextOf(sentTo, after);
176
203
  composer.fit();
177
204
  // Only the sent chips go: images added since stay (they go up at their own send).
178
- const kept = chipsAfterSend(composer.chips(), sending);
179
- composer.setChips(restored?.images?.length ? kept.concat(restoredChips(sentTo, restored.images)) : kept);
205
+ const kept = chipsAfterSend(chipsOf(sentTo), sending);
206
+ putChipsOf(sentTo, restored?.images?.length ? kept.concat(restoredChips(sentTo, restored.images)) : kept);
180
207
  // Its turn_enqueued usually tagged the echo already; the ack covers a
181
208
  // worker that wasn't live (no turn_enqueued then).
182
209
  if (ack?.enqueued_id && !echo.dataset.enqueuedId && !bubbleById(ack.enqueued_id)) {
@@ -188,6 +215,9 @@ export function createSendTurn({
188
215
  // worker process). A switch since then opened the other session's own.
189
216
  if (sentTo === session()) followWokenWorker();
190
217
  } catch (e) {
218
+ // The line is the draft's again, saved as it is (before a gone
219
+ // session's draft moves to the start page's).
220
+ composer.settle?.(sentTo, line);
191
221
  echo.remove();
192
222
  // The turn never reached the worker's queue: an early restore waiting
193
223
  // for its ack is never taken back (4.13), so drop it here. The prompt
@@ -213,6 +243,7 @@ export function createSendTurn({
213
243
  if (REVISE_REFUSALS.includes(e.code) && sentTo === session()) return addCommandBubble({ label: null, line, text: e.body?.detail || e.message, failed: true });
214
244
  toast(cantText("send", e), null, { sticky: true });
215
245
  } finally {
246
+ composer.settle?.(sentTo, line);
216
247
  if (inFlight === flight) inFlight = null;
217
248
  actionBtn.disabled = store.current.owner === "tui";
218
249
  composer.focus();