@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,596 @@
|
|
|
1
|
+
// jade/tui 的前端:把会话接到终端界面上。旧 jade 的 main.ts 里属于界面的部分:
|
|
2
|
+
// 输入排队、斜杠命令、esc 中断、换会话后重新显示对话、状态栏。
|
|
3
|
+
//
|
|
4
|
+
// 这里不认识任何具体功能:命令来自 commands@1,快捷键来自 keys@1,工具和事件怎么显示来自 renderers@1,
|
|
5
|
+
// 模式来自 modes@1,模型名来自 models@1。
|
|
6
|
+
|
|
7
|
+
import { execFile } from "node:child_process";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import type { Block } from "uncarved";
|
|
12
|
+
import type { Frontend, OpenOptions, Session, SessionManager, SessionUI, UI } from "../../src/api.ts";
|
|
13
|
+
import type { CommandContext, Commands } from "../commands/index.ts";
|
|
14
|
+
import type { Keys } from "../keys/index.ts";
|
|
15
|
+
import type { Compact } from "../compact/index.ts";
|
|
16
|
+
import type { Models } from "../models/index.ts";
|
|
17
|
+
import type { Modes } from "../modes/index.ts";
|
|
18
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
19
|
+
import { InputHistory } from "./history.ts";
|
|
20
|
+
import { mentionCandidates } from "./mentions.ts";
|
|
21
|
+
import type { SessionIndex } from "../sessions/index.ts";
|
|
22
|
+
import { dim, red, safeScreen, yellow, type CommandInfo, type Screen } from "./screen.ts";
|
|
23
|
+
import { transcript, type Transcript } from "./transcript.ts";
|
|
24
|
+
import { tuiScreen } from "./tui.ts";
|
|
25
|
+
|
|
26
|
+
const USAGE = `usage: jade [prompt] [--resume <session id> | --continue]
|
|
27
|
+
|
|
28
|
+
Starts the terminal interface. With a prompt, sends it first.
|
|
29
|
+
-c, --continue continue the most recent session in this directory
|
|
30
|
+
-r, --resume resume a session by id (inside jade, /resume lists them)
|
|
31
|
+
Run one prompt without the interface: jade -p <prompt>`;
|
|
32
|
+
|
|
33
|
+
/** 前端自己的命令:帮助和退出。其余命令都来自 commands@1 */
|
|
34
|
+
const OWN: CommandInfo[] = [
|
|
35
|
+
{ name: "help", about: "list the commands and keys" },
|
|
36
|
+
{ name: "exit", about: "quit (the session stays resumable)" },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const KEYS =
|
|
40
|
+
"Keys: enter send · \\ or alt+enter new line · ↑↓ history · esc interrupt (twice to also move queued messages back to the input) · ↑ on the first line edits queued messages · ctrl+o expand · ctrl+c twice to exit";
|
|
41
|
+
|
|
42
|
+
/** 第二次 esc 算作"再按一次"的时间窗口 */
|
|
43
|
+
const SECOND_ESC_MS = 3000;
|
|
44
|
+
|
|
45
|
+
const VERSION = (() => {
|
|
46
|
+
try {
|
|
47
|
+
return (JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")) as { version: string }).version;
|
|
48
|
+
} catch {
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
|
|
53
|
+
const errorText = (e: unknown) => {
|
|
54
|
+
// 经过膜的错误包着插件的名字;用户要看的是原因
|
|
55
|
+
const cause = e instanceof Error && e.name === "PluginError" && e.cause instanceof Error ? e.cause : e;
|
|
56
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** 测试用:换掉终端(假的 stdin、stdout)。不设时用 process.stdin、process.stdout */
|
|
60
|
+
export const terminal: { io?: { stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream } } = {};
|
|
61
|
+
|
|
62
|
+
export interface FrontendServices {
|
|
63
|
+
home: string;
|
|
64
|
+
cwd: string;
|
|
65
|
+
commands: Commands;
|
|
66
|
+
keys: Keys;
|
|
67
|
+
renderers: Renderers;
|
|
68
|
+
modes?: Modes;
|
|
69
|
+
models?: Models;
|
|
70
|
+
/** 自动压缩的阈值,状态栏显示 */
|
|
71
|
+
compact?: Compact;
|
|
72
|
+
/** 会话索引(jade/sessions),--continue 用 */
|
|
73
|
+
index?: SessionIndex;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 状态栏上插件的一段最多显示这么多字符(超出的截断,换行并成空格) */
|
|
77
|
+
const MAX_SEGMENT = 80;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* ui@1:界面出来之前的提示先攒着,出来之后显示;界面关掉之后写到 stderr。
|
|
81
|
+
* 前端运行期间才能提问;会话里的插件经 UI.session 拿到自己的状态栏段落和输入框(4.3)
|
|
82
|
+
*/
|
|
83
|
+
export class ScreenBridge {
|
|
84
|
+
screen: Screen | undefined;
|
|
85
|
+
#early: string[] = [];
|
|
86
|
+
/** 会话打开期间的提示:打开之后要清屏,先攒着,清屏之后再显示 */
|
|
87
|
+
#held: string[] | undefined;
|
|
88
|
+
#closed = false;
|
|
89
|
+
/** 状态栏上各插件的一段:会话 id → 插件 id → 文字 */
|
|
90
|
+
#segments = new Map<string, Map<string, string>>();
|
|
91
|
+
/** 界面现在显示哪个会话;换会话时由前端调用 show */
|
|
92
|
+
#session: string | undefined;
|
|
93
|
+
|
|
94
|
+
readonly ui: UI = {
|
|
95
|
+
interactive: true,
|
|
96
|
+
notify: (message, level = "info") => {
|
|
97
|
+
const text = level === "error" ? red(`[error] ${message}`) : level === "warning" ? yellow(`[warning] ${message}`) : dim(message);
|
|
98
|
+
if (this.#held) this.#held.push(text);
|
|
99
|
+
else if (this.screen) this.screen.log(text);
|
|
100
|
+
else if (this.#closed) process.stderr.write(`jade: ${message}\n`);
|
|
101
|
+
else this.#early.push(text);
|
|
102
|
+
},
|
|
103
|
+
select: async (req, signal) => {
|
|
104
|
+
if (!this.screen) return undefined;
|
|
105
|
+
return this.screen.select(req.title, req.options.map((o) => ({ label: o.label, value: o.value, ...(o.hint ? { hint: o.hint } : {}) })), { ...(req.detail ? { body: req.detail } : {}), ...(signal ? { signal } : {}) });
|
|
106
|
+
},
|
|
107
|
+
input: async (req, signal) => {
|
|
108
|
+
if (!this.screen) return undefined;
|
|
109
|
+
return this.screen.input(req.title, { ...(req.initial ? { initial: req.initial } : {}), ...(req.placeholder ? { placeholder: req.placeholder } : {}), ...(req.mask ? { mask: true } : {}), ...(signal ? { signal } : {}) });
|
|
110
|
+
},
|
|
111
|
+
session: ({ session, plugin }) => this.#sessionUI(session, plugin),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
attach(screen: Screen): void {
|
|
115
|
+
this.screen = screen;
|
|
116
|
+
for (const t of this.#early.splice(0)) screen.log(t);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 开始攒提示,直到 release */
|
|
120
|
+
hold(): void {
|
|
121
|
+
this.#held ??= [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 显示攒下的提示,之后的照常显示 */
|
|
125
|
+
release(): void {
|
|
126
|
+
const held = this.#held ?? [];
|
|
127
|
+
this.#held = undefined;
|
|
128
|
+
for (const t of held) {
|
|
129
|
+
if (this.screen) this.screen.log(t);
|
|
130
|
+
else this.#early.push(t);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
detach(): void {
|
|
135
|
+
this.screen = undefined;
|
|
136
|
+
this.#closed = true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 界面换到另一个会话:显示它的插件段落(之前那个会话的留着,换回去还在) */
|
|
140
|
+
show(session: string | undefined): void {
|
|
141
|
+
this.#session = session;
|
|
142
|
+
this.#apply();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#apply(): void {
|
|
146
|
+
if (!this.screen) return;
|
|
147
|
+
const segments = this.#session === undefined ? undefined : this.#segments.get(this.#session);
|
|
148
|
+
this.screen.status({ plugins: [...(segments ?? [])].map(([plugin, text]) => ({ plugin, text })) });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 一个插件在一个会话里的界面。作用域关闭时核心调用 dispose,这里把它的段落去掉 */
|
|
152
|
+
#sessionUI(session: string, plugin: string): SessionUI {
|
|
153
|
+
const bridge = this;
|
|
154
|
+
let gone = false;
|
|
155
|
+
const apply = () => {
|
|
156
|
+
if (bridge.#session === session) bridge.#apply();
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
status(text) {
|
|
160
|
+
if (gone) return;
|
|
161
|
+
// 每次现取:别的插件 dispose 可能已经把空的 map 删了
|
|
162
|
+
let segments = bridge.#segments.get(session);
|
|
163
|
+
if (text === undefined) segments?.delete(plugin);
|
|
164
|
+
else {
|
|
165
|
+
if (!segments) bridge.#segments.set(session, (segments = new Map()));
|
|
166
|
+
segments.set(plugin, clipSegment(text));
|
|
167
|
+
}
|
|
168
|
+
apply();
|
|
169
|
+
},
|
|
170
|
+
draft: {
|
|
171
|
+
text: () => bridge.screen?.draft?.text() ?? "",
|
|
172
|
+
insert: (text) => bridge.screen?.draft?.insert(text),
|
|
173
|
+
set: (text) => bridge.screen?.draft?.set(text),
|
|
174
|
+
hint: (text) => bridge.screen?.draft?.hint(text),
|
|
175
|
+
},
|
|
176
|
+
dispose() {
|
|
177
|
+
gone = true;
|
|
178
|
+
const segments = bridge.#segments.get(session);
|
|
179
|
+
segments?.delete(plugin);
|
|
180
|
+
if (segments?.size === 0) bridge.#segments.delete(session);
|
|
181
|
+
apply();
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 状态栏的一段是一行:空白并成一个空格,长的截断 */
|
|
188
|
+
function clipSegment(text: string): string {
|
|
189
|
+
const one = String(text).replace(/\s+/g, " ").trim();
|
|
190
|
+
return one.length > MAX_SEGMENT ? `${one.slice(0, MAX_SEGMENT - 1)}…` : one;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function tuiFrontend(services: FrontendServices, bridge: ScreenBridge): Frontend {
|
|
194
|
+
return {
|
|
195
|
+
run: (opts) => run(services, bridge, opts),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function parse(argv: readonly string[]): { prompt?: string; resume?: string; cont?: boolean; help?: boolean } | string {
|
|
200
|
+
const out: { prompt?: string; resume?: string; cont?: boolean; help?: boolean } = {};
|
|
201
|
+
const words: string[] = [];
|
|
202
|
+
for (let i = 0; i < argv.length; i++) {
|
|
203
|
+
const a = argv[i]!;
|
|
204
|
+
if (a === "-h" || a === "--help") out.help = true;
|
|
205
|
+
else if (a === "-c" || a === "--continue") out.cont = true;
|
|
206
|
+
else if (a === "--resume" || a === "-r") {
|
|
207
|
+
const id = argv[++i];
|
|
208
|
+
if (!id) return `${a} needs a session id (inside jade, /resume lists the sessions)`;
|
|
209
|
+
out.resume = id;
|
|
210
|
+
} else if (a.startsWith("-")) return `unknown option ${a}`;
|
|
211
|
+
else words.push(a);
|
|
212
|
+
}
|
|
213
|
+
if (out.cont && out.resume) return "--continue and --resume both pick the session; use one of them";
|
|
214
|
+
if (words.length > 0) out.prompt = words.join(" ");
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function run(services: FrontendServices, bridge: ScreenBridge, opts: Parameters<Frontend["run"]>[0]): Promise<number> {
|
|
219
|
+
const args = parse(opts.argv);
|
|
220
|
+
if (typeof args === "string") {
|
|
221
|
+
process.stderr.write(`${args}\n\n${USAGE}\n`);
|
|
222
|
+
return 2;
|
|
223
|
+
}
|
|
224
|
+
if (args.help) {
|
|
225
|
+
process.stdout.write(`${USAGE}\n`);
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
const io = terminal.io;
|
|
229
|
+
if (!(io ?? process).stdin.isTTY || !(io ?? process).stdout.isTTY) {
|
|
230
|
+
process.stderr.write(`jade's terminal interface needs a terminal. To run one prompt, use: jade -p <prompt>\n`);
|
|
231
|
+
return 2;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const { cwd, home, commands, keys, renderers, modes, models, compact, index } = services;
|
|
235
|
+
const sessions = opts.sessions;
|
|
236
|
+
// --continue:这个目录里最近用过的会话(索引由 jade/sessions 在收到第一条消息时写)
|
|
237
|
+
if (args.cont && !index) {
|
|
238
|
+
process.stderr.write("jade: --continue needs the jade/sessions plugin, which is not enabled\n");
|
|
239
|
+
return 2;
|
|
240
|
+
}
|
|
241
|
+
const resumed = args.cont ? (await index!.list(cwd))[0]?.id : args.resume;
|
|
242
|
+
if (args.cont && !resumed) {
|
|
243
|
+
process.stderr.write(`jade: no session to continue in ${cwd}\n`);
|
|
244
|
+
return 2;
|
|
245
|
+
}
|
|
246
|
+
const history = new InputHistory(path.join(home, "history.jsonl"), cwd);
|
|
247
|
+
const [past, branch] = await Promise.all([history.load(), gitBranch(cwd)]);
|
|
248
|
+
|
|
249
|
+
/** 菜单里的命令:前端自己的 + 注册表里的,每次打开菜单时取 */
|
|
250
|
+
const commandList = (): CommandInfo[] => [
|
|
251
|
+
...OWN,
|
|
252
|
+
...commands
|
|
253
|
+
.list()
|
|
254
|
+
.filter((c) => !OWN.some((o) => o.name === c.name))
|
|
255
|
+
.map((c) => ({ name: c.name, ...(c.args ? { args: c.args } : {}), about: `${c.description ?? ""}${c.plugin.startsWith("jade/") ? "" : ` (plugin ${c.plugin})`}`.trim() })),
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
let ready = false;
|
|
259
|
+
const early: string[] = [];
|
|
260
|
+
let finish!: (code: number) => void;
|
|
261
|
+
const finished = new Promise<number>((r) => (finish = r));
|
|
262
|
+
|
|
263
|
+
// 插件、工具输出和模型的文字都经过 safeScreen 显示:只留颜色,去掉能搅乱终端的控制序列
|
|
264
|
+
const screen: Screen = safeScreen(
|
|
265
|
+
tuiScreen({
|
|
266
|
+
status: { cwd: shortPath(cwd), ...(branch ? { branch } : {}), model: models?.current()?.model ?? "(no model)", mode: modes?.current() ?? "" },
|
|
267
|
+
version: VERSION,
|
|
268
|
+
commands: commandList,
|
|
269
|
+
history: past,
|
|
270
|
+
...(io ? { io } : {}),
|
|
271
|
+
handlers: {
|
|
272
|
+
submit: (text) => {
|
|
273
|
+
history.add(text).catch(() => {});
|
|
274
|
+
if (ready) onSubmit(text);
|
|
275
|
+
else early.push(text);
|
|
276
|
+
},
|
|
277
|
+
// 输入 @ 时列工作目录里的文件
|
|
278
|
+
files: () => mentionCandidates(cwd),
|
|
279
|
+
interrupt: () => ready && interrupt(),
|
|
280
|
+
unqueue: () => takeBack(),
|
|
281
|
+
exit: () => void quit(0),
|
|
282
|
+
cycleMode: () => void key("shift+tab"),
|
|
283
|
+
key: (spec) => key(spec),
|
|
284
|
+
},
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
bridge.attach(screen);
|
|
288
|
+
|
|
289
|
+
const keyAbort = new AbortController();
|
|
290
|
+
/** 插件的快捷键:找到了就在后台执行 */
|
|
291
|
+
function key(spec: string): boolean {
|
|
292
|
+
const found = keys.get(spec);
|
|
293
|
+
if (!found) return false;
|
|
294
|
+
const signal = keyAbort.signal;
|
|
295
|
+
Promise.resolve()
|
|
296
|
+
.then(() => found.key.run({ key: spec, signal }))
|
|
297
|
+
.catch((e) => {
|
|
298
|
+
if (!signal.aborted) screen.log(yellow(`[warning] plugin ${found.plugin}: ${spec} failed: ${errorText(e)}`));
|
|
299
|
+
});
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const offMode = modes?.subscribe((mode) => screen.status({ mode }));
|
|
304
|
+
// /model 换了配置:状态栏跟着换
|
|
305
|
+
const offModel = models?.subscribe?.(() => screen.status({ model: models.current()?.model ?? "(no model)" }));
|
|
306
|
+
|
|
307
|
+
// ---------- 会话 ----------
|
|
308
|
+
|
|
309
|
+
let current: Session | undefined;
|
|
310
|
+
let unsubscribe: (() => void) | undefined;
|
|
311
|
+
const canRetry = () => current?.state.phase === "idle" && current.state.messages.at(-1)?.role === "user";
|
|
312
|
+
|
|
313
|
+
const view: Transcript = transcript({
|
|
314
|
+
screen,
|
|
315
|
+
cwd,
|
|
316
|
+
contextWindow: () => models?.current()?.contextWindow ?? 0,
|
|
317
|
+
...(compact ? { compactAt: () => compact.threshold() } : {}),
|
|
318
|
+
canRetry,
|
|
319
|
+
renderer: (tool) => renderers.forTool(tool),
|
|
320
|
+
eventRenderer: (type) => renderers.forEvent(type),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
/** 换成另一个会话:清屏,恢复的会话重新显示之前的对话,然后继续日志里没做完的工作 */
|
|
324
|
+
async function adopt(s: Session, resumed: boolean): Promise<void> {
|
|
325
|
+
unsubscribe?.();
|
|
326
|
+
current = s;
|
|
327
|
+
view.reset();
|
|
328
|
+
screen.clear();
|
|
329
|
+
screen.status({ session: s.id, context: undefined, cache: undefined, spent: undefined, speed: undefined });
|
|
330
|
+
// 状态栏上插件自己的段落跟着换:新会话的显示出来,旧会话的留着(换回去还在)
|
|
331
|
+
bridge.show(s.id);
|
|
332
|
+
if (resumed) await view.replay(await s.history(), true);
|
|
333
|
+
bridge.release();
|
|
334
|
+
unsubscribe = s.subscribe(view.onNotice);
|
|
335
|
+
if (!resumed) return;
|
|
336
|
+
const held = s.state.held;
|
|
337
|
+
if (held) {
|
|
338
|
+
screen.log(yellow(`Plugins changed since this session was last used: ${held.plugins.map((p) => `${p.id}@${p.version}`).join(", ")}`));
|
|
339
|
+
if (held.resolution === "abort") {
|
|
340
|
+
screen.log(red("A plugin requires this session to stop. Restore the previous plugins to continue it, or /clear to start a new one."));
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const ok =
|
|
344
|
+
(await screen.select("Continue with the new plugins? Pending tool calls will be re-checked.", [
|
|
345
|
+
{ label: "Yes, continue", value: true },
|
|
346
|
+
{ label: "No", value: false },
|
|
347
|
+
])) ?? false;
|
|
348
|
+
await guarded(() => s.send({ kind: "plugins", approved: ok }));
|
|
349
|
+
if (!ok) return;
|
|
350
|
+
}
|
|
351
|
+
await guarded(() => s.recover());
|
|
352
|
+
screen.log(dim(`Resumed ${s.id} (${s.state.messages.length} messages)`));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** 交给命令的会话管理器:换会话之后界面跟着换 */
|
|
356
|
+
const manager: SessionManager = {
|
|
357
|
+
get current() {
|
|
358
|
+
return sessions.current;
|
|
359
|
+
},
|
|
360
|
+
async open(o?: OpenOptions) {
|
|
361
|
+
// 插件在会话打开时发的提示(例如读了哪些指令文件)留到 adopt 清屏之后再显示
|
|
362
|
+
bridge.hold();
|
|
363
|
+
try {
|
|
364
|
+
const s = await sessions.open(o);
|
|
365
|
+
await adopt(s, o?.resume !== undefined);
|
|
366
|
+
return s;
|
|
367
|
+
} finally {
|
|
368
|
+
bridge.release();
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
async close() {
|
|
372
|
+
unsubscribe?.();
|
|
373
|
+
unsubscribe = undefined;
|
|
374
|
+
current = undefined;
|
|
375
|
+
bridge.show(undefined);
|
|
376
|
+
await sessions.close();
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// ---------- 运行 ----------
|
|
381
|
+
|
|
382
|
+
/** 等待执行的输入:运行中提交的消息和需要独占会话的命令排在这里,按顺序执行 */
|
|
383
|
+
const queue: string[] = [];
|
|
384
|
+
let running = false;
|
|
385
|
+
let work: Promise<void> | undefined;
|
|
386
|
+
let interruptedAt = 0;
|
|
387
|
+
let interruptPending = false;
|
|
388
|
+
const commandAborts = new Set<AbortController>();
|
|
389
|
+
let quitting = false;
|
|
390
|
+
|
|
391
|
+
async function occupy(fn: () => Promise<void>) {
|
|
392
|
+
running = true;
|
|
393
|
+
work = fn();
|
|
394
|
+
try {
|
|
395
|
+
await work;
|
|
396
|
+
} finally {
|
|
397
|
+
running = false;
|
|
398
|
+
work = undefined;
|
|
399
|
+
interruptPending = false;
|
|
400
|
+
screen.busy(undefined);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** guarded 里这一轮已经由 turn.ended 显示过原因的错误不再重复显示 */
|
|
405
|
+
async function guarded(fn: () => Promise<void>) {
|
|
406
|
+
view.turnFailed = false;
|
|
407
|
+
view.lastError = undefined;
|
|
408
|
+
try {
|
|
409
|
+
await fn();
|
|
410
|
+
} catch (e) {
|
|
411
|
+
if (!view.turnFailed) screen.log(red(`[error] ${errorText(e)}`));
|
|
412
|
+
} finally {
|
|
413
|
+
screen.endStream();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function interrupt() {
|
|
418
|
+
if (!running) return;
|
|
419
|
+
const again = interruptPending || Date.now() - interruptedAt < SECOND_ESC_MS;
|
|
420
|
+
interruptedAt = Date.now();
|
|
421
|
+
interruptPending = true;
|
|
422
|
+
for (const ac of commandAborts) ac.abort(new Error("interrupted by user"));
|
|
423
|
+
current?.abort(new Error("interrupted by user"));
|
|
424
|
+
screen.busy("Interrupting");
|
|
425
|
+
if (queue.length === 0) return;
|
|
426
|
+
const n = queue.length;
|
|
427
|
+
if (again) {
|
|
428
|
+
if (takeBack()) screen.log(dim(`[moved ${n} queued message${n > 1 ? "s" : ""} back to the input]`));
|
|
429
|
+
else {
|
|
430
|
+
queue.length = 0;
|
|
431
|
+
screen.queued(queue);
|
|
432
|
+
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
433
|
+
}
|
|
434
|
+
} else {
|
|
435
|
+
screen.log(dim(`[${n} queued message${n > 1 ? "s" : ""} will still be sent; press esc again to edit ${n > 1 ? "them" : "it"}]`));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** 排队的消息不再自动发送,放回输入框(在已经输入的内容前面,一条一行);没有可放回的返回 false */
|
|
440
|
+
function takeBack(): boolean {
|
|
441
|
+
if (queue.length === 0 || !screen.draft) return false;
|
|
442
|
+
const typed = screen.draft.text();
|
|
443
|
+
screen.draft.set([...queue.splice(0), ...(typed.trim() ? [typed] : [])].join("\n"));
|
|
444
|
+
screen.queued(queue);
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async function quit(code: number) {
|
|
449
|
+
if (quitting) return;
|
|
450
|
+
quitting = true;
|
|
451
|
+
if (running) current?.abort(new Error("exiting"));
|
|
452
|
+
await work?.catch(() => {});
|
|
453
|
+
keyAbort.abort(new Error("exiting"));
|
|
454
|
+
offMode?.();
|
|
455
|
+
offModel?.();
|
|
456
|
+
unsubscribe?.();
|
|
457
|
+
await screen.close();
|
|
458
|
+
bridge.detach();
|
|
459
|
+
// 退出备用屏幕之后,里面的内容就看不到了:告诉用户怎么回来
|
|
460
|
+
if (current && current.state.messages.length > 0) (io?.stdout ?? process.stdout).write(`${dim(`Resume with: jade --resume ${current.id}`)}\n`);
|
|
461
|
+
finish(code);
|
|
462
|
+
}
|
|
463
|
+
opts.signal.addEventListener("abort", () => void quit(130), { once: true });
|
|
464
|
+
|
|
465
|
+
/** 发一条用户消息,跑到会话空闲(包括插件排在这一轮之后的工作) */
|
|
466
|
+
async function say(text: string) {
|
|
467
|
+
const s = current;
|
|
468
|
+
if (!s) return screen.log(red("[error] there is no open session. Use /clear or /resume to continue."));
|
|
469
|
+
screen.busy(undefined);
|
|
470
|
+
screen.busy("Working");
|
|
471
|
+
const content: Block[] = [{ kind: "text", text, origin: { by: "user" }, trust: "trusted" }];
|
|
472
|
+
await guarded(async () => {
|
|
473
|
+
await s.send({ kind: "message", content });
|
|
474
|
+
await s.idle();
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const instant = (name: string) => OWN.some((o) => o.name === name) || name === "quit" || commands.get(name)?.command.instant === true;
|
|
479
|
+
|
|
480
|
+
/** 执行一条命令;返回 false 表示退出 */
|
|
481
|
+
async function command(input: string): Promise<boolean> {
|
|
482
|
+
const name = input.slice(1).split(/\s+/)[0]!.toLowerCase();
|
|
483
|
+
const rest = input.slice(1).replace(/^\S+\s*/, "");
|
|
484
|
+
if (name === "exit" || name === "quit") return false;
|
|
485
|
+
if (name === "help") {
|
|
486
|
+
const all = commandList();
|
|
487
|
+
const width = Math.max(...all.map((c) => `/${c.name}${c.args ? ` ${c.args}` : ""}`.length)) + 2;
|
|
488
|
+
screen.log(all.map((c) => `${`/${c.name}${c.args ? ` ${c.args}` : ""}`.padEnd(width)}${dim(c.about)}`).join("\n"));
|
|
489
|
+
screen.log(dim(KEYS));
|
|
490
|
+
const pluginKeys = keys.list();
|
|
491
|
+
if (pluginKeys.length > 0) screen.log(dim(`Plugin keys: ${pluginKeys.map((k) => `${k.key}${k.description ? ` ${k.description}` : ""} (${k.plugin})`).join(" · ")}`));
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
const found = commands.get(name);
|
|
495
|
+
if (!found) {
|
|
496
|
+
screen.log(red(`unknown command /${name}; /help lists the commands`));
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
const ac = new AbortController();
|
|
500
|
+
commandAborts.add(ac);
|
|
501
|
+
const ctx: CommandContext = { signal: ac.signal, ui: bridge.ui, sessions: manager, print: (t) => screen.log(t) };
|
|
502
|
+
try {
|
|
503
|
+
const result = await found.command.run(rest, ctx);
|
|
504
|
+
if (typeof result === "string") screen.log(result);
|
|
505
|
+
else if (result && typeof result === "object" && typeof result.prompt === "string") {
|
|
506
|
+
if (found.command.instant) screen.log(yellow(`[warning] /${name} is instant and cannot send a prompt`));
|
|
507
|
+
else await say(result.prompt);
|
|
508
|
+
}
|
|
509
|
+
} catch (e) {
|
|
510
|
+
if (!ac.signal.aborted) screen.log(red(`[error] /${name}: ${errorText(e)}`));
|
|
511
|
+
} finally {
|
|
512
|
+
commandAborts.delete(ac);
|
|
513
|
+
}
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function onSubmit(text: string) {
|
|
518
|
+
if (quitting) return;
|
|
519
|
+
const name = text.startsWith("/") ? text.slice(1).split(/\s+/)[0]!.toLowerCase() : undefined;
|
|
520
|
+
if (name !== undefined && instant(name)) {
|
|
521
|
+
// 不碰会话的命令运行中也直接执行
|
|
522
|
+
view.echo(text);
|
|
523
|
+
void command(text).then((go) => {
|
|
524
|
+
if (!go) void quit(0);
|
|
525
|
+
});
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
queue.push(text);
|
|
529
|
+
if (running) screen.queued(queue);
|
|
530
|
+
void pump();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function pump() {
|
|
534
|
+
if (running) return;
|
|
535
|
+
let exit = false;
|
|
536
|
+
await occupy(async () => {
|
|
537
|
+
while (queue.length > 0 && !quitting) {
|
|
538
|
+
if (interruptPending) interruptedAt = Date.now();
|
|
539
|
+
interruptPending = false;
|
|
540
|
+
const text = queue.shift()!;
|
|
541
|
+
screen.queued(queue);
|
|
542
|
+
view.echo(text);
|
|
543
|
+
if (text.startsWith("/")) {
|
|
544
|
+
if (!(await command(text))) {
|
|
545
|
+
exit = true;
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
} else await say(text);
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
if (exit) await quit(0);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// ---------- 开始 ----------
|
|
555
|
+
|
|
556
|
+
try {
|
|
557
|
+
let opened = false;
|
|
558
|
+
await occupy(async () => {
|
|
559
|
+
try {
|
|
560
|
+
await manager.open(resumed ? { resume: resumed } : {});
|
|
561
|
+
opened = true;
|
|
562
|
+
} catch (e) {
|
|
563
|
+
screen.log(red(`[error] ${resumed ? `could not resume ${resumed}` : "could not start a session"}: ${errorText(e)}`));
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
if (!opened && resumed) {
|
|
567
|
+
// 恢复不了:新开一个,免得界面里没有会话
|
|
568
|
+
screen.log(dim("Starting a new session instead."));
|
|
569
|
+
await occupy(async () => {
|
|
570
|
+
await manager.open().catch((e) => screen.log(red(`[error] could not start a session: ${errorText(e)}`)));
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
} catch (e) {
|
|
574
|
+
screen.log(red(`[error] ${errorText(e)}`));
|
|
575
|
+
}
|
|
576
|
+
if (!models?.current()) screen.log(yellow(`No model is configured yet. Put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${path.join(home, ".env")}, then restart jade.`));
|
|
577
|
+
else if (!resumed) screen.log(dim("Type / for commands, esc to interrupt."));
|
|
578
|
+
ready = true;
|
|
579
|
+
for (const text of [...(args.prompt ? [args.prompt] : []), ...early.splice(0)]) onSubmit(text);
|
|
580
|
+
|
|
581
|
+
return finished;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** 当前分支;不在仓库里或没有 git 时 undefined */
|
|
585
|
+
function gitBranch(cwd: string): Promise<string | undefined> {
|
|
586
|
+
return new Promise((resolve) => {
|
|
587
|
+
execFile("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd, timeout: 2000, windowsHide: true }, (err, stdout) => resolve(err ? undefined : stdout.trim() || undefined));
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** 家目录下的路径写成 ~/… */
|
|
592
|
+
function shortPath(p: string): string {
|
|
593
|
+
const home = os.homedir();
|
|
594
|
+
const rel = path.relative(home, p);
|
|
595
|
+
return rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? `~${path.sep}${rel}` : p === home ? "~" : p;
|
|
596
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// jade/tui:终端界面,默认的前端。提供 frontend@1 和 ui@1;命令、快捷键、显示方式都来自注册表插件。
|
|
2
|
+
|
|
3
|
+
import { definePlugin } from "../../src/api.ts";
|
|
4
|
+
import type { Commands } from "../commands/index.ts";
|
|
5
|
+
import type { Compact } from "../compact/index.ts";
|
|
6
|
+
import type { Keys } from "../keys/index.ts";
|
|
7
|
+
import type { Models } from "../models/index.ts";
|
|
8
|
+
import type { Modes } from "../modes/index.ts";
|
|
9
|
+
import type { Renderers } from "../renderers/index.ts";
|
|
10
|
+
import type { SessionIndex } from "../sessions/index.ts";
|
|
11
|
+
import { ScreenBridge, tuiFrontend } from "./frontend.ts";
|
|
12
|
+
|
|
13
|
+
export default definePlugin({
|
|
14
|
+
process(ctx) {
|
|
15
|
+
const bridge = new ScreenBridge();
|
|
16
|
+
ctx.provide("ui@1", bridge.ui);
|
|
17
|
+
ctx.provide(
|
|
18
|
+
"frontend@1",
|
|
19
|
+
tuiFrontend(
|
|
20
|
+
{
|
|
21
|
+
home: ctx.home,
|
|
22
|
+
cwd: ctx.cwd,
|
|
23
|
+
commands: ctx.require<Commands>("commands@1"),
|
|
24
|
+
keys: ctx.require<Keys>("keys@1"),
|
|
25
|
+
renderers: ctx.require<Renderers>("renderers@1"),
|
|
26
|
+
...optional("modes", ctx.service<Modes>("modes@1")),
|
|
27
|
+
...optional("models", ctx.service<Models>("models@1")),
|
|
28
|
+
...optional("compact", ctx.service<Compact>("compact@1")),
|
|
29
|
+
...optional("index", ctx.service<SessionIndex>("sessions@1")),
|
|
30
|
+
},
|
|
31
|
+
bridge,
|
|
32
|
+
),
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const optional = <K extends string, T>(k: K, v: T | undefined) => (v === undefined ? {} : ({ [k]: v } as { [P in K]: T }));
|