@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.
- package/LICENSE +117 -12
- package/LICENSES/MIT-legacy.txt +22 -0
- package/LICENSES/README.md +15 -0
- package/README.md +9 -3
- package/bin/cli.js +40 -5
- package/deploy/clagentic-console.service +10 -6
- package/lib/agent-dispatch-tool.js +20 -0
- package/lib/audit.js +3 -2
- package/lib/build-changes.js +79 -0
- package/lib/build-identity.js +192 -0
- package/lib/build-monitor.js +125 -0
- package/lib/build-sha.json +2 -2
- package/lib/build-update-check.js +138 -0
- package/lib/build-update-offer.js +141 -0
- package/lib/build-update-restart.js +142 -0
- package/lib/build-update-standing.js +184 -0
- package/lib/cli/daemon-launch.js +25 -22
- package/lib/cli/ipc-subcommands.js +6 -0
- package/lib/cli/session-subcommands.js +122 -0
- package/lib/cli-sessions.js +8 -1
- package/lib/codex-defaults.js +8 -2
- package/lib/config.js +32 -20
- package/lib/daemon-env-settings.js +84 -0
- package/lib/daemon-projects.js +57 -0
- package/lib/daemon.js +288 -205
- package/lib/drain.js +74 -17
- package/lib/env-compat.js +61 -0
- package/lib/extension-commands.js +45 -0
- package/lib/history-visibility.js +4 -0
- package/lib/idle-adapter-reaper.js +71 -0
- package/lib/link-trap.js +78 -0
- package/lib/mcp-local.js +8 -6
- package/lib/mcp-tool-input.js +116 -0
- package/lib/mcp-tool-schema.js +179 -0
- package/lib/memory-limits.js +61 -5
- package/lib/memory-setting-validate.js +65 -0
- package/lib/memory-shed.js +13 -32
- package/lib/new-work-gate.js +47 -0
- package/lib/os-users.js +5 -3
- package/lib/project-access.js +139 -0
- package/lib/project-connection.js +37 -22
- package/lib/project-external-trigger.js +166 -105
- package/lib/project-file-scope.js +172 -0
- package/lib/project-file-watch.js +86 -61
- package/lib/project-filesystem.js +144 -128
- package/lib/project-git.js +119 -0
- package/lib/project-http.js +44 -28
- package/lib/project-loop.js +17 -9
- package/lib/project-mcp.js +53 -72
- package/lib/project-message-gate.js +70 -0
- package/lib/project-notifications.js +31 -4
- package/lib/project-prompt-responses.js +217 -0
- package/lib/project-sessions.js +192 -355
- package/lib/project-user-message.js +27 -15
- package/lib/project.js +212 -147
- package/lib/projects-dir.js +90 -0
- package/lib/prompt-access.js +22 -0
- package/lib/prompt-kinds/answer-limits.js +15 -0
- package/lib/prompt-kinds/ask-user.js +58 -0
- package/lib/prompt-kinds/codecs.js +97 -0
- package/lib/prompt-kinds/elicitation.js +77 -0
- package/lib/prompt-kinds/extension.js +39 -0
- package/lib/prompt-kinds/index.js +68 -0
- package/lib/prompt-kinds/permission.js +114 -0
- package/lib/prompt-kinds/plan.js +79 -0
- package/lib/prompt-kinds/tool-call.js +38 -0
- package/lib/prompt-registry.js +680 -0
- package/lib/public/app.js +1 -1
- package/lib/public/css/diagnostics.css +80 -0
- package/lib/public/css/input.css +17 -3
- package/lib/public/css/menus.css +10 -0
- package/lib/public/css/messages.css +5 -0
- package/lib/public/css/rewind.css +22 -0
- package/lib/public/index.html +4 -12
- package/lib/public/modules/agent-dispatch-tool.js +9 -0
- package/lib/public/modules/app-connection.js +7 -0
- package/lib/public/modules/app-cursors.js +7 -5
- package/lib/public/modules/app-messages.js +52 -47
- package/lib/public/modules/app-notifications.js +47 -71
- package/lib/public/modules/app-panels.js +61 -59
- package/lib/public/modules/app-projects.js +2 -1
- package/lib/public/modules/app-rendering.js +4 -10
- package/lib/public/modules/build-update-detail.js +54 -0
- package/lib/public/modules/diagnostics.js +170 -20
- package/lib/public/modules/filebrowser.js +18 -16
- package/lib/public/modules/history-visibility.js +4 -0
- package/lib/public/modules/mention.js +3 -2
- package/lib/public/modules/mode-options.js +38 -0
- package/lib/public/modules/project-settings.js +19 -8
- package/lib/public/modules/prompt-banner.js +78 -0
- package/lib/public/modules/prompt-card.js +201 -0
- package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
- package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
- package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
- package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
- package/lib/public/modules/prompt-kinds/index.js +18 -0
- package/lib/public/modules/prompt-kinds/own-key.js +29 -0
- package/lib/public/modules/prompt-kinds/package.json +3 -0
- package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
- package/lib/public/modules/prompt-kinds/permission.js +209 -0
- package/lib/public/modules/prompt-kinds/plan.js +102 -0
- package/lib/public/modules/prompt-state.js +142 -0
- package/lib/public/modules/prompts.js +134 -0
- package/lib/public/modules/rewind.js +4 -1
- package/lib/public/modules/server-settings.js +2 -4
- package/lib/public/modules/sidebar-mobile.js +19 -9
- package/lib/public/modules/sidebar-sessions.js +21 -14
- package/lib/public/modules/sticky-notes.js +3 -2
- package/lib/public/modules/team-panel.js +5 -4
- package/lib/public/modules/tools.js +81 -906
- package/lib/public/modules/vendor-picker.js +53 -0
- package/lib/public/modules/vendor-registry.js +142 -0
- package/lib/public/sw.js +3 -1
- package/lib/rc-migrate.js +44 -0
- package/lib/runtime-env-keys.js +109 -0
- package/lib/runtime-env.js +236 -0
- package/lib/sdk-bridge.js +348 -284
- package/lib/sdk-message-processor.js +117 -146
- package/lib/sdk-skill-discovery.js +65 -60
- package/lib/server-hub-sessions.js +13 -10
- package/lib/server-palette.js +28 -19
- package/lib/server.js +156 -115
- package/lib/service-install.js +96 -0
- package/lib/session-capabilities.js +76 -0
- package/lib/session-id.js +33 -0
- package/lib/session-input-ipc.js +32 -0
- package/lib/session-input.js +379 -0
- package/lib/session-reclaim.js +284 -0
- package/lib/session-resume.js +172 -0
- package/lib/sessions.js +61 -30
- package/lib/supervisor.js +39 -0
- package/lib/turn-initiator.js +108 -0
- package/lib/users-permissions.js +9 -6
- package/lib/users.js +9 -0
- package/lib/utils.js +4 -2
- package/lib/vendor-auth.js +22 -0
- package/lib/vendor-registry-message.js +66 -0
- package/lib/ws-origin.js +99 -0
- package/lib/ws-schema.js +36 -23
- package/lib/yoke/adapters/claude-capabilities.js +38 -0
- package/lib/yoke/adapters/claude-vendor.js +34 -0
- package/lib/yoke/adapters/claude-worker.js +57 -87
- package/lib/yoke/adapters/claude.js +182 -88
- package/lib/yoke/adapters/codex-capabilities.js +40 -0
- package/lib/yoke/adapters/codex-vendor.js +46 -0
- package/lib/yoke/adapters/codex.js +462 -211
- package/lib/yoke/adapters/effort-control.js +34 -0
- package/lib/yoke/adapters/permission-relay.js +58 -0
- package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
- package/lib/yoke/auth-checks.js +91 -0
- package/lib/yoke/capabilities.js +49 -0
- package/lib/yoke/codex-app-server.js +30 -56
- package/lib/yoke/codex-binary.js +254 -0
- package/lib/yoke/codex-command.js +122 -0
- package/lib/yoke/codex-protocol.js +96 -0
- package/lib/yoke/codex-version.js +135 -0
- package/lib/yoke/env-ownership.js +40 -0
- package/lib/yoke/index.js +84 -174
- package/lib/yoke/install-probe.js +136 -0
- package/lib/yoke/instructions.js +7 -11
- package/lib/yoke/interface.js +206 -4
- package/lib/yoke/package.json +1 -1
- package/lib/yoke/provider-credentials.js +96 -0
- package/lib/yoke/registry.js +377 -0
- package/package.json +12 -9
- package/scripts/check-sdk-lock-drift.js +160 -0
- package/scripts/check-test-count.js +76 -1
- package/scripts/postinstall.js +218 -136
- package/scripts/prompt-harness-history.js +202 -0
- package/scripts/verify-installed-build.js +59 -17
- package/scripts/verify-installed.js +67 -0
- package/lib/sdk-permission-ownership.js +0 -136
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// service-install.js — pure helpers for scripts/postinstall.js (systemd unit
|
|
2
|
+
// install). Kept free of side effects so each decision can be tested without
|
|
3
|
+
// touching /etc/systemd or /usr/local/bin.
|
|
4
|
+
|
|
5
|
+
var path = require("path");
|
|
6
|
+
|
|
7
|
+
var BIN_NAME = "clagentic-console";
|
|
8
|
+
var LEGACY_WRAPPER_PATH = "/usr/local/bin/clagentic-daemon.sh";
|
|
9
|
+
|
|
10
|
+
// Exact text of the hand-placed wrapper that earlier releases told operators to
|
|
11
|
+
// create. It is removed only when the file matches this byte for byte (modulo
|
|
12
|
+
// line endings and trailing whitespace); an operator-modified copy is theirs.
|
|
13
|
+
var LEGACY_WRAPPER_CONTENT = [
|
|
14
|
+
"#!/bin/bash",
|
|
15
|
+
"# Run Clagentic Console daemon directly so systemd can track the process",
|
|
16
|
+
"# Prefer global npm install, fall back to npx cache",
|
|
17
|
+
"",
|
|
18
|
+
"# Safety: refuse to start if a workspace symlink exists at the unscoped package name.",
|
|
19
|
+
"# npm link / npm install -g <workspace-path> creates exactly this trap — workspace edits",
|
|
20
|
+
"# become live in the daemon without going through pack/install. If this fires, remove the",
|
|
21
|
+
"# symlink with: rm /usr/lib/node_modules/clagentic-console",
|
|
22
|
+
"SYMLINK_TRAP=\"/usr/lib/node_modules/clagentic-console\"",
|
|
23
|
+
"if [ -L \"$SYMLINK_TRAP\" ]; then",
|
|
24
|
+
" echo \"ERROR: $SYMLINK_TRAP is a symlink (npm link trap). Remove it before starting:\" >&2",
|
|
25
|
+
" echo \" rm $SYMLINK_TRAP\" >&2",
|
|
26
|
+
" echo \"Then reinstall via: npm pack --pack-destination /tmp/ && npm install -g /tmp/clagentic-console-*.tgz\" >&2",
|
|
27
|
+
" exit 1",
|
|
28
|
+
"fi",
|
|
29
|
+
"",
|
|
30
|
+
"DAEMON_LIB=\"/usr/lib/node_modules/@clagentic/console/lib/daemon.js\"",
|
|
31
|
+
"if [ ! -f \"$DAEMON_LIB\" ]; then",
|
|
32
|
+
" DAEMON_LIB=$(node -e \"console.log(require.resolve('@clagentic/console/lib/daemon'))\" 2>/dev/null || \\",
|
|
33
|
+
" ls /root/.npm/_npx/*/node_modules/@clagentic/console/lib/daemon.js 2>/dev/null | tail -1)",
|
|
34
|
+
"fi",
|
|
35
|
+
"exec /usr/bin/node \"$DAEMON_LIB\"",
|
|
36
|
+
].join("\n");
|
|
37
|
+
|
|
38
|
+
function normalize(text) {
|
|
39
|
+
return String(text).replace(/\r\n/g, "\n").split("\n").map(function (l) { return l.replace(/[ \t]+$/, ""); }).join("\n").replace(/\n+$/, "");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isLegacyWrapperContent(content) {
|
|
43
|
+
return normalize(content) === normalize(LEGACY_WRAPPER_CONTENT);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* npm exposes its config to lifecycle scripts as npm_config_* variables.
|
|
48
|
+
* `npm install -g` sets npm_config_global=true (and npm_config_location=global
|
|
49
|
+
* on npm 8+); a local `npm ci` / `npm install` sets neither.
|
|
50
|
+
*/
|
|
51
|
+
function isGlobalInstall(env) {
|
|
52
|
+
return env.npm_config_global === "true" || env.npm_config_location === "global";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Absolute path of the globally installed bin symlink, or null when it cannot
|
|
57
|
+
* be determined. Prefers npm's own prefix; otherwise derives it from the
|
|
58
|
+
* package location <prefix>/lib/node_modules/@clagentic/console.
|
|
59
|
+
*/
|
|
60
|
+
function resolveCliBin(opts) {
|
|
61
|
+
var prefix = opts.env.npm_config_prefix;
|
|
62
|
+
if (!prefix || !path.isAbsolute(prefix)) {
|
|
63
|
+
var scopeDir = path.dirname(opts.pkgDir);
|
|
64
|
+
if (path.basename(scopeDir) !== "@clagentic") return null;
|
|
65
|
+
var modulesDir = path.dirname(scopeDir);
|
|
66
|
+
if (path.basename(modulesDir) !== "node_modules") return null;
|
|
67
|
+
prefix = path.dirname(path.dirname(modulesDir));
|
|
68
|
+
}
|
|
69
|
+
return path.join(prefix, "bin", BIN_NAME);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var BIN_PLACEHOLDER = "@CLAGENTIC_CONSOLE_BIN@";
|
|
73
|
+
var BIN_DIR_PLACEHOLDER = "@CLAGENTIC_CONSOLE_BIN_DIR@";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Fill the shipped unit template. Throws if a placeholder is left behind, so a
|
|
77
|
+
* half-rendered unit is never written to /etc/systemd.
|
|
78
|
+
*/
|
|
79
|
+
function renderUnit(template, cliBin) {
|
|
80
|
+
var out = template
|
|
81
|
+
.split(BIN_PLACEHOLDER).join(cliBin)
|
|
82
|
+
.split(BIN_DIR_PLACEHOLDER).join(path.dirname(cliBin));
|
|
83
|
+
var left = out.match(/@CLAGENTIC_CONSOLE_[A-Z_]+@/);
|
|
84
|
+
if (left) throw new Error("unit template has unfilled placeholder " + left[0]);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = {
|
|
89
|
+
BIN_NAME: BIN_NAME,
|
|
90
|
+
LEGACY_WRAPPER_PATH: LEGACY_WRAPPER_PATH,
|
|
91
|
+
LEGACY_WRAPPER_CONTENT: LEGACY_WRAPPER_CONTENT,
|
|
92
|
+
isLegacyWrapperContent: isLegacyWrapperContent,
|
|
93
|
+
isGlobalInstall: isGlobalInstall,
|
|
94
|
+
resolveCliBin: resolveCliBin,
|
|
95
|
+
renderUnit: renderUnit,
|
|
96
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// session-capabilities.js - what each agent can do in this project right now,
|
|
2
|
+
// kept on the session manager as sm.capabilitiesByVendor.
|
|
3
|
+
//
|
|
4
|
+
// The value is always a RESOLVED set (yoke/capabilities.js): the agent's
|
|
5
|
+
// declaration, overlaid by a connection, narrowed by what its live session
|
|
6
|
+
// advertised. Every session-facing runtime decision reads it through
|
|
7
|
+
// capabilitiesFor and nothing else, so no site can act on a declaration the live
|
|
8
|
+
// agent has already withdrawn. Declarations enter only here, through
|
|
9
|
+
// resolveCapabilities, never read directly.
|
|
10
|
+
|
|
11
|
+
var yoke = require("./yoke");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The vendor a session's decisions are made for: the session's own, else the
|
|
15
|
+
* adapter that serves it, else the project default.
|
|
16
|
+
* @param {object} sm
|
|
17
|
+
* @param {?{vendor: ?string}} session
|
|
18
|
+
* @param {?string} [adapterVendor]
|
|
19
|
+
* @returns {?string}
|
|
20
|
+
*/
|
|
21
|
+
function effectiveVendorId(sm, session, adapterVendor) {
|
|
22
|
+
return (session && session.vendor) || adapterVendor || (sm && sm.defaultVendor) || null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The resolved capabilities recorded for a vendor, as a copy. A vendor with
|
|
27
|
+
* nothing recorded (or not registered) has none: every flag off, every list
|
|
28
|
+
* empty, so a control gated on it stays disabled and a guard fails closed.
|
|
29
|
+
* @param {object} sm
|
|
30
|
+
* @param {?string} vendorId
|
|
31
|
+
* @returns {object} every ledger key
|
|
32
|
+
*/
|
|
33
|
+
function capabilitiesFor(sm, vendorId) {
|
|
34
|
+
var recorded = sm && sm.capabilitiesByVendor;
|
|
35
|
+
var has = typeof vendorId === "string" && recorded
|
|
36
|
+
&& Object.prototype.hasOwnProperty.call(recorded, vendorId);
|
|
37
|
+
return yoke.completeCapabilities(has ? recorded[vendorId] : {});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Records the resolved capabilities of one vendor from what its live session
|
|
42
|
+
* advertised (the capabilities member of init()'s result; none when the
|
|
43
|
+
* adapter reported none).
|
|
44
|
+
* @param {object} sm
|
|
45
|
+
* @param {string} vendorId
|
|
46
|
+
* @param {?object} advertised
|
|
47
|
+
* @returns {object} what was recorded
|
|
48
|
+
*/
|
|
49
|
+
function recordCapabilities(sm, vendorId, advertised) {
|
|
50
|
+
sm.capabilitiesByVendor = sm.capabilitiesByVendor || {};
|
|
51
|
+
var resolved = yoke.registry.isKnown(vendorId)
|
|
52
|
+
? yoke.resolveCapabilities(yoke.registry.get(vendorId), null, advertised || null)
|
|
53
|
+
: (advertised || {});
|
|
54
|
+
sm.capabilitiesByVendor[vendorId] = resolved;
|
|
55
|
+
return resolved;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Gives every registered vendor an entry before any session has reported, so a
|
|
60
|
+
* session on a vendor other than the project default is gated on what its own
|
|
61
|
+
* agent can do. An entry already recorded from a live session is kept.
|
|
62
|
+
* @param {object} sm
|
|
63
|
+
*/
|
|
64
|
+
function seedCapabilities(sm) {
|
|
65
|
+
sm.capabilitiesByVendor = sm.capabilitiesByVendor || {};
|
|
66
|
+
yoke.registry.list().forEach(function (vendor) {
|
|
67
|
+
if (!sm.capabilitiesByVendor[vendor.id]) recordCapabilities(sm, vendor.id, null);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = {
|
|
72
|
+
effectiveVendorId: effectiveVendorId,
|
|
73
|
+
capabilitiesFor: capabilitiesFor,
|
|
74
|
+
recordCapabilities: recordCapabilities,
|
|
75
|
+
seedCapabilities: seedCapabilities,
|
|
76
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// session-id.js - the one place a session id is checked before it becomes part
|
|
2
|
+
// of a file path. An id arrives from outside (a browser message, an injected
|
|
3
|
+
// input), so it is validated before any filesystem use and every path built from
|
|
4
|
+
// it is checked to stay inside the directory it is meant for.
|
|
5
|
+
|
|
6
|
+
var path = require("path");
|
|
7
|
+
|
|
8
|
+
// Letters, digits, "_" and "-", not starting with a separator-like character.
|
|
9
|
+
var SAFE_SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
10
|
+
|
|
11
|
+
function isSafeSessionId(id) {
|
|
12
|
+
return typeof id === "string" && SAFE_SESSION_ID.test(id);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Returns <dir>/<id><ext>, throwing when the id is unsafe or the resolved path
|
|
16
|
+
// is not inside dir. The containment check is separate from the allowlist so a
|
|
17
|
+
// later loosening of the allowlist cannot reopen traversal.
|
|
18
|
+
function sessionPathWithin(dir, id, ext) {
|
|
19
|
+
if (!isSafeSessionId(id)) {
|
|
20
|
+
throw new Error("unsafe session id " + JSON.stringify(String(id).slice(0, 64)));
|
|
21
|
+
}
|
|
22
|
+
var root = path.resolve(dir);
|
|
23
|
+
var full = path.resolve(root, id + ext);
|
|
24
|
+
if (full.indexOf(root + path.sep) !== 0) {
|
|
25
|
+
throw new Error("session path escapes its directory: " + JSON.stringify(String(id).slice(0, 64)));
|
|
26
|
+
}
|
|
27
|
+
return full;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
isSafeSessionId: isSafeSessionId,
|
|
32
|
+
sessionPathWithin: sessionPathWithin,
|
|
33
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// session-input-ipc.js - the daemon IPC side of open-session and send-input:
|
|
2
|
+
// which fields of an IPC message reach lib/session-input.js. Kept out of
|
|
3
|
+
// daemon.js so the handler a CLI call reaches can be exercised without booting
|
|
4
|
+
// the daemon.
|
|
5
|
+
|
|
6
|
+
// Handles one IPC message with a session-input instance. Returns the result
|
|
7
|
+
// (or a Promise of it), or undefined when msg.cmd is not a session command.
|
|
8
|
+
function handleSessionCommand(sessionInput, msg) {
|
|
9
|
+
if (msg.cmd === "open_session") {
|
|
10
|
+
return sessionInput.openSession({
|
|
11
|
+
projectSlug: msg.projectSlug,
|
|
12
|
+
prompt: msg.prompt,
|
|
13
|
+
contextNote: msg.contextNote,
|
|
14
|
+
vendor: msg.vendor,
|
|
15
|
+
agentName: msg.agentName,
|
|
16
|
+
sessionVisibility: msg.sessionVisibility,
|
|
17
|
+
initiator: msg.initiator,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if (msg.cmd === "send_session_input") {
|
|
21
|
+
return sessionInput.sendInput({
|
|
22
|
+
projectSlug: msg.projectSlug,
|
|
23
|
+
sessionId: msg.sessionId,
|
|
24
|
+
prompt: msg.prompt,
|
|
25
|
+
contextNote: msg.contextNote,
|
|
26
|
+
initiator: msg.initiator,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = { handleSessionCommand: handleSessionCommand };
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
// session-input.js - open a session, or send input to one, on behalf of a
|
|
2
|
+
// caller that is not the authenticated browser: an external-trigger file
|
|
3
|
+
// (project-external-trigger.js) or the daemon IPC verbs (lib/daemon.js).
|
|
4
|
+
// One implementation behind both, so the same request gets the same result.
|
|
5
|
+
//
|
|
6
|
+
// Any caller, any session kind: the vendor and agent come from the request on
|
|
7
|
+
// open, and from the stored session on send. Input never counts as a human
|
|
8
|
+
// turn: an initiator of "human" (or anything unknown) is refused, and an absent
|
|
9
|
+
// one is "trigger". Human turns come only from the browser's WebSocket path.
|
|
10
|
+
//
|
|
11
|
+
// Security is unchanged from the external trigger: the callers that reach this
|
|
12
|
+
// are the OS user that owns the config directory (file drop; daemon.sock is
|
|
13
|
+
// chmod 0700). Routing per user in multi-user mode remains future work.
|
|
14
|
+
|
|
15
|
+
var { INITIATORS } = require("./turn-initiator");
|
|
16
|
+
var { isKnownVendor } = require("./yoke");
|
|
17
|
+
var { getNewWorkRefusal } = require("./new-work-gate");
|
|
18
|
+
var agentsModule = require("./agents");
|
|
19
|
+
var sessionResume = require("./session-resume");
|
|
20
|
+
|
|
21
|
+
// The result vocabulary reported to the caller. "error" is not a verdict on
|
|
22
|
+
// the request: delivery was attempted and failed, so a caller may retry.
|
|
23
|
+
var RESULT = Object.freeze({
|
|
24
|
+
DELIVERED: "delivered",
|
|
25
|
+
RESUMED: "resumed-and-delivered",
|
|
26
|
+
OPENED: "opened",
|
|
27
|
+
NOT_FOUND: "not-found",
|
|
28
|
+
REFUSED: "refused",
|
|
29
|
+
ERROR: "error",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Initiators injected input may name. HUMAN is deliberately absent.
|
|
33
|
+
var ACCEPTED_INITIATORS = Object.freeze([
|
|
34
|
+
INITIATORS.TRIGGER,
|
|
35
|
+
INITIATORS.SCHEDULED,
|
|
36
|
+
INITIATORS.SCHEDULED_NOW,
|
|
37
|
+
INITIATORS.LOOP,
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
var VISIBILITIES = Object.freeze(["shared", "private"]);
|
|
41
|
+
|
|
42
|
+
function shown(value) {
|
|
43
|
+
return JSON.stringify(String(value).slice(0, 64));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function refused(reason) {
|
|
47
|
+
return { ok: false, result: RESULT.REFUSED, reason: reason };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The daemon is shedding new work (memory pressure). Not a verdict on the
|
|
51
|
+
// request, so a file-drop caller keeps its trigger and tries again later.
|
|
52
|
+
function refusedForLoad(reason) {
|
|
53
|
+
return { ok: false, result: RESULT.REFUSED, reason: reason, retryable: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Returns { initiator } or { refusal }.
|
|
57
|
+
function resolveInitiator(raw) {
|
|
58
|
+
if (raw === undefined || raw === null) return { initiator: INITIATORS.TRIGGER };
|
|
59
|
+
if (typeof raw === "string" && ACCEPTED_INITIATORS.indexOf(raw) !== -1) return { initiator: raw };
|
|
60
|
+
var why = raw === INITIATORS.HUMAN
|
|
61
|
+
? "human turns come only from the authenticated browser"
|
|
62
|
+
: "unknown initiator";
|
|
63
|
+
return {
|
|
64
|
+
refusal: refused("initiator " + shown(raw) + " refused: " + why + " (accepted: " + ACCEPTED_INITIATORS.join(", ") + ")"),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A session in memory is addressed by the id its vendor assigned or by the
|
|
69
|
+
// console's own session id (what open-session returns, always present).
|
|
70
|
+
function findSession(sm, id) {
|
|
71
|
+
var byVendorId = null;
|
|
72
|
+
var byConsoleId = null;
|
|
73
|
+
sm.sessions.forEach(function (s) {
|
|
74
|
+
if (!byVendorId && s.cliSessionId === id) byVendorId = s;
|
|
75
|
+
if (!byConsoleId && String(s.localId) === id) byConsoleId = s;
|
|
76
|
+
});
|
|
77
|
+
return byVendorId || byConsoleId;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function messageOf(err) {
|
|
81
|
+
return err && err.message ? err.message : String(err);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The query did not start, so the input was not delivered and sending it again
|
|
85
|
+
// cannot deliver it twice.
|
|
86
|
+
function sendStartFailure(project, session, label, err) {
|
|
87
|
+
session.isProcessing = false;
|
|
88
|
+
project.onProcessingChanged();
|
|
89
|
+
console.error("[session-input] turn start failed for " + label + ":", messageOf(err));
|
|
90
|
+
return { ok: false, result: RESULT.ERROR, retryable: true, reason: "turn start failed: " + messageOf(err) };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A session this request created whose first turn did not start is removed, so
|
|
94
|
+
// a failed open leaves nothing behind, and is not retried: the caller opens
|
|
95
|
+
// again if it wants to.
|
|
96
|
+
function openStartFailure(project, session, label, err) {
|
|
97
|
+
session.isProcessing = false;
|
|
98
|
+
project.onProcessingChanged();
|
|
99
|
+
console.error("[session-input] open failed for " + label + ":", messageOf(err));
|
|
100
|
+
try { project.sm.deleteSession(session.localId); }
|
|
101
|
+
catch (e) { console.error("[session-input] could not remove " + label + ":", messageOf(e)); }
|
|
102
|
+
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "open failed: " + messageOf(err) };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Starts the session's query and answers when it has actually started: a
|
|
106
|
+
// rejection (or a synchronous throw) is a failed delivery, never a delivered
|
|
107
|
+
// one. Returns the value of onStarted(), or a Promise of it when the start is
|
|
108
|
+
// asynchronous. onFailed(project, session, label, err) makes the failure result.
|
|
109
|
+
function startThen(project, session, label, start, onStarted, onFailed) {
|
|
110
|
+
var started;
|
|
111
|
+
try { started = start(); }
|
|
112
|
+
catch (e) { return onFailed(project, session, label, e); }
|
|
113
|
+
if (started && typeof started.then === "function") {
|
|
114
|
+
return started.then(onStarted, function (err) { return onFailed(project, session, label, err); });
|
|
115
|
+
}
|
|
116
|
+
return onStarted();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A caller on the daemon's IPC or the trigger watcher must always get a
|
|
120
|
+
// result, so an unexpected throw is reported as an error, not propagated.
|
|
121
|
+
function neverThrows(fn) {
|
|
122
|
+
return function (req) {
|
|
123
|
+
try { return fn(req); }
|
|
124
|
+
catch (e) {
|
|
125
|
+
console.error("[session-input] unexpected failure:", messageOf(e));
|
|
126
|
+
return { ok: false, result: RESULT.ERROR, reason: "unexpected failure: " + messageOf(e) };
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// deps:
|
|
132
|
+
// getProject(slug) -> project context | null
|
|
133
|
+
// agentCatalog(project) -> [{ name }] named agents on offer (default: the picker's catalog)
|
|
134
|
+
function createSessionInput(deps) {
|
|
135
|
+
var getProject = deps.getProject;
|
|
136
|
+
|
|
137
|
+
function defaultAgentCatalog(project) {
|
|
138
|
+
// Neither lookup throws: both answer [] when discovery has not run or the
|
|
139
|
+
// directory is absent.
|
|
140
|
+
return agentsModule.getAll().concat(agentsModule.readProjectAgents(project.cwd));
|
|
141
|
+
}
|
|
142
|
+
var agentCatalog = deps.agentCatalog || defaultAgentCatalog;
|
|
143
|
+
|
|
144
|
+
function resolveProject(slug) {
|
|
145
|
+
var project = typeof slug === "string" && slug ? getProject(slug) : null;
|
|
146
|
+
if (!project) return { refusal: refused("unknown project " + shown(slug)) };
|
|
147
|
+
if (!project.sm || !project.sdk) {
|
|
148
|
+
return { refusal: { ok: false, result: RESULT.ERROR, reason: "project " + shown(slug) + " is not ready" } };
|
|
149
|
+
}
|
|
150
|
+
return { project: project };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function checkPrompt(prompt) {
|
|
154
|
+
return typeof prompt === "string" && prompt ? null : refused("prompt must be a non-empty string");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// History entry that marks the turn as injected, not typed. The input is
|
|
158
|
+
// already delivered when this runs, so a failure to record is logged and does
|
|
159
|
+
// not turn a delivered turn into an error a caller would retry.
|
|
160
|
+
function recordInjected(project, session, req, initiator) {
|
|
161
|
+
var entry = {
|
|
162
|
+
type: "user_message",
|
|
163
|
+
text: req.prompt,
|
|
164
|
+
source: "external-trigger",
|
|
165
|
+
initiator: initiator,
|
|
166
|
+
};
|
|
167
|
+
if (req.triggerId) entry.triggerId = req.triggerId;
|
|
168
|
+
if (req.contextNote) entry.contextNote = req.contextNote;
|
|
169
|
+
try {
|
|
170
|
+
project.sm.recordHistoryEntry(session, entry, true);
|
|
171
|
+
project.sm.appendToSessionFile(session, entry);
|
|
172
|
+
} catch (e) {
|
|
173
|
+
console.error("[session-input] failed to record injected message:", e.message || e);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function describe(project, session) {
|
|
178
|
+
return {
|
|
179
|
+
consoleSessionId: String(session.localId),
|
|
180
|
+
sessionId: session.cliSessionId || null,
|
|
181
|
+
localId: session.localId,
|
|
182
|
+
vendor: session.vendor || project.sm.defaultVendor || null,
|
|
183
|
+
agent: session.agentName || null,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Delivers req.prompt as the session's next turn: into the running query,
|
|
188
|
+
// or by starting one (the session's runtime was ended) on the session's own
|
|
189
|
+
// vendor and agent.
|
|
190
|
+
function deliver(project, session, req, initiator, resumed) {
|
|
191
|
+
var sdk = project.sdk;
|
|
192
|
+
var processing = !!session.isProcessing;
|
|
193
|
+
var needsStart = !processing && !session.queryInstance && (!session.worker || session.messageQueue !== "worker");
|
|
194
|
+
|
|
195
|
+
if (needsStart) {
|
|
196
|
+
var refusal = getNewWorkRefusal();
|
|
197
|
+
if (refusal) return refusedForLoad(refusal);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function delivered() {
|
|
201
|
+
recordInjected(project, session, req, initiator);
|
|
202
|
+
var out = describe(project, session);
|
|
203
|
+
out.ok = true;
|
|
204
|
+
out.result = (resumed || needsStart) ? RESULT.RESUMED : RESULT.DELIVERED;
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function pushFailure(e) {
|
|
209
|
+
console.error("[session-input] delivery failed:", messageOf(e));
|
|
210
|
+
return { ok: false, result: RESULT.ERROR, reason: "delivery failed: " + messageOf(e) };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (processing) {
|
|
214
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
215
|
+
catch (e) { return pushFailure(e); }
|
|
216
|
+
return delivered();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
session.isProcessing = true;
|
|
220
|
+
project.onProcessingChanged();
|
|
221
|
+
session.sentToolResults = {};
|
|
222
|
+
if (needsStart) {
|
|
223
|
+
return startThen(project, session, "session " + String(session.cliSessionId), function () {
|
|
224
|
+
return sdk.startQuery(session, req.prompt, undefined, project.getLinuxUserForSession(session), initiator);
|
|
225
|
+
}, delivered, sendStartFailure);
|
|
226
|
+
}
|
|
227
|
+
try { sdk.pushMessage(session, req.prompt, null, initiator); }
|
|
228
|
+
catch (e) {
|
|
229
|
+
session.isProcessing = false;
|
|
230
|
+
project.onProcessingChanged();
|
|
231
|
+
return pushFailure(e);
|
|
232
|
+
}
|
|
233
|
+
return delivered();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// req: { projectSlug, sessionId, prompt, initiator?, contextNote?, triggerId? }
|
|
237
|
+
// Returns the result, or a Promise of it when the session must be read from
|
|
238
|
+
// the store first (see settle()).
|
|
239
|
+
function sendInput(req) {
|
|
240
|
+
req = req || {};
|
|
241
|
+
var init = resolveInitiator(req.initiator);
|
|
242
|
+
if (init.refusal) return init.refusal;
|
|
243
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
244
|
+
if (promptRefusal) return promptRefusal;
|
|
245
|
+
if (typeof req.sessionId !== "string" || !req.sessionId) {
|
|
246
|
+
return refused("sessionId must be a non-empty string");
|
|
247
|
+
}
|
|
248
|
+
// An id names files in the store; one that is not a plain id is refused, not
|
|
249
|
+
// looked up and not reported as absent.
|
|
250
|
+
if (!sessionResume.isSafeSessionId(req.sessionId)) {
|
|
251
|
+
return refused("unsafe session id " + shown(req.sessionId) + " refused");
|
|
252
|
+
}
|
|
253
|
+
var resolved = resolveProject(req.projectSlug);
|
|
254
|
+
if (resolved.refusal) return resolved.refusal;
|
|
255
|
+
var project = resolved.project;
|
|
256
|
+
|
|
257
|
+
var live = findSession(project.sm, req.sessionId);
|
|
258
|
+
if (live) return deliver(project, live, req, init.initiator, false);
|
|
259
|
+
|
|
260
|
+
// Only the console's own store is asked: a session started outside the
|
|
261
|
+
// console is not addressable, and its absence here is positive.
|
|
262
|
+
var storeDeps = { sm: project.sm, cwd: project.cwd };
|
|
263
|
+
function notFound() {
|
|
264
|
+
return { ok: false, result: RESULT.NOT_FOUND, reason: "no session " + shown(req.sessionId) + " in project " + shown(req.projectSlug) };
|
|
265
|
+
}
|
|
266
|
+
return sessionResume.sessionExistsInStore(project.sm, req.sessionId).then(function (exists) {
|
|
267
|
+
if (!exists) return notFound();
|
|
268
|
+
var gate = getNewWorkRefusal();
|
|
269
|
+
if (gate) return refusedForLoad(gate);
|
|
270
|
+
// activate:false: nobody asked to look at this session.
|
|
271
|
+
return sessionResume.resumeStoredSession(storeDeps, req.sessionId, { activate: false }).then(function (session) {
|
|
272
|
+
// The session was found in the store, so a manager that declines to
|
|
273
|
+
// bring it back is a failure to resume, not an absence.
|
|
274
|
+
if (!session) {
|
|
275
|
+
return { ok: false, result: RESULT.ERROR, retryable: false, reason: "session " + shown(req.sessionId) + " exists but could not be resumed" };
|
|
276
|
+
}
|
|
277
|
+
return deliver(project, session, req, init.initiator, true);
|
|
278
|
+
});
|
|
279
|
+
}).catch(function (err) {
|
|
280
|
+
// Lookup and resume failures carry whether trying again can help
|
|
281
|
+
// (session-resume.js); the input was not delivered either way.
|
|
282
|
+
console.error("[session-input] send failed:", messageOf(err));
|
|
283
|
+
return { ok: false, result: RESULT.ERROR, retryable: !!(err && err.retryable), reason: "send failed: " + messageOf(err) };
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Checks an open request's session kind; returns { options } for
|
|
288
|
+
// sm.createSession (the options the browser's new_session passes) or { refusal }.
|
|
289
|
+
function resolveSessionKind(project, req) {
|
|
290
|
+
var options = {};
|
|
291
|
+
if (req.vendor !== undefined && req.vendor !== null) {
|
|
292
|
+
if (typeof req.vendor !== "string" || !isKnownVendor(req.vendor)) {
|
|
293
|
+
return { refusal: refused("unknown vendor " + shown(req.vendor)) };
|
|
294
|
+
}
|
|
295
|
+
options.vendor = req.vendor;
|
|
296
|
+
}
|
|
297
|
+
if (req.agentName !== undefined && req.agentName !== null) {
|
|
298
|
+
var known = typeof req.agentName === "string" && agentCatalog(project).some(function (a) {
|
|
299
|
+
return a && a.name === req.agentName;
|
|
300
|
+
});
|
|
301
|
+
if (!known) return { refusal: refused("unknown agent " + shown(req.agentName)) };
|
|
302
|
+
options.agentName = req.agentName;
|
|
303
|
+
}
|
|
304
|
+
if (req.sessionVisibility !== undefined && req.sessionVisibility !== null) {
|
|
305
|
+
if (VISIBILITIES.indexOf(req.sessionVisibility) === -1) {
|
|
306
|
+
return { refusal: refused("unknown sessionVisibility " + shown(req.sessionVisibility) + " (accepted: " + VISIBILITIES.join(", ") + ")") };
|
|
307
|
+
}
|
|
308
|
+
options.sessionVisibility = req.sessionVisibility;
|
|
309
|
+
}
|
|
310
|
+
return { options: options };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// req: { projectSlug, prompt, vendor?, agentName?, sessionVisibility?,
|
|
314
|
+
// initiator?, contextNote?, cwd?, triggerId? }
|
|
315
|
+
// Returns the result, or a Promise of it when the first turn starts
|
|
316
|
+
// asynchronously (see settle()).
|
|
317
|
+
function openSession(req) {
|
|
318
|
+
req = req || {};
|
|
319
|
+
var init = resolveInitiator(req.initiator);
|
|
320
|
+
if (init.refusal) return init.refusal;
|
|
321
|
+
var promptRefusal = checkPrompt(req.prompt);
|
|
322
|
+
if (promptRefusal) return promptRefusal;
|
|
323
|
+
var resolved = resolveProject(req.projectSlug);
|
|
324
|
+
if (resolved.refusal) return resolved.refusal;
|
|
325
|
+
var project = resolved.project;
|
|
326
|
+
var kind = resolveSessionKind(project, req);
|
|
327
|
+
if (kind.refusal) return kind.refusal;
|
|
328
|
+
var gate = getNewWorkRefusal();
|
|
329
|
+
if (gate) return refusedForLoad(gate);
|
|
330
|
+
|
|
331
|
+
var sm = project.sm;
|
|
332
|
+
var sess = sm.createSession(kind.options);
|
|
333
|
+
var label = req.triggerId ? " — " + String(req.triggerId).substring(0, 12) : "";
|
|
334
|
+
sess.title = (req.contextNote || "External trigger") + label;
|
|
335
|
+
if (req.cwd) sess.cwd = req.cwd;
|
|
336
|
+
sm.saveSessionFile(sess);
|
|
337
|
+
sm.broadcastSessionList();
|
|
338
|
+
|
|
339
|
+
sess.isProcessing = true;
|
|
340
|
+
project.onProcessingChanged();
|
|
341
|
+
sess.sentToolResults = {};
|
|
342
|
+
// The session stays open for follow-up from a person.
|
|
343
|
+
sess.acceptEditsAfterStart = true;
|
|
344
|
+
|
|
345
|
+
function opened() {
|
|
346
|
+
var out = describe(project, sess);
|
|
347
|
+
out.ok = true;
|
|
348
|
+
out.result = RESULT.OPENED;
|
|
349
|
+
return out;
|
|
350
|
+
}
|
|
351
|
+
return startThen(project, sess, "local session " + sess.localId, function () {
|
|
352
|
+
return project.sdk.startQuery(sess, req.prompt, undefined, project.getLinuxUserForSession(sess), init.initiator);
|
|
353
|
+
}, function () {
|
|
354
|
+
// Recorded once, only now that the query has started: a rejected start
|
|
355
|
+
// leaves no history entry for a prompt that never ran.
|
|
356
|
+
recordInjected(project, sess, req, init.initiator);
|
|
357
|
+
console.log("[session-input] session opened: project=" + req.projectSlug + " session=" + sess.localId);
|
|
358
|
+
return opened();
|
|
359
|
+
}, openStartFailure);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return { openSession: neverThrows(openSession), sendInput: neverThrows(sendInput) };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// sendInput and openSession answer synchronously when nothing has to wait and
|
|
366
|
+
// with a Promise otherwise, so a caller that can act at once (the trigger
|
|
367
|
+
// watcher on a live session) does. settle() runs fn on either.
|
|
368
|
+
function settle(valueOrPromise, fn) {
|
|
369
|
+
if (valueOrPromise && typeof valueOrPromise.then === "function") return valueOrPromise.then(fn);
|
|
370
|
+
return fn(valueOrPromise);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
module.exports = {
|
|
374
|
+
createSessionInput: createSessionInput,
|
|
375
|
+
settle: settle,
|
|
376
|
+
resolveInitiator: resolveInitiator,
|
|
377
|
+
RESULT: RESULT,
|
|
378
|
+
ACCEPTED_INITIATORS: ACCEPTED_INITIATORS,
|
|
379
|
+
};
|