@handlebar/governance-schema 0.0.1
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/README.md +44 -0
- package/dist/audit/events.d.ts +459 -0
- package/dist/audit/governance-actions.d.ts +21 -0
- package/dist/audit.d.ts +17 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +12615 -0
- package/dist/rules/action.types.d.ts +9 -0
- package/dist/rules/condition.types.d.ts +121 -0
- package/dist/rules/constructors.d.ts +55 -0
- package/dist/rules/rule.types.d.ts +34 -0
- package/package.json +30 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strongly-typed rule condition/action schema for rule enforcement.
|
|
3
|
+
*
|
|
4
|
+
* Notes:
|
|
5
|
+
* - Conditions are composable via AND / OR / NOT
|
|
6
|
+
* - Actions are currently limited to "block" and "allow" but are modeled for future extension
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Case-insensitive glob pattern (e.g. "search-*", "*-prod", "exact-name")
|
|
10
|
+
*/
|
|
11
|
+
export type Glob = string;
|
|
12
|
+
/**
|
|
13
|
+
* JSON-safe value for condition parameters and custom function args.
|
|
14
|
+
*/
|
|
15
|
+
export type JSONValue = string | number | boolean | null | {
|
|
16
|
+
[k: string]: JSONValue;
|
|
17
|
+
} | JSONValue[];
|
|
18
|
+
/**
|
|
19
|
+
* Match on a tool's name.
|
|
20
|
+
* - glob comparator supports wildcard matching
|
|
21
|
+
* - in comparator permits list membership check
|
|
22
|
+
*/
|
|
23
|
+
export type ToolNameCondition = {
|
|
24
|
+
kind: "toolName";
|
|
25
|
+
op: "eq" | "neq" | "contains" | "startsWith" | "endsWith" | "glob";
|
|
26
|
+
value: string | Glob;
|
|
27
|
+
} | {
|
|
28
|
+
kind: "toolName";
|
|
29
|
+
op: "in";
|
|
30
|
+
value: (string | Glob)[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Match on tool tags present on the tool.
|
|
34
|
+
* - has: single tag must be present
|
|
35
|
+
* - anyOf: at least one tag present
|
|
36
|
+
* - allOf: every provided tag must be present
|
|
37
|
+
*/
|
|
38
|
+
export type ToolTagCondition = {
|
|
39
|
+
kind: "toolTag";
|
|
40
|
+
op: "has";
|
|
41
|
+
tag: string;
|
|
42
|
+
} | {
|
|
43
|
+
kind: "toolTag";
|
|
44
|
+
op: "anyOf";
|
|
45
|
+
tags: string[];
|
|
46
|
+
} | {
|
|
47
|
+
kind: "toolTag";
|
|
48
|
+
op: "allOf";
|
|
49
|
+
tags: string[];
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Scope for execution time measurement.
|
|
53
|
+
* - "tool": the single tool call duration
|
|
54
|
+
* - "total": end-to-end agent run (from start to now)
|
|
55
|
+
*/
|
|
56
|
+
export type ExecutionTimeScope = "tool" | "total";
|
|
57
|
+
/**
|
|
58
|
+
* Match against execution time thresholds (milliseconds).
|
|
59
|
+
*/
|
|
60
|
+
export type ExecutionTimeCondition = {
|
|
61
|
+
kind: "executionTime";
|
|
62
|
+
scope: ExecutionTimeScope;
|
|
63
|
+
op: "gt" | "gte" | "lt" | "lte" | "eq" | "neq";
|
|
64
|
+
ms: number;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Enforce sequencing constraints within the current run history.
|
|
68
|
+
* - mustHaveCalled: all listed tool name patterns must have been called earlier
|
|
69
|
+
* - mustNotHaveCalled: none of the listed patterns may have been called earlier
|
|
70
|
+
*/
|
|
71
|
+
export type SequenceCondition = {
|
|
72
|
+
kind: "sequence";
|
|
73
|
+
mustHaveCalled?: Glob[];
|
|
74
|
+
mustNotHaveCalled?: Glob[];
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Select tools for counting within a run.
|
|
78
|
+
* - by toolName: count calls whose name matches any provided glob patterns
|
|
79
|
+
* - by toolTag: count calls whose tool includes any of the provided tags
|
|
80
|
+
*/
|
|
81
|
+
export type MaxCallsSelector = {
|
|
82
|
+
by: "toolName";
|
|
83
|
+
patterns: Glob[];
|
|
84
|
+
} | {
|
|
85
|
+
by: "toolTag";
|
|
86
|
+
tags: string[];
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Assert a maximum number of calls within a run for the selected tools (inclusive).
|
|
90
|
+
*/
|
|
91
|
+
export type MaxCallsCondition = {
|
|
92
|
+
kind: "maxCalls";
|
|
93
|
+
selector: MaxCallsSelector;
|
|
94
|
+
max: number;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Delegate condition evaluation to a user-defined function.
|
|
98
|
+
* - `name` is resolved by the host SDK/application
|
|
99
|
+
* - `args` is an opaque, JSON-serializable payload consumed by user code
|
|
100
|
+
*/
|
|
101
|
+
export type CustomFunctionCondition = {
|
|
102
|
+
kind: "custom";
|
|
103
|
+
name: string;
|
|
104
|
+
args?: JSONValue;
|
|
105
|
+
};
|
|
106
|
+
export type AndCondition = {
|
|
107
|
+
kind: "and";
|
|
108
|
+
all: RuleCondition[];
|
|
109
|
+
};
|
|
110
|
+
export type OrCondition = {
|
|
111
|
+
kind: "or";
|
|
112
|
+
any: RuleCondition[];
|
|
113
|
+
};
|
|
114
|
+
export type NotCondition = {
|
|
115
|
+
kind: "not";
|
|
116
|
+
not: RuleCondition;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* The full condition algebra supported by the rule engine.
|
|
120
|
+
*/
|
|
121
|
+
export type RuleCondition = ToolNameCondition | ToolTagCondition | ExecutionTimeCondition | SequenceCondition | MaxCallsCondition | CustomFunctionCondition | AndCondition | OrCondition | NotCondition;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { RuleAction } from "./action.types";
|
|
2
|
+
import type { Glob, JSONValue, RuleCondition } from "./condition.types";
|
|
3
|
+
import type { Rule, RuleConfig, RuleWhen } from "./rule.types";
|
|
4
|
+
export declare const and: (...all: RuleCondition[]) => RuleCondition;
|
|
5
|
+
export declare const or: (...any: RuleCondition[]) => RuleCondition;
|
|
6
|
+
export declare const not: (cond: RuleCondition) => RuleCondition;
|
|
7
|
+
export declare const toolName: {
|
|
8
|
+
eq: (value: string | Glob) => RuleCondition;
|
|
9
|
+
neq: (value: string | Glob) => RuleCondition;
|
|
10
|
+
glob: (value: Glob) => RuleCondition;
|
|
11
|
+
in: (values: (string | Glob)[]) => RuleCondition;
|
|
12
|
+
startsWith: (value: string) => RuleCondition;
|
|
13
|
+
endsWith: (value: string) => RuleCondition;
|
|
14
|
+
contains: (value: string) => RuleCondition;
|
|
15
|
+
};
|
|
16
|
+
export declare const toolTag: {
|
|
17
|
+
has: (tag: string) => RuleCondition;
|
|
18
|
+
anyOf: (tags: string[]) => RuleCondition;
|
|
19
|
+
allOf: (tags: string[]) => RuleCondition;
|
|
20
|
+
};
|
|
21
|
+
export declare const execTime: {
|
|
22
|
+
gt: (scope: "tool" | "total", ms: number) => RuleCondition;
|
|
23
|
+
gte: (scope: "tool" | "total", ms: number) => RuleCondition;
|
|
24
|
+
lt: (scope: "tool" | "total", ms: number) => RuleCondition;
|
|
25
|
+
lte: (scope: "tool" | "total", ms: number) => RuleCondition;
|
|
26
|
+
};
|
|
27
|
+
export declare const sequence: (opts: {
|
|
28
|
+
mustHaveCalled?: Glob[];
|
|
29
|
+
mustNotHaveCalled?: Glob[];
|
|
30
|
+
}) => RuleCondition;
|
|
31
|
+
export declare const maxCalls: (opts: {
|
|
32
|
+
selector: {
|
|
33
|
+
by: "toolName";
|
|
34
|
+
patterns: Glob[];
|
|
35
|
+
} | {
|
|
36
|
+
by: "toolTag";
|
|
37
|
+
tags: string[];
|
|
38
|
+
};
|
|
39
|
+
max: number;
|
|
40
|
+
}) => RuleCondition;
|
|
41
|
+
export declare const custom: (name: string, args?: JSONValue) => RuleCondition;
|
|
42
|
+
export declare const block: () => RuleAction;
|
|
43
|
+
export declare const allow: () => RuleAction;
|
|
44
|
+
type BaseRuleInput = {
|
|
45
|
+
priority: number;
|
|
46
|
+
if: RuleCondition;
|
|
47
|
+
then: RuleAction[];
|
|
48
|
+
};
|
|
49
|
+
export declare const rule: ((when: RuleWhen, input: BaseRuleInput) => RuleConfig) & {
|
|
50
|
+
pre: (input: BaseRuleInput) => RuleConfig;
|
|
51
|
+
post: (input: BaseRuleInput) => RuleConfig;
|
|
52
|
+
both: (input: BaseRuleInput) => RuleConfig;
|
|
53
|
+
};
|
|
54
|
+
export declare function configToRule(config: RuleConfig): Rule;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import type { RuleAction } from "./action.types";
|
|
3
|
+
import type { RuleCondition } from "./condition.types";
|
|
4
|
+
/**
|
|
5
|
+
* Timing for rule evaluation relative to tool call lifecycle.
|
|
6
|
+
* - pre: evaluate before the tool executes
|
|
7
|
+
* - post: evaluate after the tool executes
|
|
8
|
+
* - both: evaluate both pre and post
|
|
9
|
+
*/
|
|
10
|
+
export type RuleWhen = "pre" | "post" | "both";
|
|
11
|
+
/**
|
|
12
|
+
* A single rule definition combining condition, actions, timing, and priority.
|
|
13
|
+
* This can be stored as JSONB or constructed/transmitted over the wire.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RuleConfigSchema: z.ZodObject<{
|
|
16
|
+
priority: z.ZodNumber;
|
|
17
|
+
when: z.ZodCustom<RuleWhen, RuleWhen>;
|
|
18
|
+
condition: z.ZodCustom<RuleCondition, RuleCondition>;
|
|
19
|
+
actions: z.ZodArray<z.ZodCustom<RuleAction, RuleAction>>;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
/**
|
|
22
|
+
* Rule object coming from API.
|
|
23
|
+
*/
|
|
24
|
+
export declare const RuleSchema: z.ZodIntersection<z.ZodObject<{
|
|
25
|
+
id: z.ZodUUID;
|
|
26
|
+
policy_id: z.ZodUUID;
|
|
27
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
28
|
+
priority: z.ZodNumber;
|
|
29
|
+
when: z.ZodCustom<RuleWhen, RuleWhen>;
|
|
30
|
+
condition: z.ZodCustom<RuleCondition, RuleCondition>;
|
|
31
|
+
actions: z.ZodArray<z.ZodCustom<RuleAction, RuleAction>>;
|
|
32
|
+
}, z.core.$strip>>;
|
|
33
|
+
export type RuleConfig = z.infer<typeof RuleConfigSchema>;
|
|
34
|
+
export type Rule = z.infer<typeof RuleSchema>;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@handlebar/governance-schema",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "bun run build.ts && tsc -p tsconfig.json"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"llm",
|
|
18
|
+
"agent",
|
|
19
|
+
"agentic",
|
|
20
|
+
"ai",
|
|
21
|
+
"sdk",
|
|
22
|
+
"governance",
|
|
23
|
+
"tool-calling",
|
|
24
|
+
"mcp",
|
|
25
|
+
"handlebar"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"zod": "^4.1.12"
|
|
29
|
+
}
|
|
30
|
+
}
|