Support model and sandbox mode in the sdk (#4503)
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import { spawn } from "child_process";
|
||||
import readline from "node:readline";
|
||||
|
||||
import { SandboxMode } from "./turnOptions";
|
||||
|
||||
export type CodexExecArgs = {
|
||||
input: string;
|
||||
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
threadId?: string | null;
|
||||
model?: string;
|
||||
sandboxMode?: SandboxMode;
|
||||
};
|
||||
|
||||
export class CodexExec {
|
||||
@@ -17,6 +21,15 @@ export class CodexExec {
|
||||
|
||||
async *run(args: CodexExecArgs): AsyncGenerator<string> {
|
||||
const commandArgs: string[] = ["exec", "--experimental-json"];
|
||||
|
||||
if (args.model) {
|
||||
commandArgs.push("--model", args.model);
|
||||
}
|
||||
|
||||
if (args.sandboxMode) {
|
||||
commandArgs.push("--sandbox", args.sandboxMode);
|
||||
}
|
||||
|
||||
if (args.threadId) {
|
||||
commandArgs.push("resume", args.threadId, args.input);
|
||||
} else {
|
||||
|
||||
@@ -27,3 +27,5 @@ export type { Thread, RunResult, RunStreamedResult, Input } from "./thread";
|
||||
export type { Codex } from "./codex";
|
||||
|
||||
export type { CodexOptions } from "./codexOptions";
|
||||
|
||||
export type { TurnOptions, ApprovalMode, SandboxMode } from "./turnOptions";
|
||||
|
||||
@@ -2,6 +2,7 @@ import { CodexOptions } from "./codexOptions";
|
||||
import { ThreadEvent } from "./events";
|
||||
import { CodexExec } from "./exec";
|
||||
import { ThreadItem } from "./items";
|
||||
import { TurnOptions } from "./turnOptions";
|
||||
|
||||
export type RunResult = {
|
||||
items: ThreadItem[];
|
||||
@@ -25,16 +26,21 @@ export class Thread {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
async runStreamed(input: string): Promise<RunStreamedResult> {
|
||||
return { events: this.runStreamedInternal(input) };
|
||||
async runStreamed(input: string, options?: TurnOptions): Promise<RunStreamedResult> {
|
||||
return { events: this.runStreamedInternal(input, options) };
|
||||
}
|
||||
|
||||
private async *runStreamedInternal(input: string): AsyncGenerator<ThreadEvent> {
|
||||
private async *runStreamedInternal(
|
||||
input: string,
|
||||
options?: TurnOptions,
|
||||
): AsyncGenerator<ThreadEvent> {
|
||||
const generator = this.exec.run({
|
||||
input,
|
||||
baseUrl: this.options.baseUrl,
|
||||
apiKey: this.options.apiKey,
|
||||
threadId: this.id,
|
||||
model: options?.model,
|
||||
sandboxMode: options?.sandboxMode,
|
||||
});
|
||||
for await (const item of generator) {
|
||||
const parsed = JSON.parse(item) as ThreadEvent;
|
||||
@@ -45,8 +51,8 @@ export class Thread {
|
||||
}
|
||||
}
|
||||
|
||||
async run(input: string): Promise<RunResult> {
|
||||
const generator = this.runStreamedInternal(input);
|
||||
async run(input: string, options?: TurnOptions): Promise<RunResult> {
|
||||
const generator = this.runStreamedInternal(input, options);
|
||||
const items: ThreadItem[] = [];
|
||||
let finalResponse: string = "";
|
||||
for await (const event of generator) {
|
||||
|
||||
8
sdk/typescript/src/turnOptions.ts
Normal file
8
sdk/typescript/src/turnOptions.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export type ApprovalMode = "never" | "on-request" | "on-failure" | "untrusted";
|
||||
|
||||
export type SandboxMode = "read-only" | "workspace-write" | "danger-full-access";
|
||||
|
||||
export type TurnOptions = {
|
||||
model?: string;
|
||||
sandboxMode?: SandboxMode;
|
||||
};
|
||||
Reference in New Issue
Block a user