@vessel-agent/tool-policy 0.0.0-stage → 2.3.0-beta.3

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.
Files changed (3) hide show
  1. package/package.json +29 -4
  2. package/src/index.ts +184 -0
  3. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@vessel-agent/tool-policy",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.3.0-beta.3",
4
+ "type": "module",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "exports": {
8
+ ".": "./src/index.ts"
9
+ },
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/egg-rolls/Vessel.git",
17
+ "directory": "plugins/security/tool-policy"
18
+ },
19
+ "engines": {
20
+ "bun": ">=1.3.9"
21
+ },
22
+ "scripts": {
23
+ "typecheck": "tsc --noEmit"
24
+ },
25
+ "dependencies": {
26
+ "@vessel-agent/core": "2.3.0-beta.3"
27
+ },
28
+ "devDependencies": {
29
+ "typescript": "^7.0.2"
30
+ }
31
+ }
package/src/index.ts ADDED
@@ -0,0 +1,184 @@
1
+ /**
2
+ * @vessel-agent/tool-policy - 工具允许/禁止策略插件
3
+ * @module @vessel-agent/tool-policy
4
+ *
5
+ * 通过 Guardrail (ToolCall 阶段) 拦截或允许工具调用。
6
+ * 支持 allowlist / denylist 两种模式。
7
+ * Tier 1。
8
+ */
9
+
10
+ import type {
11
+ Guardrail,
12
+ GuardrailContext,
13
+ GuardrailResult,
14
+ Plugin,
15
+ PluginHost,
16
+ } from '@vessel-agent/core';
17
+ import { GuardrailStage } from '@vessel-agent/core';
18
+
19
+ // ── 类型 ──────────────────────────────────────────
20
+
21
+ /** 策略模式 */
22
+ export type PolicyMode = 'allowlist' | 'denylist';
23
+
24
+ /** 插件配置 */
25
+ export interface ToolPolicyConfig {
26
+ /** 策略模式:allowlist(仅允许列表内工具)/ denylist(禁止列表内工具,默认) */
27
+ mode?: PolicyMode;
28
+ /** 工具名列表 */
29
+ tools?: string[];
30
+ /** 是否允许通配符匹配(例如 "mcp__*" 匹配所有 MCP 工具) */
31
+ enableWildcards?: boolean;
32
+ /** 阻断时的提示信息 */
33
+ blockMessage?: string;
34
+ }
35
+
36
+ /** E1:`install(host, {config})` 的窄类型(core 尚未声明第二参数) */
37
+ export interface PluginInstallOptions {
38
+ config?: Record<string, unknown>;
39
+ }
40
+
41
+ /** 类型守卫 */
42
+ function isRecord(value: unknown): value is Record<string, unknown> {
43
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
44
+ }
45
+
46
+ /** 读取布尔值(同时接受 camelCase / snake_case) */
47
+ function readBoolean(
48
+ config: Record<string, unknown>,
49
+ camel: string,
50
+ snake: string,
51
+ ): boolean | undefined {
52
+ const value = config[camel] ?? config[snake];
53
+ return typeof value === 'boolean' ? value : undefined;
54
+ }
55
+
56
+ /** 把 YAML 里的插件配置解析成已知字段(未知键忽略) */
57
+ export function parseToolPolicyConfig(value: unknown): ToolPolicyConfig {
58
+ if (!isRecord(value)) return {};
59
+ const parsed: ToolPolicyConfig = {};
60
+ if (value.mode === 'allowlist' || value.mode === 'denylist') parsed.mode = value.mode;
61
+ if (Array.isArray(value.tools)) {
62
+ parsed.tools = value.tools.filter((tool): tool is string => typeof tool === 'string');
63
+ }
64
+ const enableWildcards = readBoolean(value, 'enableWildcards', 'enable_wildcards');
65
+ if (enableWildcards !== undefined) parsed.enableWildcards = enableWildcards;
66
+ const blockMessage = value.blockMessage ?? value.block_message;
67
+ if (typeof blockMessage === 'string') parsed.blockMessage = blockMessage;
68
+ return parsed;
69
+ }
70
+
71
+ // ── 默认配置 ──────────────────────────────────────
72
+
73
+ const DEFAULT_CONFIG: Required<ToolPolicyConfig> = {
74
+ mode: 'denylist',
75
+ tools: [],
76
+ enableWildcards: true,
77
+ blockMessage: '此工具调用已被 tool-policy 策略拦截。',
78
+ };
79
+
80
+ // ── 工具匹配 ──────────────────────────────────────
81
+
82
+ /** 转义正则元字符(通配符之外的部分按字面量匹配) */
83
+ function escapeRegExp(text: string): string {
84
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
85
+ }
86
+
87
+ /**
88
+ * 检查工具名是否匹配策略规则。
89
+ * 支持通配符 `*`(匹配任意字符序列);`enableWildcards: false` 时按字面量匹配。
90
+ */
91
+ function matchesPolicy(toolName: string, patterns: string[], enableWildcards: boolean): boolean {
92
+ for (const pattern of patterns) {
93
+ if (toolName === pattern) return true;
94
+ if (!enableWildcards) continue;
95
+
96
+ if (pattern === '*') return true;
97
+ if (pattern.includes('*')) {
98
+ const source = pattern.split('*').map(escapeRegExp).join('.*');
99
+ if (new RegExp(`^${source}$`).test(toolName)) return true;
100
+ }
101
+ }
102
+ return false;
103
+ }
104
+
105
+ /** 从 ToolCall / 工具定义 / 上下文中取工具名(runtime 传的是 ToolCall) */
106
+ function readToolName(value: unknown, ctx: GuardrailContext): string {
107
+ if (typeof value === 'object' && value !== null) {
108
+ const record = value as Record<string, unknown>;
109
+ const fn = record.function;
110
+ if (typeof fn === 'object' && fn !== null) {
111
+ const name = (fn as Record<string, unknown>).name;
112
+ if (typeof name === 'string' && name !== '') return name;
113
+ }
114
+ if (typeof record.name === 'string' && record.name !== '') return record.name;
115
+ }
116
+ const contextName = (ctx as unknown as Record<string, unknown>).tool_name;
117
+ return typeof contextName === 'string' ? contextName : '';
118
+ }
119
+
120
+ // ── Guardrail ─────────────────────────────────────
121
+
122
+ function createToolPolicyGuardrail(config: Required<ToolPolicyConfig>): Guardrail {
123
+ return {
124
+ name: 'tool-policy',
125
+ stage: GuardrailStage.ToolCall,
126
+ priority: 50,
127
+ check: async (value: unknown, ctx: GuardrailContext): Promise<GuardrailResult> => {
128
+ const toolName = readToolName(value, ctx);
129
+
130
+ if (!toolName) {
131
+ return { allowed: true };
132
+ }
133
+
134
+ const matched = matchesPolicy(toolName, config.tools, config.enableWildcards);
135
+
136
+ if (config.mode === 'allowlist') {
137
+ // 白名单模式:不在列表中的工具被拦截
138
+ if (!matched) {
139
+ return {
140
+ allowed: false,
141
+ reason: `${config.blockMessage} 工具 "${toolName}" 不在白名单中。`,
142
+ };
143
+ }
144
+ } else {
145
+ // 黑名单模式:在列表中的工具被拦截
146
+ if (matched) {
147
+ return {
148
+ allowed: false,
149
+ reason: `${config.blockMessage} 工具 "${toolName}" 已被禁止。`,
150
+ };
151
+ }
152
+ }
153
+
154
+ return { allowed: true };
155
+ },
156
+ };
157
+ }
158
+
159
+ // ── 插件导出 ──────────────────────────────────────
160
+
161
+ /**
162
+ * 创建 Tool Policy 插件
163
+ */
164
+ export function createToolPolicyPlugin(config?: ToolPolicyConfig): Plugin {
165
+ return {
166
+ name: 'tool-policy',
167
+ version: '0.1.0',
168
+ description: 'Tool allow/deny policy — intercepts tool calls via ToolCall guardrail',
169
+ install(host: PluginHost, options?: PluginInstallOptions) {
170
+ const mergedConfig: Required<ToolPolicyConfig> = {
171
+ ...DEFAULT_CONFIG,
172
+ ...(config ?? {}),
173
+ ...parseToolPolicyConfig(options?.config),
174
+ };
175
+
176
+ host.registerGuardrail(createToolPolicyGuardrail(mergedConfig));
177
+ },
178
+ };
179
+ }
180
+
181
+ /** 默认实例——现有调用方无需改动 */
182
+ export const toolPolicyPlugin = createToolPolicyPlugin();
183
+
184
+ export default toolPolicyPlugin;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.