@daytonaio/sdk 0.9.0 → 0.10.0

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.
Files changed (222) hide show
  1. package/README.md +4 -4
  2. package/dist/Daytona.d.ts +243 -47
  3. package/dist/Daytona.js +278 -60
  4. package/dist/FileSystem.d.ts +183 -32
  5. package/dist/FileSystem.js +168 -28
  6. package/dist/Git.d.ts +178 -30
  7. package/dist/Git.js +178 -30
  8. package/dist/LspServer.d.ts +170 -22
  9. package/dist/LspServer.js +162 -18
  10. package/dist/Process.d.ts +187 -37
  11. package/dist/Process.js +185 -44
  12. package/dist/Workspace.d.ts +257 -35
  13. package/dist/Workspace.js +255 -48
  14. package/dist/code-toolbox/WorkspacePythonCodeToolbox.d.ts +2 -1
  15. package/dist/code-toolbox/WorkspacePythonCodeToolbox.js +4 -2
  16. package/dist/code-toolbox/WorkspaceTsCodeToolbox.d.ts +2 -1
  17. package/dist/code-toolbox/WorkspaceTsCodeToolbox.js +4 -2
  18. package/dist/errors/DaytonaError.d.ts +8 -0
  19. package/dist/errors/DaytonaError.js +12 -0
  20. package/dist/index.d.ts +4 -0
  21. package/dist/index.js +12 -1
  22. package/dist/utils/Axios.d.ts +6 -0
  23. package/dist/utils/Axios.js +28 -0
  24. package/dist/utils/errors.d.ts +10 -0
  25. package/dist/utils/errors.js +36 -0
  26. package/dist/utils/timeout.d.ts +35 -0
  27. package/dist/utils/timeout.js +73 -0
  28. package/package.json +11 -10
  29. package/dist/client/apis/ApiKeyApi.d.ts +0 -54
  30. package/dist/client/apis/ApiKeyApi.js +0 -135
  31. package/dist/client/apis/BuildApi.d.ts +0 -95
  32. package/dist/client/apis/BuildApi.js +0 -228
  33. package/dist/client/apis/ContainerRegistryApi.d.ts +0 -68
  34. package/dist/client/apis/ContainerRegistryApi.js +0 -163
  35. package/dist/client/apis/DefaultApi.d.ts +0 -31
  36. package/dist/client/apis/DefaultApi.js +0 -72
  37. package/dist/client/apis/GitProviderApi.d.ts +0 -197
  38. package/dist/client/apis/GitProviderApi.js +0 -477
  39. package/dist/client/apis/PrebuildApi.d.ts +0 -97
  40. package/dist/client/apis/PrebuildApi.js +0 -241
  41. package/dist/client/apis/ProfileApi.d.ts +0 -51
  42. package/dist/client/apis/ProfileApi.js +0 -128
  43. package/dist/client/apis/ProjectConfigApi.d.ts +0 -94
  44. package/dist/client/apis/ProjectConfigApi.js +0 -220
  45. package/dist/client/apis/ProviderApi.d.ts +0 -71
  46. package/dist/client/apis/ProviderApi.js +0 -160
  47. package/dist/client/apis/SampleApi.d.ts +0 -28
  48. package/dist/client/apis/SampleApi.js +0 -73
  49. package/dist/client/apis/ServerApi.d.ts +0 -61
  50. package/dist/client/apis/ServerApi.js +0 -156
  51. package/dist/client/apis/TargetApi.d.ts +0 -67
  52. package/dist/client/apis/TargetApi.js +0 -159
  53. package/dist/client/apis/WorkspaceApi.d.ts +0 -141
  54. package/dist/client/apis/WorkspaceApi.js +0 -330
  55. package/dist/client/apis/WorkspaceToolboxApi.d.ts +0 -464
  56. package/dist/client/apis/WorkspaceToolboxApi.js +0 -1248
  57. package/dist/client/apis/index.d.ts +0 -14
  58. package/dist/client/apis/index.js +0 -32
  59. package/dist/client/index.d.ts +0 -3
  60. package/dist/client/index.js +0 -21
  61. package/dist/client/models/ApiKey.d.ts +0 -45
  62. package/dist/client/models/ApiKey.js +0 -60
  63. package/dist/client/models/ApikeyApiKeyType.d.ts +0 -26
  64. package/dist/client/models/ApikeyApiKeyType.js +0 -52
  65. package/dist/client/models/Build.d.ts +0 -98
  66. package/dist/client/models/Build.js +0 -91
  67. package/dist/client/models/BuildBuildState.d.ts +0 -31
  68. package/dist/client/models/BuildBuildState.js +0 -57
  69. package/dist/client/models/BuildConfig.d.ts +0 -40
  70. package/dist/client/models/BuildConfig.js +0 -57
  71. package/dist/client/models/CachedBuild.d.ts +0 -38
  72. package/dist/client/models/CachedBuild.js +0 -55
  73. package/dist/client/models/CloneTarget.d.ts +0 -25
  74. package/dist/client/models/CloneTarget.js +0 -51
  75. package/dist/client/models/CompletionContext.d.ts +0 -38
  76. package/dist/client/models/CompletionContext.js +0 -53
  77. package/dist/client/models/CompletionItem.d.ts +0 -68
  78. package/dist/client/models/CompletionItem.js +0 -63
  79. package/dist/client/models/CompletionList.d.ts +0 -39
  80. package/dist/client/models/CompletionList.js +0 -56
  81. package/dist/client/models/ContainerConfig.d.ts +0 -38
  82. package/dist/client/models/ContainerConfig.js +0 -55
  83. package/dist/client/models/ContainerRegistry.d.ts +0 -44
  84. package/dist/client/models/ContainerRegistry.js +0 -59
  85. package/dist/client/models/CreateBuildDTO.d.ts +0 -52
  86. package/dist/client/models/CreateBuildDTO.js +0 -62
  87. package/dist/client/models/CreatePrebuildDTO.d.ts +0 -56
  88. package/dist/client/models/CreatePrebuildDTO.js +0 -59
  89. package/dist/client/models/CreateProjectConfigDTO.d.ts +0 -71
  90. package/dist/client/models/CreateProjectConfigDTO.js +0 -72
  91. package/dist/client/models/CreateProjectDTO.d.ts +0 -72
  92. package/dist/client/models/CreateProjectDTO.js +0 -73
  93. package/dist/client/models/CreateProjectSourceDTO.d.ts +0 -33
  94. package/dist/client/models/CreateProjectSourceDTO.js +0 -52
  95. package/dist/client/models/CreateProviderTargetDTO.d.ts +0 -45
  96. package/dist/client/models/CreateProviderTargetDTO.js +0 -60
  97. package/dist/client/models/CreateWorkspaceDTO.d.ts +0 -51
  98. package/dist/client/models/CreateWorkspaceDTO.js +0 -64
  99. package/dist/client/models/DevcontainerConfig.d.ts +0 -32
  100. package/dist/client/models/DevcontainerConfig.js +0 -51
  101. package/dist/client/models/ExecuteRequest.d.ts +0 -38
  102. package/dist/client/models/ExecuteRequest.js +0 -53
  103. package/dist/client/models/ExecuteResponse.d.ts +0 -38
  104. package/dist/client/models/ExecuteResponse.js +0 -55
  105. package/dist/client/models/FRPSConfig.d.ts +0 -44
  106. package/dist/client/models/FRPSConfig.js +0 -59
  107. package/dist/client/models/FileInfo.d.ts +0 -74
  108. package/dist/client/models/FileInfo.js +0 -79
  109. package/dist/client/models/FileStatus.d.ts +0 -51
  110. package/dist/client/models/FileStatus.js +0 -64
  111. package/dist/client/models/GetRepositoryContext.d.ts +0 -80
  112. package/dist/client/models/GetRepositoryContext.js +0 -67
  113. package/dist/client/models/GitAddRequest.d.ts +0 -38
  114. package/dist/client/models/GitAddRequest.js +0 -55
  115. package/dist/client/models/GitBranch.d.ts +0 -38
  116. package/dist/client/models/GitBranch.js +0 -55
  117. package/dist/client/models/GitBranchRequest.d.ts +0 -38
  118. package/dist/client/models/GitBranchRequest.js +0 -55
  119. package/dist/client/models/GitCloneRequest.d.ts +0 -62
  120. package/dist/client/models/GitCloneRequest.js +0 -63
  121. package/dist/client/models/GitCommitInfo.d.ts +0 -56
  122. package/dist/client/models/GitCommitInfo.js +0 -67
  123. package/dist/client/models/GitCommitRequest.d.ts +0 -50
  124. package/dist/client/models/GitCommitRequest.js +0 -63
  125. package/dist/client/models/GitCommitResponse.d.ts +0 -32
  126. package/dist/client/models/GitCommitResponse.js +0 -51
  127. package/dist/client/models/GitNamespace.d.ts +0 -38
  128. package/dist/client/models/GitNamespace.js +0 -55
  129. package/dist/client/models/GitProvider.d.ts +0 -75
  130. package/dist/client/models/GitProvider.js +0 -76
  131. package/dist/client/models/GitPullRequest.d.ts +0 -68
  132. package/dist/client/models/GitPullRequest.js +0 -75
  133. package/dist/client/models/GitRepoRequest.d.ts +0 -44
  134. package/dist/client/models/GitRepoRequest.js +0 -55
  135. package/dist/client/models/GitRepository.d.ts +0 -87
  136. package/dist/client/models/GitRepository.js +0 -84
  137. package/dist/client/models/GitStatus.d.ts +0 -57
  138. package/dist/client/models/GitStatus.js +0 -62
  139. package/dist/client/models/GitUser.d.ts +0 -50
  140. package/dist/client/models/GitUser.js +0 -63
  141. package/dist/client/models/InstallProviderRequest.d.ts +0 -40
  142. package/dist/client/models/InstallProviderRequest.js +0 -55
  143. package/dist/client/models/ListBranchResponse.d.ts +0 -32
  144. package/dist/client/models/ListBranchResponse.js +0 -51
  145. package/dist/client/models/LogFileConfig.d.ts +0 -62
  146. package/dist/client/models/LogFileConfig.js +0 -67
  147. package/dist/client/models/LspCompletionParams.d.ts +0 -58
  148. package/dist/client/models/LspCompletionParams.js +0 -69
  149. package/dist/client/models/LspDocumentRequest.d.ts +0 -44
  150. package/dist/client/models/LspDocumentRequest.js +0 -59
  151. package/dist/client/models/LspLocation.d.ts +0 -39
  152. package/dist/client/models/LspLocation.js +0 -56
  153. package/dist/client/models/LspPosition.d.ts +0 -38
  154. package/dist/client/models/LspPosition.js +0 -55
  155. package/dist/client/models/LspRange.d.ts +0 -39
  156. package/dist/client/models/LspRange.js +0 -56
  157. package/dist/client/models/LspServerRequest.d.ts +0 -38
  158. package/dist/client/models/LspServerRequest.js +0 -55
  159. package/dist/client/models/LspSymbol.d.ts +0 -45
  160. package/dist/client/models/LspSymbol.js +0 -60
  161. package/dist/client/models/Match.d.ts +0 -44
  162. package/dist/client/models/Match.js +0 -59
  163. package/dist/client/models/NetworkKey.d.ts +0 -32
  164. package/dist/client/models/NetworkKey.js +0 -51
  165. package/dist/client/models/Position.d.ts +0 -38
  166. package/dist/client/models/Position.js +0 -55
  167. package/dist/client/models/PrebuildConfig.d.ts +0 -56
  168. package/dist/client/models/PrebuildConfig.js +0 -67
  169. package/dist/client/models/PrebuildDTO.d.ts +0 -62
  170. package/dist/client/models/PrebuildDTO.js +0 -68
  171. package/dist/client/models/ProfileData.d.ts +0 -34
  172. package/dist/client/models/ProfileData.js +0 -51
  173. package/dist/client/models/Project.d.ts +0 -91
  174. package/dist/client/models/Project.js +0 -88
  175. package/dist/client/models/ProjectConfig.d.ts +0 -84
  176. package/dist/client/models/ProjectConfig.js +0 -87
  177. package/dist/client/models/ProjectDirResponse.d.ts +0 -32
  178. package/dist/client/models/ProjectDirResponse.js +0 -49
  179. package/dist/client/models/ProjectInfo.d.ts +0 -56
  180. package/dist/client/models/ProjectInfo.js +0 -65
  181. package/dist/client/models/ProjectState.d.ts +0 -45
  182. package/dist/client/models/ProjectState.js +0 -60
  183. package/dist/client/models/Provider.d.ts +0 -44
  184. package/dist/client/models/Provider.js +0 -57
  185. package/dist/client/models/ProviderProviderInfo.d.ts +0 -44
  186. package/dist/client/models/ProviderProviderInfo.js +0 -57
  187. package/dist/client/models/ProviderProviderTargetProperty.d.ts +0 -72
  188. package/dist/client/models/ProviderProviderTargetProperty.js +0 -64
  189. package/dist/client/models/ProviderProviderTargetPropertyType.d.ts +0 -29
  190. package/dist/client/models/ProviderProviderTargetPropertyType.js +0 -55
  191. package/dist/client/models/ProviderTarget.d.ts +0 -51
  192. package/dist/client/models/ProviderTarget.js +0 -64
  193. package/dist/client/models/ReplaceRequest.d.ts +0 -44
  194. package/dist/client/models/ReplaceRequest.js +0 -59
  195. package/dist/client/models/ReplaceResult.d.ts +0 -44
  196. package/dist/client/models/ReplaceResult.js +0 -53
  197. package/dist/client/models/RepositoryUrl.d.ts +0 -32
  198. package/dist/client/models/RepositoryUrl.js +0 -51
  199. package/dist/client/models/Sample.d.ts +0 -44
  200. package/dist/client/models/Sample.js +0 -59
  201. package/dist/client/models/SearchFilesResponse.d.ts +0 -32
  202. package/dist/client/models/SearchFilesResponse.js +0 -51
  203. package/dist/client/models/ServerConfig.d.ts +0 -130
  204. package/dist/client/models/ServerConfig.js +0 -119
  205. package/dist/client/models/SetGitProviderConfig.d.ts +0 -75
  206. package/dist/client/models/SetGitProviderConfig.js +0 -70
  207. package/dist/client/models/SetProjectState.d.ts +0 -39
  208. package/dist/client/models/SetProjectState.js +0 -56
  209. package/dist/client/models/SigningMethod.d.ts +0 -25
  210. package/dist/client/models/SigningMethod.js +0 -51
  211. package/dist/client/models/Status.d.ts +0 -31
  212. package/dist/client/models/Status.js +0 -57
  213. package/dist/client/models/Workspace.d.ts +0 -51
  214. package/dist/client/models/Workspace.js +0 -64
  215. package/dist/client/models/WorkspaceDTO.d.ts +0 -58
  216. package/dist/client/models/WorkspaceDTO.js +0 -67
  217. package/dist/client/models/WorkspaceInfo.d.ts +0 -45
  218. package/dist/client/models/WorkspaceInfo.js +0 -58
  219. package/dist/client/models/index.d.ts +0 -79
  220. package/dist/client/models/index.js +0 -97
  221. package/dist/client/runtime.d.ts +0 -182
  222. package/dist/client/runtime.js +0 -340
package/dist/Daytona.js CHANGED
@@ -1,32 +1,134 @@
1
1
  "use strict";
2
+ /**
3
+ * Sandboxes are isolated development environments managed by Daytona.
4
+ * This guide covers how to create, manage, and remove Sandboxes using the SDK.
5
+ *
6
+ * @module Daytona
7
+ *
8
+ * @example
9
+ * // Initialize using environment variables (DAYTONA_API_KEY, DAYTONA_SERVER_URL, DAYTONA_TARGET)
10
+ * const daytona = new Daytona();
11
+ *
12
+ * // Create and use a workspace
13
+ * const workspace = await daytona.create({
14
+ * language: 'typescript',
15
+ * envVars: { NODE_ENV: 'development' }
16
+ * });
17
+ *
18
+ * // Execute commands in the workspace
19
+ * const response = await workspace.process.executeCommand('echo "Hello, World!"');
20
+ * console.log(response.result);
21
+ *
22
+ * // Execute code in the workspace
23
+ * const response = await workspace.process.codeRun('console.log("Hello, World!")');
24
+ * console.log(response.result);
25
+ *
26
+ * @example
27
+ * // Initialize with explicit configuration
28
+ * const daytona = new Daytona({
29
+ * apiKey: process.env.CUSTOM_API_KEY,
30
+ * serverUrl: 'https://daytona.example.com',
31
+ * target: 'us'
32
+ * });
33
+ *
34
+ * // Create a custom workspace
35
+ * const workspace = await daytona.create({
36
+ * language: 'typescript',
37
+ * image: 'node:18',
38
+ * resources: {
39
+ * cpu: 2,
40
+ * memory: 4 // 4GB RAM
41
+ * },
42
+ * autoStopInterval: 60 // Auto-stop after 1 hour of inactivity
43
+ * });
44
+ *
45
+ * // Use workspace features
46
+ * await workspace.git.clone('https://github.com/user/repo.git');
47
+ * await workspace.process.executeCommand('npm test');
48
+ */
49
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
50
+ if (k2 === undefined) k2 = k;
51
+ var desc = Object.getOwnPropertyDescriptor(m, k);
52
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
53
+ desc = { enumerable: true, get: function() { return m[k]; } };
54
+ }
55
+ Object.defineProperty(o, k2, desc);
56
+ }) : (function(o, m, k, k2) {
57
+ if (k2 === undefined) k2 = k;
58
+ o[k2] = m[k];
59
+ }));
60
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
61
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
62
+ }) : function(o, v) {
63
+ o["default"] = v;
64
+ });
65
+ var __importStar = (this && this.__importStar) || function (mod) {
66
+ if (mod && mod.__esModule) return mod;
67
+ var result = {};
68
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
69
+ __setModuleDefault(result, mod);
70
+ return result;
71
+ };
2
72
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Daytona = void 0;
73
+ exports.Daytona = exports.CodeLanguage = void 0;
4
74
  const uuid_1 = require("uuid");
5
75
  const WorkspacePythonCodeToolbox_1 = require("./code-toolbox/WorkspacePythonCodeToolbox");
6
76
  const Workspace_1 = require("./Workspace");
7
77
  const api_client_1 = require("@daytonaio/api-client");
8
78
  const WorkspaceTsCodeToolbox_1 = require("./code-toolbox/WorkspaceTsCodeToolbox");
79
+ const axios_1 = __importStar(require("axios"));
80
+ const DaytonaError_1 = require("./errors/DaytonaError");
81
+ /**
82
+ * Supported programming languages for code execution
83
+ */
84
+ var CodeLanguage;
85
+ (function (CodeLanguage) {
86
+ CodeLanguage["PYTHON"] = "python";
87
+ CodeLanguage["TYPESCRIPT"] = "typescript";
88
+ CodeLanguage["JAVASCRIPT"] = "javascript";
89
+ })(CodeLanguage || (exports.CodeLanguage = CodeLanguage = {}));
9
90
  /**
10
- * Main class for interacting with Daytona Server API
11
- * @class Daytona
91
+ * Main class for interacting with Daytona Server API.
92
+ *
93
+ * Provides methods for creating, managing, and interacting with Daytona Sandboxes.
94
+ * Can be initialized either with explicit configuration or using environment variables.
95
+ *
96
+ *
97
+ * @example
98
+ * // Using environment variables
99
+ * // Uses DAYTONA_API_KEY, DAYTONA_SERVER_URL, DAYTONA_TARGET
100
+ * const daytona = new Daytona();
101
+ * const workspace = await daytona.create();
102
+ *
103
+ * @example
104
+ * // Using explicit configuration
105
+ * const config: DaytonaConfig = {
106
+ * apiKey: "your-api-key",
107
+ * serverUrl: "https://your-server.com",
108
+ * target: "us"
109
+ * };
110
+ * const daytona = new Daytona(config);
111
+ *
112
+ * @class
12
113
  */
13
114
  class Daytona {
14
115
  /**
15
- * Creates a new Daytona client instance
116
+ * Creates a new Daytona client instance.
117
+ *
16
118
  * @param {DaytonaConfig} [config] - Configuration options
17
- * @throws {Error} When API key or server URL is missing
119
+ * @throws {DaytonaError} - `DaytonaError` - When API key or server URL is missing
18
120
  */
19
121
  constructor(config) {
20
122
  const apiKey = (config === null || config === void 0 ? void 0 : config.apiKey) || process.env.DAYTONA_API_KEY;
21
123
  if (!apiKey) {
22
- throw new Error('API key is required');
124
+ throw new DaytonaError_1.DaytonaError('API key is required');
23
125
  }
24
126
  const serverUrl = (config === null || config === void 0 ? void 0 : config.serverUrl) || process.env.DAYTONA_SERVER_URL;
25
127
  if (!serverUrl) {
26
- throw new Error('Server URL is required');
128
+ throw new DaytonaError_1.DaytonaError('Server URL is required');
27
129
  }
28
130
  const envTarget = process.env.DAYTONA_TARGET;
29
- const target = (config === null || config === void 0 ? void 0 : config.target) || envTarget;
131
+ const target = (config === null || config === void 0 ? void 0 : config.target) || envTarget || api_client_1.CreateWorkspaceTargetEnum.US;
30
132
  this.apiKey = apiKey;
31
133
  this.serverUrl = serverUrl;
32
134
  this.target = target;
@@ -38,109 +140,225 @@ class Daytona {
38
140
  },
39
141
  },
40
142
  });
41
- this.workspaceApi = new api_client_1.WorkspaceApi(configuration);
42
- this.toolboxApi = new api_client_1.ToolboxApi(configuration);
143
+ const axiosInstance = axios_1.default.create();
144
+ axiosInstance.interceptors.response.use((response) => {
145
+ return response;
146
+ }, (error) => {
147
+ var _a, _b, _c;
148
+ let errorMessage;
149
+ if (error instanceof axios_1.AxiosError && error.message.includes('timeout of')) {
150
+ errorMessage = "Operation timed out";
151
+ }
152
+ else {
153
+ errorMessage =
154
+ ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
155
+ ((_c = error.response) === null || _c === void 0 ? void 0 : _c.data) ||
156
+ error.message ||
157
+ String(error);
158
+ }
159
+ throw new DaytonaError_1.DaytonaError(errorMessage);
160
+ });
161
+ this.workspaceApi = new api_client_1.WorkspaceApi(configuration, '', axiosInstance);
162
+ this.toolboxApi = new api_client_1.ToolboxApi(configuration, '', axiosInstance);
43
163
  }
44
164
  /**
45
- * Creates a new workspace
46
- * @param {CreateWorkspaceParams} [params] - Parameters for workspace creation
47
- * @returns {Promise<Workspace>} The created workspace instance
165
+ * Creates Sandboxes with default or custom configurations. You can specify various parameters,
166
+ * including language, image, resources, environment variables, and volumes for the Sandbox.
167
+ *
168
+ * @param {CreateWorkspaceParams} [params] - Parameters for Sandbox creation
169
+ * @param {number} [timeout] - Timeout in seconds (0 means no timeout, default is 60)
170
+ * @returns {Promise<Workspace>} The created Sandbox instance
171
+ *
172
+ * @example
173
+ * // Create a default workspace
174
+ * const workspace = await daytona.create();
175
+ *
176
+ * @example
177
+ * // Create a custom workspace
178
+ * const params: CreateWorkspaceParams = {
179
+ * language: 'typescript',
180
+ * image: 'node:18',
181
+ * envVars: {
182
+ * NODE_ENV: 'development',
183
+ * DEBUG: 'true'
184
+ * },
185
+ * resources: {
186
+ * cpu: 2,
187
+ * memory: 4 // 4GB RAM
188
+ * },
189
+ * autoStopInterval: 60
190
+ * };
191
+ * const workspace = await daytona.create(params, 40);
48
192
  */
49
- async create(params) {
193
+ async create(params, timeout = 60) {
50
194
  var _a, _b, _c, _d;
51
- const workspaceId = (params === null || params === void 0 ? void 0 : params.id) || `sandbox-${(0, uuid_1.v4)().slice(0, 8)}`;
52
- const codeToolbox = this.getCodeToolbox(params === null || params === void 0 ? void 0 : params.language);
53
- const labels = (params === null || params === void 0 ? void 0 : params.labels) || {};
54
- if (params === null || params === void 0 ? void 0 : params.language) {
195
+ const startTime = Date.now();
196
+ if (params == null) {
197
+ params = { language: 'python' };
198
+ }
199
+ const labels = params.labels || {};
200
+ if (params.language) {
55
201
  labels[`code-toolbox-language`] = params.language;
56
202
  }
57
- const reponse = await this.workspaceApi.createWorkspace({
58
- id: workspaceId,
59
- name: workspaceId, // todo: remove this after project refactor
60
- image: params === null || params === void 0 ? void 0 : params.image,
61
- user: params === null || params === void 0 ? void 0 : params.user,
62
- env: (params === null || params === void 0 ? void 0 : params.envVars) || {},
63
- target: this.target,
64
- cpu: (_a = params === null || params === void 0 ? void 0 : params.resources) === null || _a === void 0 ? void 0 : _a.cpu,
65
- gpu: (_b = params === null || params === void 0 ? void 0 : params.resources) === null || _b === void 0 ? void 0 : _b.gpu,
66
- memory: (_c = params === null || params === void 0 ? void 0 : params.resources) === null || _c === void 0 ? void 0 : _c.memory,
67
- disk: (_d = params === null || params === void 0 ? void 0 : params.resources) === null || _d === void 0 ? void 0 : _d.disk,
68
- });
69
- const workspaceInstance = reponse.data;
70
- const workspace = new Workspace_1.Workspace(workspaceId, workspaceInstance, this.workspaceApi, this.toolboxApi, codeToolbox);
71
- if (!(params === null || params === void 0 ? void 0 : params.async)) {
72
- await workspace.waitUntilStarted();
203
+ params.id = params.id || `sandbox-${(0, uuid_1.v4)().slice(0, 8)}`;
204
+ // remove this when params.timeout is removed
205
+ const effectiveTimeout = params.timeout || timeout;
206
+ if (effectiveTimeout < 0) {
207
+ throw new DaytonaError_1.DaytonaError('Timeout must be a non-negative number');
208
+ }
209
+ if (params.autoStopInterval !== undefined && (!Number.isInteger(params.autoStopInterval) || params.autoStopInterval < 0)) {
210
+ throw new DaytonaError_1.DaytonaError('autoStopInterval must be a non-negative integer');
211
+ }
212
+ const codeToolbox = this.getCodeToolbox(params.language);
213
+ try {
214
+ const response = await this.workspaceApi.createWorkspace({
215
+ id: params.id,
216
+ name: params.id,
217
+ image: params.image,
218
+ user: params.user,
219
+ env: params.envVars || {},
220
+ labels: params.labels,
221
+ public: params.public,
222
+ target: this.target,
223
+ cpu: (_a = params.resources) === null || _a === void 0 ? void 0 : _a.cpu,
224
+ gpu: (_b = params.resources) === null || _b === void 0 ? void 0 : _b.gpu,
225
+ memory: (_c = params.resources) === null || _c === void 0 ? void 0 : _c.memory,
226
+ disk: (_d = params.resources) === null || _d === void 0 ? void 0 : _d.disk,
227
+ autoStopInterval: params.autoStopInterval,
228
+ }, {
229
+ timeout: effectiveTimeout * 1000
230
+ });
231
+ const workspaceInstance = response.data;
232
+ const workspaceInfo = Workspace_1.Workspace.toWorkspaceInfo(workspaceInstance);
233
+ workspaceInstance.info = workspaceInfo;
234
+ const workspace = new Workspace_1.Workspace(params.id, workspaceInstance, this.workspaceApi, this.toolboxApi, codeToolbox);
235
+ if (!params.async) {
236
+ const timeElapsed = Date.now() - startTime;
237
+ await workspace.waitUntilStarted(effectiveTimeout ? effectiveTimeout - (timeElapsed / 1000) : 0);
238
+ }
239
+ return workspace;
240
+ }
241
+ catch (error) {
242
+ void this.workspaceApi.deleteWorkspace(params.id, true).catch(() => { });
243
+ if (error instanceof DaytonaError_1.DaytonaError && error.message.includes("Operation timed out")) {
244
+ throw new DaytonaError_1.DaytonaError(`Failed to create and start workspace within ${effectiveTimeout} seconds. Operation timed out.`);
245
+ }
246
+ throw error;
73
247
  }
74
- return workspace;
75
248
  }
76
249
  /**
77
- * Gets a workspace by its ID
78
- * @param {string} workspaceId - The ID of the workspace to retrieve
79
- * @returns {Promise<Workspace>} The workspace instance
250
+ * Gets a Sandbox by its ID.
251
+ *
252
+ * @param {string} workspaceId - The ID of the Sandbox to retrieve
253
+ * @returns {Promise<Workspace>} The Sandbox
254
+ *
255
+ * @example
256
+ * const workspace = await daytona.get('my-workspace-id');
257
+ * console.log(`Workspace state: ${workspace.instance.state}`);
80
258
  */
81
259
  async get(workspaceId) {
82
260
  const response = await this.workspaceApi.getWorkspace(workspaceId);
83
261
  const workspaceInstance = response.data;
84
262
  const language = workspaceInstance.labels && workspaceInstance.labels[`code-toolbox-language`];
85
263
  const codeToolbox = this.getCodeToolbox(language);
264
+ const workspaceInfo = Workspace_1.Workspace.toWorkspaceInfo(workspaceInstance);
265
+ workspaceInstance.info = workspaceInfo;
86
266
  return new Workspace_1.Workspace(workspaceId, workspaceInstance, this.workspaceApi, this.toolboxApi, codeToolbox);
87
267
  }
88
268
  /**
89
- * Lists all workspaces
90
- * @returns {Promise<Workspace[]>} The list of workspaces
269
+ * Lists all Sandboxes.
270
+ *
271
+ * @returns {Promise<Workspace[]>} Array of Sandboxes
272
+ *
273
+ * @example
274
+ * const workspaces = await daytona.list();
275
+ * for (const workspace of workspaces) {
276
+ * console.log(`${workspace.id}: ${workspace.instance.state}`);
277
+ * }
91
278
  */
92
279
  async list() {
93
280
  const response = await this.workspaceApi.listWorkspaces();
94
281
  return response.data.map((workspace) => {
95
282
  var _a;
96
283
  const language = (_a = workspace.labels) === null || _a === void 0 ? void 0 : _a[`code-toolbox-language`];
97
- if (language && !['python', 'javascript', 'typescript'].includes(language)) {
98
- throw new Error(`Invalid code-toolbox-language: ${language}`);
99
- }
284
+ const workspaceInfo = Workspace_1.Workspace.toWorkspaceInfo(workspace);
285
+ workspace.info = workspaceInfo;
100
286
  return new Workspace_1.Workspace(workspace.id, workspace, this.workspaceApi, this.toolboxApi, this.getCodeToolbox(language));
101
287
  });
102
288
  }
103
289
  /**
104
- * Starts a workspace
105
- * @param {Workspace} workspace - The workspace to start
290
+ * Starts a Sandbox and waits for it to be ready.
291
+ *
292
+ * @param {Workspace} workspace - The Sandbox to start
293
+ * @param {number} [timeout] - Optional timeout in seconds (0 means no timeout)
294
+ * @returns {Promise<void>}
295
+ *
296
+ * @example
297
+ * const workspace = await daytona.get('my-workspace-id');
298
+ * // Wait up to 60 seconds for the workspace to start
299
+ * await daytona.start(workspace, 60);
106
300
  */
107
- async start(workspace) {
108
- await workspace.start();
301
+ async start(workspace, timeout) {
302
+ await workspace.start(timeout);
109
303
  }
110
304
  /**
111
- * Stops a workspace
112
- * @param {Workspace} workspace - The workspace to stop
305
+ * Stops a Sandbox.
306
+ *
307
+ * @param {Workspace} workspace - The Sandbox to stop
113
308
  * @returns {Promise<void>}
309
+ *
310
+ * @example
311
+ * const workspace = await daytona.get('my-workspace-id');
312
+ * await daytona.stop(workspace);
114
313
  */
115
314
  async stop(workspace) {
116
315
  await workspace.stop();
117
316
  }
118
317
  /**
119
- * Removes a workspace
120
- * @param {Workspace} workspace - The workspace to remove
318
+ * Removes a Sandbox.
319
+ *
320
+ * @param {Workspace} workspace - The Sandbox to remove
321
+ * @param {number} timeout - Timeout in seconds (0 means no timeout, default is 60)
121
322
  * @returns {Promise<void>}
323
+ *
324
+ * @example
325
+ * const workspace = await daytona.get('my-workspace-id');
326
+ * await daytona.remove(workspace);
122
327
  */
123
- async remove(workspace) {
124
- await this.workspaceApi.deleteWorkspace(workspace.id, true);
328
+ async remove(workspace, timeout = 60) {
329
+ await this.workspaceApi.deleteWorkspace(workspace.id, true, { timeout: timeout * 1000 });
125
330
  }
126
331
  /**
127
- * Gets the current workspace by ID
128
- * @param {string} workspaceId - The ID of the workspace to retrieve
129
- * @returns {Promise<Workspace>} The workspace instance
332
+ * Gets the Sandbox by ID.
333
+ *
334
+ * @param {string} workspaceId - The ID of the Sandbox to retrieve
335
+ * @returns {Promise<Workspace>} The Sandbox
336
+ *
337
+ * @example
338
+ * const workspace = await daytona.getCurrentWorkspace('my-workspace-id');
339
+ * console.log(`Current workspace state: ${workspace.instance.state}`);
130
340
  */
131
341
  async getCurrentWorkspace(workspaceId) {
132
342
  return this.get(workspaceId);
133
343
  }
344
+ /**
345
+ * Gets the appropriate code toolbox based on language.
346
+ *
347
+ * @private
348
+ * @param {CodeLanguage} [language] - Programming language for the toolbox
349
+ * @returns {WorkspaceCodeToolbox} The appropriate code toolbox instance
350
+ * @throws {DaytonaError} - `DaytonaError` - When an unsupported language is specified
351
+ */
134
352
  getCodeToolbox(language) {
135
353
  switch (language) {
136
- case 'javascript':
137
- case 'typescript':
354
+ case CodeLanguage.JAVASCRIPT:
355
+ case CodeLanguage.TYPESCRIPT:
138
356
  return new WorkspaceTsCodeToolbox_1.WorkspaceTsCodeToolbox();
139
- case 'python':
357
+ case CodeLanguage.PYTHON:
140
358
  case undefined:
141
359
  return new WorkspacePythonCodeToolbox_1.WorkspacePythonCodeToolbox();
142
360
  default:
143
- throw new Error(`Unsupported language: ${language}`);
361
+ throw new DaytonaError_1.DaytonaError(`Unsupported language: ${language}, supported languages: ${Object.values(CodeLanguage).join(', ')}`);
144
362
  }
145
363
  }
146
364
  }