@mastra/cloudflare-sandbox 0.5.0-alpha.0 → 0.5.1-alpha.0
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.cjs +1 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/sandbox.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let crypto = require("crypto");
|
|
3
2
|
let path = require("path");
|
|
4
3
|
let _mastra_core_workspace = require("@mastra/core/workspace");
|
|
5
4
|
//#region src/bridge-client.ts
|
|
@@ -305,7 +304,7 @@ var CloudflareSandbox = class extends _mastra_core_workspace.MastraSandbox {
|
|
|
305
304
|
...options,
|
|
306
305
|
name
|
|
307
306
|
});
|
|
308
|
-
this.id = options.id ?? `cloudflare-sandbox-${
|
|
307
|
+
this.id = options.id ?? `cloudflare-sandbox-${globalThis.crypto.randomUUID()}`;
|
|
309
308
|
this.name = name;
|
|
310
309
|
this.sandboxId = options.sandboxId;
|
|
311
310
|
this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["posix","path","MastraSandbox"],"sources":["../src/bridge-client.ts","../src/sandbox.ts"],"sourcesContent":["export interface CloudflareSandboxBridgeClientOptions {\n baseUrl: string;\n apiToken?: string;\n fetch?: typeof globalThis.fetch;\n}\n\n/** Terminal and streaming events emitted by `POST /v1/sandbox/:id/exec`. */\nexport type CloudflareCommandEvent =\n | { type: 'stdout'; data: Uint8Array }\n | { type: 'stderr'; data: Uint8Array }\n | { type: 'exit'; exitCode: number }\n | { type: 'error'; message: string; code?: string };\n\nexport interface CloudflareExecRequest {\n /** Command and arguments. The bridge applies ANSI-C quoting to each element. */\n argv: string[];\n timeoutMs?: number;\n cwd?: string;\n}\n\nexport interface CloudflarePersistWorkspaceOptions {\n /** Relative paths (under /workspace) to exclude from the archive. */\n excludes?: string[];\n}\n\nexport interface CloudflareMountBucketCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface CloudflareMountBucketOptions {\n /** S3-compatible endpoint, e.g. `https://<account>.r2.cloudflarestorage.com`. */\n endpoint?: string;\n /** Mount the bucket read-only. */\n readOnly?: boolean;\n /** Only expose objects under this bucket prefix at the mount point. */\n prefix?: string;\n /** Storage provider hint, e.g. `r2`. */\n provider?: string;\n /** Explicit credentials; omitted when the Worker resolves them from secrets. */\n credentials?: CloudflareMountBucketCredentials;\n}\n\nexport interface CloudflareMountBucketRequest {\n /** Bucket name, e.g. `my-r2-bucket`. */\n bucket: string;\n /** Local filesystem path to mount at, e.g. `/mnt/data`. */\n mountPath: string;\n options?: CloudflareMountBucketOptions;\n}\n\nexport interface CloudflareCreateSessionRequest {\n /** Working directory the session starts in. */\n cwd?: string;\n /** Environment variables seeded into the session. */\n env?: Record<string, string>;\n /** Caller-chosen session id; must match `^[a-zA-Z0-9._-]{1,128}$`. Generated when omitted. */\n sessionId?: string;\n}\n\nexport interface CloudflareSession {\n id: string;\n}\n\nexport class CloudflareSandboxBridgeError extends Error {\n readonly status: number;\n readonly body: string;\n\n constructor(status: number, body: string) {\n super(`Cloudflare Sandbox Bridge request failed (${status}): ${body || 'empty response'}`);\n this.name = 'CloudflareSandboxBridgeError';\n this.status = status;\n this.body = body;\n }\n}\n\nfunction stripTrailingSlashes(url: string): string {\n let end = url.length;\n while (end > 0 && url[end - 1] === '/') end--;\n return url.slice(0, end);\n}\n\n/** Encodes an absolute sandbox path for the `/file/*` route, which omits the leading slash. */\nfunction encodeFilePath(absolutePath: string): string {\n let start = 0;\n while (start < absolutePath.length && absolutePath[start] === '/') start++;\n return absolutePath\n .slice(start)\n .split('/')\n .map(segment => encodeURIComponent(segment))\n .join('/');\n}\n\n/**\n * Client for the Cloudflare Sandbox Bridge Worker.\n *\n * @see https://developers.cloudflare.com/sandbox/bridge/http-api/\n */\nexport class CloudflareSandboxBridgeClient {\n readonly baseUrl: string;\n private readonly apiToken?: string;\n private readonly fetchImpl: typeof globalThis.fetch;\n\n constructor(options: CloudflareSandboxBridgeClientOptions) {\n this.baseUrl = stripTrailingSlashes(options.baseUrl);\n this.apiToken = options.apiToken;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n /** `POST /v1/sandbox` */\n async createSandbox(): Promise<string> {\n const created = await this.request<{ id: string }>('/v1/sandbox', { method: 'POST' });\n return created.id;\n }\n\n /** `GET /v1/sandbox/:id/running` */\n async isRunning(id: string): Promise<boolean> {\n const status = await this.request<{ running: boolean }>(`/v1/sandbox/${encodeURIComponent(id)}/running`, {});\n return status.running === true;\n }\n\n /** `DELETE /v1/sandbox/:id` */\n async deleteSandbox(id: string): Promise<void> {\n await this.request(`/v1/sandbox/${encodeURIComponent(id)}`, { method: 'DELETE' }, true);\n }\n\n /** `PUT /v1/sandbox/:id/file/*` — one file per request, raw bytes as the body. */\n async writeFile(id: string, absolutePath: string, content: Uint8Array | string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`,\n {\n method: 'PUT',\n body: content as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `GET /v1/sandbox/:id/file/*` — reads one file, returning its raw bytes. */\n async readFile(id: string, absolutePath: string): Promise<Uint8Array> {\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`, {});\n }\n\n /** `GET /v1/sandbox/:id/persist` — archives `/workspace`, returning raw tar bytes. */\n async persistWorkspace(id: string, options: CloudflarePersistWorkspaceOptions = {}): Promise<Uint8Array> {\n const query = options.excludes?.length ? `?excludes=${encodeURIComponent(options.excludes.join(','))}` : '';\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/persist${query}`, {});\n }\n\n /** `POST /v1/sandbox/:id/hydrate` — restores `/workspace` from a raw tar payload. */\n async hydrateWorkspace(id: string, tar: Uint8Array): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/hydrate`,\n {\n method: 'POST',\n body: tar as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/mount` — mounts an S3-compatible bucket as a local directory. */\n async mountBucket(id: string, request: CloudflareMountBucketRequest): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/mount`,\n {\n method: 'POST',\n body: JSON.stringify(request),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/unmount` — unmounts a previously mounted bucket. */\n async unmountBucket(id: string, mountPath: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/unmount`,\n {\n method: 'POST',\n body: JSON.stringify({ mountPath }),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/session` — creates an execution session, returning its id. */\n async createSession(id: string, request: CloudflareCreateSessionRequest = {}): Promise<CloudflareSession> {\n const body: Record<string, unknown> = {};\n if (request.cwd !== undefined) body.cwd = request.cwd;\n if (request.env !== undefined) body.env = request.env;\n if (request.sessionId !== undefined) body.id = request.sessionId;\n return this.request<CloudflareSession>(`/v1/sandbox/${encodeURIComponent(id)}/session`, {\n method: 'POST',\n body: JSON.stringify(body),\n headers: { 'content-type': 'application/json' },\n });\n }\n\n /** `DELETE /v1/sandbox/:id/session/:sessionId` — tears down an execution session. */\n async deleteSession(id: string, sessionId: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/session/${encodeURIComponent(sessionId)}`,\n { method: 'DELETE' },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/exec` — streams SSE events until `exit` or `error`. */\n async exec(\n id: string,\n request: CloudflareExecRequest,\n options: {\n signal?: AbortSignal;\n onEvent: (event: CloudflareCommandEvent) => void;\n },\n ): Promise<void> {\n const response = await this.fetchImpl(`${this.baseUrl}/v1/sandbox/${encodeURIComponent(id)}/exec`, {\n method: 'POST',\n headers: { ...this.headers(), 'content-type': 'application/json', accept: 'text/event-stream' },\n body: JSON.stringify({\n argv: request.argv,\n ...(request.timeoutMs === undefined ? {} : { timeout_ms: request.timeoutMs }),\n ...(request.cwd === undefined ? {} : { cwd: request.cwd }),\n }),\n signal: options.signal,\n });\n\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (!response.body) {\n throw new Error('Cloudflare Sandbox Bridge returned an empty command stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n buffer += decoder.decode(value, { stream: !done }).replace(/\\r\\n/g, '\\n');\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary !== -1) {\n this.emitBlock(buffer.slice(0, boundary), options.onEvent);\n buffer = buffer.slice(boundary + 2);\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n if (buffer.trim()) this.emitBlock(buffer, options.onEvent);\n }\n\n private emitBlock(block: string, onEvent: (event: CloudflareCommandEvent) => void): void {\n let eventName: string | undefined;\n const dataLines: string[] = [];\n for (const line of block.split('\\n')) {\n if (line.startsWith('event:')) eventName = line.slice(6).trim();\n else if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));\n }\n const data = dataLines.join('\\n');\n if (!eventName || !data) return;\n\n switch (eventName) {\n case 'stdout':\n case 'stderr':\n onEvent({ type: eventName, data: base64ToBytes(data) });\n return;\n case 'exit': {\n const parsed = safeJsonParse(data);\n onEvent({ type: 'exit', exitCode: typeof parsed?.exit_code === 'number' ? parsed.exit_code : 0 });\n return;\n }\n case 'error': {\n const parsed = safeJsonParse(data);\n onEvent({\n type: 'error',\n message: typeof parsed?.error === 'string' ? parsed.error : data,\n code: typeof parsed?.code === 'string' ? parsed.code : undefined,\n });\n return;\n }\n default:\n return;\n }\n }\n\n private headers(): Record<string, string> {\n return this.apiToken ? { authorization: `Bearer ${this.apiToken}` } : {};\n }\n\n private async request<T>(path: string, init: RequestInit, allowEmpty = false): Promise<T> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (allowEmpty || response.status === 204) return undefined as T;\n return response.json() as Promise<T>;\n }\n\n private async requestBytes(path: string, init: RequestInit): Promise<Uint8Array> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n return new Uint8Array(await response.arrayBuffer());\n }\n}\n\nfunction base64ToBytes(value: string): Uint8Array {\n return new Uint8Array(Buffer.from(value, 'base64'));\n}\n\nfunction safeJsonParse(value: string): Record<string, unknown> | undefined {\n try {\n return JSON.parse(value) as Record<string, unknown>;\n } catch {\n return undefined;\n }\n}\n","import { randomUUID } from 'node:crypto';\nimport { posix } from 'node:path';\nimport type {\n CommandResult,\n ExecuteCommandOptions,\n FilesystemMountConfig,\n MastraSandboxOptions,\n MountManager,\n MountResult,\n ProviderStatus,\n SandboxFileInput,\n SandboxInfo,\n WorkspaceFilesystem,\n} from '@mastra/core/workspace';\nimport { MastraSandbox, assertModesUnsupported } from '@mastra/core/workspace';\nimport {\n CloudflareSandboxBridgeClient,\n type CloudflareMountBucketRequest,\n type CloudflarePersistWorkspaceOptions,\n type CloudflareSandboxBridgeClientOptions,\n} from './bridge-client';\n\nconst DEFAULT_COMMAND_TIMEOUT_MS = 300_000;\nconst WORKSPACE_ROOT = '/workspace';\n\ntype InstructionsOption = string | ((options: { defaultInstructions: string }) => string);\ntype BridgeClient = Pick<\n CloudflareSandboxBridgeClient,\n | 'createSandbox'\n | 'isRunning'\n | 'deleteSandbox'\n | 'writeFile'\n | 'readFile'\n | 'persistWorkspace'\n | 'hydrateWorkspace'\n | 'mountBucket'\n | 'unmountBucket'\n | 'exec'\n>;\n\n/**\n * Mount config accepted by the Cloudflare bridge: any S3-compatible bucket\n * (R2, S3, MinIO, ...) as produced by `S3Filesystem.getMountConfig()`.\n * Declared structurally so this package does not depend on `@mastra/s3`.\n */\ninterface S3CompatibleMountConfig extends FilesystemMountConfig {\n type: 's3';\n bucket: string;\n region?: string;\n endpoint?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n sessionToken?: string;\n prefix?: string;\n readOnly?: boolean;\n}\n\n/** Allowlist pattern for mount paths, matching the other remote sandbox providers. */\nconst SAFE_MOUNT_PATH = /^\\/[a-zA-Z0-9_.\\-/]+$/;\n\nfunction validateMountPath(mountPath: string): void {\n if (!SAFE_MOUNT_PATH.test(mountPath)) {\n throw new Error(\n `Invalid mount path: ${mountPath}. Must be an absolute path with alphanumeric, dash, dot, underscore, or slash characters only.`,\n );\n }\n}\n\n/**\n * Translates a Workspace mount config into the bridge's mount request, or\n * explains why the bridge cannot serve it. The bridge mounts S3-compatible\n * buckets with s3fs; GCS and Azure mount configs have no equivalent route.\n */\nfunction toMountRequest(\n config: FilesystemMountConfig,\n mountPath: string,\n): { request: CloudflareMountBucketRequest } | { error: string } {\n if (config.type !== 's3') {\n return { error: `Cloudflare Sandbox can only mount S3-compatible buckets; got mount type \"${config.type}\"` };\n }\n const s3 = config as S3CompatibleMountConfig;\n if (s3.sessionToken) {\n return { error: 'Cloudflare Sandbox bucket mounts do not support temporary credentials (sessionToken)' };\n }\n if (Boolean(s3.accessKeyId) !== Boolean(s3.secretAccessKey)) {\n return { error: 'Cloudflare Sandbox bucket mounts need both accessKeyId and secretAccessKey, or neither' };\n }\n // The bridge treats a request with no endpoint as an R2 binding mount, so a\n // region-only AWS filesystem must resolve to an explicit S3 endpoint.\n const endpoint = s3.endpoint ?? (s3.region ? `https://s3.${s3.region}.amazonaws.com` : undefined);\n // The bridge requires the prefix to start with `/`; S3Filesystem emits `dir/`.\n const prefix = s3.prefix ? (s3.prefix.startsWith('/') ? s3.prefix : `/${s3.prefix}`) : undefined;\n return {\n request: {\n bucket: s3.bucket,\n mountPath,\n options: {\n endpoint,\n prefix,\n readOnly: s3.readOnly,\n credentials:\n s3.accessKeyId && s3.secretAccessKey\n ? { accessKeyId: s3.accessKeyId, secretAccessKey: s3.secretAccessKey }\n : undefined,\n },\n },\n };\n}\n\nexport interface CloudflareSandboxOptions extends Omit<MastraSandboxOptions, 'processes'> {\n /** URL of a deployed Cloudflare Sandbox Bridge Worker. */\n baseUrl: string;\n /** Bearer token matching the Worker's `SANDBOX_API_KEY` secret, when authentication is enabled. */\n apiToken?: string;\n /** Stable Mastra identifier for this sandbox instance. */\n id?: string;\n /** Existing Cloudflare sandbox ID to reconnect to instead of creating a sandbox. */\n sandboxId?: string;\n /** Human-readable name shown in Mastra sandbox metadata. */\n name?: string;\n /** Environment variables applied to every command. */\n env?: Record<string, string>;\n /** Working directory applied to every command. Must be under /workspace. */\n workingDirectory?: string;\n /** Default command timeout in milliseconds. */\n commandTimeout?: number;\n /** Custom instructions returned by getInstructions(). */\n instructions?: InstructionsOption;\n /** Custom fetch implementation, primarily for advanced networking setup and tests. */\n fetch?: CloudflareSandboxBridgeClientOptions['fetch'];\n /** Preconfigured Bridge client, primarily for tests. */\n client?: BridgeClient;\n}\n\n/**\n * Absolute path to the shell used to interpret bare command strings. Absolute so it\n * resolves even when a custom PATH excludes the standard system directories.\n */\nconst SHELL_PATH = '/bin/bash';\n\n/**\n * Builds the argv array sent to the bridge. The bridge applies ANSI-C quoting to\n * every element, so no local escaping is needed. Environment variables are applied\n * with `env`, which keeps each assignment a separate argv element.\n *\n * When no separate arguments are supplied (the shape the built-in Workspace\n * `execute_command` tool uses), `command` is a shell command string — pipes,\n * chaining, quoting, redirection — so it is run through a non-login shell rather\n * than treated as a single executable name. When explicit arguments are given,\n * each element stays a literal argv token.\n */\nfunction buildArgv(command: string, args: string[] | undefined, env: Record<string, string>): string[] {\n const assignments = Object.entries(env).map(([key, value]) => {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error(`Invalid environment variable name: ${key}`);\n return `${key}=${value}`;\n });\n const invocation = args && args.length > 0 ? [command, ...args] : [SHELL_PATH, '-c', command];\n return assignments.length ? ['env', ...assignments, ...invocation] : invocation;\n}\n\n/** Resolves a path inside /workspace, rejecting anything that escapes the workspace root. */\nfunction resolveWorkspacePath(path: string): string {\n const resolved = posix.resolve(WORKSPACE_ROOT, path);\n if (resolved !== WORKSPACE_ROOT && !resolved.startsWith(`${WORKSPACE_ROOT}/`)) {\n throw new Error(`Cloudflare Sandbox files must be written under ${WORKSPACE_ROOT}: ${path}`);\n }\n return resolved;\n}\n\nexport class CloudflareSandbox extends MastraSandbox {\n readonly id: string;\n readonly name: string;\n readonly provider = 'cloudflare-sandbox';\n status: ProviderStatus = 'pending';\n /** Created by MastraSandbox because this class implements mount(). */\n declare readonly mounts: MountManager;\n\n private readonly client: BridgeClient;\n private readonly commandTimeout: number;\n private readonly instructions?: InstructionsOption;\n private sandboxId?: string;\n private createdAt = new Date();\n private lastUsedAt?: Date;\n /** Shared across concurrent callers so a wake triggers a single re-mount pass. */\n private ensureMountsPromise?: Promise<void>;\n\n constructor(options: CloudflareSandboxOptions) {\n const name = options.name ?? 'Cloudflare Sandbox';\n super({ ...options, name });\n this.id = options.id ?? `cloudflare-sandbox-${randomUUID()}`;\n this.name = name;\n this.sandboxId = options.sandboxId;\n this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;\n this.instructions = options.instructions;\n this.client =\n options.client ??\n new CloudflareSandboxBridgeClient({ baseUrl: options.baseUrl, apiToken: options.apiToken, fetch: options.fetch });\n }\n\n async start(): Promise<void> {\n if (this.sandboxId) {\n // The bridge boots the container on demand, so a stopped container is not fatal.\n const running = await this.client.isRunning(this.sandboxId);\n if (!running) {\n this.logger?.debug(`Cloudflare sandbox ${this.sandboxId} is not running yet; it starts on first use`);\n }\n return;\n }\n this.sandboxId = await this.client.createSandbox();\n this.createdAt = new Date();\n }\n\n async stop(): Promise<void> {\n // The bridge exposes create/delete but no suspend operation. Stop detaches this\n // Mastra lifecycle while preserving the remote sandbox for later reconnection.\n }\n\n async destroy(): Promise<void> {\n if (!this.sandboxId) return;\n await this.client.deleteSandbox(this.sandboxId);\n this.sandboxId = undefined;\n }\n\n async executeCommand(command: string, args?: string[], options?: ExecuteCommandOptions): Promise<CommandResult> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n\n const startedAt = Date.now();\n const timeout = options?.timeout ?? this.commandTimeout;\n if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError('Command timeout must be positive');\n\n const controller = new AbortController();\n let didTimeout = false;\n const timer = setTimeout(() => {\n didTimeout = true;\n controller.abort();\n }, timeout);\n const signal = options?.abortSignal ? AbortSignal.any([controller.signal, options.abortSignal]) : controller.signal;\n\n // stdout and stderr are separate byte streams, so each needs its own streaming decoder.\n const stdoutDecoder = new TextDecoder();\n const stderrDecoder = new TextDecoder();\n let stdout = '';\n let stderr = '';\n let exitCode = 1;\n\n const env = Object.fromEntries(\n Object.entries({ ...this.getEnv(), ...options?.env }).filter(\n (entry): entry is [string, string] => entry[1] !== undefined,\n ),\n );\n\n try {\n await this.client.exec(\n sandboxId,\n {\n argv: buildArgv(command, args, env),\n timeoutMs: timeout,\n cwd: options?.cwd ?? this.workingDirectory,\n },\n {\n signal,\n onEvent: event => {\n switch (event.type) {\n case 'stdout': {\n const chunk = stdoutDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stdout += chunk;\n options?.onStdout?.(chunk);\n return;\n }\n case 'stderr': {\n const chunk = stderrDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stderr += chunk;\n options?.onStderr?.(chunk);\n return;\n }\n case 'exit':\n exitCode = event.exitCode;\n return;\n case 'error':\n stderr += event.message;\n options?.onStderr?.(event.message);\n return;\n }\n },\n },\n );\n } catch (error) {\n if (!signal.aborted) throw error;\n } finally {\n clearTimeout(timer);\n }\n\n // Flush each decoder so a trailing truncated multi-byte sequence isn't dropped.\n const stdoutTail = stdoutDecoder.decode();\n if (stdoutTail) {\n stdout += stdoutTail;\n options?.onStdout?.(stdoutTail);\n }\n const stderrTail = stderrDecoder.decode();\n if (stderrTail) {\n stderr += stderrTail;\n options?.onStderr?.(stderrTail);\n }\n\n this.lastUsedAt = new Date();\n return {\n command,\n args,\n success: exitCode === 0 && !signal.aborted,\n exitCode,\n stdout,\n stderr,\n executionTimeMs: Date.now() - startedAt,\n timedOut: didTimeout,\n killed: signal.aborted && !didTimeout,\n };\n }\n\n async writeFiles(files: SandboxFileInput[]): Promise<void> {\n assertModesUnsupported(files, 'Cloudflare');\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n // The bridge writes one file per request.\n for (const file of files) {\n await this.client.writeFile(sandboxId, resolveWorkspacePath(file.path), file.content);\n }\n this.lastUsedAt = new Date();\n }\n\n /** Reads a single file under /workspace, returning its raw bytes. */\n async readFile(path: string): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const bytes = await this.client.readFile(sandboxId, resolveWorkspacePath(path));\n this.lastUsedAt = new Date();\n return bytes;\n }\n\n /** Archives /workspace, returning raw tar bytes that can later restore it via hydrateWorkspace. */\n async persistWorkspace(options?: CloudflarePersistWorkspaceOptions): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const archive = await this.client.persistWorkspace(sandboxId, options);\n this.lastUsedAt = new Date();\n return archive;\n }\n\n /** Restores /workspace from a raw tar payload produced by persistWorkspace. */\n async hydrateWorkspace(tar: Uint8Array): Promise<void> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n await this.client.hydrateWorkspace(sandboxId, tar);\n this.lastUsedAt = new Date();\n }\n\n getInfo(): SandboxInfo {\n return {\n id: this.id,\n name: this.name,\n provider: this.provider,\n status: this.status,\n createdAt: this.createdAt,\n lastUsedAt: this.lastUsedAt,\n metadata: {\n sandboxId: this.sandboxId,\n bridgeBaseUrl: this.client instanceof CloudflareSandboxBridgeClient ? this.client.baseUrl : undefined,\n },\n };\n }\n\n /**\n * Mounts an S3-compatible bucket (R2, S3, MinIO, ...) at `mountPath` through\n * the bridge's mount route. Called by MountManager for each Workspace `mounts`\n * entry after start(). The Cloudflare Sandbox SDK forgets mounts when an idle\n * container is stopped and does not restore them on wake, so\n * {@link ensureMountsActive} re-mounts stale paths before each operation; that\n * makes mounted paths the durable part of the filesystem.\n */\n async mount(filesystem: WorkspaceFilesystem, mountPath: string): Promise<MountResult> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n\n const config = filesystem.getMountConfig?.();\n if (!config) {\n const error = `Filesystem \"${filesystem.id}\" does not provide a mount config`;\n this.mounts.set(mountPath, { filesystem, state: 'error', error });\n return { success: false, mountPath, error };\n }\n\n const translated = toMountRequest(config, mountPath);\n if ('error' in translated) {\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error: translated.error });\n return { success: false, mountPath, error: translated.error };\n }\n\n this.mounts.set(mountPath, { filesystem, state: 'mounting', config });\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n const error = cause instanceof Error ? cause.message : String(cause);\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error });\n return { success: false, mountPath, error };\n }\n this.mounts.set(mountPath, { filesystem, state: 'mounted', config });\n this.lastUsedAt = new Date();\n return { success: true, mountPath };\n }\n\n /** Unmounts a bucket previously mounted with {@link mount}. */\n async unmount(mountPath: string): Promise<void> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n await this.client.unmountBucket(sandboxId, mountPath);\n this.mounts.delete(mountPath);\n this.lastUsedAt = new Date();\n }\n\n getInstructions(): string {\n const mounted = [...this.mounts.entries].filter(([, entry]) => entry.state === 'mounted').map(([path]) => path);\n const defaultInstructions =\n mounted.length > 0\n ? `Commands execute in a remote Cloudflare Sandbox. The container sleeps when idle and files under /workspace do NOT survive between commands, except under the mounted paths: ${mounted.join(', ')}. Keep anything that must persist under a mounted path.`\n : 'Commands execute in a remote Cloudflare Sandbox. Use /workspace as scratch space only: the container sleeps when idle and files under /workspace do NOT survive between commands. Do not assume earlier files still exist.';\n return typeof this.instructions === 'function'\n ? this.instructions({ defaultInstructions })\n : (this.instructions ?? defaultInstructions);\n }\n\n /**\n * A slept container boots fresh without its mounts: `@cloudflare/sandbox` keeps\n * `activeMounts` in memory and clears it on stop, so it never re-mounts on wake,\n * and `GET /running` still reports `true` until the DO next talks to the\n * container. Before any operation that reads or writes the filesystem, probe the\n * mounted paths with `mountpoint` and re-mount the ones that are gone. The pass\n * is shared across concurrent callers, and there is no probe when nothing is\n * mounted.\n */\n private ensureMountsActive(sandboxId: string): Promise<void> {\n const mountedPaths = [...this.mounts.entries]\n .filter(([, entry]) => entry.state === 'mounted')\n .map(([mountPath]) => mountPath);\n if (mountedPaths.length === 0) return Promise.resolve();\n if (!this.ensureMountsPromise) {\n this.ensureMountsPromise = this.remountStalePaths(sandboxId, mountedPaths).finally(() => {\n this.ensureMountsPromise = undefined;\n });\n }\n return this.ensureMountsPromise;\n }\n\n private async remountStalePaths(sandboxId: string, mountedPaths: string[]): Promise<void> {\n // Mount paths are validated against SAFE_MOUNT_PATH, so they are safe to embed\n // directly. `mountpoint -q` exits non-zero for a path that is no longer a mount,\n // and that path is echoed so a single exec reports every stale mount at once.\n const script = `for p in ${mountedPaths.join(' ')}; do mountpoint -q \"$p\" || echo \"$p\"; done`;\n const decoder = new TextDecoder();\n let stdout = '';\n await this.client.exec(\n sandboxId,\n { argv: [SHELL_PATH, '-c', script], timeoutMs: this.commandTimeout },\n {\n onEvent: event => {\n if (event.type === 'stdout') stdout += decoder.decode(event.data, { stream: true });\n },\n },\n );\n stdout += decoder.decode();\n\n const stalePaths = stdout\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n for (const mountPath of stalePaths) {\n const entry = this.mounts.get(mountPath);\n if (!entry?.config) continue;\n const translated = toMountRequest(entry.config, mountPath);\n if ('error' in translated) continue;\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n this.logger?.warn(`Failed to re-mount ${mountPath} after container wake`, { error: cause });\n }\n }\n }\n\n private requireSandboxId(): string {\n if (!this.sandboxId) throw new Error(`Cloudflare Sandbox ${this.id} has not been started`);\n return this.sandboxId;\n }\n}\n"],"mappings":";;;;;AAgEA,IAAa,+BAAb,cAAkD,MAAM;CACtD;CACA;CAEA,YAAY,QAAgB,MAAc;EACxC,MAAM,6CAA6C,OAAO,KAAK,QAAQ,kBAAkB;EACzF,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,KAAK,OAAO;CACd;AACF;AAEA,SAAS,qBAAqB,KAAqB;CACjD,IAAI,MAAM,IAAI;CACd,OAAO,MAAM,KAAK,IAAI,MAAM,OAAO,KAAK;CACxC,OAAO,IAAI,MAAM,GAAG,GAAG;AACzB;;AAGA,SAAS,eAAe,cAA8B;CACpD,IAAI,QAAQ;CACZ,OAAO,QAAQ,aAAa,UAAU,aAAa,WAAW,KAAK;CACnE,OAAO,aACJ,MAAM,KAAK,CAAC,CACZ,MAAM,GAAG,CAAC,CACV,KAAI,YAAW,mBAAmB,OAAO,CAAC,CAAC,CAC3C,KAAK,GAAG;AACb;;;;;;AAOA,IAAa,gCAAb,MAA2C;CACzC;CACA;CACA;CAEA,YAAY,SAA+C;EACzD,KAAK,UAAU,qBAAqB,QAAQ,OAAO;EACnD,KAAK,WAAW,QAAQ;EACxB,KAAK,YAAY,QAAQ,SAAS,WAAW;CAC/C;;CAGA,MAAM,gBAAiC;EAErC,QAAO,MADe,KAAK,QAAwB,eAAe,EAAE,QAAQ,OAAO,CAAC,EAAA,CACrE;CACjB;;CAGA,MAAM,UAAU,IAA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,OAAO;EAEzB,MAAM,aAAa,OAChB,MAAM,IAAI,CAAC,CACX,KAAI,SAAQ,KAAK,KAAK,CAAC,CAAC,CACxB,OAAO,OAAO;EACjB,KAAK,MAAM,aAAa,YAAY;GAClC,MAAM,QAAQ,KAAK,OAAO,IAAI,SAAS;GACvC,IAAI,CAAC,OAAO,QAAQ;GACpB,MAAM,aAAa,eAAe,MAAM,QAAQ,SAAS;GACzD,IAAI,WAAW,YAAY;GAC3B,IAAI;IACF,MAAM,KAAK,OAAO,YAAY,WAAW,WAAW,OAAO;GAC7D,SAAS,OAAO;IACd,KAAK,QAAQ,KAAK,sBAAsB,UAAU,wBAAwB,EAAE,OAAO,MAAM,CAAC;GAC5F;EACF;CACF;CAEA,mBAAmC;EACjC,IAAI,CAAC,KAAK,WAAW,MAAM,IAAI,MAAM,sBAAsB,KAAK,GAAG,sBAAsB;EACzF,OAAO,KAAK;CACd;AACF"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["posix","path","MastraSandbox"],"sources":["../src/bridge-client.ts","../src/sandbox.ts"],"sourcesContent":["export interface CloudflareSandboxBridgeClientOptions {\n baseUrl: string;\n apiToken?: string;\n fetch?: typeof globalThis.fetch;\n}\n\n/** Terminal and streaming events emitted by `POST /v1/sandbox/:id/exec`. */\nexport type CloudflareCommandEvent =\n | { type: 'stdout'; data: Uint8Array }\n | { type: 'stderr'; data: Uint8Array }\n | { type: 'exit'; exitCode: number }\n | { type: 'error'; message: string; code?: string };\n\nexport interface CloudflareExecRequest {\n /** Command and arguments. The bridge applies ANSI-C quoting to each element. */\n argv: string[];\n timeoutMs?: number;\n cwd?: string;\n}\n\nexport interface CloudflarePersistWorkspaceOptions {\n /** Relative paths (under /workspace) to exclude from the archive. */\n excludes?: string[];\n}\n\nexport interface CloudflareMountBucketCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface CloudflareMountBucketOptions {\n /** S3-compatible endpoint, e.g. `https://<account>.r2.cloudflarestorage.com`. */\n endpoint?: string;\n /** Mount the bucket read-only. */\n readOnly?: boolean;\n /** Only expose objects under this bucket prefix at the mount point. */\n prefix?: string;\n /** Storage provider hint, e.g. `r2`. */\n provider?: string;\n /** Explicit credentials; omitted when the Worker resolves them from secrets. */\n credentials?: CloudflareMountBucketCredentials;\n}\n\nexport interface CloudflareMountBucketRequest {\n /** Bucket name, e.g. `my-r2-bucket`. */\n bucket: string;\n /** Local filesystem path to mount at, e.g. `/mnt/data`. */\n mountPath: string;\n options?: CloudflareMountBucketOptions;\n}\n\nexport interface CloudflareCreateSessionRequest {\n /** Working directory the session starts in. */\n cwd?: string;\n /** Environment variables seeded into the session. */\n env?: Record<string, string>;\n /** Caller-chosen session id; must match `^[a-zA-Z0-9._-]{1,128}$`. Generated when omitted. */\n sessionId?: string;\n}\n\nexport interface CloudflareSession {\n id: string;\n}\n\nexport class CloudflareSandboxBridgeError extends Error {\n readonly status: number;\n readonly body: string;\n\n constructor(status: number, body: string) {\n super(`Cloudflare Sandbox Bridge request failed (${status}): ${body || 'empty response'}`);\n this.name = 'CloudflareSandboxBridgeError';\n this.status = status;\n this.body = body;\n }\n}\n\nfunction stripTrailingSlashes(url: string): string {\n let end = url.length;\n while (end > 0 && url[end - 1] === '/') end--;\n return url.slice(0, end);\n}\n\n/** Encodes an absolute sandbox path for the `/file/*` route, which omits the leading slash. */\nfunction encodeFilePath(absolutePath: string): string {\n let start = 0;\n while (start < absolutePath.length && absolutePath[start] === '/') start++;\n return absolutePath\n .slice(start)\n .split('/')\n .map(segment => encodeURIComponent(segment))\n .join('/');\n}\n\n/**\n * Client for the Cloudflare Sandbox Bridge Worker.\n *\n * @see https://developers.cloudflare.com/sandbox/bridge/http-api/\n */\nexport class CloudflareSandboxBridgeClient {\n readonly baseUrl: string;\n private readonly apiToken?: string;\n private readonly fetchImpl: typeof globalThis.fetch;\n\n constructor(options: CloudflareSandboxBridgeClientOptions) {\n this.baseUrl = stripTrailingSlashes(options.baseUrl);\n this.apiToken = options.apiToken;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n /** `POST /v1/sandbox` */\n async createSandbox(): Promise<string> {\n const created = await this.request<{ id: string }>('/v1/sandbox', { method: 'POST' });\n return created.id;\n }\n\n /** `GET /v1/sandbox/:id/running` */\n async isRunning(id: string): Promise<boolean> {\n const status = await this.request<{ running: boolean }>(`/v1/sandbox/${encodeURIComponent(id)}/running`, {});\n return status.running === true;\n }\n\n /** `DELETE /v1/sandbox/:id` */\n async deleteSandbox(id: string): Promise<void> {\n await this.request(`/v1/sandbox/${encodeURIComponent(id)}`, { method: 'DELETE' }, true);\n }\n\n /** `PUT /v1/sandbox/:id/file/*` — one file per request, raw bytes as the body. */\n async writeFile(id: string, absolutePath: string, content: Uint8Array | string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`,\n {\n method: 'PUT',\n body: content as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `GET /v1/sandbox/:id/file/*` — reads one file, returning its raw bytes. */\n async readFile(id: string, absolutePath: string): Promise<Uint8Array> {\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`, {});\n }\n\n /** `GET /v1/sandbox/:id/persist` — archives `/workspace`, returning raw tar bytes. */\n async persistWorkspace(id: string, options: CloudflarePersistWorkspaceOptions = {}): Promise<Uint8Array> {\n const query = options.excludes?.length ? `?excludes=${encodeURIComponent(options.excludes.join(','))}` : '';\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/persist${query}`, {});\n }\n\n /** `POST /v1/sandbox/:id/hydrate` — restores `/workspace` from a raw tar payload. */\n async hydrateWorkspace(id: string, tar: Uint8Array): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/hydrate`,\n {\n method: 'POST',\n body: tar as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/mount` — mounts an S3-compatible bucket as a local directory. */\n async mountBucket(id: string, request: CloudflareMountBucketRequest): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/mount`,\n {\n method: 'POST',\n body: JSON.stringify(request),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/unmount` — unmounts a previously mounted bucket. */\n async unmountBucket(id: string, mountPath: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/unmount`,\n {\n method: 'POST',\n body: JSON.stringify({ mountPath }),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/session` — creates an execution session, returning its id. */\n async createSession(id: string, request: CloudflareCreateSessionRequest = {}): Promise<CloudflareSession> {\n const body: Record<string, unknown> = {};\n if (request.cwd !== undefined) body.cwd = request.cwd;\n if (request.env !== undefined) body.env = request.env;\n if (request.sessionId !== undefined) body.id = request.sessionId;\n return this.request<CloudflareSession>(`/v1/sandbox/${encodeURIComponent(id)}/session`, {\n method: 'POST',\n body: JSON.stringify(body),\n headers: { 'content-type': 'application/json' },\n });\n }\n\n /** `DELETE /v1/sandbox/:id/session/:sessionId` — tears down an execution session. */\n async deleteSession(id: string, sessionId: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/session/${encodeURIComponent(sessionId)}`,\n { method: 'DELETE' },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/exec` — streams SSE events until `exit` or `error`. */\n async exec(\n id: string,\n request: CloudflareExecRequest,\n options: {\n signal?: AbortSignal;\n onEvent: (event: CloudflareCommandEvent) => void;\n },\n ): Promise<void> {\n const response = await this.fetchImpl(`${this.baseUrl}/v1/sandbox/${encodeURIComponent(id)}/exec`, {\n method: 'POST',\n headers: { ...this.headers(), 'content-type': 'application/json', accept: 'text/event-stream' },\n body: JSON.stringify({\n argv: request.argv,\n ...(request.timeoutMs === undefined ? {} : { timeout_ms: request.timeoutMs }),\n ...(request.cwd === undefined ? {} : { cwd: request.cwd }),\n }),\n signal: options.signal,\n });\n\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (!response.body) {\n throw new Error('Cloudflare Sandbox Bridge returned an empty command stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n buffer += decoder.decode(value, { stream: !done }).replace(/\\r\\n/g, '\\n');\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary !== -1) {\n this.emitBlock(buffer.slice(0, boundary), options.onEvent);\n buffer = buffer.slice(boundary + 2);\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n if (buffer.trim()) this.emitBlock(buffer, options.onEvent);\n }\n\n private emitBlock(block: string, onEvent: (event: CloudflareCommandEvent) => void): void {\n let eventName: string | undefined;\n const dataLines: string[] = [];\n for (const line of block.split('\\n')) {\n if (line.startsWith('event:')) eventName = line.slice(6).trim();\n else if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));\n }\n const data = dataLines.join('\\n');\n if (!eventName || !data) return;\n\n switch (eventName) {\n case 'stdout':\n case 'stderr':\n onEvent({ type: eventName, data: base64ToBytes(data) });\n return;\n case 'exit': {\n const parsed = safeJsonParse(data);\n onEvent({ type: 'exit', exitCode: typeof parsed?.exit_code === 'number' ? parsed.exit_code : 0 });\n return;\n }\n case 'error': {\n const parsed = safeJsonParse(data);\n onEvent({\n type: 'error',\n message: typeof parsed?.error === 'string' ? parsed.error : data,\n code: typeof parsed?.code === 'string' ? parsed.code : undefined,\n });\n return;\n }\n default:\n return;\n }\n }\n\n private headers(): Record<string, string> {\n return this.apiToken ? { authorization: `Bearer ${this.apiToken}` } : {};\n }\n\n private async request<T>(path: string, init: RequestInit, allowEmpty = false): Promise<T> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (allowEmpty || response.status === 204) return undefined as T;\n return response.json() as Promise<T>;\n }\n\n private async requestBytes(path: string, init: RequestInit): Promise<Uint8Array> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n return new Uint8Array(await response.arrayBuffer());\n }\n}\n\nfunction base64ToBytes(value: string): Uint8Array {\n return new Uint8Array(Buffer.from(value, 'base64'));\n}\n\nfunction safeJsonParse(value: string): Record<string, unknown> | undefined {\n try {\n return JSON.parse(value) as Record<string, unknown>;\n } catch {\n return undefined;\n }\n}\n","import { posix } from 'node:path';\nimport type {\n CommandResult,\n ExecuteCommandOptions,\n FilesystemMountConfig,\n MastraSandboxOptions,\n MountManager,\n MountResult,\n ProviderStatus,\n SandboxFileInput,\n SandboxInfo,\n WorkspaceFilesystem,\n} from '@mastra/core/workspace';\nimport { MastraSandbox, assertModesUnsupported } from '@mastra/core/workspace';\nimport {\n CloudflareSandboxBridgeClient,\n type CloudflareMountBucketRequest,\n type CloudflarePersistWorkspaceOptions,\n type CloudflareSandboxBridgeClientOptions,\n} from './bridge-client';\n\nconst DEFAULT_COMMAND_TIMEOUT_MS = 300_000;\nconst WORKSPACE_ROOT = '/workspace';\n\ntype InstructionsOption = string | ((options: { defaultInstructions: string }) => string);\ntype BridgeClient = Pick<\n CloudflareSandboxBridgeClient,\n | 'createSandbox'\n | 'isRunning'\n | 'deleteSandbox'\n | 'writeFile'\n | 'readFile'\n | 'persistWorkspace'\n | 'hydrateWorkspace'\n | 'mountBucket'\n | 'unmountBucket'\n | 'exec'\n>;\n\n/**\n * Mount config accepted by the Cloudflare bridge: any S3-compatible bucket\n * (R2, S3, MinIO, ...) as produced by `S3Filesystem.getMountConfig()`.\n * Declared structurally so this package does not depend on `@mastra/s3`.\n */\ninterface S3CompatibleMountConfig extends FilesystemMountConfig {\n type: 's3';\n bucket: string;\n region?: string;\n endpoint?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n sessionToken?: string;\n prefix?: string;\n readOnly?: boolean;\n}\n\n/** Allowlist pattern for mount paths, matching the other remote sandbox providers. */\nconst SAFE_MOUNT_PATH = /^\\/[a-zA-Z0-9_.\\-/]+$/;\n\nfunction validateMountPath(mountPath: string): void {\n if (!SAFE_MOUNT_PATH.test(mountPath)) {\n throw new Error(\n `Invalid mount path: ${mountPath}. Must be an absolute path with alphanumeric, dash, dot, underscore, or slash characters only.`,\n );\n }\n}\n\n/**\n * Translates a Workspace mount config into the bridge's mount request, or\n * explains why the bridge cannot serve it. The bridge mounts S3-compatible\n * buckets with s3fs; GCS and Azure mount configs have no equivalent route.\n */\nfunction toMountRequest(\n config: FilesystemMountConfig,\n mountPath: string,\n): { request: CloudflareMountBucketRequest } | { error: string } {\n if (config.type !== 's3') {\n return { error: `Cloudflare Sandbox can only mount S3-compatible buckets; got mount type \"${config.type}\"` };\n }\n const s3 = config as S3CompatibleMountConfig;\n if (s3.sessionToken) {\n return { error: 'Cloudflare Sandbox bucket mounts do not support temporary credentials (sessionToken)' };\n }\n if (Boolean(s3.accessKeyId) !== Boolean(s3.secretAccessKey)) {\n return { error: 'Cloudflare Sandbox bucket mounts need both accessKeyId and secretAccessKey, or neither' };\n }\n // The bridge treats a request with no endpoint as an R2 binding mount, so a\n // region-only AWS filesystem must resolve to an explicit S3 endpoint.\n const endpoint = s3.endpoint ?? (s3.region ? `https://s3.${s3.region}.amazonaws.com` : undefined);\n // The bridge requires the prefix to start with `/`; S3Filesystem emits `dir/`.\n const prefix = s3.prefix ? (s3.prefix.startsWith('/') ? s3.prefix : `/${s3.prefix}`) : undefined;\n return {\n request: {\n bucket: s3.bucket,\n mountPath,\n options: {\n endpoint,\n prefix,\n readOnly: s3.readOnly,\n credentials:\n s3.accessKeyId && s3.secretAccessKey\n ? { accessKeyId: s3.accessKeyId, secretAccessKey: s3.secretAccessKey }\n : undefined,\n },\n },\n };\n}\n\nexport interface CloudflareSandboxOptions extends Omit<MastraSandboxOptions, 'processes'> {\n /** URL of a deployed Cloudflare Sandbox Bridge Worker. */\n baseUrl: string;\n /** Bearer token matching the Worker's `SANDBOX_API_KEY` secret, when authentication is enabled. */\n apiToken?: string;\n /** Stable Mastra identifier for this sandbox instance. */\n id?: string;\n /** Existing Cloudflare sandbox ID to reconnect to instead of creating a sandbox. */\n sandboxId?: string;\n /** Human-readable name shown in Mastra sandbox metadata. */\n name?: string;\n /** Environment variables applied to every command. */\n env?: Record<string, string>;\n /** Working directory applied to every command. Must be under /workspace. */\n workingDirectory?: string;\n /** Default command timeout in milliseconds. */\n commandTimeout?: number;\n /** Custom instructions returned by getInstructions(). */\n instructions?: InstructionsOption;\n /** Custom fetch implementation, primarily for advanced networking setup and tests. */\n fetch?: CloudflareSandboxBridgeClientOptions['fetch'];\n /** Preconfigured Bridge client, primarily for tests. */\n client?: BridgeClient;\n}\n\n/**\n * Absolute path to the shell used to interpret bare command strings. Absolute so it\n * resolves even when a custom PATH excludes the standard system directories.\n */\nconst SHELL_PATH = '/bin/bash';\n\n/**\n * Builds the argv array sent to the bridge. The bridge applies ANSI-C quoting to\n * every element, so no local escaping is needed. Environment variables are applied\n * with `env`, which keeps each assignment a separate argv element.\n *\n * When no separate arguments are supplied (the shape the built-in Workspace\n * `execute_command` tool uses), `command` is a shell command string — pipes,\n * chaining, quoting, redirection — so it is run through a non-login shell rather\n * than treated as a single executable name. When explicit arguments are given,\n * each element stays a literal argv token.\n */\nfunction buildArgv(command: string, args: string[] | undefined, env: Record<string, string>): string[] {\n const assignments = Object.entries(env).map(([key, value]) => {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error(`Invalid environment variable name: ${key}`);\n return `${key}=${value}`;\n });\n const invocation = args && args.length > 0 ? [command, ...args] : [SHELL_PATH, '-c', command];\n return assignments.length ? ['env', ...assignments, ...invocation] : invocation;\n}\n\n/** Resolves a path inside /workspace, rejecting anything that escapes the workspace root. */\nfunction resolveWorkspacePath(path: string): string {\n const resolved = posix.resolve(WORKSPACE_ROOT, path);\n if (resolved !== WORKSPACE_ROOT && !resolved.startsWith(`${WORKSPACE_ROOT}/`)) {\n throw new Error(`Cloudflare Sandbox files must be written under ${WORKSPACE_ROOT}: ${path}`);\n }\n return resolved;\n}\n\nexport class CloudflareSandbox extends MastraSandbox {\n readonly id: string;\n readonly name: string;\n readonly provider = 'cloudflare-sandbox';\n status: ProviderStatus = 'pending';\n /** Created by MastraSandbox because this class implements mount(). */\n declare readonly mounts: MountManager;\n\n private readonly client: BridgeClient;\n private readonly commandTimeout: number;\n private readonly instructions?: InstructionsOption;\n private sandboxId?: string;\n private createdAt = new Date();\n private lastUsedAt?: Date;\n /** Shared across concurrent callers so a wake triggers a single re-mount pass. */\n private ensureMountsPromise?: Promise<void>;\n\n constructor(options: CloudflareSandboxOptions) {\n const name = options.name ?? 'Cloudflare Sandbox';\n super({ ...options, name });\n this.id = options.id ?? `cloudflare-sandbox-${globalThis.crypto.randomUUID()}`;\n this.name = name;\n this.sandboxId = options.sandboxId;\n this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;\n this.instructions = options.instructions;\n this.client =\n options.client ??\n new CloudflareSandboxBridgeClient({ baseUrl: options.baseUrl, apiToken: options.apiToken, fetch: options.fetch });\n }\n\n async start(): Promise<void> {\n if (this.sandboxId) {\n // The bridge boots the container on demand, so a stopped container is not fatal.\n const running = await this.client.isRunning(this.sandboxId);\n if (!running) {\n this.logger?.debug(`Cloudflare sandbox ${this.sandboxId} is not running yet; it starts on first use`);\n }\n return;\n }\n this.sandboxId = await this.client.createSandbox();\n this.createdAt = new Date();\n }\n\n async stop(): Promise<void> {\n // The bridge exposes create/delete but no suspend operation. Stop detaches this\n // Mastra lifecycle while preserving the remote sandbox for later reconnection.\n }\n\n async destroy(): Promise<void> {\n if (!this.sandboxId) return;\n await this.client.deleteSandbox(this.sandboxId);\n this.sandboxId = undefined;\n }\n\n async executeCommand(command: string, args?: string[], options?: ExecuteCommandOptions): Promise<CommandResult> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n\n const startedAt = Date.now();\n const timeout = options?.timeout ?? this.commandTimeout;\n if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError('Command timeout must be positive');\n\n const controller = new AbortController();\n let didTimeout = false;\n const timer = setTimeout(() => {\n didTimeout = true;\n controller.abort();\n }, timeout);\n const signal = options?.abortSignal ? AbortSignal.any([controller.signal, options.abortSignal]) : controller.signal;\n\n // stdout and stderr are separate byte streams, so each needs its own streaming decoder.\n const stdoutDecoder = new TextDecoder();\n const stderrDecoder = new TextDecoder();\n let stdout = '';\n let stderr = '';\n let exitCode = 1;\n\n const env = Object.fromEntries(\n Object.entries({ ...this.getEnv(), ...options?.env }).filter(\n (entry): entry is [string, string] => entry[1] !== undefined,\n ),\n );\n\n try {\n await this.client.exec(\n sandboxId,\n {\n argv: buildArgv(command, args, env),\n timeoutMs: timeout,\n cwd: options?.cwd ?? this.workingDirectory,\n },\n {\n signal,\n onEvent: event => {\n switch (event.type) {\n case 'stdout': {\n const chunk = stdoutDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stdout += chunk;\n options?.onStdout?.(chunk);\n return;\n }\n case 'stderr': {\n const chunk = stderrDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stderr += chunk;\n options?.onStderr?.(chunk);\n return;\n }\n case 'exit':\n exitCode = event.exitCode;\n return;\n case 'error':\n stderr += event.message;\n options?.onStderr?.(event.message);\n return;\n }\n },\n },\n );\n } catch (error) {\n if (!signal.aborted) throw error;\n } finally {\n clearTimeout(timer);\n }\n\n // Flush each decoder so a trailing truncated multi-byte sequence isn't dropped.\n const stdoutTail = stdoutDecoder.decode();\n if (stdoutTail) {\n stdout += stdoutTail;\n options?.onStdout?.(stdoutTail);\n }\n const stderrTail = stderrDecoder.decode();\n if (stderrTail) {\n stderr += stderrTail;\n options?.onStderr?.(stderrTail);\n }\n\n this.lastUsedAt = new Date();\n return {\n command,\n args,\n success: exitCode === 0 && !signal.aborted,\n exitCode,\n stdout,\n stderr,\n executionTimeMs: Date.now() - startedAt,\n timedOut: didTimeout,\n killed: signal.aborted && !didTimeout,\n };\n }\n\n async writeFiles(files: SandboxFileInput[]): Promise<void> {\n assertModesUnsupported(files, 'Cloudflare');\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n // The bridge writes one file per request.\n for (const file of files) {\n await this.client.writeFile(sandboxId, resolveWorkspacePath(file.path), file.content);\n }\n this.lastUsedAt = new Date();\n }\n\n /** Reads a single file under /workspace, returning its raw bytes. */\n async readFile(path: string): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const bytes = await this.client.readFile(sandboxId, resolveWorkspacePath(path));\n this.lastUsedAt = new Date();\n return bytes;\n }\n\n /** Archives /workspace, returning raw tar bytes that can later restore it via hydrateWorkspace. */\n async persistWorkspace(options?: CloudflarePersistWorkspaceOptions): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const archive = await this.client.persistWorkspace(sandboxId, options);\n this.lastUsedAt = new Date();\n return archive;\n }\n\n /** Restores /workspace from a raw tar payload produced by persistWorkspace. */\n async hydrateWorkspace(tar: Uint8Array): Promise<void> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n await this.client.hydrateWorkspace(sandboxId, tar);\n this.lastUsedAt = new Date();\n }\n\n getInfo(): SandboxInfo {\n return {\n id: this.id,\n name: this.name,\n provider: this.provider,\n status: this.status,\n createdAt: this.createdAt,\n lastUsedAt: this.lastUsedAt,\n metadata: {\n sandboxId: this.sandboxId,\n bridgeBaseUrl: this.client instanceof CloudflareSandboxBridgeClient ? this.client.baseUrl : undefined,\n },\n };\n }\n\n /**\n * Mounts an S3-compatible bucket (R2, S3, MinIO, ...) at `mountPath` through\n * the bridge's mount route. Called by MountManager for each Workspace `mounts`\n * entry after start(). The Cloudflare Sandbox SDK forgets mounts when an idle\n * container is stopped and does not restore them on wake, so\n * {@link ensureMountsActive} re-mounts stale paths before each operation; that\n * makes mounted paths the durable part of the filesystem.\n */\n async mount(filesystem: WorkspaceFilesystem, mountPath: string): Promise<MountResult> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n\n const config = filesystem.getMountConfig?.();\n if (!config) {\n const error = `Filesystem \"${filesystem.id}\" does not provide a mount config`;\n this.mounts.set(mountPath, { filesystem, state: 'error', error });\n return { success: false, mountPath, error };\n }\n\n const translated = toMountRequest(config, mountPath);\n if ('error' in translated) {\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error: translated.error });\n return { success: false, mountPath, error: translated.error };\n }\n\n this.mounts.set(mountPath, { filesystem, state: 'mounting', config });\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n const error = cause instanceof Error ? cause.message : String(cause);\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error });\n return { success: false, mountPath, error };\n }\n this.mounts.set(mountPath, { filesystem, state: 'mounted', config });\n this.lastUsedAt = new Date();\n return { success: true, mountPath };\n }\n\n /** Unmounts a bucket previously mounted with {@link mount}. */\n async unmount(mountPath: string): Promise<void> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n await this.client.unmountBucket(sandboxId, mountPath);\n this.mounts.delete(mountPath);\n this.lastUsedAt = new Date();\n }\n\n getInstructions(): string {\n const mounted = [...this.mounts.entries].filter(([, entry]) => entry.state === 'mounted').map(([path]) => path);\n const defaultInstructions =\n mounted.length > 0\n ? `Commands execute in a remote Cloudflare Sandbox. The container sleeps when idle and files under /workspace do NOT survive between commands, except under the mounted paths: ${mounted.join(', ')}. Keep anything that must persist under a mounted path.`\n : 'Commands execute in a remote Cloudflare Sandbox. Use /workspace as scratch space only: the container sleeps when idle and files under /workspace do NOT survive between commands. Do not assume earlier files still exist.';\n return typeof this.instructions === 'function'\n ? this.instructions({ defaultInstructions })\n : (this.instructions ?? defaultInstructions);\n }\n\n /**\n * A slept container boots fresh without its mounts: `@cloudflare/sandbox` keeps\n * `activeMounts` in memory and clears it on stop, so it never re-mounts on wake,\n * and `GET /running` still reports `true` until the DO next talks to the\n * container. Before any operation that reads or writes the filesystem, probe the\n * mounted paths with `mountpoint` and re-mount the ones that are gone. The pass\n * is shared across concurrent callers, and there is no probe when nothing is\n * mounted.\n */\n private ensureMountsActive(sandboxId: string): Promise<void> {\n const mountedPaths = [...this.mounts.entries]\n .filter(([, entry]) => entry.state === 'mounted')\n .map(([mountPath]) => mountPath);\n if (mountedPaths.length === 0) return Promise.resolve();\n if (!this.ensureMountsPromise) {\n this.ensureMountsPromise = this.remountStalePaths(sandboxId, mountedPaths).finally(() => {\n this.ensureMountsPromise = undefined;\n });\n }\n return this.ensureMountsPromise;\n }\n\n private async remountStalePaths(sandboxId: string, mountedPaths: string[]): Promise<void> {\n // Mount paths are validated against SAFE_MOUNT_PATH, so they are safe to embed\n // directly. `mountpoint -q` exits non-zero for a path that is no longer a mount,\n // and that path is echoed so a single exec reports every stale mount at once.\n const script = `for p in ${mountedPaths.join(' ')}; do mountpoint -q \"$p\" || echo \"$p\"; done`;\n const decoder = new TextDecoder();\n let stdout = '';\n await this.client.exec(\n sandboxId,\n { argv: [SHELL_PATH, '-c', script], timeoutMs: this.commandTimeout },\n {\n onEvent: event => {\n if (event.type === 'stdout') stdout += decoder.decode(event.data, { stream: true });\n },\n },\n );\n stdout += decoder.decode();\n\n const stalePaths = stdout\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n for (const mountPath of stalePaths) {\n const entry = this.mounts.get(mountPath);\n if (!entry?.config) continue;\n const translated = toMountRequest(entry.config, mountPath);\n if ('error' in translated) continue;\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n this.logger?.warn(`Failed to re-mount ${mountPath} after container wake`, { error: cause });\n }\n }\n }\n\n private requireSandboxId(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,MAAM,KAAK,OAAO,YAAY,WAAW,WAAW,OAAO;GAC7D,SAAS,OAAO;IACd,KAAK,QAAQ,KAAK,sBAAsB,UAAU,wBAAwB,EAAE,OAAO,MAAM,CAAC;GAC5F;EACF;CACF;CAEA,mBAAmC;EACjC,IAAI,CAAC,KAAK,WAAW,MAAM,IAAI,MAAM,sBAAsB,KAAK,GAAG,sBAAsB;EACzF,OAAO,KAAK;CACd;AACF"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { randomUUID } from "crypto";
|
|
2
1
|
import { posix } from "path";
|
|
3
2
|
import { MastraSandbox, assertModesUnsupported } from "@mastra/core/workspace";
|
|
4
3
|
//#region src/bridge-client.ts
|
|
@@ -304,7 +303,7 @@ var CloudflareSandbox = class extends MastraSandbox {
|
|
|
304
303
|
...options,
|
|
305
304
|
name
|
|
306
305
|
});
|
|
307
|
-
this.id = options.id ?? `cloudflare-sandbox-${randomUUID()}`;
|
|
306
|
+
this.id = options.id ?? `cloudflare-sandbox-${globalThis.crypto.randomUUID()}`;
|
|
308
307
|
this.name = name;
|
|
309
308
|
this.sandboxId = options.sandboxId;
|
|
310
309
|
this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/bridge-client.ts","../src/sandbox.ts"],"sourcesContent":["export interface CloudflareSandboxBridgeClientOptions {\n baseUrl: string;\n apiToken?: string;\n fetch?: typeof globalThis.fetch;\n}\n\n/** Terminal and streaming events emitted by `POST /v1/sandbox/:id/exec`. */\nexport type CloudflareCommandEvent =\n | { type: 'stdout'; data: Uint8Array }\n | { type: 'stderr'; data: Uint8Array }\n | { type: 'exit'; exitCode: number }\n | { type: 'error'; message: string; code?: string };\n\nexport interface CloudflareExecRequest {\n /** Command and arguments. The bridge applies ANSI-C quoting to each element. */\n argv: string[];\n timeoutMs?: number;\n cwd?: string;\n}\n\nexport interface CloudflarePersistWorkspaceOptions {\n /** Relative paths (under /workspace) to exclude from the archive. */\n excludes?: string[];\n}\n\nexport interface CloudflareMountBucketCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface CloudflareMountBucketOptions {\n /** S3-compatible endpoint, e.g. `https://<account>.r2.cloudflarestorage.com`. */\n endpoint?: string;\n /** Mount the bucket read-only. */\n readOnly?: boolean;\n /** Only expose objects under this bucket prefix at the mount point. */\n prefix?: string;\n /** Storage provider hint, e.g. `r2`. */\n provider?: string;\n /** Explicit credentials; omitted when the Worker resolves them from secrets. */\n credentials?: CloudflareMountBucketCredentials;\n}\n\nexport interface CloudflareMountBucketRequest {\n /** Bucket name, e.g. `my-r2-bucket`. */\n bucket: string;\n /** Local filesystem path to mount at, e.g. `/mnt/data`. */\n mountPath: string;\n options?: CloudflareMountBucketOptions;\n}\n\nexport interface CloudflareCreateSessionRequest {\n /** Working directory the session starts in. */\n cwd?: string;\n /** Environment variables seeded into the session. */\n env?: Record<string, string>;\n /** Caller-chosen session id; must match `^[a-zA-Z0-9._-]{1,128}$`. Generated when omitted. */\n sessionId?: string;\n}\n\nexport interface CloudflareSession {\n id: string;\n}\n\nexport class CloudflareSandboxBridgeError extends Error {\n readonly status: number;\n readonly body: string;\n\n constructor(status: number, body: string) {\n super(`Cloudflare Sandbox Bridge request failed (${status}): ${body || 'empty response'}`);\n this.name = 'CloudflareSandboxBridgeError';\n this.status = status;\n this.body = body;\n }\n}\n\nfunction stripTrailingSlashes(url: string): string {\n let end = url.length;\n while (end > 0 && url[end - 1] === '/') end--;\n return url.slice(0, end);\n}\n\n/** Encodes an absolute sandbox path for the `/file/*` route, which omits the leading slash. */\nfunction encodeFilePath(absolutePath: string): string {\n let start = 0;\n while (start < absolutePath.length && absolutePath[start] === '/') start++;\n return absolutePath\n .slice(start)\n .split('/')\n .map(segment => encodeURIComponent(segment))\n .join('/');\n}\n\n/**\n * Client for the Cloudflare Sandbox Bridge Worker.\n *\n * @see https://developers.cloudflare.com/sandbox/bridge/http-api/\n */\nexport class CloudflareSandboxBridgeClient {\n readonly baseUrl: string;\n private readonly apiToken?: string;\n private readonly fetchImpl: typeof globalThis.fetch;\n\n constructor(options: CloudflareSandboxBridgeClientOptions) {\n this.baseUrl = stripTrailingSlashes(options.baseUrl);\n this.apiToken = options.apiToken;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n /** `POST /v1/sandbox` */\n async createSandbox(): Promise<string> {\n const created = await this.request<{ id: string }>('/v1/sandbox', { method: 'POST' });\n return created.id;\n }\n\n /** `GET /v1/sandbox/:id/running` */\n async isRunning(id: string): Promise<boolean> {\n const status = await this.request<{ running: boolean }>(`/v1/sandbox/${encodeURIComponent(id)}/running`, {});\n return status.running === true;\n }\n\n /** `DELETE /v1/sandbox/:id` */\n async deleteSandbox(id: string): Promise<void> {\n await this.request(`/v1/sandbox/${encodeURIComponent(id)}`, { method: 'DELETE' }, true);\n }\n\n /** `PUT /v1/sandbox/:id/file/*` — one file per request, raw bytes as the body. */\n async writeFile(id: string, absolutePath: string, content: Uint8Array | string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`,\n {\n method: 'PUT',\n body: content as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `GET /v1/sandbox/:id/file/*` — reads one file, returning its raw bytes. */\n async readFile(id: string, absolutePath: string): Promise<Uint8Array> {\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`, {});\n }\n\n /** `GET /v1/sandbox/:id/persist` — archives `/workspace`, returning raw tar bytes. */\n async persistWorkspace(id: string, options: CloudflarePersistWorkspaceOptions = {}): Promise<Uint8Array> {\n const query = options.excludes?.length ? `?excludes=${encodeURIComponent(options.excludes.join(','))}` : '';\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/persist${query}`, {});\n }\n\n /** `POST /v1/sandbox/:id/hydrate` — restores `/workspace` from a raw tar payload. */\n async hydrateWorkspace(id: string, tar: Uint8Array): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/hydrate`,\n {\n method: 'POST',\n body: tar as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/mount` — mounts an S3-compatible bucket as a local directory. */\n async mountBucket(id: string, request: CloudflareMountBucketRequest): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/mount`,\n {\n method: 'POST',\n body: JSON.stringify(request),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/unmount` — unmounts a previously mounted bucket. */\n async unmountBucket(id: string, mountPath: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/unmount`,\n {\n method: 'POST',\n body: JSON.stringify({ mountPath }),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/session` — creates an execution session, returning its id. */\n async createSession(id: string, request: CloudflareCreateSessionRequest = {}): Promise<CloudflareSession> {\n const body: Record<string, unknown> = {};\n if (request.cwd !== undefined) body.cwd = request.cwd;\n if (request.env !== undefined) body.env = request.env;\n if (request.sessionId !== undefined) body.id = request.sessionId;\n return this.request<CloudflareSession>(`/v1/sandbox/${encodeURIComponent(id)}/session`, {\n method: 'POST',\n body: JSON.stringify(body),\n headers: { 'content-type': 'application/json' },\n });\n }\n\n /** `DELETE /v1/sandbox/:id/session/:sessionId` — tears down an execution session. */\n async deleteSession(id: string, sessionId: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/session/${encodeURIComponent(sessionId)}`,\n { method: 'DELETE' },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/exec` — streams SSE events until `exit` or `error`. */\n async exec(\n id: string,\n request: CloudflareExecRequest,\n options: {\n signal?: AbortSignal;\n onEvent: (event: CloudflareCommandEvent) => void;\n },\n ): Promise<void> {\n const response = await this.fetchImpl(`${this.baseUrl}/v1/sandbox/${encodeURIComponent(id)}/exec`, {\n method: 'POST',\n headers: { ...this.headers(), 'content-type': 'application/json', accept: 'text/event-stream' },\n body: JSON.stringify({\n argv: request.argv,\n ...(request.timeoutMs === undefined ? {} : { timeout_ms: request.timeoutMs }),\n ...(request.cwd === undefined ? {} : { cwd: request.cwd }),\n }),\n signal: options.signal,\n });\n\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (!response.body) {\n throw new Error('Cloudflare Sandbox Bridge returned an empty command stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n buffer += decoder.decode(value, { stream: !done }).replace(/\\r\\n/g, '\\n');\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary !== -1) {\n this.emitBlock(buffer.slice(0, boundary), options.onEvent);\n buffer = buffer.slice(boundary + 2);\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n if (buffer.trim()) this.emitBlock(buffer, options.onEvent);\n }\n\n private emitBlock(block: string, onEvent: (event: CloudflareCommandEvent) => void): void {\n let eventName: string | undefined;\n const dataLines: string[] = [];\n for (const line of block.split('\\n')) {\n if (line.startsWith('event:')) eventName = line.slice(6).trim();\n else if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));\n }\n const data = dataLines.join('\\n');\n if (!eventName || !data) return;\n\n switch (eventName) {\n case 'stdout':\n case 'stderr':\n onEvent({ type: eventName, data: base64ToBytes(data) });\n return;\n case 'exit': {\n const parsed = safeJsonParse(data);\n onEvent({ type: 'exit', exitCode: typeof parsed?.exit_code === 'number' ? parsed.exit_code : 0 });\n return;\n }\n case 'error': {\n const parsed = safeJsonParse(data);\n onEvent({\n type: 'error',\n message: typeof parsed?.error === 'string' ? parsed.error : data,\n code: typeof parsed?.code === 'string' ? parsed.code : undefined,\n });\n return;\n }\n default:\n return;\n }\n }\n\n private headers(): Record<string, string> {\n return this.apiToken ? { authorization: `Bearer ${this.apiToken}` } : {};\n }\n\n private async request<T>(path: string, init: RequestInit, allowEmpty = false): Promise<T> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (allowEmpty || response.status === 204) return undefined as T;\n return response.json() as Promise<T>;\n }\n\n private async requestBytes(path: string, init: RequestInit): Promise<Uint8Array> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n return new Uint8Array(await response.arrayBuffer());\n }\n}\n\nfunction base64ToBytes(value: string): Uint8Array {\n return new Uint8Array(Buffer.from(value, 'base64'));\n}\n\nfunction safeJsonParse(value: string): Record<string, unknown> | undefined {\n try {\n return JSON.parse(value) as Record<string, unknown>;\n } catch {\n return undefined;\n }\n}\n","import { randomUUID } from 'node:crypto';\nimport { posix } from 'node:path';\nimport type {\n CommandResult,\n ExecuteCommandOptions,\n FilesystemMountConfig,\n MastraSandboxOptions,\n MountManager,\n MountResult,\n ProviderStatus,\n SandboxFileInput,\n SandboxInfo,\n WorkspaceFilesystem,\n} from '@mastra/core/workspace';\nimport { MastraSandbox, assertModesUnsupported } from '@mastra/core/workspace';\nimport {\n CloudflareSandboxBridgeClient,\n type CloudflareMountBucketRequest,\n type CloudflarePersistWorkspaceOptions,\n type CloudflareSandboxBridgeClientOptions,\n} from './bridge-client';\n\nconst DEFAULT_COMMAND_TIMEOUT_MS = 300_000;\nconst WORKSPACE_ROOT = '/workspace';\n\ntype InstructionsOption = string | ((options: { defaultInstructions: string }) => string);\ntype BridgeClient = Pick<\n CloudflareSandboxBridgeClient,\n | 'createSandbox'\n | 'isRunning'\n | 'deleteSandbox'\n | 'writeFile'\n | 'readFile'\n | 'persistWorkspace'\n | 'hydrateWorkspace'\n | 'mountBucket'\n | 'unmountBucket'\n | 'exec'\n>;\n\n/**\n * Mount config accepted by the Cloudflare bridge: any S3-compatible bucket\n * (R2, S3, MinIO, ...) as produced by `S3Filesystem.getMountConfig()`.\n * Declared structurally so this package does not depend on `@mastra/s3`.\n */\ninterface S3CompatibleMountConfig extends FilesystemMountConfig {\n type: 's3';\n bucket: string;\n region?: string;\n endpoint?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n sessionToken?: string;\n prefix?: string;\n readOnly?: boolean;\n}\n\n/** Allowlist pattern for mount paths, matching the other remote sandbox providers. */\nconst SAFE_MOUNT_PATH = /^\\/[a-zA-Z0-9_.\\-/]+$/;\n\nfunction validateMountPath(mountPath: string): void {\n if (!SAFE_MOUNT_PATH.test(mountPath)) {\n throw new Error(\n `Invalid mount path: ${mountPath}. Must be an absolute path with alphanumeric, dash, dot, underscore, or slash characters only.`,\n );\n }\n}\n\n/**\n * Translates a Workspace mount config into the bridge's mount request, or\n * explains why the bridge cannot serve it. The bridge mounts S3-compatible\n * buckets with s3fs; GCS and Azure mount configs have no equivalent route.\n */\nfunction toMountRequest(\n config: FilesystemMountConfig,\n mountPath: string,\n): { request: CloudflareMountBucketRequest } | { error: string } {\n if (config.type !== 's3') {\n return { error: `Cloudflare Sandbox can only mount S3-compatible buckets; got mount type \"${config.type}\"` };\n }\n const s3 = config as S3CompatibleMountConfig;\n if (s3.sessionToken) {\n return { error: 'Cloudflare Sandbox bucket mounts do not support temporary credentials (sessionToken)' };\n }\n if (Boolean(s3.accessKeyId) !== Boolean(s3.secretAccessKey)) {\n return { error: 'Cloudflare Sandbox bucket mounts need both accessKeyId and secretAccessKey, or neither' };\n }\n // The bridge treats a request with no endpoint as an R2 binding mount, so a\n // region-only AWS filesystem must resolve to an explicit S3 endpoint.\n const endpoint = s3.endpoint ?? (s3.region ? `https://s3.${s3.region}.amazonaws.com` : undefined);\n // The bridge requires the prefix to start with `/`; S3Filesystem emits `dir/`.\n const prefix = s3.prefix ? (s3.prefix.startsWith('/') ? s3.prefix : `/${s3.prefix}`) : undefined;\n return {\n request: {\n bucket: s3.bucket,\n mountPath,\n options: {\n endpoint,\n prefix,\n readOnly: s3.readOnly,\n credentials:\n s3.accessKeyId && s3.secretAccessKey\n ? { accessKeyId: s3.accessKeyId, secretAccessKey: s3.secretAccessKey }\n : undefined,\n },\n },\n };\n}\n\nexport interface CloudflareSandboxOptions extends Omit<MastraSandboxOptions, 'processes'> {\n /** URL of a deployed Cloudflare Sandbox Bridge Worker. */\n baseUrl: string;\n /** Bearer token matching the Worker's `SANDBOX_API_KEY` secret, when authentication is enabled. */\n apiToken?: string;\n /** Stable Mastra identifier for this sandbox instance. */\n id?: string;\n /** Existing Cloudflare sandbox ID to reconnect to instead of creating a sandbox. */\n sandboxId?: string;\n /** Human-readable name shown in Mastra sandbox metadata. */\n name?: string;\n /** Environment variables applied to every command. */\n env?: Record<string, string>;\n /** Working directory applied to every command. Must be under /workspace. */\n workingDirectory?: string;\n /** Default command timeout in milliseconds. */\n commandTimeout?: number;\n /** Custom instructions returned by getInstructions(). */\n instructions?: InstructionsOption;\n /** Custom fetch implementation, primarily for advanced networking setup and tests. */\n fetch?: CloudflareSandboxBridgeClientOptions['fetch'];\n /** Preconfigured Bridge client, primarily for tests. */\n client?: BridgeClient;\n}\n\n/**\n * Absolute path to the shell used to interpret bare command strings. Absolute so it\n * resolves even when a custom PATH excludes the standard system directories.\n */\nconst SHELL_PATH = '/bin/bash';\n\n/**\n * Builds the argv array sent to the bridge. The bridge applies ANSI-C quoting to\n * every element, so no local escaping is needed. Environment variables are applied\n * with `env`, which keeps each assignment a separate argv element.\n *\n * When no separate arguments are supplied (the shape the built-in Workspace\n * `execute_command` tool uses), `command` is a shell command string — pipes,\n * chaining, quoting, redirection — so it is run through a non-login shell rather\n * than treated as a single executable name. When explicit arguments are given,\n * each element stays a literal argv token.\n */\nfunction buildArgv(command: string, args: string[] | undefined, env: Record<string, string>): string[] {\n const assignments = Object.entries(env).map(([key, value]) => {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error(`Invalid environment variable name: ${key}`);\n return `${key}=${value}`;\n });\n const invocation = args && args.length > 0 ? [command, ...args] : [SHELL_PATH, '-c', command];\n return assignments.length ? ['env', ...assignments, ...invocation] : invocation;\n}\n\n/** Resolves a path inside /workspace, rejecting anything that escapes the workspace root. */\nfunction resolveWorkspacePath(path: string): string {\n const resolved = posix.resolve(WORKSPACE_ROOT, path);\n if (resolved !== WORKSPACE_ROOT && !resolved.startsWith(`${WORKSPACE_ROOT}/`)) {\n throw new Error(`Cloudflare Sandbox files must be written under ${WORKSPACE_ROOT}: ${path}`);\n }\n return resolved;\n}\n\nexport class CloudflareSandbox extends MastraSandbox {\n readonly id: string;\n readonly name: string;\n readonly provider = 'cloudflare-sandbox';\n status: ProviderStatus = 'pending';\n /** Created by MastraSandbox because this class implements mount(). */\n declare readonly mounts: MountManager;\n\n private readonly client: BridgeClient;\n private readonly commandTimeout: number;\n private readonly instructions?: InstructionsOption;\n private sandboxId?: string;\n private createdAt = new Date();\n private lastUsedAt?: Date;\n /** Shared across concurrent callers so a wake triggers a single re-mount pass. */\n private ensureMountsPromise?: Promise<void>;\n\n constructor(options: CloudflareSandboxOptions) {\n const name = options.name ?? 'Cloudflare Sandbox';\n super({ ...options, name });\n this.id = options.id ?? `cloudflare-sandbox-${randomUUID()}`;\n this.name = name;\n this.sandboxId = options.sandboxId;\n this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;\n this.instructions = options.instructions;\n this.client =\n options.client ??\n new CloudflareSandboxBridgeClient({ baseUrl: options.baseUrl, apiToken: options.apiToken, fetch: options.fetch });\n }\n\n async start(): Promise<void> {\n if (this.sandboxId) {\n // The bridge boots the container on demand, so a stopped container is not fatal.\n const running = await this.client.isRunning(this.sandboxId);\n if (!running) {\n this.logger?.debug(`Cloudflare sandbox ${this.sandboxId} is not running yet; it starts on first use`);\n }\n return;\n }\n this.sandboxId = await this.client.createSandbox();\n this.createdAt = new Date();\n }\n\n async stop(): Promise<void> {\n // The bridge exposes create/delete but no suspend operation. Stop detaches this\n // Mastra lifecycle while preserving the remote sandbox for later reconnection.\n }\n\n async destroy(): Promise<void> {\n if (!this.sandboxId) return;\n await this.client.deleteSandbox(this.sandboxId);\n this.sandboxId = undefined;\n }\n\n async executeCommand(command: string, args?: string[], options?: ExecuteCommandOptions): Promise<CommandResult> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n\n const startedAt = Date.now();\n const timeout = options?.timeout ?? this.commandTimeout;\n if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError('Command timeout must be positive');\n\n const controller = new AbortController();\n let didTimeout = false;\n const timer = setTimeout(() => {\n didTimeout = true;\n controller.abort();\n }, timeout);\n const signal = options?.abortSignal ? AbortSignal.any([controller.signal, options.abortSignal]) : controller.signal;\n\n // stdout and stderr are separate byte streams, so each needs its own streaming decoder.\n const stdoutDecoder = new TextDecoder();\n const stderrDecoder = new TextDecoder();\n let stdout = '';\n let stderr = '';\n let exitCode = 1;\n\n const env = Object.fromEntries(\n Object.entries({ ...this.getEnv(), ...options?.env }).filter(\n (entry): entry is [string, string] => entry[1] !== undefined,\n ),\n );\n\n try {\n await this.client.exec(\n sandboxId,\n {\n argv: buildArgv(command, args, env),\n timeoutMs: timeout,\n cwd: options?.cwd ?? this.workingDirectory,\n },\n {\n signal,\n onEvent: event => {\n switch (event.type) {\n case 'stdout': {\n const chunk = stdoutDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stdout += chunk;\n options?.onStdout?.(chunk);\n return;\n }\n case 'stderr': {\n const chunk = stderrDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stderr += chunk;\n options?.onStderr?.(chunk);\n return;\n }\n case 'exit':\n exitCode = event.exitCode;\n return;\n case 'error':\n stderr += event.message;\n options?.onStderr?.(event.message);\n return;\n }\n },\n },\n );\n } catch (error) {\n if (!signal.aborted) throw error;\n } finally {\n clearTimeout(timer);\n }\n\n // Flush each decoder so a trailing truncated multi-byte sequence isn't dropped.\n const stdoutTail = stdoutDecoder.decode();\n if (stdoutTail) {\n stdout += stdoutTail;\n options?.onStdout?.(stdoutTail);\n }\n const stderrTail = stderrDecoder.decode();\n if (stderrTail) {\n stderr += stderrTail;\n options?.onStderr?.(stderrTail);\n }\n\n this.lastUsedAt = new Date();\n return {\n command,\n args,\n success: exitCode === 0 && !signal.aborted,\n exitCode,\n stdout,\n stderr,\n executionTimeMs: Date.now() - startedAt,\n timedOut: didTimeout,\n killed: signal.aborted && !didTimeout,\n };\n }\n\n async writeFiles(files: SandboxFileInput[]): Promise<void> {\n assertModesUnsupported(files, 'Cloudflare');\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n // The bridge writes one file per request.\n for (const file of files) {\n await this.client.writeFile(sandboxId, resolveWorkspacePath(file.path), file.content);\n }\n this.lastUsedAt = new Date();\n }\n\n /** Reads a single file under /workspace, returning its raw bytes. */\n async readFile(path: string): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const bytes = await this.client.readFile(sandboxId, resolveWorkspacePath(path));\n this.lastUsedAt = new Date();\n return bytes;\n }\n\n /** Archives /workspace, returning raw tar bytes that can later restore it via hydrateWorkspace. */\n async persistWorkspace(options?: CloudflarePersistWorkspaceOptions): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const archive = await this.client.persistWorkspace(sandboxId, options);\n this.lastUsedAt = new Date();\n return archive;\n }\n\n /** Restores /workspace from a raw tar payload produced by persistWorkspace. */\n async hydrateWorkspace(tar: Uint8Array): Promise<void> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n await this.client.hydrateWorkspace(sandboxId, tar);\n this.lastUsedAt = new Date();\n }\n\n getInfo(): SandboxInfo {\n return {\n id: this.id,\n name: this.name,\n provider: this.provider,\n status: this.status,\n createdAt: this.createdAt,\n lastUsedAt: this.lastUsedAt,\n metadata: {\n sandboxId: this.sandboxId,\n bridgeBaseUrl: this.client instanceof CloudflareSandboxBridgeClient ? this.client.baseUrl : undefined,\n },\n };\n }\n\n /**\n * Mounts an S3-compatible bucket (R2, S3, MinIO, ...) at `mountPath` through\n * the bridge's mount route. Called by MountManager for each Workspace `mounts`\n * entry after start(). The Cloudflare Sandbox SDK forgets mounts when an idle\n * container is stopped and does not restore them on wake, so\n * {@link ensureMountsActive} re-mounts stale paths before each operation; that\n * makes mounted paths the durable part of the filesystem.\n */\n async mount(filesystem: WorkspaceFilesystem, mountPath: string): Promise<MountResult> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n\n const config = filesystem.getMountConfig?.();\n if (!config) {\n const error = `Filesystem \"${filesystem.id}\" does not provide a mount config`;\n this.mounts.set(mountPath, { filesystem, state: 'error', error });\n return { success: false, mountPath, error };\n }\n\n const translated = toMountRequest(config, mountPath);\n if ('error' in translated) {\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error: translated.error });\n return { success: false, mountPath, error: translated.error };\n }\n\n this.mounts.set(mountPath, { filesystem, state: 'mounting', config });\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n const error = cause instanceof Error ? cause.message : String(cause);\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error });\n return { success: false, mountPath, error };\n }\n this.mounts.set(mountPath, { filesystem, state: 'mounted', config });\n this.lastUsedAt = new Date();\n return { success: true, mountPath };\n }\n\n /** Unmounts a bucket previously mounted with {@link mount}. */\n async unmount(mountPath: string): Promise<void> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n await this.client.unmountBucket(sandboxId, mountPath);\n this.mounts.delete(mountPath);\n this.lastUsedAt = new Date();\n }\n\n getInstructions(): string {\n const mounted = [...this.mounts.entries].filter(([, entry]) => entry.state === 'mounted').map(([path]) => path);\n const defaultInstructions =\n mounted.length > 0\n ? `Commands execute in a remote Cloudflare Sandbox. The container sleeps when idle and files under /workspace do NOT survive between commands, except under the mounted paths: ${mounted.join(', ')}. Keep anything that must persist under a mounted path.`\n : 'Commands execute in a remote Cloudflare Sandbox. Use /workspace as scratch space only: the container sleeps when idle and files under /workspace do NOT survive between commands. Do not assume earlier files still exist.';\n return typeof this.instructions === 'function'\n ? this.instructions({ defaultInstructions })\n : (this.instructions ?? defaultInstructions);\n }\n\n /**\n * A slept container boots fresh without its mounts: `@cloudflare/sandbox` keeps\n * `activeMounts` in memory and clears it on stop, so it never re-mounts on wake,\n * and `GET /running` still reports `true` until the DO next talks to the\n * container. Before any operation that reads or writes the filesystem, probe the\n * mounted paths with `mountpoint` and re-mount the ones that are gone. The pass\n * is shared across concurrent callers, and there is no probe when nothing is\n * mounted.\n */\n private ensureMountsActive(sandboxId: string): Promise<void> {\n const mountedPaths = [...this.mounts.entries]\n .filter(([, entry]) => entry.state === 'mounted')\n .map(([mountPath]) => mountPath);\n if (mountedPaths.length === 0) return Promise.resolve();\n if (!this.ensureMountsPromise) {\n this.ensureMountsPromise = this.remountStalePaths(sandboxId, mountedPaths).finally(() => {\n this.ensureMountsPromise = undefined;\n });\n }\n return this.ensureMountsPromise;\n }\n\n private async remountStalePaths(sandboxId: string, mountedPaths: string[]): Promise<void> {\n // Mount paths are validated against SAFE_MOUNT_PATH, so they are safe to embed\n // directly. `mountpoint -q` exits non-zero for a path that is no longer a mount,\n // and that path is echoed so a single exec reports every stale mount at once.\n const script = `for p in ${mountedPaths.join(' ')}; do mountpoint -q \"$p\" || echo \"$p\"; done`;\n const decoder = new TextDecoder();\n let stdout = '';\n await this.client.exec(\n sandboxId,\n { argv: [SHELL_PATH, '-c', script], timeoutMs: this.commandTimeout },\n {\n onEvent: event => {\n if (event.type === 'stdout') stdout += decoder.decode(event.data, { stream: true });\n },\n },\n );\n stdout += decoder.decode();\n\n const stalePaths = stdout\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n for (const mountPath of stalePaths) {\n const entry = this.mounts.get(mountPath);\n if (!entry?.config) continue;\n const translated = toMountRequest(entry.config, mountPath);\n if ('error' in translated) continue;\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n this.logger?.warn(`Failed to re-mount ${mountPath} after container wake`, { error: cause });\n }\n }\n }\n\n private requireSandboxId(): string {\n if (!this.sandboxId) throw new Error(`Cloudflare Sandbox ${this.id} has not been started`);\n return this.sandboxId;\n }\n}\n"],"mappings":";;;;AAgEA,IAAa,+BAAb,cAAkD,MAAM;CACtD;CACA;CAEA,YAAY,QAAgB,MAAc;EACxC,MAAM,6CAA6C,OAAO,KAAK,QAAQ,kBAAkB;EACzF,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,KAAK,OAAO;CACd;AACF;AAEA,SAAS,qBAAqB,KAAqB;CACjD,IAAI,MAAM,IAAI;CACd,OAAO,MAAM,KAAK,IAAI,MAAM,OAAO,KAAK;CACxC,OAAO,IAAI,MAAM,GAAG,GAAG;AACzB;;AAGA,SAAS,eAAe,cAA8B;CACpD,IAAI,QAAQ;CACZ,OAAO,QAAQ,aAAa,UAAU,aAAa,WAAW,KAAK;CACnE,OAAO,aACJ,MAAM,KAAK,CAAC,CACZ,MAAM,GAAG,CAAC,CACV,KAAI,YAAW,mBAAmB,OAAO,CAAC,CAAC,CAC3C,KAAK,GAAG;AACb;;;;;;AAOA,IAAa,gCAAb,MAA2C;CACzC;CACA;CACA;CAEA,YAAY,SAA+C;EACzD,KAAK,UAAU,qBAAqB,QAAQ,OAAO;EACnD,KAAK,WAAW,QAAQ;EACxB,KAAK,YAAY,QAAQ,SAAS,WAAW;CAC/C;;CAGA,MAAM,gBAAiC;EAErC,QAAO,MADe,KAAK,QAAwB,eAAe,EAAE,QAAQ,OAAO,CAAC,EAAA,CACrE;CACjB;;CAGA,MAAM,UAAU,IAA8B;EAE5C,QAAO,MADc,KAAK,QAA8B,eAAe,mBA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EACjB,KAAK,MAAM,aAAa,YAAY;GAClC,MAAM,QAAQ,KAAK,OAAO,IAAI,SAAS;GACvC,IAAI,CAAC,OAAO,QAAQ;GACpB,MAAM,aAAa,eAAe,MAAM,QAAQ,SAAS;GACzD,IAAI,WAAW,YAAY;GAC3B,IAAI;IACF,MAAM,KAAK,OAAO,YAAY,WAAW,WAAW,OAAO;GAC7D,SAAS,OAAO;IACd,KAAK,QAAQ,KAAK,sBAAsB,UAAU,wBAAwB,EAAE,OAAO,MAAM,CAAC;GAC5F;EACF;CACF;CAEA,mBAAmC;EACjC,IAAI,CAAC,KAAK,WAAW,MAAM,IAAI,MAAM,sBAAsB,KAAK,GAAG,sBAAsB;EACzF,OAAO,KAAK;CACd;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/bridge-client.ts","../src/sandbox.ts"],"sourcesContent":["export interface CloudflareSandboxBridgeClientOptions {\n baseUrl: string;\n apiToken?: string;\n fetch?: typeof globalThis.fetch;\n}\n\n/** Terminal and streaming events emitted by `POST /v1/sandbox/:id/exec`. */\nexport type CloudflareCommandEvent =\n | { type: 'stdout'; data: Uint8Array }\n | { type: 'stderr'; data: Uint8Array }\n | { type: 'exit'; exitCode: number }\n | { type: 'error'; message: string; code?: string };\n\nexport interface CloudflareExecRequest {\n /** Command and arguments. The bridge applies ANSI-C quoting to each element. */\n argv: string[];\n timeoutMs?: number;\n cwd?: string;\n}\n\nexport interface CloudflarePersistWorkspaceOptions {\n /** Relative paths (under /workspace) to exclude from the archive. */\n excludes?: string[];\n}\n\nexport interface CloudflareMountBucketCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface CloudflareMountBucketOptions {\n /** S3-compatible endpoint, e.g. `https://<account>.r2.cloudflarestorage.com`. */\n endpoint?: string;\n /** Mount the bucket read-only. */\n readOnly?: boolean;\n /** Only expose objects under this bucket prefix at the mount point. */\n prefix?: string;\n /** Storage provider hint, e.g. `r2`. */\n provider?: string;\n /** Explicit credentials; omitted when the Worker resolves them from secrets. */\n credentials?: CloudflareMountBucketCredentials;\n}\n\nexport interface CloudflareMountBucketRequest {\n /** Bucket name, e.g. `my-r2-bucket`. */\n bucket: string;\n /** Local filesystem path to mount at, e.g. `/mnt/data`. */\n mountPath: string;\n options?: CloudflareMountBucketOptions;\n}\n\nexport interface CloudflareCreateSessionRequest {\n /** Working directory the session starts in. */\n cwd?: string;\n /** Environment variables seeded into the session. */\n env?: Record<string, string>;\n /** Caller-chosen session id; must match `^[a-zA-Z0-9._-]{1,128}$`. Generated when omitted. */\n sessionId?: string;\n}\n\nexport interface CloudflareSession {\n id: string;\n}\n\nexport class CloudflareSandboxBridgeError extends Error {\n readonly status: number;\n readonly body: string;\n\n constructor(status: number, body: string) {\n super(`Cloudflare Sandbox Bridge request failed (${status}): ${body || 'empty response'}`);\n this.name = 'CloudflareSandboxBridgeError';\n this.status = status;\n this.body = body;\n }\n}\n\nfunction stripTrailingSlashes(url: string): string {\n let end = url.length;\n while (end > 0 && url[end - 1] === '/') end--;\n return url.slice(0, end);\n}\n\n/** Encodes an absolute sandbox path for the `/file/*` route, which omits the leading slash. */\nfunction encodeFilePath(absolutePath: string): string {\n let start = 0;\n while (start < absolutePath.length && absolutePath[start] === '/') start++;\n return absolutePath\n .slice(start)\n .split('/')\n .map(segment => encodeURIComponent(segment))\n .join('/');\n}\n\n/**\n * Client for the Cloudflare Sandbox Bridge Worker.\n *\n * @see https://developers.cloudflare.com/sandbox/bridge/http-api/\n */\nexport class CloudflareSandboxBridgeClient {\n readonly baseUrl: string;\n private readonly apiToken?: string;\n private readonly fetchImpl: typeof globalThis.fetch;\n\n constructor(options: CloudflareSandboxBridgeClientOptions) {\n this.baseUrl = stripTrailingSlashes(options.baseUrl);\n this.apiToken = options.apiToken;\n this.fetchImpl = options.fetch ?? globalThis.fetch;\n }\n\n /** `POST /v1/sandbox` */\n async createSandbox(): Promise<string> {\n const created = await this.request<{ id: string }>('/v1/sandbox', { method: 'POST' });\n return created.id;\n }\n\n /** `GET /v1/sandbox/:id/running` */\n async isRunning(id: string): Promise<boolean> {\n const status = await this.request<{ running: boolean }>(`/v1/sandbox/${encodeURIComponent(id)}/running`, {});\n return status.running === true;\n }\n\n /** `DELETE /v1/sandbox/:id` */\n async deleteSandbox(id: string): Promise<void> {\n await this.request(`/v1/sandbox/${encodeURIComponent(id)}`, { method: 'DELETE' }, true);\n }\n\n /** `PUT /v1/sandbox/:id/file/*` — one file per request, raw bytes as the body. */\n async writeFile(id: string, absolutePath: string, content: Uint8Array | string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`,\n {\n method: 'PUT',\n body: content as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `GET /v1/sandbox/:id/file/*` — reads one file, returning its raw bytes. */\n async readFile(id: string, absolutePath: string): Promise<Uint8Array> {\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/file/${encodeFilePath(absolutePath)}`, {});\n }\n\n /** `GET /v1/sandbox/:id/persist` — archives `/workspace`, returning raw tar bytes. */\n async persistWorkspace(id: string, options: CloudflarePersistWorkspaceOptions = {}): Promise<Uint8Array> {\n const query = options.excludes?.length ? `?excludes=${encodeURIComponent(options.excludes.join(','))}` : '';\n return this.requestBytes(`/v1/sandbox/${encodeURIComponent(id)}/persist${query}`, {});\n }\n\n /** `POST /v1/sandbox/:id/hydrate` — restores `/workspace` from a raw tar payload. */\n async hydrateWorkspace(id: string, tar: Uint8Array): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/hydrate`,\n {\n method: 'POST',\n body: tar as RequestInit['body'],\n headers: { 'content-type': 'application/octet-stream' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/mount` — mounts an S3-compatible bucket as a local directory. */\n async mountBucket(id: string, request: CloudflareMountBucketRequest): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/mount`,\n {\n method: 'POST',\n body: JSON.stringify(request),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/unmount` — unmounts a previously mounted bucket. */\n async unmountBucket(id: string, mountPath: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/unmount`,\n {\n method: 'POST',\n body: JSON.stringify({ mountPath }),\n headers: { 'content-type': 'application/json' },\n },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/session` — creates an execution session, returning its id. */\n async createSession(id: string, request: CloudflareCreateSessionRequest = {}): Promise<CloudflareSession> {\n const body: Record<string, unknown> = {};\n if (request.cwd !== undefined) body.cwd = request.cwd;\n if (request.env !== undefined) body.env = request.env;\n if (request.sessionId !== undefined) body.id = request.sessionId;\n return this.request<CloudflareSession>(`/v1/sandbox/${encodeURIComponent(id)}/session`, {\n method: 'POST',\n body: JSON.stringify(body),\n headers: { 'content-type': 'application/json' },\n });\n }\n\n /** `DELETE /v1/sandbox/:id/session/:sessionId` — tears down an execution session. */\n async deleteSession(id: string, sessionId: string): Promise<void> {\n await this.request(\n `/v1/sandbox/${encodeURIComponent(id)}/session/${encodeURIComponent(sessionId)}`,\n { method: 'DELETE' },\n true,\n );\n }\n\n /** `POST /v1/sandbox/:id/exec` — streams SSE events until `exit` or `error`. */\n async exec(\n id: string,\n request: CloudflareExecRequest,\n options: {\n signal?: AbortSignal;\n onEvent: (event: CloudflareCommandEvent) => void;\n },\n ): Promise<void> {\n const response = await this.fetchImpl(`${this.baseUrl}/v1/sandbox/${encodeURIComponent(id)}/exec`, {\n method: 'POST',\n headers: { ...this.headers(), 'content-type': 'application/json', accept: 'text/event-stream' },\n body: JSON.stringify({\n argv: request.argv,\n ...(request.timeoutMs === undefined ? {} : { timeout_ms: request.timeoutMs }),\n ...(request.cwd === undefined ? {} : { cwd: request.cwd }),\n }),\n signal: options.signal,\n });\n\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (!response.body) {\n throw new Error('Cloudflare Sandbox Bridge returned an empty command stream');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n while (true) {\n const { done, value } = await reader.read();\n buffer += decoder.decode(value, { stream: !done }).replace(/\\r\\n/g, '\\n');\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary !== -1) {\n this.emitBlock(buffer.slice(0, boundary), options.onEvent);\n buffer = buffer.slice(boundary + 2);\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n if (buffer.trim()) this.emitBlock(buffer, options.onEvent);\n }\n\n private emitBlock(block: string, onEvent: (event: CloudflareCommandEvent) => void): void {\n let eventName: string | undefined;\n const dataLines: string[] = [];\n for (const line of block.split('\\n')) {\n if (line.startsWith('event:')) eventName = line.slice(6).trim();\n else if (line.startsWith('data:')) dataLines.push(line.slice(5).replace(/^ /, ''));\n }\n const data = dataLines.join('\\n');\n if (!eventName || !data) return;\n\n switch (eventName) {\n case 'stdout':\n case 'stderr':\n onEvent({ type: eventName, data: base64ToBytes(data) });\n return;\n case 'exit': {\n const parsed = safeJsonParse(data);\n onEvent({ type: 'exit', exitCode: typeof parsed?.exit_code === 'number' ? parsed.exit_code : 0 });\n return;\n }\n case 'error': {\n const parsed = safeJsonParse(data);\n onEvent({\n type: 'error',\n message: typeof parsed?.error === 'string' ? parsed.error : data,\n code: typeof parsed?.code === 'string' ? parsed.code : undefined,\n });\n return;\n }\n default:\n return;\n }\n }\n\n private headers(): Record<string, string> {\n return this.apiToken ? { authorization: `Bearer ${this.apiToken}` } : {};\n }\n\n private async request<T>(path: string, init: RequestInit, allowEmpty = false): Promise<T> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n if (allowEmpty || response.status === 204) return undefined as T;\n return response.json() as Promise<T>;\n }\n\n private async requestBytes(path: string, init: RequestInit): Promise<Uint8Array> {\n const response = await this.fetchImpl(`${this.baseUrl}${path}`, {\n ...init,\n headers: { ...this.headers(), ...init.headers },\n });\n if (!response.ok) {\n throw new CloudflareSandboxBridgeError(response.status, await response.text());\n }\n return new Uint8Array(await response.arrayBuffer());\n }\n}\n\nfunction base64ToBytes(value: string): Uint8Array {\n return new Uint8Array(Buffer.from(value, 'base64'));\n}\n\nfunction safeJsonParse(value: string): Record<string, unknown> | undefined {\n try {\n return JSON.parse(value) as Record<string, unknown>;\n } catch {\n return undefined;\n }\n}\n","import { posix } from 'node:path';\nimport type {\n CommandResult,\n ExecuteCommandOptions,\n FilesystemMountConfig,\n MastraSandboxOptions,\n MountManager,\n MountResult,\n ProviderStatus,\n SandboxFileInput,\n SandboxInfo,\n WorkspaceFilesystem,\n} from '@mastra/core/workspace';\nimport { MastraSandbox, assertModesUnsupported } from '@mastra/core/workspace';\nimport {\n CloudflareSandboxBridgeClient,\n type CloudflareMountBucketRequest,\n type CloudflarePersistWorkspaceOptions,\n type CloudflareSandboxBridgeClientOptions,\n} from './bridge-client';\n\nconst DEFAULT_COMMAND_TIMEOUT_MS = 300_000;\nconst WORKSPACE_ROOT = '/workspace';\n\ntype InstructionsOption = string | ((options: { defaultInstructions: string }) => string);\ntype BridgeClient = Pick<\n CloudflareSandboxBridgeClient,\n | 'createSandbox'\n | 'isRunning'\n | 'deleteSandbox'\n | 'writeFile'\n | 'readFile'\n | 'persistWorkspace'\n | 'hydrateWorkspace'\n | 'mountBucket'\n | 'unmountBucket'\n | 'exec'\n>;\n\n/**\n * Mount config accepted by the Cloudflare bridge: any S3-compatible bucket\n * (R2, S3, MinIO, ...) as produced by `S3Filesystem.getMountConfig()`.\n * Declared structurally so this package does not depend on `@mastra/s3`.\n */\ninterface S3CompatibleMountConfig extends FilesystemMountConfig {\n type: 's3';\n bucket: string;\n region?: string;\n endpoint?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n sessionToken?: string;\n prefix?: string;\n readOnly?: boolean;\n}\n\n/** Allowlist pattern for mount paths, matching the other remote sandbox providers. */\nconst SAFE_MOUNT_PATH = /^\\/[a-zA-Z0-9_.\\-/]+$/;\n\nfunction validateMountPath(mountPath: string): void {\n if (!SAFE_MOUNT_PATH.test(mountPath)) {\n throw new Error(\n `Invalid mount path: ${mountPath}. Must be an absolute path with alphanumeric, dash, dot, underscore, or slash characters only.`,\n );\n }\n}\n\n/**\n * Translates a Workspace mount config into the bridge's mount request, or\n * explains why the bridge cannot serve it. The bridge mounts S3-compatible\n * buckets with s3fs; GCS and Azure mount configs have no equivalent route.\n */\nfunction toMountRequest(\n config: FilesystemMountConfig,\n mountPath: string,\n): { request: CloudflareMountBucketRequest } | { error: string } {\n if (config.type !== 's3') {\n return { error: `Cloudflare Sandbox can only mount S3-compatible buckets; got mount type \"${config.type}\"` };\n }\n const s3 = config as S3CompatibleMountConfig;\n if (s3.sessionToken) {\n return { error: 'Cloudflare Sandbox bucket mounts do not support temporary credentials (sessionToken)' };\n }\n if (Boolean(s3.accessKeyId) !== Boolean(s3.secretAccessKey)) {\n return { error: 'Cloudflare Sandbox bucket mounts need both accessKeyId and secretAccessKey, or neither' };\n }\n // The bridge treats a request with no endpoint as an R2 binding mount, so a\n // region-only AWS filesystem must resolve to an explicit S3 endpoint.\n const endpoint = s3.endpoint ?? (s3.region ? `https://s3.${s3.region}.amazonaws.com` : undefined);\n // The bridge requires the prefix to start with `/`; S3Filesystem emits `dir/`.\n const prefix = s3.prefix ? (s3.prefix.startsWith('/') ? s3.prefix : `/${s3.prefix}`) : undefined;\n return {\n request: {\n bucket: s3.bucket,\n mountPath,\n options: {\n endpoint,\n prefix,\n readOnly: s3.readOnly,\n credentials:\n s3.accessKeyId && s3.secretAccessKey\n ? { accessKeyId: s3.accessKeyId, secretAccessKey: s3.secretAccessKey }\n : undefined,\n },\n },\n };\n}\n\nexport interface CloudflareSandboxOptions extends Omit<MastraSandboxOptions, 'processes'> {\n /** URL of a deployed Cloudflare Sandbox Bridge Worker. */\n baseUrl: string;\n /** Bearer token matching the Worker's `SANDBOX_API_KEY` secret, when authentication is enabled. */\n apiToken?: string;\n /** Stable Mastra identifier for this sandbox instance. */\n id?: string;\n /** Existing Cloudflare sandbox ID to reconnect to instead of creating a sandbox. */\n sandboxId?: string;\n /** Human-readable name shown in Mastra sandbox metadata. */\n name?: string;\n /** Environment variables applied to every command. */\n env?: Record<string, string>;\n /** Working directory applied to every command. Must be under /workspace. */\n workingDirectory?: string;\n /** Default command timeout in milliseconds. */\n commandTimeout?: number;\n /** Custom instructions returned by getInstructions(). */\n instructions?: InstructionsOption;\n /** Custom fetch implementation, primarily for advanced networking setup and tests. */\n fetch?: CloudflareSandboxBridgeClientOptions['fetch'];\n /** Preconfigured Bridge client, primarily for tests. */\n client?: BridgeClient;\n}\n\n/**\n * Absolute path to the shell used to interpret bare command strings. Absolute so it\n * resolves even when a custom PATH excludes the standard system directories.\n */\nconst SHELL_PATH = '/bin/bash';\n\n/**\n * Builds the argv array sent to the bridge. The bridge applies ANSI-C quoting to\n * every element, so no local escaping is needed. Environment variables are applied\n * with `env`, which keeps each assignment a separate argv element.\n *\n * When no separate arguments are supplied (the shape the built-in Workspace\n * `execute_command` tool uses), `command` is a shell command string — pipes,\n * chaining, quoting, redirection — so it is run through a non-login shell rather\n * than treated as a single executable name. When explicit arguments are given,\n * each element stays a literal argv token.\n */\nfunction buildArgv(command: string, args: string[] | undefined, env: Record<string, string>): string[] {\n const assignments = Object.entries(env).map(([key, value]) => {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error(`Invalid environment variable name: ${key}`);\n return `${key}=${value}`;\n });\n const invocation = args && args.length > 0 ? [command, ...args] : [SHELL_PATH, '-c', command];\n return assignments.length ? ['env', ...assignments, ...invocation] : invocation;\n}\n\n/** Resolves a path inside /workspace, rejecting anything that escapes the workspace root. */\nfunction resolveWorkspacePath(path: string): string {\n const resolved = posix.resolve(WORKSPACE_ROOT, path);\n if (resolved !== WORKSPACE_ROOT && !resolved.startsWith(`${WORKSPACE_ROOT}/`)) {\n throw new Error(`Cloudflare Sandbox files must be written under ${WORKSPACE_ROOT}: ${path}`);\n }\n return resolved;\n}\n\nexport class CloudflareSandbox extends MastraSandbox {\n readonly id: string;\n readonly name: string;\n readonly provider = 'cloudflare-sandbox';\n status: ProviderStatus = 'pending';\n /** Created by MastraSandbox because this class implements mount(). */\n declare readonly mounts: MountManager;\n\n private readonly client: BridgeClient;\n private readonly commandTimeout: number;\n private readonly instructions?: InstructionsOption;\n private sandboxId?: string;\n private createdAt = new Date();\n private lastUsedAt?: Date;\n /** Shared across concurrent callers so a wake triggers a single re-mount pass. */\n private ensureMountsPromise?: Promise<void>;\n\n constructor(options: CloudflareSandboxOptions) {\n const name = options.name ?? 'Cloudflare Sandbox';\n super({ ...options, name });\n this.id = options.id ?? `cloudflare-sandbox-${globalThis.crypto.randomUUID()}`;\n this.name = name;\n this.sandboxId = options.sandboxId;\n this.commandTimeout = options.commandTimeout ?? DEFAULT_COMMAND_TIMEOUT_MS;\n this.instructions = options.instructions;\n this.client =\n options.client ??\n new CloudflareSandboxBridgeClient({ baseUrl: options.baseUrl, apiToken: options.apiToken, fetch: options.fetch });\n }\n\n async start(): Promise<void> {\n if (this.sandboxId) {\n // The bridge boots the container on demand, so a stopped container is not fatal.\n const running = await this.client.isRunning(this.sandboxId);\n if (!running) {\n this.logger?.debug(`Cloudflare sandbox ${this.sandboxId} is not running yet; it starts on first use`);\n }\n return;\n }\n this.sandboxId = await this.client.createSandbox();\n this.createdAt = new Date();\n }\n\n async stop(): Promise<void> {\n // The bridge exposes create/delete but no suspend operation. Stop detaches this\n // Mastra lifecycle while preserving the remote sandbox for later reconnection.\n }\n\n async destroy(): Promise<void> {\n if (!this.sandboxId) return;\n await this.client.deleteSandbox(this.sandboxId);\n this.sandboxId = undefined;\n }\n\n async executeCommand(command: string, args?: string[], options?: ExecuteCommandOptions): Promise<CommandResult> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n\n const startedAt = Date.now();\n const timeout = options?.timeout ?? this.commandTimeout;\n if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError('Command timeout must be positive');\n\n const controller = new AbortController();\n let didTimeout = false;\n const timer = setTimeout(() => {\n didTimeout = true;\n controller.abort();\n }, timeout);\n const signal = options?.abortSignal ? AbortSignal.any([controller.signal, options.abortSignal]) : controller.signal;\n\n // stdout and stderr are separate byte streams, so each needs its own streaming decoder.\n const stdoutDecoder = new TextDecoder();\n const stderrDecoder = new TextDecoder();\n let stdout = '';\n let stderr = '';\n let exitCode = 1;\n\n const env = Object.fromEntries(\n Object.entries({ ...this.getEnv(), ...options?.env }).filter(\n (entry): entry is [string, string] => entry[1] !== undefined,\n ),\n );\n\n try {\n await this.client.exec(\n sandboxId,\n {\n argv: buildArgv(command, args, env),\n timeoutMs: timeout,\n cwd: options?.cwd ?? this.workingDirectory,\n },\n {\n signal,\n onEvent: event => {\n switch (event.type) {\n case 'stdout': {\n const chunk = stdoutDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stdout += chunk;\n options?.onStdout?.(chunk);\n return;\n }\n case 'stderr': {\n const chunk = stderrDecoder.decode(event.data, { stream: true });\n if (!chunk) return;\n stderr += chunk;\n options?.onStderr?.(chunk);\n return;\n }\n case 'exit':\n exitCode = event.exitCode;\n return;\n case 'error':\n stderr += event.message;\n options?.onStderr?.(event.message);\n return;\n }\n },\n },\n );\n } catch (error) {\n if (!signal.aborted) throw error;\n } finally {\n clearTimeout(timer);\n }\n\n // Flush each decoder so a trailing truncated multi-byte sequence isn't dropped.\n const stdoutTail = stdoutDecoder.decode();\n if (stdoutTail) {\n stdout += stdoutTail;\n options?.onStdout?.(stdoutTail);\n }\n const stderrTail = stderrDecoder.decode();\n if (stderrTail) {\n stderr += stderrTail;\n options?.onStderr?.(stderrTail);\n }\n\n this.lastUsedAt = new Date();\n return {\n command,\n args,\n success: exitCode === 0 && !signal.aborted,\n exitCode,\n stdout,\n stderr,\n executionTimeMs: Date.now() - startedAt,\n timedOut: didTimeout,\n killed: signal.aborted && !didTimeout,\n };\n }\n\n async writeFiles(files: SandboxFileInput[]): Promise<void> {\n assertModesUnsupported(files, 'Cloudflare');\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n // The bridge writes one file per request.\n for (const file of files) {\n await this.client.writeFile(sandboxId, resolveWorkspacePath(file.path), file.content);\n }\n this.lastUsedAt = new Date();\n }\n\n /** Reads a single file under /workspace, returning its raw bytes. */\n async readFile(path: string): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const bytes = await this.client.readFile(sandboxId, resolveWorkspacePath(path));\n this.lastUsedAt = new Date();\n return bytes;\n }\n\n /** Archives /workspace, returning raw tar bytes that can later restore it via hydrateWorkspace. */\n async persistWorkspace(options?: CloudflarePersistWorkspaceOptions): Promise<Uint8Array> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n const archive = await this.client.persistWorkspace(sandboxId, options);\n this.lastUsedAt = new Date();\n return archive;\n }\n\n /** Restores /workspace from a raw tar payload produced by persistWorkspace. */\n async hydrateWorkspace(tar: Uint8Array): Promise<void> {\n const sandboxId = this.requireSandboxId();\n await this.ensureMountsActive(sandboxId);\n await this.client.hydrateWorkspace(sandboxId, tar);\n this.lastUsedAt = new Date();\n }\n\n getInfo(): SandboxInfo {\n return {\n id: this.id,\n name: this.name,\n provider: this.provider,\n status: this.status,\n createdAt: this.createdAt,\n lastUsedAt: this.lastUsedAt,\n metadata: {\n sandboxId: this.sandboxId,\n bridgeBaseUrl: this.client instanceof CloudflareSandboxBridgeClient ? this.client.baseUrl : undefined,\n },\n };\n }\n\n /**\n * Mounts an S3-compatible bucket (R2, S3, MinIO, ...) at `mountPath` through\n * the bridge's mount route. Called by MountManager for each Workspace `mounts`\n * entry after start(). The Cloudflare Sandbox SDK forgets mounts when an idle\n * container is stopped and does not restore them on wake, so\n * {@link ensureMountsActive} re-mounts stale paths before each operation; that\n * makes mounted paths the durable part of the filesystem.\n */\n async mount(filesystem: WorkspaceFilesystem, mountPath: string): Promise<MountResult> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n\n const config = filesystem.getMountConfig?.();\n if (!config) {\n const error = `Filesystem \"${filesystem.id}\" does not provide a mount config`;\n this.mounts.set(mountPath, { filesystem, state: 'error', error });\n return { success: false, mountPath, error };\n }\n\n const translated = toMountRequest(config, mountPath);\n if ('error' in translated) {\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error: translated.error });\n return { success: false, mountPath, error: translated.error };\n }\n\n this.mounts.set(mountPath, { filesystem, state: 'mounting', config });\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n const error = cause instanceof Error ? cause.message : String(cause);\n this.mounts.set(mountPath, { filesystem, state: 'error', config, error });\n return { success: false, mountPath, error };\n }\n this.mounts.set(mountPath, { filesystem, state: 'mounted', config });\n this.lastUsedAt = new Date();\n return { success: true, mountPath };\n }\n\n /** Unmounts a bucket previously mounted with {@link mount}. */\n async unmount(mountPath: string): Promise<void> {\n validateMountPath(mountPath);\n const sandboxId = this.requireSandboxId();\n await this.client.unmountBucket(sandboxId, mountPath);\n this.mounts.delete(mountPath);\n this.lastUsedAt = new Date();\n }\n\n getInstructions(): string {\n const mounted = [...this.mounts.entries].filter(([, entry]) => entry.state === 'mounted').map(([path]) => path);\n const defaultInstructions =\n mounted.length > 0\n ? `Commands execute in a remote Cloudflare Sandbox. The container sleeps when idle and files under /workspace do NOT survive between commands, except under the mounted paths: ${mounted.join(', ')}. Keep anything that must persist under a mounted path.`\n : 'Commands execute in a remote Cloudflare Sandbox. Use /workspace as scratch space only: the container sleeps when idle and files under /workspace do NOT survive between commands. Do not assume earlier files still exist.';\n return typeof this.instructions === 'function'\n ? this.instructions({ defaultInstructions })\n : (this.instructions ?? defaultInstructions);\n }\n\n /**\n * A slept container boots fresh without its mounts: `@cloudflare/sandbox` keeps\n * `activeMounts` in memory and clears it on stop, so it never re-mounts on wake,\n * and `GET /running` still reports `true` until the DO next talks to the\n * container. Before any operation that reads or writes the filesystem, probe the\n * mounted paths with `mountpoint` and re-mount the ones that are gone. The pass\n * is shared across concurrent callers, and there is no probe when nothing is\n * mounted.\n */\n private ensureMountsActive(sandboxId: string): Promise<void> {\n const mountedPaths = [...this.mounts.entries]\n .filter(([, entry]) => entry.state === 'mounted')\n .map(([mountPath]) => mountPath);\n if (mountedPaths.length === 0) return Promise.resolve();\n if (!this.ensureMountsPromise) {\n this.ensureMountsPromise = this.remountStalePaths(sandboxId, mountedPaths).finally(() => {\n this.ensureMountsPromise = undefined;\n });\n }\n return this.ensureMountsPromise;\n }\n\n private async remountStalePaths(sandboxId: string, mountedPaths: string[]): Promise<void> {\n // Mount paths are validated against SAFE_MOUNT_PATH, so they are safe to embed\n // directly. `mountpoint -q` exits non-zero for a path that is no longer a mount,\n // and that path is echoed so a single exec reports every stale mount at once.\n const script = `for p in ${mountedPaths.join(' ')}; do mountpoint -q \"$p\" || echo \"$p\"; done`;\n const decoder = new TextDecoder();\n let stdout = '';\n await this.client.exec(\n sandboxId,\n { argv: [SHELL_PATH, '-c', script], timeoutMs: this.commandTimeout },\n {\n onEvent: event => {\n if (event.type === 'stdout') stdout += decoder.decode(event.data, { stream: true });\n },\n },\n );\n stdout += decoder.decode();\n\n const stalePaths = stdout\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n for (const mountPath of stalePaths) {\n const entry = this.mounts.get(mountPath);\n if (!entry?.config) continue;\n const translated = toMountRequest(entry.config, mountPath);\n if ('error' in translated) continue;\n try {\n await this.client.mountBucket(sandboxId, translated.request);\n } catch (cause) {\n this.logger?.warn(`Failed to re-mount ${mountPath} after container wake`, { error: cause });\n }\n }\n }\n\n private requireSandboxId(): string {\n if (!this.sandboxId) throw new Error(`Cloudflare Sandbox ${this.id} has not been started`);\n return this.sandboxId;\n }\n}\n"],"mappings":";;;AAgEA,IAAa,+BAAb,cAAkD,MAAM;CACtD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AAC,KAAK,WAAW,MAAM,IAAI,MAAM,sBAAsB,KAAK,GAAG,sBAAsB;EACzF,OAAO,KAAK;CACd;AACF"}
|
package/dist/sandbox.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,qBAAqB,EAErB,oBAAoB,EACpB,YAAY,EACZ,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,aAAa,EAA0B,MAAM,wBAAwB,CAAC;AAC/E,OAAO,EACL,6BAA6B,EAE7B,KAAK,iCAAiC,EACtC,KAAK,oCAAoC,EAC1C,MAAM,iBAAiB,CAAC;AAKzB,KAAK,kBAAkB,GAAG,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE;IAAE,mBAAmB,EAAE,MAAM,CAAA;CAAE,KAAK,MAAM,CAAC,CAAC;AAC1F,KAAK,YAAY,GAAG,IAAI,CACtB,6BAA6B,EAC3B,eAAe,GACf,WAAW,GACX,eAAe,GACf,WAAW,GACX,UAAU,GACV,kBAAkB,GAClB,kBAAkB,GAClB,aAAa,GACb,eAAe,GACf,MAAM,CACT,CAAC;AAuEF,MAAM,WAAW,wBAAyB,SAAQ,IAAI,CAAC,oBAAoB,EAAE,WAAW,CAAC;IACvF,0DAA0D;IAC1D,OAAO,EAAE,MAAM,CAAC;IAChB,mGAAmG;IACnG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,oFAAoF;IACpF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,IAAI,CAAC,EAAE,MAAM,CAAC;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gG9G;IAEK,UAAU,CAAC,KAAK,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CASzD;IAED,qEAAqE;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAMhD;IAED,mGAAmG;IAC7F,gBAAgB,CAAC,OAAO,CAAC,EAAE,iCAAiC,GAAG,OAAO,CAAC,UAAU,CAAC,CAMvF;IAED,+EAA+E;IACzE,gBAAgB,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAKrD;IAED,OAAO,IAAI,WAAW,CAarB;IAED;;;;;;;OAOG;IACG,KAAK,CAAC,UAAU,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CA4BpF;IAED,+DAA+D;IACzD,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAM9C;IAED,eAAe,IAAI,MAAM,CASxB;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,kBAAkB;YAaZ,iBAAiB;IAmC/B,OAAO,CAAC,gBAAgB;CAIzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/cloudflare-sandbox",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1-alpha.0",
|
|
4
4
|
"description": "Cloudflare Sandbox provider for Mastra workspaces",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"tsdown": "0.22.9",
|
|
29
29
|
"typescript": "^7.0.2",
|
|
30
30
|
"vitest": "4.1.11",
|
|
31
|
-
"@internal/lint": "0.0.
|
|
32
|
-
"@internal/workspace-test-utils": "0.0.
|
|
33
|
-
"@internal/types-builder": "0.0.
|
|
34
|
-
"@mastra/core": "1.
|
|
31
|
+
"@internal/lint": "0.0.138",
|
|
32
|
+
"@internal/workspace-test-utils": "0.0.82",
|
|
33
|
+
"@internal/types-builder": "0.0.113",
|
|
34
|
+
"@mastra/core": "1.73.0-alpha.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@mastra/core": ">=1.67.0-0 <2.0.0-0"
|