@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,67 @@
|
|
|
1
|
+
// 宿主显示用的纯函数:工具调用的默认摘要和预览、折叠的工具结果。不依赖运行时和界面状态。
|
|
2
|
+
// 各个工具自己的显示由插件经 renderers@1 登记。
|
|
3
|
+
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { dim } from "./screen.ts";
|
|
7
|
+
|
|
8
|
+
/** 显示用的路径:工作目录里的用相对路径,其余用绝对路径;都用正斜杠。和 jade/fs 的 display 相同,插件之间不 import 值,所以这里留一份 */
|
|
9
|
+
function display(cwd: string, full: string): string {
|
|
10
|
+
const rel = path.relative(cwd, full);
|
|
11
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
12
|
+
return shown.split(path.sep).join("/");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 带颜色(SGR 序列)的文字截到 n 个可见字符:序列不算长度、不会被切开;截断时在最后复位颜色,
|
|
19
|
+
* 不让没关上的颜色渗到后面的行
|
|
20
|
+
*/
|
|
21
|
+
export function clipStyled(s: string, n: number): string {
|
|
22
|
+
const sgr = /\x1b\[[0-9;:]*m/y;
|
|
23
|
+
let out = "";
|
|
24
|
+
let visible = 0;
|
|
25
|
+
let styled = false;
|
|
26
|
+
for (let i = 0; i < s.length; ) {
|
|
27
|
+
sgr.lastIndex = i;
|
|
28
|
+
const m = sgr.exec(s);
|
|
29
|
+
if (m) {
|
|
30
|
+
out += m[0];
|
|
31
|
+
styled = true;
|
|
32
|
+
i += m[0].length;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (visible === n) return out + "…" + (styled ? "\x1b[0m" : "");
|
|
36
|
+
out += s[i];
|
|
37
|
+
visible++;
|
|
38
|
+
i++;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 家目录下的路径显示成 ~/… */
|
|
44
|
+
export function shortPath(p: string): string {
|
|
45
|
+
const rel = path.relative(os.homedir(), p);
|
|
46
|
+
const shown = rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? `~/${rel}` : rel === "" ? "~" : p;
|
|
47
|
+
return shown.split(path.sep).join("/");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 工具调用默认的一行摘要,显示在工具名后面的括号里:path 参数,没有时是参数的 JSON */
|
|
51
|
+
export function summarize(cwd: string, input: unknown): string {
|
|
52
|
+
const i = (input ?? {}) as Record<string, unknown>;
|
|
53
|
+
const where = typeof i.path === "string" ? display(cwd, i.path) : "";
|
|
54
|
+
return where || clip(JSON.stringify(input), 120);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 审批时默认的预览:参数的 JSON */
|
|
58
|
+
export function preview(input: unknown): string {
|
|
59
|
+
return dim(clip(JSON.stringify(input, null, 2), 2000));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 工具结果展开后的内容:缩进的全部行,超过 max 行时截断 */
|
|
63
|
+
export function detailsOf(lines: string[], color: (s: string) => string, max: number): string {
|
|
64
|
+
const body = lines.slice(0, max).map((l) => color(` ${l}`));
|
|
65
|
+
if (lines.length > max) body.push(dim(` … ${lines.length - max} more lines`));
|
|
66
|
+
return body.join("\n");
|
|
67
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// 宿主的输出与交互接口。两种实现:
|
|
2
|
+
// - 终端界面(tui.tsx):全屏,标题在顶部,对话在中间自己滚动,输入框、状态栏固定在底部,运行中也能输入
|
|
3
|
+
// - 纯文本(这里):-p 和非交互运行时使用,逐行输出,提问用 readline
|
|
4
|
+
|
|
5
|
+
import { createInterface, type Interface } from "node:readline/promises";
|
|
6
|
+
import { stripVTControlCharacters } from "node:util";
|
|
7
|
+
|
|
8
|
+
export interface Choice<T> {
|
|
9
|
+
label: string;
|
|
10
|
+
value: T;
|
|
11
|
+
/** 选项后面的灰色说明 */
|
|
12
|
+
hint?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface StatusInfo {
|
|
16
|
+
/** 插件用 ctx.status 设置的段落(已经是一行、截断过) */
|
|
17
|
+
plugins?: { plugin: string; text: string }[];
|
|
18
|
+
cwd: string;
|
|
19
|
+
branch?: string;
|
|
20
|
+
model: string;
|
|
21
|
+
mode: string;
|
|
22
|
+
session?: string;
|
|
23
|
+
/** 上一次请求的上下文用量,limit 是模型的上下文窗口 */
|
|
24
|
+
context?: { used: number; limit: number; /** 超过这个 token 数会自动压缩 */ compactAt?: number };
|
|
25
|
+
/** 本会话累计的用量(含子 Agent 和压缩) */
|
|
26
|
+
spent?: { input: number; output: number };
|
|
27
|
+
/** 上一次请求的输入中命中缓存的比例(0–1);服务不报告缓存用量时为 undefined */
|
|
28
|
+
cache?: number;
|
|
29
|
+
/** 输出速度(token/秒):live 为 true 时是正在输出的这次回复按字数估的,否则是上一次回复按服务端报的用量算的 */
|
|
30
|
+
speed?: { tps: number; live?: boolean };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CommandInfo {
|
|
34
|
+
name: string;
|
|
35
|
+
args?: string;
|
|
36
|
+
about: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Screen {
|
|
40
|
+
/** 能向用户提问;为 false 时 select / input 直接返回 undefined */
|
|
41
|
+
readonly interactive: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* 追加已经完成的输出,可以带 ANSI 颜色。
|
|
44
|
+
* details:折叠起来的完整内容,终端界面里点击 text 展开;纯文本输出不显示。
|
|
45
|
+
* id:之后用 amend 给这一项补充内容(工具调用的结果);终端界面里这一项收起时只占一行
|
|
46
|
+
*/
|
|
47
|
+
log(text: string, opts?: { details?: string; id?: string }): void;
|
|
48
|
+
/**
|
|
49
|
+
* 给之前用 id 记下的一项补充内容:终端界面里接在那一项后面合成一项(details 替换原来的),
|
|
50
|
+
* 纯文本输出另起一行缩进显示。找不到那一项时也是另起一行
|
|
51
|
+
*/
|
|
52
|
+
amend(id: string, text: string, opts?: { details?: string }): void;
|
|
53
|
+
/**
|
|
54
|
+
* 正在运行的工具 id 的一行进度(已经排好版);undefined 表示工具结束了。
|
|
55
|
+
* 终端界面只在对话最后显示每个工具最新的一行,结束时去掉;纯文本输出逐行打印。
|
|
56
|
+
* output 表示是工具自己的输出,纯文本输出里每个工具最多打印 20 行
|
|
57
|
+
*/
|
|
58
|
+
progress(id: string, line: string | undefined, opts?: { output?: boolean }): void;
|
|
59
|
+
/** 流式输出模型的正文或思考 */
|
|
60
|
+
stream(kind: "text" | "thinking", text: string): void;
|
|
61
|
+
/** 结束当前的流式块 */
|
|
62
|
+
endStream(): void;
|
|
63
|
+
/** 正在进行的活动;undefined 表示空闲 */
|
|
64
|
+
busy(label: string | undefined): void;
|
|
65
|
+
status(info: Partial<StatusInfo>): void;
|
|
66
|
+
/** 排队等待发送的输入 */
|
|
67
|
+
queued(items: string[]): void;
|
|
68
|
+
/** 让用户从几项里选一项;取消时返回 undefined。signal 中止时关掉对话框,也返回 undefined */
|
|
69
|
+
select<T>(title: string, choices: Choice<T>[], opts?: { body?: string; signal?: AbortSignal }): Promise<T | undefined>;
|
|
70
|
+
/** 让用户输入一行;取消时返回 undefined。signal 同 select */
|
|
71
|
+
input(title: string, opts?: { initial?: string; mask?: boolean; placeholder?: string; signal?: AbortSignal }): Promise<string | undefined>;
|
|
72
|
+
/** 输入框里还没提交的文字和灰色提示(插件的 ctx.input 用);只有终端界面有 */
|
|
73
|
+
readonly draft?: { text(): string; insert(text: string): void; set(text: string): void; hint(text: string | undefined): void };
|
|
74
|
+
/** 清空已经显示的对话(开始新会话时) */
|
|
75
|
+
clear(): void;
|
|
76
|
+
close(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ---------- 颜色 ----------
|
|
80
|
+
|
|
81
|
+
// 设了 NO_COLOR(非空)时不输出颜色,见 https://no-color.org
|
|
82
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
83
|
+
/**
|
|
84
|
+
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:粗体里套着行内代码、链接时,后面的文字仍是粗体。
|
|
85
|
+
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
86
|
+
*/
|
|
87
|
+
const paint = (open: string, close: string) => (s: string) =>
|
|
88
|
+
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
89
|
+
export const dim = paint("2", "22");
|
|
90
|
+
export const red = paint("31", "39");
|
|
91
|
+
export const green = paint("32", "39");
|
|
92
|
+
export const yellow = paint("33", "39");
|
|
93
|
+
export const cyan = paint("36", "39");
|
|
94
|
+
export const bold = paint("1", "22");
|
|
95
|
+
export const italic = paint("3", "23");
|
|
96
|
+
export const underline = paint("4", "24");
|
|
97
|
+
export const strike = paint("9", "29");
|
|
98
|
+
|
|
99
|
+
// ---------- 过滤控制字符 ----------
|
|
100
|
+
|
|
101
|
+
/** 转义序列(CSI、OSC、DCS 这类字符串、其余 ESC 开头的)和换行、制表符之外的控制字符(含 C1) */
|
|
102
|
+
const CONTROL = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b[\]P^_X][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b[^[\]P^_X]?|[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
|
|
103
|
+
const SGR = /^\x1b\[[0-9;:]*m$/;
|
|
104
|
+
/** 文字末尾还没写完的转义序列:单独的 ESC、没有结束字节的 CSI、没有结束符的 OSC/DCS 这类字符串 */
|
|
105
|
+
const UNFINISHED = /\x1b(?:\[[0-?]*[ -/]*|[\]P^_X][^\x07\x1b]*)?$/;
|
|
106
|
+
/** 最多为一个没写完的序列留这么多字符 */
|
|
107
|
+
const MAX_HELD = 256;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 去掉可能搅乱终端的东西,只留颜色(SGR)、换行和制表符:插件、工具输出和模型的文字里可能有移动光标、清屏、
|
|
111
|
+
* 改窗口标题、写剪贴板之类的序列。单独的 \r(进度条)按终端的效果处理:每行只留最后一段
|
|
112
|
+
*/
|
|
113
|
+
export function terminalSafe(text: string): string {
|
|
114
|
+
const lines = text.replaceAll("\r\n", "\n").split("\n");
|
|
115
|
+
return lines
|
|
116
|
+
.map((l) => (l.includes("\r") ? (l.split("\r").filter(Boolean).at(-1) ?? "") : l).replace(CONTROL, (m) => (SGR.test(m) ? m : "")))
|
|
117
|
+
.join("\n");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 让显示到屏幕上的每一段文字都经过 terminalSafe */
|
|
121
|
+
export function safeScreen(screen: Screen): Screen {
|
|
122
|
+
const s = terminalSafe;
|
|
123
|
+
const opt = (v: string | undefined) => (v === undefined ? undefined : s(v));
|
|
124
|
+
const lf = (t: string) => t.replace(/\r\n?/g, "\n");
|
|
125
|
+
const plain = (t: string) => stripVTControlCharacters(s(t));
|
|
126
|
+
/** 流式文字末尾还没写完的转义序列,等下一段 */
|
|
127
|
+
let held: { kind: "text" | "thinking"; text: string } | undefined;
|
|
128
|
+
return {
|
|
129
|
+
interactive: screen.interactive,
|
|
130
|
+
log: (text, o) => screen.log(s(text), o && { ...o, details: opt(o.details) }),
|
|
131
|
+
amend: (id, text, o) => screen.amend(id, s(text), o && { ...o, details: opt(o.details) }),
|
|
132
|
+
progress: (id, line, o) => screen.progress(id, opt(line), o),
|
|
133
|
+
stream: (kind, text) => {
|
|
134
|
+
// 上一段末尾没完的转义序列接到这一段前面再过滤,不然它被拆成两半,后一半当成文字显示出来
|
|
135
|
+
if (held && held.kind !== kind) held = undefined;
|
|
136
|
+
const whole = (held?.text ?? "") + text;
|
|
137
|
+
// 很长还没结束的(OSC 之类的字符串一直不给结束符)不再等,按现在的样子过滤
|
|
138
|
+
const found = whole.search(UNFINISHED);
|
|
139
|
+
const cut = found >= 0 && whole.length - found <= MAX_HELD ? found : -1;
|
|
140
|
+
held = cut >= 0 ? { kind, text: whole.slice(cut) } : undefined;
|
|
141
|
+
const shown = s(cut >= 0 ? whole.slice(0, cut) : whole);
|
|
142
|
+
if (shown) screen.stream(kind, shown);
|
|
143
|
+
},
|
|
144
|
+
endStream: () => {
|
|
145
|
+
// 到结束都没写完的序列不完整,丢掉
|
|
146
|
+
held = undefined;
|
|
147
|
+
screen.endStream();
|
|
148
|
+
},
|
|
149
|
+
busy: (label) => screen.busy(opt(label)),
|
|
150
|
+
status: (info) => {
|
|
151
|
+
const out: Partial<StatusInfo> = { ...info };
|
|
152
|
+
for (const k of ["cwd", "branch", "model", "mode", "session"] as const) if (typeof info[k] === "string") out[k] = s(info[k]);
|
|
153
|
+
if (info.plugins) out.plugins = info.plugins.map((p) => ({ plugin: p.plugin, text: s(p.text) }));
|
|
154
|
+
screen.status(out);
|
|
155
|
+
},
|
|
156
|
+
queued: (items) => screen.queued(items.map(s)),
|
|
157
|
+
select: (title, choices, o) =>
|
|
158
|
+
screen.select(
|
|
159
|
+
s(title),
|
|
160
|
+
choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint) })),
|
|
161
|
+
o && { ...o, body: opt(o.body) },
|
|
162
|
+
),
|
|
163
|
+
input: (title, o) => screen.input(s(title), o && { ...o, initial: opt(o.initial), placeholder: opt(o.placeholder) }),
|
|
164
|
+
// 输入框里的 \r 是换行(旧 Mac 的换行符),不是进度条:先换成 \n,terminalSafe 才不会只留最后一段。
|
|
165
|
+
// 输入框里是要发给模型的纯文字,颜色也去掉
|
|
166
|
+
draft: screen.draft && {
|
|
167
|
+
text: () => screen.draft!.text(),
|
|
168
|
+
insert: (t) => screen.draft!.insert(plain(lf(t))),
|
|
169
|
+
set: (t) => screen.draft!.set(plain(lf(t))),
|
|
170
|
+
hint: (t) => screen.draft!.hint(t === undefined ? undefined : plain(lf(t))),
|
|
171
|
+
},
|
|
172
|
+
clear: () => screen.clear(),
|
|
173
|
+
close: () => screen.close(),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ---------- 纯文本实现 ----------
|
|
178
|
+
|
|
179
|
+
const MAX_OUTPUT_LINES = 20;
|
|
180
|
+
|
|
181
|
+
export function plainScreen(opts: { interactive: boolean }): Screen {
|
|
182
|
+
let atLineStart = true;
|
|
183
|
+
let streaming: "text" | "thinking" | undefined;
|
|
184
|
+
let rl: Interface | undefined;
|
|
185
|
+
/** 每个工具已经打印了多少行输出 */
|
|
186
|
+
const outputLines = new Map<string, number>();
|
|
187
|
+
|
|
188
|
+
const write = (s: string) => {
|
|
189
|
+
if (!s) return;
|
|
190
|
+
process.stdout.write(s);
|
|
191
|
+
atLineStart = s.endsWith("\n");
|
|
192
|
+
};
|
|
193
|
+
const line = (s: string) => {
|
|
194
|
+
if (!atLineStart) write("\n");
|
|
195
|
+
write(s + "\n");
|
|
196
|
+
};
|
|
197
|
+
/** 中止时返回 undefined */
|
|
198
|
+
const ask = async (q: string, signal?: AbortSignal) => {
|
|
199
|
+
rl ??= createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
200
|
+
try {
|
|
201
|
+
return (await rl.question(q, { signal })).trim();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
if (!signal?.aborted) throw err;
|
|
204
|
+
write("\n");
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
interactive: opts.interactive,
|
|
211
|
+
log: (text) => {
|
|
212
|
+
streaming = undefined;
|
|
213
|
+
line(text);
|
|
214
|
+
},
|
|
215
|
+
amend: (_id, text) => {
|
|
216
|
+
streaming = undefined;
|
|
217
|
+
line(` ${text}`);
|
|
218
|
+
},
|
|
219
|
+
progress: (id, text, o) => {
|
|
220
|
+
if (text === undefined) return void outputLines.delete(id);
|
|
221
|
+
streaming = undefined;
|
|
222
|
+
if (o?.output) {
|
|
223
|
+
const shown = outputLines.get(id) ?? 0;
|
|
224
|
+
if (shown > MAX_OUTPUT_LINES) return;
|
|
225
|
+
outputLines.set(id, shown + 1);
|
|
226
|
+
if (shown === MAX_OUTPUT_LINES) return line(dim(" │ …"));
|
|
227
|
+
}
|
|
228
|
+
line(text);
|
|
229
|
+
},
|
|
230
|
+
stream: (kind, text) => {
|
|
231
|
+
if (kind !== streaming) {
|
|
232
|
+
// 不少模型的正文以空行开头;整块都是换行时等下一块
|
|
233
|
+
if (kind === "text") text = text.replace(/^\n+/, "");
|
|
234
|
+
if (!text) return;
|
|
235
|
+
if (kind === "thinking") line(dim("thinking…"));
|
|
236
|
+
else if (!atLineStart) write("\n");
|
|
237
|
+
streaming = kind;
|
|
238
|
+
}
|
|
239
|
+
write(kind === "thinking" ? dim(text) : text);
|
|
240
|
+
},
|
|
241
|
+
endStream: () => {
|
|
242
|
+
streaming = undefined;
|
|
243
|
+
if (!atLineStart) write("\n");
|
|
244
|
+
},
|
|
245
|
+
busy: () => {},
|
|
246
|
+
status: () => {},
|
|
247
|
+
queued: () => {},
|
|
248
|
+
async select(title, choices, o) {
|
|
249
|
+
if (!opts.interactive) return undefined;
|
|
250
|
+
line(yellow(title));
|
|
251
|
+
if (o?.body) line(o.body);
|
|
252
|
+
choices.forEach((c, i) => line(` ${i + 1}. ${c.label}${c.hint ? dim(` ${c.hint}`) : ""}`));
|
|
253
|
+
if (o?.signal?.aborted) return undefined;
|
|
254
|
+
const n = Number(await ask(`Choose 1-${choices.length} (empty to cancel): `, o?.signal));
|
|
255
|
+
return Number.isInteger(n) && n >= 1 && n <= choices.length ? choices[n - 1]!.value : undefined;
|
|
256
|
+
},
|
|
257
|
+
async input(title, o) {
|
|
258
|
+
if (!opts.interactive) return undefined;
|
|
259
|
+
if (o?.signal?.aborted) return undefined;
|
|
260
|
+
const answer = await ask(`${title}${o?.initial ? ` [${o.initial}]` : ""}: `, o?.signal);
|
|
261
|
+
return answer === undefined ? undefined : answer || o?.initial || "";
|
|
262
|
+
},
|
|
263
|
+
clear: () => {},
|
|
264
|
+
async close() {
|
|
265
|
+
rl?.close();
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|