Vercel AI SDK
Gate AI SDK tools with the call-level toolApproval option; approvals round-trip as tool-approval parts in the message list.
Code blocks on this page are excerpts of tested programs; each block's title names its file in examples/, where the complete program runs.
TypeScript only (the AI SDK is a TypeScript library).
Install from the registry (to run the repo's own examples, clone it as in the README):
npm install @viewportai/hitl ai zod # ai >= 7Verified against ai 7.0.182. The example uses MockLanguageModelV4, so it runs with no key.
Spread approval() into the call
approval(gate, tools, { scope }) returns { tools, toolApproval }: your tools with an execution-time check wrapped around
each execute, and the call-level toolApproval function (tool-level needsApproval is deprecated in AI SDK 7).
Allowed calls run, denied calls return the policy reason to the model as the tool result, and asks end the call with
a tool-approval-request part.
scope is required (a conversation or user id), unless an action hook sets the actor or tenant: tool call ids
come from the client, so an approval is only ever redeemed inside the scope it was asked in. Without either,
approval() throws hitl.scope_required.
const gate = new Gate(
{
approvers: { finance: { channel: "desk", members: ["desk:dana"] } },
rules: {
refunds: { allow: "issue_refund" },
"large-refunds": {
ask: "issue_refund",
when: { args: { amount: { gt: 50 } } },
approver: "finance",
reason: "Refunds over $50 need finance",
},
},
},
{ channels: [desk], clock: new FakeClock("2026-11-25T10:00:00Z") },
);
const issue_refund = tool({
description: "Refund an order",
inputSchema: z.object({ order_id: z.string(), amount: z.number() }),
execute: async ({ order_id, amount }) => {
refunded.push({ order_id, amount });
return `Refunded $${amount} on ${order_id}`;
},
});
// Tool call ids come from the client, so approvals are scoped to the conversation (or user) they belong to.
const gated = approval(gate, { issue_refund }, { scope: "conversation-42" }); // { tools, toolApproval }const messages: ModelMessage[] = [{ role: "user", content: "Refund A-1002, it arrived broken" }];
const first = await generateText({ model: new MockLanguageModelV4({ doGenerate: [refundCall] }), messages, ...gated });
messages.push(...first.response.messages); // ends with a tool-approval-request partContinue after a decision
The AI SDK is stateless: the state is the message list. Once the approval is decided through any channel,
responses(gate, messages, gated) appends tool-approval-response parts that carry the store's decision, and the
next call runs the tool, which consumes the approval once.
await gate.respond(requests[0]?.approval_id as string, approve({ by: "desk:dana" })); // from any channel
const next = await responses(gate, messages, gated); // adds tool-approval-response parts from the store
const second = await generateText({
model: new MockLanguageModelV4({ doGenerate: [answer] }),
messages: next,
...gated,
});
console.log(`agent: ${second.text}`); // agent: Refund sent.responses() answers only requests that have no tool result yet, and strips approval responses a client put in the
messages: the decision always comes from the store. Approval keys use the toolCallId.
Provider-executed tools ignore toolApproval in the AI SDK; the adapter warns about them when you call
approval().