@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
|
@@ -30,7 +30,8 @@ export function markdownStream() {
|
|
|
30
30
|
fence = undefined;
|
|
31
31
|
return [];
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
// 左边一条竖线和正文分开;长行折行后续行也带竖线
|
|
34
|
+
return [hanging(dim("┃ "), line, dim("┃ "))];
|
|
34
35
|
}
|
|
35
36
|
if (table) {
|
|
36
37
|
if (line.includes("|") && line.trim()) {
|
|
@@ -54,7 +55,7 @@ export function markdownStream() {
|
|
|
54
55
|
const m = line.match(FENCE);
|
|
55
56
|
if (m) {
|
|
56
57
|
fence = m[1];
|
|
57
|
-
return m[2] ? [dim(
|
|
58
|
+
return m[2] ? [dim(`┃ ${m[2]}`)] : [];
|
|
58
59
|
}
|
|
59
60
|
if (splitCells(line).length > 1) {
|
|
60
61
|
candidate = line;
|
|
@@ -89,34 +90,71 @@ function renderLine(line) {
|
|
|
89
90
|
}
|
|
90
91
|
const quote = line.match(/^\s*>\s?(.*)$/);
|
|
91
92
|
if (quote)
|
|
92
|
-
return dim("│ ")
|
|
93
|
+
return hanging(dim("│ "), inline(quote[1]), dim("│ "));
|
|
93
94
|
const item = line.match(/^(\s*)([-*+])\s+(\[[ xX]\]\s+)?(.*)$/);
|
|
95
|
+
// 列表项折行时续行和正文对齐
|
|
94
96
|
if (item) {
|
|
95
97
|
const box = item[3] ? (item[3][1] === " " ? "☐ " : "☑ ") : "";
|
|
96
|
-
return `${item[1]}${box ? "" : cyan("• ")}${box}
|
|
98
|
+
return hanging(`${item[1]}${box ? "" : cyan("• ")}${box}`, inline(item[4]));
|
|
97
99
|
}
|
|
98
100
|
const ordered = line.match(/^(\s*)(\d+[.)])\s+(.*)$/);
|
|
99
101
|
if (ordered)
|
|
100
|
-
return `${ordered[1]}${cyan(ordered[2])}
|
|
102
|
+
return hanging(`${ordered[1]}${cyan(ordered[2])} `, inline(ordered[3]));
|
|
101
103
|
return inline(line);
|
|
102
104
|
}
|
|
103
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* 行内格式:`代码`、**粗体**、*斜体*、~~删除线~~、[链接](url)、反斜杠转义。代码里的内容原样显示。
|
|
107
|
+
* 行内代码先换成占位符,粗体、斜体处理完再换回来,"**用 `foo()` 实现**" 这样跨过代码的粗体才认得出
|
|
108
|
+
*/
|
|
104
109
|
export function inline(text) {
|
|
110
|
+
const codes = [];
|
|
105
111
|
return text
|
|
106
|
-
.
|
|
107
|
-
.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
.replace(/`+[^`]*?`+/g, (code) => `\x01${codes.push(cyan(code.replace(/^`+ ?|`+$/g, "").replace(/ $/, ""))) - 1}\x01`)
|
|
113
|
+
.replace(/\\([\\`*_{}[\]()#+\-.!|~<>])/g, (_, c) => `\0${c.charCodeAt(0)}\0`)
|
|
114
|
+
.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label, url) => label === url ? underline(url) : `${underline(label)}${dim(` (${url})`)}`)
|
|
115
|
+
.replace(/<(https?:\/\/[^>\s]+)>/g, (_, url) => underline(url))
|
|
116
|
+
.replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*|(?<![\w])__(?=\S)(.+?)(?<=\S)__(?![\w])/g, (_, a, b) => bold(a ?? b))
|
|
117
|
+
.replace(/(?<![*\w])\*(?=[^\s*])(.+?)(?<=[^\s*])\*(?![*\w])|(?<![_\w])_(?=[^\s_])(.+?)(?<=[^\s_])_(?![_\w])/g, (_, a, b) => italic(a ?? b))
|
|
118
|
+
.replace(/~~(?=\S)(.+?)(?<=\S)~~/g, (_, s) => strike(s))
|
|
119
|
+
.replace(/\0(\d+)\0/g, (_, c) => String.fromCharCode(Number(c)))
|
|
120
|
+
.replace(/\x01(\d+)\x01/g, (_, i) => codes[Number(i)]);
|
|
121
|
+
}
|
|
122
|
+
/** 制表符换成空格,按 stop 列一个制表位对齐(按显示宽度算,忽略颜色)。终端按 8 列显示 tab,按 0 列算宽度会对不齐、超宽 */
|
|
123
|
+
export function expandTabs(text, stop = 4) {
|
|
124
|
+
if (!text.includes("\t"))
|
|
125
|
+
return text;
|
|
126
|
+
return text
|
|
127
|
+
.split("\n")
|
|
128
|
+
.map((line) => {
|
|
129
|
+
if (!line.includes("\t"))
|
|
130
|
+
return line;
|
|
131
|
+
let out = "";
|
|
132
|
+
let col = 0;
|
|
133
|
+
const parts = line.split("\t");
|
|
134
|
+
parts.forEach((p, i) => {
|
|
135
|
+
out += p;
|
|
136
|
+
col += stringWidth(p);
|
|
137
|
+
if (i < parts.length - 1) {
|
|
138
|
+
const n = stop - (col % stop);
|
|
139
|
+
out += " ".repeat(n);
|
|
140
|
+
col += n;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return out;
|
|
118
144
|
})
|
|
119
|
-
.join("");
|
|
145
|
+
.join("\n");
|
|
146
|
+
}
|
|
147
|
+
/** 折行时续行缩进到 head 之后(列表项的正文、代码块的竖线);rest 是续行开头显示的内容,宽度和 head 相同 */
|
|
148
|
+
function hanging(head, body, rest = " ".repeat(stringWidth(head))) {
|
|
149
|
+
return {
|
|
150
|
+
render: (width) => {
|
|
151
|
+
const room = Math.max(1, width - stringWidth(head));
|
|
152
|
+
return wrapAnsi(expandTabs(body), room, { hard: true, trim: false })
|
|
153
|
+
.split("\n")
|
|
154
|
+
// 折行处留下的行尾空格看不见,去掉免得多占一列
|
|
155
|
+
.map((r, i) => (i === 0 ? head : rest) + r.trimEnd());
|
|
156
|
+
},
|
|
157
|
+
};
|
|
120
158
|
}
|
|
121
159
|
/** 按没转义、不在行内代码里的 | 拆开一行 */
|
|
122
160
|
function splitCells(line) {
|
|
@@ -201,4 +239,3 @@ function renderTable(rows, align, width) {
|
|
|
201
239
|
const [head, ...body] = grid;
|
|
202
240
|
return [border("┌", "┬", "┐"), ...row(head), border("├", "┼", "┤"), ...body.flatMap(row), border("└", "┴", "┘")];
|
|
203
241
|
}
|
|
204
|
-
//# sourceMappingURL=markdown.js.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @ 引用文件的补全:输入框里打 @ 时找出光标处正在输入的路径、给候选路径排序。
|
|
2
|
+
// 候选从工作目录来(git 仓库里只看版本控制的,和 glob 一样),发送时附上文件内容由 jade/mentions 负责。
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { readdir } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
/** 候选(文件加目录)最多这么多,再多也只是拖慢补全 */
|
|
7
|
+
const MAX_CANDIDATES = 20_000;
|
|
8
|
+
/** 光标正在输入的 @ 引用:@ 的下标和已经输入的部分;不在引用里时返回 undefined */
|
|
9
|
+
export function mentionAt(value, cursor) {
|
|
10
|
+
const m = /(?:^|\s)@("?[^\s"]*)$/.exec(value.slice(0, cursor));
|
|
11
|
+
if (!m)
|
|
12
|
+
return undefined;
|
|
13
|
+
return { start: cursor - m[1].length - 1, query: m[1].replace(/^"/, "") };
|
|
14
|
+
}
|
|
15
|
+
/** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
|
|
16
|
+
export const mentionText = (p) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
|
|
17
|
+
/**
|
|
18
|
+
* 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
|
|
19
|
+
* 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
|
|
20
|
+
*/
|
|
21
|
+
export function rankPaths(paths, query, limit) {
|
|
22
|
+
const q = query.toLowerCase().replaceAll("\\", "/");
|
|
23
|
+
if (!q)
|
|
24
|
+
return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
|
|
25
|
+
const scored = [];
|
|
26
|
+
for (const p of paths) {
|
|
27
|
+
const lower = p.toLowerCase();
|
|
28
|
+
const base = lower.replace(/\/$/, "").split("/").pop();
|
|
29
|
+
let score;
|
|
30
|
+
if (lower.startsWith(q))
|
|
31
|
+
score = 0;
|
|
32
|
+
else if (base.startsWith(q))
|
|
33
|
+
score = 1;
|
|
34
|
+
else if (base.includes(q))
|
|
35
|
+
score = 2;
|
|
36
|
+
else if (lower.includes(q))
|
|
37
|
+
score = 3;
|
|
38
|
+
else if (subsequence(lower, q))
|
|
39
|
+
score = 4;
|
|
40
|
+
else
|
|
41
|
+
continue;
|
|
42
|
+
scored.push({ p, score });
|
|
43
|
+
}
|
|
44
|
+
scored.sort((a, b) => a.score - b.score || a.p.length - b.p.length || a.p.localeCompare(b.p));
|
|
45
|
+
return scored.slice(0, limit).map((s) => s.p);
|
|
46
|
+
}
|
|
47
|
+
function subsequence(text, q) {
|
|
48
|
+
let i = 0;
|
|
49
|
+
for (const ch of text)
|
|
50
|
+
if (ch === q[i] && ++i === q.length)
|
|
51
|
+
return true;
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @ 补全的候选:工作目录里的文件(git 仓库里只看版本控制的)和它们所在的目录,目录以 / 结尾。
|
|
56
|
+
* 5 秒还没列完就先给已经找到的(大仓库第一次列得慢,不该让输入框卡住)
|
|
57
|
+
*/
|
|
58
|
+
export async function mentionCandidates(cwd, signal = AbortSignal.timeout(5000)) {
|
|
59
|
+
const files = [];
|
|
60
|
+
const dirs = new Set();
|
|
61
|
+
try {
|
|
62
|
+
for await (const full of await listFiles(cwd, signal)) {
|
|
63
|
+
const rel = display(cwd, full);
|
|
64
|
+
files.push(rel);
|
|
65
|
+
for (let i = rel.lastIndexOf("/"); i > 0; i = rel.lastIndexOf("/", i - 1)) {
|
|
66
|
+
const dir = rel.slice(0, i + 1);
|
|
67
|
+
if (dirs.has(dir))
|
|
68
|
+
break;
|
|
69
|
+
dirs.add(dir);
|
|
70
|
+
}
|
|
71
|
+
if (files.length + dirs.size >= MAX_CANDIDATES)
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// 列到一半被中止(超时):用已经找到的
|
|
77
|
+
}
|
|
78
|
+
return [...dirs, ...files];
|
|
79
|
+
}
|
|
80
|
+
// 以下和 jade/fs 的 listFiles、display 相同:插件之间不共享代码(只能共享类型),所以这里留一份
|
|
81
|
+
const SKIP_DIRS = new Set([".git", "node_modules"]);
|
|
82
|
+
/** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
|
|
83
|
+
function display(cwd, full) {
|
|
84
|
+
const rel = path.relative(cwd, full);
|
|
85
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
86
|
+
return shown.split(path.sep).join("/");
|
|
87
|
+
}
|
|
88
|
+
/** git 仓库里的文件(版本控制的和没忽略的新文件);不是仓库、没有 git 时 undefined */
|
|
89
|
+
function gitFiles(root) {
|
|
90
|
+
return new Promise((resolve) => {
|
|
91
|
+
execFile("git", ["ls-files", "-z", "--cached", "--others", "--exclude-standard"], { cwd: root, maxBuffer: 256 * 1024 * 1024, windowsHide: true }, (err, out) =>
|
|
92
|
+
// 合并冲突中的文件会出现多次
|
|
93
|
+
resolve(err ? undefined : [...new Set(out.split("\0").filter(Boolean))]));
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async function listFiles(root, signal) {
|
|
97
|
+
const tracked = await gitFiles(root);
|
|
98
|
+
if (!tracked)
|
|
99
|
+
return walk(root, signal);
|
|
100
|
+
return tracked.filter((rel) => !rel.split("/").some((seg) => SKIP_DIRS.has(seg))).map((rel) => path.join(root, rel));
|
|
101
|
+
}
|
|
102
|
+
async function* walk(dir, signal) {
|
|
103
|
+
signal.throwIfAborted();
|
|
104
|
+
const entries = (await readdir(dir, { withFileTypes: true })).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
105
|
+
for (const e of entries) {
|
|
106
|
+
const full = path.join(dir, e.name);
|
|
107
|
+
if (e.isDirectory()) {
|
|
108
|
+
if (!SKIP_DIRS.has(e.name))
|
|
109
|
+
yield* walk(full, signal);
|
|
110
|
+
}
|
|
111
|
+
else if (e.isFile()) {
|
|
112
|
+
yield full;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/tui",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"provides": [
|
|
7
|
+
"frontend@1",
|
|
8
|
+
"ui@1"
|
|
9
|
+
],
|
|
10
|
+
"requires": [
|
|
11
|
+
"commands@1",
|
|
12
|
+
"keys@1",
|
|
13
|
+
"renderers@1"
|
|
14
|
+
],
|
|
15
|
+
"optional": [
|
|
16
|
+
"modes@1",
|
|
17
|
+
"models@1",
|
|
18
|
+
"compact@1",
|
|
19
|
+
"sessions@1"
|
|
20
|
+
],
|
|
21
|
+
"frontend": {
|
|
22
|
+
"name": "tui",
|
|
23
|
+
"default": true
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// 宿主显示用的纯函数:工具调用的默认摘要和预览、折叠的工具结果。不依赖运行时和界面状态。
|
|
2
|
+
// 各个工具自己的显示由插件经 renderers@1 登记。
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { dim } from "./screen.js";
|
|
6
|
+
/** 显示用的路径:工作目录里的用相对路径,其余用绝对路径;都用正斜杠。和 jade/fs 的 display 相同,插件之间不 import 值,所以这里留一份 */
|
|
7
|
+
function display(cwd, full) {
|
|
8
|
+
const rel = path.relative(cwd, full);
|
|
9
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
10
|
+
return shown.split(path.sep).join("/");
|
|
11
|
+
}
|
|
12
|
+
export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
13
|
+
/**
|
|
14
|
+
* 带颜色(SGR 序列)的文字截到 n 个可见字符:序列不算长度、不会被切开;截断时在最后复位颜色,
|
|
15
|
+
* 不让没关上的颜色渗到后面的行
|
|
16
|
+
*/
|
|
17
|
+
export function clipStyled(s, n) {
|
|
18
|
+
const sgr = /\x1b\[[0-9;:]*m/y;
|
|
19
|
+
let out = "";
|
|
20
|
+
let visible = 0;
|
|
21
|
+
let styled = false;
|
|
22
|
+
for (let i = 0; i < s.length;) {
|
|
23
|
+
sgr.lastIndex = i;
|
|
24
|
+
const m = sgr.exec(s);
|
|
25
|
+
if (m) {
|
|
26
|
+
out += m[0];
|
|
27
|
+
styled = true;
|
|
28
|
+
i += m[0].length;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (visible === n)
|
|
32
|
+
return out + "…" + (styled ? "\x1b[0m" : "");
|
|
33
|
+
out += s[i];
|
|
34
|
+
visible++;
|
|
35
|
+
i++;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** 家目录下的路径显示成 ~/… */
|
|
40
|
+
export function shortPath(p) {
|
|
41
|
+
const rel = path.relative(os.homedir(), p);
|
|
42
|
+
const shown = rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? `~/${rel}` : rel === "" ? "~" : p;
|
|
43
|
+
return shown.split(path.sep).join("/");
|
|
44
|
+
}
|
|
45
|
+
/** 工具调用默认的一行摘要,显示在工具名后面的括号里:path 参数,没有时是参数的 JSON */
|
|
46
|
+
export function summarize(cwd, input) {
|
|
47
|
+
const i = (input ?? {});
|
|
48
|
+
const where = typeof i.path === "string" ? display(cwd, i.path) : "";
|
|
49
|
+
return where || clip(JSON.stringify(input), 120);
|
|
50
|
+
}
|
|
51
|
+
/** 审批时默认的预览:参数的 JSON */
|
|
52
|
+
export function preview(input) {
|
|
53
|
+
return dim(clip(JSON.stringify(input, null, 2), 2000));
|
|
54
|
+
}
|
|
55
|
+
/** 工具结果展开后的内容:缩进的全部行,超过 max 行时截断 */
|
|
56
|
+
export function detailsOf(lines, color, max) {
|
|
57
|
+
const body = lines.slice(0, max).map((l) => color(` ${l}`));
|
|
58
|
+
if (lines.length > max)
|
|
59
|
+
body.push(dim(` … ${lines.length - max} more lines`));
|
|
60
|
+
return body.join("\n");
|
|
61
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// 宿主的输出与交互接口。两种实现:
|
|
2
|
+
// - 终端界面(tui.tsx):全屏,标题在顶部,对话在中间自己滚动,输入框、状态栏固定在底部,运行中也能输入
|
|
3
|
+
// - 纯文本(这里):-p 和非交互运行时使用,逐行输出,提问用 readline
|
|
4
|
+
import { createInterface } from "node:readline/promises";
|
|
5
|
+
import { stripVTControlCharacters } from "node:util";
|
|
6
|
+
// ---------- 颜色 ----------
|
|
7
|
+
// 设了 NO_COLOR(非空)时不输出颜色,见 https://no-color.org
|
|
8
|
+
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
9
|
+
/**
|
|
10
|
+
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:粗体里套着行内代码、链接时,后面的文字仍是粗体。
|
|
11
|
+
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
12
|
+
*/
|
|
13
|
+
const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
14
|
+
export const dim = paint("2", "22");
|
|
15
|
+
export const red = paint("31", "39");
|
|
16
|
+
export const green = paint("32", "39");
|
|
17
|
+
export const yellow = paint("33", "39");
|
|
18
|
+
export const cyan = paint("36", "39");
|
|
19
|
+
export const bold = paint("1", "22");
|
|
20
|
+
export const italic = paint("3", "23");
|
|
21
|
+
export const underline = paint("4", "24");
|
|
22
|
+
export const strike = paint("9", "29");
|
|
23
|
+
// ---------- 过滤控制字符 ----------
|
|
24
|
+
/** 转义序列(CSI、OSC、DCS 这类字符串、其余 ESC 开头的)和换行、制表符之外的控制字符(含 C1) */
|
|
25
|
+
const CONTROL = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b[\]P^_X][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b[^[\]P^_X]?|[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
|
|
26
|
+
const SGR = /^\x1b\[[0-9;:]*m$/;
|
|
27
|
+
/** 文字末尾还没写完的转义序列:单独的 ESC、没有结束字节的 CSI、没有结束符的 OSC/DCS 这类字符串 */
|
|
28
|
+
const UNFINISHED = /\x1b(?:\[[0-?]*[ -/]*|[\]P^_X][^\x07\x1b]*)?$/;
|
|
29
|
+
/** 最多为一个没写完的序列留这么多字符 */
|
|
30
|
+
const MAX_HELD = 256;
|
|
31
|
+
/**
|
|
32
|
+
* 去掉可能搅乱终端的东西,只留颜色(SGR)、换行和制表符:插件、工具输出和模型的文字里可能有移动光标、清屏、
|
|
33
|
+
* 改窗口标题、写剪贴板之类的序列。单独的 \r(进度条)按终端的效果处理:每行只留最后一段
|
|
34
|
+
*/
|
|
35
|
+
export function terminalSafe(text) {
|
|
36
|
+
const lines = text.replaceAll("\r\n", "\n").split("\n");
|
|
37
|
+
return lines
|
|
38
|
+
.map((l) => (l.includes("\r") ? (l.split("\r").filter(Boolean).at(-1) ?? "") : l).replace(CONTROL, (m) => (SGR.test(m) ? m : "")))
|
|
39
|
+
.join("\n");
|
|
40
|
+
}
|
|
41
|
+
/** 让显示到屏幕上的每一段文字都经过 terminalSafe */
|
|
42
|
+
export function safeScreen(screen) {
|
|
43
|
+
const s = terminalSafe;
|
|
44
|
+
const opt = (v) => (v === undefined ? undefined : s(v));
|
|
45
|
+
const lf = (t) => t.replace(/\r\n?/g, "\n");
|
|
46
|
+
const plain = (t) => stripVTControlCharacters(s(t));
|
|
47
|
+
/** 流式文字末尾还没写完的转义序列,等下一段 */
|
|
48
|
+
let held;
|
|
49
|
+
return {
|
|
50
|
+
interactive: screen.interactive,
|
|
51
|
+
log: (text, o) => screen.log(s(text), o && { ...o, details: opt(o.details) }),
|
|
52
|
+
amend: (id, text, o) => screen.amend(id, s(text), o && { ...o, details: opt(o.details) }),
|
|
53
|
+
progress: (id, line, o) => screen.progress(id, opt(line), o),
|
|
54
|
+
stream: (kind, text) => {
|
|
55
|
+
// 上一段末尾没完的转义序列接到这一段前面再过滤,不然它被拆成两半,后一半当成文字显示出来
|
|
56
|
+
if (held && held.kind !== kind)
|
|
57
|
+
held = undefined;
|
|
58
|
+
const whole = (held?.text ?? "") + text;
|
|
59
|
+
// 很长还没结束的(OSC 之类的字符串一直不给结束符)不再等,按现在的样子过滤
|
|
60
|
+
const found = whole.search(UNFINISHED);
|
|
61
|
+
const cut = found >= 0 && whole.length - found <= MAX_HELD ? found : -1;
|
|
62
|
+
held = cut >= 0 ? { kind, text: whole.slice(cut) } : undefined;
|
|
63
|
+
const shown = s(cut >= 0 ? whole.slice(0, cut) : whole);
|
|
64
|
+
if (shown)
|
|
65
|
+
screen.stream(kind, shown);
|
|
66
|
+
},
|
|
67
|
+
endStream: () => {
|
|
68
|
+
// 到结束都没写完的序列不完整,丢掉
|
|
69
|
+
held = undefined;
|
|
70
|
+
screen.endStream();
|
|
71
|
+
},
|
|
72
|
+
busy: (label) => screen.busy(opt(label)),
|
|
73
|
+
status: (info) => {
|
|
74
|
+
const out = { ...info };
|
|
75
|
+
for (const k of ["cwd", "branch", "model", "mode", "session"])
|
|
76
|
+
if (typeof info[k] === "string")
|
|
77
|
+
out[k] = s(info[k]);
|
|
78
|
+
if (info.plugins)
|
|
79
|
+
out.plugins = info.plugins.map((p) => ({ plugin: p.plugin, text: s(p.text) }));
|
|
80
|
+
screen.status(out);
|
|
81
|
+
},
|
|
82
|
+
queued: (items) => screen.queued(items.map(s)),
|
|
83
|
+
select: (title, choices, o) => screen.select(s(title), choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint) })), o && { ...o, body: opt(o.body) }),
|
|
84
|
+
input: (title, o) => screen.input(s(title), o && { ...o, initial: opt(o.initial), placeholder: opt(o.placeholder) }),
|
|
85
|
+
// 输入框里的 \r 是换行(旧 Mac 的换行符),不是进度条:先换成 \n,terminalSafe 才不会只留最后一段。
|
|
86
|
+
// 输入框里是要发给模型的纯文字,颜色也去掉
|
|
87
|
+
draft: screen.draft && {
|
|
88
|
+
text: () => screen.draft.text(),
|
|
89
|
+
insert: (t) => screen.draft.insert(plain(lf(t))),
|
|
90
|
+
set: (t) => screen.draft.set(plain(lf(t))),
|
|
91
|
+
hint: (t) => screen.draft.hint(t === undefined ? undefined : plain(lf(t))),
|
|
92
|
+
},
|
|
93
|
+
clear: () => screen.clear(),
|
|
94
|
+
close: () => screen.close(),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
// ---------- 纯文本实现 ----------
|
|
98
|
+
const MAX_OUTPUT_LINES = 20;
|
|
99
|
+
export function plainScreen(opts) {
|
|
100
|
+
let atLineStart = true;
|
|
101
|
+
let streaming;
|
|
102
|
+
let rl;
|
|
103
|
+
/** 每个工具已经打印了多少行输出 */
|
|
104
|
+
const outputLines = new Map();
|
|
105
|
+
const write = (s) => {
|
|
106
|
+
if (!s)
|
|
107
|
+
return;
|
|
108
|
+
process.stdout.write(s);
|
|
109
|
+
atLineStart = s.endsWith("\n");
|
|
110
|
+
};
|
|
111
|
+
const line = (s) => {
|
|
112
|
+
if (!atLineStart)
|
|
113
|
+
write("\n");
|
|
114
|
+
write(s + "\n");
|
|
115
|
+
};
|
|
116
|
+
/** 中止时返回 undefined */
|
|
117
|
+
const ask = async (q, signal) => {
|
|
118
|
+
rl ??= createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
119
|
+
try {
|
|
120
|
+
return (await rl.question(q, { signal })).trim();
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
if (!signal?.aborted)
|
|
124
|
+
throw err;
|
|
125
|
+
write("\n");
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
interactive: opts.interactive,
|
|
131
|
+
log: (text) => {
|
|
132
|
+
streaming = undefined;
|
|
133
|
+
line(text);
|
|
134
|
+
},
|
|
135
|
+
amend: (_id, text) => {
|
|
136
|
+
streaming = undefined;
|
|
137
|
+
line(` ${text}`);
|
|
138
|
+
},
|
|
139
|
+
progress: (id, text, o) => {
|
|
140
|
+
if (text === undefined)
|
|
141
|
+
return void outputLines.delete(id);
|
|
142
|
+
streaming = undefined;
|
|
143
|
+
if (o?.output) {
|
|
144
|
+
const shown = outputLines.get(id) ?? 0;
|
|
145
|
+
if (shown > MAX_OUTPUT_LINES)
|
|
146
|
+
return;
|
|
147
|
+
outputLines.set(id, shown + 1);
|
|
148
|
+
if (shown === MAX_OUTPUT_LINES)
|
|
149
|
+
return line(dim(" │ …"));
|
|
150
|
+
}
|
|
151
|
+
line(text);
|
|
152
|
+
},
|
|
153
|
+
stream: (kind, text) => {
|
|
154
|
+
if (kind !== streaming) {
|
|
155
|
+
// 不少模型的正文以空行开头;整块都是换行时等下一块
|
|
156
|
+
if (kind === "text")
|
|
157
|
+
text = text.replace(/^\n+/, "");
|
|
158
|
+
if (!text)
|
|
159
|
+
return;
|
|
160
|
+
if (kind === "thinking")
|
|
161
|
+
line(dim("thinking…"));
|
|
162
|
+
else if (!atLineStart)
|
|
163
|
+
write("\n");
|
|
164
|
+
streaming = kind;
|
|
165
|
+
}
|
|
166
|
+
write(kind === "thinking" ? dim(text) : text);
|
|
167
|
+
},
|
|
168
|
+
endStream: () => {
|
|
169
|
+
streaming = undefined;
|
|
170
|
+
if (!atLineStart)
|
|
171
|
+
write("\n");
|
|
172
|
+
},
|
|
173
|
+
busy: () => { },
|
|
174
|
+
status: () => { },
|
|
175
|
+
queued: () => { },
|
|
176
|
+
async select(title, choices, o) {
|
|
177
|
+
if (!opts.interactive)
|
|
178
|
+
return undefined;
|
|
179
|
+
line(yellow(title));
|
|
180
|
+
if (o?.body)
|
|
181
|
+
line(o.body);
|
|
182
|
+
choices.forEach((c, i) => line(` ${i + 1}. ${c.label}${c.hint ? dim(` ${c.hint}`) : ""}`));
|
|
183
|
+
if (o?.signal?.aborted)
|
|
184
|
+
return undefined;
|
|
185
|
+
const n = Number(await ask(`Choose 1-${choices.length} (empty to cancel): `, o?.signal));
|
|
186
|
+
return Number.isInteger(n) && n >= 1 && n <= choices.length ? choices[n - 1].value : undefined;
|
|
187
|
+
},
|
|
188
|
+
async input(title, o) {
|
|
189
|
+
if (!opts.interactive)
|
|
190
|
+
return undefined;
|
|
191
|
+
if (o?.signal?.aborted)
|
|
192
|
+
return undefined;
|
|
193
|
+
const answer = await ask(`${title}${o?.initial ? ` [${o.initial}]` : ""}: `, o?.signal);
|
|
194
|
+
return answer === undefined ? undefined : answer || o?.initial || "";
|
|
195
|
+
},
|
|
196
|
+
clear: () => { },
|
|
197
|
+
async close() {
|
|
198
|
+
rl?.close();
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|