@clagentic/console 1.9.0 → 1.10.0-beta.2

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 (172) hide show
  1. package/LICENSE +117 -12
  2. package/LICENSES/MIT-legacy.txt +22 -0
  3. package/LICENSES/README.md +15 -0
  4. package/README.md +9 -3
  5. package/bin/cli.js +40 -5
  6. package/deploy/clagentic-console.service +10 -6
  7. package/lib/agent-dispatch-tool.js +20 -0
  8. package/lib/audit.js +3 -2
  9. package/lib/build-changes.js +79 -0
  10. package/lib/build-identity.js +192 -0
  11. package/lib/build-monitor.js +125 -0
  12. package/lib/build-sha.json +2 -2
  13. package/lib/build-update-check.js +138 -0
  14. package/lib/build-update-offer.js +141 -0
  15. package/lib/build-update-restart.js +142 -0
  16. package/lib/build-update-standing.js +184 -0
  17. package/lib/cli/daemon-launch.js +25 -22
  18. package/lib/cli/ipc-subcommands.js +6 -0
  19. package/lib/cli/session-subcommands.js +122 -0
  20. package/lib/cli-sessions.js +8 -1
  21. package/lib/codex-defaults.js +8 -2
  22. package/lib/config.js +32 -20
  23. package/lib/daemon-env-settings.js +84 -0
  24. package/lib/daemon-projects.js +57 -0
  25. package/lib/daemon.js +288 -205
  26. package/lib/drain.js +74 -17
  27. package/lib/env-compat.js +61 -0
  28. package/lib/extension-commands.js +45 -0
  29. package/lib/history-visibility.js +4 -0
  30. package/lib/idle-adapter-reaper.js +71 -0
  31. package/lib/link-trap.js +78 -0
  32. package/lib/mcp-local.js +8 -6
  33. package/lib/mcp-tool-input.js +116 -0
  34. package/lib/mcp-tool-schema.js +179 -0
  35. package/lib/memory-limits.js +61 -5
  36. package/lib/memory-setting-validate.js +65 -0
  37. package/lib/memory-shed.js +13 -32
  38. package/lib/new-work-gate.js +47 -0
  39. package/lib/os-users.js +5 -3
  40. package/lib/project-access.js +139 -0
  41. package/lib/project-connection.js +37 -22
  42. package/lib/project-external-trigger.js +166 -105
  43. package/lib/project-file-scope.js +172 -0
  44. package/lib/project-file-watch.js +86 -61
  45. package/lib/project-filesystem.js +144 -128
  46. package/lib/project-git.js +119 -0
  47. package/lib/project-http.js +44 -28
  48. package/lib/project-loop.js +17 -9
  49. package/lib/project-mcp.js +53 -72
  50. package/lib/project-message-gate.js +70 -0
  51. package/lib/project-notifications.js +31 -4
  52. package/lib/project-prompt-responses.js +217 -0
  53. package/lib/project-sessions.js +192 -355
  54. package/lib/project-user-message.js +27 -15
  55. package/lib/project.js +212 -147
  56. package/lib/projects-dir.js +90 -0
  57. package/lib/prompt-access.js +22 -0
  58. package/lib/prompt-kinds/answer-limits.js +15 -0
  59. package/lib/prompt-kinds/ask-user.js +58 -0
  60. package/lib/prompt-kinds/codecs.js +97 -0
  61. package/lib/prompt-kinds/elicitation.js +77 -0
  62. package/lib/prompt-kinds/extension.js +39 -0
  63. package/lib/prompt-kinds/index.js +68 -0
  64. package/lib/prompt-kinds/permission.js +114 -0
  65. package/lib/prompt-kinds/plan.js +79 -0
  66. package/lib/prompt-kinds/tool-call.js +38 -0
  67. package/lib/prompt-registry.js +680 -0
  68. package/lib/public/app.js +1 -1
  69. package/lib/public/css/diagnostics.css +80 -0
  70. package/lib/public/css/input.css +17 -3
  71. package/lib/public/css/menus.css +10 -0
  72. package/lib/public/css/messages.css +5 -0
  73. package/lib/public/css/rewind.css +22 -0
  74. package/lib/public/index.html +4 -12
  75. package/lib/public/modules/agent-dispatch-tool.js +9 -0
  76. package/lib/public/modules/app-connection.js +7 -0
  77. package/lib/public/modules/app-cursors.js +7 -5
  78. package/lib/public/modules/app-messages.js +52 -47
  79. package/lib/public/modules/app-notifications.js +47 -71
  80. package/lib/public/modules/app-panels.js +61 -59
  81. package/lib/public/modules/app-projects.js +2 -1
  82. package/lib/public/modules/app-rendering.js +4 -10
  83. package/lib/public/modules/build-update-detail.js +54 -0
  84. package/lib/public/modules/diagnostics.js +170 -20
  85. package/lib/public/modules/filebrowser.js +18 -16
  86. package/lib/public/modules/history-visibility.js +4 -0
  87. package/lib/public/modules/mention.js +3 -2
  88. package/lib/public/modules/mode-options.js +38 -0
  89. package/lib/public/modules/project-settings.js +19 -8
  90. package/lib/public/modules/prompt-banner.js +78 -0
  91. package/lib/public/modules/prompt-card.js +201 -0
  92. package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
  93. package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
  94. package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
  95. package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
  96. package/lib/public/modules/prompt-kinds/index.js +18 -0
  97. package/lib/public/modules/prompt-kinds/own-key.js +29 -0
  98. package/lib/public/modules/prompt-kinds/package.json +3 -0
  99. package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
  100. package/lib/public/modules/prompt-kinds/permission.js +209 -0
  101. package/lib/public/modules/prompt-kinds/plan.js +102 -0
  102. package/lib/public/modules/prompt-state.js +142 -0
  103. package/lib/public/modules/prompts.js +134 -0
  104. package/lib/public/modules/rewind.js +4 -1
  105. package/lib/public/modules/server-settings.js +2 -4
  106. package/lib/public/modules/sidebar-mobile.js +19 -9
  107. package/lib/public/modules/sidebar-sessions.js +21 -14
  108. package/lib/public/modules/sticky-notes.js +3 -2
  109. package/lib/public/modules/team-panel.js +5 -4
  110. package/lib/public/modules/tools.js +81 -906
  111. package/lib/public/modules/vendor-picker.js +53 -0
  112. package/lib/public/modules/vendor-registry.js +142 -0
  113. package/lib/public/sw.js +3 -1
  114. package/lib/rc-migrate.js +44 -0
  115. package/lib/runtime-env-keys.js +109 -0
  116. package/lib/runtime-env.js +236 -0
  117. package/lib/sdk-bridge.js +348 -284
  118. package/lib/sdk-message-processor.js +117 -146
  119. package/lib/sdk-skill-discovery.js +65 -60
  120. package/lib/server-hub-sessions.js +13 -10
  121. package/lib/server-palette.js +28 -19
  122. package/lib/server.js +156 -115
  123. package/lib/service-install.js +96 -0
  124. package/lib/session-capabilities.js +76 -0
  125. package/lib/session-id.js +33 -0
  126. package/lib/session-input-ipc.js +32 -0
  127. package/lib/session-input.js +379 -0
  128. package/lib/session-reclaim.js +284 -0
  129. package/lib/session-resume.js +172 -0
  130. package/lib/sessions.js +61 -30
  131. package/lib/supervisor.js +39 -0
  132. package/lib/turn-initiator.js +108 -0
  133. package/lib/users-permissions.js +9 -6
  134. package/lib/users.js +9 -0
  135. package/lib/utils.js +4 -2
  136. package/lib/vendor-auth.js +22 -0
  137. package/lib/vendor-registry-message.js +66 -0
  138. package/lib/ws-origin.js +99 -0
  139. package/lib/ws-schema.js +36 -23
  140. package/lib/yoke/adapters/claude-capabilities.js +38 -0
  141. package/lib/yoke/adapters/claude-vendor.js +34 -0
  142. package/lib/yoke/adapters/claude-worker.js +57 -87
  143. package/lib/yoke/adapters/claude.js +182 -88
  144. package/lib/yoke/adapters/codex-capabilities.js +40 -0
  145. package/lib/yoke/adapters/codex-vendor.js +46 -0
  146. package/lib/yoke/adapters/codex.js +462 -211
  147. package/lib/yoke/adapters/effort-control.js +34 -0
  148. package/lib/yoke/adapters/permission-relay.js +58 -0
  149. package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
  150. package/lib/yoke/auth-checks.js +91 -0
  151. package/lib/yoke/capabilities.js +49 -0
  152. package/lib/yoke/codex-app-server.js +30 -56
  153. package/lib/yoke/codex-binary.js +254 -0
  154. package/lib/yoke/codex-command.js +122 -0
  155. package/lib/yoke/codex-protocol.js +96 -0
  156. package/lib/yoke/codex-version.js +135 -0
  157. package/lib/yoke/env-ownership.js +40 -0
  158. package/lib/yoke/index.js +84 -174
  159. package/lib/yoke/install-probe.js +136 -0
  160. package/lib/yoke/instructions.js +7 -11
  161. package/lib/yoke/interface.js +206 -4
  162. package/lib/yoke/package.json +1 -1
  163. package/lib/yoke/provider-credentials.js +96 -0
  164. package/lib/yoke/registry.js +377 -0
  165. package/package.json +12 -9
  166. package/scripts/check-sdk-lock-drift.js +160 -0
  167. package/scripts/check-test-count.js +76 -1
  168. package/scripts/postinstall.js +218 -136
  169. package/scripts/prompt-harness-history.js +202 -0
  170. package/scripts/verify-installed-build.js +59 -17
  171. package/scripts/verify-installed.js +67 -0
  172. package/lib/sdk-permission-ownership.js +0 -136
@@ -0,0 +1,201 @@
1
+ // prompt-card.js - the answer state machine every surface that answers an
2
+ // operator prompt shares (the transcript card and the notification banner),
3
+ // and the card's terminal rendering.
4
+ //
5
+ // A surface never displays an outcome ("Allowed", "Submitted") until the
6
+ // server has confirmed it. Answering only moves it to a "sending" state; the
7
+ // final label is applied when the server's prompt_resolved (or prompt_cancel)
8
+ // arrives, or, for a banner, the server dismisses it. If the socket is down,
9
+ // the connection drops, or no confirmation arrives in time, the surface
10
+ // returns to its answerable state so the operator can retry.
11
+ //
12
+ // Deliberately imports nothing but the import-free own-key.js, so it can be
13
+ // exercised without the rest of the frontend module graph.
14
+
15
+ import { ownValue } from './prompt-kinds/own-key.js';
16
+
17
+ export var SENDING_CLASS = "sending";
18
+ export var ACK_TIMEOUT_MS = 10000;
19
+
20
+ var NOTE_CLASS = "permission-sending-note";
21
+ var DEFAULT_CONTROLS = "button, input, select, textarea";
22
+ var DEFAULT_ACTIONS = ".permission-actions";
23
+ // container -> { timer, disabled: controls this send disabled, actions }
24
+ var sends = new WeakMap();
25
+ // container -> { controls, actions } for a surface that is not a card.
26
+ var surfaces = new WeakMap();
27
+
28
+ function surfaceOf(container) {
29
+ return surfaces.get(container) || { controls: DEFAULT_CONTROLS, actions: DEFAULT_ACTIONS };
30
+ }
31
+
32
+ // Ends the send in flight: its timer stops and the controls it disabled are
33
+ // live again (those already disabled before it stay as they were).
34
+ function endSend(container, reenable) {
35
+ var send = sends.get(container);
36
+ if (!send) return;
37
+ sends.delete(container);
38
+ clearTimeout(send.timer);
39
+ if (reenable) send.disabled.forEach(function (el) { el.disabled = false; });
40
+ }
41
+
42
+ function removeNote(container) {
43
+ var notes = container.querySelectorAll("." + NOTE_CLASS);
44
+ for (var i = 0; i < notes.length; i++) notes[i].remove();
45
+ }
46
+
47
+ function addNote(container, text, isError) {
48
+ removeNote(container);
49
+ var note = document.createElement("div");
50
+ note.className = NOTE_CLASS + (isError ? " permission-note-error" : "");
51
+ note.textContent = text;
52
+ var actions = container.querySelector(surfaceOf(container).actions);
53
+ if (actions && actions.parentNode) {
54
+ actions.parentNode.appendChild(note);
55
+ } else {
56
+ container.appendChild(note);
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Declare a surface other than a card: which of its controls an answer
62
+ * locks, and where its notes go.
63
+ * @param {Element} container
64
+ * @param {{controls: string, actions: string}} opts - selectors.
65
+ */
66
+ export function definePromptSurface(container, opts) {
67
+ surfaces.set(container, { controls: opts.controls, actions: opts.actions });
68
+ }
69
+
70
+ /** True while a card is awaiting server confirmation. */
71
+ export function isUnconfirmed(container) {
72
+ return container.classList.contains(SENDING_CLASS);
73
+ }
74
+
75
+ /** True once a card shows its final outcome. */
76
+ export function isSettled(container) {
77
+ return container.classList.contains("resolved");
78
+ }
79
+
80
+ /**
81
+ * Restore a sending card to its answerable state.
82
+ * @param {Element} container
83
+ * @param {string} [message] - shown as an inline note explaining why.
84
+ */
85
+ export function restorePromptCard(container, message) {
86
+ if (isSettled(container)) {
87
+ endSend(container, false);
88
+ return;
89
+ }
90
+ endSend(container, true);
91
+ container.classList.remove(SENDING_CLASS);
92
+ if (message) addNote(container, message, true);
93
+ else removeNote(container);
94
+ }
95
+
96
+ /**
97
+ * Restore every surface under root that is awaiting a confirmation (the
98
+ * connection that would have carried it is gone).
99
+ * @param {Element} root
100
+ * @param {string} [message]
101
+ */
102
+ export function restoreUnconfirmedUnder(root, message) {
103
+ var waiting = root.querySelectorAll("." + SENDING_CLASS);
104
+ for (var i = 0; i < waiting.length; i++) restorePromptCard(waiting[i], message);
105
+ }
106
+
107
+ /**
108
+ * Submit an answer for a card without claiming it took effect.
109
+ *
110
+ * @param {Element} container
111
+ * @param {function(): boolean} trySend - sends the payload; returns false when
112
+ * the socket is not usable.
113
+ * @param {object} [opts]
114
+ * @param {number} [opts.ackTimeoutMs]
115
+ * @param {function()} [opts.onSent]
116
+ * @returns {boolean} true when the answer was handed to the socket.
117
+ */
118
+ export function submitPromptResponse(container, trySend, opts) {
119
+ opts = opts || {};
120
+ if (isSettled(container) || isUnconfirmed(container)) return false;
121
+
122
+ var sent = false;
123
+ try {
124
+ sent = !!trySend();
125
+ } catch (e) {
126
+ // A failure of the send itself is not a dropped connection, and saying so
127
+ // would send the operator to reconnect for nothing.
128
+ console.error("[prompt-card] sending an answer failed:", e);
129
+ addNote(container, "Could not send the answer (" + ((e && e.message) || "unexpected error") + "). Choose again to retry.", true);
130
+ return false;
131
+ }
132
+ if (!sent) {
133
+ addNote(container, "Not connected. Reconnect, then choose again.", true);
134
+ return false;
135
+ }
136
+
137
+ container.classList.add(SENDING_CLASS);
138
+ var disabled = [];
139
+ var controls = container.querySelectorAll(surfaceOf(container).controls);
140
+ for (var i = 0; i < controls.length; i++) {
141
+ if (controls[i].disabled) continue;
142
+ controls[i].disabled = true;
143
+ disabled.push(controls[i]);
144
+ }
145
+ addNote(container, "Sending...", false);
146
+ if (opts.onSent) opts.onSent();
147
+
148
+ var timeoutMs = opts.ackTimeoutMs || ACK_TIMEOUT_MS;
149
+ sends.set(container, {
150
+ disabled: disabled,
151
+ timer: setTimeout(function () {
152
+ restorePromptCard(container, "No confirmation from the server. Choose again to retry.");
153
+ }, timeoutMs),
154
+ });
155
+ return true;
156
+ }
157
+
158
+ /** Called when the server confirms or ends the prompt. */
159
+ export function settlePromptCard(container) {
160
+ endSend(container, false);
161
+ container.classList.remove(SENDING_CLASS);
162
+ removeNote(container);
163
+ }
164
+
165
+ var TONE_CLASSES = { allowed: "resolved-allowed", denied: "resolved-denied", cancelled: "resolved-cancelled" };
166
+
167
+ /**
168
+ * The terminal rendering every card shares: the card's actions are replaced
169
+ * by the outcome label and no control stays live.
170
+ * @param {Element} container
171
+ * @param {{text: string, tone: "allowed"|"denied"|"cancelled"}} outcome
172
+ */
173
+ export function showOutcome(container, outcome) {
174
+ container.classList.add("resolved", ownValue(TONE_CLASSES, outcome.tone) || TONE_CLASSES.cancelled);
175
+ var actions = container.querySelector(".permission-actions");
176
+ if (actions) {
177
+ // The label is text, never markup, whatever a kind's outcome returns.
178
+ var label = document.createElement("span");
179
+ label.className = "permission-decision-label";
180
+ label.textContent = outcome.text;
181
+ actions.textContent = "";
182
+ actions.appendChild(label);
183
+ }
184
+ var feedbackRow = container.querySelector(".plan-feedback-row");
185
+ if (feedbackRow) feedbackRow.remove();
186
+ var controls = container.querySelectorAll("button, input, select, textarea");
187
+ for (var i = 0; i < controls.length; i++) controls[i].disabled = true;
188
+ }
189
+
190
+ // How a prompt that ended without an answer reads, by reason. "stale" means
191
+ // the server no longer holds it (answered elsewhere, or its process ended
192
+ // before recording an outcome).
193
+ var CANCEL_LABELS = {
194
+ stale: "No longer active (already resolved or expired)",
195
+ expired: "Expired",
196
+ shutdown: "Ended by a daemon restart",
197
+ };
198
+
199
+ export function cancelledLabel(reason) {
200
+ return ownValue(CANCEL_LABELS, reason) || "Cancelled";
201
+ }
@@ -0,0 +1,131 @@
1
+ // ask-user-codec.js - the one definition of an AskUserQuestion answer, shared
2
+ // by the card (this directory) and the server (lib/prompt-kinds/ask-user.js).
3
+ //
4
+ // This file is the only copy: the server imports it as is
5
+ // (lib/prompt-kinds/codecs.js).
6
+
7
+ // How the CLI joins several labels into the one string its tool takes for
8
+ // a question; older histories recorded that joined string.
9
+ var MULTI_SELECT_SEPARATOR = ", ";
10
+ // Bound on "Other" text (lib/prompt-kinds/answer-limits.js applies the same).
11
+ var MAX_ANSWER_CHARS = 10000;
12
+
13
+ function questionList(input) {
14
+ return input && Array.isArray(input.questions) ? input.questions : [];
15
+ }
16
+
17
+ // A question's choices: its option labels, each once, in the order first
18
+ // offered. Options sharing a label are one choice, because the tool is
19
+ // answered with the label and cannot tell them apart.
20
+ function choiceLabels(question) {
21
+ var out = [];
22
+ var options = question && Array.isArray(question.options) ? question.options : [];
23
+ for (var i = 0; i < options.length; i++) {
24
+ var label = options[i] && options[i].label;
25
+ if (typeof label === "string" && out.indexOf(label) === -1) out.push(label);
26
+ }
27
+ return out;
28
+ }
29
+
30
+ // One question's answer as the operator gave it, or null when it is not a
31
+ // usable answer to that question: for a multi-select question the chosen
32
+ // labels (each one of its choices, once, in the order given); else non-blank
33
+ // text within the bound, trimmed (a choice's label or "Other" text).
34
+ function normalizeAnswer(question, value) {
35
+ if (Array.isArray(value)) {
36
+ if (!question || !question.multiSelect) return null;
37
+ var labels = choiceLabels(question);
38
+ var picked = [];
39
+ for (var i = 0; i < value.length; i++) {
40
+ if (labels.indexOf(value[i]) !== -1 && picked.indexOf(value[i]) === -1) picked.push(value[i]);
41
+ }
42
+ return picked.length ? picked : null;
43
+ }
44
+ if (typeof value !== "string" || value.length > MAX_ANSWER_CHARS) return null;
45
+ var text = value.trim();
46
+ return text ? text : null;
47
+ }
48
+
49
+ // Every usable answer, keyed by question index, limited to the questions
50
+ // asked.
51
+ function normalizeAnswers(input, answers) {
52
+ var out = {};
53
+ var given = answers && typeof answers === "object" && !Array.isArray(answers) ? answers : {};
54
+ var questions = questionList(input);
55
+ for (var i = 0; i < questions.length; i++) {
56
+ var answer = normalizeAnswer(questions[i], given[i]);
57
+ if (answer !== null) out[i] = answer;
58
+ }
59
+ return out;
60
+ }
61
+
62
+ // The answer a card's state stands for: the chosen labels of a multi-select
63
+ // question (in choice order), the chosen label of any other, else the
64
+ // "Other" text; null when the operator gave nothing.
65
+ function answerFromCard(question, chosen, other) {
66
+ var labels = choiceLabels(question).filter(function (label) { return chosen.indexOf(label) !== -1; });
67
+ if (labels.length) return normalizeAnswer(question, question && question.multiSelect ? labels : labels[0]);
68
+ return normalizeAnswer(question, other == null ? "" : String(other));
69
+ }
70
+
71
+ // One answer as the single string the CLI's tool takes.
72
+ function answerText(value) {
73
+ return Array.isArray(value) ? value.join(MULTI_SELECT_SEPARATOR) : String(value);
74
+ }
75
+
76
+ // The answers keyed by question text, as the CLI's AskUserQuestion tool
77
+ // takes them. Questions that share their text share that one slot; each
78
+ // answer is kept, in question order, rather than the last one winning.
79
+ function toolAnswers(input, answers) {
80
+ var out = {};
81
+ var questions = questionList(input);
82
+ for (var i = 0; i < questions.length; i++) {
83
+ var text = questions[i] && questions[i].question;
84
+ if (typeof text !== "string" || !text || answers[i] == null) continue;
85
+ var value = answerText(answers[i]);
86
+ var prior = Object.prototype.hasOwnProperty.call(out, text) ? out[text] : null;
87
+ // Defined, not assigned, so a question text such as "__proto__" is a key.
88
+ Object.defineProperty(out, text, {
89
+ value: prior === null ? value : prior + MULTI_SELECT_SEPARATOR + value,
90
+ enumerable: true, writable: true, configurable: true,
91
+ });
92
+ }
93
+ return out;
94
+ }
95
+
96
+ // The labels an older history's joined multi-select answer was made of, or
97
+ // null when value is not a join of distinct choices (it was "Other" text).
98
+ // Labels may themselves contain the separator, so every split is tried.
99
+ function splitJoinedLabels(value, labels) {
100
+ function from(pos, used) {
101
+ for (var i = 0; i < labels.length; i++) {
102
+ var label = labels[i];
103
+ if (!label || used.indexOf(label) !== -1 || value.indexOf(label, pos) !== pos) continue;
104
+ var end = pos + label.length;
105
+ if (end === value.length) return used.concat([label]);
106
+ if (value.indexOf(MULTI_SELECT_SEPARATOR, end) !== end) continue;
107
+ var rest = from(end + MULTI_SELECT_SEPARATOR.length, used.concat([label]));
108
+ if (rest) return rest;
109
+ }
110
+ return null;
111
+ }
112
+ return value ? from(0, []) : null;
113
+ }
114
+
115
+ // What a recorded answer shows on a card: the choices it selects and the
116
+ // "Other" text it carries (one or the other).
117
+ function readAnswer(question, value) {
118
+ var labels = choiceLabels(question);
119
+ if (Array.isArray(value)) {
120
+ return { labels: labels.filter(function (label) { return value.indexOf(label) !== -1; }), other: "" };
121
+ }
122
+ var text = value == null ? "" : String(value);
123
+ if (labels.indexOf(text) !== -1) return { labels: [text], other: "" };
124
+ var joined = question && question.multiSelect ? splitJoinedLabels(text, labels) : null;
125
+ return joined ? { labels: labels.filter(function (label) { return joined.indexOf(label) !== -1; }), other: "" } : { labels: [], other: text };
126
+ }
127
+
128
+ export {
129
+ MULTI_SELECT_SEPARATOR, MAX_ANSWER_CHARS, choiceLabels, normalizeAnswer, normalizeAnswers,
130
+ answerFromCard, answerText, toolAnswers, readAnswer,
131
+ };
@@ -0,0 +1,213 @@
1
+ // AskUserQuestion card: one or more multiple-choice questions with an
2
+ // "Other" field, answered with Submit or skipped. The prompt's id is the
3
+ // tool-use id. The shell (prompts.js, prompt-card.js) owns its state; this
4
+ // module draws the questions and, once answered, the answer summary.
5
+ //
6
+ // What an answer is - chosen labels or "Other" text, how a multi-select
7
+ // answer is listed, how options sharing a label count as one choice, how a
8
+ // recorded answer reads back - is the shared codec's (./ask-user-codec.js,
9
+ // which the server holds answers to). A multi-select answer is sent and
10
+ // recorded as a list, so a replayed card shows exactly what was chosen; the
11
+ // server joins it for the CLI.
12
+
13
+ import { MAX_ANSWER_CHARS, answerFromCard, answerText, readAnswer } from './ask-user-codec.js';
14
+
15
+ function isLocked(container) {
16
+ return container.classList.contains("resolved") || container.classList.contains("sending");
17
+ }
18
+
19
+ function showAnswerSummary(container, questions, answers) {
20
+ if (!answers || Object.keys(answers).length === 0) return;
21
+ if (container.querySelector(".ask-user-answer-summary")) return;
22
+ var summary = document.createElement("div");
23
+ summary.className = "ask-user-answer-summary";
24
+ Object.keys(answers).forEach(function (key) {
25
+ var qi = parseInt(key, 10);
26
+ var row = document.createElement("div");
27
+ row.className = "ask-user-answer-row";
28
+ var labelEl = document.createElement("span");
29
+ labelEl.className = "ask-user-answer-label";
30
+ labelEl.textContent = (questions[qi] && questions[qi].question) ? questions[qi].question : "Answer";
31
+ var valueEl = document.createElement("span");
32
+ valueEl.className = "ask-user-answer-value";
33
+ valueEl.textContent = answerText(answers[key]);
34
+ row.appendChild(labelEl);
35
+ row.appendChild(valueEl);
36
+ summary.appendChild(row);
37
+ });
38
+ container.appendChild(summary);
39
+ }
40
+
41
+ // Every option whose label is chosen shows as selected, and no other: the
42
+ // live card and a replayed one draw selection the same way.
43
+ function showChosen(qEl, labels) {
44
+ var options = qEl.querySelectorAll(".ask-user-option");
45
+ for (var i = 0; i < options.length; i++) {
46
+ var labelEl = options[i].querySelector(".option-label");
47
+ if (labelEl && labels.indexOf(labelEl.textContent) !== -1) options[i].classList.add("selected");
48
+ else options[i].classList.remove("selected");
49
+ }
50
+ }
51
+
52
+ // Reflect recorded answers in the options, so a replayed card reads the
53
+ // same as the one the operator answered: exactly the chosen options are
54
+ // selected, and only an "Other" answer fills the text field.
55
+ function markSelections(container, questions, answers) {
56
+ var questionEls = container.querySelectorAll(".ask-user-question");
57
+ Object.keys(answers).forEach(function (key) {
58
+ var qIdx = parseInt(key, 10);
59
+ var qEl = questionEls[qIdx];
60
+ if (!qEl) return;
61
+ var read = readAnswer(questions[qIdx], answers[key]);
62
+ showChosen(qEl, read.labels);
63
+ var otherInput = qEl.querySelector(".ask-user-other input");
64
+ if (otherInput) otherInput.value = read.other;
65
+ });
66
+ }
67
+
68
+ function drawQuestion(container, q, qIdx, state, submit) {
69
+ var qDiv = document.createElement("div");
70
+ qDiv.className = "ask-user-question";
71
+ if (q.header) {
72
+ var qHeader = document.createElement("div");
73
+ qHeader.className = "ask-user-question-header";
74
+ qHeader.textContent = q.header;
75
+ qDiv.appendChild(qHeader);
76
+ }
77
+ var qText = document.createElement("div");
78
+ qText.className = "ask-user-question-text";
79
+ qText.textContent = q.question || "";
80
+ qDiv.appendChild(qText);
81
+
82
+ var optionsDiv = document.createElement("div");
83
+ optionsDiv.className = "ask-user-options";
84
+ var isMulti = q.multiSelect || false;
85
+ state.chosen[qIdx] = [];
86
+ state.other[qIdx] = "";
87
+
88
+ (q.options || []).forEach(function (opt) {
89
+ var btn = document.createElement("button");
90
+ btn.className = "ask-user-option";
91
+ var labelEl = document.createElement("div");
92
+ labelEl.className = "option-label";
93
+ labelEl.textContent = opt.label;
94
+ btn.appendChild(labelEl);
95
+ if (opt.description) {
96
+ var descEl = document.createElement("div");
97
+ descEl.className = "option-desc";
98
+ descEl.textContent = opt.description;
99
+ btn.appendChild(descEl);
100
+ }
101
+ if (opt.markdown) {
102
+ var pre = document.createElement("pre");
103
+ pre.className = "option-markdown";
104
+ pre.textContent = opt.markdown;
105
+ btn.appendChild(pre);
106
+ }
107
+ btn.addEventListener("click", function () {
108
+ if (isLocked(container)) return;
109
+ // Choosing an option replaces any "Other" answer.
110
+ var other = qDiv.querySelector(".ask-user-other input");
111
+ if (other) other.value = "";
112
+ state.other[qIdx] = "";
113
+ var chosen = state.chosen[qIdx];
114
+ if (!isMulti) state.chosen[qIdx] = [opt.label];
115
+ else if (chosen.indexOf(opt.label) !== -1) state.chosen[qIdx] = chosen.filter(function (l) { return l !== opt.label; });
116
+ else state.chosen[qIdx] = chosen.concat([opt.label]);
117
+ showChosen(qDiv, state.chosen[qIdx]);
118
+ });
119
+ optionsDiv.appendChild(btn);
120
+ });
121
+ qDiv.appendChild(optionsDiv);
122
+
123
+ var otherDiv = document.createElement("div");
124
+ otherDiv.className = "ask-user-other";
125
+ var otherInput = document.createElement("input");
126
+ otherInput.type = "text";
127
+ otherInput.placeholder = "Other...";
128
+ otherInput.maxLength = MAX_ANSWER_CHARS;
129
+ otherInput.addEventListener("input", function () {
130
+ if (isLocked(container)) return;
131
+ // An emptied field is no answer: Submit must not send what was typed
132
+ // before it was cleared.
133
+ state.other[qIdx] = otherInput.value;
134
+ if (!otherInput.value.trim()) return;
135
+ state.chosen[qIdx] = [];
136
+ showChosen(qDiv, []);
137
+ });
138
+ otherInput.addEventListener("keydown", function (e) {
139
+ if (e.key === "Enter" && !e.shiftKey) {
140
+ e.preventDefault();
141
+ submit();
142
+ }
143
+ });
144
+ otherDiv.appendChild(otherInput);
145
+ qDiv.appendChild(otherDiv);
146
+ container.appendChild(qDiv);
147
+ }
148
+
149
+ export default {
150
+ kind: "ask_user",
151
+
152
+ draw: function (fields, requestId, host) {
153
+ var questions = (fields.input && fields.input.questions) || [];
154
+ var container = document.createElement("div");
155
+ container.className = "ask-user-container";
156
+ container.dataset.requestId = requestId;
157
+ container.dataset.toolId = requestId;
158
+
159
+ // Per question index: the chosen labels and the "Other" text.
160
+ var state = { chosen: {}, other: {} };
161
+ function submit() {
162
+ if (isLocked(container)) return;
163
+ var result = {};
164
+ questions.forEach(function (q, qIdx) {
165
+ var answer = answerFromCard(q, state.chosen[qIdx] || [], state.other[qIdx]);
166
+ if (answer !== null) result[qIdx] = answer;
167
+ });
168
+ if (Object.keys(result).length === 0) return;
169
+ host.respond(container, { answers: result });
170
+ }
171
+ questions.forEach(function (q, qIdx) { drawQuestion(container, q, qIdx, state, submit); });
172
+
173
+ var submitBtn = document.createElement("button");
174
+ submitBtn.className = "ask-user-submit";
175
+ submitBtn.textContent = "Submit";
176
+ submitBtn.addEventListener("click", submit);
177
+ container.appendChild(submitBtn);
178
+
179
+ var skipBtn = document.createElement("button");
180
+ skipBtn.className = "ask-user-skip";
181
+ skipBtn.textContent = "Skip";
182
+ skipBtn.addEventListener("click", function () {
183
+ if (!isLocked(container)) host.respond(container, { decision: "skip" });
184
+ });
185
+ container.appendChild(skipBtn);
186
+
187
+ host.setMainInputDisabled(true);
188
+ return container;
189
+ },
190
+
191
+ outcome: function (state) {
192
+ if (state.skipped) return { text: "Skipped", tone: "denied" };
193
+ return { text: "Answered", tone: "allowed" };
194
+ },
195
+
196
+ // An answered card shows the answers; any other ending shows its label.
197
+ settle: function (container, state, fields, outcome, host) {
198
+ container.classList.add("answered");
199
+ host.setMainInputDisabled(false);
200
+ var questions = (fields.input && fields.input.questions) || [];
201
+ if (state.state === "resolved" && state.answers && Object.keys(state.answers).length) {
202
+ showAnswerSummary(container, questions, state.answers);
203
+ markSelections(container, questions, state.answers);
204
+ return;
205
+ }
206
+ if (!container.querySelector(".ask-user-status")) {
207
+ var status = document.createElement("div");
208
+ status.className = "ask-user-status permission-decision-label";
209
+ status.textContent = outcome.text;
210
+ container.appendChild(status);
211
+ }
212
+ },
213
+ };