Skip to content

Commit 80682ed

Browse files
dnukumamrasclaude
andcommitted
feat(policy): add @ai-sdk/policy-opa package (OPA adapter for toolApproval)
Introduces a new monorepo package at packages/policy-opa that adapts Open Policy Agent policies to the AI SDK's public toolApproval callback. The package sits entirely on top of the published surface; no edits to packages/ai, packages/provider, or packages/provider-utils. Engine-neutral root exports a PolicyClient interface, shadow() for safe policy rollout with fire-and-forget telemetry, and wrapMcpTools() for making approval configuration total over a discovered tool surface. The ./opa subpath ships opaPolicy / optionalOpaPolicy (Rego-as-code), the wasmPolicyClient and httpPolicyClient backends (lazy-loaded optional peer deps), opaCapabilityMiddleware for fail-closed model-level tool filtering, and normalizeOpaDecision for users who call OPA themselves. Transitive enforcement (coarse dispatchers like bash / http.request / MCP proxies) is handled inside the user's toolApproval by parsing the dispatcher input and routing to the same Rego rule that gates the direct tool. README documents two DRY forms: a shared TS predicate and a shared Rego helper rule. Includes an end-to-end integration test (generateText + MockLanguageModelV3) plus a runnable example at examples/mock/basic.ts that demonstrates allow / deny / transitive-deny paths. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 9b96132 commit 80682ed

33 files changed

Lines changed: 2874 additions & 2 deletions

.changeset/policy-opa-package.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@ai-sdk/policy-opa': patch
3+
---
4+
5+
Introduce `@ai-sdk/policy-opa`, an Open Policy Agent adapter for the
6+
`toolApproval` callback on `generateText` / `streamText` / `ToolLoopAgent`.
7+
8+
Engine-neutral root exports a `PolicyClient` interface, `shadow()` for safe
9+
policy rollout with fire-and-forget telemetry, and `wrapMcpTools()` for making
10+
approval configuration total over a discovered tool surface. The `./opa`
11+
subpath ships `opaPolicy` / `optionalOpaPolicy` (Rego-as-code authorization),
12+
`wasmPolicyClient` and `httpPolicyClient` backends (lazy-loaded optional peer
13+
deps), `opaCapabilityMiddleware` for fail-closed model-level tool filtering,
14+
and `normalizeOpaDecision` for users who call OPA themselves.
15+
16+
Sits entirely on top of the public SDK surface, with no changes to `ai`,
17+
`@ai-sdk/provider`, or `@ai-sdk/provider-utils`. Transitive enforcement
18+
(coarse dispatchers like `bash` / `http.request` / MCP proxies) is handled
19+
inside the user's `toolApproval` by parsing the dispatcher input and routing
20+
to the same Rego rule that gates the direct tool.

packages/policy-opa/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @ai-sdk/policy

0 commit comments

Comments
 (0)