@uncarved/jade 0.1.3 → 0.2.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 +161 -150
- package/bin/jade.js +14 -25
- 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 +160 -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 +311 -0
- package/plugins/approval/plugin.json +22 -0
- package/{src/plugins → plugins/approval}/policy.ts +45 -104
- 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 +80 -39
- 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 +89 -46
- package/{src/tools → plugins/fs}/grep.ts +36 -8
- package/plugins/fs/index.ts +37 -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 +99 -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 +213 -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 +160 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +111 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +258 -0
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +84 -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 +168 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +343 -0
- package/plugins/todo/index.ts +93 -0
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +3 -0
- package/plugins/tui/frontend.ts +619 -0
- package/plugins/tui/index.ts +37 -0
- package/plugins/tui/mentions.ts +153 -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 +79 -100
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +360 -392
- package/plugins/tui/vendor.ts +16 -0
- package/{src/tools → plugins/undo}/checkpoint.ts +45 -60
- package/plugins/undo/index.ts +143 -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 +64 -31
- package/plugins-dist/approval/index.js +287 -0
- package/plugins-dist/approval/plugin.json +28 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +41 -85
- 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 +58 -34
- 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 +82 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +36 -11
- package/plugins-dist/fs/index.js +39 -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 +87 -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 +181 -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 +172 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +85 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +231 -0
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +58 -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 +152 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +284 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +31 -9
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
- package/plugins-dist/tui/frontend.js +609 -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 +153 -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 +79 -99
- package/{dist/ui → plugins-dist/tui}/tui.js +185 -54
- package/plugins-dist/tui/vendor.js +13822 -0
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +48 -55
- package/plugins-dist/undo/index.js +143 -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 +56 -29
- 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 +156 -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/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/tools/todo.ts +0 -67
- 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,7 +8,8 @@
|
|
|
8
8
|
// 检查来源:
|
|
9
9
|
// - 自动发现:TypeScript(离文件最近的 tsconfig.json,加上项目里装的 typescript)、Python(PATH 里的 ruff)、Go(go.mod 和 go)
|
|
10
10
|
// - 配置:仓库根目录的 .jade/checks.json 和 $JADE_HOME/checks.json,见 CheckConfigFile
|
|
11
|
-
// 检查会执行仓库里的代码(node_modules 里的 tsc
|
|
11
|
+
// 检查会执行仓库里的代码(node_modules 里的 tsc、配置里的命令),每条命令第一次运行前请用户确认(approve,见 index.ts)。
|
|
12
|
+
// 从旧 jade 的 src/plugins/checks.ts 移植,只是把内核插件换成一个 hook(交给 ctx.kernel.hook)。
|
|
12
13
|
//
|
|
13
14
|
// 只报告改过的文件里的问题;其他文件里的问题只在数量比上一次检查多时报告(例如改了签名,调用方报错)。
|
|
14
15
|
|
|
@@ -16,9 +17,8 @@ import { createHash } from "node:crypto";
|
|
|
16
17
|
import { existsSync, realpathSync } from "node:fs";
|
|
17
18
|
import { readFile } from "node:fs/promises";
|
|
18
19
|
import path from "node:path";
|
|
19
|
-
import type { Block, HookContext,
|
|
20
|
-
import type { RunCommand } from "../
|
|
21
|
-
import { which as findExecutable } from "../sys.ts";
|
|
20
|
+
import type { Block, HookContext, LocalHookSpec, Message } from "uncarved";
|
|
21
|
+
import type { RunCommand } from "../shell/shell.ts";
|
|
22
22
|
|
|
23
23
|
/** 老版本把整个 ChecksState 记在这一个 key 下、每次检查整份重记;恢复老会话时还要读它 */
|
|
24
24
|
export const CHECKS_KEY = "jade/checks";
|
|
@@ -71,15 +71,16 @@ export interface ChecksOptions {
|
|
|
71
71
|
home: string;
|
|
72
72
|
run: RunCommand;
|
|
73
73
|
approve: ApproveCheck;
|
|
74
|
-
/** 运行 tsc 的 JavaScript 运行时,默认当前进程(bun) */
|
|
74
|
+
/** 运行 tsc 的 JavaScript 运行时,默认当前进程(bun 或 node) */
|
|
75
75
|
runtime?: string;
|
|
76
|
-
/** 默认在 PATH
|
|
76
|
+
/** 默认在 PATH 里找(由调用方给,见 index.ts 的 shell@1);测试用 */
|
|
77
77
|
which?: (bin: string) => string | null;
|
|
78
78
|
id?: string;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export interface Checks {
|
|
82
|
-
|
|
82
|
+
/** context.project 的 hook */
|
|
83
|
+
hook: LocalHookSpec;
|
|
83
84
|
/** 本会话是否运行检查(/checks on|off) */
|
|
84
85
|
enabled: boolean;
|
|
85
86
|
/** 对这些文件会运行的检查(/checks 显示用) */
|
|
@@ -162,7 +163,7 @@ export function parseProblems(output: string, cwd: string): Problem[] {
|
|
|
162
163
|
|
|
163
164
|
export function checks(opts: ChecksOptions): Checks {
|
|
164
165
|
const id = opts.id ?? "jade/checks";
|
|
165
|
-
const which = opts.which ?? ((
|
|
166
|
+
const which = opts.which ?? (() => null);
|
|
166
167
|
const runtime = opts.runtime ?? process.execPath;
|
|
167
168
|
/** 本会话里不再运行的检查:用户拒绝了,或者输出认不出来 */
|
|
168
169
|
const disabled = new Set<string>();
|
|
@@ -373,37 +374,46 @@ export function checks(opts: ChecksOptions): Checks {
|
|
|
373
374
|
async resolve(files) {
|
|
374
375
|
return [...(await plan(files)).values()].map((e) => e.check);
|
|
375
376
|
},
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
await ctx.emit("checked", { toolUseId: last, files: batch.files, summary, report: text });
|
|
399
|
-
return inject(messages, { ...state.results, [last]: text });
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
return inject(messages, state.results);
|
|
403
|
-
},
|
|
404
|
-
},
|
|
405
|
-
],
|
|
377
|
+
hook: {
|
|
378
|
+
kind: "transform",
|
|
379
|
+
point: "context.project",
|
|
380
|
+
// 检查可能要跑一两分钟,第一次还要等用户确认;出错或超时就跳过,不让这一轮失败
|
|
381
|
+
timeoutMs: 900_000,
|
|
382
|
+
optional: true,
|
|
383
|
+
fn: async (messages, ctx) => {
|
|
384
|
+
const state = stateOf(ctx.derived);
|
|
385
|
+
const batch = self.enabled ? lastBatch(messages) : undefined;
|
|
386
|
+
const last = batch?.ids.at(-1);
|
|
387
|
+
if (batch && last && !(last in state.results)) {
|
|
388
|
+
const { text, summary, others } = await runAll(batch.files, state, ctx);
|
|
389
|
+
if (summary.length > 0 || text) {
|
|
390
|
+
await ctx.record(`${RESULT_PREFIX}${last}`, text);
|
|
391
|
+
// others 是 state.others 加上这次的数,有变化才记
|
|
392
|
+
if (Object.keys(others).some((k) => others[k] !== state.others[k])) await ctx.record(OTHERS_KEY, others);
|
|
393
|
+
await ctx.emit("checked", { toolUseId: last, files: batch.files, summary, report: text });
|
|
394
|
+
return inject(messages, { ...state.results, [last]: text });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return inject(messages, state.results);
|
|
398
|
+
},
|
|
406
399
|
},
|
|
407
400
|
};
|
|
408
401
|
return self;
|
|
409
402
|
}
|
|
403
|
+
|
|
404
|
+
/** 会话里成功改过的文件(/checks 显示用):所有写入调用里结果不是错误的 */
|
|
405
|
+
export function changedFiles(messages: readonly Message[], cwd: string): string[] {
|
|
406
|
+
const ok = new Set(messages.flatMap((m) => (m.role === "user" ? m.content.flatMap((b) => (b.kind === "tool_result" && !b.isError ? [b.toolUseId] : [])) : [])));
|
|
407
|
+
const files: string[] = [];
|
|
408
|
+
for (const m of messages) {
|
|
409
|
+
if (m.role !== "assistant") continue;
|
|
410
|
+
for (const b of m.content) {
|
|
411
|
+
if (b.kind !== "tool_use" || !WRITE_TOOLS.has(b.name) || !ok.has(b.id)) continue;
|
|
412
|
+
const p = (b.input as { path?: unknown } | null)?.path;
|
|
413
|
+
if (typeof p !== "string") continue;
|
|
414
|
+
const full = canonical(path.resolve(cwd, p));
|
|
415
|
+
if (!files.some((f) => sameFile(f, full))) files.push(full);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return files;
|
|
419
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// jade/checks:模型改了文件之后,下一次请求模型之前对改过的文件跑项目的检查(类型检查、lint),
|
|
2
|
+
// 把发现的问题附在这批修改的最后一个结果后面(见 checks.ts);/checks 查看、开关;对话里显示检查的结果。
|
|
3
|
+
//
|
|
4
|
+
// 检查在本轮之内、下一次请求模型之前(context.project)运行,不放到 session.afterTurn:模型要在同一轮里看到
|
|
5
|
+
// 自己刚引入的错误并改掉;轮次结束后才检查就得再发起一轮(session.send),等于替用户多说一句话。
|
|
6
|
+
//
|
|
7
|
+
// 检查会执行仓库里的代码(node_modules 里的 tsc、.jade/checks.json 里的命令),每条命令第一次运行前请用户确认,
|
|
8
|
+
// 记在 $JADE_HOME/trust.jsonl(和旧 jade 同一个文件、同一种记录,旧 jade 里确认过的不用再确认)。
|
|
9
|
+
// yolo 模式不问;不能提问时不运行没确认过的命令。
|
|
10
|
+
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { definePlugin, type SessionContext, type UI } from "../../src/api.ts";
|
|
14
|
+
import type { Approval } from "../approval/index.ts";
|
|
15
|
+
import type { Commands } from "../commands/index.ts";
|
|
16
|
+
import type { EventRenderer, Renderers } from "../renderers/index.ts";
|
|
17
|
+
import { changedFiles, checks, type Check, type Checks } from "./checks.ts";
|
|
18
|
+
import { bold, dim, display, realPath, repoRoot, TrustFile, yellow } from "./project.ts";
|
|
19
|
+
import type { Shell } from "../shell/service.ts";
|
|
20
|
+
|
|
21
|
+
/** 确认记录:同一个目录里的同一条命令只问一次。和旧 jade 的算法相同 */
|
|
22
|
+
const checkHash = (c: Check) => createHash("sha256").update(`jade-check\0${c.cwd}\0${c.command}`).digest("hex");
|
|
23
|
+
|
|
24
|
+
/** 展开后最多显示的行数 */
|
|
25
|
+
const MAX_DETAIL_LINES = 40;
|
|
26
|
+
|
|
27
|
+
/** 会话 id → 这个会话的检查(/checks 用) */
|
|
28
|
+
const bySession = new Map<string, Checks>();
|
|
29
|
+
/** process() 里准备好、session() 里调用:用进程级的确认记录和"一次只问一个问题" */
|
|
30
|
+
let sessionSetup: ((ctx: SessionContext) => Promise<void>) | undefined;
|
|
31
|
+
|
|
32
|
+
export const checksRenderers: EventRenderer[] = [
|
|
33
|
+
{
|
|
34
|
+
event: "checked",
|
|
35
|
+
render: (data) => {
|
|
36
|
+
const d = data as { summary: { name: string; problems: number; others?: number; failed?: string }[]; report: string };
|
|
37
|
+
const bad = d.summary.filter((s) => s.problems > 0 || s.others || s.failed);
|
|
38
|
+
const lines = d.report ? d.report.split("\n").slice(1) : [];
|
|
39
|
+
const details = lines.length
|
|
40
|
+
? [...lines.slice(0, MAX_DETAIL_LINES), ...(lines.length > MAX_DETAIL_LINES ? [dim(`… ${lines.length - MAX_DETAIL_LINES} more lines`)] : [])].join("\n")
|
|
41
|
+
: undefined;
|
|
42
|
+
const withDetails = (line: string) => (details ? { line, details } : { line });
|
|
43
|
+
if (bad.length === 0) return withDetails(dim(`✓ checks passed: ${d.summary.map((s) => s.name).join(", ")}`));
|
|
44
|
+
const what = bad.map((s) =>
|
|
45
|
+
s.failed
|
|
46
|
+
? `${s.name} ${s.failed}`
|
|
47
|
+
: `${s.name}: ${[s.problems > 0 ? `${s.problems} problem${s.problems > 1 ? "s" : ""} in changed files` : "", s.others ? `${s.others} new elsewhere` : ""]
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.join(", ")}`,
|
|
50
|
+
);
|
|
51
|
+
return withDetails(yellow(`⚠ checks: ${what.join("; ")}`));
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
export default definePlugin({
|
|
57
|
+
process(ctx) {
|
|
58
|
+
const off = ctx.config.enabled === false || process.env.JADE_CHECKS === "off";
|
|
59
|
+
const trust = new TrustFile(path.join(ctx.home, "trust.jsonl"));
|
|
60
|
+
const renderers = ctx.service<Renderers>("renderers@1");
|
|
61
|
+
if (renderers) for (const r of checksRenderers) renderers.event(r);
|
|
62
|
+
|
|
63
|
+
ctx.service<Commands>("commands@1")?.register({
|
|
64
|
+
name: "checks",
|
|
65
|
+
args: "[on|off]",
|
|
66
|
+
description: "checks jade runs after the agent edits files (type checker, linter)",
|
|
67
|
+
instant: true,
|
|
68
|
+
async run(args, c) {
|
|
69
|
+
const session = c.sessions.current;
|
|
70
|
+
const checks = session && bySession.get(session.id);
|
|
71
|
+
if (!checks) {
|
|
72
|
+
if (off) return dim(`Edit-time checks are turned off${process.env.JADE_CHECKS === "off" ? " by JADE_CHECKS=off" : ` ({"jade/checks": {"enabled": false}} in ${path.join(ctx.home, "config.json")})`}.`);
|
|
73
|
+
return dim(unavailable ?? "Edit-time checks are not available in this session.");
|
|
74
|
+
}
|
|
75
|
+
const arg = args.trim();
|
|
76
|
+
if (arg === "on" || arg === "off") {
|
|
77
|
+
checks.enabled = arg === "on";
|
|
78
|
+
if (arg === "on") checks.reset();
|
|
79
|
+
return `checks: ${arg}`;
|
|
80
|
+
}
|
|
81
|
+
if (arg) return yellow("usage: /checks [on|off]");
|
|
82
|
+
const files = changedFiles(session.state.messages, realPath(ctx.cwd));
|
|
83
|
+
const found = await checks.resolve(files);
|
|
84
|
+
const lines = [`Edit-time checks are ${checks.enabled ? "on" : "off"} in this session.`];
|
|
85
|
+
if (found.length > 0) {
|
|
86
|
+
lines.push("For the files changed in this session:");
|
|
87
|
+
for (const check of found) {
|
|
88
|
+
const pending = (await trust.has(checkHash(check))) ? "" : ", not confirmed yet";
|
|
89
|
+
lines.push(` ${bold(check.name)} ${dim(`in ${display(ctx.cwd, check.cwd) || "."}${pending}`)}\n ${dim(check.command)}`);
|
|
90
|
+
}
|
|
91
|
+
} else lines.push(dim(files.length ? "No checks apply to the files changed in this session." : "No files changed in this session yet."));
|
|
92
|
+
lines.push(
|
|
93
|
+
dim(
|
|
94
|
+
"Detected per file: TypeScript (nearest tsconfig.json + the project's typescript), Python (ruff), Go (go vet).\n" +
|
|
95
|
+
`Add your own in .jade/checks.json or ${path.join(ctx.home, "checks.json")}; JADE_CHECKS=off turns them off.`,
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
return lines.join("\n");
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/** 找不到 shell 时的说明,/checks 显示 */
|
|
103
|
+
let unavailable: string | undefined;
|
|
104
|
+
/** 一次只问一个问题 */
|
|
105
|
+
let asking: Promise<unknown> = Promise.resolve();
|
|
106
|
+
sessionSetup = async (s) => {
|
|
107
|
+
if (off) return;
|
|
108
|
+
// 找 shell、杀进程树都在 jade/shell 里(shell@1),这里不再复制一份
|
|
109
|
+
const shell = s.require<Shell>("shell@1");
|
|
110
|
+
if (!shell.executable) {
|
|
111
|
+
unavailable = `edit-time checks are off: ${shell.unavailable ?? "no shell was found"}`;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const ui = s.service<UI>("ui@1");
|
|
115
|
+
const approve = async (check: Check, signal: AbortSignal): Promise<boolean> => {
|
|
116
|
+
const hash = checkHash(check);
|
|
117
|
+
if (await trust.has(hash)) return true;
|
|
118
|
+
if (s.service<Approval>("approval@1")?.mode() === "yolo") return true;
|
|
119
|
+
if (!ui?.interactive) return false;
|
|
120
|
+
const turn = asking.then(async () => {
|
|
121
|
+
if (await trust.has(hash)) return true;
|
|
122
|
+
signal.throwIfAborted();
|
|
123
|
+
const answer = await ui.select(
|
|
124
|
+
{
|
|
125
|
+
title: `Run ${check.name} after the agent edits files, and show it the problems found?`,
|
|
126
|
+
detail: `${check.command}\nin ${display(s.cwd, check.cwd) || "."}${check.source === "config" ? " (from checks.json)" : " (detected)"}`,
|
|
127
|
+
options: [
|
|
128
|
+
{ label: "Yes, in this project from now on", value: "yes" },
|
|
129
|
+
{ label: "No, not in this session", value: "no" },
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
signal,
|
|
133
|
+
);
|
|
134
|
+
if (answer === "yes") {
|
|
135
|
+
await trust.add(hash, check.cwd);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
ui.notify(`${check.name} will not run in this session; /checks on to ask again`);
|
|
139
|
+
return false;
|
|
140
|
+
});
|
|
141
|
+
asking = turn.catch(() => {});
|
|
142
|
+
return turn;
|
|
143
|
+
};
|
|
144
|
+
const cwd = realPath(s.cwd);
|
|
145
|
+
const c = checks({ cwd, root: realPath(repoRoot(cwd)), home: s.home, run: shell.run, which: (bin) => shell.which(bin) ?? null, approve });
|
|
146
|
+
const id = s.session.id;
|
|
147
|
+
bySession.set(id, c);
|
|
148
|
+
await s.effect(() => () => {
|
|
149
|
+
if (bySession.get(id) === c) bySession.delete(id);
|
|
150
|
+
});
|
|
151
|
+
s.kernel.hook(c.hook);
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
async session(ctx) {
|
|
156
|
+
await sessionSetup?.(ctx);
|
|
157
|
+
},
|
|
158
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/checks",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"requires": ["shell@1"],
|
|
7
|
+
"optional": ["ui@1", "approval@1", "renderers@1", "commands@1"],
|
|
8
|
+
"config": {
|
|
9
|
+
"enabled": {
|
|
10
|
+
"default": true,
|
|
11
|
+
"description": "run the project's checks after the agent edits files; JADE_CHECKS=off turns them off too"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// 仓库目录、确认记录、显示用的小工具。插件之间不共享代码(只能共享类型),jade/mcp、jade/skills 各有一份。
|
|
2
|
+
|
|
3
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
4
|
+
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
export function realPath(p: string): string {
|
|
8
|
+
try {
|
|
9
|
+
return realpathSync.native(p);
|
|
10
|
+
} catch {
|
|
11
|
+
return path.resolve(p);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** git 仓库的根目录(往上找 .git,目录或文件都算);不在仓库里时是 cwd 本身 */
|
|
16
|
+
export function repoRoot(cwd: string): string {
|
|
17
|
+
let d = path.resolve(cwd);
|
|
18
|
+
for (;;) {
|
|
19
|
+
if (existsSync(path.join(d, ".git"))) return d;
|
|
20
|
+
const parent = path.dirname(d);
|
|
21
|
+
if (parent === d) return path.resolve(cwd);
|
|
22
|
+
d = parent;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 工作目录里的显示成相对路径,外面的显示绝对路径 */
|
|
27
|
+
export function display(cwd: string, full: string): string {
|
|
28
|
+
const rel = path.relative(cwd, full);
|
|
29
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
30
|
+
return shown.split(path.sep).join("/");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 用户确认过的东西(检查命令),每行一条 { hash, path, at }。
|
|
35
|
+
* 和旧 jade 的 $JADE_HOME/trust.jsonl 同一个文件、同一种格式,旧 jade 里确认过的不用再确认
|
|
36
|
+
*/
|
|
37
|
+
export class TrustFile {
|
|
38
|
+
readonly file: string;
|
|
39
|
+
#known = new Set<string>();
|
|
40
|
+
|
|
41
|
+
constructor(file: string) {
|
|
42
|
+
this.file = file;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async has(hash: string): Promise<boolean> {
|
|
46
|
+
if (this.#known.has(hash)) return true;
|
|
47
|
+
let text = "";
|
|
48
|
+
try {
|
|
49
|
+
text = await readFile(this.file, "utf8");
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
for (const line of text.split("\n")) {
|
|
54
|
+
try {
|
|
55
|
+
if ((JSON.parse(line) as { hash?: unknown }).hash === hash) {
|
|
56
|
+
this.#known.add(hash);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// 写到一半的行
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async add(hash: string, where: string): Promise<void> {
|
|
67
|
+
await mkdir(path.dirname(this.file), { recursive: true });
|
|
68
|
+
await appendFile(this.file, `${JSON.stringify({ hash, path: where, at: new Date().toISOString() })}\n`);
|
|
69
|
+
this.#known.add(hash);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
74
|
+
|
|
75
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
76
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
77
|
+
const paint = (open: string, close: string) => (s: string) =>
|
|
78
|
+
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
79
|
+
export const dim = paint("2", "22");
|
|
80
|
+
export const yellow = paint("33", "39");
|
|
81
|
+
export const bold = paint("1", "22");
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// jade/commands:斜杠命令的注册表。只登记,不执行:前端解析输入、带着自己的上下文调用 run。
|
|
2
|
+
// 每个调用方一个实例,调用方关闭(停用、被隔离、换会话时的会话作用域)时它注册的命令自动撤销。
|
|
3
|
+
|
|
4
|
+
import { definePlugin, type SessionManager, type UI } from "../../src/api.ts";
|
|
5
|
+
|
|
6
|
+
export interface Command {
|
|
7
|
+
/** 不带斜杠,字母、数字、- 和 _ */
|
|
8
|
+
name: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
/** 参数提示,例如 "<file>" */
|
|
11
|
+
args?: string;
|
|
12
|
+
/**
|
|
13
|
+
* 不碰会话(只查看、只改插件自己的设置)的命令设为 true:模型工作时也立即执行,不排队。
|
|
14
|
+
* 这种命令返回 { prompt } 无效
|
|
15
|
+
*/
|
|
16
|
+
instant?: boolean;
|
|
17
|
+
/** 字符串:显示给用户;{ prompt }:作为用户消息发给模型;不返回:什么也不做 */
|
|
18
|
+
run(args: string, ctx: CommandContext): CommandResult | Promise<CommandResult>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type CommandResult = string | { prompt: string } | void | undefined;
|
|
22
|
+
|
|
23
|
+
/** 前端执行命令时给的上下文 */
|
|
24
|
+
export interface CommandContext {
|
|
25
|
+
/** 用户中断(esc)或退出时中止 */
|
|
26
|
+
signal: AbortSignal;
|
|
27
|
+
ui: UI;
|
|
28
|
+
/** 当前会话、新建、恢复(/clear、/resume 用它) */
|
|
29
|
+
sessions: SessionManager;
|
|
30
|
+
/** 在对话里显示一段文字,可以带 ANSI 颜色 */
|
|
31
|
+
print(text: string): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CommandEntry {
|
|
35
|
+
command: Command;
|
|
36
|
+
/** 注册它的插件 */
|
|
37
|
+
plugin: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Commands {
|
|
41
|
+
/** 注册一条命令,返回撤销函数。名字已被占用时抛错 */
|
|
42
|
+
register(command: Command): () => void;
|
|
43
|
+
/** 按名字找(不分大小写) */
|
|
44
|
+
get(name: string): CommandEntry | undefined;
|
|
45
|
+
/** 全部命令,按名字排序 */
|
|
46
|
+
list(): { name: string; args?: string; description?: string; instant?: boolean; plugin: string }[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
50
|
+
const registry = new Map<string, CommandEntry>();
|
|
51
|
+
|
|
52
|
+
export default definePlugin({
|
|
53
|
+
process(ctx) {
|
|
54
|
+
ctx.provide("commands@1", (caller): Commands => ({
|
|
55
|
+
register(command) {
|
|
56
|
+
if (typeof command?.name !== "string" || !NAME.test(command.name)) throw new Error(`invalid command name ${JSON.stringify(command?.name)}`);
|
|
57
|
+
if (typeof command.run !== "function") throw new Error(`command /${command.name} has no run function`);
|
|
58
|
+
const key = command.name.toLowerCase();
|
|
59
|
+
const taken = registry.get(key);
|
|
60
|
+
if (taken) throw new Error(`command /${command.name} is already registered by ${taken.plugin}`);
|
|
61
|
+
const entry: CommandEntry = { command, plugin: caller.plugin };
|
|
62
|
+
registry.set(key, entry);
|
|
63
|
+
const revoke = () => {
|
|
64
|
+
if (registry.get(key) === entry) registry.delete(key);
|
|
65
|
+
};
|
|
66
|
+
caller.onDispose(revoke);
|
|
67
|
+
return revoke;
|
|
68
|
+
},
|
|
69
|
+
get: (name) => registry.get(name.toLowerCase()),
|
|
70
|
+
list: () =>
|
|
71
|
+
[...registry.values()]
|
|
72
|
+
.map(({ command: c, plugin }) => ({ name: c.name, ...(c.args ? { args: c.args } : {}), ...(c.description ? { description: c.description } : {}), ...(c.instant ? { instant: true } : {}), plugin }))
|
|
73
|
+
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
74
|
+
}));
|
|
75
|
+
},
|
|
76
|
+
});
|