@clagentic/console 1.9.0 → 1.10.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/LICENSE +117 -12
  2. package/LICENSES/MIT-legacy.txt +22 -0
  3. package/LICENSES/README.md +15 -0
  4. package/README.md +9 -3
  5. package/bin/cli.js +40 -5
  6. package/deploy/clagentic-console.service +10 -6
  7. package/lib/agent-dispatch-tool.js +20 -0
  8. package/lib/audit.js +3 -2
  9. package/lib/build-changes.js +79 -0
  10. package/lib/build-identity.js +192 -0
  11. package/lib/build-monitor.js +125 -0
  12. package/lib/build-sha.json +2 -2
  13. package/lib/build-update-check.js +138 -0
  14. package/lib/build-update-offer.js +141 -0
  15. package/lib/build-update-restart.js +142 -0
  16. package/lib/build-update-standing.js +184 -0
  17. package/lib/cli/daemon-launch.js +25 -22
  18. package/lib/cli/ipc-subcommands.js +6 -0
  19. package/lib/cli/session-subcommands.js +122 -0
  20. package/lib/cli-sessions.js +8 -1
  21. package/lib/codex-defaults.js +8 -2
  22. package/lib/config.js +32 -20
  23. package/lib/daemon-env-settings.js +84 -0
  24. package/lib/daemon-projects.js +57 -0
  25. package/lib/daemon.js +288 -205
  26. package/lib/drain.js +74 -17
  27. package/lib/env-compat.js +61 -0
  28. package/lib/extension-commands.js +45 -0
  29. package/lib/history-visibility.js +4 -0
  30. package/lib/idle-adapter-reaper.js +71 -0
  31. package/lib/link-trap.js +78 -0
  32. package/lib/mcp-local.js +8 -6
  33. package/lib/mcp-tool-input.js +116 -0
  34. package/lib/mcp-tool-schema.js +179 -0
  35. package/lib/memory-limits.js +61 -5
  36. package/lib/memory-setting-validate.js +65 -0
  37. package/lib/memory-shed.js +13 -32
  38. package/lib/new-work-gate.js +47 -0
  39. package/lib/os-users.js +5 -3
  40. package/lib/project-access.js +139 -0
  41. package/lib/project-connection.js +37 -22
  42. package/lib/project-external-trigger.js +166 -105
  43. package/lib/project-file-scope.js +172 -0
  44. package/lib/project-file-watch.js +86 -61
  45. package/lib/project-filesystem.js +144 -128
  46. package/lib/project-git.js +119 -0
  47. package/lib/project-http.js +44 -28
  48. package/lib/project-loop.js +17 -9
  49. package/lib/project-mcp.js +53 -72
  50. package/lib/project-message-gate.js +70 -0
  51. package/lib/project-notifications.js +31 -4
  52. package/lib/project-prompt-responses.js +217 -0
  53. package/lib/project-sessions.js +192 -355
  54. package/lib/project-user-message.js +27 -15
  55. package/lib/project.js +212 -147
  56. package/lib/projects-dir.js +90 -0
  57. package/lib/prompt-access.js +22 -0
  58. package/lib/prompt-kinds/answer-limits.js +15 -0
  59. package/lib/prompt-kinds/ask-user.js +58 -0
  60. package/lib/prompt-kinds/codecs.js +97 -0
  61. package/lib/prompt-kinds/elicitation.js +77 -0
  62. package/lib/prompt-kinds/extension.js +39 -0
  63. package/lib/prompt-kinds/index.js +68 -0
  64. package/lib/prompt-kinds/permission.js +114 -0
  65. package/lib/prompt-kinds/plan.js +79 -0
  66. package/lib/prompt-kinds/tool-call.js +38 -0
  67. package/lib/prompt-registry.js +680 -0
  68. package/lib/public/app.js +1 -1
  69. package/lib/public/css/diagnostics.css +80 -0
  70. package/lib/public/css/input.css +17 -3
  71. package/lib/public/css/menus.css +10 -0
  72. package/lib/public/css/messages.css +5 -0
  73. package/lib/public/css/rewind.css +22 -0
  74. package/lib/public/index.html +4 -12
  75. package/lib/public/modules/agent-dispatch-tool.js +9 -0
  76. package/lib/public/modules/app-connection.js +7 -0
  77. package/lib/public/modules/app-cursors.js +7 -5
  78. package/lib/public/modules/app-messages.js +52 -47
  79. package/lib/public/modules/app-notifications.js +47 -71
  80. package/lib/public/modules/app-panels.js +61 -59
  81. package/lib/public/modules/app-projects.js +2 -1
  82. package/lib/public/modules/app-rendering.js +4 -10
  83. package/lib/public/modules/build-update-detail.js +54 -0
  84. package/lib/public/modules/diagnostics.js +170 -20
  85. package/lib/public/modules/filebrowser.js +18 -16
  86. package/lib/public/modules/history-visibility.js +4 -0
  87. package/lib/public/modules/mention.js +3 -2
  88. package/lib/public/modules/mode-options.js +38 -0
  89. package/lib/public/modules/project-settings.js +19 -8
  90. package/lib/public/modules/prompt-banner.js +78 -0
  91. package/lib/public/modules/prompt-card.js +201 -0
  92. package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
  93. package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
  94. package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
  95. package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
  96. package/lib/public/modules/prompt-kinds/index.js +18 -0
  97. package/lib/public/modules/prompt-kinds/own-key.js +29 -0
  98. package/lib/public/modules/prompt-kinds/package.json +3 -0
  99. package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
  100. package/lib/public/modules/prompt-kinds/permission.js +209 -0
  101. package/lib/public/modules/prompt-kinds/plan.js +102 -0
  102. package/lib/public/modules/prompt-state.js +142 -0
  103. package/lib/public/modules/prompts.js +134 -0
  104. package/lib/public/modules/rewind.js +4 -1
  105. package/lib/public/modules/server-settings.js +2 -4
  106. package/lib/public/modules/sidebar-mobile.js +19 -9
  107. package/lib/public/modules/sidebar-sessions.js +21 -14
  108. package/lib/public/modules/sticky-notes.js +3 -2
  109. package/lib/public/modules/team-panel.js +5 -4
  110. package/lib/public/modules/tools.js +81 -906
  111. package/lib/public/modules/vendor-picker.js +53 -0
  112. package/lib/public/modules/vendor-registry.js +142 -0
  113. package/lib/public/sw.js +3 -1
  114. package/lib/rc-migrate.js +44 -0
  115. package/lib/runtime-env-keys.js +109 -0
  116. package/lib/runtime-env.js +236 -0
  117. package/lib/sdk-bridge.js +348 -284
  118. package/lib/sdk-message-processor.js +117 -146
  119. package/lib/sdk-skill-discovery.js +65 -60
  120. package/lib/server-hub-sessions.js +13 -10
  121. package/lib/server-palette.js +28 -19
  122. package/lib/server.js +156 -115
  123. package/lib/service-install.js +96 -0
  124. package/lib/session-capabilities.js +76 -0
  125. package/lib/session-id.js +33 -0
  126. package/lib/session-input-ipc.js +32 -0
  127. package/lib/session-input.js +379 -0
  128. package/lib/session-reclaim.js +284 -0
  129. package/lib/session-resume.js +172 -0
  130. package/lib/sessions.js +61 -30
  131. package/lib/supervisor.js +39 -0
  132. package/lib/turn-initiator.js +108 -0
  133. package/lib/users-permissions.js +9 -6
  134. package/lib/users.js +9 -0
  135. package/lib/utils.js +4 -2
  136. package/lib/vendor-auth.js +22 -0
  137. package/lib/vendor-registry-message.js +66 -0
  138. package/lib/ws-origin.js +99 -0
  139. package/lib/ws-schema.js +36 -23
  140. package/lib/yoke/adapters/claude-capabilities.js +38 -0
  141. package/lib/yoke/adapters/claude-vendor.js +34 -0
  142. package/lib/yoke/adapters/claude-worker.js +57 -87
  143. package/lib/yoke/adapters/claude.js +182 -88
  144. package/lib/yoke/adapters/codex-capabilities.js +40 -0
  145. package/lib/yoke/adapters/codex-vendor.js +46 -0
  146. package/lib/yoke/adapters/codex.js +462 -211
  147. package/lib/yoke/adapters/effort-control.js +34 -0
  148. package/lib/yoke/adapters/permission-relay.js +58 -0
  149. package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
  150. package/lib/yoke/auth-checks.js +91 -0
  151. package/lib/yoke/capabilities.js +49 -0
  152. package/lib/yoke/codex-app-server.js +30 -56
  153. package/lib/yoke/codex-binary.js +254 -0
  154. package/lib/yoke/codex-command.js +122 -0
  155. package/lib/yoke/codex-protocol.js +96 -0
  156. package/lib/yoke/codex-version.js +135 -0
  157. package/lib/yoke/env-ownership.js +40 -0
  158. package/lib/yoke/index.js +84 -174
  159. package/lib/yoke/install-probe.js +136 -0
  160. package/lib/yoke/instructions.js +7 -11
  161. package/lib/yoke/interface.js +206 -4
  162. package/lib/yoke/package.json +1 -1
  163. package/lib/yoke/provider-credentials.js +96 -0
  164. package/lib/yoke/registry.js +377 -0
  165. package/package.json +12 -9
  166. package/scripts/check-sdk-lock-drift.js +160 -0
  167. package/scripts/check-test-count.js +76 -1
  168. package/scripts/postinstall.js +218 -136
  169. package/scripts/prompt-harness-history.js +202 -0
  170. package/scripts/verify-installed-build.js +59 -17
  171. package/scripts/verify-installed.js +67 -0
  172. package/lib/sdk-permission-ownership.js +0 -136
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ // Changing the reasoning effort of a Claude session that is already running.
4
+ // The SDK's Query has no setEffort; the live control is applyFlagSettings
5
+ // with an effortLevel, which takes effect for the rest of the session.
6
+ // 'max' is session-only and runs as 'high' on a model without it, which the
7
+ // SDK decides, so the console passes it through.
8
+ //
9
+ // Shared by the in-process query handle and the worker process, so the two
10
+ // paths cannot disagree about what a valid level is or what a failure is.
11
+
12
+ var EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"];
13
+
14
+ /**
15
+ * @param {object} query - the SDK Query.
16
+ * @param {string} effort - one of EFFORT_LEVELS.
17
+ * @returns {Promise<void>} rejects with a message fit to show the operator.
18
+ */
19
+ function applyEffort(query, effort) {
20
+ if (EFFORT_LEVELS.indexOf(effort) === -1) {
21
+ return Promise.reject(new Error("Unknown effort level: " + String(effort).slice(0, 40)));
22
+ }
23
+ if (!query || typeof query.applyFlagSettings !== "function") {
24
+ return Promise.reject(new Error("This Claude Code version cannot change effort in a running session"));
25
+ }
26
+ return Promise.resolve().then(function () {
27
+ return query.applyFlagSettings({ effortLevel: effort });
28
+ });
29
+ }
30
+
31
+ module.exports = {
32
+ EFFORT_LEVELS: EFFORT_LEVELS,
33
+ applyEffort: applyEffort,
34
+ };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ // The wire shape of a tool-permission ask between a Claude worker process and
4
+ // the daemon. The worker builds the message from the options the SDK handed
5
+ // its canUseTool callback; the daemon turns it back into the options its own
6
+ // canUseTool callback takes. Both sides use these two functions so a field the
7
+ // SDK adds to an ask cannot be carried by one and dropped by the other.
8
+ //
9
+ // The presentation hints travel with the ask: suppressAlwaysAllowRule (no
10
+ // persistent "don't ask again" choice), defaultToNo (the ask must not open on
11
+ // an approve choice) and matchedAskRule (a user-written ask rule forced the
12
+ // prompt, so no host-side auto-approval may answer it). Only a literal true
13
+ // restricts, and a rule is data, not a flag: anything present but not an object
14
+ // is still a rule-forced ask.
15
+
16
+ /**
17
+ * @param {string} requestId - the worker's id for this ask.
18
+ * @param {string} toolName
19
+ * @param {object} input
20
+ * @param {object} opts - the SDK's canUseTool options.
21
+ * @returns {object} the IPC message.
22
+ */
23
+ function toRequestMessage(requestId, toolName, input, opts) {
24
+ var msg = {
25
+ type: "permission_request",
26
+ requestId: requestId,
27
+ toolName: toolName,
28
+ input: input,
29
+ toolUseId: opts.toolUseID || "",
30
+ decisionReason: opts.decisionReason || "",
31
+ };
32
+ if (opts.suppressAlwaysAllowRule === true) msg.suppressAlwaysAllowRule = true;
33
+ if (opts.defaultToNo === true) msg.defaultToNo = true;
34
+ if (opts.matchedAskRule) msg.matchedAskRule = opts.matchedAskRule;
35
+ return msg;
36
+ }
37
+
38
+ /**
39
+ * @param {object} msg - a permission_request IPC message from a worker.
40
+ * @param {AbortSignal} signal - ends the ask when the worker stops waiting.
41
+ * @returns {object} canUseTool options for the daemon-side callback.
42
+ */
43
+ function toCanUseToolOptions(msg, signal) {
44
+ var opts = {
45
+ toolUseID: msg.toolUseId,
46
+ decisionReason: msg.decisionReason,
47
+ signal: signal,
48
+ };
49
+ if (msg.suppressAlwaysAllowRule === true) opts.suppressAlwaysAllowRule = true;
50
+ if (msg.defaultToNo === true) opts.defaultToNo = true;
51
+ if (msg.matchedAskRule) opts.matchedAskRule = typeof msg.matchedAskRule === "object" ? msg.matchedAskRule : {};
52
+ return opts;
53
+ }
54
+
55
+ module.exports = {
56
+ toRequestMessage: toRequestMessage,
57
+ toCanUseToolOptions: toCanUseToolOptions,
58
+ };
@@ -0,0 +1,81 @@
1
+ // worker-mcp-servers.js - rebuilds the in-process MCP servers inside the
2
+ // worker process from the JSON-schema descriptors the daemon sends over IPC
3
+ // (the SDK's server objects are not serializable). A tool whose schema cannot
4
+ // be read (mcp-tool-input.js) is left out and reported; the other tools on its
5
+ // server, and the other servers, are still registered.
6
+
7
+ var mcpToolSchema = require("../../mcp-tool-schema");
8
+ var { prepareTool } = require("../../mcp-tool-input");
9
+
10
+ // Throws when zod cannot be loaded; the caller reports the cause per tool.
11
+ function loadZod() {
12
+ var mod = require("zod");
13
+ return mod.z || mod;
14
+ }
15
+
16
+ /**
17
+ * @param {Array} descriptors [{ serverName, tools: [{ name, description, inputSchema }] }]
18
+ * @param {object} sdk the SDK module (tool, createSdkMcpServer)
19
+ * @param {object} hooks
20
+ * @param {function(string, string): function} hooks.createHandler (serverName, toolName) -> tool handler
21
+ * @param {function(object): void} [hooks.onSkipped] receives the diagnostic for a tool left out
22
+ * @param {function(): ?object} [hooks.loadZod] returns zod, or null/throws when it cannot be loaded; tests inject it
23
+ * @returns {?object} serverName -> server, or null when none was built
24
+ */
25
+ function buildMcpServersFromDescriptors(descriptors, sdk, hooks) {
26
+ if (!descriptors || !descriptors.length) return null;
27
+ var z = null;
28
+ var zodLoadError = "";
29
+ try {
30
+ z = (hooks.loadZod || loadZod)();
31
+ } catch (e) {
32
+ zodLoadError = ": " + ((e && e.message) || String(e));
33
+ }
34
+
35
+ var servers = {};
36
+ for (var i = 0; i < descriptors.length; i++) {
37
+ var descriptor = descriptors[i];
38
+ if (!descriptor || !descriptor.serverName || !descriptor.tools || !descriptor.tools.length) continue;
39
+ var tools = [];
40
+ for (var j = 0; j < descriptor.tools.length; j++) {
41
+ var toolDescriptor = descriptor.tools[j];
42
+ if (!toolDescriptor || !toolDescriptor.name) continue;
43
+ var prepared;
44
+ try {
45
+ if (!z) throw new Error("zod could not be loaded in the worker" + zodLoadError);
46
+ prepared = prepareTool(
47
+ z,
48
+ toolDescriptor.inputSchema,
49
+ hooks.createHandler(descriptor.serverName, toolDescriptor.name)
50
+ );
51
+ } catch (e) {
52
+ mcpToolSchema.reportSkippedTool(
53
+ descriptor.serverName,
54
+ toolDescriptor.name,
55
+ (e && e.message) || String(e),
56
+ hooks.onSkipped
57
+ );
58
+ continue;
59
+ }
60
+ tools.push(sdk.tool(
61
+ toolDescriptor.name,
62
+ toolDescriptor.description || toolDescriptor.name,
63
+ prepared.inputSchema,
64
+ prepared.handler
65
+ ));
66
+ }
67
+ if (tools.length > 0) {
68
+ servers[descriptor.serverName] = sdk.createSdkMcpServer({
69
+ name: descriptor.serverName,
70
+ version: "1.0.0",
71
+ tools: tools,
72
+ });
73
+ }
74
+ }
75
+
76
+ return Object.keys(servers).length > 0 ? servers : null;
77
+ }
78
+
79
+ module.exports = {
80
+ buildMcpServersFromDescriptors: buildMcpServersFromDescriptors,
81
+ };
@@ -0,0 +1,91 @@
1
+ // auth-checks.js - per-agent "is a login in place" probes. Each registered
2
+ // vendor points at one of these through its descriptor's checkAuth.
3
+
4
+ var execSync = require("child_process").execSync;
5
+ var codexCommand = require("./codex-command");
6
+ var providerCredentials = require("./provider-credentials");
7
+
8
+ function parseClaudeAuthStatusJson(out) {
9
+ if (!out) return null;
10
+ try {
11
+ return JSON.parse(out);
12
+ } catch (e) {
13
+ return null;
14
+ }
15
+ }
16
+
17
+ function isClaudeLoggedInText(out) {
18
+ if (!out) return false;
19
+ var text = String(out).toLowerCase();
20
+ if (text.indexOf("not logged in") !== -1) return false;
21
+ if (text.indexOf("login method:") !== -1) return true;
22
+ if (text.indexOf("logged in") !== -1) return true;
23
+ return false;
24
+ }
25
+
26
+ function hasThirdPartyProviderAuth(env) {
27
+ // Claude Code supports third-party providers via env vars. When these are set,
28
+ // `claude auth status` reports "not logged in" because there is no OAuth session,
29
+ // but Claude Code itself authenticates directly through the provider.
30
+ env = env || process.env;
31
+ var cloud = providerCredentials.completeProvider(env);
32
+ if (cloud) return cloud;
33
+ if (env.ANTHROPIC_API_KEY) return "api_key";
34
+ if (env.ANTHROPIC_AUTH_TOKEN) return "auth_token";
35
+ return null;
36
+ }
37
+
38
+ function checkClaudeAuth() {
39
+ var provider = hasThirdPartyProviderAuth();
40
+ if (provider) {
41
+ console.log("[yoke] Claude auth via third-party provider: " + provider);
42
+ return true;
43
+ }
44
+
45
+ try {
46
+ var out = execSync("claude auth status --json", { timeout: 5000, encoding: "utf8", stdio: ["pipe", "pipe", "pipe"] });
47
+ var parsed = parseClaudeAuthStatusJson(out);
48
+ if (parsed) return !!parsed.loggedIn;
49
+ console.warn("[yoke] Claude auth status JSON parse failed; falling back to text output");
50
+ } catch (e) {
51
+ var stdout = e && e.stdout ? String(e.stdout) : "";
52
+ if (stdout) {
53
+ var parsedFallback = parseClaudeAuthStatusJson(stdout);
54
+ if (parsedFallback) return !!parsedFallback.loggedIn;
55
+ }
56
+ console.warn("[yoke] Claude auth status JSON check failed; falling back to text output:", e.message);
57
+ }
58
+
59
+ try {
60
+ var textOut = execSync("claude auth status --text", { timeout: 5000, encoding: "utf8", stdio: ["pipe", "pipe", "pipe"] });
61
+ return isClaudeLoggedInText(textOut);
62
+ } catch (e) {
63
+ return false;
64
+ }
65
+ }
66
+
67
+ function checkCodexAuth() {
68
+ var resolved;
69
+ try {
70
+ resolved = require("./codex-binary").resolveCached();
71
+ } catch (e) {
72
+ // The lookup itself broke: a defect worth seeing, not "not installed".
73
+ console.error("[yoke] Codex binary lookup failed:", e && e.message ? e.message : e);
74
+ return false;
75
+ }
76
+ // A Codex that is absent, too old or an invalid override is reported by the
77
+ // install probe with its reason; there is nothing to sign in to here.
78
+ if (resolved.state !== "ready") return false;
79
+ try {
80
+ codexCommand.execCodexSync(resolved.path, ["login", "status"], { timeout: 5000, encoding: "utf8", stdio: ["pipe", "pipe", "pipe"] });
81
+ return true;
82
+ } catch (e) {
83
+ return false;
84
+ }
85
+ }
86
+
87
+ module.exports = {
88
+ hasThirdPartyProviderAuth: hasThirdPartyProviderAuth,
89
+ checkClaudeAuth: checkClaudeAuth,
90
+ checkCodexAuth: checkCodexAuth,
91
+ };
@@ -0,0 +1,49 @@
1
+ // capabilities.js - what one agent can do right now: the yoke's declaration,
2
+ // overlaid with a connection's overrides, narrowed by what the live session
3
+ // advertises. The result is what the UI gates controls on.
4
+
5
+ var iface = require("./interface");
6
+
7
+ function intersect(a, b) {
8
+ return a.filter(function (item) { return b.indexOf(item) !== -1; });
9
+ }
10
+
11
+ /**
12
+ * @param {{capabilities: object}} yoke a registered vendor descriptor; its
13
+ * declared capabilities are the starting point
14
+ * @param {?{capabilityOverrides: object}} connection optional. Overrides may
15
+ * widen or narrow the declaration: a connection knows which model sits
16
+ * behind the agent.
17
+ * @param {?object} sessionAdvertised optional. What the running session
18
+ * reports. It can only narrow: a flag it reports false turns the capability
19
+ * off, a list it reports restricts the entries to those it names, and a key
20
+ * it does not report is left as it was. Keys outside the ledger are ignored.
21
+ * @returns {object} every ledger key, flags as booleans, lists as arrays
22
+ */
23
+ function resolveCapabilities(yoke, connection, sessionAdvertised) {
24
+ if (!yoke || typeof yoke !== "object") {
25
+ throw new Error("[YOKE] resolveCapabilities needs a yoke declaration");
26
+ }
27
+ var out = iface.completeCapabilities(yoke.capabilities);
28
+
29
+ var overrides = connection && connection.capabilityOverrides;
30
+ if (overrides) {
31
+ iface.validateCapabilities(overrides, "capabilityOverrides");
32
+ Object.keys(overrides).forEach(function (key) {
33
+ out[key] = iface.CAPABILITIES[key].kind === "list" ? overrides[key].slice() : overrides[key];
34
+ });
35
+ }
36
+
37
+ if (sessionAdvertised) {
38
+ iface.validateCapabilities(sessionAdvertised, "the session's advertised", { ignoreUnknown: true });
39
+ Object.keys(sessionAdvertised).forEach(function (key) {
40
+ if (!iface.isCapabilityKey(key)) return;
41
+ if (iface.CAPABILITIES[key].kind === "list") out[key] = intersect(out[key], sessionAdvertised[key]);
42
+ else out[key] = out[key] && sessionAdvertised[key];
43
+ });
44
+ }
45
+
46
+ return out;
47
+ }
48
+
49
+ module.exports = { resolveCapabilities: resolveCapabilities };
@@ -2,57 +2,11 @@
2
2
  // ---------------------------------
3
3
  // Manages a codex app-server child process with bidirectional JSON-RPC
4
4
  // communication over stdin/stdout. Replaces the SDK exec mode to enable
5
- // interactive approval flows.
5
+ // interactive approval flows. The binary is the Codex the user installed
6
+ // (codex-binary.js resolves it); the console bundles none.
6
7
 
7
- var { spawn } = require("child_process");
8
8
  var readline = require("readline");
9
- var path = require("path");
10
- var { createRequire } = require("module");
11
-
12
- // --- Find the codex binary path ---
13
- // Mirrors the logic from @openai/codex-sdk findCodexPath()
14
-
15
- var PLATFORM_PACKAGE_BY_TARGET = {
16
- "aarch64-apple-darwin": "@openai/codex-darwin-arm64",
17
- "x86_64-apple-darwin": "@openai/codex-darwin-x64",
18
- "aarch64-unknown-linux-musl": "@openai/codex-linux-arm64",
19
- "x86_64-unknown-linux-musl": "@openai/codex-linux-x64",
20
- "x86_64-pc-windows-msvc": "@openai/codex-win32-x64",
21
- };
22
-
23
- function getTargetTriple() {
24
- var arch = process.arch;
25
- var platform = process.platform;
26
- if (platform === "darwin") {
27
- return arch === "arm64" ? "aarch64-apple-darwin" : "x86_64-apple-darwin";
28
- }
29
- if (platform === "linux") {
30
- return arch === "arm64" ? "aarch64-unknown-linux-musl" : "x86_64-unknown-linux-musl";
31
- }
32
- if (platform === "win32") {
33
- return "x86_64-pc-windows-msvc";
34
- }
35
- return null;
36
- }
37
-
38
- function findCodexPath() {
39
- var triple = getTargetTriple();
40
- if (!triple) throw new Error("Unsupported platform: " + process.platform + "/" + process.arch);
41
-
42
- var platformPkg = PLATFORM_PACKAGE_BY_TARGET[triple];
43
- if (!platformPkg) throw new Error("No codex binary package for: " + triple);
44
-
45
- try {
46
- var codexPkgJson = require.resolve("@openai/codex/package.json");
47
- var codexRequire = createRequire(codexPkgJson);
48
- var platformPkgJson = codexRequire.resolve(platformPkg + "/package.json");
49
- var vendorRoot = path.join(path.dirname(platformPkgJson), "vendor");
50
- var binaryName = process.platform === "win32" ? "codex.exe" : "codex";
51
- return path.join(vendorRoot, triple, "codex", binaryName);
52
- } catch (e) {
53
- throw new Error("Could not find codex binary: " + e.message);
54
- }
55
- }
9
+ var { spawnCodex } = require("./codex-command");
56
10
 
57
11
  // --- Config serialization ---
58
12
  // Flattens a nested config object into --config key=value pairs.
@@ -60,13 +14,19 @@ function findCodexPath() {
60
14
  // e.g. { mcp_servers: { "clay-tools": { command: "node", args: ["a.js"] } } }
61
15
  // -> ["mcp_servers.clay-tools.command=\"node\"", "mcp_servers.clay-tools.args=[\"a.js\"]"]
62
16
 
17
+ // A TOML key segment that is not made of letters, digits, "_" and "-" (a
18
+ // project path, say) must be quoted, or its dots and slashes split the key.
19
+ function tomlKeySegment(key) {
20
+ return /^[A-Za-z0-9_-]+$/.test(key) ? key : JSON.stringify(key);
21
+ }
22
+
63
23
  function serializeConfig(obj, prefix) {
64
24
  var result = [];
65
25
  var keys = Object.keys(obj);
66
26
  for (var i = 0; i < keys.length; i++) {
67
27
  var key = keys[i];
68
28
  var val = obj[key];
69
- var fullKey = prefix ? prefix + "." + key : key;
29
+ var fullKey = prefix ? prefix + "." + tomlKeySegment(key) : tomlKeySegment(key);
70
30
 
71
31
  if (val === null || val === undefined) continue;
72
32
 
@@ -94,17 +54,30 @@ function toTomlValue(val) {
94
54
 
95
55
  // --- CodexAppServer ---
96
56
 
57
+ // executablePath is the resolved Codex binary (codex-binary.js). Without one
58
+ // the user's installed Codex is resolved here, and a Codex that is absent or
59
+ // too old fails construction with a named error.
60
+ //
61
+ // opts: { cwd, env, config }
62
+ // env the environment laid over the daemon's own
97
63
  function CodexAppServer(executablePath, opts) {
98
64
  this.proc = null;
99
65
  this.rl = null;
100
66
  this.nextId = 1;
101
- this.pendingRequests = {}; // id -> { resolve, reject, timer }
67
+ // Keyed by ids the app-server echoes back: no prototype to answer for them.
68
+ this.pendingRequests = Object.create(null); // id -> { resolve, reject, timer }
102
69
  // eventHandler kept for backward-compat but superseded by the multiplexer below.
103
70
  // Use addEventHandler / removeEventHandler instead of setting this directly.
104
71
  this.eventHandler = null;
105
72
  this._eventHandlers = []; // [{ threadId: string|null, fn: function }]
106
- this.executablePath = executablePath || findCodexPath();
107
73
  this.opts = opts || {};
74
+ // What resolving the binary found (path, version), when it was resolved here.
75
+ this.resolution = null;
76
+ if (!executablePath) {
77
+ this.resolution = require("./codex-binary").requireCodex();
78
+ executablePath = this.resolution.path;
79
+ }
80
+ this.executablePath = executablePath;
108
81
  this.started = false;
109
82
  this._stderrBuf = "";
110
83
  }
@@ -127,11 +100,12 @@ CodexAppServer.prototype.start = function() {
127
100
 
128
101
  console.log("[codex-app-server] Spawning:", self.executablePath, args.join(" "));
129
102
 
130
- self.proc = spawn(self.executablePath, args, {
103
+ // spawnCodex runs a Windows .cmd/.bat shim through the command interpreter.
104
+ self.proc = spawnCodex(self.executablePath, args, {
131
105
  stdio: ["pipe", "pipe", "pipe"],
132
106
  env: env,
133
107
  cwd: self.opts.cwd || process.cwd(),
134
- });
108
+ }, self.opts.commandDeps);
135
109
 
136
110
  self.proc.on("error", function(err) {
137
111
  console.error("[codex-app-server] Process error:", err.message);
@@ -300,7 +274,7 @@ CodexAppServer.prototype._rejectAllPending = function(err) {
300
274
  if (pending.timer) clearTimeout(pending.timer);
301
275
  pending.reject(err);
302
276
  }
303
- this.pendingRequests = {};
277
+ this.pendingRequests = Object.create(null);
304
278
  };
305
279
 
306
280
  // Register an event handler for a specific thread (or null to receive all events).
@@ -348,5 +322,5 @@ CodexAppServer.prototype.stop = function() {
348
322
 
349
323
  module.exports = {
350
324
  CodexAppServer: CodexAppServer,
351
- findCodexPath: findCodexPath,
325
+ serializeConfig: serializeConfig,
352
326
  };