-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathai.ts
More file actions
150 lines (137 loc) · 4.36 KB
/
ai.ts
File metadata and controls
150 lines (137 loc) · 4.36 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
/**
* Vercel AI SDK adapter for Bashkit.
*
* Returns `{ system, tools }` that plugs directly into `generateText()` /
* `streamText()` with zero boilerplate. Tools include built-in `execute`
* functions, so the AI SDK auto-executes tool calls in its `maxSteps` loop.
*
* @example
* ```typescript
* import { generateText } from "ai";
* import { anthropic } from "@ai-sdk/anthropic";
* import { bashTool } from "@everruns/bashkit/ai";
*
* const bash = bashTool({
* files: { "/home/user/data.csv": "name,age\nAlice,30\nBob,25" },
* });
*
* const { text } = await generateText({
* model: anthropic("claude-haiku-4-5-20251001"),
* system: bash.system,
* tools: bash.tools,
* maxSteps: 5,
* prompt: "Analyze the CSV file and tell me the average age",
* });
* ```
*
* @packageDocumentation
*/
import { BashTool } from "./wrapper.js";
import type { BashOptions, ExecResult } from "./wrapper.js";
// Vercel AI SDK tool types — we define them inline to avoid requiring
// the `ai` package as a dependency (it's a peer dependency).
// These match the ai@4.x Tool interface.
/** Vercel AI SDK CoreTool-compatible object. */
interface AiTool {
type?: "function";
description: string;
parameters: {
type: "object";
properties: Record<string, unknown>;
required: string[];
additionalProperties?: boolean;
$schema?: string;
};
execute: (args: Record<string, unknown>) => Promise<string>;
}
/** 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>;
}
/** Return value of `bashTool()`. */
export interface BashToolAdapter {
/** System prompt describing bash capabilities and constraints. */
system: string;
/** Tool definitions for Vercel AI SDK's generateText/streamText. */
tools: Record<string, AiTool>;
/** The underlying BashTool instance for direct access. */
bash: BashTool;
}
function formatOutput(result: ExecResult): 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}]`;
}
return output || "(no output)";
}
/**
* Create a bash tool adapter for the Vercel AI SDK.
*
* Returns `{ system, tools }` that plugs directly into `generateText()` or
* `streamText()`. The tool includes a built-in `execute` function, so tool
* calls are auto-executed when using `maxSteps`.
*
* @param options - Configuration for the bash interpreter
*
* @example
* ```typescript
* import { generateText } from "ai";
* import { anthropic } from "@ai-sdk/anthropic";
* import { bashTool } from "@everruns/bashkit/ai";
*
* const bash = bashTool({ files: { "/test.txt": "hello world" } });
*
* const { text } = await generateText({
* model: anthropic("claude-haiku-4-5-20251001"),
* system: bash.system,
* tools: bash.tools,
* maxSteps: 3,
* prompt: "Read /test.txt and tell me what it says",
* });
* ```
*/
export function bashTool(options?: BashToolOptions): BashToolAdapter {
const { files, ...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: Record<string, AiTool> = {
bash: {
description: bash.description(),
parameters: {
type: "object",
properties: {
commands: {
type: "string",
description:
"Bash commands to execute. State persists between calls.",
},
},
required: ["commands"],
additionalProperties: false,
$schema: "http://json-schema.org/draft-07/schema#",
},
execute: async (args: Record<string, unknown>): Promise<string> => {
const commands = args.commands as string;
if (!commands) {
return "Error: missing 'commands' parameter";
}
try {
const result = await bash.execute(commands);
return formatOutput(result);
} catch (err) {
return `Execution error: ${err instanceof Error ? err.message : String(err)}`;
}
},
},
};
return { system, tools, bash };
}