@computesdk/beam 0.1.7 → 0.1.9
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/index.js +8 -68
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -39,28 +39,6 @@ function shellEscape(arg) {
|
|
|
39
39
|
if (arg === "") return "''";
|
|
40
40
|
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
41
41
|
}
|
|
42
|
-
function isNodeParserFailure(output) {
|
|
43
|
-
const hasSyntaxMarker = output.includes("SyntaxError") || output.includes("Unexpected token") || output.includes("Unexpected identifier") || output.includes("Unexpected end of input") || output.includes("Invalid or unexpected token");
|
|
44
|
-
const hasCompileContext = output.includes("[eval]") || output.includes("makeContextifyScript") || output.includes("compileScript") || output.includes("wrapSafe");
|
|
45
|
-
const hasRuntimeSyntaxSignature = output.includes("at JSON.parse") || output.includes("JSON.parse (<anonymous>)") || output.includes("in JSON at position");
|
|
46
|
-
return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;
|
|
47
|
-
}
|
|
48
|
-
function isPythonParserFailure(output) {
|
|
49
|
-
const hasSyntaxMarker = output.includes("SyntaxError") || output.includes("IndentationError") || output.includes("TabError");
|
|
50
|
-
const hasStringFileContext = output.includes('File "<string>"');
|
|
51
|
-
const hasRuntimeTraceback = output.includes("Traceback (most recent call last)");
|
|
52
|
-
return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;
|
|
53
|
-
}
|
|
54
|
-
function isParserFailure(output, runtime) {
|
|
55
|
-
if (!output.trim()) return false;
|
|
56
|
-
if (runtime === "node") {
|
|
57
|
-
return isNodeParserFailure(output);
|
|
58
|
-
}
|
|
59
|
-
if (runtime === "python") {
|
|
60
|
-
return isPythonParserFailure(output);
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
42
|
var beam = (0, import_provider.defineProvider)({
|
|
65
43
|
name: "beam",
|
|
66
44
|
methods: {
|
|
@@ -83,10 +61,6 @@ var beam = (0, import_provider.defineProvider)({
|
|
|
83
61
|
);
|
|
84
62
|
}
|
|
85
63
|
try {
|
|
86
|
-
if (options?.sandboxId) {
|
|
87
|
-
const instance2 = await import_beam_js.Sandbox.connect(options.sandboxId);
|
|
88
|
-
return { sandbox: instance2, sandboxId: instance2.containerId };
|
|
89
|
-
}
|
|
90
64
|
const {
|
|
91
65
|
runtime: optRuntime,
|
|
92
66
|
timeout: optTimeout,
|
|
@@ -98,8 +72,6 @@ var beam = (0, import_provider.defineProvider)({
|
|
|
98
72
|
sandboxId: _sandboxId,
|
|
99
73
|
namespace: _namespace,
|
|
100
74
|
directory: _directory,
|
|
101
|
-
overlays: _overlays,
|
|
102
|
-
servers: _servers,
|
|
103
75
|
...providerOptions
|
|
104
76
|
} = options || {};
|
|
105
77
|
const sandboxConfig = {
|
|
@@ -180,38 +152,6 @@ var beam = (0, import_provider.defineProvider)({
|
|
|
180
152
|
*
|
|
181
153
|
* Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().
|
|
182
154
|
*/
|
|
183
|
-
runCode: async (sandbox, code, runtime) => {
|
|
184
|
-
const effectiveRuntime = runtime || (code.includes("print(") || code.includes("import ") || code.includes("def ") || code.includes("sys.") || code.includes("json.") || code.includes("__") || code.includes('f"') || code.includes("f'") || code.includes("raise ") ? "python" : "node");
|
|
185
|
-
try {
|
|
186
|
-
const command = effectiveRuntime === "python" ? ["python3", "-c", code] : ["node", "-e", code];
|
|
187
|
-
const proc = await sandbox.exec(command);
|
|
188
|
-
await proc.wait();
|
|
189
|
-
const [stdoutStr, stderrStr] = await Promise.all([
|
|
190
|
-
proc.stdout.read(),
|
|
191
|
-
proc.stderr.read()
|
|
192
|
-
]);
|
|
193
|
-
const output = stderrStr ? `${stdoutStr}${stdoutStr && stderrStr ? "\n" : ""}${stderrStr}` : stdoutStr;
|
|
194
|
-
const combinedOutput = `${stdoutStr || ""} ${stderrStr || ""}`;
|
|
195
|
-
if (proc.exitCode !== 0 && isParserFailure(combinedOutput, effectiveRuntime)) {
|
|
196
|
-
throw new Error(`Syntax error: ${(stderrStr || stdoutStr || "").trim()}`);
|
|
197
|
-
}
|
|
198
|
-
if (proc.exitCode !== 0 && !stdoutStr && !stderrStr) {
|
|
199
|
-
throw new Error(`Code execution failed with exit code ${proc.exitCode}`);
|
|
200
|
-
}
|
|
201
|
-
return {
|
|
202
|
-
output,
|
|
203
|
-
exitCode: proc.exitCode,
|
|
204
|
-
language: effectiveRuntime
|
|
205
|
-
};
|
|
206
|
-
} catch (error) {
|
|
207
|
-
if (error instanceof Error && error.message.includes("Syntax error")) {
|
|
208
|
-
throw error;
|
|
209
|
-
}
|
|
210
|
-
throw new Error(
|
|
211
|
-
`Beam execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
},
|
|
215
155
|
/**
|
|
216
156
|
* Execute a shell command in the sandbox
|
|
217
157
|
*
|
|
@@ -222,7 +162,7 @@ var beam = (0, import_provider.defineProvider)({
|
|
|
222
162
|
try {
|
|
223
163
|
let fullCommand = command;
|
|
224
164
|
if (options?.env && Object.keys(options.env).length > 0) {
|
|
225
|
-
const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${(0, import_provider.escapeShellArg)(v)}"`).join(" ");
|
|
165
|
+
const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${(0, import_provider.escapeShellArg)(String(v))}"`).join(" ");
|
|
226
166
|
fullCommand = `${envPrefix} ${fullCommand}`;
|
|
227
167
|
}
|
|
228
168
|
if (options?.cwd) {
|
|
@@ -257,23 +197,23 @@ var beam = (0, import_provider.defineProvider)({
|
|
|
257
197
|
*/
|
|
258
198
|
getInfo: async (sandbox) => {
|
|
259
199
|
let runtime = "python";
|
|
260
|
-
const
|
|
261
|
-
if (typeof
|
|
262
|
-
const lower =
|
|
200
|
+
const runtimeHint = sandbox;
|
|
201
|
+
if (typeof runtimeHint.runtime === "string") {
|
|
202
|
+
const lower = runtimeHint.runtime.toLowerCase();
|
|
263
203
|
if (lower.includes("node")) {
|
|
264
204
|
runtime = "node";
|
|
265
205
|
} else if (lower.includes("python")) {
|
|
266
206
|
runtime = "python";
|
|
267
207
|
}
|
|
268
|
-
} else if (typeof
|
|
269
|
-
const imageStr =
|
|
208
|
+
} else if (typeof runtimeHint.image === "string") {
|
|
209
|
+
const imageStr = runtimeHint.image.toLowerCase();
|
|
270
210
|
if (imageStr.includes("node")) {
|
|
271
211
|
runtime = "node";
|
|
272
212
|
} else if (imageStr.includes("python")) {
|
|
273
213
|
runtime = "python";
|
|
274
214
|
}
|
|
275
|
-
} else if (typeof
|
|
276
|
-
const imageNameStr =
|
|
215
|
+
} else if (typeof runtimeHint.imageName === "string") {
|
|
216
|
+
const imageNameStr = runtimeHint.imageName.toLowerCase();
|
|
277
217
|
if (imageNameStr.includes("node")) {
|
|
278
218
|
runtime = "node";
|
|
279
219
|
} else if (imageNameStr.includes("python")) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Beam Provider - Factory-based Implementation\n *\n * Containerized sandbox environments using the Beam platform.\n * Beam provides sandboxes with process management, filesystem access,\n * and port exposure capabilities.\n *\n * Features:\n * - Process execution via sandbox.exec()\n * - Code execution in Python, JavaScript\n * - Shell command execution\n * - Filesystem operations (shell-based + native listFiles)\n * - Dynamic port exposure for accessing sandbox services\n */\n\nimport { Sandbox, SandboxInstance, beamOpts, Image } from '@beamcloud/beam-js';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\nimport type {\n CodeResult,\n CommandResult,\n SandboxInfo,\n Runtime,\n CreateSandboxOptions,\n FileEntry,\n RunCommandOptions,\n} from 'computesdk';\n\n/** Type alias for the runCommand function passed to filesystem methods */\ntype RunCommandFn = (sandbox: SandboxInstance, command: string, options?: RunCommandOptions) => Promise<CommandResult>;\n\n/**\n * Beam-specific configuration options\n */\nexport interface BeamConfig {\n /** Beam API token - if not provided, will fallback to BEAM_TOKEN environment variable */\n token?: string;\n /** Beam workspace ID - if not provided, will fallback to BEAM_WORKSPACE_ID environment variable */\n workspaceId?: string;\n /** Gateway URL for custom/staging environments */\n gatewayUrl?: string;\n /** Request timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Configure the global beamOpts singleton before each SDK call.\n * Beam uses a global config pattern rather than per-call config.\n */\nfunction configureBeamOpts(config: BeamConfig): void {\n beamOpts.token = config.token || (typeof process !== 'undefined' && process.env?.BEAM_TOKEN) || '';\n beamOpts.workspaceId = config.workspaceId || (typeof process !== 'undefined' && process.env?.BEAM_WORKSPACE_ID) || '';\n if (config.gatewayUrl) {\n beamOpts.gatewayUrl = config.gatewayUrl;\n }\n if (config.timeout) {\n (beamOpts as any).timeout = config.timeout;\n }\n}\n\n/**\n * Shell-escape a string using single quotes (POSIX).\n */\nfunction shellEscape(arg: string): string {\n if (arg === '') return \"''\";\n return `'${arg.replace(/'/g, \"'\\\\''\")}'`;\n}\n\n/**\n * Detect Node parser/compile failures while avoiding runtime SyntaxError matches.\n */\nfunction isNodeParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('Unexpected token') ||\n output.includes('Unexpected identifier') ||\n output.includes('Unexpected end of input') ||\n output.includes('Invalid or unexpected token');\n\n const hasCompileContext =\n output.includes('[eval]') ||\n output.includes('makeContextifyScript') ||\n output.includes('compileScript') ||\n output.includes('wrapSafe');\n\n const hasRuntimeSyntaxSignature =\n output.includes('at JSON.parse') ||\n output.includes('JSON.parse (<anonymous>)') ||\n output.includes('in JSON at position');\n\n return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;\n}\n\n/**\n * Detect Python parser failures (syntax/indentation/tab) emitted by python -c.\n */\nfunction isPythonParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('IndentationError') ||\n output.includes('TabError');\n\n const hasStringFileContext = output.includes('File \"<string>\"');\n const hasRuntimeTraceback = output.includes('Traceback (most recent call last)');\n\n return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;\n}\n\n/**\n * Detect parser/compile failures that should throw instead of returning CodeResult.\n */\nfunction isParserFailure(output: string, runtime: Runtime): boolean {\n if (!output.trim()) return false;\n\n if (runtime === 'node') {\n return isNodeParserFailure(output);\n }\n\n if (runtime === 'python') {\n return isPythonParserFailure(output);\n }\n\n return false;\n}\n\n/**\n * Create a Beam provider instance using the factory pattern\n *\n * Beam provides containerized sandbox environments with:\n * - Process management (exec, runCode)\n * - Filesystem access\n * - Dynamic port exposure\n * - Snapshot and image creation capabilities\n */\nexport const beam = defineProvider<SandboxInstance, BeamConfig>({\n name: 'beam',\n methods: {\n sandbox: {\n /**\n * Create a new Beam sandbox\n *\n * Uses Sandbox class from @beamcloud/beam-js to provision a container.\n */\n create: async (config: BeamConfig, options?: CreateSandboxOptions) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n throw new Error(\n `Missing Beam token. Provide 'token' in config or set BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n\n if (!beamOpts.workspaceId) {\n throw new Error(\n `Missing Beam workspace ID. Provide 'workspaceId' in config or set BEAM_WORKSPACE_ID environment variable.`\n );\n }\n try {\n if (options?.sandboxId) {\n const instance = await Sandbox.connect(options.sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n }\n\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: optRuntime,\n timeout: optTimeout,\n envs,\n name,\n metadata: _metadata,\n templateId: _templateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n overlays: _overlays,\n servers: _servers,\n ...providerOptions\n } = options || {};\n\n const sandboxConfig: any = {\n name: name || `computesdk-${Date.now()}`,\n keepWarmSeconds: 300,\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.timeout takes precedence over config.timeout\n const timeout = optTimeout ?? config.timeout;\n if (timeout) {\n sandboxConfig.keepWarmSeconds = Math.ceil(timeout / 1000);\n }\n\n if (optRuntime === 'node') {\n sandboxConfig.image = Image.fromRegistry('node:20-slim');\n }\n\n if (envs) {\n sandboxConfig.envVars = Object.entries(envs).map(([name, value]) => `${name}=${value}`);\n }\n\n const sandbox = new Sandbox(sandboxConfig);\n const instance = await sandbox.create();\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('401')) {\n throw new Error(\n `Beam authentication failed. Please check your BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n }\n throw new Error(\n `Failed to create Beam sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Connect to an existing Beam sandbox by ID\n *\n * Uses Sandbox.connect() to reconnect to a running sandbox.\n */\n getById: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return null;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch {\n return null;\n }\n },\n\n /**\n * List all active Beam sandboxes\n *\n * Beam SDK has no sandbox list API; returns empty array.\n */\n list: async (_config: BeamConfig) => {\n return [];\n },\n\n /**\n * Destroy a Beam sandbox\n *\n * Connects to the sandbox and terminates it.\n */\n destroy: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n await instance.terminate();\n } catch {\n // Sandbox might already be destroyed\n }\n },\n\n /**\n * Execute code in the sandbox\n *\n * Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().\n */\n runCode: async (sandbox: SandboxInstance, code: string, runtime?: Runtime): Promise<CodeResult> => {\n const effectiveRuntime = runtime || (\n code.includes('print(') ||\n code.includes('import ') ||\n code.includes('def ') ||\n code.includes('sys.') ||\n code.includes('json.') ||\n code.includes('__') ||\n code.includes('f\"') ||\n code.includes(\"f'\") ||\n code.includes('raise ')\n ? 'python'\n : 'node'\n );\n\n try {\n const command = effectiveRuntime === 'python'\n ? ['python3', '-c', code]\n : ['node', '-e', code];\n\n const proc = await sandbox.exec(command);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n const output = stderrStr\n ? `${stdoutStr}${stdoutStr && stderrStr ? '\\n' : ''}${stderrStr}`\n : stdoutStr;\n\n const combinedOutput = `${stdoutStr || ''} ${stderrStr || ''}`;\n\n if (proc.exitCode !== 0 && isParserFailure(combinedOutput, effectiveRuntime)) {\n throw new Error(`Syntax error: ${(stderrStr || stdoutStr || '').trim()}`);\n }\n\n if (proc.exitCode !== 0 && !stdoutStr && !stderrStr) {\n throw new Error(`Code execution failed with exit code ${proc.exitCode}`);\n }\n\n return {\n output,\n exitCode: proc.exitCode,\n language: effectiveRuntime,\n };\n } catch (error) {\n if (error instanceof Error && error.message.includes('Syntax error')) {\n throw error;\n }\n throw new Error(\n `Beam execution failed: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Execute a shell command in the sandbox\n *\n * Uses sandbox.exec() with shell command string.\n */\n runCommand: async (sandbox: SandboxInstance, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n let fullCommand = command;\n\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]: [string, string]) => `${k}=\"${escapeShellArg(v)}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Wrap in sh -c to get shell parsing (pipes, redirects, etc.)\n const proc = await sandbox.exec(['sh', '-c', fullCommand]);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n return {\n stdout: stdoutStr || '',\n stderr: stderrStr || '',\n exitCode: proc.exitCode || 0,\n durationMs: Date.now() - startTime,\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime,\n };\n }\n },\n\n /**\n * Get sandbox information\n */\n getInfo: async (sandbox: SandboxInstance): Promise<SandboxInfo> => {\n // Derive runtime from sandbox instance if available; otherwise default to 'python'\n let runtime: Runtime = 'python';\n const anySandbox = sandbox as any;\n\n if (typeof anySandbox.runtime === 'string') {\n const lower = anySandbox.runtime.toLowerCase();\n if (lower.includes('node')) {\n runtime = 'node';\n } else if (lower.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof anySandbox.image === 'string') {\n const imageStr = anySandbox.image.toLowerCase();\n if (imageStr.includes('node')) {\n runtime = 'node';\n } else if (imageStr.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof anySandbox.imageName === 'string') {\n const imageNameStr = anySandbox.imageName.toLowerCase();\n if (imageNameStr.includes('node')) {\n runtime = 'node';\n } else if (imageNameStr.includes('python')) {\n runtime = 'python';\n }\n }\n\n return {\n id: sandbox.containerId,\n provider: 'beam',\n runtime,\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n containerId: sandbox.containerId,\n },\n };\n },\n\n /**\n * Get public URL for a specific port\n *\n * Uses sandbox.exposePort() to dynamically expose a port.\n */\n getUrl: async (sandbox: SandboxInstance, options: { port: number; protocol?: string }): Promise<string> => {\n try {\n return await sandbox.exposePort(options.port);\n } catch (error) {\n throw new Error(\n `Failed to get Beam URL for port ${options.port}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Filesystem operations\n *\n * Most operations use shell commands via runCommand since Beam's file API\n * is local-path oriented (uploadFile/downloadFile require local filesystem paths).\n * readdir uses the native listFiles API since it returns structured data.\n */\n filesystem: {\n readFile: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<string> => {\n const result = await runCommand(sandbox, `cat ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to read file ${path}: ${result.stderr}`);\n }\n return result.stdout;\n },\n\n writeFile: async (sandbox: SandboxInstance, path: string, content: string, runCommand: RunCommandFn): Promise<void> => {\n const b64 = Buffer.from(content).toString('base64');\n const result = await runCommand(sandbox, `echo '${b64}' | base64 -d > ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to write file ${path}: ${result.stderr}`);\n }\n },\n\n mkdir: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `mkdir -p ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to create directory ${path}: ${result.stderr}`);\n }\n },\n\n readdir: async (sandbox: SandboxInstance, path: string, _runCommand: RunCommandFn): Promise<FileEntry[]> => {\n const files = await sandbox.fs.listFiles(path);\n return files.map((file: any) => ({\n name: file.name,\n type: file.isDir ? 'directory' as const : 'file' as const,\n size: Number(file.size) || 0,\n modified: file.modTime ? new Date(file.modTime * 1000) : new Date(),\n }));\n },\n\n exists: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<boolean> => {\n const result = await runCommand(sandbox, `test -f ${shellEscape(path)} || test -d ${shellEscape(path)}`);\n return result.exitCode === 0;\n },\n\n remove: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `rm -rf ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to remove ${path}: ${result.stderr}`);\n }\n },\n },\n\n /**\n * Get the native SandboxInstance for advanced usage\n */\n getInstance: (sandbox: SandboxInstance): SandboxInstance => {\n return sandbox;\n },\n },\n },\n});\n\n// Export Beam sandbox type for explicit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B,MAAc,eAA+C;AACpG,gBAAM,SAAS,MAAM,WAAW,SAAS,WAAW,YAAY,IAAI,CAAC,eAAe,YAAY,IAAI,CAAC,EAAE;AACvG,iBAAO,OAAO,aAAa;AAAA,QAC7B;AAAA,QAEA,QAAQ,OAAO,SAA0B,MAAc,eAA4C;AACjG,gBAAM,SAAS,MAAM,WAAW,SAAS,UAAU,YAAY,IAAI,CAAC,EAAE;AACtE,cAAI,OAAO,aAAa,GAAG;AACzB,kBAAM,IAAI,MAAM,oBAAoB,IAAI,KAAK,OAAO,MAAM,EAAE;AAAA,UAC9D;AAAA,QACF;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAKA,aAAa,CAAC,YAA8C;AAC1D,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":["instance","name"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Beam Provider - Factory-based Implementation\n *\n * Containerized sandbox environments using the Beam platform.\n * Beam provides sandboxes with process management, filesystem access,\n * and port exposure capabilities.\n *\n * Features:\n * - Process execution via sandbox.exec()\n * - Code execution in Python, JavaScript\n * - Shell command execution\n * - Filesystem operations (shell-based + native listFiles)\n * - Dynamic port exposure for accessing sandbox services\n */\n\nimport { Sandbox, SandboxInstance, beamOpts, Image } from '@beamcloud/beam-js';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\nimport type {\n CodeResult,\n CommandResult,\n SandboxInfo,\n Runtime,\n CreateSandboxOptions,\n FileEntry,\n RunCommandOptions,\n} from 'computesdk';\n\n/** Type alias for the runCommand function passed to filesystem methods */\ntype RunCommandFn = (sandbox: SandboxInstance, command: string, options?: RunCommandOptions) => Promise<CommandResult>;\n\n/**\n * Beam-specific configuration options\n */\nexport interface BeamConfig {\n /** Beam API token - if not provided, will fallback to BEAM_TOKEN environment variable */\n token?: string;\n /** Beam workspace ID - if not provided, will fallback to BEAM_WORKSPACE_ID environment variable */\n workspaceId?: string;\n /** Gateway URL for custom/staging environments */\n gatewayUrl?: string;\n /** Request timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Configure the global beamOpts singleton before each SDK call.\n * Beam uses a global config pattern rather than per-call config.\n */\nfunction configureBeamOpts(config: BeamConfig): void {\n beamOpts.token = config.token || (typeof process !== 'undefined' && process.env?.BEAM_TOKEN) || '';\n beamOpts.workspaceId = config.workspaceId || (typeof process !== 'undefined' && process.env?.BEAM_WORKSPACE_ID) || '';\n if (config.gatewayUrl) {\n beamOpts.gatewayUrl = config.gatewayUrl;\n }\n if (config.timeout) {\n (beamOpts as any).timeout = config.timeout;\n }\n}\n\n/**\n * Shell-escape a string using single quotes (POSIX).\n */\nfunction shellEscape(arg: string): string {\n if (arg === '') return \"''\";\n return `'${arg.replace(/'/g, \"'\\\\''\")}'`;\n}\n\n/**\n * Detect Node parser/compile failures while avoiding runtime SyntaxError matches.\n */\nfunction isNodeParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('Unexpected token') ||\n output.includes('Unexpected identifier') ||\n output.includes('Unexpected end of input') ||\n output.includes('Invalid or unexpected token');\n\n const hasCompileContext =\n output.includes('[eval]') ||\n output.includes('makeContextifyScript') ||\n output.includes('compileScript') ||\n output.includes('wrapSafe');\n\n const hasRuntimeSyntaxSignature =\n output.includes('at JSON.parse') ||\n output.includes('JSON.parse (<anonymous>)') ||\n output.includes('in JSON at position');\n\n return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;\n}\n\n/**\n * Detect Python parser failures (syntax/indentation/tab) emitted by python -c.\n */\nfunction isPythonParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('IndentationError') ||\n output.includes('TabError');\n\n const hasStringFileContext = output.includes('File \"<string>\"');\n const hasRuntimeTraceback = output.includes('Traceback (most recent call last)');\n\n return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;\n}\n\n/**\n * Detect parser/compile failures that should throw instead of returning CodeResult.\n */\nfunction isParserFailure(output: string, runtime: Runtime): boolean {\n if (!output.trim()) return false;\n\n if (runtime === 'node') {\n return isNodeParserFailure(output);\n }\n\n if (runtime === 'python') {\n return isPythonParserFailure(output);\n }\n\n return false;\n}\n\n/**\n * Create a Beam provider instance using the factory pattern\n *\n * Beam provides containerized sandbox environments with:\n * - Process management (exec, runCode)\n * - Filesystem access\n * - Dynamic port exposure\n * - Snapshot and image creation capabilities\n */\nexport const beam = defineProvider<SandboxInstance, BeamConfig>({\n name: 'beam',\n methods: {\n sandbox: {\n /**\n * Create a new Beam sandbox\n *\n * Uses Sandbox class from @beamcloud/beam-js to provision a container.\n */\n create: async (config: BeamConfig, options?: CreateSandboxOptions) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n throw new Error(\n `Missing Beam token. Provide 'token' in config or set BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n\n if (!beamOpts.workspaceId) {\n throw new Error(\n `Missing Beam workspace ID. Provide 'workspaceId' in config or set BEAM_WORKSPACE_ID environment variable.`\n );\n }\n\n try {\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: optRuntime,\n timeout: optTimeout,\n envs,\n name,\n metadata: _metadata,\n templateId: _templateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n ...providerOptions\n } = options || {};\n\n const sandboxConfig: any = {\n name: name || `computesdk-${Date.now()}`,\n keepWarmSeconds: 300,\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.timeout takes precedence over config.timeout\n const timeout = optTimeout ?? config.timeout;\n if (timeout) {\n sandboxConfig.keepWarmSeconds = Math.ceil(timeout / 1000);\n }\n\n if (optRuntime === 'node') {\n sandboxConfig.image = Image.fromRegistry('node:20-slim');\n }\n\n if (envs) {\n sandboxConfig.envVars = Object.entries(envs).map(([name, value]) => `${name}=${value}`);\n }\n\n const sandbox = new Sandbox(sandboxConfig);\n const instance = await sandbox.create();\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('401')) {\n throw new Error(\n `Beam authentication failed. Please check your BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n }\n throw new Error(\n `Failed to create Beam sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Connect to an existing Beam sandbox by ID\n *\n * Uses Sandbox.connect() to reconnect to a running sandbox.\n */\n getById: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return null;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch {\n return null;\n }\n },\n\n /**\n * List all active Beam sandboxes\n *\n * Beam SDK has no sandbox list API; returns empty array.\n */\n list: async (_config: BeamConfig) => {\n return [];\n },\n\n /**\n * Destroy a Beam sandbox\n *\n * Connects to the sandbox and terminates it.\n */\n destroy: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n await instance.terminate();\n } catch {\n // Sandbox might already be destroyed\n }\n },\n\n /**\n * Execute code in the sandbox\n *\n * Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().\n */\n\n /**\n * Execute a shell command in the sandbox\n *\n * Uses sandbox.exec() with shell command string.\n */\n runCommand: async (sandbox: SandboxInstance, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n let fullCommand = command;\n\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]) => `${k}=\"${escapeShellArg(String(v))}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Wrap in sh -c to get shell parsing (pipes, redirects, etc.)\n const proc = await sandbox.exec(['sh', '-c', fullCommand]);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n return {\n stdout: stdoutStr || '',\n stderr: stderrStr || '',\n exitCode: proc.exitCode || 0,\n durationMs: Date.now() - startTime,\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime,\n };\n }\n },\n\n /**\n * Get sandbox information\n */\n getInfo: async (sandbox: SandboxInstance): Promise<SandboxInfo> => {\n // Derive runtime from sandbox instance if available; otherwise default to 'python'\n let runtime: Runtime = 'python';\n const runtimeHint = sandbox as SandboxInstance & {\n runtime?: unknown;\n image?: unknown;\n imageName?: unknown;\n };\n\n if (typeof runtimeHint.runtime === 'string') {\n const lower = runtimeHint.runtime.toLowerCase();\n if (lower.includes('node')) {\n runtime = 'node';\n } else if (lower.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof runtimeHint.image === 'string') {\n const imageStr = runtimeHint.image.toLowerCase();\n if (imageStr.includes('node')) {\n runtime = 'node';\n } else if (imageStr.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof runtimeHint.imageName === 'string') {\n const imageNameStr = runtimeHint.imageName.toLowerCase();\n if (imageNameStr.includes('node')) {\n runtime = 'node';\n } else if (imageNameStr.includes('python')) {\n runtime = 'python';\n }\n }\n\n return {\n id: sandbox.containerId,\n provider: 'beam',\n runtime,\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n containerId: sandbox.containerId,\n },\n };\n },\n\n /**\n * Get public URL for a specific port\n *\n * Uses sandbox.exposePort() to dynamically expose a port.\n */\n getUrl: async (sandbox: SandboxInstance, options: { port: number; protocol?: string }): Promise<string> => {\n try {\n return await sandbox.exposePort(options.port);\n } catch (error) {\n throw new Error(\n `Failed to get Beam URL for port ${options.port}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Filesystem operations\n *\n * Most operations use shell commands via runCommand since Beam's file API\n * is local-path oriented (uploadFile/downloadFile require local filesystem paths).\n * readdir uses the native listFiles API since it returns structured data.\n */\n filesystem: {\n readFile: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<string> => {\n const result = await runCommand(sandbox, `cat ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to read file ${path}: ${result.stderr}`);\n }\n return result.stdout;\n },\n\n writeFile: async (sandbox: SandboxInstance, path: string, content: string, runCommand: RunCommandFn): Promise<void> => {\n const b64 = Buffer.from(content).toString('base64');\n const result = await runCommand(sandbox, `echo '${b64}' | base64 -d > ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to write file ${path}: ${result.stderr}`);\n }\n },\n\n mkdir: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `mkdir -p ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to create directory ${path}: ${result.stderr}`);\n }\n },\n\n readdir: async (sandbox: SandboxInstance, path: string, _runCommand: RunCommandFn): Promise<FileEntry[]> => {\n const files = await sandbox.fs.listFiles(path);\n return files.map((file: any) => ({\n name: file.name,\n type: file.isDir ? 'directory' as const : 'file' as const,\n size: Number(file.size) || 0,\n modified: file.modTime ? new Date(file.modTime * 1000) : new Date(),\n }));\n },\n\n exists: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<boolean> => {\n const result = await runCommand(sandbox, `test -f ${shellEscape(path)} || test -d ${shellEscape(path)}`);\n return result.exitCode === 0;\n },\n\n remove: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `rm -rf ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to remove ${path}: ${result.stderr}`);\n }\n },\n },\n\n /**\n * Get the native SandboxInstance for advanced usage\n */\n getInstance: (sandbox: SandboxInstance): SandboxInstance => {\n return sandbox;\n },\n },\n },\n});\n\n// Export Beam sandbox type for explicit typing\nexport type { SandboxInstance as BeamSandboxInstance } from '@beamcloud/beam-js';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,qBAA0D;AAC1D,sBAA+C;AAgC/C,SAAS,kBAAkB,QAA0B;AACnD,0BAAS,QAAQ,OAAO,SAAU,OAAO,YAAY,eAAe,QAAQ,KAAK,cAAe;AAChG,0BAAS,cAAc,OAAO,eAAgB,OAAO,YAAY,eAAe,QAAQ,KAAK,qBAAsB;AACnH,MAAI,OAAO,YAAY;AACrB,4BAAS,aAAa,OAAO;AAAA,EAC/B;AACA,MAAI,OAAO,SAAS;AAClB,IAAC,wBAAiB,UAAU,OAAO;AAAA,EACrC;AACF;AAKA,SAAS,YAAY,KAAqB;AACxC,MAAI,QAAQ,GAAI,QAAO;AACvB,SAAO,IAAI,IAAI,QAAQ,MAAM,OAAO,CAAC;AACvC;AAoEO,IAAM,WAAO,gCAA4C;AAAA,EAC9D,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAMP,QAAQ,OAAO,QAAoB,YAAmC;AACpE,0BAAkB,MAAM;AAExB,YAAI,CAAC,wBAAS,OAAO;AACnB,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,YAAI,CAAC,wBAAS,aAAa;AACzB,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,YAAI;AAEF,gBAAM;AAAA,YACJ,SAAS;AAAA,YACT,SAAS;AAAA,YACT;AAAA,YACA;AAAA,YACA,UAAU;AAAA,YACV,YAAY;AAAA,YACZ,YAAY;AAAA,YACZ,WAAW;AAAA,YACX,WAAW;AAAA,YACX,WAAW;AAAA,YACX,GAAG;AAAA,UACL,IAAI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AA,YACX,MAAM,KAAK,QAAQ,cAAuB;AAAA,YAC1C,MAAM,OAAO,KAAK,IAAI,KAAK;AAAA,YAC3B,UAAU,KAAK,UAAU,IAAI,KAAK,KAAK,UAAU,GAAI,IAAI,oBAAI,KAAK;AAAA,UACpE,EAAE;AAAA,QACJ;AAAA,QAEA,QAAQ,OAAO,SAA0B,MAAc,eAA+C;AACpG,gBAAM,SAAS,MAAM,WAAW,SAAS,WAAW,YAAY,IAAI,CAAC,eAAe,YAAY,IAAI,CAAC,EAAE;AACvG,iBAAO,OAAO,aAAa;AAAA,QAC7B;AAAA,QAEA,QAAQ,OAAO,SAA0B,MAAc,eAA4C;AACjG,gBAAM,SAAS,MAAM,WAAW,SAAS,UAAU,YAAY,IAAI,CAAC,EAAE;AACtE,cAAI,OAAO,aAAa,GAAG;AACzB,kBAAM,IAAI,MAAM,oBAAoB,IAAI,KAAK,OAAO,MAAM,EAAE;AAAA,UAC9D;AAAA,QACF;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAKA,aAAa,CAAC,YAA8C;AAC1D,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":["name"]}
|
package/dist/index.mjs
CHANGED
|
@@ -15,28 +15,6 @@ function shellEscape(arg) {
|
|
|
15
15
|
if (arg === "") return "''";
|
|
16
16
|
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
17
17
|
}
|
|
18
|
-
function isNodeParserFailure(output) {
|
|
19
|
-
const hasSyntaxMarker = output.includes("SyntaxError") || output.includes("Unexpected token") || output.includes("Unexpected identifier") || output.includes("Unexpected end of input") || output.includes("Invalid or unexpected token");
|
|
20
|
-
const hasCompileContext = output.includes("[eval]") || output.includes("makeContextifyScript") || output.includes("compileScript") || output.includes("wrapSafe");
|
|
21
|
-
const hasRuntimeSyntaxSignature = output.includes("at JSON.parse") || output.includes("JSON.parse (<anonymous>)") || output.includes("in JSON at position");
|
|
22
|
-
return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;
|
|
23
|
-
}
|
|
24
|
-
function isPythonParserFailure(output) {
|
|
25
|
-
const hasSyntaxMarker = output.includes("SyntaxError") || output.includes("IndentationError") || output.includes("TabError");
|
|
26
|
-
const hasStringFileContext = output.includes('File "<string>"');
|
|
27
|
-
const hasRuntimeTraceback = output.includes("Traceback (most recent call last)");
|
|
28
|
-
return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;
|
|
29
|
-
}
|
|
30
|
-
function isParserFailure(output, runtime) {
|
|
31
|
-
if (!output.trim()) return false;
|
|
32
|
-
if (runtime === "node") {
|
|
33
|
-
return isNodeParserFailure(output);
|
|
34
|
-
}
|
|
35
|
-
if (runtime === "python") {
|
|
36
|
-
return isPythonParserFailure(output);
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
18
|
var beam = defineProvider({
|
|
41
19
|
name: "beam",
|
|
42
20
|
methods: {
|
|
@@ -59,10 +37,6 @@ var beam = defineProvider({
|
|
|
59
37
|
);
|
|
60
38
|
}
|
|
61
39
|
try {
|
|
62
|
-
if (options?.sandboxId) {
|
|
63
|
-
const instance2 = await Sandbox.connect(options.sandboxId);
|
|
64
|
-
return { sandbox: instance2, sandboxId: instance2.containerId };
|
|
65
|
-
}
|
|
66
40
|
const {
|
|
67
41
|
runtime: optRuntime,
|
|
68
42
|
timeout: optTimeout,
|
|
@@ -74,8 +48,6 @@ var beam = defineProvider({
|
|
|
74
48
|
sandboxId: _sandboxId,
|
|
75
49
|
namespace: _namespace,
|
|
76
50
|
directory: _directory,
|
|
77
|
-
overlays: _overlays,
|
|
78
|
-
servers: _servers,
|
|
79
51
|
...providerOptions
|
|
80
52
|
} = options || {};
|
|
81
53
|
const sandboxConfig = {
|
|
@@ -156,38 +128,6 @@ var beam = defineProvider({
|
|
|
156
128
|
*
|
|
157
129
|
* Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().
|
|
158
130
|
*/
|
|
159
|
-
runCode: async (sandbox, code, runtime) => {
|
|
160
|
-
const effectiveRuntime = runtime || (code.includes("print(") || code.includes("import ") || code.includes("def ") || code.includes("sys.") || code.includes("json.") || code.includes("__") || code.includes('f"') || code.includes("f'") || code.includes("raise ") ? "python" : "node");
|
|
161
|
-
try {
|
|
162
|
-
const command = effectiveRuntime === "python" ? ["python3", "-c", code] : ["node", "-e", code];
|
|
163
|
-
const proc = await sandbox.exec(command);
|
|
164
|
-
await proc.wait();
|
|
165
|
-
const [stdoutStr, stderrStr] = await Promise.all([
|
|
166
|
-
proc.stdout.read(),
|
|
167
|
-
proc.stderr.read()
|
|
168
|
-
]);
|
|
169
|
-
const output = stderrStr ? `${stdoutStr}${stdoutStr && stderrStr ? "\n" : ""}${stderrStr}` : stdoutStr;
|
|
170
|
-
const combinedOutput = `${stdoutStr || ""} ${stderrStr || ""}`;
|
|
171
|
-
if (proc.exitCode !== 0 && isParserFailure(combinedOutput, effectiveRuntime)) {
|
|
172
|
-
throw new Error(`Syntax error: ${(stderrStr || stdoutStr || "").trim()}`);
|
|
173
|
-
}
|
|
174
|
-
if (proc.exitCode !== 0 && !stdoutStr && !stderrStr) {
|
|
175
|
-
throw new Error(`Code execution failed with exit code ${proc.exitCode}`);
|
|
176
|
-
}
|
|
177
|
-
return {
|
|
178
|
-
output,
|
|
179
|
-
exitCode: proc.exitCode,
|
|
180
|
-
language: effectiveRuntime
|
|
181
|
-
};
|
|
182
|
-
} catch (error) {
|
|
183
|
-
if (error instanceof Error && error.message.includes("Syntax error")) {
|
|
184
|
-
throw error;
|
|
185
|
-
}
|
|
186
|
-
throw new Error(
|
|
187
|
-
`Beam execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
131
|
/**
|
|
192
132
|
* Execute a shell command in the sandbox
|
|
193
133
|
*
|
|
@@ -198,7 +138,7 @@ var beam = defineProvider({
|
|
|
198
138
|
try {
|
|
199
139
|
let fullCommand = command;
|
|
200
140
|
if (options?.env && Object.keys(options.env).length > 0) {
|
|
201
|
-
const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${escapeShellArg(v)}"`).join(" ");
|
|
141
|
+
const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${escapeShellArg(String(v))}"`).join(" ");
|
|
202
142
|
fullCommand = `${envPrefix} ${fullCommand}`;
|
|
203
143
|
}
|
|
204
144
|
if (options?.cwd) {
|
|
@@ -233,23 +173,23 @@ var beam = defineProvider({
|
|
|
233
173
|
*/
|
|
234
174
|
getInfo: async (sandbox) => {
|
|
235
175
|
let runtime = "python";
|
|
236
|
-
const
|
|
237
|
-
if (typeof
|
|
238
|
-
const lower =
|
|
176
|
+
const runtimeHint = sandbox;
|
|
177
|
+
if (typeof runtimeHint.runtime === "string") {
|
|
178
|
+
const lower = runtimeHint.runtime.toLowerCase();
|
|
239
179
|
if (lower.includes("node")) {
|
|
240
180
|
runtime = "node";
|
|
241
181
|
} else if (lower.includes("python")) {
|
|
242
182
|
runtime = "python";
|
|
243
183
|
}
|
|
244
|
-
} else if (typeof
|
|
245
|
-
const imageStr =
|
|
184
|
+
} else if (typeof runtimeHint.image === "string") {
|
|
185
|
+
const imageStr = runtimeHint.image.toLowerCase();
|
|
246
186
|
if (imageStr.includes("node")) {
|
|
247
187
|
runtime = "node";
|
|
248
188
|
} else if (imageStr.includes("python")) {
|
|
249
189
|
runtime = "python";
|
|
250
190
|
}
|
|
251
|
-
} else if (typeof
|
|
252
|
-
const imageNameStr =
|
|
191
|
+
} else if (typeof runtimeHint.imageName === "string") {
|
|
192
|
+
const imageNameStr = runtimeHint.imageName.toLowerCase();
|
|
253
193
|
if (imageNameStr.includes("node")) {
|
|
254
194
|
runtime = "node";
|
|
255
195
|
} else if (imageNameStr.includes("python")) {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Beam Provider - Factory-based Implementation\n *\n * Containerized sandbox environments using the Beam platform.\n * Beam provides sandboxes with process management, filesystem access,\n * and port exposure capabilities.\n *\n * Features:\n * - Process execution via sandbox.exec()\n * - Code execution in Python, JavaScript\n * - Shell command execution\n * - Filesystem operations (shell-based + native listFiles)\n * - Dynamic port exposure for accessing sandbox services\n */\n\nimport { Sandbox, SandboxInstance, beamOpts, Image } from '@beamcloud/beam-js';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\nimport type {\n CodeResult,\n CommandResult,\n SandboxInfo,\n Runtime,\n CreateSandboxOptions,\n FileEntry,\n RunCommandOptions,\n} from 'computesdk';\n\n/** Type alias for the runCommand function passed to filesystem methods */\ntype RunCommandFn = (sandbox: SandboxInstance, command: string, options?: RunCommandOptions) => Promise<CommandResult>;\n\n/**\n * Beam-specific configuration options\n */\nexport interface BeamConfig {\n /** Beam API token - if not provided, will fallback to BEAM_TOKEN environment variable */\n token?: string;\n /** Beam workspace ID - if not provided, will fallback to BEAM_WORKSPACE_ID environment variable */\n workspaceId?: string;\n /** Gateway URL for custom/staging environments */\n gatewayUrl?: string;\n /** Request timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Configure the global beamOpts singleton before each SDK call.\n * Beam uses a global config pattern rather than per-call config.\n */\nfunction configureBeamOpts(config: BeamConfig): void {\n beamOpts.token = config.token || (typeof process !== 'undefined' && process.env?.BEAM_TOKEN) || '';\n beamOpts.workspaceId = config.workspaceId || (typeof process !== 'undefined' && process.env?.BEAM_WORKSPACE_ID) || '';\n if (config.gatewayUrl) {\n beamOpts.gatewayUrl = config.gatewayUrl;\n }\n if (config.timeout) {\n (beamOpts as any).timeout = config.timeout;\n }\n}\n\n/**\n * Shell-escape a string using single quotes (POSIX).\n */\nfunction shellEscape(arg: string): string {\n if (arg === '') return \"''\";\n return `'${arg.replace(/'/g, \"'\\\\''\")}'`;\n}\n\n/**\n * Detect Node parser/compile failures while avoiding runtime SyntaxError matches.\n */\nfunction isNodeParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('Unexpected token') ||\n output.includes('Unexpected identifier') ||\n output.includes('Unexpected end of input') ||\n output.includes('Invalid or unexpected token');\n\n const hasCompileContext =\n output.includes('[eval]') ||\n output.includes('makeContextifyScript') ||\n output.includes('compileScript') ||\n output.includes('wrapSafe');\n\n const hasRuntimeSyntaxSignature =\n output.includes('at JSON.parse') ||\n output.includes('JSON.parse (<anonymous>)') ||\n output.includes('in JSON at position');\n\n return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;\n}\n\n/**\n * Detect Python parser failures (syntax/indentation/tab) emitted by python -c.\n */\nfunction isPythonParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('IndentationError') ||\n output.includes('TabError');\n\n const hasStringFileContext = output.includes('File \"<string>\"');\n const hasRuntimeTraceback = output.includes('Traceback (most recent call last)');\n\n return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;\n}\n\n/**\n * Detect parser/compile failures that should throw instead of returning CodeResult.\n */\nfunction isParserFailure(output: string, runtime: Runtime): boolean {\n if (!output.trim()) return false;\n\n if (runtime === 'node') {\n return isNodeParserFailure(output);\n }\n\n if (runtime === 'python') {\n return isPythonParserFailure(output);\n }\n\n return false;\n}\n\n/**\n * Create a Beam provider instance using the factory pattern\n *\n * Beam provides containerized sandbox environments with:\n * - Process management (exec, runCode)\n * - Filesystem access\n * - Dynamic port exposure\n * - Snapshot and image creation capabilities\n */\nexport const beam = defineProvider<SandboxInstance, BeamConfig>({\n name: 'beam',\n methods: {\n sandbox: {\n /**\n * Create a new Beam sandbox\n *\n * Uses Sandbox class from @beamcloud/beam-js to provision a container.\n */\n create: async (config: BeamConfig, options?: CreateSandboxOptions) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n throw new Error(\n `Missing Beam token. Provide 'token' in config or set BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n\n if (!beamOpts.workspaceId) {\n throw new Error(\n `Missing Beam workspace ID. Provide 'workspaceId' in config or set BEAM_WORKSPACE_ID environment variable.`\n );\n }\n try {\n if (options?.sandboxId) {\n const instance = await Sandbox.connect(options.sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n }\n\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: optRuntime,\n timeout: optTimeout,\n envs,\n name,\n metadata: _metadata,\n templateId: _templateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n overlays: _overlays,\n servers: _servers,\n ...providerOptions\n } = options || {};\n\n const sandboxConfig: any = {\n name: name || `computesdk-${Date.now()}`,\n keepWarmSeconds: 300,\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.timeout takes precedence over config.timeout\n const timeout = optTimeout ?? config.timeout;\n if (timeout) {\n sandboxConfig.keepWarmSeconds = Math.ceil(timeout / 1000);\n }\n\n if (optRuntime === 'node') {\n sandboxConfig.image = Image.fromRegistry('node:20-slim');\n }\n\n if (envs) {\n sandboxConfig.envVars = Object.entries(envs).map(([name, value]) => `${name}=${value}`);\n }\n\n const sandbox = new Sandbox(sandboxConfig);\n const instance = await sandbox.create();\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('401')) {\n throw new Error(\n `Beam authentication failed. Please check your BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n }\n throw new Error(\n `Failed to create Beam sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Connect to an existing Beam sandbox by ID\n *\n * Uses Sandbox.connect() to reconnect to a running sandbox.\n */\n getById: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return null;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch {\n return null;\n }\n },\n\n /**\n * List all active Beam sandboxes\n *\n * Beam SDK has no sandbox list API; returns empty array.\n */\n list: async (_config: BeamConfig) => {\n return [];\n },\n\n /**\n * Destroy a Beam sandbox\n *\n * Connects to the sandbox and terminates it.\n */\n destroy: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n await instance.terminate();\n } catch {\n // Sandbox might already be destroyed\n }\n },\n\n /**\n * Execute code in the sandbox\n *\n * Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().\n */\n runCode: async (sandbox: SandboxInstance, code: string, runtime?: Runtime): Promise<CodeResult> => {\n const effectiveRuntime = runtime || (\n code.includes('print(') ||\n code.includes('import ') ||\n code.includes('def ') ||\n code.includes('sys.') ||\n code.includes('json.') ||\n code.includes('__') ||\n code.includes('f\"') ||\n code.includes(\"f'\") ||\n code.includes('raise ')\n ? 'python'\n : 'node'\n );\n\n try {\n const command = effectiveRuntime === 'python'\n ? ['python3', '-c', code]\n : ['node', '-e', code];\n\n const proc = await sandbox.exec(command);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n const output = stderrStr\n ? `${stdoutStr}${stdoutStr && stderrStr ? '\\n' : ''}${stderrStr}`\n : stdoutStr;\n\n const combinedOutput = `${stdoutStr || ''} ${stderrStr || ''}`;\n\n if (proc.exitCode !== 0 && isParserFailure(combinedOutput, effectiveRuntime)) {\n throw new Error(`Syntax error: ${(stderrStr || stdoutStr || '').trim()}`);\n }\n\n if (proc.exitCode !== 0 && !stdoutStr && !stderrStr) {\n throw new Error(`Code execution failed with exit code ${proc.exitCode}`);\n }\n\n return {\n output,\n exitCode: proc.exitCode,\n language: effectiveRuntime,\n };\n } catch (error) {\n if (error instanceof Error && error.message.includes('Syntax error')) {\n throw error;\n }\n throw new Error(\n `Beam execution failed: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Execute a shell command in the sandbox\n *\n * Uses sandbox.exec() with shell command string.\n */\n runCommand: async (sandbox: SandboxInstance, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n let fullCommand = command;\n\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]: [string, string]) => `${k}=\"${escapeShellArg(v)}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Wrap in sh -c to get shell parsing (pipes, redirects, etc.)\n const proc = await sandbox.exec(['sh', '-c', fullCommand]);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n return {\n stdout: stdoutStr || '',\n stderr: stderrStr || '',\n exitCode: proc.exitCode || 0,\n durationMs: Date.now() - startTime,\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime,\n };\n }\n },\n\n /**\n * Get sandbox information\n */\n getInfo: async (sandbox: SandboxInstance): Promise<SandboxInfo> => {\n // Derive runtime from sandbox instance if available; otherwise default to 'python'\n let runtime: Runtime = 'python';\n const anySandbox = sandbox as any;\n\n if (typeof anySandbox.runtime === 'string') {\n const lower = anySandbox.runtime.toLowerCase();\n if (lower.includes('node')) {\n runtime = 'node';\n } else if (lower.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof anySandbox.image === 'string') {\n const imageStr = anySandbox.image.toLowerCase();\n if (imageStr.includes('node')) {\n runtime = 'node';\n } else if (imageStr.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof anySandbox.imageName === 'string') {\n const imageNameStr = anySandbox.imageName.toLowerCase();\n if (imageNameStr.includes('node')) {\n runtime = 'node';\n } else if (imageNameStr.includes('python')) {\n runtime = 'python';\n }\n }\n\n return {\n id: sandbox.containerId,\n provider: 'beam',\n runtime,\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n containerId: sandbox.containerId,\n },\n };\n },\n\n /**\n * Get public URL for a specific port\n *\n * Uses sandbox.exposePort() to dynamically expose a port.\n */\n getUrl: async (sandbox: SandboxInstance, options: { port: number; protocol?: string }): Promise<string> => {\n try {\n return await sandbox.exposePort(options.port);\n } catch (error) {\n throw new Error(\n `Failed to get Beam URL for port ${options.port}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Filesystem operations\n *\n * Most operations use shell commands via runCommand since Beam's file API\n * is local-path oriented (uploadFile/downloadFile require local filesystem paths).\n * readdir uses the native listFiles API since it returns structured data.\n */\n filesystem: {\n readFile: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<string> => {\n const result = await runCommand(sandbox, `cat ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to read file ${path}: ${result.stderr}`);\n }\n return result.stdout;\n },\n\n writeFile: async (sandbox: SandboxInstance, path: string, content: string, runCommand: RunCommandFn): Promise<void> => {\n const b64 = Buffer.from(content).toString('base64');\n const result = await runCommand(sandbox, `echo '${b64}' | base64 -d > ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to write file ${path}: ${result.stderr}`);\n }\n },\n\n mkdir: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `mkdir -p ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to create directory ${path}: ${result.stderr}`);\n }\n },\n\n readdir: async (sandbox: SandboxInstance, path: string, _runCommand: RunCommandFn): Promise<FileEntry[]> => {\n const files = await sandbox.fs.listFiles(path);\n return files.map((file: any) => ({\n name: file.name,\n type: file.isDir ? 'directory' as const : 'file' as const,\n size: Number(file.size) || 0,\n modified: file.modTime ? new Date(file.modTime * 1000) : new Date(),\n }));\n },\n\n exists: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<boolean> => {\n const result = await runCommand(sandbox, `test -f ${shellEscape(path)} || test -d ${shellEscape(path)}`);\n return result.exitCode === 0;\n },\n\n remove: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `rm -rf ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to remove ${path}: ${result.stderr}`);\n }\n },\n },\n\n /**\n * Get the native SandboxInstance for advanced usage\n */\n getInstance: (sandbox: SandboxInstance): SandboxInstance => {\n return sandbox;\n },\n },\n },\n});\n\n// Export Beam sandbox type for explicit typing\nexport type { SandboxInstance as BeamSandboxInstance } from '@beamcloud/beam-js';\n"],"mappings":";AAeA,SAAS,SAA0B,UAAU,aAAa;AAC1D,SAAS,gBAAgB,sBAAsB;AAgC/C,SAAS,kBAAkB,QAA0B;AACnD,WAAS,QAAQ,OAAO,SAAU,OAAO,YAAY,eAAe,QAAQ,KAAK,cAAe;AAChG,WAAS,cAAc,OAAO,eAAgB,OAAO,YAAY,eAAe,QAAQ,KAAK,qBAAsB;AACnH,MAAI,OAAO,YAAY;AACrB,aAAS,aAAa,OAAO;AAAA,EAC/B;AACA,MAAI,OAAO,SAAS;AAClB,IAAC,SAAiB,UAAU,OAAO;AAAA,EACrC;AACF;AAKA,SAAS,YAAY,KAAqB;AACxC,MAAI,QAAQ,GAAI,QAAO;AACvB,SAAO,IAAI,IAAI,QAAQ,MAAM,OAAO,CAAC;AACvC;AAKA,SAAS,oBAAoB,QAAyB;AACpD,QAAM,kBACJ,OAAO,SAAS,aAAa,KAC7B,OAAO,SAAS,kBAAkB,KAClC,OAAO,SAAS,uBAAuB,KACvC,OAAO,SAAS,yBAAyB,KACzC,OAAO,SAAS,6BAA6B;AAE/C,QAAM,oBACJ,OAAO,SAAS,QAAQ,KACxB,OAAO,SAAS,sBAAsB,KACtC,OAAO,SAAS,eAAe,KAC/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ACF;AAAA,QAEA,SAAS,OAAO,SAA0B,MAAc,gBAAoD;AAC1G,gBAAM,QAAQ,MAAM,QAAQ,GAAG,UAAU,IAAI;AAC7C,iBAAO,MAAM,IAAI,CAAC,UAAe;AAAA,YAC/B,MAAM,KAAK;AAAA,YACX,MAAM,KAAK,QAAQ,cAAuB;AAAA,YAC1C,MAAM,OAAO,KAAK,IAAI,KAAK;AAAA,YAC3B,UAAU,KAAK,UAAU,IAAI,KAAK,KAAK,UAAU,GAAI,IAAI,oBAAI,KAAK;AAAA,UACpE,EAAE;AAAA,QACJ;AAAA,QAEA,QAAQ,OAAO,SAA0B,MAAc,eAA+C;AACpG,gBAAM,SAAS,MAAM,WAAW,SAAS,WAAW,YAAY,IAAI,CAAC,eAAe,YAAY,IAAI,CAAC,EAAE;AACvG,iBAAO,OAAO,aAAa;AAAA,QAC7B;AAAA,QAEA,QAAQ,OAAO,SAA0B,MAAc,eAA4C;AACjG,gBAAM,SAAS,MAAM,WAAW,SAAS,UAAU,YAAY,IAAI,CAAC,EAAE;AACtE,cAAI,OAAO,aAAa,GAAG;AACzB,kBAAM,IAAI,MAAM,oBAAoB,IAAI,KAAK,OAAO,MAAM,EAAE;AAAA,UAC9D;AAAA,QACF;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAKA,aAAa,CAAC,YAA8C;AAC1D,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":["instance","name"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Beam Provider - Factory-based Implementation\n *\n * Containerized sandbox environments using the Beam platform.\n * Beam provides sandboxes with process management, filesystem access,\n * and port exposure capabilities.\n *\n * Features:\n * - Process execution via sandbox.exec()\n * - Code execution in Python, JavaScript\n * - Shell command execution\n * - Filesystem operations (shell-based + native listFiles)\n * - Dynamic port exposure for accessing sandbox services\n */\n\nimport { Sandbox, SandboxInstance, beamOpts, Image } from '@beamcloud/beam-js';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\nimport type {\n CodeResult,\n CommandResult,\n SandboxInfo,\n Runtime,\n CreateSandboxOptions,\n FileEntry,\n RunCommandOptions,\n} from 'computesdk';\n\n/** Type alias for the runCommand function passed to filesystem methods */\ntype RunCommandFn = (sandbox: SandboxInstance, command: string, options?: RunCommandOptions) => Promise<CommandResult>;\n\n/**\n * Beam-specific configuration options\n */\nexport interface BeamConfig {\n /** Beam API token - if not provided, will fallback to BEAM_TOKEN environment variable */\n token?: string;\n /** Beam workspace ID - if not provided, will fallback to BEAM_WORKSPACE_ID environment variable */\n workspaceId?: string;\n /** Gateway URL for custom/staging environments */\n gatewayUrl?: string;\n /** Request timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Configure the global beamOpts singleton before each SDK call.\n * Beam uses a global config pattern rather than per-call config.\n */\nfunction configureBeamOpts(config: BeamConfig): void {\n beamOpts.token = config.token || (typeof process !== 'undefined' && process.env?.BEAM_TOKEN) || '';\n beamOpts.workspaceId = config.workspaceId || (typeof process !== 'undefined' && process.env?.BEAM_WORKSPACE_ID) || '';\n if (config.gatewayUrl) {\n beamOpts.gatewayUrl = config.gatewayUrl;\n }\n if (config.timeout) {\n (beamOpts as any).timeout = config.timeout;\n }\n}\n\n/**\n * Shell-escape a string using single quotes (POSIX).\n */\nfunction shellEscape(arg: string): string {\n if (arg === '') return \"''\";\n return `'${arg.replace(/'/g, \"'\\\\''\")}'`;\n}\n\n/**\n * Detect Node parser/compile failures while avoiding runtime SyntaxError matches.\n */\nfunction isNodeParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('Unexpected token') ||\n output.includes('Unexpected identifier') ||\n output.includes('Unexpected end of input') ||\n output.includes('Invalid or unexpected token');\n\n const hasCompileContext =\n output.includes('[eval]') ||\n output.includes('makeContextifyScript') ||\n output.includes('compileScript') ||\n output.includes('wrapSafe');\n\n const hasRuntimeSyntaxSignature =\n output.includes('at JSON.parse') ||\n output.includes('JSON.parse (<anonymous>)') ||\n output.includes('in JSON at position');\n\n return hasSyntaxMarker && hasCompileContext && !hasRuntimeSyntaxSignature;\n}\n\n/**\n * Detect Python parser failures (syntax/indentation/tab) emitted by python -c.\n */\nfunction isPythonParserFailure(output: string): boolean {\n const hasSyntaxMarker =\n output.includes('SyntaxError') ||\n output.includes('IndentationError') ||\n output.includes('TabError');\n\n const hasStringFileContext = output.includes('File \"<string>\"');\n const hasRuntimeTraceback = output.includes('Traceback (most recent call last)');\n\n return hasSyntaxMarker && hasStringFileContext && !hasRuntimeTraceback;\n}\n\n/**\n * Detect parser/compile failures that should throw instead of returning CodeResult.\n */\nfunction isParserFailure(output: string, runtime: Runtime): boolean {\n if (!output.trim()) return false;\n\n if (runtime === 'node') {\n return isNodeParserFailure(output);\n }\n\n if (runtime === 'python') {\n return isPythonParserFailure(output);\n }\n\n return false;\n}\n\n/**\n * Create a Beam provider instance using the factory pattern\n *\n * Beam provides containerized sandbox environments with:\n * - Process management (exec, runCode)\n * - Filesystem access\n * - Dynamic port exposure\n * - Snapshot and image creation capabilities\n */\nexport const beam = defineProvider<SandboxInstance, BeamConfig>({\n name: 'beam',\n methods: {\n sandbox: {\n /**\n * Create a new Beam sandbox\n *\n * Uses Sandbox class from @beamcloud/beam-js to provision a container.\n */\n create: async (config: BeamConfig, options?: CreateSandboxOptions) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n throw new Error(\n `Missing Beam token. Provide 'token' in config or set BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n\n if (!beamOpts.workspaceId) {\n throw new Error(\n `Missing Beam workspace ID. Provide 'workspaceId' in config or set BEAM_WORKSPACE_ID environment variable.`\n );\n }\n\n try {\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: optRuntime,\n timeout: optTimeout,\n envs,\n name,\n metadata: _metadata,\n templateId: _templateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n ...providerOptions\n } = options || {};\n\n const sandboxConfig: any = {\n name: name || `computesdk-${Date.now()}`,\n keepWarmSeconds: 300,\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.timeout takes precedence over config.timeout\n const timeout = optTimeout ?? config.timeout;\n if (timeout) {\n sandboxConfig.keepWarmSeconds = Math.ceil(timeout / 1000);\n }\n\n if (optRuntime === 'node') {\n sandboxConfig.image = Image.fromRegistry('node:20-slim');\n }\n\n if (envs) {\n sandboxConfig.envVars = Object.entries(envs).map(([name, value]) => `${name}=${value}`);\n }\n\n const sandbox = new Sandbox(sandboxConfig);\n const instance = await sandbox.create();\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('401')) {\n throw new Error(\n `Beam authentication failed. Please check your BEAM_TOKEN environment variable. Get your token from https://app.beam.cloud`\n );\n }\n }\n throw new Error(\n `Failed to create Beam sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Connect to an existing Beam sandbox by ID\n *\n * Uses Sandbox.connect() to reconnect to a running sandbox.\n */\n getById: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return null;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n return { sandbox: instance, sandboxId: instance.containerId };\n } catch {\n return null;\n }\n },\n\n /**\n * List all active Beam sandboxes\n *\n * Beam SDK has no sandbox list API; returns empty array.\n */\n list: async (_config: BeamConfig) => {\n return [];\n },\n\n /**\n * Destroy a Beam sandbox\n *\n * Connects to the sandbox and terminates it.\n */\n destroy: async (config: BeamConfig, sandboxId: string) => {\n configureBeamOpts(config);\n\n if (!beamOpts.token) {\n return;\n }\n\n try {\n const instance = await Sandbox.connect(sandboxId);\n await instance.terminate();\n } catch {\n // Sandbox might already be destroyed\n }\n },\n\n /**\n * Execute code in the sandbox\n *\n * Auto-detects runtime (Python vs Node.js) and executes via sandbox.exec().\n */\n\n /**\n * Execute a shell command in the sandbox\n *\n * Uses sandbox.exec() with shell command string.\n */\n runCommand: async (sandbox: SandboxInstance, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n let fullCommand = command;\n\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]) => `${k}=\"${escapeShellArg(String(v))}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Wrap in sh -c to get shell parsing (pipes, redirects, etc.)\n const proc = await sandbox.exec(['sh', '-c', fullCommand]);\n await proc.wait();\n const [stdoutStr, stderrStr] = await Promise.all([\n proc.stdout.read(),\n proc.stderr.read(),\n ]);\n\n return {\n stdout: stdoutStr || '',\n stderr: stderrStr || '',\n exitCode: proc.exitCode || 0,\n durationMs: Date.now() - startTime,\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime,\n };\n }\n },\n\n /**\n * Get sandbox information\n */\n getInfo: async (sandbox: SandboxInstance): Promise<SandboxInfo> => {\n // Derive runtime from sandbox instance if available; otherwise default to 'python'\n let runtime: Runtime = 'python';\n const runtimeHint = sandbox as SandboxInstance & {\n runtime?: unknown;\n image?: unknown;\n imageName?: unknown;\n };\n\n if (typeof runtimeHint.runtime === 'string') {\n const lower = runtimeHint.runtime.toLowerCase();\n if (lower.includes('node')) {\n runtime = 'node';\n } else if (lower.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof runtimeHint.image === 'string') {\n const imageStr = runtimeHint.image.toLowerCase();\n if (imageStr.includes('node')) {\n runtime = 'node';\n } else if (imageStr.includes('python')) {\n runtime = 'python';\n }\n } else if (typeof runtimeHint.imageName === 'string') {\n const imageNameStr = runtimeHint.imageName.toLowerCase();\n if (imageNameStr.includes('node')) {\n runtime = 'node';\n } else if (imageNameStr.includes('python')) {\n runtime = 'python';\n }\n }\n\n return {\n id: sandbox.containerId,\n provider: 'beam',\n runtime,\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n containerId: sandbox.containerId,\n },\n };\n },\n\n /**\n * Get public URL for a specific port\n *\n * Uses sandbox.exposePort() to dynamically expose a port.\n */\n getUrl: async (sandbox: SandboxInstance, options: { port: number; protocol?: string }): Promise<string> => {\n try {\n return await sandbox.exposePort(options.port);\n } catch (error) {\n throw new Error(\n `Failed to get Beam URL for port ${options.port}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n /**\n * Filesystem operations\n *\n * Most operations use shell commands via runCommand since Beam's file API\n * is local-path oriented (uploadFile/downloadFile require local filesystem paths).\n * readdir uses the native listFiles API since it returns structured data.\n */\n filesystem: {\n readFile: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<string> => {\n const result = await runCommand(sandbox, `cat ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to read file ${path}: ${result.stderr}`);\n }\n return result.stdout;\n },\n\n writeFile: async (sandbox: SandboxInstance, path: string, content: string, runCommand: RunCommandFn): Promise<void> => {\n const b64 = Buffer.from(content).toString('base64');\n const result = await runCommand(sandbox, `echo '${b64}' | base64 -d > ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to write file ${path}: ${result.stderr}`);\n }\n },\n\n mkdir: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `mkdir -p ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to create directory ${path}: ${result.stderr}`);\n }\n },\n\n readdir: async (sandbox: SandboxInstance, path: string, _runCommand: RunCommandFn): Promise<FileEntry[]> => {\n const files = await sandbox.fs.listFiles(path);\n return files.map((file: any) => ({\n name: file.name,\n type: file.isDir ? 'directory' as const : 'file' as const,\n size: Number(file.size) || 0,\n modified: file.modTime ? new Date(file.modTime * 1000) : new Date(),\n }));\n },\n\n exists: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<boolean> => {\n const result = await runCommand(sandbox, `test -f ${shellEscape(path)} || test -d ${shellEscape(path)}`);\n return result.exitCode === 0;\n },\n\n remove: async (sandbox: SandboxInstance, path: string, runCommand: RunCommandFn): Promise<void> => {\n const result = await runCommand(sandbox, `rm -rf ${shellEscape(path)}`);\n if (result.exitCode !== 0) {\n throw new Error(`Failed to remove ${path}: ${result.stderr}`);\n }\n },\n },\n\n /**\n * Get the native SandboxInstance for advanced usage\n */\n getInstance: (sandbox: SandboxInstance): SandboxInstance => {\n return sandbox;\n },\n },\n },\n});\n\n// Export Beam sandbox type for explicit typing\nexport type { SandboxInstance as BeamSandboxInstance } from '@beamcloud/beam-js';\n"],"mappings":";AAeA,SAAS,SAA0B,UAAU,aAAa;AAC1D,SAAS,gBAAgB,sBAAsB;AAgC/C,SAAS,kBAAkB,QAA0B;AACnD,WAAS,QAAQ,OAAO,SAAU,OAAO,YAAY,eAAe,QAAQ,KAAK,cAAe;AAChG,WAAS,cAAc,OAAO,eAAgB,OAAO,YAAY,eAAe,QAAQ,KAAK,qBAAsB;AACnH,MAAI,OAAO,YAAY;AACrB,aAAS,aAAa,OAAO;AAAA,EAC/B;AACA,MAAI,OAAO,SAAS;AAClB,IAAC,SAAiB,UAAU,OAAO;AAAA,EACrC;AACF;AAKA,SAAS,YAAY,KAAqB;AACxC,MAAI,QAAQ,GAAI,QAAO;AACvB,SAAO,IAAI,IAAI,QAAQ,MAAM,OAAO,CAAC;AACvC;AAoEO,IAAM,OAAO,eAA4C;AAAA,EAC9D,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAMP,QAAQ,OAAO,QAAoB,YAAmC;AACpE,0BAAkB,MAAM;AAExB,YAAI,CAAC,SAAS,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SDK - containerized sandbox environments with process management and filesystem access",
|
|
5
5
|
"author": "ComputeSDK",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@beamcloud/beam-js": "1.0.0-rc.30",
|
|
22
|
-
"computesdk": "
|
|
23
|
-
"@computesdk/provider": "1.
|
|
22
|
+
"computesdk": "3.0.0",
|
|
23
|
+
"@computesdk/provider": "1.4.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/node": "^20.0.0",
|