@fastgpt-sdk/sandbox-adapter 0.0.3 → 0.0.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 +11 -146
- package/dist/adapters/BaseSandboxAdapter.d.ts +0 -3
- package/dist/adapters/MinimalProviderAdapter.d.ts +3 -9
- package/dist/adapters/OpenSandboxAdapter.d.ts +6 -159
- package/dist/adapters/SealosDevboxAdapter/api.d.ts +29 -0
- package/dist/adapters/SealosDevboxAdapter/index.d.ts +33 -0
- package/dist/adapters/SealosDevboxAdapter/type.d.ts +107 -0
- package/dist/adapters/index.d.ts +4 -4
- package/dist/index.js +241 -173
- package/dist/interfaces/ISandbox.d.ts +0 -5
- package/dist/interfaces/ISandboxLifecycle.d.ts +0 -10
- package/dist/types/sandbox.d.ts +1 -1
- package/package.json +2 -3
- package/dist/adapters/FastGPTSandboxAdapter/index.d.ts +0 -85
package/README.md
CHANGED
|
@@ -4,160 +4,25 @@ A unified, high-level abstraction layer for cloud sandbox providers. It offers a
|
|
|
4
4
|
|
|
5
5
|
> This package is ESM-only (`"type": "module"`) and requires Node.js **>= 20**.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 安装
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
pnpm add @fastgpt/sandbox
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## 用途
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
### 1. 操作沙盒
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
1. 执行命令:执行命令并返回结果
|
|
18
|
+
1. Create 接口:成功返回,则认为沙盒已创建成功,可以执行命令。
|
|
19
|
+
2. 执行
|
|
20
|
+
2. 下载文件。
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
// 1. Create a sandbox with the OpenSandbox provider
|
|
22
|
-
const sandbox = createSandbox({
|
|
23
|
-
provider: 'opensandbox',
|
|
24
|
-
connection: {
|
|
25
|
-
apiKey: process.env.OPEN_SANDBOX_API_KEY,
|
|
26
|
-
baseUrl: 'http://127.0.0.1:8080', // Your OpenSandbox server
|
|
27
|
-
runtime: 'kubernetes',
|
|
28
|
-
},
|
|
29
|
-
});
|
|
22
|
+
### 2. 管理沙盒
|
|
30
23
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// 2. Create the sandbox instance with a specific image
|
|
34
|
-
await sandbox.create({
|
|
35
|
-
image: { repository: 'nginx', tag: 'latest' },
|
|
36
|
-
timeout: 3600, // Expiration in seconds
|
|
37
|
-
});
|
|
38
|
-
console.log(`Sandbox created: ${sandbox.id}`);
|
|
24
|
+
1. 定期暂停:每 n 分钟不活跃则暂停
|
|
25
|
+
2. 定期销毁:每 n 分钟不活跃则销毁
|
|
39
26
|
|
|
40
|
-
// 3. Wait until the sandbox is fully ready
|
|
41
|
-
await sandbox.waitUntilReady(60000); // 60-second timeout
|
|
42
|
-
console.log('Sandbox is ready.');
|
|
43
27
|
|
|
44
|
-
|
|
45
|
-
const version = await sandbox.execute('nginx -v');
|
|
46
|
-
console.log(`Nginx version: ${version.stdout || version.stderr}`);
|
|
47
|
-
|
|
48
|
-
// 5. Execute a command with streaming output
|
|
49
|
-
console.log('--- Streaming Execution ---');
|
|
50
|
-
await sandbox.executeStream('for i in 1 2 3; do echo "Line $i"; sleep 0.5; done', {
|
|
51
|
-
onStdout: (msg) => console.log(` [stdout] ${msg.text}`),
|
|
52
|
-
onStderr: (msg) => console.log(` [stderr] ${msg.text}`),
|
|
53
|
-
onComplete: (result) => console.log(` [done] Exit code: ${result.exitCode}`),
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// 6. Work with the filesystem
|
|
57
|
-
console.log('\n--- Filesystem Operations ---');
|
|
58
|
-
// Write a file
|
|
59
|
-
await sandbox.writeFiles([
|
|
60
|
-
{
|
|
61
|
-
path: '/app/hello.js',
|
|
62
|
-
data: `console.log('Hello from sandbox!');`,
|
|
63
|
-
},
|
|
64
|
-
]);
|
|
65
|
-
console.log('Written /app/hello.js');
|
|
66
|
-
|
|
67
|
-
// Read the file back
|
|
68
|
-
const [file] = await sandbox.readFiles(['/app/hello.js']);
|
|
69
|
-
if (file && !file.error) {
|
|
70
|
-
const content = new TextDecoder().decode(file.content);
|
|
71
|
-
console.log(`Read content: "${content}"`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// List directory
|
|
75
|
-
const entries = await sandbox.listDirectory('/app');
|
|
76
|
-
console.log('Directory listing for /app:', entries.map(e => e.name));
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// 7. Stop and delete the sandbox
|
|
80
|
-
console.log('\n--- Cleanup ---');
|
|
81
|
-
await sandbox.stop();
|
|
82
|
-
console.log('Sandbox stopped.');
|
|
83
|
-
|
|
84
|
-
if (sandbox.runtime !== 'kubernetes') {
|
|
85
|
-
await sandbox.delete();
|
|
86
|
-
console.log('Sandbox deleted.');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
} catch (error) {
|
|
90
|
-
console.error('An error occurred:', error);
|
|
91
|
-
} finally {
|
|
92
|
-
// 8. Close the connection
|
|
93
|
-
await sandbox.close();
|
|
94
|
-
console.log('Connection closed.');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
main();
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## API (`ISandbox`)
|
|
102
|
-
|
|
103
|
-
The `createSandbox(options)` function returns an instance that implements the `ISandbox` interface.
|
|
104
|
-
|
|
105
|
-
### Lifecycle Management
|
|
106
|
-
|
|
107
|
-
- **`create(options)`**: Creates a new sandbox instance.
|
|
108
|
-
- **`getInfo()`**: Retrieves detailed information about the sandbox.
|
|
109
|
-
- **`waitUntilReady(timeout)`**: Waits for the sandbox to become fully operational.
|
|
110
|
-
- **`renewExpiration(seconds)`**: Extends the sandbox's lifetime.
|
|
111
|
-
- **`pause()` / `resume()`**: Pauses and resumes a running sandbox (if supported).
|
|
112
|
-
- **`stop()`**: Stops the sandbox gracefully.
|
|
113
|
-
- **`delete()`**: Deletes the sandbox instance.
|
|
114
|
-
- **`close()`**: Closes the connection to the provider.
|
|
115
|
-
|
|
116
|
-
### Command Execution
|
|
117
|
-
|
|
118
|
-
- **`execute(command)`**: Executes a command and returns the result after completion.
|
|
119
|
-
- **`executeStream(command, handlers)`**: Executes a command and streams `stdout` and `stderr` in real-time.
|
|
120
|
-
- **`executeBackground(command)`**: Starts a command in the background and returns a session handle.
|
|
121
|
-
|
|
122
|
-
### Filesystem Operations
|
|
123
|
-
|
|
124
|
-
- **`writeFiles(files)`**: Writes one or more files to the sandbox.
|
|
125
|
-
- **`readFiles(paths)`**: Reads one or more files from the sandbox.
|
|
126
|
-
- **`listDirectory(path)`**: Lists the contents of a directory.
|
|
127
|
-
- **`createDirectories(paths)`**: Creates directories.
|
|
128
|
-
- **`deleteFiles(paths)`**: Deletes files.
|
|
129
|
-
- **`moveFiles(files)`**: Moves or renames files.
|
|
130
|
-
|
|
131
|
-
### Health and Metrics
|
|
132
|
-
|
|
133
|
-
- **`ping()`**: Performs a quick health check.
|
|
134
|
-
- **`getMetrics()`**: Retrieves CPU and memory usage statistics.
|
|
135
|
-
|
|
136
|
-
## Polyfill Behavior
|
|
137
|
-
|
|
138
|
-
The SDK uses `CommandPolyfillService` to provide filesystem, search, health, and metrics
|
|
139
|
-
operations by running shell commands inside the sandbox. Providers can override these
|
|
140
|
-
methods if they have native APIs, but the default behavior is consistent across adapters.
|
|
141
|
-
|
|
142
|
-
## Error Handling
|
|
143
|
-
|
|
144
|
-
The SDK exports specific error types to facilitate robust error handling:
|
|
145
|
-
|
|
146
|
-
- `SandboxException`
|
|
147
|
-
- `FeatureNotSupportedError`
|
|
148
|
-
- `FileOperationError`
|
|
149
|
-
- `CommandExecutionError`
|
|
150
|
-
- `TimeoutError`
|
|
151
|
-
|
|
152
|
-
Example:
|
|
153
|
-
```ts
|
|
154
|
-
import { FileOperationError } from '@fastgpt/sandbox';
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
await sandbox.readFiles(['/nonexistent-file']);
|
|
158
|
-
} catch (error) {
|
|
159
|
-
if (error instanceof FileOperationError) {
|
|
160
|
-
console.error(`File operation failed: ${error.message}`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
```
|
|
28
|
+
## 添加新适配器
|
|
@@ -18,11 +18,8 @@ export declare abstract class BaseSandboxAdapter implements ISandbox {
|
|
|
18
18
|
abstract create(config: SandboxConfig): Promise<void>;
|
|
19
19
|
abstract start(): Promise<void>;
|
|
20
20
|
abstract stop(): Promise<void>;
|
|
21
|
-
abstract pause(): Promise<void>;
|
|
22
|
-
abstract resume(): Promise<void>;
|
|
23
21
|
abstract delete(): Promise<void>;
|
|
24
22
|
abstract getInfo(): Promise<SandboxInfo | null>;
|
|
25
|
-
abstract close(): Promise<void>;
|
|
26
23
|
waitUntilReady(timeoutMs?: number): Promise<void>;
|
|
27
24
|
renewExpiration(_additionalSeconds: number): Promise<void>;
|
|
28
25
|
abstract execute(command: string, options?: ExecuteOptions): Promise<ExecuteResult>;
|
|
@@ -15,7 +15,6 @@ export interface MinimalProviderConnection {
|
|
|
15
15
|
}>;
|
|
16
16
|
/** Get current status */
|
|
17
17
|
getStatus(): Promise<SandboxStatus>;
|
|
18
|
-
/** Close the connection */
|
|
19
18
|
close(): Promise<void>;
|
|
20
19
|
}
|
|
21
20
|
export type MinimalProviderConfig = {
|
|
@@ -24,9 +23,8 @@ export type MinimalProviderConfig = {
|
|
|
24
23
|
/**
|
|
25
24
|
* Minimal provider adapter.
|
|
26
25
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* the CommandPolyfillService.
|
|
26
|
+
* Adapts a provider with minimal capabilities (only command execution)
|
|
27
|
+
* to the full ISandbox interface using the CommandPolyfillService.
|
|
30
28
|
*
|
|
31
29
|
* Use case: Legacy SSH-based sandboxes, custom container providers,
|
|
32
30
|
* or any provider that only exposes a shell interface.
|
|
@@ -38,15 +36,11 @@ export declare class MinimalProviderAdapter extends BaseSandboxAdapter {
|
|
|
38
36
|
private connection?;
|
|
39
37
|
constructor(config?: MinimalProviderConfig | undefined);
|
|
40
38
|
get id(): SandboxId;
|
|
41
|
-
get status(): SandboxStatus;
|
|
42
39
|
create(config: SandboxConfig): Promise<void>;
|
|
43
40
|
connect(connection: MinimalProviderConnection): Promise<void>;
|
|
44
41
|
start(): Promise<void>;
|
|
45
42
|
stop(): Promise<void>;
|
|
46
|
-
pause(): Promise<void>;
|
|
47
|
-
resume(): Promise<void>;
|
|
48
43
|
delete(): Promise<void>;
|
|
49
|
-
getInfo(): Promise<SandboxInfo>;
|
|
50
|
-
close(): Promise<void>;
|
|
44
|
+
getInfo(): Promise<SandboxInfo | null>;
|
|
51
45
|
execute(command: string, options?: ExecuteOptions): Promise<ExecuteResult>;
|
|
52
46
|
}
|
|
@@ -3,21 +3,19 @@ import { BaseSandboxAdapter } from './BaseSandboxAdapter';
|
|
|
3
3
|
/**
|
|
4
4
|
* Sandbox runtime type.
|
|
5
5
|
* - docker: Full-featured runtime with pause/resume support
|
|
6
|
-
* - kubernetes: Container orchestration runtime
|
|
6
|
+
* - kubernetes: Container orchestration runtime
|
|
7
7
|
*/
|
|
8
8
|
export type SandboxRuntimeType = 'docker' | 'kubernetes';
|
|
9
9
|
/**
|
|
10
10
|
* Connection configuration options for OpenSandboxAdapter.
|
|
11
11
|
*/
|
|
12
12
|
export interface OpenSandboxConnectionConfig {
|
|
13
|
-
/** Base URL for the OpenSandbox API
|
|
13
|
+
/** Base URL for the OpenSandbox API */
|
|
14
14
|
baseUrl?: string;
|
|
15
15
|
/** API key for authentication */
|
|
16
16
|
apiKey?: string;
|
|
17
17
|
/**
|
|
18
18
|
* Sandbox runtime type.
|
|
19
|
-
* - docker: Full-featured with pause/resume support
|
|
20
|
-
* - kubernetes: No pause/resume, stop operation deletes the sandbox
|
|
21
19
|
* @default 'docker'
|
|
22
20
|
*/
|
|
23
21
|
runtime?: SandboxRuntimeType;
|
|
@@ -25,9 +23,7 @@ export interface OpenSandboxConnectionConfig {
|
|
|
25
23
|
/**
|
|
26
24
|
* OpenSandbox provider adapter.
|
|
27
25
|
*
|
|
28
|
-
*
|
|
29
|
-
* support for all features. Uses the OpenSandbox TypeScript SDK
|
|
30
|
-
* for all operations.
|
|
26
|
+
* Full native support for all features via the OpenSandbox TypeScript SDK.
|
|
31
27
|
*
|
|
32
28
|
* @example
|
|
33
29
|
* ```typescript
|
|
@@ -46,184 +42,35 @@ export interface OpenSandboxConnectionConfig {
|
|
|
46
42
|
*/
|
|
47
43
|
export declare class OpenSandboxAdapter extends BaseSandboxAdapter {
|
|
48
44
|
private connectionConfig;
|
|
49
|
-
/** Provider identifier */
|
|
50
45
|
readonly provider: "opensandbox";
|
|
51
|
-
/** Runtime type for this adapter instance */
|
|
52
46
|
readonly runtime: SandboxRuntimeType;
|
|
53
|
-
/** Internal SDK sandbox instance */
|
|
54
47
|
private _sandbox?;
|
|
55
|
-
/** SDK connection configuration */
|
|
56
48
|
private _connection;
|
|
57
|
-
/** Cached sandbox ID */
|
|
58
49
|
private _id;
|
|
59
|
-
/** Current adapter state */
|
|
60
|
-
private _connectionState;
|
|
61
|
-
/**
|
|
62
|
-
* Creates a new OpenSandboxAdapter instance.
|
|
63
|
-
*
|
|
64
|
-
* @param connectionConfig - Connection configuration options
|
|
65
|
-
*/
|
|
66
50
|
constructor(connectionConfig?: OpenSandboxConnectionConfig);
|
|
67
|
-
/**
|
|
68
|
-
* Get the sandbox ID. Returns empty string if not created/connected.
|
|
69
|
-
*/
|
|
70
51
|
get id(): SandboxId;
|
|
71
|
-
/**
|
|
72
|
-
* Get the current connection state.
|
|
73
|
-
*/
|
|
74
|
-
get connectionState(): typeof this._connectionState;
|
|
75
|
-
/**
|
|
76
|
-
* Get the underlying SDK sandbox instance.
|
|
77
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
78
|
-
*/
|
|
79
52
|
private get sandbox();
|
|
80
|
-
/**
|
|
81
|
-
* Create ConnectionConfig from adapter's connection options.
|
|
82
|
-
* Handles URL parsing with fallback to domain string.
|
|
83
|
-
*/
|
|
84
53
|
private createConnectionConfig;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
* Format: repository[:tag][@digest]
|
|
88
|
-
*/
|
|
54
|
+
private static readonly STATE_MAP;
|
|
55
|
+
private mapStatus;
|
|
89
56
|
private convertImageSpec;
|
|
90
|
-
/**
|
|
91
|
-
* Parse SDK image string into ImageSpec.
|
|
92
|
-
* Handles formats: repository, repository:tag, repository@digest
|
|
93
|
-
*/
|
|
94
57
|
private parseImageSpec;
|
|
95
|
-
/**
|
|
96
|
-
* Convert ResourceLimits to SDK resource format.
|
|
97
|
-
* Maps cpuCount -> cpu, memoryMiB -> memory, diskGiB -> disk
|
|
98
|
-
*/
|
|
99
58
|
private convertResourceLimits;
|
|
100
|
-
/**
|
|
101
|
-
* Parse SDK resource limits (Record<string, string>) to ResourceLimits.
|
|
102
|
-
* Handles memory format: 512Mi, 2Gi
|
|
103
|
-
*/
|
|
104
59
|
private parseResourceLimits;
|
|
105
|
-
/**
|
|
106
|
-
* Create a new sandbox with the given configuration.
|
|
107
|
-
*
|
|
108
|
-
* @param config - Sandbox configuration
|
|
109
|
-
* @throws {ConnectionError} If connection to the API fails
|
|
110
|
-
* @throws {CommandExecutionError} If sandbox creation fails
|
|
111
|
-
*/
|
|
112
60
|
create(config: SandboxConfig): Promise<void>;
|
|
113
|
-
/**
|
|
114
|
-
* Connect to an existing OpenSandbox instance.
|
|
115
|
-
*
|
|
116
|
-
* @param sandboxId - The ID of the sandbox to connect to
|
|
117
|
-
* @throws {ConnectionError} If connection fails or sandbox not found
|
|
118
|
-
*/
|
|
119
61
|
connect(sandboxId: string): Promise<void>;
|
|
120
|
-
/**
|
|
121
|
-
* Start a stopped or paused sandbox.
|
|
122
|
-
* For OpenSandbox, this resumes from paused state if applicable.
|
|
123
|
-
*
|
|
124
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
125
|
-
*/
|
|
126
62
|
start(): Promise<void>;
|
|
127
|
-
/**
|
|
128
|
-
* Stop the sandbox (graceful shutdown).
|
|
129
|
-
*
|
|
130
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
131
|
-
*/
|
|
132
63
|
stop(): Promise<void>;
|
|
133
|
-
/**
|
|
134
|
-
* Pause a running sandbox.
|
|
135
|
-
*
|
|
136
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
137
|
-
* @throws {FeatureNotSupportedError} If pause is not supported by the runtime
|
|
138
|
-
* @throws {CommandExecutionError} If pause fails
|
|
139
|
-
*/
|
|
140
|
-
pause(): Promise<void>;
|
|
141
|
-
/**
|
|
142
|
-
* Resume a paused sandbox.
|
|
143
|
-
*
|
|
144
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
145
|
-
* @throws {FeatureNotSupportedError} If resume is not supported by the runtime
|
|
146
|
-
* @throws {CommandExecutionError} If resume fails
|
|
147
|
-
*/
|
|
148
|
-
resume(): Promise<void>;
|
|
149
|
-
/**
|
|
150
|
-
* Delete the sandbox permanently.
|
|
151
|
-
*
|
|
152
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
153
|
-
* @throws {CommandExecutionError} If deletion fails
|
|
154
|
-
*/
|
|
155
64
|
delete(): Promise<void>;
|
|
156
|
-
|
|
157
|
-
* Get detailed information about the sandbox.
|
|
158
|
-
*
|
|
159
|
-
* @returns Sandbox information
|
|
160
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
161
|
-
*/
|
|
162
|
-
getInfo(): Promise<SandboxInfo>;
|
|
163
|
-
/**
|
|
164
|
-
* Close the connection and release resources.
|
|
165
|
-
*/
|
|
166
|
-
close(): Promise<void>;
|
|
167
|
-
/**
|
|
168
|
-
* Renew the sandbox expiration.
|
|
169
|
-
*
|
|
170
|
-
* @param additionalSeconds - Seconds to extend the expiration by
|
|
171
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
172
|
-
* @throws {CommandExecutionError} If renewal fails
|
|
173
|
-
*/
|
|
65
|
+
getInfo(): Promise<SandboxInfo | null>;
|
|
174
66
|
renewExpiration(additionalSeconds: number): Promise<void>;
|
|
175
|
-
/**
|
|
176
|
-
* Execute a command and wait for completion.
|
|
177
|
-
*
|
|
178
|
-
* @param command - The command to execute
|
|
179
|
-
* @param options - Execution options
|
|
180
|
-
* @returns Execution result with stdout, stderr, and exit code
|
|
181
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
182
|
-
* @throws {CommandExecutionError} If execution fails
|
|
183
|
-
*/
|
|
184
67
|
execute(command: string, options?: ExecuteOptions): Promise<ExecuteResult>;
|
|
185
|
-
/**
|
|
186
|
-
* Execute a command with streaming output.
|
|
187
|
-
*
|
|
188
|
-
* @param command - The command to execute
|
|
189
|
-
* @param handlers - Stream handlers for output
|
|
190
|
-
* @param options - Execution options
|
|
191
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
192
|
-
* @throws {CommandExecutionError} If execution fails
|
|
193
|
-
*/
|
|
194
68
|
executeStream(command: string, handlers: StreamHandlers, options?: ExecuteOptions): Promise<void>;
|
|
195
|
-
/**
|
|
196
|
-
* Execute a command in the background.
|
|
197
|
-
*
|
|
198
|
-
* @param command - The command to execute
|
|
199
|
-
* @param options - Execution options
|
|
200
|
-
* @returns Handle with sessionId and kill function
|
|
201
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
202
|
-
* @throws {CommandExecutionError} If execution fails
|
|
203
|
-
*/
|
|
204
69
|
executeBackground(command: string, options?: ExecuteOptions): Promise<{
|
|
205
70
|
sessionId: string;
|
|
206
71
|
kill(): Promise<void>;
|
|
207
72
|
}>;
|
|
208
|
-
/**
|
|
209
|
-
* Interrupt/kill a running command session.
|
|
210
|
-
*
|
|
211
|
-
* @param sessionId - The session ID from executeBackground
|
|
212
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
213
|
-
* @throws {CommandExecutionError} If interruption fails
|
|
214
|
-
*/
|
|
215
73
|
interrupt(sessionId: string): Promise<void>;
|
|
216
|
-
/**
|
|
217
|
-
* Check if the sandbox is healthy.
|
|
218
|
-
*
|
|
219
|
-
* @returns true if healthy, false otherwise
|
|
220
|
-
*/
|
|
221
74
|
ping(): Promise<boolean>;
|
|
222
|
-
/**
|
|
223
|
-
* Get current resource metrics.
|
|
224
|
-
*
|
|
225
|
-
* @returns Current metrics
|
|
226
|
-
* @throws {SandboxStateError} If sandbox is not initialized
|
|
227
|
-
*/
|
|
228
75
|
getMetrics(): Promise<SandboxMetrics>;
|
|
229
76
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type DevboxApiConfig, type DevboxApiResponse, type DevboxInfoData, type DevboxMutationData, type DownloadFileParams, type ExecRequest, type ExecResponseData, type UploadFileParams, type UploadResponseData } from './type';
|
|
2
|
+
/**
|
|
3
|
+
* HTTP client for the Sealos Devbox REST API.
|
|
4
|
+
*
|
|
5
|
+
* @see https://devbox-server.staging-usw-1.sealos.io
|
|
6
|
+
*/
|
|
7
|
+
export declare class DevboxApi {
|
|
8
|
+
private baseUrl;
|
|
9
|
+
private token;
|
|
10
|
+
constructor(config: DevboxApiConfig);
|
|
11
|
+
private url;
|
|
12
|
+
private request;
|
|
13
|
+
/** POST /api/v1/devbox — create a devbox */
|
|
14
|
+
create(name: string): Promise<DevboxApiResponse<DevboxMutationData>>;
|
|
15
|
+
/** GET /api/v1/devbox/{name} — query devbox info (state + SSH) */
|
|
16
|
+
info(name: string): Promise<DevboxApiResponse<DevboxInfoData>>;
|
|
17
|
+
/** POST /api/v1/devbox/{name}/pause */
|
|
18
|
+
pause(name: string): Promise<DevboxApiResponse<DevboxMutationData>>;
|
|
19
|
+
/** POST /api/v1/devbox/{name}/resume */
|
|
20
|
+
resume(name: string): Promise<DevboxApiResponse<DevboxMutationData>>;
|
|
21
|
+
/** DELETE /api/v1/devbox/{name} */
|
|
22
|
+
delete(name: string): Promise<DevboxApiResponse<DevboxMutationData>>;
|
|
23
|
+
/** POST /api/v1/devbox/{name}/exec */
|
|
24
|
+
exec(name: string, req: ExecRequest): Promise<DevboxApiResponse<ExecResponseData>>;
|
|
25
|
+
/** POST /api/v1/devbox/{name}/files/upload */
|
|
26
|
+
uploadFile(name: string, params: UploadFileParams, content: BodyInit): Promise<DevboxApiResponse<UploadResponseData>>;
|
|
27
|
+
/** GET /api/v1/devbox/{name}/files/download */
|
|
28
|
+
downloadFile(name: string, params: DownloadFileParams): Promise<ArrayBuffer>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ExecuteOptions, ExecuteResult, SandboxConfig, SandboxId, SandboxInfo } from '../../types';
|
|
2
|
+
import { BaseSandboxAdapter } from '../BaseSandboxAdapter';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for Sealos Devbox Adapter.
|
|
5
|
+
*/
|
|
6
|
+
export interface SealosDevboxConfig {
|
|
7
|
+
/** Base URL for the Sealos Devbox Server API */
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
/** JWT authentication token */
|
|
10
|
+
token: string;
|
|
11
|
+
sandboxId: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
14
|
+
private config;
|
|
15
|
+
readonly provider: "sealos-devbox";
|
|
16
|
+
private api;
|
|
17
|
+
private _id;
|
|
18
|
+
constructor(config: SealosDevboxConfig);
|
|
19
|
+
get id(): SandboxId;
|
|
20
|
+
private StatusAdapt;
|
|
21
|
+
private waitUntilDeleted;
|
|
22
|
+
getInfo(): Promise<SandboxInfo | null>;
|
|
23
|
+
create(_config: SandboxConfig): Promise<void>;
|
|
24
|
+
stop(): Promise<void>;
|
|
25
|
+
start(): Promise<void>;
|
|
26
|
+
delete(): Promise<void>;
|
|
27
|
+
execute(command: string, options?: ExecuteOptions): Promise<ExecuteResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Check if the devbox is ready by querying info endpoint.
|
|
30
|
+
* Ready when spec, status, and phase are all "Running".
|
|
31
|
+
*/
|
|
32
|
+
ping(): Promise<boolean>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Devbox phases representing various lifecycle states.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum DevboxPhaseEnum {
|
|
5
|
+
Running = "Running",
|
|
6
|
+
Pending = "Pending",
|
|
7
|
+
Paused = "Paused",
|
|
8
|
+
Pausing = "Pausing",
|
|
9
|
+
Stopped = "Stopped",
|
|
10
|
+
Stopping = "Stopping",
|
|
11
|
+
Shutdown = "Shutdown",
|
|
12
|
+
Shutting = "Shutting",
|
|
13
|
+
Error = "Error",
|
|
14
|
+
Unknown = "Unknown"
|
|
15
|
+
}
|
|
16
|
+
/** Configuration for the Devbox REST API client. */
|
|
17
|
+
export interface DevboxApiConfig {
|
|
18
|
+
/** Base URL of the devbox server */
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
/** JWT token for authentication */
|
|
21
|
+
token: string;
|
|
22
|
+
}
|
|
23
|
+
/** Unified API response envelope. */
|
|
24
|
+
export interface DevboxApiResponse<T> {
|
|
25
|
+
code: number;
|
|
26
|
+
message: string;
|
|
27
|
+
data: T;
|
|
28
|
+
}
|
|
29
|
+
/** Request body for the exec endpoint. */
|
|
30
|
+
export interface ExecRequest {
|
|
31
|
+
command: string[];
|
|
32
|
+
/** Optional stdin content */
|
|
33
|
+
stdin?: string;
|
|
34
|
+
/** Timeout in seconds [1, 600], default 30 */
|
|
35
|
+
timeoutSeconds?: number;
|
|
36
|
+
/** Target container name, defaults to first container in Pod */
|
|
37
|
+
container?: string;
|
|
38
|
+
}
|
|
39
|
+
/** Response data from the exec endpoint. */
|
|
40
|
+
export interface ExecResponseData {
|
|
41
|
+
podName: string;
|
|
42
|
+
container: string;
|
|
43
|
+
command: string[];
|
|
44
|
+
exitCode: number;
|
|
45
|
+
stdout: string;
|
|
46
|
+
stderr: string;
|
|
47
|
+
executedAt: string;
|
|
48
|
+
}
|
|
49
|
+
/** Response data from create/pause/resume/delete endpoints. */
|
|
50
|
+
export interface DevboxMutationData {
|
|
51
|
+
name: string;
|
|
52
|
+
state?: string;
|
|
53
|
+
status?: string;
|
|
54
|
+
}
|
|
55
|
+
/** SSH connection info returned by the info endpoint. */
|
|
56
|
+
export interface DevboxSshInfo {
|
|
57
|
+
user: string;
|
|
58
|
+
host: string;
|
|
59
|
+
port: number;
|
|
60
|
+
target: string;
|
|
61
|
+
link: string;
|
|
62
|
+
command: string;
|
|
63
|
+
privateKeyEncoding: string;
|
|
64
|
+
privateKeyBase64: string;
|
|
65
|
+
}
|
|
66
|
+
/** Response data from the GET info endpoint. */
|
|
67
|
+
export interface DevboxInfoData {
|
|
68
|
+
name: string;
|
|
69
|
+
deletionTimestamp?: string | null;
|
|
70
|
+
state: {
|
|
71
|
+
phase: `${DevboxPhaseEnum}`;
|
|
72
|
+
};
|
|
73
|
+
ssh: DevboxSshInfo;
|
|
74
|
+
}
|
|
75
|
+
/** Response data from the upload endpoint. */
|
|
76
|
+
export interface UploadResponseData {
|
|
77
|
+
name: string;
|
|
78
|
+
podName: string;
|
|
79
|
+
container: string;
|
|
80
|
+
path: string;
|
|
81
|
+
sizeBytes: number;
|
|
82
|
+
mode: string;
|
|
83
|
+
uploadedAt: string;
|
|
84
|
+
timeoutSecond: number;
|
|
85
|
+
}
|
|
86
|
+
/** Query parameters for file upload. */
|
|
87
|
+
export interface UploadFileParams {
|
|
88
|
+
/** Remote path inside the container */
|
|
89
|
+
path: string;
|
|
90
|
+
/** File permission, octal string e.g. "0644" */
|
|
91
|
+
mode?: string;
|
|
92
|
+
/** Timeout in seconds [1, 3600], default 300 */
|
|
93
|
+
timeoutSeconds?: number;
|
|
94
|
+
/** Target container name */
|
|
95
|
+
container?: string;
|
|
96
|
+
}
|
|
97
|
+
/** Query parameters for file download. */
|
|
98
|
+
export interface DownloadFileParams {
|
|
99
|
+
/** Remote file path inside the container */
|
|
100
|
+
path: string;
|
|
101
|
+
/** Suggested filename for the response header */
|
|
102
|
+
filename?: string;
|
|
103
|
+
/** Timeout in seconds [1, 3600], default 300 */
|
|
104
|
+
timeoutSeconds?: number;
|
|
105
|
+
/** Target container name */
|
|
106
|
+
container?: string;
|
|
107
|
+
}
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ISandbox } from '../interfaces';
|
|
2
|
-
import { type
|
|
2
|
+
import { type SealosDevboxConfig } from './SealosDevboxAdapter';
|
|
3
3
|
import { type MinimalProviderConfig } from './MinimalProviderAdapter';
|
|
4
4
|
import { type OpenSandboxConnectionConfig } from './OpenSandboxAdapter';
|
|
5
5
|
export { BaseSandboxAdapter } from './BaseSandboxAdapter';
|
|
6
|
-
export {
|
|
6
|
+
export { SealosDevboxAdapter, type SealosDevboxConfig } from './SealosDevboxAdapter';
|
|
7
7
|
export { MinimalProviderAdapter, type MinimalProviderConfig, type MinimalProviderConnection } from './MinimalProviderAdapter';
|
|
8
8
|
export { OpenSandboxAdapter, type OpenSandboxConnectionConfig, type SandboxRuntimeType } from './OpenSandboxAdapter';
|
|
9
9
|
type CreateProviderType = {
|
|
@@ -13,8 +13,8 @@ type CreateProviderType = {
|
|
|
13
13
|
provider: 'minimal';
|
|
14
14
|
config: MinimalProviderConfig;
|
|
15
15
|
} | {
|
|
16
|
-
provider: '
|
|
17
|
-
config:
|
|
16
|
+
provider: 'sealos-devbox';
|
|
17
|
+
config: SealosDevboxConfig;
|
|
18
18
|
};
|
|
19
19
|
/**
|
|
20
20
|
* Create a sandbox provider instance.
|