@uncarved/jade 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +86 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +496 -36
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// 仓库目录、确认记录、显示用的小工具。插件之间不共享代码(只能共享类型),jade/mcp、jade/skills 各有一份。
|
|
2
2
|
|
|
3
|
+
import type { Span, Text, TextStyle, Trust } from "@uncarved/jade-api";
|
|
3
4
|
import { existsSync, realpathSync } from "node:fs";
|
|
4
|
-
import {
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
5
6
|
import path from "node:path";
|
|
6
7
|
|
|
7
8
|
export function realPath(p: string): string {
|
|
@@ -30,52 +31,50 @@ export function display(cwd: string, full: string): string {
|
|
|
30
31
|
return shown.split(path.sep).join("/");
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
* 用户确认过的东西(检查命令),每行一条 { hash, path, at }。
|
|
35
|
-
* 和旧 jade 的 $JADE_HOME/trust.jsonl 同一个文件、同一种格式,旧 jade 里确认过的不用再确认
|
|
36
|
-
*/
|
|
37
|
-
export class TrustFile {
|
|
38
|
-
readonly file: string;
|
|
39
|
-
#known = new Set<string>();
|
|
34
|
+
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
40
35
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
// 带语义样式的文字(jade-api 的 Text):插件只说这段是次要的、成功、警告……,怎么画由前端决定
|
|
37
|
+
const styled = (style: TextStyle) => (text: string): Span => ({ text, style });
|
|
38
|
+
export const dim = styled("dim");
|
|
39
|
+
export const bold = styled("bold");
|
|
40
|
+
export const ok = styled("ok");
|
|
41
|
+
export const warn = styled("warn");
|
|
42
|
+
export const error = styled("error");
|
|
43
|
+
/** 几行接成一段,行之间换行 */
|
|
44
|
+
export const lines = (rows: readonly Text[]): Text => rows.flatMap((r, i) => (i ? ["\n", r] : [r]));
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
/**
|
|
47
|
+
* 0.3.1 及以前,本插件自己把确认记在 $JADE_HOME/trust.jsonl(每行 { hash, path, at })。升级后照认,不用再确认一遍:
|
|
48
|
+
* trust@1 里没有时查这份旧文件,查到了顺手记进 trust@1。这是本插件自己的旧数据,核心不认识它
|
|
49
|
+
*/
|
|
50
|
+
export function withLegacyTrust(trust: Trust, home: string): Trust {
|
|
51
|
+
let legacy: Promise<Map<string, string>> | undefined;
|
|
52
|
+
const read = async () => {
|
|
53
|
+
const hashes = new Map<string, string>();
|
|
47
54
|
let text = "";
|
|
48
55
|
try {
|
|
49
|
-
text = await readFile(
|
|
56
|
+
text = await readFile(path.join(home, "trust.jsonl"), "utf8");
|
|
50
57
|
} catch {
|
|
51
|
-
return
|
|
58
|
+
return hashes;
|
|
52
59
|
}
|
|
53
60
|
for (const line of text.split("\n")) {
|
|
54
61
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
62
|
+
const r = JSON.parse(line) as { hash?: unknown; path?: unknown };
|
|
63
|
+
if (typeof r.hash === "string") hashes.set(r.hash, typeof r.path === "string" ? r.path : "");
|
|
59
64
|
} catch {
|
|
60
|
-
//
|
|
65
|
+
// 空行、写到一半的行
|
|
61
66
|
}
|
|
62
67
|
}
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
return hashes;
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
has: async (hash) => {
|
|
72
|
+
if (await trust.has(hash)) return true;
|
|
73
|
+
const where = (await (legacy ??= read())).get(hash);
|
|
74
|
+
if (where === undefined) return false;
|
|
75
|
+
await trust.add(hash, where).catch(() => {});
|
|
76
|
+
return true;
|
|
77
|
+
},
|
|
78
|
+
add: (hash, where) => trust.add(hash, where),
|
|
79
|
+
};
|
|
71
80
|
}
|
|
72
|
-
|
|
73
|
-
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
74
|
-
|
|
75
|
-
// 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
|
|
76
|
-
const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
|
|
77
|
-
const paint = (open: string, close: string) => (s: string) =>
|
|
78
|
-
colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
|
|
79
|
-
export const dim = paint("2", "22");
|
|
80
|
-
export const yellow = paint("33", "39");
|
|
81
|
-
export const bold = paint("1", "22");
|
|
@@ -6,6 +6,8 @@ import type { CommandEntry, Commands } from "@uncarved/jade-api/services";
|
|
|
6
6
|
|
|
7
7
|
const NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
8
8
|
const registry = new Map<string, CommandEntry>();
|
|
9
|
+
/** 简写字符 → 命令名(小写) */
|
|
10
|
+
const prefixes = new Map<string, string>();
|
|
9
11
|
|
|
10
12
|
export default definePlugin({
|
|
11
13
|
process(ctx) {
|
|
@@ -14,20 +16,42 @@ export default definePlugin({
|
|
|
14
16
|
if (typeof command?.name !== "string" || !NAME.test(command.name)) throw new Error(`invalid command name ${JSON.stringify(command?.name)}`);
|
|
15
17
|
if (typeof command.run !== "function") throw new Error(`command /${command.name} has no run function`);
|
|
16
18
|
const key = command.name.toLowerCase();
|
|
19
|
+
// 重名时先注册的留下,后来的这一条跳过并报告:抛错会让调用方整个激活失败,连它别的功能一起没了
|
|
17
20
|
const taken = registry.get(key);
|
|
18
|
-
if (taken)
|
|
19
|
-
|
|
21
|
+
if (taken) {
|
|
22
|
+
ctx.report(`/${command.name} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
|
|
23
|
+
return () => {};
|
|
24
|
+
}
|
|
25
|
+
if (command.prefix !== undefined && (typeof command.prefix !== "string" || [...command.prefix].length !== 1 || /[\p{L}\p{N}\s/]/u.test(command.prefix))) {
|
|
26
|
+
throw new Error(`command /${command.name}: prefix must be one character that is not a letter, digit, space or "/"`);
|
|
27
|
+
}
|
|
28
|
+
let entry: CommandEntry = { command, plugin: caller.plugin };
|
|
29
|
+
let prefix = command.prefix;
|
|
30
|
+
const owner = prefix === undefined ? undefined : prefixes.get(prefix);
|
|
31
|
+
if (prefix !== undefined && owner !== undefined) {
|
|
32
|
+
// 简写被占了:命令照常注册,只是没有简写
|
|
33
|
+
ctx.report(`the ${prefix} shortcut for /${command.name} from ${caller.plugin} was skipped: /${owner} already uses it`);
|
|
34
|
+
const { prefix: _, prefixHint: __, ...rest } = command;
|
|
35
|
+
entry = { command: rest as typeof command, plugin: caller.plugin };
|
|
36
|
+
prefix = undefined;
|
|
37
|
+
}
|
|
20
38
|
registry.set(key, entry);
|
|
39
|
+
if (prefix !== undefined) prefixes.set(prefix, key);
|
|
21
40
|
const revoke = () => {
|
|
22
41
|
if (registry.get(key) === entry) registry.delete(key);
|
|
42
|
+
if (prefix !== undefined && prefixes.get(prefix) === key) prefixes.delete(prefix);
|
|
23
43
|
};
|
|
24
44
|
caller.onDispose(revoke);
|
|
25
45
|
return revoke;
|
|
26
46
|
},
|
|
27
47
|
get: (name) => registry.get(name.toLowerCase()),
|
|
48
|
+
byPrefix: (prefix) => {
|
|
49
|
+
const name = prefixes.get(prefix);
|
|
50
|
+
return name === undefined ? undefined : registry.get(name);
|
|
51
|
+
},
|
|
28
52
|
list: () =>
|
|
29
53
|
[...registry.values()]
|
|
30
|
-
.map(({ command: c, plugin }) => ({ name: c.name, ...(c.args ? { args: c.args } : {}), ...(c.description ? { description: c.description } : {}), ...(c.instant ? { instant: true } : {}), plugin }))
|
|
54
|
+
.map(({ command: c, plugin }) => ({ name: c.name, ...(c.args ? { args: c.args } : {}), ...(c.description ? { description: c.description } : {}), ...(c.instant ? { instant: true } : {}), ...(c.prefix ? { prefix: c.prefix } : {}), ...(c.prefixHint ? { prefixHint: c.prefixHint } : {}), plugin }))
|
|
31
55
|
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
32
56
|
}));
|
|
33
57
|
},
|
|
@@ -32,6 +32,8 @@ export interface CompactorOptions {
|
|
|
32
32
|
/** 压缩后保留最近大约这么多 token 的原始消息,默认阈值的 30% */
|
|
33
33
|
keepTokens?: number;
|
|
34
34
|
id?: string;
|
|
35
|
+
/** 写文件的工具把路径放在输入的哪个字段(工具的 meta.writes);摘要后面列出改过的文件用 */
|
|
36
|
+
writes(tool: string): string | undefined;
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
export interface Compactor {
|
|
@@ -41,6 +43,8 @@ export interface Compactor {
|
|
|
41
43
|
observe(e: LogEvent): void;
|
|
42
44
|
/** 下一次请求前无论是否超过阈值都总结一次;focus 是用户要总结特别留意的事(/compact 后面写的) */
|
|
43
45
|
request(focus?: string): void;
|
|
46
|
+
/** 最近一次 project 给出的投影有多少 token:和判断要不要压缩用的是同一个数(能用真实用量就用);还没投影过时 undefined */
|
|
47
|
+
projected(): number | undefined;
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
const SUMMARY_SYSTEM =
|
|
@@ -129,6 +133,7 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
129
133
|
/** 最近一次投影:它对应的消息条数和压缩状态;收到回复后补上真实用量 */
|
|
130
134
|
let last: { length: number; state: string; tokens?: number } | undefined;
|
|
131
135
|
let pending: { length: number; state: string } | undefined;
|
|
136
|
+
let projected: number | undefined;
|
|
132
137
|
|
|
133
138
|
const view = (messages: Message[], c: CompactState | undefined): Message[] => {
|
|
134
139
|
if (!c) return messages;
|
|
@@ -169,6 +174,8 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
169
174
|
const keep = opts.keepTokens ?? Math.floor(threshold * 0.3);
|
|
170
175
|
const done = (out: Message[], c = prev) => {
|
|
171
176
|
pending = { length: messages.length, state: JSON.stringify(c ?? null) };
|
|
177
|
+
// 没有清理、没有压缩时就是上面量出来的那个数;变了的(少见)重新估
|
|
178
|
+
projected = out === current ? tokens : estimateTokens(out);
|
|
172
179
|
return out;
|
|
173
180
|
};
|
|
174
181
|
const current = view(messages, prev);
|
|
@@ -222,7 +229,7 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
222
229
|
const todos = latestTodos(messages.slice(cut)) ? undefined : latestTodos(messages.slice(0, cut));
|
|
223
230
|
if (todos?.length) summary += `\n\nTodo list at the time of compaction:\n${renderTodos(todos)}`;
|
|
224
231
|
// 用户的原话和改过的文件也原样带上:只靠总结,压缩几轮后要求和约束容易走样
|
|
225
|
-
summary += appendix(messages.slice(0, cut));
|
|
232
|
+
summary += appendix(messages.slice(0, cut), opts.writes);
|
|
226
233
|
|
|
227
234
|
// 保留部分里已经清理过的输出继续保持清理,否则投影反而变大
|
|
228
235
|
const next: CompactState = { upTo: cut, summary, ...(prev?.trimUpTo && prev.trimUpTo > cut ? { trimUpTo: prev.trimUpTo } : {}) };
|
|
@@ -243,6 +250,7 @@ export function compactor(opts: CompactorOptions): Compactor {
|
|
|
243
250
|
forced = true;
|
|
244
251
|
focus = what;
|
|
245
252
|
},
|
|
253
|
+
projected: () => projected,
|
|
246
254
|
};
|
|
247
255
|
}
|
|
248
256
|
|
|
@@ -288,13 +296,12 @@ function renderTodos(todos: Todo[]): string {
|
|
|
288
296
|
return todos.map((t) => `${MARK[t.status] ?? "[ ]"} ${t.content}`).join("\n");
|
|
289
297
|
}
|
|
290
298
|
|
|
291
|
-
const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
292
299
|
/** 摘要后面附的用户原话的总长度上限 */
|
|
293
300
|
const MAX_USER_CHARS = 6000;
|
|
294
301
|
const MAX_USER_MESSAGE_CHARS = 1500;
|
|
295
302
|
|
|
296
303
|
/** 被压缩部分里用户的原话(第一条和最近的几条)和成功修改过的文件 */
|
|
297
|
-
function appendix(messages: Message[]): string {
|
|
304
|
+
function appendix(messages: Message[], writes: (tool: string) => string | undefined): string {
|
|
298
305
|
const said: string[] = [];
|
|
299
306
|
const ok = new Set<string>();
|
|
300
307
|
const files: string[] = [];
|
|
@@ -305,8 +312,9 @@ function appendix(messages: Message[]): string {
|
|
|
305
312
|
const text = m.content.flatMap((b) => (b.kind === "text" && b.origin.by === "user" ? [b.text.trim()] : [])).join("\n");
|
|
306
313
|
if (text) said.push(text.length > MAX_USER_MESSAGE_CHARS ? `${text.slice(0, MAX_USER_MESSAGE_CHARS)}…` : text);
|
|
307
314
|
for (const b of m.content) {
|
|
308
|
-
|
|
309
|
-
|
|
315
|
+
const field = b.kind === "tool_use" && ok.has(b.id) ? writes(b.name) : undefined;
|
|
316
|
+
if (field === undefined || b.kind !== "tool_use") continue;
|
|
317
|
+
const p = (b.input as Record<string, unknown> | null)?.[field];
|
|
310
318
|
if (typeof p === "string" && !files.includes(p)) files.push(p);
|
|
311
319
|
}
|
|
312
320
|
}
|
package/plugins/compact/index.ts
CHANGED
|
@@ -58,12 +58,24 @@ export default definePlugin({
|
|
|
58
58
|
},
|
|
59
59
|
|
|
60
60
|
session(ctx) {
|
|
61
|
+
// 子会话(子 Agent)不压缩:它活不长,上下文由开它的插件管(jade/task 接近窗口时让它写报告、只放最近的工具输出)
|
|
62
|
+
if (ctx.session.parent) return;
|
|
61
63
|
const models = ctx.require<Models>("models@1");
|
|
62
64
|
const configured = parseTokens(ctx.config.thresholdTokens);
|
|
63
65
|
const keepTokens = parseTokens(ctx.config.keepTokens);
|
|
64
66
|
const threshold = thresholdFor(ctx.config, models);
|
|
65
|
-
const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin });
|
|
67
|
+
const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin, writes: (t) => ctx.session.tool(t)?.meta?.writes });
|
|
66
68
|
const id = ctx.session.id;
|
|
69
|
+
/** 状态栏上的一段:多少 token 时自动压缩,投影离它不远(85%)时变黄 */
|
|
70
|
+
const show = (projected?: number) => {
|
|
71
|
+
const at = threshold();
|
|
72
|
+
if (!at) return ctx.status?.(undefined);
|
|
73
|
+
ctx.status?.({ text: `compacts at ${tokens(at)}`, ...(projected !== undefined && projected >= at * 0.85 ? { tone: "warn" as const } : {}) });
|
|
74
|
+
};
|
|
75
|
+
show();
|
|
76
|
+
// 换了模型,窗口和阈值跟着变
|
|
77
|
+
const offModel = models.subscribe?.(() => show());
|
|
78
|
+
if (offModel) void ctx.effect(() => offModel);
|
|
67
79
|
|
|
68
80
|
ctx.kernel.hook({
|
|
69
81
|
kind: "transform",
|
|
@@ -71,13 +83,15 @@ export default definePlugin({
|
|
|
71
83
|
// 排在其他改投影的插件(例如 jade/mentions 附上引用的文件)之后:估算的是最终的投影
|
|
72
84
|
order: 100,
|
|
73
85
|
timeoutMs: SUMMARY_TIMEOUT_MS,
|
|
74
|
-
fn: (messages, hook) => {
|
|
86
|
+
fn: async (messages, hook) => {
|
|
75
87
|
const focus = requested.get(id);
|
|
76
88
|
if (focus !== undefined) {
|
|
77
89
|
requested.delete(id);
|
|
78
90
|
c.request(focus || undefined);
|
|
79
91
|
}
|
|
80
|
-
|
|
92
|
+
const out = await c.project(messages, hook);
|
|
93
|
+
show(c.projected());
|
|
94
|
+
return out;
|
|
81
95
|
},
|
|
82
96
|
});
|
|
83
97
|
|
|
@@ -110,3 +124,6 @@ export default definePlugin({
|
|
|
110
124
|
},
|
|
111
125
|
});
|
|
112
126
|
|
|
127
|
+
/** 状态栏上的 token 数:12.3k、160k、1.2M */
|
|
128
|
+
const tokens = (n: number) =>
|
|
129
|
+
n >= 1_000_000 ? `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M` : n >= 1000 ? `${(n / 1000).toFixed(n >= 10_000 ? 0 : 1)}k` : String(n);
|
package/plugins/compact/style.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 显示用的小工具:截断,和带语义样式的文字(jade-api 的 Text)。插件只说这段是次要的、成功、警告……,
|
|
2
|
+
// 怎么画由前端决定(终端界面画成颜色,-p 只取文字)。插件之间不共享代码(只能共享类型),所以每个插件有一份。
|
|
3
|
+
|
|
4
|
+
import type { Span, Text, TextStyle } from "@uncarved/jade-api";
|
|
2
5
|
|
|
3
6
|
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export const
|
|
14
|
-
export const red = paint("31", "39");
|
|
15
|
-
export const green = paint("32", "39");
|
|
16
|
-
export const yellow = paint("33", "39");
|
|
17
|
-
export const bold = paint("1", "22");
|
|
8
|
+
const styled = (style: TextStyle) => (text: string): Span => ({ text, style });
|
|
9
|
+
export const dim = styled("dim");
|
|
10
|
+
export const bold = styled("bold");
|
|
11
|
+
export const ok = styled("ok");
|
|
12
|
+
export const warn = styled("warn");
|
|
13
|
+
export const error = styled("error");
|
|
14
|
+
|
|
15
|
+
/** 几行接成一段,行之间换行 */
|
|
16
|
+
export const lines = (rows: readonly Text[]): Text => rows.flatMap((r, i) => (i ? ["\n", r] : [r]));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// jade/completions:输入框补全的注册表(completions@1)。前端在输入框里遇到触发字符时查这里取候选;
|
|
2
|
+
// 候选是什么、发送时拿它做什么归登记的插件(例如 jade/mentions 的 @ 文件)。只登记,不知道谁在用。
|
|
3
|
+
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
|
+
import type { Completion, Completions } from "@uncarved/jade-api/services";
|
|
6
|
+
|
|
7
|
+
const registry = new Map<string, { completion: Completion; plugin: string }>();
|
|
8
|
+
|
|
9
|
+
export default definePlugin({
|
|
10
|
+
process(ctx) {
|
|
11
|
+
ctx.provide("completions@1", (caller): Completions => ({
|
|
12
|
+
register(completion) {
|
|
13
|
+
const trigger = completion?.trigger;
|
|
14
|
+
if (typeof trigger !== "string" || [...trigger].length !== 1 || /[\p{L}\p{N}\s/]/u.test(trigger)) {
|
|
15
|
+
throw new Error(`invalid completion trigger ${JSON.stringify(trigger)}: use one character that is not a letter, digit, space or "/"`);
|
|
16
|
+
}
|
|
17
|
+
if (typeof completion.candidates !== "function") throw new Error(`completion ${trigger} has no candidates function`);
|
|
18
|
+
// 重名时先登记的留下,后来的跳过并报告(同 commands@1)
|
|
19
|
+
const taken = registry.get(trigger);
|
|
20
|
+
if (taken) {
|
|
21
|
+
ctx.report(`completion ${trigger} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
|
|
22
|
+
return () => {};
|
|
23
|
+
}
|
|
24
|
+
const entry = { completion, plugin: caller.plugin };
|
|
25
|
+
registry.set(trigger, entry);
|
|
26
|
+
const revoke = () => {
|
|
27
|
+
if (registry.get(trigger) === entry) registry.delete(trigger);
|
|
28
|
+
};
|
|
29
|
+
caller.onDispose(revoke);
|
|
30
|
+
return revoke;
|
|
31
|
+
},
|
|
32
|
+
list: () => [...registry.values()],
|
|
33
|
+
}));
|
|
34
|
+
},
|
|
35
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// jade/copy:/copy 把最后一条回复(模型写的原文,没有折行和排版)放进剪贴板;/copy code 只放它的最后一个代码块。
|
|
2
|
+
// 原来写在 jade/tui 里;回复从当前会话的状态里取,剪贴板经 ui@1.copy,所以任何有剪贴板的前端都能用。
|
|
3
|
+
|
|
4
|
+
import type { Block } from "uncarved";
|
|
5
|
+
import { definePlugin, type Span, type TextStyle } from "@uncarved/jade-api";
|
|
6
|
+
import type { Commands } from "@uncarved/jade-api/services";
|
|
7
|
+
|
|
8
|
+
const styled = (style: TextStyle) => (text: string): Span => ({ text, style });
|
|
9
|
+
const dim = styled("dim");
|
|
10
|
+
const error = styled("error");
|
|
11
|
+
|
|
12
|
+
/** 最近一条有正文的助手消息的正文 */
|
|
13
|
+
export function lastReply(messages: readonly { role: string; content: readonly Block[] }[]): string | undefined {
|
|
14
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
15
|
+
const m = messages[i]!;
|
|
16
|
+
if (m.role !== "assistant") continue;
|
|
17
|
+
const text = m.content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n").trim();
|
|
18
|
+
if (text) return text;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Markdown 里最后一个围栏代码块的内容 */
|
|
24
|
+
export function lastCodeBlock(markdown: string): string | undefined {
|
|
25
|
+
const blocks = [...markdown.matchAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[`~]*[ \t]*$/gm)];
|
|
26
|
+
return blocks.at(-1)?.[2]?.replace(/\n$/, "");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default definePlugin({
|
|
30
|
+
process(ctx) {
|
|
31
|
+
ctx.require<Commands>("commands@1").register({
|
|
32
|
+
name: "copy",
|
|
33
|
+
args: "[code]",
|
|
34
|
+
description: "copy the last reply to the clipboard as it was written (code: only its last code block)",
|
|
35
|
+
instant: true,
|
|
36
|
+
run(args, c) {
|
|
37
|
+
const code = args.trim() === "code";
|
|
38
|
+
const reply = lastReply(c.sessions.current?.state.messages ?? []);
|
|
39
|
+
const text = code ? reply && lastCodeBlock(reply) : reply;
|
|
40
|
+
if (!text) return dim(code ? "The last reply has no code block." : "There is no reply to copy yet.");
|
|
41
|
+
if (!c.ui.copy?.(text)) return error("[error] this front end cannot copy to the clipboard");
|
|
42
|
+
return dim(`[copied ${[...text].length} characters]`);
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
});
|
package/plugins/editor/index.ts
CHANGED
|
@@ -66,10 +66,16 @@ export default definePlugin({
|
|
|
66
66
|
};
|
|
67
67
|
// 键被别的插件占了不算这个插件坏了:只是没有这个键,换一个就行
|
|
68
68
|
noKey = undefined;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
69
|
+
const keys = ctx.require<Keys>("keys@1");
|
|
70
|
+
const taken = keys.get(binding.key)?.plugin;
|
|
71
|
+
const fix = `set {"jade/editor": {"key": "ctrl+<letter>"}} in config.json to use another key`;
|
|
72
|
+
if (taken) noKey = `${binding.key} is already used by ${taken}; ${fix}`;
|
|
73
|
+
else {
|
|
74
|
+
try {
|
|
75
|
+
keys.register(binding);
|
|
76
|
+
} catch (e) {
|
|
77
|
+
noKey = `${e instanceof Error ? e.message : String(e)}; ${fix}`;
|
|
78
|
+
}
|
|
73
79
|
}
|
|
74
80
|
},
|
|
75
81
|
|
package/plugins/fs/diff.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// 先去掉相同的开头和结尾,只对中间部分求最长公共子序列;中间部分太大时(整个文件重写)
|
|
4
4
|
// 不再逐行比较,整段显示为删除加新增,避免占用大量内存和时间。
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { Text } from "@uncarved/jade-api";
|
|
7
|
+
import { added as plus, dim, lines as joinLines, removed as minus } from "./style.ts";
|
|
7
8
|
|
|
8
9
|
export type DiffLine = { kind: " " | "-" | "+"; text: string; /** 旧文件里的行号(删除、相同) */ old?: number; /** 新文件里的行号(新增、相同) */ new?: number };
|
|
9
10
|
|
|
@@ -110,11 +111,11 @@ export function diffStats(lines: DiffLine[]): DiffStats {
|
|
|
110
111
|
* 显示用:每行前面是行号(新增行用新文件的行号,其余用旧文件的),段与段之间用 ⋯ 分开。
|
|
111
112
|
* 超过 maxLines 行时截断
|
|
112
113
|
*/
|
|
113
|
-
export function renderDiff(before: string, after: string, maxLines = 60, lines = diffLines(before, after)):
|
|
114
|
+
export function renderDiff(before: string, after: string, maxLines = 60, lines = diffLines(before, after)): Text {
|
|
114
115
|
const parts = hunks(lines);
|
|
115
116
|
if (parts.length === 0) return dim(" (no changes)");
|
|
116
117
|
const width = String(lines.reduce((w, l) => Math.max(w, l.old ?? 0, l.new ?? 0), 0)).length;
|
|
117
|
-
const out:
|
|
118
|
+
const out: Text[] = [];
|
|
118
119
|
let shown = 0;
|
|
119
120
|
let total = 0;
|
|
120
121
|
for (const h of parts) total += h.length;
|
|
@@ -124,12 +125,12 @@ export function renderDiff(before: string, after: string, maxLines = 60, lines =
|
|
|
124
125
|
if (shown >= maxLines) break;
|
|
125
126
|
const no = String((l.kind === "+" ? l.new : l.old) ?? "").padStart(width);
|
|
126
127
|
const text = `${no} ${l.kind} ${l.text}`;
|
|
127
|
-
out.push(l.kind === "+" ?
|
|
128
|
+
out.push(l.kind === "+" ? plus(text) : l.kind === "-" ? minus(text) : dim(text));
|
|
128
129
|
shown++;
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
132
|
if (total > shown) out.push(dim(` … ${total - shown} more lines`));
|
|
132
133
|
const { added, removed } = diffStats(lines);
|
|
133
|
-
out.push(
|
|
134
|
-
return out
|
|
134
|
+
out.push([" ", plus(`+${added}`), " ", minus(`-${removed}`)]);
|
|
135
|
+
return joinLines(out);
|
|
135
136
|
}
|
package/plugins/fs/fs.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { Stats } from "node:fs";
|
|
|
10
10
|
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
11
11
|
import path from "node:path";
|
|
12
12
|
import type { Block, Message, Tool } from "uncarved";
|
|
13
|
+
import type { ToolMeta } from "@uncarved/jade-api";
|
|
13
14
|
import { grepTool } from "./grep.ts";
|
|
14
15
|
import { globMatcher, run } from "./sys.ts";
|
|
15
16
|
|
|
@@ -94,7 +95,7 @@ export class SeenFiles {
|
|
|
94
95
|
}
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
98
|
+
export function fsTools(cwd: string, opts: FsOptions = {}): (Tool & { meta?: ToolMeta })[] {
|
|
98
99
|
const at = (p: unknown) => resolveIn(cwd, p as string);
|
|
99
100
|
const tracked = opts.seen ?? new SeenFiles();
|
|
100
101
|
const seen = tracked.files;
|
|
@@ -201,8 +202,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
201
202
|
},
|
|
202
203
|
};
|
|
203
204
|
|
|
204
|
-
const writeFileTool: Tool = {
|
|
205
|
+
const writeFileTool: Tool & { meta: ToolMeta } = {
|
|
205
206
|
name: "write_file",
|
|
207
|
+
meta: { writes: "path" },
|
|
206
208
|
description:
|
|
207
209
|
"Create a file or overwrite it with the complete new content. Missing parent directories are created. " +
|
|
208
210
|
"An existing file must have been read with read_file first. Prefer edit_file for changing part of an existing file.",
|
|
@@ -262,8 +264,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
262
264
|
return { content: `Edited ${shown} (${total} replacement${total === 1 ? "" : "s"})` };
|
|
263
265
|
};
|
|
264
266
|
|
|
265
|
-
const editFileTool: Tool = {
|
|
267
|
+
const editFileTool: Tool & { meta: ToolMeta } = {
|
|
266
268
|
name: "edit_file",
|
|
269
|
+
meta: { writes: "path" },
|
|
267
270
|
description:
|
|
268
271
|
"Replace exact text in a file. old_string must match the file exactly (including whitespace and indentation) " +
|
|
269
272
|
"and must be unique unless replace_all is true. The file must have been read with read_file first, " +
|
|
@@ -284,8 +287,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
284
287
|
},
|
|
285
288
|
};
|
|
286
289
|
|
|
287
|
-
const multiEditTool: Tool = {
|
|
290
|
+
const multiEditTool: Tool & { meta: ToolMeta } = {
|
|
288
291
|
name: "multi_edit",
|
|
292
|
+
meta: { writes: "path" },
|
|
289
293
|
description:
|
|
290
294
|
"Make several exact-text replacements in one file at once. The edits are applied in order, each to the result of the previous one, " +
|
|
291
295
|
"with the same rules as edit_file. Either all edits succeed or the file is left unchanged.",
|
package/plugins/fs/grep.ts
CHANGED
|
@@ -173,9 +173,23 @@ const enough = (s: Search, files: FileHits[], matches: number) =>
|
|
|
173
173
|
|
|
174
174
|
// ---------- ripgrep ----------
|
|
175
175
|
|
|
176
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* ripgrep 不存在时返回 "missing";正则语法 ripgrep 不支持时返回 undefined,由调用方改用 JS 搜索。
|
|
178
|
+
* 先不用 --sort path:它让 ripgrep 只用一个线程(几万个文件的仓库慢好几倍),文件的先后在收完之后自己排。
|
|
179
|
+
* 结果多到提前停下时,先搜到的是哪些文件每次不同:按路径顺序再搜一次,同样的调用总是留下路径最靠前的那些。
|
|
180
|
+
* 这时命中很密,按顺序搜很快就够数了
|
|
181
|
+
*/
|
|
177
182
|
async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "missing"> {
|
|
178
|
-
const
|
|
183
|
+
const first = await ripgrepOnce(rg, s, false);
|
|
184
|
+
return typeof first === "object" && first.truncated ? ripgrepOnce(rg, s, true) : first;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function ripgrepOnce(rg: string, s: Search, sorted: boolean): Promise<Found | undefined | "missing"> {
|
|
188
|
+
const args = [...(sorted ? ["--sort", "path"] : []), "--hidden", "--glob", "!.git", "--glob", "!node_modules", "--max-filesize", `${MAX_FILE_BYTES}`];
|
|
189
|
+
// files、count 模式用不到每一行的内容:让 ripgrep 只报文件名(和行数),不用逐条解析 JSON
|
|
190
|
+
if (s.mode === "content") args.push("--json");
|
|
191
|
+
else if (s.mode === "files") args.push("--files-with-matches", "--null");
|
|
192
|
+
else args.push("--count", "--with-filename", "--null");
|
|
179
193
|
if (s.ignoreCase) args.push("--ignore-case");
|
|
180
194
|
if (s.context > 0) args.push("--context", String(s.context));
|
|
181
195
|
if (s.include) args.push("--glob", s.include);
|
|
@@ -214,15 +228,32 @@ async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "miss
|
|
|
214
228
|
hits.count++;
|
|
215
229
|
matches++;
|
|
216
230
|
}
|
|
217
|
-
|
|
231
|
+
hits.lines.push({ n: ev.data.line_number, text: text.replace(/\r?\n$/, ""), match });
|
|
232
|
+
};
|
|
233
|
+
/** files、count 模式的一个文件;不是合法 UTF-8 的路径跳过(同上) */
|
|
234
|
+
const add = (file: string, count: number) => {
|
|
235
|
+
if (!file.includes("�")) files.push({ file: path.resolve(file), count, lines: [] });
|
|
236
|
+
};
|
|
237
|
+
/** 取走 buf 里已经完整的记录。content:一行一个 JSON;files:路径后面一个 NUL;count:路径、NUL、行数、换行 */
|
|
238
|
+
const drain = () => {
|
|
239
|
+
for (;;) {
|
|
240
|
+
const end = buf.indexOf(s.mode === "content" ? "\n" : "\0");
|
|
241
|
+
if (end < 0) return;
|
|
242
|
+
let next = end + 1;
|
|
243
|
+
if (s.mode === "content") handle(buf.slice(0, end));
|
|
244
|
+
else if (s.mode === "files") add(buf.slice(0, end), 1);
|
|
245
|
+
else {
|
|
246
|
+
const nl = buf.indexOf("\n", end);
|
|
247
|
+
if (nl < 0) return;
|
|
248
|
+
add(buf.slice(0, end), Number(buf.slice(end + 1, nl)));
|
|
249
|
+
next = nl + 1;
|
|
250
|
+
}
|
|
251
|
+
buf = buf.slice(next);
|
|
252
|
+
}
|
|
218
253
|
};
|
|
219
254
|
for await (const chunk of proc.stdout) {
|
|
220
255
|
buf += decoder.decode(chunk, { stream: true });
|
|
221
|
-
|
|
222
|
-
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
223
|
-
handle(buf.slice(0, nl));
|
|
224
|
-
buf = buf.slice(nl + 1);
|
|
225
|
-
}
|
|
256
|
+
drain();
|
|
226
257
|
// count 模式要数完每个文件,只有文件数超了才停
|
|
227
258
|
if (enough(s, files, matches)) {
|
|
228
259
|
stopped = true;
|
|
@@ -230,11 +261,13 @@ async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "miss
|
|
|
230
261
|
break;
|
|
231
262
|
}
|
|
232
263
|
}
|
|
233
|
-
if (!stopped) handle(buf);
|
|
264
|
+
if (!stopped && s.mode === "content") handle(buf);
|
|
234
265
|
const [code, err] = await Promise.all([proc.exited, stderr]);
|
|
235
266
|
s.signal.throwIfAborted();
|
|
236
267
|
// 1:没有命中;2:出错,但部分文件出错(例如没有权限)时其余结果仍然有效
|
|
237
268
|
if (!stopped && code === 2 && /regex parse error|error parsing regex|look-?around|backreference/i.test(err)) return undefined;
|
|
269
|
+
// 和 JS 搜索一样按路径排
|
|
270
|
+
files.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0));
|
|
238
271
|
return { files, truncated: stopped };
|
|
239
272
|
} finally {
|
|
240
273
|
s.signal.removeEventListener("abort", kill);
|
package/plugins/fs/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// jade/fs:文件工具 read_file、write_file、edit_file、multi_edit、list_dir、glob、grep。
|
|
2
2
|
|
|
3
3
|
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
-
import type { Renderers
|
|
4
|
+
import type { Renderers } from "@uncarved/jade-api/services";
|
|
5
5
|
import { fsTools, SeenFiles } from "./fs.ts";
|
|
6
6
|
import { fsRenderers } from "./renderers.ts";
|
|
7
7
|
|
|
@@ -25,12 +25,7 @@ export default definePlugin({
|
|
|
25
25
|
seen.prune(request.messages);
|
|
26
26
|
},
|
|
27
27
|
});
|
|
28
|
+
// 子 Agent 是核心开的子会话,本插件在里面另有一份(读过的文件各算各的),不用再经 tools@1 另登记
|
|
28
29
|
for (const tool of fsTools(ctx.cwd, { responses, seen })) ctx.kernel.tool(tool);
|
|
29
|
-
// 嵌套 Agent(例如 jade/task 的子 Agent)的只读工具:另一套实例,它读过的文件不算主会话读过
|
|
30
|
-
// jade/tools 被隔离时它的服务已失效:嵌套 Agent 没有文件工具,文件工具本身照常
|
|
31
|
-
try {
|
|
32
|
-
const shared = ctx.service<Tools>("tools@1");
|
|
33
|
-
if (shared) for (const tool of fsTools(ctx.cwd)) if (tool.effect === "read") shared.register(tool);
|
|
34
|
-
} catch {}
|
|
35
30
|
},
|
|
36
31
|
});
|
package/plugins/fs/plugin.json
CHANGED