Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

approvals.ts

97 lines3.7 KB

1/**
2 * Shared interactive-approval logic for harness adapters.
3 *
4 * Flow (rides chat()'s existing resume-based approval mechanism):
5 * 1. The agent (inside the sandbox) asks to run a risky action; the harness's
6 * host-side permission callback fires.
7 * 2. `resolveApproval` evaluates the sandbox policy: `allow`/`deny` are final;
8 * `ask` consults the client's approval decisions (threaded via
9 * `TextOptions.approvals`, keyed by a stable `approvalId`).
10 * 3. On `ask` with no decision yet, the adapter emits an `approval-requested`
11 * CUSTOM event (carrying the `approvalId`) and denies the action this turn.
12 * The client shows UI, then re-runs chat() with the decision in the message;
13 * the engine surfaces it as `approvals`, and the next run allows it.
14 *
15 * `approvalId` is stable for a given (provider, kind, target) so a client grant
16 * matches the same action on the resumed run.
17 */
18import { EventType, withTanstackMetadata } from '@tanstack/ai'
19import { evaluateCommand } from './policy'
20import type { SandboxPolicy } from './policy'
21import type { StreamChunk } from '@tanstack/ai'
22​
23/** CUSTOM event name emitted when a harness action needs client approval. */
24export const APPROVAL_REQUESTED_EVENT = 'approval-requested'
25​
26/** A stable, opaque approval id for a harness action. */
27export function approvalId(input: {
28 provider: string
29 kind: 'command' | 'fileWrite' | 'network' | 'tool'
30 target: string
31}): string {
32 return `${input.provider}:${input.kind}:${input.target}`
34​
35export interface ResolveApprovalInput {
36 policy: SandboxPolicy | undefined
37 /** Client approval decisions, keyed by `approvalId`. */
38 approvals: ReadonlyMap<string, boolean> | undefined
39 /** Precomputed approval id for this action. */
40 id: string
41 /** A shell command to match against `policy.commands`. */
42 command?: string
43 /** Named workspace scripts for policy alias resolution. */
44 scripts?: Record<string, string>
45 /** A coarse capability to match against `policy.capabilities`. */
46 capability?: 'fileWrite' | 'network'
48​
49export interface ApprovalOutcome {
50 decision: 'allow' | 'deny'
51 /** True when policy said `ask` and the client hasn't decided yet. */
52 needsApproval: boolean
54​
55/** Resolve a harness permission request against policy + client approvals. */
56export function resolveApproval(input: ResolveApprovalInput): ApprovalOutcome {
57 const base =
58 input.command !== undefined
59 ? evaluateCommand(input.command, input.policy, input.scripts)
60 : input.capability !== undefined
61 ? (input.policy?.capabilities?.[input.capability] ??
62 input.policy?.default ??
63 'ask')
64 : (input.policy?.default ?? 'ask')
65​
66 if (base === 'allow') return { decision: 'allow', needsApproval: false }
67 if (base === 'deny') return { decision: 'deny', needsApproval: false }
68​
69 // base === 'ask' — consult the client's decision.
70 const granted = input.approvals?.get(input.id)
71 if (granted === true) return { decision: 'allow', needsApproval: false }
72 if (granted === false) return { decision: 'deny', needsApproval: false }
73 return { decision: 'deny', needsApproval: true }
75​
76/** Build the AG-UI `approval-requested` CUSTOM event for a harness action. */
77export function buildApprovalRequestedEvent(input: {
78 approvalId: string
79 title: string
80 threadId: string
81 runId: string
82 detail?: Record<string, unknown>
83}): StreamChunk {
84 return withTanstackMetadata(
85 {
86 type: EventType.CUSTOM,
87 name: APPROVAL_REQUESTED_EVENT,
88 value: {
89 approvalId: input.approvalId,
90 title: input.title,
91 ...(input.detail ?? {}),
92 },
93 timestamp: Date.now(),
94 },
95 { threadId: input.threadId, runId: input.runId },
96 ) as StreamChunk