@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
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
// one user_mention per target. This keeps the wire protocol and progress
|
|
13
13
|
// tracking simple. Multi-target support can be layered on later.
|
|
14
14
|
|
|
15
|
+
var { notifyFor } = require("./notify");
|
|
16
|
+
|
|
15
17
|
function attachUserMention(ctx) {
|
|
16
18
|
var slug = ctx.slug || "";
|
|
17
19
|
var sm = ctx.sm;
|
|
@@ -146,43 +148,29 @@ function attachUserMention(ctx) {
|
|
|
146
148
|
"[End of @mention context. This is for your reference only. Do not respond to it directly.]"
|
|
147
149
|
);
|
|
148
150
|
|
|
149
|
-
//
|
|
151
|
+
// One notify() for the mentioned user: the banner and the push go to them
|
|
152
|
+
// alone and land on this project's session. The push is skipped while they
|
|
153
|
+
// are online in the app (the banner already reaches them), which the core
|
|
154
|
+
// decides from the presence reader.
|
|
150
155
|
var preview = buildPreview(msg.text);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
notificationsModule.notify("user_mention", {
|
|
156
|
+
try {
|
|
157
|
+
notifyFor(pushModule, getNotificationsModule()).notify("user_mention", {
|
|
154
158
|
slug: slug,
|
|
155
159
|
sessionId: session.localId,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// double-buzzing for users who are actively connected.
|
|
171
|
-
if (pushModule && typeof pushModule.sendPushToUser === "function" && !isUserOnline(target.id)) {
|
|
172
|
-
try {
|
|
173
|
-
pushModule.sendPushToUser(target.id, {
|
|
174
|
-
type: "user_mention",
|
|
175
|
-
title: "@" + fromName + " mentioned you",
|
|
176
|
-
body: preview || "Tap to open the session",
|
|
177
|
-
tag: "clay-user-mention-" + target.id,
|
|
178
|
-
data: {
|
|
179
|
-
slug: slug,
|
|
180
|
-
sessionId: session.localId,
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
} catch (e) {
|
|
184
|
-
console.error("[user-mention] push failed:", e && e.message ? e.message : e);
|
|
185
|
-
}
|
|
160
|
+
}, {
|
|
161
|
+
push: { title: "@" + fromName + " mentioned you", body: preview || "Tap to open the session" },
|
|
162
|
+
banner: {
|
|
163
|
+
fromUserId: fromId,
|
|
164
|
+
fromName: fromName,
|
|
165
|
+
fromAvatarStyle: fromAvatar.style || null,
|
|
166
|
+
fromAvatarSeed: fromAvatar.seed || null,
|
|
167
|
+
fromAvatarColor: fromAvatar.color || null,
|
|
168
|
+
fromAvatarCustom: fromAvatar.custom || "",
|
|
169
|
+
preview: preview || "Mentioned you",
|
|
170
|
+
},
|
|
171
|
+
}, { ownerId: target.id, isOnline: isUserOnline });
|
|
172
|
+
} catch (e) {
|
|
173
|
+
console.error("[user-mention] notification failed:", e && e.message ? e.message : e);
|
|
186
174
|
}
|
|
187
175
|
}
|
|
188
176
|
|
|
@@ -5,8 +5,15 @@ var { putOwn } = require("./prompt-kinds/codecs");
|
|
|
5
5
|
var { isKnownVendor } = require("./yoke");
|
|
6
6
|
var { INITIATORS } = require("./turn-initiator");
|
|
7
7
|
var { createWsHandlers: createScheduledInputHandlers } = require("./scheduled-input/ws");
|
|
8
|
+
var { createWsHandlers: createTeamTrafficHandlers } = require("./team-traffic-ws");
|
|
9
|
+
var { createWsHandlers: createAgentRosterHandlers } = require("./agent-roster-ws");
|
|
8
10
|
var { stampRecord } = require("./injected-sender");
|
|
9
11
|
var promptSource = require("./prompt-source");
|
|
12
|
+
var queuedChat = require("./queued-chat");
|
|
13
|
+
|
|
14
|
+
// How long after a cancel the SDK answered false to an idle session waits for the
|
|
15
|
+
// agent to take the message before reporting it dropped.
|
|
16
|
+
var UNCONFIRMED_GRACE_MS = 10000;
|
|
10
17
|
|
|
11
18
|
/**
|
|
12
19
|
* Attach user-message handler and remaining small handlers
|
|
@@ -134,6 +141,9 @@ function attachUserMessage(ctx) {
|
|
|
134
141
|
sendTo: sendTo,
|
|
135
142
|
});
|
|
136
143
|
|
|
144
|
+
var teamTraffic = createTeamTrafficHandlers({ service: ctx.teamTraffic || null, sendTo: sendTo });
|
|
145
|
+
var agentRoster = createAgentRosterHandlers({ service: ctx.agentRoster || null, sendTo: sendTo });
|
|
146
|
+
|
|
137
147
|
// --------------- Terminal list broadcast ---------------
|
|
138
148
|
|
|
139
149
|
// Each client only sees their own terminals — send filtered term_list messages individually.
|
|
@@ -211,7 +221,9 @@ function attachUserMessage(ctx) {
|
|
|
211
221
|
}
|
|
212
222
|
tm.attach(t.id, ws);
|
|
213
223
|
broadcastTermList();
|
|
214
|
-
|
|
224
|
+
var created = { type: "term_created", id: t.id };
|
|
225
|
+
if (typeof msg.requestId === "string" && msg.requestId.length <= 64) created.requestId = msg.requestId;
|
|
226
|
+
sendTo(ws, created);
|
|
215
227
|
return true;
|
|
216
228
|
}
|
|
217
229
|
|
|
@@ -331,6 +343,22 @@ function attachUserMessage(ctx) {
|
|
|
331
343
|
// --- Scheduled input: the clock button and the scheduled-message bubble ---
|
|
332
344
|
if (scheduledInput.handle(ws, msg)) return true;
|
|
333
345
|
|
|
346
|
+
// --- Team view: agent-to-agent traffic through the console (read-only) ---
|
|
347
|
+
if (teamTraffic.handle(ws, msg)) return true;
|
|
348
|
+
if (agentRoster.handle(ws, msg)) return true;
|
|
349
|
+
|
|
350
|
+
// --- Withdraw a chat message that has not been taken into the turn yet ---
|
|
351
|
+
if (msg.type === "cancel_queued_message") {
|
|
352
|
+
handleCancelQueued(ws, msg);
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// --- Send now on a chat message already with the agent ---
|
|
357
|
+
if (msg.type === "send_now_queued_message") {
|
|
358
|
+
handleSendNowQueued(ws, msg);
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
|
|
334
362
|
if (msg.type !== "message") return false;
|
|
335
363
|
if (!msg.text && (!msg.images || msg.images.length === 0) && (!msg.pastes || msg.pastes.length === 0)) return true;
|
|
336
364
|
|
|
@@ -366,7 +394,16 @@ function attachUserMessage(ctx) {
|
|
|
366
394
|
|
|
367
395
|
// The human initiator is what marks an entry as the operator's own: this is
|
|
368
396
|
// the one place the authenticated browser composer's text is recorded.
|
|
397
|
+
// A page that minted an id for the message gets it back on the delivery
|
|
398
|
+
// event; the same id is the message's uuid at the agent (stampRecord keeps
|
|
399
|
+
// an id already set).
|
|
400
|
+
var clientMessageId = queuedChat.isMessageId(msg.messageId) ? msg.messageId : null;
|
|
369
401
|
var userMsg2 = stampRecord({ type: "user_message", text: msg.text || "", initiator: INITIATORS.HUMAN });
|
|
402
|
+
if (clientMessageId) userMsg2.messageId = clientMessageId;
|
|
403
|
+
// Send-now into a running turn the agent can steer: the record is written
|
|
404
|
+
// once, after the steer outcome is known, and carries the mid-turn mark only
|
|
405
|
+
// when the message reached the running turn. Every other path records at once.
|
|
406
|
+
var steerNow = msg.sendNow === true && session.isProcessing === true && sdk.canSteer(session);
|
|
370
407
|
// Attach sender info for multi-user attribution (backward-compatible: old clients ignore these)
|
|
371
408
|
if (ws._clagenticUser) {
|
|
372
409
|
userMsg2.from = ws._clagenticUser.id;
|
|
@@ -395,9 +432,25 @@ function attachUserMessage(ctx) {
|
|
|
395
432
|
if (!userMsg2._ts) userMsg2._ts = Date.now();
|
|
396
433
|
// lr-2ea2a7: routes through the shared history-cap helper (see grep-guard
|
|
397
434
|
// test) instead of pushing directly.
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
435
|
+
var recorded = false;
|
|
436
|
+
function recordUserMessage() {
|
|
437
|
+
if (recorded) return;
|
|
438
|
+
recorded = true;
|
|
439
|
+
sm.recordHistoryEntry(session, userMsg2, true);
|
|
440
|
+
sm.appendToSessionFile(session, userMsg2);
|
|
441
|
+
sendToSessionOthers(ws, session.localId, hydrateImageRefs(userMsg2));
|
|
442
|
+
}
|
|
443
|
+
// Chat pushed into a running turn on an agent that can withdraw it is
|
|
444
|
+
// recorded when the agent takes it in (and only then), so a message that is
|
|
445
|
+
// cancelled before that leaves no trace in the conversation.
|
|
446
|
+
var trackQueued = !steerNow && clientMessageId !== null && session.isProcessing === true &&
|
|
447
|
+
typeof sdk.canCancelQueued === "function" && sdk.canCancelQueued(session);
|
|
448
|
+
if (!steerNow && !trackQueued) recordUserMessage();
|
|
449
|
+
// The page drew this message as queued: what ends that is said once, here,
|
|
450
|
+
// for every path on which the message is not tracked as queued.
|
|
451
|
+
function announceDelivered() {
|
|
452
|
+
if (clientMessageId) sendTo(ws, { type: "message_delivered", messageId: clientMessageId });
|
|
453
|
+
}
|
|
401
454
|
|
|
402
455
|
if (!session.title) {
|
|
403
456
|
session.title = deriveProvisionalTitle(msg);
|
|
@@ -531,9 +584,11 @@ function attachUserMessage(ctx) {
|
|
|
531
584
|
|
|
532
585
|
function dispatchToSdk(finalText) {
|
|
533
586
|
if (!session.isProcessing) {
|
|
587
|
+
recordUserMessage(); // a steer-now whose turn ended while the context was gathered is a plain send
|
|
534
588
|
session.isProcessing = true;
|
|
535
589
|
onProcessingChanged();
|
|
536
590
|
session.sentToolResults = {};
|
|
591
|
+
announceDelivered();
|
|
537
592
|
sendToSession(session.localId, { type: "status", status: "processing" });
|
|
538
593
|
if (!session.queryInstance && (!session.worker || session.messageQueue !== "worker")) {
|
|
539
594
|
// No active query (or worker idle between queries): start a new query
|
|
@@ -543,7 +598,60 @@ function attachUserMessage(ctx) {
|
|
|
543
598
|
} else {
|
|
544
599
|
sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN, userMsg2.messageId);
|
|
545
600
|
}
|
|
601
|
+
} else if (steerNow) {
|
|
602
|
+
sdk.steerMessage(session, finalText, msg.images, INITIATORS.HUMAN, userMsg2.messageId).then(null, function (err) {
|
|
603
|
+
console.error("[project-user-message] steer failed:", err && err.message);
|
|
604
|
+
return { steered: false, reason: "unconfirmed" };
|
|
605
|
+
}).then(function (result) {
|
|
606
|
+
var steered = !!(result && result.steered);
|
|
607
|
+
if (steered) userMsg2.sentMidTurn = true;
|
|
608
|
+
recordUserMessage();
|
|
609
|
+
if (steered) return;
|
|
610
|
+
// Not delivered into the turn. The record above is unmarked. Queued
|
|
611
|
+
// means it runs next turn; unconfirmed means it may already have landed,
|
|
612
|
+
// so it is not sent again and the user decides whether to resend.
|
|
613
|
+
sendToSession(session.localId, {
|
|
614
|
+
type: "diagnostic",
|
|
615
|
+
severity: "info",
|
|
616
|
+
source: "send-now",
|
|
617
|
+
message: result && result.reason === "unconfirmed"
|
|
618
|
+
? "Clagentic: Console could not confirm this message reached the running turn. It was not queued again, so resend it if it did not arrive."
|
|
619
|
+
: "The turn finished before this message could be sent into it, so it was queued for the next turn.",
|
|
620
|
+
});
|
|
621
|
+
});
|
|
546
622
|
} else {
|
|
623
|
+
if (trackQueued) {
|
|
624
|
+
// Carried on the dropped and cancelled replies: a page that no longer
|
|
625
|
+
// holds its queued item (a session switch, a reload) still gets the text back.
|
|
626
|
+
var restore = {
|
|
627
|
+
text: msg.text || "",
|
|
628
|
+
pastes: Array.isArray(msg.pastes) ? msg.pastes.filter(function (p) { return typeof p === "string"; }) : [],
|
|
629
|
+
imageCount: Array.isArray(msg.images) ? msg.images.length : 0,
|
|
630
|
+
};
|
|
631
|
+
queuedChat.register(session, clientMessageId, {
|
|
632
|
+
onDelivered: function () {
|
|
633
|
+
recordUserMessage();
|
|
634
|
+
sendToSession(session.localId, { type: "message_delivered", messageId: clientMessageId });
|
|
635
|
+
sm.saveSessionFile(session);
|
|
636
|
+
},
|
|
637
|
+
onDropped: function (cause) {
|
|
638
|
+
sendToSession(session.localId, Object.assign({ type: "queued_message_cancelled", messageId: clientMessageId, result: "dropped", cause: cause }, restore));
|
|
639
|
+
// A reply nobody is there to read (no page shows the session, or the
|
|
640
|
+
// daemon is going down) is not a hand-back: the text is kept with the
|
|
641
|
+
// session and goes out the next time a page shows it.
|
|
642
|
+
if (session.destroying || !sessionHasViewer(session)) queuedChat.park(session, clientMessageId, restore, cause);
|
|
643
|
+
sm.saveSessionFile(session);
|
|
644
|
+
},
|
|
645
|
+
onUnconfirmed: function () {
|
|
646
|
+
sendToSession(session.localId, { type: "queued_message_cancelled", messageId: clientMessageId, result: "pending" });
|
|
647
|
+
},
|
|
648
|
+
}, restore, session.queryInstance);
|
|
649
|
+
// Written down before the agent can answer: a daemon that dies now still
|
|
650
|
+
// knows what was waiting.
|
|
651
|
+
sm.saveSessionFile(session);
|
|
652
|
+
} else {
|
|
653
|
+
announceDelivered();
|
|
654
|
+
}
|
|
547
655
|
sdk.pushMessage(session, finalText, msg.images, INITIATORS.HUMAN, userMsg2.messageId);
|
|
548
656
|
}
|
|
549
657
|
sm.broadcastSessionList();
|
|
@@ -706,6 +814,102 @@ function attachUserMessage(ctx) {
|
|
|
706
814
|
return true;
|
|
707
815
|
}
|
|
708
816
|
|
|
817
|
+
// Whether a page that is on screen shows this session, so a reply sent to it is read.
|
|
818
|
+
function sessionHasViewer(session) {
|
|
819
|
+
var seen = false;
|
|
820
|
+
clients.forEach(function (c) { if (c._clagenticActiveSession === session.localId) seen = true; });
|
|
821
|
+
return seen;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// A cancel the agent answered false to ends delivered (a frame names it),
|
|
825
|
+
// closed (its query closes) or here: after a grace period with no turn running
|
|
826
|
+
// nothing is left to take it, so it is dropped with its text. While a turn
|
|
827
|
+
// runs it is left alone, since the agent may run the message as the next turn.
|
|
828
|
+
// One timer per session: every pending cancel re-arms it, so the newest
|
|
829
|
+
// unconfirmed message still gets the full grace period and cancels do not
|
|
830
|
+
// stack timer chains.
|
|
831
|
+
var idleTimers = new WeakMap();
|
|
832
|
+
function settleWhenIdle(session) {
|
|
833
|
+
var armed = idleTimers.get(session);
|
|
834
|
+
if (armed) clearTimeout(armed);
|
|
835
|
+
var timer = setTimeout(function () {
|
|
836
|
+
idleTimers.delete(session);
|
|
837
|
+
queuedChat.settleIdle(session, !!session.isProcessing);
|
|
838
|
+
if (queuedChat.hasUnconfirmed(session)) settleWhenIdle(session);
|
|
839
|
+
}, UNCONFIRMED_GRACE_MS);
|
|
840
|
+
if (timer && typeof timer.unref === "function") timer.unref();
|
|
841
|
+
idleTimers.set(session, timer);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// A page asks to withdraw a chat message it drew as queued. The answer is
|
|
845
|
+
// "cancelled" (the agent dropped it; it is never delivered), "already_delivered"
|
|
846
|
+
// (it was already consumed, or it is not tracked: the page keeps the message),
|
|
847
|
+
// "pending" (the agent did not drop it from its queue and does not say whether
|
|
848
|
+
// it runs: it stays queued until delivered, or is reported dropped with its
|
|
849
|
+
// text), or "unsupported" when this agent cannot withdraw one.
|
|
850
|
+
function handleCancelQueued(ws, msg) {
|
|
851
|
+
var session = getSessionForWs(ws);
|
|
852
|
+
var id = msg.messageId;
|
|
853
|
+
if (!session || !queuedChat.isMessageId(id)) return;
|
|
854
|
+
function reply(result, extra) {
|
|
855
|
+
sendTo(ws, Object.assign({ type: "queued_message_cancelled", messageId: id, result: result }, extra || {}));
|
|
856
|
+
}
|
|
857
|
+
if (typeof sdk.canCancelQueued !== "function" || !sdk.canCancelQueued(session)) {
|
|
858
|
+
reply("unsupported");
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
var restore = queuedChat.restoreOf(session, id);
|
|
862
|
+
queuedChat.cancel(session, id, function (messageId) {
|
|
863
|
+
return sdk.cancelQueuedMessage(session, messageId);
|
|
864
|
+
}).then(function (result) {
|
|
865
|
+
reply(result, result === "cancelled" ? restore : null);
|
|
866
|
+
if (result === "cancelled") {
|
|
867
|
+
// Another page may have drawn the same item (a reload, a second tab): it
|
|
868
|
+
// drops it. No text goes with this, so it puts nothing in its composer.
|
|
869
|
+
sendToSessionOthers(ws, session.localId, { type: "queued_message_cancelled", messageId: id, result: "cancelled" });
|
|
870
|
+
sm.saveSessionFile(session);
|
|
871
|
+
}
|
|
872
|
+
if (result === "pending") settleWhenIdle(session);
|
|
873
|
+
}, function (err) {
|
|
874
|
+
console.error("[project-user-message] cancel of a queued message failed:", err && err.message);
|
|
875
|
+
reply("error", { message: (err && err.message) || "The agent did not answer." });
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// A page presses Send now on a chat item drawn as queued. The message was
|
|
880
|
+
// pushed into the agent's input when it was sent, so nothing is pushed again:
|
|
881
|
+
// the running turn is interrupted with the agent's queue kept and the message
|
|
882
|
+
// runs as the next input (queued-chat.js decides what the interrupt receipt
|
|
883
|
+
// means). The answer, queued_message_send_now, is "requested" (the item is
|
|
884
|
+
// drawn as sending on every page of the session), "already_delivered" (the
|
|
885
|
+
// message is not tracked: delivered, cancelled or unknown), "no_turn" (nothing
|
|
886
|
+
// runs, so nothing was interrupted), "missed" (the agent no longer holds the
|
|
887
|
+
// message: it is handed back through queued_message_cancelled, never re-pushed),
|
|
888
|
+
// "unsupported" or "error".
|
|
889
|
+
function handleSendNowQueued(ws, msg) {
|
|
890
|
+
var session = getSessionForWs(ws);
|
|
891
|
+
var id = msg.messageId;
|
|
892
|
+
if (!session || !queuedChat.isMessageId(id)) return;
|
|
893
|
+
function reply(result, extra) {
|
|
894
|
+
sendTo(ws, Object.assign({ type: "queued_message_send_now", messageId: id, result: result }, extra || {}));
|
|
895
|
+
}
|
|
896
|
+
if (typeof sdk.canCancelQueued !== "function" || !sdk.canCancelQueued(session) || typeof sdk.interruptKeepingQueued !== "function") {
|
|
897
|
+
reply("unsupported");
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
queuedChat.requestSendNow(session, id, {
|
|
901
|
+
turnRunning: !!session.isProcessing,
|
|
902
|
+
liveOwner: session.queryInstance,
|
|
903
|
+
interrupt: function () { return sdk.interruptKeepingQueued(session); },
|
|
904
|
+
}).then(function (result) {
|
|
905
|
+
reply(result);
|
|
906
|
+
if (result === "requested") sendToSessionOthers(ws, session.localId, { type: "queued_message_send_now", messageId: id, result: "requested" });
|
|
907
|
+
}, function (err) {
|
|
908
|
+
console.error("[project-user-message] send-now interrupt failed:", err && err.message);
|
|
909
|
+
reply("error", { message: (err && err.message) || "The agent did not answer." });
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
|
|
709
913
|
return {
|
|
710
914
|
handleUserMessage: handleUserMessage,
|
|
711
915
|
};
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The server side of the ! command line: a `shell` request runs a command the
|
|
4
|
+
// person typed, answered with `shell_result`. This is human input, so its
|
|
5
|
+
// refusals bound what a person may type; none of it is offered to an agent.
|
|
6
|
+
//
|
|
7
|
+
// A request is checked in this order and the first check that applies decides:
|
|
8
|
+
// 1. the ! setting is off -> refused, reason "disabled"
|
|
9
|
+
// (an empty command is refused here too, reason "empty")
|
|
10
|
+
// 2. OS-user isolation is on -> refused, reason "os_users"
|
|
11
|
+
// 3. the session is in plan mode -> refused, reason "plan"
|
|
12
|
+
// (a user without the terminal permission is refused, reason "no_terminal")
|
|
13
|
+
// 4. a turn or a ! command is running -> refused, reason "busy"
|
|
14
|
+
// 5. the session has no live query -> refused, reason "no_query"
|
|
15
|
+
// 6. the adapter lacks runUserShellCommand -> refused, reason "unsupported"
|
|
16
|
+
// 7. the permission gate, as a Bash tool call (sdk.handleCanUseTool, never
|
|
17
|
+
// checkToolWhitelist alone), so every mode and rule decides as it does for
|
|
18
|
+
// Bash
|
|
19
|
+
// Every outcome, a refusal included, is recorded in the session history and
|
|
20
|
+
// sent to the session's clients as `shell_result`.
|
|
21
|
+
//
|
|
22
|
+
// The server holds no chat: a chat send during a command takes the path it
|
|
23
|
+
// takes during a busy turn (project-user-message.js).
|
|
24
|
+
//
|
|
25
|
+
// ctx fields: slug, cwd, osUsers, sm, sdk (handleCanUseTool), sendTo,
|
|
26
|
+
// getSessionForWs, usersModule, projectAccess, opts (onGetUserShellSettings,
|
|
27
|
+
// onSetUserShellSettings, broadcastAll, onProcessingChanged)
|
|
28
|
+
|
|
29
|
+
var crypto = require("crypto");
|
|
30
|
+
var userShellSettings = require("./user-shell-settings");
|
|
31
|
+
var { isServerAdmin } = require("./project-permission-settings");
|
|
32
|
+
var { bumpLastActivity } = require("./activity-clock");
|
|
33
|
+
|
|
34
|
+
var MAX_REQUEST_ID_CHARS = 64;
|
|
35
|
+
// A long command must not let the idle reaper end its session; the reaper's
|
|
36
|
+
// limit is 30 minutes, so a minute is ample.
|
|
37
|
+
var ACTIVITY_TOUCH_MS = 60 * 1000;
|
|
38
|
+
|
|
39
|
+
// Reason codes are the wire contract; the text is what a person reads.
|
|
40
|
+
var REASONS = {
|
|
41
|
+
disabled: { code: "disabled", message: "The ! command line is turned off on this server." },
|
|
42
|
+
empty: { code: "empty", message: "Type a command after !." },
|
|
43
|
+
osUsers: { code: "os_users", message: "The ! command line is off while OS-user isolation is on: the console cannot yet run a command as the session owner's own account, and it never runs one as the daemon's account." },
|
|
44
|
+
plan: { code: "plan", message: "The ! command line is not available in plan mode, which does not run commands. Leave plan mode to use it." },
|
|
45
|
+
noTerminal: { code: "no_terminal", message: "Your account is not allowed to run shell commands." },
|
|
46
|
+
busy: { code: "busy", message: "The session is busy: a turn or another command is running. Stop it or wait for it to end." },
|
|
47
|
+
noQuery: { code: "no_query", message: "This session has no live agent process to record the command in yet. Send a message first (the process starts with it and ends after a long idle time), then use !." },
|
|
48
|
+
unsupported: { code: "unsupported", message: "This session's agent cannot run a command typed with !." },
|
|
49
|
+
denied: { code: "denied", message: "The command was not approved, so it did not run." },
|
|
50
|
+
gone: { code: "gone", message: "The session's agent ended while the command waited for approval, so it did not run." },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function attachUserShell(ctx) {
|
|
54
|
+
var opts = ctx.opts || {};
|
|
55
|
+
// One entry per session with a request in flight: { requestId, ac, busy }.
|
|
56
|
+
var running = new Map();
|
|
57
|
+
|
|
58
|
+
// A context built without the daemon's callback (a bare harness) runs with
|
|
59
|
+
// the defaults.
|
|
60
|
+
function currentSettings() {
|
|
61
|
+
return typeof opts.onGetUserShellSettings === "function"
|
|
62
|
+
? opts.onGetUserShellSettings()
|
|
63
|
+
: userShellSettings.readUserShellSettings(null);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function requestIdOf(msg) {
|
|
67
|
+
return typeof msg.requestId === "string" && msg.requestId && msg.requestId.length <= MAX_REQUEST_ID_CHARS
|
|
68
|
+
? msg.requestId : crypto.randomUUID();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function entryFor(requestId, command, fields) {
|
|
72
|
+
return Object.assign({
|
|
73
|
+
type: "shell_result",
|
|
74
|
+
requestId: requestId,
|
|
75
|
+
command: command,
|
|
76
|
+
exitCode: null,
|
|
77
|
+
durationMs: 0,
|
|
78
|
+
output: "",
|
|
79
|
+
truncated: false,
|
|
80
|
+
timedOut: false,
|
|
81
|
+
interrupted: false,
|
|
82
|
+
}, fields);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function refusal(requestId, command, reason) {
|
|
86
|
+
return entryFor(requestId, command, { refused: true, reason: reason.code, message: reason.message });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function planMode(session) {
|
|
90
|
+
return (session.permissionMode || ctx.sm.currentPermissionMode) === "plan";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Records the outcome and tells every client viewing the session. The busy
|
|
94
|
+
// state ends here whatever happens to the send or the record, so no exit path
|
|
95
|
+
// leaves the session stuck busy.
|
|
96
|
+
// Once per run: a late path (a throw after the result, a second handler)
|
|
97
|
+
// cannot send or record twice or clear a later run's busy state.
|
|
98
|
+
function conclude(session, run, entry) {
|
|
99
|
+
if (run.done) return;
|
|
100
|
+
run.done = true;
|
|
101
|
+
if (run.touch) clearInterval(run.touch);
|
|
102
|
+
if (running.get(session.localId) === run) running.delete(session.localId);
|
|
103
|
+
try {
|
|
104
|
+
bumpLastActivity(session, "user-shell", ctx.slug);
|
|
105
|
+
ctx.sm.sendAndRecord(session, entry);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.error("[project-user-shell] could not record the result (slug=" + ctx.slug + "):", (err && err.message) || err);
|
|
108
|
+
} finally {
|
|
109
|
+
if (run.busy) clearBusy(session);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The terminal handler of every chain: whatever threw, the run ends with an
|
|
114
|
+
// error entry instead of leaving the session busy with no result.
|
|
115
|
+
function failRun(session, run, command, err) {
|
|
116
|
+
console.error("[project-user-shell] command failed (slug=" + ctx.slug + "):", (err && err.message) || err);
|
|
117
|
+
conclude(session, run, entryFor(run.requestId, command, run.ac.signal.aborted
|
|
118
|
+
? { interrupted: true }
|
|
119
|
+
: { error: true, output: (err && err.message) || String(err) }));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function processingChanged() {
|
|
123
|
+
if (typeof opts.onProcessingChanged === "function") opts.onProcessingChanged();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// From approval until the result is recorded the session is busy, the same
|
|
127
|
+
// isProcessing state a model turn uses.
|
|
128
|
+
function markBusy(session, run) {
|
|
129
|
+
run.busy = true;
|
|
130
|
+
session.isProcessing = true;
|
|
131
|
+
processingChanged();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The flag is cleared first: a throwing callback must not leave it set.
|
|
135
|
+
function clearBusy(session) {
|
|
136
|
+
session.isProcessing = false;
|
|
137
|
+
try {
|
|
138
|
+
processingChanged();
|
|
139
|
+
} catch (err) {
|
|
140
|
+
console.error("[project-user-shell] processing callback failed (slug=" + ctx.slug + "):", (err && err.message) || err);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function runLine(ws, msg) {
|
|
145
|
+
var session = ctx.getSessionForWs(ws);
|
|
146
|
+
if (!session) {
|
|
147
|
+
console.error("[project-user-shell] Dropping shell request: no session bound to this websocket (slug=" + ctx.slug + ")");
|
|
148
|
+
ctx.sendTo(ws, { type: "error", text: "Clagentic: Console couldn't find your session — try reloading." });
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Only leading whitespace is dropped: the rest is the command as typed, so
|
|
153
|
+
// a trailing escaped space is part of it.
|
|
154
|
+
var typed = typeof msg.command === "string" ? msg.command : "";
|
|
155
|
+
var command = typed.replace(/^\s+/, "");
|
|
156
|
+
var requestId = requestIdOf(msg);
|
|
157
|
+
var settings = currentSettings();
|
|
158
|
+
|
|
159
|
+
function refuse(reason) {
|
|
160
|
+
ctx.sm.sendAndRecord(session, refusal(requestId, command, reason));
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!settings.enabled) return refuse(REASONS.disabled);
|
|
165
|
+
if (!command.trim()) return refuse(REASONS.empty);
|
|
166
|
+
// The daemon's own account is never used on a user's behalf, and no
|
|
167
|
+
// run-as-user helper exists yet.
|
|
168
|
+
if (ctx.osUsers) return refuse(REASONS.osUsers);
|
|
169
|
+
// Plan mode does not run commands, and the tool-call path does not check it.
|
|
170
|
+
if (planMode(session)) return refuse(REASONS.plan);
|
|
171
|
+
if (ws._clagenticUser && ctx.usersModule) {
|
|
172
|
+
var perms = ctx.usersModule.getEffectivePermissions(ws._clagenticUser, ctx.osUsers);
|
|
173
|
+
if (!perms.terminal) return refuse(REASONS.noTerminal);
|
|
174
|
+
}
|
|
175
|
+
if (running.has(session.localId) || session.isProcessing) return refuse(REASONS.busy);
|
|
176
|
+
var handle = session.queryInstance;
|
|
177
|
+
if (!handle) return refuse(REASONS.noQuery);
|
|
178
|
+
if (typeof handle.runUserShellCommand !== "function") return refuse(REASONS.unsupported);
|
|
179
|
+
|
|
180
|
+
// Registered before the gate asks, so a second request during the approval
|
|
181
|
+
// is refused as busy.
|
|
182
|
+
var ac = new AbortController();
|
|
183
|
+
var run = { requestId: requestId, ac: ac, busy: false };
|
|
184
|
+
running.set(session.localId, run);
|
|
185
|
+
|
|
186
|
+
var asked;
|
|
187
|
+
try {
|
|
188
|
+
asked = Promise.resolve(ctx.sdk.handleCanUseTool(session, "Bash", { command: command }, {
|
|
189
|
+
toolUseID: "user-shell-" + requestId,
|
|
190
|
+
signal: ac.signal,
|
|
191
|
+
decisionReason: "Typed with !",
|
|
192
|
+
}));
|
|
193
|
+
} catch (err) {
|
|
194
|
+
asked = Promise.reject(err);
|
|
195
|
+
}
|
|
196
|
+
asked.then(function (decision) {
|
|
197
|
+
if (ac.signal.aborted) return conclude(session, run, entryFor(requestId, command, { interrupted: true }));
|
|
198
|
+
if (!decision || decision.behavior !== "allow") {
|
|
199
|
+
return conclude(session, run, entryFor(requestId, command, {
|
|
200
|
+
denied: true,
|
|
201
|
+
reason: REASONS.denied.code,
|
|
202
|
+
message: (decision && decision.message) || REASONS.denied.message,
|
|
203
|
+
}));
|
|
204
|
+
}
|
|
205
|
+
return execute(session, run, command, settings);
|
|
206
|
+
}, function (err) {
|
|
207
|
+
conclude(session, run, entryFor(requestId, command, ac.signal.aborted
|
|
208
|
+
? { interrupted: true }
|
|
209
|
+
: { denied: true, reason: REASONS.denied.code, message: "The approval ended before it was answered: " + ((err && err.message) || err) }));
|
|
210
|
+
}).catch(function (err) {
|
|
211
|
+
failRun(session, run, command, err);
|
|
212
|
+
});
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function execute(session, run, command, settings) {
|
|
217
|
+
var requestId = run.requestId;
|
|
218
|
+
// The query may have ended while the approval waited.
|
|
219
|
+
var handle = session.queryInstance;
|
|
220
|
+
if (!handle || typeof handle.runUserShellCommand !== "function") {
|
|
221
|
+
return conclude(session, run, entryFor(requestId, command, { error: true, reason: REASONS.gone.code, output: REASONS.gone.message }));
|
|
222
|
+
}
|
|
223
|
+
// A chat send during the approval may have started a turn. Two writers on one
|
|
224
|
+
// handle would interleave, so the command is refused and the turn's own busy
|
|
225
|
+
// state is left alone.
|
|
226
|
+
if (session.isProcessing) return conclude(session, run, refusal(requestId, command, REASONS.busy));
|
|
227
|
+
|
|
228
|
+
markBusy(session, run);
|
|
229
|
+
var touch = setInterval(function () { bumpLastActivity(session, "user-shell", ctx.slug); }, ACTIVITY_TOUCH_MS);
|
|
230
|
+
if (typeof touch.unref === "function") touch.unref();
|
|
231
|
+
run.touch = touch;
|
|
232
|
+
var started;
|
|
233
|
+
try {
|
|
234
|
+
started = handle.runUserShellCommand(command, session.cwd || ctx.cwd, settings.timeoutMs, settings.maxOutputBytes, { signal: run.ac.signal });
|
|
235
|
+
} catch (err) {
|
|
236
|
+
started = Promise.reject(err);
|
|
237
|
+
}
|
|
238
|
+
return Promise.resolve(started).then(function (result) {
|
|
239
|
+
// An adapter that resolves with anything but a result object is a failure
|
|
240
|
+
// to report, not a reason to leave the session busy.
|
|
241
|
+
if (!result || typeof result !== "object") {
|
|
242
|
+
throw new Error("the agent returned no result for the command");
|
|
243
|
+
}
|
|
244
|
+
return conclude(session, run, entryFor(requestId, command, {
|
|
245
|
+
exitCode: result.exitCode,
|
|
246
|
+
durationMs: result.durationMs,
|
|
247
|
+
output: result.output,
|
|
248
|
+
truncated: !!result.truncated,
|
|
249
|
+
timedOut: !!result.timedOut,
|
|
250
|
+
interrupted: !!result.interrupted,
|
|
251
|
+
}));
|
|
252
|
+
}).catch(function (err) {
|
|
253
|
+
failRun(session, run, command, err);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Stop cancels a pending approval prompt and aborts a running command.
|
|
258
|
+
function interrupt(ws) {
|
|
259
|
+
var session = ctx.getSessionForWs(ws);
|
|
260
|
+
var run = session && running.get(session.localId);
|
|
261
|
+
if (run) run.ac.abort();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function changeSettings(ws, msg) {
|
|
265
|
+
if (!isServerAdmin(ctx.projectAccess, ws)) {
|
|
266
|
+
console.warn("[project-user-shell] refused set_user_shell_settings in " + ctx.slug + ": caller is not an admin");
|
|
267
|
+
ctx.sendTo(ws, { type: "error", text: "Only admins can change the ! command line settings." });
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
var picked = userShellSettings.pickSettingChanges(msg);
|
|
271
|
+
if (picked.rejected.length > 0) {
|
|
272
|
+
ctx.sendTo(ws, { type: "error", text: "Unknown or invalid ! command line setting: " + picked.rejected.join(", ") });
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
if (Object.keys(picked.values).length === 0) return true;
|
|
276
|
+
if (typeof opts.onSetUserShellSettings !== "function" || typeof opts.onGetUserShellSettings !== "function") {
|
|
277
|
+
ctx.sendTo(ws, { type: "error", text: "The server cannot store the ! command line settings." });
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
opts.onSetUserShellSettings(picked.values);
|
|
281
|
+
var announce = typeof opts.broadcastAll === "function" ? opts.broadcastAll : ctx.send;
|
|
282
|
+
announce(userShellSettings.settingsMessage(opts.onGetUserShellSettings()));
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Returns true when msg was handled here. */
|
|
287
|
+
function handleUserShellMessage(ws, msg) {
|
|
288
|
+
if (msg.type === "shell") return runLine(ws, msg);
|
|
289
|
+
if (msg.type === "set_user_shell_settings") return changeSettings(ws, msg);
|
|
290
|
+
// Stop is also the sessions handler's: it ends a running turn.
|
|
291
|
+
if (msg.type === "stop") interrupt(ws);
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return { handleUserShellMessage: handleUserShellMessage };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
module.exports = { attachUserShell: attachUserShell, REASONS: REASONS };
|