Repository navigation
Expand file tree
/
Copy pathlocal-agent-runtime.ts
More file actions
138 lines (121 loc) · 3.91 KB
/
Copy pathlocal-agent-runtime.ts
File metadata and controls
138 lines (121 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type {
Codex,
CodexOptions,
ModelReasoningEffort,
RunResult,
SandboxMode,
ThreadOptions,
TurnOptions,
} from "@openai/codex-sdk";
import type { JsonSchema } from "./json-types.js";
import type { LocalAgentProvider } from "./local-agent-profiles.js";
import {
isNativeSchemaUnsupportedFailure,
ProviderSchemaUnsupportedError,
} from "./local-agent-errors.js";
export {
isNativeSchemaUnsupportedFailure,
isProviderSchemaUnsupportedError,
ProviderSchemaUnsupportedError,
} from "./local-agent-errors.js";
export type LocalAgentWriteMode = "read_only" | "allowed" | "full_access";
export interface LocalAgentRunInput {
prompt: string;
workspace: string;
providerSessionId?: string;
writeMode?: LocalAgentWriteMode;
model?: string;
/** Provider-native effort / reasoning level (was thinking). */
effort?: string;
/** JSON Schema for native structured output (codex/claude). */
schema?: JsonSchema;
}
export interface LocalAgentRunResult {
provider: LocalAgentProvider;
providerSessionId: string | null;
finalResponse: string;
items: unknown[];
/** Provider-native structured object when schema was requested. */
structured?: unknown;
}
export interface LocalAgentRuntime {
readonly provider: LocalAgentProvider;
run(input: LocalAgentRunInput): Promise<LocalAgentRunResult>;
}
interface CodexThreadLike {
readonly id: string | null;
run(prompt: string, turnOptions?: TurnOptions): Promise<RunResult>;
}
interface CodexClientLike {
startThread(options?: ThreadOptions): CodexThreadLike;
resumeThread(id: string, options?: ThreadOptions): CodexThreadLike;
}
type CodexFactory = (options?: CodexOptions) => CodexClientLike;
function sandboxModeFor(writeMode: LocalAgentWriteMode | undefined): SandboxMode {
switch (writeMode) {
case "allowed":
return "workspace-write";
case "full_access":
return "danger-full-access";
case "read_only":
case undefined:
return "read-only";
}
}
function threadOptionsFor(input: LocalAgentRunInput): ThreadOptions {
return {
workingDirectory: input.workspace,
sandboxMode: sandboxModeFor(input.writeMode),
approvalPolicy: "never",
model: input.model,
modelReasoningEffort: input.effort as ModelReasoningEffort | undefined,
};
}
export class CodexSdkLocalAgentRuntime implements LocalAgentRuntime {
readonly provider = "codex" as const;
private readonly codex: CodexClientLike;
constructor(codex: CodexClientLike) {
this.codex = codex;
}
async run(input: LocalAgentRunInput): Promise<LocalAgentRunResult> {
const options = threadOptionsFor(input);
const thread = input.providerSessionId
? this.codex.resumeThread(input.providerSessionId, options)
: this.codex.startThread(options);
const turnOptions = input.schema ? { outputSchema: input.schema } : undefined;
let turn: RunResult;
try {
turn = await thread.run(input.prompt, turnOptions);
} catch (error) {
if (input.schema && isNativeSchemaUnsupportedFailure(error)) {
throw new ProviderSchemaUnsupportedError(this.provider, error);
}
throw error;
}
return {
provider: this.provider,
providerSessionId: thread.id,
finalResponse: turn.finalResponse,
items: turn.items,
...(input.schema ? { structured: tryParseJson(turn.finalResponse) } : {}),
};
}
}
function tryParseJson(text: string): unknown | undefined {
try {
return JSON.parse(text) as unknown;
} catch {
return undefined;
}
}
export async function createCodexSdkLocalAgentRuntime(
options?: CodexOptions,
codexFactory?: CodexFactory,
): Promise<CodexSdkLocalAgentRuntime> {
const factory = codexFactory ?? (await defaultCodexFactory());
return new CodexSdkLocalAgentRuntime(factory(options));
}
async function defaultCodexFactory(): Promise<CodexFactory> {
const module = await import("@openai/codex-sdk");
return (options) => new module.Codex(options) as Codex;
}