@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
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// jade/compact:历史接近上下文窗口时压缩投影(见 compact.ts);/compact 让下一次请求前总结一次;
|
|
2
|
+
// 请求因为超出上下文窗口失败时,压缩后用 { kind: "retry" } 重试一次(session.afterTurn,设计 4.3)。
|
|
3
|
+
|
|
4
|
+
import type { LogEvent } from "uncarved";
|
|
5
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
6
|
+
import type { Commands } from "../commands/index.ts";
|
|
7
|
+
import type { Models } from "../models/index.ts";
|
|
8
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
9
|
+
import { compactor, compactThreshold, isContextOverflow, parseTokens } from "./compact.ts";
|
|
10
|
+
import { dim } from "./style.ts";
|
|
11
|
+
|
|
12
|
+
/** 认不出窗口时的阈值 */
|
|
13
|
+
const FALLBACK_THRESHOLD = 120_000;
|
|
14
|
+
/** Claude 默认的回复上限,阈值要给它留出位置 */
|
|
15
|
+
const ANTHROPIC_REPLY_TOKENS = 64_000;
|
|
16
|
+
/** 总结要调用模型,比 hook 默认的 30 秒长 */
|
|
17
|
+
const SUMMARY_TIMEOUT_MS = 300_000;
|
|
18
|
+
|
|
19
|
+
/** compact@1 */
|
|
20
|
+
export interface Compact {
|
|
21
|
+
/** 历史超过这么多 token 时自动压缩(界面显示用) */
|
|
22
|
+
threshold(): number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 自动压缩的阈值:配置的 thresholdTokens,否则按当前模型的上下文窗口算 */
|
|
26
|
+
function thresholdFor(config: Record<string, unknown>, models: Models): () => number {
|
|
27
|
+
const configured = parseTokens(config.thresholdTokens);
|
|
28
|
+
return () => {
|
|
29
|
+
if (configured) return configured;
|
|
30
|
+
const m = models.current();
|
|
31
|
+
// 别的模型插件可能不给窗口(或给错)
|
|
32
|
+
if (!m || !(typeof m.contextWindow === "number" && m.contextWindow > 0)) return FALLBACK_THRESHOLD;
|
|
33
|
+
return compactThreshold(m.contextWindow, m.kind === "anthropic" ? ANTHROPIC_REPLY_TOKENS : 0);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** /compact 请求过的会话:下一次投影时无论是否超过阈值都总结一次 */
|
|
38
|
+
const requested = new Set<string>();
|
|
39
|
+
|
|
40
|
+
export default definePlugin({
|
|
41
|
+
process(ctx) {
|
|
42
|
+
ctx.provide("compact@1", { threshold: thresholdFor(ctx.config, ctx.require<Models>("models@1")) } satisfies Compact);
|
|
43
|
+
const commands = ctx.service<Commands>("commands@1");
|
|
44
|
+
commands?.register({
|
|
45
|
+
name: "compact",
|
|
46
|
+
description: "compact the history before the next request",
|
|
47
|
+
run(_args, c) {
|
|
48
|
+
const s = c.sessions.current;
|
|
49
|
+
if (!s) return "No session is open.";
|
|
50
|
+
requested.add(s.id);
|
|
51
|
+
return dim("History will be compacted before the next request.");
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const renderers = ctx.service<Renderers>("renderers@1");
|
|
55
|
+
if (renderers) {
|
|
56
|
+
renderers.event({
|
|
57
|
+
event: "compacted",
|
|
58
|
+
render: (data) => {
|
|
59
|
+
const d = data as { from: number; upTo: number; before: number };
|
|
60
|
+
return { line: dim(`[compacted ${d.upTo - d.from} messages, ~${d.before} tokens before]`) };
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
renderers.event({ event: "trimmed", render: (data) => ({ line: dim(`[cleared old tool output, ~${(data as { saved: number }).saved} tokens]`) }) });
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
session(ctx) {
|
|
68
|
+
const models = ctx.require<Models>("models@1");
|
|
69
|
+
const configured = parseTokens(ctx.config.thresholdTokens);
|
|
70
|
+
const keepTokens = parseTokens(ctx.config.keepTokens);
|
|
71
|
+
const threshold = thresholdFor(ctx.config, models);
|
|
72
|
+
const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin });
|
|
73
|
+
const id = ctx.session.id;
|
|
74
|
+
|
|
75
|
+
ctx.kernel.hook({
|
|
76
|
+
kind: "transform",
|
|
77
|
+
point: "context.project",
|
|
78
|
+
// 排在其他改投影的插件(例如 jade/mentions 附上引用的文件)之后:估算的是最终的投影
|
|
79
|
+
order: 100,
|
|
80
|
+
timeoutMs: SUMMARY_TIMEOUT_MS,
|
|
81
|
+
fn: (messages, hook) => {
|
|
82
|
+
if (requested.delete(id)) c.request();
|
|
83
|
+
return c.project(messages, hook);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 上下文溢出后压缩重试,每次用户输入只重试一次
|
|
88
|
+
let retried = false;
|
|
89
|
+
ctx.kernel.hook({
|
|
90
|
+
kind: "observe",
|
|
91
|
+
point: "event",
|
|
92
|
+
fn: (e: LogEvent) => {
|
|
93
|
+
c.observe(e);
|
|
94
|
+
if (e.type === "input.received" && (e.data as { kind: string }).kind === "message") retried = false;
|
|
95
|
+
if (e.type !== "turn.ended") return;
|
|
96
|
+
const d = (e as LogEvent<"turn.ended">).data;
|
|
97
|
+
if (d.reason !== "error" || retried || !isContextOverflow(d.error)) return;
|
|
98
|
+
retried = true;
|
|
99
|
+
c.request();
|
|
100
|
+
const ui = ctx.service<UI>("ui@1");
|
|
101
|
+
const window = models.current()?.contextWindow;
|
|
102
|
+
ui?.notify(
|
|
103
|
+
"the request exceeded the model's context window; compacting the history and retrying" +
|
|
104
|
+
(configured || !window ? "" : ` (jade assumed a ${window}-token window; set the real one with JADE_CONTEXT_WINDOW)`),
|
|
105
|
+
"warning",
|
|
106
|
+
);
|
|
107
|
+
// 不能在这里等 send:它要等这一轮结束,而这一轮在等这个 hook
|
|
108
|
+
ctx.session.afterTurn(() => {
|
|
109
|
+
ctx.session.send({ kind: "retry" }).catch((err: unknown) => ctx.signal.aborted || ui?.notify(`retry after compaction failed: ${err instanceof Error ? err.message : String(err)}`, "error"));
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/compact",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"provides": ["compact@1"],
|
|
7
|
+
"requires": ["models@1"],
|
|
8
|
+
"optional": ["commands@1", "renderers@1", "ui@1"],
|
|
9
|
+
"config": {
|
|
10
|
+
"thresholdTokens": {
|
|
11
|
+
"description": "compact when the history reaches this many tokens (a number or e.g. \"120k\"); default: 80% of the model's context window, leaving room for the reply"
|
|
12
|
+
},
|
|
13
|
+
"keepTokens": {
|
|
14
|
+
"description": "after compacting, keep about this many tokens of the most recent messages verbatim; default 30% of the threshold"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// 显示用的小工具:截断和 ANSI 颜色。插件之间不共享代码(只能共享类型),所以这里也有一份(同 jade/fs 的 style.ts)。
|
|
2
|
+
|
|
3
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
4
|
+
|
|
5
|
+
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
6
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
7
|
+
/**
|
|
8
|
+
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
|
|
9
|
+
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
10
|
+
*/
|
|
11
|
+
const paint = (open: string, close: string) => (s: string) =>
|
|
12
|
+
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
13
|
+
export const dim = paint("2", "22");
|
|
14
|
+
export const red = paint("31", "39");
|
|
15
|
+
export const green = paint("32", "39");
|
|
16
|
+
export const yellow = paint("33", "39");
|
|
17
|
+
export const bold = paint("1", "22");
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// jade/continue:模型的回复被输出上限截断(这一轮以 max_tokens 结束)时,让模型接着做,而不是把半截任务丢给用户。
|
|
2
|
+
//
|
|
3
|
+
// 从 turn.ended 事件知道这一轮怎么结束的(observe event),在 session.afterTurn 里用 session.send 发一条 [Note from jade]。
|
|
4
|
+
// 每次用户输入之后最多续写 maxContinues 次。从旧 jade 的 plugins/turns.ts 的 continueTurns 移植。
|
|
5
|
+
//
|
|
6
|
+
// 旧 jade 的"上下文溢出后压缩重试"不在这里:不压缩就重试只会再溢出一次,整件事归 jade/compact。
|
|
7
|
+
|
|
8
|
+
import type { Block, UserInput } from "uncarved";
|
|
9
|
+
import { definePlugin, type UI } from "../../src/api.ts";
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_CONTINUES = 3;
|
|
12
|
+
export const CONTINUE_NOTE =
|
|
13
|
+
"Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " +
|
|
14
|
+
"If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
|
|
15
|
+
|
|
16
|
+
export default definePlugin({
|
|
17
|
+
session(ctx) {
|
|
18
|
+
const ui = ctx.service<UI>("ui@1");
|
|
19
|
+
const raw = ctx.config.maxContinues;
|
|
20
|
+
let max = DEFAULT_CONTINUES;
|
|
21
|
+
if (typeof raw === "number" && Number.isInteger(raw) && raw >= 0) max = raw;
|
|
22
|
+
else if (raw !== undefined) ui?.notify(`jade/continue: maxContinues must be a non-negative integer, got ${JSON.stringify(raw)}; using ${DEFAULT_CONTINUES}`, "warning");
|
|
23
|
+
|
|
24
|
+
/** 这次用户输入之后已经续写了几次 */
|
|
25
|
+
let count = 0;
|
|
26
|
+
/** 我们发出、还没收到的续写;收到它不算用户的新输入 */
|
|
27
|
+
let pending: Block | undefined;
|
|
28
|
+
|
|
29
|
+
const note = (): Block => ({ kind: "text", text: `[Note from jade] ${CONTINUE_NOTE}`, origin: { by: "plugin", id: ctx.plugin }, trust: "trusted" });
|
|
30
|
+
|
|
31
|
+
ctx.kernel.hook({
|
|
32
|
+
kind: "observe",
|
|
33
|
+
point: "event",
|
|
34
|
+
fn: (e) => {
|
|
35
|
+
if (e.type === "input.received") {
|
|
36
|
+
const input = e.data as UserInput;
|
|
37
|
+
// 用户(或别的插件替用户)发的新消息:重新计数。/retry、审批不算新输入
|
|
38
|
+
if (input.kind !== "message") return;
|
|
39
|
+
const ours = pending !== undefined && input.content.length === 1 && input.content[0]!.kind === "text" && input.content[0]!.origin.by === "plugin" && input.content[0]!.origin.id === ctx.plugin;
|
|
40
|
+
if (ours) pending = undefined;
|
|
41
|
+
else count = 0;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (e.type !== "turn.ended") return;
|
|
45
|
+
if ((e.data as { reason?: unknown }).reason !== "max_tokens" || count >= max) return;
|
|
46
|
+
count++;
|
|
47
|
+
const n = count;
|
|
48
|
+
// 本轮完全结束之后再发:现在发会排在本轮之后,但 send 要等本轮结束,不能在 hook 里等
|
|
49
|
+
ctx.session.afterTurn(() => {
|
|
50
|
+
ui?.notify(`[asking the model to continue (${n}/${max})]`);
|
|
51
|
+
pending = note();
|
|
52
|
+
// 不能 await:afterTurn 跑完之后会话才处理排队的输入。出错时前端从 turn.ended 显示原因
|
|
53
|
+
ctx.session.send({ kind: "message", content: [pending] }).catch(() => {});
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/continue",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"optional": ["ui@1"],
|
|
7
|
+
"config": {
|
|
8
|
+
"maxContinues": {
|
|
9
|
+
"default": 3,
|
|
10
|
+
"description": "how many times in a row the model is asked to continue a reply cut off at the output token limit; 0 turns it off"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// 按行比较两段文本,生成带行号和上下文的 unified diff
|
|
1
|
+
// 按行比较两段文本,生成带行号和上下文的 unified diff:审批修改时的预览、对话里修改结果的展开内容。从旧 jade 的 ui/diff.ts 移植。
|
|
2
2
|
//
|
|
3
3
|
// 先去掉相同的开头和结尾,只对中间部分求最长公共子序列;中间部分太大时(整个文件重写)
|
|
4
4
|
// 不再逐行比较,整段显示为删除加新增,避免占用大量内存和时间。
|
|
5
5
|
|
|
6
|
-
import { dim, green, red } from "./
|
|
6
|
+
import { dim, green, red } from "./style.ts";
|
|
7
7
|
|
|
8
8
|
export type DiffLine = { kind: " " | "-" | "+"; text: string; /** 旧文件里的行号(删除、相同) */ old?: number; /** 新文件里的行号(新增、相同) */ new?: number };
|
|
9
9
|
|
|
@@ -110,8 +110,7 @@ export function diffStats(lines: DiffLine[]): DiffStats {
|
|
|
110
110
|
* 显示用:每行前面是行号(新增行用新文件的行号,其余用旧文件的),段与段之间用 ⋯ 分开。
|
|
111
111
|
* 超过 maxLines 行时截断
|
|
112
112
|
*/
|
|
113
|
-
export function renderDiff(before: string, after: string, maxLines = 60): string {
|
|
114
|
-
const lines = diffLines(before, after);
|
|
113
|
+
export function renderDiff(before: string, after: string, maxLines = 60, lines = diffLines(before, after)): string {
|
|
115
114
|
const parts = hunks(lines);
|
|
116
115
|
if (parts.length === 0) return dim(" (no changes)");
|
|
117
116
|
const width = String(lines.reduce((w, l) => Math.max(w, l.old ?? 0, l.new ?? 0), 0)).length;
|
|
@@ -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
|
// 否则模型是按过时的内容在改,会覆盖掉别人的修改。和修改在同一次回复里发出的读取不算:
|
|
@@ -11,12 +10,13 @@ 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
12
|
import type { Block, Tool } from "uncarved";
|
|
14
|
-
import { sha256, type Checkpoints } from "./checkpoint.ts";
|
|
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;
|
|
18
|
+
/** 一次 read_file 返回的文本上限(约 25k token) */
|
|
19
|
+
const MAX_READ_CHARS = 100_000;
|
|
20
20
|
const MAX_GLOB = 500;
|
|
21
21
|
const MAX_IMAGE_BYTES = 5_000_000;
|
|
22
22
|
const MAX_PDF_BYTES = 10_000_000;
|
|
@@ -45,15 +45,13 @@ export function display(cwd: string, full: string): string {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export interface FsOptions {
|
|
48
|
-
/**
|
|
49
|
-
checkpoints?: Checkpoints;
|
|
50
|
-
/** 模型调用的次数,每次调用模型前加一(见 distro 的 model.before hook)。不给就不检查同一次回复里的先读后改 */
|
|
48
|
+
/** 模型调用的次数,每次调用模型前加一(见 index.ts 的 model.before hook)。不给就不检查同一次回复里的先读后改 */
|
|
51
49
|
responses?: { readonly calls: number };
|
|
52
50
|
/** grep 用的 ripgrep,默认 "rg";null 表示不用(测试) */
|
|
53
51
|
rg?: string | null;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
/**
|
|
54
|
+
/** 读过或写过的文件当时的样子;只在本会话、本进程内有效,恢复的会话要重新读 */
|
|
57
55
|
interface Seen {
|
|
58
56
|
mtimeMs: number;
|
|
59
57
|
size: number;
|
|
@@ -65,14 +63,13 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
65
63
|
const at = (p: unknown) => resolveIn(cwd, p as string);
|
|
66
64
|
const seen = new Map<string, Seen>();
|
|
67
65
|
/** 文件没变时保留更早的 call:之前的回复里读过,这次回复里又读一遍,不影响修改 */
|
|
68
|
-
const remember =
|
|
66
|
+
const remember = (full: string, info: Stats, call: number) => {
|
|
69
67
|
const s = seen.get(full);
|
|
70
68
|
const same = s !== undefined && s.mtimeMs === info.mtimeMs && s.size === info.size;
|
|
71
69
|
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call });
|
|
72
|
-
await opts.checkpoints?.markSeen(ctx.sessionId, full, sha256(text));
|
|
73
70
|
};
|
|
74
71
|
/** 不能修改已有文件的原因;可以修改时返回 undefined。text 是文件现在的内容 */
|
|
75
|
-
const staleness =
|
|
72
|
+
const staleness = (full: string, info: Stats): string | undefined => {
|
|
76
73
|
const shown = display(cwd, full);
|
|
77
74
|
const modified = `${shown} has been modified since it was last read (by the user or a command). Read it again before changing it.`;
|
|
78
75
|
const s = seen.get(full);
|
|
@@ -83,22 +80,15 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
83
80
|
}
|
|
84
81
|
return undefined;
|
|
85
82
|
}
|
|
86
|
-
|
|
87
|
-
const hash = await opts.checkpoints?.seenHash(ctx.sessionId, full);
|
|
88
|
-
if (hash === undefined) return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
89
|
-
if (hash !== sha256(text)) return modified;
|
|
90
|
-
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: -1 });
|
|
91
|
-
return undefined;
|
|
92
|
-
};
|
|
93
|
-
const checkpoint = async (ctx: { sessionId: string; toolUseId: string }, tool: string, full: string, before: string | null, after: string) => {
|
|
94
|
-
await opts.checkpoints?.save(ctx.sessionId, { toolUseId: ctx.toolUseId, tool, path: full, before, afterHash: sha256(after) });
|
|
83
|
+
return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
95
84
|
};
|
|
96
85
|
|
|
97
86
|
const readFileTool: Tool = {
|
|
98
87
|
name: "read_file",
|
|
99
88
|
description:
|
|
100
89
|
"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. " +
|
|
101
|
-
`At most ${MAX_LINES} lines are returned per call; use offset (1-based start line) and limit to page through larger files
|
|
90
|
+
`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. ` +
|
|
91
|
+
`Lines longer than ${MAX_LINE_CHARS} characters are cut and marked "[line truncated]"; do not copy a cut line into edit_file.`,
|
|
102
92
|
inputSchema: {
|
|
103
93
|
type: "object",
|
|
104
94
|
properties: { path: str, offset: int, limit: int },
|
|
@@ -108,7 +98,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
108
98
|
effect: "read",
|
|
109
99
|
concurrency: "safe",
|
|
110
100
|
idempotent: true,
|
|
111
|
-
async execute(input
|
|
101
|
+
async execute(input) {
|
|
112
102
|
const { path: p, offset = 1, limit = MAX_LINES } = input as { path: string; offset?: number; limit?: number };
|
|
113
103
|
if (limit < 1) return { content: "limit must be at least 1", isError: true };
|
|
114
104
|
const full = at(p);
|
|
@@ -140,17 +130,27 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
140
130
|
return { content: `${display(cwd, full)} is a binary file (${buf.length} bytes) and cannot be shown as text`, isError: true };
|
|
141
131
|
}
|
|
142
132
|
const text = buf.toString("utf8");
|
|
143
|
-
|
|
133
|
+
remember(full, info, opts.responses?.calls ?? -1);
|
|
144
134
|
if (text.length === 0) return { content: "(empty file)" };
|
|
145
135
|
const lines = text.split(/\r?\n/);
|
|
146
136
|
if (lines.at(-1) === "") lines.pop();
|
|
147
137
|
const start = Math.max(1, offset);
|
|
148
138
|
const slice = lines.slice(start - 1, start - 1 + Math.min(limit, MAX_LINES));
|
|
149
139
|
if (slice.length === 0) return { content: `(file has ${lines.length} lines; offset ${start} is past the end)`, isError: true };
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
140
|
+
// 按整行截断到 MAX_READ_CHARS:压缩过的代码、lock 文件一次就能占掉大半个上下文
|
|
141
|
+
const out: string[] = [];
|
|
142
|
+
let size = 0;
|
|
143
|
+
for (const [i, l] of slice.entries()) {
|
|
144
|
+
const row = `${start + i}\t${l.length > MAX_LINE_CHARS ? `${l.slice(0, MAX_LINE_CHARS)}… [line truncated: ${l.length} characters]` : l}`;
|
|
145
|
+
if (out.length > 0 && size + row.length + 1 > MAX_READ_CHARS) break;
|
|
146
|
+
out.push(row);
|
|
147
|
+
size += row.length + 1;
|
|
148
|
+
}
|
|
149
|
+
const body = out.join("\n");
|
|
150
|
+
const end = start + out.length - 1;
|
|
151
|
+
if (out.length < slice.length) {
|
|
152
|
+
return { content: `${body}\n(output capped at about ${MAX_READ_CHARS} characters: showing lines ${start}-${end} of ${lines.length}; continue with offset ${end + 1})` };
|
|
153
|
+
}
|
|
154
154
|
return { content: end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body };
|
|
155
155
|
},
|
|
156
156
|
};
|
|
@@ -170,19 +170,20 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
170
170
|
concurrency: "exclusive",
|
|
171
171
|
// 写入完整内容,重复执行结果相同
|
|
172
172
|
idempotent: true,
|
|
173
|
-
async execute(input
|
|
174
|
-
const { path: p, content } = input as { path: string; content: string };
|
|
173
|
+
async execute(input) {
|
|
174
|
+
const { path: p, content: given } = input as { path: string; content: string };
|
|
175
175
|
const full = at(p);
|
|
176
176
|
const info = await statOrUndefined(full);
|
|
177
177
|
if (info?.isDirectory()) return { content: `${display(cwd, full)} is a directory`, isError: true };
|
|
178
178
|
// 同样内容重复写入(崩溃后重跑)不算覆盖别人的修改
|
|
179
179
|
const before = info ? await readFile(full, "utf8") : null;
|
|
180
|
-
|
|
180
|
+
// read_file 不显示换行风格和 BOM:覆盖已有文件时沿用原来的,否则整个文件每一行都算改了
|
|
181
|
+
const content = before === null ? given : keepStyle(before, given);
|
|
182
|
+
const problem = info && before !== null && before !== content ? staleness(full, info) : undefined;
|
|
181
183
|
if (problem) return { content: problem, isError: true };
|
|
182
184
|
await mkdir(path.dirname(full), { recursive: true });
|
|
183
185
|
await writeFile(full, content);
|
|
184
|
-
|
|
185
|
-
if (before !== content) await checkpoint(ctx, "write_file", full, before, content);
|
|
186
|
+
remember(full, await stat(full), -1);
|
|
186
187
|
// 和 read_file 一样数行:末尾的换行不算多出一行
|
|
187
188
|
const lines = content === "" ? 0 : content.split(/\r?\n/).length - (/\r?\n$/.test(content) ? 1 : 0);
|
|
188
189
|
return { content: `Wrote ${lines} lines to ${display(cwd, full)}` };
|
|
@@ -190,7 +191,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
190
191
|
};
|
|
191
192
|
|
|
192
193
|
/** edit_file 和 multi_edit 共用:检查文件能否修改,按顺序应用全部替换,全部成功才写入 */
|
|
193
|
-
const applyEdits = async (
|
|
194
|
+
const applyEdits = async (p: string, edits: Edit[]) => {
|
|
194
195
|
const full = at(p);
|
|
195
196
|
const shown = display(cwd, full);
|
|
196
197
|
const info = await statOrUndefined(full);
|
|
@@ -198,7 +199,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
198
199
|
return { content: seen.has(full) ? `${shown} no longer exists` : `${shown} does not exist; use write_file to create it`, isError: true };
|
|
199
200
|
}
|
|
200
201
|
const text = await readFile(full, "utf8");
|
|
201
|
-
const problem =
|
|
202
|
+
const problem = staleness(full, info);
|
|
202
203
|
if (problem) return { content: problem, isError: true };
|
|
203
204
|
let next = text;
|
|
204
205
|
let total = 0;
|
|
@@ -209,8 +210,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
209
210
|
total += r.count;
|
|
210
211
|
}
|
|
211
212
|
await writeFile(full, next);
|
|
212
|
-
|
|
213
|
-
await checkpoint(ctx, tool, full, text, next);
|
|
213
|
+
remember(full, await stat(full), -1);
|
|
214
214
|
return { content: `Edited ${shown} (${total} replacement${total === 1 ? "" : "s"})` };
|
|
215
215
|
};
|
|
216
216
|
|
|
@@ -230,9 +230,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
230
230
|
concurrency: "exclusive",
|
|
231
231
|
// 第二次执行时 old_string 可能已经不存在,或者被替换两次
|
|
232
232
|
idempotent: false,
|
|
233
|
-
async execute(input
|
|
233
|
+
async execute(input) {
|
|
234
234
|
const { path: p, ...edit } = input as { path: string } & Edit;
|
|
235
|
-
return applyEdits(
|
|
235
|
+
return applyEdits(p, [edit]);
|
|
236
236
|
},
|
|
237
237
|
};
|
|
238
238
|
|
|
@@ -261,10 +261,10 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
261
261
|
effect: "write",
|
|
262
262
|
concurrency: "exclusive",
|
|
263
263
|
idempotent: false,
|
|
264
|
-
async execute(input
|
|
264
|
+
async execute(input) {
|
|
265
265
|
const { path: p, edits } = input as { path: string; edits: Edit[] };
|
|
266
266
|
if (edits.length === 0) return { content: "edits must not be empty", isError: true };
|
|
267
|
-
return applyEdits(
|
|
267
|
+
return applyEdits(p, edits);
|
|
268
268
|
},
|
|
269
269
|
};
|
|
270
270
|
|
|
@@ -341,19 +341,37 @@ export function applyEditsToText(text: string, edits: Edit[], shown: string): st
|
|
|
341
341
|
return { text: next };
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
+
/** text 里多数换行是 CRLF 时返回 "\r\n",多数是 LF 时返回 "\n",一样多或没有换行时返回 fallback */
|
|
345
|
+
function eolOf(text: string, fallback: string): string {
|
|
346
|
+
const crlf = text.split("\r\n").length - 1;
|
|
347
|
+
const lf = text.split("\n").length - 1 - crlf;
|
|
348
|
+
return crlf > lf ? "\r\n" : lf > crlf ? "\n" : fallback;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** 原文件主要用 CRLF、新内容只有 LF 时换成 CRLF;原文件有 BOM、新内容没有时补上 */
|
|
352
|
+
export function keepStyle(before: string, content: string): string {
|
|
353
|
+
let out = content;
|
|
354
|
+
if (eolOf(before, "\n") === "\r\n" && !out.includes("\r\n")) out = out.replaceAll("\n", "\r\n");
|
|
355
|
+
if (before.startsWith("") && !out.startsWith("")) out = `${out}`;
|
|
356
|
+
return out;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
360
|
+
|
|
344
361
|
/** 在 text 上做一次替换;失败时返回原因 */
|
|
345
362
|
function replaceIn(text: string, { old_string, new_string, replace_all }: Edit, shown: string): { text: string; count: number } | string {
|
|
346
363
|
if (old_string === new_string) return "old_string and new_string are identical";
|
|
347
364
|
if (old_string === "") return "old_string must not be empty; use write_file to create a file";
|
|
348
|
-
//
|
|
349
|
-
const
|
|
350
|
-
const
|
|
351
|
-
const to = crlf ? new_string.replaceAll("\n", "\r\n") : new_string;
|
|
352
|
-
const count = text.split(from).length - 1;
|
|
365
|
+
// read_file 不显示换行风格:old_string 里的换行匹配 LF 和 CRLF,CRLF 文件、换行混用的文件都能对上
|
|
366
|
+
const pattern = new RegExp(old_string.split(/\r?\n/).map(escapeRegExp).join("\\r?\\n"), "g");
|
|
367
|
+
const count = text.match(pattern)?.length ?? 0;
|
|
353
368
|
if (count === 0) return `old_string not found in ${shown}${nearMiss(text, old_string)}`;
|
|
354
369
|
if (count > 1 && !replace_all) return `old_string occurs ${count} times in ${shown}; add context to make it unique or set replace_all`;
|
|
370
|
+
// new_string 的换行沿用被替换那段的;那段只有一行时沿用整个文件的,单行改成多行也不会混进 LF
|
|
371
|
+
const fileEol = eolOf(text, "\n");
|
|
372
|
+
const to = (matched: string) => (new_string.includes("\r\n") ? new_string : new_string.replaceAll("\n", eolOf(matched, fileEol)));
|
|
355
373
|
// 用函数作替换值,new_string 里的 $& $1 等不会被当成替换模式
|
|
356
|
-
return { text:
|
|
374
|
+
return { text: text.replace(replace_all ? pattern : new RegExp(pattern.source), to), count: replace_all ? count : 1 };
|
|
357
375
|
}
|
|
358
376
|
|
|
359
377
|
/**
|
|
@@ -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;
|
|
@@ -61,7 +61,7 @@ export function grepTool(cwd: string, opts: GrepOptions = {}): Tool {
|
|
|
61
61
|
name: "grep",
|
|
62
62
|
description:
|
|
63
63
|
"Search file contents with a regular expression (JavaScript syntax). Searches path (a file or directory, default: the working directory), " +
|
|
64
|
-
`optionally only files matching the glob include (e.g. "
|
|
64
|
+
`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. ` +
|
|
65
65
|
`output_mode: "content" (default) returns "file:line:text" for matches and "file-line-text" for context lines, at most ${MAX_MATCHES} matches; ` +
|
|
66
66
|
`"files" returns only the paths of matching files; "count" returns "file:count". ` +
|
|
67
67
|
`context (0-${MAX_CONTEXT}) adds that many lines before and after each match in content mode.`,
|
|
@@ -232,7 +232,8 @@ async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "miss
|
|
|
232
232
|
// ---------- JS ----------
|
|
233
233
|
|
|
234
234
|
async function jsGrep(s: Search): Promise<Found> {
|
|
235
|
-
|
|
235
|
+
// 和 ripgrep 的 --glob 一致:不含 / 的模式("*.ts")匹配任意深度的文件名
|
|
236
|
+
const filter = s.include ? globMatcher(s.include.includes("/") ? s.include : `**/${s.include}`) : undefined;
|
|
236
237
|
const files: FileHits[] = [];
|
|
237
238
|
let matches = 0;
|
|
238
239
|
const single = (await stat(s.root)).isFile();
|
|
@@ -0,0 +1,34 @@
|
|
|
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 } 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
|
+
const responses = { calls: 0 };
|
|
19
|
+
ctx.kernel.hook({
|
|
20
|
+
kind: "transform",
|
|
21
|
+
point: "model.before",
|
|
22
|
+
fn: () => {
|
|
23
|
+
responses.calls++;
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
for (const tool of fsTools(ctx.cwd, { responses })) ctx.kernel.tool(tool);
|
|
27
|
+
// 子 Agent(jade/task)的只读工具:另一套实例,子 Agent 读过的文件不算主会话读过
|
|
28
|
+
// jade/task 被隔离时它的服务已失效:子 Agent 没有文件工具,文件工具本身照常
|
|
29
|
+
try {
|
|
30
|
+
const task = ctx.service<Task>("task@1");
|
|
31
|
+
if (task) for (const tool of fsTools(ctx.cwd)) if (tool.effect === "read") task.tool(tool);
|
|
32
|
+
} catch {}
|
|
33
|
+
},
|
|
34
|
+
});
|