@uncarved/jade 0.1.3 → 0.2.0
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/bin/jade.js +5 -27
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -30
- package/dist/index.js +11 -30
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +159 -1362
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +2 -0
- package/dist/update.js +6 -3
- package/dist/update.js.map +1 -1
- package/package.json +10 -8
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -100
- package/{src/plugins → plugins/approval}/review.ts +62 -16
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +48 -38
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +73 -38
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +2 -2
- package/{src/tools → plugins/fs}/fs.ts +19 -42
- package/{src/tools → plugins/fs}/grep.ts +1 -1
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/{src/ui → plugins/fs}/renderers.ts +25 -70
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +79 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +198 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +16 -119
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +142 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +83 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +253 -0
- package/plugins/prompts/sys.ts +72 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +31 -20
- package/{src → plugins/shell}/sys.ts +2 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +167 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +257 -0
- package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +3 -0
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/{src/ui → plugins/tui}/render.ts +8 -2
- package/{src/ui → plugins/tui}/screen.ts +8 -3
- package/{src/ui → plugins/tui}/transcript.ts +74 -99
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +358 -387
- package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
- package/plugins/undo/index.ts +139 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/{src/tools → plugins/web}/web.ts +58 -26
- package/plugins-dist/approval/index.js +271 -0
- package/plugins-dist/approval/plugin.json +24 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +15 -81
- package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +51 -33
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
- package/{dist/tools → plugins-dist/fs}/fs.js +17 -42
- package/{dist/tools → plugins-dist/fs}/grep.js +1 -2
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +69 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +167 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +18 -81
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +153 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +57 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +227 -0
- package/plugins-dist/prompts/sys.js +46 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
- package/{dist → plugins-dist/shell}/sys.js +2 -43
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +151 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +208 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
- package/plugins-dist/tui/frontend.js +586 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/{dist/ui → plugins-dist/tui}/render.js +7 -3
- package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
- package/{dist/ui → plugins-dist/tui}/transcript.js +76 -98
- package/{dist/ui → plugins-dist/tui}/tui.js +185 -53
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
- package/plugins-dist/undo/index.js +140 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/{dist/tools → plugins-dist/web}/web.js +50 -24
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -30
- package/src/install.ts +355 -0
- package/src/main.ts +155 -1387
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -262
- package/LICENSE +0 -21
- package/README.md +0 -204
- package/dist/commands.d.ts +0 -50
- package/dist/commands.js +0 -126
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -141
- package/dist/distro.js +0 -462
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -294
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -25
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -71
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -47
- package/dist/plugins/core.js +0 -229
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/host.d.ts +0 -52
- package/dist/plugins/host.js +0 -115
- package/dist/plugins/host.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -410
- package/dist/plugins/loader.js +0 -407
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -98
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -43
- package/dist/plugins/prompts.js +0 -284
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -16
- package/dist/plugins/repeats.js +0 -151
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -18
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/plugins/sessions.d.ts +0 -7
- package/dist/plugins/sessions.js +0 -110
- package/dist/plugins/sessions.js.map +0 -1
- package/dist/provider.d.ts +0 -165
- package/dist/provider.js.map +0 -1
- package/dist/session-lifecycle.d.ts +0 -61
- package/dist/session-lifecycle.js +0 -167
- package/dist/session-lifecycle.js.map +0 -1
- package/dist/session-scope.d.ts +0 -53
- package/dist/session-scope.js +0 -116
- package/dist/session-scope.js.map +0 -1
- package/dist/sessions.d.ts +0 -28
- package/dist/sessions.js +0 -73
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -51
- package/dist/sys.js.map +0 -1
- package/dist/tools/ask.d.ts +0 -11
- package/dist/tools/ask.js +0 -62
- package/dist/tools/ask.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -39
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -39
- package/dist/tools/task.js +0 -117
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -53
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -447
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -41
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -22
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -14
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/renderers.d.ts +0 -8
- package/dist/ui/renderers.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -118
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -46
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -47
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -144
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -577
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -278
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -34
- package/src/plugins/core.ts +0 -257
- package/src/plugins/host.ts +0 -146
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -742
- package/src/plugins/prompts.ts +0 -327
- package/src/plugins/repeats.ts +0 -157
- package/src/plugins/sessions.ts +0 -109
- package/src/session-lifecycle.ts +0 -194
- package/src/session-scope.ts +0 -135
- package/src/sessions.ts +0 -78
- package/src/tools/ask.ts +0 -72
- package/src/tools/task.ts +0 -149
- package/src/ui/configure.ts +0 -471
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
- /package/{src/ui → plugins/tui}/markdown.ts +0 -0
|
@@ -8,12 +8,15 @@
|
|
|
8
8
|
// web_fetch 拒绝 localhost 和内网地址:网页里的提示注入不能借它去访问本机的服务。
|
|
9
9
|
// 主机名要先解析,解析出的地址也要检查,否则 127.0.0.1.nip.io 这类域名就能绕过。
|
|
10
10
|
// 检查和 fetch 各自解析一次,DNS 重绑定仍有很小的窗口;要彻底杜绝需要按解析出的 IP 连接。
|
|
11
|
-
// web_search
|
|
11
|
+
// web_search 只在配置了搜索服务时发给模型(见 index.ts),否则模型每次调用都会失败。
|
|
12
|
+
//
|
|
13
|
+
// 从旧 jade 的 tools/web.ts 移植。服务的 key 每次调用时取(search()),/search 改了 key 当前会话立即用上,不用重开会话。
|
|
12
14
|
|
|
13
15
|
import { lookup as dnsLookup } from "node:dns/promises";
|
|
14
16
|
import { isIP } from "node:net";
|
|
15
17
|
import { Parser } from "htmlparser2";
|
|
16
18
|
import type { Block, Tool } from "uncarved";
|
|
19
|
+
import type { Env } from "./env.ts";
|
|
17
20
|
|
|
18
21
|
/** 一次返回的最多字符数,更长的用 offset 分页 */
|
|
19
22
|
const MAX_CHARS = 30_000;
|
|
@@ -31,8 +34,12 @@ const MAX_RESULTS = 20;
|
|
|
31
34
|
const USER_AGENT = "Mozilla/5.0 (compatible; jade/0.1; +https://github.com/MochiNek0/uncarved)";
|
|
32
35
|
|
|
33
36
|
export interface WebOptions {
|
|
34
|
-
/**
|
|
35
|
-
env?:
|
|
37
|
+
/** API key 所在的环境,每次调用时取;默认 process.env */
|
|
38
|
+
env?: Env | (() => Env);
|
|
39
|
+
/** 用哪些搜索服务、按什么顺序(服务名);不给时用所有配了 key 的服务,按 SEARCH_PROVIDERS 的顺序 */
|
|
40
|
+
order?: string[];
|
|
41
|
+
/** 每次搜索返回的条数,默认 DEFAULT_RESULTS */
|
|
42
|
+
results?: number;
|
|
36
43
|
/** 测试用 */
|
|
37
44
|
fetch?: typeof fetch;
|
|
38
45
|
/** 主机名解析出的地址;测试用 */
|
|
@@ -42,17 +49,19 @@ export interface WebOptions {
|
|
|
42
49
|
const defaultLookup = async (hostname: string) => (await dnsLookup(hostname, { all: true })).map((a) => a.address);
|
|
43
50
|
|
|
44
51
|
export interface WebTools {
|
|
45
|
-
/** web_fetch
|
|
52
|
+
/** web_fetch 和 web_search;没有配置搜索服务时 web_search 返回错误,不应发给模型 */
|
|
46
53
|
tools: Tool[];
|
|
54
|
+
/** 当前配了 key 的搜索服务名,按调用顺序 */
|
|
55
|
+
search(): string[];
|
|
47
56
|
}
|
|
48
57
|
|
|
49
58
|
export function webTools(opts: WebOptions = {}): WebTools {
|
|
50
|
-
const env = opts.env ?? process.env;
|
|
59
|
+
const env = typeof opts.env === "function" ? opts.env : () => (opts.env as Env | undefined) ?? process.env;
|
|
51
60
|
const fetchImpl = opts.fetch ?? fetch;
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
return { tools };
|
|
61
|
+
if (opts.order) checkOrder(opts.order);
|
|
62
|
+
const services = () => backends(env(), opts.order, fetchImpl);
|
|
63
|
+
const tools = [webFetchTool(fetchImpl, opts.lookup ?? defaultLookup, () => services().filter((s) => s.read)), webSearchTool(services, opts.results ?? DEFAULT_RESULTS)];
|
|
64
|
+
return { tools, search: () => services().map((s) => s.name) };
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
// ---------- web_fetch ----------
|
|
@@ -78,7 +87,7 @@ const THIN_CHARS = 200;
|
|
|
78
87
|
type Lookup = (hostname: string) => Promise<string[]>;
|
|
79
88
|
|
|
80
89
|
/** readers:能读网页的搜索服务;直接下载不成功时按顺序试 */
|
|
81
|
-
function webFetchTool(fetchImpl: typeof fetch, lookup: Lookup, readers: Backend[]): Tool {
|
|
90
|
+
function webFetchTool(fetchImpl: typeof fetch, lookup: Lookup, readers: () => Backend[]): Tool {
|
|
82
91
|
const cache = new Map<string, Page>();
|
|
83
92
|
return {
|
|
84
93
|
name: "web_fetch",
|
|
@@ -124,7 +133,7 @@ function webFetchTool(fetchImpl: typeof fetch, lookup: Lookup, readers: Backend[
|
|
|
124
133
|
// 直接下载被拦、出错或只拿到 JS 外壳时,交给能读网页的服务;404 这类明确的结果直接返回
|
|
125
134
|
const failures: string[] = [];
|
|
126
135
|
if ("content" in direct ? direct.retry : direct.thin) {
|
|
127
|
-
for (const r of readers) {
|
|
136
|
+
for (const r of readers()) {
|
|
128
137
|
try {
|
|
129
138
|
page = { at: Date.now(), url: target.href, via: r.name, text: await r.read!(target.href, signal) };
|
|
130
139
|
break;
|
|
@@ -599,29 +608,51 @@ export const SEARCH_PROVIDERS: SearchProvider[] = [
|
|
|
599
608
|
];
|
|
600
609
|
|
|
601
610
|
/**
|
|
602
|
-
* 配置了 key 的服务,按调用顺序。
|
|
603
|
-
*
|
|
611
|
+
* 配置了 key 的服务,按调用顺序。order 是服务名,决定顺序和范围(没 key 的跳过);
|
|
612
|
+
* 不给时用所有配了 key 的服务,按 SEARCH_PROVIDERS 的顺序
|
|
604
613
|
*/
|
|
605
|
-
function backends(env:
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
614
|
+
function backends(env: Env, order: string[] | undefined, fetchImpl: typeof fetch): Backend[] {
|
|
615
|
+
const list = order?.length ? order.map((n) => SEARCH_PROVIDERS.find((p) => p.name === n)!) : SEARCH_PROVIDERS;
|
|
616
|
+
return list.flatMap((p) => (env[p.env] ? [p.make(env[p.env]!, fetchImpl)] : []));
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function checkOrder(order: string[]): void {
|
|
620
|
+
const unknown = order.filter((n) => !SEARCH_PROVIDERS.some((p) => p.name === n));
|
|
621
|
+
if (unknown.length) throw new Error(`unknown search service ${unknown.join(", ")}; choose from ${SEARCH_PROVIDERS.map((p) => p.name).join(", ")}`);
|
|
611
622
|
}
|
|
612
623
|
|
|
613
|
-
/**
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
624
|
+
/**
|
|
625
|
+
* 配置项 search(或旧 jade 的 JADE_SEARCH):逗号分隔的服务名或服务名的数组。不设时返回 undefined;
|
|
626
|
+
* 不认识的服务名抛错,错误信息里带来源 label
|
|
627
|
+
*/
|
|
628
|
+
export function parseOrder(value: unknown, label: string): string[] | undefined {
|
|
629
|
+
if (value === undefined || value === null || value === "") return undefined;
|
|
630
|
+
const list = typeof value === "string" ? value.split(",") : Array.isArray(value) && value.every((v) => typeof v === "string") ? (value as string[]) : undefined;
|
|
631
|
+
if (!list) throw new Error(`${label} must be a comma-separated list of search services, got ${JSON.stringify(value)}`);
|
|
632
|
+
const named = list.map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
633
|
+
try {
|
|
634
|
+
checkOrder(named);
|
|
635
|
+
} catch (e) {
|
|
636
|
+
throw new Error(`${label}: ${(e as Error).message}`);
|
|
637
|
+
}
|
|
638
|
+
return named.length ? named : undefined;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** 配置项 results(或旧 jade 的 JADE_SEARCH_RESULTS):每次搜索返回的条数,1 到 MAX_RESULTS */
|
|
642
|
+
export function parseResults(value: unknown, label: string): number {
|
|
643
|
+
const raw = typeof value === "string" ? value.trim() : value;
|
|
644
|
+
if (raw === undefined || raw === null || raw === "") return DEFAULT_RESULTS;
|
|
617
645
|
const n = Number(raw);
|
|
618
|
-
if (!Number.isInteger(n) || n < 1 || n > MAX_RESULTS) throw new Error(
|
|
646
|
+
if (!Number.isInteger(n) || n < 1 || n > MAX_RESULTS) throw new Error(`${label} must be an integer from 1 to ${MAX_RESULTS}, got ${JSON.stringify(value)}`);
|
|
619
647
|
return n;
|
|
620
648
|
}
|
|
621
649
|
|
|
622
650
|
// ---------- web_search ----------
|
|
623
651
|
|
|
624
|
-
|
|
652
|
+
/** 没有配置搜索服务时调用 web_search 的结果(正常情况下这时 web_search 不发给模型) */
|
|
653
|
+
export const SEARCH_OFF = "Web search is not set up. The user can turn it on with the /search command.";
|
|
654
|
+
|
|
655
|
+
function webSearchTool(live: () => Backend[], count: number): Tool {
|
|
625
656
|
return {
|
|
626
657
|
name: "web_search",
|
|
627
658
|
description:
|
|
@@ -639,6 +670,8 @@ function webSearchTool(services: Backend[], count: number): Tool {
|
|
|
639
670
|
async execute(input, { signal }) {
|
|
640
671
|
const { query } = input as { query: string };
|
|
641
672
|
if (!query.trim()) return { content: "query must not be empty", isError: true };
|
|
673
|
+
const services = live();
|
|
674
|
+
if (!services.length) return { content: SEARCH_OFF, isError: true };
|
|
642
675
|
// 按顺序试,出错或没有结果时换下一个
|
|
643
676
|
const failures: string[] = [];
|
|
644
677
|
let failed = false;
|
|
@@ -678,4 +711,3 @@ function errorText(err: unknown): string {
|
|
|
678
711
|
}
|
|
679
712
|
|
|
680
713
|
const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
681
|
-
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// jade/approval:审批。tool.before 把路径统一成绝对路径,tool.gate 按模式和规则决定,要问时经 ui@1 问用户。
|
|
2
|
+
// auto 模式下规则判断不了、本来要问的调用先让模型审一次(review.ts):它只能放行或转给用户,不能替用户拒绝。
|
|
3
|
+
//
|
|
4
|
+
// 没有这个插件(停用、坏掉、被隔离)时,核心的兜底关卡拒绝所有非只读工具(第 7 节),所以这里出错不会放行。
|
|
5
|
+
// 用户选的"总是允许"记进日志(derived),恢复会话后仍然有效;模式是进程级的,来自配置,可以经 approval@1 改。
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { definePlugin } from "../../dist/api.js";
|
|
8
|
+
import { describeRule, MODES, policy } from "./policy.js";
|
|
9
|
+
import { reviewer } from "./review.js";
|
|
10
|
+
const ANSWERS = ["once", "always", "deny"];
|
|
11
|
+
/** derived 里保存"总是允许"规则的键 */
|
|
12
|
+
const RULES_KEY = "jade/approval/rules";
|
|
13
|
+
/** 等用户回答的时限:内核要求 hook 有有限的超时,超时按拒绝处理 */
|
|
14
|
+
const ASK_TIMEOUT_MS = 24 * 60 * 60 * 1000;
|
|
15
|
+
const MAX_DETAIL_CHARS = 2000;
|
|
16
|
+
/** 等工具的审批预览的时限,过了就用默认的 detail */
|
|
17
|
+
const PREVIEW_TIMEOUT_MS = 5000;
|
|
18
|
+
/**
|
|
19
|
+
* 带 URL 的只读工具审查放行之后,完全相同的调用不再审查。不按主机记:同一主机上换一个路径或查询参数
|
|
20
|
+
* 就能把本地数据带出去,正是审查要拦的
|
|
21
|
+
*/
|
|
22
|
+
const reviewedKey = (call) => {
|
|
23
|
+
const url = call.input?.url;
|
|
24
|
+
if (call.tool.effect !== "read" || typeof url !== "string")
|
|
25
|
+
return undefined;
|
|
26
|
+
return `${call.name} ${JSON.stringify(call.input)}`;
|
|
27
|
+
};
|
|
28
|
+
let mode = "ask";
|
|
29
|
+
/** 配置里写错的模式:第一个会话打开时告诉用户(process 里可能还拿不到 ui@1:它的提供者可能排在后面激活) */
|
|
30
|
+
let badMode;
|
|
31
|
+
/** 经 allowRead 登记的目录;policy 每次判断时读它 */
|
|
32
|
+
const readRoots = [];
|
|
33
|
+
/** 经 allowWrite 登记的目录;policy 每次判断时读它 */
|
|
34
|
+
const writeRoots = [];
|
|
35
|
+
/** 经 addApprover 登记的审批渠道 */
|
|
36
|
+
const approvers = new Set();
|
|
37
|
+
/** 打开着的会话:会话 id → 判断只读调用能不能不问就执行(allows) */
|
|
38
|
+
const sessionChecks = new Map();
|
|
39
|
+
export default definePlugin({
|
|
40
|
+
process(ctx) {
|
|
41
|
+
const configured = ctx.config.mode;
|
|
42
|
+
if (typeof configured === "string" && MODES.includes(configured))
|
|
43
|
+
mode = configured;
|
|
44
|
+
else if (configured !== undefined)
|
|
45
|
+
badMode = JSON.stringify(configured);
|
|
46
|
+
ctx.provide("approval@1", (caller) => ({
|
|
47
|
+
mode: () => mode,
|
|
48
|
+
setMode(next) {
|
|
49
|
+
if (!MODES.includes(next))
|
|
50
|
+
throw new Error(`mode must be one of ${MODES.join(", ")}`);
|
|
51
|
+
mode = next;
|
|
52
|
+
},
|
|
53
|
+
allowRead: (dir) => allow(caller, "allowRead", readRoots, dir),
|
|
54
|
+
allowWrite: (dir) => allow(caller, "allowWrite", writeRoots, dir),
|
|
55
|
+
addApprover(approver) {
|
|
56
|
+
if (caller.layer === "project")
|
|
57
|
+
throw new Error("approval.addApprover: project plugins cannot answer approvals");
|
|
58
|
+
if (typeof approver !== "function")
|
|
59
|
+
throw new Error("approval.addApprover: approver must be a function");
|
|
60
|
+
approvers.add(approver);
|
|
61
|
+
const remove = () => void approvers.delete(approver);
|
|
62
|
+
caller.onDispose(remove);
|
|
63
|
+
return remove;
|
|
64
|
+
},
|
|
65
|
+
allows: (session, call) => call.tool?.effect === "read" && (sessionChecks.get(session)?.(call) ?? false),
|
|
66
|
+
}));
|
|
67
|
+
},
|
|
68
|
+
async session(ctx) {
|
|
69
|
+
const rules = policy({ cwd: ctx.cwd, readRoots, writeRoots });
|
|
70
|
+
const ui = ctx.service("ui@1");
|
|
71
|
+
if (badMode !== undefined) {
|
|
72
|
+
ui?.notify(`jade/approval: mode must be one of ${MODES.join(", ")}, got ${badMode}; using ask`, "warning");
|
|
73
|
+
badMode = undefined;
|
|
74
|
+
}
|
|
75
|
+
const renderers = ctx.service("renderers@1");
|
|
76
|
+
// auto 模式的审查用当前配置的模型;没有 models@1(模型插件被停用)或者配置里关掉时不做审查
|
|
77
|
+
const models = ctx.service("models@1");
|
|
78
|
+
const reviewing = models !== undefined && ctx.config.review !== false;
|
|
79
|
+
/** 最近一次请求里的消息:auto 模式的审查从中取用户最近的请求 */
|
|
80
|
+
const recent = { messages: [] };
|
|
81
|
+
if (reviewing) {
|
|
82
|
+
ctx.kernel.hook({
|
|
83
|
+
kind: "transform",
|
|
84
|
+
point: "context.build",
|
|
85
|
+
fn: (req) => {
|
|
86
|
+
recent.messages = req.messages;
|
|
87
|
+
return req;
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const review = reviewing ? reviewer({ models: models, cwd: ctx.cwd, writeRoots: () => writeRoots, messages: () => recent.messages }) : undefined;
|
|
92
|
+
/** 本会话里审查过一次、放行了的调用 */
|
|
93
|
+
const reviewedCalls = new Set();
|
|
94
|
+
let warned = false;
|
|
95
|
+
/** 本进程里刚选的"总是允许":并发的关卡拿到的 derived 是各自开始时的快照,可能还没有它 */
|
|
96
|
+
const added = [];
|
|
97
|
+
/** 一次只问一个问题;排在后面的问之前重新判断,前面刚选的"总是允许"可能已经覆盖它 */
|
|
98
|
+
let asking = Promise.resolve();
|
|
99
|
+
/** 日志里记下的"总是允许"规则,每次调用工具前取一次:子 Agent 在工具执行期间经 allows 用它 */
|
|
100
|
+
let logged = [];
|
|
101
|
+
const id = ctx.session.id;
|
|
102
|
+
const check = (call) => rules.decide(call, { mode: mode === "yolo" ? "yolo" : "readonly", rules: [...logged, ...added] }).decision === "allow";
|
|
103
|
+
sessionChecks.set(id, check);
|
|
104
|
+
await ctx.effect(() => () => {
|
|
105
|
+
if (sessionChecks.get(id) === check)
|
|
106
|
+
sessionChecks.delete(id);
|
|
107
|
+
});
|
|
108
|
+
ctx.kernel.hook({
|
|
109
|
+
kind: "transform",
|
|
110
|
+
point: "tool.before",
|
|
111
|
+
fn: (call, hook) => {
|
|
112
|
+
logged = hook.derived[RULES_KEY] ?? [];
|
|
113
|
+
return { ...call, input: rules.absolutePath(call.input) };
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
ctx.kernel.hook({
|
|
117
|
+
kind: "gate",
|
|
118
|
+
point: "tool.gate",
|
|
119
|
+
timeoutMs: ASK_TIMEOUT_MS,
|
|
120
|
+
fn: async (call, hook) => {
|
|
121
|
+
const saved = () => {
|
|
122
|
+
const logged = hook.derived[RULES_KEY] ?? [];
|
|
123
|
+
return [...logged, ...added.filter((r) => !logged.some((l) => JSON.stringify(l) === JSON.stringify(r)))];
|
|
124
|
+
};
|
|
125
|
+
const result = rules.decide(call, { mode, rules: saved() });
|
|
126
|
+
if (result.decision !== "ask")
|
|
127
|
+
return result;
|
|
128
|
+
// auto 模式:规则判断不了、本来要问的调用先让模型审一次。审查只能放行或转给用户,不会替用户拒绝
|
|
129
|
+
let why;
|
|
130
|
+
if (review && mode === "auto") {
|
|
131
|
+
const key = reviewedKey(call);
|
|
132
|
+
if (key !== undefined && reviewedCalls.has(key))
|
|
133
|
+
return { decision: "allow", reason: "This exact call was already approved in this session." };
|
|
134
|
+
const verdict = await review(call, hook.signal);
|
|
135
|
+
// 审查调用不经过 model.responded,用量只能记在这里,界面据此计入会话用量
|
|
136
|
+
if (verdict.usage)
|
|
137
|
+
await hook.emit("reviewed", { toolUseId: call.toolUseId, usage: verdict.usage });
|
|
138
|
+
if (verdict.decision === "allow") {
|
|
139
|
+
if (key !== undefined)
|
|
140
|
+
reviewedCalls.add(key);
|
|
141
|
+
return { decision: "allow", reason: verdict.reason };
|
|
142
|
+
}
|
|
143
|
+
why = verdict.reason;
|
|
144
|
+
}
|
|
145
|
+
if (!ui?.interactive && approvers.size === 0) {
|
|
146
|
+
if (!warned) {
|
|
147
|
+
warned = true;
|
|
148
|
+
ui?.notify(`${call.name} needs approval, but this jade cannot ask. To let it make changes in the working directory, ` +
|
|
149
|
+
`set {"jade/approval": {"mode": "auto"}} in ${ctx.home}/config.json`, "warning");
|
|
150
|
+
}
|
|
151
|
+
return { decision: "deny", reason: `${call.name} needs the user's approval, but nobody can approve it in this non-interactive run. Do not retry it; tell the user what you wanted to do.` };
|
|
152
|
+
}
|
|
153
|
+
const turn = asking.then(async () => {
|
|
154
|
+
const again = rules.decide(call, { mode, rules: saved() });
|
|
155
|
+
if (again.decision !== "ask")
|
|
156
|
+
return again;
|
|
157
|
+
const rule = rules.ruleFor(call);
|
|
158
|
+
const shown = (await preview(renderers, call)) ?? detail(call);
|
|
159
|
+
const answer = await ask(ui, {
|
|
160
|
+
title: `Allow ${call.name}?`,
|
|
161
|
+
// 审查转给用户的:先说为什么
|
|
162
|
+
detail: why ? `${why}\n\n${shown}` : shown,
|
|
163
|
+
options: [
|
|
164
|
+
{ label: "Allow once", value: "once" },
|
|
165
|
+
{ label: `Always allow ${describeRule(rule)} in this session`, value: "always" },
|
|
166
|
+
{ label: "Deny", value: "deny" },
|
|
167
|
+
],
|
|
168
|
+
}, hook.signal);
|
|
169
|
+
if (answer === "always") {
|
|
170
|
+
added.push(rule);
|
|
171
|
+
await hook.record(RULES_KEY, saved());
|
|
172
|
+
return { decision: "allow" };
|
|
173
|
+
}
|
|
174
|
+
if (answer === "once")
|
|
175
|
+
return { decision: "allow" };
|
|
176
|
+
return { decision: "deny", reason: "The user denied this tool call. Do not retry it unchanged: adjust, or ask what to do instead." };
|
|
177
|
+
});
|
|
178
|
+
asking = turn.catch(() => { });
|
|
179
|
+
return turn;
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
/** allowRead、allowWrite:把目录登记进 roots,调用方的作用域关闭时撤销 */
|
|
185
|
+
function allow(caller, name, roots, dir) {
|
|
186
|
+
if (caller.layer === "project")
|
|
187
|
+
throw new Error(`approval.${name}: project plugins cannot widen what runs without asking`);
|
|
188
|
+
if (typeof dir !== "string" || !path.isAbsolute(dir))
|
|
189
|
+
throw new Error(`approval.${name}: dir must be an absolute path`);
|
|
190
|
+
const root = path.resolve(dir);
|
|
191
|
+
roots.push(root);
|
|
192
|
+
const remove = () => {
|
|
193
|
+
const i = roots.indexOf(root);
|
|
194
|
+
if (i !== -1)
|
|
195
|
+
roots.splice(i, 1);
|
|
196
|
+
};
|
|
197
|
+
caller.onDispose(remove);
|
|
198
|
+
return remove;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 把问题同时交给终端(能提问时)和所有审批渠道,返回先到的回答,然后中止其余的。
|
|
202
|
+
* 终端里取消(undefined)算拒绝;渠道返回 undefined、不认识的值或出错算它不回答。都不回答时返回 undefined(拒绝)
|
|
203
|
+
*/
|
|
204
|
+
async function ask(ui, request, signal) {
|
|
205
|
+
const done = new AbortController();
|
|
206
|
+
const stop = () => done.abort(signal.reason);
|
|
207
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
208
|
+
try {
|
|
209
|
+
return await new Promise((resolve, reject) => {
|
|
210
|
+
const channels = [];
|
|
211
|
+
if (ui?.interactive)
|
|
212
|
+
channels.push(ui.select(request, done.signal).then((a) => (a !== undefined && ANSWERS.includes(a) ? a : "deny")));
|
|
213
|
+
for (const approver of approvers) {
|
|
214
|
+
channels.push(Promise.resolve()
|
|
215
|
+
.then(() => approver(request, done.signal))
|
|
216
|
+
.then((a) => (typeof a === "string" && ANSWERS.includes(a) ? a : NONE), () => NONE));
|
|
217
|
+
}
|
|
218
|
+
let pending = channels.length;
|
|
219
|
+
if (pending === 0)
|
|
220
|
+
return resolve(undefined);
|
|
221
|
+
for (const c of channels) {
|
|
222
|
+
c.then((a) => {
|
|
223
|
+
if (a !== NONE)
|
|
224
|
+
resolve(a);
|
|
225
|
+
else if (--pending === 0)
|
|
226
|
+
resolve(undefined);
|
|
227
|
+
}, reject);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
signal.removeEventListener("abort", stop);
|
|
233
|
+
done.abort(new Error("answered"));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const NONE = Symbol("no answer");
|
|
237
|
+
/**
|
|
238
|
+
* 工具自己登记的审批预览(例如文件修改的 diff)。没有登记、返回的不是文字、或者出错时返回 undefined,
|
|
239
|
+
* 用下面的 detail:预览是别的插件的代码,它坏了不能让审批卡住或失败
|
|
240
|
+
*/
|
|
241
|
+
async function preview(renderers, call) {
|
|
242
|
+
let timer;
|
|
243
|
+
try {
|
|
244
|
+
// 预览可能读文件、算 diff;一直不返回时也要能问用户
|
|
245
|
+
const late = new Promise((resolve) => {
|
|
246
|
+
timer = setTimeout(() => resolve(undefined), PREVIEW_TIMEOUT_MS);
|
|
247
|
+
});
|
|
248
|
+
const text = await Promise.race([Promise.resolve(renderers?.forTool(call.name)?.renderer.preview?.(call.input)), late]);
|
|
249
|
+
return typeof text === "string" ? text : undefined;
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
clearTimeout(timer);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/** 给用户看的调用内容:命令、路径和要写的内容,太长的截断 */
|
|
259
|
+
function detail(call) {
|
|
260
|
+
const input = call.input;
|
|
261
|
+
let text;
|
|
262
|
+
if (typeof input?.command === "string")
|
|
263
|
+
text = input.command;
|
|
264
|
+
else if (typeof input?.path === "string" && typeof input.content === "string")
|
|
265
|
+
text = `${input.path}\n\n${input.content}`;
|
|
266
|
+
else if (typeof input?.path === "string" && typeof input.old_string === "string")
|
|
267
|
+
text = `${input.path}\n\n- ${input.old_string}\n+ ${String(input.new_string)}`;
|
|
268
|
+
else
|
|
269
|
+
text = JSON.stringify(input, null, 2);
|
|
270
|
+
return text.length > MAX_DETAIL_CHARS ? `${text.slice(0, MAX_DETAIL_CHARS)}\n… (${text.length - MAX_DETAIL_CHARS} more characters)` : text;
|
|
271
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/approval",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"provides": [
|
|
7
|
+
"approval@1"
|
|
8
|
+
],
|
|
9
|
+
"optional": [
|
|
10
|
+
"ui@1",
|
|
11
|
+
"renderers@1",
|
|
12
|
+
"models@1"
|
|
13
|
+
],
|
|
14
|
+
"config": {
|
|
15
|
+
"mode": {
|
|
16
|
+
"default": "ask",
|
|
17
|
+
"description": "ask: ask before changes; auto: changes inside the working directory and harmless commands go through; plan: change nothing, present a plan; readonly: change nothing; yolo: never ask"
|
|
18
|
+
},
|
|
19
|
+
"review": {
|
|
20
|
+
"default": true,
|
|
21
|
+
"description": "in auto mode, let the model review the calls the rules cannot decide (it can only allow or pass them to you); false turns the review off"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 审批规则:按模式、工具的 effect、路径和"总是允许"的规则决定一次工具调用是放行、拒绝还是问用户。
|
|
2
|
+
// 纯判断,不碰界面和日志;那些在 index.ts。
|
|
2
3
|
//
|
|
3
4
|
// 带 url 参数的只读工具(web_fetch)也要问:网页里的提示注入可以让模型把读到的内容拼进 URL 发出去。
|
|
4
|
-
//
|
|
5
|
-
// 模式和"总是允许"的规则由宿主用 Runtime.record 写进日志(derived),关卡从 ctx.derived 读取:
|
|
6
|
-
// 决定只依赖日志里的东西和调用本身,恢复会话后设置仍然有效。
|
|
7
5
|
import { realpathSync, statSync } from "node:fs";
|
|
8
6
|
import path from "node:path";
|
|
9
7
|
import { validate } from "./schema.js";
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
/**
|
|
9
|
+
* 权限模式。ask:改东西先问;auto:工作目录里的改动和只建目录、碰文件的命令不问;
|
|
10
|
+
* plan:不改任何东西,拒绝时告诉模型先给出计划;readonly:不改任何东西(子 Agent 用);yolo:什么都不问
|
|
11
|
+
*/
|
|
12
|
+
export const MODES = ["ask", "auto", "plan", "readonly", "yolo"];
|
|
14
13
|
/** 显示用 */
|
|
15
14
|
export function describeRule(r) {
|
|
16
15
|
if (r.command !== undefined)
|
|
@@ -23,8 +22,6 @@ export function describeRule(r) {
|
|
|
23
22
|
return `${r.tool} from ${r.host}`;
|
|
24
23
|
return r.tool;
|
|
25
24
|
}
|
|
26
|
-
/** 审查要调用模型,内核默认 30 秒的 hook 时限不够(超时按拒绝处理);审查自己会更早放弃并改为问用户 */
|
|
27
|
-
const REVIEW_HOOK_TIMEOUT_MS = 120_000;
|
|
28
25
|
/** 白名单命令里会写文件或执行其他程序的参数 */
|
|
29
26
|
const UNSAFE_ARGS = [/\s--(output|pre|pre-glob|ext-diff|textconv|open-files-in-pager)(=|\s|$)/];
|
|
30
27
|
/** 不管参数是什么都只读的命令(路径另外检查) */
|
|
@@ -378,8 +375,6 @@ function real(p) {
|
|
|
378
375
|
}
|
|
379
376
|
}
|
|
380
377
|
export function policy(opts) {
|
|
381
|
-
const id = opts.id ?? "jade/policy";
|
|
382
|
-
const defaults = { mode: opts.mode ?? "ask", rules: [] };
|
|
383
378
|
/** p 在 base 目录里 */
|
|
384
379
|
const within = (base, p) => {
|
|
385
380
|
// 按真实路径比较:目录里指向外面的符号链接也算外面
|
|
@@ -387,7 +382,7 @@ export function policy(opts) {
|
|
|
387
382
|
// 只有 ".." 这一段才表示上级目录,名为 "..foo" 的目录仍在里面
|
|
388
383
|
return rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
|
|
389
384
|
};
|
|
390
|
-
const inside = (p) => within(opts.cwd, p) || (opts.
|
|
385
|
+
const inside = (p) => within(opts.cwd, p) || (opts.writeRoots ?? []).some((r) => within(r, p));
|
|
391
386
|
/** glob 的 pattern 是绝对路径或含 ".." 段时,匹配范围会超出 path */
|
|
392
387
|
const globEscapes = (pattern) => path.isAbsolute(pattern) || pattern.split(/[\\/]/).includes("..");
|
|
393
388
|
/** 以 "." 或 "[" 开头的通配段可能匹配到 ".."(取决于 bash 版本和选项),按越界处理 */
|
|
@@ -440,30 +435,13 @@ export function policy(opts) {
|
|
|
440
435
|
}
|
|
441
436
|
return outside ? "outside" : "inside";
|
|
442
437
|
};
|
|
443
|
-
/**
|
|
444
|
-
* auto 模式下审查放行过的"工具 + 主机",本会话里之后访问同一个主机不再审。
|
|
445
|
-
* URL 带出数据要对方主机由攻击者控制,审查已经认可了这个主机,再逐个 URL 审收益很小、每次却要多等一次模型。
|
|
446
|
-
* 只在内存里:恢复会话后重新审一次;每个决定照样记在 tool.decided 里(I-5)
|
|
447
|
-
*/
|
|
448
|
-
const reviewedSites = new Set();
|
|
449
|
-
const reviewedSite = (call) => {
|
|
450
|
-
const url = call.input?.url;
|
|
451
|
-
const host = call.tool.effect === "read" && typeof url === "string" ? hostOf(url) : undefined;
|
|
452
|
-
return host ? `${call.name} ${host}` : undefined;
|
|
453
|
-
};
|
|
454
438
|
/** 把 classifyBash 的结果变成决定 */
|
|
455
439
|
const bashGate = (r) => r === undefined ? undefined : { decision: r === "outside" ? "ask" : "allow" };
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
if (legacy === "auto-edit")
|
|
462
|
-
stored.mode = "auto";
|
|
463
|
-
else if (legacy === "readonly" && derived[POLICY_KEY] !== undefined)
|
|
464
|
-
stored.mode = "plan";
|
|
465
|
-
const pending = opts.pendingMode?.();
|
|
466
|
-
return pending ? { ...stored, mode: pending } : stored;
|
|
440
|
+
return {
|
|
441
|
+
absolutePath(input) {
|
|
442
|
+
if (!input || typeof input !== "object" || typeof input.path !== "string")
|
|
443
|
+
return input;
|
|
444
|
+
return { ...input, path: path.resolve(opts.cwd, input.path) };
|
|
467
445
|
},
|
|
468
446
|
ruleFor(call) {
|
|
469
447
|
if (call.name === "bash")
|
|
@@ -511,7 +489,7 @@ export function policy(opts) {
|
|
|
511
489
|
return network && hostOf(input.url) === r.host;
|
|
512
490
|
return !outside;
|
|
513
491
|
};
|
|
514
|
-
const allRules =
|
|
492
|
+
const allRules = rules;
|
|
515
493
|
const ruled = allRules.some(covers);
|
|
516
494
|
/** 复合命令里的一条简单命令被 bash 规则覆盖 */
|
|
517
495
|
const ruledWords = (words) => allRules.some((r) => r.tool === "bash" &&
|
|
@@ -534,7 +512,7 @@ export function policy(opts) {
|
|
|
534
512
|
return {
|
|
535
513
|
decision: "deny",
|
|
536
514
|
reason: mode === "plan"
|
|
537
|
-
? `The session is in plan mode, so ${call.name} is not allowed yet. Finish investigating, then present your plan
|
|
515
|
+
? `The session is in plan mode, so ${call.name} is not allowed yet. Finish investigating, then present your plan for the user to approve as the plan mode instructions describe (with propose_plan when you have it).`
|
|
538
516
|
: `The session is in read-only mode, so ${call.name} is not allowed. Describe the change instead of making it.`,
|
|
539
517
|
};
|
|
540
518
|
}
|
|
@@ -546,49 +524,5 @@ export function policy(opts) {
|
|
|
546
524
|
const gate = isBash ? bashGate(classifyBash(command, { edits: mode === "auto", ruled: ruledWords })) : undefined;
|
|
547
525
|
return gate ?? { decision: "ask" };
|
|
548
526
|
},
|
|
549
|
-
plugin: {
|
|
550
|
-
id,
|
|
551
|
-
version: "0.3.0",
|
|
552
|
-
kernel: "^0.1",
|
|
553
|
-
// log:emit:auto 模式的审查把用掉的 token 记进日志
|
|
554
|
-
needs: ["transform:tool.before", "gate:tool.gate", "log:emit"],
|
|
555
|
-
hooks: [
|
|
556
|
-
{
|
|
557
|
-
kind: "transform",
|
|
558
|
-
point: "tool.before",
|
|
559
|
-
// 让日志里的 tool.decided 记录的是确定的绝对路径,关卡也按它判断
|
|
560
|
-
fn: (call) => {
|
|
561
|
-
const input = call.input;
|
|
562
|
-
if (!input || typeof input !== "object" || typeof input.path !== "string")
|
|
563
|
-
return;
|
|
564
|
-
return { ...call, input: { ...input, path: path.resolve(opts.cwd, input.path) } };
|
|
565
|
-
},
|
|
566
|
-
},
|
|
567
|
-
{
|
|
568
|
-
kind: "gate",
|
|
569
|
-
point: "tool.gate",
|
|
570
|
-
timeoutMs: REVIEW_HOOK_TIMEOUT_MS,
|
|
571
|
-
fn: async (call, ctx) => {
|
|
572
|
-
const settings = self.settings(ctx.derived);
|
|
573
|
-
const result = self.decide(call, settings);
|
|
574
|
-
// 决定记在 tool.decided 里(I-5),重建时不会再审一遍
|
|
575
|
-
if (settings.mode !== "auto" || result.decision !== "ask" || !opts.review)
|
|
576
|
-
return result;
|
|
577
|
-
const site = reviewedSite(call);
|
|
578
|
-
if (site && reviewedSites.has(site))
|
|
579
|
-
return { decision: "allow", reason: `${hostOf(String(call.input.url))} was already approved in this session` };
|
|
580
|
-
const { usage, ...verdict } = await opts.review(call, ctx.signal);
|
|
581
|
-
// 审查调用不经过 model.responded,用量只能记在这里(和压缩一样),界面据此计入会话用量
|
|
582
|
-
if (usage)
|
|
583
|
-
await ctx.emit("reviewed", { toolUseId: call.toolUseId, usage });
|
|
584
|
-
if (site && verdict.decision === "allow")
|
|
585
|
-
reviewedSites.add(site);
|
|
586
|
-
return verdict;
|
|
587
|
-
},
|
|
588
|
-
},
|
|
589
|
-
],
|
|
590
|
-
},
|
|
591
527
|
};
|
|
592
|
-
return self;
|
|
593
528
|
}
|
|
594
|
-
//# sourceMappingURL=policy.js.map
|