@ai-sdk/policy-opa 1.0.120 → 1.0.123
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/index.d.ts +113 -104
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +435 -279
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @ai-sdk/policy
|
|
2
2
|
|
|
3
|
+
## 1.0.123
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ede5b89: chore: migrate package builds from tsup to tsdown
|
|
8
|
+
- Updated dependencies [05cdac6]
|
|
9
|
+
- Updated dependencies [4514fc1]
|
|
10
|
+
- Updated dependencies [ede5b89]
|
|
11
|
+
- Updated dependencies [2a625cf]
|
|
12
|
+
- Updated dependencies [50a26d5]
|
|
13
|
+
- ai@7.0.123
|
|
14
|
+
- @ai-sdk/provider@4.0.20
|
|
15
|
+
- @ai-sdk/provider-utils@5.0.52
|
|
16
|
+
|
|
17
|
+
## 1.0.122
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [f3575f8]
|
|
22
|
+
- Updated dependencies [27ab8d4]
|
|
23
|
+
- ai@7.0.122
|
|
24
|
+
|
|
25
|
+
## 1.0.121
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- Updated dependencies [c5e90bb]
|
|
30
|
+
- Updated dependencies [c2511c1]
|
|
31
|
+
- Updated dependencies [868c475]
|
|
32
|
+
- Updated dependencies [119536f]
|
|
33
|
+
- Updated dependencies [9941f32]
|
|
34
|
+
- ai@7.0.121
|
|
35
|
+
- @ai-sdk/provider-utils@5.0.51
|
|
36
|
+
|
|
3
37
|
## 1.0.120
|
|
4
38
|
|
|
5
39
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ToolApprovalConfiguration, ToolApprovalStatus } from
|
|
3
|
-
import { LanguageModelV4CallOptions, LanguageModelV4Middleware } from
|
|
4
|
-
|
|
1
|
+
import { Context, InferToolSetContext, ModelMessage, Tool, ToolSet } from "@ai-sdk/provider-utils";
|
|
2
|
+
import { ToolApprovalConfiguration, ToolApprovalStatus } from "ai";
|
|
3
|
+
import { LanguageModelV4CallOptions, LanguageModelV4Middleware } from "@ai-sdk/provider";
|
|
4
|
+
//#region src/policy-client.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* A generic client for evaluating a policy decision against some external
|
|
7
7
|
* engine (OPA, Cedar, OpenFGA, a remote HTTP rule service, etc.).
|
|
@@ -11,15 +11,16 @@ import { LanguageModelV4CallOptions, LanguageModelV4Middleware } from '@ai-sdk/p
|
|
|
11
11
|
* call sites.
|
|
12
12
|
*/
|
|
13
13
|
interface PolicyClient {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Evaluate the given input against the policy identified by `path` and
|
|
16
|
+
* return the decision payload the engine emitted. Adapters are responsible
|
|
17
|
+
* for interpreting that payload. For OPA, `opaPolicy` normalizes it into
|
|
18
|
+
* the SDK's `ToolApprovalStatus` shape.
|
|
19
|
+
*/
|
|
20
|
+
evaluate<TInput = unknown, TResult = unknown>(path: string, input: TInput): Promise<TResult>;
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/policy-decision.d.ts
|
|
23
24
|
/**
|
|
24
25
|
* Narrowed object form of the SDK's `ToolApprovalStatus`.
|
|
25
26
|
*
|
|
@@ -29,18 +30,19 @@ interface PolicyClient {
|
|
|
29
30
|
* `shadow` can rely on a single discriminant.
|
|
30
31
|
*/
|
|
31
32
|
type PolicyDecision = {
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
type: 'approved';
|
|
34
|
+
reason?: string;
|
|
34
35
|
} | {
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
type: 'denied';
|
|
37
|
+
reason?: string;
|
|
37
38
|
} | {
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
type: 'user-approval';
|
|
40
|
+
reason?: string;
|
|
40
41
|
} | {
|
|
41
|
-
|
|
42
|
+
type: 'not-applicable';
|
|
42
43
|
};
|
|
43
|
-
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/shadow.d.ts
|
|
44
46
|
/**
|
|
45
47
|
* Event emitted by {@link shadow} every time the wrapped policy is evaluated.
|
|
46
48
|
*
|
|
@@ -50,21 +52,21 @@ type PolicyDecision = {
|
|
|
50
52
|
* have blocked or escalated.
|
|
51
53
|
*/
|
|
52
54
|
interface PolicyDecisionEvent {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
55
|
+
/** Identifying info from the tool call being evaluated. */
|
|
56
|
+
toolCall: {
|
|
57
|
+
toolName: string;
|
|
58
|
+
toolCallId: string;
|
|
59
|
+
input: unknown;
|
|
60
|
+
};
|
|
61
|
+
/** The decision the wrapped policy returned, normalized to the object form. */
|
|
62
|
+
decision: PolicyDecision;
|
|
63
|
+
/** Whether the SDK will act on `decision` (true) or override to allow (false). */
|
|
64
|
+
enforced: boolean;
|
|
65
|
+
/** The decision the SDK actually acts on. Equals `decision` when enforcing,
|
|
66
|
+
* `{ type: 'approved' }` in shadow mode. */
|
|
67
|
+
effective: PolicyDecision;
|
|
68
|
+
/** ISO 8601 timestamp of the evaluation. */
|
|
69
|
+
timestamp: string;
|
|
68
70
|
}
|
|
69
71
|
/**
|
|
70
72
|
* Wrap a `toolApproval` in shadow mode so the policy is evaluated and the
|
|
@@ -100,14 +102,15 @@ interface PolicyDecisionEvent {
|
|
|
100
102
|
* );
|
|
101
103
|
* ```
|
|
102
104
|
*/
|
|
103
|
-
declare function shadow<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(approval: ToolApprovalConfiguration<TOOLS & ToolSet, RUNTIME_CONTEXT>, opts?: {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
105
|
+
export declare function shadow<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(approval: ToolApprovalConfiguration<TOOLS & ToolSet, RUNTIME_CONTEXT>, opts?: {
|
|
106
|
+
/** When true, the SDK acts on the policy's decision. When false (default),
|
|
107
|
+
* the SDK is told every call is approved. */
|
|
108
|
+
enforce?: boolean;
|
|
109
|
+
/** Invoked once per evaluated tool call with the captured decision. */
|
|
110
|
+
onDecision?: (event: PolicyDecisionEvent) => void | Promise<void>;
|
|
109
111
|
}): ToolApprovalConfiguration<TOOLS & ToolSet, RUNTIME_CONTEXT>;
|
|
110
|
-
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/wrap-mcp-tools.d.ts
|
|
111
114
|
type ApprovalLiteralStatus = Extract<ToolApprovalStatus, string>;
|
|
112
115
|
/**
|
|
113
116
|
* Result returned by {@link wrapMcpTools}: the original tool set plus a
|
|
@@ -115,8 +118,8 @@ type ApprovalLiteralStatus = Extract<ToolApprovalStatus, string>;
|
|
|
115
118
|
* supplied approval does not explicitly handle.
|
|
116
119
|
*/
|
|
117
120
|
interface WrappedMcpTools<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT extends Context | unknown | never> {
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
tools: TOOLS;
|
|
122
|
+
toolApproval: ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT>;
|
|
120
123
|
}
|
|
121
124
|
/**
|
|
122
125
|
* Apply a fallback approval policy to a discovered tool set so the resulting
|
|
@@ -150,10 +153,11 @@ interface WrappedMcpTools<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT ex
|
|
|
150
153
|
* await generateText({ model, tools, toolApproval, prompt });
|
|
151
154
|
* ```
|
|
152
155
|
*/
|
|
153
|
-
declare function wrapMcpTools<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(tools: TOOLS, approval: ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT>, opts?: {
|
|
154
|
-
|
|
156
|
+
export declare function wrapMcpTools<TOOLS extends Record<string, Tool>, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(tools: TOOLS, approval: ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT>, opts?: {
|
|
157
|
+
default?: ApprovalLiteralStatus;
|
|
155
158
|
}): WrappedMcpTools<TOOLS, RUNTIME_CONTEXT>;
|
|
156
|
-
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/opa/http-policy-client.d.ts
|
|
157
161
|
/**
|
|
158
162
|
* Construct a {@link PolicyClient} that talks to a running OPA server over
|
|
159
163
|
* HTTP using `@open-policy-agent/opa`.
|
|
@@ -168,11 +172,12 @@ declare function wrapMcpTools<TOOLS extends Record<string, Tool>, RUNTIME_CONTEX
|
|
|
168
172
|
* The `url` typically points at `http://localhost:8181` for a locally running
|
|
169
173
|
* OPA. `headers` is forwarded for Styra DAS / EOPA authentication.
|
|
170
174
|
*/
|
|
171
|
-
declare function httpPolicyClient(opts: {
|
|
172
|
-
|
|
173
|
-
|
|
175
|
+
export declare function httpPolicyClient(opts: {
|
|
176
|
+
url: string;
|
|
177
|
+
headers?: Record<string, string>;
|
|
174
178
|
}): PolicyClient;
|
|
175
|
-
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/opa/normalize-opa-decision.d.ts
|
|
176
181
|
/**
|
|
177
182
|
* Normalize an OPA evaluation result into the package's {@link PolicyDecision}
|
|
178
183
|
* shape.
|
|
@@ -190,15 +195,16 @@ declare function httpPolicyClient(opts: {
|
|
|
190
195
|
* blocking. Any other unrecognized result is denied so malformed policy
|
|
191
196
|
* output cannot silently bypass the approval gate.
|
|
192
197
|
*/
|
|
193
|
-
declare function normalizeOpaDecision(result: unknown): PolicyDecision;
|
|
194
|
-
|
|
198
|
+
export declare function normalizeOpaDecision(result: unknown): PolicyDecision;
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/opa/opa-capability-middleware.d.ts
|
|
195
201
|
/**
|
|
196
202
|
* Default OPA input shape passed to the capability-scoping rule.
|
|
197
203
|
* Override with `toInput` if your Rego expects a different schema.
|
|
198
204
|
*/
|
|
199
205
|
interface DefaultOpaCapabilityInput {
|
|
200
|
-
|
|
201
|
-
|
|
206
|
+
messages: LanguageModelV4CallOptions['prompt'];
|
|
207
|
+
providerOptions: LanguageModelV4CallOptions['providerOptions'];
|
|
202
208
|
}
|
|
203
209
|
/**
|
|
204
210
|
* Construct an experimental {@link LanguageModelV4Middleware} that narrows
|
|
@@ -238,26 +244,27 @@ interface DefaultOpaCapabilityInput {
|
|
|
238
244
|
* });
|
|
239
245
|
* ```
|
|
240
246
|
*/
|
|
241
|
-
declare function opaCapabilityMiddleware(opts: {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
247
|
+
export declare function opaCapabilityMiddleware(opts: {
|
|
248
|
+
client: PolicyClient;
|
|
249
|
+
path: string;
|
|
250
|
+
toInput?: (args: {
|
|
251
|
+
messages: LanguageModelV4CallOptions['prompt'];
|
|
252
|
+
providerOptions: LanguageModelV4CallOptions['providerOptions'];
|
|
253
|
+
}) => unknown;
|
|
248
254
|
}): LanguageModelV4Middleware;
|
|
249
|
-
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/opa/opa-policy.d.ts
|
|
250
257
|
/**
|
|
251
258
|
* The default shape passed to the OPA rule as `input` when no `toInput` is
|
|
252
259
|
* supplied. Rego rules can read `input.tool.name`, `input.args`, and so on.
|
|
253
260
|
*/
|
|
254
261
|
interface DefaultOpaInput {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
262
|
+
tool: {
|
|
263
|
+
name: string;
|
|
264
|
+
};
|
|
265
|
+
args: unknown;
|
|
266
|
+
messages: ReadonlyArray<ModelMessage>;
|
|
267
|
+
runtimeContext: unknown;
|
|
261
268
|
}
|
|
262
269
|
/**
|
|
263
270
|
* Construct a {@link ToolApprovalConfiguration} backed by an OPA policy.
|
|
@@ -281,20 +288,20 @@ interface DefaultOpaInput {
|
|
|
281
288
|
* @param opts.path The Rego entrypoint that returns the decision object.
|
|
282
289
|
* @param opts.toInput Optional transformer to shape the OPA input.
|
|
283
290
|
*/
|
|
284
|
-
declare function opaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(opts: {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
291
|
+
export declare function opaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(opts: {
|
|
292
|
+
client: PolicyClient;
|
|
293
|
+
path: string;
|
|
294
|
+
toInput?: (args: {
|
|
295
|
+
toolCall: {
|
|
296
|
+
toolName: string;
|
|
297
|
+
toolCallId: string;
|
|
298
|
+
input: unknown;
|
|
299
|
+
};
|
|
300
|
+
tools: TOOLS | undefined;
|
|
301
|
+
toolsContext: InferToolSetContext<TOOLS>;
|
|
302
|
+
runtimeContext: RUNTIME_CONTEXT;
|
|
303
|
+
messages: ModelMessage[];
|
|
304
|
+
}) => unknown;
|
|
298
305
|
}): ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT>;
|
|
299
306
|
/**
|
|
300
307
|
* Optional variant of {@link opaPolicy} that gracefully degrades when no
|
|
@@ -324,22 +331,23 @@ declare function opaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT exte
|
|
|
324
331
|
* await generateText({ model, tools, toolApproval, prompt });
|
|
325
332
|
* ```
|
|
326
333
|
*/
|
|
327
|
-
declare function optionalOpaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(opts: {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
334
|
+
export declare function optionalOpaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context | unknown | never = unknown>(opts: {
|
|
335
|
+
client: PolicyClient | undefined;
|
|
336
|
+
path: string;
|
|
337
|
+
toInput?: (args: {
|
|
338
|
+
toolCall: {
|
|
339
|
+
toolName: string;
|
|
340
|
+
toolCallId: string;
|
|
341
|
+
input: unknown;
|
|
342
|
+
};
|
|
343
|
+
tools: TOOLS | undefined;
|
|
344
|
+
toolsContext: InferToolSetContext<TOOLS>;
|
|
345
|
+
runtimeContext: RUNTIME_CONTEXT;
|
|
346
|
+
messages: ModelMessage[];
|
|
347
|
+
}) => unknown;
|
|
341
348
|
}): ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT> | undefined;
|
|
342
|
-
|
|
349
|
+
//#endregion
|
|
350
|
+
//#region src/opa/wasm-policy-client.d.ts
|
|
343
351
|
/**
|
|
344
352
|
* Construct a {@link PolicyClient} that evaluates a compiled OPA WASM bundle
|
|
345
353
|
* in-process using `@open-policy-agent/opa-wasm`.
|
|
@@ -355,10 +363,11 @@ declare function optionalOpaPolicy<TOOLS extends ToolSet = ToolSet, RUNTIME_CONT
|
|
|
355
363
|
* bundle is built around a fixed entrypoint at `opa build` time. The path is
|
|
356
364
|
* recorded for audit logs but does not affect the evaluation.
|
|
357
365
|
*/
|
|
358
|
-
declare function wasmPolicyClient(opts: {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
366
|
+
export declare function wasmPolicyClient(opts: {
|
|
367
|
+
wasm: Uint8Array | ArrayBuffer;
|
|
368
|
+
/** Optional data document bundled into the policy (passed to `setData`). */
|
|
369
|
+
data?: unknown;
|
|
362
370
|
}): Promise<PolicyClient>;
|
|
363
|
-
|
|
364
|
-
export {
|
|
371
|
+
//#endregion
|
|
372
|
+
export type { DefaultOpaCapabilityInput, DefaultOpaInput, PolicyClient, PolicyDecision, PolicyDecisionEvent, WrappedMcpTools };
|
|
373
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/policy-client.ts","../src/policy-decision.ts","../src/shadow.ts","../src/wrap-mcp-tools.ts","../src/opa/http-policy-client.ts","../src/opa/normalize-opa-decision.ts","../src/opa/opa-capability-middleware.ts","../src/opa/opa-policy.ts","../src/opa/wasm-policy-client.ts"],"mappings":";;;;;;;;;;;;UAQiB;;;;;;;EAOf,SAAS,kBAAkB,mBACzB,cACA,OAAO,SACN,QAAQ;;;;;;;;;;;;KCVD;EACN;EAAkB;;EAClB;EAAgB;;EAChB;EAAuB;;EACvB;;;;;;;;;;;;UCaW;;EAEf;IAAY;IAAkB;IAAoB;;;EAElD,UAAU;;EAEV;;;EAGA,WAAW;;EAEX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAqCc,OACd,cAAc,eAAe,OAC7B,wBAAwB,qCAExB,UAAU,0BAA0B,QAAQ,SAAS,kBACrD;;;EAGE;;EAEA,cAAc,OAAO,+BAA+B;IAErD,0BAA0B,QAAQ,SAAS;;;KClFzC,wBAAwB,QAAQ;;;;;;UAOpB,gBACf,cAAc,eAAe,OAC7B,wBAAwB;EAExB,OAAO;EACP,cAAc,0BAA0B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAmCjC,aACd,cAAc,eAAe,OAC7B,wBAAwB,qCAExB,OAAO,OACP,UAAU,0BAA0B,OAAO,kBAC3C;EAAS,UAAU;IAClB,gBAAgB,OAAO;;;;;;;;;;;;;;;;;wBCzCV,iBAAiB;EAC/B;EACA,UAAU;IACR;;;;;;;;;;;;;;;;;;;;wBCAY,qBAAqB,kBAAkB;;;;;;;UCRtC;EACf,UAAU;EACV,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCH,wBAAwB;EACtC,QAAQ;EACR;EACA,WAAW;IACT,UAAU;IACV,iBAAiB;;IAEjB;;;;;;;UC9Ca;EACf;IAAQ;;EACR;EACA,UAAU,cAAc;EACxB;;;;;;;;;;;;;;;;;;;;;;;;wBAyBc,UACd,cAAc,UAAU,SACxB,wBAAwB,qCACxB;EACA,QAAQ;EACR;EACA,WAAW;IACT;MAAY;MAAkB;MAAoB;;IAClD,OAAO;IACP,cAAc,oBAAoB;IAClC,gBAAgB;IAChB,UAAU;;IAEV,0BAA0B,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0ErB,kBACd,cAAc,UAAU,SACxB,wBAAwB,qCACxB;EACA,QAAQ;EACR;EACA,WAAW;IACT;MAAY;MAAkB;MAAoB;;IAClD,OAAO;IACP,cAAc,oBAAoB;IAClC,gBAAgB;IAChB,UAAU;;IAEV,0BAA0B,OAAO;;;;;;;;;;;;;;;;;;wBCrHf,iBAAiB;EACrC,MAAM,aAAa;;EAEnB;IACE,QAAQ"}
|