@uncarved/jade 0.3.1 → 0.4.1
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 +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- 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 +42 -9
- 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 +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -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 +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- 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 +80 -50
- package/plugins/task/plugin.json +6 -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 +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- 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 +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -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 -418
- package/plugins-dist/approval/policy.js +391 -508
- 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 -253
- 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 +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- 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 +86 -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 +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- 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 -117
- package/plugins-dist/task/plugin.json +5 -2
- 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 +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- 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 -355
- 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 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -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 +496 -36
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
// 宿主的输出与交互接口。两种实现:
|
|
2
|
-
// - 终端界面(tui.tsx):全屏,标题在顶部,对话在中间自己滚动,输入框、状态栏固定在底部,运行中也能输入
|
|
3
|
-
// - 纯文本(这里):-p 和非交互运行时使用,逐行输出,提问用 readline
|
|
4
1
|
import { createInterface } from "node:readline/promises";
|
|
5
2
|
import { stripVTControlCharacters } from "node:util";
|
|
6
|
-
// ---------- 颜色 ----------
|
|
7
|
-
// 设了 NO_COLOR(非空)时不输出颜色,见 https://no-color.org
|
|
8
3
|
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
9
|
-
|
|
10
|
-
* 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:粗体里套着行内代码、链接时,后面的文字仍是粗体。
|
|
11
|
-
* 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
|
|
12
|
-
*/
|
|
13
|
-
const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
4
|
+
const paint = (open, close) => (s) => colors ? `\x1B[${open}m${s.replaceAll(`\x1B[${close}m`, `\x1B[${close}m\x1B[${open}m`)}\x1B[${close}m` : s;
|
|
14
5
|
export const dim = paint("2", "22");
|
|
15
6
|
export const red = paint("31", "39");
|
|
16
7
|
export const green = paint("32", "39");
|
|
@@ -20,184 +11,210 @@ export const bold = paint("1", "22");
|
|
|
20
11
|
export const italic = paint("3", "23");
|
|
21
12
|
export const underline = paint("4", "24");
|
|
22
13
|
export const strike = paint("9", "29");
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
const magenta = paint("35", "39");
|
|
15
|
+
const STYLES = {
|
|
16
|
+
dim,
|
|
17
|
+
bold,
|
|
18
|
+
ok: green,
|
|
19
|
+
warn: yellow,
|
|
20
|
+
error: red,
|
|
21
|
+
accent: magenta,
|
|
22
|
+
code: cyan,
|
|
23
|
+
added: green,
|
|
24
|
+
removed: red
|
|
25
|
+
};
|
|
26
|
+
export function ansi(t) {
|
|
27
|
+
if (t === undefined || t === null)
|
|
28
|
+
return "";
|
|
29
|
+
if (typeof t === "string")
|
|
30
|
+
return t;
|
|
31
|
+
if (Array.isArray(t))
|
|
32
|
+
return t.map(ansi).join("");
|
|
33
|
+
const span = t;
|
|
34
|
+
const styles = Array.isArray(span.style) ? span.style : [span.style];
|
|
35
|
+
return styles.reduce((s, style) => STYLES[style]?.(s) ?? s, String(span.text ?? ""));
|
|
36
|
+
}
|
|
37
|
+
export function plain(t) {
|
|
38
|
+
if (t === undefined || t === null)
|
|
39
|
+
return "";
|
|
40
|
+
if (typeof t === "string")
|
|
41
|
+
return stripVTControlCharacters(t);
|
|
42
|
+
if (Array.isArray(t))
|
|
43
|
+
return t.map(plain).join("");
|
|
44
|
+
return stripVTControlCharacters(String(t.text ?? ""));
|
|
45
|
+
}
|
|
25
46
|
const CONTROL = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b[\]P^_X][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b[^[\]P^_X]?|[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
|
|
26
47
|
const SGR = /^\x1b\[[0-9;:]*m$/;
|
|
27
|
-
/** 文字末尾还没写完的转义序列:单独的 ESC、没有结束字节的 CSI、没有结束符的 OSC/DCS 这类字符串 */
|
|
28
48
|
const UNFINISHED = /\x1b(?:\[[0-?]*[ -/]*|[\]P^_X][^\x07\x1b]*)?$/;
|
|
29
|
-
/** 最多为一个没写完的序列留这么多字符 */
|
|
30
49
|
const MAX_HELD = 256;
|
|
31
|
-
/**
|
|
32
|
-
* 去掉可能搅乱终端的东西,只留颜色(SGR)、换行和制表符:插件、工具输出和模型的文字里可能有移动光标、清屏、
|
|
33
|
-
* 改窗口标题、写剪贴板之类的序列。单独的 \r(进度条)按终端的效果处理:每行只留最后一段
|
|
34
|
-
*/
|
|
35
50
|
export function terminalSafe(text) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
const lines = text.replaceAll(`\r
|
|
52
|
+
`, `
|
|
53
|
+
`).split(`
|
|
54
|
+
`);
|
|
55
|
+
return lines.map((l) => (l.includes("\r") ? l.split("\r").filter(Boolean).at(-1) ?? "" : l).replace(CONTROL, (m) => SGR.test(m) ? m : "")).join(`
|
|
56
|
+
`);
|
|
40
57
|
}
|
|
41
|
-
/** 让显示到屏幕上的每一段文字都经过 terminalSafe */
|
|
42
58
|
export function safeScreen(screen) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
59
|
+
const s = terminalSafe;
|
|
60
|
+
const opt = (v) => v === undefined ? undefined : s(v);
|
|
61
|
+
const lf = (t) => t.replace(/\r\n?/g, `
|
|
62
|
+
`);
|
|
63
|
+
const plain = (t) => stripVTControlCharacters(s(t));
|
|
64
|
+
let held;
|
|
65
|
+
return {
|
|
66
|
+
interactive: screen.interactive,
|
|
67
|
+
log: (text, o) => screen.log(s(text), o && { ...o, details: opt(o.details) }),
|
|
68
|
+
amend: (id, text, o) => screen.amend(id, s(text), o && { ...o, details: opt(o.details) }),
|
|
69
|
+
progress: (id, line, o) => screen.progress(id, opt(line), o),
|
|
70
|
+
stream: (kind, text) => {
|
|
71
|
+
if (held && held.kind !== kind)
|
|
72
|
+
held = undefined;
|
|
73
|
+
const whole = (held?.text ?? "") + text;
|
|
74
|
+
const found = whole.search(UNFINISHED);
|
|
75
|
+
const cut = found >= 0 && whole.length - found <= MAX_HELD ? found : -1;
|
|
76
|
+
held = cut >= 0 ? { kind, text: whole.slice(cut) } : undefined;
|
|
77
|
+
const shown = s(cut >= 0 ? whole.slice(0, cut) : whole);
|
|
78
|
+
if (shown)
|
|
79
|
+
screen.stream(kind, shown);
|
|
80
|
+
},
|
|
81
|
+
endStream: () => {
|
|
82
|
+
held = undefined;
|
|
83
|
+
screen.endStream();
|
|
84
|
+
},
|
|
85
|
+
busy: (label) => screen.busy(opt(label)),
|
|
86
|
+
status: (info) => {
|
|
87
|
+
const out = { ...info };
|
|
88
|
+
for (const k of ["cwd", "branch", "model", "session"])
|
|
89
|
+
if (typeof info[k] === "string")
|
|
90
|
+
out[k] = s(info[k]);
|
|
91
|
+
if (info.plugins)
|
|
92
|
+
out.plugins = info.plugins.map((p) => ({
|
|
93
|
+
plugin: p.plugin,
|
|
94
|
+
text: typeof p.text === "string" ? s(p.text) : { ...p.text, text: s(p.text.text), ...p.text.short !== undefined ? { short: s(p.text.short) } : {} }
|
|
95
|
+
}));
|
|
96
|
+
if (info.panels)
|
|
97
|
+
out.panels = info.panels.map((p) => ({ plugin: p.plugin, text: s(p.text) }));
|
|
98
|
+
screen.status(out);
|
|
99
|
+
},
|
|
100
|
+
queued: (items) => screen.queued(items.map(s)),
|
|
101
|
+
select: (title, choices, o) => screen.select(s(title), choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint) })), o && { ...o, body: opt(o.body) }),
|
|
102
|
+
input: (title, o) => screen.input(s(title), o && { ...o, initial: opt(o.initial), placeholder: opt(o.placeholder) }),
|
|
103
|
+
draft: screen.draft && {
|
|
104
|
+
text: () => screen.draft.text(),
|
|
105
|
+
insert: (t) => screen.draft.insert(plain(lf(t))),
|
|
106
|
+
set: (t) => screen.draft.set(plain(lf(t))),
|
|
107
|
+
hint: (t) => screen.draft.hint(t === undefined ? undefined : plain(lf(t)))
|
|
108
|
+
},
|
|
109
|
+
...screen.copy ? { copy: (t) => screen.copy(t) } : {},
|
|
110
|
+
...screen.suspend ? { suspend: (fn) => screen.suspend(fn) } : {},
|
|
111
|
+
clear: () => screen.clear(),
|
|
112
|
+
close: () => screen.close()
|
|
113
|
+
};
|
|
98
114
|
}
|
|
99
|
-
// ---------- 纯文本实现 ----------
|
|
100
115
|
const MAX_OUTPUT_LINES = 20;
|
|
101
116
|
export function plainScreen(opts) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
117
|
+
let atLineStart = true;
|
|
118
|
+
let streaming;
|
|
119
|
+
let rl;
|
|
120
|
+
const outputLines = new Map;
|
|
121
|
+
const write = (s) => {
|
|
122
|
+
if (!s)
|
|
123
|
+
return;
|
|
124
|
+
process.stdout.write(s);
|
|
125
|
+
atLineStart = s.endsWith(`
|
|
126
|
+
`);
|
|
127
|
+
};
|
|
128
|
+
const line = (s) => {
|
|
129
|
+
if (!atLineStart)
|
|
130
|
+
write(`
|
|
131
|
+
`);
|
|
132
|
+
write(s + `
|
|
133
|
+
`);
|
|
134
|
+
};
|
|
135
|
+
const ask = async (q, signal) => {
|
|
136
|
+
rl ??= createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
137
|
+
try {
|
|
138
|
+
return (await rl.question(q, { signal })).trim();
|
|
139
|
+
} catch (err) {
|
|
140
|
+
if (!signal?.aborted)
|
|
141
|
+
throw err;
|
|
142
|
+
write(`
|
|
143
|
+
`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
interactive: opts.interactive,
|
|
149
|
+
log: (text) => {
|
|
150
|
+
streaming = undefined;
|
|
151
|
+
line(text);
|
|
152
|
+
},
|
|
153
|
+
amend: (_id, text) => {
|
|
154
|
+
streaming = undefined;
|
|
155
|
+
line(` ${text}`);
|
|
156
|
+
},
|
|
157
|
+
progress: (id, text, o) => {
|
|
158
|
+
if (text === undefined)
|
|
159
|
+
return void outputLines.delete(id);
|
|
160
|
+
streaming = undefined;
|
|
161
|
+
if (o?.output) {
|
|
162
|
+
const shown = outputLines.get(id) ?? 0;
|
|
163
|
+
if (shown > MAX_OUTPUT_LINES)
|
|
164
|
+
return;
|
|
165
|
+
outputLines.set(id, shown + 1);
|
|
166
|
+
if (shown === MAX_OUTPUT_LINES)
|
|
167
|
+
return line(dim(" │ …"));
|
|
168
|
+
}
|
|
169
|
+
line(text);
|
|
170
|
+
},
|
|
171
|
+
stream: (kind, text) => {
|
|
172
|
+
if (kind !== streaming) {
|
|
173
|
+
if (kind === "text")
|
|
174
|
+
text = text.replace(/^\n+/, "");
|
|
175
|
+
if (!text)
|
|
176
|
+
return;
|
|
177
|
+
if (kind === "thinking")
|
|
178
|
+
line(dim("thinking…"));
|
|
179
|
+
else if (!atLineStart)
|
|
180
|
+
write(`
|
|
181
|
+
`);
|
|
182
|
+
streaming = kind;
|
|
183
|
+
}
|
|
184
|
+
write(kind === "thinking" ? dim(text) : text);
|
|
185
|
+
},
|
|
186
|
+
endStream: () => {
|
|
187
|
+
streaming = undefined;
|
|
188
|
+
if (!atLineStart)
|
|
189
|
+
write(`
|
|
190
|
+
`);
|
|
191
|
+
},
|
|
192
|
+
busy: () => {},
|
|
193
|
+
status: () => {},
|
|
194
|
+
queued: () => {},
|
|
195
|
+
async select(title, choices, o) {
|
|
196
|
+
if (!opts.interactive)
|
|
197
|
+
return;
|
|
198
|
+
line(yellow(title));
|
|
199
|
+
if (o?.body)
|
|
200
|
+
line(o.body);
|
|
201
|
+
choices.forEach((c, i) => line(` ${i + 1}. ${c.label}${c.hint ? dim(` ${c.hint}`) : ""}`));
|
|
202
|
+
if (o?.signal?.aborted)
|
|
203
|
+
return;
|
|
204
|
+
const n = Number(await ask(`Choose 1-${choices.length} (empty to cancel): `, o?.signal));
|
|
205
|
+
return Number.isInteger(n) && n >= 1 && n <= choices.length ? choices[n - 1].value : undefined;
|
|
206
|
+
},
|
|
207
|
+
async input(title, o) {
|
|
208
|
+
if (!opts.interactive)
|
|
209
|
+
return;
|
|
210
|
+
if (o?.signal?.aborted)
|
|
211
|
+
return;
|
|
212
|
+
const answer = await ask(`${title}${o?.initial ? ` [${o.initial}]` : ""}: `, o?.signal);
|
|
213
|
+
return answer === undefined ? undefined : answer || o?.initial || "";
|
|
214
|
+
},
|
|
215
|
+
clear: () => {},
|
|
216
|
+
async close() {
|
|
217
|
+
rl?.close();
|
|
218
|
+
}
|
|
219
|
+
};
|
|
203
220
|
}
|