@ai-sdk/policy-opa
v1.0.153
Published
Downloads
22,289
Readme
Policy-Based Tool Approvals
Policy-as-code authorization for AI SDK tool calls, powered by Open Policy Agent.
Setup
npm i ai @ai-sdk/policy-opa @ai-sdk/anthropic @open-policy-agent/opa-wasmUsage
import { generateText } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
import { opaPolicy, wasmPolicyClient } from '@ai-sdk/policy-opa';
import { readFile } from 'node:fs/promises';
// 1. Load the compiled policy bundle.
const wasm = await readFile('./policy.wasm');
const client = await wasmPolicyClient({ wasm });
// 2. Build the toolApproval configuration.
const toolApproval = opaPolicy({
client,
path: 'agent/call/decision',
});
// 3. Pass it to generateText. Everything else is normal.
const result = await generateText({
model: anthropic('claude-sonnet-5-5'),
tools: { git, bash, queryLogs },
toolApproval,
prompt: 'find the failing test and push the fix',
});See the Policy-Based Tool Approvals documentation for more information.
