@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.
94 lines (93 loc) • 4.01 kB
TypeScript
import { SandboxPolicy } from './policy.js';
import { ActivityDeltaEvent, ActivitySnapshotEvent, ModelMessage, StreamChunk } from '@tanstack/ai';
import { BridgeEventChannel } from './bridge-events.js';
/**
* The messages for the prompt of a harness adapter. `respondToSandboxApproval`
* starts a run with no new user message, and a harness prompt needs one. So
* when the last message is not from the user and `approvals` has a decision,
* this adds a user message that tells the agent to continue. Otherwise it
* returns `messages`.
*/
export declare function withApprovalContinuation(messages: Array<ModelMessage>, approvals: ReadonlyMap<string, boolean> | undefined): Array<ModelMessage>;
/**
* CUSTOM event name emitted when a harness action needs client approval.
*
* @deprecated Read the `sandbox.approval` activity row instead
* (`buildApprovalActivity`). The event is still sent, and a later breaking
* release removes it.
*/
export declare const APPROVAL_REQUESTED_EVENT = "approval-requested";
/** A stable, opaque approval id for a harness action. */
export declare function approvalId(input: {
provider: string;
kind: 'command' | 'fileWrite' | 'network' | 'tool';
target: string;
}): string;
export interface ResolveApprovalInput {
policy: SandboxPolicy | undefined;
/** Client approval decisions, keyed by `approvalId`. */
approvals: ReadonlyMap<string, boolean> | undefined;
/** Precomputed approval id for this action. */
id: string;
/** A shell command to match against `policy.commands`. */
command?: string;
/** Named workspace scripts for policy alias resolution. */
scripts?: Record<string, string>;
/** A coarse capability to match against `policy.capabilities`. */
capability?: 'fileWrite' | 'network';
}
export interface ApprovalOutcome {
decision: 'allow' | 'deny';
/** True when policy said `ask` and the client hasn't decided yet. */
needsApproval: boolean;
}
/** Resolve a harness permission request against policy + client approvals. */
export declare function resolveApproval(input: ResolveApprovalInput): ApprovalOutcome;
/** Build the AG-UI `approval-requested` CUSTOM event for a harness action. */
export declare function buildApprovalRequestedEvent(input: {
approvalId: string;
title: string;
threadId: string;
runId: string;
detail?: Record<string, unknown>;
}): StreamChunk;
/**
* Emit the pending `sandbox.approval` row and the deprecated
* `approval-requested` event on a live bridge channel. Call this from a
* permission callback so a crash or takeover still journals the row.
*/
export declare function emitPendingApproval(channel: Pick<BridgeEventChannel, 'emitCustomEvent' | 'emitActivity'>, input: {
approvalId: string;
title: string;
detail?: Record<string, unknown>;
}): void;
/**
* Build the `sandbox.approval` activity row for a harness action that waits
* for a client decision. The status is `pending`. The row id is
* `sandbox.approval:<approvalId>`, so a later run updates the same row.
*
* A harness adapter yields it right after the `approval-requested` event,
* with the same input.
*/
export declare function buildApprovalActivity(input: {
approvalId: string;
title: string;
threadId: string;
runId: string;
detail?: Record<string, unknown>;
}): ActivitySnapshotEvent;
/**
* Build one `ACTIVITY_DELTA` for each client decision of this provider. Each
* delta sets the `status` of the `sandbox.approval:<approvalId>` row to
* `granted` or `denied`.
*
* `approvals` holds every approval decision of the run, also the decisions
* of normal tool calls. Only the ids that start with `<provider>:` (see
* {@link approvalId}) make a delta. A harness adapter yields these right
* after its `RUN_STARTED`.
*/
export declare function approvalDecisionActivities(approvals: ReadonlyMap<string, boolean> | undefined, meta: {
provider: string;
threadId: string;
runId: string;
}): Array<ActivityDeltaEvent>;