pi-permission-system 0.1.5 → 0.1.6
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 +16 -0
- package/LICENSE +21 -21
- package/README.md +8 -6
- package/config/config.example.json +27 -27
- package/index.ts +3 -3
- package/package.json +59 -59
- package/schemas/permissions.schema.json +86 -86
- package/src/bash-filter.ts +77 -77
- package/src/index.ts +3 -1
- package/src/system-prompt-sanitizer.ts +130 -0
- package/src/tool-registry.ts +148 -148
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.6] - 2026-03-09
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Sanitized the `Available tools:` system prompt section so denied tools are removed before the agent starts.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
- Updated README documentation to describe system-prompt tool sanitization and refreshed the displayed package version.
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
- Prevented hidden tools from remaining advertised in the startup system prompt after runtime tool filtering.
|
|
20
|
+
|
|
21
|
+
## [0.1.5] - 2026-03-09
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
- Added `repository`, `homepage`, and `bugs` package metadata so npm links back to the public GitHub repository and issue tracker.
|
|
25
|
+
|
|
10
26
|
## [0.1.4] - 2026-03-07
|
|
11
27
|
|
|
12
28
|
### Added
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 MasuRii
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MasuRii
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 🔐 pi-permission-system
|
|
2
2
|
|
|
3
|
-
[](package.json)
|
|
4
4
|
[](LICENSE)
|
|
5
5
|
|
|
6
6
|
Permission enforcement extension for the Pi coding agent that provides centralized, deterministic permission gates for tool, bash, MCP, skill, and special operations.
|
|
@@ -10,6 +10,7 @@ Permission enforcement extension for the Pi coding agent that provides centraliz
|
|
|
10
10
|
## Features
|
|
11
11
|
|
|
12
12
|
- **Tool Filtering** — Hides disallowed tools from the agent before it starts (reduces "try another tool" behavior)
|
|
13
|
+
- **System Prompt Sanitization** — Removes denied tool entries from the `Available tools:` system prompt section so the agent only sees tools it can actually call
|
|
13
14
|
- **Runtime Enforcement** — Blocks/asks/allows at tool call time with UI confirmation dialogs
|
|
14
15
|
- **Bash Command Control** — Wildcard pattern matching for granular bash command permissions
|
|
15
16
|
- **MCP Access Control** — Server and tool-level permissions for MCP operations
|
|
@@ -67,14 +68,15 @@ All permissions use one of three states:
|
|
|
67
68
|
|
|
68
69
|
The extension integrates via Pi's lifecycle hooks:
|
|
69
70
|
|
|
70
|
-
| Hook | Behavior
|
|
71
|
-
|
|
72
|
-
| `before_agent_start` | Filters active tools
|
|
73
|
-
| `tool_call` | Enforces permissions for every tool invocation
|
|
74
|
-
| `input` | Intercepts `/skill:<name>` requests and enforces skill policy
|
|
71
|
+
| Hook | Behavior |
|
|
72
|
+
|----------------------|-------------------------------------------------------------------------------------------|
|
|
73
|
+
| `before_agent_start` | Filters active tools, removes denied tool entries from the system prompt, and hides denied skills |
|
|
74
|
+
| `tool_call` | Enforces permissions for every tool invocation |
|
|
75
|
+
| `input` | Intercepts `/skill:<name>` requests and enforces skill policy |
|
|
75
76
|
|
|
76
77
|
**Additional behaviors:**
|
|
77
78
|
- Unknown/unregistered tools are blocked before permission checks (prevents bypass attempts)
|
|
79
|
+
- The `Available tools:` system prompt section is rewritten to match the filtered active tool set
|
|
78
80
|
- The `task` delegation tool is restricted to the `orchestrator` agent only
|
|
79
81
|
- When a subagent hits an `ask` permission without direct UI access, the request can be forwarded to the main interactive session for confirmation
|
|
80
82
|
- When a subagent triggers an `ask` permission without UI access, the request can be forwarded to the main session and answered there
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"defaultPolicy": {
|
|
3
|
-
"tools": "ask",
|
|
4
|
-
"bash": "ask",
|
|
5
|
-
"mcp": "ask",
|
|
6
|
-
"skills": "ask",
|
|
7
|
-
"special": "ask"
|
|
8
|
-
},
|
|
9
|
-
"tools": {
|
|
10
|
-
"read": "allow",
|
|
11
|
-
"write": "deny"
|
|
12
|
-
},
|
|
13
|
-
"bash": {
|
|
14
|
-
"git status": "allow",
|
|
15
|
-
"git *": "ask"
|
|
16
|
-
},
|
|
17
|
-
"mcp": {
|
|
18
|
-
"mcp_status": "allow"
|
|
19
|
-
},
|
|
20
|
-
"skills": {
|
|
21
|
-
"*": "ask"
|
|
22
|
-
},
|
|
23
|
-
"special": {
|
|
24
|
-
"doom_loop": "deny",
|
|
25
|
-
"external_directory": "ask"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"defaultPolicy": {
|
|
3
|
+
"tools": "ask",
|
|
4
|
+
"bash": "ask",
|
|
5
|
+
"mcp": "ask",
|
|
6
|
+
"skills": "ask",
|
|
7
|
+
"special": "ask"
|
|
8
|
+
},
|
|
9
|
+
"tools": {
|
|
10
|
+
"read": "allow",
|
|
11
|
+
"write": "deny"
|
|
12
|
+
},
|
|
13
|
+
"bash": {
|
|
14
|
+
"git status": "allow",
|
|
15
|
+
"git *": "ask"
|
|
16
|
+
},
|
|
17
|
+
"mcp": {
|
|
18
|
+
"mcp_status": "allow"
|
|
19
|
+
},
|
|
20
|
+
"skills": {
|
|
21
|
+
"*": "ask"
|
|
22
|
+
},
|
|
23
|
+
"special": {
|
|
24
|
+
"doom_loop": "deny",
|
|
25
|
+
"external_directory": "ask"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import permissionSystemExtension from "./src/index.js";
|
|
2
|
-
|
|
3
|
-
export default permissionSystemExtension;
|
|
1
|
+
import permissionSystemExtension from "./src/index.js";
|
|
2
|
+
|
|
3
|
+
export default permissionSystemExtension;
|
package/package.json
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "pi-permission-system",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Permission enforcement extension for the Pi coding agent.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./index.ts",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./index.ts"
|
|
9
|
-
},
|
|
10
|
-
"files": [
|
|
11
|
-
"index.ts",
|
|
12
|
-
"src",
|
|
13
|
-
"config/config.example.json",
|
|
14
|
-
"schemas/permissions.schema.json",
|
|
15
|
-
"asset",
|
|
16
|
-
"README.md",
|
|
17
|
-
"CHANGELOG.md",
|
|
18
|
-
"LICENSE"
|
|
19
|
-
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json --noCheck",
|
|
22
|
-
"lint": "npm run build",
|
|
23
|
-
"test": "bun ./src/test.ts",
|
|
24
|
-
"check": "npm run lint && npm run test"
|
|
25
|
-
},
|
|
26
|
-
"keywords": [
|
|
27
|
-
"pi-package",
|
|
28
|
-
"pi",
|
|
29
|
-
"pi-extension",
|
|
30
|
-
"permissions",
|
|
31
|
-
"policy",
|
|
32
|
-
"coding-agent"
|
|
33
|
-
],
|
|
34
|
-
"author": "MasuRii",
|
|
35
|
-
"license": "MIT",
|
|
36
|
-
"repository": {
|
|
37
|
-
"type": "git",
|
|
38
|
-
"url": "git+https://github.com/MasuRii/pi-permission-system.git"
|
|
39
|
-
},
|
|
40
|
-
"homepage": "https://github.com/MasuRii/pi-permission-system#readme",
|
|
41
|
-
"bugs": {
|
|
42
|
-
"url": "https://github.com/MasuRii/pi-permission-system/issues"
|
|
43
|
-
},
|
|
44
|
-
"engines": {
|
|
45
|
-
"node": ">=20"
|
|
46
|
-
},
|
|
47
|
-
"publishConfig": {
|
|
48
|
-
"access": "public"
|
|
49
|
-
},
|
|
50
|
-
"pi": {
|
|
51
|
-
"extensions": [
|
|
52
|
-
"./index.ts"
|
|
53
|
-
]
|
|
54
|
-
},
|
|
55
|
-
"peerDependencies": {
|
|
56
|
-
"@mariozechner/pi-coding-agent": "*",
|
|
57
|
-
"@sinclair/typebox": "*"
|
|
58
|
-
}
|
|
59
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-permission-system",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Permission enforcement extension for the Pi coding agent.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"index.ts",
|
|
12
|
+
"src",
|
|
13
|
+
"config/config.example.json",
|
|
14
|
+
"schemas/permissions.schema.json",
|
|
15
|
+
"asset",
|
|
16
|
+
"README.md",
|
|
17
|
+
"CHANGELOG.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "npx --yes -p typescript@5.7.3 tsc -p tsconfig.json --noCheck",
|
|
22
|
+
"lint": "npm run build",
|
|
23
|
+
"test": "bun ./src/test.ts",
|
|
24
|
+
"check": "npm run lint && npm run test"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"pi-package",
|
|
28
|
+
"pi",
|
|
29
|
+
"pi-extension",
|
|
30
|
+
"permissions",
|
|
31
|
+
"policy",
|
|
32
|
+
"coding-agent"
|
|
33
|
+
],
|
|
34
|
+
"author": "MasuRii",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/MasuRii/pi-permission-system.git"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/MasuRii/pi-permission-system#readme",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/MasuRii/pi-permission-system/issues"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"pi": {
|
|
51
|
+
"extensions": [
|
|
52
|
+
"./index.ts"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
57
|
+
"@sinclair/typebox": "*"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
-
"$id": "https://pi-coding-agent.local/schemas/permissions.schema.json",
|
|
4
|
-
"title": "PI Permission Configuration",
|
|
5
|
-
"type": "object",
|
|
6
|
-
"additionalProperties": false,
|
|
7
|
-
"properties": {
|
|
8
|
-
"$schema": {
|
|
9
|
-
"type": "string"
|
|
10
|
-
},
|
|
11
|
-
"defaultPolicy": {
|
|
12
|
-
"type": "object",
|
|
13
|
-
"additionalProperties": false,
|
|
14
|
-
"required": ["tools", "bash", "mcp", "skills"],
|
|
15
|
-
"properties": {
|
|
16
|
-
"tools": {
|
|
17
|
-
"$ref": "#/$defs/permissionState"
|
|
18
|
-
},
|
|
19
|
-
"bash": {
|
|
20
|
-
"$ref": "#/$defs/permissionState"
|
|
21
|
-
},
|
|
22
|
-
"mcp": {
|
|
23
|
-
"$ref": "#/$defs/permissionState"
|
|
24
|
-
},
|
|
25
|
-
"skills": {
|
|
26
|
-
"$ref": "#/$defs/permissionState"
|
|
27
|
-
},
|
|
28
|
-
"special": {
|
|
29
|
-
"$ref": "#/$defs/permissionState"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
"tools": {
|
|
34
|
-
"$ref": "#/$defs/permissionMap"
|
|
35
|
-
},
|
|
36
|
-
"bash": {
|
|
37
|
-
"$ref": "#/$defs/permissionMap"
|
|
38
|
-
},
|
|
39
|
-
"mcp": {
|
|
40
|
-
"$ref": "#/$defs/permissionMap"
|
|
41
|
-
},
|
|
42
|
-
"skills": {
|
|
43
|
-
"$ref": "#/$defs/permissionMap"
|
|
44
|
-
},
|
|
45
|
-
"special": {
|
|
46
|
-
"type": "object",
|
|
47
|
-
"additionalProperties": false,
|
|
48
|
-
"properties": {
|
|
49
|
-
"doom_loop": {
|
|
50
|
-
"$ref": "#/$defs/permissionState"
|
|
51
|
-
},
|
|
52
|
-
"external_directory": {
|
|
53
|
-
"$ref": "#/$defs/permissionState"
|
|
54
|
-
},
|
|
55
|
-
"tool_call_limit": {
|
|
56
|
-
"oneOf": [
|
|
57
|
-
{
|
|
58
|
-
"$ref": "#/$defs/permissionState"
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
"type": "integer",
|
|
62
|
-
"minimum": 0
|
|
63
|
-
}
|
|
64
|
-
]
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
"required": ["defaultPolicy"],
|
|
70
|
-
"$defs": {
|
|
71
|
-
"permissionState": {
|
|
72
|
-
"type": "string",
|
|
73
|
-
"enum": ["allow", "deny", "ask"]
|
|
74
|
-
},
|
|
75
|
-
"permissionMap": {
|
|
76
|
-
"type": "object",
|
|
77
|
-
"propertyNames": {
|
|
78
|
-
"type": "string",
|
|
79
|
-
"minLength": 1
|
|
80
|
-
},
|
|
81
|
-
"additionalProperties": {
|
|
82
|
-
"$ref": "#/$defs/permissionState"
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://pi-coding-agent.local/schemas/permissions.schema.json",
|
|
4
|
+
"title": "PI Permission Configuration",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"properties": {
|
|
8
|
+
"$schema": {
|
|
9
|
+
"type": "string"
|
|
10
|
+
},
|
|
11
|
+
"defaultPolicy": {
|
|
12
|
+
"type": "object",
|
|
13
|
+
"additionalProperties": false,
|
|
14
|
+
"required": ["tools", "bash", "mcp", "skills"],
|
|
15
|
+
"properties": {
|
|
16
|
+
"tools": {
|
|
17
|
+
"$ref": "#/$defs/permissionState"
|
|
18
|
+
},
|
|
19
|
+
"bash": {
|
|
20
|
+
"$ref": "#/$defs/permissionState"
|
|
21
|
+
},
|
|
22
|
+
"mcp": {
|
|
23
|
+
"$ref": "#/$defs/permissionState"
|
|
24
|
+
},
|
|
25
|
+
"skills": {
|
|
26
|
+
"$ref": "#/$defs/permissionState"
|
|
27
|
+
},
|
|
28
|
+
"special": {
|
|
29
|
+
"$ref": "#/$defs/permissionState"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"tools": {
|
|
34
|
+
"$ref": "#/$defs/permissionMap"
|
|
35
|
+
},
|
|
36
|
+
"bash": {
|
|
37
|
+
"$ref": "#/$defs/permissionMap"
|
|
38
|
+
},
|
|
39
|
+
"mcp": {
|
|
40
|
+
"$ref": "#/$defs/permissionMap"
|
|
41
|
+
},
|
|
42
|
+
"skills": {
|
|
43
|
+
"$ref": "#/$defs/permissionMap"
|
|
44
|
+
},
|
|
45
|
+
"special": {
|
|
46
|
+
"type": "object",
|
|
47
|
+
"additionalProperties": false,
|
|
48
|
+
"properties": {
|
|
49
|
+
"doom_loop": {
|
|
50
|
+
"$ref": "#/$defs/permissionState"
|
|
51
|
+
},
|
|
52
|
+
"external_directory": {
|
|
53
|
+
"$ref": "#/$defs/permissionState"
|
|
54
|
+
},
|
|
55
|
+
"tool_call_limit": {
|
|
56
|
+
"oneOf": [
|
|
57
|
+
{
|
|
58
|
+
"$ref": "#/$defs/permissionState"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"type": "integer",
|
|
62
|
+
"minimum": 0
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"required": ["defaultPolicy"],
|
|
70
|
+
"$defs": {
|
|
71
|
+
"permissionState": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"enum": ["allow", "deny", "ask"]
|
|
74
|
+
},
|
|
75
|
+
"permissionMap": {
|
|
76
|
+
"type": "object",
|
|
77
|
+
"propertyNames": {
|
|
78
|
+
"type": "string",
|
|
79
|
+
"minLength": 1
|
|
80
|
+
},
|
|
81
|
+
"additionalProperties": {
|
|
82
|
+
"$ref": "#/$defs/permissionState"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/bash-filter.ts
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import type { BashPermissions, PermissionState } from "./types.js";
|
|
2
|
-
|
|
3
|
-
type CompiledPattern = {
|
|
4
|
-
pattern: string;
|
|
5
|
-
state: PermissionState;
|
|
6
|
-
regex: RegExp;
|
|
7
|
-
wildcardCount: number;
|
|
8
|
-
literalLength: number;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export interface BashPermissionCheck {
|
|
12
|
-
state: PermissionState;
|
|
13
|
-
matchedPattern?: string;
|
|
14
|
-
command: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function escapeRegExp(value: string): string {
|
|
18
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function compilePattern(pattern: string, state: PermissionState): CompiledPattern {
|
|
22
|
-
const escaped = pattern
|
|
23
|
-
.split("*")
|
|
24
|
-
.map((part) => escapeRegExp(part))
|
|
25
|
-
.join(".*");
|
|
26
|
-
|
|
27
|
-
const wildcardCount = (pattern.match(/\*/g) || []).length;
|
|
28
|
-
const literalLength = pattern.replace(/\*/g, "").length;
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
pattern,
|
|
32
|
-
state,
|
|
33
|
-
regex: new RegExp(`^${escaped}$`),
|
|
34
|
-
wildcardCount,
|
|
35
|
-
literalLength,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function bySpecificity(a: CompiledPattern, b: CompiledPattern): number {
|
|
40
|
-
if (a.wildcardCount !== b.wildcardCount) {
|
|
41
|
-
return a.wildcardCount - b.wildcardCount;
|
|
42
|
-
}
|
|
43
|
-
if (a.literalLength !== b.literalLength) {
|
|
44
|
-
return b.literalLength - a.literalLength;
|
|
45
|
-
}
|
|
46
|
-
return b.pattern.length - a.pattern.length;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class BashFilter {
|
|
50
|
-
private readonly compiledPatterns: CompiledPattern[];
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
private readonly permissions: BashPermissions,
|
|
54
|
-
private readonly defaultState: PermissionState,
|
|
55
|
-
) {
|
|
56
|
-
this.compiledPatterns = Object.entries(permissions)
|
|
57
|
-
.map(([pattern, state]) => compilePattern(pattern, state))
|
|
58
|
-
.sort(bySpecificity);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
check(command: string): BashPermissionCheck {
|
|
62
|
-
for (const pattern of this.compiledPatterns) {
|
|
63
|
-
if (pattern.regex.test(command)) {
|
|
64
|
-
return {
|
|
65
|
-
state: pattern.state,
|
|
66
|
-
matchedPattern: pattern.pattern,
|
|
67
|
-
command,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
state: this.defaultState,
|
|
74
|
-
command,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
1
|
+
import type { BashPermissions, PermissionState } from "./types.js";
|
|
2
|
+
|
|
3
|
+
type CompiledPattern = {
|
|
4
|
+
pattern: string;
|
|
5
|
+
state: PermissionState;
|
|
6
|
+
regex: RegExp;
|
|
7
|
+
wildcardCount: number;
|
|
8
|
+
literalLength: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface BashPermissionCheck {
|
|
12
|
+
state: PermissionState;
|
|
13
|
+
matchedPattern?: string;
|
|
14
|
+
command: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function escapeRegExp(value: string): string {
|
|
18
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function compilePattern(pattern: string, state: PermissionState): CompiledPattern {
|
|
22
|
+
const escaped = pattern
|
|
23
|
+
.split("*")
|
|
24
|
+
.map((part) => escapeRegExp(part))
|
|
25
|
+
.join(".*");
|
|
26
|
+
|
|
27
|
+
const wildcardCount = (pattern.match(/\*/g) || []).length;
|
|
28
|
+
const literalLength = pattern.replace(/\*/g, "").length;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
pattern,
|
|
32
|
+
state,
|
|
33
|
+
regex: new RegExp(`^${escaped}$`),
|
|
34
|
+
wildcardCount,
|
|
35
|
+
literalLength,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function bySpecificity(a: CompiledPattern, b: CompiledPattern): number {
|
|
40
|
+
if (a.wildcardCount !== b.wildcardCount) {
|
|
41
|
+
return a.wildcardCount - b.wildcardCount;
|
|
42
|
+
}
|
|
43
|
+
if (a.literalLength !== b.literalLength) {
|
|
44
|
+
return b.literalLength - a.literalLength;
|
|
45
|
+
}
|
|
46
|
+
return b.pattern.length - a.pattern.length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class BashFilter {
|
|
50
|
+
private readonly compiledPatterns: CompiledPattern[];
|
|
51
|
+
|
|
52
|
+
constructor(
|
|
53
|
+
private readonly permissions: BashPermissions,
|
|
54
|
+
private readonly defaultState: PermissionState,
|
|
55
|
+
) {
|
|
56
|
+
this.compiledPatterns = Object.entries(permissions)
|
|
57
|
+
.map(([pattern, state]) => compilePattern(pattern, state))
|
|
58
|
+
.sort(bySpecificity);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
check(command: string): BashPermissionCheck {
|
|
62
|
+
for (const pattern of this.compiledPatterns) {
|
|
63
|
+
if (pattern.regex.test(command)) {
|
|
64
|
+
return {
|
|
65
|
+
state: pattern.state,
|
|
66
|
+
matchedPattern: pattern.pattern,
|
|
67
|
+
command,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
state: this.defaultState,
|
|
74
|
+
command,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { homedir } from "node:os";
|
|
|
4
4
|
import { dirname, join, normalize, resolve, sep } from "node:path";
|
|
5
5
|
|
|
6
6
|
import { PermissionManager } from "./permission-manager.js";
|
|
7
|
+
import { sanitizeAvailableToolsSection } from "./system-prompt-sanitizer.js";
|
|
7
8
|
import { checkRequestedToolRegistration, getToolNameFromValue } from "./tool-registry.js";
|
|
8
9
|
import type { PermissionCheckResult, PermissionState } from "./types.js";
|
|
9
10
|
|
|
@@ -1087,7 +1088,8 @@ export default function piPermissionSystemExtension(pi: ExtensionAPI): void {
|
|
|
1087
1088
|
|
|
1088
1089
|
pi.setActiveTools(allowedTools);
|
|
1089
1090
|
|
|
1090
|
-
const
|
|
1091
|
+
const toolPromptResult = sanitizeAvailableToolsSection(event.systemPrompt, allowedTools);
|
|
1092
|
+
const skillPromptResult = resolveSkillPromptEntries(toolPromptResult.prompt, permissionManager, agentName, ctx.cwd);
|
|
1091
1093
|
activeSkillEntries = skillPromptResult.entries;
|
|
1092
1094
|
|
|
1093
1095
|
if (skillPromptResult.prompt !== event.systemPrompt) {
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export interface SanitizeSystemPromptResult {
|
|
2
|
+
prompt: string;
|
|
3
|
+
removed: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
type ToolPromptEntry = {
|
|
7
|
+
name: string;
|
|
8
|
+
lines: string[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type ToolPromptSection = {
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
entries: ToolPromptEntry[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const AVAILABLE_TOOLS_SECTION_HEADER = "Available tools:";
|
|
18
|
+
|
|
19
|
+
function normalizePrompt(prompt: string): string {
|
|
20
|
+
return (prompt || "").replace(/\r\n/g, "\n");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function collapseExtraBlankLines(text: string): string {
|
|
24
|
+
return text.replace(/\n{3,}/g, "\n\n").trimEnd();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function parseAvailableToolsSection(systemPrompt: string): ToolPromptSection | null {
|
|
28
|
+
const lines = normalizePrompt(systemPrompt).split("\n");
|
|
29
|
+
const start = lines.findIndex((line) => line.trim() === AVAILABLE_TOOLS_SECTION_HEADER);
|
|
30
|
+
if (start === -1) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const entries: ToolPromptEntry[] = [];
|
|
35
|
+
let index = start + 1;
|
|
36
|
+
|
|
37
|
+
while (index < lines.length) {
|
|
38
|
+
const line = lines[index];
|
|
39
|
+
const trimmed = line.trim();
|
|
40
|
+
|
|
41
|
+
if (!trimmed) {
|
|
42
|
+
index += 1;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!trimmed.startsWith("- ")) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const match = trimmed.match(/^\-\s+([^:]+):/);
|
|
51
|
+
if (!match) {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const entryLines = [line];
|
|
56
|
+
index += 1;
|
|
57
|
+
|
|
58
|
+
while (index < lines.length) {
|
|
59
|
+
const nextLine = lines[index];
|
|
60
|
+
const nextTrimmed = nextLine.trim();
|
|
61
|
+
|
|
62
|
+
if (!nextTrimmed) {
|
|
63
|
+
entryLines.push(nextLine);
|
|
64
|
+
index += 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (nextTrimmed.startsWith("- ")) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!/^\s/.test(nextLine)) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
entryLines.push(nextLine);
|
|
77
|
+
index += 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
while (entryLines.length > 0 && entryLines[entryLines.length - 1].trim().length === 0) {
|
|
81
|
+
entryLines.pop();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
entries.push({
|
|
85
|
+
name: match[1].trim(),
|
|
86
|
+
lines: entryLines,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (entries.length === 0) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
start,
|
|
96
|
+
end: index,
|
|
97
|
+
entries,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function sanitizeAvailableToolsSection(
|
|
102
|
+
systemPrompt: string,
|
|
103
|
+
allowedToolNames: readonly string[],
|
|
104
|
+
): SanitizeSystemPromptResult {
|
|
105
|
+
const section = parseAvailableToolsSection(systemPrompt);
|
|
106
|
+
if (!section) {
|
|
107
|
+
return { prompt: systemPrompt, removed: false };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const allowedTools = new Set(allowedToolNames.map((toolName) => toolName.trim()).filter(Boolean));
|
|
111
|
+
const visibleEntries = section.entries.filter((entry) => allowedTools.has(entry.name));
|
|
112
|
+
|
|
113
|
+
if (visibleEntries.length === section.entries.length) {
|
|
114
|
+
return { prompt: systemPrompt, removed: false };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const lines = normalizePrompt(systemPrompt).split("\n");
|
|
118
|
+
const replacement = visibleEntries.length > 0
|
|
119
|
+
? [lines[section.start], ...visibleEntries.flatMap((entry) => entry.lines)]
|
|
120
|
+
: [];
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
prompt: collapseExtraBlankLines([
|
|
124
|
+
...lines.slice(0, section.start),
|
|
125
|
+
...replacement,
|
|
126
|
+
...lines.slice(section.end),
|
|
127
|
+
].join("\n")),
|
|
128
|
+
removed: true,
|
|
129
|
+
};
|
|
130
|
+
}
|
package/src/tool-registry.ts
CHANGED
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
type UnknownRecord = Record<string, unknown>;
|
|
2
|
-
|
|
3
|
-
export type ToolRegistrationCheckResult =
|
|
4
|
-
| {
|
|
5
|
-
status: "missing-tool-name";
|
|
6
|
-
}
|
|
7
|
-
| {
|
|
8
|
-
status: "registered";
|
|
9
|
-
requestedToolName: string;
|
|
10
|
-
normalizedToolName: string;
|
|
11
|
-
}
|
|
12
|
-
| {
|
|
13
|
-
status: "unregistered";
|
|
14
|
-
requestedToolName: string;
|
|
15
|
-
normalizedToolName: string;
|
|
16
|
-
availableToolNames: string[];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function toRecord(value: unknown): UnknownRecord {
|
|
20
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
21
|
-
return {};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return value as UnknownRecord;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getNonEmptyString(value: unknown): string | null {
|
|
28
|
-
if (typeof value !== "string") {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const trimmed = value.trim();
|
|
33
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function normalizeToolName(toolName: string, aliases: Record<string, string>): string {
|
|
37
|
-
return aliases[toolName] || toolName;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function buildReverseAliases(aliases: Record<string, string>): Map<string, string[]> {
|
|
41
|
-
const reverse = new Map<string, string[]>();
|
|
42
|
-
|
|
43
|
-
for (const [alias, canonical] of Object.entries(aliases)) {
|
|
44
|
-
const existing = reverse.get(canonical) || [];
|
|
45
|
-
if (!existing.includes(alias)) {
|
|
46
|
-
existing.push(alias);
|
|
47
|
-
}
|
|
48
|
-
reverse.set(canonical, existing);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return reverse;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function addToolNameVariants(
|
|
55
|
-
value: string,
|
|
56
|
-
names: Set<string>,
|
|
57
|
-
aliases: Record<string, string>,
|
|
58
|
-
reverseAliases: ReadonlyMap<string, readonly string[]>,
|
|
59
|
-
): void {
|
|
60
|
-
names.add(value);
|
|
61
|
-
|
|
62
|
-
const normalized = normalizeToolName(value, aliases);
|
|
63
|
-
names.add(normalized);
|
|
64
|
-
|
|
65
|
-
const canonicalFromAlias = aliases[value];
|
|
66
|
-
if (canonicalFromAlias) {
|
|
67
|
-
names.add(canonicalFromAlias);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const aliasValues = reverseAliases.get(value);
|
|
71
|
-
if (aliasValues) {
|
|
72
|
-
for (const alias of aliasValues) {
|
|
73
|
-
names.add(alias);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const aliasValuesForNormalized = reverseAliases.get(normalized);
|
|
78
|
-
if (aliasValuesForNormalized) {
|
|
79
|
-
for (const alias of aliasValuesForNormalized) {
|
|
80
|
-
names.add(alias);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function getToolNameFromValue(value: unknown): string | null {
|
|
86
|
-
const direct = getNonEmptyString(value);
|
|
87
|
-
if (direct) {
|
|
88
|
-
return direct;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const record = toRecord(value);
|
|
92
|
-
const candidates = [record.toolName, record.name, record.tool];
|
|
93
|
-
|
|
94
|
-
for (const candidate of candidates) {
|
|
95
|
-
const stringValue = getNonEmptyString(candidate);
|
|
96
|
-
if (stringValue) {
|
|
97
|
-
return stringValue;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function checkRequestedToolRegistration(
|
|
105
|
-
requestedToolName: string | null,
|
|
106
|
-
registeredTools: readonly unknown[],
|
|
107
|
-
aliases: Record<string, string> = {},
|
|
108
|
-
): ToolRegistrationCheckResult {
|
|
109
|
-
const requested = getNonEmptyString(requestedToolName);
|
|
110
|
-
if (!requested) {
|
|
111
|
-
return {
|
|
112
|
-
status: "missing-tool-name",
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const normalizedToolName = normalizeToolName(requested, aliases);
|
|
117
|
-
const reverseAliases = buildReverseAliases(aliases);
|
|
118
|
-
|
|
119
|
-
const registeredLookup = new Set<string>();
|
|
120
|
-
const availableToolNames = new Set<string>();
|
|
121
|
-
|
|
122
|
-
for (const tool of registeredTools) {
|
|
123
|
-
const name = getToolNameFromValue(tool);
|
|
124
|
-
if (!name) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
availableToolNames.add(name);
|
|
129
|
-
addToolNameVariants(name, registeredLookup, aliases, reverseAliases);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const isRegistered = registeredLookup.has(requested) || registeredLookup.has(normalizedToolName);
|
|
133
|
-
|
|
134
|
-
if (isRegistered) {
|
|
135
|
-
return {
|
|
136
|
-
status: "registered",
|
|
137
|
-
requestedToolName: requested,
|
|
138
|
-
normalizedToolName,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
status: "unregistered",
|
|
144
|
-
requestedToolName: requested,
|
|
145
|
-
normalizedToolName,
|
|
146
|
-
availableToolNames: [...availableToolNames].sort((a, b) => a.localeCompare(b)),
|
|
147
|
-
};
|
|
148
|
-
}
|
|
1
|
+
type UnknownRecord = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
export type ToolRegistrationCheckResult =
|
|
4
|
+
| {
|
|
5
|
+
status: "missing-tool-name";
|
|
6
|
+
}
|
|
7
|
+
| {
|
|
8
|
+
status: "registered";
|
|
9
|
+
requestedToolName: string;
|
|
10
|
+
normalizedToolName: string;
|
|
11
|
+
}
|
|
12
|
+
| {
|
|
13
|
+
status: "unregistered";
|
|
14
|
+
requestedToolName: string;
|
|
15
|
+
normalizedToolName: string;
|
|
16
|
+
availableToolNames: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function toRecord(value: unknown): UnknownRecord {
|
|
20
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return value as UnknownRecord;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getNonEmptyString(value: unknown): string | null {
|
|
28
|
+
if (typeof value !== "string") {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const trimmed = value.trim();
|
|
33
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function normalizeToolName(toolName: string, aliases: Record<string, string>): string {
|
|
37
|
+
return aliases[toolName] || toolName;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function buildReverseAliases(aliases: Record<string, string>): Map<string, string[]> {
|
|
41
|
+
const reverse = new Map<string, string[]>();
|
|
42
|
+
|
|
43
|
+
for (const [alias, canonical] of Object.entries(aliases)) {
|
|
44
|
+
const existing = reverse.get(canonical) || [];
|
|
45
|
+
if (!existing.includes(alias)) {
|
|
46
|
+
existing.push(alias);
|
|
47
|
+
}
|
|
48
|
+
reverse.set(canonical, existing);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return reverse;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function addToolNameVariants(
|
|
55
|
+
value: string,
|
|
56
|
+
names: Set<string>,
|
|
57
|
+
aliases: Record<string, string>,
|
|
58
|
+
reverseAliases: ReadonlyMap<string, readonly string[]>,
|
|
59
|
+
): void {
|
|
60
|
+
names.add(value);
|
|
61
|
+
|
|
62
|
+
const normalized = normalizeToolName(value, aliases);
|
|
63
|
+
names.add(normalized);
|
|
64
|
+
|
|
65
|
+
const canonicalFromAlias = aliases[value];
|
|
66
|
+
if (canonicalFromAlias) {
|
|
67
|
+
names.add(canonicalFromAlias);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const aliasValues = reverseAliases.get(value);
|
|
71
|
+
if (aliasValues) {
|
|
72
|
+
for (const alias of aliasValues) {
|
|
73
|
+
names.add(alias);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const aliasValuesForNormalized = reverseAliases.get(normalized);
|
|
78
|
+
if (aliasValuesForNormalized) {
|
|
79
|
+
for (const alias of aliasValuesForNormalized) {
|
|
80
|
+
names.add(alias);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function getToolNameFromValue(value: unknown): string | null {
|
|
86
|
+
const direct = getNonEmptyString(value);
|
|
87
|
+
if (direct) {
|
|
88
|
+
return direct;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const record = toRecord(value);
|
|
92
|
+
const candidates = [record.toolName, record.name, record.tool];
|
|
93
|
+
|
|
94
|
+
for (const candidate of candidates) {
|
|
95
|
+
const stringValue = getNonEmptyString(candidate);
|
|
96
|
+
if (stringValue) {
|
|
97
|
+
return stringValue;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function checkRequestedToolRegistration(
|
|
105
|
+
requestedToolName: string | null,
|
|
106
|
+
registeredTools: readonly unknown[],
|
|
107
|
+
aliases: Record<string, string> = {},
|
|
108
|
+
): ToolRegistrationCheckResult {
|
|
109
|
+
const requested = getNonEmptyString(requestedToolName);
|
|
110
|
+
if (!requested) {
|
|
111
|
+
return {
|
|
112
|
+
status: "missing-tool-name",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const normalizedToolName = normalizeToolName(requested, aliases);
|
|
117
|
+
const reverseAliases = buildReverseAliases(aliases);
|
|
118
|
+
|
|
119
|
+
const registeredLookup = new Set<string>();
|
|
120
|
+
const availableToolNames = new Set<string>();
|
|
121
|
+
|
|
122
|
+
for (const tool of registeredTools) {
|
|
123
|
+
const name = getToolNameFromValue(tool);
|
|
124
|
+
if (!name) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
availableToolNames.add(name);
|
|
129
|
+
addToolNameVariants(name, registeredLookup, aliases, reverseAliases);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const isRegistered = registeredLookup.has(requested) || registeredLookup.has(normalizedToolName);
|
|
133
|
+
|
|
134
|
+
if (isRegistered) {
|
|
135
|
+
return {
|
|
136
|
+
status: "registered",
|
|
137
|
+
requestedToolName: requested,
|
|
138
|
+
normalizedToolName,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
status: "unregistered",
|
|
144
|
+
requestedToolName: requested,
|
|
145
|
+
normalizedToolName,
|
|
146
|
+
availableToolNames: [...availableToolNames].sort((a, b) => a.localeCompare(b)),
|
|
147
|
+
};
|
|
148
|
+
}
|