@uncarved/jade 0.1.2 → 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 -4
- 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 -28
- package/dist/index.js +11 -28
- 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 +156 -1223
- 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 +55 -0
- package/dist/update.js +258 -0
- package/dist/update.js.map +1 -0
- package/package.json +12 -9
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -84
- package/plugins/approval/review.ts +192 -0
- 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 +68 -39
- 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 +112 -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 +3 -4
- package/{src/tools → plugins/fs}/fs.ts +64 -46
- package/{src/tools → plugins/fs}/grep.ts +4 -3
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/plugins/fs/renderers.ts +138 -0
- 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 +23 -27
- 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 +36 -21
- package/{src → plugins/shell}/sys.ts +19 -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 +53 -1
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/{src/ui → plugins/tui}/markdown.ts +258 -219
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/plugins/tui/render.ts +67 -0
- package/plugins/tui/screen.ts +268 -0
- package/{src/ui → plugins/tui}/transcript.ts +179 -111
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +684 -418
- 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/plugins/web/web.ts +713 -0
- 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 -65
- package/plugins-dist/approval/review.js +176 -0
- 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 +65 -34
- 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 +95 -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 +3 -5
- package/{dist/tools → plugins-dist/fs}/fs.js +62 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +4 -4
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/plugins-dist/fs/renderers.js +132 -0
- 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 +22 -19
- 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 +36 -23
- package/{dist → plugins-dist/shell}/sys.js +20 -44
- 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 +53 -2
- 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 +57 -20
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/plugins-dist/tui/render.js +61 -0
- package/plugins-dist/tui/screen.js +201 -0
- package/{dist/ui → plugins-dist/tui}/transcript.js +179 -111
- package/{dist/ui → plugins-dist/tui}/tui.js +484 -101
- 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/plugins-dist/web/web.js +660 -0
- 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 -28
- package/src/install.ts +355 -0
- package/src/main.ts +152 -1263
- 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 -0
- package/LICENSE +0 -21
- package/README.md +0 -198
- package/dist/commands.d.ts +0 -30
- package/dist/commands.js +0 -106
- 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 -104
- package/dist/distro.js +0 -312
- 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 -251
- 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 -22
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -70
- 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 -42
- package/dist/plugins/core.js +0 -190
- package/dist/plugins/core.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 -182
- package/dist/plugins/loader.js +0 -253
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -96
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -38
- package/dist/plugins/prompts.js +0 -196
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -9
- package/dist/plugins/repeats.js +0 -90
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -16
- package/dist/plugins/review.js +0 -92
- 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/provider.d.ts +0 -111
- package/dist/provider.js.map +0 -1
- package/dist/sessions.d.ts +0 -22
- package/dist/sessions.js +0 -53
- 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 -48
- package/dist/sys.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 -35
- 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 -35
- package/dist/tools/task.js +0 -89
- 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 -23
- package/dist/tools/web.js +0 -426
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -378
- 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 -33
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -17
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -15
- package/dist/ui/render.js +0 -94
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -98
- package/dist/ui/screen.js +0 -108
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -33
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -45
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -120
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -400
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -239
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -31
- package/src/plugins/core.ts +0 -217
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -386
- package/src/plugins/prompts.ts +0 -234
- package/src/plugins/repeats.ts +0 -96
- package/src/plugins/review.ts +0 -109
- package/src/sessions.ts +0 -61
- package/src/tools/task.ts +0 -109
- package/src/tools/web.ts +0 -451
- package/src/ui/configure.ts +0 -408
- package/src/ui/render.ts +0 -83
- package/src/ui/screen.ts +0 -169
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
|
@@ -1,18 +1,18 @@
|
|
|
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
|
// 否则模型是按过时的内容在改,会覆盖掉别人的修改。和修改在同一次回复里发出的读取不算:
|
|
8
7
|
// 那次修改的内容是模型看到读取结果之前就写好的。
|
|
9
8
|
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
10
9
|
import path from "node:path";
|
|
11
|
-
import { sha256 } from "./checkpoint.js";
|
|
12
10
|
import { grepTool } from "./grep.js";
|
|
13
|
-
import { globMatcher, run } from "
|
|
11
|
+
import { globMatcher, run } from "./sys.js";
|
|
14
12
|
const MAX_LINES = 2000;
|
|
15
13
|
const MAX_LINE_CHARS = 2000;
|
|
14
|
+
/** 一次 read_file 返回的文本上限(约 25k token) */
|
|
15
|
+
const MAX_READ_CHARS = 100_000;
|
|
16
16
|
const MAX_GLOB = 500;
|
|
17
17
|
const MAX_IMAGE_BYTES = 5_000_000;
|
|
18
18
|
const MAX_PDF_BYTES = 10_000_000;
|
|
@@ -40,14 +40,13 @@ export function fsTools(cwd, opts = {}) {
|
|
|
40
40
|
const at = (p) => resolveIn(cwd, p);
|
|
41
41
|
const seen = new Map();
|
|
42
42
|
/** 文件没变时保留更早的 call:之前的回复里读过,这次回复里又读一遍,不影响修改 */
|
|
43
|
-
const remember =
|
|
43
|
+
const remember = (full, info, call) => {
|
|
44
44
|
const s = seen.get(full);
|
|
45
45
|
const same = s !== undefined && s.mtimeMs === info.mtimeMs && s.size === info.size;
|
|
46
46
|
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call });
|
|
47
|
-
await opts.checkpoints?.markSeen(ctx.sessionId, full, sha256(text));
|
|
48
47
|
};
|
|
49
48
|
/** 不能修改已有文件的原因;可以修改时返回 undefined。text 是文件现在的内容 */
|
|
50
|
-
const staleness =
|
|
49
|
+
const staleness = (full, info) => {
|
|
51
50
|
const shown = display(cwd, full);
|
|
52
51
|
const modified = `${shown} has been modified since it was last read (by the user or a command). Read it again before changing it.`;
|
|
53
52
|
const s = seen.get(full);
|
|
@@ -59,22 +58,13 @@ export function fsTools(cwd, opts = {}) {
|
|
|
59
58
|
}
|
|
60
59
|
return undefined;
|
|
61
60
|
}
|
|
62
|
-
|
|
63
|
-
const hash = await opts.checkpoints?.seenHash(ctx.sessionId, full);
|
|
64
|
-
if (hash === undefined)
|
|
65
|
-
return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
66
|
-
if (hash !== sha256(text))
|
|
67
|
-
return modified;
|
|
68
|
-
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: -1 });
|
|
69
|
-
return undefined;
|
|
70
|
-
};
|
|
71
|
-
const checkpoint = async (ctx, tool, full, before, after) => {
|
|
72
|
-
await opts.checkpoints?.save(ctx.sessionId, { toolUseId: ctx.toolUseId, tool, path: full, before, afterHash: sha256(after) });
|
|
61
|
+
return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
73
62
|
};
|
|
74
63
|
const readFileTool = {
|
|
75
64
|
name: "read_file",
|
|
76
65
|
description: "Read a UTF-8 text file, an image (png, jpg, gif, webp) or a PDF. Text is returned as lines prefixed with their 1-based line number and a tab. " +
|
|
77
|
-
`At most ${MAX_LINES} lines are returned per call; use offset (1-based start line) and limit to page through larger files
|
|
66
|
+
`At most ${MAX_LINES} lines (about ${MAX_READ_CHARS / 1000}k characters) are returned per call; use offset (1-based start line) and limit to page through larger files. ` +
|
|
67
|
+
`Lines longer than ${MAX_LINE_CHARS} characters are cut and marked "[line truncated]"; do not copy a cut line into edit_file.`,
|
|
78
68
|
inputSchema: {
|
|
79
69
|
type: "object",
|
|
80
70
|
properties: { path: str, offset: int, limit: int },
|
|
@@ -84,7 +74,7 @@ export function fsTools(cwd, opts = {}) {
|
|
|
84
74
|
effect: "read",
|
|
85
75
|
concurrency: "safe",
|
|
86
76
|
idempotent: true,
|
|
87
|
-
async execute(input
|
|
77
|
+
async execute(input) {
|
|
88
78
|
const { path: p, offset = 1, limit = MAX_LINES } = input;
|
|
89
79
|
if (limit < 1)
|
|
90
80
|
return { content: "limit must be at least 1", isError: true };
|
|
@@ -120,7 +110,7 @@ export function fsTools(cwd, opts = {}) {
|
|
|
120
110
|
return { content: `${display(cwd, full)} is a binary file (${buf.length} bytes) and cannot be shown as text`, isError: true };
|
|
121
111
|
}
|
|
122
112
|
const text = buf.toString("utf8");
|
|
123
|
-
|
|
113
|
+
remember(full, info, opts.responses?.calls ?? -1);
|
|
124
114
|
if (text.length === 0)
|
|
125
115
|
return { content: "(empty file)" };
|
|
126
116
|
const lines = text.split(/\r?\n/);
|
|
@@ -130,10 +120,21 @@ export function fsTools(cwd, opts = {}) {
|
|
|
130
120
|
const slice = lines.slice(start - 1, start - 1 + Math.min(limit, MAX_LINES));
|
|
131
121
|
if (slice.length === 0)
|
|
132
122
|
return { content: `(file has ${lines.length} lines; offset ${start} is past the end)`, isError: true };
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
123
|
+
// 按整行截断到 MAX_READ_CHARS:压缩过的代码、lock 文件一次就能占掉大半个上下文
|
|
124
|
+
const out = [];
|
|
125
|
+
let size = 0;
|
|
126
|
+
for (const [i, l] of slice.entries()) {
|
|
127
|
+
const row = `${start + i}\t${l.length > MAX_LINE_CHARS ? `${l.slice(0, MAX_LINE_CHARS)}… [line truncated: ${l.length} characters]` : l}`;
|
|
128
|
+
if (out.length > 0 && size + row.length + 1 > MAX_READ_CHARS)
|
|
129
|
+
break;
|
|
130
|
+
out.push(row);
|
|
131
|
+
size += row.length + 1;
|
|
132
|
+
}
|
|
133
|
+
const body = out.join("\n");
|
|
134
|
+
const end = start + out.length - 1;
|
|
135
|
+
if (out.length < slice.length) {
|
|
136
|
+
return { content: `${body}\n(output capped at about ${MAX_READ_CHARS} characters: showing lines ${start}-${end} of ${lines.length}; continue with offset ${end + 1})` };
|
|
137
|
+
}
|
|
137
138
|
return { content: end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body };
|
|
138
139
|
},
|
|
139
140
|
};
|
|
@@ -151,29 +152,29 @@ export function fsTools(cwd, opts = {}) {
|
|
|
151
152
|
concurrency: "exclusive",
|
|
152
153
|
// 写入完整内容,重复执行结果相同
|
|
153
154
|
idempotent: true,
|
|
154
|
-
async execute(input
|
|
155
|
-
const { path: p, content } = input;
|
|
155
|
+
async execute(input) {
|
|
156
|
+
const { path: p, content: given } = input;
|
|
156
157
|
const full = at(p);
|
|
157
158
|
const info = await statOrUndefined(full);
|
|
158
159
|
if (info?.isDirectory())
|
|
159
160
|
return { content: `${display(cwd, full)} is a directory`, isError: true };
|
|
160
161
|
// 同样内容重复写入(崩溃后重跑)不算覆盖别人的修改
|
|
161
162
|
const before = info ? await readFile(full, "utf8") : null;
|
|
162
|
-
|
|
163
|
+
// read_file 不显示换行风格和 BOM:覆盖已有文件时沿用原来的,否则整个文件每一行都算改了
|
|
164
|
+
const content = before === null ? given : keepStyle(before, given);
|
|
165
|
+
const problem = info && before !== null && before !== content ? staleness(full, info) : undefined;
|
|
163
166
|
if (problem)
|
|
164
167
|
return { content: problem, isError: true };
|
|
165
168
|
await mkdir(path.dirname(full), { recursive: true });
|
|
166
169
|
await writeFile(full, content);
|
|
167
|
-
|
|
168
|
-
if (before !== content)
|
|
169
|
-
await checkpoint(ctx, "write_file", full, before, content);
|
|
170
|
+
remember(full, await stat(full), -1);
|
|
170
171
|
// 和 read_file 一样数行:末尾的换行不算多出一行
|
|
171
172
|
const lines = content === "" ? 0 : content.split(/\r?\n/).length - (/\r?\n$/.test(content) ? 1 : 0);
|
|
172
173
|
return { content: `Wrote ${lines} lines to ${display(cwd, full)}` };
|
|
173
174
|
},
|
|
174
175
|
};
|
|
175
176
|
/** edit_file 和 multi_edit 共用:检查文件能否修改,按顺序应用全部替换,全部成功才写入 */
|
|
176
|
-
const applyEdits = async (
|
|
177
|
+
const applyEdits = async (p, edits) => {
|
|
177
178
|
const full = at(p);
|
|
178
179
|
const shown = display(cwd, full);
|
|
179
180
|
const info = await statOrUndefined(full);
|
|
@@ -181,7 +182,7 @@ export function fsTools(cwd, opts = {}) {
|
|
|
181
182
|
return { content: seen.has(full) ? `${shown} no longer exists` : `${shown} does not exist; use write_file to create it`, isError: true };
|
|
182
183
|
}
|
|
183
184
|
const text = await readFile(full, "utf8");
|
|
184
|
-
const problem =
|
|
185
|
+
const problem = staleness(full, info);
|
|
185
186
|
if (problem)
|
|
186
187
|
return { content: problem, isError: true };
|
|
187
188
|
let next = text;
|
|
@@ -194,8 +195,7 @@ export function fsTools(cwd, opts = {}) {
|
|
|
194
195
|
total += r.count;
|
|
195
196
|
}
|
|
196
197
|
await writeFile(full, next);
|
|
197
|
-
|
|
198
|
-
await checkpoint(ctx, tool, full, text, next);
|
|
198
|
+
remember(full, await stat(full), -1);
|
|
199
199
|
return { content: `Edited ${shown} (${total} replacement${total === 1 ? "" : "s"})` };
|
|
200
200
|
};
|
|
201
201
|
const editFileTool = {
|
|
@@ -213,9 +213,9 @@ export function fsTools(cwd, opts = {}) {
|
|
|
213
213
|
concurrency: "exclusive",
|
|
214
214
|
// 第二次执行时 old_string 可能已经不存在,或者被替换两次
|
|
215
215
|
idempotent: false,
|
|
216
|
-
async execute(input
|
|
216
|
+
async execute(input) {
|
|
217
217
|
const { path: p, ...edit } = input;
|
|
218
|
-
return applyEdits(
|
|
218
|
+
return applyEdits(p, [edit]);
|
|
219
219
|
},
|
|
220
220
|
};
|
|
221
221
|
const multiEditTool = {
|
|
@@ -242,11 +242,11 @@ export function fsTools(cwd, opts = {}) {
|
|
|
242
242
|
effect: "write",
|
|
243
243
|
concurrency: "exclusive",
|
|
244
244
|
idempotent: false,
|
|
245
|
-
async execute(input
|
|
245
|
+
async execute(input) {
|
|
246
246
|
const { path: p, edits } = input;
|
|
247
247
|
if (edits.length === 0)
|
|
248
248
|
return { content: "edits must not be empty", isError: true };
|
|
249
|
-
return applyEdits(
|
|
249
|
+
return applyEdits(p, edits);
|
|
250
250
|
},
|
|
251
251
|
};
|
|
252
252
|
const listDirTool = {
|
|
@@ -314,23 +314,40 @@ export function applyEditsToText(text, edits, shown) {
|
|
|
314
314
|
}
|
|
315
315
|
return { text: next };
|
|
316
316
|
}
|
|
317
|
+
/** text 里多数换行是 CRLF 时返回 "\r\n",多数是 LF 时返回 "\n",一样多或没有换行时返回 fallback */
|
|
318
|
+
function eolOf(text, fallback) {
|
|
319
|
+
const crlf = text.split("\r\n").length - 1;
|
|
320
|
+
const lf = text.split("\n").length - 1 - crlf;
|
|
321
|
+
return crlf > lf ? "\r\n" : lf > crlf ? "\n" : fallback;
|
|
322
|
+
}
|
|
323
|
+
/** 原文件主要用 CRLF、新内容只有 LF 时换成 CRLF;原文件有 BOM、新内容没有时补上 */
|
|
324
|
+
export function keepStyle(before, content) {
|
|
325
|
+
let out = content;
|
|
326
|
+
if (eolOf(before, "\n") === "\r\n" && !out.includes("\r\n"))
|
|
327
|
+
out = out.replaceAll("\n", "\r\n");
|
|
328
|
+
if (before.startsWith("") && !out.startsWith(""))
|
|
329
|
+
out = `${out}`;
|
|
330
|
+
return out;
|
|
331
|
+
}
|
|
332
|
+
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
317
333
|
/** 在 text 上做一次替换;失败时返回原因 */
|
|
318
334
|
function replaceIn(text, { old_string, new_string, replace_all }, shown) {
|
|
319
335
|
if (old_string === new_string)
|
|
320
336
|
return "old_string and new_string are identical";
|
|
321
337
|
if (old_string === "")
|
|
322
338
|
return "old_string must not be empty; use write_file to create a file";
|
|
323
|
-
//
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
const to = crlf ? new_string.replaceAll("\n", "\r\n") : new_string;
|
|
327
|
-
const count = text.split(from).length - 1;
|
|
339
|
+
// read_file 不显示换行风格:old_string 里的换行匹配 LF 和 CRLF,CRLF 文件、换行混用的文件都能对上
|
|
340
|
+
const pattern = new RegExp(old_string.split(/\r?\n/).map(escapeRegExp).join("\\r?\\n"), "g");
|
|
341
|
+
const count = text.match(pattern)?.length ?? 0;
|
|
328
342
|
if (count === 0)
|
|
329
343
|
return `old_string not found in ${shown}${nearMiss(text, old_string)}`;
|
|
330
344
|
if (count > 1 && !replace_all)
|
|
331
345
|
return `old_string occurs ${count} times in ${shown}; add context to make it unique or set replace_all`;
|
|
346
|
+
// new_string 的换行沿用被替换那段的;那段只有一行时沿用整个文件的,单行改成多行也不会混进 LF
|
|
347
|
+
const fileEol = eolOf(text, "\n");
|
|
348
|
+
const to = (matched) => (new_string.includes("\r\n") ? new_string : new_string.replaceAll("\n", eolOf(matched, fileEol)));
|
|
332
349
|
// 用函数作替换值,new_string 里的 $& $1 等不会被当成替换模式
|
|
333
|
-
return { text:
|
|
350
|
+
return { text: text.replace(replace_all ? pattern : new RegExp(pattern.source), to), count: replace_all ? count : 1 };
|
|
334
351
|
}
|
|
335
352
|
/**
|
|
336
353
|
* old_string 找不到时帮模型定位:忽略空白后能匹配到哪几行,或者第一行出现在哪里。
|
|
@@ -414,4 +431,3 @@ async function* walk(dir, signal) {
|
|
|
414
431
|
}
|
|
415
432
|
}
|
|
416
433
|
}
|
|
417
|
-
//# sourceMappingURL=fs.js.map
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { readFile, stat } from "node:fs/promises";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { display, listFiles, statOrUndefined } from "./fs.js";
|
|
9
|
-
import { globMatcher, spawn, text } from "
|
|
9
|
+
import { globMatcher, spawn, text } from "./sys.js";
|
|
10
10
|
const MAX_MATCHES = 200;
|
|
11
11
|
const MAX_FILES = 500;
|
|
12
12
|
const MAX_CONTEXT = 10;
|
|
@@ -18,7 +18,7 @@ export function grepTool(cwd, opts = {}) {
|
|
|
18
18
|
return {
|
|
19
19
|
name: "grep",
|
|
20
20
|
description: "Search file contents with a regular expression (JavaScript syntax). Searches path (a file or directory, default: the working directory), " +
|
|
21
|
-
`optionally only files matching the glob include (e.g. "
|
|
21
|
+
`optionally only files matching the glob include (e.g. "*.ts" matches at any depth; "src/**/*.ts" limits it to src). Skips files ignored by git, binary files and .git/node_modules. ` +
|
|
22
22
|
`output_mode: "content" (default) returns "file:line:text" for matches and "file-line-text" for context lines, at most ${MAX_MATCHES} matches; ` +
|
|
23
23
|
`"files" returns only the paths of matching files; "count" returns "file:count". ` +
|
|
24
24
|
`context (0-${MAX_CONTEXT}) adds that many lines before and after each match in content mode.`,
|
|
@@ -197,7 +197,8 @@ async function ripgrep(rg, s) {
|
|
|
197
197
|
}
|
|
198
198
|
// ---------- JS ----------
|
|
199
199
|
async function jsGrep(s) {
|
|
200
|
-
|
|
200
|
+
// 和 ripgrep 的 --glob 一致:不含 / 的模式("*.ts")匹配任意深度的文件名
|
|
201
|
+
const filter = s.include ? globMatcher(s.include.includes("/") ? s.include : `**/${s.include}`) : undefined;
|
|
201
202
|
const files = [];
|
|
202
203
|
let matches = 0;
|
|
203
204
|
const single = (await stat(s.root)).isFile();
|
|
@@ -240,4 +241,3 @@ async function jsGrep(s) {
|
|
|
240
241
|
}
|
|
241
242
|
return { files, truncated: false };
|
|
242
243
|
}
|
|
243
|
-
//# sourceMappingURL=grep.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// jade/fs:文件工具 read_file、write_file、edit_file、multi_edit、list_dir、glob、grep。
|
|
2
|
+
import { definePlugin } from "../../dist/api.js";
|
|
3
|
+
import { fsTools } from "./fs.js";
|
|
4
|
+
import { fsRenderers } from "./renderers.js";
|
|
5
|
+
export default definePlugin({
|
|
6
|
+
process(ctx) {
|
|
7
|
+
// 显示方式和会话无关,登记一次留到进程结束;没有 renderers@1(例如 print)时用前端的默认显示
|
|
8
|
+
const renderers = ctx.service("renderers@1");
|
|
9
|
+
if (renderers)
|
|
10
|
+
for (const r of fsRenderers(ctx.cwd))
|
|
11
|
+
renderers.tool(r);
|
|
12
|
+
},
|
|
13
|
+
session(ctx) {
|
|
14
|
+
// 数模型调用的次数:同一次回复里先读后改的,改的内容是看到读取结果之前写好的
|
|
15
|
+
const responses = { calls: 0 };
|
|
16
|
+
ctx.kernel.hook({
|
|
17
|
+
kind: "transform",
|
|
18
|
+
point: "model.before",
|
|
19
|
+
fn: () => {
|
|
20
|
+
responses.calls++;
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
for (const tool of fsTools(ctx.cwd, { responses }))
|
|
24
|
+
ctx.kernel.tool(tool);
|
|
25
|
+
// 子 Agent(jade/task)的只读工具:另一套实例,子 Agent 读过的文件不算主会话读过
|
|
26
|
+
// jade/task 被隔离时它的服务已失效:子 Agent 没有文件工具,文件工具本身照常
|
|
27
|
+
try {
|
|
28
|
+
const task = ctx.service("task@1");
|
|
29
|
+
if (task)
|
|
30
|
+
for (const tool of fsTools(ctx.cwd))
|
|
31
|
+
if (tool.effect === "read")
|
|
32
|
+
task.tool(tool);
|
|
33
|
+
}
|
|
34
|
+
catch { }
|
|
35
|
+
},
|
|
36
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// 文件工具在对话和审批里怎么显示(登记到 renderers@1)。从旧 jade 的 ui/renderers.ts 移植,去掉了检查点。
|
|
2
|
+
//
|
|
3
|
+
// 审批插件的 tool.before 把 path 换成了绝对路径,审批预览、before、result 拿到的都是绝对路径;
|
|
4
|
+
// 显示时用 display 换回工作目录内的相对路径。没有登记 renderer 的部分用前端的默认显示。
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { diffLines, diffStats, renderDiff } from "./diff.js";
|
|
9
|
+
import { applyEditsToText, display } from "./fs.js";
|
|
10
|
+
import { clip, dim, green, red, yellow } from "./style.js";
|
|
11
|
+
const args = (input) => (input !== null && typeof input === "object" ? input : {});
|
|
12
|
+
/** 显示用的路径;工作目录本身显示成 "."(display 对它给的是绝对路径,审批插件会把 "." 换成工作目录) */
|
|
13
|
+
function pathOf(cwd, i) {
|
|
14
|
+
if (typeof i.path !== "string")
|
|
15
|
+
return "";
|
|
16
|
+
const full = path.resolve(cwd, i.path);
|
|
17
|
+
return path.relative(cwd, full) === "" ? "." : display(cwd, full);
|
|
18
|
+
}
|
|
19
|
+
/** 预览里的多行文字:每行前面加上标记,最多 max 行 */
|
|
20
|
+
function marked(s, mark, color, max = 30) {
|
|
21
|
+
const all = s.split("\n");
|
|
22
|
+
const shown = all.slice(0, max).map((l) => color(`${mark} ${l}`));
|
|
23
|
+
if (all.length > max)
|
|
24
|
+
shown.push(dim(` … ${all.length - max} more lines`));
|
|
25
|
+
return shown.join("\n");
|
|
26
|
+
}
|
|
27
|
+
/** 折叠内容里的多行文字(缩进由前端加),最多 max 行 */
|
|
28
|
+
function capped(lines, color, max) {
|
|
29
|
+
const shown = lines.slice(0, max).map(color);
|
|
30
|
+
if (lines.length > max)
|
|
31
|
+
shown.push(dim(`… ${lines.length - max} more lines`));
|
|
32
|
+
return shown.join("\n");
|
|
33
|
+
}
|
|
34
|
+
const editsOf = (tool, i) => (tool === "edit_file" ? [i] : Array.isArray(i.edits) ? i.edits : []);
|
|
35
|
+
/**
|
|
36
|
+
* 审批时的预览:带行号和上下文的 diff(读出文件、按工具的规则算出修改后的内容)。
|
|
37
|
+
* 文件读不了、替换不成立时退回到只显示参数里的新旧文本
|
|
38
|
+
*/
|
|
39
|
+
async function filePreview(cwd, tool, input) {
|
|
40
|
+
const i = args(input);
|
|
41
|
+
const file = typeof i.path === "string" ? path.resolve(cwd, i.path) : undefined;
|
|
42
|
+
const current = file ? await readFile(file, "utf8").catch(() => undefined) : undefined;
|
|
43
|
+
if (tool === "write_file") {
|
|
44
|
+
const content = String(i.content ?? "");
|
|
45
|
+
if (current !== undefined)
|
|
46
|
+
return `${dim(" overwrites the existing file:")}\n${renderDiff(current, content)}`;
|
|
47
|
+
return marked(content, "+", green, 20);
|
|
48
|
+
}
|
|
49
|
+
if (tool === "multi_edit" && !Array.isArray(i.edits))
|
|
50
|
+
return dim(clip(JSON.stringify(input, null, 2), 2000));
|
|
51
|
+
const edits = editsOf(tool, i);
|
|
52
|
+
const raw = (e) => `${marked(String(e.old_string ?? ""), "-", red)}\n${marked(String(e.new_string ?? ""), "+", green)}`;
|
|
53
|
+
const fallback = () => edits.map((e) => raw(e)).join(dim("\n ⋯\n"));
|
|
54
|
+
if (current === undefined)
|
|
55
|
+
return fallback();
|
|
56
|
+
const r = applyEditsToText(current, edits, display(cwd, file));
|
|
57
|
+
if (typeof r === "string")
|
|
58
|
+
return `${fallback()}\n${yellow(` (this edit will fail: ${clip(r, 200)})`)}`;
|
|
59
|
+
return renderDiff(current, r.text);
|
|
60
|
+
}
|
|
61
|
+
/** 对话里折叠的 diff 最多显示这么多行 */
|
|
62
|
+
const MAX_DIFF_LINES = 400;
|
|
63
|
+
/** 结果文字(例如编辑后检查的输出)折叠起来最多这么多行 */
|
|
64
|
+
const MAX_RESULT_LINES = 2000;
|
|
65
|
+
/**
|
|
66
|
+
* 执行前后的差异。在工具执行之前调用(读的是修改前的文件),同步读文件:
|
|
67
|
+
* 执行前的通知是同步的,等异步读完文件可能已经改了。替换不成立时返回 undefined
|
|
68
|
+
*/
|
|
69
|
+
function fileDiff(cwd, tool, input) {
|
|
70
|
+
const i = args(input);
|
|
71
|
+
if (typeof i.path !== "string")
|
|
72
|
+
return undefined;
|
|
73
|
+
const file = path.resolve(cwd, i.path);
|
|
74
|
+
let before;
|
|
75
|
+
try {
|
|
76
|
+
before = readFileSync(file, "utf8");
|
|
77
|
+
}
|
|
78
|
+
catch { }
|
|
79
|
+
let after;
|
|
80
|
+
if (tool === "write_file") {
|
|
81
|
+
after = String(i.content ?? "");
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
if (before === undefined)
|
|
85
|
+
return undefined;
|
|
86
|
+
const r = applyEditsToText(before, editsOf(tool, i), display(cwd, file));
|
|
87
|
+
if (typeof r === "string")
|
|
88
|
+
return undefined;
|
|
89
|
+
after = r.text;
|
|
90
|
+
}
|
|
91
|
+
const lines = diffLines(before ?? "", after);
|
|
92
|
+
const { added, removed } = diffStats(lines);
|
|
93
|
+
return { added, removed, text: renderDiff(before ?? "", after, MAX_DIFF_LINES, lines) };
|
|
94
|
+
}
|
|
95
|
+
/** 修改文件的工具:预览是 diff;结果行后面是增删的行数,展开看完整的 diff */
|
|
96
|
+
function fileRenderer(cwd, tool, summary) {
|
|
97
|
+
return {
|
|
98
|
+
tool,
|
|
99
|
+
summary,
|
|
100
|
+
preview: (input) => filePreview(cwd, tool, input),
|
|
101
|
+
before: (input) => fileDiff(cwd, tool, input),
|
|
102
|
+
result: ({ text, isError, before }) => {
|
|
103
|
+
const diff = before;
|
|
104
|
+
if (!diff || isError)
|
|
105
|
+
return undefined;
|
|
106
|
+
const [first = "", ...rest] = text.split("\n");
|
|
107
|
+
const extra = rest.length ? [capped(rest, (s) => s, MAX_RESULT_LINES)] : [];
|
|
108
|
+
return { line: dim(`${clip(first, 150)} `) + `${green(`+${diff.added}`)} ${red(`-${diff.removed}`)}`, details: [...extra, diff.text].join("\n") };
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export function fsRenderers(cwd) {
|
|
113
|
+
// 默认摘要是 path 参数,审批插件换成绝对路径后太长:统一换回相对路径
|
|
114
|
+
const where = (input) => pathOf(cwd, args(input));
|
|
115
|
+
const search = (input) => {
|
|
116
|
+
const i = args(input);
|
|
117
|
+
const at = pathOf(cwd, i);
|
|
118
|
+
return `${i.pattern}${at && at !== "." ? ` in ${at}` : ""}`;
|
|
119
|
+
};
|
|
120
|
+
return [
|
|
121
|
+
{ tool: "read_file", summary: where },
|
|
122
|
+
{ tool: "list_dir", summary: where },
|
|
123
|
+
fileRenderer(cwd, "edit_file", where),
|
|
124
|
+
fileRenderer(cwd, "multi_edit", (input) => {
|
|
125
|
+
const i = args(input);
|
|
126
|
+
return `${pathOf(cwd, i)}, ${Array.isArray(i.edits) ? i.edits.length : 0} edits`;
|
|
127
|
+
}),
|
|
128
|
+
fileRenderer(cwd, "write_file", where),
|
|
129
|
+
{ tool: "grep", summary: search },
|
|
130
|
+
{ tool: "glob", summary: search },
|
|
131
|
+
];
|
|
132
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// 显示用的小工具:截断和 ANSI 颜色。从旧 jade 的 ui/render.ts、ui/screen.ts 复制过来:
|
|
2
|
+
// 插件之间不共享代码(只能共享类型),所以 shell 插件也有一份。
|
|
3
|
+
export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
4
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
5
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
6
|
+
/**
|
|
7
|
+
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
|
|
8
|
+
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
9
|
+
*/
|
|
10
|
+
const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
11
|
+
export const dim = paint("2", "22");
|
|
12
|
+
export const red = paint("31", "39");
|
|
13
|
+
export const green = paint("32", "39");
|
|
14
|
+
export const yellow = paint("33", "39");
|
|
15
|
+
export const bold = paint("1", "22");
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// 跨运行时的进程和 glob:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
3
|
+
import picomatch from "picomatch";
|
|
4
|
+
const EMPTY = { async *[Symbol.asyncIterator]() { } };
|
|
5
|
+
/**
|
|
6
|
+
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
7
|
+
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
8
|
+
*/
|
|
9
|
+
export function spawn(cmd, opts = {}) {
|
|
10
|
+
const child = nodeSpawn(cmd[0], cmd.slice(1), {
|
|
11
|
+
cwd: opts.cwd,
|
|
12
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
13
|
+
detached: opts.detached ?? false,
|
|
14
|
+
env: opts.env,
|
|
15
|
+
// Windows 上不弹出控制台窗口
|
|
16
|
+
windowsHide: true,
|
|
17
|
+
});
|
|
18
|
+
const exited = new Promise((resolve) => child.once("exit", (code) => resolve(code)));
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
child.once("error", reject);
|
|
21
|
+
child.once("spawn", () => {
|
|
22
|
+
child.off("error", reject);
|
|
23
|
+
// 启动之后的错误(例如 kill 一个已经退出的进程)不应该变成未处理的异常
|
|
24
|
+
child.on("error", () => { });
|
|
25
|
+
resolve({
|
|
26
|
+
pid: child.pid,
|
|
27
|
+
stdout: child.stdout ?? EMPTY,
|
|
28
|
+
stderr: child.stderr ?? EMPTY,
|
|
29
|
+
exited,
|
|
30
|
+
kill: () => void child.kill(),
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
|
|
36
|
+
export async function run(cmd, opts = {}) {
|
|
37
|
+
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
38
|
+
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
39
|
+
return { code, stdout };
|
|
40
|
+
}
|
|
41
|
+
export async function text(stream) {
|
|
42
|
+
const decoder = new TextDecoder();
|
|
43
|
+
let out = "";
|
|
44
|
+
for await (const chunk of stream)
|
|
45
|
+
out += decoder.decode(chunk, { stream: true });
|
|
46
|
+
return out + decoder.decode();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 编译 glob,返回匹配函数。路径用正斜杠、相对于搜索根目录。
|
|
50
|
+
* 和原先的 Bun.Glob#match 一致:* 也匹配点开头的文件,反斜杠是转义符而不是分隔符
|
|
51
|
+
*/
|
|
52
|
+
export function globMatcher(pattern) {
|
|
53
|
+
return picomatch(pattern, { dot: true, windows: false });
|
|
54
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// jade/keys:快捷键的注册表。前端在输入框有焦点时按下自己不用的键,查这里找到插件的处理函数。
|
|
2
|
+
import { definePlugin } from "../../dist/api.js";
|
|
3
|
+
/** "ctrl+<字母>"、"alt+<字母或数字>" 或 "shift+tab" */
|
|
4
|
+
export const KEY_SPEC = /^(ctrl\+[a-z]|alt\+[a-z0-9]|shift\+tab)$/;
|
|
5
|
+
/**
|
|
6
|
+
* 前端自己用的键,插件不能用:编辑(ctrl+a/e/d/w/u/k,按词移动的 alt+f/b)、搜索历史(ctrl+r)、重画(ctrl+l)、展开(ctrl+o)、
|
|
7
|
+
* 中断和退出(ctrl+c),以及终端里等同于退格、Tab、回车的 ctrl+h/i/j/m
|
|
8
|
+
*/
|
|
9
|
+
export const RESERVED_KEYS = new Set([..."acdehijklmoruw"].map((c) => `ctrl+${c}`).concat("alt+f", "alt+b"));
|
|
10
|
+
const registry = new Map();
|
|
11
|
+
export default definePlugin({
|
|
12
|
+
process(ctx) {
|
|
13
|
+
ctx.provide("keys@1", (caller) => ({
|
|
14
|
+
register(key) {
|
|
15
|
+
const spec = String(key?.key ?? "").toLowerCase();
|
|
16
|
+
if (!KEY_SPEC.test(spec))
|
|
17
|
+
throw new Error(`invalid key ${JSON.stringify(key?.key)}: use ctrl+<letter>, alt+<letter or digit> or shift+tab`);
|
|
18
|
+
if (RESERVED_KEYS.has(spec))
|
|
19
|
+
throw new Error(`key ${spec} is reserved by the frontend`);
|
|
20
|
+
const taken = registry.get(spec);
|
|
21
|
+
if (taken)
|
|
22
|
+
throw new Error(`key ${spec} is already registered by ${taken.plugin}`);
|
|
23
|
+
const entry = { key, plugin: caller.plugin };
|
|
24
|
+
registry.set(spec, entry);
|
|
25
|
+
const revoke = () => {
|
|
26
|
+
if (registry.get(spec) === entry)
|
|
27
|
+
registry.delete(spec);
|
|
28
|
+
};
|
|
29
|
+
caller.onDispose(revoke);
|
|
30
|
+
return revoke;
|
|
31
|
+
},
|
|
32
|
+
get: (spec) => registry.get(spec.toLowerCase()),
|
|
33
|
+
list: () => [...registry.entries()].map(([k, { key, plugin }]) => ({ key: k, ...(key.description ? { description: key.description } : {}), plugin })).sort((a, b) => a.key.localeCompare(b.key)),
|
|
34
|
+
}));
|
|
35
|
+
},
|
|
36
|
+
});
|