@uncarved/jade 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +277 -0
- package/assets/logo.svg +1 -0
- package/bin/jade.js +15 -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 +28 -5
- 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 +6 -4
- package/plugins/approval/index.ts +246 -84
- package/plugins/approval/plugin.json +5 -1
- package/plugins/approval/policy.ts +27 -6
- 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 +15 -4
- 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 +110 -17
- package/plugins/fs/grep.ts +41 -11
- package/plugins/fs/index.ts +9 -7
- 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 +40 -12
- 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 +33 -13
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +50 -42
- package/plugins/models/plugin.json +1 -1
- package/plugins/models/provider.ts +22 -4
- 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 +60 -12
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +50 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +14 -8
- package/plugins/prompts/results.ts +63 -0
- package/plugins/prompts/sys.ts +16 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +1 -1
- package/plugins/schedule/index.ts +2 -2
- package/plugins/scratch/index.ts +2 -3
- package/plugins/screenshot/index.ts +26 -13
- package/plugins/screenshot/plugin.json +1 -1
- package/plugins/sessions/index.ts +2 -17
- package/plugins/shell/index.ts +56 -6
- 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 +31 -16
- 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 +8 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +119 -15
- package/plugins/todo/index.ts +4 -4
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +130 -25
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -0
- package/plugins/tui/mentions.ts +48 -8
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +22 -3
- package/plugins/tui/tui.ts +323 -95
- package/plugins/tui/vendor.ts +16 -0
- package/plugins/undo/checkpoint.ts +26 -2
- package/plugins/undo/index.ts +86 -24
- 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 +2 -5
- package/plugins/web/renderers.ts +1 -1
- package/plugins/web/web.ts +6 -5
- package/plugins-dist/approval/index.js +232 -50
- package/plugins-dist/approval/plugin.json +5 -1
- package/plugins-dist/approval/policy.js +27 -5
- 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 +13 -2
- 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 +106 -17
- package/plugins-dist/fs/grep.js +40 -12
- package/plugins-dist/fs/index.js +8 -5
- 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 +41 -9
- 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 +29 -11
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +53 -16
- package/plugins-dist/models/plugin.json +1 -1
- package/plugins-dist/models/provider.js +4 -2
- 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 +62 -9
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +49 -2
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +13 -8
- package/plugins-dist/prompts/results.js +60 -0
- package/plugins-dist/prompts/sys.js +15 -3
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +1 -1
- package/plugins-dist/schedule/index.js +1 -1
- package/plugins-dist/scratch/index.js +1 -1
- package/plugins-dist/screenshot/index.js +25 -11
- package/plugins-dist/screenshot/plugin.json +1 -1
- package/plugins-dist/sessions/index.js +1 -1
- package/plugins-dist/shell/index.js +55 -3
- package/plugins-dist/shell/plugin.json +3 -1
- package/plugins-dist/shell/shell.js +26 -8
- 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 +7 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +106 -15
- package/plugins-dist/todo/index.js +3 -3
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +135 -18
- package/plugins-dist/tui/index.js +2 -1
- package/plugins-dist/tui/markdown.js +48 -6
- package/plugins-dist/tui/mascot.js +439 -0
- package/plugins-dist/tui/mentions.js +44 -6
- package/plugins-dist/tui/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +19 -2
- package/plugins-dist/tui/tui.js +303 -83
- package/plugins-dist/tui/vendor.js +13822 -0
- package/plugins-dist/undo/checkpoint.js +28 -1
- package/plugins-dist/undo/index.js +92 -22
- 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 +1 -1
- package/plugins-dist/web/web.js +6 -5
- 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 +25 -6
- 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/fs/fs.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
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
|
-
import type { Block, Tool } from "uncarved";
|
|
12
|
+
import type { Block, Message, Tool } from "uncarved";
|
|
13
13
|
import { grepTool } from "./grep.ts";
|
|
14
14
|
import { globMatcher, run } from "./sys.ts";
|
|
15
15
|
|
|
@@ -49,6 +49,8 @@ export interface FsOptions {
|
|
|
49
49
|
responses?: { readonly calls: number };
|
|
50
50
|
/** grep 用的 ripgrep,默认 "rg";null 表示不用(测试) */
|
|
51
51
|
rg?: string | null;
|
|
52
|
+
/** 记录读过的文件;给了才能在压缩后让它们失效(见 SeenFiles.prune) */
|
|
53
|
+
seen?: SeenFiles;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
/** 读过或写过的文件当时的样子;只在本会话、本进程内有效,恢复的会话要重新读 */
|
|
@@ -57,16 +59,54 @@ interface Seen {
|
|
|
57
59
|
size: number;
|
|
58
60
|
/** 读取时的 responses.calls;写入的文件模型知道内容,记为 -1 */
|
|
59
61
|
call: number;
|
|
62
|
+
/**
|
|
63
|
+
* 模型从哪一次调用知道这个文件的内容:read_file 或 write_file 的 toolUseId。
|
|
64
|
+
* edit_file 只改了一部分,模型知道的仍是之前那次读取(加上改动),所以沿用原来的
|
|
65
|
+
*/
|
|
66
|
+
id?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 本会话读过、写过的文件 */
|
|
70
|
+
export class SeenFiles {
|
|
71
|
+
readonly files = new Map<string, Seen>();
|
|
72
|
+
/** 因为压缩而失效的文件:再改时说明原因,而不是说"没读过" */
|
|
73
|
+
readonly dropped = new Set<string>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 压缩或清理之后,模型看不到当初读到(写入)的内容了,但 mtime 和 size 没变,修改仍会放行;
|
|
77
|
+
* write_file 可能凭摘要里的记忆写出整个文件。这里按最终的投影检查:那次调用已被总结掉(投影里没有它的 tool_use),
|
|
78
|
+
* 或者结果已被清理(tool_result 里没有工具自己给的内容,只剩插件的占位说明),就当作没读过
|
|
79
|
+
*/
|
|
80
|
+
prune(projection: Message[]): void {
|
|
81
|
+
const uses = new Set<string>();
|
|
82
|
+
const cleared = new Set<string>();
|
|
83
|
+
for (const m of projection) {
|
|
84
|
+
for (const b of m.content) {
|
|
85
|
+
if (b.kind === "tool_use") uses.add(b.id);
|
|
86
|
+
else if (b.kind === "tool_result" && !b.content.some((c) => c.origin.by === "tool")) cleared.add(b.toolUseId);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const [full, s] of this.files) {
|
|
90
|
+
if (s.id === undefined || (uses.has(s.id) && !cleared.has(s.id))) continue;
|
|
91
|
+
this.files.delete(full);
|
|
92
|
+
this.dropped.add(full);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
60
95
|
}
|
|
61
96
|
|
|
62
97
|
export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
63
98
|
const at = (p: unknown) => resolveIn(cwd, p as string);
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
99
|
+
const tracked = opts.seen ?? new SeenFiles();
|
|
100
|
+
const seen = tracked.files;
|
|
101
|
+
/**
|
|
102
|
+
* 文件没变时保留更早的 call:之前的回复里读过,这次回复里又读一遍,不影响修改。
|
|
103
|
+
* id 为 undefined 表示沿用原来的(edit_file)
|
|
104
|
+
*/
|
|
105
|
+
const remember = (full: string, info: Stats, call: number, id: string | undefined) => {
|
|
67
106
|
const s = seen.get(full);
|
|
68
107
|
const same = s !== undefined && s.mtimeMs === info.mtimeMs && s.size === info.size;
|
|
69
|
-
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call });
|
|
108
|
+
seen.set(full, { mtimeMs: info.mtimeMs, size: info.size, call: same ? Math.min(s.call, call) : call, id: id ?? s?.id });
|
|
109
|
+
tracked.dropped.delete(full);
|
|
70
110
|
};
|
|
71
111
|
/** 不能修改已有文件的原因;可以修改时返回 undefined。text 是文件现在的内容 */
|
|
72
112
|
const staleness = (full: string, info: Stats): string | undefined => {
|
|
@@ -80,6 +120,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
80
120
|
}
|
|
81
121
|
return undefined;
|
|
82
122
|
}
|
|
123
|
+
if (tracked.dropped.has(full)) {
|
|
124
|
+
return `The earlier read of ${shown} was compacted out of the conversation, so its content is no longer in context. Read it again before changing it.`;
|
|
125
|
+
}
|
|
83
126
|
return `${shown} exists but has not been read in this session. Read it with read_file before changing it.`;
|
|
84
127
|
};
|
|
85
128
|
|
|
@@ -98,7 +141,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
98
141
|
effect: "read",
|
|
99
142
|
concurrency: "safe",
|
|
100
143
|
idempotent: true,
|
|
101
|
-
async execute(input) {
|
|
144
|
+
async execute(input, ctx) {
|
|
102
145
|
const { path: p, offset = 1, limit = MAX_LINES } = input as { path: string; offset?: number; limit?: number };
|
|
103
146
|
if (limit < 1) return { content: "limit must be at least 1", isError: true };
|
|
104
147
|
const full = at(p);
|
|
@@ -129,8 +172,11 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
129
172
|
if (buf.subarray(0, 8000).includes(0)) {
|
|
130
173
|
return { content: `${display(cwd, full)} is a binary file (${buf.length} bytes) and cannot be shown as text`, isError: true };
|
|
131
174
|
}
|
|
132
|
-
|
|
133
|
-
|
|
175
|
+
// 不是 UTF-8 的文件(多半是 GBK)照样给模型看,但不算读过:改它会把原来的字节写坏
|
|
176
|
+
const strict = utf8(buf);
|
|
177
|
+
const text = strict ?? legacyText(buf);
|
|
178
|
+
const note = strict === undefined ? `\n(${notUtf8(display(cwd, full))})` : "";
|
|
179
|
+
if (strict !== undefined) remember(full, info, opts.responses?.calls ?? -1, ctx?.toolUseId);
|
|
134
180
|
if (text.length === 0) return { content: "(empty file)" };
|
|
135
181
|
const lines = text.split(/\r?\n/);
|
|
136
182
|
if (lines.at(-1) === "") lines.pop();
|
|
@@ -149,9 +195,9 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
149
195
|
const body = out.join("\n");
|
|
150
196
|
const end = start + out.length - 1;
|
|
151
197
|
if (out.length < slice.length) {
|
|
152
|
-
return { content: `${body}\n(output capped at about ${MAX_READ_CHARS} characters: showing lines ${start}-${end} of ${lines.length}; continue with offset ${end + 1})` };
|
|
198
|
+
return { content: `${body}\n(output capped at about ${MAX_READ_CHARS} characters: showing lines ${start}-${end} of ${lines.length}; continue with offset ${end + 1})${note}` };
|
|
153
199
|
}
|
|
154
|
-
return { content: end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body };
|
|
200
|
+
return { content: (end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body) + note };
|
|
155
201
|
},
|
|
156
202
|
};
|
|
157
203
|
|
|
@@ -170,20 +216,21 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
170
216
|
concurrency: "exclusive",
|
|
171
217
|
// 写入完整内容,重复执行结果相同
|
|
172
218
|
idempotent: true,
|
|
173
|
-
async execute(input) {
|
|
219
|
+
async execute(input, ctx) {
|
|
174
220
|
const { path: p, content: given } = input as { path: string; content: string };
|
|
175
221
|
const full = at(p);
|
|
176
222
|
const info = await statOrUndefined(full);
|
|
177
223
|
if (info?.isDirectory()) return { content: `${display(cwd, full)} is a directory`, isError: true };
|
|
178
224
|
// 同样内容重复写入(崩溃后重跑)不算覆盖别人的修改
|
|
179
|
-
const before = info ? await readFile(full
|
|
225
|
+
const before = info ? utf8(await readFile(full)) ?? undefined : null;
|
|
226
|
+
if (before === undefined) return { content: notUtf8(display(cwd, full)), isError: true };
|
|
180
227
|
// read_file 不显示换行风格和 BOM:覆盖已有文件时沿用原来的,否则整个文件每一行都算改了
|
|
181
228
|
const content = before === null ? given : keepStyle(before, given);
|
|
182
229
|
const problem = info && before !== null && before !== content ? staleness(full, info) : undefined;
|
|
183
230
|
if (problem) return { content: problem, isError: true };
|
|
184
231
|
await mkdir(path.dirname(full), { recursive: true });
|
|
185
232
|
await writeFile(full, content);
|
|
186
|
-
remember(full, await stat(full), -1);
|
|
233
|
+
remember(full, await stat(full), -1, ctx?.toolUseId);
|
|
187
234
|
// 和 read_file 一样数行:末尾的换行不算多出一行
|
|
188
235
|
const lines = content === "" ? 0 : content.split(/\r?\n/).length - (/\r?\n$/.test(content) ? 1 : 0);
|
|
189
236
|
return { content: `Wrote ${lines} lines to ${display(cwd, full)}` };
|
|
@@ -198,7 +245,8 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
198
245
|
if (!info) {
|
|
199
246
|
return { content: seen.has(full) ? `${shown} no longer exists` : `${shown} does not exist; use write_file to create it`, isError: true };
|
|
200
247
|
}
|
|
201
|
-
const text = await readFile(full
|
|
248
|
+
const text = utf8(await readFile(full));
|
|
249
|
+
if (text === undefined) return { content: notUtf8(shown), isError: true };
|
|
202
250
|
const problem = staleness(full, info);
|
|
203
251
|
if (problem) return { content: problem, isError: true };
|
|
204
252
|
let next = text;
|
|
@@ -210,7 +258,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
210
258
|
total += r.count;
|
|
211
259
|
}
|
|
212
260
|
await writeFile(full, next);
|
|
213
|
-
remember(full, await stat(full), -1);
|
|
261
|
+
remember(full, await stat(full), -1, undefined);
|
|
214
262
|
return { content: `Edited ${shown} (${total} replacement${total === 1 ? "" : "s"})` };
|
|
215
263
|
};
|
|
216
264
|
|
|
@@ -286,6 +334,7 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
286
334
|
name: "glob",
|
|
287
335
|
description:
|
|
288
336
|
`Find files by glob pattern (e.g. "src/**/*.ts"), relative to path (default: the working directory). ` +
|
|
337
|
+
`Unlike grep's include, "*.ts" matches only directly under path; use "**/*.ts" for any depth. ` +
|
|
289
338
|
`Skips files ignored by git, .git and node_modules. Returns at most ${MAX_GLOB} paths.`,
|
|
290
339
|
inputSchema: {
|
|
291
340
|
type: "object",
|
|
@@ -316,7 +365,8 @@ export function fsTools(cwd: string, opts: FsOptions = {}): Tool[] {
|
|
|
316
365
|
out.push(display(cwd, path.join(root, rel)));
|
|
317
366
|
}
|
|
318
367
|
out.sort();
|
|
319
|
-
|
|
368
|
+
// 和 grep 的 include 不同,不含 / 的模式只匹配第一层:模型常按 grep 的习惯写
|
|
369
|
+
if (out.length === 0) return { content: pattern.includes("/") || pattern.startsWith("**") ? "No files matched" : `No files matched. "${pattern}" only matches directly under ${p === "." ? "the working directory" : p}; use "**/${pattern}" to search at any depth.` };
|
|
320
370
|
return { content: out.join("\n") + (more ? `\n(more than ${MAX_GLOB} matches; narrow the pattern)` : "") };
|
|
321
371
|
},
|
|
322
372
|
};
|
|
@@ -341,6 +391,28 @@ export function applyEditsToText(text: string, edits: Edit[], shown: string): st
|
|
|
341
391
|
return { text: next };
|
|
342
392
|
}
|
|
343
393
|
|
|
394
|
+
/** 按 UTF-8 解码(BOM 留着);不是合法的 UTF-8 时 undefined */
|
|
395
|
+
export function utf8(buf: Uint8Array): string | undefined {
|
|
396
|
+
try {
|
|
397
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buf);
|
|
398
|
+
} catch {
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** 不是 UTF-8 的文本只用来显示:按 GB18030(含 GBK)解码,运行时不支持时按 UTF-8 解码、坏的字节显示成 U+FFFD */
|
|
404
|
+
function legacyText(buf: Buffer): string {
|
|
405
|
+
try {
|
|
406
|
+
return new TextDecoder("gb18030").decode(buf);
|
|
407
|
+
} catch {
|
|
408
|
+
return buf.toString("utf8");
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const notUtf8 = (shown: string) =>
|
|
413
|
+
`${shown} is not valid UTF-8 (read_file shows it decoded as GB18030). write_file and edit_file only handle UTF-8 and would corrupt it, so they refuse it: ` +
|
|
414
|
+
"change it with a command that keeps its encoding, or ask the user before converting it to UTF-8.";
|
|
415
|
+
|
|
344
416
|
/** text 里多数换行是 CRLF 时返回 "\r\n",多数是 LF 时返回 "\n",一样多或没有换行时返回 fallback */
|
|
345
417
|
function eolOf(text: string, fallback: string): string {
|
|
346
418
|
const crlf = text.split("\r\n").length - 1;
|
|
@@ -366,7 +438,9 @@ function replaceIn(text: string, { old_string, new_string, replace_all }: Edit,
|
|
|
366
438
|
const pattern = new RegExp(old_string.split(/\r?\n/).map(escapeRegExp).join("\\r?\\n"), "g");
|
|
367
439
|
const count = text.match(pattern)?.length ?? 0;
|
|
368
440
|
if (count === 0) return `old_string not found in ${shown}${nearMiss(text, old_string)}`;
|
|
369
|
-
if (count > 1 && !replace_all)
|
|
441
|
+
if (count > 1 && !replace_all) {
|
|
442
|
+
return `old_string occurs ${count} times in ${shown} (${linesOf(text, pattern, 5)}); add context to make it unique or set replace_all`;
|
|
443
|
+
}
|
|
370
444
|
// new_string 的换行沿用被替换那段的;那段只有一行时沿用整个文件的,单行改成多行也不会混进 LF
|
|
371
445
|
const fileEol = eolOf(text, "\n");
|
|
372
446
|
const to = (matched: string) => (new_string.includes("\r\n") ? new_string : new_string.replaceAll("\n", eolOf(matched, fileEol)));
|
|
@@ -374,11 +448,30 @@ function replaceIn(text: string, { old_string, new_string, replace_all }: Edit,
|
|
|
374
448
|
return { text: text.replace(replace_all ? pattern : new RegExp(pattern.source), to), count: replace_all ? count : 1 };
|
|
375
449
|
}
|
|
376
450
|
|
|
451
|
+
/** pattern 前 max 处匹配所在的行号,例如 "lines 3, 17, 40" */
|
|
452
|
+
function linesOf(text: string, pattern: RegExp, max: number): string {
|
|
453
|
+
const found: number[] = [];
|
|
454
|
+
let line = 1;
|
|
455
|
+
let from = 0;
|
|
456
|
+
for (const m of text.matchAll(new RegExp(pattern.source, "g"))) {
|
|
457
|
+
for (let i = from; i < m.index; i++) if (text.charCodeAt(i) === 10) line++;
|
|
458
|
+
from = m.index;
|
|
459
|
+
found.push(line);
|
|
460
|
+
if (found.length === max) break;
|
|
461
|
+
}
|
|
462
|
+
return `line${found.length > 1 ? "s" : ""} ${found.join(", ")}${found.length === max ? ", …" : ""}`;
|
|
463
|
+
}
|
|
464
|
+
|
|
377
465
|
/**
|
|
378
466
|
* old_string 找不到时帮模型定位:忽略空白后能匹配到哪几行,或者第一行出现在哪里。
|
|
379
467
|
* 只给提示,不自动按模糊匹配修改:那样可能改错地方
|
|
380
468
|
*/
|
|
381
469
|
export function nearMiss(text: string, needle: string): string {
|
|
470
|
+
// 最常见的一种:把 read_file 每行开头的行号和制表符一起抄进来了
|
|
471
|
+
const raw = needle.split(/\r?\n/).filter((l) => l.trim() !== "");
|
|
472
|
+
if (raw.length > 0 && raw.every((l) => /^\s*\d+\t/.test(l))) {
|
|
473
|
+
return ". It seems to include read_file's line-number prefixes (the number and tab at the start of each line); they are not part of the file. Remove them and keep the rest exactly";
|
|
474
|
+
}
|
|
382
475
|
const norm = (l: string) => l.trim().replace(/\s+/g, " ");
|
|
383
476
|
const lines = text.split(/\r?\n/).map(norm);
|
|
384
477
|
const want = needle.split(/\r?\n/).map(norm);
|
package/plugins/fs/grep.ts
CHANGED
|
@@ -72,7 +72,8 @@ export function grepTool(cwd: string, opts: GrepOptions = {}): Tool {
|
|
|
72
72
|
path: { type: "string" },
|
|
73
73
|
include: { type: "string" },
|
|
74
74
|
ignore_case: { type: "boolean" },
|
|
75
|
-
|
|
75
|
+
// files_with_matches 是 ripgrep / Claude Code 的叫法,很多模型会这样传,当作 files
|
|
76
|
+
output_mode: { type: "string", enum: ["content", "files", "count", "files_with_matches"] },
|
|
76
77
|
context: { type: "integer" },
|
|
77
78
|
},
|
|
78
79
|
required: ["pattern"],
|
|
@@ -87,9 +88,10 @@ export function grepTool(cwd: string, opts: GrepOptions = {}): Tool {
|
|
|
87
88
|
path?: string;
|
|
88
89
|
include?: string;
|
|
89
90
|
ignore_case?: boolean;
|
|
90
|
-
output_mode?: Mode;
|
|
91
|
+
output_mode?: Mode | "files_with_matches";
|
|
91
92
|
context?: number;
|
|
92
93
|
};
|
|
94
|
+
const mode: Mode = output_mode === "files_with_matches" ? "files" : output_mode;
|
|
93
95
|
let re: RegExp;
|
|
94
96
|
try {
|
|
95
97
|
re = new RegExp(pattern, ignore_case ? "i" : "");
|
|
@@ -102,8 +104,8 @@ export function grepTool(cwd: string, opts: GrepOptions = {}): Tool {
|
|
|
102
104
|
pattern,
|
|
103
105
|
ignoreCase: ignore_case,
|
|
104
106
|
include,
|
|
105
|
-
context:
|
|
106
|
-
mode
|
|
107
|
+
context: mode === "content" ? Math.min(Math.max(context, 0), MAX_CONTEXT) : 0,
|
|
108
|
+
mode,
|
|
107
109
|
signal,
|
|
108
110
|
};
|
|
109
111
|
let found: Found | undefined;
|
|
@@ -231,6 +233,33 @@ async function ripgrep(rg: string, s: Search): Promise<Found | undefined | "miss
|
|
|
231
233
|
|
|
232
234
|
// ---------- JS ----------
|
|
233
235
|
|
|
236
|
+
/** 同时读几个文件。逐个等 stat、readFile 时,几万个文件的仓库要好几秒(多数时间花在等 IO 上) */
|
|
237
|
+
const READ_AHEAD = 16;
|
|
238
|
+
|
|
239
|
+
/** 读出可以搜的文本;不存在(git 列出的文件可能已在工作区删除)、太大、二进制的返回 undefined */
|
|
240
|
+
async function readSearchable(file: string): Promise<Buffer | undefined> {
|
|
241
|
+
const info = await statOrUndefined(file);
|
|
242
|
+
if (!info?.isFile() || info.size > MAX_FILE_BYTES) return undefined;
|
|
243
|
+
const buf = await readFile(file).catch(() => undefined);
|
|
244
|
+
return buf && !buf.includes(0) ? buf : undefined;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 按 items 的顺序产出 fn 的结果,最多 n 个同时进行;调用方提前停止时,已经开始的读取随它结束 */
|
|
248
|
+
async function* ordered<T, R>(items: Iterable<T> | AsyncIterable<T>, n: number, fn: (item: T) => Promise<R>): AsyncGenerator<[T, R]> {
|
|
249
|
+
const pending: { item: T; result: Promise<R> }[] = [];
|
|
250
|
+
for await (const item of items) {
|
|
251
|
+
const result = fn(item);
|
|
252
|
+
// 先挂上处理,免得提前停止时没人等的 Promise 报 unhandled rejection
|
|
253
|
+
result.catch(() => {});
|
|
254
|
+
pending.push({ item, result });
|
|
255
|
+
if (pending.length >= n) {
|
|
256
|
+
const head = pending.shift()!;
|
|
257
|
+
yield [head.item, await head.result];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
for (const p of pending) yield [p.item, await p.result];
|
|
261
|
+
}
|
|
262
|
+
|
|
234
263
|
async function jsGrep(s: Search): Promise<Found> {
|
|
235
264
|
// 和 ripgrep 的 --glob 一致:不含 / 的模式("*.ts")匹配任意深度的文件名
|
|
236
265
|
const filter = s.include ? globMatcher(s.include.includes("/") ? s.include : `**/${s.include}`) : undefined;
|
|
@@ -239,14 +268,15 @@ async function jsGrep(s: Search): Promise<Found> {
|
|
|
239
268
|
const single = (await stat(s.root)).isFile();
|
|
240
269
|
const candidates = single ? [s.root] : await listFiles(s.root, s.signal);
|
|
241
270
|
const sorted = Array.isArray(candidates) ? [...candidates].sort() : candidates;
|
|
242
|
-
|
|
271
|
+
const wanted = async function* () {
|
|
272
|
+
for await (const file of sorted) {
|
|
273
|
+
if (filter && !single && !filter(path.relative(s.root, file).split(path.sep).join("/"))) continue;
|
|
274
|
+
yield file;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
for await (const [file, buf] of ordered(wanted(), READ_AHEAD, readSearchable)) {
|
|
243
278
|
s.signal.throwIfAborted();
|
|
244
|
-
if (
|
|
245
|
-
// git 列出的文件可能已在工作区删除
|
|
246
|
-
const info = await statOrUndefined(file);
|
|
247
|
-
if (!info?.isFile() || info.size > MAX_FILE_BYTES) continue;
|
|
248
|
-
const buf = await readFile(file);
|
|
249
|
-
if (buf.includes(0)) continue; // 二进制文件
|
|
279
|
+
if (!buf) continue;
|
|
250
280
|
const all = buf.toString("utf8").split(/\r?\n/);
|
|
251
281
|
if (all.at(-1) === "") all.pop();
|
|
252
282
|
const hits: FileHits = { file, count: 0, lines: [] };
|
package/plugins/fs/index.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
// jade/fs:文件工具 read_file、write_file、edit_file、multi_edit、list_dir、glob、grep。
|
|
2
2
|
|
|
3
|
-
import { definePlugin } from "
|
|
4
|
-
import type { Renderers } from "
|
|
5
|
-
import
|
|
6
|
-
import { fsTools } from "./fs.ts";
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
+
import type { Renderers, Task } from "@uncarved/jade-api/services";
|
|
5
|
+
import { fsTools, SeenFiles } from "./fs.ts";
|
|
7
6
|
import { fsRenderers } from "./renderers.ts";
|
|
8
7
|
|
|
9
8
|
export default definePlugin({
|
|
@@ -14,16 +13,19 @@ export default definePlugin({
|
|
|
14
13
|
},
|
|
15
14
|
|
|
16
15
|
session(ctx) {
|
|
17
|
-
//
|
|
16
|
+
// 数模型调用的次数:同一次回复里先读后改的,改的内容是看到读取结果之前写好的。
|
|
17
|
+
// 同时按最终发给模型的消息让被压缩(jade/compact 总结或清理)掉的读取失效:那些文件要重新读才能改
|
|
18
18
|
const responses = { calls: 0 };
|
|
19
|
+
const seen = new SeenFiles();
|
|
19
20
|
ctx.kernel.hook({
|
|
20
21
|
kind: "transform",
|
|
21
22
|
point: "model.before",
|
|
22
|
-
fn: () => {
|
|
23
|
+
fn: (request) => {
|
|
23
24
|
responses.calls++;
|
|
25
|
+
seen.prune(request.messages);
|
|
24
26
|
},
|
|
25
27
|
});
|
|
26
|
-
for (const tool of fsTools(ctx.cwd, { responses })) ctx.kernel.tool(tool);
|
|
28
|
+
for (const tool of fsTools(ctx.cwd, { responses, seen })) ctx.kernel.tool(tool);
|
|
27
29
|
// 子 Agent(jade/task)的只读工具:另一套实例,子 Agent 读过的文件不算主会话读过
|
|
28
30
|
// jade/task 被隔离时它的服务已失效:子 Agent 没有文件工具,文件工具本身照常
|
|
29
31
|
try {
|
package/plugins/fs/renderers.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { readFileSync } from "node:fs";
|
|
7
7
|
import { readFile } from "node:fs/promises";
|
|
8
8
|
import path from "node:path";
|
|
9
|
-
import type { ToolRenderer } from "
|
|
9
|
+
import type { ToolRenderer } from "@uncarved/jade-api/services";
|
|
10
10
|
import { diffLines, diffStats, renderDiff } from "./diff.ts";
|
|
11
11
|
import { applyEditsToText, display, type Edit } from "./fs.ts";
|
|
12
12
|
import { clip, dim, green, red, yellow } from "./style.ts";
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// jade/hooks:不写插件,在配置里写几条 shell 命令,就能在工具调用前后、一轮结束时做点事。
|
|
2
|
+
//
|
|
3
|
+
// $JADE_HOME/config.json(项目层的配置设不了这一项:克隆的仓库不能让你的机器跑命令):
|
|
4
|
+
//
|
|
5
|
+
// { "jade/hooks": { "hooks": [
|
|
6
|
+
// { "on": "before", "tool": "bash", "command": "python ~/.jade/guard.py" },
|
|
7
|
+
// { "on": "after", "tool": "edit_file", "command": "bunx prettier --write \"$JADE_TOOL_PATH\"" },
|
|
8
|
+
// { "on": "turn", "command": "powershell -c \"New-BurntToastNotification -Text 'jade is done'\"" }
|
|
9
|
+
// ] } }
|
|
10
|
+
//
|
|
11
|
+
// - before:工具执行前(审批的关卡里)运行。退出码不是 0 时这次调用被拒绝,命令的输出作为理由告诉模型;是 0 时不表态。
|
|
12
|
+
// - after:工具成功执行后运行。退出码不是 0 时把输出留给模型看(下一次请求时附上),不改工具的结果。
|
|
13
|
+
// - turn:一轮结束后运行,结果不影响会话。
|
|
14
|
+
// tool 是工具名,不写或写 "*" 表示所有工具。命令经 shell@1 在工作目录里运行(和 bash 工具同一个 shell),环境变量:
|
|
15
|
+
// JADE_TOOL(工具名)、JADE_TOOL_INPUT(参数的 JSON 文件的路径)、JADE_TOOL_PATH(参数里有 path 时)。
|
|
16
|
+
|
|
17
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
18
|
+
import os from "node:os";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
21
|
+
import type { Prompts, Shell } from "@uncarved/jade-api/services";
|
|
22
|
+
|
|
23
|
+
export interface UserHook {
|
|
24
|
+
on: "before" | "after" | "turn";
|
|
25
|
+
/** 工具名;undefined 表示所有工具 */
|
|
26
|
+
tool?: string;
|
|
27
|
+
command: string;
|
|
28
|
+
timeoutMs: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_TIMEOUT_SECONDS = 60;
|
|
32
|
+
const MAX_TIMEOUT_SECONDS = 600;
|
|
33
|
+
const MAX_OUTPUT_CHARS = 4000;
|
|
34
|
+
|
|
35
|
+
/** 检查配置里的 hooks;写错的条目说明原因,其余的照用 */
|
|
36
|
+
export function parseHooks(value: unknown): { hooks: UserHook[]; problems: string[] } {
|
|
37
|
+
if (value === undefined) return { hooks: [], problems: [] };
|
|
38
|
+
if (!Array.isArray(value)) return { hooks: [], problems: ["hooks must be a list"] };
|
|
39
|
+
const hooks: UserHook[] = [];
|
|
40
|
+
const problems: string[] = [];
|
|
41
|
+
for (const [i, raw] of value.entries()) {
|
|
42
|
+
const h = (raw ?? {}) as { on?: unknown; tool?: unknown; command?: unknown; timeoutSeconds?: unknown };
|
|
43
|
+
const where = `hooks[${i}]`;
|
|
44
|
+
if (h.on !== "before" && h.on !== "after" && h.on !== "turn") problems.push(`${where}: "on" must be "before", "after" or "turn"`);
|
|
45
|
+
else if (typeof h.command !== "string" || !h.command.trim()) problems.push(`${where}: "command" must be a shell command`);
|
|
46
|
+
else if (h.tool !== undefined && typeof h.tool !== "string") problems.push(`${where}: "tool" must be a tool name`);
|
|
47
|
+
else if (h.timeoutSeconds !== undefined && !(typeof h.timeoutSeconds === "number" && h.timeoutSeconds > 0 && h.timeoutSeconds <= MAX_TIMEOUT_SECONDS)) {
|
|
48
|
+
problems.push(`${where}: "timeoutSeconds" must be a number up to ${MAX_TIMEOUT_SECONDS}`);
|
|
49
|
+
} else {
|
|
50
|
+
hooks.push({
|
|
51
|
+
on: h.on,
|
|
52
|
+
command: h.command,
|
|
53
|
+
timeoutMs: ((h.timeoutSeconds as number | undefined) ?? DEFAULT_TIMEOUT_SECONDS) * 1000,
|
|
54
|
+
...(typeof h.tool === "string" && h.tool !== "*" ? { tool: h.tool } : {}),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { hooks, problems };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 单引号括起来,给 POSIX shell 用 */
|
|
62
|
+
const quote = (s: string) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
63
|
+
const clip = (s: string) => (s.length > MAX_OUTPUT_CHARS ? `${s.slice(0, MAX_OUTPUT_CHARS)}\n… (${s.length - MAX_OUTPUT_CHARS} more characters)` : s);
|
|
64
|
+
|
|
65
|
+
let warned = false;
|
|
66
|
+
|
|
67
|
+
export default definePlugin({
|
|
68
|
+
session(ctx) {
|
|
69
|
+
const { hooks, problems } = parseHooks(ctx.config.hooks);
|
|
70
|
+
const ui = () => ctx.service<UI>("ui@1");
|
|
71
|
+
if (problems.length > 0 && !warned) {
|
|
72
|
+
warned = true;
|
|
73
|
+
ui()?.notify(`jade/hooks: ${problems.join("; ")}`, "warning");
|
|
74
|
+
}
|
|
75
|
+
if (hooks.length === 0) return;
|
|
76
|
+
const shell = ctx.require<Shell>("shell@1");
|
|
77
|
+
const sessionId = ctx.session.id;
|
|
78
|
+
|
|
79
|
+
/** 运行一条 hook;call 给了就把工具名和参数经环境变量交给它 */
|
|
80
|
+
const run = async (hook: UserHook, call: { name: string; input: unknown } | undefined, signal?: AbortSignal) => {
|
|
81
|
+
let dir: string | undefined;
|
|
82
|
+
try {
|
|
83
|
+
let exports = "";
|
|
84
|
+
if (call) {
|
|
85
|
+
dir = await mkdtemp(path.join(os.tmpdir(), "jade-hook-"));
|
|
86
|
+
const file = path.join(dir, "input.json");
|
|
87
|
+
await writeFile(file, JSON.stringify(call.input ?? null));
|
|
88
|
+
const p = (call.input as { path?: unknown } | null)?.path;
|
|
89
|
+
// 路径用正斜杠:Git Bash 里反斜杠是转义符
|
|
90
|
+
const vars = { JADE_TOOL: call.name, JADE_TOOL_INPUT: file.split(path.sep).join("/"), ...(typeof p === "string" ? { JADE_TOOL_PATH: p.split(path.sep).join("/") } : {}) };
|
|
91
|
+
exports = `export ${Object.entries(vars).map(([k, v]) => `${k}=${quote(v)}`).join(" ")}; `;
|
|
92
|
+
}
|
|
93
|
+
const r = await shell.run(exports + hook.command, { cwd: ctx.cwd, timeoutMs: hook.timeoutMs, ...(signal ? { signal } : {}) });
|
|
94
|
+
return { ok: r.code === 0, output: r.timedOut ? `timed out after ${hook.timeoutMs / 1000}s` : clip(r.output.trim()) };
|
|
95
|
+
} finally {
|
|
96
|
+
if (dir) await rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const matching = (on: UserHook["on"], tool?: string) => hooks.filter((h) => h.on === on && (h.tool === undefined || h.tool === tool));
|
|
100
|
+
|
|
101
|
+
if (hooks.some((h) => h.on === "before")) {
|
|
102
|
+
ctx.kernel.hook({
|
|
103
|
+
kind: "gate",
|
|
104
|
+
point: "tool.gate",
|
|
105
|
+
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
106
|
+
fn: async (call, hook) => {
|
|
107
|
+
for (const h of matching("before", call.name)) {
|
|
108
|
+
const r = await run(h, call, hook.signal);
|
|
109
|
+
if (!r.ok) return { decision: "deny", reason: `A hook the user configured (${h.command}) refused this ${call.name} call${r.output ? `:\n${r.output}` : "."}` };
|
|
110
|
+
}
|
|
111
|
+
return { decision: "abstain" };
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (hooks.some((h) => h.on === "after")) {
|
|
116
|
+
ctx.kernel.hook({
|
|
117
|
+
kind: "transform",
|
|
118
|
+
point: "tool.after",
|
|
119
|
+
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
120
|
+
// 出错时只是这条 hook 没跑,不影响工具的结果
|
|
121
|
+
optional: true,
|
|
122
|
+
fn: async (out) => {
|
|
123
|
+
if (out.isError) return undefined;
|
|
124
|
+
for (const h of matching("after", out.call.name)) {
|
|
125
|
+
const r = await run(h, out.call);
|
|
126
|
+
if (r.ok) continue;
|
|
127
|
+
const text = `The user's hook after ${out.call.name} (${h.command}) failed${r.output ? `:\n${r.output}` : "."}`;
|
|
128
|
+
ui()?.notify(text, "warning");
|
|
129
|
+
ctx.service<Prompts>("prompts@1")?.note(sessionId, text);
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (hooks.some((h) => h.on === "turn")) {
|
|
136
|
+
ctx.kernel.hook({
|
|
137
|
+
kind: "observe",
|
|
138
|
+
point: "event",
|
|
139
|
+
fn: (e) => {
|
|
140
|
+
if (e.type !== "turn.ended") return;
|
|
141
|
+
// 不等它:一轮结束不该被用户的命令拖住
|
|
142
|
+
for (const h of matching("turn")) {
|
|
143
|
+
void run(h, undefined, ctx.signal).then(
|
|
144
|
+
(r) => void (r.ok || ui()?.notify(`jade/hooks: ${h.command} failed${r.output ? `: ${r.output}` : ""}`, "warning")),
|
|
145
|
+
() => {},
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"requires": ["shell@1"],
|
|
7
|
+
"optional": ["ui@1", "prompts@1"],
|
|
8
|
+
"config": {
|
|
9
|
+
"hooks": {
|
|
10
|
+
"default": [],
|
|
11
|
+
"description": "shell commands to run around tool calls: a list of { \"on\": \"before\" | \"after\" | \"turn\", \"tool\": \"<name>\" (optional), \"command\": \"...\", \"timeoutSeconds\": 60 }. A failing \"before\" command denies the call; a failing \"after\" command is reported to the agent; \"turn\" runs when a turn ends"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
package/plugins/keys/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// jade/keys:快捷键的注册表。前端在输入框有焦点时按下自己不用的键,查这里找到插件的处理函数。
|
|
2
2
|
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
+
import type { Key, Keys } from "@uncarved/jade-api/services";
|
|
4
5
|
|
|
5
6
|
/** "ctrl+<字母>"、"alt+<字母或数字>" 或 "shift+tab" */
|
|
6
7
|
export const KEY_SPEC = /^(ctrl\+[a-z]|alt\+[a-z0-9]|shift\+tab)$/;
|
|
@@ -10,23 +11,6 @@ export const KEY_SPEC = /^(ctrl\+[a-z]|alt\+[a-z0-9]|shift\+tab)$/;
|
|
|
10
11
|
*/
|
|
11
12
|
export const RESERVED_KEYS: ReadonlySet<string> = new Set([..."acdehijklmoruw"].map((c) => `ctrl+${c}`).concat("alt+f", "alt+b"));
|
|
12
13
|
|
|
13
|
-
export interface Key {
|
|
14
|
-
/** 见 KEY_SPEC,不分大小写 */
|
|
15
|
-
key: string;
|
|
16
|
-
/** /help 里显示 */
|
|
17
|
-
description?: string;
|
|
18
|
-
/** 按下时调用;出错时前端显示警告。上一次还没执行完时再按,会再调用一次 */
|
|
19
|
-
run(ctx: { key: string; signal: AbortSignal }): void | Promise<void>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface Keys {
|
|
23
|
-
/** 注册一个键,返回撤销函数。键不合法、是保留键或已被占用时抛错 */
|
|
24
|
-
register(key: Key): () => void;
|
|
25
|
-
/** 前端按下一个自己不用的键时查找;找到了由前端执行 run,出错时显示警告 */
|
|
26
|
-
get(key: string): { key: Key; plugin: string } | undefined;
|
|
27
|
-
list(): { key: string; description?: string; plugin: string }[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
14
|
const registry = new Map<string, { key: Key; plugin: string }>();
|
|
31
15
|
|
|
32
16
|
export default definePlugin({
|