@blaxel/core 0.2.49-preview.112 → 0.2.50-dev.215
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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/agents/index.js +2 -2
- package/dist/cjs/authentication/clientcredentials.js +4 -1
- package/dist/cjs/common/internal.js +40 -0
- package/dist/cjs/common/settings.js +5 -2
- package/dist/cjs/jobs/jobs.js +2 -2
- package/dist/cjs/jobs/start.js +32 -1
- package/dist/cjs/sandbox/action.js +1 -2
- package/dist/cjs/sandbox/codegen/codegen-ws.js +30 -0
- package/dist/cjs/sandbox/filesystem/filesystem-ws.js +106 -0
- package/dist/cjs/sandbox/network/network-ws.js +12 -0
- package/dist/cjs/sandbox/process/process-ws.js +139 -0
- package/dist/cjs/sandbox/sandbox.js +67 -10
- package/dist/cjs/sandbox/websocket/client.js +269 -0
- package/dist/cjs/sandbox/websocket/index.js +17 -0
- package/dist/cjs/tools/mcpTool.js +2 -2
- package/dist/cjs/types/client/types.gen.d.ts +1 -1
- package/dist/cjs/types/common/internal.d.ts +2 -0
- package/dist/cjs/types/common/settings.d.ts +1 -0
- package/dist/cjs/types/sandbox/codegen/codegen-ws.d.ts +10 -0
- package/dist/cjs/types/sandbox/filesystem/filesystem-ws.d.ts +35 -0
- package/dist/cjs/types/sandbox/network/network-ws.d.ts +7 -0
- package/dist/cjs/types/sandbox/process/process-ws.d.ts +27 -0
- package/dist/cjs/types/sandbox/sandbox.d.ts +12 -6
- package/dist/cjs/types/sandbox/types.d.ts +3 -0
- package/dist/cjs/types/sandbox/websocket/client.d.ts +48 -0
- package/dist/cjs/types/sandbox/websocket/index.d.ts +1 -0
- package/dist/cjs-browser/.tsbuildinfo +1 -1
- package/dist/cjs-browser/agents/index.js +2 -2
- package/dist/cjs-browser/authentication/clientcredentials.js +4 -1
- package/dist/cjs-browser/common/internal.js +40 -0
- package/dist/cjs-browser/common/settings.js +5 -2
- package/dist/cjs-browser/jobs/jobs.js +2 -2
- package/dist/cjs-browser/jobs/start.js +32 -1
- package/dist/cjs-browser/sandbox/action.js +1 -2
- package/dist/cjs-browser/sandbox/codegen/codegen-ws.js +30 -0
- package/dist/cjs-browser/sandbox/filesystem/filesystem-ws.js +106 -0
- package/dist/cjs-browser/sandbox/network/network-ws.js +12 -0
- package/dist/cjs-browser/sandbox/process/process-ws.js +139 -0
- package/dist/cjs-browser/sandbox/sandbox.js +67 -10
- package/dist/cjs-browser/sandbox/websocket/client.js +269 -0
- package/dist/cjs-browser/sandbox/websocket/index.js +17 -0
- package/dist/cjs-browser/tools/mcpTool.js +2 -2
- package/dist/cjs-browser/types/client/types.gen.d.ts +1 -1
- package/dist/cjs-browser/types/common/internal.d.ts +2 -0
- package/dist/cjs-browser/types/common/settings.d.ts +1 -0
- package/dist/cjs-browser/types/sandbox/codegen/codegen-ws.d.ts +10 -0
- package/dist/cjs-browser/types/sandbox/filesystem/filesystem-ws.d.ts +35 -0
- package/dist/cjs-browser/types/sandbox/network/network-ws.d.ts +7 -0
- package/dist/cjs-browser/types/sandbox/process/process-ws.d.ts +27 -0
- package/dist/cjs-browser/types/sandbox/sandbox.d.ts +12 -6
- package/dist/cjs-browser/types/sandbox/types.d.ts +3 -0
- package/dist/cjs-browser/types/sandbox/websocket/client.d.ts +48 -0
- package/dist/cjs-browser/types/sandbox/websocket/index.d.ts +1 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/agents/index.js +3 -3
- package/dist/esm/authentication/clientcredentials.js +4 -1
- package/dist/esm/common/internal.js +38 -0
- package/dist/esm/common/settings.js +5 -2
- package/dist/esm/jobs/jobs.js +3 -3
- package/dist/esm/jobs/start.js +32 -1
- package/dist/esm/sandbox/action.js +2 -3
- package/dist/esm/sandbox/codegen/codegen-ws.js +26 -0
- package/dist/esm/sandbox/filesystem/filesystem-ws.js +102 -0
- package/dist/esm/sandbox/network/network-ws.js +8 -0
- package/dist/esm/sandbox/process/process-ws.js +135 -0
- package/dist/esm/sandbox/sandbox.js +67 -10
- package/dist/esm/sandbox/websocket/client.js +265 -0
- package/dist/esm/sandbox/websocket/index.js +1 -0
- package/dist/esm/tools/mcpTool.js +3 -3
- package/dist/esm-browser/.tsbuildinfo +1 -1
- package/dist/esm-browser/agents/index.js +3 -3
- package/dist/esm-browser/authentication/clientcredentials.js +4 -1
- package/dist/esm-browser/common/internal.js +38 -0
- package/dist/esm-browser/common/settings.js +5 -2
- package/dist/esm-browser/jobs/jobs.js +3 -3
- package/dist/esm-browser/jobs/start.js +32 -1
- package/dist/esm-browser/sandbox/action.js +2 -3
- package/dist/esm-browser/sandbox/codegen/codegen-ws.js +26 -0
- package/dist/esm-browser/sandbox/filesystem/filesystem-ws.js +102 -0
- package/dist/esm-browser/sandbox/network/network-ws.js +8 -0
- package/dist/esm-browser/sandbox/process/process-ws.js +135 -0
- package/dist/esm-browser/sandbox/sandbox.js +67 -10
- package/dist/esm-browser/sandbox/websocket/client.js +265 -0
- package/dist/esm-browser/sandbox/websocket/index.js +1 -0
- package/dist/esm-browser/tools/mcpTool.js +3 -3
- package/package.json +2 -2
|
@@ -180,3 +180,41 @@ export function getForcedUrl(type, name) {
|
|
|
180
180
|
}
|
|
181
181
|
return null;
|
|
182
182
|
}
|
|
183
|
+
export function getWorkloadTypeShort(type) {
|
|
184
|
+
const lowerType = type.toLowerCase();
|
|
185
|
+
switch (lowerType) {
|
|
186
|
+
case 'agent':
|
|
187
|
+
case 'agents':
|
|
188
|
+
return 'agt';
|
|
189
|
+
case 'mcp':
|
|
190
|
+
case 'mcps':
|
|
191
|
+
case 'function':
|
|
192
|
+
case 'functions':
|
|
193
|
+
return 'mcp';
|
|
194
|
+
case 'sandbox':
|
|
195
|
+
case 'sandboxes':
|
|
196
|
+
return 'sbx';
|
|
197
|
+
case 'job':
|
|
198
|
+
case 'jobs':
|
|
199
|
+
return 'job';
|
|
200
|
+
case 'model':
|
|
201
|
+
case 'models':
|
|
202
|
+
return 'mdl';
|
|
203
|
+
default:
|
|
204
|
+
// fallback to first 3 letters of type
|
|
205
|
+
return lowerType.substring(0, 3);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export function generateInternalUrl(workspace, type, name, env, protocol, hostname, blCloud, workspaceId) {
|
|
209
|
+
if (blCloud && workspaceId) {
|
|
210
|
+
// New cloud format: bl-ENV-WORKLOAD_CALLED_NAME-WORKLOAD_TYPE_SHORT-WORKSPACE_ID
|
|
211
|
+
const workloadTypeShort = getWorkloadTypeShort(type);
|
|
212
|
+
const subdomain = `bl-${env}-${name}-${workloadTypeShort}-${workspaceId}`;
|
|
213
|
+
return `${protocol}://${subdomain}.${hostname}`;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
// Legacy format: bl-ENV-HASH.internalhostname
|
|
217
|
+
const hash = getGlobalUniqueHash(workspace, type, name);
|
|
218
|
+
return `${protocol}://bl-${env}-${hash}.${hostname}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -7,7 +7,7 @@ function getPackageVersion() {
|
|
|
7
7
|
if (typeof require !== "undefined") {
|
|
8
8
|
// Try to require package.json (Node.js only, gracefully fails in browser)
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
10
|
-
const packageJson = {"version":"0.2.
|
|
10
|
+
const packageJson = {"version":"0.2.50-dev.215","commit":"833c34cbba3735060a62bdd3594fa917160f3d3c"};
|
|
11
11
|
return packageJson.version || "unknown";
|
|
12
12
|
}
|
|
13
13
|
else {
|
|
@@ -59,7 +59,7 @@ function getCommitHash() {
|
|
|
59
59
|
if (typeof require !== "undefined") {
|
|
60
60
|
// Try to require package.json and look for commit field (set during build)
|
|
61
61
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
62
|
-
const packageJson = {"version":"0.2.
|
|
62
|
+
const packageJson = {"version":"0.2.50-dev.215","commit":"833c34cbba3735060a62bdd3594fa917160f3d3c"};
|
|
63
63
|
// Check for commit in various possible locations
|
|
64
64
|
const commit = packageJson.commit || packageJson.buildInfo?.commit;
|
|
65
65
|
if (commit) {
|
|
@@ -162,6 +162,9 @@ class Settings {
|
|
|
162
162
|
get blCloud() {
|
|
163
163
|
return env.BL_CLOUD === "true";
|
|
164
164
|
}
|
|
165
|
+
get workspaceId() {
|
|
166
|
+
return env.BL_WORKSPACE_ID || "";
|
|
167
|
+
}
|
|
165
168
|
get generation() {
|
|
166
169
|
return env.BL_GENERATION || "";
|
|
167
170
|
}
|
package/dist/esm/jobs/jobs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { generateInternalUrl, getForcedUrl } from "../common/internal.js";
|
|
2
2
|
import { logger } from "../common/logger.js";
|
|
3
3
|
import { settings } from "../common/settings.js";
|
|
4
4
|
import { startSpan } from '../telemetry/telemetry.js';
|
|
@@ -17,8 +17,8 @@ class BlJob {
|
|
|
17
17
|
return new URL(`${settings.runUrl}/${settings.workspace}/jobs/${this.jobName}`);
|
|
18
18
|
}
|
|
19
19
|
get internalUrl() {
|
|
20
|
-
const
|
|
21
|
-
return new URL(
|
|
20
|
+
const url = generateInternalUrl(settings.workspace, "job", this.jobName, settings.env, settings.runInternalProtocol, settings.runInternalHostname, settings.blCloud, settings.workspaceId);
|
|
21
|
+
return new URL(url);
|
|
22
22
|
}
|
|
23
23
|
get forcedUrl() {
|
|
24
24
|
return getForcedUrl('job', this.jobName);
|
package/dist/esm/jobs/start.js
CHANGED
|
@@ -2,12 +2,43 @@ import { authenticate } from '../common/autoload.js';
|
|
|
2
2
|
import { env } from '../common/env.js';
|
|
3
3
|
import { flush } from '../telemetry/telemetry.js';
|
|
4
4
|
class BlJobWrapper {
|
|
5
|
+
async fetchWithRetry(url, maxRetries = 3) {
|
|
6
|
+
let lastError;
|
|
7
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
8
|
+
try {
|
|
9
|
+
const response = await fetch(url);
|
|
10
|
+
// If the response is successful, return it
|
|
11
|
+
if (response.ok) {
|
|
12
|
+
return response;
|
|
13
|
+
}
|
|
14
|
+
// If it's not the last attempt and the status is retriable, retry
|
|
15
|
+
if (attempt < maxRetries && (response.status >= 500 || response.status === 429)) {
|
|
16
|
+
lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
// For non-retriable errors or last attempt, return the response
|
|
20
|
+
return response;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
25
|
+
// If this is the last attempt, throw the error
|
|
26
|
+
if (attempt === maxRetries) {
|
|
27
|
+
throw lastError;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Calculate exponential backoff delay: 2^attempt * 1000ms (1s, 2s, 4s)
|
|
31
|
+
const delay = Math.pow(2, attempt) * 1000;
|
|
32
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
33
|
+
}
|
|
34
|
+
throw lastError || new Error('Failed to fetch after retries');
|
|
35
|
+
}
|
|
5
36
|
async getArguments() {
|
|
6
37
|
if (!env.BL_EXECUTION_DATA_URL) {
|
|
7
38
|
const args = this.parseCommandLineArgs();
|
|
8
39
|
return args;
|
|
9
40
|
}
|
|
10
|
-
const response = await
|
|
41
|
+
const response = await this.fetchWithRetry(env.BL_EXECUTION_DATA_URL);
|
|
11
42
|
const data = await response.json();
|
|
12
43
|
return data.tasks[this.index] ?? {};
|
|
13
44
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createClient } from "@hey-api/client-fetch";
|
|
2
|
-
import {
|
|
2
|
+
import { generateInternalUrl, getForcedUrl } from "../common/internal.js";
|
|
3
3
|
import { settings } from "../common/settings.js";
|
|
4
4
|
import { client as defaultClient } from "./client/client.gen.js";
|
|
5
5
|
export class ResponseError extends Error {
|
|
@@ -44,8 +44,7 @@ export class SandboxAction {
|
|
|
44
44
|
return this.sandbox.metadata?.url ?? `${settings.runUrl}/${settings.workspace}/sandboxes/${this.name}`;
|
|
45
45
|
}
|
|
46
46
|
get internalUrl() {
|
|
47
|
-
|
|
48
|
-
return `${settings.runInternalProtocol}://bl-${settings.env}-${hash}.${settings.runInternalHostname}`;
|
|
47
|
+
return generateInternalUrl(settings.workspace, "sandbox", this.name, settings.env, settings.runInternalProtocol, settings.runInternalHostname, settings.blCloud, settings.workspaceId);
|
|
49
48
|
}
|
|
50
49
|
get client() {
|
|
51
50
|
if (this.sandbox.forceUrl) {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SandboxAction } from "../action.js";
|
|
2
|
+
export class SandboxCodegenWebSocket extends SandboxAction {
|
|
3
|
+
wsClient;
|
|
4
|
+
constructor(sandbox, wsClient) {
|
|
5
|
+
super(sandbox);
|
|
6
|
+
this.wsClient = wsClient;
|
|
7
|
+
}
|
|
8
|
+
async fastapply(path, codeEdit, model) {
|
|
9
|
+
const data = await this.wsClient.send("codegen:fastapply", {
|
|
10
|
+
path,
|
|
11
|
+
codeEdit,
|
|
12
|
+
model,
|
|
13
|
+
});
|
|
14
|
+
return data;
|
|
15
|
+
}
|
|
16
|
+
async reranking(path, query, scoreThreshold, tokenLimit, filePattern) {
|
|
17
|
+
const data = await this.wsClient.send("codegen:reranking", {
|
|
18
|
+
path,
|
|
19
|
+
query,
|
|
20
|
+
scoreThreshold,
|
|
21
|
+
tokenLimit,
|
|
22
|
+
filePattern,
|
|
23
|
+
});
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { SandboxAction } from "../action.js";
|
|
2
|
+
import { SandboxFileSystem } from "./filesystem.js";
|
|
3
|
+
export class SandboxFileSystemWebSocket extends SandboxAction {
|
|
4
|
+
process;
|
|
5
|
+
wsClient;
|
|
6
|
+
httpClient;
|
|
7
|
+
constructor(sandbox, process, wsClient) {
|
|
8
|
+
super(sandbox);
|
|
9
|
+
this.process = process;
|
|
10
|
+
this.wsClient = wsClient;
|
|
11
|
+
// Create HTTP client for fallback operations
|
|
12
|
+
this.httpClient = new SandboxFileSystem(sandbox, process);
|
|
13
|
+
}
|
|
14
|
+
async mkdir(path, permissions = "0755") {
|
|
15
|
+
path = this.formatPath(path);
|
|
16
|
+
const data = await this.wsClient.send("filesystem:create", {
|
|
17
|
+
path,
|
|
18
|
+
isDirectory: true,
|
|
19
|
+
permissions,
|
|
20
|
+
});
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
async write(path, content) {
|
|
24
|
+
path = this.formatPath(path);
|
|
25
|
+
const data = await this.wsClient.send("filesystem:create", {
|
|
26
|
+
path,
|
|
27
|
+
content,
|
|
28
|
+
isDirectory: false,
|
|
29
|
+
});
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
32
|
+
async writeBinary(path, content) {
|
|
33
|
+
return this.httpClient.writeBinary(path, content);
|
|
34
|
+
}
|
|
35
|
+
async writeTree(files, destinationPath = null) {
|
|
36
|
+
const path = this.formatPath(destinationPath ?? "");
|
|
37
|
+
const filesMap = files.reduce((acc, file) => {
|
|
38
|
+
acc[file.path] = file.content;
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
const data = await this.wsClient.send("filesystem:tree:create", {
|
|
42
|
+
path,
|
|
43
|
+
files: filesMap,
|
|
44
|
+
});
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
async read(path) {
|
|
48
|
+
path = this.formatPath(path);
|
|
49
|
+
const data = await this.wsClient.send("filesystem:get", { path });
|
|
50
|
+
return data.content;
|
|
51
|
+
}
|
|
52
|
+
async readBinary(path) {
|
|
53
|
+
// Binary downloads are better suited for HTTP
|
|
54
|
+
// Fall back to HTTP client for binary operations
|
|
55
|
+
return this.httpClient.readBinary(path);
|
|
56
|
+
}
|
|
57
|
+
async download(src, destinationPath, options = {}) {
|
|
58
|
+
// File downloads are better suited for HTTP
|
|
59
|
+
// Fall back to HTTP client
|
|
60
|
+
return this.httpClient.download(src, destinationPath, options);
|
|
61
|
+
}
|
|
62
|
+
async rm(path, recursive = false) {
|
|
63
|
+
path = this.formatPath(path);
|
|
64
|
+
const data = await this.wsClient.send("filesystem:delete", {
|
|
65
|
+
path,
|
|
66
|
+
recursive,
|
|
67
|
+
});
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
async ls(path) {
|
|
71
|
+
path = this.formatPath(path);
|
|
72
|
+
const data = await this.wsClient.send("filesystem:get", { path });
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
async cp(source, destination, { maxWait = 180000 } = {}) {
|
|
76
|
+
// Copy operation is typically done via process execution
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
78
|
+
let process = await this.process.exec({
|
|
79
|
+
command: `cp -r ${source} ${destination}`,
|
|
80
|
+
});
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
|
|
82
|
+
process = await this.process.wait(process.pid, { maxWait, interval: 100 });
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
84
|
+
if (process.status === "failed") {
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
86
|
+
throw new Error(`Could not copy ${source} to ${destination} cause: ${process.logs}`);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
message: "Files copied",
|
|
90
|
+
source,
|
|
91
|
+
destination,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
watch(path, callback, options) {
|
|
95
|
+
// File watching uses HTTP streaming which is already optimized
|
|
96
|
+
// Fall back to HTTP client
|
|
97
|
+
return this.httpClient.watch(path, callback, options);
|
|
98
|
+
}
|
|
99
|
+
formatPath(path) {
|
|
100
|
+
return path;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { SandboxAction } from "../action.js";
|
|
2
|
+
import { SandboxProcess } from "./process.js";
|
|
3
|
+
export class SandboxProcessWebSocket extends SandboxAction {
|
|
4
|
+
wsClient;
|
|
5
|
+
httpClient;
|
|
6
|
+
constructor(sandbox, wsClient) {
|
|
7
|
+
super(sandbox);
|
|
8
|
+
this.wsClient = wsClient;
|
|
9
|
+
// Create HTTP client for fallback operations
|
|
10
|
+
this.httpClient = new SandboxProcess(sandbox);
|
|
11
|
+
}
|
|
12
|
+
streamLogs(identifier, options) {
|
|
13
|
+
const streamId = this.wsClient.sendStream("process:logs:stream:start", { identifier }, (data) => {
|
|
14
|
+
// Handle streaming log data
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
16
|
+
if (data && data.log) {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
18
|
+
const log = String(data.log);
|
|
19
|
+
// Parse log format: "stdout:" or "stderr:" prefix
|
|
20
|
+
if (log.startsWith('stdout:')) {
|
|
21
|
+
const stdout = log.slice(7);
|
|
22
|
+
options.onStdout?.(stdout);
|
|
23
|
+
options.onLog?.(stdout);
|
|
24
|
+
}
|
|
25
|
+
else if (log.startsWith('stderr:')) {
|
|
26
|
+
const stderr = log.slice(7);
|
|
27
|
+
options.onStderr?.(stderr);
|
|
28
|
+
options.onLog?.(stderr);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
options.onLog?.(log);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}, () => {
|
|
35
|
+
// Stream ended
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
close: () => this.wsClient.cancelStream(streamId),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async exec(process) {
|
|
42
|
+
let onLog;
|
|
43
|
+
if ('onLog' in process && process.onLog) {
|
|
44
|
+
onLog = process.onLog;
|
|
45
|
+
delete process.onLog;
|
|
46
|
+
}
|
|
47
|
+
// Store original wait_for_completion setting
|
|
48
|
+
const shouldWaitForCompletion = process.waitForCompletion;
|
|
49
|
+
// Always start process without wait_for_completion to avoid server-side blocking
|
|
50
|
+
if (shouldWaitForCompletion && onLog) {
|
|
51
|
+
process.waitForCompletion = false;
|
|
52
|
+
}
|
|
53
|
+
const data = await this.wsClient.send("process:execute", process);
|
|
54
|
+
let result = data;
|
|
55
|
+
// Handle wait_for_completion with parallel log streaming
|
|
56
|
+
if (shouldWaitForCompletion && onLog) {
|
|
57
|
+
const streamControl = this.streamLogs(result.pid, { onLog });
|
|
58
|
+
try {
|
|
59
|
+
// Wait for process completion
|
|
60
|
+
result = await this.wait(result.pid, { interval: 500, maxWait: 1000 * 60 * 60 });
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
// Clean up log streaming
|
|
64
|
+
if (streamControl) {
|
|
65
|
+
streamControl.close();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// For non-blocking execution, set up log streaming immediately if requested
|
|
71
|
+
if (onLog) {
|
|
72
|
+
const streamControl = this.streamLogs(result.pid, { onLog });
|
|
73
|
+
return {
|
|
74
|
+
...result,
|
|
75
|
+
close() {
|
|
76
|
+
if (streamControl) {
|
|
77
|
+
streamControl.close();
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { ...result, close: () => { } };
|
|
84
|
+
}
|
|
85
|
+
async wait(identifier, { maxWait = 60000, interval = 1000 } = {}) {
|
|
86
|
+
const startTime = Date.now();
|
|
87
|
+
let status = "running";
|
|
88
|
+
let data = await this.get(identifier);
|
|
89
|
+
while (status === "running") {
|
|
90
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
91
|
+
try {
|
|
92
|
+
data = await this.get(identifier);
|
|
93
|
+
status = data.status ?? "running";
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (Date.now() - startTime > maxWait) {
|
|
99
|
+
throw new Error("Process did not finish in time");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return data;
|
|
103
|
+
}
|
|
104
|
+
async get(identifier) {
|
|
105
|
+
const data = await this.wsClient.send("process:get", { identifier });
|
|
106
|
+
return data;
|
|
107
|
+
}
|
|
108
|
+
async list() {
|
|
109
|
+
const data = await this.wsClient.send("process:list", {});
|
|
110
|
+
return data.processes || [];
|
|
111
|
+
}
|
|
112
|
+
async stop(identifier) {
|
|
113
|
+
const data = await this.wsClient.send("process:stop", { identifier });
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
async kill(identifier) {
|
|
117
|
+
const data = await this.wsClient.send("process:kill", { identifier });
|
|
118
|
+
return data;
|
|
119
|
+
}
|
|
120
|
+
async logs(identifier, type = "all") {
|
|
121
|
+
const data = await this.wsClient.send("process:logs", {
|
|
122
|
+
identifier,
|
|
123
|
+
});
|
|
124
|
+
if (type === "all") {
|
|
125
|
+
return data.logs || "";
|
|
126
|
+
}
|
|
127
|
+
else if (type === "stdout") {
|
|
128
|
+
return data.stdout || "";
|
|
129
|
+
}
|
|
130
|
+
else if (type === "stderr") {
|
|
131
|
+
return data.stderr || "";
|
|
132
|
+
}
|
|
133
|
+
throw new Error("Unsupported log type");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -2,11 +2,16 @@ import { v4 as uuidv4 } from "uuid";
|
|
|
2
2
|
import { createSandbox, deleteSandbox, getSandbox, listSandboxes, updateSandbox } from "../client/index.js";
|
|
3
3
|
import { logger } from "../common/logger.js";
|
|
4
4
|
import { SandboxFileSystem } from "./filesystem/index.js";
|
|
5
|
+
import { SandboxFileSystemWebSocket } from "./filesystem/filesystem-ws.js";
|
|
5
6
|
import { SandboxNetwork } from "./network/index.js";
|
|
7
|
+
import { SandboxNetworkWebSocket } from "./network/network-ws.js";
|
|
6
8
|
import { SandboxPreviews } from "./preview.js";
|
|
7
9
|
import { SandboxProcess } from "./process/index.js";
|
|
10
|
+
import { SandboxProcessWebSocket } from "./process/process-ws.js";
|
|
8
11
|
import { SandboxCodegen } from "./codegen/index.js";
|
|
12
|
+
import { SandboxCodegenWebSocket } from "./codegen/codegen-ws.js";
|
|
9
13
|
import { SandboxSessions } from "./session.js";
|
|
14
|
+
import { WebSocketClient } from "./websocket/index.js";
|
|
10
15
|
import { normalizeEnvs, normalizePorts, normalizeVolumes } from "./types.js";
|
|
11
16
|
export class SandboxInstance {
|
|
12
17
|
sandbox;
|
|
@@ -16,14 +21,32 @@ export class SandboxInstance {
|
|
|
16
21
|
previews;
|
|
17
22
|
sessions;
|
|
18
23
|
codegen;
|
|
24
|
+
wsClient;
|
|
19
25
|
constructor(sandbox) {
|
|
20
26
|
this.sandbox = sandbox;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
// If connection type is websocket, initialize WebSocket client and use WebSocket transport layers
|
|
28
|
+
if (sandbox.connectionType === "websocket") {
|
|
29
|
+
const url = sandbox.forceUrl || sandbox.metadata?.url || "";
|
|
30
|
+
this.wsClient = new WebSocketClient({
|
|
31
|
+
url,
|
|
32
|
+
headers: sandbox.headers,
|
|
33
|
+
});
|
|
34
|
+
// Initialize WebSocket-based action handlers
|
|
35
|
+
this.process = new SandboxProcessWebSocket(sandbox, this.wsClient);
|
|
36
|
+
this.fs = new SandboxFileSystemWebSocket(sandbox, this.process, this.wsClient);
|
|
37
|
+
this.network = new SandboxNetworkWebSocket(sandbox, this.wsClient);
|
|
38
|
+
this.codegen = new SandboxCodegenWebSocket(sandbox, this.wsClient);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// Default to HTTP-based action handlers
|
|
42
|
+
this.process = new SandboxProcess(sandbox);
|
|
43
|
+
this.fs = new SandboxFileSystem(sandbox, this.process);
|
|
44
|
+
this.network = new SandboxNetwork(sandbox);
|
|
45
|
+
this.codegen = new SandboxCodegen(sandbox);
|
|
46
|
+
}
|
|
47
|
+
// These are always HTTP-based
|
|
24
48
|
this.previews = new SandboxPreviews(sandbox);
|
|
25
49
|
this.sessions = new SandboxSessions(sandbox);
|
|
26
|
-
this.codegen = new SandboxCodegen(sandbox);
|
|
27
50
|
}
|
|
28
51
|
get metadata() {
|
|
29
52
|
return this.sandbox.metadata;
|
|
@@ -42,10 +65,17 @@ export class SandboxInstance {
|
|
|
42
65
|
logger.warn("⚠️ Warning: sandbox.wait() is deprecated. You don't need to wait for the sandbox to be deployed anymore.");
|
|
43
66
|
return this;
|
|
44
67
|
}
|
|
68
|
+
closeConnection() {
|
|
69
|
+
if (this.wsClient) {
|
|
70
|
+
this.wsClient.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
45
73
|
static async create(sandbox, { safe = true } = {}) {
|
|
46
74
|
const defaultName = `sandbox-${uuidv4().replace(/-/g, '').substring(0, 8)}`;
|
|
47
75
|
const defaultImage = `blaxel/base-image:latest`;
|
|
48
76
|
const defaultMemory = 4096;
|
|
77
|
+
// Store connection type if provided
|
|
78
|
+
let connectionType;
|
|
49
79
|
// Handle SandboxCreateConfiguration or simple dict with name/image/memory/ports/envs/volumes keys
|
|
50
80
|
if (!sandbox ||
|
|
51
81
|
'name' in sandbox ||
|
|
@@ -55,7 +85,8 @@ export class SandboxInstance {
|
|
|
55
85
|
'envs' in sandbox ||
|
|
56
86
|
'volumes' in sandbox ||
|
|
57
87
|
'lifecycle' in sandbox ||
|
|
58
|
-
'snapshotEnabled' in sandbox
|
|
88
|
+
'snapshotEnabled' in sandbox ||
|
|
89
|
+
'connectionType' in sandbox) {
|
|
59
90
|
if (!sandbox)
|
|
60
91
|
sandbox = {};
|
|
61
92
|
if (!sandbox.name)
|
|
@@ -64,6 +95,7 @@ export class SandboxInstance {
|
|
|
64
95
|
sandbox.image = defaultImage;
|
|
65
96
|
if (!sandbox.memory)
|
|
66
97
|
sandbox.memory = defaultMemory;
|
|
98
|
+
connectionType = sandbox.connectionType;
|
|
67
99
|
const ports = normalizePorts(sandbox.ports);
|
|
68
100
|
const envs = normalizeEnvs(sandbox.envs);
|
|
69
101
|
const volumes = normalizeVolumes(sandbox.volumes);
|
|
@@ -112,7 +144,16 @@ export class SandboxInstance {
|
|
|
112
144
|
body: sandbox,
|
|
113
145
|
throwOnError: true,
|
|
114
146
|
});
|
|
115
|
-
|
|
147
|
+
// Add connection type to configuration
|
|
148
|
+
const config = {
|
|
149
|
+
...data,
|
|
150
|
+
connectionType: connectionType || "http",
|
|
151
|
+
};
|
|
152
|
+
const instance = new SandboxInstance(config);
|
|
153
|
+
// Connect WebSocket if needed
|
|
154
|
+
if (connectionType === "websocket" && instance.wsClient) {
|
|
155
|
+
await instance.wsClient.connect();
|
|
156
|
+
}
|
|
116
157
|
// TODO remove this part once we have a better way to handle this
|
|
117
158
|
if (safe) {
|
|
118
159
|
try {
|
|
@@ -122,20 +163,34 @@ export class SandboxInstance {
|
|
|
122
163
|
}
|
|
123
164
|
return instance;
|
|
124
165
|
}
|
|
125
|
-
static async get(sandboxName) {
|
|
166
|
+
static async get(sandboxName, connectionType) {
|
|
126
167
|
const { data } = await getSandbox({
|
|
127
168
|
path: {
|
|
128
169
|
sandboxName,
|
|
129
170
|
},
|
|
130
171
|
throwOnError: true,
|
|
131
172
|
});
|
|
132
|
-
|
|
173
|
+
// Add connection type to configuration
|
|
174
|
+
const config = {
|
|
175
|
+
...data,
|
|
176
|
+
connectionType: connectionType || "http",
|
|
177
|
+
};
|
|
178
|
+
const instance = new SandboxInstance(config);
|
|
179
|
+
// Connect WebSocket if needed
|
|
180
|
+
if (connectionType === "websocket" && instance.wsClient) {
|
|
181
|
+
await instance.wsClient.connect();
|
|
182
|
+
}
|
|
183
|
+
return instance;
|
|
133
184
|
}
|
|
134
185
|
static async list() {
|
|
135
186
|
const { data } = await listSandboxes({ throwOnError: true });
|
|
136
187
|
return data.map((sandbox) => new SandboxInstance(sandbox));
|
|
137
188
|
}
|
|
138
|
-
static async delete(sandboxName) {
|
|
189
|
+
static async delete(sandboxName, instance) {
|
|
190
|
+
// Close WebSocket connection if instance is provided
|
|
191
|
+
if (instance && instance.wsClient) {
|
|
192
|
+
instance.closeConnection();
|
|
193
|
+
}
|
|
139
194
|
const { data } = await deleteSandbox({
|
|
140
195
|
path: {
|
|
141
196
|
sandboxName,
|
|
@@ -165,8 +220,10 @@ export class SandboxInstance {
|
|
|
165
220
|
if (!name) {
|
|
166
221
|
throw new Error("Sandbox name is required");
|
|
167
222
|
}
|
|
223
|
+
// Get connection type if specified
|
|
224
|
+
const connectionType = 'connectionType' in sandbox ? sandbox.connectionType : undefined;
|
|
168
225
|
// Get the existing sandbox to check its status
|
|
169
|
-
const sandboxInstance = await SandboxInstance.get(name);
|
|
226
|
+
const sandboxInstance = await SandboxInstance.get(name, connectionType);
|
|
170
227
|
// If the sandbox is TERMINATED, treat it as not existing
|
|
171
228
|
if (sandboxInstance.status === "TERMINATED") {
|
|
172
229
|
// Create a new sandbox - backend will handle cleanup of the terminated one
|