@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
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// 文件工具:read_file / write_file / edit_file / list_dir / glob,以及 grep(见 grep.ts)。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
// 再由关卡判断是否在工作目录内;工具本身不做权限判断。
|
|
3
|
+
// 路径可以是相对路径(相对于工作目录)或绝对路径。工具本身不做权限判断:那是审批插件的事。
|
|
5
4
|
//
|
|
6
5
|
// 修改已有文件之前必须先读过它,且读过之后文件没有被别人(用户、bash 命令)改过,
|
|
7
6
|
// 否则模型是按过时的内容在改,会覆盖掉别人的修改。和修改在同一次回复里发出的读取不算:
|
|
@@ -10,10 +9,9 @@
|
|
|
10
9
|
import type { Stats } from "node:fs";
|
|
11
10
|
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
12
11
|
import path from "node:path";
|
|
13
|
-
import type { Block, Tool } from "uncarved";
|
|
14
|
-
import { sha256, type Checkpoints } from "./checkpoint.ts";
|
|
12
|
+
import type { Block, Message, Tool } from "uncarved";
|
|
15
13
|
import { grepTool } from "./grep.ts";
|
|
16
|
-
import { globMatcher, run } from "
|
|
14
|
+
import { globMatcher, run } from "./sys.ts";
|
|
17
15
|
|
|
18
16
|
const MAX_LINES = 2000;
|
|
19
17
|
const MAX_LINE_CHARS = 2000;
|
|
@@ -47,42 +45,71 @@ export function display(cwd: string, full: string): string {
|
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
export interface FsOptions {
|
|
50
|
-
/**
|
|
51
|
-
checkpoints?: Checkpoints;
|
|
52
|
-
/** 模型调用的次数,每次调用模型前加一(见 distro 的 model.before hook)。不给就不检查同一次回复里的先读后改 */
|
|
48
|
+
/** 模型调用的次数,每次调用模型前加一(见 index.ts 的 model.before hook)。不给就不检查同一次回复里的先读后改 */
|
|
53
49
|
responses?: { readonly calls: number };
|
|
54
50
|
/** grep 用的 ripgrep,默认 "rg";null 表示不用(测试) */
|
|
55
51
|
rg?: string | null;
|
|
56
|
-
/**
|
|
57
|
-
|
|
52
|
+
/** 记录读过的文件;给了才能在压缩后让它们失效(见 SeenFiles.prune) */
|
|
53
|
+
seen?: SeenFiles;
|
|
58
54
|
}
|
|
59
55
|
|
|
60
|
-
/**
|
|
56
|
+
/** 读过或写过的文件当时的样子;只在本会话、本进程内有效,恢复的会话要重新读 */
|
|
61
57
|
interface Seen {
|
|
62
58
|
mtimeMs: number;
|
|
63
59
|
size: number;
|
|
64
60
|
/** 读取时的 responses.calls;写入的文件模型知道内容,记为 -1 */
|
|
65
61
|
call: number;
|
|
62
|
+
/**
|
|
63
|
+
* 模型从哪一次调用知道这个文件的内容:read_file 或 write_file 的 toolUseId。
|
|
64
|
+
* edit_file 只改了一部分,模型知道的仍是之前那次读取(加上改动),所以沿用原来的
|
|
65
|
+
*/
|
|
66
|
+
id?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 本会话读过、写过的文件 */
|
|
70
|
+
export class SeenFiles {
|
|
71
|
+
readonly files = new Map<string, Seen>();
|
|
72
|
+
/** 因为压缩而失效的文件:再改时说明原因,而不是说"没读过" */
|
|
73
|
+
readonly dropped = new Set<string>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 压缩或清理之后,模型看不到当初读到(写入)的内容了,但 mtime 和 size 没变,修改仍会放行;
|
|
77
|
+
* write_file 可能凭摘要里的记忆写出整个文件。这里按最终的投影检查:那次调用已被总结掉(投影里没有它的 tool_use),
|
|
78
|
+
* 或者结果已被清理(tool_result 里没有工具自己给的内容,只剩插件的占位说明),就当作没读过
|
|
79
|
+
*/
|
|
80
|
+
prune(projection: Message[]): void {
|
|
81
|
+
const uses = new Set<string>();
|
|
82
|
+
const cleared = new Set<string>();
|
|
83
|
+
for (const m of projection) {
|
|
84
|
+
for (const b of m.content) {
|
|
85
|
+
if (b.kind === "tool_use") uses.add(b.id);
|
|
86
|
+
else if (b.kind === "tool_result" && !b.content.some((c) => c.origin.by === "tool")) cleared.add(b.toolUseId);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const [full, s] of this.files) {
|
|
90
|
+
if (s.id === undefined || (uses.has(s.id) && !cleared.has(s.id))) continue;
|
|
91
|
+
this.files.delete(full);
|
|
92
|
+
this.dropped.add(full);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
66
95
|
}
|
|
67
96
|
|
|
68
97
|
export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
69
98
|
const at = (p: unknown) => resolveIn(cwd, p as string);
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const remember = async (ctx: { sessionId: string }, full: string, info: Stats, text: string, call: number) => {
|
|
99
|
+
const tracked = opts.seen ?? new SeenFiles();
|
|
100
|
+
const seen = tracked.files;
|
|
101
|
+
/**
|
|
102
|
+
* 文件没变时保留更早的 call:之前的回复里读过,这次回复里又读一遍,不影响修改。
|
|
103
|
+
* id 为 undefined 表示沿用原来的(edit_file)
|
|
104
|
+
*/
|
|
105
|
+
const remember = (full: string, info: Stats, call: number, id: string | undefined) => {
|
|
78
106
|
const s = seen.get(full);
|
|
79
107
|
const same = s !== undefined && s.mtimeMs === info.mtimeMs && s.size === info.size;
|
|
80
|
-
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call });
|
|
81
|
-
|
|
108
|
+
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call, id: id ?? s?.id });
|
|
109
|
+
tracked.dropped.delete(full);
|
|
82
110
|
};
|
|
83
111
|
/** 不能修改已有文件的原因;可以修改时返回 undefined。text 是文件现在的内容 */
|
|
84
|
-
const staleness =
|
|
85
|
-
if (inScratch(full)) return undefined;
|
|
112
|
+
const staleness = (full: string, info: Stats): string | undefined => {
|
|
86
113
|
const shown = display(cwd, full);
|
|
87
114
|
const modified = `${shown} has been modified since it was last read (by the user or a command). Read it again before changing it.`;
|
|
88
115
|
const s = seen.get(full);
|
|
@@ -93,15 +120,10 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
93
120
|
}
|
|
94
121
|
return undefined;
|
|
95
122
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: -1 });
|
|
101
|
-
return undefined;
|
|
102
|
-
};
|
|
103
|
-
const checkpoint = async (ctx: { sessionId: string; toolUseId: string }, tool: string, full: string, before: string | null, after: string) => {
|
|
104
|
-
await opts.checkpoints?.save(ctx.sessionId, { toolUseId: ctx.toolUseId, tool, path: full, before, afterHash: sha256(after) });
|
|
123
|
+
if (tracked.dropped.has(full)) {
|
|
124
|
+
return `The earlier read of ${shown} was compacted out of the conversation, so its content is no longer in context. Read it again before changing it.`;
|
|
125
|
+
}
|
|
126
|
+
return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
105
127
|
};
|
|
106
128
|
|
|
107
129
|
const readFileTool: Tool = {
|
|
@@ -151,7 +173,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
151
173
|
return { content: `${display(cwd, full)} is a binary file (${buf.length} bytes) and cannot be shown as text`, isError: true };
|
|
152
174
|
}
|
|
153
175
|
const text = buf.toString("utf8");
|
|
154
|
-
|
|
176
|
+
remember(full, info, opts.responses?.calls ?? -1, ctx?.toolUseId);
|
|
155
177
|
if (text.length === 0) return { content: "(empty file)" };
|
|
156
178
|
const lines = text.split(/\r?\n/);
|
|
157
179
|
if (lines.at(-1) === "") lines.pop();
|
|
@@ -200,12 +222,11 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
200
222
|
const before = info ? await readFile(full, "utf8") : null;
|
|
201
223
|
// read_file 不显示换行风格和 BOM:覆盖已有文件时沿用原来的,否则整个文件每一行都算改了
|
|
202
224
|
const content = before === null ? given : keepStyle(before, given);
|
|
203
|
-
const problem = info && before !== null && before !== content ?
|
|
225
|
+
const problem = info && before !== null && before !== content ? staleness(full, info) : undefined;
|
|
204
226
|
if (problem) return { content: problem, isError: true };
|
|
205
227
|
await mkdir(path.dirname(full), { recursive: true });
|
|
206
228
|
await writeFile(full, content);
|
|
207
|
-
|
|
208
|
-
if (before !== content) await checkpoint(ctx, "write_file", full, before, content);
|
|
229
|
+
remember(full, await stat(full), -1, ctx?.toolUseId);
|
|
209
230
|
// 和 read_file 一样数行:末尾的换行不算多出一行
|
|
210
231
|
const lines = content === "" ? 0 : content.split(/\r?\n/).length - (/\r?\n$/.test(content) ? 1 : 0);
|
|
211
232
|
return { content: `Wrote ${lines} lines to ${display(cwd, full)}` };
|
|
@@ -213,7 +234,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
213
234
|
};
|
|
214
235
|
|
|
215
236
|
/** edit_file 和 multi_edit 共用:检查文件能否修改,按顺序应用全部替换,全部成功才写入 */
|
|
216
|
-
const applyEdits = async (
|
|
237
|
+
const applyEdits = async (p: string, edits: Edit[]) => {
|
|
217
238
|
const full = at(p);
|
|
218
239
|
const shown = display(cwd, full);
|
|
219
240
|
const info = await statOrUndefined(full);
|
|
@@ -221,7 +242,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
221
242
|
return { content: seen.has(full) ? `${shown} no longer exists` : `${shown} does not exist; use write_file to create it`, isError: true };
|
|
222
243
|
}
|
|
223
244
|
const text = await readFile(full, "utf8");
|
|
224
|
-
const problem =
|
|
245
|
+
const problem = staleness(full, info);
|
|
225
246
|
if (problem) return { content: problem, isError: true };
|
|
226
247
|
let next = text;
|
|
227
248
|
let total = 0;
|
|
@@ -232,8 +253,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
232
253
|
total += r.count;
|
|
233
254
|
}
|
|
234
255
|
await writeFile(full, next);
|
|
235
|
-
|
|
236
|
-
await checkpoint(ctx, tool, full, text, next);
|
|
256
|
+
remember(full, await stat(full), -1, undefined);
|
|
237
257
|
return { content: `Edited ${shown} (${total} replacement${total === 1 ? "" : "s"})` };
|
|
238
258
|
};
|
|
239
259
|
|
|
@@ -253,9 +273,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
253
273
|
concurrency: "exclusive",
|
|
254
274
|
// 第二次执行时 old_string 可能已经不存在,或者被替换两次
|
|
255
275
|
idempotent: false,
|
|
256
|
-
async execute(input
|
|
276
|
+
async execute(input) {
|
|
257
277
|
const { path: p, ...edit } = input as { path: string } & Edit;
|
|
258
|
-
return applyEdits(
|
|
278
|
+
return applyEdits(p, [edit]);
|
|
259
279
|
},
|
|
260
280
|
};
|
|
261
281
|
|
|
@@ -284,10 +304,10 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
284
304
|
effect: "write",
|
|
285
305
|
concurrency: "exclusive",
|
|
286
306
|
idempotent: false,
|
|
287
|
-
async execute(input
|
|
307
|
+
async execute(input) {
|
|
288
308
|
const { path: p, edits } = input as { path: string; edits: Edit[] };
|
|
289
309
|
if (edits.length === 0) return { content: "edits must not be empty", isError: true };
|
|
290
|
-
return applyEdits(
|
|
310
|
+
return applyEdits(p, edits);
|
|
291
311
|
},
|
|
292
312
|
};
|
|
293
313
|
|
|
@@ -309,6 +329,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
309
329
|
name: "glob",
|
|
310
330
|
description:
|
|
311
331
|
`Find files by glob pattern (e.g. "src/**/*.ts"), relative to path (default: the working directory). ` +
|
|
332
|
+
`Unlike grep's include, "*.ts" matches only directly under path; use "**/*.ts" for any depth. ` +
|
|
312
333
|
`Skips files ignored by git, .git and node_modules. Returns at most ${MAX_GLOB} paths.`,
|
|
313
334
|
inputSchema: {
|
|
314
335
|
type: "object",
|
|
@@ -339,7 +360,8 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
339
360
|
out.push(display(cwd, path.join(root, rel)));
|
|
340
361
|
}
|
|
341
362
|
out.sort();
|
|
342
|
-
|
|
363
|
+
// 和 grep 的 include 不同,不含 / 的模式只匹配第一层:模型常按 grep 的习惯写
|
|
364
|
+
if (out.length === 0) return { content: pattern.includes("/") || pattern.startsWith("**") ? "No files matched" : `No files matched. "${pattern}" only matches directly under ${p === "." ? "the working directory" : p}; use "**/${pattern}" to search at any depth.` };
|
|
343
365
|
return { content: out.join("\n") + (more ? `\n(more than ${MAX_GLOB} matches; narrow the pattern)` : "") };
|
|
344
366
|
},
|
|
345
367
|
};
|
|
@@ -389,7 +411,9 @@ function replaceIn(text: string, { old_string, new_string, replace_all }: Edit,
|
|
|
389
411
|
const pattern = new RegExp(old_string.split(/\r?\n/).map(escapeRegExp).join("\\r?\\n"), "g");
|
|
390
412
|
const count = text.match(pattern)?.length ?? 0;
|
|
391
413
|
if (count === 0) return `old_string not found in ${shown}${nearMiss(text, old_string)}`;
|
|
392
|
-
if (count > 1 && !replace_all)
|
|
414
|
+
if (count > 1 && !replace_all) {
|
|
415
|
+
return `old_string occurs ${count} times in ${shown} (${linesOf(text, pattern, 5)}); add context to make it unique or set replace_all`;
|
|
416
|
+
}
|
|
393
417
|
// new_string 的换行沿用被替换那段的;那段只有一行时沿用整个文件的,单行改成多行也不会混进 LF
|
|
394
418
|
const fileEol = eolOf(text, "\n");
|
|
395
419
|
const to = (matched: string) => (new_string.includes("\r\n") ? new_string : new_string.replaceAll("\n", eolOf(matched, fileEol)));
|
|
@@ -397,11 +421,30 @@ function replaceIn(text: string, { old_string, new_string, replace_all }: Edit,
|
|
|
397
421
|
return { text: text.replace(replace_all ? pattern : new RegExp(pattern.source), to), count: replace_all ? count : 1 };
|
|
398
422
|
}
|
|
399
423
|
|
|
424
|
+
/** pattern 前 max 处匹配所在的行号,例如 "lines 3, 17, 40" */
|
|
425
|
+
function linesOf(text: string, pattern: RegExp, max: number): string {
|
|
426
|
+
const found: number[] = [];
|
|
427
|
+
let line = 1;
|
|
428
|
+
let from = 0;
|
|
429
|
+
for (const m of text.matchAll(new RegExp(pattern.source, "g"))) {
|
|
430
|
+
for (let i = from; i < m.index; i++) if (text.charCodeAt(i) === 10) line++;
|
|
431
|
+
from = m.index;
|
|
432
|
+
found.push(line);
|
|
433
|
+
if (found.length === max) break;
|
|
434
|
+
}
|
|
435
|
+
return `line${found.length > 1 ? "s" : ""} ${found.join(", ")}${found.length === max ? ", …" : ""}`;
|
|
436
|
+
}
|
|
437
|
+
|
|
400
438
|
/**
|
|
401
439
|
* old_string 找不到时帮模型定位:忽略空白后能匹配到哪几行,或者第一行出现在哪里。
|
|
402
440
|
* 只给提示,不自动按模糊匹配修改:那样可能改错地方
|
|
403
441
|
*/
|
|
404
442
|
export function nearMiss(text: string, needle: string): string {
|
|
443
|
+
// 最常见的一种:把 read_file 每行开头的行号和制表符一起抄进来了
|
|
444
|
+
const raw = needle.split(/\r?\n/).filter((l) => l.trim() !== "");
|
|
445
|
+
if (raw.length > 0 && raw.every((l) => /^\s*\d+\t/.test(l))) {
|
|
446
|
+
return ". It seems to include read_file's line-number prefixes (the number and tab at the start of each line); they are not part of the file. Remove them and keep the rest exactly";
|
|
447
|
+
}
|
|
405
448
|
const norm = (l: string) => l.trim().replace(/\s+/g, " ");
|
|
406
449
|
const lines = text.split(/\r?\n/).map(norm);
|
|
407
450
|
const want = needle.split(/\r?\n/).map(norm);
|
|
@@ -8,7 +8,7 @@ import { readFile, stat } from "node:fs/promises";
|
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
import type { Tool } from "uncarved";
|
|
10
10
|
import { display, listFiles, statOrUndefined } from "./fs.ts";
|
|
11
|
-
import { globMatcher, spawn, text, type Proc } from "
|
|
11
|
+
import { globMatcher, spawn, text, type Proc } from "./sys.ts";
|
|
12
12
|
|
|
13
13
|
const MAX_MATCHES = 200;
|
|
14
14
|
const MAX_FILES = 500;
|
|
@@ -231,6 +231,33 @@ async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "miss
|
|
|
231
231
|
|
|
232
232
|
// ---------- JS ----------
|
|
233
233
|
|
|
234
|
+
/** 同时读几个文件。逐个等 stat、readFile 时,几万个文件的仓库要好几秒(多数时间花在等 IO 上) */
|
|
235
|
+
const READ_AHEAD = 16;
|
|
236
|
+
|
|
237
|
+
/** 读出可以搜的文本;不存在(git 列出的文件可能已在工作区删除)、太大、二进制的返回 undefined */
|
|
238
|
+
async function readSearchable(file: string): Promise<Buffer | undefined> {
|
|
239
|
+
const info = await statOrUndefined(file);
|
|
240
|
+
if (!info?.isFile() || info.size > MAX_FILE_BYTES) return undefined;
|
|
241
|
+
const buf = await readFile(file).catch(() => undefined);
|
|
242
|
+
return buf && !buf.includes(0) ? buf : undefined;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** 按 items 的顺序产出 fn 的结果,最多 n 个同时进行;调用方提前停止时,已经开始的读取随它结束 */
|
|
246
|
+
async function* ordered<T, R>(items: Iterable<T> | AsyncIterable<T>, n: number, fn: (item: T) => Promise<R>): AsyncGenerator<[T, R]> {
|
|
247
|
+
const pending: { item: T; result: Promise<R> }[] = [];
|
|
248
|
+
for await (const item of items) {
|
|
249
|
+
const result = fn(item);
|
|
250
|
+
// 先挂上处理,免得提前停止时没人等的 Promise 报 unhandled rejection
|
|
251
|
+
result.catch(() => {});
|
|
252
|
+
pending.push({ item, result });
|
|
253
|
+
if (pending.length >= n) {
|
|
254
|
+
const head = pending.shift()!;
|
|
255
|
+
yield [head.item, await head.result];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
for (const p of pending) yield [p.item, await p.result];
|
|
259
|
+
}
|
|
260
|
+
|
|
234
261
|
async function jsGrep(s: Search): Promise<Found> {
|
|
235
262
|
// 和 ripgrep 的 --glob 一致:不含 / 的模式("*.ts")匹配任意深度的文件名
|
|
236
263
|
const filter = s.include ? globMatcher(s.include.includes("/") ? s.include : `**/${s.include}`) : undefined;
|
|
@@ -239,14 +266,15 @@ async function jsGrep(s: Search): Promise<Found> {
|
|
|
239
266
|
const single = (await stat(s.root)).isFile();
|
|
240
267
|
const candidates = single ? [s.root] : await listFiles(s.root, s.signal);
|
|
241
268
|
const sorted = Array.isArray(candidates) ? [...candidates].sort() : candidates;
|
|
242
|
-
|
|
269
|
+
const wanted = async function* () {
|
|
270
|
+
for await (const file of sorted) {
|
|
271
|
+
if (filter && !single && !filter(path.relative(s.root, file).split(path.sep).join("/"))) continue;
|
|
272
|
+
yield file;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
for await (const [file, buf] of ordered(wanted(), READ_AHEAD, readSearchable)) {
|
|
243
276
|
s.signal.throwIfAborted();
|
|
244
|
-
if (
|
|
245
|
-
// git 列出的文件可能已在工作区删除
|
|
246
|
-
const info = await statOrUndefined(file);
|
|
247
|
-
if (!info?.isFile() || info.size > MAX_FILE_BYTES) continue;
|
|
248
|
-
const buf = await readFile(file);
|
|
249
|
-
if (buf.includes(0)) continue; // 二进制文件
|
|
277
|
+
if (!buf) continue;
|
|
250
278
|
const all = buf.toString("utf8").split(/\r?\n/);
|
|
251
279
|
if (all.at(-1) === "") all.pop();
|
|
252
280
|
const hits: FileHits = { file, count: 0, lines: [] };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// jade/fs:文件工具 read_file、write_file、edit_file、multi_edit、list_dir、glob、grep。
|
|
2
|
+
|
|
3
|
+
import { definePlugin } from "../../src/api.ts";
|
|
4
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
5
|
+
import type { Task } from "../task/index.ts";
|
|
6
|
+
import { fsTools, SeenFiles } from "./fs.ts";
|
|
7
|
+
import { fsRenderers } from "./renderers.ts";
|
|
8
|
+
|
|
9
|
+
export default definePlugin({
|
|
10
|
+
process(ctx) {
|
|
11
|
+
// 显示方式和会话无关,登记一次留到进程结束;没有 renderers@1(例如 print)时用前端的默认显示
|
|
12
|
+
const renderers = ctx.service<Renderers>("renderers@1");
|
|
13
|
+
if (renderers) for (const r of fsRenderers(ctx.cwd)) renderers.tool(r);
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
session(ctx) {
|
|
17
|
+
// 数模型调用的次数:同一次回复里先读后改的,改的内容是看到读取结果之前写好的。
|
|
18
|
+
// 同时按最终发给模型的消息让被压缩(jade/compact 总结或清理)掉的读取失效:那些文件要重新读才能改
|
|
19
|
+
const responses = { calls: 0 };
|
|
20
|
+
const seen = new SeenFiles();
|
|
21
|
+
ctx.kernel.hook({
|
|
22
|
+
kind: "transform",
|
|
23
|
+
point: "model.before",
|
|
24
|
+
fn: (request) => {
|
|
25
|
+
responses.calls++;
|
|
26
|
+
seen.prune(request.messages);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
for (const tool of fsTools(ctx.cwd, { responses, seen })) ctx.kernel.tool(tool);
|
|
30
|
+
// 子 Agent(jade/task)的只读工具:另一套实例,子 Agent 读过的文件不算主会话读过
|
|
31
|
+
// jade/task 被隔离时它的服务已失效:子 Agent 没有文件工具,文件工具本身照常
|
|
32
|
+
try {
|
|
33
|
+
const task = ctx.service<Task>("task@1");
|
|
34
|
+
if (task) for (const tool of fsTools(ctx.cwd)) if (tool.effect === "read") task.tool(tool);
|
|
35
|
+
} catch {}
|
|
36
|
+
},
|
|
37
|
+
});
|
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-
// jade
|
|
2
|
-
//
|
|
1
|
+
// 文件工具在对话和审批里怎么显示(登记到 renderers@1)。从旧 jade 的 ui/renderers.ts 移植,去掉了检查点。
|
|
2
|
+
//
|
|
3
|
+
// 审批插件的 tool.before 把 path 换成了绝对路径,审批预览、before、result 拿到的都是绝对路径;
|
|
4
|
+
// 显示时用 display 换回工作目录内的相对路径。没有登记 renderer 的部分用前端的默认显示。
|
|
3
5
|
|
|
4
6
|
import { readFileSync } from "node:fs";
|
|
5
7
|
import { readFile } from "node:fs/promises";
|
|
6
8
|
import path from "node:path";
|
|
7
|
-
import type { ToolRenderer } from "../
|
|
8
|
-
import { applyEditsToText, display, type Edit } from "../tools/fs.ts";
|
|
9
|
+
import type { ToolRenderer } from "../renderers/index.ts";
|
|
9
10
|
import { diffLines, diffStats, renderDiff } from "./diff.ts";
|
|
10
|
-
import {
|
|
11
|
-
import { dim, green, red, yellow } from "./
|
|
11
|
+
import { applyEditsToText, display, type Edit } from "./fs.ts";
|
|
12
|
+
import { clip, dim, green, red, yellow } from "./style.ts";
|
|
12
13
|
|
|
13
14
|
type Args = Record<string, unknown>;
|
|
14
|
-
const args = (input: unknown) => (input
|
|
15
|
-
|
|
15
|
+
const args = (input: unknown) => (input !== null && typeof input === "object" ? input : {}) as Args;
|
|
16
|
+
/** 显示用的路径;工作目录本身显示成 "."(display 对它给的是绝对路径,审批插件会把 "." 换成工作目录) */
|
|
17
|
+
function pathOf(cwd: string, i: Args): string {
|
|
18
|
+
if (typeof i.path !== "string") return "";
|
|
19
|
+
const full = path.resolve(cwd, i.path);
|
|
20
|
+
return path.relative(cwd, full) === "" ? "." : display(cwd, full);
|
|
21
|
+
}
|
|
16
22
|
|
|
17
23
|
/** 预览里的多行文字:每行前面加上标记,最多 max 行 */
|
|
18
24
|
function marked(s: string, mark: string, color: (s: string) => string, max = 30): string {
|
|
@@ -22,15 +28,13 @@ function marked(s: string, mark: string, color: (s: string) => string, max = 30)
|
|
|
22
28
|
return shown.join("\n");
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
/**
|
|
31
|
+
/** 折叠内容里的多行文字(缩进由前端加),最多 max 行 */
|
|
26
32
|
function capped(lines: string[], color: (s: string) => string, max: number): string {
|
|
27
33
|
const shown = lines.slice(0, max).map(color);
|
|
28
34
|
if (lines.length > max) shown.push(dim(`… ${lines.length - max} more lines`));
|
|
29
35
|
return shown.join("\n");
|
|
30
36
|
}
|
|
31
37
|
|
|
32
|
-
// ---------- 文件 ----------
|
|
33
|
-
|
|
34
38
|
type FileTool = "edit_file" | "multi_edit" | "write_file";
|
|
35
39
|
const editsOf = (tool: FileTool, i: Args) => (tool === "edit_file" ? [i] : Array.isArray(i.edits) ? i.edits : []) as unknown as Edit[];
|
|
36
40
|
|
|
@@ -70,7 +74,7 @@ const MAX_RESULT_LINES = 2000;
|
|
|
70
74
|
|
|
71
75
|
/**
|
|
72
76
|
* 执行前后的差异。在工具执行之前调用(读的是修改前的文件),同步读文件:
|
|
73
|
-
*
|
|
77
|
+
* 执行前的通知是同步的,等异步读完文件可能已经改了。替换不成立时返回 undefined
|
|
74
78
|
*/
|
|
75
79
|
function fileDiff(cwd: string, tool: FileTool, input: unknown): FileDiff | undefined {
|
|
76
80
|
const i = args(input);
|
|
@@ -95,7 +99,7 @@ function fileDiff(cwd: string, tool: FileTool, input: unknown): FileDiff | undef
|
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
/** 修改文件的工具:预览是 diff;结果行后面是增删的行数,展开看完整的 diff */
|
|
98
|
-
function fileRenderer(cwd: string, tool: FileTool, summary
|
|
102
|
+
function fileRenderer(cwd: string, tool: FileTool, summary: (input: unknown) => string): ToolRenderer {
|
|
99
103
|
return {
|
|
100
104
|
tool,
|
|
101
105
|
summary,
|
|
@@ -112,72 +116,23 @@ function fileRenderer(cwd: string, tool: FileTool, summary?: (input: unknown) =>
|
|
|
112
116
|
}
|
|
113
117
|
|
|
114
118
|
export function fsRenderers(cwd: string): ToolRenderer[] {
|
|
119
|
+
// 默认摘要是 path 参数,审批插件换成绝对路径后太长:统一换回相对路径
|
|
120
|
+
const where = (input: unknown) => pathOf(cwd, args(input));
|
|
115
121
|
const search = (input: unknown) => {
|
|
116
122
|
const i = args(input);
|
|
117
|
-
const
|
|
118
|
-
return `${i.pattern}${
|
|
123
|
+
const at = pathOf(cwd, i);
|
|
124
|
+
return `${i.pattern}${at && at !== "." ? ` in ${at}` : ""}`;
|
|
119
125
|
};
|
|
120
126
|
return [
|
|
121
|
-
|
|
127
|
+
{ tool: "read_file", summary: where },
|
|
128
|
+
{ tool: "list_dir", summary: where },
|
|
129
|
+
fileRenderer(cwd, "edit_file", where),
|
|
122
130
|
fileRenderer(cwd, "multi_edit", (input) => {
|
|
123
131
|
const i = args(input);
|
|
124
132
|
return `${pathOf(cwd, i)}, ${Array.isArray(i.edits) ? i.edits.length : 0} edits`;
|
|
125
133
|
}),
|
|
126
|
-
fileRenderer(cwd, "write_file"),
|
|
134
|
+
fileRenderer(cwd, "write_file", where),
|
|
127
135
|
{ tool: "grep", summary: search },
|
|
128
136
|
{ tool: "glob", summary: search },
|
|
129
137
|
];
|
|
130
138
|
}
|
|
131
|
-
|
|
132
|
-
// ---------- 其他内置工具 ----------
|
|
133
|
-
|
|
134
|
-
export const shellRenderers: ToolRenderer[] = [
|
|
135
|
-
{
|
|
136
|
-
tool: "bash",
|
|
137
|
-
summary: (input) => clip(String(args(input).command ?? "").split("\n")[0]!, 120),
|
|
138
|
-
// 摘要省略了内容时(多行或很长的命令),展开后先显示完整的命令
|
|
139
|
-
details: (input) => {
|
|
140
|
-
const lines = String(args(input).command ?? "").split("\n");
|
|
141
|
-
if (lines.length === 1 && lines[0]!.length <= 120) return undefined;
|
|
142
|
-
return capped(lines.map((l, i) => `${i === 0 ? "$" : " "} ${l}`), dim, 200);
|
|
143
|
-
},
|
|
144
|
-
preview: (input) => marked(String(args(input).command ?? ""), "$", (s) => s),
|
|
145
|
-
},
|
|
146
|
-
{ tool: "bash_output", summary: (input) => String(args(input).id ?? "") },
|
|
147
|
-
{ tool: "kill_shell", summary: (input) => String(args(input).id ?? "") },
|
|
148
|
-
];
|
|
149
|
-
|
|
150
|
-
export const webRenderers: ToolRenderer[] = [
|
|
151
|
-
{ tool: "web_fetch", summary: (input) => clip(String(args(input).url ?? ""), 120) },
|
|
152
|
-
{ tool: "web_search", summary: (input) => clip(String(args(input).query ?? ""), 120) },
|
|
153
|
-
];
|
|
154
|
-
|
|
155
|
-
export const todoRenderers: ToolRenderer[] = [
|
|
156
|
-
{ tool: "todo_write", summary: (input) => `${Array.isArray(args(input).todos) ? (args(input).todos as unknown[]).length : 0} items` },
|
|
157
|
-
];
|
|
158
|
-
|
|
159
|
-
export const askRenderers: ToolRenderer[] = [
|
|
160
|
-
{
|
|
161
|
-
tool: "ask_user",
|
|
162
|
-
summary: (input) => {
|
|
163
|
-
const q = args(input).questions;
|
|
164
|
-
return Array.isArray(q) && q.length === 1 ? clip(String(q[0]?.question ?? ""), 120) : `${Array.isArray(q) ? q.length : 0} questions`;
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
];
|
|
168
|
-
|
|
169
|
-
export const memoryRenderers: ToolRenderer[] = [
|
|
170
|
-
{
|
|
171
|
-
tool: "memory",
|
|
172
|
-
summary: (input) => {
|
|
173
|
-
const i = args(input);
|
|
174
|
-
return `${i.action} ${i.scope}: ${clip(String(i.text ?? ""), 120)}`;
|
|
175
|
-
},
|
|
176
|
-
preview: (input) => {
|
|
177
|
-
const i = args(input);
|
|
178
|
-
return `${i.action === "remove" ? red("- ") : green("+ ")}${String(i.text ?? "")}`;
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
];
|
|
182
|
-
|
|
183
|
-
export const taskRenderers: ToolRenderer[] = [{ tool: "task", summary: (input) => String(args(input).description ?? "") }];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// 显示用的小工具:截断和 ANSI 颜色。从旧 jade 的 ui/render.ts、ui/screen.ts 复制过来:
|
|
2
|
+
// 插件之间不共享代码(只能共享类型),所以 shell 插件也有一份。
|
|
3
|
+
|
|
4
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
5
|
+
|
|
6
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
7
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
8
|
+
/**
|
|
9
|
+
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
|
|
10
|
+
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
11
|
+
*/
|
|
12
|
+
const paint = (open: string, close: string) => (s: string) =>
|
|
13
|
+
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
14
|
+
export const dim = paint("2", "22");
|
|
15
|
+
export const red = paint("31", "39");
|
|
16
|
+
export const green = paint("32", "39");
|
|
17
|
+
export const yellow = paint("33", "39");
|
|
18
|
+
export const bold = paint("1", "22");
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// 跨运行时的进程和 glob:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
|
+
|
|
3
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
4
|
+
import picomatch from "picomatch";
|
|
5
|
+
|
|
6
|
+
type Stdio = "pipe" | "ignore";
|
|
7
|
+
|
|
8
|
+
export interface Proc {
|
|
9
|
+
pid: number;
|
|
10
|
+
/** stdout 为 "pipe" 时才有;Node 的 Readable 可以直接 for await 读出 Uint8Array */
|
|
11
|
+
stdout: AsyncIterable<Uint8Array>;
|
|
12
|
+
stderr: AsyncIterable<Uint8Array>;
|
|
13
|
+
/** 进程退出时 resolve 退出码;被信号杀掉时为 null */
|
|
14
|
+
exited: Promise<number | null>;
|
|
15
|
+
kill(): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SpawnOptions {
|
|
19
|
+
cwd?: string;
|
|
20
|
+
stdout?: Stdio;
|
|
21
|
+
stderr?: Stdio;
|
|
22
|
+
/** POSIX 上自成进程组,便于连同子进程一起杀掉 */
|
|
23
|
+
detached?: boolean;
|
|
24
|
+
/** 子进程的环境变量,默认继承 process.env */
|
|
25
|
+
env?: NodeJS.ProcessEnv;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const EMPTY: AsyncIterable<Uint8Array> = { async *[Symbol.asyncIterator]() {} };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
32
|
+
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
33
|
+
*/
|
|
34
|
+
export function spawn(cmd: string[], opts: SpawnOptions = {}): Promise<Proc> {
|
|
35
|
+
const child = nodeSpawn(cmd[0]!, cmd.slice(1), {
|
|
36
|
+
cwd: opts.cwd,
|
|
37
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
38
|
+
detached: opts.detached ?? false,
|
|
39
|
+
env: opts.env,
|
|
40
|
+
// Windows 上不弹出控制台窗口
|
|
41
|
+
windowsHide: true,
|
|
42
|
+
});
|
|
43
|
+
const exited = new Promise<number | null>((resolve) => child.once("exit", (code) => resolve(code)));
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
child.once("error", reject);
|
|
46
|
+
child.once("spawn", () => {
|
|
47
|
+
child.off("error", reject);
|
|
48
|
+
// 启动之后的错误(例如 kill 一个已经退出的进程)不应该变成未处理的异常
|
|
49
|
+
child.on("error", () => {});
|
|
50
|
+
resolve({
|
|
51
|
+
pid: child.pid!,
|
|
52
|
+
stdout: child.stdout ?? EMPTY,
|
|
53
|
+
stderr: child.stderr ?? EMPTY,
|
|
54
|
+
exited,
|
|
55
|
+
kill: () => void child.kill(),
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
|
|
62
|
+
export async function run(cmd: string[], opts: { cwd?: string } = {}): Promise<{ code: number | null; stdout: string }> {
|
|
63
|
+
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
64
|
+
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
65
|
+
return { code, stdout };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function text(stream: AsyncIterable<Uint8Array>): Promise<string> {
|
|
69
|
+
const decoder = new TextDecoder();
|
|
70
|
+
let out = "";
|
|
71
|
+
for await (const chunk of stream) out += decoder.decode(chunk, { stream: true });
|
|
72
|
+
return out + decoder.decode();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 编译 glob,返回匹配函数。路径用正斜杠、相对于搜索根目录。
|
|
77
|
+
* 和原先的 Bun.Glob#match 一致:* 也匹配点开头的文件,反斜杠是转义符而不是分隔符
|
|
78
|
+
*/
|
|
79
|
+
export function globMatcher(pattern: string): (rel: string) => boolean {
|
|
80
|
+
return picomatch(pattern, { dot: true, windows: false });
|
|
81
|
+
}
|