@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.
Files changed (206) 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 +490 -29
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +36 -14
  14. package/plugins/approval/policy.ts +15 -10
  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 +42 -9
  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 +47 -12
  50. package/plugins/models/index.ts +27 -1
  51. package/plugins/models/provider.ts +24 -0
  52. package/plugins/models/saved.ts +1 -1
  53. package/plugins/modes/index.ts +13 -2
  54. package/plugins/print/index.ts +7 -4
  55. package/plugins/prompts/index.ts +0 -0
  56. package/plugins/renderers/index.ts +10 -5
  57. package/plugins/repeats/index.ts +8 -8
  58. package/plugins/schedule/index.ts +5 -1
  59. package/plugins/screenshot/browser.ts +10 -0
  60. package/plugins/sessions/index.ts +48 -2
  61. package/plugins/shell/index.ts +27 -28
  62. package/plugins/shell/plugin.json +1 -1
  63. package/plugins/shell/renderers.ts +9 -7
  64. package/plugins/shell/shell.ts +3 -1
  65. package/plugins/skills/index.ts +28 -19
  66. package/plugins/skills/project.ts +12 -7
  67. package/plugins/steer/index.ts +5 -4
  68. package/plugins/task/index.ts +80 -50
  69. package/plugins/task/plugin.json +6 -2
  70. package/plugins/task/style.ts +13 -14
  71. package/plugins/task/subagent.ts +67 -184
  72. package/plugins/todo/index.ts +39 -9
  73. package/plugins/tools/index.ts +9 -2
  74. package/plugins/tui/frontend.ts +139 -89
  75. package/plugins/tui/index.ts +2 -3
  76. package/plugins/tui/mentions.ts +11 -80
  77. package/plugins/tui/plugin.json +1 -1
  78. package/plugins/tui/screen.ts +55 -5
  79. package/plugins/tui/transcript.ts +17 -7
  80. package/plugins/tui/tui.ts +337 -101
  81. package/plugins/tui/vendor.ts +1 -1
  82. package/plugins/undo/checkpoint.ts +8 -4
  83. package/plugins/undo/index.ts +170 -31
  84. package/plugins/undo/plugin.json +1 -0
  85. package/plugins/undo/snapshot.ts +87 -14
  86. package/plugins/undo/style.ts +13 -14
  87. package/plugins/web/index.ts +7 -18
  88. package/plugins/web/plugin.json +1 -1
  89. package/plugins-dist/approval/index.js +363 -418
  90. package/plugins-dist/approval/policy.js +391 -508
  91. package/plugins-dist/approval/review.js +132 -158
  92. package/plugins-dist/approval/schema.js +50 -55
  93. package/plugins-dist/ask/index.js +115 -129
  94. package/plugins-dist/budget/index.js +27 -31
  95. package/plugins-dist/checks/checks.js +293 -329
  96. package/plugins-dist/checks/index.js +119 -139
  97. package/plugins-dist/checks/plugin.json +2 -1
  98. package/plugins-dist/checks/project.js +57 -65
  99. package/plugins-dist/commands/index.js +47 -29
  100. package/plugins-dist/compact/compact.js +233 -249
  101. package/plugins-dist/compact/index.js +98 -95
  102. package/plugins-dist/compact/style.js +9 -14
  103. package/plugins-dist/completions/index.js +30 -0
  104. package/plugins-dist/completions/plugin.json +9 -0
  105. package/plugins-dist/continue/index.js +40 -52
  106. package/plugins-dist/copy/index.js +40 -0
  107. package/plugins-dist/copy/plugin.json +9 -0
  108. package/plugins-dist/cost/index.js +98 -115
  109. package/plugins-dist/custom-commands/index.js +55 -72
  110. package/plugins-dist/editor/index.js +64 -72
  111. package/plugins-dist/fs/diff.js +111 -124
  112. package/plugins-dist/fs/fs.js +426 -469
  113. package/plugins-dist/fs/grep.js +262 -253
  114. package/plugins-dist/fs/index.js +20 -34
  115. package/plugins-dist/fs/plugin.json +1 -2
  116. package/plugins-dist/fs/renderers.js +100 -112
  117. package/plugins-dist/fs/style.js +11 -15
  118. package/plugins-dist/fs/sys.js +31 -43
  119. package/plugins-dist/hooks/index.js +107 -127
  120. package/plugins-dist/keys/index.js +27 -31
  121. package/plugins-dist/mcp/index.js +271 -199
  122. package/plugins-dist/mcp/install.js +123 -137
  123. package/plugins-dist/mcp/mcp.js +638 -577
  124. package/plugins-dist/mcp/plugin.json +3 -1
  125. package/plugins-dist/mcp/project.js +98 -115
  126. package/plugins-dist/memory/index.js +83 -88
  127. package/plugins-dist/memory/memory.js +111 -128
  128. package/plugins-dist/memory/style.js +9 -14
  129. package/plugins-dist/mentions/attach.js +91 -99
  130. package/plugins-dist/mentions/candidates.js +49 -0
  131. package/plugins-dist/mentions/index.js +71 -81
  132. package/plugins-dist/mentions/plugin.json +2 -1
  133. package/plugins-dist/mentions/style.js +9 -14
  134. package/plugins-dist/models/config.js +29 -43
  135. package/plugins-dist/models/configure.js +421 -474
  136. package/plugins-dist/models/context.js +14 -22
  137. package/plugins-dist/models/index.js +182 -189
  138. package/plugins-dist/models/provider.js +263 -307
  139. package/plugins-dist/models/saved.js +60 -72
  140. package/plugins-dist/modes/index.js +138 -165
  141. package/plugins-dist/plugins/index.js +34 -34
  142. package/plugins-dist/print/index.js +192 -175
  143. package/plugins-dist/prompts/index.js +86 -89
  144. package/plugins-dist/prompts/notes.js +31 -37
  145. package/plugins-dist/prompts/prompts.js +155 -193
  146. package/plugins-dist/prompts/results.js +40 -53
  147. package/plugins-dist/prompts/sys.js +39 -50
  148. package/plugins-dist/renderers/index.js +32 -32
  149. package/plugins-dist/repeats/index.js +131 -174
  150. package/plugins-dist/schedule/cron.js +49 -56
  151. package/plugins-dist/schedule/index.js +230 -247
  152. package/plugins-dist/schedule/store.js +93 -109
  153. package/plugins-dist/scratch/index.js +12 -18
  154. package/plugins-dist/screenshot/browser.js +136 -154
  155. package/plugins-dist/screenshot/index.js +37 -46
  156. package/plugins-dist/screenshot/sys.js +53 -65
  157. package/plugins-dist/screenshot/tool.js +98 -126
  158. package/plugins-dist/sessions/index.js +149 -117
  159. package/plugins-dist/shell/index.js +117 -135
  160. package/plugins-dist/shell/plugin.json +1 -1
  161. package/plugins-dist/shell/renderers.js +28 -33
  162. package/plugins-dist/shell/service.js +17 -23
  163. package/plugins-dist/shell/shell.js +307 -362
  164. package/plugins-dist/shell/sys.js +64 -79
  165. package/plugins-dist/skills/index.js +193 -211
  166. package/plugins-dist/skills/install.js +122 -149
  167. package/plugins-dist/skills/project.js +65 -73
  168. package/plugins-dist/skills/skills.js +135 -171
  169. package/plugins-dist/steer/index.js +108 -125
  170. package/plugins-dist/suggest/index.js +235 -259
  171. package/plugins-dist/task/index.js +130 -117
  172. package/plugins-dist/task/plugin.json +5 -2
  173. package/plugins-dist/task/style.js +9 -14
  174. package/plugins-dist/task/subagent.js +156 -279
  175. package/plugins-dist/todo/index.js +79 -63
  176. package/plugins-dist/tools/index.js +36 -42
  177. package/plugins-dist/tui/editor.js +145 -160
  178. package/plugins-dist/tui/frontend.js +639 -678
  179. package/plugins-dist/tui/history.js +34 -37
  180. package/plugins-dist/tui/index.js +16 -18
  181. package/plugins-dist/tui/markdown.js +203 -247
  182. package/plugins-dist/tui/mascot.js +266 -355
  183. package/plugins-dist/tui/mentions.js +67 -144
  184. package/plugins-dist/tui/plugin.json +1 -2
  185. package/plugins-dist/tui/render.js +35 -45
  186. package/plugins-dist/tui/screen.js +197 -180
  187. package/plugins-dist/tui/transcript.js +436 -468
  188. package/plugins-dist/tui/tui.js +1554 -1656
  189. package/plugins-dist/tui/vendor.js +1088 -1388
  190. package/plugins-dist/undo/checkpoint.js +97 -118
  191. package/plugins-dist/undo/index.js +281 -192
  192. package/plugins-dist/undo/plugin.json +3 -0
  193. package/plugins-dist/undo/snapshot.js +134 -99
  194. package/plugins-dist/undo/style.js +9 -14
  195. package/plugins-dist/update/index.js +26 -28
  196. package/plugins-dist/web/env.js +55 -68
  197. package/plugins-dist/web/index.js +80 -110
  198. package/plugins-dist/web/plugin.json +1 -2
  199. package/plugins-dist/web/renderers.js +4 -5
  200. package/plugins-dist/web/web.js +514 -593
  201. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  202. package/sdk/python/jade_plugin.py +9 -2
  203. package/src/api.ts +1 -1
  204. package/src/host.ts +39 -4
  205. package/src/remote.ts +6 -1
  206. 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
- /** 转义序列(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
  }