@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.
Files changed (207) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +482 -27
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +49 -24
  14. package/plugins/approval/policy.ts +48 -25
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +55 -12
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +10 -9
  50. package/plugins/models/index.ts +31 -4
  51. package/plugins/models/provider.ts +12 -1
  52. package/plugins/modes/index.ts +13 -2
  53. package/plugins/print/index.ts +7 -4
  54. package/plugins/prompts/index.ts +0 -0
  55. package/plugins/renderers/index.ts +10 -5
  56. package/plugins/repeats/index.ts +39 -9
  57. package/plugins/schedule/index.ts +2 -0
  58. package/plugins/screenshot/browser.ts +231 -0
  59. package/plugins/screenshot/index.ts +3 -2
  60. package/plugins/screenshot/tool.ts +27 -51
  61. package/plugins/sessions/index.ts +48 -2
  62. package/plugins/shell/index.ts +75 -22
  63. package/plugins/shell/plugin.json +1 -2
  64. package/plugins/shell/renderers.ts +9 -7
  65. package/plugins/shell/shell.ts +93 -34
  66. package/plugins/skills/index.ts +28 -19
  67. package/plugins/skills/project.ts +12 -7
  68. package/plugins/steer/index.ts +5 -4
  69. package/plugins/task/index.ts +81 -81
  70. package/plugins/task/plugin.json +5 -2
  71. package/plugins/task/style.ts +13 -14
  72. package/plugins/task/subagent.ts +67 -184
  73. package/plugins/todo/index.ts +39 -9
  74. package/plugins/tools/index.ts +57 -0
  75. package/plugins/tools/plugin.json +7 -0
  76. package/plugins/tui/frontend.ts +161 -87
  77. package/plugins/tui/index.ts +2 -3
  78. package/plugins/tui/mascot.ts +8 -7
  79. package/plugins/tui/mentions.ts +11 -80
  80. package/plugins/tui/plugin.json +1 -1
  81. package/plugins/tui/screen.ts +55 -5
  82. package/plugins/tui/transcript.ts +17 -7
  83. package/plugins/tui/tui.ts +356 -104
  84. package/plugins/tui/vendor.ts +1 -1
  85. package/plugins/undo/checkpoint.ts +5 -3
  86. package/plugins/undo/index.ts +43 -14
  87. package/plugins/undo/style.ts +13 -14
  88. package/plugins/web/index.ts +7 -18
  89. package/plugins/web/plugin.json +1 -1
  90. package/plugins-dist/approval/index.js +363 -414
  91. package/plugins-dist/approval/policy.js +395 -497
  92. package/plugins-dist/approval/review.js +132 -158
  93. package/plugins-dist/approval/schema.js +50 -55
  94. package/plugins-dist/ask/index.js +115 -129
  95. package/plugins-dist/budget/index.js +27 -31
  96. package/plugins-dist/checks/checks.js +293 -329
  97. package/plugins-dist/checks/index.js +119 -139
  98. package/plugins-dist/checks/plugin.json +2 -1
  99. package/plugins-dist/checks/project.js +57 -65
  100. package/plugins-dist/commands/index.js +47 -29
  101. package/plugins-dist/compact/compact.js +233 -249
  102. package/plugins-dist/compact/index.js +98 -95
  103. package/plugins-dist/compact/style.js +9 -14
  104. package/plugins-dist/completions/index.js +30 -0
  105. package/plugins-dist/completions/plugin.json +9 -0
  106. package/plugins-dist/continue/index.js +40 -52
  107. package/plugins-dist/copy/index.js +40 -0
  108. package/plugins-dist/copy/plugin.json +9 -0
  109. package/plugins-dist/cost/index.js +98 -115
  110. package/plugins-dist/custom-commands/index.js +55 -72
  111. package/plugins-dist/editor/index.js +64 -72
  112. package/plugins-dist/fs/diff.js +111 -124
  113. package/plugins-dist/fs/fs.js +426 -469
  114. package/plugins-dist/fs/grep.js +262 -244
  115. package/plugins-dist/fs/index.js +20 -34
  116. package/plugins-dist/fs/plugin.json +1 -2
  117. package/plugins-dist/fs/renderers.js +100 -112
  118. package/plugins-dist/fs/style.js +11 -15
  119. package/plugins-dist/fs/sys.js +31 -43
  120. package/plugins-dist/hooks/index.js +107 -127
  121. package/plugins-dist/keys/index.js +27 -31
  122. package/plugins-dist/mcp/index.js +271 -199
  123. package/plugins-dist/mcp/install.js +123 -137
  124. package/plugins-dist/mcp/mcp.js +638 -577
  125. package/plugins-dist/mcp/plugin.json +3 -1
  126. package/plugins-dist/mcp/project.js +98 -115
  127. package/plugins-dist/memory/index.js +83 -88
  128. package/plugins-dist/memory/memory.js +111 -128
  129. package/plugins-dist/memory/style.js +9 -14
  130. package/plugins-dist/mentions/attach.js +91 -99
  131. package/plugins-dist/mentions/candidates.js +49 -0
  132. package/plugins-dist/mentions/index.js +71 -81
  133. package/plugins-dist/mentions/plugin.json +2 -1
  134. package/plugins-dist/mentions/style.js +9 -14
  135. package/plugins-dist/models/config.js +29 -43
  136. package/plugins-dist/models/configure.js +392 -473
  137. package/plugins-dist/models/context.js +14 -22
  138. package/plugins-dist/models/index.js +182 -188
  139. package/plugins-dist/models/provider.js +243 -295
  140. package/plugins-dist/models/saved.js +59 -71
  141. package/plugins-dist/modes/index.js +138 -165
  142. package/plugins-dist/plugins/index.js +34 -34
  143. package/plugins-dist/print/index.js +192 -175
  144. package/plugins-dist/prompts/index.js +81 -89
  145. package/plugins-dist/prompts/notes.js +31 -37
  146. package/plugins-dist/prompts/prompts.js +155 -193
  147. package/plugins-dist/prompts/results.js +40 -53
  148. package/plugins-dist/prompts/sys.js +39 -50
  149. package/plugins-dist/renderers/index.js +32 -32
  150. package/plugins-dist/repeats/index.js +134 -151
  151. package/plugins-dist/schedule/cron.js +49 -56
  152. package/plugins-dist/schedule/index.js +229 -247
  153. package/plugins-dist/schedule/store.js +93 -109
  154. package/plugins-dist/scratch/index.js +12 -18
  155. package/plugins-dist/screenshot/browser.js +169 -0
  156. package/plugins-dist/screenshot/index.js +37 -45
  157. package/plugins-dist/screenshot/sys.js +53 -65
  158. package/plugins-dist/screenshot/tool.js +101 -151
  159. package/plugins-dist/sessions/index.js +149 -117
  160. package/plugins-dist/shell/index.js +117 -74
  161. package/plugins-dist/shell/plugin.json +1 -2
  162. package/plugins-dist/shell/renderers.js +28 -33
  163. package/plugins-dist/shell/service.js +17 -23
  164. package/plugins-dist/shell/shell.js +317 -320
  165. package/plugins-dist/shell/sys.js +64 -79
  166. package/plugins-dist/skills/index.js +193 -211
  167. package/plugins-dist/skills/install.js +122 -149
  168. package/plugins-dist/skills/project.js +65 -73
  169. package/plugins-dist/skills/skills.js +135 -171
  170. package/plugins-dist/steer/index.js +108 -125
  171. package/plugins-dist/suggest/index.js +235 -259
  172. package/plugins-dist/task/index.js +130 -148
  173. package/plugins-dist/task/plugin.json +5 -4
  174. package/plugins-dist/task/style.js +9 -14
  175. package/plugins-dist/task/subagent.js +156 -279
  176. package/plugins-dist/todo/index.js +79 -63
  177. package/plugins-dist/tools/index.js +43 -0
  178. package/plugins-dist/tools/plugin.json +9 -0
  179. package/plugins-dist/tui/editor.js +145 -160
  180. package/plugins-dist/tui/frontend.js +641 -655
  181. package/plugins-dist/tui/history.js +34 -37
  182. package/plugins-dist/tui/index.js +16 -18
  183. package/plugins-dist/tui/markdown.js +203 -247
  184. package/plugins-dist/tui/mascot.js +266 -353
  185. package/plugins-dist/tui/mentions.js +67 -144
  186. package/plugins-dist/tui/plugin.json +1 -2
  187. package/plugins-dist/tui/render.js +35 -45
  188. package/plugins-dist/tui/screen.js +197 -180
  189. package/plugins-dist/tui/transcript.js +436 -468
  190. package/plugins-dist/tui/tui.js +1554 -1645
  191. package/plugins-dist/tui/vendor.js +1388 -1088
  192. package/plugins-dist/undo/checkpoint.js +95 -118
  193. package/plugins-dist/undo/index.js +185 -192
  194. package/plugins-dist/undo/snapshot.js +83 -99
  195. package/plugins-dist/undo/style.js +9 -14
  196. package/plugins-dist/update/index.js +26 -28
  197. package/plugins-dist/web/env.js +55 -68
  198. package/plugins-dist/web/index.js +80 -110
  199. package/plugins-dist/web/plugin.json +1 -2
  200. package/plugins-dist/web/renderers.js +4 -5
  201. package/plugins-dist/web/web.js +514 -593
  202. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  203. package/sdk/python/jade_plugin.py +9 -2
  204. package/src/api.ts +1 -1
  205. package/src/host.ts +39 -4
  206. package/src/remote.ts +6 -1
  207. package/src/session.ts +489 -34
@@ -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
- /** 转义序列(CSI、OSC、DCS 这类字符串、其余 ESC 开头的)和换行、制表符之外的控制字符(含 C1) */
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
- const lines = text.replaceAll("\r\n", "\n").split("\n");
37
- return lines
38
- .map((l) => (l.includes("\r") ? (l.split("\r").filter(Boolean).at(-1) ?? "") : l).replace(CONTROL, (m) => (SGR.test(m) ? m : "")))
39
- .join("\n");
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
- const s = terminalSafe;
44
- const opt = (v) => (v === undefined ? undefined : s(v));
45
- const lf = (t) => t.replace(/\r\n?/g, "\n");
46
- const plain = (t) => stripVTControlCharacters(s(t));
47
- /** 流式文字末尾还没写完的转义序列,等下一段 */
48
- let held;
49
- return {
50
- interactive: screen.interactive,
51
- log: (text, o) => screen.log(s(text), o && { ...o, details: opt(o.details) }),
52
- amend: (id, text, o) => screen.amend(id, s(text), o && { ...o, details: opt(o.details) }),
53
- progress: (id, line, o) => screen.progress(id, opt(line), o),
54
- stream: (kind, text) => {
55
- // 上一段末尾没完的转义序列接到这一段前面再过滤,不然它被拆成两半,后一半当成文字显示出来
56
- if (held && held.kind !== kind)
57
- held = undefined;
58
- const whole = (held?.text ?? "") + text;
59
- // 很长还没结束的(OSC 之类的字符串一直不给结束符)不再等,按现在的样子过滤
60
- const found = whole.search(UNFINISHED);
61
- const cut = found >= 0 && whole.length - found <= MAX_HELD ? found : -1;
62
- held = cut >= 0 ? { kind, text: whole.slice(cut) } : undefined;
63
- const shown = s(cut >= 0 ? whole.slice(0, cut) : whole);
64
- if (shown)
65
- screen.stream(kind, shown);
66
- },
67
- endStream: () => {
68
- // 到结束都没写完的序列不完整,丢掉
69
- held = undefined;
70
- screen.endStream();
71
- },
72
- busy: (label) => screen.busy(opt(label)),
73
- status: (info) => {
74
- const out = { ...info };
75
- for (const k of ["cwd", "branch", "model", "mode", "session"])
76
- if (typeof info[k] === "string")
77
- out[k] = s(info[k]);
78
- if (info.plugins)
79
- out.plugins = info.plugins.map((p) => ({ plugin: p.plugin, text: s(p.text) }));
80
- screen.status(out);
81
- },
82
- queued: (items) => screen.queued(items.map(s)),
83
- select: (title, choices, o) => screen.select(s(title), choices.map((c) => ({ ...c, label: s(c.label), hint: opt(c.hint) })), o && { ...o, body: opt(o.body) }),
84
- input: (title, o) => screen.input(s(title), o && { ...o, initial: opt(o.initial), placeholder: opt(o.placeholder) }),
85
- // 输入框里的 \r 是换行(旧 Mac 的换行符),不是进度条:先换成 \n,terminalSafe 才不会只留最后一段。
86
- // 输入框里是要发给模型的纯文字,颜色也去掉
87
- draft: screen.draft && {
88
- text: () => screen.draft.text(),
89
- insert: (t) => screen.draft.insert(plain(lf(t))),
90
- set: (t) => screen.draft.set(plain(lf(t))),
91
- hint: (t) => screen.draft.hint(t === undefined ? undefined : plain(lf(t))),
92
- },
93
- ...(screen.copy ? { copy: (t) => screen.copy(t) } : {}),
94
- ...(screen.suspend ? { suspend: (fn) => screen.suspend(fn) } : {}),
95
- clear: () => screen.clear(),
96
- close: () => screen.close(),
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
- let atLineStart = true;
103
- let streaming;
104
- let rl;
105
- /** 每个工具已经打印了多少行输出 */
106
- const outputLines = new Map();
107
- const write = (s) => {
108
- if (!s)
109
- return;
110
- process.stdout.write(s);
111
- atLineStart = s.endsWith("\n");
112
- };
113
- const line = (s) => {
114
- if (!atLineStart)
115
- write("\n");
116
- write(s + "\n");
117
- };
118
- /** 中止时返回 undefined */
119
- const ask = async (q, signal) => {
120
- rl ??= createInterface({ input: process.stdin, output: process.stdout, terminal: true });
121
- try {
122
- return (await rl.question(q, { signal })).trim();
123
- }
124
- catch (err) {
125
- if (!signal?.aborted)
126
- throw err;
127
- write("\n");
128
- return undefined;
129
- }
130
- };
131
- return {
132
- interactive: opts.interactive,
133
- log: (text) => {
134
- streaming = undefined;
135
- line(text);
136
- },
137
- amend: (_id, text) => {
138
- streaming = undefined;
139
- line(` ${text}`);
140
- },
141
- progress: (id, text, o) => {
142
- if (text === undefined)
143
- return void outputLines.delete(id);
144
- streaming = undefined;
145
- if (o?.output) {
146
- const shown = outputLines.get(id) ?? 0;
147
- if (shown > MAX_OUTPUT_LINES)
148
- return;
149
- outputLines.set(id, shown + 1);
150
- if (shown === MAX_OUTPUT_LINES)
151
- return line(dim(" │ …"));
152
- }
153
- line(text);
154
- },
155
- stream: (kind, text) => {
156
- if (kind !== streaming) {
157
- // 不少模型的正文以空行开头;整块都是换行时等下一块
158
- if (kind === "text")
159
- text = text.replace(/^\n+/, "");
160
- if (!text)
161
- return;
162
- if (kind === "thinking")
163
- line(dim("thinking…"));
164
- else if (!atLineStart)
165
- write("\n");
166
- streaming = kind;
167
- }
168
- write(kind === "thinking" ? dim(text) : text);
169
- },
170
- endStream: () => {
171
- streaming = undefined;
172
- if (!atLineStart)
173
- write("\n");
174
- },
175
- busy: () => { },
176
- status: () => { },
177
- queued: () => { },
178
- async select(title, choices, o) {
179
- if (!opts.interactive)
180
- return undefined;
181
- line(yellow(title));
182
- if (o?.body)
183
- line(o.body);
184
- choices.forEach((c, i) => line(` ${i + 1}. ${c.label}${c.hint ? dim(` ${c.hint}`) : ""}`));
185
- if (o?.signal?.aborted)
186
- return undefined;
187
- const n = Number(await ask(`Choose 1-${choices.length} (empty to cancel): `, o?.signal));
188
- return Number.isInteger(n) && n >= 1 && n <= choices.length ? choices[n - 1].value : undefined;
189
- },
190
- async input(title, o) {
191
- if (!opts.interactive)
192
- return undefined;
193
- if (o?.signal?.aborted)
194
- return undefined;
195
- const answer = await ask(`${title}${o?.initial ? ` [${o.initial}]` : ""}: `, o?.signal);
196
- return answer === undefined ? undefined : answer || o?.initial || "";
197
- },
198
- clear: () => { },
199
- async close() {
200
- rl?.close();
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
  }