@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.
114 lines (113 loc) • 4.45 kB
TypeScript
import { SandboxHandle } from './contracts.js';
/** The device URL and the code that the user types on that page. */
export interface DeviceCode {
verificationUri: string;
userCode?: string;
}
/** Structural. `providerKeys` in ai-harness accepts it as `KeyProvider.login`. */
export interface PlanLogin {
/** For `/keys` and messages, for example 'ChatGPT plan'. */
planLabel: string;
signIn: (ctx: {
onCode: (code: DeviceCode) => void;
signal?: AbortSignal;
}) => Promise<void>;
signOut: () => Promise<void>;
status: () => Promise<boolean>;
}
/** `executable`: a command, or [command, ...leadingArgs] (tests pass [process.execPath, script]). */
export type CliExecutable = string | ReadonlyArray<string>;
/**
* Makes a {@link PlanLogin} for a vendor CLI that has a device-code login,
* for example `codex login --device-auth`. The login lives in the CLI's own
* file. This code keeps no copy of the tokens.
*
* @param options.executable - The CLI command, or `[command, ...leadingArgs]`.
* @param options.loginArgs - Arguments for the device-code login.
* @param options.logoutArgs - Arguments for sign out.
* @param options.authFile - Returns the absolute host path of the CLI login file.
* @param options.planLabel - A name for messages, for example 'ChatGPT plan'.
* @param options.env - Extra environment variables for the CLI.
*
* `signIn` calls `onCode` once with the device URL and code, and resolves
* when the CLI exits with 0. It rejects with an `Error` that holds the CLI
* output when the CLI fails, and with `signal.reason` when the signal aborts.
*
* @example
* ```ts
* const login = cliPlanLogin({
* executable: 'codex',
* loginArgs: ['login', '--device-auth'],
* logoutArgs: ['logout'],
* authFile: () => join(homedir(), '.codex', 'auth.json'),
* planLabel: 'ChatGPT plan',
* })
* await login.signIn({ onCode: (code) => show(code) })
* ```
*/
export declare function cliPlanLogin(options: {
executable: CliExecutable;
loginArgs: ReadonlyArray<string>;
logoutArgs: ReadonlyArray<string>;
/** Absolute host path of the CLI login file. Resolved at call time. */
authFile: () => string;
planLabel: string;
env?: Record<string, string>;
}): PlanLogin;
/**
* Copies the host login file of a CLI into the sandbox home for one run.
* Call `release()` in a `finally` after the run. It writes the sandbox file
* back to the host when the CLI changed it (a refreshed token) and the host
* file did not change during the run. Then it removes the file from the
* sandbox, also when the read-back fails.
*
* Runs on the same sandbox share one copy. The last `release()` writes it
* back and removes it.
*
* A durable agent can outlive the stream of its host. For that run, call
* `release({ keep: true })` to leave the login in the sandbox, and mount with
* `adopt: true` on the host that attaches. That host then uses the login
* that is already there, which can hold a refreshed token.
*
* On a `local-process` sandbox, nothing is copied: the CLI reads the host
* file.
*
* @param sandbox - The sandbox of the run.
* @param options.hostPath - Absolute host path of the login file.
* @param options.sandboxHomePath - Path in the sandbox, relative to `$HOME`.
* @param options.label - The CLI name for the error, for example 'Codex'.
* @param options.connectCommand - The command that signs in, for example '/connect:codex'.
* @param options.adopt - Keep a login that is already in the sandbox. For a host that attaches to a running agent.
*
* Throws an `Error` when the host has no login file, unless `adopt` finds a
* login already in the sandbox.
*
* @example
* ```ts
* const login = await mountHostLogin(sandbox, {
* hostPath: join(homedir(), '.codex', 'auth.json'),
* sandboxHomePath: '.codex/auth.json',
* label: 'Codex',
* connectCommand: '/connect:codex',
* })
* try {
* await run()
* } finally {
* await login.release()
* }
* ```
*/
export declare function mountHostLogin(sandbox: SandboxHandle, options: HostLoginMountOptions): Promise<{
/** `keep: true` leaves the login in the sandbox for a host that attaches. */
release: (releaseOptions?: {
keep?: boolean;
}) => Promise<void>;
}>;
interface HostLoginMountOptions {
hostPath: string;
sandboxHomePath: string;
label: string;
connectCommand: string;
adopt?: boolean;
}
export {};