@uncarved/jade 0.2.1 → 0.3.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 +65 -2
- package/assets/logo.svg +1 -0
- package/bin/jade.js +4 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +26 -4
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +4 -2
- package/plugins/approval/index.ts +236 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +36 -17
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +8 -3
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +33 -6
- package/plugins/fs/grep.ts +19 -7
- package/plugins/fs/index.ts +6 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +1 -1
- package/plugins/hooks/index.ts +152 -0
- package/plugins/hooks/plugin.json +14 -0
- package/plugins/keys/index.ts +2 -18
- package/plugins/mcp/index.ts +43 -15
- package/plugins/mcp/install.ts +139 -0
- package/plugins/mcp/mcp.ts +2 -1
- package/plugins/mcp/plugin.json +1 -1
- package/plugins/mcp/project.ts +37 -1
- package/plugins/memory/index.ts +4 -9
- package/plugins/memory/plugin.json +1 -1
- package/plugins/mentions/index.ts +2 -2
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +39 -45
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +34 -5
- package/plugins/models/saved.ts +8 -4
- package/plugins/modes/index.ts +2 -17
- package/plugins/plugins/index.ts +2 -2
- package/plugins/print/index.ts +36 -6
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +22 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +5 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +32 -2
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/browser.ts +221 -0
- package/plugins/screenshot/index.ts +29 -15
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +115 -7
- package/plugins/shell/plugin.json +3 -1
- package/plugins/shell/renderers.ts +1 -1
- package/plugins/shell/service.ts +2 -13
- package/plugins/shell/shell.ts +119 -47
- package/plugins/skills/index.ts +172 -37
- package/plugins/skills/install.ts +212 -0
- package/plugins/skills/plugin.json +3 -2
- package/plugins/skills/project.ts +38 -1
- package/plugins/skills/skills.ts +28 -9
- package/plugins/steer/index.ts +142 -0
- package/plugins/steer/plugin.json +8 -0
- package/plugins/suggest/index.ts +16 -11
- package/plugins/suggest/plugin.json +1 -1
- package/plugins/task/index.ts +18 -57
- package/plugins/task/plugin.json +9 -3
- package/plugins/task/subagent.ts +25 -7
- package/plugins/todo/index.ts +2 -2
- package/plugins/tools/index.ts +50 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +129 -23
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +506 -0
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +17 -2
- package/plugins/tui/tui.ts +344 -97
- package/plugins/undo/checkpoint.ts +2 -2
- package/plugins/undo/index.ts +84 -26
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +8 -11
- package/plugins/web/plugin.json +1 -1
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +221 -51
- package/plugins-dist/approval/plugin.json +1 -1
- package/plugins-dist/approval/policy.js +30 -15
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +6 -1
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +34 -6
- package/plugins-dist/fs/grep.js +17 -6
- package/plugins-dist/fs/index.js +6 -6
- package/plugins-dist/fs/plugin.json +1 -1
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +45 -13
- package/plugins-dist/mcp/install.js +160 -0
- package/plugins-dist/mcp/mcp.js +2 -1
- package/plugins-dist/mcp/plugin.json +2 -1
- package/plugins-dist/mcp/project.js +42 -1
- package/plugins-dist/memory/index.js +3 -5
- package/plugins-dist/memory/plugin.json +1 -1
- package/plugins-dist/mentions/index.js +1 -1
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +41 -17
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +17 -3
- package/plugins-dist/models/saved.js +9 -4
- package/plugins-dist/modes/index.js +1 -1
- package/plugins-dist/plugins/index.js +1 -1
- package/plugins-dist/print/index.js +37 -3
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +22 -3
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +5 -4
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +28 -2
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/browser.js +187 -0
- package/plugins-dist/screenshot/index.js +28 -13
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/screenshot/tool.js +27 -49
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +119 -4
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +106 -36
- package/plugins-dist/skills/index.js +184 -35
- package/plugins-dist/skills/install.js +178 -0
- package/plugins-dist/skills/plugin.json +6 -2
- package/plugins-dist/skills/project.js +43 -1
- package/plugins-dist/skills/skills.js +27 -10
- package/plugins-dist/steer/index.js +135 -0
- package/plugins-dist/steer/plugin.json +12 -0
- package/plugins-dist/suggest/index.js +15 -9
- package/plugins-dist/suggest/plugin.json +1 -1
- package/plugins-dist/task/index.js +17 -46
- package/plugins-dist/task/plugin.json +11 -5
- package/plugins-dist/task/subagent.js +21 -6
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tools/index.js +49 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +134 -15
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +441 -0
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +16 -1
- package/plugins-dist/tui/tui.js +320 -88
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +1 -1
- package/plugins-dist/undo/index.js +91 -24
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +7 -7
- package/plugins-dist/web/plugin.json +1 -1
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +23 -5
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
package/plugins/tui/frontend.ts
CHANGED
|
@@ -9,16 +9,10 @@ import { readFileSync } from "node:fs";
|
|
|
9
9
|
import os from "node:os";
|
|
10
10
|
import path from "node:path";
|
|
11
11
|
import type { Block } from "uncarved";
|
|
12
|
-
import type { Frontend, OpenOptions, Session, SessionManager, SessionUI, UI } from "
|
|
13
|
-
import type { CommandContext, Commands } from "
|
|
14
|
-
import type { Keys } from "../keys/index.ts";
|
|
15
|
-
import type { Compact } from "../compact/index.ts";
|
|
16
|
-
import type { Models } from "../models/index.ts";
|
|
17
|
-
import type { Modes } from "../modes/index.ts";
|
|
18
|
-
import type { Renderers } from "../renderers/index.ts";
|
|
12
|
+
import type { Frontend, OpenOptions, Session, SessionManager, SessionUI, UI } from "@uncarved/jade-api";
|
|
13
|
+
import type { CommandContext, Commands, Compact, Keys, Models, Modes, Renderers, SessionIndex, Steer } from "@uncarved/jade-api/services";
|
|
19
14
|
import { InputHistory } from "./history.ts";
|
|
20
15
|
import { mentionCandidates } from "./mentions.ts";
|
|
21
|
-
import type { SessionIndex } from "../sessions/index.ts";
|
|
22
16
|
import { dim, red, safeScreen, yellow, type CommandInfo, type Screen } from "./screen.ts";
|
|
23
17
|
import { transcript, type Transcript } from "./transcript.ts";
|
|
24
18
|
|
|
@@ -51,11 +45,18 @@ jade's own commands (plugins, update, --safe): jade help`;
|
|
|
51
45
|
/** 前端自己的命令:帮助和退出。其余命令都来自 commands@1 */
|
|
52
46
|
const OWN: CommandInfo[] = [
|
|
53
47
|
{ name: "help", about: "list the commands and keys" },
|
|
48
|
+
{ name: "copy", args: "[code]", about: "copy the last reply to the clipboard as it was written (code: only its last code block)" },
|
|
54
49
|
{ name: "exit", about: "quit (the session stays resumable)" },
|
|
55
50
|
];
|
|
56
51
|
|
|
57
52
|
const KEYS =
|
|
58
|
-
"Keys: enter send ·
|
|
53
|
+
"Keys: enter send · !<command> runs a shell command yourself · ctrl+j, alt+enter or \\ then enter: new line · ↑↓ history · esc interrupt (twice to also move queued messages back to the input) · ↑ on the first line edits queued messages · ctrl+o expand · ctrl+c twice to exit";
|
|
54
|
+
|
|
55
|
+
/** 退出时最多等正在做的事这么久 */
|
|
56
|
+
const QUIT_WAIT_MS = 3000;
|
|
57
|
+
|
|
58
|
+
/** 命令运行中的临时显示(CommandContext.progress)最多占几行 */
|
|
59
|
+
const LIVE_ROWS = 8;
|
|
59
60
|
|
|
60
61
|
/** 第二次 esc 算作"再按一次"的时间窗口 */
|
|
61
62
|
const SECOND_ESC_MS = 3000;
|
|
@@ -89,6 +90,8 @@ export interface FrontendServices {
|
|
|
89
90
|
compact?: Compact;
|
|
90
91
|
/** 会话索引(jade/sessions),--continue 用 */
|
|
91
92
|
index?: SessionIndex;
|
|
93
|
+
/** 一轮中途说的话(jade/steer):有它时,模型工作中提交的消息在下一次请求模型时就交给它,不等这一轮结束 */
|
|
94
|
+
steer?: Steer;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
/** 状态栏上插件的一段最多显示这么多字符(超出的截断,换行并成空格) */
|
|
@@ -126,6 +129,13 @@ export class ScreenBridge {
|
|
|
126
129
|
if (!this.screen) return undefined;
|
|
127
130
|
return this.screen.input(req.title, { ...(req.initial ? { initial: req.initial } : {}), ...(req.placeholder ? { placeholder: req.placeholder } : {}), ...(req.mask ? { mask: true } : {}), ...(signal ? { signal } : {}) });
|
|
128
131
|
},
|
|
132
|
+
copy: (text) => {
|
|
133
|
+
if (!this.screen?.copy) return false;
|
|
134
|
+
this.screen.copy(String(text));
|
|
135
|
+
return true;
|
|
136
|
+
},
|
|
137
|
+
// 界面没有占着终端(不是终端界面、还没起来)时没有什么要让的,直接执行
|
|
138
|
+
suspend: async (fn) => (this.screen?.suspend ? this.screen.suspend(fn) : fn()),
|
|
129
139
|
session: ({ session, plugin }) => this.#sessionUI(session, plugin),
|
|
130
140
|
};
|
|
131
141
|
|
|
@@ -249,7 +259,7 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
249
259
|
return 2;
|
|
250
260
|
}
|
|
251
261
|
|
|
252
|
-
const { cwd, home, commands, keys, renderers, modes, models, compact, index } = services;
|
|
262
|
+
const { cwd, home, commands, keys, renderers, modes, models, compact, index, steer } = services;
|
|
253
263
|
const sessions = opts.sessions;
|
|
254
264
|
// --continue:这个目录里最近用过的会话(索引由 jade/sessions 在收到第一条消息时写)
|
|
255
265
|
if (args.cont && !index) {
|
|
@@ -400,6 +410,41 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
400
410
|
|
|
401
411
|
/** 等待执行的输入:运行中提交的消息和需要独占会话的命令排在这里,按顺序执行 */
|
|
402
412
|
const queue: string[] = [];
|
|
413
|
+
/**
|
|
414
|
+
* 已经交给 jade/steer、还没交给模型的话:下一次请求模型时它们就到模型那里;这一轮先结束了的取回来,
|
|
415
|
+
* 排到队列最前面当普通消息发。steer@1 出错时当作没有它
|
|
416
|
+
*/
|
|
417
|
+
const steered = {
|
|
418
|
+
pending(): string[] {
|
|
419
|
+
try {
|
|
420
|
+
return current && steer ? steer.pending(current.id) : [];
|
|
421
|
+
} catch {
|
|
422
|
+
return [];
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
take(): string[] {
|
|
426
|
+
try {
|
|
427
|
+
return current && steer ? steer.take(current.id) : [];
|
|
428
|
+
} catch {
|
|
429
|
+
return [];
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
push(text: string): boolean {
|
|
433
|
+
try {
|
|
434
|
+
return current !== undefined && steer !== undefined && steer.push(current.id, text);
|
|
435
|
+
} catch {
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
/** 输入框上方显示的等待中的消息:中途说的话在前(它们先到模型那里) */
|
|
441
|
+
const showQueued = () => screen.queued([...steered.pending(), ...queue]);
|
|
442
|
+
let offSteer: (() => void) | undefined;
|
|
443
|
+
try {
|
|
444
|
+
offSteer = steer?.subscribe(showQueued);
|
|
445
|
+
} catch {
|
|
446
|
+
// 没有订阅上:交给模型之后显示的列表要等下一次变化才更新
|
|
447
|
+
}
|
|
403
448
|
let running = false;
|
|
404
449
|
let work: Promise<void> | undefined;
|
|
405
450
|
let interruptedAt = 0;
|
|
@@ -441,13 +486,14 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
441
486
|
for (const ac of commandAborts) ac.abort(new Error("interrupted by user"));
|
|
442
487
|
current?.abort(new Error("interrupted by user"));
|
|
443
488
|
screen.busy("Interrupting");
|
|
444
|
-
|
|
445
|
-
|
|
489
|
+
const n = queue.length + steered.pending().length;
|
|
490
|
+
if (n === 0) return;
|
|
446
491
|
if (again) {
|
|
447
492
|
if (takeBack()) screen.log(dim(`[moved ${n} queued message${n > 1 ? "s" : ""} back to the input]`));
|
|
448
493
|
else {
|
|
494
|
+
steered.take();
|
|
449
495
|
queue.length = 0;
|
|
450
|
-
|
|
496
|
+
showQueued();
|
|
451
497
|
screen.log(dim(`[cleared ${n} queued message${n > 1 ? "s" : ""}]`));
|
|
452
498
|
}
|
|
453
499
|
} else {
|
|
@@ -457,10 +503,10 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
457
503
|
|
|
458
504
|
/** 排队的消息不再自动发送,放回输入框(在已经输入的内容前面,一条一行);没有可放回的返回 false */
|
|
459
505
|
function takeBack(): boolean {
|
|
460
|
-
if (queue.length === 0
|
|
506
|
+
if (!screen.draft || queue.length + steered.pending().length === 0) return false;
|
|
461
507
|
const typed = screen.draft.text();
|
|
462
|
-
screen.draft.set([...queue.splice(0), ...(typed.trim() ? [typed] : [])].join("\n"));
|
|
463
|
-
|
|
508
|
+
screen.draft.set([...steered.take(), ...queue.splice(0), ...(typed.trim() ? [typed] : [])].join("\n"));
|
|
509
|
+
showQueued();
|
|
464
510
|
return true;
|
|
465
511
|
}
|
|
466
512
|
|
|
@@ -468,10 +514,13 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
468
514
|
if (quitting) return;
|
|
469
515
|
quitting = true;
|
|
470
516
|
if (running) current?.abort(new Error("exiting"));
|
|
471
|
-
|
|
517
|
+
// 正在做的事停不下来时(例如命令杀不掉)不一直等:退出不能被它卡住
|
|
518
|
+
for (const ac of commandAborts) ac.abort(new Error("exiting"));
|
|
519
|
+
await Promise.race([work?.catch(() => {}), new Promise((r) => setTimeout(r, QUIT_WAIT_MS))]);
|
|
472
520
|
keyAbort.abort(new Error("exiting"));
|
|
473
521
|
offMode?.();
|
|
474
522
|
offModel?.();
|
|
523
|
+
offSteer?.();
|
|
475
524
|
unsubscribe?.();
|
|
476
525
|
await screen.close();
|
|
477
526
|
bridge.detach();
|
|
@@ -494,6 +543,8 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
494
543
|
});
|
|
495
544
|
}
|
|
496
545
|
|
|
546
|
+
/** 输入是命令时返回命令名。第一个词里还有 / 的是路径(/c/Users/…、/usr/bin/x),当普通消息发给模型 */
|
|
547
|
+
const commandName = (text: string) => /^\/([^\s/]+)(?=\s|$)/.exec(text)?.[1]!.toLowerCase();
|
|
497
548
|
const instant = (name: string) => OWN.some((o) => o.name === name) || name === "quit" || commands.get(name)?.command.instant === true;
|
|
498
549
|
|
|
499
550
|
/** 执行一条命令;返回 false 表示退出 */
|
|
@@ -510,6 +561,17 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
510
561
|
if (pluginKeys.length > 0) screen.log(dim(`Plugin keys: ${pluginKeys.map((k) => `${k.key}${k.description ? ` ${k.description}` : ""} (${k.plugin})`).join(" · ")}`));
|
|
511
562
|
return true;
|
|
512
563
|
}
|
|
564
|
+
if (name === "copy") {
|
|
565
|
+
const reply = lastReply(current?.state.messages ?? []);
|
|
566
|
+
const text = rest.trim() === "code" ? reply && lastCodeBlock(reply) : reply;
|
|
567
|
+
if (!text) screen.log(dim(rest.trim() === "code" ? "The last reply has no code block." : "There is no reply to copy yet."));
|
|
568
|
+
else if (!screen.copy) screen.log(red("[error] this terminal cannot copy"));
|
|
569
|
+
else {
|
|
570
|
+
screen.copy(text);
|
|
571
|
+
screen.log(dim(`[copied ${[...text].length} characters]`));
|
|
572
|
+
}
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
513
575
|
const found = commands.get(name);
|
|
514
576
|
if (!found) {
|
|
515
577
|
screen.log(red(`unknown command /${name}; /help lists the commands`));
|
|
@@ -517,7 +579,18 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
517
579
|
}
|
|
518
580
|
const ac = new AbortController();
|
|
519
581
|
commandAborts.add(ac);
|
|
520
|
-
|
|
582
|
+
// 命令的临时显示:末尾几行,每行占进度区的一行
|
|
583
|
+
const showLive = (text: string | undefined) => {
|
|
584
|
+
const lines = text?.replace(/\r\n?/g, "\n").trimEnd().split("\n").slice(-LIVE_ROWS) ?? [];
|
|
585
|
+
for (let i = 0; i < LIVE_ROWS; i++) screen.progress(`command:${i}`, text !== undefined && i < lines.length ? dim(` │ ${lines[i]}`) : undefined);
|
|
586
|
+
};
|
|
587
|
+
const ctx: CommandContext = {
|
|
588
|
+
signal: ac.signal,
|
|
589
|
+
ui: bridge.ui,
|
|
590
|
+
sessions: manager,
|
|
591
|
+
print: (t, o) => screen.log(t, o?.details ? { details: o.details } : undefined),
|
|
592
|
+
progress: showLive,
|
|
593
|
+
};
|
|
521
594
|
try {
|
|
522
595
|
const result = await found.command.run(rest, ctx);
|
|
523
596
|
if (typeof result === "string") screen.log(result);
|
|
@@ -528,6 +601,7 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
528
601
|
} catch (e) {
|
|
529
602
|
if (!ac.signal.aborted) screen.log(red(`[error] /${name}: ${errorText(e)}`));
|
|
530
603
|
} finally {
|
|
604
|
+
showLive(undefined);
|
|
531
605
|
commandAborts.delete(ac);
|
|
532
606
|
}
|
|
533
607
|
return true;
|
|
@@ -535,7 +609,9 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
535
609
|
|
|
536
610
|
function onSubmit(text: string) {
|
|
537
611
|
if (quitting) return;
|
|
538
|
-
|
|
612
|
+
// !<命令> 是 /sh <命令> 的简写(jade/shell 提供):用户自己执行,不经过模型
|
|
613
|
+
if (/^!\s*\S/.test(text)) text = `/sh ${text.slice(1).trim()}`;
|
|
614
|
+
const name = commandName(text);
|
|
539
615
|
if (name !== undefined && instant(name)) {
|
|
540
616
|
// 不碰会话的命令运行中也直接执行
|
|
541
617
|
view.echo(text);
|
|
@@ -544,8 +620,16 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
544
620
|
});
|
|
545
621
|
return;
|
|
546
622
|
}
|
|
623
|
+
// 模型正在工作、前面也没有别的在排队:这句话在下一次请求模型时就交给它(jade/steer),不等这一轮结束。
|
|
624
|
+
// 命令、已经有东西在排队时照旧排队,先后顺序不变
|
|
625
|
+
if (running && name === undefined && queue.length === 0 && steered.push(text)) {
|
|
626
|
+
showQueued();
|
|
627
|
+
// 没赶上这一轮的,等正在做的事做完再发(那时不一定是 pump 在跑,例如刚恢复会话)
|
|
628
|
+
void work?.catch(() => {}).then(() => pump());
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
547
631
|
queue.push(text);
|
|
548
|
-
if (running)
|
|
632
|
+
if (running) showQueued();
|
|
549
633
|
void pump();
|
|
550
634
|
}
|
|
551
635
|
|
|
@@ -553,13 +637,18 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
553
637
|
if (running) return;
|
|
554
638
|
let exit = false;
|
|
555
639
|
await occupy(async () => {
|
|
556
|
-
|
|
640
|
+
for (;;) {
|
|
641
|
+
// 中途说的话没赶上这一轮(模型没有再被请求就结束了):当普通消息发,排在后来排队的前面
|
|
642
|
+
queue.unshift(...steered.take());
|
|
643
|
+
if (queue.length === 0 || quitting) break;
|
|
557
644
|
if (interruptPending) interruptedAt = Date.now();
|
|
558
645
|
interruptPending = false;
|
|
559
646
|
const text = queue.shift()!;
|
|
560
|
-
|
|
647
|
+
showQueued();
|
|
561
648
|
view.echo(text);
|
|
562
|
-
if (text
|
|
649
|
+
if (commandName(text) !== undefined) {
|
|
650
|
+
// 命令可能要跑一阵(/sh 跑测试):让用户看到它在跑
|
|
651
|
+
screen.busy("Running");
|
|
563
652
|
if (!(await command(text))) {
|
|
564
653
|
exit = true;
|
|
565
654
|
return;
|
|
@@ -605,6 +694,23 @@ async function run(services: FrontendServices, bridge: ScreenBridge, opts: Param
|
|
|
605
694
|
}
|
|
606
695
|
|
|
607
696
|
/** 当前分支;不在仓库里或没有 git 时 undefined */
|
|
697
|
+
/** 最近一条有正文的助手消息的正文(模型写的原文,没有折行和排版) */
|
|
698
|
+
export function lastReply(messages: readonly { role: string; content: readonly Block[] }[]): string | undefined {
|
|
699
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
700
|
+
const m = messages[i]!;
|
|
701
|
+
if (m.role !== "assistant") continue;
|
|
702
|
+
const text = m.content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n").trim();
|
|
703
|
+
if (text) return text;
|
|
704
|
+
}
|
|
705
|
+
return undefined;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/** Markdown 里最后一个围栏代码块的内容 */
|
|
709
|
+
export function lastCodeBlock(markdown: string): string | undefined {
|
|
710
|
+
const blocks = [...markdown.matchAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[`~]*[ \t]*$/gm)];
|
|
711
|
+
return blocks.at(-1)?.[2]?.replace(/\n$/, "");
|
|
712
|
+
}
|
|
713
|
+
|
|
608
714
|
function gitBranch(cwd: string): Promise<string | undefined> {
|
|
609
715
|
return new Promise((resolve) => {
|
|
610
716
|
execFile("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd, timeout: 2000, windowsHide: true }, (err, stdout) => resolve(err ? undefined : stdout.trim() || undefined));
|
package/plugins/tui/index.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
// jade/tui:终端界面,默认的前端。提供 frontend@1 和 ui@1;命令、快捷键、显示方式都来自注册表插件。
|
|
2
2
|
|
|
3
|
-
import { definePlugin } from "
|
|
4
|
-
import type { Commands } from "
|
|
5
|
-
import type { Compact } from "../compact/index.ts";
|
|
6
|
-
import type { Keys } from "../keys/index.ts";
|
|
7
|
-
import type { Models } from "../models/index.ts";
|
|
8
|
-
import type { Modes } from "../modes/index.ts";
|
|
9
|
-
import type { Renderers } from "../renderers/index.ts";
|
|
10
|
-
import type { SessionIndex } from "../sessions/index.ts";
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
+
import type { Commands, Compact, Keys, Models, Modes, Renderers, SessionIndex, Steer } from "@uncarved/jade-api/services";
|
|
11
5
|
import { ScreenBridge, tuiFrontend } from "./frontend.ts";
|
|
12
6
|
|
|
13
7
|
export default definePlugin({
|
|
@@ -27,6 +21,7 @@ export default definePlugin({
|
|
|
27
21
|
...optional("models", ctx.service<Models>("models@1")),
|
|
28
22
|
...optional("compact", ctx.service<Compact>("compact@1")),
|
|
29
23
|
...optional("index", ctx.service<SessionIndex>("sessions@1")),
|
|
24
|
+
...optional("steer", ctx.service<Steer>("steer@1")),
|
|
30
25
|
},
|
|
31
26
|
bridge,
|
|
32
27
|
),
|
package/plugins/tui/markdown.ts
CHANGED
|
@@ -95,7 +95,52 @@ export function markdownStream(): MarkdownStream {
|
|
|
95
95
|
export function renderMarkdown(text: string, width: number): string[] {
|
|
96
96
|
const md = markdownStream();
|
|
97
97
|
const out = [...text.split("\n").flatMap((l) => md.push(l)), ...md.flush()];
|
|
98
|
-
return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width)));
|
|
98
|
+
return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width))).map(visibleRow);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------- 自动折行的记号 ----------
|
|
102
|
+
//
|
|
103
|
+
// 一行太长折成屏幕上的几行时,后面的几行(续行)开头带一个看不见的记号:用鼠标选中复制时,续行接回上一行,
|
|
104
|
+
// 而不是变成换行。记号只用几个控制字符(显示之前去掉;外面来的文字里的控制字符早被 safeScreen 去掉了,不会撞上):
|
|
105
|
+
// \x1f \x1e × gap \x1d × indent \x1c
|
|
106
|
+
// gap:折行时上一行行尾去掉了几个空格,接回去时补上;indent:续行开头排版加的几列(列表的缩进、代码块的竖线),接回去时跳过
|
|
107
|
+
|
|
108
|
+
const SOFT_HEAD = /^\x1f(\x1e*)(\x1d*)\x1c/;
|
|
109
|
+
|
|
110
|
+
/** 把一行标成续行 */
|
|
111
|
+
export const softRow = (row: string, gap = 0, indent = 0): string => `\x1f${"\x1e".repeat(gap)}${"\x1d".repeat(indent)}\x1c${row}`;
|
|
112
|
+
|
|
113
|
+
/** 去掉记号,得到要显示的那一行 */
|
|
114
|
+
export const visibleRow = (row: string): string => (row.charCodeAt(0) === 0x1f ? row.replace(SOFT_HEAD, "") : row);
|
|
115
|
+
|
|
116
|
+
/** 一行是不是续行,以及接回上一行时要补的空格数、要跳过的列数;text 是去掉记号的那一行 */
|
|
117
|
+
export function softInfo(row: string): { text: string; soft: boolean; gap: number; indent: number } {
|
|
118
|
+
const m = row.charCodeAt(0) === 0x1f ? SOFT_HEAD.exec(row) : null;
|
|
119
|
+
if (!m) return { text: row, soft: false, gap: 0, indent: 0 };
|
|
120
|
+
return { text: row.slice(m[0].length), soft: true, gap: m[1]!.length, indent: m[2]!.length };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const WRAP = { hard: true, trim: false } as const;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 按显示宽度折行,保留颜色;每一项是屏幕上的一行,续行带记号。制表符先换成空格:按 0 列算宽度会对不齐、超宽。
|
|
127
|
+
* 不去掉空格(trim: false),所以续行直接接在上一行后面就是原文
|
|
128
|
+
*/
|
|
129
|
+
export function wrapRows(text: string, width: number): string[] {
|
|
130
|
+
const expanded = expandTabs(text);
|
|
131
|
+
// 整段一起折:颜色可能跨行,分开折会丢掉后面几行的颜色
|
|
132
|
+
const rows = wrapAnsi(expanded, width, WRAP).split("\n");
|
|
133
|
+
const logical = expanded.split("\n");
|
|
134
|
+
if (rows.length === logical.length) return rows;
|
|
135
|
+
// 每个原来的行占了几行:放得下的占一行,放不下的单独折一次数出来(颜色不占宽度,不影响在哪里折)
|
|
136
|
+
const counts = logical.map((l) => (stringWidth(l) <= width ? 1 : wrapAnsi(l, width, WRAP).split("\n").length));
|
|
137
|
+
if (counts.reduce((a, b) => a + b, 0) !== rows.length) return rows;
|
|
138
|
+
let at = 0;
|
|
139
|
+
for (const n of counts) {
|
|
140
|
+
for (let i = 1; i < n; i++) rows[at + i] = softRow(rows[at + i]!);
|
|
141
|
+
at += n;
|
|
142
|
+
}
|
|
143
|
+
return rows;
|
|
99
144
|
}
|
|
100
145
|
|
|
101
146
|
/** 代码块和表格以外的一行 */
|
|
@@ -167,11 +212,11 @@ export function expandTabs(text: string, stop = 4): string {
|
|
|
167
212
|
function hanging(head: string, body: string, rest = " ".repeat(stringWidth(head))): MdLine {
|
|
168
213
|
return {
|
|
169
214
|
render: (width) => {
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
215
|
+
const indent = stringWidth(head);
|
|
216
|
+
const room = Math.max(1, width - indent);
|
|
217
|
+
const rows = wrapAnsi(expandTabs(body), room, WRAP).split("\n");
|
|
218
|
+
// 折行处留下的行尾空格看不见,去掉免得多占一列;去掉了几个记在续行的记号里,复制时补回去
|
|
219
|
+
return rows.map((r, i) => (i === 0 ? head + r.trimEnd() : softRow(rest + r.trimEnd(), rows[i - 1]!.length - rows[i - 1]!.trimEnd().length, indent)));
|
|
175
220
|
},
|
|
176
221
|
};
|
|
177
222
|
}
|