@ai-sdk/harness-codex 0.0.0-07500555-20260730161446
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/CHANGELOG.md +498 -0
- package/LICENSE +13 -0
- package/README.md +73 -0
- package/dist/bridge/index.mjs +1303 -0
- package/dist/bridge/index.mjs.map +1 -0
- package/dist/bridge/package.json +10 -0
- package/dist/bridge/pnpm-lock.yaml +110 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +894 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
- package/src/bridge/cli-relay.ts +268 -0
- package/src/bridge/codex-step-tracker.ts +83 -0
- package/src/bridge/index.ts +579 -0
- package/src/bridge/package.json +10 -0
- package/src/bridge/pnpm-lock.yaml +110 -0
- package/src/bridge/tool-relay-auth.ts +156 -0
- package/src/bridge/tool-relay.ts +119 -0
- package/src/codex-auth.ts +119 -0
- package/src/codex-bridge-protocol.ts +38 -0
- package/src/codex-harness.ts +1127 -0
- package/src/index.ts +13 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
// Long-running process that runs alongside the `codex` CLI in the sandbox.
|
|
2
|
+
// The generic transport — WebSocket server, token auth, single-flight
|
|
3
|
+
// reconnect, the in-memory event log + `seq`, resume replay, and the
|
|
4
|
+
// lifecycle/meta files — lives in the shared `@ai-sdk/harness/bridge` runtime.
|
|
5
|
+
// This file supplies only the Codex-specific turn driver.
|
|
6
|
+
//
|
|
7
|
+
// Host-defined tools are routed through an HTTP relay bound to
|
|
8
|
+
// `127.0.0.1:0`. The bridge writes a CLI shim into the per-session runtime
|
|
9
|
+
// directory; the shim POSTs each tool call to the relay, which emits
|
|
10
|
+
// `tool-call` to the host and waits for the matching `tool-result`.
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
runBridge,
|
|
14
|
+
type BridgeEvent,
|
|
15
|
+
type BridgeTurn,
|
|
16
|
+
} from '@ai-sdk/harness/bridge';
|
|
17
|
+
import type { HarnessV1BuiltinToolName } from '@ai-sdk/harness';
|
|
18
|
+
import type { StartMessage } from '../codex-bridge-protocol';
|
|
19
|
+
import { randomUUID } from 'node:crypto';
|
|
20
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
21
|
+
// Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts
|
|
22
|
+
import {
|
|
23
|
+
CLI_SHIM_FILENAME,
|
|
24
|
+
buildCliShimScript,
|
|
25
|
+
parseToolRelayCommands,
|
|
26
|
+
} from './cli-relay';
|
|
27
|
+
import {
|
|
28
|
+
createCodexStepTracker,
|
|
29
|
+
defaultUsage,
|
|
30
|
+
type CodexStepTracker,
|
|
31
|
+
} from './codex-step-tracker';
|
|
32
|
+
import { startAuthorizedToolRelay, type ToolRelay } from './tool-relay';
|
|
33
|
+
import { argv, env as procEnv, stdout } from 'node:process';
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* CONSTRAINT — the third-party imports below are NEVER bundled into the
|
|
37
|
+
* compiled `bridge/index.mjs`. They are declared `external` in
|
|
38
|
+
* tsup.config.ts and resolved at runtime from the node_modules that this
|
|
39
|
+
* bridge installs *inside the sandbox* from `src/bridge/package.json` (and
|
|
40
|
+
* its pinned `pnpm-lock.yaml`). That bridge package.json — NOT this host
|
|
41
|
+
* package — is the single source of truth for these packages and their
|
|
42
|
+
* versions; the published `@ai-sdk/harness-codex` package does not provide
|
|
43
|
+
* them at runtime.
|
|
44
|
+
*
|
|
45
|
+
* When adding or changing a third-party import here you MUST keep all three
|
|
46
|
+
* in sync, or the bridge will either get the dependency bundled in or fail
|
|
47
|
+
* to resolve it in the sandbox:
|
|
48
|
+
* 1. the import statement below,
|
|
49
|
+
* 2. the `external` array in tsup.config.ts, and
|
|
50
|
+
* 3. the dependency entry in `src/bridge/package.json`.
|
|
51
|
+
*/
|
|
52
|
+
import * as codexSdkModule from '@openai/codex-sdk';
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* Native Codex tool name → cross-harness common name. Tools outside this map
|
|
56
|
+
* (e.g. MCP tools the model invokes by name) have no common equivalent; their
|
|
57
|
+
* native name is forwarded as-is on `tool-call` events.
|
|
58
|
+
*/
|
|
59
|
+
const NATIVE_TO_COMMON: Readonly<Record<string, HarnessV1BuiltinToolName>> = {
|
|
60
|
+
shell: 'bash',
|
|
61
|
+
web_search: 'webSearch',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function toCommonName(nativeName: string): HarnessV1BuiltinToolName | string {
|
|
65
|
+
return NATIVE_TO_COMMON[nativeName] ?? nativeName;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const args = parseArgs(argv.slice(2));
|
|
69
|
+
const workdir = requireArg({ value: args.workdir, name: '--workdir' });
|
|
70
|
+
const bridgeStateDir = requireArg({
|
|
71
|
+
value: args.bridgeStateDir,
|
|
72
|
+
name: '--bridge-state-dir',
|
|
73
|
+
});
|
|
74
|
+
const cliShimDir = requireArg({
|
|
75
|
+
value: args.cliShimDir,
|
|
76
|
+
name: '--cli-shim-dir',
|
|
77
|
+
});
|
|
78
|
+
const HARNESS_CLIENT_APP = procEnv.AI_SDK_HARNESS_CLIENT_APP;
|
|
79
|
+
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
const codexSdk = codexSdkModule as any;
|
|
82
|
+
|
|
83
|
+
// Codex thread id — survives across turns within this bridge process and is
|
|
84
|
+
// returned to the host on `detach` so a future process can resume the thread.
|
|
85
|
+
const threadState: { id: string | undefined } = { id: undefined };
|
|
86
|
+
|
|
87
|
+
await runBridge<StartMessage>({
|
|
88
|
+
bridgeType: 'codex',
|
|
89
|
+
bridgeStateDir,
|
|
90
|
+
onStart: runTurn,
|
|
91
|
+
onDetach: () => (threadState.id ? { threadId: threadState.id } : {}),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
type Emit = (msg: Record<string, unknown>) => void;
|
|
95
|
+
|
|
96
|
+
async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
97
|
+
const emit: Emit = msg => turn.emit(msg as BridgeEvent);
|
|
98
|
+
|
|
99
|
+
// Cross-process resume: the host carries the threadId we returned on detach.
|
|
100
|
+
// Seed `threadState.id` so the codex SDK call below takes the `resumeThread`
|
|
101
|
+
// branch.
|
|
102
|
+
if (
|
|
103
|
+
typeof start.resumeThreadId === 'string' &&
|
|
104
|
+
start.resumeThreadId.length > 0
|
|
105
|
+
) {
|
|
106
|
+
threadState.id = start.resumeThreadId;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
* Known limitation: codex CLI does not reliably surface MCP tools to the
|
|
111
|
+
* model in `codex exec --experimental-json` mode (the path the
|
|
112
|
+
* `@openai/codex-sdk` uses). Some versions do not register MCP tools at all;
|
|
113
|
+
* others expose the tool names but pass empty arguments.
|
|
114
|
+
*
|
|
115
|
+
* Until that's fixed, host tools are made available to the model via a
|
|
116
|
+
* separate CLI-relay workaround (see `./cli-relay.ts`). Writing the shim
|
|
117
|
+
* here, adding matching prompt guidance in the host adapter, and filtering
|
|
118
|
+
* the shim command below implement the workaround and can be removed once the
|
|
119
|
+
* upstream bug is fixed.
|
|
120
|
+
*/
|
|
121
|
+
let relay: ToolRelay | undefined;
|
|
122
|
+
let cliShimPath: string | undefined;
|
|
123
|
+
if (start.tools && start.tools.length > 0) {
|
|
124
|
+
cliShimPath = `${cliShimDir}/${CLI_SHIM_FILENAME}`;
|
|
125
|
+
relay = await startToolRelay({
|
|
126
|
+
tools: start.tools,
|
|
127
|
+
emit,
|
|
128
|
+
requestToolResult: turn.requestToolResult,
|
|
129
|
+
});
|
|
130
|
+
// Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts
|
|
131
|
+
await mkdir(cliShimDir, { recursive: true });
|
|
132
|
+
await writeFile(
|
|
133
|
+
cliShimPath,
|
|
134
|
+
buildCliShimScript({ relayPort: relay.port }),
|
|
135
|
+
'utf8',
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const codexConfig: Record<string, unknown> = {};
|
|
140
|
+
|
|
141
|
+
const gatewayBaseUrl = procEnv.AI_GATEWAY_BASE_URL;
|
|
142
|
+
const hasGatewayAuth = Boolean(procEnv.AI_GATEWAY_API_KEY || gatewayBaseUrl);
|
|
143
|
+
if (hasGatewayAuth && !gatewayBaseUrl) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
'AI Gateway auth was selected but AI_GATEWAY_BASE_URL is missing from the Codex bridge environment.',
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
const apiBaseUrl = hasGatewayAuth ? gatewayBaseUrl : procEnv.OPENAI_BASE_URL;
|
|
149
|
+
if (apiBaseUrl) {
|
|
150
|
+
codexConfig.preferred_auth_method = 'apikey';
|
|
151
|
+
codexConfig.model_provider = 'agent_bridge_openai';
|
|
152
|
+
codexConfig.model_providers = {
|
|
153
|
+
agent_bridge_openai: {
|
|
154
|
+
name: procEnv.CODEX_MODEL_PROVIDER_NAME || 'Agent Bridge OpenAI',
|
|
155
|
+
base_url: apiBaseUrl,
|
|
156
|
+
env_key: 'CODEX_API_KEY',
|
|
157
|
+
wire_api: 'responses',
|
|
158
|
+
supports_websockets: false,
|
|
159
|
+
...(hasGatewayAuth && HARNESS_CLIENT_APP
|
|
160
|
+
? {
|
|
161
|
+
http_headers: {
|
|
162
|
+
'User-Agent': HARNESS_CLIENT_APP,
|
|
163
|
+
'x-client-app': HARNESS_CLIENT_APP,
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
: {}),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const usesConfiguredModelProvider =
|
|
171
|
+
typeof codexConfig.model_provider === 'string';
|
|
172
|
+
|
|
173
|
+
const codex = new codexSdk.Codex({
|
|
174
|
+
...(procEnv.CODEX_API_KEY ? { apiKey: procEnv.CODEX_API_KEY } : {}),
|
|
175
|
+
...(!usesConfiguredModelProvider && apiBaseUrl
|
|
176
|
+
? { baseUrl: apiBaseUrl }
|
|
177
|
+
: {}),
|
|
178
|
+
env: Object.fromEntries(
|
|
179
|
+
Object.entries(procEnv).filter(
|
|
180
|
+
(entry): entry is [string, string] => typeof entry[1] === 'string',
|
|
181
|
+
),
|
|
182
|
+
),
|
|
183
|
+
...(Object.keys(codexConfig).length > 0 ? { config: codexConfig } : {}),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const threadOptions = {
|
|
187
|
+
...(start.model ? { model: start.model } : {}),
|
|
188
|
+
sandboxMode: 'danger-full-access',
|
|
189
|
+
approvalPolicy: 'never',
|
|
190
|
+
workingDirectory: workdir,
|
|
191
|
+
skipGitRepoCheck: true,
|
|
192
|
+
...(start.reasoningEffort
|
|
193
|
+
? { modelReasoningEffort: start.reasoningEffort }
|
|
194
|
+
: {}),
|
|
195
|
+
webSearchMode: start.webSearch ? 'live' : 'disabled',
|
|
196
|
+
};
|
|
197
|
+
const thread = threadState.id
|
|
198
|
+
? codex.resumeThread(threadState.id, threadOptions)
|
|
199
|
+
: codex.startThread(threadOptions);
|
|
200
|
+
|
|
201
|
+
emit({ type: 'stream-start' });
|
|
202
|
+
|
|
203
|
+
const userMessage = start.prompt;
|
|
204
|
+
let turnUsage: Record<string, unknown> | undefined;
|
|
205
|
+
const textByItem = new Map<string, string>();
|
|
206
|
+
const reasoningByItem = new Map<string, string>();
|
|
207
|
+
const stepTracker = createCodexStepTracker({ send: emit });
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
const { events } = await thread.runStreamed(userMessage, {
|
|
211
|
+
signal: turn.abortSignal,
|
|
212
|
+
});
|
|
213
|
+
for await (const event of events as AsyncIterable<CodexEvent>) {
|
|
214
|
+
if (turn.abortSignal.aborted) break;
|
|
215
|
+
if (
|
|
216
|
+
event.type === 'thread.started' &&
|
|
217
|
+
typeof event.thread_id === 'string'
|
|
218
|
+
) {
|
|
219
|
+
threadState.id = event.thread_id;
|
|
220
|
+
// Announce to the host so it can include the id in resume state.
|
|
221
|
+
emit({ type: 'bridge-thread', threadId: event.thread_id });
|
|
222
|
+
}
|
|
223
|
+
// Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts
|
|
224
|
+
if (cliShimPath && event.item?.type === 'command_execution') {
|
|
225
|
+
const relayCalls =
|
|
226
|
+
typeof event.item.command === 'string'
|
|
227
|
+
? parseToolRelayCommands({
|
|
228
|
+
command: event.item.command,
|
|
229
|
+
cliShimPath,
|
|
230
|
+
})
|
|
231
|
+
: undefined;
|
|
232
|
+
if (event.type === 'item.started' && relay && relayCalls) {
|
|
233
|
+
for (const relayCall of relayCalls) {
|
|
234
|
+
relay.authorizeToolCall(relayCall);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (relayCalls) {
|
|
238
|
+
stepTracker.observeEvent({ event, itemId: event.item.id });
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
translateAndEmit(event, {
|
|
243
|
+
send: emit,
|
|
244
|
+
textByItem,
|
|
245
|
+
reasoningByItem,
|
|
246
|
+
stepTracker,
|
|
247
|
+
setTurnUsage: u => (turnUsage = u),
|
|
248
|
+
emitWarning: turn.emitWarning,
|
|
249
|
+
emitError: turn.emitError,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
} catch (err) {
|
|
253
|
+
turn.emitError({ error: err, message: 'codex turn failed' });
|
|
254
|
+
return;
|
|
255
|
+
} finally {
|
|
256
|
+
relay?.close();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
emit({
|
|
260
|
+
type: 'finish',
|
|
261
|
+
finishReason: { unified: 'stop', raw: 'stop' },
|
|
262
|
+
totalUsage: turnUsage ?? defaultUsage(),
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
void turn.pendingUserMessages; // accepted but only consumed when codex supports streamed user input
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
type CodexItem = {
|
|
269
|
+
type: string;
|
|
270
|
+
id?: string;
|
|
271
|
+
text?: string;
|
|
272
|
+
command?: string;
|
|
273
|
+
exit_code?: number;
|
|
274
|
+
aggregated_output?: string;
|
|
275
|
+
status?: 'in_progress' | 'completed' | 'failed';
|
|
276
|
+
server?: string;
|
|
277
|
+
tool?: string;
|
|
278
|
+
arguments?: unknown;
|
|
279
|
+
result?: { content?: unknown; structured_content?: unknown } | unknown;
|
|
280
|
+
error?: { message?: string };
|
|
281
|
+
query?: string;
|
|
282
|
+
message?: string;
|
|
283
|
+
changes?: ReadonlyArray<{
|
|
284
|
+
path: string;
|
|
285
|
+
kind: 'add' | 'delete' | 'update';
|
|
286
|
+
}>;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
function extractMcpToolCallResult(item: CodexItem): unknown {
|
|
290
|
+
if (
|
|
291
|
+
item.result === undefined ||
|
|
292
|
+
item.result === null ||
|
|
293
|
+
typeof item.result !== 'object'
|
|
294
|
+
) {
|
|
295
|
+
return item.error?.message ? { error: item.error.message } : null;
|
|
296
|
+
}
|
|
297
|
+
const result = item.result as {
|
|
298
|
+
content?: unknown;
|
|
299
|
+
structured_content?: unknown;
|
|
300
|
+
};
|
|
301
|
+
if (
|
|
302
|
+
result.structured_content !== undefined &&
|
|
303
|
+
result.structured_content !== null
|
|
304
|
+
) {
|
|
305
|
+
return result.structured_content;
|
|
306
|
+
}
|
|
307
|
+
return result.content ?? null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
type CodexEvent = {
|
|
311
|
+
type:
|
|
312
|
+
| 'thread.started'
|
|
313
|
+
| 'turn.completed'
|
|
314
|
+
| 'turn.failed'
|
|
315
|
+
| 'error'
|
|
316
|
+
| 'item.started'
|
|
317
|
+
| 'item.updated'
|
|
318
|
+
| 'item.completed';
|
|
319
|
+
item?: CodexItem;
|
|
320
|
+
usage?: Record<string, number>;
|
|
321
|
+
error?: { message: string };
|
|
322
|
+
message?: string;
|
|
323
|
+
thread_id?: string;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
function translateAndEmit(
|
|
327
|
+
event: CodexEvent,
|
|
328
|
+
ctx: {
|
|
329
|
+
send: Emit;
|
|
330
|
+
textByItem: Map<string, string>;
|
|
331
|
+
reasoningByItem: Map<string, string>;
|
|
332
|
+
stepTracker: CodexStepTracker;
|
|
333
|
+
setTurnUsage: (u: Record<string, unknown>) => void;
|
|
334
|
+
emitWarning: BridgeTurn['emitWarning'];
|
|
335
|
+
emitError: BridgeTurn['emitError'];
|
|
336
|
+
},
|
|
337
|
+
): void {
|
|
338
|
+
if (event.type === 'turn.completed') {
|
|
339
|
+
if (event.usage) ctx.setTurnUsage(mapUsage(event.usage));
|
|
340
|
+
ctx.stepTracker.finishStep();
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (event.type === 'turn.failed') {
|
|
344
|
+
ctx.emitError({
|
|
345
|
+
error: event.error?.message ?? 'codex turn failed',
|
|
346
|
+
message: 'codex turn failed',
|
|
347
|
+
});
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (event.type === 'error') {
|
|
351
|
+
ctx.emitError({
|
|
352
|
+
error: event.message ?? 'codex error',
|
|
353
|
+
message: 'codex stream error',
|
|
354
|
+
});
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (!event.item) return;
|
|
358
|
+
const item = event.item;
|
|
359
|
+
const id = item.id ?? randomUUID();
|
|
360
|
+
const observeStep = (): void => {
|
|
361
|
+
ctx.stepTracker.observeEvent({ event, itemId: id });
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
if (item.type === 'agent_message' && typeof item.text === 'string') {
|
|
365
|
+
/*
|
|
366
|
+
* The presence of `id` in `textByItem` — not the `item.started` event —
|
|
367
|
+
* marks the text part as opened. Codex does not guarantee an
|
|
368
|
+
* `item.started` event carrying text precedes the first `item.updated`
|
|
369
|
+
* with text, so keying the `text-start` off the event type can emit a
|
|
370
|
+
* `text-delta` for a part that was never opened. Opening lazily on the
|
|
371
|
+
* first event with text keeps `text-start` before any `text-delta`.
|
|
372
|
+
*/
|
|
373
|
+
if (!ctx.textByItem.has(id)) {
|
|
374
|
+
ctx.send({ type: 'text-start', id });
|
|
375
|
+
ctx.textByItem.set(id, '');
|
|
376
|
+
}
|
|
377
|
+
const last = ctx.textByItem.get(id) ?? '';
|
|
378
|
+
const next = item.text;
|
|
379
|
+
if (next.length > last.length) {
|
|
380
|
+
ctx.send({ type: 'text-delta', id, delta: next.slice(last.length) });
|
|
381
|
+
ctx.textByItem.set(id, next);
|
|
382
|
+
}
|
|
383
|
+
if (event.type === 'item.completed') ctx.send({ type: 'text-end', id });
|
|
384
|
+
observeStep();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (item.type === 'reasoning' && typeof item.text === 'string') {
|
|
389
|
+
if (!ctx.reasoningByItem.has(id)) {
|
|
390
|
+
ctx.send({ type: 'reasoning-start', id });
|
|
391
|
+
ctx.reasoningByItem.set(id, '');
|
|
392
|
+
}
|
|
393
|
+
const last = ctx.reasoningByItem.get(id) ?? '';
|
|
394
|
+
const next = item.text;
|
|
395
|
+
if (next.length > last.length) {
|
|
396
|
+
ctx.send({ type: 'reasoning-delta', id, delta: next.slice(last.length) });
|
|
397
|
+
ctx.reasoningByItem.set(id, next);
|
|
398
|
+
}
|
|
399
|
+
if (event.type === 'item.completed')
|
|
400
|
+
ctx.send({ type: 'reasoning-end', id });
|
|
401
|
+
observeStep();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (item.type === 'command_execution') {
|
|
406
|
+
const nativeName = 'shell';
|
|
407
|
+
if (event.type === 'item.started') {
|
|
408
|
+
ctx.send({
|
|
409
|
+
type: 'tool-call',
|
|
410
|
+
toolCallId: id,
|
|
411
|
+
toolName: toCommonName(nativeName),
|
|
412
|
+
nativeName,
|
|
413
|
+
input: JSON.stringify({ command: item.command ?? '' }),
|
|
414
|
+
providerExecuted: true,
|
|
415
|
+
});
|
|
416
|
+
} else if (event.type === 'item.completed') {
|
|
417
|
+
ctx.send({
|
|
418
|
+
type: 'tool-result',
|
|
419
|
+
toolCallId: id,
|
|
420
|
+
toolName: toCommonName(nativeName),
|
|
421
|
+
result: {
|
|
422
|
+
exitCode: item.exit_code ?? null,
|
|
423
|
+
output: item.aggregated_output ?? '',
|
|
424
|
+
status: item.status ?? 'completed',
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
observeStep();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (item.type === 'mcp_tool_call') {
|
|
433
|
+
if (event.type === 'item.started') {
|
|
434
|
+
ctx.send({
|
|
435
|
+
type: 'tool-call',
|
|
436
|
+
toolCallId: id,
|
|
437
|
+
toolName: item.tool ?? 'unknown',
|
|
438
|
+
nativeName: item.tool ?? 'unknown',
|
|
439
|
+
input: JSON.stringify(item.arguments ?? {}),
|
|
440
|
+
providerExecuted: true,
|
|
441
|
+
});
|
|
442
|
+
} else if (event.type === 'item.completed') {
|
|
443
|
+
ctx.send({
|
|
444
|
+
type: 'tool-result',
|
|
445
|
+
toolCallId: id,
|
|
446
|
+
toolName: item.tool ?? 'unknown',
|
|
447
|
+
result: extractMcpToolCallResult(item),
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
observeStep();
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (item.type === 'web_search') {
|
|
455
|
+
const nativeName = 'web_search';
|
|
456
|
+
if (event.type === 'item.started') {
|
|
457
|
+
ctx.send({
|
|
458
|
+
type: 'tool-call',
|
|
459
|
+
toolCallId: id,
|
|
460
|
+
toolName: toCommonName(nativeName),
|
|
461
|
+
nativeName,
|
|
462
|
+
input: JSON.stringify({ query: item.query ?? '' }),
|
|
463
|
+
providerExecuted: true,
|
|
464
|
+
});
|
|
465
|
+
} else if (event.type === 'item.completed') {
|
|
466
|
+
ctx.send({
|
|
467
|
+
type: 'tool-result',
|
|
468
|
+
toolCallId: id,
|
|
469
|
+
toolName: toCommonName(nativeName),
|
|
470
|
+
result: item.result ?? null,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
observeStep();
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (item.type === 'file_change' && event.type === 'item.completed') {
|
|
478
|
+
for (const change of item.changes ?? []) {
|
|
479
|
+
ctx.send({
|
|
480
|
+
type: 'file-change',
|
|
481
|
+
event:
|
|
482
|
+
change.kind === 'add'
|
|
483
|
+
? 'create'
|
|
484
|
+
: change.kind === 'delete'
|
|
485
|
+
? 'delete'
|
|
486
|
+
: 'modify',
|
|
487
|
+
path: change.path,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
observeStep();
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (item.type === 'error' && event.type === 'item.completed') {
|
|
495
|
+
const message =
|
|
496
|
+
typeof item.message === 'string' && item.message.trim()
|
|
497
|
+
? item.message
|
|
498
|
+
: 'codex reported a non-fatal error item';
|
|
499
|
+
ctx.emitWarning({ message });
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function mapUsage(usage: Record<string, number>): Record<string, unknown> {
|
|
505
|
+
const input = usage.input_tokens ?? 0;
|
|
506
|
+
const cacheRead = usage.cached_input_tokens ?? 0;
|
|
507
|
+
return {
|
|
508
|
+
inputTokens: {
|
|
509
|
+
total: input,
|
|
510
|
+
noCache: Math.max(0, input - cacheRead),
|
|
511
|
+
cacheRead,
|
|
512
|
+
cacheWrite: 0,
|
|
513
|
+
},
|
|
514
|
+
outputTokens: {
|
|
515
|
+
total: usage.output_tokens ?? 0,
|
|
516
|
+
text: usage.output_tokens ?? 0,
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Tool relay — HTTP server on 127.0.0.1:0. The CLI shim invoked by Codex POSTs
|
|
523
|
+
* each tool invocation here; the relay forwards the call to the host (via the
|
|
524
|
+
* shared runtime's `emit`), awaits the matching `tool-result` (via
|
|
525
|
+
* `requestToolResult`), and responds with `{ result }`.
|
|
526
|
+
*/
|
|
527
|
+
async function startToolRelay({
|
|
528
|
+
tools,
|
|
529
|
+
emit,
|
|
530
|
+
requestToolResult,
|
|
531
|
+
}: {
|
|
532
|
+
tools: ReadonlyArray<{ name: string }>;
|
|
533
|
+
emit: Emit;
|
|
534
|
+
requestToolResult: (
|
|
535
|
+
toolCallId: string,
|
|
536
|
+
) => Promise<{ output: unknown; isError?: boolean }>;
|
|
537
|
+
}): Promise<ToolRelay> {
|
|
538
|
+
return startAuthorizedToolRelay({ tools, emit, requestToolResult });
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function parseArgs(args: string[]): {
|
|
542
|
+
workdir?: string;
|
|
543
|
+
bridgeStateDir?: string;
|
|
544
|
+
cliShimDir?: string;
|
|
545
|
+
} {
|
|
546
|
+
const out: {
|
|
547
|
+
workdir?: string;
|
|
548
|
+
bridgeStateDir?: string;
|
|
549
|
+
cliShimDir?: string;
|
|
550
|
+
} = {};
|
|
551
|
+
for (let i = 0; i < args.length; i++) {
|
|
552
|
+
if (args[i] === '--workdir' && i + 1 < args.length) {
|
|
553
|
+
out.workdir = args[++i];
|
|
554
|
+
} else if (args[i] === '--bridge-state-dir' && i + 1 < args.length) {
|
|
555
|
+
out.bridgeStateDir = args[++i];
|
|
556
|
+
} else if (args[i] === '--cli-shim-dir' && i + 1 < args.length) {
|
|
557
|
+
out.cliShimDir = args[++i];
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return out;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function emitFatal(message: string): never {
|
|
564
|
+
stdout.write(JSON.stringify({ type: 'bridge-fatal', message }) + '\n');
|
|
565
|
+
process.exit(1);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function requireArg({
|
|
569
|
+
value,
|
|
570
|
+
name,
|
|
571
|
+
}: {
|
|
572
|
+
value: string | undefined;
|
|
573
|
+
name: string;
|
|
574
|
+
}): string {
|
|
575
|
+
if (!value) {
|
|
576
|
+
emitFatal(`Missing ${name} argument.`);
|
|
577
|
+
}
|
|
578
|
+
return value;
|
|
579
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
lockfileVersion: '9.0'
|
|
2
|
+
|
|
3
|
+
settings:
|
|
4
|
+
autoInstallPeers: true
|
|
5
|
+
excludeLinksFromLockfile: false
|
|
6
|
+
|
|
7
|
+
importers:
|
|
8
|
+
|
|
9
|
+
.:
|
|
10
|
+
dependencies:
|
|
11
|
+
'@openai/codex-sdk':
|
|
12
|
+
specifier: 0.144.5
|
|
13
|
+
version: 0.144.5
|
|
14
|
+
ws:
|
|
15
|
+
specifier: 8.21.0
|
|
16
|
+
version: 8.21.0
|
|
17
|
+
|
|
18
|
+
packages:
|
|
19
|
+
|
|
20
|
+
'@openai/codex-sdk@0.144.5':
|
|
21
|
+
resolution: {integrity: sha512-90wHPEGyk74On6gwQPNtw+wzuDJ2zYpiVADDxw43S1cWn3QbBh/21zFS2xlAWWCrdY0gE90yXfmmrzwdUDlBGw==}
|
|
22
|
+
engines: {node: '>=18'}
|
|
23
|
+
|
|
24
|
+
'@openai/codex@0.144.5':
|
|
25
|
+
resolution: {integrity: sha512-jjB+K+OMv572mKhS+2QuLxWXDJNdpwbPenf+V+8bdq7wg4Scqt3cn6WEekD8wPqDVZqck0HSX17K9rD9kbDJQA==}
|
|
26
|
+
engines: {node: '>=16'}
|
|
27
|
+
hasBin: true
|
|
28
|
+
|
|
29
|
+
'@openai/codex@0.144.5-darwin-arm64':
|
|
30
|
+
resolution: {integrity: sha512-zcT6NfBCqLFt+BReNSETTZW6v6PdbH0dzNtm9j7l7mDGqwPbKZDGJdnpkBao2389I0ZacyIKgSZoI0vez1d4Dw==}
|
|
31
|
+
engines: {node: '>=16'}
|
|
32
|
+
cpu: [arm64]
|
|
33
|
+
os: [darwin]
|
|
34
|
+
|
|
35
|
+
'@openai/codex@0.144.5-darwin-x64':
|
|
36
|
+
resolution: {integrity: sha512-//Mo0m1MwaoT6psu5xsmofXpKx4/0irIkeq10xJvk59+886EG355ibjA+ZmlRcKhE3bLjsKD7p81nTbAdRL/bw==}
|
|
37
|
+
engines: {node: '>=16'}
|
|
38
|
+
cpu: [x64]
|
|
39
|
+
os: [darwin]
|
|
40
|
+
|
|
41
|
+
'@openai/codex@0.144.5-linux-arm64':
|
|
42
|
+
resolution: {integrity: sha512-zAHggxVwR2TBxKmybXY7ZMiB0G8DMonY2YPdwNNjwXcf+LOIqNGgswwNCDMbP/HEe6r8j+R9ZX/yYoo8f+n/RQ==}
|
|
43
|
+
engines: {node: '>=16'}
|
|
44
|
+
cpu: [arm64]
|
|
45
|
+
os: [linux]
|
|
46
|
+
|
|
47
|
+
'@openai/codex@0.144.5-linux-x64':
|
|
48
|
+
resolution: {integrity: sha512-FalLJlBQGFdK8Gc3kj9sa/ekNdgkHhUawLaKkvy5CtB18JaP2YxtTP/Pe1pD2iBiq8mMUliRnafpF6AdBdQMbg==}
|
|
49
|
+
engines: {node: '>=16'}
|
|
50
|
+
cpu: [x64]
|
|
51
|
+
os: [linux]
|
|
52
|
+
|
|
53
|
+
'@openai/codex@0.144.5-win32-arm64':
|
|
54
|
+
resolution: {integrity: sha512-0Pj7iqjEOEvPQPO3kFfCy9vGX4BTu76ChFFZHr2eNNIfVc3FOENAv/X98u4L+iIUtDOK9DbqmfUudW3DPapshg==}
|
|
55
|
+
engines: {node: '>=16'}
|
|
56
|
+
cpu: [arm64]
|
|
57
|
+
os: [win32]
|
|
58
|
+
|
|
59
|
+
'@openai/codex@0.144.5-win32-x64':
|
|
60
|
+
resolution: {integrity: sha512-DnsSTlnnzleTxvLwIGnBitKInscxn2I7qASqosS8Fv+qysBygd+ZiBn/SQsRCgQ28PAlsNzmd3Gf3ZTecolAmg==}
|
|
61
|
+
engines: {node: '>=16'}
|
|
62
|
+
cpu: [x64]
|
|
63
|
+
os: [win32]
|
|
64
|
+
|
|
65
|
+
ws@8.21.0:
|
|
66
|
+
resolution: {integrity: sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==}
|
|
67
|
+
engines: {node: '>=10.0.0'}
|
|
68
|
+
peerDependencies:
|
|
69
|
+
bufferutil: ^4.0.1
|
|
70
|
+
utf-8-validate: '>=5.0.2'
|
|
71
|
+
peerDependenciesMeta:
|
|
72
|
+
bufferutil:
|
|
73
|
+
optional: true
|
|
74
|
+
utf-8-validate:
|
|
75
|
+
optional: true
|
|
76
|
+
|
|
77
|
+
snapshots:
|
|
78
|
+
|
|
79
|
+
'@openai/codex-sdk@0.144.5':
|
|
80
|
+
dependencies:
|
|
81
|
+
'@openai/codex': 0.144.5
|
|
82
|
+
|
|
83
|
+
'@openai/codex@0.144.5':
|
|
84
|
+
optionalDependencies:
|
|
85
|
+
'@openai/codex-darwin-arm64': '@openai/codex@0.144.5-darwin-arm64'
|
|
86
|
+
'@openai/codex-darwin-x64': '@openai/codex@0.144.5-darwin-x64'
|
|
87
|
+
'@openai/codex-linux-arm64': '@openai/codex@0.144.5-linux-arm64'
|
|
88
|
+
'@openai/codex-linux-x64': '@openai/codex@0.144.5-linux-x64'
|
|
89
|
+
'@openai/codex-win32-arm64': '@openai/codex@0.144.5-win32-arm64'
|
|
90
|
+
'@openai/codex-win32-x64': '@openai/codex@0.144.5-win32-x64'
|
|
91
|
+
|
|
92
|
+
'@openai/codex@0.144.5-darwin-arm64':
|
|
93
|
+
optional: true
|
|
94
|
+
|
|
95
|
+
'@openai/codex@0.144.5-darwin-x64':
|
|
96
|
+
optional: true
|
|
97
|
+
|
|
98
|
+
'@openai/codex@0.144.5-linux-arm64':
|
|
99
|
+
optional: true
|
|
100
|
+
|
|
101
|
+
'@openai/codex@0.144.5-linux-x64':
|
|
102
|
+
optional: true
|
|
103
|
+
|
|
104
|
+
'@openai/codex@0.144.5-win32-arm64':
|
|
105
|
+
optional: true
|
|
106
|
+
|
|
107
|
+
'@openai/codex@0.144.5-win32-x64':
|
|
108
|
+
optional: true
|
|
109
|
+
|
|
110
|
+
ws@8.21.0: {}
|