-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathopenai.ts
More file actions
278 lines (258 loc) · 7.42 KB
/
openai.ts
File metadata and controls
278 lines (258 loc) · 7.42 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* OpenAI SDK adapter for Bashkit.
*
* Returns a ready-to-use `{ system, tools, handler }` object for OpenAI's
* `chat.completions.create()` API.
*
* @example
* ```typescript
* import OpenAI from "openai";
* import { bashTool } from "@everruns/bashkit/openai";
*
* const client = new OpenAI();
* const bash = bashTool();
*
* const response = await client.chat.completions.create({
* model: "gpt-4.1-mini",
* tools: bash.tools,
* messages: [
* { role: "system", content: bash.system },
* { role: "user", content: "Create a file with today's date" },
* ],
* });
*
* for (const call of response.choices[0].message.tool_calls ?? []) {
* const result = await bash.handler(call);
* // send result back as tool message
* }
* ```
*
* @packageDocumentation
*/
import { BashTool } from "./wrapper.js";
import type { BashOptions, ExecResult } from "./wrapper.js";
/** Options for configuring the bash tool adapter. */
export interface BashToolOptions extends Omit<BashOptions, "files"> {
/** Pre-populate VFS files. Keys are absolute paths, values are file contents. */
files?: Record<string, string>;
/**
* Execution timeout in milliseconds.
*
* When set, this is passed to the underlying BashTool as `timeoutMs`.
* Commands exceeding this duration are aborted with exit code 124.
* Framework-level timeouts can be propagated here to ensure bashkit
* stops execution when the framework cancels a tool call.
*/
timeoutMs?: number;
/**
* Maximum output length in characters (default: 100000).
*
* Output exceeding this limit is truncated with a `[truncated]` marker.
* Prevents context window flooding when scripts produce large output.
*/
maxOutputLength?: number;
/**
* Wrap tool output in XML boundary markers (default: false).
*
* When enabled, output is wrapped in `<tool_output>...</tool_output>` tags
* to help LLMs distinguish tool output data from instructions, reducing
* prompt injection risk via tool output.
*/
sanitizeOutput?: boolean;
}
/** OpenAI function tool definition (matches the `tools` array in chat.completions.create). */
interface OpenAITool {
type: "function";
function: {
name: string;
description: string;
parameters: {
type: "object";
properties: Record<string, unknown>;
required: string[];
};
};
}
/** OpenAI tool_call from a chat completion response. */
interface OpenAIToolCall {
id: string;
type: "function";
function: {
name: string;
arguments: string;
};
}
/** Result from handling a tool call, ready to send as a tool message. */
export interface ToolResult {
role: "tool";
tool_call_id: string;
content: string;
}
/** Options for handler invocation. */
export interface HandlerOptions {
/** AbortSignal to cancel execution when the framework aborts the tool call. */
signal?: AbortSignal;
}
/** Return value of `bashTool()`. */
export interface BashToolAdapter {
/** System prompt describing bash capabilities and constraints. */
system: string;
/** Tool definitions for OpenAI's chat.completions.create() API. */
tools: OpenAITool[];
/**
* Handler that executes a tool_call and returns a tool message.
*
* Pass an AbortSignal via the options parameter to cancel execution
* when the framework aborts the tool call:
*
* ```typescript
* const controller = new AbortController();
* const result = await bash.handler(call, { signal: controller.signal });
* ```
*/
handler: (
toolCall: OpenAIToolCall,
options?: HandlerOptions,
) => Promise<ToolResult>;
/** The underlying BashTool instance for direct access. */
bash: BashTool;
}
const DEFAULT_MAX_OUTPUT_LENGTH = 100_000;
function formatOutput(
result: ExecResult,
maxOutputLength: number = DEFAULT_MAX_OUTPUT_LENGTH,
sanitize: boolean = false,
): string {
let output = result.stdout;
if (result.stderr) {
output += (output ? "\n" : "") + `STDERR: ${result.stderr}`;
}
if (result.exitCode !== 0) {
output += (output ? "\n" : "") + `[Exit code: ${result.exitCode}]`;
}
output = output || "(no output)";
if (output.length > maxOutputLength) {
output = output.slice(0, maxOutputLength) + "\n[truncated]";
}
if (sanitize) {
output = `<tool_output>\n${output}\n</tool_output>`;
}
return output;
}
/**
* Create a bash tool adapter for the OpenAI SDK.
*
* Returns `{ system, tools, handler }` that plugs directly into
* `client.chat.completions.create()`.
*
* @param options - Configuration for the bash interpreter
*
* @example
* ```typescript
* import OpenAI from "openai";
* import { bashTool } from "@everruns/bashkit/openai";
*
* const client = new OpenAI();
* const bash = bashTool({ files: { "/data.txt": "42" } });
*
* const response = await client.chat.completions.create({
* model: "gpt-4.1-nano",
* tools: bash.tools,
* messages: [
* { role: "system", content: bash.system },
* { role: "user", content: "What's in /data.txt?" },
* ],
* });
* ```
*/
export function bashTool(options?: BashToolOptions): BashToolAdapter {
const { files, maxOutputLength, sanitizeOutput, ...bashOptions } =
options ?? {};
const bash = new BashTool(bashOptions);
if (files) {
for (const [path, content] of Object.entries(files)) {
bash.writeFile(path, content);
}
}
const system = bash.systemPrompt();
const tools: OpenAITool[] = [
{
type: "function",
function: {
name: "bash",
description: bash.description(),
parameters: {
type: "object",
properties: {
commands: {
type: "string",
description:
"Bash commands to execute. State persists between calls.",
},
},
required: ["commands"],
},
},
},
];
const handler = async (
toolCall: OpenAIToolCall,
handlerOptions?: HandlerOptions,
): Promise<ToolResult> => {
let commands: string;
try {
const args = JSON.parse(toolCall.function.arguments);
commands = args.commands;
} catch {
return {
role: "tool",
tool_call_id: toolCall.id,
content: "Error: invalid JSON in function arguments",
};
}
if (!commands) {
return {
role: "tool",
tool_call_id: toolCall.id,
content: "Error: missing 'commands' parameter",
};
}
// Wire up AbortSignal to cancel bashkit execution when the
// framework (or caller) aborts the tool call.
const signal = handlerOptions?.signal;
if (signal?.aborted) {
return {
role: "tool",
tool_call_id: toolCall.id,
content: "Execution cancelled",
};
}
let onAbort: (() => void) | undefined;
if (signal) {
onAbort = () => bash.cancel();
signal.addEventListener("abort", onAbort, { once: true });
}
try {
const result = await bash.execute(commands);
return {
role: "tool",
tool_call_id: toolCall.id,
content: formatOutput(result, maxOutputLength, sanitizeOutput),
};
} catch (err) {
return {
role: "tool",
tool_call_id: toolCall.id,
content: `Execution error: ${err instanceof Error ? err.message : String(err)}`,
};
} finally {
if (signal && onAbort) {
signal.removeEventListener("abort", onAbort);
if (signal.aborted) {
bash.clearCancel();
}
}
}
};
return { system, tools, handler, bash };
}