@cloudflare/sandbox 0.0.0-eec5bb6 → 0.0.0-f06fee3

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 (74) hide show
  1. package/CHANGELOG.md +136 -15
  2. package/Dockerfile +100 -53
  3. package/README.md +92 -769
  4. package/dist/index.d.ts +1889 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +3146 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +16 -8
  9. package/src/clients/base-client.ts +295 -0
  10. package/src/clients/command-client.ts +115 -0
  11. package/src/clients/file-client.ts +300 -0
  12. package/src/clients/git-client.ts +91 -0
  13. package/src/clients/index.ts +60 -0
  14. package/src/clients/interpreter-client.ts +333 -0
  15. package/src/clients/port-client.ts +105 -0
  16. package/src/clients/process-client.ts +180 -0
  17. package/src/clients/sandbox-client.ts +39 -0
  18. package/src/clients/types.ts +88 -0
  19. package/src/clients/utility-client.ts +123 -0
  20. package/src/errors/adapter.ts +238 -0
  21. package/src/errors/classes.ts +594 -0
  22. package/src/errors/index.ts +109 -0
  23. package/src/file-stream.ts +169 -0
  24. package/src/index.ts +88 -63
  25. package/src/interpreter.ts +58 -40
  26. package/src/request-handler.ts +94 -55
  27. package/src/sandbox.ts +980 -492
  28. package/src/security.ts +34 -28
  29. package/src/sse-parser.ts +8 -11
  30. package/src/version.ts +6 -0
  31. package/startup.sh +3 -0
  32. package/tests/base-client.test.ts +364 -0
  33. package/tests/command-client.test.ts +444 -0
  34. package/tests/file-client.test.ts +831 -0
  35. package/tests/file-stream.test.ts +310 -0
  36. package/tests/get-sandbox.test.ts +149 -0
  37. package/tests/git-client.test.ts +415 -0
  38. package/tests/port-client.test.ts +293 -0
  39. package/tests/process-client.test.ts +683 -0
  40. package/tests/request-handler.test.ts +292 -0
  41. package/tests/sandbox.test.ts +706 -0
  42. package/tests/sse-parser.test.ts +291 -0
  43. package/tests/utility-client.test.ts +339 -0
  44. package/tests/version.test.ts +16 -0
  45. package/tests/wrangler.jsonc +35 -0
  46. package/tsconfig.json +9 -1
  47. package/tsdown.config.ts +12 -0
  48. package/vitest.config.ts +31 -0
  49. package/container_src/bun.lock +0 -76
  50. package/container_src/circuit-breaker.ts +0 -121
  51. package/container_src/control-process.ts +0 -784
  52. package/container_src/handler/exec.ts +0 -185
  53. package/container_src/handler/file.ts +0 -406
  54. package/container_src/handler/git.ts +0 -130
  55. package/container_src/handler/ports.ts +0 -314
  56. package/container_src/handler/process.ts +0 -568
  57. package/container_src/handler/session.ts +0 -92
  58. package/container_src/index.ts +0 -592
  59. package/container_src/interpreter-service.ts +0 -276
  60. package/container_src/isolation.ts +0 -1038
  61. package/container_src/mime-processor.ts +0 -255
  62. package/container_src/package.json +0 -18
  63. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  64. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  65. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  66. package/container_src/runtime/process-pool.ts +0 -464
  67. package/container_src/shell-escape.ts +0 -42
  68. package/container_src/startup.sh +0 -11
  69. package/container_src/types.ts +0 -131
  70. package/src/client.ts +0 -1009
  71. package/src/errors.ts +0 -219
  72. package/src/interpreter-client.ts +0 -352
  73. package/src/interpreter-types.ts +0 -390
  74. package/src/types.ts +0 -502
@@ -0,0 +1,180 @@
1
+ import type {
2
+ ProcessCleanupResult,
3
+ ProcessInfoResult,
4
+ ProcessKillResult,
5
+ ProcessListResult,
6
+ ProcessLogsResult,
7
+ ProcessStartResult,
8
+ StartProcessRequest
9
+ } from '@repo/shared';
10
+ import { BaseHttpClient } from './base-client';
11
+ import type { HttpClientOptions } from './types';
12
+
13
+ // Re-export for convenience
14
+ export type {
15
+ StartProcessRequest,
16
+ ProcessStartResult,
17
+ ProcessListResult,
18
+ ProcessInfoResult,
19
+ ProcessKillResult,
20
+ ProcessLogsResult,
21
+ ProcessCleanupResult
22
+ };
23
+
24
+ /**
25
+ * Client for background process management
26
+ */
27
+ export class ProcessClient extends BaseHttpClient {
28
+ /**
29
+ * Start a background process
30
+ * @param command - Command to execute as a background process
31
+ * @param sessionId - The session ID for this operation
32
+ * @param options - Optional settings (processId)
33
+ */
34
+ async startProcess(
35
+ command: string,
36
+ sessionId: string,
37
+ options?: { processId?: string }
38
+ ): Promise<ProcessStartResult> {
39
+ try {
40
+ const data: StartProcessRequest = {
41
+ command,
42
+ sessionId,
43
+ processId: options?.processId
44
+ };
45
+
46
+ const response = await this.post<ProcessStartResult>(
47
+ '/api/process/start',
48
+ data
49
+ );
50
+
51
+ this.logSuccess(
52
+ 'Process started',
53
+ `${command} (ID: ${response.processId})`
54
+ );
55
+
56
+ return response;
57
+ } catch (error) {
58
+ this.logError('startProcess', error);
59
+ throw error;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * List all processes (sandbox-scoped, not session-scoped)
65
+ */
66
+ async listProcesses(): Promise<ProcessListResult> {
67
+ try {
68
+ const url = `/api/process/list`;
69
+ const response = await this.get<ProcessListResult>(url);
70
+
71
+ this.logSuccess(
72
+ 'Processes listed',
73
+ `${response.processes.length} processes`
74
+ );
75
+ return response;
76
+ } catch (error) {
77
+ this.logError('listProcesses', error);
78
+ throw error;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Get information about a specific process (sandbox-scoped, not session-scoped)
84
+ * @param processId - ID of the process to retrieve
85
+ */
86
+ async getProcess(processId: string): Promise<ProcessInfoResult> {
87
+ try {
88
+ const url = `/api/process/${processId}`;
89
+ const response = await this.get<ProcessInfoResult>(url);
90
+
91
+ this.logSuccess('Process retrieved', `ID: ${processId}`);
92
+ return response;
93
+ } catch (error) {
94
+ this.logError('getProcess', error);
95
+ throw error;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Kill a specific process (sandbox-scoped, not session-scoped)
101
+ * @param processId - ID of the process to kill
102
+ */
103
+ async killProcess(processId: string): Promise<ProcessKillResult> {
104
+ try {
105
+ const url = `/api/process/${processId}`;
106
+ const response = await this.delete<ProcessKillResult>(url);
107
+
108
+ this.logSuccess('Process killed', `ID: ${processId}`);
109
+ return response;
110
+ } catch (error) {
111
+ this.logError('killProcess', error);
112
+ throw error;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Kill all running processes (sandbox-scoped, not session-scoped)
118
+ */
119
+ async killAllProcesses(): Promise<ProcessCleanupResult> {
120
+ try {
121
+ const url = `/api/process/kill-all`;
122
+ const response = await this.delete<ProcessCleanupResult>(url);
123
+
124
+ this.logSuccess(
125
+ 'All processes killed',
126
+ `${response.cleanedCount} processes terminated`
127
+ );
128
+
129
+ return response;
130
+ } catch (error) {
131
+ this.logError('killAllProcesses', error);
132
+ throw error;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Get logs from a specific process (sandbox-scoped, not session-scoped)
138
+ * @param processId - ID of the process to get logs from
139
+ */
140
+ async getProcessLogs(processId: string): Promise<ProcessLogsResult> {
141
+ try {
142
+ const url = `/api/process/${processId}/logs`;
143
+ const response = await this.get<ProcessLogsResult>(url);
144
+
145
+ this.logSuccess(
146
+ 'Process logs retrieved',
147
+ `ID: ${processId}, stdout: ${response.stdout.length} chars, stderr: ${response.stderr.length} chars`
148
+ );
149
+
150
+ return response;
151
+ } catch (error) {
152
+ this.logError('getProcessLogs', error);
153
+ throw error;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Stream logs from a specific process (sandbox-scoped, not session-scoped)
159
+ * @param processId - ID of the process to stream logs from
160
+ */
161
+ async streamProcessLogs(
162
+ processId: string
163
+ ): Promise<ReadableStream<Uint8Array>> {
164
+ try {
165
+ const url = `/api/process/${processId}/stream`;
166
+ const response = await this.doFetch(url, {
167
+ method: 'GET'
168
+ });
169
+
170
+ const stream = await this.handleStreamResponse(response);
171
+
172
+ this.logSuccess('Process log stream started', `ID: ${processId}`);
173
+
174
+ return stream;
175
+ } catch (error) {
176
+ this.logError('streamProcessLogs', error);
177
+ throw error;
178
+ }
179
+ }
180
+ }
@@ -0,0 +1,39 @@
1
+ import { CommandClient } from './command-client';
2
+ import { FileClient } from './file-client';
3
+ import { GitClient } from './git-client';
4
+ import { InterpreterClient } from './interpreter-client';
5
+ import { PortClient } from './port-client';
6
+ import { ProcessClient } from './process-client';
7
+ import type { HttpClientOptions } from './types';
8
+ import { UtilityClient } from './utility-client';
9
+
10
+ /**
11
+ * Main sandbox client that composes all domain-specific clients
12
+ * Provides organized access to all sandbox functionality
13
+ */
14
+ export class SandboxClient {
15
+ public readonly commands: CommandClient;
16
+ public readonly files: FileClient;
17
+ public readonly processes: ProcessClient;
18
+ public readonly ports: PortClient;
19
+ public readonly git: GitClient;
20
+ public readonly interpreter: InterpreterClient;
21
+ public readonly utils: UtilityClient;
22
+
23
+ constructor(options: HttpClientOptions) {
24
+ // Ensure baseUrl is provided for all clients
25
+ const clientOptions: HttpClientOptions = {
26
+ baseUrl: 'http://localhost:3000',
27
+ ...options
28
+ };
29
+
30
+ // Initialize all domain clients with shared options
31
+ this.commands = new CommandClient(clientOptions);
32
+ this.files = new FileClient(clientOptions);
33
+ this.processes = new ProcessClient(clientOptions);
34
+ this.ports = new PortClient(clientOptions);
35
+ this.git = new GitClient(clientOptions);
36
+ this.interpreter = new InterpreterClient(clientOptions);
37
+ this.utils = new UtilityClient(clientOptions);
38
+ }
39
+ }
@@ -0,0 +1,88 @@
1
+ import type { Logger } from '@repo/shared';
2
+
3
+ /**
4
+ * Minimal interface for container fetch functionality
5
+ */
6
+ export interface ContainerStub {
7
+ containerFetch(
8
+ url: string,
9
+ options: RequestInit,
10
+ port?: number
11
+ ): Promise<Response>;
12
+ }
13
+
14
+ /**
15
+ * Shared HTTP client configuration options
16
+ */
17
+ export interface HttpClientOptions {
18
+ logger?: Logger;
19
+ baseUrl?: string;
20
+ port?: number;
21
+ stub?: ContainerStub;
22
+ onCommandComplete?: (
23
+ success: boolean,
24
+ exitCode: number,
25
+ stdout: string,
26
+ stderr: string,
27
+ command: string
28
+ ) => void;
29
+ onError?: (error: string, command?: string) => void;
30
+ }
31
+
32
+ /**
33
+ * Base response interface for all API responses
34
+ */
35
+ export interface BaseApiResponse {
36
+ success: boolean;
37
+ timestamp: string;
38
+ }
39
+
40
+ /**
41
+ * Standard error response structure - matches BaseHandler.createErrorResponse()
42
+ */
43
+ export interface ApiErrorResponse {
44
+ success: false;
45
+ error: string;
46
+ code: string;
47
+ details?: any;
48
+ timestamp: string;
49
+ }
50
+
51
+ /**
52
+ * Validation error response structure - matches ValidationMiddleware
53
+ */
54
+ export interface ValidationErrorResponse {
55
+ error: string;
56
+ message: string;
57
+ details?: any[];
58
+ timestamp: string;
59
+ }
60
+
61
+ /**
62
+ * Legacy error response interface - deprecated, use ApiErrorResponse
63
+ */
64
+ export interface ErrorResponse {
65
+ error: string;
66
+ details?: string;
67
+ code?: string;
68
+ }
69
+
70
+ /**
71
+ * HTTP request configuration
72
+ */
73
+ export interface RequestConfig extends RequestInit {
74
+ endpoint: string;
75
+ data?: Record<string, any>;
76
+ }
77
+
78
+ /**
79
+ * Typed response handler
80
+ */
81
+ export type ResponseHandler<T> = (response: Response) => Promise<T>;
82
+
83
+ /**
84
+ * Common session-aware request interface
85
+ */
86
+ export interface SessionRequest {
87
+ sessionId?: string;
88
+ }
@@ -0,0 +1,123 @@
1
+ import { BaseHttpClient } from './base-client';
2
+ import type { BaseApiResponse, HttpClientOptions } from './types';
3
+
4
+ /**
5
+ * Response interface for ping operations
6
+ */
7
+ export interface PingResponse extends BaseApiResponse {
8
+ message: string;
9
+ uptime?: number;
10
+ }
11
+
12
+ /**
13
+ * Response interface for getting available commands
14
+ */
15
+ export interface CommandsResponse extends BaseApiResponse {
16
+ availableCommands: string[];
17
+ count: number;
18
+ }
19
+
20
+ /**
21
+ * Response interface for getting container version
22
+ */
23
+ export interface VersionResponse extends BaseApiResponse {
24
+ version: string;
25
+ }
26
+
27
+ /**
28
+ * Request interface for creating sessions
29
+ */
30
+ export interface CreateSessionRequest {
31
+ id: string;
32
+ env?: Record<string, string>;
33
+ cwd?: string;
34
+ }
35
+
36
+ /**
37
+ * Response interface for creating sessions
38
+ */
39
+ export interface CreateSessionResponse extends BaseApiResponse {
40
+ id: string;
41
+ message: string;
42
+ }
43
+
44
+ /**
45
+ * Client for health checks and utility operations
46
+ */
47
+ export class UtilityClient extends BaseHttpClient {
48
+ /**
49
+ * Ping the sandbox to check if it's responsive
50
+ */
51
+ async ping(): Promise<string> {
52
+ try {
53
+ const response = await this.get<PingResponse>('/api/ping');
54
+
55
+ this.logSuccess('Ping successful', response.message);
56
+ return response.message;
57
+ } catch (error) {
58
+ this.logError('ping', error);
59
+ throw error;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Get list of available commands in the sandbox environment
65
+ */
66
+ async getCommands(): Promise<string[]> {
67
+ try {
68
+ const response = await this.get<CommandsResponse>('/api/commands');
69
+
70
+ this.logSuccess(
71
+ 'Commands retrieved',
72
+ `${response.count} commands available`
73
+ );
74
+
75
+ return response.availableCommands;
76
+ } catch (error) {
77
+ this.logError('getCommands', error);
78
+ throw error;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Create a new execution session
84
+ * @param options - Session configuration (id, env, cwd)
85
+ */
86
+ async createSession(
87
+ options: CreateSessionRequest
88
+ ): Promise<CreateSessionResponse> {
89
+ try {
90
+ const response = await this.post<CreateSessionResponse>(
91
+ '/api/session/create',
92
+ options
93
+ );
94
+
95
+ this.logSuccess('Session created', `ID: ${options.id}`);
96
+ return response;
97
+ } catch (error) {
98
+ this.logError('createSession', error);
99
+ throw error;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Get the container version
105
+ * Returns the version embedded in the Docker image during build
106
+ */
107
+ async getVersion(): Promise<string> {
108
+ try {
109
+ const response = await this.get<VersionResponse>('/api/version');
110
+
111
+ this.logSuccess('Version retrieved', response.version);
112
+ return response.version;
113
+ } catch (error) {
114
+ // If version endpoint doesn't exist (old container), return 'unknown'
115
+ // This allows for backward compatibility
116
+ this.logger.debug(
117
+ 'Failed to get container version (may be old container)',
118
+ { error }
119
+ );
120
+ return 'unknown';
121
+ }
122
+ }
123
+ }
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Error adapter that converts ErrorResponse to appropriate Error class
3
+ *
4
+ * Simple switch statement - we trust the container sends correct context
5
+ * No validation overhead since we control both sides
6
+ */
7
+
8
+ import type {
9
+ CodeExecutionContext,
10
+ CommandErrorContext,
11
+ CommandNotFoundContext,
12
+ ContextNotFoundContext,
13
+ ErrorResponse,
14
+ FileExistsContext,
15
+ FileNotFoundContext,
16
+ FileSystemContext,
17
+ GitAuthFailedContext,
18
+ GitBranchNotFoundContext,
19
+ GitErrorContext,
20
+ GitRepositoryNotFoundContext,
21
+ InternalErrorContext,
22
+ InterpreterNotReadyContext,
23
+ InvalidPortContext,
24
+ PortAlreadyExposedContext,
25
+ PortErrorContext,
26
+ PortNotExposedContext,
27
+ ProcessErrorContext,
28
+ ProcessNotFoundContext,
29
+ ValidationFailedContext
30
+ } from '@repo/shared/errors';
31
+ import { ErrorCode } from '@repo/shared/errors';
32
+
33
+ import {
34
+ CodeExecutionError,
35
+ CommandError,
36
+ CommandNotFoundError,
37
+ ContextNotFoundError,
38
+ CustomDomainRequiredError,
39
+ FileExistsError,
40
+ FileNotFoundError,
41
+ FileSystemError,
42
+ GitAuthenticationError,
43
+ GitBranchNotFoundError,
44
+ GitCheckoutError,
45
+ GitCloneError,
46
+ GitError,
47
+ GitNetworkError,
48
+ GitRepositoryNotFoundError,
49
+ InterpreterNotReadyError,
50
+ InvalidGitUrlError,
51
+ InvalidPortError,
52
+ PermissionDeniedError,
53
+ PortAlreadyExposedError,
54
+ PortError,
55
+ PortInUseError,
56
+ PortNotExposedError,
57
+ ProcessError,
58
+ ProcessNotFoundError,
59
+ SandboxError,
60
+ ServiceNotRespondingError,
61
+ ValidationFailedError
62
+ } from './classes';
63
+
64
+ /**
65
+ * Convert ErrorResponse to appropriate Error class
66
+ * Simple switch statement - we trust the container sends correct context
67
+ */
68
+ export function createErrorFromResponse(errorResponse: ErrorResponse): Error {
69
+ // We trust the container sends correct context, use type assertions
70
+ switch (errorResponse.code) {
71
+ // File System Errors
72
+ case ErrorCode.FILE_NOT_FOUND:
73
+ return new FileNotFoundError(
74
+ errorResponse as unknown as ErrorResponse<FileNotFoundContext>
75
+ );
76
+
77
+ case ErrorCode.FILE_EXISTS:
78
+ return new FileExistsError(
79
+ errorResponse as unknown as ErrorResponse<FileExistsContext>
80
+ );
81
+
82
+ case ErrorCode.PERMISSION_DENIED:
83
+ return new PermissionDeniedError(
84
+ errorResponse as unknown as ErrorResponse<FileSystemContext>
85
+ );
86
+
87
+ case ErrorCode.IS_DIRECTORY:
88
+ case ErrorCode.NOT_DIRECTORY:
89
+ case ErrorCode.NO_SPACE:
90
+ case ErrorCode.TOO_MANY_FILES:
91
+ case ErrorCode.RESOURCE_BUSY:
92
+ case ErrorCode.READ_ONLY:
93
+ case ErrorCode.NAME_TOO_LONG:
94
+ case ErrorCode.TOO_MANY_LINKS:
95
+ case ErrorCode.FILESYSTEM_ERROR:
96
+ return new FileSystemError(
97
+ errorResponse as unknown as ErrorResponse<FileSystemContext>
98
+ );
99
+
100
+ // Command Errors
101
+ case ErrorCode.COMMAND_NOT_FOUND:
102
+ return new CommandNotFoundError(
103
+ errorResponse as unknown as ErrorResponse<CommandNotFoundContext>
104
+ );
105
+
106
+ case ErrorCode.COMMAND_PERMISSION_DENIED:
107
+ case ErrorCode.COMMAND_EXECUTION_ERROR:
108
+ case ErrorCode.INVALID_COMMAND:
109
+ case ErrorCode.STREAM_START_ERROR:
110
+ return new CommandError(
111
+ errorResponse as unknown as ErrorResponse<CommandErrorContext>
112
+ );
113
+
114
+ // Process Errors
115
+ case ErrorCode.PROCESS_NOT_FOUND:
116
+ return new ProcessNotFoundError(
117
+ errorResponse as unknown as ErrorResponse<ProcessNotFoundContext>
118
+ );
119
+
120
+ case ErrorCode.PROCESS_PERMISSION_DENIED:
121
+ case ErrorCode.PROCESS_ERROR:
122
+ return new ProcessError(
123
+ errorResponse as unknown as ErrorResponse<ProcessErrorContext>
124
+ );
125
+
126
+ // Port Errors
127
+ case ErrorCode.PORT_ALREADY_EXPOSED:
128
+ return new PortAlreadyExposedError(
129
+ errorResponse as unknown as ErrorResponse<PortAlreadyExposedContext>
130
+ );
131
+
132
+ case ErrorCode.PORT_NOT_EXPOSED:
133
+ return new PortNotExposedError(
134
+ errorResponse as unknown as ErrorResponse<PortNotExposedContext>
135
+ );
136
+
137
+ case ErrorCode.INVALID_PORT_NUMBER:
138
+ case ErrorCode.INVALID_PORT:
139
+ return new InvalidPortError(
140
+ errorResponse as unknown as ErrorResponse<InvalidPortContext>
141
+ );
142
+
143
+ case ErrorCode.SERVICE_NOT_RESPONDING:
144
+ return new ServiceNotRespondingError(
145
+ errorResponse as unknown as ErrorResponse<PortErrorContext>
146
+ );
147
+
148
+ case ErrorCode.PORT_IN_USE:
149
+ return new PortInUseError(
150
+ errorResponse as unknown as ErrorResponse<PortErrorContext>
151
+ );
152
+
153
+ case ErrorCode.PORT_OPERATION_ERROR:
154
+ return new PortError(
155
+ errorResponse as unknown as ErrorResponse<PortErrorContext>
156
+ );
157
+
158
+ case ErrorCode.CUSTOM_DOMAIN_REQUIRED:
159
+ return new CustomDomainRequiredError(
160
+ errorResponse as unknown as ErrorResponse<InternalErrorContext>
161
+ );
162
+
163
+ // Git Errors
164
+ case ErrorCode.GIT_REPOSITORY_NOT_FOUND:
165
+ return new GitRepositoryNotFoundError(
166
+ errorResponse as unknown as ErrorResponse<GitRepositoryNotFoundContext>
167
+ );
168
+
169
+ case ErrorCode.GIT_AUTH_FAILED:
170
+ return new GitAuthenticationError(
171
+ errorResponse as unknown as ErrorResponse<GitAuthFailedContext>
172
+ );
173
+
174
+ case ErrorCode.GIT_BRANCH_NOT_FOUND:
175
+ return new GitBranchNotFoundError(
176
+ errorResponse as unknown as ErrorResponse<GitBranchNotFoundContext>
177
+ );
178
+
179
+ case ErrorCode.GIT_NETWORK_ERROR:
180
+ return new GitNetworkError(
181
+ errorResponse as unknown as ErrorResponse<GitErrorContext>
182
+ );
183
+
184
+ case ErrorCode.GIT_CLONE_FAILED:
185
+ return new GitCloneError(
186
+ errorResponse as unknown as ErrorResponse<GitErrorContext>
187
+ );
188
+
189
+ case ErrorCode.GIT_CHECKOUT_FAILED:
190
+ return new GitCheckoutError(
191
+ errorResponse as unknown as ErrorResponse<GitErrorContext>
192
+ );
193
+
194
+ case ErrorCode.INVALID_GIT_URL:
195
+ return new InvalidGitUrlError(
196
+ errorResponse as unknown as ErrorResponse<ValidationFailedContext>
197
+ );
198
+
199
+ case ErrorCode.GIT_OPERATION_FAILED:
200
+ return new GitError(
201
+ errorResponse as unknown as ErrorResponse<GitErrorContext>
202
+ );
203
+
204
+ // Code Interpreter Errors
205
+ case ErrorCode.INTERPRETER_NOT_READY:
206
+ return new InterpreterNotReadyError(
207
+ errorResponse as unknown as ErrorResponse<InterpreterNotReadyContext>
208
+ );
209
+
210
+ case ErrorCode.CONTEXT_NOT_FOUND:
211
+ return new ContextNotFoundError(
212
+ errorResponse as unknown as ErrorResponse<ContextNotFoundContext>
213
+ );
214
+
215
+ case ErrorCode.CODE_EXECUTION_ERROR:
216
+ return new CodeExecutionError(
217
+ errorResponse as unknown as ErrorResponse<CodeExecutionContext>
218
+ );
219
+
220
+ // Validation Errors
221
+ case ErrorCode.VALIDATION_FAILED:
222
+ return new ValidationFailedError(
223
+ errorResponse as unknown as ErrorResponse<ValidationFailedContext>
224
+ );
225
+
226
+ // Generic Errors
227
+ case ErrorCode.INVALID_JSON_RESPONSE:
228
+ case ErrorCode.UNKNOWN_ERROR:
229
+ case ErrorCode.INTERNAL_ERROR:
230
+ return new SandboxError(
231
+ errorResponse as unknown as ErrorResponse<InternalErrorContext>
232
+ );
233
+
234
+ default:
235
+ // Fallback for unknown error codes
236
+ return new SandboxError(errorResponse);
237
+ }
238
+ }