@clagentic/console 1.12.0-beta.2 → 1.12.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.
- package/LICENSES/README.md +5 -0
- package/bin/cli.js +4 -2
- package/lib/agent-roster-ws.js +84 -0
- package/lib/agent-roster.js +312 -0
- package/lib/auth-required-notify.js +42 -0
- package/lib/build-sha.json +2 -2
- package/lib/build-update-notify.js +45 -0
- package/lib/build-update-offer.js +3 -4
- package/lib/build-update-standing.js +34 -14
- package/lib/cli/session-subcommands.js +26 -8
- package/lib/client-address.js +44 -0
- package/lib/daemon.js +25 -10
- package/lib/idle-compaction-setting.js +203 -0
- package/lib/mcp-bridge-credential.js +41 -0
- package/lib/notification-prefs.js +16 -22
- package/lib/notify/core.js +53 -18
- package/lib/notify/payload.js +2 -3
- package/lib/notify/type-registry.js +17 -0
- package/lib/notify/types.js +38 -1
- package/lib/outbound-access.js +12 -1
- package/lib/outbound-entry.js +8 -1
- package/lib/project-connection.js +9 -8
- package/lib/project-filesystem.js +23 -0
- package/lib/project-loop.js +16 -19
- package/lib/project-notifications.js +68 -7
- package/lib/project-sessions.js +9 -0
- package/lib/project-user-mention.js +22 -34
- package/lib/project-user-message.js +208 -4
- package/lib/project-user-shell.js +298 -0
- package/lib/project.js +26 -4
- package/lib/prompt-answer.js +145 -0
- package/lib/prompt-expiry.js +50 -0
- package/lib/prompt-registry.js +24 -1
- package/lib/prompt-source.js +16 -0
- package/lib/public/app.js +31 -2
- package/lib/public/css/agent-messages.css +96 -0
- package/lib/public/css/agent-roster.css +93 -0
- package/lib/public/css/diagnostics.css +1 -1
- package/lib/public/css/input.css +20 -141
- package/lib/public/css/login-panel.css +73 -0
- package/lib/public/css/menus.css +2 -2
- package/lib/public/css/sidebar.css +11 -0
- package/lib/public/css/team-panel.css +1 -6
- package/lib/public/css/team-view.css +101 -0
- package/lib/public/css/title-bar.css +10 -0
- package/lib/public/css/user-shell.css +144 -0
- package/lib/public/index.html +41 -7
- package/lib/public/modules/agent-avatar-picker.js +162 -0
- package/lib/public/modules/agent-avatar.js +153 -0
- package/lib/public/modules/agent-messages.js +285 -0
- package/lib/public/modules/agent-roster.js +157 -0
- package/lib/public/modules/app-connection.js +12 -0
- package/lib/public/modules/app-messages.js +55 -16
- package/lib/public/modules/app-notifications.js +59 -29
- package/lib/public/modules/app-panels.js +26 -3
- package/lib/public/modules/app-rate-limit.js +8 -165
- package/lib/public/modules/app-rendering.js +22 -7
- package/lib/public/modules/collapsible-text.js +33 -18
- package/lib/public/modules/compaction-state.js +34 -0
- package/lib/public/modules/custom-icons.js +23 -5
- package/lib/public/modules/diagnostics.js +52 -55
- package/lib/public/modules/injected-identity.js +24 -21
- package/lib/public/modules/input.js +176 -15
- package/lib/public/modules/login-panel.js +136 -0
- package/lib/public/modules/login-parser.js +73 -0
- package/lib/public/modules/login-tab.js +66 -0
- package/lib/public/modules/notifications.js +9 -1
- package/lib/public/modules/notify-client.js +114 -21
- package/lib/public/modules/pending-term-request.js +30 -0
- package/lib/public/modules/project-icon.js +22 -2
- package/lib/public/modules/queued-button.js +11 -0
- package/lib/public/modules/scheduled-input-actions.js +12 -4
- package/lib/public/modules/scheduled-outbox.js +230 -0
- package/lib/public/modules/send-now.js +200 -0
- package/lib/public/modules/server-settings.js +47 -1
- package/lib/public/modules/shell-card.js +76 -0
- package/lib/public/modules/shell-line.js +49 -0
- package/lib/public/modules/shell-queue.js +253 -0
- package/lib/public/modules/shell-settings-controls.js +76 -0
- package/lib/public/modules/sidebar-mobile.js +3 -3
- package/lib/public/modules/sidebar-sessions.js +8 -0
- package/lib/public/modules/sidebar-users.js +5 -7
- package/lib/public/modules/team-panel.js +30 -88
- package/lib/public/modules/team-view.js +211 -0
- package/lib/public/modules/terminal-links.js +18 -0
- package/lib/public/modules/terminal.js +65 -4
- package/lib/public/modules/tools.js +19 -0
- package/lib/public/modules/user-shell.js +824 -0
- package/lib/public/notify-sw-logic.js +35 -83
- package/lib/public/notify-target.js +70 -10
- package/lib/public/style.css +5 -0
- package/lib/public/sw.js +7 -3
- package/lib/queued-chat.js +392 -0
- package/lib/scheduled-input/ws.js +67 -9
- package/lib/sdk-bridge.js +122 -43
- package/lib/sdk-message-processor.js +46 -39
- package/lib/server-auth.js +6 -4
- package/lib/server-dm.js +11 -20
- package/lib/server-settings.js +39 -1
- package/lib/server.js +83 -19
- package/lib/session-directory.js +33 -8
- package/lib/session-end.js +97 -0
- package/lib/session-input-ipc.js +26 -0
- package/lib/session-input.js +176 -9
- package/lib/session-resume.js +1 -0
- package/lib/session-settings-file.js +53 -0
- package/lib/sessions.js +49 -5
- package/lib/team-traffic-ws.js +68 -0
- package/lib/team-traffic.js +318 -0
- package/lib/usage-store.js +7 -0
- package/lib/user-shell-settings.js +105 -0
- package/lib/ws-schema.js +27 -4
- package/lib/yoke/adapters/claude-capabilities.js +2 -0
- package/lib/yoke/adapters/claude-vendor.js +6 -0
- package/lib/yoke/adapters/claude-worker.js +39 -11
- package/lib/yoke/adapters/claude.js +218 -6
- package/lib/yoke/adapters/codex-capabilities.js +6 -1
- package/lib/yoke/adapters/codex-user-shell.js +265 -0
- package/lib/yoke/adapters/codex-vendor.js +7 -0
- package/lib/yoke/adapters/codex.js +186 -52
- package/lib/yoke/codex-app-server.js +8 -2
- package/lib/yoke/interface.js +54 -1
- package/lib/yoke/mcp-bridge-server.js +12 -8
- package/lib/yoke/registry.js +32 -0
- package/lib/yoke/user-shell.js +224 -0
- package/package.json +1 -1
- package/lib/public/modules/build-notice-gate.js +0 -73
|
@@ -0,0 +1,824 @@
|
|
|
1
|
+
// user-shell.js - the composer's ! command line on the page: the request a ! line
|
|
2
|
+
// becomes, the card that shows a command and its outcome, the chat queue that
|
|
3
|
+
// holds what is typed (chat and ! lines) while a turn or a command runs, and the
|
|
4
|
+
// settings the server sends.
|
|
5
|
+
// The server decides everything that matters (the setting, OS-user isolation,
|
|
6
|
+
// plan mode, the permission gate); this module sends the line and draws what
|
|
7
|
+
// comes back. Handlers are registered by app-messages.js.
|
|
8
|
+
|
|
9
|
+
import { store } from './store.js';
|
|
10
|
+
import { addToMessages, addSystemMessage, scrollToBottom } from './app-rendering.js';
|
|
11
|
+
import { setSendBtnMode } from './app-favicon.js';
|
|
12
|
+
import { buildCard, fillCard, showRunning } from './shell-card.js';
|
|
13
|
+
import { createChatQueue } from './shell-queue.js';
|
|
14
|
+
import { currentSendNowState, currentQueuedControls } from './send-now.js';
|
|
15
|
+
import { renderUserShellSettings } from './shell-settings-controls.js';
|
|
16
|
+
import { queuedButton } from './queued-button.js';
|
|
17
|
+
|
|
18
|
+
var STORAGE_KEY = "clagentic-console-shell-queue";
|
|
19
|
+
var IMAGE_NOTE = "[an image attachment of this message was not restored; attach it again]";
|
|
20
|
+
|
|
21
|
+
// requestId -> { scope, command, card }: commands sent from this page that have
|
|
22
|
+
// not finished. A request is entered only after its frame was sent.
|
|
23
|
+
var pending = new Map();
|
|
24
|
+
// What input.js supplies: the composer and the way a queued chat message is sent.
|
|
25
|
+
var composer = null;
|
|
26
|
+
var chatSender = null;
|
|
27
|
+
// Sends one frame; true when the socket took it.
|
|
28
|
+
var frameSender = null;
|
|
29
|
+
// Draws a delivered chat message as the normal user message.
|
|
30
|
+
var deliveredRenderer = null;
|
|
31
|
+
// messageId -> the chat messages sent into a running turn that the agent has
|
|
32
|
+
// not yet taken in, drawn as queued (oldest first). Unlike the entries of the
|
|
33
|
+
// queue below, these have already been sent: nothing here is ever sent again.
|
|
34
|
+
var inflight = new Map();
|
|
35
|
+
// Which socket sent each chat message still waiting, with its images. The server
|
|
36
|
+
// echoes a delivered message to every socket but the sender's, so a page whose
|
|
37
|
+
// item was redrawn after a session switch on that same socket gets no echo and
|
|
38
|
+
// has to draw the message itself.
|
|
39
|
+
var sentOn = new Map();
|
|
40
|
+
// Returns the page's current socket; compared by identity only.
|
|
41
|
+
var socketProbe = null;
|
|
42
|
+
// A draft set aside while a refused command is edited in the composer.
|
|
43
|
+
var setAside = null;
|
|
44
|
+
var lastScope = null;
|
|
45
|
+
// A busy refusal of a flushed command arrives after the page already reads idle, so
|
|
46
|
+
// no processing change follows to flush again. This timer retries instead, with a
|
|
47
|
+
// doubling delay up to a cap; it never runs as a tight loop.
|
|
48
|
+
var RETRY_BASE_MS = 500;
|
|
49
|
+
var RETRY_CAP_MS = 8000;
|
|
50
|
+
var retryTimer = null;
|
|
51
|
+
var retryCount = 0;
|
|
52
|
+
|
|
53
|
+
function cancelRetry() {
|
|
54
|
+
if (retryTimer !== null) { clearTimeout(retryTimer); retryTimer = null; }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function armRetry() {
|
|
58
|
+
cancelRetry();
|
|
59
|
+
var delay = Math.min(RETRY_BASE_MS * Math.pow(2, retryCount), RETRY_CAP_MS);
|
|
60
|
+
retryCount++;
|
|
61
|
+
retryTimer = setTimeout(function () {
|
|
62
|
+
retryTimer = null;
|
|
63
|
+
flushQueuedChat();
|
|
64
|
+
}, delay);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function currentScope() {
|
|
68
|
+
return (store.get("currentSlug") || "") + ":" + String(store.get("activeSessionId"));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var sessionStore = {
|
|
72
|
+
load: function () {
|
|
73
|
+
try {
|
|
74
|
+
var raw = window.sessionStorage.getItem(STORAGE_KEY);
|
|
75
|
+
return raw ? JSON.parse(raw) : null;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.warn("[user-shell] could not read the saved queue:", (err && err.message) || err);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
save: function (pairs) {
|
|
82
|
+
try {
|
|
83
|
+
if (pairs.length === 0) window.sessionStorage.removeItem(STORAGE_KEY);
|
|
84
|
+
else window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(pairs));
|
|
85
|
+
} catch (err) {
|
|
86
|
+
addSystemMessage("Queued messages could not be saved for a reload: " + ((err && err.message) || err), true);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Send now on a queued ! entry follows the capability table: unavailable while a
|
|
92
|
+
// turn runs (aria-disabled, so a tap can still say why), usable when idle.
|
|
93
|
+
function paintSendNow(btn, kind) {
|
|
94
|
+
var st = currentSendNowState(kind);
|
|
95
|
+
var off = st.mode === "disabled";
|
|
96
|
+
btn.classList.toggle("unavailable", off);
|
|
97
|
+
btn.setAttribute("aria-disabled", off ? "true" : "false");
|
|
98
|
+
btn.title = off ? "Send now (unavailable): " + st.reason : "Send now: run this command next";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function sendQueuedItemNow(entry) {
|
|
102
|
+
var st = currentSendNowState(entry.kind);
|
|
103
|
+
if (st.mode === "disabled") {
|
|
104
|
+
addSystemMessage("Send now is unavailable: " + st.reason + ".", true);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
chatQueue.promote(currentScope(), function (e) { return e.id === entry.id; });
|
|
108
|
+
flushQueuedChat();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
var chatQueue = createChatQueue({
|
|
112
|
+
storage: sessionStore,
|
|
113
|
+
show: function (entry) {
|
|
114
|
+
var el = document.createElement("div");
|
|
115
|
+
el.className = "shell-queued";
|
|
116
|
+
var tag = document.createElement("span");
|
|
117
|
+
tag.className = "shell-queued-tag";
|
|
118
|
+
tag.textContent = "Queued";
|
|
119
|
+
var body = document.createElement("span");
|
|
120
|
+
body.className = "shell-queued-text";
|
|
121
|
+
body.textContent = entry.kind === "shell" ? "!" + entry.command : entry.text || (entry.imageCount > 0 ? "[image]" : "[attachment]");
|
|
122
|
+
el.appendChild(tag);
|
|
123
|
+
el.appendChild(body);
|
|
124
|
+
var handle = { el: el, tag: tag, refresh: null };
|
|
125
|
+
if (entry.kind === "shell") {
|
|
126
|
+
var actions = document.createElement("span");
|
|
127
|
+
actions.className = "shell-queued-actions";
|
|
128
|
+
var sendNowBtn = queuedButton("Send now", "shell-queued-send-now");
|
|
129
|
+
sendNowBtn.addEventListener("click", function () { sendQueuedItemNow(entry); });
|
|
130
|
+
var cancelBtn = queuedButton("Cancel", "shell-queued-cancel");
|
|
131
|
+
cancelBtn.addEventListener("click", function () { chatQueue.remove(entry.id); });
|
|
132
|
+
var editBtn = queuedButton("Edit", "shell-queued-edit");
|
|
133
|
+
editBtn.addEventListener("click", function () {
|
|
134
|
+
chatQueue.remove(entry.id);
|
|
135
|
+
editRefusedCommand(entry.command);
|
|
136
|
+
});
|
|
137
|
+
actions.appendChild(sendNowBtn);
|
|
138
|
+
actions.appendChild(editBtn);
|
|
139
|
+
actions.appendChild(cancelBtn);
|
|
140
|
+
el.appendChild(actions);
|
|
141
|
+
handle.refresh = function () { paintSendNow(sendNowBtn, entry.kind); };
|
|
142
|
+
handle.refresh();
|
|
143
|
+
}
|
|
144
|
+
addToMessages(el);
|
|
145
|
+
scrollToBottom();
|
|
146
|
+
return handle;
|
|
147
|
+
},
|
|
148
|
+
hide: function (handle) { handle.el.remove(); },
|
|
149
|
+
markUnsent: function (handle, why) {
|
|
150
|
+
handle.el.className = "shell-queued unsent";
|
|
151
|
+
handle.tag.textContent = "Not sent";
|
|
152
|
+
handle.el.setAttribute("title", "Not sent: " + why + ". It is sent when the connection is back.");
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
/** Hands over the composer: read() its text, write(text) to replace it. */
|
|
157
|
+
export function registerComposer(c) { composer = c; }
|
|
158
|
+
|
|
159
|
+
/** Hands over how one queued chat entry is sent: send(entry) returns true when it was sent. */
|
|
160
|
+
export function registerChatSender(fn) { chatSender = fn; }
|
|
161
|
+
|
|
162
|
+
/** Hands over how a frame is sent: send(frame) returns true when the socket took it. */
|
|
163
|
+
export function registerFrameSender(fn) { frameSender = fn; }
|
|
164
|
+
|
|
165
|
+
/** Hands over how the page's current socket is read: probe() returns it. */
|
|
166
|
+
export function registerSocketProbe(fn) { socketProbe = fn; }
|
|
167
|
+
|
|
168
|
+
/** Hands over how a delivered chat message is shown: render({text, images, pastes}). */
|
|
169
|
+
export function registerDeliveredRenderer(fn) { deliveredRenderer = fn; }
|
|
170
|
+
|
|
171
|
+
/** Whether a ! line is sent as a command: the server's setting is on (the default). */
|
|
172
|
+
export function isShellModeOn() {
|
|
173
|
+
var s = store.get("userShellSettings");
|
|
174
|
+
return !s || s.enabled !== false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Whether a command sent from this page, in the session on screen, has not finished. */
|
|
178
|
+
export function isShellPending() {
|
|
179
|
+
var scope = currentScope();
|
|
180
|
+
var found = false;
|
|
181
|
+
pending.forEach(function (p) { if (p.scope === scope) found = true; });
|
|
182
|
+
return found;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Whether anything (chat or a ! line) of the session on screen is waiting to be sent. */
|
|
186
|
+
export function hasQueuedChat() {
|
|
187
|
+
return chatQueue.sizeFor(currentScope()) > 0;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Ctrl/Cmd+Enter: the session's queued ! entries are set to run next, ahead of any
|
|
192
|
+
* chat behind them, and the queue is flushed (they still wait for a running turn).
|
|
193
|
+
*/
|
|
194
|
+
export function runQueuedShellNext() {
|
|
195
|
+
chatQueue.promote(currentScope(), function (e) { return e.kind === "shell"; });
|
|
196
|
+
flushQueuedChat();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Up from an empty composer: when everything queued for the session is a ! entry,
|
|
201
|
+
* takes the LAST one out of the queue and returns it as a single "!" line for the
|
|
202
|
+
* composer (the rest stay queued; Up again takes the next). One draft is one command,
|
|
203
|
+
* so no path writes several queued entries into the composer. Returns null, and
|
|
204
|
+
* changes nothing, otherwise.
|
|
205
|
+
* @returns {?string}
|
|
206
|
+
*/
|
|
207
|
+
export function takeBackQueuedShell() {
|
|
208
|
+
var mine = chatQueue.entriesFor(currentScope());
|
|
209
|
+
if (mine.length === 0 || mine.some(function (e) { return e.kind !== "shell"; })) return null;
|
|
210
|
+
var last = mine[mine.length - 1];
|
|
211
|
+
chatQueue.remove(last.id);
|
|
212
|
+
return "!" + last.command;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ---------------------------------------------------------------- queued chat
|
|
216
|
+
|
|
217
|
+
// A random version-4 uuid. crypto.randomUUID exists only in a secure context and
|
|
218
|
+
// this page can be served over plain http, so getRandomValues (available in
|
|
219
|
+
// both) is used. Returns null when the page has no source of randomness, in
|
|
220
|
+
// which case the message is sent as it always was, not drawn as queued.
|
|
221
|
+
export function newMessageId() {
|
|
222
|
+
var c = typeof window !== "undefined" ? window.crypto : null;
|
|
223
|
+
if (!c || typeof c.getRandomValues !== "function") return null;
|
|
224
|
+
var b = new Uint8Array(16);
|
|
225
|
+
c.getRandomValues(b);
|
|
226
|
+
b[6] = (b[6] & 0x0f) | 0x40;
|
|
227
|
+
b[8] = (b[8] & 0x3f) | 0x80;
|
|
228
|
+
var hex = Array.from(b, function (x) { return (x + 256).toString(16).slice(1); }).join("");
|
|
229
|
+
return hex.slice(0, 8) + "-" + hex.slice(8, 12) + "-" + hex.slice(12, 16) + "-" + hex.slice(16, 20) + "-" + hex.slice(20);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Whether a chat message typed now is sent into the running turn and drawn as
|
|
234
|
+
* queued until the agent takes it: a turn runs and the agent can withdraw a
|
|
235
|
+
* message that is waiting. Otherwise it is shown as sent, as ever.
|
|
236
|
+
*/
|
|
237
|
+
export function chatQueuesWithAgent() {
|
|
238
|
+
return !!store.get("processing") && currentQueuedControls("chat").cancel.enabled;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Cancel, Edit and Send now of one chat item, painted from what the item and the
|
|
242
|
+
// agent allow now: delivered or mid-request items cannot be withdrawn, and the
|
|
243
|
+
// reason is in the title and stated on a tap (aria-disabled keeps the tap).
|
|
244
|
+
function paintInflight(item) {
|
|
245
|
+
// After Send now the item is not queued any more: no label, no controls, until
|
|
246
|
+
// the agent takes the message (message_delivered) or hands it back.
|
|
247
|
+
paintSendNowState(item);
|
|
248
|
+
if (item.sendNow) return;
|
|
249
|
+
var controls = currentQueuedControls("chat");
|
|
250
|
+
var why = "";
|
|
251
|
+
if (item.delivered) why = "Already delivered to the agent.";
|
|
252
|
+
else if (item.unconfirmed) why = "The agent may already be running it.";
|
|
253
|
+
else if (item.action) why = "Working on it.";
|
|
254
|
+
else if (!controls.cancel.enabled) why = controls.cancel.reason;
|
|
255
|
+
item.withdrawBlocked = why;
|
|
256
|
+
[["cancel", "Cancel", "Cancel: withdraw this message"], ["edit", "Edit", "Edit: withdraw it and edit it in the composer"]].forEach(function (b) {
|
|
257
|
+
var btn = item.buttons[b[0]];
|
|
258
|
+
btn.classList.toggle("unavailable", !!why);
|
|
259
|
+
btn.setAttribute("aria-disabled", why ? "true" : "false");
|
|
260
|
+
btn.title = why ? b[1] + " (unavailable): " + why : b[2];
|
|
261
|
+
});
|
|
262
|
+
var st = currentSendNowState("chat");
|
|
263
|
+
var off = item.delivered || st.mode === "disabled";
|
|
264
|
+
item.buttons.sendNow.classList.toggle("unavailable", off);
|
|
265
|
+
item.buttons.sendNow.setAttribute("aria-disabled", off ? "true" : "false");
|
|
266
|
+
item.buttons.sendNow.title = item.delivered
|
|
267
|
+
? "Send now (unavailable): Already delivered to the agent."
|
|
268
|
+
: off ? "Send now (unavailable): " + st.reason
|
|
269
|
+
: "Send now: this message is already with the agent and joins the turn at its next step";
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function paintSendNowState(item) {
|
|
273
|
+
if (!item.tagEl || !item.actionsEl) return;
|
|
274
|
+
item.tagEl.textContent = item.sendNow ? "Sending" : "Queued";
|
|
275
|
+
item.actionsEl.style.display = item.sendNow ? "none" : "";
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function drawInflight(item) {
|
|
279
|
+
var el = document.createElement("div");
|
|
280
|
+
el.className = "shell-queued";
|
|
281
|
+
var tag = document.createElement("span");
|
|
282
|
+
tag.className = "shell-queued-tag";
|
|
283
|
+
tag.textContent = "Queued";
|
|
284
|
+
var body = document.createElement("span");
|
|
285
|
+
body.className = "shell-queued-text";
|
|
286
|
+
body.textContent = item.text || (item.imageCount > 0 ? "[image]" : "[attachment]");
|
|
287
|
+
var actions = document.createElement("span");
|
|
288
|
+
actions.className = "shell-queued-actions";
|
|
289
|
+
var sendNowBtn = queuedButton("Send now", "shell-queued-send-now");
|
|
290
|
+
var editBtn = queuedButton("Edit", "shell-queued-edit");
|
|
291
|
+
var cancelBtn = queuedButton("Cancel", "shell-queued-cancel");
|
|
292
|
+
sendNowBtn.addEventListener("click", function () { sendInflightNow(item); });
|
|
293
|
+
editBtn.addEventListener("click", function () { withdrawInflight(item, "edit"); });
|
|
294
|
+
cancelBtn.addEventListener("click", function () { withdrawInflight(item, "cancel"); });
|
|
295
|
+
actions.appendChild(sendNowBtn);
|
|
296
|
+
actions.appendChild(editBtn);
|
|
297
|
+
actions.appendChild(cancelBtn);
|
|
298
|
+
el.appendChild(tag);
|
|
299
|
+
el.appendChild(body);
|
|
300
|
+
el.appendChild(actions);
|
|
301
|
+
item.el = el;
|
|
302
|
+
item.tagEl = tag;
|
|
303
|
+
item.actionsEl = actions;
|
|
304
|
+
item.buttons = { sendNow: sendNowBtn, edit: editBtn, cancel: cancelBtn };
|
|
305
|
+
paintInflight(item);
|
|
306
|
+
addToMessages(el);
|
|
307
|
+
scrollToBottom();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function removeInflight(item) {
|
|
311
|
+
inflight.delete(item.messageId);
|
|
312
|
+
sentOn.delete(item.messageId);
|
|
313
|
+
if (item.el && item.el.remove) item.el.remove();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// The composer text for a queued chat handed back: its text and pastes as one
|
|
317
|
+
// entry, with the note that attached images are not restored.
|
|
318
|
+
function queuedRestoreText(text, pastes, imageCount) {
|
|
319
|
+
var restoreText = [text].concat(pastes || []).filter(Boolean).join("\n\n");
|
|
320
|
+
if (imageCount > 0) restoreText = restoreText ? restoreText + "\n" + IMAGE_NOTE : IMAGE_NOTE;
|
|
321
|
+
return restoreText;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* A chat message was sent into the running turn: draws it as queued, with its
|
|
326
|
+
* controls, until the agent takes it in (message_delivered).
|
|
327
|
+
* @param {{messageId: string, text: string, images: Array, pastes: Array<string>}} message
|
|
328
|
+
*/
|
|
329
|
+
export function trackSentChat(message) {
|
|
330
|
+
sentOn.set(message.messageId, { socket: socketProbe ? socketProbe() : null, images: message.images });
|
|
331
|
+
addInflight(message.messageId, message.text, message.images, message.pastes, message.images.length, false, false);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function addInflight(messageId, text, images, pastes, imageCount, unconfirmed, sendNow) {
|
|
335
|
+
var item = {
|
|
336
|
+
messageId: messageId,
|
|
337
|
+
scope: currentScope(),
|
|
338
|
+
text: text,
|
|
339
|
+
images: images,
|
|
340
|
+
pastes: pastes,
|
|
341
|
+
imageCount: imageCount,
|
|
342
|
+
restoreText: queuedRestoreText(text, pastes, imageCount),
|
|
343
|
+
action: null,
|
|
344
|
+
delivered: false,
|
|
345
|
+
unconfirmed: unconfirmed,
|
|
346
|
+
// Send now was pressed (here, on another page, or before a reload): the item is
|
|
347
|
+
// drawn as sending and is never drawn queued again.
|
|
348
|
+
sendNow: !!sendNow,
|
|
349
|
+
// Set for an item drawn from the server's word rather than from a send on this
|
|
350
|
+
// page: the transcript gets the message itself when the agent takes it in.
|
|
351
|
+
redrawn: false,
|
|
352
|
+
el: null,
|
|
353
|
+
buttons: null,
|
|
354
|
+
};
|
|
355
|
+
inflight.set(item.messageId, item);
|
|
356
|
+
drawInflight(item);
|
|
357
|
+
return item;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* queued_message_tracked: the server says a chat message is still waiting for the
|
|
362
|
+
* agent, on a page that holds no queued item of its own (it was reloaded, or this
|
|
363
|
+
* is a second tab). Draws it as queued again with its controls. When the agent
|
|
364
|
+
* takes it in, every socket but the sender's gets the message itself, so delivery
|
|
365
|
+
* here only removes the item. The sender's own socket (a switch away and back)
|
|
366
|
+
* gets no echo: its item draws the message on delivery.
|
|
367
|
+
*/
|
|
368
|
+
export function handleQueuedMessageTracked(msg) {
|
|
369
|
+
if (inflight.has(msg.messageId) || typeof msg.text !== "string") return;
|
|
370
|
+
var sent = sentOn.get(msg.messageId);
|
|
371
|
+
var echoed = !(sent && socketProbe && sent.socket !== null && sent.socket === socketProbe());
|
|
372
|
+
var item = addInflight(msg.messageId, msg.text, echoed ? [] : sent.images, Array.isArray(msg.pastes) ? msg.pastes : [], msg.imageCount | 0, !!msg.unconfirmed, msg.sendNow === true);
|
|
373
|
+
item.redrawn = echoed;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Asks the server to withdraw a queued chat message. What follows (the text in
|
|
377
|
+
// the composer for Edit and take-back) happens only on its answer, so a message
|
|
378
|
+
// the agent already took is never both delivered and handed back.
|
|
379
|
+
function withdrawInflight(item, action) {
|
|
380
|
+
if (item.withdrawBlocked) {
|
|
381
|
+
addSystemMessage((action === "cancel" ? "Cancel" : action === "edit" ? "Edit" : "Take back") + " is unavailable: " + item.withdrawBlocked, true);
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
if (!frameSender || !frameSender({ type: "cancel_queued_message", messageId: item.messageId })) {
|
|
385
|
+
addSystemMessage("Not connected: the message could not be withdrawn.", true);
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
item.action = action;
|
|
389
|
+
paintInflight(item);
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Send now on a message that is already with the agent: it was pushed the moment
|
|
394
|
+
// it was sent, so nothing is sent again, never a second copy. The server
|
|
395
|
+
// interrupts the running turn with the agent's queue kept, so the message runs as
|
|
396
|
+
// the next input at once. The item is drawn as sending from the press on, which
|
|
397
|
+
// also makes a second press a no-op; the server's answer undoes that only when
|
|
398
|
+
// nothing was interrupted.
|
|
399
|
+
function sendInflightNow(item) {
|
|
400
|
+
var st = currentSendNowState("chat");
|
|
401
|
+
if (item.delivered) {
|
|
402
|
+
addSystemMessage("Send now is unavailable: Already delivered to the agent.", true);
|
|
403
|
+
} else if (st.mode === "disabled") {
|
|
404
|
+
addSystemMessage("Send now is unavailable: " + st.reason, true);
|
|
405
|
+
} else if (item.sendNow) {
|
|
406
|
+
return;
|
|
407
|
+
} else if (!frameSender || !frameSender({ type: "send_now_queued_message", messageId: item.messageId })) {
|
|
408
|
+
addSystemMessage("Not connected: Send now could not be sent.", true);
|
|
409
|
+
} else {
|
|
410
|
+
item.sendNow = true;
|
|
411
|
+
paintInflight(item);
|
|
412
|
+
addSystemMessage("Interrupting the running turn so this message runs now.", false);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* queued_message_send_now: the server's answer to Send now, or word from another
|
|
418
|
+
* page of the session that it was pressed. Only a request that interrupted nothing
|
|
419
|
+
* draws the item as queued again, with the reason; a miss arrives as a dropped reply.
|
|
420
|
+
*/
|
|
421
|
+
export function handleQueuedMessageSendNow(msg) {
|
|
422
|
+
var item = inflight.get(msg.messageId);
|
|
423
|
+
if (!item) return;
|
|
424
|
+
if (msg.result === "requested") {
|
|
425
|
+
item.sendNow = true;
|
|
426
|
+
paintInflight(item);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (msg.result === "already_delivered") {
|
|
430
|
+
// Delivery follows, or came first and the item is gone; nothing was interrupted.
|
|
431
|
+
item.delivered = true;
|
|
432
|
+
paintInflight(item);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (msg.result === "missed") return;
|
|
436
|
+
item.sendNow = false;
|
|
437
|
+
paintInflight(item);
|
|
438
|
+
addSystemMessage(msg.result === "no_turn"
|
|
439
|
+
? "No turn is running, so there is nothing to interrupt. The message is with the agent and starts when it can."
|
|
440
|
+
: "Send now did not take effect" + (msg.message ? ": " + msg.message : ".") + " The message is still queued.", true);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Up from an empty composer takes back ONE queued entry, never several joined:
|
|
445
|
+
* the last queued ! entry while any is waiting on this page (and nothing else
|
|
446
|
+
* is held here), otherwise the last chat message still waiting to be delivered.
|
|
447
|
+
* A chat message is withdrawn at the agent first; its text reaches the composer
|
|
448
|
+
* only if the agent confirms it dropped it. Returns false, and changes nothing,
|
|
449
|
+
* when there is nothing to take back.
|
|
450
|
+
* @param {function(string): void} write puts the entry's text in the composer
|
|
451
|
+
* @returns {boolean} whether a take-back was started
|
|
452
|
+
*/
|
|
453
|
+
export function takeBackLastQueued(write) {
|
|
454
|
+
if (chatQueue.sizeFor(currentScope()) > 0) {
|
|
455
|
+
var line = takeBackQueuedShell();
|
|
456
|
+
if (line === null) return false;
|
|
457
|
+
write(line);
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
var scope = currentScope();
|
|
461
|
+
var waiting = Array.from(inflight.values()).filter(function (it) { return it.scope === scope && !it.delivered && !it.unconfirmed && !it.action && !it.sendNow; });
|
|
462
|
+
if (waiting.length === 0) return false;
|
|
463
|
+
return withdrawInflight(waiting[waiting.length - 1], "takeback");
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** message_delivered: the agent took the message in. The queued item becomes the normal message, once. */
|
|
467
|
+
export function handleMessageDelivered(msg) {
|
|
468
|
+
var item = inflight.get(msg.messageId);
|
|
469
|
+
if (!item) {
|
|
470
|
+
// Delivered while this page showed another session: the message is over, so
|
|
471
|
+
// the sender record kept for a switch back has no more use.
|
|
472
|
+
sentOn.delete(msg.messageId);
|
|
473
|
+
settleMissedDelivery(msg.messageId, true);
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
removeInflight(item);
|
|
477
|
+
if (!item.redrawn && deliveredRenderer) deliveredRenderer({ text: item.text, images: item.images, pastes: item.pastes });
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Send now receipt misses: messageId -> {scope, restoreText, notice, message, draw,
|
|
481
|
+
// delivered}. The SDK may have taken the message into the interrupted turn before
|
|
482
|
+
// the receipt was read, so its delivery can still follow the miss. The record
|
|
483
|
+
// belongs to the session that sent the message and touches only that session's page.
|
|
484
|
+
var missedBySendNow = new Map();
|
|
485
|
+
|
|
486
|
+
// message: {text, images, pastes}; draw: whether this page draws the delivered
|
|
487
|
+
// message itself (no server echo reaches the sending socket).
|
|
488
|
+
function noteSendNowMiss(messageId, scope, restoreText, message, draw) {
|
|
489
|
+
var notice = addSystemMessage(droppedNotice("send_now_missed"), true);
|
|
490
|
+
missedBySendNow.set(messageId, { scope: scope, restoreText: restoreText, notice: notice, message: message, draw: draw, delivered: false });
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// The message ran after all: the restore notice is replaced, the message is drawn
|
|
494
|
+
// like a normal delivery, and the composer is cleared only while it still holds
|
|
495
|
+
// exactly the restored text. Never sends anything. A record of another session
|
|
496
|
+
// stays pending (marked delivered) until that session is on screen again, where
|
|
497
|
+
// the replayed history already holds the message (fromReplay: nothing to draw).
|
|
498
|
+
function settleMissedDelivery(messageId, live, fromReplay) {
|
|
499
|
+
var missed = missedBySendNow.get(messageId);
|
|
500
|
+
if (!missed) return;
|
|
501
|
+
if (missed.scope !== currentScope()) {
|
|
502
|
+
if (live) missed.delivered = true;
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
missedBySendNow.delete(messageId);
|
|
506
|
+
if (missed.notice && missed.notice.remove) missed.notice.remove();
|
|
507
|
+
if (composer && missed.restoreText && composerText() === missed.restoreText) composer.write("");
|
|
508
|
+
if (!fromReplay && missed.draw && deliveredRenderer) deliveredRenderer(missed.message);
|
|
509
|
+
addSystemMessage("This message was delivered after all, so it ran. It is no longer in your composer.", false);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// A session is on screen again with its history replayed: its missed records whose
|
|
513
|
+
// delivery arrived meanwhile are settled now.
|
|
514
|
+
function settleMissedOfShownSession() {
|
|
515
|
+
Array.from(missedBySendNow.keys()).forEach(function (id) {
|
|
516
|
+
if (missedBySendNow.get(id).delivered) settleMissedDelivery(id, false, true);
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// What a dropped message's notice says, by the cause the server names.
|
|
521
|
+
function droppedNotice(cause) {
|
|
522
|
+
if (cause === "send_now_missed") return "Send now interrupted the turn, but the agent no longer held this message, so it was not sent. It is back in your composer.";
|
|
523
|
+
if (cause === "not_taken") return "The agent did not run this message, so it was not sent. It is back in your composer.";
|
|
524
|
+
return "The session ended before this message was delivered, so it was not sent. It is back in your composer.";
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** The server's answer to a withdraw request, or word that the turn ended with the message untaken. */
|
|
528
|
+
export function handleQueuedMessageCancelled(msg) {
|
|
529
|
+
var item = inflight.get(msg.messageId);
|
|
530
|
+
if (!item) {
|
|
531
|
+
// Whether this socket sent the message: if not, the server's echo draws it.
|
|
532
|
+
var sentHere = sentOn.get(msg.messageId);
|
|
533
|
+
var ownSend = !!(sentHere && socketProbe && sentHere.socket !== null && sentHere.socket === socketProbe());
|
|
534
|
+
// Only a final answer ends the message: a pending or already_delivered one
|
|
535
|
+
// leaves it live, and the record is still needed if its item is drawn again.
|
|
536
|
+
if (msg.result === "cancelled" || msg.result === "dropped") sentOn.delete(msg.messageId);
|
|
537
|
+
// The page no longer holds the queued item (a session switch or a reload),
|
|
538
|
+
// but the server confirmed the message will not be delivered and sent its
|
|
539
|
+
// text: it goes back to the composer as one entry, never lost.
|
|
540
|
+
if ((msg.result === "cancelled" || msg.result === "dropped") && typeof msg.text === "string") {
|
|
541
|
+
var back = queuedRestoreText(msg.text, msg.pastes, msg.imageCount | 0);
|
|
542
|
+
if (back) {
|
|
543
|
+
putInComposer(back);
|
|
544
|
+
if (msg.cause === "send_now_missed") {
|
|
545
|
+
noteSendNowMiss(msg.messageId, currentScope(), back, { text: msg.text, images: ownSend ? sentHere.images : [], pastes: Array.isArray(msg.pastes) ? msg.pastes : [] }, ownSend);
|
|
546
|
+
}
|
|
547
|
+
else addSystemMessage("A message you queued was not delivered. It is back in your composer.", true);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
var action = item.action;
|
|
553
|
+
item.action = null;
|
|
554
|
+
if (msg.result === "cancelled") {
|
|
555
|
+
removeInflight(item);
|
|
556
|
+
// Edit and take-back both hand the one entry to the composer; a draft typed
|
|
557
|
+
// meanwhile is set aside, not overwritten.
|
|
558
|
+
if (action === "edit" || action === "takeback") putInComposer(item.restoreText);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
if (msg.result === "dropped") {
|
|
562
|
+
removeInflight(item);
|
|
563
|
+
putInComposer(item.restoreText);
|
|
564
|
+
if (msg.cause === "send_now_missed") {
|
|
565
|
+
noteSendNowMiss(item.messageId, item.scope, item.restoreText, { text: item.text, images: item.images, pastes: item.pastes }, !item.redrawn);
|
|
566
|
+
}
|
|
567
|
+
else addSystemMessage(droppedNotice(msg.cause), true);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (msg.result === "pending") {
|
|
571
|
+
// The agent did not drop it, and cannot say whether it runs. It stays
|
|
572
|
+
// queued: delivery turns it into the normal message, otherwise a dropped
|
|
573
|
+
// reply follows with its text.
|
|
574
|
+
item.unconfirmed = true;
|
|
575
|
+
paintInflight(item);
|
|
576
|
+
addSystemMessage(action
|
|
577
|
+
? "The agent could not confirm the withdrawal. The message stays queued; if the agent does not run it, it comes back to your composer."
|
|
578
|
+
: "Withdrawing another message may have dropped this one too. It stays queued; if the agent does not run it, it comes back to your composer.", false);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
if (msg.result === "already_delivered") {
|
|
582
|
+
// The server no longer tracks it. Usually the agent has it and the delivery
|
|
583
|
+
// event follows (or came first, and the item is already gone); the same
|
|
584
|
+
// result also answers an id cancelled earlier or unknown, where none follows.
|
|
585
|
+
// Nothing is handed back, so nothing is doubled.
|
|
586
|
+
item.delivered = true;
|
|
587
|
+
paintInflight(item);
|
|
588
|
+
addSystemMessage("This message can no longer be cancelled.", false);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
paintInflight(item);
|
|
592
|
+
addSystemMessage("The message could not be withdrawn" + (msg.message ? ": " + msg.message : ".") + " It is still queued.", true);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Whether a turn of the session on screen is running. */
|
|
596
|
+
export function isTurnRunning() {
|
|
597
|
+
return !!store.get("processing");
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function restoreSendButton() {
|
|
601
|
+
setSendBtnMode(store.get("processing") || isShellPending() ? "stop" : "send");
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function newRequestId() {
|
|
605
|
+
if (window.crypto && window.crypto.randomUUID) return window.crypto.randomUUID();
|
|
606
|
+
return String(Date.now()) + Math.random().toString(36).slice(2);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Sends a command and, only once the frame was sent, draws its card and marks it
|
|
611
|
+
* pending. A frame that was not sent leaves nothing pending, so later chat is
|
|
612
|
+
* never held for a command the server never saw.
|
|
613
|
+
* @param {string} command
|
|
614
|
+
* @param {function(object): boolean} transmit - sends a frame; true when sent
|
|
615
|
+
* @returns {boolean} whether the request was sent
|
|
616
|
+
*/
|
|
617
|
+
export function sendShellCommand(command, transmit) {
|
|
618
|
+
var requestId = newRequestId();
|
|
619
|
+
var payload = { type: "shell", command: command, requestId: requestId };
|
|
620
|
+
var vendor = store.get("currentVendor");
|
|
621
|
+
if (vendor) payload.vendor = vendor;
|
|
622
|
+
if (!transmit(payload)) return false;
|
|
623
|
+
var card = buildCard(command, "pending");
|
|
624
|
+
showRunning(card);
|
|
625
|
+
addToMessages(card);
|
|
626
|
+
scrollToBottom();
|
|
627
|
+
pending.set(requestId, { scope: currentScope(), command: command, card: card });
|
|
628
|
+
setSendBtnMode("stop");
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Keeps a chat message typed while a command runs (or while an earlier message
|
|
634
|
+
* is still unsent), shown as queued. It is sent in order by flushQueuedChat.
|
|
635
|
+
* @param {{text: string, images: Array, pastes: Array<string>, steerNow: boolean}} message
|
|
636
|
+
*/
|
|
637
|
+
export function queueChatMessage(message) {
|
|
638
|
+
var restoreText = [message.text].concat(message.pastes).filter(Boolean).join("\n\n");
|
|
639
|
+
if (message.images.length > 0) restoreText = restoreText ? restoreText + "\n" + IMAGE_NOTE : IMAGE_NOTE;
|
|
640
|
+
chatQueue.add({
|
|
641
|
+
scope: currentScope(),
|
|
642
|
+
text: message.text,
|
|
643
|
+
images: message.images,
|
|
644
|
+
pastes: message.pastes,
|
|
645
|
+
steerNow: message.steerNow,
|
|
646
|
+
imageCount: message.images.length,
|
|
647
|
+
restoreText: restoreText,
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Keeps a ! line typed while a turn runs, while a command is pending, or behind
|
|
653
|
+
* earlier queued entries, shown as queued. It is sent in order by flushQueuedChat.
|
|
654
|
+
* @param {string} command - the line after the "!", as parseShellLine returns it
|
|
655
|
+
*/
|
|
656
|
+
export function queueShellCommand(command) {
|
|
657
|
+
chatQueue.add(shellItem(command, currentScope()));
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function shellItem(command, scope) {
|
|
661
|
+
return { scope: scope, kind: "shell", command: command, text: command, imageCount: 0, restoreText: "!" + command };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** Whether a turn or a command of this session must end before the entry can be sent. */
|
|
665
|
+
function mustWaitFor(entry) {
|
|
666
|
+
if (isShellPending()) return true;
|
|
667
|
+
return entry.kind === "shell" && isTurnRunning();
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Sends the session's queued entries, one at a time in order: chat goes at once, a
|
|
672
|
+
* ! entry only when no turn is running, and anything behind a ! entry only after
|
|
673
|
+
* its result.
|
|
674
|
+
* Called when a command ends, when the page has rebuilt a session, and before
|
|
675
|
+
* any new chat is sent while an earlier message is still unsent.
|
|
676
|
+
*/
|
|
677
|
+
export function flushQueuedChat() {
|
|
678
|
+
// Any flush supersedes a pending retry; a busy refusal arms a new one.
|
|
679
|
+
cancelRetry();
|
|
680
|
+
if (!chatSender || store.get("replayingHistory") || !store.get("connected")) return;
|
|
681
|
+
chatQueue.flush(currentScope(), chatSender, function (entry) { return !mustWaitFor(entry); });
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
function composerText() { return composer ? composer.read() : ""; }
|
|
685
|
+
|
|
686
|
+
// Edit on a refused command: the command goes back to the composer. A draft
|
|
687
|
+
// already there is set aside, not overwritten (and not run: the draft is never
|
|
688
|
+
// placed behind a "!"), and returns to the composer after the next send.
|
|
689
|
+
function editRefusedCommand(command) {
|
|
690
|
+
putInComposer("!" + command);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Puts one queued entry's text in the composer for editing. A draft already
|
|
694
|
+
// there is set aside, never joined to it, so the composer holds one entry.
|
|
695
|
+
function putInComposer(text) {
|
|
696
|
+
if (!composer) return;
|
|
697
|
+
var draft = composerText();
|
|
698
|
+
if (draft.trim() && draft !== text) {
|
|
699
|
+
setAside = { scope: currentScope(), text: setAside ? setAside.text + "\n\n" + draft : draft };
|
|
700
|
+
addSystemMessage("Your draft was set aside while you edit. It comes back in the composer after you send.", false);
|
|
701
|
+
}
|
|
702
|
+
composer.write(text);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** After a send: puts a draft that was set aside by an Edit back in the empty composer. */
|
|
706
|
+
export function restoreSetAsideDraft() {
|
|
707
|
+
if (!setAside || !composer || composerText().trim()) return;
|
|
708
|
+
var text = setAside.text;
|
|
709
|
+
setAside = null;
|
|
710
|
+
composer.write(text);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
export function handleShellResult(msg) {
|
|
714
|
+
var entry = pending.get(msg.requestId);
|
|
715
|
+
pending.delete(msg.requestId);
|
|
716
|
+
var card = entry ? entry.card : null;
|
|
717
|
+
// A command the server refused as busy (a turn started first) is not an answer
|
|
718
|
+
// to show: it goes back to the head of the queue and is sent when the session is
|
|
719
|
+
// next free. The server's refusal stays as the backstop; the page never shows it.
|
|
720
|
+
if (entry && msg.refused && msg.reason === "busy" && !store.get("replayingHistory")) {
|
|
721
|
+
if (card && card.remove) card.remove();
|
|
722
|
+
chatQueue.addFirst(shellItem(entry.command, entry.scope), true);
|
|
723
|
+
scrollToBottom();
|
|
724
|
+
restoreSendButton();
|
|
725
|
+
armRetry();
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
retryCount = 0;
|
|
729
|
+
if (!card) {
|
|
730
|
+
card = buildCard(msg.command, "pending");
|
|
731
|
+
addToMessages(card);
|
|
732
|
+
}
|
|
733
|
+
fillCard(card, msg, { onEdit: editRefusedCommand });
|
|
734
|
+
scrollToBottom();
|
|
735
|
+
if (!store.get("replayingHistory")) restoreSendButton();
|
|
736
|
+
// Whatever way the command ended (result, refusal, denial, error, timeout or
|
|
737
|
+
// stop) it arrives here, and what was typed meanwhile goes now, in order.
|
|
738
|
+
if (entry) flushQueuedChat();
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* The server put a session on screen (first load, a switch or a reconnect), and
|
|
743
|
+
* the transcript was cleared. Queued chat of a session left behind is parked as
|
|
744
|
+
* text; what is parked for this session goes back in front of its draft.
|
|
745
|
+
* @param {HTMLTextAreaElement} inputEl - the composer, already holding the session's draft
|
|
746
|
+
*/
|
|
747
|
+
export function handleSessionShown(inputEl) {
|
|
748
|
+
var scope = currentScope();
|
|
749
|
+
if (lastScope !== null && lastScope !== scope) {
|
|
750
|
+
// A result for a command of a session left behind is not sent to this page.
|
|
751
|
+
pending.forEach(function (p, id) { if (p.scope !== scope) pending.delete(id); });
|
|
752
|
+
if (setAside && setAside.scope !== scope) {
|
|
753
|
+
chatQueue.park(setAside.scope, setAside.text);
|
|
754
|
+
setAside = null;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
lastScope = scope;
|
|
758
|
+
cancelRetry();
|
|
759
|
+
retryCount = 0;
|
|
760
|
+
chatQueue.leave(scope);
|
|
761
|
+
chatQueue.forgetDrawn();
|
|
762
|
+
// The transcript is rebuilt from the server's history, which has every message
|
|
763
|
+
// the agent took in. What was still waiting is drawn again from the server's
|
|
764
|
+
// queued_message_tracked frames, which follow the replay.
|
|
765
|
+
inflight.clear();
|
|
766
|
+
pending.forEach(function (p) { p.card = null; });
|
|
767
|
+
var back = chatQueue.takeParked(scope);
|
|
768
|
+
if (back.some(function (b) { return b.kind === "shell"; })) {
|
|
769
|
+
// A ! line must stay a command of its own: the entries go back in the queue,
|
|
770
|
+
// in typed order, not into the composer where they would join into one line.
|
|
771
|
+
var drafts = [];
|
|
772
|
+
var items = [];
|
|
773
|
+
back.forEach(function (b) {
|
|
774
|
+
if (b.kind === "shell") items.push(shellItem(b.command, scope));
|
|
775
|
+
else if (b.kind === "draft") drafts.push(b.text);
|
|
776
|
+
else items.push({ scope: scope, kind: "chat", text: b.text, images: [], pastes: [], steerNow: false, imageCount: 0, restoreText: b.text });
|
|
777
|
+
});
|
|
778
|
+
chatQueue.restore(items);
|
|
779
|
+
if (inputEl.value) drafts.push(inputEl.value);
|
|
780
|
+
inputEl.value = drafts.join("\n\n");
|
|
781
|
+
} else if (back.length > 0) {
|
|
782
|
+
var texts = back.map(function (b) { return b.text; });
|
|
783
|
+
if (inputEl.value) texts.push(inputEl.value);
|
|
784
|
+
inputEl.value = texts.join("\n\n");
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* The session's history has been replayed, so the transcript is whole: puts back
|
|
790
|
+
* what the page holds that history cannot (a running command's card, queued
|
|
791
|
+
* chat), and resumes the queue if the command it waited for is done.
|
|
792
|
+
*/
|
|
793
|
+
export function handleHistoryReplayed() {
|
|
794
|
+
var scope = currentScope();
|
|
795
|
+
pending.forEach(function (p) {
|
|
796
|
+
if (p.scope !== scope || p.card) return;
|
|
797
|
+
p.card = buildCard(p.command, "pending");
|
|
798
|
+
showRunning(p.card);
|
|
799
|
+
addToMessages(p.card);
|
|
800
|
+
});
|
|
801
|
+
chatQueue.draw(scope);
|
|
802
|
+
settleMissedOfShownSession();
|
|
803
|
+
scrollToBottom();
|
|
804
|
+
restoreSendButton();
|
|
805
|
+
flushQueuedChat();
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
export function handleUserShellSettings(msg) {
|
|
809
|
+
store.set({
|
|
810
|
+
userShellSettings: {
|
|
811
|
+
enabled: msg.userShellEnabled !== false,
|
|
812
|
+
timeoutMinutes: msg.userShellTimeoutMinutes,
|
|
813
|
+
maxOutputKiB: msg.userShellMaxOutputKiB,
|
|
814
|
+
},
|
|
815
|
+
});
|
|
816
|
+
renderUserShellSettings();
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// A turn ending releases what was typed meanwhile (a ! entry waits for it).
|
|
820
|
+
store.subscribe(["processing"], function (state, prev) {
|
|
821
|
+
chatQueue.eachDrawn(function (e) { if (e.handle && e.handle.refresh) e.handle.refresh(); });
|
|
822
|
+
inflight.forEach(paintInflight);
|
|
823
|
+
if (prev.processing && !state.processing) flushQueuedChat();
|
|
824
|
+
});
|