@openai/codex-sdk 0.43.0-alpha.16 → 0.43.0-alpha.18
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/index.d.ts +11 -11
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/vendor/aarch64-apple-darwin/codex/codex +0 -0
- package/vendor/aarch64-pc-windows-msvc/codex/codex.exe +0 -0
- package/vendor/aarch64-unknown-linux-musl/codex/codex +0 -0
- package/vendor/x86_64-apple-darwin/codex/codex +0 -0
- package/vendor/x86_64-pc-windows-msvc/codex/codex.exe +0 -0
- package/vendor/x86_64-unknown-linux-musl/codex/codex +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type CommandExecutionStatus = "in_progress" | "completed" | "failed";
|
|
|
3
3
|
/** A command executed by the agent. */
|
|
4
4
|
type CommandExecutionItem = {
|
|
5
5
|
id: string;
|
|
6
|
-
|
|
6
|
+
type: "command_execution";
|
|
7
7
|
/** The command line executed by the agent. */
|
|
8
8
|
command: string;
|
|
9
9
|
/** Aggregated stdout and stderr captured while the command was running. */
|
|
@@ -25,7 +25,7 @@ type PatchApplyStatus = "completed" | "failed";
|
|
|
25
25
|
/** A set of file changes by the agent. Emitted once the patch succeeds or fails. */
|
|
26
26
|
type FileChangeItem = {
|
|
27
27
|
id: string;
|
|
28
|
-
|
|
28
|
+
type: "file_change";
|
|
29
29
|
/** Individual file changes that comprise the patch. */
|
|
30
30
|
changes: FileUpdateChange[];
|
|
31
31
|
/** Whether the patch ultimately succeeded or failed. */
|
|
@@ -39,7 +39,7 @@ type McpToolCallStatus = "in_progress" | "completed" | "failed";
|
|
|
39
39
|
*/
|
|
40
40
|
type McpToolCallItem = {
|
|
41
41
|
id: string;
|
|
42
|
-
|
|
42
|
+
type: "mcp_tool_call";
|
|
43
43
|
/** Name of the MCP server handling the request. */
|
|
44
44
|
server: string;
|
|
45
45
|
/** The tool invoked on the MCP server. */
|
|
@@ -48,28 +48,28 @@ type McpToolCallItem = {
|
|
|
48
48
|
status: McpToolCallStatus;
|
|
49
49
|
};
|
|
50
50
|
/** Response from the agent. Either natural-language text or JSON when structured output is requested. */
|
|
51
|
-
type
|
|
51
|
+
type AgentMessageItem = {
|
|
52
52
|
id: string;
|
|
53
|
-
|
|
53
|
+
type: "agent_message";
|
|
54
54
|
/** Either natural-language text or JSON when structured output is requested. */
|
|
55
55
|
text: string;
|
|
56
56
|
};
|
|
57
57
|
/** Agent's reasoning summary. */
|
|
58
58
|
type ReasoningItem = {
|
|
59
59
|
id: string;
|
|
60
|
-
|
|
60
|
+
type: "reasoning";
|
|
61
61
|
text: string;
|
|
62
62
|
};
|
|
63
63
|
/** Captures a web search request. Completes when results are returned to the agent. */
|
|
64
64
|
type WebSearchItem = {
|
|
65
65
|
id: string;
|
|
66
|
-
|
|
66
|
+
type: "web_search";
|
|
67
67
|
query: string;
|
|
68
68
|
};
|
|
69
69
|
/** Describes a non-fatal error surfaced as an item. */
|
|
70
70
|
type ErrorItem = {
|
|
71
71
|
id: string;
|
|
72
|
-
|
|
72
|
+
type: "error";
|
|
73
73
|
message: string;
|
|
74
74
|
};
|
|
75
75
|
/** An item in the agent's to-do list. */
|
|
@@ -83,11 +83,11 @@ type TodoItem = {
|
|
|
83
83
|
*/
|
|
84
84
|
type TodoListItem = {
|
|
85
85
|
id: string;
|
|
86
|
-
|
|
86
|
+
type: "todo_list";
|
|
87
87
|
items: TodoItem[];
|
|
88
88
|
};
|
|
89
89
|
/** Canonical union of thread items and their type-specific payloads. */
|
|
90
|
-
type ThreadItem =
|
|
90
|
+
type ThreadItem = AgentMessageItem | ReasoningItem | CommandExecutionItem | FileChangeItem | McpToolCallItem | WebSearchItem | TodoListItem | ErrorItem;
|
|
91
91
|
|
|
92
92
|
/** Emitted when a new thread is started as the first event. */
|
|
93
93
|
type ThreadStartedEvent = {
|
|
@@ -218,4 +218,4 @@ declare class Codex {
|
|
|
218
218
|
resumeThread(id: string, options?: ThreadOptions): Thread;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
export { type
|
|
221
|
+
export { type AgentMessageItem, type ApprovalMode, Codex, type CodexOptions, type CommandExecutionItem, type ErrorItem, type FileChangeItem, type Input, type ItemCompletedEvent, type ItemStartedEvent, type ItemUpdatedEvent, type McpToolCallItem, type ReasoningItem, type RunResult, type RunStreamedResult, type SandboxMode, type ThreadOptions as TheadOptions, Thread, type ThreadError, type ThreadErrorEvent, type ThreadEvent, type ThreadItem, type ThreadStartedEvent, type TodoListItem, type TurnCompletedEvent, type TurnFailedEvent, type TurnStartedEvent, type WebSearchItem };
|
package/dist/index.js
CHANGED
|
@@ -51,7 +51,7 @@ var Thread = class {
|
|
|
51
51
|
let finalResponse = "";
|
|
52
52
|
for await (const event of generator) {
|
|
53
53
|
if (event.type === "item.completed") {
|
|
54
|
-
if (event.item.
|
|
54
|
+
if (event.item.type === "agent_message") {
|
|
55
55
|
finalResponse = event.item.text;
|
|
56
56
|
}
|
|
57
57
|
items.push(event.item);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/thread.ts","../src/exec.ts","../src/codex.ts"],"sourcesContent":["import { CodexOptions } from \"./codexOptions\";\nimport { ThreadEvent } from \"./events\";\nimport { CodexExec } from \"./exec\";\nimport { ThreadItem } from \"./items\";\nimport { ThreadOptions } from \"./threadOptions\";\n\n/** Completed turn. */\nexport type Turn = {\n items: ThreadItem[];\n finalResponse: string;\n};\n\n/** Alias for `Turn` to describe the result of `run()`. */\nexport type RunResult = Turn;\n\n/** The result of the `runStreamed` method. */\nexport type StreamedTurn = {\n events: AsyncGenerator<ThreadEvent>;\n};\n\n/** Alias for `StreamedTurn` to describe the result of `runStreamed()`. */\nexport type RunStreamedResult = StreamedTurn;\n\n/** An input to send to the agent. */\nexport type Input = string;\n\n/** Respesent a thread of conversation with the agent. One thread can have multiple consecutive turns. */\nexport class Thread {\n private _exec: CodexExec;\n private _options: CodexOptions;\n private _id: string | null;\n private _threadOptions: ThreadOptions;\n\n /** Returns the ID of the thread. Populated after the first turn starts. */\n public get id(): string | null {\n return this._id;\n }\n\n /* @internal */\n constructor(\n exec: CodexExec,\n options: CodexOptions,\n threadOptions: ThreadOptions,\n id: string | null = null,\n ) {\n this._exec = exec;\n this._options = options;\n this._id = id;\n this._threadOptions = threadOptions;\n }\n\n /** Provides the input to the agent and streams events as they are produced during the turn. */\n async runStreamed(input: string): Promise<StreamedTurn> {\n return { events: this.runStreamedInternal(input) };\n }\n\n private async *runStreamedInternal(input: string): AsyncGenerator<ThreadEvent> {\n const options = this._threadOptions;\n const generator = this._exec.run({\n input,\n baseUrl: this._options.baseUrl,\n apiKey: this._options.apiKey,\n threadId: this._id,\n model: options?.model,\n sandboxMode: options?.sandboxMode,\n workingDirectory: options?.workingDirectory,\n skipGitRepoCheck: options?.skipGitRepoCheck,\n });\n for await (const item of generator) {\n let parsed: ThreadEvent;\n try {\n parsed = JSON.parse(item) as ThreadEvent;\n } catch (error) {\n throw new Error(`Failed to parse item: ${item}`, { cause: error });\n }\n if (parsed.type === \"thread.started\") {\n this._id = parsed.thread_id;\n }\n yield parsed;\n }\n }\n\n /** Provides the input to the agent and returns the completed turn. */\n async run(input: string): Promise<Turn> {\n const generator = this.runStreamedInternal(input);\n const items: ThreadItem[] = [];\n let finalResponse: string = \"\";\n for await (const event of generator) {\n if (event.type === \"item.completed\") {\n if (event.item.item_type === \"assistant_message\") {\n finalResponse = event.item.text;\n }\n items.push(event.item);\n }\n }\n return { items, finalResponse };\n }\n}\n","import { spawn } from \"node:child_process\";\n\nimport readline from \"node:readline\";\n\nimport { SandboxMode } from \"./threadOptions\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nexport type CodexExecArgs = {\n input: string;\n\n baseUrl?: string;\n apiKey?: string;\n threadId?: string | null;\n // --model\n model?: string;\n // --sandbox\n sandboxMode?: SandboxMode;\n // --cd\n workingDirectory?: string;\n // --skip-git-repo-check\n skipGitRepoCheck?: boolean;\n};\n\nexport class CodexExec {\n private executablePath: string;\n constructor(executablePath: string | null = null) {\n this.executablePath = executablePath || findCodexPath();\n }\n\n async *run(args: CodexExecArgs): AsyncGenerator<string> {\n const commandArgs: string[] = [\"exec\", \"--experimental-json\"];\n\n if (args.model) {\n commandArgs.push(\"--model\", args.model);\n }\n\n if (args.sandboxMode) {\n commandArgs.push(\"--sandbox\", args.sandboxMode);\n }\n\n if (args.workingDirectory) {\n commandArgs.push(\"--cd\", args.workingDirectory);\n }\n\n if (args.skipGitRepoCheck) {\n commandArgs.push(\"--skip-git-repo-check\");\n }\n\n if (args.threadId) {\n commandArgs.push(\"resume\", args.threadId);\n }\n\n const env = {\n ...process.env,\n };\n if (args.baseUrl) {\n env.OPENAI_BASE_URL = args.baseUrl;\n }\n if (args.apiKey) {\n env.CODEX_API_KEY = args.apiKey;\n }\n\n const child = spawn(this.executablePath, commandArgs, {\n env,\n });\n\n let spawnError: unknown | null = null;\n child.once(\"error\", (err) => (spawnError = err));\n\n if (!child.stdin) {\n child.kill();\n throw new Error(\"Child process has no stdin\");\n }\n child.stdin.write(args.input);\n child.stdin.end();\n\n if (!child.stdout) {\n child.kill();\n throw new Error(\"Child process has no stdout\");\n }\n const stderrChunks: Buffer[] = [];\n\n if (child.stderr) {\n child.stderr.on(\"data\", (data) => {\n stderrChunks.push(data);\n });\n }\n\n const rl = readline.createInterface({\n input: child.stdout,\n crlfDelay: Infinity,\n });\n\n try {\n for await (const line of rl) {\n // `line` is a string (Node sets default encoding to utf8 for readline)\n yield line as string;\n }\n\n const exitCode = new Promise((resolve, reject) => {\n child.once(\"exit\", (code) => {\n if (code === 0) {\n resolve(code);\n } else {\n const stderrBuffer = Buffer.concat(stderrChunks);\n reject(\n new Error(`Codex Exec exited with code ${code}: ${stderrBuffer.toString(\"utf8\")}`),\n );\n }\n });\n });\n\n if (spawnError) throw spawnError;\n await exitCode;\n } finally {\n rl.close();\n child.removeAllListeners();\n try {\n if (!child.killed) child.kill();\n } catch {\n // ignore\n }\n }\n }\n}\n\nconst scriptFileName = fileURLToPath(import.meta.url);\nconst scriptDirName = path.dirname(scriptFileName);\n\nfunction findCodexPath() {\n const { platform, arch } = process;\n\n let targetTriple = null;\n switch (platform) {\n case \"linux\":\n case \"android\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-unknown-linux-musl\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-unknown-linux-musl\";\n break;\n default:\n break;\n }\n break;\n case \"darwin\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-apple-darwin\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-apple-darwin\";\n break;\n default:\n break;\n }\n break;\n case \"win32\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-pc-windows-msvc\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-pc-windows-msvc\";\n break;\n default:\n break;\n }\n break;\n default:\n break;\n }\n\n if (!targetTriple) {\n throw new Error(`Unsupported platform: ${platform} (${arch})`);\n }\n\n const vendorRoot = path.join(scriptDirName, \"..\", \"vendor\");\n const archRoot = path.join(vendorRoot, targetTriple);\n const codexBinaryName = process.platform === \"win32\" ? \"codex.exe\" : \"codex\";\n const binaryPath = path.join(archRoot, \"codex\", codexBinaryName);\n\n return binaryPath;\n}\n","import { CodexOptions } from \"./codexOptions\";\nimport { CodexExec } from \"./exec\";\nimport { Thread } from \"./thread\";\nimport { ThreadOptions } from \"./threadOptions\";\n\n/**\n * Codex is the main class for interacting with the Codex agent.\n *\n * Use the `startThread()` method to start a new thread or `resumeThread()` to resume a previously started thread.\n */\nexport class Codex {\n private exec: CodexExec;\n private options: CodexOptions;\n\n constructor(options: CodexOptions = {}) {\n this.exec = new CodexExec(options.codexPathOverride);\n this.options = options;\n }\n\n /**\n * Starts a new conversation with an agent.\n * @returns A new thread instance.\n */\n startThread(options: ThreadOptions = {}): Thread {\n return new Thread(this.exec, this.options, options);\n }\n\n /**\n * Resumes a conversation with an agent based on the thread id.\n * Threads are persisted in ~/.codex/sessions.\n *\n * @param id The id of the thread to resume.\n * @returns A new thread instance.\n */\n resumeThread(id: string, options: ThreadOptions = {}): Thread {\n return new Thread(this.exec, this.options, options, id);\n }\n}\n"],"mappings":";AA2BO,IAAM,SAAN,MAAa;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGR,IAAW,KAAoB;AAC7B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,YACE,MACA,SACA,eACA,KAAoB,MACpB;AACA,SAAK,QAAQ;AACb,SAAK,WAAW;AAChB,SAAK,MAAM;AACX,SAAK,iBAAiB;AAAA,EACxB;AAAA;AAAA,EAGA,MAAM,YAAY,OAAsC;AACtD,WAAO,EAAE,QAAQ,KAAK,oBAAoB,KAAK,EAAE;AAAA,EACnD;AAAA,EAEA,OAAe,oBAAoB,OAA4C;AAC7E,UAAM,UAAU,KAAK;AACrB,UAAM,YAAY,KAAK,MAAM,IAAI;AAAA,MAC/B;AAAA,MACA,SAAS,KAAK,SAAS;AAAA,MACvB,QAAQ,KAAK,SAAS;AAAA,MACtB,UAAU,KAAK;AAAA,MACf,OAAO,SAAS;AAAA,MAChB,aAAa,SAAS;AAAA,MACtB,kBAAkB,SAAS;AAAA,MAC3B,kBAAkB,SAAS;AAAA,IAC7B,CAAC;AACD,qBAAiB,QAAQ,WAAW;AAClC,UAAI;AACJ,UAAI;AACF,iBAAS,KAAK,MAAM,IAAI;AAAA,MAC1B,SAAS,OAAO;AACd,cAAM,IAAI,MAAM,yBAAyB,IAAI,IAAI,EAAE,OAAO,MAAM,CAAC;AAAA,MACnE;AACA,UAAI,OAAO,SAAS,kBAAkB;AACpC,aAAK,MAAM,OAAO;AAAA,MACpB;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA,EAGA,MAAM,IAAI,OAA8B;AACtC,UAAM,YAAY,KAAK,oBAAoB,KAAK;AAChD,UAAM,QAAs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3,"sources":["../src/thread.ts","../src/exec.ts","../src/codex.ts"],"sourcesContent":["import { CodexOptions } from \"./codexOptions\";\nimport { ThreadEvent } from \"./events\";\nimport { CodexExec } from \"./exec\";\nimport { ThreadItem } from \"./items\";\nimport { ThreadOptions } from \"./threadOptions\";\n\n/** Completed turn. */\nexport type Turn = {\n items: ThreadItem[];\n finalResponse: string;\n};\n\n/** Alias for `Turn` to describe the result of `run()`. */\nexport type RunResult = Turn;\n\n/** The result of the `runStreamed` method. */\nexport type StreamedTurn = {\n events: AsyncGenerator<ThreadEvent>;\n};\n\n/** Alias for `StreamedTurn` to describe the result of `runStreamed()`. */\nexport type RunStreamedResult = StreamedTurn;\n\n/** An input to send to the agent. */\nexport type Input = string;\n\n/** Respesent a thread of conversation with the agent. One thread can have multiple consecutive turns. */\nexport class Thread {\n private _exec: CodexExec;\n private _options: CodexOptions;\n private _id: string | null;\n private _threadOptions: ThreadOptions;\n\n /** Returns the ID of the thread. Populated after the first turn starts. */\n public get id(): string | null {\n return this._id;\n }\n\n /* @internal */\n constructor(\n exec: CodexExec,\n options: CodexOptions,\n threadOptions: ThreadOptions,\n id: string | null = null,\n ) {\n this._exec = exec;\n this._options = options;\n this._id = id;\n this._threadOptions = threadOptions;\n }\n\n /** Provides the input to the agent and streams events as they are produced during the turn. */\n async runStreamed(input: string): Promise<StreamedTurn> {\n return { events: this.runStreamedInternal(input) };\n }\n\n private async *runStreamedInternal(input: string): AsyncGenerator<ThreadEvent> {\n const options = this._threadOptions;\n const generator = this._exec.run({\n input,\n baseUrl: this._options.baseUrl,\n apiKey: this._options.apiKey,\n threadId: this._id,\n model: options?.model,\n sandboxMode: options?.sandboxMode,\n workingDirectory: options?.workingDirectory,\n skipGitRepoCheck: options?.skipGitRepoCheck,\n });\n for await (const item of generator) {\n let parsed: ThreadEvent;\n try {\n parsed = JSON.parse(item) as ThreadEvent;\n } catch (error) {\n throw new Error(`Failed to parse item: ${item}`, { cause: error });\n }\n if (parsed.type === \"thread.started\") {\n this._id = parsed.thread_id;\n }\n yield parsed;\n }\n }\n\n /** Provides the input to the agent and returns the completed turn. */\n async run(input: string): Promise<Turn> {\n const generator = this.runStreamedInternal(input);\n const items: ThreadItem[] = [];\n let finalResponse: string = \"\";\n for await (const event of generator) {\n if (event.type === \"item.completed\") {\n if (event.item.type === \"agent_message\") {\n finalResponse = event.item.text;\n }\n items.push(event.item);\n }\n }\n return { items, finalResponse };\n }\n}\n","import { spawn } from \"node:child_process\";\n\nimport readline from \"node:readline\";\n\nimport { SandboxMode } from \"./threadOptions\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nexport type CodexExecArgs = {\n input: string;\n\n baseUrl?: string;\n apiKey?: string;\n threadId?: string | null;\n // --model\n model?: string;\n // --sandbox\n sandboxMode?: SandboxMode;\n // --cd\n workingDirectory?: string;\n // --skip-git-repo-check\n skipGitRepoCheck?: boolean;\n};\n\nexport class CodexExec {\n private executablePath: string;\n constructor(executablePath: string | null = null) {\n this.executablePath = executablePath || findCodexPath();\n }\n\n async *run(args: CodexExecArgs): AsyncGenerator<string> {\n const commandArgs: string[] = [\"exec\", \"--experimental-json\"];\n\n if (args.model) {\n commandArgs.push(\"--model\", args.model);\n }\n\n if (args.sandboxMode) {\n commandArgs.push(\"--sandbox\", args.sandboxMode);\n }\n\n if (args.workingDirectory) {\n commandArgs.push(\"--cd\", args.workingDirectory);\n }\n\n if (args.skipGitRepoCheck) {\n commandArgs.push(\"--skip-git-repo-check\");\n }\n\n if (args.threadId) {\n commandArgs.push(\"resume\", args.threadId);\n }\n\n const env = {\n ...process.env,\n };\n if (args.baseUrl) {\n env.OPENAI_BASE_URL = args.baseUrl;\n }\n if (args.apiKey) {\n env.CODEX_API_KEY = args.apiKey;\n }\n\n const child = spawn(this.executablePath, commandArgs, {\n env,\n });\n\n let spawnError: unknown | null = null;\n child.once(\"error\", (err) => (spawnError = err));\n\n if (!child.stdin) {\n child.kill();\n throw new Error(\"Child process has no stdin\");\n }\n child.stdin.write(args.input);\n child.stdin.end();\n\n if (!child.stdout) {\n child.kill();\n throw new Error(\"Child process has no stdout\");\n }\n const stderrChunks: Buffer[] = [];\n\n if (child.stderr) {\n child.stderr.on(\"data\", (data) => {\n stderrChunks.push(data);\n });\n }\n\n const rl = readline.createInterface({\n input: child.stdout,\n crlfDelay: Infinity,\n });\n\n try {\n for await (const line of rl) {\n // `line` is a string (Node sets default encoding to utf8 for readline)\n yield line as string;\n }\n\n const exitCode = new Promise((resolve, reject) => {\n child.once(\"exit\", (code) => {\n if (code === 0) {\n resolve(code);\n } else {\n const stderrBuffer = Buffer.concat(stderrChunks);\n reject(\n new Error(`Codex Exec exited with code ${code}: ${stderrBuffer.toString(\"utf8\")}`),\n );\n }\n });\n });\n\n if (spawnError) throw spawnError;\n await exitCode;\n } finally {\n rl.close();\n child.removeAllListeners();\n try {\n if (!child.killed) child.kill();\n } catch {\n // ignore\n }\n }\n }\n}\n\nconst scriptFileName = fileURLToPath(import.meta.url);\nconst scriptDirName = path.dirname(scriptFileName);\n\nfunction findCodexPath() {\n const { platform, arch } = process;\n\n let targetTriple = null;\n switch (platform) {\n case \"linux\":\n case \"android\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-unknown-linux-musl\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-unknown-linux-musl\";\n break;\n default:\n break;\n }\n break;\n case \"darwin\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-apple-darwin\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-apple-darwin\";\n break;\n default:\n break;\n }\n break;\n case \"win32\":\n switch (arch) {\n case \"x64\":\n targetTriple = \"x86_64-pc-windows-msvc\";\n break;\n case \"arm64\":\n targetTriple = \"aarch64-pc-windows-msvc\";\n break;\n default:\n break;\n }\n break;\n default:\n break;\n }\n\n if (!targetTriple) {\n throw new Error(`Unsupported platform: ${platform} (${arch})`);\n }\n\n const vendorRoot = path.join(scriptDirName, \"..\", \"vendor\");\n const archRoot = path.join(vendorRoot, targetTriple);\n const codexBinaryName = process.platform === \"win32\" ? \"codex.exe\" : \"codex\";\n const binaryPath = path.join(archRoot, \"codex\", codexBinaryName);\n\n return binaryPath;\n}\n","import { CodexOptions } from \"./codexOptions\";\nimport { CodexExec } from \"./exec\";\nimport { Thread } from \"./thread\";\nimport { ThreadOptions } from \"./threadOptions\";\n\n/**\n * Codex is the main class for interacting with the Codex agent.\n *\n * Use the `startThread()` method to start a new thread or `resumeThread()` to resume a previously started thread.\n */\nexport class Codex {\n private exec: CodexExec;\n private options: CodexOptions;\n\n constructor(options: CodexOptions = {}) {\n this.exec = new CodexExec(options.codexPathOverride);\n this.options = options;\n }\n\n /**\n * Starts a new conversation with an agent.\n * @returns A new thread instance.\n */\n startThread(options: ThreadOptions = {}): Thread {\n return new Thread(this.exec, this.options, options);\n }\n\n /**\n * Resumes a conversation with an agent based on the thread id.\n * Threads are persisted in ~/.codex/sessions.\n *\n * @param id The id of the thread to resume.\n * @returns A new thread instance.\n */\n resumeThread(id: string, options: ThreadOptions = {}): Thread {\n return new Thread(this.exec, this.options, options, id);\n }\n}\n"],"mappings":";AA2BO,IAAM,SAAN,MAAa;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGR,IAAW,KAAoB;AAC7B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,YACE,MACA,SACA,eACA,KAAoB,MACpB;AACA,SAAK,QAAQ;AACb,SAAK,WAAW;AAChB,SAAK,MAAM;AACX,SAAK,iBAAiB;AAAA,EACxB;AAAA;AAAA,EAGA,MAAM,YAAY,OAAsC;AACtD,WAAO,EAAE,QAAQ,KAAK,oBAAoB,KAAK,EAAE;AAAA,EACnD;AAAA,EAEA,OAAe,oBAAoB,OAA4C;AAC7E,UAAM,UAAU,KAAK;AACrB,UAAM,YAAY,KAAK,MAAM,IAAI;AAAA,MAC/B;AAAA,MACA,SAAS,KAAK,SAAS;AAAA,MACvB,QAAQ,KAAK,SAAS;AAAA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AAK,UAAU,SAAS,eAAe;AAE/D,SAAO;AACT;;;AChLO,IAAM,QAAN,MAAY;AAAA,EACT;AAAA,EACA;AAAA,EAER,YAAY,UAAwB,CAAC,GAAG;AACtC,SAAK,OAAO,IAAI,UAAU,QAAQ,iBAAiB;AACnD,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAY,UAAyB,CAAC,GAAW;AAC/C,WAAO,IAAI,OAAO,KAAK,MAAM,KAAK,SAAS,OAAO;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,aAAa,IAAY,UAAyB,CAAC,GAAW;AAC5D,WAAO,IAAI,OAAO,KAAK,MAAM,KAAK,SAAS,SAAS,EAAE;AAAA,EACxD;AACF;","names":[]}
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|