@moxt-ai/mobius 0.0.71 → 0.0.73
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/dist/acp/acp-error.d.ts +17 -0
- package/dist/acp/acp-error.d.ts.map +1 -0
- package/dist/acp/acp-error.js +28 -0
- package/dist/acp/acp-error.js.map +1 -0
- package/dist/acp/codex/agent.d.ts +26 -0
- package/dist/acp/codex/agent.d.ts.map +1 -0
- package/dist/acp/codex/agent.js +706 -0
- package/dist/acp/codex/agent.js.map +1 -0
- package/dist/acp/codex/app-server-client.d.ts +45 -0
- package/dist/acp/codex/app-server-client.d.ts.map +1 -0
- package/dist/acp/codex/app-server-client.js +195 -0
- package/dist/acp/codex/app-server-client.js.map +1 -0
- package/dist/acp/codex/app-server-protocol.d.ts +225 -0
- package/dist/acp/codex/app-server-protocol.d.ts.map +1 -0
- package/dist/acp/codex/app-server-protocol.js +197 -0
- package/dist/acp/codex/app-server-protocol.js.map +1 -0
- package/dist/acp/codex/items.d.ts +17 -0
- package/dist/acp/codex/items.d.ts.map +1 -0
- package/dist/acp/codex/items.js +141 -0
- package/dist/acp/codex/items.js.map +1 -0
- package/dist/acp/in-process/acp-endpoint.d.ts +33 -0
- package/dist/acp/in-process/acp-endpoint.d.ts.map +1 -0
- package/dist/acp/in-process/acp-endpoint.js +2 -0
- package/dist/acp/in-process/acp-endpoint.js.map +1 -0
- package/dist/acp/session-manager.d.ts +5 -16
- package/dist/acp/session-manager.d.ts.map +1 -1
- package/dist/acp/session-manager.js +63 -38
- package/dist/acp/session-manager.js.map +1 -1
- package/dist/agent-catalog/acp-readiness-probe.d.ts +2 -2
- package/dist/agent-catalog/acp-readiness-probe.d.ts.map +1 -1
- package/dist/agent-catalog/acp-readiness-probe.js +55 -0
- package/dist/agent-catalog/acp-readiness-probe.js.map +1 -1
- package/dist/agent-catalog/agent-diagnostics.d.ts.map +1 -1
- package/dist/agent-catalog/agent-diagnostics.js +4 -1
- package/dist/agent-catalog/agent-diagnostics.js.map +1 -1
- package/dist/agent-catalog/agent-discovery.d.ts +4 -4
- package/dist/agent-catalog/agent-discovery.d.ts.map +1 -1
- package/dist/agent-catalog/agent-discovery.js.map +1 -1
- package/dist/agent-catalog/agent-settings-adapter.d.ts +8 -8
- package/dist/agent-catalog/agent-settings-adapter.d.ts.map +1 -1
- package/dist/agent-catalog/agent-settings-adapter.js.map +1 -1
- package/dist/agent-catalog/agent-specification.d.ts +21 -0
- package/dist/agent-catalog/agent-specification.d.ts.map +1 -1
- package/dist/agent-catalog/agent-specification.js +25 -0
- package/dist/agent-catalog/agent-specification.js.map +1 -1
- package/dist/agent-catalog/agents/codex-agent.d.ts +5 -9
- package/dist/agent-catalog/agents/codex-agent.d.ts.map +1 -1
- package/dist/agent-catalog/agents/codex-agent.js +9 -18
- package/dist/agent-catalog/agents/codex-agent.js.map +1 -1
- package/dist/agent-catalog/discover-agent.d.ts.map +1 -1
- package/dist/agent-catalog/discover-agent.js +11 -6
- package/dist/agent-catalog/discover-agent.js.map +1 -1
- package/dist/agent-catalog/executable-discovery.d.ts.map +1 -1
- package/dist/agent-catalog/executable-discovery.js +2 -2
- package/dist/agent-catalog/executable-discovery.js.map +1 -1
- package/dist/agent-catalog/local-agent.d.ts +25 -11
- package/dist/agent-catalog/local-agent.d.ts.map +1 -1
- package/dist/agent-catalog/local-agent.js +32 -12
- package/dist/agent-catalog/local-agent.js.map +1 -1
- package/dist/agent-installation/fixed-installation-executor.d.ts.map +1 -1
- package/dist/agent-installation/fixed-installation-executor.js +3 -1
- package/dist/agent-installation/fixed-installation-executor.js.map +1 -1
- package/dist/cli.js +8045 -6582
- package/dist/cli.js.map +4 -4
- package/dist/config.d.ts +5 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/daemon-diagnostic-log.d.ts +6 -0
- package/dist/daemon-diagnostic-log.d.ts.map +1 -0
- package/dist/daemon-diagnostic-log.js +48 -0
- package/dist/daemon-diagnostic-log.js.map +1 -0
- package/dist/index.js +1630 -167
- package/dist/index.js.map +4 -4
- package/dist/native-sessions/codex-history.d.ts +3 -3
- package/dist/native-sessions/codex-history.d.ts.map +1 -1
- package/dist/native-sessions/codex-history.js +1 -1
- package/dist/native-sessions/codex-history.js.map +1 -1
- package/dist/native-sessions/service.d.ts.map +1 -1
- package/dist/native-sessions/service.js +6 -5
- package/dist/native-sessions/service.js.map +1 -1
- package/dist/pairing/pair-local-machine.d.ts.map +1 -1
- package/dist/pairing/pair-local-machine.js.map +1 -1
- package/dist/relay-connection/diagnostics.d.ts +3 -1
- package/dist/relay-connection/diagnostics.d.ts.map +1 -1
- package/dist/relay-connection/diagnostics.js +16 -1
- package/dist/relay-connection/diagnostics.js.map +1 -1
- package/dist/run-daemon.d.ts.map +1 -1
- package/dist/run-daemon.js +17 -2
- package/dist/run-daemon.js.map +1 -1
- package/dist/runtime.js +5526 -4063
- package/dist/runtime.js.map +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import { agent as createAgentApp, methods, PROTOCOL_VERSION, } from "@agentclientprotocol/sdk";
|
|
2
|
+
import { captureProcessTree, stopProcessTree } from "../../process/executable.js";
|
|
3
|
+
import { AcpExecutionError } from "../acp-error.js";
|
|
4
|
+
import { CodexAppServerClient } from "./app-server-client.js";
|
|
5
|
+
import { commandApprovalParamsSchema, fileChangeApprovalParamsSchema, mcpElicitationParamsSchema, modelListResponseSchema, requestUserInputParamsSchema, threadStartResponseSchema, turnNotificationSchema, turnSchema, } from "./app-server-protocol.js";
|
|
6
|
+
import { agentMessageMeta, decodeThreadItem, toolCallUpdate } from "./items.js";
|
|
7
|
+
const APPROVAL_REASON_CHARACTER_LIMIT = 500;
|
|
8
|
+
const APPROVAL_TIMEOUT_MILLISECONDS = 3_600_000;
|
|
9
|
+
const CODEX_AGENT_VERSION = "0.154.0";
|
|
10
|
+
// The setting identifiers Mobius matches on. These are the ids the retired
|
|
11
|
+
// `@agentclientprotocol/codex-acp` adapter advertised, so conversations that stored a selection
|
|
12
|
+
// under them keep applying after the move in-process.
|
|
13
|
+
const MODEL_SETTING_ID = "model";
|
|
14
|
+
const REASONING_EFFORT_SETTING_ID = "reasoning_effort";
|
|
15
|
+
const MODE_SETTING_ID = "mode";
|
|
16
|
+
const FAST_MODE_SETTING_ID = "fast-mode";
|
|
17
|
+
const MODEL_CATALOG_LIMIT = 1_000;
|
|
18
|
+
// `additionalSpeedTiers` and `serviceTier` are two vocabularies for one concept: a model advertises
|
|
19
|
+
// "fast" while the thread reports the resolved tier as "priority". Fast mode is offered per model,
|
|
20
|
+
// because most models have no speed tier at all.
|
|
21
|
+
const FAST_TIER = "fast";
|
|
22
|
+
const SERVICE_TIER_FAST = "priority";
|
|
23
|
+
const WORKSPACE_WRITE_POLICY = Object.freeze({
|
|
24
|
+
excludeSlashTmp: false,
|
|
25
|
+
excludeTmpdirEnvVar: false,
|
|
26
|
+
networkAccess: false,
|
|
27
|
+
type: "workspaceWrite",
|
|
28
|
+
writableRoots: [],
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* The approval and sandboxing presets Mobius offers, matching the adapter this Agent replaced so an
|
|
32
|
+
* existing conversation's stored selection keeps its meaning. Each preset carries the full policy
|
|
33
|
+
* pair, because Codex takes approval and sandboxing together.
|
|
34
|
+
*/
|
|
35
|
+
const READ_ONLY_MODE = Object.freeze({
|
|
36
|
+
approvalPolicy: "on-request",
|
|
37
|
+
approvalsReviewer: "user",
|
|
38
|
+
description: "Always ask to edit external files and use the internet",
|
|
39
|
+
id: "read-only",
|
|
40
|
+
name: "Ask for approval",
|
|
41
|
+
sandboxPolicy: WORKSPACE_WRITE_POLICY,
|
|
42
|
+
});
|
|
43
|
+
const AGENT_MODE = Object.freeze({
|
|
44
|
+
approvalPolicy: "on-request",
|
|
45
|
+
approvalsReviewer: "auto_review",
|
|
46
|
+
description: "Only ask for actions detected as potentially unsafe",
|
|
47
|
+
id: "agent",
|
|
48
|
+
name: "Approve for me",
|
|
49
|
+
sandboxPolicy: WORKSPACE_WRITE_POLICY,
|
|
50
|
+
});
|
|
51
|
+
const FULL_ACCESS_MODE = Object.freeze({
|
|
52
|
+
approvalPolicy: "never",
|
|
53
|
+
approvalsReviewer: "user",
|
|
54
|
+
description: "Unrestricted access to the internet and any file on your computer",
|
|
55
|
+
id: "agent-full-access",
|
|
56
|
+
name: "Full access",
|
|
57
|
+
sandboxPolicy: { type: "dangerFullAccess" },
|
|
58
|
+
});
|
|
59
|
+
const AGENT_MODES = Object.freeze([READ_ONLY_MODE, AGENT_MODE, FULL_ACCESS_MODE]);
|
|
60
|
+
const DEFAULT_MODE = AGENT_MODE;
|
|
61
|
+
/**
|
|
62
|
+
* The mode the session opens in, from the same `INITIAL_AGENT_MODE` variable the adapter this Agent
|
|
63
|
+
* replaced honoured. The default is only a fallback: a host that configured the variable keeps the
|
|
64
|
+
* behavior it had.
|
|
65
|
+
*/
|
|
66
|
+
function initialMode(environment) {
|
|
67
|
+
const requested = environment["INITIAL_AGENT_MODE"];
|
|
68
|
+
if (requested === undefined) {
|
|
69
|
+
return DEFAULT_MODE.id;
|
|
70
|
+
}
|
|
71
|
+
return AGENT_MODES.some((mode) => mode.id === requested) ? requested : DEFAULT_MODE.id;
|
|
72
|
+
}
|
|
73
|
+
function capitalize(value) {
|
|
74
|
+
return value.length === 0 ? value : value.charAt(0).toUpperCase() + value.slice(1);
|
|
75
|
+
}
|
|
76
|
+
/** The choices Mobius offers when the app-server asks it to approve a local side effect. */
|
|
77
|
+
const APPROVAL_OPTIONS = Object.freeze([
|
|
78
|
+
{ kind: "allow_once", name: "Allow once", optionId: "accept" },
|
|
79
|
+
{ kind: "allow_always", name: "Allow for this session", optionId: "acceptForSession" },
|
|
80
|
+
{ kind: "reject_once", name: "Deny", optionId: "decline" },
|
|
81
|
+
]);
|
|
82
|
+
function permissionDecision(optionId) {
|
|
83
|
+
if (optionId === "accept" || optionId === "acceptForSession" || optionId === "decline") {
|
|
84
|
+
return optionId;
|
|
85
|
+
}
|
|
86
|
+
return "cancel";
|
|
87
|
+
}
|
|
88
|
+
/** Converts ACP prompt content into the app-server's `UserInput` list. */
|
|
89
|
+
function promptInput(prompt) {
|
|
90
|
+
const input = [];
|
|
91
|
+
for (const block of prompt) {
|
|
92
|
+
if (block.type === "text") {
|
|
93
|
+
input.push({ text: block.text, text_elements: [], type: "text" });
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (block.type === "image") {
|
|
97
|
+
input.push({ type: "image", url: `data:${block.mimeType};base64,${block.data}` });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (block.type === "resource") {
|
|
101
|
+
const resource = block.resource;
|
|
102
|
+
input.push({
|
|
103
|
+
text: "text" in resource ? (resource.text ?? "") : "",
|
|
104
|
+
text_elements: [],
|
|
105
|
+
type: "text",
|
|
106
|
+
});
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (block.type === "resource_link") {
|
|
110
|
+
input.push({ text: `Resource: ${block.uri}`, text_elements: [], type: "text" });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return input;
|
|
114
|
+
}
|
|
115
|
+
function modelValues(models) {
|
|
116
|
+
return models.map((model) => ({
|
|
117
|
+
description: model.description ?? "",
|
|
118
|
+
name: model.displayName ?? model.id,
|
|
119
|
+
value: model.id,
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
function effortValues(model) {
|
|
123
|
+
return (model?.supportedReasoningEfforts ?? []).map((option) => ({
|
|
124
|
+
description: option.description ?? "",
|
|
125
|
+
name: capitalize(option.reasoningEffort),
|
|
126
|
+
value: option.reasoningEffort,
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
function modelSetting(models, currentValue) {
|
|
130
|
+
const values = modelValues(models);
|
|
131
|
+
// The daemon drops any option whose current value is not one it offers, so a mismatch costs the
|
|
132
|
+
// user the whole selector rather than one entry. Codex may name a model its catalog omits, and
|
|
133
|
+
// may name none at all, so the offered list absorbs both cases.
|
|
134
|
+
const current = currentValue.length > 0
|
|
135
|
+
? currentValue
|
|
136
|
+
: (models.find((model) => model.isDefault === true)?.id ?? values[0]?.value ?? "");
|
|
137
|
+
if (current.length === 0) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const options = values.some((value) => value.value === current)
|
|
141
|
+
? values
|
|
142
|
+
: [{ description: "", name: current, value: current }, ...values];
|
|
143
|
+
return {
|
|
144
|
+
category: "model",
|
|
145
|
+
currentValue: current,
|
|
146
|
+
description: "Model Codex uses for the session",
|
|
147
|
+
id: MODEL_SETTING_ID,
|
|
148
|
+
name: "Model",
|
|
149
|
+
options,
|
|
150
|
+
type: "select",
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function modeSetting(currentValue) {
|
|
154
|
+
return {
|
|
155
|
+
category: "mode",
|
|
156
|
+
currentValue,
|
|
157
|
+
description: "Approval and sandboxing preset for the session",
|
|
158
|
+
id: MODE_SETTING_ID,
|
|
159
|
+
name: "Mode",
|
|
160
|
+
options: AGENT_MODES.map((mode) => ({
|
|
161
|
+
description: mode.description,
|
|
162
|
+
name: mode.name,
|
|
163
|
+
value: mode.id,
|
|
164
|
+
})),
|
|
165
|
+
type: "select",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Fast mode is a toggle, and it is only offered for a model that has a speed tier. A model without
|
|
170
|
+
* one is not a disabled toggle: switching to it removes the setting, which is how the adapter this
|
|
171
|
+
* Agent replaced behaved and why the option disappears rather than greys out.
|
|
172
|
+
*/
|
|
173
|
+
function fastModeSetting(model, enabled) {
|
|
174
|
+
if (model?.additionalSpeedTiers?.includes(FAST_TIER) !== true) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
category: "model_config",
|
|
179
|
+
currentValue: enabled,
|
|
180
|
+
description: "1.5x speed, increased usage",
|
|
181
|
+
id: FAST_MODE_SETTING_ID,
|
|
182
|
+
name: "Fast mode",
|
|
183
|
+
type: "boolean",
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function effortSetting(model, currentValue) {
|
|
187
|
+
const values = effortValues(model);
|
|
188
|
+
if (values.length === 0) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const current = values.some((value) => value.value === currentValue) ? currentValue : (values[0]?.value ?? "");
|
|
192
|
+
if (current.length === 0) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
category: "thought_level",
|
|
197
|
+
currentValue: current,
|
|
198
|
+
description: "How much reasoning effort the model should use",
|
|
199
|
+
id: REASONING_EFFORT_SETTING_ID,
|
|
200
|
+
name: "Reasoning effort",
|
|
201
|
+
options: values,
|
|
202
|
+
type: "select",
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function userInputSchema(questions) {
|
|
206
|
+
const properties = {};
|
|
207
|
+
const required = [];
|
|
208
|
+
for (const question of questions) {
|
|
209
|
+
required.push(question.id);
|
|
210
|
+
properties[question.id] = {
|
|
211
|
+
description: question.question,
|
|
212
|
+
title: question.header,
|
|
213
|
+
type: "string",
|
|
214
|
+
...(question.options == null || question.options.length === 0
|
|
215
|
+
? {}
|
|
216
|
+
: {
|
|
217
|
+
oneOf: question.options.map((option) => ({
|
|
218
|
+
const: option.label,
|
|
219
|
+
description: option.description,
|
|
220
|
+
title: option.label,
|
|
221
|
+
})),
|
|
222
|
+
}),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return { properties, required, type: "object" };
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* One in-process Codex ACP agent: an ACP `AgentApp` bound to one `codex app-server` child process.
|
|
229
|
+
* Mobius connects its normal ACP client to this app in-process, so every ACP behavior the daemon
|
|
230
|
+
* relies on is produced here instead of by an adapter child process.
|
|
231
|
+
*
|
|
232
|
+
* Turn lifecycle: `session/prompt` maps to `turn/start`, whose response arrives before the turn
|
|
233
|
+
* finishes. Completion is therefore captured from the `turn/completed` notification, and `session/
|
|
234
|
+
* cancel` resolves the same completion with `stopReason: "cancelled"` so a cancelled turn never
|
|
235
|
+
* waits on a notification that may not arrive.
|
|
236
|
+
*/
|
|
237
|
+
export class CodexAcpAgent {
|
|
238
|
+
#app;
|
|
239
|
+
#appServer;
|
|
240
|
+
#connection = null;
|
|
241
|
+
#models = [];
|
|
242
|
+
#nativeSessionId = "";
|
|
243
|
+
// Whether the current turn must carry a policy at all. Until the user picks a mode, Codex's own
|
|
244
|
+
// configuration decides, so `turn/start` omits the fields and inherits what the host set up.
|
|
245
|
+
#policySelected = false;
|
|
246
|
+
#selection;
|
|
247
|
+
#turn = null;
|
|
248
|
+
#updateTail = Promise.resolve();
|
|
249
|
+
constructor(options) {
|
|
250
|
+
this.#selection = {
|
|
251
|
+
effort: "",
|
|
252
|
+
fastMode: false,
|
|
253
|
+
mode: initialMode(options.environment),
|
|
254
|
+
model: "",
|
|
255
|
+
};
|
|
256
|
+
this.#appServer = new CodexAppServerClient({
|
|
257
|
+
command: options.command,
|
|
258
|
+
cwd: options.cwd,
|
|
259
|
+
environment: options.environment,
|
|
260
|
+
handlers: {
|
|
261
|
+
onNotification: async (method, params) => {
|
|
262
|
+
await this.#onNotification(method, params);
|
|
263
|
+
},
|
|
264
|
+
onServerRequest: async (request) => {
|
|
265
|
+
await this.#onServerRequest(request);
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
this.#app = createAgentApp({ name: "mobius-codex" })
|
|
270
|
+
.onConnect((connection) => {
|
|
271
|
+
this.#connection = connection;
|
|
272
|
+
})
|
|
273
|
+
// Declare only what this Agent actually serves. Mobius gates every request on these bits,
|
|
274
|
+
// so an optimistic bit here becomes an unsupported ACP call at the far end. Session
|
|
275
|
+
// restore, listing, forking, and steering stay undeclared until they are implemented;
|
|
276
|
+
// the daemon's existing degradation paths handle their absence.
|
|
277
|
+
.onRequest(methods.agent.initialize, async () => ({
|
|
278
|
+
agentCapabilities: {
|
|
279
|
+
loadSession: false,
|
|
280
|
+
promptCapabilities: { embeddedContext: true, image: true },
|
|
281
|
+
sessionCapabilities: { close: {} },
|
|
282
|
+
},
|
|
283
|
+
agentInfo: { name: "Codex", version: CODEX_AGENT_VERSION },
|
|
284
|
+
authMethods: [],
|
|
285
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
286
|
+
}))
|
|
287
|
+
.onRequest(methods.agent.session.new, async (context) => {
|
|
288
|
+
// Sandbox and approval policy stay with Codex's own configuration. Mobius reports the
|
|
289
|
+
// session it was given instead of imposing policy the user did not ask for.
|
|
290
|
+
const created = threadStartResponseSchema.parse(await this.#appServer.request("thread/start", { cwd: context.params.cwd }, AbortSignal.any([context.signal, AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS)])));
|
|
291
|
+
this.#nativeSessionId = created.thread.id;
|
|
292
|
+
// A new thread always starts in the default mode, so only the model and effort come
|
|
293
|
+
// from Codex. The mode is re-applied on the first turn.
|
|
294
|
+
this.#selection = {
|
|
295
|
+
...this.#selection,
|
|
296
|
+
effort: created.reasoningEffort ?? "",
|
|
297
|
+
model: created.model ?? "",
|
|
298
|
+
};
|
|
299
|
+
// The settings Mobius discovers come from this response. Without them the Run
|
|
300
|
+
// settings panel has nothing to render and saved selections cannot be re-applied.
|
|
301
|
+
return { configOptions: this.#configOptions(), sessionId: created.thread.id };
|
|
302
|
+
})
|
|
303
|
+
.onRequest(methods.agent.session.setConfigOption, async (context) => {
|
|
304
|
+
this.#applySelection(context.params.configId, context.params.value);
|
|
305
|
+
return { configOptions: this.#configOptions() };
|
|
306
|
+
})
|
|
307
|
+
// Mobius routes a `category: "mode"` selection here rather than through
|
|
308
|
+
// set_config_option, so the mode needs its own entry point.
|
|
309
|
+
.onRequest(methods.agent.session.setMode, async (context) => {
|
|
310
|
+
this.#applySelection(MODE_SETTING_ID, context.params.modeId);
|
|
311
|
+
return {};
|
|
312
|
+
})
|
|
313
|
+
.onRequest(methods.agent.session.prompt, async (context) => {
|
|
314
|
+
return await this.#runTurn(promptInput(context.params.prompt), context.signal);
|
|
315
|
+
})
|
|
316
|
+
.onRequest(methods.agent.session.close, async () => {
|
|
317
|
+
await this.#interruptTurn();
|
|
318
|
+
})
|
|
319
|
+
.onNotification(methods.agent.session.cancel, async () => {
|
|
320
|
+
await this.#interruptTurn();
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/** The ACP agent app Mobius connects its own ACP client to, in-process. */
|
|
324
|
+
app = () => this.#app;
|
|
325
|
+
/** Starts the app-server, performs its handshake, and reads the model catalog Codex offers. */
|
|
326
|
+
initialize = async (signal) => {
|
|
327
|
+
await this.#appServer.initialize(signal);
|
|
328
|
+
await this.#loadModels(signal);
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Releases the app-server and every process it started. Mirrors the adapter path's teardown so a
|
|
332
|
+
* cancelled or closed session cannot leak the tool subprocesses running under the app-server.
|
|
333
|
+
*/
|
|
334
|
+
close = async () => {
|
|
335
|
+
const child = this.#appServer.child();
|
|
336
|
+
const captured = await captureProcessTree(child);
|
|
337
|
+
this.#appServer.detach();
|
|
338
|
+
await stopProcessTree(child, captured);
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* Reads the model catalog once per app-server. A failure leaves the catalog empty, which costs
|
|
342
|
+
* the Run settings panel its model selector; it must not cost the conversation itself, so the
|
|
343
|
+
* turn lifecycle continues without settings rather than failing initialization.
|
|
344
|
+
*/
|
|
345
|
+
#loadModels = async (signal) => {
|
|
346
|
+
const models = [];
|
|
347
|
+
let cursor = null;
|
|
348
|
+
try {
|
|
349
|
+
do {
|
|
350
|
+
const decoded = modelListResponseSchema.parse(await this.#appServer.request("model/list", { cursor, limit: null }, AbortSignal.any([signal, AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS)])));
|
|
351
|
+
models.push(...decoded.data);
|
|
352
|
+
cursor = decoded.nextCursor ?? null;
|
|
353
|
+
} while (cursor !== null && models.length < MODEL_CATALOG_LIMIT);
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
signal.throwIfAborted();
|
|
357
|
+
process.stderr.write(`[codex-acp] model catalog unavailable: ${String(error)}\n`);
|
|
358
|
+
}
|
|
359
|
+
this.#models = Object.freeze(models.slice(0, MODEL_CATALOG_LIMIT));
|
|
360
|
+
};
|
|
361
|
+
#model = (id) => this.#models.find((model) => model.id === id);
|
|
362
|
+
/** The ACP settings Mobius discovers and renders in Run settings. */
|
|
363
|
+
#configOptions = () => {
|
|
364
|
+
const options = [];
|
|
365
|
+
const model = modelSetting(this.#models, this.#selection.model);
|
|
366
|
+
if (model !== null) {
|
|
367
|
+
options.push(model);
|
|
368
|
+
}
|
|
369
|
+
const effort = effortSetting(this.#model(this.#selection.model), this.#selection.effort);
|
|
370
|
+
if (effort !== null) {
|
|
371
|
+
options.push(effort);
|
|
372
|
+
}
|
|
373
|
+
const fastMode = fastModeSetting(this.#model(this.#selection.model), this.#selection.fastMode);
|
|
374
|
+
if (fastMode !== null) {
|
|
375
|
+
options.push(fastMode);
|
|
376
|
+
}
|
|
377
|
+
options.push(modeSetting(this.#selection.mode));
|
|
378
|
+
return options;
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* Applies one `session/set_config_option`. Switching models re-resolves the effort the way Codex
|
|
382
|
+
* does — the current effort when the new model supports it, otherwise the model's own default —
|
|
383
|
+
* so the reply never advertises an effort the selected model cannot run.
|
|
384
|
+
*/
|
|
385
|
+
#applySelection = (configId, value) => {
|
|
386
|
+
if (configId === FAST_MODE_SETTING_ID) {
|
|
387
|
+
if (typeof value !== "boolean") {
|
|
388
|
+
throw new AcpExecutionError("The requested Codex setting is invalid", {}, "invalid_session_configuration");
|
|
389
|
+
}
|
|
390
|
+
// A model without a speed tier has no fast mode, so enabling it there would advertise a
|
|
391
|
+
// capability the next turn cannot honour.
|
|
392
|
+
if (value && this.#model(this.#selection.model)?.additionalSpeedTiers?.includes(FAST_TIER) !== true) {
|
|
393
|
+
throw new AcpExecutionError("The selected Codex model has no fast mode", {}, "invalid_session_configuration");
|
|
394
|
+
}
|
|
395
|
+
this.#selection = { ...this.#selection, fastMode: value };
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (typeof value !== "string") {
|
|
399
|
+
throw new AcpExecutionError("The requested Codex setting is invalid", {}, "invalid_session_configuration");
|
|
400
|
+
}
|
|
401
|
+
if (configId === MODE_SETTING_ID) {
|
|
402
|
+
if (!AGENT_MODES.some((mode) => mode.id === value)) {
|
|
403
|
+
throw new AcpExecutionError("The requested Codex mode is unavailable", {}, "invalid_session_configuration");
|
|
404
|
+
}
|
|
405
|
+
this.#selection = { ...this.#selection, mode: value };
|
|
406
|
+
this.#policySelected = true;
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (configId === MODEL_SETTING_ID) {
|
|
410
|
+
if (value === this.#selection.model) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const model = this.#model(value);
|
|
414
|
+
if (model === undefined) {
|
|
415
|
+
throw new AcpExecutionError("The requested Codex model is unavailable", {}, "invalid_session_configuration");
|
|
416
|
+
}
|
|
417
|
+
const supported = (model.supportedReasoningEfforts ?? []).map((option) => option.reasoningEffort);
|
|
418
|
+
const effort = supported.includes(this.#selection.effort)
|
|
419
|
+
? this.#selection.effort
|
|
420
|
+
: (model.defaultReasoningEffort ?? supported[0] ?? "");
|
|
421
|
+
// Fast mode follows the model: a model with no speed tier drops a fast mode that was on.
|
|
422
|
+
this.#selection = {
|
|
423
|
+
...this.#selection,
|
|
424
|
+
effort,
|
|
425
|
+
fastMode: this.#selection.fastMode && model.additionalSpeedTiers?.includes(FAST_TIER) === true,
|
|
426
|
+
model: value,
|
|
427
|
+
};
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (configId === REASONING_EFFORT_SETTING_ID) {
|
|
431
|
+
const supported = (this.#model(this.#selection.model)?.supportedReasoningEfforts ?? []).map((option) => option.reasoningEffort);
|
|
432
|
+
if (!supported.includes(value)) {
|
|
433
|
+
throw new AcpExecutionError("The requested Codex reasoning effort is unavailable", {}, "invalid_session_configuration");
|
|
434
|
+
}
|
|
435
|
+
this.#selection = { ...this.#selection, effort: value };
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
throw new AcpExecutionError("The requested Codex setting is unavailable", {}, "invalid_session_configuration");
|
|
439
|
+
};
|
|
440
|
+
#emit = async (update) => {
|
|
441
|
+
const connection = this.#connection;
|
|
442
|
+
if (connection === null || this.#nativeSessionId.length === 0) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
await connection.client.notify(methods.client.session.update, {
|
|
446
|
+
sessionId: this.#nativeSessionId,
|
|
447
|
+
update,
|
|
448
|
+
});
|
|
449
|
+
};
|
|
450
|
+
// Notifications are serialized so two app-server items cannot interleave into one ACP update
|
|
451
|
+
// stream, and the caller awaits the result: `turn/completed` must not resolve the prompt before
|
|
452
|
+
// the updates it precedes have reached the client.
|
|
453
|
+
#enqueue = async (operation) => {
|
|
454
|
+
const pending = this.#updateTail.then(operation, operation);
|
|
455
|
+
this.#updateTail = pending;
|
|
456
|
+
await pending;
|
|
457
|
+
};
|
|
458
|
+
#onNotification = async (method, params) => {
|
|
459
|
+
if (method === "turn/started") {
|
|
460
|
+
const decoded = turnNotificationSchema.safeParse(params);
|
|
461
|
+
if (decoded.success && decoded.data.threadId === this.#nativeSessionId && this.#turn !== null) {
|
|
462
|
+
this.#turn = { completion: this.#turn.completion, turn: { id: decoded.data.turn.id } };
|
|
463
|
+
}
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
if (method === "turn/completed") {
|
|
467
|
+
const decoded = turnNotificationSchema.safeParse(params);
|
|
468
|
+
if (decoded.success && decoded.data.threadId === this.#nativeSessionId) {
|
|
469
|
+
await this.#completeTurn(decoded.data.turn);
|
|
470
|
+
}
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (method === "thread/name/updated") {
|
|
474
|
+
const threadName = params.threadName;
|
|
475
|
+
if (typeof threadName === "string" && threadName.length > 0) {
|
|
476
|
+
await this.#enqueue(async () => {
|
|
477
|
+
await this.#emit({ sessionUpdate: "session_info_update", title: threadName });
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (method === "turn/plan/updated") {
|
|
483
|
+
const plan = params.plan;
|
|
484
|
+
if (Array.isArray(plan) && params.threadId === this.#nativeSessionId) {
|
|
485
|
+
const entries = plan.map((step) => ({
|
|
486
|
+
content: typeof step === "object" && step !== null && "step" in step
|
|
487
|
+
? String(Reflect.get(step, "step"))
|
|
488
|
+
: "",
|
|
489
|
+
priority: "medium",
|
|
490
|
+
status: "in_progress",
|
|
491
|
+
}));
|
|
492
|
+
await this.#enqueue(async () => {
|
|
493
|
+
await this.#emit({ entries, sessionUpdate: "plan" });
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (method !== "item/started" && method !== "item/completed") {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const item = decodeThreadItem(params.item);
|
|
502
|
+
if (item === null) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (item.type === "agentMessage") {
|
|
506
|
+
// An item/completed agent message carries the whole text and would duplicate the deltas
|
|
507
|
+
// already streamed for the same item.
|
|
508
|
+
if (method === "item/started") {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
await this.#enqueue(async () => {
|
|
512
|
+
await this.#emit({
|
|
513
|
+
_meta: agentMessageMeta(item.phase),
|
|
514
|
+
content: { text: item.text, type: "text" },
|
|
515
|
+
messageId: item.id,
|
|
516
|
+
sessionUpdate: "agent_message_chunk",
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const update = toolCallUpdate(item);
|
|
522
|
+
if (update !== null) {
|
|
523
|
+
await this.#enqueue(async () => {
|
|
524
|
+
await this.#emit({ ...update, sessionUpdate: "tool_call_update" });
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
#onServerRequest = async (request) => {
|
|
529
|
+
if (request.method === "item/commandExecution/requestApproval" ||
|
|
530
|
+
request.method === "item/fileChange/requestApproval") {
|
|
531
|
+
await this.#requestApproval(request);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (request.method === "item/tool/requestUserInput") {
|
|
535
|
+
await this.#requestUserInput(request);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (request.method === "mcpServer/elicitation/request") {
|
|
539
|
+
await this.#requestMcpElicitation(request);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
// Mobius did not opt into these capabilities. Answering an error keeps the conversation
|
|
543
|
+
// honest instead of letting the app-server wait on a request nobody will handle.
|
|
544
|
+
this.#appServer.respondWithError(request.id, -32_601, `Mobius does not support ${request.method}`);
|
|
545
|
+
};
|
|
546
|
+
#askPermission = async (title, toolCallId, signal) => {
|
|
547
|
+
const connection = this.#connection;
|
|
548
|
+
if (connection === null) {
|
|
549
|
+
return "cancel";
|
|
550
|
+
}
|
|
551
|
+
const response = await connection.client.request(methods.client.session.requestPermission, {
|
|
552
|
+
options: [...APPROVAL_OPTIONS],
|
|
553
|
+
sessionId: this.#nativeSessionId,
|
|
554
|
+
toolCall: { title, toolCallId },
|
|
555
|
+
}, { cancellationSignal: signal });
|
|
556
|
+
return response.outcome.outcome === "selected" ? permissionDecision(response.outcome.optionId) : "cancel";
|
|
557
|
+
};
|
|
558
|
+
#requestApproval = async (request) => {
|
|
559
|
+
const decoded = request.method === "item/commandExecution/requestApproval"
|
|
560
|
+
? commandApprovalParamsSchema.safeParse(request.params)
|
|
561
|
+
: fileChangeApprovalParamsSchema.safeParse(request.params);
|
|
562
|
+
if (!decoded.success) {
|
|
563
|
+
this.#appServer.respondWithError(request.id, -32_602, "Mobius could not read the approval request");
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const params = decoded.data;
|
|
567
|
+
const command = "command" in params ? params.command : undefined;
|
|
568
|
+
const title = (command ?? params.reason ?? "Approve a local change").slice(0, APPROVAL_REASON_CHARACTER_LIMIT);
|
|
569
|
+
const turn = this.#turn;
|
|
570
|
+
const decision = await this.#askPermission(title, params.itemId, AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS));
|
|
571
|
+
void turn;
|
|
572
|
+
this.#appServer.respond(request.id, { decision });
|
|
573
|
+
};
|
|
574
|
+
#requestUserInput = async (request) => {
|
|
575
|
+
const decoded = requestUserInputParamsSchema.safeParse(request.params);
|
|
576
|
+
const connection = this.#connection;
|
|
577
|
+
if (!decoded.success || connection === null) {
|
|
578
|
+
this.#appServer.respond(request.id, { answers: {} });
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
const questions = decoded.data.questions;
|
|
582
|
+
const response = await connection.client.request(methods.client.elicitation.create, {
|
|
583
|
+
message: questions
|
|
584
|
+
.map((question) => question.question)
|
|
585
|
+
.join("\n\n")
|
|
586
|
+
.slice(0, 2_000),
|
|
587
|
+
mode: "form",
|
|
588
|
+
requestedSchema: userInputSchema(questions),
|
|
589
|
+
sessionId: this.#nativeSessionId,
|
|
590
|
+
...(decoded.data.itemId == null ? {} : { toolCallId: decoded.data.itemId }),
|
|
591
|
+
}, { cancellationSignal: AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS) });
|
|
592
|
+
const content = response.action === "accept" ? Reflect.get(response, "content") : undefined;
|
|
593
|
+
const answers = {};
|
|
594
|
+
for (const question of questions) {
|
|
595
|
+
const value = typeof content === "object" && content !== null ? Reflect.get(content, question.id) : undefined;
|
|
596
|
+
answers[question.id] = { answers: typeof value === "string" && value.length > 0 ? [value] : [] };
|
|
597
|
+
}
|
|
598
|
+
this.#appServer.respond(request.id, { answers });
|
|
599
|
+
};
|
|
600
|
+
#requestMcpElicitation = async (request) => {
|
|
601
|
+
const decoded = mcpElicitationParamsSchema.safeParse(request.params);
|
|
602
|
+
const connection = this.#connection;
|
|
603
|
+
if (!decoded.success || connection === null) {
|
|
604
|
+
this.#appServer.respondWithError(request.id, -32_602, "Mobius could not read the elicitation request");
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
const params = decoded.data;
|
|
608
|
+
const signal = AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS);
|
|
609
|
+
if (params.mode === "url" && params.url !== undefined) {
|
|
610
|
+
const response = await connection.client.request(methods.client.elicitation.create, {
|
|
611
|
+
elicitationId: `${this.#nativeSessionId}-${String(request.id)}`,
|
|
612
|
+
message: params.message,
|
|
613
|
+
mode: "url",
|
|
614
|
+
sessionId: this.#nativeSessionId,
|
|
615
|
+
url: params.url,
|
|
616
|
+
}, { cancellationSignal: signal });
|
|
617
|
+
this.#appServer.respond(request.id, { action: response.action });
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const response = await connection.client.request(methods.client.elicitation.create, {
|
|
621
|
+
message: params.message,
|
|
622
|
+
mode: "form",
|
|
623
|
+
requestedSchema: params.requestedSchema ?? { properties: {}, required: [], type: "object" },
|
|
624
|
+
sessionId: this.#nativeSessionId,
|
|
625
|
+
}, { cancellationSignal: signal });
|
|
626
|
+
const content = response.action === "accept" ? (Reflect.get(response, "content") ?? null) : null;
|
|
627
|
+
this.#appServer.respond(request.id, { action: response.action, content });
|
|
628
|
+
};
|
|
629
|
+
#runTurn = async (input, signal) => {
|
|
630
|
+
if (this.#turn !== null) {
|
|
631
|
+
throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
|
|
632
|
+
}
|
|
633
|
+
const completion = Promise.withResolvers();
|
|
634
|
+
// The turn is armed before `turn/start` is sent: a short turn can complete before its own
|
|
635
|
+
// response arrives, and a completion for an unarmed turn would be lost.
|
|
636
|
+
const turn = { completion, turn: { id: "" } };
|
|
637
|
+
this.#turn = turn;
|
|
638
|
+
try {
|
|
639
|
+
// The selection rides on the turn rather than the thread, so a choice made between turns
|
|
640
|
+
// applies to the next one without restarting the conversation. Approval and sandboxing
|
|
641
|
+
// travel as a pair: Codex resolves them together into the mode's single preset.
|
|
642
|
+
const selection = this.#selection;
|
|
643
|
+
const mode = AGENT_MODES.find((candidate) => candidate.id === selection.mode) ?? DEFAULT_MODE;
|
|
644
|
+
const supportsFast = this.#model(selection.model)?.additionalSpeedTiers?.includes(FAST_TIER) === true;
|
|
645
|
+
const started = turnSchema.parse((await this.#appServer.request("turn/start", {
|
|
646
|
+
// Until a mode is chosen, `config.toml` decides the sandbox and approval
|
|
647
|
+
// policy, and those fields stay off the turn so the host's own setup is
|
|
648
|
+
// inherited rather than overridden.
|
|
649
|
+
...(this.#policySelected
|
|
650
|
+
? {
|
|
651
|
+
approvalPolicy: mode.approvalPolicy,
|
|
652
|
+
approvalsReviewer: mode.approvalsReviewer,
|
|
653
|
+
sandboxPolicy: mode.sandboxPolicy,
|
|
654
|
+
}
|
|
655
|
+
: {}),
|
|
656
|
+
...(selection.effort.length === 0 ? {} : { effort: selection.effort }),
|
|
657
|
+
input,
|
|
658
|
+
...(selection.model.length === 0 ? {} : { model: selection.model }),
|
|
659
|
+
...(selection.fastMode && supportsFast ? { serviceTier: SERVICE_TIER_FAST } : {}),
|
|
660
|
+
threadId: this.#nativeSessionId,
|
|
661
|
+
}, signal)).turn);
|
|
662
|
+
if (this.#turn === turn) {
|
|
663
|
+
turn.turn.id = started.id;
|
|
664
|
+
}
|
|
665
|
+
return await completion.promise;
|
|
666
|
+
}
|
|
667
|
+
finally {
|
|
668
|
+
if (this.#turn === turn) {
|
|
669
|
+
this.#turn = null;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
#interruptTurn = async () => {
|
|
674
|
+
const turn = this.#turn;
|
|
675
|
+
if (turn === null) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
this.#turn = null;
|
|
679
|
+
if (turn.turn.id.length > 0) {
|
|
680
|
+
try {
|
|
681
|
+
await this.#appServer.request("turn/interrupt", { threadId: this.#nativeSessionId, turnId: turn.turn.id }, AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS));
|
|
682
|
+
}
|
|
683
|
+
catch {
|
|
684
|
+
// Turn completion below remains authoritative.
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
turn.completion.resolve({ stopReason: "cancelled" });
|
|
688
|
+
};
|
|
689
|
+
#completeTurn = async (turn) => {
|
|
690
|
+
const active = this.#turn;
|
|
691
|
+
if (active === null) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
this.#turn = null;
|
|
695
|
+
// The app-server dispatches notifications concurrently, so an item update can still be in
|
|
696
|
+
// flight when the turn completes. Drain it: a client must never see the turn finish before
|
|
697
|
+
// the activity that belongs to it.
|
|
698
|
+
await this.#updateTail;
|
|
699
|
+
if (turn.status === "failed") {
|
|
700
|
+
active.completion.reject(new AcpExecutionError(turn.error?.message ?? "The Codex turn failed", {}, "codex_turn_failed"));
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
active.completion.resolve({ stopReason: turn.status === "interrupted" ? "cancelled" : "end_turn" });
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
//# sourceMappingURL=agent.js.map
|