UNPKG

@tanstack/ai-sandbox

Version:

Provider-agnostic sandbox layer for TanStack AI — run harness adapters inside isolated sandboxes (defineSandbox, defineWorkspace, withSandbox) with a uniform SandboxHandle, workspace bootstrap, policy, and resumable lifecycle.

137 lines (136 loc) • 5.49 kB
import { ModelMessage } from '@tanstack/ai'; import { SandboxHandle } from './contracts.js'; import { SandboxCheckpoint, SandboxCheckpointStore } from './checkpoint-store.js'; import { SandboxSnapshotBundle, SandboxSnapshotPolicy } from './snapshots.js'; export interface CaptureSandboxCheckpointInput { /** The sandbox to read the files from. */ handle: SandboxHandle; /** Where the checkpoint is stored. */ checkpoints: SandboxCheckpointStore; /** Where the file bytes are stored. */ blobs: SandboxSnapshotBundle['blobs']; /** The thread that gets the new checkpoint. */ threadId: string; /** Checkpoint id. Default: `checkpoint-<random UUID>`. */ id?: string; /** The folder to capture. Default: `/workspace`. */ workspaceRoot?: string; /** Which files to keep. Default: all files except `.git`, `node_modules` and `.env*`. */ policy?: SandboxSnapshotPolicy; /** A name for the checkpoint. With a label, the reason is `named`. */ label?: string; /** The messages to keep with the files. Default: `[]`. */ conversation?: ReadonlyArray<ModelMessage>; /** Secret values. The capture replaces them with zero bytes in the stored files. */ secrets?: Readonly<Record<string, string>>; /** Stops the capture before it writes the checkpoint. */ signal?: AbortSignal; } export interface RestoreSandboxCheckpointInput { /** The sandbox to write the files into. */ handle: SandboxHandle; /** Where the checkpoint is stored. */ checkpoints: SandboxCheckpointStore; /** Where the file bytes are stored. */ blobs: SandboxSnapshotBundle['blobs']; /** The thread of the checkpoint. */ threadId: string; /** The checkpoint to restore. Default: the head of the thread. */ checkpointId?: string; /** The folder to restore into. Default: `/workspace`. */ workspaceRoot?: string; /** Use the same policy as the capture. */ policy?: SandboxSnapshotPolicy; } export interface SweepSandboxBlobsInput { /** The store that counts which blobs a checkpoint uses. */ checkpoints: SandboxCheckpointStore; /** The blob storage to clean. */ blobs: { /** * All stored keys that start with `prefix`, with the upload time in * milliseconds. Collect all pages before you return. With R2, use * `objects[].key` and `objects[].uploaded.getTime()` from each page. */ list: (prefix: string) => Promise<ReadonlyArray<{ key: string; uploadedAt: number; }>>; delete: (key: string) => Promise<unknown>; }; /** Keep blobs that are younger than this, in milliseconds. */ olderThanMs: number; /** The clock. Default: `Date.now`. */ now?: () => number; } /** * Saves the files of a sandbox as a new checkpoint at the head of a thread. * It works on any `SandboxHandle`, also outside `withSandbox`. * * It takes the writer lease of the thread for the full capture. If another * writer holds the lease, it throws `SandboxCheckpointWriterConflictError`. * If the head moves during the capture, the store throws * `SandboxCheckpointConflictError`. A bad file in the sandbox throws * `SandboxSnapshotError`. * * ```ts * const checkpoint = await captureSandboxCheckpoint({ * handle, * checkpoints, * blobs, * threadId, * label: 'before-migration', * }) * ``` */ export declare function captureSandboxCheckpoint(input: CaptureSandboxCheckpointInput): Promise<SandboxCheckpoint>; /** * Writes the files of a checkpoint into a sandbox. The sandbox can be a new * one, for example a new container after a crash. * * Without `checkpointId`, it restores the head of the thread. When the thread * has no checkpoint, it returns `null` and does not touch the sandbox. * When `checkpointId` does not exist or is on another thread, it throws * `SandboxSnapshotError` with code `SANDBOX_SNAPSHOT_MISSING_CHECKPOINT`. * * Restore only into a sandbox that no other process writes to during the * restore. Files in the folder that are not in the checkpoint are removed. * * ```ts * const restored = await restoreSandboxCheckpoint({ * handle: newHandle, * checkpoints, * blobs, * threadId, * }) * if (restored === null) { * // First run of this thread. Nothing to restore. * } * ``` */ export declare function restoreSandboxCheckpoint(input: RestoreSandboxCheckpointInput): Promise<SandboxCheckpoint | null>; /** * Deletes stored blobs that no checkpoint uses. It looks only at keys under * `sandbox-files/` and `sandbox-artifacts/`, and returns the deleted keys, * sorted. * * A capture puts its blobs first and appends the checkpoint after. Until the * append, a new blob has no reference. So the sweep keeps every blob that is * younger than `olderThanMs`. Make `olderThanMs` longer than your longest * capture. * * A capture does not upload a blob that already exists. So a capture that * runs during the sweep can use an old blob that the sweep deletes. Run the * sweep when no capture runs, for example from a nightly job. * * Throws `RangeError` when `olderThanMs` is not a finite number of 0 or more. * * ```ts * const deleted = await sweepSandboxBlobs({ * checkpoints, * blobs: { list: listAllKeys, delete: (key) => bucket.delete(key) }, * olderThanMs: 24 * 60 * 60 * 1000, * }) * ``` */ export declare function sweepSandboxBlobs(input: SweepSandboxBlobsInput): Promise<string[]>;