@gakr-gakr/codex 0.1.0 → 0.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.
Files changed (115) hide show
  1. package/dist/client-DNN2uyJW.js +642 -0
  2. package/dist/client-factory-Bu9OClHJ.js +9 -0
  3. package/dist/command-formatters-BpPOTePl.js +520 -0
  4. package/dist/command-handlers-BBs7Vws9.js +1533 -0
  5. package/dist/compact-CDboBy7o.js +329 -0
  6. package/dist/computer-use-DCZB46Sw.js +367 -0
  7. package/dist/config-CLMSw0p2.js +510 -0
  8. package/dist/doctor-contract-api.js +53 -0
  9. package/dist/harness.js +51 -0
  10. package/dist/index.js +1171 -0
  11. package/dist/media-understanding-provider.js +335 -0
  12. package/dist/models-jLA2SIvd.js +110 -0
  13. package/dist/node-cli-sessions-BLRDs_US.js +1216 -0
  14. package/dist/plugin-activation-CEy_oYpx.js +452 -0
  15. package/dist/prompt-overlay.js +12 -0
  16. package/dist/protocol-C9UWI98H.js +9 -0
  17. package/dist/protocol-validators-BGBspNmF.js +5988 -0
  18. package/dist/provider-catalog.js +84 -0
  19. package/dist/provider-discovery.js +33 -0
  20. package/dist/provider.js +150 -0
  21. package/dist/rate-limit-cache-9LxQdE0K.js +24 -0
  22. package/dist/request-DbSPeTcV.js +89 -0
  23. package/dist/rolldown-runtime-DUslC3ob.js +14 -0
  24. package/dist/run-attempt-BoEwzQCv.js +5463 -0
  25. package/dist/session-binding-e2GFp9VH.js +222 -0
  26. package/dist/shared-client-D7Vy0glq.js +631 -0
  27. package/dist/side-question-BDLuEzFP.js +668 -0
  28. package/dist/test-api.js +49 -0
  29. package/dist/thread-lifecycle-Clo0EHMk.js +1565 -0
  30. package/dist/vision-tools-Cofrv35p.js +1379 -0
  31. package/package.json +16 -1
  32. package/doctor-contract-api.ts +0 -68
  33. package/harness.ts +0 -72
  34. package/index.ts +0 -124
  35. package/media-understanding-provider.ts +0 -521
  36. package/prompt-overlay.ts +0 -21
  37. package/provider-catalog.ts +0 -83
  38. package/provider-discovery.ts +0 -45
  39. package/provider.ts +0 -243
  40. package/src/app-server/app-inventory-cache.ts +0 -324
  41. package/src/app-server/approval-bridge.ts +0 -1211
  42. package/src/app-server/auth-bridge.ts +0 -614
  43. package/src/app-server/capabilities.ts +0 -27
  44. package/src/app-server/client-factory.ts +0 -24
  45. package/src/app-server/client.ts +0 -715
  46. package/src/app-server/compact.ts +0 -512
  47. package/src/app-server/computer-use.ts +0 -683
  48. package/src/app-server/config.ts +0 -1038
  49. package/src/app-server/context-engine-projection.ts +0 -403
  50. package/src/app-server/dynamic-tool-diagnostics.ts +0 -73
  51. package/src/app-server/dynamic-tool-profile.ts +0 -70
  52. package/src/app-server/dynamic-tools.ts +0 -623
  53. package/src/app-server/elicitation-bridge.ts +0 -783
  54. package/src/app-server/event-projector.ts +0 -2065
  55. package/src/app-server/image-payload-sanitizer.ts +0 -167
  56. package/src/app-server/local-runtime-attribution.ts +0 -39
  57. package/src/app-server/managed-binary.ts +0 -193
  58. package/src/app-server/models.ts +0 -172
  59. package/src/app-server/native-hook-relay.ts +0 -150
  60. package/src/app-server/native-subagent-task-mirror.ts +0 -497
  61. package/src/app-server/plugin-activation.ts +0 -283
  62. package/src/app-server/plugin-app-cache-key.ts +0 -74
  63. package/src/app-server/plugin-approval-roundtrip.ts +0 -122
  64. package/src/app-server/plugin-inventory.ts +0 -357
  65. package/src/app-server/plugin-thread-config.ts +0 -455
  66. package/src/app-server/protocol-generated/json/DynamicToolCallParams.json +0 -33
  67. package/src/app-server/protocol-generated/json/v2/ErrorNotification.json +0 -199
  68. package/src/app-server/protocol-generated/json/v2/GetAccountResponse.json +0 -102
  69. package/src/app-server/protocol-generated/json/v2/ModelListResponse.json +0 -227
  70. package/src/app-server/protocol-generated/json/v2/ThreadResumeResponse.json +0 -2630
  71. package/src/app-server/protocol-generated/json/v2/ThreadStartResponse.json +0 -2630
  72. package/src/app-server/protocol-generated/json/v2/TurnCompletedNotification.json +0 -1659
  73. package/src/app-server/protocol-generated/json/v2/TurnStartResponse.json +0 -1655
  74. package/src/app-server/protocol-validators.ts +0 -203
  75. package/src/app-server/protocol.ts +0 -520
  76. package/src/app-server/rate-limit-cache.ts +0 -48
  77. package/src/app-server/rate-limits.ts +0 -583
  78. package/src/app-server/request.ts +0 -73
  79. package/src/app-server/run-attempt.ts +0 -4862
  80. package/src/app-server/session-binding.ts +0 -398
  81. package/src/app-server/session-history.ts +0 -44
  82. package/src/app-server/shared-client.ts +0 -289
  83. package/src/app-server/side-question.ts +0 -1009
  84. package/src/app-server/test-support.ts +0 -48
  85. package/src/app-server/thread-lifecycle.ts +0 -959
  86. package/src/app-server/timeout.ts +0 -9
  87. package/src/app-server/tool-progress-normalization.ts +0 -77
  88. package/src/app-server/trajectory.ts +0 -368
  89. package/src/app-server/transcript-mirror.ts +0 -208
  90. package/src/app-server/transport-stdio.ts +0 -107
  91. package/src/app-server/transport-websocket.ts +0 -90
  92. package/src/app-server/transport.ts +0 -117
  93. package/src/app-server/user-input-bridge.ts +0 -316
  94. package/src/app-server/version.ts +0 -4
  95. package/src/app-server/vision-tools.ts +0 -12
  96. package/src/command-account.ts +0 -544
  97. package/src/command-formatters.ts +0 -426
  98. package/src/command-handlers.ts +0 -2021
  99. package/src/command-plugins-management.ts +0 -137
  100. package/src/command-rpc.ts +0 -142
  101. package/src/commands.ts +0 -65
  102. package/src/conversation-binding-data.ts +0 -124
  103. package/src/conversation-binding.ts +0 -561
  104. package/src/conversation-control.ts +0 -303
  105. package/src/conversation-turn-collector.ts +0 -186
  106. package/src/conversation-turn-input.ts +0 -106
  107. package/src/migration/apply.ts +0 -501
  108. package/src/migration/helpers.ts +0 -55
  109. package/src/migration/plan.ts +0 -461
  110. package/src/migration/provider.ts +0 -41
  111. package/src/migration/source.ts +0 -643
  112. package/src/migration/targets.ts +0 -25
  113. package/src/node-cli-sessions.ts +0 -711
  114. package/test-api.ts +0 -95
  115. package/tsconfig.json +0 -16
@@ -0,0 +1,642 @@
1
+ import { c as resolveCodexAppServerRuntimeOptions } from "./config-CLMSw0p2.js";
2
+ import { n as isRpcResponse } from "./protocol-C9UWI98H.js";
3
+ import { createInterface } from "node:readline";
4
+ import { AUTOBOT_VERSION, embeddedAgentLog } from "autobot/plugin-sdk/agent-harness-runtime";
5
+ import { spawn } from "node:child_process";
6
+ import { materializeWindowsSpawnProgram, resolveWindowsSpawnProgram } from "autobot/plugin-sdk/windows-spawn";
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
+ const UNSAFE_ENVIRONMENT_KEYS = new Set([
12
+ "__proto__",
13
+ "constructor",
14
+ "prototype"
15
+ ]);
16
+ const DEFAULT_SPAWN_RUNTIME = {
17
+ platform: process.platform,
18
+ env: process.env,
19
+ execPath: process.execPath
20
+ };
21
+ function resolveCodexAppServerSpawnInvocation(options, runtime = DEFAULT_SPAWN_RUNTIME) {
22
+ if (options.commandSource === "managed") throw new Error("Managed Codex app-server start options must be resolved before spawn.");
23
+ const resolved = materializeWindowsSpawnProgram(resolveWindowsSpawnProgram({
24
+ command: options.command,
25
+ platform: runtime.platform,
26
+ env: runtime.env,
27
+ execPath: runtime.execPath,
28
+ packageName: "@openai/codex"
29
+ }), options.args);
30
+ return {
31
+ command: resolved.command,
32
+ args: resolved.argv,
33
+ shell: resolved.shell,
34
+ windowsHide: resolved.windowsHide
35
+ };
36
+ }
37
+ function resolveCodexAppServerSpawnEnv(options, baseEnv = process.env, platform = process.platform) {
38
+ const env = Object.create(null);
39
+ copySafeEnvironmentEntries(env, baseEnv);
40
+ copySafeEnvironmentEntries(env, options.env ?? {});
41
+ const keysToClear = normalizedEnvironmentKeys(options.clearEnv ?? []);
42
+ if (platform === "win32") {
43
+ const lowerCaseKeysToClear = new Set(keysToClear.map((key) => key.toLowerCase()));
44
+ for (const candidate of Object.keys(env)) if (lowerCaseKeysToClear.has(candidate.toLowerCase())) delete env[candidate];
45
+ } else for (const key of keysToClear) delete env[key];
46
+ return env;
47
+ }
48
+ function normalizedEnvironmentKeys(rawKeys) {
49
+ const keys = [];
50
+ for (const rawKey of rawKeys) {
51
+ const key = rawKey.trim();
52
+ if (key.length > 0) keys.push(key);
53
+ }
54
+ return keys;
55
+ }
56
+ function copySafeEnvironmentEntries(target, source) {
57
+ for (const [key, value] of Object.entries(source)) {
58
+ if (UNSAFE_ENVIRONMENT_KEYS.has(key)) continue;
59
+ target[key] = value;
60
+ }
61
+ }
62
+ function createStdioTransport(options) {
63
+ const env = resolveCodexAppServerSpawnEnv(options);
64
+ const invocation = resolveCodexAppServerSpawnInvocation(options, {
65
+ platform: process.platform,
66
+ env,
67
+ execPath: process.execPath
68
+ });
69
+ return spawn(invocation.command, invocation.args, {
70
+ env,
71
+ detached: process.platform !== "win32",
72
+ shell: invocation.shell,
73
+ stdio: [
74
+ "pipe",
75
+ "pipe",
76
+ "pipe"
77
+ ],
78
+ windowsHide: invocation.windowsHide
79
+ });
80
+ }
81
+ //#endregion
82
+ //#region extensions/codex/src/app-server/transport-websocket.ts
83
+ function createWebSocketTransport(options) {
84
+ if (!options.url) throw new Error("codex app-server websocket transport requires plugins.entries.codex.config.appServer.url");
85
+ const events = new EventEmitter();
86
+ const stdout = new PassThrough();
87
+ const stderr = new PassThrough();
88
+ const headers = {
89
+ ...options.headers,
90
+ ...options.authToken ? { Authorization: `Bearer ${options.authToken}` } : {}
91
+ };
92
+ const socket = new WebSocket(options.url, { headers });
93
+ const pendingFrames = [];
94
+ let killed = false;
95
+ const sendFrame = (frame) => {
96
+ const trimmed = frame.trim();
97
+ if (!trimmed) return;
98
+ if (socket.readyState === WebSocket.OPEN) {
99
+ socket.send(trimmed);
100
+ return;
101
+ }
102
+ pendingFrames.push(trimmed);
103
+ };
104
+ socket.once("open", () => {
105
+ for (const frame of pendingFrames.splice(0)) socket.send(frame);
106
+ });
107
+ socket.once("error", (error) => events.emit("error", error));
108
+ socket.once("close", (code, reason) => {
109
+ killed = true;
110
+ events.emit("exit", code, reason.toString("utf8"));
111
+ });
112
+ socket.on("message", (data) => {
113
+ const text = websocketFrameToText(data);
114
+ stdout.write(text.endsWith("\n") ? text : `${text}\n`);
115
+ });
116
+ return {
117
+ stdin: new Writable({ write(chunk, _encoding, callback) {
118
+ for (const frame of chunk.toString("utf8").split("\n")) sendFrame(frame);
119
+ callback();
120
+ } }),
121
+ stdout,
122
+ stderr,
123
+ get killed() {
124
+ return killed;
125
+ },
126
+ kill: () => {
127
+ killed = true;
128
+ socket.close();
129
+ },
130
+ once: (event, listener) => events.once(event, listener)
131
+ };
132
+ }
133
+ function websocketFrameToText(data) {
134
+ if (typeof data === "string") return data;
135
+ if (Buffer.isBuffer(data)) return data.toString("utf8");
136
+ if (Array.isArray(data)) return Buffer.concat(data).toString("utf8");
137
+ return Buffer.from(data).toString("utf8");
138
+ }
139
+ //#endregion
140
+ //#region extensions/codex/src/app-server/transport.ts
141
+ function closeCodexAppServerTransport(child, options = {}) {
142
+ child.stdin.end?.();
143
+ child.stdin.destroy?.();
144
+ const forceKillDelayMs = options.forceKillDelayMs ?? 1e3;
145
+ const forceKill = setTimeout(() => {
146
+ if (hasCodexAppServerTransportExited(child)) return;
147
+ signalCodexAppServerTransport(child, "SIGKILL");
148
+ }, Math.max(1, forceKillDelayMs));
149
+ forceKill.unref?.();
150
+ child.once("exit", () => {
151
+ clearTimeout(forceKill);
152
+ child.stdout.destroy?.();
153
+ child.stderr.destroy?.();
154
+ });
155
+ child.unref?.();
156
+ child.stdout.unref?.();
157
+ child.stderr.unref?.();
158
+ child.stdin.unref?.();
159
+ }
160
+ async function closeCodexAppServerTransportAndWait(child, options = {}) {
161
+ if (!hasCodexAppServerTransportExited(child)) closeCodexAppServerTransport(child, options);
162
+ return await waitForCodexAppServerTransportExit(child, options.exitTimeoutMs ?? 2e3);
163
+ }
164
+ function hasCodexAppServerTransportExited(child) {
165
+ return child.exitCode !== null && child.exitCode !== void 0 ? true : child.signalCode !== null && child.signalCode !== void 0;
166
+ }
167
+ async function waitForCodexAppServerTransportExit(child, timeoutMs) {
168
+ if (hasCodexAppServerTransportExited(child)) return true;
169
+ return await new Promise((resolve) => {
170
+ let settled = false;
171
+ const onExit = () => {
172
+ if (settled) return;
173
+ settled = true;
174
+ clearTimeout(timeout);
175
+ resolve(true);
176
+ };
177
+ const timeout = setTimeout(() => {
178
+ if (settled) return;
179
+ settled = true;
180
+ child.off?.("exit", onExit);
181
+ resolve(false);
182
+ }, Math.max(1, timeoutMs));
183
+ child.once("exit", onExit);
184
+ });
185
+ }
186
+ function signalCodexAppServerTransport(child, signal) {
187
+ if (child.pid && process.platform !== "win32") try {
188
+ process.kill(-child.pid, signal);
189
+ return;
190
+ } catch {}
191
+ child.kill?.(signal);
192
+ }
193
+ //#endregion
194
+ //#region extensions/codex/src/app-server/version.ts
195
+ const MIN_CODEX_APP_SERVER_VERSION = "0.125.0";
196
+ const MANAGED_CODEX_APP_SERVER_PACKAGE = "@openai/codex";
197
+ //#endregion
198
+ //#region extensions/codex/src/app-server/client.ts
199
+ const CODEX_APP_SERVER_PARSE_LOG_MAX = 500;
200
+ const CODEX_APP_SERVER_PARSE_BUFFER_MAX = 1e6;
201
+ const CODEX_APP_SERVER_PARSE_BUFFER_MAX_LINES = 1e3;
202
+ const CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS = 6e5;
203
+ const CODEX_APP_SERVER_STDERR_TAIL_MAX = 2e3;
204
+ const UNPAIRED_SURROGATE_RE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
205
+ var CodexAppServerRpcError = class extends Error {
206
+ constructor(error, method) {
207
+ super(formatCodexAppServerRpcErrorMessage(error, method));
208
+ this.name = "CodexAppServerRpcError";
209
+ this.code = error.code;
210
+ this.data = error.data;
211
+ }
212
+ };
213
+ function formatCodexAppServerRpcErrorMessage(error, method) {
214
+ const message = error.message || `${method} failed`;
215
+ const detail = readCodexAppServerRpcReloginDetail(error.data);
216
+ return detail && !message.includes(detail) ? `${message}: ${detail}` : message;
217
+ }
218
+ function readCodexAppServerRpcReloginDetail(data) {
219
+ const record = isJsonObject(data) ? data : void 0;
220
+ const nested = isJsonObject(record?.error) ? record.error : record;
221
+ if (!nested) return;
222
+ const isRelogin = nested.action === "relogin" || nested.reason === "cloudRequirements" && nested.errorCode === "Auth";
223
+ const detail = typeof nested.detail === "string" ? nested.detail.trim() : "";
224
+ return isRelogin && detail ? detail : void 0;
225
+ }
226
+ function isJsonObject(value) {
227
+ return Boolean(value && typeof value === "object" && !Array.isArray(value));
228
+ }
229
+ function isCodexAppServerConnectionClosedError(error) {
230
+ if (!(error instanceof Error)) return false;
231
+ return error.message === "codex app-server client is closed" || error.message.startsWith("codex app-server exited:");
232
+ }
233
+ var CodexAppServerClient = class CodexAppServerClient {
234
+ constructor(child) {
235
+ this.pending = /* @__PURE__ */ new Map();
236
+ this.requestHandlers = /* @__PURE__ */ new Set();
237
+ this.notificationHandlers = /* @__PURE__ */ new Set();
238
+ this.closeHandlers = /* @__PURE__ */ new Set();
239
+ this.nextId = 1;
240
+ this.initialized = false;
241
+ this.closed = false;
242
+ this.stderrTail = "";
243
+ this.child = child;
244
+ this.lines = createInterface({ input: child.stdout });
245
+ this.lines.on("line", (line) => this.handleLine(line));
246
+ child.stderr.on("data", (chunk) => {
247
+ const text = chunk.toString("utf8");
248
+ this.stderrTail = appendBoundedTail(this.stderrTail, text, CODEX_APP_SERVER_STDERR_TAIL_MAX);
249
+ const trimmed = text.trim();
250
+ if (trimmed) embeddedAgentLog.debug(`codex app-server stderr: ${trimmed}`);
251
+ });
252
+ child.once("error", (error) => this.closeWithError(error instanceof Error ? error : new Error(String(error))));
253
+ child.once("exit", (code, signal) => {
254
+ this.closeWithError(buildCodexAppServerExitError(code, signal, this.stderrTail));
255
+ });
256
+ child.stdin.on?.("error", (error) => this.closeWithError(error instanceof Error ? error : new Error(String(error))));
257
+ }
258
+ static start(options) {
259
+ const defaults = resolveCodexAppServerRuntimeOptions().start;
260
+ const startOptions = {
261
+ ...defaults,
262
+ ...options,
263
+ headers: options?.headers ?? defaults.headers
264
+ };
265
+ if (startOptions.transport === "stdio" && startOptions.commandSource === "managed") throw new Error("Managed Codex app-server start options must be resolved before spawn.");
266
+ if (startOptions.transport === "websocket") return new CodexAppServerClient(createWebSocketTransport(startOptions));
267
+ return new CodexAppServerClient(createStdioTransport(startOptions));
268
+ }
269
+ static fromTransportForTests(child) {
270
+ return new CodexAppServerClient(child);
271
+ }
272
+ async initialize() {
273
+ if (this.initialized) return;
274
+ assertSupportedCodexAppServerVersion(await this.request("initialize", {
275
+ clientInfo: {
276
+ name: "autobot",
277
+ title: "AutoBot",
278
+ version: AUTOBOT_VERSION
279
+ },
280
+ capabilities: { experimentalApi: true }
281
+ }));
282
+ this.notify("initialized");
283
+ this.initialized = true;
284
+ }
285
+ request(method, params, options) {
286
+ options ??= {};
287
+ if (this.closed) return Promise.reject(this.closeError ?? /* @__PURE__ */ new Error("codex app-server client is closed"));
288
+ if (options.signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(`${method} aborted`));
289
+ const id = this.nextId++;
290
+ const message = {
291
+ id,
292
+ method,
293
+ params
294
+ };
295
+ return new Promise((resolve, reject) => {
296
+ let timeout;
297
+ let cleanupAbort;
298
+ const cleanup = () => {
299
+ if (timeout) {
300
+ clearTimeout(timeout);
301
+ timeout = void 0;
302
+ }
303
+ cleanupAbort?.();
304
+ cleanupAbort = void 0;
305
+ };
306
+ const rejectPending = (error) => {
307
+ if (!this.pending.has(id)) return;
308
+ this.pending.delete(id);
309
+ cleanup();
310
+ reject(error);
311
+ };
312
+ if (options.timeoutMs && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0) {
313
+ timeout = setTimeout(() => rejectPending(/* @__PURE__ */ new Error(`${method} timed out`)), Math.max(100, options.timeoutMs));
314
+ timeout.unref?.();
315
+ }
316
+ if (options.signal) {
317
+ const abortListener = () => rejectPending(/* @__PURE__ */ new Error(`${method} aborted`));
318
+ options.signal.addEventListener("abort", abortListener, { once: true });
319
+ cleanupAbort = () => options.signal?.removeEventListener("abort", abortListener);
320
+ }
321
+ this.pending.set(id, {
322
+ method,
323
+ resolve: (value) => {
324
+ cleanup();
325
+ resolve(value);
326
+ },
327
+ reject: (error) => {
328
+ cleanup();
329
+ reject(error);
330
+ },
331
+ cleanup
332
+ });
333
+ if (options.signal?.aborted) {
334
+ rejectPending(/* @__PURE__ */ new Error(`${method} aborted`));
335
+ return;
336
+ }
337
+ try {
338
+ this.writeMessage(message);
339
+ } catch (error) {
340
+ rejectPending(error instanceof Error ? error : new Error(String(error)));
341
+ }
342
+ });
343
+ }
344
+ notify(method, params) {
345
+ this.writeMessage({
346
+ method,
347
+ params
348
+ });
349
+ }
350
+ addRequestHandler(handler) {
351
+ this.requestHandlers.add(handler);
352
+ return () => this.requestHandlers.delete(handler);
353
+ }
354
+ addNotificationHandler(handler) {
355
+ this.notificationHandlers.add(handler);
356
+ return () => this.notificationHandlers.delete(handler);
357
+ }
358
+ addCloseHandler(handler) {
359
+ this.closeHandlers.add(handler);
360
+ return () => this.closeHandlers.delete(handler);
361
+ }
362
+ close() {
363
+ if (!this.markClosed(/* @__PURE__ */ new Error("codex app-server client is closed"))) return;
364
+ closeCodexAppServerTransport(this.child);
365
+ }
366
+ async closeAndWait(options) {
367
+ this.markClosed(/* @__PURE__ */ new Error("codex app-server client is closed"));
368
+ await closeCodexAppServerTransportAndWait(this.child, options);
369
+ }
370
+ writeMessage(message) {
371
+ if (this.closed) return;
372
+ const id = "id" in message ? message.id : void 0;
373
+ const method = "method" in message ? message.method : void 0;
374
+ this.child.stdin.write(`${stringifyCodexAppServerMessage(message)}\n`, (error) => {
375
+ if (error) embeddedAgentLog.warn("codex app-server write failed", {
376
+ error,
377
+ id,
378
+ method
379
+ });
380
+ });
381
+ }
382
+ handleLine(line) {
383
+ const rawLine = line.endsWith("\r") ? line.slice(0, -1) : line;
384
+ if (this.pendingParse) {
385
+ this.handlePendingParseLine(rawLine);
386
+ return;
387
+ }
388
+ const trimmed = rawLine.trim();
389
+ if (!trimmed) return;
390
+ let parsed;
391
+ try {
392
+ parsed = JSON.parse(trimmed);
393
+ } catch (error) {
394
+ if (shouldBufferCodexAppServerParseFailure(trimmed, error)) {
395
+ this.pendingParse = {
396
+ text: trimmed,
397
+ lineCount: 1,
398
+ firstError: error
399
+ };
400
+ return;
401
+ }
402
+ logCodexAppServerParseFailure(trimmed, error, 1);
403
+ return;
404
+ }
405
+ this.handleParsedMessage(parsed);
406
+ }
407
+ handlePendingParseLine(line) {
408
+ const pending = this.pendingParse;
409
+ if (!pending) return;
410
+ const candidate = `${pending.text}\\n${line}`;
411
+ let parsed;
412
+ try {
413
+ parsed = JSON.parse(candidate);
414
+ } catch (error) {
415
+ const lineCount = pending.lineCount + 1;
416
+ if (candidate.length <= CODEX_APP_SERVER_PARSE_BUFFER_MAX && lineCount <= CODEX_APP_SERVER_PARSE_BUFFER_MAX_LINES) {
417
+ this.pendingParse = {
418
+ text: candidate,
419
+ lineCount,
420
+ firstError: pending.firstError
421
+ };
422
+ return;
423
+ }
424
+ this.pendingParse = void 0;
425
+ logCodexAppServerParseFailure(candidate, error, lineCount);
426
+ return;
427
+ }
428
+ this.pendingParse = void 0;
429
+ this.handleParsedMessage(parsed);
430
+ }
431
+ handleParsedMessage(parsed) {
432
+ if (!parsed || typeof parsed !== "object") return;
433
+ const message = parsed;
434
+ if (isRpcResponse(message)) {
435
+ this.handleResponse(message);
436
+ return;
437
+ }
438
+ if (!("method" in message)) return;
439
+ if ("id" in message && message.id !== void 0) {
440
+ this.handleServerRequest({
441
+ id: message.id,
442
+ method: message.method,
443
+ params: message.params
444
+ });
445
+ return;
446
+ }
447
+ this.handleNotification({
448
+ method: message.method,
449
+ params: message.params
450
+ });
451
+ }
452
+ handleResponse(response) {
453
+ const pending = this.pending.get(response.id);
454
+ if (!pending) return;
455
+ this.pending.delete(response.id);
456
+ if (response.error) {
457
+ pending.reject(new CodexAppServerRpcError(response.error, pending.method));
458
+ return;
459
+ }
460
+ pending.resolve(response.result);
461
+ }
462
+ async handleServerRequest(request) {
463
+ try {
464
+ const result = await this.runServerRequestHandlers(request);
465
+ if (result !== void 0) {
466
+ this.writeMessage({
467
+ id: request.id,
468
+ result
469
+ });
470
+ return;
471
+ }
472
+ this.writeMessage({
473
+ id: request.id,
474
+ result: defaultServerRequestResponse(request)
475
+ });
476
+ } catch (error) {
477
+ this.writeMessage({
478
+ id: request.id,
479
+ error: { message: error instanceof Error ? error.message : String(error) }
480
+ });
481
+ }
482
+ }
483
+ async runServerRequestHandlers(request) {
484
+ const timeoutResponse = timeoutServerRequestResponse(request);
485
+ if (!timeoutResponse) return await this.runServerRequestHandlersWithoutTimeout(request);
486
+ let timeout;
487
+ try {
488
+ return await Promise.race([this.runServerRequestHandlersWithoutTimeout(request), new Promise((resolve) => {
489
+ timeout = setTimeout(() => {
490
+ embeddedAgentLog.warn("codex app-server server request timed out", {
491
+ id: request.id,
492
+ method: request.method,
493
+ timeoutMs: CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS
494
+ });
495
+ resolve(timeoutResponse);
496
+ }, CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS);
497
+ timeout.unref?.();
498
+ })]);
499
+ } finally {
500
+ if (timeout) clearTimeout(timeout);
501
+ }
502
+ }
503
+ async runServerRequestHandlersWithoutTimeout(request) {
504
+ for (const handler of this.requestHandlers) {
505
+ const result = await handler(request);
506
+ if (result !== void 0) return result;
507
+ }
508
+ }
509
+ handleNotification(notification) {
510
+ for (const handler of this.notificationHandlers) Promise.resolve(handler(notification)).catch((error) => {
511
+ embeddedAgentLog.warn("codex app-server notification handler failed", { error });
512
+ });
513
+ }
514
+ closeWithError(error) {
515
+ if (this.markClosed(error)) closeCodexAppServerTransport(this.child);
516
+ }
517
+ markClosed(error) {
518
+ if (this.closed) return false;
519
+ this.closed = true;
520
+ this.closeError = error;
521
+ this.lines.close();
522
+ this.rejectPendingRequests(error);
523
+ return true;
524
+ }
525
+ rejectPendingRequests(error) {
526
+ for (const pending of this.pending.values()) {
527
+ pending.cleanup();
528
+ pending.reject(error);
529
+ }
530
+ this.pending.clear();
531
+ for (const handler of this.closeHandlers) handler(this);
532
+ }
533
+ };
534
+ function defaultServerRequestResponse(request) {
535
+ if (request.method === "item/tool/call") return {
536
+ contentItems: [{
537
+ type: "inputText",
538
+ text: "AutoBot did not register a handler for this app-server tool call."
539
+ }],
540
+ success: false
541
+ };
542
+ if (request.method === "item/commandExecution/requestApproval" || request.method === "item/fileChange/requestApproval") return { decision: "decline" };
543
+ if (request.method === "item/permissions/requestApproval") return {
544
+ permissions: {},
545
+ scope: "turn"
546
+ };
547
+ if (isCodexAppServerApprovalRequest(request.method)) return {
548
+ decision: "decline",
549
+ reason: "AutoBot codex app-server bridge does not grant native approvals yet."
550
+ };
551
+ if (request.method === "item/tool/requestUserInput") return { answers: {} };
552
+ if (request.method === "mcpServer/elicitation/request") return { action: "decline" };
553
+ return {};
554
+ }
555
+ function stringifyCodexAppServerMessage(message) {
556
+ return JSON.stringify(message, (_key, value) => typeof value === "string" ? value.replace(UNPAIRED_SURROGATE_RE, "") : value) ?? "null";
557
+ }
558
+ function timeoutServerRequestResponse(request) {
559
+ if (request.method !== "item/tool/call") return;
560
+ return {
561
+ contentItems: [{
562
+ type: "inputText",
563
+ text: `AutoBot dynamic tool call timed out after ${CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS}ms before sending a response to Codex.`
564
+ }],
565
+ success: false
566
+ };
567
+ }
568
+ function assertSupportedCodexAppServerVersion(response) {
569
+ const detectedVersion = readCodexVersionFromUserAgent(response.userAgent);
570
+ if (!detectedVersion) throw new Error(`Codex app-server ${MIN_CODEX_APP_SERVER_VERSION} or newer is required, but AutoBot could not determine the running Codex version. Update the configured Codex app-server binary, or remove custom command overrides to use the managed binary.`);
571
+ if (compareVersions(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.`);
572
+ }
573
+ function readCodexVersionFromUserAgent(userAgent) {
574
+ return (userAgent?.match(/^[^/]+\/(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)(?:[\s(]|$)/))?.[1];
575
+ }
576
+ function compareVersions(left, right) {
577
+ const leftVersion = parseVersionForComparison(left);
578
+ const rightVersion = parseVersionForComparison(right);
579
+ const leftParts = leftVersion.parts;
580
+ const rightParts = rightVersion.parts;
581
+ for (let index = 0; index < Math.max(leftParts.length, rightParts.length); index += 1) {
582
+ const leftPart = leftParts[index] ?? 0;
583
+ const rightPart = rightParts[index] ?? 0;
584
+ if (leftPart !== rightPart) return leftPart < rightPart ? -1 : 1;
585
+ }
586
+ if (leftVersion.unstableSuffix && !rightVersion.unstableSuffix) return -1;
587
+ if (!leftVersion.unstableSuffix && rightVersion.unstableSuffix) return 1;
588
+ return 0;
589
+ }
590
+ function parseVersionForComparison(version) {
591
+ const hasBuildMetadata = version.includes("+");
592
+ const [withoutBuild = version] = version.split("+", 1);
593
+ const prereleaseIndex = withoutBuild.indexOf("-");
594
+ return {
595
+ parts: (prereleaseIndex >= 0 ? withoutBuild.slice(0, prereleaseIndex) : withoutBuild).split(".").map((part) => Number.parseInt(part, 10)).map((part) => Number.isFinite(part) ? part : 0),
596
+ unstableSuffix: prereleaseIndex >= 0 || hasBuildMetadata
597
+ };
598
+ }
599
+ function redactCodexAppServerLinePreview(value) {
600
+ 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");
601
+ return redacted.length > CODEX_APP_SERVER_PARSE_LOG_MAX ? `${redacted.slice(0, CODEX_APP_SERVER_PARSE_LOG_MAX)}...` : redacted;
602
+ }
603
+ function appendBoundedTail(current, next, maxLength) {
604
+ const combined = `${current}${next}`;
605
+ return combined.length > maxLength ? combined.slice(combined.length - maxLength) : combined;
606
+ }
607
+ function buildCodexAppServerExitError(code, signal, stderrTail) {
608
+ const stderrPreview = redactCodexAppServerLinePreview(stderrTail);
609
+ const suffix = stderrPreview ? ` stderr=${JSON.stringify(stderrPreview)}` : "";
610
+ return /* @__PURE__ */ new Error(`codex app-server exited: code=${formatExitValue(code)} signal=${formatExitValue(signal)}${suffix}`);
611
+ }
612
+ function shouldBufferCodexAppServerParseFailure(value, error) {
613
+ if (!value.startsWith("{") && !value.startsWith("[")) return false;
614
+ const message = error instanceof Error ? error.message : String(error);
615
+ return message.includes("Unterminated string") || message.includes("Unexpected end of JSON input");
616
+ }
617
+ function logCodexAppServerParseFailure(value, error, fragmentCount) {
618
+ const linePreview = redactCodexAppServerLinePreview(value);
619
+ const suffix = fragmentCount > 1 ? ` fragments=${fragmentCount}` : "";
620
+ embeddedAgentLog.warn("failed to parse codex app-server message", {
621
+ error,
622
+ errorMessage: error instanceof Error ? error.message : String(error),
623
+ fragmentCount,
624
+ linePreview,
625
+ consoleMessage: `failed to parse codex app-server message${suffix}: preview=${JSON.stringify(linePreview)}`
626
+ });
627
+ }
628
+ const CODEX_APP_SERVER_APPROVAL_REQUEST_METHODS = new Set([
629
+ "item/commandExecution/requestApproval",
630
+ "item/fileChange/requestApproval",
631
+ "item/permissions/requestApproval"
632
+ ]);
633
+ function isCodexAppServerApprovalRequest(method) {
634
+ return CODEX_APP_SERVER_APPROVAL_REQUEST_METHODS.has(method);
635
+ }
636
+ function formatExitValue(value) {
637
+ if (value === null || value === void 0) return "null";
638
+ if (typeof value === "string" || typeof value === "number") return String(value);
639
+ return "unknown";
640
+ }
641
+ //#endregion
642
+ export { MANAGED_CODEX_APP_SERVER_PACKAGE as a, isCodexAppServerConnectionClosedError as i, CodexAppServerRpcError as n, resolveCodexAppServerSpawnEnv as o, isCodexAppServerApprovalRequest as r, CodexAppServerClient as t };
@@ -0,0 +1,9 @@
1
+ //#region extensions/codex/src/app-server/client-factory.ts
2
+ const defaultCodexAppServerClientFactory = (startOptions, authProfileId, agentDir, config) => import("./shared-client-D7Vy0glq.js").then((n) => n.a).then(({ getSharedCodexAppServerClient }) => getSharedCodexAppServerClient({
3
+ startOptions,
4
+ authProfileId,
5
+ agentDir,
6
+ config
7
+ }));
8
+ //#endregion
9
+ export { defaultCodexAppServerClientFactory as t };