forked from langchain-ai/deepagentsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-sandbox.ts
More file actions
290 lines (252 loc) · 8.38 KB
/
Copy pathlocal-sandbox.ts
File metadata and controls
290 lines (252 loc) · 8.38 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
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Local Shell Sandbox Example
*
* This example demonstrates the Sandbox Execution Support feature of DeepAgents.
* It shows how to:
* 1. Create a concrete sandbox backend by extending BaseSandbox
* 2. Use the `execute` tool to run shell commands
* 3. Leverage file upload/download capabilities
*
* The LocalShellSandbox runs commands in an isolated working directory,
* perfect for code analysis, project scaffolding, and automation tasks.
*/
import "dotenv/config";
import cp from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { HumanMessage, AIMessage } from "@langchain/core/messages";
import { ChatAnthropic } from "@langchain/anthropic";
import {
createDeepAgent,
BaseSandbox,
type ExecuteResponse,
type FileUploadResponse,
type FileDownloadResponse,
} from "deepagents";
/**
* LocalShellSandbox - A concrete sandbox implementation for local shell execution.
*
* Extends BaseSandbox to provide command execution in a specified working directory.
* All file operations (read, write, ls, grep, glob) are automatically implemented
* by BaseSandbox using shell commands, so we only need to implement:
* - execute(): Run shell commands
* - uploadFiles(): Write files to the sandbox
* - downloadFiles(): Read files from the sandbox
*/
export class LocalShellSandbox extends BaseSandbox {
readonly id: string;
private readonly workingDirectory: string;
private readonly timeout: number;
/**
* Create a new LocalShellSandbox.
*
* @param options - Configuration options
* @param options.workingDirectory - Directory where commands will be executed
* @param options.timeout - Command timeout in milliseconds (default: 30000)
*/
constructor(options: { workingDirectory: string; timeout?: number }) {
super();
this.workingDirectory = path.resolve(options.workingDirectory);
this.timeout = options.timeout ?? 30000;
this.id = `local-shell-${this.workingDirectory.replace(/[^a-zA-Z0-9]/g, "-")}`;
// Ensure working directory exists
if (!fs.existsSync(this.workingDirectory)) {
fs.mkdirSync(this.workingDirectory, { recursive: true });
}
}
/**
* Execute a shell command in the sandbox.
*
* Uses /bin/bash to run commands with proper shell interpretation.
* Captures both stdout and stderr, respects timeout.
*/
async execute(command: string): Promise<ExecuteResponse> {
return new Promise((resolve) => {
const chunks: string[] = [];
let truncated = false;
const maxOutputBytes = 1024 * 1024; // 1MB output limit
let totalBytes = 0;
const child = cp.spawn("/bin/bash", ["-c", command], {
cwd: this.workingDirectory,
env: { ...process.env, HOME: process.env.HOME },
});
const collectOutput = (data: Buffer) => {
const str = data.toString();
totalBytes += data.byteLength;
if (totalBytes <= maxOutputBytes) {
chunks.push(str);
} else {
truncated = true;
}
};
child.stdout.on("data", collectOutput);
child.stderr.on("data", collectOutput);
// Handle timeout
const timer = setTimeout(() => {
child.kill("SIGTERM");
resolve({
output: chunks.join("") + "\n[Command timed out]",
exitCode: null,
truncated,
});
}, this.timeout);
child.on("close", (exitCode) => {
clearTimeout(timer);
resolve({
output: chunks.join(""),
exitCode,
truncated,
});
});
child.on("error", (err) => {
clearTimeout(timer);
resolve({
output: `Error spawning process: ${err.message}`,
exitCode: 1,
truncated: false,
});
});
});
}
/**
* Upload files to the sandbox.
*
* Writes files to the working directory, creating parent directories as needed.
*/
async uploadFiles(
files: Array<[string, Uint8Array]>,
): Promise<FileUploadResponse[]> {
const results: FileUploadResponse[] = [];
for (const [filePath, content] of files) {
try {
const fullPath = path.join(this.workingDirectory, filePath);
const parentDir = path.dirname(fullPath);
// Ensure parent directory exists
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true });
}
fs.writeFileSync(fullPath, content);
results.push({ path: filePath, error: null });
} catch (err) {
const error = err as NodeJS.ErrnoException;
if (error.code === "EACCES") {
results.push({ path: filePath, error: "permission_denied" });
} else if (error.code === "EISDIR") {
results.push({ path: filePath, error: "is_directory" });
} else {
results.push({ path: filePath, error: "invalid_path" });
}
}
}
return results;
}
/**
* Download files from the sandbox.
*
* Reads files from the working directory.
*/
async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {
const results: FileDownloadResponse[] = [];
for (const filePath of paths) {
try {
const fullPath = path.join(this.workingDirectory, filePath);
if (!fs.existsSync(fullPath)) {
results.push({
path: filePath,
content: null,
error: "file_not_found",
});
continue;
}
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
results.push({
path: filePath,
content: null,
error: "is_directory",
});
continue;
}
const content = fs.readFileSync(fullPath);
results.push({
path: filePath,
content: new Uint8Array(content),
error: null,
});
} catch (err) {
const error = err as NodeJS.ErrnoException;
if (error.code === "EACCES") {
results.push({
path: filePath,
content: null,
error: "permission_denied",
});
} else {
results.push({
path: filePath,
content: null,
error: "file_not_found",
});
}
}
}
return results;
}
}
// System prompt that leverages the execute capability
const systemPrompt = `You are a powerful coding assistant with access to a sandboxed shell environment.
You can execute shell commands to:
- Analyze code and projects (e.g., find patterns, count lines, check dependencies)
- Run build tools and scripts (npm, pip, make, etc.)
- Scaffold new projects
- Run tests and linters
- Manipulate files and directories
## Tools Available
- **execute**: Run any shell command and see the output
- **ls**: List directory contents
- **read_file**: Read file contents
- **write_file**: Create new files
- **edit_file**: Modify existing files
- **grep**: Search for patterns in files
- **glob**: Find files matching patterns
## Best Practices
1. Start by exploring the workspace: \`ls\` or \`execute("ls -la")\`
2. Use the right tool for the job:
- Use \`execute\` for complex commands, pipelines, and running programs
- Use \`read_file\` for viewing file contents
- Use \`write_file\` for creating new files
3. Chain commands when needed: \`execute("npm install && npm test")\`
4. Check exit codes to verify success
You're working in an isolated sandbox, so feel free to experiment!`;
// Create the sandbox pointing to a workspace directory
const workspaceDir = path.join(process.cwd(), "sandbox-workspace");
const sandbox = new LocalShellSandbox({ workingDirectory: workspaceDir });
// Create the agent with sandbox backend
export const agent = createDeepAgent({
model: new ChatAnthropic({
model: "claude-haiku-4-5",
temperature: 0,
}),
systemPrompt,
backend: sandbox,
});
console.log(`🚀 Starting sandbox agent with workspace: ${workspaceDir}\n`);
const result = await agent.invoke(
{
messages: [
new HumanMessage(
`Create a simple Node.js project with a hello.js file that prints "Hello from DeepAgents!".
Then run it with node to verify it works.
Finally, show me the output.`,
),
],
},
{ recursionLimit: 50 },
);
// Show the final AI response
const messages = result.messages;
const lastAIMessage = messages.findLast(AIMessage.isInstance);
if (lastAIMessage) {
console.log("\n📝 Agent Response:\n");
console.log(lastAIMessage.content);
}