@uncarved/jade 0.1.0 → 0.1.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 +6 -2
- package/dist/distro.d.ts +4 -0
- package/dist/distro.js +36 -2
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +110 -20
- package/dist/main.js.map +1 -1
- package/dist/plugins/policy.d.ts +21 -5
- package/dist/plugins/policy.js +325 -53
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +2 -0
- package/dist/plugins/prompts.js +13 -0
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.js +42 -7
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/provider.d.ts +62 -3
- package/dist/provider.js +145 -6
- package/dist/provider.js.map +1 -1
- package/dist/sessions.d.ts +9 -0
- package/dist/sessions.js +14 -1
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +5 -1
- package/dist/sys.js +3 -2
- package/dist/sys.js.map +1 -1
- package/dist/tools/screenshot.d.ts +15 -0
- package/dist/tools/screenshot.js +162 -0
- package/dist/tools/screenshot.js.map +1 -0
- package/dist/tools/shell.d.ts +6 -0
- package/dist/tools/shell.js +26 -7
- package/dist/tools/shell.js.map +1 -1
- package/dist/ui/configure.js +89 -5
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/screen.d.ts +5 -0
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.js +36 -0
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +17 -0
- package/dist/ui/tui.js +298 -75
- package/dist/ui/tui.js.map +1 -1
- package/package.json +3 -3
- package/src/distro.ts +40 -3
- package/src/index.ts +1 -0
- package/src/main.ts +106 -19
- package/src/plugins/policy.ts +317 -43
- package/src/plugins/prompts.ts +15 -0
- package/src/plugins/repeats.ts +42 -8
- package/src/provider.ts +165 -10
- package/src/sessions.ts +14 -1
- package/src/sys.ts +5 -2
- package/src/tools/screenshot.ts +171 -0
- package/src/tools/shell.ts +25 -7
- package/src/ui/configure.ts +95 -8
- package/src/ui/screen.ts +2 -0
- package/src/ui/transcript.ts +32 -0
- package/src/ui/tui.tsx +410 -122
package/dist/ui/tui.js
CHANGED
|
@@ -5,10 +5,20 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
5
5
|
import { writeSync } from "node:fs";
|
|
6
6
|
import { Box, render, Text, useBoxMetrics, useCursor, useInput, usePaste, useWindowSize, } from "ink";
|
|
7
7
|
import { useEffect, useReducer, useRef, useState, useSyncExternalStore, } from "react";
|
|
8
|
+
import stringWidth from "string-width";
|
|
8
9
|
import wrapAnsi from "wrap-ansi";
|
|
9
10
|
import { mentionAt, mentionText, rankPaths } from "../mentions.js";
|
|
10
11
|
import { edit, empty, layout } from "./editor.js";
|
|
11
12
|
import { dim, } from "./screen.js";
|
|
13
|
+
/**
|
|
14
|
+
* 界面可用的尺寸:宽度比终端少一列。
|
|
15
|
+
* Ink 增量重画时每行写成「内容 + ESC[K」;一行正好占满终端宽度时光标停在最后一列(等待换行),
|
|
16
|
+
* 紧跟的 ESC[K 会把最后一个字符擦掉,输入框之类的右边框就缺了一块。留出最后一列就不会这样
|
|
17
|
+
*/
|
|
18
|
+
function useScreenSize() {
|
|
19
|
+
const { columns, rows } = useWindowSize();
|
|
20
|
+
return { columns: Math.max(1, columns - 1), rows };
|
|
21
|
+
}
|
|
12
22
|
/** 对话最多保留这么多行(折行前),超出时丢掉最早的一段 */
|
|
13
23
|
const MAX_LINES = 10_000;
|
|
14
24
|
/** 按显示宽度折行,保留颜色;每一项是屏幕上的一行 */
|
|
@@ -21,11 +31,59 @@ function oneRow(text, width) {
|
|
|
21
31
|
}
|
|
22
32
|
/** 这一项能不能展开:有折叠的内容,或者收起时被截掉了一部分 */
|
|
23
33
|
function foldable(l, width) {
|
|
34
|
+
if (l.think)
|
|
35
|
+
return l.think.text.trim() !== "";
|
|
24
36
|
return l.details !== undefined || (Boolean(l.oneRow) && oneRow(l.text, width).cut);
|
|
25
37
|
}
|
|
26
|
-
|
|
27
|
-
|
|
38
|
+
const elapsed = (t) => `${Math.max(0, Math.round(((t.end ?? Date.now()) - t.start) / 1000))}s`;
|
|
39
|
+
/** 按显示宽度取 text 末尾最多 cols 列;截掉了开头时以 … 开头 */
|
|
40
|
+
export function tail(text, cols) {
|
|
41
|
+
if (stringWidth(text) <= cols)
|
|
42
|
+
return text;
|
|
43
|
+
const chars = [...text];
|
|
44
|
+
let out = "";
|
|
45
|
+
let w = 1;
|
|
46
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
47
|
+
const cw = stringWidth(chars[i]);
|
|
48
|
+
if (w + cw > cols)
|
|
49
|
+
break;
|
|
50
|
+
out = chars[i] + out;
|
|
51
|
+
w += cw;
|
|
52
|
+
}
|
|
53
|
+
return `…${out}`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 思考收起时的一行(不含展开标记):思考中显示"✻ Thinking 12s · "加上最新一行的末尾,跟着模型实时往前走;
|
|
57
|
+
* 结束后显示"✻ Thought for 12s"
|
|
58
|
+
*/
|
|
59
|
+
function thinkingRow(t, width) {
|
|
60
|
+
if (t.end !== undefined)
|
|
61
|
+
return dim(`✻ Thought for ${elapsed(t)}`);
|
|
62
|
+
const label = `✻ Thinking ${elapsed(t)}`;
|
|
63
|
+
const latest = t.text
|
|
64
|
+
.split("\n")
|
|
65
|
+
.findLast((l) => l.trim())
|
|
66
|
+
?.trim()
|
|
67
|
+
.replace(/\s+/g, " ");
|
|
68
|
+
// 留出 " · "、" ▸" 和一列余量
|
|
69
|
+
const room = width - stringWidth(label) - 6;
|
|
70
|
+
return dim(latest && room >= 8 ? `${label} · ${tail(latest, room)}` : `${label}…`);
|
|
71
|
+
}
|
|
28
72
|
function wrapLine(l, width) {
|
|
73
|
+
if (l.think) {
|
|
74
|
+
if (!foldable(l, width))
|
|
75
|
+
return [thinkingRow(l.think, width)];
|
|
76
|
+
if (!l.open)
|
|
77
|
+
return [`${thinkingRow(l.think, width)}${dim(" ▸")}`];
|
|
78
|
+
const t = l.think;
|
|
79
|
+
const title = dim(t.end === undefined ? `✻ Thinking ${elapsed(t)}` : `✻ Thought for ${elapsed(t)}`);
|
|
80
|
+
const body = t.text
|
|
81
|
+
.replace(/\s+$/, "")
|
|
82
|
+
.split("\n")
|
|
83
|
+
.map((r) => (r ? dim(` ${r}`) : r))
|
|
84
|
+
.join("\n");
|
|
85
|
+
return [...wrap(`${title}${dim(" ▾")}`, width), ...wrap(body, width)];
|
|
86
|
+
}
|
|
29
87
|
if (!foldable(l, width))
|
|
30
88
|
return wrap(l.text, width);
|
|
31
89
|
if (!l.open) {
|
|
@@ -184,38 +242,38 @@ export function tuiScreen(opts) {
|
|
|
184
242
|
const stdout = opts.io?.stdout ?? process.stdout;
|
|
185
243
|
const store = new Store(opts.status);
|
|
186
244
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
245
|
+
* 正在进行的思考:一开始就作为一项放进对话(可以点击展开),之后的增量攒起来,每隔一小段时间改写一次那一项。
|
|
246
|
+
* 收起时只占一行,显示模型最新想到了哪里;结束时换成用了多久
|
|
189
247
|
*/
|
|
248
|
+
let thinking;
|
|
249
|
+
let thinkingCount = 0;
|
|
250
|
+
const redrawThinking = () => {
|
|
251
|
+
if (!thinking)
|
|
252
|
+
return;
|
|
253
|
+
clearTimeout(thinking.timer);
|
|
254
|
+
thinking.timer = undefined;
|
|
255
|
+
store.amend(thinking.id, () => { });
|
|
256
|
+
};
|
|
257
|
+
const endThinking = () => {
|
|
258
|
+
if (!thinking)
|
|
259
|
+
return;
|
|
260
|
+
thinking.think.end = Date.now();
|
|
261
|
+
redrawThinking();
|
|
262
|
+
thinking = undefined;
|
|
263
|
+
};
|
|
264
|
+
/** 推进正文:已经完整的行移进对话,只把最后不完整的一行留在底部;all 时全部移进去,连同思考一起结束 */
|
|
190
265
|
const flushLive = (all = false) => {
|
|
266
|
+
if (all)
|
|
267
|
+
endThinking();
|
|
191
268
|
const live = store.state.live;
|
|
192
269
|
if (!live)
|
|
193
270
|
return;
|
|
194
|
-
if (live.kind === "thinking") {
|
|
195
|
-
if (!all)
|
|
196
|
-
return;
|
|
197
|
-
store.update({ live: undefined });
|
|
198
|
-
const text = live.text.replace(/\s+$/, "");
|
|
199
|
-
if (!text)
|
|
200
|
-
return;
|
|
201
|
-
store.append([thinkingHead(text)], {
|
|
202
|
-
oneRow: true,
|
|
203
|
-
title: dim("✻ thinking"),
|
|
204
|
-
details: text
|
|
205
|
-
.split("\n")
|
|
206
|
-
.map((l) => (l ? dim(` ${l}`) : l))
|
|
207
|
-
.join("\n"),
|
|
208
|
-
});
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
271
|
const lines = live.text.split("\n");
|
|
212
272
|
const rest = all ? "" : lines.pop();
|
|
213
273
|
if (all && lines.at(-1) === "")
|
|
214
274
|
lines.pop();
|
|
215
275
|
store.append(lines);
|
|
216
|
-
store.update({
|
|
217
|
-
live: rest || !all ? { kind: live.kind, text: rest } : undefined,
|
|
218
|
-
});
|
|
276
|
+
store.update({ live: rest || !all ? { text: rest } : undefined });
|
|
219
277
|
};
|
|
220
278
|
const ask = (dialog) => new Promise((resolve) => {
|
|
221
279
|
const d = {
|
|
@@ -276,20 +334,29 @@ export function tuiScreen(opts) {
|
|
|
276
334
|
});
|
|
277
335
|
},
|
|
278
336
|
stream(kind, text) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
337
|
+
if (kind === "thinking") {
|
|
338
|
+
if (store.state.live) {
|
|
339
|
+
flushLive(true);
|
|
340
|
+
store.update({ live: undefined });
|
|
341
|
+
}
|
|
342
|
+
if (!thinking) {
|
|
343
|
+
if (!text.trim())
|
|
344
|
+
return;
|
|
345
|
+
const think = { text: "", start: Date.now() };
|
|
346
|
+
thinking = { id: `\0thinking-${thinkingCount++}`, think };
|
|
347
|
+
store.append([""], { id: thinking.id, think });
|
|
348
|
+
}
|
|
349
|
+
thinking.think.text += text;
|
|
350
|
+
thinking.timer ??= setTimeout(redrawThinking, 50);
|
|
351
|
+
return;
|
|
283
352
|
}
|
|
353
|
+
endThinking();
|
|
284
354
|
if (!store.state.live) {
|
|
285
|
-
|
|
286
|
-
text = text.replace(/^\n+/, "");
|
|
355
|
+
text = text.replace(/^\n+/, "");
|
|
287
356
|
if (!text)
|
|
288
357
|
return;
|
|
289
358
|
}
|
|
290
|
-
store.update({
|
|
291
|
-
live: { kind, text: (store.state.live?.text ?? "") + text },
|
|
292
|
-
});
|
|
359
|
+
store.update({ live: { text: (store.state.live?.text ?? "") + text } });
|
|
293
360
|
if (text.includes("\n"))
|
|
294
361
|
flushLive();
|
|
295
362
|
},
|
|
@@ -324,6 +391,8 @@ export function tuiScreen(opts) {
|
|
|
324
391
|
resolve: () => { },
|
|
325
392
|
}),
|
|
326
393
|
clear() {
|
|
394
|
+
clearTimeout(thinking?.timer);
|
|
395
|
+
thinking = undefined;
|
|
327
396
|
store.clear();
|
|
328
397
|
},
|
|
329
398
|
async close() {
|
|
@@ -345,10 +414,17 @@ export function tuiScreen(opts) {
|
|
|
345
414
|
*/
|
|
346
415
|
const MOUSE = /^\[<(\d+);\d+;(\d+)([Mm])$/;
|
|
347
416
|
const WHEEL_LINES = 3;
|
|
417
|
+
/** 终端矮于这么多行时标题只占一行 */
|
|
418
|
+
const COMPACT_ROWS = 24;
|
|
419
|
+
/**
|
|
420
|
+
* 底部的菜单、选择框、多行输入最多占多少行:终端很矮时整个界面会高过终端,
|
|
421
|
+
* 超出的部分被裁掉,输入框的边框和状态栏就显示不全了
|
|
422
|
+
*/
|
|
423
|
+
const roomFor = (rows, max) => Math.max(3, Math.min(max, Math.floor((rows - 10) / 2)));
|
|
348
424
|
function App({ store, version, commands, handlers, history, }) {
|
|
349
425
|
const state = useSyncExternalStore(store.subscribe, store.get);
|
|
350
426
|
const dialog = state.dialogs[0];
|
|
351
|
-
const { columns, rows } =
|
|
427
|
+
const { columns, rows } = useScreenSize();
|
|
352
428
|
// 对话区占掉标题和底部之外的全部高度,只画能看见的那几行
|
|
353
429
|
const ref = useRef(null);
|
|
354
430
|
const { height, top } = useBoxMetrics(ref);
|
|
@@ -356,11 +432,7 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
356
432
|
const done = store.lines(width);
|
|
357
433
|
// 还没结束的流式块和正在运行的工具的进度:思考只显示最新的一行,进度每个工具一行
|
|
358
434
|
const live = [
|
|
359
|
-
...(state.live?.text
|
|
360
|
-
? state.live.kind === "thinking"
|
|
361
|
-
? [oneRow(thinkingHead(state.live.text), width).row]
|
|
362
|
-
: wrap(state.live.text, width)
|
|
363
|
-
: []),
|
|
435
|
+
...(state.live?.text ? wrap(state.live.text, width) : []),
|
|
364
436
|
...state.progress.map((p) => oneRow(p.line, width).row),
|
|
365
437
|
];
|
|
366
438
|
const total = done.length + live.length;
|
|
@@ -424,7 +496,7 @@ function App({ store, version, commands, handlers, history, }) {
|
|
|
424
496
|
// 总高度要比终端少一行,否则 Ink 认为输出占满了屏幕,每帧清屏重画。
|
|
425
497
|
// 重画那一帧上下各垫一行空白:终端把顶上那行滚出去后,看到的和平时逐行一样,所以不会闪。
|
|
426
498
|
// 只做纵向裁剪:每行都已按宽度折好或截断,横向裁剪会让 Ink 每帧对每一行再切一遍 ANSI 字符串
|
|
427
|
-
return (_jsx(Box, { flexDirection: "column", width: columns, height: repaint ? rows + 1 : Math.max(1, rows - 1), paddingY: repaint ? 1 : 0, overflowY: "hidden", children: _jsxs(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden", children: [_jsx(Header, { status: state.status, version: version }), _jsxs(Box, { ref: ref, flexDirection: "column", flexGrow: 1, flexShrink: 1, flexBasis: 0, overflowY: "hidden", paddingX: 1, children: [shown.map((l, i) => (_jsx(Text, { wrap: "truncate-end", children: l || " " }, i))), scrolled ? (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──` })) : null] }), _jsxs(Box, { flexDirection: "column", flexShrink: 0, children: [state.busy && !dialog ? (_jsx(Spinner, { label: state.busy.label, since: state.busy.since })) : null, state.queued.map((q, i) => (_jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [" ↳ queued: ", q.replace(/\s+/g, " ")] }, i))), dialog ? (dialog.kind === "select" ? (_jsx(SelectDialog, { dialog: dialog }, dialogKey(dialog))) : (_jsx(InputDialog, { dialog: dialog }, dialogKey(dialog)))) : null, _jsx(Prompt, { commands: commands, handlers: handlers, history: history, busy: Boolean(state.busy), active: !dialog, onSubmit: () => store.scrollBy(-max, max) }), _jsx(StatusBar, { status: state.status })] })] }) }));
|
|
499
|
+
return (_jsx(Box, { flexDirection: "column", width: columns, height: repaint ? rows + 1 : Math.max(1, rows - 1), paddingY: repaint ? 1 : 0, overflowY: "hidden", children: _jsxs(Box, { flexDirection: "column", flexGrow: 1, overflowY: "hidden", children: [_jsx(Header, { status: state.status, version: version, compact: rows < COMPACT_ROWS }), _jsxs(Box, { ref: ref, flexDirection: "column", flexGrow: 1, flexShrink: 1, flexBasis: 0, overflowY: "hidden", paddingX: 1, children: [shown.map((l, i) => (_jsx(Text, { wrap: "truncate-end", children: l || " " }, i))), scrolled ? (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: `── ↓ ${scroll} more lines · wheel / PgDn to scroll · ctrl+end to jump to the bottom ──` })) : null] }), _jsxs(Box, { flexDirection: "column", flexShrink: 0, children: [state.busy && !dialog ? (_jsx(Spinner, { label: state.busy.label, since: state.busy.since })) : null, state.queued.map((q, i) => (_jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [" ↳ queued: ", q.replace(/\s+/g, " ")] }, i))), dialog ? (dialog.kind === "select" ? (_jsx(SelectDialog, { dialog: dialog }, dialogKey(dialog))) : (_jsx(InputDialog, { dialog: dialog }, dialogKey(dialog)))) : null, _jsx(Prompt, { commands: commands, handlers: handlers, history: history, busy: Boolean(state.busy), active: !dialog, onSubmit: () => store.scrollBy(-max, max) }), _jsx(StatusBar, { status: state.status, width: Math.max(10, columns - 2) })] })] }) }));
|
|
428
500
|
}
|
|
429
501
|
const JADE = "#00a86b";
|
|
430
502
|
const SHINE = "#b6f2d4";
|
|
@@ -435,8 +507,11 @@ const SHINE = "#b6f2d4";
|
|
|
435
507
|
function Bot() {
|
|
436
508
|
return (_jsxs(Box, { flexDirection: "column", flexShrink: 0, marginRight: 2, children: [_jsxs(Text, { color: JADE, children: [" ▄", _jsx(Text, { color: SHINE, backgroundColor: JADE, children: "\u2584" }), "████▄"] }), _jsx(Text, { color: JADE, children: "\u2588\u2588 \u2588\u2588\u2588 \u2588\u2588" }), _jsx(Text, { color: JADE, children: "\u2580\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2580" })] }));
|
|
437
509
|
}
|
|
438
|
-
function Header({ status, version }) {
|
|
439
|
-
|
|
510
|
+
function Header({ status, version, compact, }) {
|
|
511
|
+
const { columns } = useScreenSize();
|
|
512
|
+
if (compact)
|
|
513
|
+
return (_jsx(Box, { flexShrink: 0, paddingX: 1, children: _jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", bold: true, children: "jade" }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { color: "cyan", children: status.model }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { children: status.cwd }), status.branch ? (_jsxs(Text, { color: "magenta", children: [" (", status.branch, ")"] })) : null] }) }));
|
|
514
|
+
return (_jsxs(Box, { flexShrink: 0, borderStyle: "round", borderColor: "cyan", paddingX: 1, children: [columns >= 50 ? _jsx(Bot, {}) : null, _jsxs(Box, { flexDirection: "column", flexGrow: 1, flexShrink: 1, children: [_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", bold: true, children: "jade" }), _jsxs(Text, { dimColor: true, children: [" v", version] }), status.session ? (_jsx(Text, { dimColor: true, children: ` · session ${status.session}` })) : null] }), _jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: status.model }), _jsx(Text, { dimColor: true, children: " \u00B7 " }), _jsx(Text, { children: status.cwd }), status.branch ? (_jsxs(Text, { color: "magenta", children: [" (", status.branch, ")"] })) : null] }), _jsx(Text, { dimColor: true, wrap: "truncate-end", children: "/help for commands \u00B7 @ to attach files" })] })] }));
|
|
440
515
|
}
|
|
441
516
|
const dialogIds = new WeakMap();
|
|
442
517
|
let dialogCounter = 0;
|
|
@@ -453,9 +528,31 @@ function Spinner({ label, since }) {
|
|
|
453
528
|
return () => clearInterval(t);
|
|
454
529
|
}, []);
|
|
455
530
|
const seconds = Math.floor((now - since) / 1000);
|
|
456
|
-
return (
|
|
531
|
+
return (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: "cyan", children: [FRAMES[Math.floor(now / 100) % FRAMES.length], " "] }), _jsxs(Text, { children: [label, "\u2026 "] }), _jsxs(Text, { dimColor: true, children: ["(", seconds, "s \u00B7 esc to interrupt)"] })] }) }));
|
|
457
532
|
}
|
|
458
|
-
|
|
533
|
+
/** 在 width 列里放下尽量多的内容(保持原来的顺序) */
|
|
534
|
+
export function fitSegments(segments, width) {
|
|
535
|
+
const kept = segments.map((s) => ({ ...s }));
|
|
536
|
+
const total = () => kept.reduce((n, s, i) => n + stringWidth(s.text) + (i ? 3 : 0), 0);
|
|
537
|
+
const steps = kept
|
|
538
|
+
.flatMap((s) => [
|
|
539
|
+
...(s.short !== undefined && s.shortAt !== undefined
|
|
540
|
+
? [{ s, at: s.shortAt, drop: false }]
|
|
541
|
+
: []),
|
|
542
|
+
...(s.dropAt !== undefined ? [{ s, at: s.dropAt, drop: true }] : []),
|
|
543
|
+
])
|
|
544
|
+
.sort((a, b) => b.at - a.at);
|
|
545
|
+
for (const { s, drop } of steps) {
|
|
546
|
+
if (total() <= width)
|
|
547
|
+
break;
|
|
548
|
+
if (drop)
|
|
549
|
+
kept.splice(kept.indexOf(s), 1);
|
|
550
|
+
else
|
|
551
|
+
s.text = s.short;
|
|
552
|
+
}
|
|
553
|
+
return kept;
|
|
554
|
+
}
|
|
555
|
+
function StatusBar({ status, width }) {
|
|
459
556
|
const ctx = status.context;
|
|
460
557
|
const pct = ctx ? Math.round((ctx.used / ctx.limit) * 100) : 0;
|
|
461
558
|
/** 离自动压缩还有多远:到阈值的 85% 起变黄 */
|
|
@@ -469,7 +566,62 @@ function StatusBar({ status }) {
|
|
|
469
566
|
: status.mode === "plan"
|
|
470
567
|
? "magenta"
|
|
471
568
|
: "green";
|
|
472
|
-
|
|
569
|
+
const speed = status.speed;
|
|
570
|
+
// 窄的时候依次:提示变短、去掉提示、用量变短、去掉缓存、模式变短、去掉用量、上下文变短、去掉速度
|
|
571
|
+
const segments = [
|
|
572
|
+
{
|
|
573
|
+
text: `${status.mode} (shift+tab)`,
|
|
574
|
+
short: status.mode,
|
|
575
|
+
shortAt: 6,
|
|
576
|
+
color: modeColor,
|
|
577
|
+
},
|
|
578
|
+
ctx
|
|
579
|
+
? {
|
|
580
|
+
text: `ctx ${tokens(ctx.used)}/${tokens(ctx.limit)} (${pct}%)${ctx.compactAt ? `, compacts at ${tokens(ctx.compactAt)}` : ""}`,
|
|
581
|
+
short: `ctx ${pct}%`,
|
|
582
|
+
shortAt: 4,
|
|
583
|
+
color: nearCompact ? "yellow" : undefined,
|
|
584
|
+
dim: !nearCompact,
|
|
585
|
+
}
|
|
586
|
+
: { text: "ctx —", dim: true },
|
|
587
|
+
...(speed
|
|
588
|
+
? [
|
|
589
|
+
{
|
|
590
|
+
text: `${speed.live ? "~" : ""}${speed.tps >= 100 ? Math.round(speed.tps) : speed.tps.toFixed(1)} tok/s`,
|
|
591
|
+
dropAt: 3,
|
|
592
|
+
dim: true,
|
|
593
|
+
},
|
|
594
|
+
]
|
|
595
|
+
: []),
|
|
596
|
+
...(status.cache !== undefined
|
|
597
|
+
? [{ text: `cache ${Math.round(status.cache * 100)}%`, dropAt: 7, dim: true }]
|
|
598
|
+
: []),
|
|
599
|
+
...(status.spent
|
|
600
|
+
? [
|
|
601
|
+
{
|
|
602
|
+
text: `session ${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
603
|
+
short: `${tokens(status.spent.input)} in / ${tokens(status.spent.output)} out`,
|
|
604
|
+
shortAt: 8,
|
|
605
|
+
dropAt: 5,
|
|
606
|
+
dim: true,
|
|
607
|
+
},
|
|
608
|
+
]
|
|
609
|
+
: []),
|
|
610
|
+
{
|
|
611
|
+
text: "PgUp/PgDn scroll · click ▸ to expand · shift+drag to select",
|
|
612
|
+
short: "click ▸ to expand",
|
|
613
|
+
shortAt: 10,
|
|
614
|
+
dropAt: 9,
|
|
615
|
+
dim: true,
|
|
616
|
+
},
|
|
617
|
+
];
|
|
618
|
+
const shown = fitSegments(segments, width);
|
|
619
|
+
return (_jsx(Box, { paddingX: 1, children: _jsx(Text, { wrap: "truncate-end", children: shown.map((s, i) => (_jsxs(Text, { children: [i ? _jsx(Text, { dimColor: true, children: " \u00B7 " }) : null, _jsx(Text, { color: s.color, dimColor: s.dim, children: s.text })] }, i))) }) }));
|
|
620
|
+
}
|
|
621
|
+
/** 一行提示:按宽度选第一个放得下的写法,都放不下时用最后一个并截断 */
|
|
622
|
+
function Hint({ variants, width }) {
|
|
623
|
+
const text = variants.find((v) => stringWidth(v) <= width) ?? variants.at(-1);
|
|
624
|
+
return (_jsx(Text, { dimColor: true, wrap: "truncate-end", children: text }));
|
|
473
625
|
}
|
|
474
626
|
const tokens = (n) => n >= 1_000_000
|
|
475
627
|
? `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`
|
|
@@ -532,7 +684,7 @@ function editKey(s, input, key) {
|
|
|
532
684
|
return text ? edit.insert(s, text) : undefined;
|
|
533
685
|
}
|
|
534
686
|
/** 渲染可编辑文本并把终端光标放到输入位置(输入法的候选框跟着它) */
|
|
535
|
-
function EditableText({ state, width, prefix, mask, placeholder, active, }) {
|
|
687
|
+
function EditableText({ state, width, prefix, mask, placeholder, active, maxRows = Infinity, }) {
|
|
536
688
|
const ref = useRef(null);
|
|
537
689
|
// 只用来在布局变化后重画一次、重新计算光标位置;它给的是相对父元素的坐标,光标要的是相对整个输出
|
|
538
690
|
const metrics = useBoxMetrics(ref);
|
|
@@ -541,7 +693,12 @@ function EditableText({ state, width, prefix, mask, placeholder, active, }) {
|
|
|
541
693
|
const cursor = mask
|
|
542
694
|
? [...state.value.slice(0, state.cursor)].length
|
|
543
695
|
: state.cursor;
|
|
544
|
-
const
|
|
696
|
+
const all = layout(shown, cursor, width - prefix.length);
|
|
697
|
+
const { col } = all;
|
|
698
|
+
// 行数超过 maxRows 时滚动,光标所在的行总在可见范围里
|
|
699
|
+
const first = Math.max(0, Math.min(all.row - maxRows + 1, all.rows.length - maxRows));
|
|
700
|
+
const rows = all.rows.slice(first, first + maxRows);
|
|
701
|
+
const row = all.row - first;
|
|
545
702
|
// 光标位置要在渲染时设:useCursor 在这次提交时交给 Ink,跟这一帧一起画出来。
|
|
546
703
|
// 放在 useEffect 里会晚一帧,光标总是停在上一次按键的位置(只移动光标时最明显)。
|
|
547
704
|
// 这里用的是上一次的布局;输入框挪了位置时 useBoxMetrics 会再触发一次渲染来纠正
|
|
@@ -549,7 +706,11 @@ function EditableText({ state, width, prefix, mask, placeholder, active, }) {
|
|
|
549
706
|
? origin(ref.current)
|
|
550
707
|
: undefined;
|
|
551
708
|
setCursorPosition(at && { x: at.x + prefix.length + col, y: at.y + row });
|
|
552
|
-
return (_jsx(Box, { ref: ref, flexDirection: "column", children: state.value === "" && placeholder ? (_jsxs(Text, { children: [_jsx(Text, { color: "cyan", children: prefix }), _jsx(Text, { dimColor: true, children: placeholder })] })) : (rows.map((r, i) => (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: i === 0
|
|
709
|
+
return (_jsx(Box, { ref: ref, flexDirection: "column", children: state.value === "" && placeholder ? (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: prefix }), _jsx(Text, { dimColor: true, children: placeholder })] })) : (rows.map((r, i) => (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: first + i === 0
|
|
710
|
+
? prefix
|
|
711
|
+
: i === 0
|
|
712
|
+
? "↑".padEnd(prefix.length)
|
|
713
|
+
: " ".repeat(prefix.length) }), r] }, first + i)))) }));
|
|
553
714
|
}
|
|
554
715
|
/** 元素相对整个动态输出(Static 之外的部分)左上角的位置 */
|
|
555
716
|
function origin(node) {
|
|
@@ -570,7 +731,11 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
570
731
|
const browsing = useRef(-1);
|
|
571
732
|
const draft = useRef("");
|
|
572
733
|
const lastCtrlC = useRef(0);
|
|
573
|
-
const { columns } =
|
|
734
|
+
const { columns, rows } = useScreenSize();
|
|
735
|
+
/** 补全菜单、搜索结果最多显示几项;输入框最多几行 */
|
|
736
|
+
const menuRows = roomFor(rows, MENTION_ROWS);
|
|
737
|
+
const inputRows = roomFor(rows, 10);
|
|
738
|
+
const hintWidth = Math.max(10, columns - 3);
|
|
574
739
|
// @ 引用补全用的路径;排好的结果按(路径表,查询)缓存,按键处理和渲染都要用时不重复排
|
|
575
740
|
const [getPaths, setPaths] = useLive([]);
|
|
576
741
|
const ranked = useRef(undefined);
|
|
@@ -642,7 +807,9 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
642
807
|
};
|
|
643
808
|
}, [mentioning]);
|
|
644
809
|
// 结果多时只显示选中项附近的几条
|
|
645
|
-
const first =
|
|
810
|
+
const first = around(hit, results.length, menuRows);
|
|
811
|
+
const firstMatch = around(Math.max(0, matches.indexOf(pick)), matches.length, menuRows);
|
|
812
|
+
const firstFound = around(Math.max(0, found.indexOf(file)), found.length, menuRows);
|
|
646
813
|
const set = (s) => {
|
|
647
814
|
setState(s);
|
|
648
815
|
setSelected(0);
|
|
@@ -804,31 +971,62 @@ function Prompt({ commands, handlers, history: initial, busy, active, onSubmit,
|
|
|
804
971
|
set(next);
|
|
805
972
|
}, { isActive: active });
|
|
806
973
|
// 对话框打开时隐藏但不卸载,输入到一半的内容和历史都保留
|
|
807
|
-
return (_jsxs(Box, { flexDirection: "column", marginTop: 1, display: active ? "flex" : "none", children: [_jsx(Box, { borderStyle: "round", borderColor: busy ? "gray" : "cyan", paddingX: 1, children: search ? (_jsx(EditableText, { state: { value: search.query, cursor: search.query.length }, width: Math.max(10, columns - 5), prefix: "search history\u203A ", placeholder: "type to filter", active: active })) : (_jsx(EditableText, { state: state, width: Math.max(10, columns - 5), prefix: "\u203A ", placeholder: busy
|
|
974
|
+
return (_jsxs(Box, { flexDirection: "column", marginTop: 1, display: active ? "flex" : "none", children: [_jsx(Box, { borderStyle: "round", borderColor: busy ? "gray" : "cyan", paddingX: 1, children: search ? (_jsx(EditableText, { state: { value: search.query, cursor: search.query.length }, width: Math.max(10, columns - 5), prefix: "search history\u203A ", placeholder: "type to filter", active: active, maxRows: inputRows })) : (_jsx(EditableText, { state: state, width: Math.max(10, columns - 5), prefix: "\u203A ", placeholder: busy
|
|
808
975
|
? "Type to queue a message · esc to interrupt"
|
|
809
|
-
:
|
|
976
|
+
: columns >= 70
|
|
977
|
+
? "Message jade · / commands · @ files · ↑ or ctrl+r for history"
|
|
978
|
+
: "Message jade · / commands · @ files", active: active, maxRows: inputRows })) }), search ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [results.length ? (results.slice(first, first + menuRows).map((h, i) => {
|
|
810
979
|
const on = first + i === hit;
|
|
811
980
|
return (_jsxs(Text, { wrap: "truncate-end", color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", h.replace(/\s*\n\s*/g, " ⏎ ")] }, first + i));
|
|
812
|
-
})) : (_jsx(Text, { dimColor: true, children: " no match" })),
|
|
981
|
+
})) : (_jsx(Text, { dimColor: true, children: " no match" })), _jsx(Hint, { width: hintWidth, variants: [
|
|
982
|
+
" ↑↓ or ctrl+r select · enter/tab put in the input · esc cancel",
|
|
983
|
+
" ↑↓ select · enter use · esc cancel",
|
|
984
|
+
] })] })) : matches.length ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [matches.slice(firstMatch, firstMatch + menuRows).map((c) => {
|
|
813
985
|
const on = c === pick;
|
|
814
986
|
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", usageOf(c).padEnd(column)] }), _jsx(Text, { dimColor: true, children: c.about })] }, c.name));
|
|
815
|
-
}), _jsx(
|
|
987
|
+
}), _jsx(Hint, { width: hintWidth, variants: [
|
|
988
|
+
` ↑↓ select · enter run · tab complete${matches.length > menuRows ? ` · ${matches.length} commands` : ""}`,
|
|
989
|
+
" ↑↓ · enter run · tab complete",
|
|
990
|
+
] })] })) : found.length ? (_jsxs(Box, { flexDirection: "column", paddingX: 2, children: [found.slice(firstFound, firstFound + menuRows).map((p) => {
|
|
816
991
|
const on = p === file;
|
|
817
992
|
return (_jsxs(Text, { wrap: "truncate-middle", color: on ? "cyan" : p.endsWith("/") ? "blue" : undefined, bold: on, children: [on ? "❯ " : " ", p] }, p));
|
|
818
|
-
}),
|
|
993
|
+
}), _jsx(Hint, { width: hintWidth, variants: [
|
|
994
|
+
" ↑↓ select · enter/tab insert the path · the file's content is sent with the message",
|
|
995
|
+
" ↑↓ select · enter/tab insert the path",
|
|
996
|
+
" ↑↓ · enter insert",
|
|
997
|
+
] })] })) : notice ? (_jsxs(Text, { color: "yellow", children: [" ", notice] })) : null] }));
|
|
819
998
|
}
|
|
820
999
|
/** @ 补全最多显示几项 */
|
|
821
1000
|
const MENTION_ROWS = 8;
|
|
1001
|
+
/** 长列表只显示 size 项时,为了让第 selected 项在中间附近,从第几项开始显示 */
|
|
1002
|
+
function around(selected, total, size) {
|
|
1003
|
+
return Math.max(0, Math.min(selected - Math.floor(size / 2), total - size));
|
|
1004
|
+
}
|
|
822
1005
|
/** 选择框一次最多显示几项 */
|
|
823
1006
|
const VISIBLE = 10;
|
|
1007
|
+
/** 选项多于这么多时可以打字筛选(这时 j/k 和数字键不再是快捷键) */
|
|
1008
|
+
const FILTER_FROM = 10;
|
|
824
1009
|
function SelectDialog({ dialog, }) {
|
|
825
1010
|
const [getSelected, setSelected] = useLive(0);
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
const
|
|
1011
|
+
const [getQuery, setQuery] = useLive("");
|
|
1012
|
+
const { rows, columns } = useScreenSize();
|
|
1013
|
+
const filtering = dialog.choices.length > FILTER_FROM;
|
|
1014
|
+
const query = getQuery();
|
|
1015
|
+
/** 筛选后的选项和它们原来的序号 */
|
|
1016
|
+
const filtered = () => {
|
|
1017
|
+
const q = getQuery().toLowerCase();
|
|
1018
|
+
return dialog.choices
|
|
1019
|
+
.map((c, i) => ({ c, i }))
|
|
1020
|
+
.filter(({ c }) => !q || `${c.label} ${c.hint ?? ""}`.toLowerCase().includes(q));
|
|
1021
|
+
};
|
|
1022
|
+
const items = filtered();
|
|
1023
|
+
const n = items.length;
|
|
1024
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1025
|
+
// 终端矮时少显示几项,加上预览一起不超过终端的高度
|
|
1026
|
+
const visible = roomFor(rows, VISIBLE);
|
|
829
1027
|
// 预览(例如要写入的文件)过长时截断:整个界面高过终端时 Ink 会每帧清屏重画
|
|
830
1028
|
const lines = dialog.body?.split("\n") ?? [];
|
|
831
|
-
const room = Math.max(3, rows - Math.min(n,
|
|
1029
|
+
const room = Math.max(3, rows - Math.min(n, visible) - 16);
|
|
832
1030
|
const body = lines.length > room
|
|
833
1031
|
? [
|
|
834
1032
|
...lines.slice(0, room),
|
|
@@ -836,29 +1034,54 @@ function SelectDialog({ dialog, }) {
|
|
|
836
1034
|
].join("\n")
|
|
837
1035
|
: dialog.body;
|
|
838
1036
|
useInput((input, key) => {
|
|
839
|
-
const
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (key.
|
|
843
|
-
return setSelected((selected +
|
|
844
|
-
if (key.
|
|
845
|
-
return
|
|
1037
|
+
const items = filtered();
|
|
1038
|
+
const n = items.length;
|
|
1039
|
+
const selected = Math.min(getSelected(), Math.max(0, n - 1));
|
|
1040
|
+
if (key.upArrow || (!filtering && input === "k"))
|
|
1041
|
+
return n && setSelected((selected - 1 + n) % n);
|
|
1042
|
+
if (key.downArrow || (!filtering && input === "j"))
|
|
1043
|
+
return n && setSelected((selected + 1) % n);
|
|
1044
|
+
if (key.return) {
|
|
1045
|
+
if (n)
|
|
1046
|
+
dialog.resolve(items[selected].c.value);
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (key.escape && filtering && getQuery()) {
|
|
1050
|
+
setQuery("");
|
|
1051
|
+
return setSelected(0);
|
|
1052
|
+
}
|
|
846
1053
|
if (key.escape || (key.ctrl && input === "c"))
|
|
847
1054
|
return dialog.resolve(undefined);
|
|
848
1055
|
if (key.pageDown)
|
|
849
|
-
return setSelected(Math.min(n - 1, selected +
|
|
1056
|
+
return setSelected(Math.min(n - 1, selected + visible));
|
|
850
1057
|
if (key.pageUp)
|
|
851
|
-
return setSelected(Math.max(0, selected -
|
|
1058
|
+
return setSelected(Math.max(0, selected - visible));
|
|
1059
|
+
if (filtering) {
|
|
1060
|
+
if (key.backspace || key.delete) {
|
|
1061
|
+
setQuery([...getQuery()].slice(0, -1).join(""));
|
|
1062
|
+
return setSelected(0);
|
|
1063
|
+
}
|
|
1064
|
+
if (key.ctrl || key.meta || key.tab || MOUSE.test(input))
|
|
1065
|
+
return;
|
|
1066
|
+
const text = input.replace(/[\x00-\x1f\x7f]/g, "");
|
|
1067
|
+
if (text) {
|
|
1068
|
+
setQuery(getQuery() + text);
|
|
1069
|
+
setSelected(0);
|
|
1070
|
+
}
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
852
1073
|
const digit = Number(input);
|
|
853
1074
|
if (Number.isInteger(digit) && digit >= 1 && digit <= Math.min(n, 9))
|
|
854
|
-
dialog.resolve(
|
|
1075
|
+
dialog.resolve(items[digit - 1].c.value);
|
|
855
1076
|
});
|
|
856
1077
|
// 选项多时只显示选中项附近的一段
|
|
857
|
-
const start =
|
|
858
|
-
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1, children: [_jsx(Text, { bold: true, children: dialog.title }), body ? _jsx(Text, { children: body }) : null, _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [start > 0 ? _jsxs(Text, { dimColor: true, children: [" \u2191 ", start, " more"] }) : null,
|
|
859
|
-
const
|
|
860
|
-
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color:
|
|
861
|
-
}), start +
|
|
1078
|
+
const start = around(selected, n, visible);
|
|
1079
|
+
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, marginTop: 1, children: [_jsx(Text, { bold: true, wrap: "truncate-end", children: dialog.title }), body ? _jsx(Text, { children: body }) : null, filtering ? (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { color: "cyan", children: "filter\u203A " }), query ? (_jsx(Text, { children: query })) : (_jsxs(Text, { dimColor: true, children: ["type to filter ", dialog.choices.length, " items"] }))] })) : null, _jsxs(Box, { flexDirection: "column", marginTop: filtering ? 0 : 1, children: [start > 0 ? _jsxs(Text, { dimColor: true, children: [" \u2191 ", start, " more"] }) : null, n === 0 ? _jsx(Text, { dimColor: true, children: " no match" }) : null, items.slice(start, start + visible).map(({ c, i }, j) => {
|
|
1080
|
+
const on = start + j === selected;
|
|
1081
|
+
return (_jsxs(Text, { wrap: "truncate-end", children: [_jsxs(Text, { color: on ? "cyan" : undefined, bold: on, children: [on ? "❯ " : " ", filtering ? "" : `${i + 1}. `, c.label] }), c.hint ? _jsxs(Text, { dimColor: true, children: [" ", c.hint] }) : null] }, i));
|
|
1082
|
+
}), start + visible < n ? (_jsxs(Text, { dimColor: true, children: [" \u2193 ", n - start - visible, " more"] })) : null] }), _jsx(Hint, { width: Math.max(10, columns - 4), variants: filtering
|
|
1083
|
+
? ["↑↓ select · type to filter · enter confirm · esc clear/cancel", "↑↓ · enter · esc"]
|
|
1084
|
+
: ["↑↓ select · enter confirm · esc cancel", "↑↓ · enter · esc"] })] }));
|
|
862
1085
|
}
|
|
863
1086
|
function InputDialog({ dialog, }) {
|
|
864
1087
|
const initial = dialog.initial ?? "";
|
|
@@ -867,7 +1090,7 @@ function InputDialog({ dialog, }) {
|
|
|
867
1090
|
cursor: initial.length,
|
|
868
1091
|
});
|
|
869
1092
|
const state = current();
|
|
870
|
-
const { columns } =
|
|
1093
|
+
const { columns } = useScreenSize();
|
|
871
1094
|
usePaste((text) => setState(edit.insert(current(), text.replace(/\r?\n/g, ""))));
|
|
872
1095
|
useInput((input, key) => {
|
|
873
1096
|
const state = current();
|