@daytonaio/sdk 0.19.0-alpha.2 → 0.19.0-alpha.4

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 ADDED
@@ -0,0 +1,145 @@
1
+ # Daytona SDK for TypeScript
2
+
3
+ A TypeScript SDK for interacting with the Daytona API, providing a simple interface for Daytona Sandbox management, Git operations, file system operations, and language server protocol support.
4
+
5
+ ## Installation
6
+
7
+ You can install the package using npm:
8
+
9
+ ```bash
10
+ npm install @daytonaio/sdk
11
+ ```
12
+
13
+ Or using yarn:
14
+
15
+ ```bash
16
+ yarn add @daytonaio/sdk
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ Here's a simple example of using the SDK:
22
+
23
+ ```typescript
24
+ import { Daytona } from '@daytonaio/sdk'
25
+
26
+ // Initialize using environment variables
27
+ const daytona = new Daytona()
28
+
29
+ // Create a sandbox
30
+ const sandbox = await daytona.create()
31
+
32
+ // Run code in the sandbox
33
+ const response = await sandbox.process.codeRun('console.log("Hello World!")')
34
+ console.log(response.result)
35
+
36
+ // Clean up when done
37
+ await daytona.delete(sandbox)
38
+ ```
39
+
40
+ ## Configuration
41
+
42
+ The SDK can be configured using environment variables or by passing a configuration object:
43
+
44
+ ```typescript
45
+ import { Daytona } from '@daytonaio/sdk'
46
+
47
+ // Initialize with configuration
48
+ const daytona = new Daytona({
49
+ apiKey: 'your-api-key',
50
+ apiUrl: 'your-api-url',
51
+ target: 'us',
52
+ })
53
+ ```
54
+
55
+ Or using environment variables:
56
+
57
+ - `DAYTONA_API_KEY`: Your Daytona API key
58
+ - `DAYTONA_API_URL`: The Daytona API URL
59
+ - `DAYTONA_TARGET`: Your target environment
60
+
61
+ You can also customize sandbox creation:
62
+
63
+ ```typescript
64
+ const sandbox = await daytona.create({
65
+ language: 'typescript',
66
+ envVars: { NODE_ENV: 'development' },
67
+ autoStopInterval: 60, // Auto-stop after 1 hour of inactivity
68
+ })
69
+ ```
70
+
71
+ ## Features
72
+
73
+ - **Sandbox Management**: Create, manage and remove sandboxes
74
+ - **Git Operations**: Clone repositories, manage branches, and more
75
+ - **File System Operations**: Upload, download, search and manipulate files
76
+ - **Language Server Protocol**: Interact with language servers for code intelligence
77
+ - **Process Management**: Execute code and commands in sandboxes
78
+
79
+ ## Examples
80
+
81
+ ### Execute Commands
82
+
83
+ ```typescript
84
+ // Execute a shell command
85
+ const response = await sandbox.process.executeCommand('echo "Hello, World!"')
86
+ console.log(response.result)
87
+
88
+ // Run TypeScript code
89
+ const response = await sandbox.process.codeRun(`
90
+ const x = 10
91
+ const y = 20
92
+ console.log(\`Sum: \${x + y}\`)
93
+ `)
94
+ console.log(response.result)
95
+ ```
96
+
97
+ ### File Operations
98
+
99
+ ```typescript
100
+ // Upload a file
101
+ await sandbox.fs.uploadFile(Buffer.from('Hello, World!'), 'path/to/file.txt')
102
+
103
+ // Download a file
104
+ const content = await sandbox.fs.downloadFile('path/to/file.txt')
105
+
106
+ // Search for files
107
+ const matches = await sandbox.fs.findFiles(root_dir, 'search_pattern')
108
+ ```
109
+
110
+ ### Git Operations
111
+
112
+ ```typescript
113
+ // Clone a repository
114
+ await sandbox.git.clone('https://github.com/example/repo', 'path/to/clone')
115
+
116
+ // List branches
117
+ const branches = await sandbox.git.branches('path/to/repo')
118
+
119
+ // Add files
120
+ await sandbox.git.add('path/to/repo', ['file1.txt', 'file2.txt'])
121
+ ```
122
+
123
+ ### Language Server Protocol
124
+
125
+ ```typescript
126
+ // Create and start a language server
127
+ const lsp = await sandbox.createLspServer('typescript', 'path/to/project')
128
+ await lsp.start()
129
+
130
+ // Notify the lsp for the file
131
+ await lsp.didOpen('path/to/file.ts')
132
+
133
+ // Get document symbols
134
+ const symbols = await lsp.documentSymbols('path/to/file.ts')
135
+
136
+ // Get completions
137
+ const completions = await lsp.completions('path/to/file.ts', {
138
+ line: 10,
139
+ character: 15,
140
+ })
141
+ ```
142
+
143
+ ## Contributing
144
+
145
+ Daytona is Open Source under the [GNU AFFERO GENERAL PUBLIC LICENSE](LICENSE), and is the [copyright of its contributors](NOTICE). If you would like to contribute to the software, read the Developer Certificate of Origin Version 1.1 (https://developercertificate.org/). Afterwards, navigate to the [contributing guide](CONTRIBUTING.md) to get started.
@@ -0,0 +1,393 @@
1
+ import { CreateWorkspaceTargetEnum as SandboxTargetRegion, WorkspaceVolume } from '@daytonaio/api-client';
2
+ import { Image } from './Image';
3
+ import { Sandbox, Sandbox as Workspace } from './Sandbox';
4
+ import { VolumeService } from './Volume';
5
+ /**
6
+ * Represents a volume mount for a Sandbox.
7
+ *
8
+ * @interface
9
+ * @property {string} volumeId - ID of the Volume to mount
10
+ * @property {string} mountPath - Path on the Sandbox to mount the Volume
11
+ */
12
+ export interface VolumeMount extends WorkspaceVolume {
13
+ volumeId: string;
14
+ mountPath: string;
15
+ }
16
+ /**
17
+ * Configuration options for initializing the Daytona client.
18
+ *
19
+ * @interface
20
+ * @property {string} apiKey - API key for authentication with the Daytona API
21
+ * @property {string} jwtToken - JWT token for authentication with the Daytona API. If not set, it must be provided
22
+ * via the environment variable `DAYTONA_JWT_TOKEN`, or an API key must be provided instead.
23
+ * @property {string} organizationId - Organization ID used for JWT-based authentication. Required if a JWT token
24
+ * is provided, and must be set either here or in the environment variable `DAYTONA_ORGANIZATION_ID`.
25
+ * @property {string} apiUrl - URL of the Daytona API. Defaults to 'https://app.daytona.io/api'
26
+ * if not set here and not set in environment variable DAYTONA_API_URL.
27
+ * @property {CreateSandboxTargetEnum} target - Target location for Sandboxes
28
+ *
29
+ * @example
30
+ * const config: DaytonaConfig = {
31
+ * apiKey: "your-api-key",
32
+ * apiUrl: "https://your-api.com",
33
+ * target: "us"
34
+ * };
35
+ * const daytona = new Daytona(config);
36
+ */
37
+ export interface DaytonaConfig {
38
+ /** API key for authentication with the Daytona API */
39
+ apiKey?: string;
40
+ /** JWT token for authentication with the Daytona API */
41
+ jwtToken?: string;
42
+ /** Organization ID for authentication with the Daytona API */
43
+ organizationId?: string;
44
+ /** URL of the Daytona API.
45
+ */
46
+ apiUrl?: string;
47
+ /**
48
+ * @deprecated Use `apiUrl` instead. This property will be removed in future versions.
49
+ */
50
+ serverUrl?: string;
51
+ /** Target environment for sandboxes */
52
+ target?: SandboxTargetRegion;
53
+ }
54
+ /**
55
+ * Supported programming languages for code execution
56
+ */
57
+ export declare enum CodeLanguage {
58
+ PYTHON = "python",
59
+ TYPESCRIPT = "typescript",
60
+ JAVASCRIPT = "javascript"
61
+ }
62
+ /**
63
+ * Resource allocation for a Sandbox.
64
+ *
65
+ * @interface
66
+ * @property {number} [cpu] - CPU allocation for the Sandbox in cores
67
+ * @property {number} [gpu] - GPU allocation for the Sandbox in units
68
+ * @property {number} [memory] - Memory allocation for the Sandbox in GB
69
+ * @property {number} [disk] - Disk space allocation for the Sandbox in GB
70
+ *
71
+ * @example
72
+ * const resources: SandboxResources = {
73
+ * cpu: 2,
74
+ * memory: 4, // 4GB RAM
75
+ * disk: 20 // 20GB disk
76
+ * };
77
+ */
78
+ export interface SandboxResources {
79
+ /** CPU allocation for the Sandbox */
80
+ cpu?: number;
81
+ /** GPU allocation for the Sandbox */
82
+ gpu?: number;
83
+ /** Memory allocation for the Sandbox in GB */
84
+ memory?: number;
85
+ /** Disk space allocation for the Sandbox in GB */
86
+ disk?: number;
87
+ }
88
+ /**
89
+ * Parameters for creating a new Sandbox.
90
+ *
91
+ * @interface
92
+ * @property {string | Image} [image] - Optional Docker image to use for the Sandbox or an Image instance
93
+ * @property {string} [user] - Optional os user to use for the Sandbox
94
+ * @property {CodeLanguage | string} [language] - Programming language for direct code execution
95
+ * @property {Record<string, string>} [envVars] - Optional environment variables to set in the Sandbox
96
+ * @property {Record<string, string>} [labels] - Sandbox labels
97
+ * @property {boolean} [public] - Is the Sandbox port preview public
98
+ * @property {SandboxResources} [resources] - Resource allocation for the Sandbox
99
+ * @property {boolean} [async] - If true, will not wait for the Sandbox to be ready before returning
100
+ * @property {number} [timeout] - Timeout in seconds for the Sandbox to be ready (0 means no timeout)
101
+ * @property {number} [autoStopInterval] - Auto-stop interval in minutes (0 means disabled)
102
+ *
103
+ * @example
104
+ * const params: CreateSandboxParams = {
105
+ * language: 'typescript',
106
+ * envVars: { NODE_ENV: 'development' },
107
+ * resources: {
108
+ * cpu: 2,
109
+ * memory: 4 // 4GB RAM
110
+ * },
111
+ * autoStopInterval: 60 // Auto-stop after 1 hour of inactivity
112
+ * };
113
+ * const sandbox = await daytona.create(params, 50);
114
+ */
115
+ export type CreateSandboxParams = {
116
+ /** Optional Docker image to use for the Sandbox or an Image instance */
117
+ image?: string | Image;
118
+ /** Optional os user to use for the Sandbox */
119
+ user?: string;
120
+ /** Programming language for direct code execution */
121
+ language?: CodeLanguage | string;
122
+ /** Optional environment variables to set in the sandbox */
123
+ envVars?: Record<string, string>;
124
+ /** Sandbox labels */
125
+ labels?: Record<string, string>;
126
+ /** Is the Sandbox port preview public */
127
+ public?: boolean;
128
+ /** Resource allocation for the Sandbox */
129
+ resources?: SandboxResources;
130
+ /** If true, will not wait for the Sandbox to be ready before returning */
131
+ async?: boolean;
132
+ /**
133
+ * Timeout in seconds, for the Sandbox to be ready (0 means no timeout)
134
+ * @deprecated Use methods with `timeout` parameter instead
135
+ */
136
+ timeout?: number;
137
+ /** Auto-stop interval in minutes (0 means disabled) (must be a non-negative integer) */
138
+ autoStopInterval?: number;
139
+ /** List of volumes to mount in the Sandbox */
140
+ volumes?: VolumeMount[];
141
+ };
142
+ /**
143
+ * Filter for Sandboxes.
144
+ *
145
+ * @interface
146
+ * @property {string} [id] - The ID of the Sandbox to retrieve
147
+ * @property {Record<string, string>} [labels] - Labels to filter Sandboxes
148
+ */
149
+ export type SandboxFilter = {
150
+ id?: string;
151
+ labels?: Record<string, string>;
152
+ };
153
+ /**
154
+ * Main class for interacting with the Daytona API.
155
+ * Provides methods for creating, managing, and interacting with Daytona Sandboxes.
156
+ * Can be initialized either with explicit configuration or using environment variables.
157
+ *
158
+ * @property {VolumeService} volume - Service for managing Daytona Volumes
159
+ *
160
+ * @example
161
+ * // Using environment variables
162
+ * // Uses DAYTONA_API_KEY, DAYTONA_API_URL, DAYTONA_TARGET
163
+ * const daytona = new Daytona();
164
+ * const sandbox = await daytona.create();
165
+ *
166
+ * @example
167
+ * // Using explicit configuration
168
+ * const config: DaytonaConfig = {
169
+ * apiKey: "your-api-key",
170
+ * apiUrl: "https://your-api.com",
171
+ * target: "us"
172
+ * };
173
+ * const daytona = new Daytona(config);
174
+ *
175
+ * @class
176
+ */
177
+ export declare class Daytona {
178
+ private readonly sandboxApi;
179
+ private readonly toolboxApi;
180
+ private readonly imagesApi;
181
+ private readonly objectStorageApi;
182
+ private readonly target;
183
+ private readonly apiKey?;
184
+ private readonly jwtToken?;
185
+ private readonly organizationId?;
186
+ private readonly apiUrl;
187
+ readonly volume: VolumeService;
188
+ /**
189
+ * Creates a new Daytona client instance.
190
+ *
191
+ * @param {DaytonaConfig} [config] - Configuration options
192
+ * @throws {DaytonaError} - `DaytonaError` - When API key is missing
193
+ */
194
+ constructor(config?: DaytonaConfig);
195
+ /**
196
+ * @deprecated Use `create` with `options` object instead. This method will be removed in a future version.
197
+ *
198
+ * Creates Sandboxes with default or custom configurations. You can specify various parameters,
199
+ * including language, image, resources, environment variables, and volumes for the Sandbox.
200
+ *
201
+ * @param {CreateSandboxParams} [params] - Parameters for Sandbox creation
202
+ * @param {number} [timeout] - Timeout in seconds (0 means no timeout, default is 60)
203
+ * @returns {Promise<Sandbox>} The created Sandbox instance
204
+ *
205
+ * @example
206
+ * // Create a default sandbox
207
+ * const sandbox = await daytona.create();
208
+ *
209
+ * @example
210
+ * // Create a custom sandbox
211
+ * const params: CreateSandboxParams = {
212
+ * language: 'typescript',
213
+ * image: 'node:18',
214
+ * envVars: {
215
+ * NODE_ENV: 'development',
216
+ * DEBUG: 'true'
217
+ * },
218
+ * resources: {
219
+ * cpu: 2,
220
+ * memory: 4 // 4GB RAM
221
+ * },
222
+ * autoStopInterval: 60
223
+ * };
224
+ * const sandbox = await daytona.create(params, 40);
225
+ */
226
+ create(params?: CreateSandboxParams, options?: number): Promise<Sandbox>;
227
+ /**
228
+ * Creates Sandboxes with default or custom configurations. You can specify various parameters,
229
+ * including language, image, resources, environment variables, and volumes for the Sandbox.
230
+ *
231
+ * @param {CreateSandboxParams} [params] - Parameters for Sandbox creation
232
+ * @param {object} [options] - Options for the create operation
233
+ * @param {number} [options.timeout] - Timeout in seconds (0 means no timeout, default is 60)
234
+ * @param {function} [options.onImageBuildLogs] - Callback function to handle image build logs.
235
+ * It's invoked only when `params.image` is an instance of `Image` and there's no existing
236
+ * image in Daytona with the same configuration.
237
+ * @returns {Promise<Sandbox>} The created Sandbox instance
238
+ *
239
+ * @example
240
+ * const image = Image.debianSlim('3.12').pipInstall('numpy');
241
+ * const sandbox = await daytona.create({ image }, { timeout: 90, onImageBuildLogs: console.log });
242
+ *
243
+ * @example
244
+ * // Create a custom sandbox
245
+ * const image = Image.debianSlim('3.12').pipInstall('numpy');
246
+ * const params: CreateSandboxParams = {
247
+ * language: 'typescript',
248
+ * image,
249
+ * envVars: {
250
+ * NODE_ENV: 'development',
251
+ * DEBUG: 'true'
252
+ * },
253
+ * resources: {
254
+ * cpu: 2,
255
+ * memory: 4 // 4GB RAM
256
+ * },
257
+ * autoStopInterval: 60
258
+ * };
259
+ * const sandbox = await daytona.create(params, { timeout: 100, onImageBuildLogs: console.log });
260
+ */
261
+ create(params?: CreateSandboxParams, options?: {
262
+ onImageBuildLogs?: (chunk: string) => void;
263
+ timeout?: number;
264
+ }): Promise<Sandbox>;
265
+ /**
266
+ * Gets a Sandbox by its ID.
267
+ *
268
+ * @param {string} sandboxId - The ID of the Sandbox to retrieve
269
+ * @returns {Promise<Sandbox>} The Sandbox
270
+ *
271
+ * @example
272
+ * const sandbox = await daytona.get('my-sandbox-id');
273
+ * console.log(`Sandbox state: ${sandbox.instance.state}`);
274
+ */
275
+ get(sandboxId: string): Promise<Sandbox>;
276
+ /**
277
+ * Finds a Sandbox by its ID or labels.
278
+ *
279
+ * @param {SandboxFilter} filter - Filter for Sandboxes
280
+ * @returns {Promise<Sandbox>} First Sandbox that matches the ID or labels.
281
+ *
282
+ * @example
283
+ * const sandbox = await daytona.findOne({ labels: { 'my-label': 'my-value' } });
284
+ * console.log(`Sandbox: ${await sandbox.info()}`);
285
+ */
286
+ findOne(filter: SandboxFilter): Promise<Sandbox>;
287
+ /**
288
+ * Lists all Sandboxes filtered by labels.
289
+ *
290
+ * @param {Record<string, string>} [labels] - Labels to filter Sandboxes
291
+ * @returns {Promise<Sandbox[]>} Array of Sandboxes that match the labels.
292
+ *
293
+ * @example
294
+ * const sandboxes = await daytona.list({ 'my-label': 'my-value' });
295
+ * for (const sandbox of sandboxes) {
296
+ * console.log(`${sandbox.id}: ${sandbox.instance.state}`);
297
+ * }
298
+ */
299
+ list(labels?: Record<string, string>): Promise<Sandbox[]>;
300
+ /**
301
+ * Starts a Sandbox and waits for it to be ready.
302
+ *
303
+ * @param {Sandbox} sandbox - The Sandbox to start
304
+ * @param {number} [timeout] - Optional timeout in seconds (0 means no timeout)
305
+ * @returns {Promise<void>}
306
+ *
307
+ * @example
308
+ * const sandbox = await daytona.get('my-sandbox-id');
309
+ * // Wait up to 60 seconds for the sandbox to start
310
+ * await daytona.start(sandbox, 60);
311
+ */
312
+ start(sandbox: Sandbox, timeout?: number): Promise<void>;
313
+ /**
314
+ * Stops a Sandbox.
315
+ *
316
+ * @param {Sandbox} sandbox - The Sandbox to stop
317
+ * @returns {Promise<void>}
318
+ *
319
+ * @example
320
+ * const sandbox = await daytona.get('my-sandbox-id');
321
+ * await daytona.stop(sandbox);
322
+ */
323
+ stop(sandbox: Sandbox): Promise<void>;
324
+ /**
325
+ * Deletes a Sandbox.
326
+ *
327
+ * @param {Sandbox} sandbox - The Sandbox to delete
328
+ * @param {number} timeout - Timeout in seconds (0 means no timeout, default is 60)
329
+ * @returns {Promise<void>}
330
+ *
331
+ * @example
332
+ * const sandbox = await daytona.get('my-sandbox-id');
333
+ * await daytona.delete(sandbox);
334
+ */
335
+ delete(sandbox: Sandbox, timeout?: number): Promise<void>;
336
+ /** @hidden */
337
+ remove: (sandbox: Sandbox, timeout?: number) => Promise<void>;
338
+ /**
339
+ * Gets the Sandbox by ID.
340
+ *
341
+ * @param {string} workspaceId - The ID of the Sandbox to retrieve
342
+ * @returns {Promise<Workspace>} The Sandbox
343
+ *
344
+ * @deprecated Use `getCurrentSandbox` instead. This method will be removed in a future version.
345
+ */
346
+ getCurrentWorkspace(workspaceId: string): Promise<Workspace>;
347
+ /**
348
+ * Gets the Sandbox by ID.
349
+ *
350
+ * @param {string} sandboxId - The ID of the Sandbox to retrieve
351
+ * @returns {Promise<Sandbox>} The Sandbox
352
+ *
353
+ * @example
354
+ * const sandbox = await daytona.getCurrentSandbox('my-sandbox-id');
355
+ * console.log(`Current sandbox state: ${sandbox.instance.state}`);
356
+ */
357
+ getCurrentSandbox(sandboxId: string): Promise<Sandbox>;
358
+ /**
359
+ * Creates and registers a new image from the given Image definition.
360
+ *
361
+ * @param {string} name - The name of the image to create.
362
+ * @param {Image} image - The Image instance.
363
+ * @param {object} options - Options for the create operation.
364
+ * @param {boolean} options.verbose - Default is false. Whether to log progress information upon each state change of the image.
365
+ * @param {number} options.timeout - Default is no timeout. Timeout in seconds (0 means no timeout).
366
+ * @returns {Promise<void>}
367
+ *
368
+ * @example
369
+ * const image = Image.debianSlim('3.12').pipInstall('numpy');
370
+ * await daytona.createImage('my-python-image', image);
371
+ */
372
+ createImage(name: string, image: Image, options?: {
373
+ onLogs?: (chunk: string) => void;
374
+ timeout?: number;
375
+ }): Promise<void>;
376
+ /**
377
+ * Gets the appropriate code toolbox based on language.
378
+ *
379
+ * @private
380
+ * @param {CodeLanguage} [language] - Programming language for the toolbox
381
+ * @returns {SandboxCodeToolbox} The appropriate code toolbox instance
382
+ * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
383
+ */
384
+ private getCodeToolbox;
385
+ /**
386
+ * Processes the image contexts by uploading them to object storage
387
+ *
388
+ * @private
389
+ * @param {Image} image - The Image instance.
390
+ * @returns {Promise<string[]>} The list of context hashes stored in object storage.
391
+ */
392
+ private processImageContext;
393
+ }