@langchain/daytona 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) LangChain, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,354 @@
1
+ # @langchain/daytona
2
+
3
+ Daytona Sandbox backend for [deepagents](https://www.npmjs.com/package/deepagents). This package provides a `DaytonaSandbox` implementation of the `SandboxBackendProtocol`, enabling agents to execute commands, read/write files, and manage isolated sandbox environments using Daytona's infrastructure.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@langchain/daytona.svg)](https://www.npmjs.com/package/@langchain/daytona)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Features
9
+
10
+ - **Isolated Execution**: Run commands in secure, isolated sandbox environments
11
+ - **Multi-Language Support**: TypeScript, JavaScript, and Python runtimes
12
+ - **File Operations**: Upload and download files with full filesystem access
13
+ - **BaseSandbox Integration**: All inherited methods (`read`, `write`, `edit`, `ls`, `grep`, `glob`) work out of the box
14
+ - **Factory Pattern**: Compatible with deepagents' middleware architecture
15
+ - **Full SDK Access**: Access the underlying Daytona SDK via the `sandbox` property for advanced features
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ # npm
21
+ npm install @langchain/daytona
22
+
23
+ # yarn
24
+ yarn add @langchain/daytona
25
+
26
+ # pnpm
27
+ pnpm add @langchain/daytona
28
+ ```
29
+
30
+ ## Authentication Setup
31
+
32
+ The package requires Daytona API authentication:
33
+
34
+ ### Environment Variable (Recommended)
35
+
36
+ 1. Go to [https://app.daytona.io](https://app.daytona.io)
37
+ 2. Create an account and get your API key
38
+ 3. Set it as an environment variable:
39
+
40
+ ```bash
41
+ export DAYTONA_API_KEY=your_api_key_here
42
+ ```
43
+
44
+ ### Explicit API Key in Code
45
+
46
+ ```typescript
47
+ const sandbox = await DaytonaSandbox.create({
48
+ auth: { apiKey: "your-api-key-here" },
49
+ });
50
+ ```
51
+
52
+ ## Basic Usage
53
+
54
+ ```typescript
55
+ import { createDeepAgent } from "deepagents";
56
+ import { ChatAnthropic } from "@langchain/anthropic";
57
+ import { DaytonaSandbox } from "@langchain/daytona";
58
+
59
+ // Create and initialize the sandbox
60
+ const sandbox = await DaytonaSandbox.create({
61
+ language: "typescript",
62
+ timeout: 300, // 5 minutes
63
+ });
64
+
65
+ try {
66
+ const agent = createDeepAgent({
67
+ model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
68
+ systemPrompt: "You are a coding assistant with access to a sandbox.",
69
+ backend: sandbox,
70
+ });
71
+
72
+ const result = await agent.invoke({
73
+ messages: [
74
+ {
75
+ role: "user",
76
+ content: "Create a hello world TypeScript app and run it",
77
+ },
78
+ ],
79
+ });
80
+ } finally {
81
+ await sandbox.close();
82
+ }
83
+ ```
84
+
85
+ ## Configuration Options
86
+
87
+ ```typescript
88
+ interface DaytonaSandboxOptions {
89
+ /**
90
+ * Primary language for code execution.
91
+ * @default "typescript"
92
+ */
93
+ language?: "typescript" | "python" | "javascript";
94
+
95
+ /**
96
+ * Environment variables to set in the sandbox.
97
+ */
98
+ envVars?: Record<string, string>;
99
+
100
+ /**
101
+ * Custom Docker image to use (e.g., "node:20", "python:3.12").
102
+ * Required when you want to customize resources.
103
+ */
104
+ image?: string;
105
+
106
+ /**
107
+ * Snapshot name to use for the sandbox.
108
+ * Cannot be used together with `image`.
109
+ */
110
+ snapshot?: string;
111
+
112
+ /**
113
+ * Resource allocation (only available when using `image`).
114
+ */
115
+ resources?: {
116
+ cpu?: number; // Number of CPUs
117
+ memory?: number; // Memory in GiB
118
+ disk?: number; // Disk space in GiB
119
+ };
120
+
121
+ /**
122
+ * Target region.
123
+ * @default "us"
124
+ */
125
+ target?: "us" | "eu";
126
+
127
+ /**
128
+ * Auto-stop interval in minutes. Set to 0 to disable.
129
+ * @default 15
130
+ */
131
+ autoStopInterval?: number;
132
+
133
+ /**
134
+ * Default timeout for command execution in seconds.
135
+ * @default 300
136
+ */
137
+ timeout?: number;
138
+
139
+ /**
140
+ * Custom labels for the sandbox.
141
+ */
142
+ labels?: Record<string, string>;
143
+
144
+ /**
145
+ * Authentication configuration.
146
+ */
147
+ auth?: {
148
+ apiKey?: string;
149
+ apiUrl?: string;
150
+ };
151
+ }
152
+ ```
153
+
154
+ ### Using Custom Resources
155
+
156
+ To customize CPU, memory, or disk, you must specify a Docker image:
157
+
158
+ ```typescript
159
+ const sandbox = await DaytonaSandbox.create({
160
+ image: "node:20",
161
+ language: "typescript",
162
+ resources: {
163
+ cpu: 4,
164
+ memory: 8,
165
+ disk: 50,
166
+ },
167
+ });
168
+ ```
169
+
170
+ ## Available Regions
171
+
172
+ | Region | Location |
173
+ | ------ | ------------- |
174
+ | `us` | United States |
175
+ | `eu` | Europe |
176
+
177
+ ## Accessing the Daytona SDK
178
+
179
+ For advanced features not exposed by `BaseSandbox`, you can access the underlying Daytona SDK directly via the `sandbox` property:
180
+
181
+ ```typescript
182
+ const daytonaSandbox = await DaytonaSandbox.create();
183
+
184
+ // Access the raw Daytona SDK
185
+ const sdk = daytonaSandbox.sandbox;
186
+
187
+ // Use any Daytona SDK feature directly
188
+ const workDir = await sdk.getWorkDir();
189
+ const homeDir = await sdk.getUserHomeDir();
190
+
191
+ // Execute code with the process interface
192
+ const result = await sdk.process.executeCommand("npm install");
193
+
194
+ // Use the filesystem interface
195
+ await sdk.fs.createFolder("src", "755");
196
+ await sdk.fs.uploadFile(Buffer.from("content"), "src/index.ts");
197
+ ```
198
+
199
+ See the [@daytonaio/sdk documentation](https://www.npmjs.com/package/@daytonaio/sdk) for all available SDK methods.
200
+
201
+ ## Factory Functions
202
+
203
+ ### Creating New Sandboxes Per Invocation
204
+
205
+ ```typescript
206
+ import { createDaytonaSandboxFactory } from "@langchain/daytona";
207
+
208
+ // Each call creates a new sandbox
209
+ const factory = createDaytonaSandboxFactory({ language: "typescript" });
210
+
211
+ const sandbox1 = await factory();
212
+ const sandbox2 = await factory();
213
+
214
+ try {
215
+ // Use sandboxes...
216
+ } finally {
217
+ await sandbox1.close();
218
+ await sandbox2.close();
219
+ }
220
+ ```
221
+
222
+ ### Reusing an Existing Sandbox
223
+
224
+ ```typescript
225
+ import { createDeepAgent, createFilesystemMiddleware } from "deepagents";
226
+ import {
227
+ DaytonaSandbox,
228
+ createDaytonaSandboxFactoryFromSandbox,
229
+ } from "@langchain/daytona";
230
+
231
+ // Create and initialize a sandbox
232
+ const sandbox = await DaytonaSandbox.create({ language: "typescript" });
233
+
234
+ try {
235
+ const agent = createDeepAgent({
236
+ model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
237
+ systemPrompt: "You are a coding assistant.",
238
+ middlewares: [
239
+ createFilesystemMiddleware({
240
+ backend: createDaytonaSandboxFactoryFromSandbox(sandbox),
241
+ }),
242
+ ],
243
+ });
244
+
245
+ await agent.invoke({ messages: [...] });
246
+ } finally {
247
+ await sandbox.close();
248
+ }
249
+ ```
250
+
251
+ ## Reconnecting to Existing Sandboxes
252
+
253
+ Resume working with a sandbox that is still running:
254
+
255
+ ```typescript
256
+ // First session: create sandbox
257
+ const sandbox = await DaytonaSandbox.create({
258
+ language: "typescript",
259
+ autoStopInterval: 60, // Keep alive for 60 minutes of inactivity
260
+ });
261
+ const sandboxId = sandbox.id;
262
+
263
+ // Stop the sandbox (keeps it available)
264
+ await sandbox.stop();
265
+
266
+ // Later: reconnect to the same sandbox
267
+ const reconnected = await DaytonaSandbox.connect(sandboxId);
268
+ await reconnected.start(); // Restart the sandbox
269
+ const result = await reconnected.execute("ls -la");
270
+ ```
271
+
272
+ ## Sandbox Lifecycle
273
+
274
+ ```typescript
275
+ const sandbox = await DaytonaSandbox.create();
276
+
277
+ // Stop sandbox (can be restarted)
278
+ await sandbox.stop();
279
+
280
+ // Start a stopped sandbox
281
+ await sandbox.start();
282
+
283
+ // Delete sandbox permanently
284
+ await sandbox.close();
285
+
286
+ // Or use kill() as an alias
287
+ await sandbox.kill();
288
+ ```
289
+
290
+ ## Error Handling
291
+
292
+ ```typescript
293
+ import { DaytonaSandboxError } from "@langchain/daytona";
294
+
295
+ try {
296
+ await sandbox.execute("some command");
297
+ } catch (error) {
298
+ if (error instanceof DaytonaSandboxError) {
299
+ switch (error.code) {
300
+ case "NOT_INITIALIZED":
301
+ await sandbox.initialize();
302
+ break;
303
+ case "COMMAND_TIMEOUT":
304
+ console.error("Command took too long");
305
+ break;
306
+ case "AUTHENTICATION_FAILED":
307
+ console.error("Check your Daytona API key");
308
+ break;
309
+ default:
310
+ throw error;
311
+ }
312
+ }
313
+ }
314
+ ```
315
+
316
+ ### Error Codes
317
+
318
+ | Code | Description |
319
+ | ------------------------- | ------------------------------------------- |
320
+ | `NOT_INITIALIZED` | Sandbox not initialized - call initialize() |
321
+ | `ALREADY_INITIALIZED` | Cannot initialize twice |
322
+ | `AUTHENTICATION_FAILED` | Invalid or missing Daytona API key |
323
+ | `SANDBOX_CREATION_FAILED` | Failed to create sandbox |
324
+ | `SANDBOX_NOT_FOUND` | Sandbox ID not found or deleted |
325
+ | `SANDBOX_NOT_STARTED` | Sandbox is not in started state |
326
+ | `COMMAND_TIMEOUT` | Command execution timed out |
327
+ | `COMMAND_FAILED` | Command execution failed |
328
+ | `FILE_OPERATION_FAILED` | File read/write failed |
329
+ | `RESOURCE_LIMIT_EXCEEDED` | CPU, memory, or storage limits exceeded |
330
+
331
+ ## Inherited BaseSandbox Methods
332
+
333
+ `DaytonaSandbox` extends `BaseSandbox` and inherits these convenience methods:
334
+
335
+ | Method | Description |
336
+ | ------------ | ----------------------------- |
337
+ | `read()` | Read a file's contents |
338
+ | `write()` | Write content to a file |
339
+ | `edit()` | Replace text in a file |
340
+ | `lsInfo()` | List directory contents |
341
+ | `grepRaw()` | Search for patterns in files |
342
+ | `globInfo()` | Find files matching a pattern |
343
+
344
+ ## Environment Variables
345
+
346
+ | Variable | Description |
347
+ | ----------------- | ----------------------------- |
348
+ | `DAYTONA_API_KEY` | Daytona API key (required) |
349
+ | `DAYTONA_API_URL` | Custom Daytona API URL |
350
+ | `DAYTONA_TARGET` | Default target region (us/eu) |
351
+
352
+ ## License
353
+
354
+ MIT