All integrations
Vercel AI SDK
Gate tool calls in the Vercel AI SDK behind Cheqpoint approval - works with useChat, generateText, and streamText.
Install
npm install @cheqpoint/sdk ainpm install @cheqpoint/sdk ai
Wrap a tool with Cheqpoint approval
TypeScript
import { tool } from "ai";
import { z } from "zod";
import { CheqpointClient } from "@cheqpoint/sdk";
const cheqpoint = new CheqpointClient({ apiKey: process.env.CHEQPOINT_API_KEY! });
export const processRefund = tool({
description: "Process a customer refund",
parameters: z.object({
orderId: z.string(),
amount: z.number().positive(),
reason: z.string(),
}),
execute: async ({ orderId, amount, reason }) => {
// Gate behind Cheqpoint approval
const approval = await cheqpoint.checkpoint({
action: "process_refund",
summary: `Refund £${amount} for order ${orderId}`,
details: { orderId, amount, reason },
riskLevel: amount > 500 ? "high" : "medium",
});
if (approval.status !== "APPROVED") {
return { error: `Refund rejected: ${approval.decisionNote ?? "No reason provided"}` };
}
// Execute the actual refund
return { success: true, orderId, amount, refundId: `ref_${Date.now()}` };
},
});import { tool } from "ai";
import { z } from "zod";
import { CheqpointClient } from "@cheqpoint/sdk";
const cheqpoint = new CheqpointClient({ apiKey: process.env.CHEQPOINT_API_KEY! });
export const processRefund = tool({
description: "Process a customer refund",
parameters: z.object({
orderId: z.string(),
amount: z.number().positive(),
reason: z.string(),
}),
execute: async ({ orderId, amount, reason }) => {
// Gate behind Cheqpoint approval
const approval = await cheqpoint.checkpoint({
action: "process_refund",
summary: `Refund £${amount} for order ${orderId}`,
details: { orderId, amount, reason },
riskLevel: amount > 500 ? "high" : "medium",
});
if (approval.status !== "APPROVED") {
return { error: `Refund rejected: ${approval.decisionNote ?? "No reason provided"}` };
}
// Execute the actual refund
return { success: true, orderId, amount, refundId: `ref_${Date.now()}` };
},
});Use in a Next.js API route
TypeScript
// app/api/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { processRefund } from "@/lib/tools/processRefund";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai("gpt-4o"),
messages,
tools: { processRefund },
maxSteps: 5,
});
return result.toDataStreamResponse();
}// app/api/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { processRefund } from "@/lib/tools/processRefund";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai("gpt-4o"),
messages,
tools: { processRefund },
maxSteps: 5,
});
return result.toDataStreamResponse();
}