@northflare/runner 0.0.11 → 0.0.13
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/utils/config.d.ts +1 -0
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/config.js +13 -2
- package/dist/utils/config.js.map +1 -1
- package/package.json +1 -2
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/coverage-final.json +0 -12
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -176
- package/coverage/lib/index.html +0 -116
- package/coverage/lib/preload-script.js.html +0 -964
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -196
- package/coverage/src/collections/index.html +0 -116
- package/coverage/src/collections/runner-messages.ts.html +0 -312
- package/coverage/src/components/claude-manager.ts.html +0 -1290
- package/coverage/src/components/index.html +0 -146
- package/coverage/src/components/message-handler.ts.html +0 -730
- package/coverage/src/components/repository-manager.ts.html +0 -841
- package/coverage/src/index.html +0 -131
- package/coverage/src/index.ts.html +0 -448
- package/coverage/src/runner.ts.html +0 -1239
- package/coverage/src/utils/config.ts.html +0 -780
- package/coverage/src/utils/console.ts.html +0 -121
- package/coverage/src/utils/index.html +0 -161
- package/coverage/src/utils/logger.ts.html +0 -475
- package/coverage/src/utils/status-line.ts.html +0 -445
- package/exceptions.log +0 -24
- package/lib/codex-sdk/src/codex.ts +0 -38
- package/lib/codex-sdk/src/codexOptions.ts +0 -10
- package/lib/codex-sdk/src/events.ts +0 -80
- package/lib/codex-sdk/src/exec.ts +0 -336
- package/lib/codex-sdk/src/index.ts +0 -39
- package/lib/codex-sdk/src/items.ts +0 -127
- package/lib/codex-sdk/src/outputSchemaFile.ts +0 -40
- package/lib/codex-sdk/src/thread.ts +0 -155
- package/lib/codex-sdk/src/threadOptions.ts +0 -18
- package/lib/codex-sdk/src/turnOptions.ts +0 -6
- package/lib/codex-sdk/tests/abort.test.ts +0 -165
- package/lib/codex-sdk/tests/codexExecSpy.ts +0 -37
- package/lib/codex-sdk/tests/responsesProxy.ts +0 -225
- package/lib/codex-sdk/tests/run.test.ts +0 -687
- package/lib/codex-sdk/tests/runStreamed.test.ts +0 -211
- package/lib/codex-sdk/tsconfig.json +0 -24
- package/rejections.log +0 -68
- package/runner.log +0 -488
- package/src/components/claude-sdk-manager.ts +0 -1425
- package/src/components/codex-sdk-manager.ts +0 -1358
- package/src/components/enhanced-repository-manager.ts +0 -823
- package/src/components/message-handler-sse.ts +0 -1097
- package/src/components/repository-manager.ts +0 -337
- package/src/index.ts +0 -168
- package/src/runner-sse.ts +0 -917
- package/src/services/RunnerAPIClient.ts +0 -175
- package/src/services/SSEClient.ts +0 -258
- package/src/types/claude.ts +0 -66
- package/src/types/computer-name.d.ts +0 -4
- package/src/types/index.ts +0 -64
- package/src/types/messages.ts +0 -39
- package/src/types/runner-interface.ts +0 -36
- package/src/utils/StateManager.ts +0 -187
- package/src/utils/config.ts +0 -316
- package/src/utils/console.ts +0 -15
- package/src/utils/debug.ts +0 -18
- package/src/utils/expand-env.ts +0 -22
- package/src/utils/logger.ts +0 -134
- package/src/utils/model.ts +0 -29
- package/src/utils/status-line.ts +0 -122
- package/src/utils/tool-response-sanitizer.ts +0 -160
- package/test-debug.sh +0 -26
- package/tests/retry-strategies.test.ts +0 -410
- package/tests/sdk-integration.test.ts +0 -329
- package/tests/sdk-streaming.test.ts +0 -1180
- package/tests/setup.ts +0 -5
- package/tests/test-claude-manager.ts +0 -120
- package/tests/tool-response-sanitizer.test.ts +0 -63
- package/tsconfig.json +0 -36
- package/vitest.config.ts +0 -27
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import readline from "node:readline";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { createRequire } from "node:module";
|
|
7
|
-
|
|
8
|
-
import { SandboxMode, ModelReasoningEffort, ApprovalMode } from "./threadOptions";
|
|
9
|
-
|
|
10
|
-
export type CodexExecArgs = {
|
|
11
|
-
input: string;
|
|
12
|
-
|
|
13
|
-
baseUrl?: string;
|
|
14
|
-
apiKey?: string;
|
|
15
|
-
threadId?: string | null;
|
|
16
|
-
images?: string[];
|
|
17
|
-
// --model
|
|
18
|
-
model?: string;
|
|
19
|
-
// --sandbox
|
|
20
|
-
sandboxMode?: SandboxMode;
|
|
21
|
-
// --cd
|
|
22
|
-
workingDirectory?: string;
|
|
23
|
-
// --add-dir
|
|
24
|
-
additionalDirectories?: string[];
|
|
25
|
-
// --skip-git-repo-check
|
|
26
|
-
skipGitRepoCheck?: boolean;
|
|
27
|
-
// --output-schema
|
|
28
|
-
outputSchemaFile?: string;
|
|
29
|
-
// --config model_reasoning_effort
|
|
30
|
-
modelReasoningEffort?: ModelReasoningEffort;
|
|
31
|
-
// AbortSignal to cancel the execution
|
|
32
|
-
signal?: AbortSignal;
|
|
33
|
-
// --config sandbox_workspace_write.network_access
|
|
34
|
-
networkAccessEnabled?: boolean;
|
|
35
|
-
// --config features.web_search_request
|
|
36
|
-
webSearchEnabled?: boolean;
|
|
37
|
-
// --config approval_policy
|
|
38
|
-
approvalPolicy?: ApprovalMode;
|
|
39
|
-
// Generic config overrides for `-c key=value`
|
|
40
|
-
configOverrides?: Record<string, unknown>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const INTERNAL_ORIGINATOR_ENV = "CODEX_INTERNAL_ORIGINATOR_OVERRIDE";
|
|
44
|
-
const TYPESCRIPT_SDK_ORIGINATOR = "codex_sdk_ts";
|
|
45
|
-
const isDebugEnabled = process.env["DEBUG"] === "true";
|
|
46
|
-
|
|
47
|
-
export class CodexExec {
|
|
48
|
-
private executablePath: string;
|
|
49
|
-
private envOverride?: Record<string, string>;
|
|
50
|
-
|
|
51
|
-
constructor(executablePath: string | null = null, env?: Record<string, string>) {
|
|
52
|
-
this.executablePath = executablePath || findCodexPath();
|
|
53
|
-
this.envOverride = env;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async *run(args: CodexExecArgs): AsyncGenerator<string> {
|
|
57
|
-
const commandArgs: string[] = ["exec", "--experimental-json"];
|
|
58
|
-
|
|
59
|
-
if (args.model) {
|
|
60
|
-
commandArgs.push("--model", args.model);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (args.sandboxMode) {
|
|
64
|
-
commandArgs.push("--sandbox", args.sandboxMode);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (args.workingDirectory) {
|
|
68
|
-
commandArgs.push("--cd", args.workingDirectory);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (args.additionalDirectories?.length) {
|
|
72
|
-
for (const dir of args.additionalDirectories) {
|
|
73
|
-
commandArgs.push("--add-dir", dir);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (args.skipGitRepoCheck) {
|
|
78
|
-
commandArgs.push("--skip-git-repo-check");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (args.outputSchemaFile) {
|
|
82
|
-
commandArgs.push("--output-schema", args.outputSchemaFile);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (args.modelReasoningEffort) {
|
|
86
|
-
commandArgs.push("--config", `model_reasoning_effort="${args.modelReasoningEffort}"`);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (args.networkAccessEnabled !== undefined) {
|
|
90
|
-
commandArgs.push(
|
|
91
|
-
"--config",
|
|
92
|
-
`sandbox_workspace_write.network_access=${args.networkAccessEnabled}`,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (args.webSearchEnabled !== undefined) {
|
|
97
|
-
commandArgs.push("--config", `features.web_search_request=${args.webSearchEnabled}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (args.approvalPolicy) {
|
|
101
|
-
commandArgs.push("--config", `approval_policy="${args.approvalPolicy}"`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (args.configOverrides) {
|
|
105
|
-
for (const [key, value] of Object.entries(args.configOverrides)) {
|
|
106
|
-
const formatted = formatConfigValue(value);
|
|
107
|
-
commandArgs.push("--config", `${key}=${formatted}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (args.images?.length) {
|
|
112
|
-
for (const image of args.images) {
|
|
113
|
-
commandArgs.push("--image", image);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (args.threadId) {
|
|
118
|
-
commandArgs.push("resume", args.threadId);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (isDebugEnabled) {
|
|
122
|
-
console.log("[CodexExec] Executable path:", this.executablePath);
|
|
123
|
-
console.log("[CodexExec] Command arguments:", commandArgs);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const env: Record<string, string> = {};
|
|
127
|
-
if (this.envOverride) {
|
|
128
|
-
Object.assign(env, this.envOverride);
|
|
129
|
-
} else {
|
|
130
|
-
for (const [key, value] of Object.entries(process.env)) {
|
|
131
|
-
if (value !== undefined) {
|
|
132
|
-
env[key] = value;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (!env[INTERNAL_ORIGINATOR_ENV]) {
|
|
137
|
-
env[INTERNAL_ORIGINATOR_ENV] = TYPESCRIPT_SDK_ORIGINATOR;
|
|
138
|
-
}
|
|
139
|
-
if (args.baseUrl) {
|
|
140
|
-
env.OPENAI_BASE_URL = args.baseUrl;
|
|
141
|
-
}
|
|
142
|
-
if (args.apiKey) {
|
|
143
|
-
env.CODEX_API_KEY = args.apiKey;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const child = spawn(this.executablePath, commandArgs, {
|
|
147
|
-
env,
|
|
148
|
-
signal: args.signal,
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
let spawnError: unknown | null = null;
|
|
152
|
-
child.once("error", (err) => (spawnError = err));
|
|
153
|
-
|
|
154
|
-
if (!child.stdin) {
|
|
155
|
-
child.kill();
|
|
156
|
-
throw new Error("Child process has no stdin");
|
|
157
|
-
}
|
|
158
|
-
child.stdin.write(args.input);
|
|
159
|
-
child.stdin.end();
|
|
160
|
-
|
|
161
|
-
if (!child.stdout) {
|
|
162
|
-
child.kill();
|
|
163
|
-
throw new Error("Child process has no stdout");
|
|
164
|
-
}
|
|
165
|
-
const stderrChunks: Buffer[] = [];
|
|
166
|
-
|
|
167
|
-
if (child.stderr) {
|
|
168
|
-
child.stderr.on("data", (data) => {
|
|
169
|
-
stderrChunks.push(data);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const rl = readline.createInterface({
|
|
174
|
-
input: child.stdout,
|
|
175
|
-
crlfDelay: Infinity,
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
for await (const line of rl) {
|
|
180
|
-
// `line` is a string (Node sets default encoding to utf8 for readline)
|
|
181
|
-
yield line as string;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const exitCode = new Promise((resolve, reject) => {
|
|
185
|
-
child.once("exit", (code) => {
|
|
186
|
-
if (code === 0) {
|
|
187
|
-
resolve(code);
|
|
188
|
-
} else {
|
|
189
|
-
const stderrBuffer = Buffer.concat(stderrChunks);
|
|
190
|
-
reject(
|
|
191
|
-
new Error(`Codex Exec exited with code ${code}: ${stderrBuffer.toString("utf8")}`),
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
if (spawnError) throw spawnError;
|
|
198
|
-
await exitCode;
|
|
199
|
-
} finally {
|
|
200
|
-
rl.close();
|
|
201
|
-
child.removeAllListeners();
|
|
202
|
-
try {
|
|
203
|
-
if (!child.killed) child.kill();
|
|
204
|
-
} catch {
|
|
205
|
-
// ignore
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const scriptFileName = fileURLToPath(import.meta.url);
|
|
212
|
-
const scriptDirName = path.dirname(scriptFileName);
|
|
213
|
-
const require = createRequire(import.meta.url);
|
|
214
|
-
|
|
215
|
-
function findCodexPath() {
|
|
216
|
-
const { platform, arch } = process;
|
|
217
|
-
|
|
218
|
-
let targetTriple = null;
|
|
219
|
-
switch (platform) {
|
|
220
|
-
case "linux":
|
|
221
|
-
case "android":
|
|
222
|
-
switch (arch) {
|
|
223
|
-
case "x64":
|
|
224
|
-
targetTriple = "x86_64-unknown-linux-musl";
|
|
225
|
-
break;
|
|
226
|
-
case "arm64":
|
|
227
|
-
targetTriple = "aarch64-unknown-linux-musl";
|
|
228
|
-
break;
|
|
229
|
-
default:
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
break;
|
|
233
|
-
case "darwin":
|
|
234
|
-
switch (arch) {
|
|
235
|
-
case "x64":
|
|
236
|
-
targetTriple = "x86_64-apple-darwin";
|
|
237
|
-
break;
|
|
238
|
-
case "arm64":
|
|
239
|
-
targetTriple = "aarch64-apple-darwin";
|
|
240
|
-
break;
|
|
241
|
-
default:
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
break;
|
|
245
|
-
case "win32":
|
|
246
|
-
switch (arch) {
|
|
247
|
-
case "x64":
|
|
248
|
-
targetTriple = "x86_64-pc-windows-msvc";
|
|
249
|
-
break;
|
|
250
|
-
case "arm64":
|
|
251
|
-
targetTriple = "aarch64-pc-windows-msvc";
|
|
252
|
-
break;
|
|
253
|
-
default:
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
break;
|
|
257
|
-
default:
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (!targetTriple) {
|
|
262
|
-
throw new Error(`Unsupported platform: ${platform} (${arch})`);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const vendorResolution = resolveUpstreamVendorRoot();
|
|
266
|
-
let vendorRoot: string;
|
|
267
|
-
if (vendorResolution.path) {
|
|
268
|
-
vendorRoot = vendorResolution.path;
|
|
269
|
-
} else {
|
|
270
|
-
vendorRoot = path.join(scriptDirName, "..", "vendor");
|
|
271
|
-
const reason = vendorResolution.reason ? `: ${vendorResolution.reason}` : "";
|
|
272
|
-
console.warn(
|
|
273
|
-
`[Codex SDK] Failed to resolve upstream vendor package${reason}, falling back to local vendor directory`,
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
const archRoot = path.join(vendorRoot, targetTriple);
|
|
277
|
-
const codexBinaryName = process.platform === "win32" ? "codex.exe" : "codex";
|
|
278
|
-
const binaryPath = path.join(archRoot, "codex", codexBinaryName);
|
|
279
|
-
|
|
280
|
-
return binaryPath;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function resolveUpstreamVendorRoot(): { path: string | null; reason?: string } {
|
|
284
|
-
let searchPaths: string[] | null = null;
|
|
285
|
-
try {
|
|
286
|
-
searchPaths = require.resolve.paths("@openai/codex-sdk");
|
|
287
|
-
} catch (error) {
|
|
288
|
-
return { path: null, reason: (error as Error)?.message || String(error) };
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (!searchPaths?.length) {
|
|
292
|
-
return { path: null, reason: "no module search paths returned" };
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
let locatedPackageRoot: string | null = null;
|
|
296
|
-
for (const basePath of searchPaths) {
|
|
297
|
-
if (!basePath) continue;
|
|
298
|
-
const packageRoot = path.join(basePath, "@openai/codex-sdk");
|
|
299
|
-
const packageJsonPath = path.join(packageRoot, "package.json");
|
|
300
|
-
if (!existsSync(packageJsonPath)) {
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
locatedPackageRoot = packageRoot;
|
|
305
|
-
const vendorPath = path.join(packageRoot, "vendor");
|
|
306
|
-
if (existsSync(vendorPath)) {
|
|
307
|
-
return { path: vendorPath };
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (locatedPackageRoot) {
|
|
312
|
-
return { path: null, reason: `vendor directory missing in ${locatedPackageRoot}` };
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
path: null,
|
|
317
|
-
reason: `@openai/codex-sdk not found in node_modules search paths (${searchPaths.join(", ")})`,
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function formatConfigValue(value: unknown): string {
|
|
322
|
-
if (typeof value === "string") {
|
|
323
|
-
return JSON.stringify(value);
|
|
324
|
-
}
|
|
325
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
326
|
-
return String(value);
|
|
327
|
-
}
|
|
328
|
-
if (value === null || value === undefined) {
|
|
329
|
-
return "null";
|
|
330
|
-
}
|
|
331
|
-
try {
|
|
332
|
-
return JSON.stringify(value);
|
|
333
|
-
} catch {
|
|
334
|
-
return JSON.stringify(String(value));
|
|
335
|
-
}
|
|
336
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
ThreadEvent,
|
|
3
|
-
ThreadStartedEvent,
|
|
4
|
-
TurnStartedEvent,
|
|
5
|
-
TurnCompletedEvent,
|
|
6
|
-
TurnFailedEvent,
|
|
7
|
-
ItemStartedEvent,
|
|
8
|
-
ItemUpdatedEvent,
|
|
9
|
-
ItemCompletedEvent,
|
|
10
|
-
ThreadError,
|
|
11
|
-
ThreadErrorEvent,
|
|
12
|
-
Usage,
|
|
13
|
-
} from "./events";
|
|
14
|
-
export type {
|
|
15
|
-
ThreadItem,
|
|
16
|
-
AgentMessageItem,
|
|
17
|
-
ReasoningItem,
|
|
18
|
-
CommandExecutionItem,
|
|
19
|
-
FileChangeItem,
|
|
20
|
-
McpToolCallItem,
|
|
21
|
-
WebSearchItem,
|
|
22
|
-
TodoListItem,
|
|
23
|
-
ErrorItem,
|
|
24
|
-
} from "./items";
|
|
25
|
-
|
|
26
|
-
export { Thread } from "./thread";
|
|
27
|
-
export type { RunResult, RunStreamedResult, Input, UserInput } from "./thread";
|
|
28
|
-
|
|
29
|
-
export { Codex } from "./codex";
|
|
30
|
-
|
|
31
|
-
export type { CodexOptions } from "./codexOptions";
|
|
32
|
-
|
|
33
|
-
export type {
|
|
34
|
-
ThreadOptions,
|
|
35
|
-
ApprovalMode,
|
|
36
|
-
SandboxMode,
|
|
37
|
-
ModelReasoningEffort,
|
|
38
|
-
} from "./threadOptions";
|
|
39
|
-
export type { TurnOptions } from "./turnOptions";
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
// based on item types from codex-rs/exec/src/exec_events.rs
|
|
2
|
-
|
|
3
|
-
import type { ContentBlock as McpContentBlock } from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
-
|
|
5
|
-
/** The status of a command execution. */
|
|
6
|
-
export type CommandExecutionStatus = "in_progress" | "completed" | "failed";
|
|
7
|
-
|
|
8
|
-
/** A command executed by the agent. */
|
|
9
|
-
export type CommandExecutionItem = {
|
|
10
|
-
id: string;
|
|
11
|
-
type: "command_execution";
|
|
12
|
-
/** The command line executed by the agent. */
|
|
13
|
-
command: string;
|
|
14
|
-
/** Aggregated stdout and stderr captured while the command was running. */
|
|
15
|
-
aggregated_output: string;
|
|
16
|
-
/** Set when the command exits; omitted while still running. */
|
|
17
|
-
exit_code?: number;
|
|
18
|
-
/** Current status of the command execution. */
|
|
19
|
-
status: CommandExecutionStatus;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/** Indicates the type of the file change. */
|
|
23
|
-
export type PatchChangeKind = "add" | "delete" | "update";
|
|
24
|
-
|
|
25
|
-
/** A set of file changes by the agent. */
|
|
26
|
-
export type FileUpdateChange = {
|
|
27
|
-
path: string;
|
|
28
|
-
kind: PatchChangeKind;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** The status of a file change. */
|
|
32
|
-
export type PatchApplyStatus = "completed" | "failed";
|
|
33
|
-
|
|
34
|
-
/** A set of file changes by the agent. Emitted once the patch succeeds or fails. */
|
|
35
|
-
export type FileChangeItem = {
|
|
36
|
-
id: string;
|
|
37
|
-
type: "file_change";
|
|
38
|
-
/** Individual file changes that comprise the patch. */
|
|
39
|
-
changes: FileUpdateChange[];
|
|
40
|
-
/** Whether the patch ultimately succeeded or failed. */
|
|
41
|
-
status: PatchApplyStatus;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/** The status of an MCP tool call. */
|
|
45
|
-
export type McpToolCallStatus = "in_progress" | "completed" | "failed";
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Represents a call to an MCP tool. The item starts when the invocation is dispatched
|
|
49
|
-
* and completes when the MCP server reports success or failure.
|
|
50
|
-
*/
|
|
51
|
-
export type McpToolCallItem = {
|
|
52
|
-
id: string;
|
|
53
|
-
type: "mcp_tool_call";
|
|
54
|
-
/** Name of the MCP server handling the request. */
|
|
55
|
-
server: string;
|
|
56
|
-
/** The tool invoked on the MCP server. */
|
|
57
|
-
tool: string;
|
|
58
|
-
/** Arguments forwarded to the tool invocation. */
|
|
59
|
-
arguments: unknown;
|
|
60
|
-
/** Result payload returned by the MCP server for successful calls. */
|
|
61
|
-
result?: {
|
|
62
|
-
content: McpContentBlock[];
|
|
63
|
-
structured_content: unknown;
|
|
64
|
-
};
|
|
65
|
-
/** Error message reported for failed calls. */
|
|
66
|
-
error?: {
|
|
67
|
-
message: string;
|
|
68
|
-
};
|
|
69
|
-
/** Current status of the tool invocation. */
|
|
70
|
-
status: McpToolCallStatus;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/** Response from the agent. Either natural-language text or JSON when structured output is requested. */
|
|
74
|
-
export type AgentMessageItem = {
|
|
75
|
-
id: string;
|
|
76
|
-
type: "agent_message";
|
|
77
|
-
/** Either natural-language text or JSON when structured output is requested. */
|
|
78
|
-
text: string;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
/** Agent's reasoning summary. */
|
|
82
|
-
export type ReasoningItem = {
|
|
83
|
-
id: string;
|
|
84
|
-
type: "reasoning";
|
|
85
|
-
text: string;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/** Captures a web search request. Completes when results are returned to the agent. */
|
|
89
|
-
export type WebSearchItem = {
|
|
90
|
-
id: string;
|
|
91
|
-
type: "web_search";
|
|
92
|
-
query: string;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/** Describes a non-fatal error surfaced as an item. */
|
|
96
|
-
export type ErrorItem = {
|
|
97
|
-
id: string;
|
|
98
|
-
type: "error";
|
|
99
|
-
message: string;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/** An item in the agent's to-do list. */
|
|
103
|
-
export type TodoItem = {
|
|
104
|
-
text: string;
|
|
105
|
-
completed: boolean;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Tracks the agent's running to-do list. Starts when the plan is issued, updates as steps change,
|
|
110
|
-
* and completes when the turn ends.
|
|
111
|
-
*/
|
|
112
|
-
export type TodoListItem = {
|
|
113
|
-
id: string;
|
|
114
|
-
type: "todo_list";
|
|
115
|
-
items: TodoItem[];
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/** Canonical union of thread items and their type-specific payloads. */
|
|
119
|
-
export type ThreadItem =
|
|
120
|
-
| AgentMessageItem
|
|
121
|
-
| ReasoningItem
|
|
122
|
-
| CommandExecutionItem
|
|
123
|
-
| FileChangeItem
|
|
124
|
-
| McpToolCallItem
|
|
125
|
-
| WebSearchItem
|
|
126
|
-
| TodoListItem
|
|
127
|
-
| ErrorItem;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { promises as fs } from "node:fs";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
export type OutputSchemaFile = {
|
|
6
|
-
schemaPath?: string;
|
|
7
|
-
cleanup: () => Promise<void>;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export async function createOutputSchemaFile(schema: unknown): Promise<OutputSchemaFile> {
|
|
11
|
-
if (schema === undefined) {
|
|
12
|
-
return { cleanup: async () => {} };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (!isJsonObject(schema)) {
|
|
16
|
-
throw new Error("outputSchema must be a plain JSON object");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const schemaDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-output-schema-"));
|
|
20
|
-
const schemaPath = path.join(schemaDir, "schema.json");
|
|
21
|
-
const cleanup = async () => {
|
|
22
|
-
try {
|
|
23
|
-
await fs.rm(schemaDir, { recursive: true, force: true });
|
|
24
|
-
} catch {
|
|
25
|
-
// suppress
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
await fs.writeFile(schemaPath, JSON.stringify(schema), "utf8");
|
|
31
|
-
return { schemaPath, cleanup };
|
|
32
|
-
} catch (error) {
|
|
33
|
-
await cleanup();
|
|
34
|
-
throw error;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function isJsonObject(value: unknown): value is Record<string, unknown> {
|
|
39
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
40
|
-
}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { CodexOptions } from "./codexOptions";
|
|
2
|
-
import { ThreadEvent, ThreadError, Usage } from "./events";
|
|
3
|
-
import { CodexExec } from "./exec";
|
|
4
|
-
import { ThreadItem } from "./items";
|
|
5
|
-
import { ThreadOptions } from "./threadOptions";
|
|
6
|
-
import { TurnOptions } from "./turnOptions";
|
|
7
|
-
import { createOutputSchemaFile } from "./outputSchemaFile";
|
|
8
|
-
|
|
9
|
-
/** Completed turn. */
|
|
10
|
-
export type Turn = {
|
|
11
|
-
items: ThreadItem[];
|
|
12
|
-
finalResponse: string;
|
|
13
|
-
usage: Usage | null;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/** Alias for `Turn` to describe the result of `run()`. */
|
|
17
|
-
export type RunResult = Turn;
|
|
18
|
-
|
|
19
|
-
/** The result of the `runStreamed` method. */
|
|
20
|
-
export type StreamedTurn = {
|
|
21
|
-
events: AsyncGenerator<ThreadEvent>;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/** Alias for `StreamedTurn` to describe the result of `runStreamed()`. */
|
|
25
|
-
export type RunStreamedResult = StreamedTurn;
|
|
26
|
-
|
|
27
|
-
/** An input to send to the agent. */
|
|
28
|
-
export type UserInput =
|
|
29
|
-
| {
|
|
30
|
-
type: "text";
|
|
31
|
-
text: string;
|
|
32
|
-
}
|
|
33
|
-
| {
|
|
34
|
-
type: "local_image";
|
|
35
|
-
path: string;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type Input = string | UserInput[];
|
|
39
|
-
|
|
40
|
-
/** Respesent a thread of conversation with the agent. One thread can have multiple consecutive turns. */
|
|
41
|
-
export class Thread {
|
|
42
|
-
private _exec: CodexExec;
|
|
43
|
-
private _options: CodexOptions;
|
|
44
|
-
private _id: string | null;
|
|
45
|
-
private _threadOptions: ThreadOptions;
|
|
46
|
-
|
|
47
|
-
/** Returns the ID of the thread. Populated after the first turn starts. */
|
|
48
|
-
public get id(): string | null {
|
|
49
|
-
return this._id;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/* @internal */
|
|
53
|
-
constructor(
|
|
54
|
-
exec: CodexExec,
|
|
55
|
-
options: CodexOptions,
|
|
56
|
-
threadOptions: ThreadOptions,
|
|
57
|
-
id: string | null = null,
|
|
58
|
-
) {
|
|
59
|
-
this._exec = exec;
|
|
60
|
-
this._options = options;
|
|
61
|
-
this._id = id;
|
|
62
|
-
this._threadOptions = threadOptions;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Provides the input to the agent and streams events as they are produced during the turn. */
|
|
66
|
-
async runStreamed(input: Input, turnOptions: TurnOptions = {}): Promise<StreamedTurn> {
|
|
67
|
-
return { events: this.runStreamedInternal(input, turnOptions) };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private async *runStreamedInternal(
|
|
71
|
-
input: Input,
|
|
72
|
-
turnOptions: TurnOptions = {},
|
|
73
|
-
): AsyncGenerator<ThreadEvent> {
|
|
74
|
-
const { schemaPath, cleanup } = await createOutputSchemaFile(turnOptions.outputSchema);
|
|
75
|
-
const options = this._threadOptions;
|
|
76
|
-
const { prompt, images } = normalizeInput(input);
|
|
77
|
-
const generator = this._exec.run({
|
|
78
|
-
input: prompt,
|
|
79
|
-
baseUrl: this._options.baseUrl,
|
|
80
|
-
apiKey: this._options.apiKey,
|
|
81
|
-
threadId: this._id,
|
|
82
|
-
images,
|
|
83
|
-
model: options?.model,
|
|
84
|
-
sandboxMode: options?.sandboxMode,
|
|
85
|
-
workingDirectory: options?.workingDirectory,
|
|
86
|
-
skipGitRepoCheck: options?.skipGitRepoCheck,
|
|
87
|
-
outputSchemaFile: schemaPath,
|
|
88
|
-
modelReasoningEffort: options?.modelReasoningEffort,
|
|
89
|
-
signal: turnOptions.signal,
|
|
90
|
-
networkAccessEnabled: options?.networkAccessEnabled,
|
|
91
|
-
webSearchEnabled: options?.webSearchEnabled,
|
|
92
|
-
approvalPolicy: options?.approvalPolicy,
|
|
93
|
-
additionalDirectories: options?.additionalDirectories,
|
|
94
|
-
configOverrides: options?.configOverrides,
|
|
95
|
-
});
|
|
96
|
-
try {
|
|
97
|
-
for await (const item of generator) {
|
|
98
|
-
let parsed: ThreadEvent;
|
|
99
|
-
try {
|
|
100
|
-
parsed = JSON.parse(item) as ThreadEvent;
|
|
101
|
-
} catch (error) {
|
|
102
|
-
throw new Error(`Failed to parse item: ${item}`, { cause: error });
|
|
103
|
-
}
|
|
104
|
-
if (parsed.type === "thread.started") {
|
|
105
|
-
this._id = parsed.thread_id;
|
|
106
|
-
}
|
|
107
|
-
yield parsed;
|
|
108
|
-
}
|
|
109
|
-
} finally {
|
|
110
|
-
await cleanup();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Provides the input to the agent and returns the completed turn. */
|
|
115
|
-
async run(input: Input, turnOptions: TurnOptions = {}): Promise<Turn> {
|
|
116
|
-
const generator = this.runStreamedInternal(input, turnOptions);
|
|
117
|
-
const items: ThreadItem[] = [];
|
|
118
|
-
let finalResponse: string = "";
|
|
119
|
-
let usage: Usage | null = null;
|
|
120
|
-
let turnFailure: ThreadError | null = null;
|
|
121
|
-
for await (const event of generator) {
|
|
122
|
-
if (event.type === "item.completed") {
|
|
123
|
-
if (event.item.type === "agent_message") {
|
|
124
|
-
finalResponse = event.item.text;
|
|
125
|
-
}
|
|
126
|
-
items.push(event.item);
|
|
127
|
-
} else if (event.type === "turn.completed") {
|
|
128
|
-
usage = event.usage;
|
|
129
|
-
} else if (event.type === "turn.failed") {
|
|
130
|
-
turnFailure = event.error;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (turnFailure) {
|
|
135
|
-
throw new Error(turnFailure.message);
|
|
136
|
-
}
|
|
137
|
-
return { items, finalResponse, usage };
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function normalizeInput(input: Input): { prompt: string; images: string[] } {
|
|
142
|
-
if (typeof input === "string") {
|
|
143
|
-
return { prompt: input, images: [] };
|
|
144
|
-
}
|
|
145
|
-
const promptParts: string[] = [];
|
|
146
|
-
const images: string[] = [];
|
|
147
|
-
for (const item of input) {
|
|
148
|
-
if (item.type === "text") {
|
|
149
|
-
promptParts.push(item.text);
|
|
150
|
-
} else if (item.type === "local_image") {
|
|
151
|
-
images.push(item.path);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return { prompt: promptParts.join("\n\n"), images };
|
|
155
|
-
}
|