samagotchi 0.181.0 → 0.183.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 (200) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -1
  3. data/bin/chi +4 -1
  4. data/docs/architecture.md +17 -7
  5. data/docs/bundle-authoring.md +1 -1
  6. data/docs/bundles.md +103 -4
  7. data/docs/cli.md +58 -10
  8. data/docs/configuration.md +3 -1
  9. data/docs/guardrails.md +16 -1
  10. data/docs/internals/block-renderers.md +172 -3
  11. data/docs/internals/bridge.md +23 -5
  12. data/docs/internals/bundles.md +21 -4
  13. data/docs/internals/engine.md +29 -8
  14. data/docs/internals/inline-widgets.md +3 -1
  15. data/docs/internals/prompt-caching.md +9 -1
  16. data/docs/internals/remote-sessions.md +38 -7
  17. data/docs/internals/web-frontend.md +116 -26
  18. data/docs/internals/working-set.md +1 -1
  19. data/docs/mcp-server.md +80 -0
  20. data/docs/plugins.md +308 -21
  21. data/docs/sessions.md +7 -4
  22. data/docs/sub-agent.md +9 -0
  23. data/docs/testing.md +3 -1
  24. data/docs/thin-clients.md +146 -0
  25. data/lib/samagotchi/answer_display.rb +3 -2
  26. data/lib/samagotchi/block_fix/stamp.rb +49 -0
  27. data/lib/samagotchi/block_fix.rb +220 -0
  28. data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
  29. data/lib/samagotchi/bridge/pending_card.rb +4 -3
  30. data/lib/samagotchi/bridge/sse_writer.rb +16 -15
  31. data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
  32. data/lib/samagotchi/bridge.rb +69 -69
  33. data/lib/samagotchi/bridge_client.rb +9 -0
  34. data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
  35. data/lib/samagotchi/bundle_command/check.rb +36 -19
  36. data/lib/samagotchi/bundle_command.rb +50 -21
  37. data/lib/samagotchi/bundle_manifest.rb +101 -4
  38. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  39. data/lib/samagotchi/bundles/mcp/client.rb +260 -0
  40. data/lib/samagotchi/bundles/mcp/content.rb +107 -0
  41. data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
  42. data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
  43. data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
  44. data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
  45. data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
  46. data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
  47. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  48. data/lib/samagotchi/cli/parent_wait.rb +5 -3
  49. data/lib/samagotchi/command_intake.rb +7 -0
  50. data/lib/samagotchi/commands/registry.rb +8 -3
  51. data/lib/samagotchi/config.rb +3 -0
  52. data/lib/samagotchi/continue_intent.rb +8 -0
  53. data/lib/samagotchi/continue_offer.rb +24 -15
  54. data/lib/samagotchi/engine/block_fix.rb +332 -0
  55. data/lib/samagotchi/engine/cards.rb +14 -5
  56. data/lib/samagotchi/engine/events.rb +10 -15
  57. data/lib/samagotchi/engine/models.rb +1 -1
  58. data/lib/samagotchi/engine/plugins.rb +3 -0
  59. data/lib/samagotchi/engine/recap.rb +1 -1
  60. data/lib/samagotchi/engine/side_requests.rb +8 -6
  61. data/lib/samagotchi/engine/sign_in.rb +142 -0
  62. data/lib/samagotchi/engine/turn_end.rb +3 -3
  63. data/lib/samagotchi/engine.rb +47 -27
  64. data/lib/samagotchi/engine_snapshots.rb +1 -1
  65. data/lib/samagotchi/events.rb +1 -0
  66. data/lib/samagotchi/guardrail_wiring.rb +3 -1
  67. data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
  68. data/lib/samagotchi/guardrails/verdict.rb +3 -2
  69. data/lib/samagotchi/hold_stop.rb +4 -4
  70. data/lib/samagotchi/idle_client.rb +10 -6
  71. data/lib/samagotchi/idle_recap.rb +19 -11
  72. data/lib/samagotchi/idle_reminders.rb +7 -5
  73. data/lib/samagotchi/idle_scheduler.rb +7 -10
  74. data/lib/samagotchi/inline_widgets.rb +12 -0
  75. data/lib/samagotchi/installed_bundles/dir.rb +37 -0
  76. data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
  77. data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
  78. data/lib/samagotchi/installed_bundles/record.rb +3 -2
  79. data/lib/samagotchi/installed_bundles/standing.rb +3 -1
  80. data/lib/samagotchi/kernel_loop.rb +7 -5
  81. data/lib/samagotchi/llm/chat_loop.rb +2 -0
  82. data/lib/samagotchi/llm/http.rb +96 -5
  83. data/lib/samagotchi/markdown_parser.rb +28 -0
  84. data/lib/samagotchi/mcp_command.rb +110 -0
  85. data/lib/samagotchi/mcp_server/http.rb +112 -0
  86. data/lib/samagotchi/mcp_server/protocol.rb +244 -0
  87. data/lib/samagotchi/mcp_server/stdio.rb +92 -0
  88. data/lib/samagotchi/mcp_server/text.rb +155 -0
  89. data/lib/samagotchi/mcp_server/tools.rb +245 -0
  90. data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
  91. data/lib/samagotchi/memory_bundle/builder.rb +20 -2
  92. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  93. data/lib/samagotchi/memory_bundle/installer.rb +16 -3
  94. data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
  95. data/lib/samagotchi/memory_bundle/shas.rb +6 -4
  96. data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
  97. data/lib/samagotchi/memory_bundle/status.rb +15 -6
  98. data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
  99. data/lib/samagotchi/plugin/api.rb +6 -1
  100. data/lib/samagotchi/plugin/card.rb +36 -11
  101. data/lib/samagotchi/plugin/context.rb +63 -6
  102. data/lib/samagotchi/plugin/http.rb +40 -15
  103. data/lib/samagotchi/plugin/http_errors.rb +15 -0
  104. data/lib/samagotchi/plugin/http_stream.rb +86 -0
  105. data/lib/samagotchi/plugin/http_transport.rb +58 -16
  106. data/lib/samagotchi/plugin/loader.rb +18 -11
  107. data/lib/samagotchi/plugin/secrets.rb +266 -0
  108. data/lib/samagotchi/plugin/sign_in.rb +392 -0
  109. data/lib/samagotchi/plugin/sse_reader.rb +103 -0
  110. data/lib/samagotchi/queued_command.rb +28 -4
  111. data/lib/samagotchi/recap_setup.rb +13 -4
  112. data/lib/samagotchi/ref.rb +3 -2
  113. data/lib/samagotchi/relay_verifier.rb +8 -7
  114. data/lib/samagotchi/relay_watcher.rb +4 -4
  115. data/lib/samagotchi/run_options.rb +2 -1
  116. data/lib/samagotchi/send_command.rb +54 -41
  117. data/lib/samagotchi/session_access/ask.rb +244 -0
  118. data/lib/samagotchi/session_access/contract.rb +8 -0
  119. data/lib/samagotchi/session_access/delete_plan.rb +77 -0
  120. data/lib/samagotchi/session_access/exchange/token.rb +101 -0
  121. data/lib/samagotchi/session_access/exchange.rb +173 -0
  122. data/lib/samagotchi/session_access/local.rb +38 -6
  123. data/lib/samagotchi/session_access/remote.rb +23 -2
  124. data/lib/samagotchi/session_access.rb +1 -0
  125. data/lib/samagotchi/session_chain/delete.rb +39 -81
  126. data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
  127. data/lib/samagotchi/session_command_catalog.rb +10 -0
  128. data/lib/samagotchi/session_commands.rb +17 -0
  129. data/lib/samagotchi/session_config.rb +4 -4
  130. data/lib/samagotchi/session_delete_command.rb +129 -3
  131. data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
  132. data/lib/samagotchi/session_manager/listing.rb +4 -4
  133. data/lib/samagotchi/session_manager/removal.rb +38 -12
  134. data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
  135. data/lib/samagotchi/session_manager.rb +1 -0
  136. data/lib/samagotchi/session_observer.rb +27 -0
  137. data/lib/samagotchi/steer_cut.rb +4 -3
  138. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
  139. data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
  140. data/lib/samagotchi/testing/boundary.rb +4 -0
  141. data/lib/samagotchi/testing/bundle.rb +48 -2
  142. data/lib/samagotchi/testing/fake_http.rb +60 -15
  143. data/lib/samagotchi/testing/host.rb +4 -1
  144. data/lib/samagotchi/testing/recorder.rb +45 -5
  145. data/lib/samagotchi/tools/tool_index.rb +14 -3
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +20 -6
  148. data/lib/samagotchi/web/ask_routes.rb +292 -0
  149. data/lib/samagotchi/web/block_fix_routes.rb +39 -0
  150. data/lib/samagotchi/web/markdown_renderer.rb +24 -9
  151. data/lib/samagotchi/web/mcp_route.rb +88 -0
  152. data/lib/samagotchi/web/message_display.rb +6 -1
  153. data/lib/samagotchi/web/participant_view.rb +1 -1
  154. data/lib/samagotchi/web/public/app.js +7 -8
  155. data/lib/samagotchi/web/public/block_fix.js +270 -0
  156. data/lib/samagotchi/web/public/block_fix_note.js +104 -0
  157. data/lib/samagotchi/web/public/block_frame.js +77 -13
  158. data/lib/samagotchi/web/public/block_host.js +45 -9
  159. data/lib/samagotchi/web/public/block_regions.js +126 -0
  160. data/lib/samagotchi/web/public/block_renderers.css +12 -0
  161. data/lib/samagotchi/web/public/block_watch.js +16 -2
  162. data/lib/samagotchi/web/public/card.js +22 -9
  163. data/lib/samagotchi/web/public/card_actions.js +21 -0
  164. data/lib/samagotchi/web/public/card_kinds.js +1 -1
  165. data/lib/samagotchi/web/public/card_registry.js +3 -2
  166. data/lib/samagotchi/web/public/data.js +15 -12
  167. data/lib/samagotchi/web/public/delete_sheet.js +222 -162
  168. data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
  169. data/lib/samagotchi/web/public/format.js +3 -0
  170. data/lib/samagotchi/web/public/index.html +52 -7
  171. data/lib/samagotchi/web/public/inline_widgets.js +1 -1
  172. data/lib/samagotchi/web/public/more_sheet.js +6 -4
  173. data/lib/samagotchi/web/public/picture_regions.js +243 -0
  174. data/lib/samagotchi/web/public/picture_viewer.js +113 -12
  175. data/lib/samagotchi/web/public/refs.js +5 -3
  176. data/lib/samagotchi/web/public/region_hit.js +144 -0
  177. data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
  178. data/lib/samagotchi/web/public/running_tasks.js +6 -4
  179. data/lib/samagotchi/web/public/session_actions.js +105 -100
  180. data/lib/samagotchi/web/public/stream_handlers.js +5 -1
  181. data/lib/samagotchi/web/public/turn_block.js +5 -2
  182. data/lib/samagotchi/web/server.rb +6 -4
  183. data/lib/samagotchi/web/session_access_routes.rb +14 -11
  184. data/lib/samagotchi/web/session_control.rb +84 -30
  185. data/lib/samagotchi/web/session_create.rb +2 -2
  186. data/lib/samagotchi/worker/block_fixes.rb +22 -0
  187. data/lib/samagotchi/worker/child_side.rb +3 -2
  188. data/lib/samagotchi/worker/command_queue.rb +13 -13
  189. data/lib/samagotchi/worker/leave.rb +3 -3
  190. data/lib/samagotchi/worker/scratch.rb +6 -6
  191. data/lib/samagotchi/worker/signal_grace.rb +6 -5
  192. data/lib/samagotchi/worker/startup.rb +3 -3
  193. data/lib/samagotchi/worker/wake_turns.rb +4 -4
  194. data/lib/samagotchi/worker.rb +42 -24
  195. data/lib/samagotchi/worker_exit_claim.rb +1 -1
  196. data/lib/samagotchi/worker_idle_exit.rb +18 -9
  197. data/lib/samagotchi/worker_inbound.rb +12 -7
  198. data/lib/samagotchi/worker_participant_gate.rb +9 -7
  199. data/lib/samagotchi/worker_task_wakes.rb +7 -5
  200. metadata +42 -1
@@ -0,0 +1,126 @@
1
+ // A drawn diagram's regions, cleaned: what a renderer reported beside its SVG
2
+ // (block_frame.js), made safe to keep. Pure: no DOM, `node --test`.
3
+ // docs/internals/block-renderers.md, Regions.
4
+ //
5
+ // A region: {kind, id, label, box?, points?, from?, to?}; boxes and points are
6
+ // fractions of the picture (0..1). The bundle is trusted, its input is
7
+ // model-written: so nothing here stringifies the raw data (structured clone keeps
8
+ // cycles and BigInt), only an own key of a known name is read, strings are only
9
+ // ever strings, and the result is new objects. Over the caps the list goes whole
10
+ // (null), never cut: a picture with half its nodes tappable lies.
11
+ //
12
+ // What the page may do with a region's text: textContent, an aria-label, and
13
+ // composer text. Never markup, a selector, a URL, a data-* value another module
14
+ // reads back, or a key into an object.
15
+ //
16
+ // A module of its own: its exports are new (docs/internals/web-frontend.md).
17
+
18
+ export const REGION_KINDS = new Set(["node", "group", "edge", "note"]);
19
+ export const MAX_REGIONS = 600;
20
+ // The budget over the cleaned fields: the kept strings' lengths, plus 12 per number.
21
+ export const MAX_REGION_BYTES = 128 * 1024;
22
+ const NUMBER_BYTES = 12;
23
+ const ID_CHARS = 80;
24
+ const LABEL_CHARS = 120;
25
+ const MIN_POINTS = 2;
26
+ const MAX_POINTS = 16;
27
+
28
+ const own = (obj, key) => (Object.hasOwn(obj, key) ? obj[key] : undefined);
29
+
30
+ function plainObject(v) {
31
+ if (v === null || typeof v !== "object" || Array.isArray(v)) return false;
32
+ const proto = Object.getPrototypeOf(v);
33
+ return proto === null || proto === Object.prototype || Object.getPrototypeOf(proto) === null;
34
+ }
35
+
36
+ const clamp = (n) => Math.min(1, Math.max(0, n));
37
+ const finite = (n) => typeof n === "number" && Number.isFinite(n);
38
+
39
+ // One line of text, or "": controls and line separators a space, bidi and
40
+ // zero-width format characters and lone surrogates gone, whitespace runs one
41
+ // space, cut by code points (a cut never splits a pair: Ruby's JSON refuses a
42
+ // lone surrogate, and the text can reach a prompt). The raw string is sliced
43
+ // first, so a megabyte label costs what a short one does.
44
+ export function cleanText(raw, max) {
45
+ if (typeof raw !== "string") return "";
46
+ const text = raw.slice(0, max * 8)
47
+ .replace(/[\p{Cc}\p{Zl}\p{Zp}]/gu, " ")
48
+ .replace(/[\p{Cf}\p{Cs}]/gu, "")
49
+ .replace(/\s+/gu, " ")
50
+ .trim();
51
+ const points = Array.from(text);
52
+ return points.length > max ? points.slice(0, max).join("").trimEnd() : text;
53
+ }
54
+
55
+ // [x, y, w, h] clamped and cut to the picture, or null: no area, or not 4
56
+ // finite numbers.
57
+ function cleanBox(raw) {
58
+ if (!Array.isArray(raw) || raw.length !== 4 || !raw.every(finite)) return null;
59
+ const x = clamp(raw[0]);
60
+ const y = clamp(raw[1]);
61
+ const w = Math.min(clamp(raw[2]), 1 - x);
62
+ const h = Math.min(clamp(raw[3]), 1 - y);
63
+ return w > 0 && h > 0 ? [x, y, w, h] : null;
64
+ }
65
+
66
+ function cleanPoints(raw) {
67
+ if (!Array.isArray(raw) || raw.length < MIN_POINTS || raw.length > MAX_POINTS) return null;
68
+ const out = [];
69
+ for (const p of raw) {
70
+ if (!Array.isArray(p) || p.length !== 2 || !finite(p[0]) || !finite(p[1])) return null;
71
+ out.push([clamp(p[0]), clamp(p[1])]);
72
+ }
73
+ return out;
74
+ }
75
+
76
+ function cleanRegion(raw) {
77
+ if (!plainObject(raw)) return null;
78
+ const kind = own(raw, "kind");
79
+ if (typeof kind !== "string" || !REGION_KINDS.has(kind)) return null;
80
+ const region = { kind, id: cleanText(own(raw, "id"), ID_CHARS), label: cleanText(own(raw, "label"), LABEL_CHARS) };
81
+ const box = cleanBox(own(raw, "box"));
82
+ if (kind !== "edge") {
83
+ if (!box) return null;
84
+ region.box = box;
85
+ return region;
86
+ }
87
+ const points = cleanPoints(own(raw, "points"));
88
+ if (!points) return null;
89
+ region.points = points;
90
+ if (box) region.box = box;
91
+ region.from = cleanText(own(raw, "from"), ID_CHARS);
92
+ region.to = cleanText(own(raw, "to"), ID_CHARS);
93
+ return region;
94
+ }
95
+
96
+ // Something the card and a button can call it by, from the diagram's own text: a
97
+ // node, note or group its label or id, an edge its label or both its ends. A
98
+ // nameless one is skipped: the page never makes up a name ("node 3" means
99
+ // nothing in the source, nor to the model in composer text).
100
+ function namedRegion(r) {
101
+ return r.kind === "edge" ? !!(r.label || (r.from && r.to)) : !!(r.label || r.id);
102
+ }
103
+
104
+ function weight(region) {
105
+ const texts = region.id.length + region.label.length + (region.from?.length ?? 0) + (region.to?.length ?? 0);
106
+ const numbers = (region.box ? 4 : 0) + (region.points ? region.points.length * 2 : 0);
107
+ return texts + numbers * NUMBER_BYTES;
108
+ }
109
+
110
+ // @param raw what the frame's done carried (any value)
111
+ // @return a new array of clean regions, or null: none sent, not an array, over
112
+ // MAX_REGIONS entries or past MAX_REGION_BYTES; entries that aren't regions, or
113
+ // are nameless (namedRegion), are skipped
114
+ export function cleanRegions(raw) {
115
+ if (!Array.isArray(raw) || raw.length > MAX_REGIONS) return null;
116
+ const out = [];
117
+ let budget = MAX_REGION_BYTES;
118
+ for (let i = 0; i < raw.length; i++) {
119
+ const region = cleanRegion(raw[i]);
120
+ if (!region || !namedRegion(region)) continue;
121
+ budget -= weight(region);
122
+ if (budget < 0) return null;
123
+ out.push(region);
124
+ }
125
+ return out;
126
+ }
@@ -24,3 +24,15 @@
24
24
  .block-note{margin:4px 0 12px;font-size:12px;color:var(--text-3);white-space:normal}
25
25
  .block-note{cursor:copy}
26
26
  .block-note::before{content:attr(data-text)}
27
+ /* A failed block asked for again (block_fix_note.js): the line keeps one line's height while its words change, wraps only at a phone's width; "asking for a fix…" has a pulsing dot; Ask in chat is a pill whose label is CSS (data-label), so nothing of the page's own is in the bubble's text. */
28
+ .block-note[data-fix]{display:flex;flex-wrap:wrap;align-items:center;gap:2px 10px;min-height:28px}
29
+ .block-note[data-fix="asking"]::after{content:"";display:inline-block;flex:none;width:6px;height:6px;margin-left:-4px;border-radius:50%;background:var(--accent);animation:block-fix-pulse 1s steps(13) infinite}
30
+ @keyframes block-fix-pulse{50%{opacity:.25}}
31
+ @media (prefers-reduced-motion:reduce){.block-note[data-fix="asking"]::after{animation:none}}
32
+ .block-fix-ask{flex:none;font:inherit;font-size:12px;font-weight:500;line-height:1.5;color:var(--accent);background:none;border:1px solid var(--border-strong);border-radius:999px;padding:3px 10px;cursor:pointer}
33
+ .block-fix-ask::before{content:attr(data-label)}
34
+ .block-fix-ask:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
35
+ @media(hover:hover){.block-fix-ask:hover{border-color:var(--accent)}}
36
+ @media (pointer:coarse),(max-width:700px){.block-fix-ask{min-height:44px;padding:0 14px}}
37
+ /* "Drawn after a fix" under a fixed block that drew: muted, not a copy target. */
38
+ .block-note.fixed{margin-top:4px;cursor:default}
@@ -78,13 +78,23 @@ export function holdingView(pre, roots, mutate, bottomOf, win = window) {
78
78
  return result;
79
79
  }
80
80
 
81
+ // How much of a drawer's error a block keeps for a fix's request
82
+ // (BlockFix::MAX_REASON_CHARS): a Mermaid error's useful part is after its
83
+ // first line, which alone the line shows.
84
+ const MAX_REASON = 300;
85
+
81
86
  // The <pre> stays as it is, under one muted line: never markup (the words are
82
87
  // a drawer's, driven by the model's input), and shown by CSS, so a bubble's
83
88
  // textContent, its dedupe and an annotation's text stay what they were (copy.js's
84
89
  // rule for its buttons). A click on the line copies it (the watcher's listener).
85
- export function failBlock(pre, lang, message) {
90
+ // +fixable+: the failure is the model's (a refused chi-ui block, a renderer's
91
+ // own error), so a fix may be asked for (block_fix.js). Either way a bubbling
92
+ // "block-failed" event goes out from the <pre>.
93
+ export function failBlock(pre, lang, message, { fixable = false } = {}) {
86
94
  if (!pre.isConnected) return;
87
95
  pre.dataset.blockFailed = "1";
96
+ if (fixable) pre.dataset.blockFixable = "1";
97
+ pre.dataset.blockReason = String(message ?? "").trim().slice(0, MAX_REASON);
88
98
  const note = pre.ownerDocument.createElement("div");
89
99
  note.className = "block-note";
90
100
  const first = String(message ?? "").split("\n")[0].trim();
@@ -94,6 +104,7 @@ export function failBlock(pre, lang, message) {
94
104
  note.setAttribute("aria-label", words);
95
105
  note.title = "Click to copy";
96
106
  pre.after(note);
107
+ pre.dispatchEvent(new (pre.ownerDocument.defaultView?.CustomEvent || CustomEvent)("block-failed", { bubbles: true }));
97
108
  }
98
109
 
99
110
  // A copy.js wrap took the <pre> away from a figure placed first: back with it.
@@ -247,7 +258,10 @@ export function createBlockWatch({ roots, sessionId, drawers, watch = null, copy
247
258
  setSourceShown(figure, figure.nextElementSibling, !figure.classList.contains("source"));
248
259
  return;
249
260
  }
250
- const note = e.target.closest?.(".block-note");
261
+ // A button in the line (block_fix_note.js) is its own, not a copy, and
262
+ // "Drawn after a fix" (.block-note.fixed) has no error to copy.
263
+ if (e.target.closest?.(".block-note button")) return;
264
+ const note = e.target.closest?.(".block-note:not(.fixed)");
251
265
  if (note) copy(note.dataset.text);
252
266
  });
253
267
  }
@@ -1,10 +1,12 @@
1
1
  // A plugin's card (docs/plugins.md, Cards): a title, a body and actions,
2
- // each action a command line the session runs. app.js places and updates
3
- // the element; this module draws it (lit-html templates) and says where it
4
- // goes.
2
+ // each action a command line the session runs or an address opened in a new
3
+ // tab (a url action, card_actions.js: it sends nothing to the session).
4
+ // app.js places and updates the element; this module draws it (lit-html
5
+ // templates) and says where it goes.
5
6
  import { html, nothing, render } from "./vendor/lit-html.js";
6
7
  import { unsafeHTML } from "./vendor/directives/unsafe-html.js";
7
8
  import { recapPlace } from "./format.js";
9
+ import { cardActions, cardActionUrl } from "./card_actions.js";
8
10
 
9
11
  // The card's class: a warn card wears the warning colour; a notice is a
10
12
  // one-line row.
@@ -22,7 +24,7 @@ export const NOTICE_CHARS = 160;
22
24
  // one that asks (actions) and one with more to read stay cards.
23
25
  export function isNoticeCard(card) {
24
26
  if (!card || card.level === "warn") return false;
25
- if ((card.actions || []).some((a) => a && a.command)) return false;
27
+ if (cardActions(card).length) return false;
26
28
  const body = String(card.body ?? "").trim();
27
29
  return !body.includes("\n") && body.length <= NOTICE_CHARS;
28
30
  }
@@ -69,17 +71,28 @@ export function cardBodyTemplate(card) {
69
71
 
70
72
  // The card: its head (title, the command that showed it after a reload,
71
73
  // source), body and one button per action (the command as its tooltip and
72
- // data), then the line a failed action writes.
74
+ // data; a url action an anchor to a new tab, its host as the tooltip, with
75
+ // a line under the row naming each host the url actions open, so a label
76
+ // can't hide where it leads), then the line a failed action writes. The
77
+ // anchor is .card-link, never .card-action: what selects .card-action posts
78
+ // the command or counts the card as asking.
73
79
  export function cardTemplate(card) {
74
80
  const source = card.source ? html`<span class="card-source">${card.source}</span>` : nothing;
75
81
  const label = card.label ? html`<span class="card-label">${card.label}</span>` : nothing;
76
82
  const body = cardBodyTemplate(card);
77
- const actions = (card.actions || []).filter((a) => a && a.command);
83
+ const actions = cardActions(card);
84
+ const hosts = [];
78
85
  const buttons = actions.length
79
- ? html`<div class="card-actions">${actions.map((a) =>
80
- html`<button type="button" class="card-action ghost" data-command=${a.command} title=${a.command}>${a.label || a.command}</button>`)}</div>`
86
+ ? html`<div class="card-actions">${actions.map((a) => {
87
+ if (a.command) return html`<button type="button" class="card-action ghost" data-command=${a.command} title=${a.command}>${a.label || a.command}</button>`;
88
+ const url = cardActionUrl(a);
89
+ const host = new URL(url).host;
90
+ if (!hosts.includes(host)) hosts.push(host);
91
+ return html`<a class="card-link ghost" href=${url} target="_blank" rel="noopener noreferrer" title=${host}>${a.label}</a>`;
92
+ })}</div>`
81
93
  : nothing;
82
- return html`<div class="card-head"><span class="card-title">${card.title || ""}</span>${label}${source}</div>${body ? html`<div class="card-body">${body}</div>` : nothing}${buttons}<div class="card-error hidden"></div>`;
94
+ const opens = hosts.length ? html`<div class="card-opens">opens ${hosts.join(", ")}</div>` : nothing;
95
+ return html`<div class="card-head"><span class="card-title">${card.title || ""}</span>${label}${source}</div>${body ? html`<div class="card-body">${body}</div>` : nothing}${buttons}${opens}<div class="card-error hidden"></div>`;
83
96
  }
84
97
 
85
98
  // A card element → the card it shows, as drawCard drew it.
@@ -0,0 +1,21 @@
1
+ // A card's actions as the page draws them (card.js): a command one, which
2
+ // the session runs, or a url one, an address opened in a new tab.
3
+ import { httpUrl } from "./refs.js";
4
+
5
+ // The longest address a card's url action opens: the server's
6
+ // Plugin::Card::MAX_URL (an authorize address can be long).
7
+ export const CARD_MAX_URL = 4096;
8
+
9
+ // A url action's address as the page opens it, else null: re-checked here,
10
+ // as a saved cards.json is seeded without the server's check and lit-html
11
+ // doesn't sanitize href. A url action needs its label (never the address).
12
+ export function cardActionUrl(action) {
13
+ if (!action || action.command || !String(action.label ?? "").trim()) return null;
14
+ return httpUrl(action.url, { max: CARD_MAX_URL });
15
+ }
16
+
17
+ // The card's actions the page draws: a command one, or a url one whose
18
+ // address passes cardActionUrl.
19
+ export function cardActions(card) {
20
+ return (card?.actions || []).filter((a) => a && (a.command || cardActionUrl(a)));
21
+ }
@@ -33,7 +33,7 @@ export function continueWait() {
33
33
  return { kind: "continue", approval: false, summary: "" };
34
34
  }
35
35
 
36
- // A plugin card's wait: a running turn's card with an action asks (as
36
+ // A plugin card's wait: a running turn's card with a command action asks (as
37
37
  // check-in's), a warning stays in sight; null: it doesn't wait. The one rule
38
38
  // cards_place.js upsertCard and turn_block.js drawnTurnNodes ask; the DOM
39
39
  // readers (stage_cards.js, stage_waiting.js waitingKind, card.js
@@ -45,8 +45,9 @@ export const CARD_EVENTS = [
45
45
  "continue_offered", "continue_resolved", "card",
46
46
  ];
47
47
 
48
- // A card's actions as the entry keeps them: label and command only.
49
- const actionsOf = (card) => (Array.isArray(card?.actions) ? card.actions.filter(Boolean).map((a) => ({ label: a.label ?? null, command: a.command ?? null })) : []);
48
+ // A card's actions as the entry keeps them: label and command, or label
49
+ // and url for a url action.
50
+ const actionsOf = (card) => (Array.isArray(card?.actions) ? card.actions.filter(Boolean).map((a) => (a.url && !a.command ? { label: a.label ?? null, url: a.url } : { label: a.label ?? null, command: a.command ?? null })) : []);
50
51
 
51
52
  export function createCardRegistry() {
52
53
  const entries = new Map();
@@ -271,10 +271,11 @@ export function restartSession(id, opts = {}) {
271
271
  // Hides the session (and its delegates) from the lists, kept for good; the
272
272
  // server stops a live idle worker first and answers 409 while a turn runs.
273
273
  // stopTasks: stop the background tasks it and its delegates started first
274
- // (without it, a session whose tasks still run is refused: 409 tasks_running).
274
+ // (without it, a session whose tasks still run is refused: 409 tasks_running);
275
+ // with +tasks+ (the ids the sheet named) only those, and a refusal on another.
275
276
  export function archiveSession(id, opts = {}) {
276
- const { stopTasks, ...apiOpts } = opts;
277
- const body = JSON.stringify(stopTasks ? { stop_tasks: true } : {});
277
+ const { stopTasks, tasks, ...apiOpts } = opts;
278
+ const body = JSON.stringify(stopTasks ? { stop_tasks: true, ...(Array.isArray(tasks) ? { tasks } : {}) } : {});
278
279
  return api(`/api/sessions/${encodeURIComponent(id)}/archive`, { method: "POST", body, ...apiOpts });
279
280
  }
280
281
 
@@ -285,30 +286,32 @@ export function unarchiveSession(id, opts = {}) {
285
286
 
286
287
  // A delete's query after force=true: the plan digest the user confirmed
287
288
  // (expect), and stop_tasks (stop the background tasks the deleted sessions
288
- // started first; without it, 409 tasks_running while one runs).
289
- function deleteQuery({ expect, stopTasks }) {
289
+ // started first; without it, 409 tasks_running while one runs); +tasks+ (the
290
+ // ids the sheet named, "tasks=" for none) has the stop take only those.
291
+ function deleteQuery({ expect, stopTasks, tasks }) {
290
292
  const plan = expect == null ? "" : `&expect=${encodeURIComponent(expect)}`;
291
- return `${plan}${stopTasks ? "&stop_tasks=true" : ""}`;
293
+ const named = stopTasks && Array.isArray(tasks) ? `&tasks=${tasks.map(encodeURIComponent).join(",")}` : "";
294
+ return `${plan}${stopTasks ? "&stop_tasks=true" : ""}${named}`;
292
295
  }
293
296
 
294
297
  // Deletes the session for good; force=true has the server stop a live
295
298
  // worker first (the page's confirm says so; without it, 409 worker_running).
296
299
  export function deleteSession(id, opts = {}) {
297
- const { expect, stopTasks, ...apiOpts } = opts;
298
- return api(`/api/sessions/${encodeURIComponent(id)}?force=true${deleteQuery({ expect, stopTasks })}`,
300
+ const { expect, stopTasks, tasks, ...apiOpts } = opts;
301
+ return api(`/api/sessions/${encodeURIComponent(id)}?force=true${deleteQuery({ expect, stopTasks, tasks })}`,
299
302
  { method: "DELETE", ...apiOpts });
300
303
  }
301
304
 
302
305
  // Deletes every link of the session's chain with their delegates (running
303
306
  // ones are left running); force: the links' workers are stopped first.
304
307
  export function deleteChain(id, opts = {}) {
305
- const { expect, stopTasks, ...apiOpts } = opts;
306
- return api(`/api/sessions/${encodeURIComponent(id)}/chain?force=true${deleteQuery({ expect, stopTasks })}`,
308
+ const { expect, stopTasks, tasks, ...apiOpts } = opts;
309
+ return api(`/api/sessions/${encodeURIComponent(id)}/chain?force=true${deleteQuery({ expect, stopTasks, tasks })}`,
307
310
  { method: "DELETE", ...apiOpts });
308
311
  }
309
312
 
310
- // What deleting a chained session would do, for the delete sheet
311
- // (delete_sheet.js): the server's own plan and refusals, read now.
313
+ // What deleting a session would do, for the delete sheet (delete_sheet.js):
314
+ // the server's own plan and refusals, read now.
312
315
  export function deletePlan(id, opts = {}) {
313
316
  return api(`/api/sessions/${encodeURIComponent(id)}/delete_plan`, opts);
314
317
  }