@openclaw/codex 2026.6.11 → 2026.7.1-1
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/{app-server-policy-BPTiVNsW.js → app-server-policy-C968Kgin.js} +5 -5
- package/dist/{attempt-notifications-BYnndyFl.js → attempt-notifications-BGsEIIDI.js} +29 -40
- package/dist/{command-handlers-DEPTJYk0.js → command-handlers-Dv-pzAmg.js} +278 -237
- package/dist/command-rpc-eLM_mtC7.js +784 -0
- package/dist/compact-4mVBriKT.js +603 -0
- package/dist/{computer-use-ClrJwoFp.js → computer-use-Bmaz333N.js} +3 -3
- package/dist/{config-CszD0vP3.js → config-CYEDnLJ2.js} +44 -18
- package/dist/conversation-binding-data-BHRpYbX3.js +121 -0
- package/dist/doctor-contract-api.js +590 -22
- package/dist/harness.js +29 -10
- package/dist/index.js +1134 -35
- package/dist/{media-understanding-provider-ZUQHN7gT.js → media-understanding-provider-BHOu9-Gg.js} +13 -8
- package/dist/media-understanding-provider.js +1 -1
- package/dist/{models-CAmfBWRl.js → models-DDmO1zwd.js} +2 -2
- package/dist/notification-correlation-Bo7KB3ks.js +35 -0
- package/dist/{plugin-app-cache-key-C9ILt7bl.js → plugin-app-cache-key-BrhVdeEf.js} +13 -5
- package/dist/{protocol-dh-ETiNd.js → protocol-2POPqAY4.js} +2 -1
- package/dist/{protocol-validators-B19q5BIX.js → protocol-validators-dZQ-UTOa.js} +205 -56
- package/dist/provider-capabilities-DzI8fgEu.js +6172 -0
- package/dist/provider-catalog.js +4 -2
- package/dist/{provider-B-OHpbD3.js → provider-zjPfx5Fs.js} +94 -23
- package/dist/provider.js +2 -2
- package/dist/{request-Dcd452Nk.js → request-BcJyl8KL.js} +2 -2
- package/dist/{run-attempt-CXxtFC9d.js → run-attempt-DNSNk_G8.js} +1250 -2992
- package/dist/{sandbox-guard-3tnjhjFb.js → sandbox-guard-DA2TQfZW.js} +5 -3
- package/dist/session-binding-C1ZXdP-x.js +627 -0
- package/dist/session-binding-meta-B7aEMU7g.js +6 -0
- package/dist/shared-client-4ICy3U6d.js +1853 -0
- package/dist/{side-question-C-DhgJOd.js → side-question-CMOpEhvY.js} +134 -142
- package/dist/{thread-lifecycle-DtT3-ehU.js → thread-lifecycle-qWE88Dn2.js} +803 -481
- package/dist/{web-search-provider.runtime-CkwxrXQj.js → web-search-provider.runtime-BSlriav6.js} +3 -3
- package/npm-shrinkwrap.json +34 -34
- package/openclaw.plugin.json +23 -3
- package/package.json +9 -6
- package/dist/client-CLrtWgrD.js +0 -729
- package/dist/client-factory-Bm6HsGob.js +0 -18
- package/dist/compact-aQJ0ZKhe.js +0 -278
- package/dist/conversation-binding-t0XMLz-j.js +0 -1557
- package/dist/provider-capabilities-D7hnTO76.js +0 -3201
- package/dist/rate-limit-cache-C7qmZ0Jh.js +0 -26
- package/dist/session-binding-HOuPt-E0.js +0 -338
- package/dist/shared-client-Dtx5i7Ez.js +0 -1013
package/dist/client-CLrtWgrD.js
DELETED
|
@@ -1,729 +0,0 @@
|
|
|
1
|
-
import { r as isRpcResponse } from "./protocol-dh-ETiNd.js";
|
|
2
|
-
import { d as resolveCodexAppServerRuntimeOptions } from "./config-CszD0vP3.js";
|
|
3
|
-
import { materializeWindowsSpawnProgram, resolveWindowsSpawnProgram } from "openclaw/plugin-sdk/windows-spawn";
|
|
4
|
-
import { OPENCLAW_VERSION, embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
5
|
-
import { createInterface } from "node:readline";
|
|
6
|
-
import { spawn } from "node:child_process";
|
|
7
|
-
import { EventEmitter } from "node:events";
|
|
8
|
-
import { PassThrough, Writable } from "node:stream";
|
|
9
|
-
import WebSocket from "ws";
|
|
10
|
-
//#region extensions/codex/src/app-server/transport-stdio.ts
|
|
11
|
-
/**
|
|
12
|
-
* Creates and configures stdio-backed Codex app-server transports, including
|
|
13
|
-
* Windows spawn normalization and environment filtering.
|
|
14
|
-
*/
|
|
15
|
-
const UNSAFE_ENVIRONMENT_KEYS = new Set([
|
|
16
|
-
"__proto__",
|
|
17
|
-
"constructor",
|
|
18
|
-
"prototype"
|
|
19
|
-
]);
|
|
20
|
-
const DEFAULT_SPAWN_RUNTIME = {
|
|
21
|
-
platform: process.platform,
|
|
22
|
-
env: process.env,
|
|
23
|
-
execPath: process.execPath
|
|
24
|
-
};
|
|
25
|
-
/** Resolves the concrete command/argv/shell settings used to spawn Codex app-server. */
|
|
26
|
-
function resolveCodexAppServerSpawnInvocation(options, runtime = DEFAULT_SPAWN_RUNTIME) {
|
|
27
|
-
if (options.commandSource === "managed") throw new Error("Managed Codex app-server start options must be resolved before spawn.");
|
|
28
|
-
const resolved = materializeWindowsSpawnProgram(resolveWindowsSpawnProgram({
|
|
29
|
-
command: options.command,
|
|
30
|
-
platform: runtime.platform,
|
|
31
|
-
env: runtime.env,
|
|
32
|
-
execPath: runtime.execPath,
|
|
33
|
-
packageName: "@openai/codex"
|
|
34
|
-
}), options.args);
|
|
35
|
-
return {
|
|
36
|
-
command: resolved.command,
|
|
37
|
-
args: resolved.argv,
|
|
38
|
-
shell: resolved.shell,
|
|
39
|
-
windowsHide: resolved.windowsHide
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/** Merges app-server environment overrides while honoring clearEnv and unsafe key filtering. */
|
|
43
|
-
function resolveCodexAppServerSpawnEnv(options, baseEnv = process.env, platform = process.platform) {
|
|
44
|
-
const env = Object.create(null);
|
|
45
|
-
copySafeEnvironmentEntries(env, baseEnv);
|
|
46
|
-
copySafeEnvironmentEntries(env, options.env ?? {});
|
|
47
|
-
const keysToClear = normalizedEnvironmentKeys(options.clearEnv ?? []);
|
|
48
|
-
if (platform === "win32") {
|
|
49
|
-
const lowerCaseKeysToClear = new Set(keysToClear.map((key) => key.toLowerCase()));
|
|
50
|
-
for (const candidate of Object.keys(env)) if (lowerCaseKeysToClear.has(candidate.toLowerCase())) delete env[candidate];
|
|
51
|
-
} else for (const key of keysToClear) delete env[key];
|
|
52
|
-
return env;
|
|
53
|
-
}
|
|
54
|
-
function normalizedEnvironmentKeys(rawKeys) {
|
|
55
|
-
const keys = [];
|
|
56
|
-
for (const rawKey of rawKeys) {
|
|
57
|
-
const key = rawKey.trim();
|
|
58
|
-
if (key.length > 0) keys.push(key);
|
|
59
|
-
}
|
|
60
|
-
return keys;
|
|
61
|
-
}
|
|
62
|
-
function copySafeEnvironmentEntries(target, source) {
|
|
63
|
-
for (const [key, value] of Object.entries(source)) {
|
|
64
|
-
if (UNSAFE_ENVIRONMENT_KEYS.has(key)) continue;
|
|
65
|
-
target[key] = value;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/** Spawns the Codex app-server process and returns the shared transport interface. */
|
|
69
|
-
function createStdioTransport(options) {
|
|
70
|
-
const env = resolveCodexAppServerSpawnEnv(options);
|
|
71
|
-
const invocation = resolveCodexAppServerSpawnInvocation(options, {
|
|
72
|
-
platform: process.platform,
|
|
73
|
-
env,
|
|
74
|
-
execPath: process.execPath
|
|
75
|
-
});
|
|
76
|
-
return spawn(invocation.command, invocation.args, {
|
|
77
|
-
env,
|
|
78
|
-
detached: process.platform !== "win32",
|
|
79
|
-
shell: invocation.shell,
|
|
80
|
-
stdio: [
|
|
81
|
-
"pipe",
|
|
82
|
-
"pipe",
|
|
83
|
-
"pipe"
|
|
84
|
-
],
|
|
85
|
-
windowsHide: invocation.windowsHide
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
//#endregion
|
|
89
|
-
//#region extensions/codex/src/app-server/transport-websocket.ts
|
|
90
|
-
/**
|
|
91
|
-
* Adapts a remote Codex app-server WebSocket endpoint to the shared stdio-like
|
|
92
|
-
* transport interface.
|
|
93
|
-
*/
|
|
94
|
-
/** Opens a WebSocket app-server transport and maps newline-delimited frames to stdout/stdin. */
|
|
95
|
-
function createWebSocketTransport(options) {
|
|
96
|
-
if (!options.url) throw new Error("codex app-server websocket transport requires plugins.entries.codex.config.appServer.url");
|
|
97
|
-
const events = new EventEmitter();
|
|
98
|
-
const stdout = new PassThrough();
|
|
99
|
-
const stderr = new PassThrough();
|
|
100
|
-
const headers = {
|
|
101
|
-
...options.headers,
|
|
102
|
-
...options.authToken ? { Authorization: `Bearer ${options.authToken}` } : {}
|
|
103
|
-
};
|
|
104
|
-
const socket = new WebSocket(options.url, { headers });
|
|
105
|
-
const pendingFrames = [];
|
|
106
|
-
let killed = false;
|
|
107
|
-
const sendFrame = (frame) => {
|
|
108
|
-
const trimmed = frame.trim();
|
|
109
|
-
if (!trimmed) return;
|
|
110
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
111
|
-
socket.send(trimmed);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
pendingFrames.push(trimmed);
|
|
115
|
-
};
|
|
116
|
-
socket.once("open", () => {
|
|
117
|
-
for (const frame of pendingFrames.splice(0)) socket.send(frame);
|
|
118
|
-
});
|
|
119
|
-
socket.once("error", (error) => events.emit("error", error));
|
|
120
|
-
socket.once("close", (code, reason) => {
|
|
121
|
-
killed = true;
|
|
122
|
-
events.emit("exit", code, reason.toString("utf8"));
|
|
123
|
-
});
|
|
124
|
-
socket.on("message", (data) => {
|
|
125
|
-
const text = websocketFrameToText(data);
|
|
126
|
-
stdout.write(text.endsWith("\n") ? text : `${text}\n`);
|
|
127
|
-
});
|
|
128
|
-
const stdin = new Writable({ write(chunk, _encoding, callback) {
|
|
129
|
-
for (const frame of chunk.toString("utf8").split("\n")) sendFrame(frame);
|
|
130
|
-
callback();
|
|
131
|
-
} });
|
|
132
|
-
const closeSocket = () => {
|
|
133
|
-
if (socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING) return;
|
|
134
|
-
socket.close();
|
|
135
|
-
};
|
|
136
|
-
stdin.once("finish", closeSocket);
|
|
137
|
-
stdin.once("close", closeSocket);
|
|
138
|
-
return {
|
|
139
|
-
stdin,
|
|
140
|
-
stdout,
|
|
141
|
-
stderr,
|
|
142
|
-
get killed() {
|
|
143
|
-
return killed;
|
|
144
|
-
},
|
|
145
|
-
kill: () => {
|
|
146
|
-
killed = true;
|
|
147
|
-
socket.close();
|
|
148
|
-
},
|
|
149
|
-
once: (event, listener) => events.once(event, listener)
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
function websocketFrameToText(data) {
|
|
153
|
-
if (typeof data === "string") return data;
|
|
154
|
-
if (Buffer.isBuffer(data)) return data.toString("utf8");
|
|
155
|
-
if (Array.isArray(data)) return Buffer.concat(data).toString("utf8");
|
|
156
|
-
return Buffer.from(data).toString("utf8");
|
|
157
|
-
}
|
|
158
|
-
//#endregion
|
|
159
|
-
//#region extensions/codex/src/app-server/transport.ts
|
|
160
|
-
/** Starts graceful transport shutdown and schedules a force kill fallback. */
|
|
161
|
-
function closeCodexAppServerTransport(child, options = {}) {
|
|
162
|
-
child.stdin.end?.();
|
|
163
|
-
child.stdin.destroy?.();
|
|
164
|
-
const forceKillDelayMs = options.forceKillDelayMs ?? 1e3;
|
|
165
|
-
const forceKill = setTimeout(() => {
|
|
166
|
-
if (hasCodexAppServerTransportExited(child)) return;
|
|
167
|
-
signalCodexAppServerTransport(child, "SIGKILL");
|
|
168
|
-
}, Math.max(1, forceKillDelayMs));
|
|
169
|
-
forceKill.unref?.();
|
|
170
|
-
child.once("exit", () => {
|
|
171
|
-
clearTimeout(forceKill);
|
|
172
|
-
child.stdout.destroy?.();
|
|
173
|
-
child.stderr.destroy?.();
|
|
174
|
-
});
|
|
175
|
-
child.unref?.();
|
|
176
|
-
child.stdout.unref?.();
|
|
177
|
-
child.stderr.unref?.();
|
|
178
|
-
child.stdin.unref?.();
|
|
179
|
-
}
|
|
180
|
-
/** Closes a transport and waits briefly for an exit event. */
|
|
181
|
-
async function closeCodexAppServerTransportAndWait(child, options = {}) {
|
|
182
|
-
if (!hasCodexAppServerTransportExited(child)) closeCodexAppServerTransport(child, options);
|
|
183
|
-
return await waitForCodexAppServerTransportExit(child, options.exitTimeoutMs ?? 2e3);
|
|
184
|
-
}
|
|
185
|
-
function hasCodexAppServerTransportExited(child) {
|
|
186
|
-
return child.exitCode !== null && child.exitCode !== void 0 ? true : child.signalCode !== null && child.signalCode !== void 0;
|
|
187
|
-
}
|
|
188
|
-
async function waitForCodexAppServerTransportExit(child, timeoutMs) {
|
|
189
|
-
if (hasCodexAppServerTransportExited(child)) return true;
|
|
190
|
-
return await new Promise((resolve) => {
|
|
191
|
-
let settled = false;
|
|
192
|
-
const onExit = () => {
|
|
193
|
-
if (settled) return;
|
|
194
|
-
settled = true;
|
|
195
|
-
clearTimeout(timeout);
|
|
196
|
-
resolve(true);
|
|
197
|
-
};
|
|
198
|
-
const timeout = setTimeout(() => {
|
|
199
|
-
if (settled) return;
|
|
200
|
-
settled = true;
|
|
201
|
-
child.off?.("exit", onExit);
|
|
202
|
-
resolve(false);
|
|
203
|
-
}, Math.max(1, timeoutMs));
|
|
204
|
-
child.once("exit", onExit);
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
function signalCodexAppServerTransport(child, signal) {
|
|
208
|
-
if (child.pid && process.platform !== "win32") try {
|
|
209
|
-
process.kill(-child.pid, signal);
|
|
210
|
-
return;
|
|
211
|
-
} catch {}
|
|
212
|
-
child.kill?.(signal);
|
|
213
|
-
}
|
|
214
|
-
//#endregion
|
|
215
|
-
//#region extensions/codex/src/app-server/version.ts
|
|
216
|
-
/**
|
|
217
|
-
* Version and package pins for the managed Codex app-server runtime.
|
|
218
|
-
*/
|
|
219
|
-
/** Minimum Codex app-server version supported by the OpenClaw Codex bridge. */
|
|
220
|
-
const MIN_CODEX_APP_SERVER_VERSION = "0.125.0";
|
|
221
|
-
/** Minimum Codex app-server version that supports sandbox exec-server environments. */
|
|
222
|
-
const MIN_CODEX_SANDBOX_EXEC_SERVER_APP_SERVER_VERSION = "0.132.0";
|
|
223
|
-
/** npm package name for the managed Codex app-server binary. */
|
|
224
|
-
const MANAGED_CODEX_APP_SERVER_PACKAGE = "@openai/codex";
|
|
225
|
-
//#endregion
|
|
226
|
-
//#region extensions/codex/src/app-server/client.ts
|
|
227
|
-
/**
|
|
228
|
-
* JSON-RPC client for Codex app-server transports, including request/response
|
|
229
|
-
* routing, notification fanout, server request handlers, and version checks.
|
|
230
|
-
*/
|
|
231
|
-
const CODEX_APP_SERVER_PARSE_LOG_MAX = 500;
|
|
232
|
-
const CODEX_APP_SERVER_PARSE_BUFFER_MAX = 1e6;
|
|
233
|
-
const CODEX_APP_SERVER_PARSE_BUFFER_MAX_LINES = 1e3;
|
|
234
|
-
const CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS = 6e5;
|
|
235
|
-
const CODEX_APP_SERVER_STDERR_TAIL_MAX = 2e3;
|
|
236
|
-
const UNPAIRED_SURROGATE_RE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
|
237
|
-
/** RPC error wrapper that preserves app-server error code and data. */
|
|
238
|
-
var CodexAppServerRpcError = class extends Error {
|
|
239
|
-
constructor(error, method) {
|
|
240
|
-
super(formatCodexAppServerRpcErrorMessage(error, method));
|
|
241
|
-
this.name = "CodexAppServerRpcError";
|
|
242
|
-
this.code = error.code;
|
|
243
|
-
this.data = error.data;
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
function formatCodexAppServerRpcErrorMessage(error, method) {
|
|
247
|
-
const message = error.message || `${method} failed`;
|
|
248
|
-
const detail = readCodexAppServerRpcReloginDetail(error.data);
|
|
249
|
-
return detail && !message.includes(detail) ? `${message}: ${detail}` : message;
|
|
250
|
-
}
|
|
251
|
-
function readCodexAppServerRpcReloginDetail(data) {
|
|
252
|
-
const record = isJsonObject(data) ? data : void 0;
|
|
253
|
-
const nested = isJsonObject(record?.error) ? record.error : record;
|
|
254
|
-
if (!nested) return;
|
|
255
|
-
const isRelogin = nested.action === "relogin" || nested.reason === "cloudRequirements" && nested.errorCode === "Auth";
|
|
256
|
-
const detail = typeof nested.detail === "string" ? nested.detail.trim() : "";
|
|
257
|
-
return isRelogin && detail ? detail : void 0;
|
|
258
|
-
}
|
|
259
|
-
function isJsonObject(value) {
|
|
260
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
261
|
-
}
|
|
262
|
-
/** Returns true for errors that mean the app-server transport is closed. */
|
|
263
|
-
function isCodexAppServerConnectionClosedError(error) {
|
|
264
|
-
if (!(error instanceof Error)) return false;
|
|
265
|
-
return error.message === "codex app-server client is closed" || error.message.startsWith("codex app-server exited:");
|
|
266
|
-
}
|
|
267
|
-
/** Stateful app-server JSON-RPC client over stdio or websocket transport. */
|
|
268
|
-
var CodexAppServerClient = class CodexAppServerClient {
|
|
269
|
-
constructor(child) {
|
|
270
|
-
this.pending = /* @__PURE__ */ new Map();
|
|
271
|
-
this.requestHandlers = /* @__PURE__ */ new Set();
|
|
272
|
-
this.notificationHandlers = /* @__PURE__ */ new Set();
|
|
273
|
-
this.closeHandlers = /* @__PURE__ */ new Set();
|
|
274
|
-
this.nextId = 1;
|
|
275
|
-
this.initialized = false;
|
|
276
|
-
this.closed = false;
|
|
277
|
-
this.stderrTail = "";
|
|
278
|
-
this.child = child;
|
|
279
|
-
this.lines = createInterface({ input: child.stdout });
|
|
280
|
-
this.lines.on("line", (line) => this.handleLine(line));
|
|
281
|
-
child.stderr.on("data", (chunk) => {
|
|
282
|
-
const text = chunk.toString("utf8");
|
|
283
|
-
this.stderrTail = appendBoundedTail(this.stderrTail, text, CODEX_APP_SERVER_STDERR_TAIL_MAX);
|
|
284
|
-
const trimmed = text.trim();
|
|
285
|
-
if (trimmed) embeddedAgentLog.debug(`codex app-server stderr: ${trimmed}`);
|
|
286
|
-
});
|
|
287
|
-
child.once("error", (error) => this.closeWithError(error instanceof Error ? error : new Error(String(error))));
|
|
288
|
-
child.once("exit", (code, signal) => {
|
|
289
|
-
this.closeWithError(buildCodexAppServerExitError(code, signal, this.stderrTail));
|
|
290
|
-
});
|
|
291
|
-
child.stdin.on?.("error", (error) => this.closeWithError(error instanceof Error ? error : new Error(String(error))));
|
|
292
|
-
}
|
|
293
|
-
/** Starts a new app-server client using resolved runtime start options. */
|
|
294
|
-
static start(options) {
|
|
295
|
-
const defaults = resolveCodexAppServerRuntimeOptions().start;
|
|
296
|
-
const startOptions = {
|
|
297
|
-
...defaults,
|
|
298
|
-
...options,
|
|
299
|
-
headers: options?.headers ?? defaults.headers
|
|
300
|
-
};
|
|
301
|
-
if (startOptions.transport === "stdio" && startOptions.commandSource === "managed") throw new Error("Managed Codex app-server start options must be resolved before spawn.");
|
|
302
|
-
if (startOptions.transport === "websocket") return new CodexAppServerClient(createWebSocketTransport(startOptions));
|
|
303
|
-
return new CodexAppServerClient(createStdioTransport(startOptions));
|
|
304
|
-
}
|
|
305
|
-
/** Builds a client around a fake transport for tests. */
|
|
306
|
-
static fromTransportForTests(child) {
|
|
307
|
-
return new CodexAppServerClient(child);
|
|
308
|
-
}
|
|
309
|
-
/** Performs the app-server initialize handshake and validates protocol version. */
|
|
310
|
-
async initialize() {
|
|
311
|
-
if (this.initialized) return;
|
|
312
|
-
const response = await this.request("initialize", {
|
|
313
|
-
clientInfo: {
|
|
314
|
-
name: "openclaw",
|
|
315
|
-
title: "OpenClaw",
|
|
316
|
-
version: OPENCLAW_VERSION
|
|
317
|
-
},
|
|
318
|
-
capabilities: { experimentalApi: true }
|
|
319
|
-
});
|
|
320
|
-
this.serverVersion = assertSupportedCodexAppServerVersion(response);
|
|
321
|
-
this.runtimeIdentity = buildCodexAppServerRuntimeIdentity(response, this.serverVersion);
|
|
322
|
-
this.notify("initialized");
|
|
323
|
-
this.initialized = true;
|
|
324
|
-
}
|
|
325
|
-
/** Returns the version detected during initialize. */
|
|
326
|
-
getServerVersion() {
|
|
327
|
-
return this.serverVersion;
|
|
328
|
-
}
|
|
329
|
-
/** Returns runtime metadata detected during initialize. */
|
|
330
|
-
getRuntimeIdentity() {
|
|
331
|
-
return this.runtimeIdentity ? { ...this.runtimeIdentity } : void 0;
|
|
332
|
-
}
|
|
333
|
-
request(method, params, optionsInput) {
|
|
334
|
-
let options = optionsInput;
|
|
335
|
-
options ??= {};
|
|
336
|
-
if (this.closed) return Promise.reject(this.closeError ?? /* @__PURE__ */ new Error("codex app-server client is closed"));
|
|
337
|
-
if (options.signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(`${method} aborted`));
|
|
338
|
-
const id = this.nextId++;
|
|
339
|
-
const message = {
|
|
340
|
-
id,
|
|
341
|
-
method,
|
|
342
|
-
params
|
|
343
|
-
};
|
|
344
|
-
return new Promise((resolve, reject) => {
|
|
345
|
-
let timeout;
|
|
346
|
-
let cleanupAbort;
|
|
347
|
-
const cleanup = () => {
|
|
348
|
-
if (timeout) {
|
|
349
|
-
clearTimeout(timeout);
|
|
350
|
-
timeout = void 0;
|
|
351
|
-
}
|
|
352
|
-
cleanupAbort?.();
|
|
353
|
-
cleanupAbort = void 0;
|
|
354
|
-
};
|
|
355
|
-
const rejectPending = (error) => {
|
|
356
|
-
if (!this.pending.has(id)) return;
|
|
357
|
-
this.pending.delete(id);
|
|
358
|
-
cleanup();
|
|
359
|
-
reject(error);
|
|
360
|
-
};
|
|
361
|
-
if (options.timeoutMs && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0) {
|
|
362
|
-
timeout = setTimeout(() => rejectPending(/* @__PURE__ */ new Error(`${method} timed out`)), Math.max(100, options.timeoutMs));
|
|
363
|
-
timeout.unref?.();
|
|
364
|
-
}
|
|
365
|
-
if (options.signal) {
|
|
366
|
-
const abortListener = () => rejectPending(/* @__PURE__ */ new Error(`${method} aborted`));
|
|
367
|
-
options.signal.addEventListener("abort", abortListener, { once: true });
|
|
368
|
-
cleanupAbort = () => options.signal?.removeEventListener("abort", abortListener);
|
|
369
|
-
}
|
|
370
|
-
this.pending.set(id, {
|
|
371
|
-
method,
|
|
372
|
-
resolve: (value) => {
|
|
373
|
-
cleanup();
|
|
374
|
-
resolve(value);
|
|
375
|
-
},
|
|
376
|
-
reject: (error) => {
|
|
377
|
-
cleanup();
|
|
378
|
-
reject(error);
|
|
379
|
-
},
|
|
380
|
-
cleanup
|
|
381
|
-
});
|
|
382
|
-
if (options.signal?.aborted) {
|
|
383
|
-
rejectPending(/* @__PURE__ */ new Error(`${method} aborted`));
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
try {
|
|
387
|
-
this.writeMessage(message, (error) => rejectPending(error));
|
|
388
|
-
} catch (error) {
|
|
389
|
-
rejectPending(error instanceof Error ? error : new Error(String(error)));
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
/** Sends a fire-and-forget JSON-RPC notification to the app-server. */
|
|
394
|
-
notify(method, params) {
|
|
395
|
-
this.writeMessage({
|
|
396
|
-
method,
|
|
397
|
-
params
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
/** Registers a handler for app-server requests sent back to OpenClaw. */
|
|
401
|
-
addRequestHandler(handler) {
|
|
402
|
-
this.requestHandlers.add(handler);
|
|
403
|
-
return () => this.requestHandlers.delete(handler);
|
|
404
|
-
}
|
|
405
|
-
/** Registers a notification handler and returns its disposer. */
|
|
406
|
-
addNotificationHandler(handler) {
|
|
407
|
-
this.notificationHandlers.add(handler);
|
|
408
|
-
return () => this.notificationHandlers.delete(handler);
|
|
409
|
-
}
|
|
410
|
-
/** Installs a lease-count provider used to route unscoped notifications. */
|
|
411
|
-
setActiveSharedLeaseCountProviderForUnscopedNotifications(provider) {
|
|
412
|
-
this.activeSharedLeaseCountProvider = provider;
|
|
413
|
-
}
|
|
414
|
-
/** Reads the active shared-client lease count when available. */
|
|
415
|
-
getActiveSharedLeaseCountForUnscopedNotifications() {
|
|
416
|
-
return this.activeSharedLeaseCountProvider?.();
|
|
417
|
-
}
|
|
418
|
-
/** Registers a close handler and returns its disposer. */
|
|
419
|
-
addCloseHandler(handler) {
|
|
420
|
-
this.closeHandlers.add(handler);
|
|
421
|
-
return () => this.closeHandlers.delete(handler);
|
|
422
|
-
}
|
|
423
|
-
/** Closes the transport without waiting for process/socket shutdown. */
|
|
424
|
-
close() {
|
|
425
|
-
if (!this.markClosed(/* @__PURE__ */ new Error("codex app-server client is closed"))) return;
|
|
426
|
-
closeCodexAppServerTransport(this.child);
|
|
427
|
-
}
|
|
428
|
-
/** Closes the transport and waits for shutdown according to transport policy. */
|
|
429
|
-
async closeAndWait(options) {
|
|
430
|
-
this.markClosed(/* @__PURE__ */ new Error("codex app-server client is closed"));
|
|
431
|
-
await closeCodexAppServerTransportAndWait(this.child, options);
|
|
432
|
-
}
|
|
433
|
-
writeMessage(message, onError) {
|
|
434
|
-
if (this.closed) return;
|
|
435
|
-
const id = "id" in message ? message.id : void 0;
|
|
436
|
-
const method = "method" in message ? message.method : void 0;
|
|
437
|
-
this.child.stdin.write(`${stringifyCodexAppServerMessage(message)}\n`, (error) => {
|
|
438
|
-
if (error) {
|
|
439
|
-
embeddedAgentLog.warn("codex app-server write failed", {
|
|
440
|
-
error,
|
|
441
|
-
id,
|
|
442
|
-
method
|
|
443
|
-
});
|
|
444
|
-
onError?.(error);
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
handleLine(line) {
|
|
449
|
-
const rawLine = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
450
|
-
if (this.pendingParse) {
|
|
451
|
-
this.handlePendingParseLine(rawLine);
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const trimmed = rawLine.trim();
|
|
455
|
-
if (!trimmed) return;
|
|
456
|
-
let parsed;
|
|
457
|
-
try {
|
|
458
|
-
parsed = JSON.parse(trimmed);
|
|
459
|
-
} catch (error) {
|
|
460
|
-
if (shouldBufferCodexAppServerParseFailure(trimmed, error)) {
|
|
461
|
-
this.pendingParse = {
|
|
462
|
-
text: trimmed,
|
|
463
|
-
lineCount: 1,
|
|
464
|
-
firstError: error
|
|
465
|
-
};
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
logCodexAppServerParseFailure(trimmed, error, 1);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
this.handleParsedMessage(parsed);
|
|
472
|
-
}
|
|
473
|
-
handlePendingParseLine(line) {
|
|
474
|
-
const pending = this.pendingParse;
|
|
475
|
-
if (!pending) return;
|
|
476
|
-
const candidate = `${pending.text}\\n${line}`;
|
|
477
|
-
let parsed;
|
|
478
|
-
try {
|
|
479
|
-
parsed = JSON.parse(candidate);
|
|
480
|
-
} catch (error) {
|
|
481
|
-
const lineCount = pending.lineCount + 1;
|
|
482
|
-
if (shouldBufferCodexAppServerParseFailure(candidate.trim(), error) && candidate.length <= CODEX_APP_SERVER_PARSE_BUFFER_MAX && lineCount <= CODEX_APP_SERVER_PARSE_BUFFER_MAX_LINES) {
|
|
483
|
-
this.pendingParse = {
|
|
484
|
-
text: candidate,
|
|
485
|
-
lineCount,
|
|
486
|
-
firstError: pending.firstError
|
|
487
|
-
};
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
this.pendingParse = void 0;
|
|
491
|
-
logCodexAppServerParseFailure(candidate, error, lineCount);
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
this.pendingParse = void 0;
|
|
495
|
-
this.handleParsedMessage(parsed);
|
|
496
|
-
}
|
|
497
|
-
handleParsedMessage(parsed) {
|
|
498
|
-
if (!parsed || typeof parsed !== "object") return;
|
|
499
|
-
const message = parsed;
|
|
500
|
-
if (isRpcResponse(message)) {
|
|
501
|
-
this.handleResponse(message);
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
if (!("method" in message)) return;
|
|
505
|
-
if ("id" in message && message.id !== void 0) {
|
|
506
|
-
this.handleServerRequest({
|
|
507
|
-
id: message.id,
|
|
508
|
-
method: message.method,
|
|
509
|
-
params: message.params
|
|
510
|
-
});
|
|
511
|
-
return;
|
|
512
|
-
}
|
|
513
|
-
this.handleNotification({
|
|
514
|
-
method: message.method,
|
|
515
|
-
params: message.params
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
handleResponse(response) {
|
|
519
|
-
const pending = this.pending.get(response.id);
|
|
520
|
-
if (!pending) return;
|
|
521
|
-
this.pending.delete(response.id);
|
|
522
|
-
if (response.error) {
|
|
523
|
-
pending.reject(new CodexAppServerRpcError(response.error, pending.method));
|
|
524
|
-
return;
|
|
525
|
-
}
|
|
526
|
-
pending.resolve(response.result);
|
|
527
|
-
}
|
|
528
|
-
async handleServerRequest(request) {
|
|
529
|
-
try {
|
|
530
|
-
const result = await this.runServerRequestHandlers(request);
|
|
531
|
-
if (result !== void 0) {
|
|
532
|
-
this.writeMessage({
|
|
533
|
-
id: request.id,
|
|
534
|
-
result
|
|
535
|
-
});
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
this.writeMessage({
|
|
539
|
-
id: request.id,
|
|
540
|
-
result: defaultServerRequestResponse(request)
|
|
541
|
-
});
|
|
542
|
-
} catch (error) {
|
|
543
|
-
this.writeMessage({
|
|
544
|
-
id: request.id,
|
|
545
|
-
error: { message: error instanceof Error ? error.message : String(error) }
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
async runServerRequestHandlers(request) {
|
|
550
|
-
const timeoutResponse = timeoutServerRequestResponse(request);
|
|
551
|
-
if (!timeoutResponse) return await this.runServerRequestHandlersWithoutTimeout(request);
|
|
552
|
-
let timeout;
|
|
553
|
-
try {
|
|
554
|
-
return await Promise.race([this.runServerRequestHandlersWithoutTimeout(request), new Promise((resolve) => {
|
|
555
|
-
timeout = setTimeout(() => {
|
|
556
|
-
embeddedAgentLog.warn("codex app-server server request timed out", {
|
|
557
|
-
id: request.id,
|
|
558
|
-
method: request.method,
|
|
559
|
-
timeoutMs: CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS
|
|
560
|
-
});
|
|
561
|
-
resolve(timeoutResponse);
|
|
562
|
-
}, CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS);
|
|
563
|
-
timeout.unref?.();
|
|
564
|
-
})]);
|
|
565
|
-
} finally {
|
|
566
|
-
if (timeout) clearTimeout(timeout);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
async runServerRequestHandlersWithoutTimeout(request) {
|
|
570
|
-
for (const handler of this.requestHandlers) {
|
|
571
|
-
const result = await handler(request);
|
|
572
|
-
if (result !== void 0) return result;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
handleNotification(notification) {
|
|
576
|
-
for (const handler of this.notificationHandlers) Promise.resolve(handler(notification)).catch((error) => {
|
|
577
|
-
embeddedAgentLog.warn("codex app-server notification handler failed", { error });
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
closeWithError(error) {
|
|
581
|
-
if (this.markClosed(error)) closeCodexAppServerTransport(this.child);
|
|
582
|
-
}
|
|
583
|
-
markClosed(error) {
|
|
584
|
-
if (this.closed) return false;
|
|
585
|
-
this.closed = true;
|
|
586
|
-
this.closeError = error;
|
|
587
|
-
this.lines.close();
|
|
588
|
-
this.rejectPendingRequests(error);
|
|
589
|
-
return true;
|
|
590
|
-
}
|
|
591
|
-
rejectPendingRequests(error) {
|
|
592
|
-
for (const pending of this.pending.values()) {
|
|
593
|
-
pending.cleanup();
|
|
594
|
-
pending.reject(error);
|
|
595
|
-
}
|
|
596
|
-
this.pending.clear();
|
|
597
|
-
for (const handler of this.closeHandlers) handler(this);
|
|
598
|
-
}
|
|
599
|
-
};
|
|
600
|
-
function defaultServerRequestResponse(request) {
|
|
601
|
-
if (request.method === "item/tool/call") return {
|
|
602
|
-
contentItems: [{
|
|
603
|
-
type: "inputText",
|
|
604
|
-
text: "OpenClaw did not register a handler for this app-server tool call."
|
|
605
|
-
}],
|
|
606
|
-
success: false
|
|
607
|
-
};
|
|
608
|
-
if (request.method === "item/commandExecution/requestApproval" || request.method === "item/fileChange/requestApproval") return { decision: "decline" };
|
|
609
|
-
if (request.method === "item/permissions/requestApproval") return {
|
|
610
|
-
permissions: {},
|
|
611
|
-
scope: "turn"
|
|
612
|
-
};
|
|
613
|
-
if (isCodexAppServerApprovalRequest(request.method)) return {
|
|
614
|
-
decision: "decline",
|
|
615
|
-
reason: "OpenClaw codex app-server bridge does not grant native approvals yet."
|
|
616
|
-
};
|
|
617
|
-
if (request.method === "item/tool/requestUserInput") return { answers: {} };
|
|
618
|
-
if (request.method === "mcpServer/elicitation/request") return { action: "decline" };
|
|
619
|
-
return {};
|
|
620
|
-
}
|
|
621
|
-
function stringifyCodexAppServerMessage(message) {
|
|
622
|
-
return JSON.stringify(message, (_key, value) => typeof value === "string" ? value.replace(UNPAIRED_SURROGATE_RE, "") : value) ?? "null";
|
|
623
|
-
}
|
|
624
|
-
function timeoutServerRequestResponse(request) {
|
|
625
|
-
if (request.method !== "item/tool/call") return;
|
|
626
|
-
return {
|
|
627
|
-
contentItems: [{
|
|
628
|
-
type: "inputText",
|
|
629
|
-
text: `OpenClaw dynamic tool call timed out after ${CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS}ms before sending a response to Codex.`
|
|
630
|
-
}],
|
|
631
|
-
success: false
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
function assertSupportedCodexAppServerVersion(response) {
|
|
635
|
-
const detectedVersion = readCodexVersionFromUserAgent(response.userAgent);
|
|
636
|
-
if (!detectedVersion) throw new Error(`Codex app-server ${MIN_CODEX_APP_SERVER_VERSION} or newer is required, but OpenClaw could not determine the running Codex version. Update the configured Codex app-server binary, or remove custom command overrides to use the managed binary.`);
|
|
637
|
-
if (compareCodexAppServerVersions(detectedVersion, "0.125.0") < 0) throw new Error(`Codex app-server ${MIN_CODEX_APP_SERVER_VERSION} or newer is required, but detected ${detectedVersion}. Update the configured Codex app-server binary, or remove custom command overrides to use the managed binary.`);
|
|
638
|
-
return detectedVersion;
|
|
639
|
-
}
|
|
640
|
-
function buildCodexAppServerRuntimeIdentity(response, serverVersion) {
|
|
641
|
-
const userAgent = readNonEmptyInitializeString(response.userAgent);
|
|
642
|
-
const codexHome = readNonEmptyInitializeString(response.codexHome);
|
|
643
|
-
const platformFamily = readNonEmptyInitializeString(response.platformFamily);
|
|
644
|
-
const platformOs = readNonEmptyInitializeString(response.platformOs);
|
|
645
|
-
return {
|
|
646
|
-
serverVersion,
|
|
647
|
-
...userAgent ? { userAgent } : {},
|
|
648
|
-
...codexHome ? { codexHome } : {},
|
|
649
|
-
...platformFamily ? { platformFamily } : {},
|
|
650
|
-
...platformOs ? { platformOs } : {}
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
function readNonEmptyInitializeString(value) {
|
|
654
|
-
const trimmed = value?.trim();
|
|
655
|
-
return trimmed ? trimmed : void 0;
|
|
656
|
-
}
|
|
657
|
-
/** Extracts the Codex version from the app-server initialize user-agent field. */
|
|
658
|
-
function readCodexVersionFromUserAgent(userAgent) {
|
|
659
|
-
return (userAgent?.match(/^[^/]+\/(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)(?:[\s(]|$)/))?.[1];
|
|
660
|
-
}
|
|
661
|
-
/** Compares stable Codex app-server versions for protocol floor checks. */
|
|
662
|
-
function compareCodexAppServerVersions(left, right) {
|
|
663
|
-
const leftVersion = parseVersionForComparison(left);
|
|
664
|
-
const rightVersion = parseVersionForComparison(right);
|
|
665
|
-
const leftParts = leftVersion.parts;
|
|
666
|
-
const rightParts = rightVersion.parts;
|
|
667
|
-
for (let index = 0; index < Math.max(leftParts.length, rightParts.length); index += 1) {
|
|
668
|
-
const leftPart = leftParts[index] ?? 0;
|
|
669
|
-
const rightPart = rightParts[index] ?? 0;
|
|
670
|
-
if (leftPart !== rightPart) return leftPart < rightPart ? -1 : 1;
|
|
671
|
-
}
|
|
672
|
-
if (leftVersion.unstableSuffix && !rightVersion.unstableSuffix) return -1;
|
|
673
|
-
if (!leftVersion.unstableSuffix && rightVersion.unstableSuffix) return 1;
|
|
674
|
-
return 0;
|
|
675
|
-
}
|
|
676
|
-
function parseVersionForComparison(version) {
|
|
677
|
-
const hasBuildMetadata = version.includes("+");
|
|
678
|
-
const [withoutBuild = version] = version.split("+", 1);
|
|
679
|
-
const prereleaseIndex = withoutBuild.indexOf("-");
|
|
680
|
-
return {
|
|
681
|
-
parts: (prereleaseIndex >= 0 ? withoutBuild.slice(0, prereleaseIndex) : withoutBuild).split(".").map((part) => Number.parseInt(part, 10)).map((part) => Number.isFinite(part) ? part : 0),
|
|
682
|
-
unstableSuffix: prereleaseIndex >= 0 || hasBuildMetadata
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
function redactCodexAppServerLinePreview(value) {
|
|
686
|
-
const redacted = value.replace(/\s+/g, " ").trim().replace(/(Bearer\s+)[A-Za-z0-9._~+/-]+/gi, "$1<redacted>").replace(/("(?:api_?key|authorization|token|access_token|refresh_token)"\s*:\s*")([^"]+)(")/gi, "$1<redacted>$3").replace(/\b([a-z0-9_]*(?:api_?key|authorization|access_token|refresh_token|token))(\s*=\s*)(["']?)[^\s"']+(\3)/gi, "$1$2$3<redacted>$4");
|
|
687
|
-
return redacted.length > CODEX_APP_SERVER_PARSE_LOG_MAX ? `${redacted.slice(0, CODEX_APP_SERVER_PARSE_LOG_MAX)}...` : redacted;
|
|
688
|
-
}
|
|
689
|
-
function appendBoundedTail(current, next, maxLength) {
|
|
690
|
-
const combined = `${current}${next}`;
|
|
691
|
-
return combined.length > maxLength ? combined.slice(combined.length - maxLength) : combined;
|
|
692
|
-
}
|
|
693
|
-
function buildCodexAppServerExitError(code, signal, stderrTail) {
|
|
694
|
-
const stderrPreview = redactCodexAppServerLinePreview(stderrTail);
|
|
695
|
-
const suffix = stderrPreview ? ` stderr=${JSON.stringify(stderrPreview)}` : "";
|
|
696
|
-
return /* @__PURE__ */ new Error(`codex app-server exited: code=${formatExitValue(code)} signal=${formatExitValue(signal)}${suffix}`);
|
|
697
|
-
}
|
|
698
|
-
function shouldBufferCodexAppServerParseFailure(value, error) {
|
|
699
|
-
if (!value.startsWith("{") && !value.startsWith("[")) return false;
|
|
700
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
701
|
-
return message.includes("Unterminated string") || message.includes("Unexpected end of JSON input");
|
|
702
|
-
}
|
|
703
|
-
function logCodexAppServerParseFailure(value, error, fragmentCount) {
|
|
704
|
-
const linePreview = redactCodexAppServerLinePreview(value);
|
|
705
|
-
const suffix = fragmentCount > 1 ? ` fragments=${fragmentCount}` : "";
|
|
706
|
-
embeddedAgentLog.warn("failed to parse codex app-server message", {
|
|
707
|
-
error,
|
|
708
|
-
errorMessage: error instanceof Error ? error.message : String(error),
|
|
709
|
-
fragmentCount,
|
|
710
|
-
linePreview,
|
|
711
|
-
consoleMessage: `failed to parse codex app-server message${suffix}: preview=${JSON.stringify(linePreview)}`
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
const CODEX_APP_SERVER_APPROVAL_REQUEST_METHODS = new Set([
|
|
715
|
-
"item/commandExecution/requestApproval",
|
|
716
|
-
"item/fileChange/requestApproval",
|
|
717
|
-
"item/permissions/requestApproval"
|
|
718
|
-
]);
|
|
719
|
-
/** Returns true for app-server approval request methods OpenClaw can answer. */
|
|
720
|
-
function isCodexAppServerApprovalRequest(method) {
|
|
721
|
-
return CODEX_APP_SERVER_APPROVAL_REQUEST_METHODS.has(method);
|
|
722
|
-
}
|
|
723
|
-
function formatExitValue(value) {
|
|
724
|
-
if (value === null || value === void 0) return "null";
|
|
725
|
-
if (typeof value === "string" || typeof value === "number") return String(value);
|
|
726
|
-
return "unknown";
|
|
727
|
-
}
|
|
728
|
-
//#endregion
|
|
729
|
-
export { isCodexAppServerConnectionClosedError as a, resolveCodexAppServerSpawnEnv as c, isCodexAppServerApprovalRequest as i, CodexAppServerRpcError as n, MANAGED_CODEX_APP_SERVER_PACKAGE as o, compareCodexAppServerVersions as r, MIN_CODEX_SANDBOX_EXEC_SERVER_APP_SERVER_VERSION as s, CodexAppServerClient as t };
|