@daytonaio/sdk 0.12.2-alpha.1 → 0.13.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/README.md CHANGED
@@ -30,7 +30,7 @@ const daytona = new Daytona()
30
30
  const sandbox = await daytona.create()
31
31
 
32
32
  // Run code in the sandbox
33
- const response = await sandbox.process.codeRun('typescript', 'console.log("Hello World!")')
33
+ const response = await sandbox.process.codeRun('console.log("Hello World!")')
34
34
  console.log(response.result)
35
35
 
36
36
  // Clean up when done
@@ -86,14 +86,11 @@ const response = await sandbox.process.executeCommand('echo "Hello, World!"')
86
86
  console.log(response.result)
87
87
 
88
88
  // Run TypeScript code
89
- const response = await sandbox.process.codeRun(
90
- 'typescript',
91
- `
92
- const x = 10
93
- const y = 20
94
- console.log(\`Sum: \${x + y}\`)
95
- `
96
- )
89
+ const response = await sandbox.process.codeRun(`
90
+ const x = 10
91
+ const y = 20
92
+ console.log(\`Sum: \${x + y}\`)
93
+ `)
97
94
  console.log(response.result)
98
95
  ```
99
96
 
package/dist/Daytona.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Sandbox, Sandbox as Workspace } from './Sandbox';
2
1
  import { CreateWorkspaceTargetEnum as SandboxTargetRegion } from '@daytonaio/api-client';
2
+ import { Sandbox, Sandbox as Workspace } from './Sandbox';
3
3
  /**
4
4
  * Configuration options for initializing the Daytona client.
5
5
  *
@@ -38,6 +38,14 @@ export interface DaytonaConfig {
38
38
  /** Target environment for sandboxes */
39
39
  target?: SandboxTargetRegion;
40
40
  }
41
+ /**
42
+ * Supported programming languages for code execution
43
+ */
44
+ export declare enum CodeLanguage {
45
+ PYTHON = "python",
46
+ TYPESCRIPT = "typescript",
47
+ JAVASCRIPT = "javascript"
48
+ }
41
49
  /**
42
50
  * Resource allocation for a Sandbox.
43
51
  *
@@ -71,6 +79,7 @@ export interface SandboxResources {
71
79
  * @property {string} [id] - Optional Sandbox ID. If not provided, a random ID will be generated
72
80
  * @property {string} [image] - Optional Docker image to use for the Sandbox
73
81
  * @property {string} [user] - Optional os user to use for the Sandbox
82
+ * @property {CodeLanguage | string} [language] - Programming language for direct code execution
74
83
  * @property {Record<string, string>} [envVars] - Optional environment variables to set in the Sandbox
75
84
  * @property {Record<string, string>} [labels] - Sandbox labels
76
85
  * @property {boolean} [public] - Is the Sandbox port preview public
@@ -82,6 +91,7 @@ export interface SandboxResources {
82
91
  *
83
92
  * @example
84
93
  * const params: CreateSandboxParams = {
94
+ * language: 'typescript',
85
95
  * envVars: { NODE_ENV: 'development' },
86
96
  * resources: {
87
97
  * cpu: 2,
@@ -98,6 +108,8 @@ export type CreateSandboxParams = {
98
108
  image?: string;
99
109
  /** Optional os user to use for the Sandbox */
100
110
  user?: string;
111
+ /** Programming language for direct code execution */
112
+ language?: CodeLanguage | string;
101
113
  /** Optional environment variables to set in the sandbox */
102
114
  envVars?: Record<string, string>;
103
115
  /** Sandbox labels */
@@ -159,7 +171,7 @@ export declare class Daytona {
159
171
  constructor(config?: DaytonaConfig);
160
172
  /**
161
173
  * Creates Sandboxes with default or custom configurations. You can specify various parameters,
162
- * including image, resources, environment variables, and volumes for the Sandbox.
174
+ * including language, image, resources, environment variables, and volumes for the Sandbox.
163
175
  *
164
176
  * @param {CreateSandboxParams} [params] - Parameters for Sandbox creation
165
177
  * @param {number} [timeout] - Timeout in seconds (0 means no timeout, default is 60)
@@ -172,6 +184,7 @@ export declare class Daytona {
172
184
  * @example
173
185
  * // Create a custom sandbox
174
186
  * const params: CreateSandboxParams = {
187
+ * language: 'typescript',
175
188
  * image: 'node:18',
176
189
  * envVars: {
177
190
  * NODE_ENV: 'development',
@@ -265,4 +278,13 @@ export declare class Daytona {
265
278
  * console.log(`Current sandbox state: ${sandbox.instance.state}`);
266
279
  */
267
280
  getCurrentSandbox(sandboxId: string): Promise<Sandbox>;
281
+ /**
282
+ * Gets the appropriate code toolbox based on language.
283
+ *
284
+ * @private
285
+ * @param {CodeLanguage} [language] - Programming language for the toolbox
286
+ * @returns {SandboxCodeToolbox} The appropriate code toolbox instance
287
+ * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
288
+ */
289
+ private getCodeToolbox;
268
290
  }
package/dist/Daytona.js CHANGED
@@ -36,12 +36,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.Daytona = void 0;
40
- const Sandbox_1 = require("./Sandbox");
39
+ exports.Daytona = exports.CodeLanguage = void 0;
41
40
  const api_client_1 = require("@daytonaio/api-client");
42
41
  const axios_1 = __importStar(require("axios"));
43
- const DaytonaError_1 = require("./errors/DaytonaError");
44
42
  const dotenv_1 = __importDefault(require("dotenv"));
43
+ const SandboxPythonCodeToolbox_1 = require("./code-toolbox/SandboxPythonCodeToolbox");
44
+ const SandboxTsCodeToolbox_1 = require("./code-toolbox/SandboxTsCodeToolbox");
45
+ const DaytonaError_1 = require("./errors/DaytonaError");
46
+ const Sandbox_1 = require("./Sandbox");
47
+ /**
48
+ * Supported programming languages for code execution
49
+ */
50
+ var CodeLanguage;
51
+ (function (CodeLanguage) {
52
+ CodeLanguage["PYTHON"] = "python";
53
+ CodeLanguage["TYPESCRIPT"] = "typescript";
54
+ CodeLanguage["JAVASCRIPT"] = "javascript";
55
+ })(CodeLanguage || (exports.CodeLanguage = CodeLanguage = {}));
45
56
  /**
46
57
  * Main class for interacting with the Daytona API.
47
58
  *
@@ -77,7 +88,7 @@ class Daytona {
77
88
  var _a, _b, _c, _d, _e, _f, _g, _h;
78
89
  dotenv_1.default.config();
79
90
  dotenv_1.default.config({ path: '.env.local', override: true });
80
- const apiKey = (config === null || config === void 0 ? void 0 : config.apiKey) || ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.DAYTONA_API_KEY);
91
+ const apiKey = !(config === null || config === void 0 ? void 0 : config.apiKey) && (config === null || config === void 0 ? void 0 : config.jwtToken) ? undefined : (config === null || config === void 0 ? void 0 : config.apiKey) || ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.DAYTONA_API_KEY);
81
92
  const jwtToken = (config === null || config === void 0 ? void 0 : config.jwtToken) || ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.DAYTONA_JWT_TOKEN);
82
93
  const organizationId = (config === null || config === void 0 ? void 0 : config.organizationId) || ((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c.DAYTONA_ORGANIZATION_ID);
83
94
  if (!apiKey && !jwtToken) {
@@ -127,6 +138,12 @@ class Daytona {
127
138
  else {
128
139
  errorMessage = ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || ((_c = error.response) === null || _c === void 0 ? void 0 : _c.data) || error.message || String(error);
129
140
  }
141
+ try {
142
+ errorMessage = JSON.stringify(errorMessage);
143
+ }
144
+ catch (_d) {
145
+ errorMessage = String(errorMessage);
146
+ }
130
147
  throw new DaytonaError_1.DaytonaError(errorMessage);
131
148
  });
132
149
  this.sandboxApi = new api_client_1.WorkspaceApi(configuration, '', axiosInstance);
@@ -134,7 +151,7 @@ class Daytona {
134
151
  }
135
152
  /**
136
153
  * Creates Sandboxes with default or custom configurations. You can specify various parameters,
137
- * including image, resources, environment variables, and volumes for the Sandbox.
154
+ * including language, image, resources, environment variables, and volumes for the Sandbox.
138
155
  *
139
156
  * @param {CreateSandboxParams} [params] - Parameters for Sandbox creation
140
157
  * @param {number} [timeout] - Timeout in seconds (0 means no timeout, default is 60)
@@ -147,6 +164,7 @@ class Daytona {
147
164
  * @example
148
165
  * // Create a custom sandbox
149
166
  * const params: CreateSandboxParams = {
167
+ * language: 'typescript',
150
168
  * image: 'node:18',
151
169
  * envVars: {
152
170
  * NODE_ENV: 'development',
@@ -164,7 +182,11 @@ class Daytona {
164
182
  // const startTime = Date.now();
165
183
  var _a, _b, _c, _d;
166
184
  if (params == null) {
167
- params = {};
185
+ params = { language: 'python' };
186
+ }
187
+ const labels = params.labels || {};
188
+ if (params.language) {
189
+ labels['code-toolbox-language'] = params.language;
168
190
  }
169
191
  // remove this when params.timeout is removed
170
192
  const effectiveTimeout = params.timeout || timeout;
@@ -175,6 +197,7 @@ class Daytona {
175
197
  (!Number.isInteger(params.autoStopInterval) || params.autoStopInterval < 0)) {
176
198
  throw new DaytonaError_1.DaytonaError('autoStopInterval must be a non-negative integer');
177
199
  }
200
+ const codeToolbox = this.getCodeToolbox(params.language);
178
201
  try {
179
202
  const response = await this.sandboxApi.createWorkspace({
180
203
  id: params.id,
@@ -196,7 +219,7 @@ class Daytona {
196
219
  const sandboxInstance = response.data;
197
220
  const sandboxInfo = Sandbox_1.Sandbox.toSandboxInfo(sandboxInstance);
198
221
  sandboxInstance.info = sandboxInfo;
199
- const sandbox = new Sandbox_1.Sandbox(sandboxInstance.id, sandboxInstance, this.sandboxApi, this.toolboxApi);
222
+ const sandbox = new Sandbox_1.Sandbox(sandboxInstance.id, sandboxInstance, this.sandboxApi, this.toolboxApi, codeToolbox);
200
223
  // if (!params.async) {
201
224
  // const timeElapsed = Date.now() - startTime;
202
225
  // await sandbox.waitUntilStarted(effectiveTimeout ? effectiveTimeout - (timeElapsed / 1000) : 0);
@@ -224,9 +247,11 @@ class Daytona {
224
247
  async get(sandboxId) {
225
248
  const response = await this.sandboxApi.getWorkspace(sandboxId);
226
249
  const sandboxInstance = response.data;
250
+ const language = sandboxInstance.labels && sandboxInstance.labels['code-toolbox-language'];
251
+ const codeToolbox = this.getCodeToolbox(language);
227
252
  const sandboxInfo = Sandbox_1.Sandbox.toSandboxInfo(sandboxInstance);
228
253
  sandboxInstance.info = sandboxInfo;
229
- return new Sandbox_1.Sandbox(sandboxId, sandboxInstance, this.sandboxApi, this.toolboxApi);
254
+ return new Sandbox_1.Sandbox(sandboxId, sandboxInstance, this.sandboxApi, this.toolboxApi, codeToolbox);
230
255
  }
231
256
  /**
232
257
  * Lists all Sandboxes.
@@ -242,9 +267,11 @@ class Daytona {
242
267
  async list() {
243
268
  const response = await this.sandboxApi.listWorkspaces();
244
269
  return response.data.map((sandbox) => {
270
+ var _a;
271
+ const language = (_a = sandbox.labels) === null || _a === void 0 ? void 0 : _a['code-toolbox-language'];
245
272
  const sandboxInfo = Sandbox_1.Sandbox.toSandboxInfo(sandbox);
246
273
  sandbox.info = sandboxInfo;
247
- return new Sandbox_1.Sandbox(sandbox.id, sandbox, this.sandboxApi, this.toolboxApi);
274
+ return new Sandbox_1.Sandbox(sandbox.id, sandbox, this.sandboxApi, this.toolboxApi, this.getCodeToolbox(language));
248
275
  });
249
276
  }
250
277
  /**
@@ -313,5 +340,25 @@ class Daytona {
313
340
  async getCurrentSandbox(sandboxId) {
314
341
  return await this.get(sandboxId);
315
342
  }
343
+ /**
344
+ * Gets the appropriate code toolbox based on language.
345
+ *
346
+ * @private
347
+ * @param {CodeLanguage} [language] - Programming language for the toolbox
348
+ * @returns {SandboxCodeToolbox} The appropriate code toolbox instance
349
+ * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
350
+ */
351
+ getCodeToolbox(language) {
352
+ switch (language) {
353
+ case CodeLanguage.JAVASCRIPT:
354
+ case CodeLanguage.TYPESCRIPT:
355
+ return new SandboxTsCodeToolbox_1.SandboxTsCodeToolbox();
356
+ case CodeLanguage.PYTHON:
357
+ case undefined:
358
+ return new SandboxPythonCodeToolbox_1.SandboxPythonCodeToolbox();
359
+ default:
360
+ throw new DaytonaError_1.DaytonaError(`Unsupported language: ${language}, supported languages: ${Object.values(CodeLanguage).join(', ')}`);
361
+ }
362
+ }
316
363
  }
317
364
  exports.Daytona = Daytona;
@@ -23,6 +23,19 @@ export type FilePermissionsParams = {
23
23
  /** User owner of the file */
24
24
  owner?: string;
25
25
  };
26
+ /**
27
+ * Represents a file to be uploaded to the Sandbox.
28
+ *
29
+ * @interface
30
+ * @property {string} path - Absolute destination path in the Sandbox
31
+ * @property {File} content - File to upload
32
+ */
33
+ export interface FileUpload {
34
+ /** Absolute destination path in the Sandbox */
35
+ path: string;
36
+ /** File to upload */
37
+ content: File;
38
+ }
26
39
  /**
27
40
  * Provides file system operations within a Sandbox.
28
41
  *
@@ -179,4 +192,30 @@ export declare class FileSystem {
179
192
  * await fs.uploadFile('/app/config.json', configFile);
180
193
  */
181
194
  uploadFile(path: string, file: File): Promise<void>;
195
+ /**
196
+ * Uploads multiple files to the Sandbox. The parent directories must exist.
197
+ * If files already exist at the destination paths, they will be overwritten.
198
+ *
199
+ * @param {FileUpload[]} files - Array of files to upload
200
+ * @returns {Promise<void>}
201
+ *
202
+ * @example
203
+ * // Upload multiple text files
204
+ * const files = [
205
+ * {
206
+ * path: '/app/data/file1.txt',
207
+ * content: new File(['Content of file 1'], 'file1.txt')
208
+ * },
209
+ * {
210
+ * path: '/app/data/file2.txt',
211
+ * content: new File(['Content of file 2'], 'file2.txt')
212
+ * },
213
+ * {
214
+ * path: '/app/config/settings.json',
215
+ * content: new File(['{"key": "value"}'], 'settings.json')
216
+ * }
217
+ * ];
218
+ * await fs.uploadFiles(files);
219
+ */
220
+ uploadFiles(files: FileUpload[]): Promise<void>;
182
221
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FileSystem = void 0;
4
+ const DaytonaError_1 = require("./errors/DaytonaError");
4
5
  /**
5
6
  * Provides file system operations within a Sandbox.
6
7
  *
@@ -196,5 +197,43 @@ class FileSystem {
196
197
  const response = await this.toolboxApi.uploadFile(this.instance.id, path, undefined, file);
197
198
  return response.data;
198
199
  }
200
+ /**
201
+ * Uploads multiple files to the Sandbox. The parent directories must exist.
202
+ * If files already exist at the destination paths, they will be overwritten.
203
+ *
204
+ * @param {FileUpload[]} files - Array of files to upload
205
+ * @returns {Promise<void>}
206
+ *
207
+ * @example
208
+ * // Upload multiple text files
209
+ * const files = [
210
+ * {
211
+ * path: '/app/data/file1.txt',
212
+ * content: new File(['Content of file 1'], 'file1.txt')
213
+ * },
214
+ * {
215
+ * path: '/app/data/file2.txt',
216
+ * content: new File(['Content of file 2'], 'file2.txt')
217
+ * },
218
+ * {
219
+ * path: '/app/config/settings.json',
220
+ * content: new File(['{"key": "value"}'], 'settings.json')
221
+ * }
222
+ * ];
223
+ * await fs.uploadFiles(files);
224
+ */
225
+ async uploadFiles(files) {
226
+ const results = await Promise.allSettled(files.map((file) => this.uploadFile(file.path, file.content)));
227
+ const failedUploads = results
228
+ .map((result, index) => ({
229
+ path: files[index].path,
230
+ error: result.status === 'rejected' ? result.reason : undefined,
231
+ }))
232
+ .filter(({ error }) => error !== undefined);
233
+ if (failedUploads.length > 0) {
234
+ const errorMessage = failedUploads.map(({ path, error }) => `\nFailed to upload '${path}': ${error}`).join('');
235
+ throw new DaytonaError_1.DaytonaError(errorMessage);
236
+ }
237
+ }
199
238
  }
200
239
  exports.FileSystem = FileSystem;
package/dist/Process.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Command, Session, SessionExecuteRequest, SessionExecuteResponse, ToolboxApi } from '@daytonaio/api-client';
2
- import { SandboxInstance } from './Sandbox';
2
+ import { SandboxCodeToolbox, SandboxInstance } from './Sandbox';
3
3
  import { ExecuteResponse } from './types/ExecuteResponse';
4
4
  /**
5
5
  * Parameters for code execution.
@@ -14,28 +14,22 @@ export declare class CodeRunParams {
14
14
  */
15
15
  env?: Record<string, string>;
16
16
  }
17
- /**
18
- * Supported programming languages for code execution
19
- */
20
- export declare enum CodeLanguage {
21
- PYTHON = "python",
22
- TYPESCRIPT = "typescript",
23
- JAVASCRIPT = "javascript"
24
- }
25
17
  /**
26
18
  * Handles process and code execution within a Sandbox.
27
19
  *
28
20
  * @class
29
21
  */
30
22
  export declare class Process {
23
+ private readonly codeToolbox;
31
24
  private readonly toolboxApi;
32
25
  private readonly instance;
33
- constructor(toolboxApi: ToolboxApi, instance: SandboxInstance);
26
+ constructor(codeToolbox: SandboxCodeToolbox, toolboxApi: ToolboxApi, instance: SandboxInstance);
34
27
  /**
35
28
  * Executes a shell command in the Sandbox.
36
29
  *
37
30
  * @param {string} command - Shell command to execute
38
31
  * @param {string} [cwd] - Working directory for command execution. If not specified, uses the Sandbox root directory
32
+ * @param {Record<string, string>} [env] - Environment variables to set for the command
39
33
  * @param {number} [timeout] - Maximum time in seconds to wait for the command to complete. 0 means wait indefinitely.
40
34
  * @returns {Promise<ExecuteResponse>} Command execution results containing:
41
35
  * - exitCode: The command's exit status
@@ -55,11 +49,10 @@ export declare class Process {
55
49
  * // Command with timeout
56
50
  * const result = await process.executeCommand('sleep 10', undefined, 5);
57
51
  */
58
- executeCommand(command: string, cwd?: string, timeout?: number): Promise<ExecuteResponse>;
52
+ executeCommand(command: string, cwd?: string, env?: Record<string, string>, timeout?: number): Promise<ExecuteResponse>;
59
53
  /**
60
54
  * Executes code in the Sandbox using the appropriate language runtime.
61
55
  *
62
- * @param {"python" | "typescript" | "javascript"} language - Programming language for the code.
63
56
  * @param {string} code - Code to execute
64
57
  * @param {CodeRunParams} params - Parameters for code execution
65
58
  * @param {number} [timeout] - Maximum time in seconds to wait for execution to complete
@@ -70,21 +63,16 @@ export declare class Process {
70
63
  *
71
64
  * @example
72
65
  * // Run TypeScript code
73
- * const response = await sandbox.process.codeRun(
74
- * 'typescript',
75
- * `
76
- * const x = 10;
77
- * const y = 20;
78
- * console.log(\`Sum: \${x + y}\`);
79
- * `
80
- * );
66
+ * const response = await process.codeRun(`
67
+ * const x = 10;
68
+ * const y = 20;
69
+ * console.log(\`Sum: \${x + y}\`);
70
+ * `);
81
71
  * console.log(response.artifacts.stdout); // Prints: Sum: 30
82
72
  *
83
73
  * @example
84
74
  * // Run Python code with matplotlib
85
- * const response = await sandbox.process.codeRun(
86
- * 'python',
87
- * `
75
+ * const response = await process.codeRun(`
88
76
  * import matplotlib.pyplot as plt
89
77
  * import numpy as np
90
78
  *
@@ -98,8 +86,7 @@ export declare class Process {
98
86
  * plt.ylabel('Y-axis (amplitude)')
99
87
  * plt.grid(True)
100
88
  * plt.show()
101
- * `
102
- * );
89
+ * `);
103
90
  *
104
91
  * if (response.artifacts?.charts) {
105
92
  * const chart = response.artifacts.charts[0];
@@ -121,7 +108,7 @@ export declare class Process {
121
108
  * }
122
109
  * }
123
110
  */
124
- codeRun(language: 'python' | 'typescript' | 'javascript', code: string, params?: CodeRunParams, timeout?: number): Promise<ExecuteResponse>;
111
+ codeRun(code: string, params?: CodeRunParams, timeout?: number): Promise<ExecuteResponse>;
125
112
  /**
126
113
  * Creates a new long-running background session in the Sandbox.
127
114
  *
@@ -254,15 +241,4 @@ export declare class Process {
254
241
  * await process.deleteSession('my-session');
255
242
  */
256
243
  deleteSession(sessionId: string): Promise<void>;
257
- /**
258
- * Gets the appropriate code toolbox based on language.
259
- *
260
- * @private
261
- * @param {CodeLanguage} [language] - Programming language for the toolbox
262
- * @param {string} code - Code to execute
263
- * @param {CodeRunParams} [params] - Parameters for code execution
264
- * @returns {string} The appropriate code run command
265
- * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
266
- */
267
- private getCodeRunCommand;
268
244
  }
package/dist/Process.js CHANGED
@@ -1,32 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Process = exports.CodeLanguage = exports.CodeRunParams = void 0;
3
+ exports.Process = exports.CodeRunParams = void 0;
4
4
  const ArtifactParser_1 = require("./utils/ArtifactParser");
5
- const SandboxPythonCodeToolbox_1 = require("./code-toolbox/SandboxPythonCodeToolbox");
6
- const DaytonaError_1 = require("./errors/DaytonaError");
7
- const SandboxTsCodeToolbox_1 = require("./code-toolbox/SandboxTsCodeToolbox");
8
5
  /**
9
6
  * Parameters for code execution.
10
7
  */
11
8
  class CodeRunParams {
12
9
  }
13
10
  exports.CodeRunParams = CodeRunParams;
14
- /**
15
- * Supported programming languages for code execution
16
- */
17
- var CodeLanguage;
18
- (function (CodeLanguage) {
19
- CodeLanguage["PYTHON"] = "python";
20
- CodeLanguage["TYPESCRIPT"] = "typescript";
21
- CodeLanguage["JAVASCRIPT"] = "javascript";
22
- })(CodeLanguage || (exports.CodeLanguage = CodeLanguage = {}));
23
11
  /**
24
12
  * Handles process and code execution within a Sandbox.
25
13
  *
26
14
  * @class
27
15
  */
28
16
  class Process {
29
- constructor(toolboxApi, instance) {
17
+ constructor(codeToolbox, toolboxApi, instance) {
18
+ this.codeToolbox = codeToolbox;
30
19
  this.toolboxApi = toolboxApi;
31
20
  this.instance = instance;
32
21
  }
@@ -35,6 +24,7 @@ class Process {
35
24
  *
36
25
  * @param {string} command - Shell command to execute
37
26
  * @param {string} [cwd] - Working directory for command execution. If not specified, uses the Sandbox root directory
27
+ * @param {Record<string, string>} [env] - Environment variables to set for the command
38
28
  * @param {number} [timeout] - Maximum time in seconds to wait for the command to complete. 0 means wait indefinitely.
39
29
  * @returns {Promise<ExecuteResponse>} Command execution results containing:
40
30
  * - exitCode: The command's exit status
@@ -54,7 +44,16 @@ class Process {
54
44
  * // Command with timeout
55
45
  * const result = await process.executeCommand('sleep 10', undefined, 5);
56
46
  */
57
- async executeCommand(command, cwd, timeout) {
47
+ async executeCommand(command, cwd, env, timeout) {
48
+ const envVars = env
49
+ ? Object.entries(env)
50
+ .map(([key, value]) => `${key}=${value}`)
51
+ .join(' ')
52
+ : undefined;
53
+ if (envVars) {
54
+ const base64Cmd = Buffer.from(command).toString('base64');
55
+ command = `sh -c '${envVars} sh -c "echo '${base64Cmd}' | base64 -d | sh"'`;
56
+ }
58
57
  const response = await this.toolboxApi.executeCommand(this.instance.id, {
59
58
  command,
60
59
  timeout,
@@ -73,7 +72,6 @@ class Process {
73
72
  /**
74
73
  * Executes code in the Sandbox using the appropriate language runtime.
75
74
  *
76
- * @param {"python" | "typescript" | "javascript"} language - Programming language for the code.
77
75
  * @param {string} code - Code to execute
78
76
  * @param {CodeRunParams} params - Parameters for code execution
79
77
  * @param {number} [timeout] - Maximum time in seconds to wait for execution to complete
@@ -84,21 +82,16 @@ class Process {
84
82
  *
85
83
  * @example
86
84
  * // Run TypeScript code
87
- * const response = await sandbox.process.codeRun(
88
- * 'typescript',
89
- * `
90
- * const x = 10;
91
- * const y = 20;
92
- * console.log(\`Sum: \${x + y}\`);
93
- * `
94
- * );
85
+ * const response = await process.codeRun(`
86
+ * const x = 10;
87
+ * const y = 20;
88
+ * console.log(\`Sum: \${x + y}\`);
89
+ * `);
95
90
  * console.log(response.artifacts.stdout); // Prints: Sum: 30
96
91
  *
97
92
  * @example
98
93
  * // Run Python code with matplotlib
99
- * const response = await sandbox.process.codeRun(
100
- * 'python',
101
- * `
94
+ * const response = await process.codeRun(`
102
95
  * import matplotlib.pyplot as plt
103
96
  * import numpy as np
104
97
  *
@@ -112,8 +105,7 @@ class Process {
112
105
  * plt.ylabel('Y-axis (amplitude)')
113
106
  * plt.grid(True)
114
107
  * plt.show()
115
- * `
116
- * );
108
+ * `);
117
109
  *
118
110
  * if (response.artifacts?.charts) {
119
111
  * const chart = response.artifacts.charts[0];
@@ -135,9 +127,9 @@ class Process {
135
127
  * }
136
128
  * }
137
129
  */
138
- async codeRun(language, code, params, timeout) {
139
- const runCommand = this.getCodeRunCommand(language, code, params);
140
- return this.executeCommand(runCommand, undefined, timeout);
130
+ async codeRun(code, params, timeout) {
131
+ const runCommand = this.codeToolbox.getRunCommand(code, params);
132
+ return this.executeCommand(runCommand, undefined, undefined, timeout);
141
133
  }
142
134
  /**
143
135
  * Creates a new long-running background session in the Sandbox.
@@ -296,26 +288,5 @@ class Process {
296
288
  async deleteSession(sessionId) {
297
289
  await this.toolboxApi.deleteSession(this.instance.id, sessionId);
298
290
  }
299
- /**
300
- * Gets the appropriate code toolbox based on language.
301
- *
302
- * @private
303
- * @param {CodeLanguage} [language] - Programming language for the toolbox
304
- * @param {string} code - Code to execute
305
- * @param {CodeRunParams} [params] - Parameters for code execution
306
- * @returns {string} The appropriate code run command
307
- * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
308
- */
309
- getCodeRunCommand(language, code, params) {
310
- switch (language) {
311
- case CodeLanguage.JAVASCRIPT:
312
- case CodeLanguage.TYPESCRIPT:
313
- return SandboxTsCodeToolbox_1.SandboxTsCodeToolbox.getRunCommand(code, params);
314
- case CodeLanguage.PYTHON:
315
- return SandboxPythonCodeToolbox_1.SandboxPythonCodeToolbox.getRunCommand(code, params);
316
- default:
317
- throw new DaytonaError_1.DaytonaError(`Unsupported language: ${language}, supported languages: ${Object.values(CodeLanguage).join(', ')}`);
318
- }
319
- }
320
291
  }
321
292
  exports.Process = Process;
package/dist/Sandbox.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ToolboxApi, WorkspaceState as SandboxState, WorkspaceApi as SandboxApi, Workspace as ApiSandbox, WorkspaceInfo as ApiSandboxInfo, CreateNodeClassEnum as SandboxClass, CreateNodeRegionEnum as SandboxTargetRegion, Workspace as ApiWorkspace } from '@daytonaio/api-client';
2
2
  import { FileSystem } from './FileSystem';
3
3
  import { Git } from './Git';
4
- import { Process } from './Process';
4
+ import { CodeRunParams, Process } from './Process';
5
5
  import { LspLanguageId, LspServer } from './LspServer';
6
6
  /** @deprecated Use SandboxInfo instead. This type will be removed in a future version. */
7
7
  type WorkspaceInfo = SandboxInfo;
@@ -112,6 +112,14 @@ export interface SandboxInfo extends ApiSandboxInfo {
112
112
  */
113
113
  providerMetadata?: string;
114
114
  }
115
+ /**
116
+ * Interface defining methods that a code toolbox must implement
117
+ * @interface
118
+ */
119
+ export interface SandboxCodeToolbox {
120
+ /** Generates a command to run the provided code */
121
+ getRunCommand(code: string, params?: CodeRunParams): string;
122
+ }
115
123
  /**
116
124
  * Represents a Daytona Sandbox.
117
125
  *
@@ -119,6 +127,7 @@ export interface SandboxInfo extends ApiSandboxInfo {
119
127
  * @property {SandboxInstance} instance - The underlying Sandbox instance
120
128
  * @property {SandboxApi} sandboxApi - API client for Sandbox operations
121
129
  * @property {ToolboxApi} toolboxApi - API client for toolbox operations
130
+ * @property {SandboxCodeToolbox} codeToolbox - Language-specific toolbox implementation
122
131
  * @property {FileSystem} fs - File system operations interface
123
132
  * @property {Git} git - Git operations interface
124
133
  * @property {Process} process - Process execution interface
@@ -130,6 +139,7 @@ export declare class Sandbox {
130
139
  readonly instance: SandboxInstance;
131
140
  readonly sandboxApi: SandboxApi;
132
141
  readonly toolboxApi: ToolboxApi;
142
+ private readonly codeToolbox;
133
143
  /** File system operations for the Sandbox */
134
144
  readonly fs: FileSystem;
135
145
  /** Git operations for the Sandbox */
@@ -143,8 +153,9 @@ export declare class Sandbox {
143
153
  * @param {SandboxInstance} instance - The underlying Sandbox instance
144
154
  * @param {SandboxApi} sandboxApi - API client for Sandbox operations
145
155
  * @param {ToolboxApi} toolboxApi - API client for toolbox operations
156
+ * @param {SandboxCodeToolbox} codeToolbox - Language-specific toolbox implementation
146
157
  */
147
- constructor(id: string, instance: SandboxInstance, sandboxApi: SandboxApi, toolboxApi: ToolboxApi);
158
+ constructor(id: string, instance: SandboxInstance, sandboxApi: SandboxApi, toolboxApi: ToolboxApi, codeToolbox: SandboxCodeToolbox);
148
159
  /**
149
160
  * Gets the root directory path for the logged in user inside the Sandbox.
150
161
  *
package/dist/Sandbox.js CHANGED
@@ -14,6 +14,7 @@ const DaytonaError_1 = require("./errors/DaytonaError");
14
14
  * @property {SandboxInstance} instance - The underlying Sandbox instance
15
15
  * @property {SandboxApi} sandboxApi - API client for Sandbox operations
16
16
  * @property {ToolboxApi} toolboxApi - API client for toolbox operations
17
+ * @property {SandboxCodeToolbox} codeToolbox - Language-specific toolbox implementation
17
18
  * @property {FileSystem} fs - File system operations interface
18
19
  * @property {Git} git - Git operations interface
19
20
  * @property {Process} process - Process execution interface
@@ -28,15 +29,17 @@ class Sandbox {
28
29
  * @param {SandboxInstance} instance - The underlying Sandbox instance
29
30
  * @param {SandboxApi} sandboxApi - API client for Sandbox operations
30
31
  * @param {ToolboxApi} toolboxApi - API client for toolbox operations
32
+ * @param {SandboxCodeToolbox} codeToolbox - Language-specific toolbox implementation
31
33
  */
32
- constructor(id, instance, sandboxApi, toolboxApi) {
34
+ constructor(id, instance, sandboxApi, toolboxApi, codeToolbox) {
33
35
  this.id = id;
34
36
  this.instance = instance;
35
37
  this.sandboxApi = sandboxApi;
36
38
  this.toolboxApi = toolboxApi;
39
+ this.codeToolbox = codeToolbox;
37
40
  this.fs = new FileSystem_1.FileSystem(instance, this.toolboxApi);
38
41
  this.git = new Git_1.Git(this, this.toolboxApi, instance);
39
- this.process = new Process_1.Process(this.toolboxApi, instance);
42
+ this.process = new Process_1.Process(this.codeToolbox, this.toolboxApi, instance);
40
43
  }
41
44
  /**
42
45
  * Gets the root directory path for the logged in user inside the Sandbox.
@@ -1,6 +1,8 @@
1
+ import { SandboxCodeToolbox } from '../Sandbox';
1
2
  import { CodeRunParams } from '../Process';
2
- export declare class SandboxPythonCodeToolbox {
3
- static getRunCommand(code: string, params?: CodeRunParams): string;
3
+ export declare class SandboxPythonCodeToolbox implements SandboxCodeToolbox {
4
+ getDefaultImage(): string;
5
+ getRunCommand(code: string, params?: CodeRunParams): string;
4
6
  /**
5
7
  * Checks if matplotlib is imported in the given Python code string.
6
8
  * @param codeString Python code as a string
@@ -2,7 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SandboxPythonCodeToolbox = void 0;
4
4
  class SandboxPythonCodeToolbox {
5
- static getRunCommand(code, params) {
5
+ getDefaultImage() {
6
+ return 'daytonaio/sdk-python:v0.49.0-2';
7
+ }
8
+ getRunCommand(code, params) {
6
9
  // Encode the provided code in base64
7
10
  let base64Code = Buffer.from(code).toString('base64');
8
11
  // Override plt.show() method if matplotlib is imported
@@ -1,4 +1,6 @@
1
+ import { SandboxCodeToolbox } from '../Sandbox';
1
2
  import { CodeRunParams } from '../Process';
2
- export declare class SandboxTsCodeToolbox {
3
- static getRunCommand(code: string, params?: CodeRunParams): string;
3
+ export declare class SandboxTsCodeToolbox implements SandboxCodeToolbox {
4
+ getDefaultImage(): string;
5
+ getRunCommand(code: string, params?: CodeRunParams): string;
4
6
  }
@@ -2,7 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SandboxTsCodeToolbox = void 0;
4
4
  class SandboxTsCodeToolbox {
5
- static getRunCommand(code, params) {
5
+ getDefaultImage() {
6
+ return 'daytonaio/sdk-typescript:v0.49.0-3';
7
+ }
8
+ getRunCommand(code, params) {
6
9
  const base64Code = Buffer.from(code).toString('base64');
7
10
  const envVars = (params === null || params === void 0 ? void 0 : params.env)
8
11
  ? Object.entries(params.env)
package/dist/index.d.ts CHANGED
@@ -1,17 +1,19 @@
1
- export { Daytona } from './Daytona';
1
+ export { CodeLanguage, Daytona } from './Daytona';
2
2
  export type { CreateSandboxParams, DaytonaConfig, SandboxResources } from './Daytona';
3
3
  export { FileSystem } from './FileSystem';
4
4
  export { Git } from './Git';
5
5
  export { LspLanguageId } from './LspServer';
6
- export { Process, CodeLanguage } from './Process';
6
+ export { Process } from './Process';
7
7
  export { DaytonaError } from './errors/DaytonaError';
8
8
  export { Sandbox } from './Sandbox';
9
+ export type { SandboxCodeToolbox } from './Sandbox';
9
10
  export { ChartType } from './types/Charts';
10
11
  export type { BarChart, BoxAndWhiskerChart, Chart, CompositeChart, LineChart, PieChart, ScatterChart, } from './types/Charts';
11
12
  export { WorkspaceState as SandboxState, CreateWorkspaceTargetEnum as SandboxTargetRegion } from '@daytonaio/api-client';
12
13
  export type { FileInfo, GitStatus, ListBranchResponse, Match, ReplaceResult, SearchFilesResponse, } from '@daytonaio/api-client';
13
14
  import { CreateWorkspaceTargetEnum, WorkspaceState as WS } from '@daytonaio/api-client';
14
15
  import type { CreateSandboxParams, SandboxResources } from './Daytona';
16
+ import type { SandboxCodeToolbox } from './Sandbox';
15
17
  import { Sandbox } from './Sandbox';
16
18
  /** @deprecated `CreateWorkspaceParams` is deprecated. Please use `CreateSandboxParams` instead. This will be removed in a future version. */
17
19
  export type CreateWorkspaceParams = CreateSandboxParams;
@@ -19,6 +21,8 @@ export type CreateWorkspaceParams = CreateSandboxParams;
19
21
  export declare const Workspace: typeof Sandbox;
20
22
  /** @deprecated `Workspace` is deprecated. Please use `Sandbox` instead. This will be removed in a future version. */
21
23
  export type Workspace = Sandbox;
24
+ /** @deprecated `WorkspaceCodeToolbox` is deprecated. Please use `SandboxCodeToolbox` instead. This will be removed in a future version. */
25
+ export type WorkspaceCodeToolbox = SandboxCodeToolbox;
22
26
  /** @deprecated `WorkspaceResources` is deprecated. Please use `SandboxResources` instead. This will be removed in a future version. */
23
27
  export type WorkspaceResources = SandboxResources;
24
28
  /** @deprecated `WorkspaceState` is deprecated. Please use `SandboxState` instead. This will be removed in a future version. */
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkspaceTargetRegion = exports.WorkspaceState = exports.Workspace = exports.SandboxTargetRegion = exports.SandboxState = exports.ChartType = exports.Sandbox = exports.DaytonaError = exports.CodeLanguage = exports.Process = exports.LspLanguageId = exports.Git = exports.FileSystem = exports.Daytona = void 0;
3
+ exports.WorkspaceTargetRegion = exports.WorkspaceState = exports.Workspace = exports.SandboxTargetRegion = exports.SandboxState = exports.ChartType = exports.Sandbox = exports.DaytonaError = exports.Process = exports.LspLanguageId = exports.Git = exports.FileSystem = exports.Daytona = exports.CodeLanguage = void 0;
4
4
  var Daytona_1 = require("./Daytona");
5
+ Object.defineProperty(exports, "CodeLanguage", { enumerable: true, get: function () { return Daytona_1.CodeLanguage; } });
5
6
  Object.defineProperty(exports, "Daytona", { enumerable: true, get: function () { return Daytona_1.Daytona; } });
6
7
  var FileSystem_1 = require("./FileSystem");
7
8
  Object.defineProperty(exports, "FileSystem", { enumerable: true, get: function () { return FileSystem_1.FileSystem; } });
@@ -11,7 +12,6 @@ var LspServer_1 = require("./LspServer");
11
12
  Object.defineProperty(exports, "LspLanguageId", { enumerable: true, get: function () { return LspServer_1.LspLanguageId; } });
12
13
  var Process_1 = require("./Process");
13
14
  Object.defineProperty(exports, "Process", { enumerable: true, get: function () { return Process_1.Process; } });
14
- Object.defineProperty(exports, "CodeLanguage", { enumerable: true, get: function () { return Process_1.CodeLanguage; } });
15
15
  // export { LspServer } from './LspServer'
16
16
  // export type { LspLanguageId, Position } from './LspServer'
17
17
  var DaytonaError_1 = require("./errors/DaytonaError");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daytonaio/sdk",
3
- "version": "0.12.2-alpha.1",
3
+ "version": "0.13.1",
4
4
  "description": "Daytona client library for AI Agents",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",