Skip to content

Commit 3207233

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. Everything is exported from the package root. The engine-neutral core is 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 layer 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 3207233

32 files changed

Lines changed: 2870 additions & 2 deletions

.changeset/policy-opa-package.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
Everything is exported from the package root. The engine-neutral core is a
9+
`PolicyClient` interface, `shadow()` for safe policy rollout with
10+
fire-and-forget telemetry, and `wrapMcpTools()` for making approval
11+
configuration total over a discovered tool surface. The OPA layer ships
12+
`opaPolicy` / `optionalOpaPolicy` (Rego-as-code authorization),
13+
`wasmPolicyClient` and `httpPolicyClient` backends (lazy-loaded optional peer
14+
deps), `opaCapabilityMiddleware` for fail-closed model-level tool filtering,
15+
and `normalizeOpaDecision` for users who call OPA themselves.
16+
17+
Sits entirely on top of the public SDK surface, with no changes to `ai`,
18+
`@ai-sdk/provider`, or `@ai-sdk/provider-utils`. Transitive enforcement
19+
(coarse dispatchers like `bash` / `http.request` / MCP proxies) is handled
20+
inside the user's `toolApproval` by parsing the dispatcher input and routing
21+
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)