@uncarved/jade 0.3.0 → 0.4.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/README.md +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +49 -24
- package/plugins/approval/policy.ts +48 -25
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +55 -12
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +10 -9
- package/plugins/models/index.ts +31 -4
- package/plugins/models/provider.ts +12 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +39 -9
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +231 -0
- package/plugins/screenshot/index.ts +3 -2
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +75 -22
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +93 -34
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +81 -81
- package/plugins/task/plugin.json +5 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +57 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/frontend.ts +161 -87
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mascot.ts +8 -7
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +356 -104
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -414
- package/plugins-dist/approval/policy.js +395 -497
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -244
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -188
- package/plugins-dist/models/provider.js +243 -295
- package/plugins-dist/models/saved.js +59 -71
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +81 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +134 -151
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +169 -0
- package/plugins-dist/screenshot/index.js +37 -45
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +101 -151
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -74
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +317 -320
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -148
- package/plugins-dist/task/plugin.json +5 -4
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +43 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +641 -655
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -353
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1645
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +489 -34
|
@@ -1,34 +1,19 @@
|
|
|
1
|
-
// jade/tui 的前端:把会话接到终端界面上。旧 jade 的 main.ts 里属于界面的部分:
|
|
2
|
-
// 输入排队、斜杠命令、esc 中断、换会话后重新显示对话、状态栏。
|
|
3
|
-
//
|
|
4
|
-
// 这里不认识任何具体功能:命令来自 commands@1,快捷键来自 keys@1,工具和事件怎么显示来自 renderers@1,
|
|
5
|
-
// 模式来自 modes@1,模型名来自 models@1。
|
|
6
1
|
import { execFile } from "node:child_process";
|
|
7
2
|
import { readFileSync } from "node:fs";
|
|
8
3
|
import os from "node:os";
|
|
9
4
|
import path from "node:path";
|
|
10
5
|
import { InputHistory } from "./history.js";
|
|
11
|
-
import {
|
|
12
|
-
import { dim, red, safeScreen, yellow } from "./screen.js";
|
|
6
|
+
import { ansi, dim, red, safeScreen, yellow } from "./screen.js";
|
|
13
7
|
import { transcript } from "./transcript.js";
|
|
14
|
-
/**
|
|
15
|
-
* 界面(ink、React)用到时才加载:--help、参数出错、不在终端里时都不需要它,导入一次要几百毫秒。
|
|
16
|
-
* React 在加载时按 NODE_ENV 选开发版或生产版,开发版做大量检查,流式输出时 CPU 翻倍。
|
|
17
|
-
* 用户没设 NODE_ENV 时只在加载期间设成 production,加载完还原,免得传给之后 shell 工具、MCP 等子进程。
|
|
18
|
-
* ink 依赖的 yoga 有顶层 await,只能异步加载,做不到同步设置再还原:加载期间还没有会话,工具和 MCP 都没启动,
|
|
19
|
-
* 这时可能启动的子进程只有 git(不读它)和后台的 npm 自动升级(全局安装本来就不装 devDependencies)。
|
|
20
|
-
* 编译好的 plugins-dist 里 ink、react 已打包成生产版(vendor.ts),这里设 NODE_ENV 只对直接跑源码(bun)时有用
|
|
21
|
-
*/
|
|
22
8
|
async function loadTui() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
9
|
+
if (true)
|
|
10
|
+
return import("./tui.js");
|
|
11
|
+
process.env.NODE_ENV = "production";
|
|
12
|
+
try {
|
|
13
|
+
return await import("./tui.js");
|
|
14
|
+
} finally {
|
|
15
|
+
delete "development";
|
|
16
|
+
}
|
|
32
17
|
}
|
|
33
18
|
const USAGE = `usage: jade [prompt] [--resume <session id> | --continue]
|
|
34
19
|
|
|
@@ -37,667 +22,668 @@ Starts the terminal interface. With a prompt, sends it first.
|
|
|
37
22
|
-r, --resume resume a session by id (inside jade, /resume lists them)
|
|
38
23
|
Run one prompt without the interface: jade -p <prompt> (jade -p --help for more)
|
|
39
24
|
jade's own commands (plugins, update, --safe): jade help`;
|
|
40
|
-
/** 前端自己的命令:帮助和退出。其余命令都来自 commands@1 */
|
|
41
25
|
const OWN = [
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
{ name: "exit", about: "quit (the session stays resumable)" },
|
|
26
|
+
{ name: "help", about: "list the commands and keys" },
|
|
27
|
+
{ name: "exit", about: "quit (the session stays resumable)" }
|
|
45
28
|
];
|
|
46
|
-
const KEYS =
|
|
47
|
-
|
|
29
|
+
const KEYS = [
|
|
30
|
+
"Keys:",
|
|
31
|
+
" send enter send · ctrl+j, alt+enter or \\ then enter: new line · tab completes /commands",
|
|
32
|
+
" input ↑↓ history · ctrl+r search history · ↑ on the first line edits queued messages · esc twice or ctrl+c clears the input",
|
|
33
|
+
" editing ctrl+←/→ or alt+b/f move by words · ctrl+w or alt+backspace delete a word · ctrl+a/e line start/end · ctrl+u/k delete to start/end",
|
|
34
|
+
" running esc interrupt (twice to also move queued messages back to the input) · ctrl+c twice to exit",
|
|
35
|
+
" transcript pgup/pgdn scroll · ctrl+home/end top/bottom · ctrl+o expands the latest item, a click the one under it · drag to select and copy · ctrl+l redraw"
|
|
36
|
+
].join(`
|
|
37
|
+
`);
|
|
38
|
+
const paint = (v) => v === undefined || v === null ? undefined : ansi(v);
|
|
39
|
+
function ansiTool(r) {
|
|
40
|
+
const out = { tool: r.tool };
|
|
41
|
+
if (r.summary)
|
|
42
|
+
out.summary = (i) => paint(r.summary(i));
|
|
43
|
+
if (r.details)
|
|
44
|
+
out.details = (i) => paint(r.details(i));
|
|
45
|
+
if (r.call)
|
|
46
|
+
out.call = (i) => paint(r.call(i));
|
|
47
|
+
if (r.progress)
|
|
48
|
+
out.progress = (u, c) => paint(r.progress(u, c));
|
|
49
|
+
if (r.preview)
|
|
50
|
+
out.preview = async (i) => paint(await r.preview(i));
|
|
51
|
+
if (r.before)
|
|
52
|
+
out.before = (i) => r.before(i);
|
|
53
|
+
if (r.result)
|
|
54
|
+
out.result = (x) => {
|
|
55
|
+
const o = r.result(x);
|
|
56
|
+
return o ? { line: ansi(o.line), ...o.details !== undefined ? { details: ansi(o.details) } : {} } : o;
|
|
57
|
+
};
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
function ansiEvent(r) {
|
|
61
|
+
return {
|
|
62
|
+
event: r.event,
|
|
63
|
+
render: (data) => {
|
|
64
|
+
const o = r.render(data);
|
|
65
|
+
return o && { line: ansi(o.line), ...o.details !== undefined ? { details: ansi(o.details) } : {} };
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const isPrompt = (r) => typeof r === "object" && r !== null && !Array.isArray(r) && typeof r.prompt === "string";
|
|
70
|
+
const QUIT_WAIT_MS = 3000;
|
|
71
|
+
const LIVE_ROWS = 8;
|
|
48
72
|
const SECOND_ESC_MS = 3000;
|
|
49
73
|
const VERSION = (() => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
74
|
+
try {
|
|
75
|
+
return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
76
|
+
} catch {
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
56
79
|
})();
|
|
57
80
|
const errorText = (e) => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return cause instanceof Error ? cause.message : String(cause);
|
|
81
|
+
const cause = e instanceof Error && e.name === "PluginError" && e.cause instanceof Error ? e.cause : e;
|
|
82
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
61
83
|
};
|
|
62
|
-
/** 测试用:换掉终端(假的 stdin、stdout)。不设时用 process.stdin、process.stdout */
|
|
63
84
|
export const terminal = {};
|
|
64
|
-
/** 状态栏上插件的一段最多显示这么多字符(超出的截断,换行并成空格) */
|
|
65
85
|
const MAX_SEGMENT = 80;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* 前端运行期间才能提问;会话里的插件经 UI.session 拿到自己的状态栏段落和输入框(4.3)
|
|
69
|
-
*/
|
|
86
|
+
const MAX_PANEL_LINES = 10;
|
|
87
|
+
|
|
70
88
|
export class ScreenBridge {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
89
|
+
screen;
|
|
90
|
+
#early = [];
|
|
91
|
+
#held;
|
|
92
|
+
#closed = false;
|
|
93
|
+
#segments = new Map;
|
|
94
|
+
#panels = new Map;
|
|
95
|
+
#session;
|
|
96
|
+
ui = {
|
|
97
|
+
interactive: true,
|
|
98
|
+
notify: (message, level = "info") => {
|
|
99
|
+
const text = level === "error" ? red(`[error] ${message}`) : level === "warning" ? yellow(`[warning] ${message}`) : dim(message);
|
|
100
|
+
if (this.#held)
|
|
101
|
+
this.#held.push(text);
|
|
102
|
+
else if (this.screen)
|
|
103
|
+
this.screen.log(text);
|
|
104
|
+
else if (this.#closed)
|
|
105
|
+
process.stderr.write(`jade: ${message}
|
|
106
|
+
`);
|
|
107
|
+
else
|
|
108
|
+
this.#early.push(text);
|
|
109
|
+
},
|
|
110
|
+
select: async (req, signal) => {
|
|
111
|
+
if (!this.screen)
|
|
112
|
+
return;
|
|
113
|
+
return this.screen.select(req.title, req.options.map((o) => ({ label: o.label, value: o.value, ...o.hint ? { hint: o.hint } : {} })), { ...req.detail ? { body: ansi(req.detail) } : {}, ...signal ? { signal } : {} });
|
|
114
|
+
},
|
|
115
|
+
input: async (req, signal) => {
|
|
116
|
+
if (!this.screen)
|
|
117
|
+
return;
|
|
118
|
+
return this.screen.input(req.title, { ...req.initial ? { initial: req.initial } : {}, ...req.placeholder ? { placeholder: req.placeholder } : {}, ...req.mask ? { mask: true } : {}, ...signal ? { signal } : {} });
|
|
119
|
+
},
|
|
120
|
+
copy: (text) => {
|
|
121
|
+
if (!this.screen?.copy)
|
|
122
|
+
return false;
|
|
123
|
+
this.screen.copy(String(text));
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
suspend: async (fn) => this.screen?.suspend ? this.screen.suspend(fn) : fn(),
|
|
127
|
+
session: ({ session, plugin }) => this.#sessionUI(session, plugin)
|
|
128
|
+
};
|
|
129
|
+
attach(screen) {
|
|
130
|
+
this.screen = screen;
|
|
131
|
+
for (const t of this.#early.splice(0))
|
|
132
|
+
screen.log(t);
|
|
133
|
+
}
|
|
134
|
+
hold() {
|
|
135
|
+
this.#held ??= [];
|
|
136
|
+
}
|
|
137
|
+
release() {
|
|
138
|
+
const held = this.#held ?? [];
|
|
139
|
+
this.#held = undefined;
|
|
140
|
+
for (const t of held) {
|
|
141
|
+
if (this.screen)
|
|
142
|
+
this.screen.log(t);
|
|
143
|
+
else
|
|
144
|
+
this.#early.push(t);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
detach() {
|
|
148
|
+
this.screen = undefined;
|
|
149
|
+
this.#closed = true;
|
|
150
|
+
}
|
|
151
|
+
show(session) {
|
|
152
|
+
this.#session = session;
|
|
153
|
+
this.#apply();
|
|
154
|
+
}
|
|
155
|
+
#apply() {
|
|
156
|
+
if (!this.screen)
|
|
157
|
+
return;
|
|
158
|
+
const segments = this.#session === undefined ? undefined : this.#segments.get(this.#session);
|
|
159
|
+
const panels = this.#session === undefined ? undefined : this.#panels.get(this.#session);
|
|
160
|
+
this.screen.status({
|
|
161
|
+
plugins: [...segments ?? []].map(([plugin, text]) => ({ plugin, text })),
|
|
162
|
+
panels: [...panels ?? []].map(([plugin, text]) => ({ plugin, text }))
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
#sessionUI(session, plugin) {
|
|
166
|
+
const bridge = this;
|
|
167
|
+
let gone = false;
|
|
168
|
+
const apply = () => {
|
|
169
|
+
if (bridge.#session === session)
|
|
170
|
+
bridge.#apply();
|
|
112
171
|
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
172
|
+
const put = (all, text) => {
|
|
173
|
+
if (gone)
|
|
174
|
+
return;
|
|
175
|
+
let mine = all.get(session);
|
|
176
|
+
if (text === undefined)
|
|
177
|
+
mine?.delete(plugin);
|
|
178
|
+
else {
|
|
179
|
+
if (!mine)
|
|
180
|
+
all.set(session, mine = new Map);
|
|
181
|
+
mine.set(plugin, text);
|
|
182
|
+
}
|
|
183
|
+
apply();
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
status: (text) => put(bridge.#segments, text === undefined ? undefined : clipSegment(text)),
|
|
187
|
+
panel: (text) => put(bridge.#panels, text === undefined ? undefined : clipPanel(ansi(text))),
|
|
188
|
+
draft: {
|
|
189
|
+
text: () => bridge.screen?.draft?.text() ?? "",
|
|
190
|
+
insert: (text) => bridge.screen?.draft?.insert(text),
|
|
191
|
+
set: (text) => bridge.screen?.draft?.set(text),
|
|
192
|
+
hint: (text) => bridge.screen?.draft?.hint(text)
|
|
193
|
+
},
|
|
194
|
+
dispose() {
|
|
195
|
+
gone = true;
|
|
196
|
+
for (const all of [bridge.#segments, bridge.#panels]) {
|
|
197
|
+
const mine = all.get(session);
|
|
198
|
+
mine?.delete(plugin);
|
|
199
|
+
if (mine?.size === 0)
|
|
200
|
+
all.delete(session);
|
|
131
201
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return;
|
|
145
|
-
const segments = this.#session === undefined ? undefined : this.#segments.get(this.#session);
|
|
146
|
-
this.screen.status({ plugins: [...(segments ?? [])].map(([plugin, text]) => ({ plugin, text })) });
|
|
147
|
-
}
|
|
148
|
-
/** 一个插件在一个会话里的界面。作用域关闭时核心调用 dispose,这里把它的段落去掉 */
|
|
149
|
-
#sessionUI(session, plugin) {
|
|
150
|
-
const bridge = this;
|
|
151
|
-
let gone = false;
|
|
152
|
-
const apply = () => {
|
|
153
|
-
if (bridge.#session === session)
|
|
154
|
-
bridge.#apply();
|
|
155
|
-
};
|
|
156
|
-
return {
|
|
157
|
-
status(text) {
|
|
158
|
-
if (gone)
|
|
159
|
-
return;
|
|
160
|
-
// 每次现取:别的插件 dispose 可能已经把空的 map 删了
|
|
161
|
-
let segments = bridge.#segments.get(session);
|
|
162
|
-
if (text === undefined)
|
|
163
|
-
segments?.delete(plugin);
|
|
164
|
-
else {
|
|
165
|
-
if (!segments)
|
|
166
|
-
bridge.#segments.set(session, (segments = new Map()));
|
|
167
|
-
segments.set(plugin, clipSegment(text));
|
|
168
|
-
}
|
|
169
|
-
apply();
|
|
170
|
-
},
|
|
171
|
-
draft: {
|
|
172
|
-
text: () => bridge.screen?.draft?.text() ?? "",
|
|
173
|
-
insert: (text) => bridge.screen?.draft?.insert(text),
|
|
174
|
-
set: (text) => bridge.screen?.draft?.set(text),
|
|
175
|
-
hint: (text) => bridge.screen?.draft?.hint(text),
|
|
176
|
-
},
|
|
177
|
-
dispose() {
|
|
178
|
-
gone = true;
|
|
179
|
-
const segments = bridge.#segments.get(session);
|
|
180
|
-
segments?.delete(plugin);
|
|
181
|
-
if (segments?.size === 0)
|
|
182
|
-
bridge.#segments.delete(session);
|
|
183
|
-
apply();
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
}
|
|
202
|
+
apply();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function clipPanel(text) {
|
|
208
|
+
const lines = String(text).replace(/\r\n?/g, `
|
|
209
|
+
`).replace(/\n+$/, "").split(`
|
|
210
|
+
`);
|
|
211
|
+
return lines.length > MAX_PANEL_LINES ? [...lines.slice(0, MAX_PANEL_LINES - 1), ` … ${lines.length - MAX_PANEL_LINES + 1} more`].join(`
|
|
212
|
+
`) : lines.join(`
|
|
213
|
+
`);
|
|
187
214
|
}
|
|
188
|
-
|
|
189
|
-
|
|
215
|
+
function clipSegment(segment) {
|
|
216
|
+
const clip = (text) => {
|
|
190
217
|
const one = String(text).replace(/\s+/g, " ").trim();
|
|
191
218
|
return one.length > MAX_SEGMENT ? `${one.slice(0, MAX_SEGMENT - 1)}…` : one;
|
|
219
|
+
};
|
|
220
|
+
if (typeof segment === "string")
|
|
221
|
+
return clip(segment);
|
|
222
|
+
return { ...segment, text: clip(segment.text), ...segment.short !== undefined ? { short: clip(segment.short) } : {} };
|
|
192
223
|
}
|
|
193
224
|
export function tuiFrontend(services, bridge) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
225
|
+
return {
|
|
226
|
+
run: (opts) => run(services, bridge, opts)
|
|
227
|
+
};
|
|
197
228
|
}
|
|
198
229
|
function parse(argv) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return out;
|
|
230
|
+
const out = {};
|
|
231
|
+
const words = [];
|
|
232
|
+
for (let i = 0;i < argv.length; i++) {
|
|
233
|
+
const a = argv[i];
|
|
234
|
+
if (a === "-h" || a === "--help")
|
|
235
|
+
out.help = true;
|
|
236
|
+
else if (a === "-c" || a === "--continue")
|
|
237
|
+
out.cont = true;
|
|
238
|
+
else if (a === "--resume" || a === "-r") {
|
|
239
|
+
const id = argv[++i];
|
|
240
|
+
if (!id)
|
|
241
|
+
return `${a} needs a session id (inside jade, /resume lists the sessions)`;
|
|
242
|
+
out.resume = id;
|
|
243
|
+
} else if (a.startsWith("-"))
|
|
244
|
+
return `unknown option ${a}`;
|
|
245
|
+
else
|
|
246
|
+
words.push(a);
|
|
247
|
+
}
|
|
248
|
+
if (out.cont && out.resume)
|
|
249
|
+
return "--continue and --resume both pick the session; use one of them";
|
|
250
|
+
if (words.length > 0)
|
|
251
|
+
out.prompt = words.join(" ");
|
|
252
|
+
return out;
|
|
223
253
|
}
|
|
224
254
|
async function run(services, bridge, opts) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const offMode = modes?.subscribe((mode) => screen.status({ mode }));
|
|
306
|
-
// /model 换了配置:状态栏跟着换
|
|
307
|
-
const offModel = models?.subscribe?.(() => screen.status({ model: models.current()?.model ?? "(no model)" }));
|
|
308
|
-
// ---------- 会话 ----------
|
|
309
|
-
let current;
|
|
310
|
-
let unsubscribe;
|
|
311
|
-
const canRetry = () => current?.state.phase === "idle" && current.state.messages.at(-1)?.role === "user";
|
|
312
|
-
const view = transcript({
|
|
313
|
-
screen,
|
|
314
|
-
cwd,
|
|
315
|
-
contextWindow: () => models?.current()?.contextWindow ?? 0,
|
|
316
|
-
maxTokens: () => models?.current()?.maxTokens,
|
|
317
|
-
...(compact ? { compactAt: () => compact.threshold() } : {}),
|
|
318
|
-
canRetry,
|
|
319
|
-
renderer: (tool) => renderers.forTool(tool),
|
|
320
|
-
eventRenderer: (type) => renderers.forEvent(type),
|
|
255
|
+
const args = parse(opts.argv);
|
|
256
|
+
if (typeof args === "string") {
|
|
257
|
+
process.stderr.write(`${args}
|
|
258
|
+
|
|
259
|
+
${USAGE}
|
|
260
|
+
`);
|
|
261
|
+
return 2;
|
|
262
|
+
}
|
|
263
|
+
if (args.help) {
|
|
264
|
+
process.stdout.write(`${USAGE}
|
|
265
|
+
`);
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
const io = terminal.io;
|
|
269
|
+
if (!(io ?? process).stdin.isTTY || !(io ?? process).stdout.isTTY) {
|
|
270
|
+
process.stderr.write(`jade's terminal interface needs a terminal. To run one prompt, use: jade -p <prompt>
|
|
271
|
+
`);
|
|
272
|
+
return 2;
|
|
273
|
+
}
|
|
274
|
+
const { cwd, home, commands, keys, renderers, models, completions, index, steer } = services;
|
|
275
|
+
const sessions = opts.sessions;
|
|
276
|
+
if (args.cont && !index) {
|
|
277
|
+
process.stderr.write(`jade: --continue needs the jade/sessions plugin, which is not enabled
|
|
278
|
+
`);
|
|
279
|
+
return 2;
|
|
280
|
+
}
|
|
281
|
+
const resumed = args.cont ? (await index.list(cwd))[0]?.id : args.resume;
|
|
282
|
+
if (args.cont && !resumed) {
|
|
283
|
+
process.stderr.write(`jade: no session to continue in ${cwd}
|
|
284
|
+
`);
|
|
285
|
+
return 2;
|
|
286
|
+
}
|
|
287
|
+
const history = new InputHistory(path.join(home, "history.jsonl"), cwd);
|
|
288
|
+
const [past, branch, { tuiScreen }] = await Promise.all([history.load(), gitBranch(cwd), loadTui()]);
|
|
289
|
+
const commandList = () => [
|
|
290
|
+
...OWN,
|
|
291
|
+
...commands.list().filter((c) => !OWN.some((o) => o.name === c.name)).map((c) => ({
|
|
292
|
+
name: c.name,
|
|
293
|
+
...c.args ? { args: c.args } : {},
|
|
294
|
+
...c.prefix ? { prefix: c.prefix, ...c.prefixHint ? { prefixHint: c.prefixHint } : {} } : {},
|
|
295
|
+
about: `${c.description ?? ""}${c.plugin.startsWith("jade/") ? "" : ` (plugin ${c.plugin})`}`.trim()
|
|
296
|
+
}))
|
|
297
|
+
];
|
|
298
|
+
const completionList = () => (completions?.list() ?? []).map(({ completion: c }) => ({ trigger: c.trigger, label: c.label, ...c.hint ? { hint: c.hint } : {} }));
|
|
299
|
+
let ready = false;
|
|
300
|
+
const early = [];
|
|
301
|
+
let finish;
|
|
302
|
+
const finished = new Promise((r) => finish = r);
|
|
303
|
+
const screen = safeScreen(tuiScreen({
|
|
304
|
+
status: { cwd: shortPath(cwd), ...branch ? { branch } : {}, model: models?.current()?.model ?? "(no model)" },
|
|
305
|
+
version: VERSION,
|
|
306
|
+
commands: commandList,
|
|
307
|
+
history: past,
|
|
308
|
+
...io ? { io } : {},
|
|
309
|
+
handlers: {
|
|
310
|
+
submit: (text) => {
|
|
311
|
+
history.add(text).catch(() => {});
|
|
312
|
+
if (ready)
|
|
313
|
+
onSubmit(text);
|
|
314
|
+
else
|
|
315
|
+
early.push(text);
|
|
316
|
+
},
|
|
317
|
+
completions: completionList,
|
|
318
|
+
complete: async (trigger) => await completions?.list().find((c) => c.completion.trigger === trigger)?.completion.candidates() ?? [],
|
|
319
|
+
interrupt: () => ready && interrupt(),
|
|
320
|
+
unqueue: () => takeBack(),
|
|
321
|
+
exit: () => void quit(0),
|
|
322
|
+
key: (spec) => key(spec)
|
|
323
|
+
}
|
|
324
|
+
}));
|
|
325
|
+
bridge.attach(screen);
|
|
326
|
+
const keyAbort = new AbortController;
|
|
327
|
+
function key(spec) {
|
|
328
|
+
const found = keys.get(spec);
|
|
329
|
+
if (!found)
|
|
330
|
+
return false;
|
|
331
|
+
const signal = keyAbort.signal;
|
|
332
|
+
Promise.resolve().then(() => found.key.run({ key: spec, signal })).catch((e) => {
|
|
333
|
+
if (!signal.aborted)
|
|
334
|
+
screen.log(yellow(`[warning] plugin ${found.plugin}: ${spec} failed: ${errorText(e)}`));
|
|
321
335
|
});
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
const offModel = models?.subscribe?.(() => screen.status({ model: models.current()?.model ?? "(no model)" }));
|
|
339
|
+
let current;
|
|
340
|
+
let unsubscribe;
|
|
341
|
+
const canRetry = () => current?.state.phase === "idle" && current.state.messages.at(-1)?.role === "user";
|
|
342
|
+
const view = transcript({
|
|
343
|
+
screen,
|
|
344
|
+
cwd,
|
|
345
|
+
contextWindow: () => models?.current()?.contextWindow ?? 0,
|
|
346
|
+
maxTokens: () => models?.current()?.maxTokens,
|
|
347
|
+
canRetry,
|
|
348
|
+
renderer: (tool) => {
|
|
349
|
+
const found = renderers.forTool(tool);
|
|
350
|
+
return found && { plugin: found.plugin, renderer: ansiTool(found.renderer) };
|
|
351
|
+
},
|
|
352
|
+
eventRenderer: (type) => {
|
|
353
|
+
const found = renderers.forEvent(type);
|
|
354
|
+
return found && { plugin: found.plugin, renderer: ansiEvent(found.renderer) };
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
async function adopt(s, resumed) {
|
|
358
|
+
unsubscribe?.();
|
|
359
|
+
current = s;
|
|
360
|
+
view.reset();
|
|
361
|
+
screen.clear();
|
|
362
|
+
screen.status({ session: s.id, context: undefined, cache: undefined, spent: undefined, speed: undefined });
|
|
363
|
+
bridge.show(s.id);
|
|
364
|
+
if (resumed)
|
|
365
|
+
await view.replay(await s.history(), true);
|
|
366
|
+
bridge.release();
|
|
367
|
+
unsubscribe = s.subscribe(view.onNotice);
|
|
368
|
+
if (!resumed)
|
|
369
|
+
return;
|
|
370
|
+
const held = s.state.held;
|
|
371
|
+
if (held) {
|
|
372
|
+
screen.log(yellow(`Plugins changed since this session was last used: ${held.plugins.map((p) => `${p.id}@${p.version}`).join(", ")}`));
|
|
373
|
+
if (held.resolution === "abort") {
|
|
374
|
+
screen.log(red("A plugin requires this session to stop. Restore the previous plugins to continue it, or /clear to start a new one."));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const ok = await screen.select("Continue with the new plugins? Pending tool calls will be re-checked.", [
|
|
378
|
+
{ label: "Yes, continue", value: true },
|
|
379
|
+
{ label: "No", value: false }
|
|
380
|
+
]) ?? false;
|
|
381
|
+
await guarded(() => s.send({ kind: "plugins", approved: ok }));
|
|
382
|
+
if (!ok)
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
await guarded(() => s.recover());
|
|
386
|
+
screen.log(dim(`Resumed ${s.id} (${s.state.messages.length} messages)`));
|
|
387
|
+
}
|
|
388
|
+
const manager = {
|
|
389
|
+
get current() {
|
|
390
|
+
return sessions.current;
|
|
391
|
+
},
|
|
392
|
+
async open(o) {
|
|
393
|
+
bridge.hold();
|
|
394
|
+
try {
|
|
395
|
+
const s = await sessions.open(o);
|
|
396
|
+
await adopt(s, o?.resume !== undefined || o?.fork !== undefined);
|
|
397
|
+
return s;
|
|
398
|
+
} finally {
|
|
333
399
|
bridge.release();
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
async close() {
|
|
403
|
+
unsubscribe?.();
|
|
404
|
+
unsubscribe = undefined;
|
|
405
|
+
current = undefined;
|
|
406
|
+
bridge.show(undefined);
|
|
407
|
+
await sessions.close();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
const queue = [];
|
|
411
|
+
const steered = {
|
|
412
|
+
pending() {
|
|
413
|
+
try {
|
|
414
|
+
return current && steer ? steer.pending(current.id) : [];
|
|
415
|
+
} catch {
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
take() {
|
|
420
|
+
try {
|
|
421
|
+
return current && steer ? steer.take(current.id) : [];
|
|
422
|
+
} catch {
|
|
423
|
+
return [];
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
push(text) {
|
|
427
|
+
try {
|
|
428
|
+
return current !== undefined && steer !== undefined && steer.push(current.id, text);
|
|
429
|
+
} catch {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
const showQueued = () => screen.queued([...steered.pending(), ...queue]);
|
|
435
|
+
let offSteer;
|
|
436
|
+
try {
|
|
437
|
+
offSteer = steer?.subscribe(showQueued);
|
|
438
|
+
} catch {}
|
|
439
|
+
let running = false;
|
|
440
|
+
let work;
|
|
441
|
+
let interruptedAt = 0;
|
|
442
|
+
let interruptPending = false;
|
|
443
|
+
const commandAborts = new Set;
|
|
444
|
+
let quitting = false;
|
|
445
|
+
async function occupy(fn) {
|
|
446
|
+
running = true;
|
|
447
|
+
work = fn();
|
|
448
|
+
try {
|
|
449
|
+
await work;
|
|
450
|
+
} finally {
|
|
451
|
+
running = false;
|
|
452
|
+
work = undefined;
|
|
453
|
+
interruptPending = false;
|
|
454
|
+
screen.busy(undefined);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
async function guarded(fn) {
|
|
458
|
+
view.turnFailed = false;
|
|
459
|
+
view.lastError = undefined;
|
|
460
|
+
try {
|
|
461
|
+
await fn();
|
|
462
|
+
} catch (e) {
|
|
463
|
+
if (!view.turnFailed)
|
|
464
|
+
screen.log(red(`[error] ${errorText(e)}`));
|
|
465
|
+
} finally {
|
|
466
|
+
screen.endStream();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
function interrupt() {
|
|
470
|
+
if (!running)
|
|
471
|
+
return;
|
|
472
|
+
const again = interruptPending || Date.now() - interruptedAt < SECOND_ESC_MS;
|
|
473
|
+
interruptedAt = Date.now();
|
|
474
|
+
interruptPending = true;
|
|
475
|
+
for (const ac of commandAborts)
|
|
476
|
+
ac.abort(new Error("interrupted by user"));
|
|
477
|
+
current?.abort(new Error("interrupted by user"));
|
|
478
|
+
screen.busy("Interrupting");
|
|
479
|
+
const n = queue.length + steered.pending().length;
|
|
480
|
+
if (n === 0)
|
|
481
|
+
return;
|
|
482
|
+
if (again) {
|
|
483
|
+
if (takeBack())
|
|
484
|
+
screen.log(dim(`[moved ${n} queued message${n > 1 ? "s" : ""} back to the input]`));
|
|
485
|
+
else {
|
|
486
|
+
steered.take();
|
|
487
|
+
queue.length = 0;
|
|
488
|
+
showQueued();
|
|
489
|
+
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
490
|
+
}
|
|
491
|
+
} else {
|
|
492
|
+
screen.log(dim(`[${n} queued message${n > 1 ? "s" : ""} will still be sent; press esc again to edit ${n > 1 ? "them" : "it"}]`));
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function takeBack() {
|
|
496
|
+
if (!screen.draft || queue.length + steered.pending().length === 0)
|
|
497
|
+
return false;
|
|
498
|
+
const typed = screen.draft.text();
|
|
499
|
+
screen.draft.set([...steered.take(), ...queue.splice(0), ...typed.trim() ? [typed] : []].join(`
|
|
500
|
+
`));
|
|
501
|
+
showQueued();
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
async function quit(code) {
|
|
505
|
+
if (quitting)
|
|
506
|
+
return;
|
|
507
|
+
quitting = true;
|
|
508
|
+
if (running)
|
|
509
|
+
current?.abort(new Error("exiting"));
|
|
510
|
+
for (const ac of commandAborts)
|
|
511
|
+
ac.abort(new Error("exiting"));
|
|
512
|
+
await Promise.race([work?.catch(() => {}), new Promise((r) => setTimeout(r, QUIT_WAIT_MS))]);
|
|
513
|
+
keyAbort.abort(new Error("exiting"));
|
|
514
|
+
offModel?.();
|
|
515
|
+
offSteer?.();
|
|
516
|
+
unsubscribe?.();
|
|
517
|
+
await screen.close();
|
|
518
|
+
bridge.detach();
|
|
519
|
+
if (current && current.state.messages.length > 0)
|
|
520
|
+
(io?.stdout ?? process.stdout).write(`${dim(`Resume with: jade --resume ${current.id}`)}
|
|
521
|
+
`);
|
|
522
|
+
finish(code);
|
|
523
|
+
}
|
|
524
|
+
opts.signal.addEventListener("abort", () => void quit(130), { once: true });
|
|
525
|
+
async function say(text) {
|
|
526
|
+
const s = current;
|
|
527
|
+
if (!s)
|
|
528
|
+
return screen.log(red("[error] there is no open session. Use /clear or /resume to continue."));
|
|
529
|
+
screen.busy(undefined);
|
|
530
|
+
screen.busy("Working");
|
|
531
|
+
const content = [{ kind: "text", text, origin: { by: "user" }, trust: "trusted" }];
|
|
532
|
+
await guarded(async () => {
|
|
533
|
+
await s.send({ kind: "message", content });
|
|
534
|
+
await s.idle();
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
const commandName = (text) => /^\/([^\s/]+)(?=\s|$)/.exec(text)?.[1].toLowerCase();
|
|
538
|
+
const instant = (name) => OWN.some((o) => o.name === name) || name === "quit" || commands.get(name)?.command.instant === true;
|
|
539
|
+
async function command(input) {
|
|
540
|
+
const name = input.slice(1).split(/\s+/)[0].toLowerCase();
|
|
541
|
+
const rest = input.slice(1).replace(/^\S+\s*/, "");
|
|
542
|
+
if (name === "exit" || name === "quit")
|
|
543
|
+
return false;
|
|
544
|
+
if (name === "help") {
|
|
545
|
+
const extras = [
|
|
546
|
+
...commands.list().flatMap((c) => c.prefix ? [`${c.prefix}${c.args ?? ""} is /${c.name}`] : []),
|
|
547
|
+
...completionList().map((c) => `${c.trigger} completes ${c.label}`)
|
|
548
|
+
];
|
|
549
|
+
screen.log(dim(extras.length ? `${KEYS}
|
|
550
|
+
plugins ${extras.join(" · ")}` : KEYS));
|
|
551
|
+
const pluginKeys = keys.list();
|
|
552
|
+
if (pluginKeys.length > 0)
|
|
553
|
+
screen.log(dim(`Plugin keys: ${pluginKeys.map((k) => `${k.key}${k.description ? ` ${k.description}` : ""} (${k.plugin})`).join(" · ")}`));
|
|
554
|
+
const all = commandList();
|
|
555
|
+
const width = Math.max(...all.map((c) => `/${c.name}${c.args ? ` ${c.args}` : ""}`.length)) + 2;
|
|
556
|
+
screen.log(all.map((c) => `${`/${c.name}${c.args ? ` ${c.args}` : ""}`.padEnd(width)}${dim(c.about)}`).join(`
|
|
557
|
+
`));
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
const found = commands.get(name);
|
|
561
|
+
if (!found) {
|
|
562
|
+
screen.log(red(`unknown command /${name}; /help lists the commands`));
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
const ac = new AbortController;
|
|
566
|
+
commandAborts.add(ac);
|
|
567
|
+
const showLive = (live) => {
|
|
568
|
+
const text = live === undefined ? undefined : ansi(live);
|
|
569
|
+
const lines = text?.replace(/\r\n?/g, `
|
|
570
|
+
`).trimEnd().split(`
|
|
571
|
+
`).slice(-LIVE_ROWS) ?? [];
|
|
572
|
+
for (let i = 0;i < LIVE_ROWS; i++)
|
|
573
|
+
screen.progress(`command:${i}`, text !== undefined && i < lines.length ? dim(` │ ${lines[i]}`) : undefined);
|
|
379
574
|
};
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
*/
|
|
387
|
-
const steered = {
|
|
388
|
-
pending() {
|
|
389
|
-
try {
|
|
390
|
-
return current && steer ? steer.pending(current.id) : [];
|
|
391
|
-
}
|
|
392
|
-
catch {
|
|
393
|
-
return [];
|
|
394
|
-
}
|
|
395
|
-
},
|
|
396
|
-
take() {
|
|
397
|
-
try {
|
|
398
|
-
return current && steer ? steer.take(current.id) : [];
|
|
399
|
-
}
|
|
400
|
-
catch {
|
|
401
|
-
return [];
|
|
402
|
-
}
|
|
403
|
-
},
|
|
404
|
-
push(text) {
|
|
405
|
-
try {
|
|
406
|
-
return current !== undefined && steer !== undefined && steer.push(current.id, text);
|
|
407
|
-
}
|
|
408
|
-
catch {
|
|
409
|
-
return false;
|
|
410
|
-
}
|
|
411
|
-
},
|
|
575
|
+
const ctx = {
|
|
576
|
+
signal: ac.signal,
|
|
577
|
+
ui: bridge.ui,
|
|
578
|
+
sessions: manager,
|
|
579
|
+
print: (t, o) => screen.log(ansi(t), o?.details ? { details: ansi(o.details) } : undefined),
|
|
580
|
+
progress: showLive
|
|
412
581
|
};
|
|
413
|
-
/** 输入框上方显示的等待中的消息:中途说的话在前(它们先到模型那里) */
|
|
414
|
-
const showQueued = () => screen.queued([...steered.pending(), ...queue]);
|
|
415
|
-
let offSteer;
|
|
416
582
|
try {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
if (
|
|
468
|
-
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
steered.take();
|
|
474
|
-
queue.length = 0;
|
|
475
|
-
showQueued();
|
|
476
|
-
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
else {
|
|
480
|
-
screen.log(dim(`[${n} queued message${n > 1 ? "s" : ""} will still be sent; press esc again to edit ${n > 1 ? "them" : "it"}]`));
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
/** 排队的消息不再自动发送,放回输入框(在已经输入的内容前面,一条一行);没有可放回的返回 false */
|
|
484
|
-
function takeBack() {
|
|
485
|
-
if (!screen.draft || queue.length + steered.pending().length === 0)
|
|
486
|
-
return false;
|
|
487
|
-
const typed = screen.draft.text();
|
|
488
|
-
screen.draft.set([...steered.take(), ...queue.splice(0), ...(typed.trim() ? [typed] : [])].join("\n"));
|
|
583
|
+
const result = await found.command.run(rest, ctx);
|
|
584
|
+
if (isPrompt(result)) {
|
|
585
|
+
if (found.command.instant)
|
|
586
|
+
screen.log(yellow(`[warning] /${name} is instant and cannot send a prompt`));
|
|
587
|
+
else
|
|
588
|
+
await say(result.prompt);
|
|
589
|
+
} else if (result !== undefined && result !== null)
|
|
590
|
+
screen.log(ansi(result));
|
|
591
|
+
} catch (e) {
|
|
592
|
+
if (!ac.signal.aborted)
|
|
593
|
+
screen.log(red(`[error] /${name}: ${errorText(e)}`));
|
|
594
|
+
} finally {
|
|
595
|
+
showLive(undefined);
|
|
596
|
+
commandAborts.delete(ac);
|
|
597
|
+
}
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
function onSubmit(text) {
|
|
601
|
+
if (quitting)
|
|
602
|
+
return;
|
|
603
|
+
const short = [...text][0];
|
|
604
|
+
const byPrefix = short === undefined ? undefined : commands.byPrefix(short);
|
|
605
|
+
if (byPrefix && text.slice(short.length).trim())
|
|
606
|
+
text = `/${byPrefix.command.name} ${text.slice(short.length).trim()}`;
|
|
607
|
+
const name = commandName(text);
|
|
608
|
+
if (name !== undefined && instant(name)) {
|
|
609
|
+
view.echo(text);
|
|
610
|
+
command(text).then((go) => {
|
|
611
|
+
if (!go)
|
|
612
|
+
quit(0);
|
|
613
|
+
});
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (running && name === undefined && queue.length === 0 && steered.push(text)) {
|
|
617
|
+
showQueued();
|
|
618
|
+
work?.catch(() => {}).then(() => pump());
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
queue.push(text);
|
|
622
|
+
if (running)
|
|
623
|
+
showQueued();
|
|
624
|
+
pump();
|
|
625
|
+
}
|
|
626
|
+
async function pump() {
|
|
627
|
+
if (running)
|
|
628
|
+
return;
|
|
629
|
+
let exit = false;
|
|
630
|
+
await occupy(async () => {
|
|
631
|
+
for (;; ) {
|
|
632
|
+
queue.unshift(...steered.take());
|
|
633
|
+
if (queue.length === 0 || quitting)
|
|
634
|
+
break;
|
|
635
|
+
if (interruptPending)
|
|
636
|
+
interruptedAt = Date.now();
|
|
637
|
+
interruptPending = false;
|
|
638
|
+
const text = queue.shift();
|
|
489
639
|
showQueued();
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
quitting = true;
|
|
496
|
-
if (running)
|
|
497
|
-
current?.abort(new Error("exiting"));
|
|
498
|
-
await work?.catch(() => { });
|
|
499
|
-
keyAbort.abort(new Error("exiting"));
|
|
500
|
-
offMode?.();
|
|
501
|
-
offModel?.();
|
|
502
|
-
offSteer?.();
|
|
503
|
-
unsubscribe?.();
|
|
504
|
-
await screen.close();
|
|
505
|
-
bridge.detach();
|
|
506
|
-
// 退出备用屏幕之后,里面的内容就看不到了:告诉用户怎么回来
|
|
507
|
-
if (current && current.state.messages.length > 0)
|
|
508
|
-
(io?.stdout ?? process.stdout).write(`${dim(`Resume with: jade --resume ${current.id}`)}\n`);
|
|
509
|
-
finish(code);
|
|
510
|
-
}
|
|
511
|
-
opts.signal.addEventListener("abort", () => void quit(130), { once: true });
|
|
512
|
-
/** 发一条用户消息,跑到会话空闲(包括插件排在这一轮之后的工作) */
|
|
513
|
-
async function say(text) {
|
|
514
|
-
const s = current;
|
|
515
|
-
if (!s)
|
|
516
|
-
return screen.log(red("[error] there is no open session. Use /clear or /resume to continue."));
|
|
517
|
-
screen.busy(undefined);
|
|
518
|
-
screen.busy("Working");
|
|
519
|
-
const content = [{ kind: "text", text, origin: { by: "user" }, trust: "trusted" }];
|
|
520
|
-
await guarded(async () => {
|
|
521
|
-
await s.send({ kind: "message", content });
|
|
522
|
-
await s.idle();
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
/** 输入是命令时返回命令名。第一个词里还有 / 的是路径(/c/Users/…、/usr/bin/x),当普通消息发给模型 */
|
|
526
|
-
const commandName = (text) => /^\/([^\s/]+)(?=\s|$)/.exec(text)?.[1].toLowerCase();
|
|
527
|
-
const instant = (name) => OWN.some((o) => o.name === name) || name === "quit" || commands.get(name)?.command.instant === true;
|
|
528
|
-
/** 执行一条命令;返回 false 表示退出 */
|
|
529
|
-
async function command(input) {
|
|
530
|
-
const name = input.slice(1).split(/\s+/)[0].toLowerCase();
|
|
531
|
-
const rest = input.slice(1).replace(/^\S+\s*/, "");
|
|
532
|
-
if (name === "exit" || name === "quit")
|
|
533
|
-
return false;
|
|
534
|
-
if (name === "help") {
|
|
535
|
-
const all = commandList();
|
|
536
|
-
const width = Math.max(...all.map((c) => `/${c.name}${c.args ? ` ${c.args}` : ""}`.length)) + 2;
|
|
537
|
-
screen.log(all.map((c) => `${`/${c.name}${c.args ? ` ${c.args}` : ""}`.padEnd(width)}${dim(c.about)}`).join("\n"));
|
|
538
|
-
screen.log(dim(KEYS));
|
|
539
|
-
const pluginKeys = keys.list();
|
|
540
|
-
if (pluginKeys.length > 0)
|
|
541
|
-
screen.log(dim(`Plugin keys: ${pluginKeys.map((k) => `${k.key}${k.description ? ` ${k.description}` : ""} (${k.plugin})`).join(" · ")}`));
|
|
542
|
-
return true;
|
|
543
|
-
}
|
|
544
|
-
if (name === "copy") {
|
|
545
|
-
const reply = lastReply(current?.state.messages ?? []);
|
|
546
|
-
const text = rest.trim() === "code" ? reply && lastCodeBlock(reply) : reply;
|
|
547
|
-
if (!text)
|
|
548
|
-
screen.log(dim(rest.trim() === "code" ? "The last reply has no code block." : "There is no reply to copy yet."));
|
|
549
|
-
else if (!screen.copy)
|
|
550
|
-
screen.log(red("[error] this terminal cannot copy"));
|
|
551
|
-
else {
|
|
552
|
-
screen.copy(text);
|
|
553
|
-
screen.log(dim(`[copied ${[...text].length} characters]`));
|
|
554
|
-
}
|
|
555
|
-
return true;
|
|
556
|
-
}
|
|
557
|
-
const found = commands.get(name);
|
|
558
|
-
if (!found) {
|
|
559
|
-
screen.log(red(`unknown command /${name}; /help lists the commands`));
|
|
560
|
-
return true;
|
|
561
|
-
}
|
|
562
|
-
const ac = new AbortController();
|
|
563
|
-
commandAborts.add(ac);
|
|
564
|
-
const ctx = { signal: ac.signal, ui: bridge.ui, sessions: manager, print: (t) => screen.log(t) };
|
|
565
|
-
try {
|
|
566
|
-
const result = await found.command.run(rest, ctx);
|
|
567
|
-
if (typeof result === "string")
|
|
568
|
-
screen.log(result);
|
|
569
|
-
else if (result && typeof result === "object" && typeof result.prompt === "string") {
|
|
570
|
-
if (found.command.instant)
|
|
571
|
-
screen.log(yellow(`[warning] /${name} is instant and cannot send a prompt`));
|
|
572
|
-
else
|
|
573
|
-
await say(result.prompt);
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
catch (e) {
|
|
577
|
-
if (!ac.signal.aborted)
|
|
578
|
-
screen.log(red(`[error] /${name}: ${errorText(e)}`));
|
|
579
|
-
}
|
|
580
|
-
finally {
|
|
581
|
-
commandAborts.delete(ac);
|
|
582
|
-
}
|
|
583
|
-
return true;
|
|
584
|
-
}
|
|
585
|
-
function onSubmit(text) {
|
|
586
|
-
if (quitting)
|
|
587
|
-
return;
|
|
588
|
-
const name = commandName(text);
|
|
589
|
-
if (name !== undefined && instant(name)) {
|
|
590
|
-
// 不碰会话的命令运行中也直接执行
|
|
591
|
-
view.echo(text);
|
|
592
|
-
void command(text).then((go) => {
|
|
593
|
-
if (!go)
|
|
594
|
-
void quit(0);
|
|
595
|
-
});
|
|
596
|
-
return;
|
|
597
|
-
}
|
|
598
|
-
// 模型正在工作、前面也没有别的在排队:这句话在下一次请求模型时就交给它(jade/steer),不等这一轮结束。
|
|
599
|
-
// 命令、已经有东西在排队时照旧排队,先后顺序不变
|
|
600
|
-
if (running && name === undefined && queue.length === 0 && steered.push(text)) {
|
|
601
|
-
showQueued();
|
|
602
|
-
// 没赶上这一轮的,等正在做的事做完再发(那时不一定是 pump 在跑,例如刚恢复会话)
|
|
603
|
-
void work?.catch(() => { }).then(() => pump());
|
|
640
|
+
view.echo(text);
|
|
641
|
+
if (commandName(text) !== undefined) {
|
|
642
|
+
screen.busy("Running");
|
|
643
|
+
if (!await command(text)) {
|
|
644
|
+
exit = true;
|
|
604
645
|
return;
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
await quit(0);
|
|
639
|
-
}
|
|
640
|
-
// ---------- 开始 ----------
|
|
641
|
-
try {
|
|
642
|
-
let opened = false;
|
|
643
|
-
await occupy(async () => {
|
|
644
|
-
try {
|
|
645
|
-
await manager.open(resumed ? { resume: resumed } : {});
|
|
646
|
-
opened = true;
|
|
647
|
-
}
|
|
648
|
-
catch (e) {
|
|
649
|
-
screen.log(red(`[error] ${resumed ? `could not resume ${resumed}` : "could not start a session"}: ${errorText(e)}`));
|
|
650
|
-
}
|
|
651
|
-
});
|
|
652
|
-
if (!opened && resumed) {
|
|
653
|
-
// 恢复不了:新开一个,免得界面里没有会话
|
|
654
|
-
screen.log(dim("Starting a new session instead."));
|
|
655
|
-
await occupy(async () => {
|
|
656
|
-
await manager.open().catch((e) => screen.log(red(`[error] could not start a session: ${errorText(e)}`)));
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
catch (e) {
|
|
661
|
-
screen.log(red(`[error] ${errorText(e)}`));
|
|
662
|
-
}
|
|
663
|
-
if (!models?.current()) {
|
|
664
|
-
// /model(jade/models 提供)当场就能配好,不用重启;没有这条命令时只能改文件
|
|
665
|
-
const file = `put ANTHROPIC_API_KEY=... (or OPENAI_API_KEY=...) in ${path.join(home, ".env")}, then restart jade`;
|
|
666
|
-
screen.log(yellow(commands.list().some((c) => c.name === "model") ? `No model is configured yet. Type /model to set one up, or ${file}.` : `No model is configured yet. To set one up, ${file}.`));
|
|
667
|
-
}
|
|
668
|
-
else if (!resumed)
|
|
669
|
-
screen.log(dim("Type / for commands, esc to interrupt."));
|
|
670
|
-
ready = true;
|
|
671
|
-
for (const text of [...(args.prompt ? [args.prompt] : []), ...early.splice(0)])
|
|
672
|
-
onSubmit(text);
|
|
673
|
-
return finished;
|
|
674
|
-
}
|
|
675
|
-
/** 当前分支;不在仓库里或没有 git 时 undefined */
|
|
676
|
-
/** 最近一条有正文的助手消息的正文(模型写的原文,没有折行和排版) */
|
|
677
|
-
export function lastReply(messages) {
|
|
678
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
679
|
-
const m = messages[i];
|
|
680
|
-
if (m.role !== "assistant")
|
|
681
|
-
continue;
|
|
682
|
-
const text = m.content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n").trim();
|
|
683
|
-
if (text)
|
|
684
|
-
return text;
|
|
685
|
-
}
|
|
686
|
-
return undefined;
|
|
687
|
-
}
|
|
688
|
-
/** Markdown 里最后一个围栏代码块的内容 */
|
|
689
|
-
export function lastCodeBlock(markdown) {
|
|
690
|
-
const blocks = [...markdown.matchAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[`~]*[ \t]*$/gm)];
|
|
691
|
-
return blocks.at(-1)?.[2]?.replace(/\n$/, "");
|
|
646
|
+
}
|
|
647
|
+
} else
|
|
648
|
+
await say(text);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
if (exit)
|
|
652
|
+
await quit(0);
|
|
653
|
+
}
|
|
654
|
+
try {
|
|
655
|
+
let opened = false;
|
|
656
|
+
await occupy(async () => {
|
|
657
|
+
try {
|
|
658
|
+
await manager.open(resumed ? { resume: resumed } : {});
|
|
659
|
+
opened = true;
|
|
660
|
+
} catch (e) {
|
|
661
|
+
screen.log(red(`[error] ${resumed ? `could not resume ${resumed}` : "could not start a session"}: ${errorText(e)}`));
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
if (!opened && resumed) {
|
|
665
|
+
screen.log(dim("Starting a new session instead."));
|
|
666
|
+
await occupy(async () => {
|
|
667
|
+
await manager.open().catch((e) => screen.log(red(`[error] could not start a session: ${errorText(e)}`)));
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
} catch (e) {
|
|
671
|
+
screen.log(red(`[error] ${errorText(e)}`));
|
|
672
|
+
}
|
|
673
|
+
if (models?.current() && !resumed)
|
|
674
|
+
screen.log(dim("Type / for commands, esc to interrupt."));
|
|
675
|
+
ready = true;
|
|
676
|
+
for (const text of [...args.prompt ? [args.prompt] : [], ...early.splice(0)])
|
|
677
|
+
onSubmit(text);
|
|
678
|
+
return finished;
|
|
692
679
|
}
|
|
693
680
|
function gitBranch(cwd) {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
681
|
+
return new Promise((resolve) => {
|
|
682
|
+
execFile("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd, timeout: 2000, windowsHide: true }, (err, stdout) => resolve(err ? undefined : stdout.trim() || undefined));
|
|
683
|
+
});
|
|
697
684
|
}
|
|
698
|
-
/** 家目录下的路径写成 ~/… */
|
|
699
685
|
function shortPath(p) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
686
|
+
const home = os.homedir();
|
|
687
|
+
const rel = path.relative(home, p);
|
|
688
|
+
return rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? `~${path.sep}${rel}` : p === home ? "~" : p;
|
|
703
689
|
}
|