@fastgpt-sdk/sandbox-adapter 0.0.8 → 0.0.10
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/dist/adapters/BaseSandboxAdapter.d.ts +3 -3
- package/dist/adapters/OpenSandboxAdapter/index.d.ts +7 -5
- package/dist/adapters/SealosDevboxAdapter/index.d.ts +1 -1
- package/dist/adapters/index.d.ts +16 -12
- package/dist/index.js +21 -19
- package/dist/interfaces/ISandbox.d.ts +1 -1
- package/dist/interfaces/ISandboxLifecycle.d.ts +3 -3
- package/package.json +1 -1
|
@@ -8,15 +8,15 @@ import type { ContentReplaceEntry, DirectoryEntry, ExecuteOptions, ExecuteResult
|
|
|
8
8
|
* search, health, and metrics operations via CommandPolyfillService.
|
|
9
9
|
* Subclasses can override the polyfill service in their constructor.
|
|
10
10
|
*/
|
|
11
|
-
export declare abstract class BaseSandboxAdapter
|
|
11
|
+
export declare abstract class BaseSandboxAdapter implements ISandbox {
|
|
12
12
|
abstract readonly id: SandboxId;
|
|
13
13
|
abstract readonly provider: string;
|
|
14
14
|
protected _status: SandboxStatus;
|
|
15
15
|
protected polyfillService?: CommandPolyfillService;
|
|
16
16
|
constructor();
|
|
17
17
|
get status(): SandboxStatus;
|
|
18
|
-
abstract ensureRunning(
|
|
19
|
-
abstract create(
|
|
18
|
+
abstract ensureRunning(): Promise<void>;
|
|
19
|
+
abstract create(): Promise<void>;
|
|
20
20
|
abstract start(): Promise<void>;
|
|
21
21
|
abstract stop(): Promise<void>;
|
|
22
22
|
abstract delete(): Promise<void>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ExecuteOptions, ExecuteResult, SandboxId, SandboxInfo, SandboxMetrics, StreamHandlers } from '@/types';
|
|
2
2
|
import { BaseSandboxAdapter } from '../BaseSandboxAdapter';
|
|
3
|
-
import { OpenSandboxConfigType } from './type';
|
|
3
|
+
import type { OpenSandboxConfigType } from './type';
|
|
4
|
+
export type { OpenSandboxConfigType } from './type';
|
|
4
5
|
/**
|
|
5
6
|
* Sandbox runtime type.
|
|
6
7
|
* - docker: Full-featured runtime with pause/resume support
|
|
@@ -41,14 +42,15 @@ export interface OpenSandboxConnectionConfig {
|
|
|
41
42
|
* console.log(result.stdout); // v18.x.x
|
|
42
43
|
* ```
|
|
43
44
|
*/
|
|
44
|
-
export declare class OpenSandboxAdapter extends BaseSandboxAdapter
|
|
45
|
+
export declare class OpenSandboxAdapter extends BaseSandboxAdapter {
|
|
45
46
|
private connectionConfig;
|
|
47
|
+
private createConfig;
|
|
46
48
|
readonly provider: "opensandbox";
|
|
47
49
|
readonly runtime: SandboxRuntimeType;
|
|
48
50
|
private _sandbox?;
|
|
49
51
|
private _connection;
|
|
50
52
|
private _id;
|
|
51
|
-
constructor(connectionConfig
|
|
53
|
+
constructor(connectionConfig: OpenSandboxConnectionConfig | undefined, createConfig: OpenSandboxConfigType);
|
|
52
54
|
get id(): SandboxId;
|
|
53
55
|
private get sandbox();
|
|
54
56
|
private createConnectionConfig;
|
|
@@ -58,8 +60,8 @@ export declare class OpenSandboxAdapter extends BaseSandboxAdapter<OpenSandboxCo
|
|
|
58
60
|
private parseImageSpec;
|
|
59
61
|
private convertResourceLimits;
|
|
60
62
|
private parseResourceLimits;
|
|
61
|
-
ensureRunning(
|
|
62
|
-
create(
|
|
63
|
+
ensureRunning(): Promise<void>;
|
|
64
|
+
create(): Promise<void>;
|
|
63
65
|
connect(sandboxId: string): Promise<void>;
|
|
64
66
|
start(): Promise<void>;
|
|
65
67
|
stop(): Promise<void>;
|
|
@@ -12,7 +12,7 @@ export interface SealosDevboxConfig {
|
|
|
12
12
|
}
|
|
13
13
|
export declare class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
14
14
|
private config;
|
|
15
|
-
readonly provider: "
|
|
15
|
+
readonly provider: "sealosdevbox";
|
|
16
16
|
private api;
|
|
17
17
|
private _id;
|
|
18
18
|
constructor(config: SealosDevboxConfig);
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { type SealosDevboxConfig } from './SealosDevboxAdapter';
|
|
2
|
-
import { type OpenSandboxConnectionConfig } from './OpenSandboxAdapter';
|
|
2
|
+
import { type OpenSandboxConnectionConfig, type OpenSandboxConfigType } from './OpenSandboxAdapter';
|
|
3
3
|
import { ISandbox } from '@/interfaces';
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
export type SandboxProviderType = 'opensandbox' | 'sealosdevbox';
|
|
5
|
+
/** Maps each provider name to the ISandbox config type it exposes. */
|
|
6
|
+
interface SandboxConfigMap {
|
|
7
|
+
opensandbox: OpenSandboxConfigType;
|
|
8
|
+
sealosdevbox: undefined;
|
|
9
|
+
}
|
|
10
|
+
/** Resolves the concrete ISandbox type for a given provider. */
|
|
11
|
+
/** Maps each provider name to its constructor (connection) config type. */
|
|
12
|
+
interface SandboxConnectionConfig {
|
|
13
|
+
opensandbox: OpenSandboxConnectionConfig;
|
|
14
|
+
sealosdevbox: SealosDevboxConfig;
|
|
15
|
+
}
|
|
14
16
|
/**
|
|
15
17
|
* Create a sandbox provider instance.
|
|
18
|
+
* The return type is inferred from the provider name.
|
|
16
19
|
*
|
|
17
20
|
* @param config Provider configuration
|
|
18
21
|
* @returns Configured sandbox instance
|
|
19
22
|
* @throws Error if provider type is unknown
|
|
20
23
|
*/
|
|
21
|
-
export declare
|
|
24
|
+
export declare function createSandbox<P extends SandboxProviderType>(provider: P, config: SandboxConnectionConfig[P], createConfig: SandboxConfigMap[P]): ISandbox;
|
|
25
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -716,7 +716,7 @@ class DevboxApi {
|
|
|
716
716
|
// src/adapters/SealosDevboxAdapter/index.ts
|
|
717
717
|
class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
718
718
|
config;
|
|
719
|
-
provider = "
|
|
719
|
+
provider = "sealosdevbox";
|
|
720
720
|
api;
|
|
721
721
|
_id;
|
|
722
722
|
constructor(config) {
|
|
@@ -797,7 +797,7 @@ class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
|
797
797
|
await this.create();
|
|
798
798
|
return;
|
|
799
799
|
default:
|
|
800
|
-
throw new ConnectionError(`Failed to
|
|
800
|
+
throw new ConnectionError(`Failed to ensure sandbox running: ${status}, ${sandbox.status.message}`);
|
|
801
801
|
}
|
|
802
802
|
}
|
|
803
803
|
await this.create();
|
|
@@ -809,6 +809,7 @@ class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
|
809
809
|
try {
|
|
810
810
|
this._status = { state: "Creating" };
|
|
811
811
|
await this.api.create(this._id);
|
|
812
|
+
await this.waitUntilReady();
|
|
812
813
|
this._status = { state: "Running" };
|
|
813
814
|
} catch (error) {
|
|
814
815
|
throw new ConnectionError("Failed to create sandbox", this.config.baseUrl, error);
|
|
@@ -844,8 +845,9 @@ class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
|
844
845
|
}
|
|
845
846
|
}
|
|
846
847
|
async execute(command, options) {
|
|
848
|
+
const cmd = options?.workingDirectory ? ["sh", "-lc", `cd ${options.workingDirectory} && ${command}`] : ["sh", "-lc", command];
|
|
849
|
+
console.log(cmd, 2323);
|
|
847
850
|
try {
|
|
848
|
-
const cmd = options?.workingDirectory ? ["sh", "-lc", `cd ${options.workingDirectory} && ${command}`] : ["sh", "-lc", command];
|
|
849
851
|
const res = await this.api.exec(this._id, {
|
|
850
852
|
command: cmd,
|
|
851
853
|
timeoutSeconds: options?.timeoutMs ? Math.ceil(options.timeoutMs / 1000) : undefined
|
|
@@ -878,14 +880,16 @@ class SealosDevboxAdapter extends BaseSandboxAdapter {
|
|
|
878
880
|
import { ConnectionConfig, Sandbox } from "@alibaba-group/opensandbox";
|
|
879
881
|
class OpenSandboxAdapter extends BaseSandboxAdapter {
|
|
880
882
|
connectionConfig;
|
|
883
|
+
createConfig;
|
|
881
884
|
provider = "opensandbox";
|
|
882
885
|
runtime;
|
|
883
886
|
_sandbox;
|
|
884
887
|
_connection;
|
|
885
888
|
_id = "";
|
|
886
|
-
constructor(connectionConfig = {}) {
|
|
889
|
+
constructor(connectionConfig = {}, createConfig) {
|
|
887
890
|
super();
|
|
888
891
|
this.connectionConfig = connectionConfig;
|
|
892
|
+
this.createConfig = createConfig;
|
|
889
893
|
this.runtime = connectionConfig.runtime ?? "docker";
|
|
890
894
|
this._connection = this.createConnectionConfig();
|
|
891
895
|
}
|
|
@@ -990,22 +994,22 @@ class OpenSandboxAdapter extends BaseSandboxAdapter {
|
|
|
990
994
|
}
|
|
991
995
|
return result;
|
|
992
996
|
}
|
|
993
|
-
async ensureRunning(
|
|
994
|
-
return this.create(
|
|
997
|
+
async ensureRunning() {
|
|
998
|
+
return this.create();
|
|
995
999
|
}
|
|
996
|
-
async create(
|
|
1000
|
+
async create() {
|
|
997
1001
|
try {
|
|
998
1002
|
this._status = { state: "Creating" };
|
|
999
|
-
const image = this.convertImageSpec(
|
|
1000
|
-
const resource = this.convertResourceLimits(
|
|
1003
|
+
const image = this.convertImageSpec(this.createConfig.image);
|
|
1004
|
+
const resource = this.convertResourceLimits(this.createConfig.resourceLimits);
|
|
1001
1005
|
this._sandbox = await Sandbox.create({
|
|
1002
1006
|
connectionConfig: this._connection,
|
|
1003
1007
|
image,
|
|
1004
|
-
entrypoint:
|
|
1005
|
-
timeoutSeconds:
|
|
1008
|
+
entrypoint: this.createConfig.entrypoint,
|
|
1009
|
+
timeoutSeconds: this.createConfig.timeout,
|
|
1006
1010
|
resource,
|
|
1007
|
-
env:
|
|
1008
|
-
metadata:
|
|
1011
|
+
env: this.createConfig.env,
|
|
1012
|
+
metadata: this.createConfig.metadata
|
|
1009
1013
|
});
|
|
1010
1014
|
this._id = this._sandbox.id;
|
|
1011
1015
|
this._status = { state: "Running" };
|
|
@@ -1193,24 +1197,22 @@ class OpenSandboxAdapter extends BaseSandboxAdapter {
|
|
|
1193
1197
|
}
|
|
1194
1198
|
|
|
1195
1199
|
// src/adapters/index.ts
|
|
1196
|
-
|
|
1200
|
+
function createSandbox(provider, config, createConfig) {
|
|
1197
1201
|
switch (provider) {
|
|
1198
1202
|
case "opensandbox":
|
|
1199
|
-
return new OpenSandboxAdapter(config);
|
|
1200
|
-
case "
|
|
1203
|
+
return new OpenSandboxAdapter(config, createConfig);
|
|
1204
|
+
case "sealosdevbox":
|
|
1201
1205
|
return new SealosDevboxAdapter(config);
|
|
1202
1206
|
default:
|
|
1203
1207
|
throw new Error(`Unknown provider: ${provider}`);
|
|
1204
1208
|
}
|
|
1205
|
-
}
|
|
1209
|
+
}
|
|
1206
1210
|
export {
|
|
1207
1211
|
createSandbox,
|
|
1208
1212
|
TimeoutError,
|
|
1209
|
-
SealosDevboxAdapter,
|
|
1210
1213
|
SandboxStateError,
|
|
1211
1214
|
SandboxReadyTimeoutError,
|
|
1212
1215
|
SandboxException,
|
|
1213
|
-
OpenSandboxAdapter,
|
|
1214
1216
|
FileOperationError,
|
|
1215
1217
|
FeatureNotSupportedError,
|
|
1216
1218
|
ConnectionError,
|
|
@@ -12,7 +12,7 @@ import type { ISandboxLifecycle } from './ISandboxLifecycle';
|
|
|
12
12
|
* Following Interface Segregation Principle, this interface
|
|
13
13
|
* is composed of smaller, focused interfaces.
|
|
14
14
|
*/
|
|
15
|
-
export interface ISandbox
|
|
15
|
+
export interface ISandbox extends ISandboxLifecycle, ICommandExecution, IFileSystem, IHealthCheck {
|
|
16
16
|
/** Provider name (e.g., 'opensandbox') */
|
|
17
17
|
readonly provider: string;
|
|
18
18
|
}
|
|
@@ -3,7 +3,7 @@ import type { SandboxId, SandboxInfo, SandboxStatus } from '../types';
|
|
|
3
3
|
* Interface for sandbox lifecycle operations.
|
|
4
4
|
* Follows Interface Segregation Principle - only lifecycle methods.
|
|
5
5
|
*/
|
|
6
|
-
export interface ISandboxLifecycle
|
|
6
|
+
export interface ISandboxLifecycle {
|
|
7
7
|
/** Unique identifier for this sandbox */
|
|
8
8
|
readonly id: SandboxId;
|
|
9
9
|
/** Current status of the sandbox */
|
|
@@ -11,12 +11,12 @@ export interface ISandboxLifecycle<TSandboxConfig = unknown> {
|
|
|
11
11
|
/**
|
|
12
12
|
* Ensure the sandbox is running.
|
|
13
13
|
*/
|
|
14
|
-
ensureRunning(
|
|
14
|
+
ensureRunning(): Promise<void>;
|
|
15
15
|
/**
|
|
16
16
|
* Create a new sandbox with the given configuration.
|
|
17
17
|
* The sandbox ID is assigned after creation.
|
|
18
18
|
*/
|
|
19
|
-
create(
|
|
19
|
+
create(): Promise<void>;
|
|
20
20
|
/**
|
|
21
21
|
* Start a stopped sandbox.
|
|
22
22
|
*/
|
package/package.json
CHANGED