Commit 3207233
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
File tree
- .changeset
- packages/policy-opa
- examples/mock
- src
- opa
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
0 commit comments