@uncarved/jade 0.3.0 → 0.4.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 +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 +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +49 -24
- package/plugins/approval/policy.ts +48 -25
- 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 +55 -12
- 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 +10 -9
- package/plugins/models/index.ts +31 -4
- package/plugins/models/provider.ts +12 -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 +39 -9
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +231 -0
- package/plugins/screenshot/index.ts +3 -2
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +75 -22
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +93 -34
- 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 +81 -81
- package/plugins/task/plugin.json +5 -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 +57 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/frontend.ts +161 -87
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mascot.ts +8 -7
- 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 +356 -104
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -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 -414
- package/plugins-dist/approval/policy.js +395 -497
- 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 -244
- 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 +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -188
- package/plugins-dist/models/provider.js +243 -295
- package/plugins-dist/models/saved.js +59 -71
- 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 +81 -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 +134 -151
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +169 -0
- package/plugins-dist/screenshot/index.js +37 -45
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +101 -151
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -74
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +317 -320
- 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 -148
- package/plugins-dist/task/plugin.json +5 -4
- 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 +43 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +641 -655
- 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 -353
- 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 -1645
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -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 +489 -34
|
@@ -1,132 +1,120 @@
|
|
|
1
|
-
// 文件工具在对话和审批里怎么显示(登记到 renderers@1)。从旧 jade 的 ui/renderers.ts 移植,去掉了检查点。
|
|
2
|
-
//
|
|
3
|
-
// 审批插件的 tool.before 把 path 换成了绝对路径,审批预览、before、result 拿到的都是绝对路径;
|
|
4
|
-
// 显示时用 display 换回工作目录内的相对路径。没有登记 renderer 的部分用前端的默认显示。
|
|
5
1
|
import { readFileSync } from "node:fs";
|
|
6
2
|
import { readFile } from "node:fs/promises";
|
|
7
3
|
import path from "node:path";
|
|
8
4
|
import { diffLines, diffStats, renderDiff } from "./diff.js";
|
|
9
5
|
import { applyEditsToText, display } from "./fs.js";
|
|
10
|
-
import { clip, dim,
|
|
11
|
-
const args = (input) =>
|
|
12
|
-
/** 显示用的路径;工作目录本身显示成 "."(display 对它给的是绝对路径,审批插件会把 "." 换成工作目录) */
|
|
6
|
+
import { added, clip, dim, lines as joinLines, removed, warn } from "./style.js";
|
|
7
|
+
const args = (input) => input !== null && typeof input === "object" ? input : {};
|
|
13
8
|
function pathOf(cwd, i) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
if (typeof i.path !== "string")
|
|
10
|
+
return "";
|
|
11
|
+
const full = path.resolve(cwd, i.path);
|
|
12
|
+
return path.relative(cwd, full) === "" ? "." : display(cwd, full);
|
|
18
13
|
}
|
|
19
|
-
/** 预览里的多行文字:每行前面加上标记,最多 max 行 */
|
|
20
14
|
function marked(s, mark, color, max = 30) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
const all = s.split(`
|
|
16
|
+
`);
|
|
17
|
+
const shown = all.slice(0, max).map((l) => color(`${mark} ${l}`));
|
|
18
|
+
if (all.length > max)
|
|
19
|
+
shown.push(dim(` … ${all.length - max} more lines`));
|
|
20
|
+
return joinLines(shown);
|
|
26
21
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return shown.join("\n");
|
|
22
|
+
function capped(lines, max) {
|
|
23
|
+
const shown = lines.slice(0, max);
|
|
24
|
+
if (lines.length > max)
|
|
25
|
+
shown.push(dim(`… ${lines.length - max} more lines`));
|
|
26
|
+
return joinLines(shown);
|
|
33
27
|
}
|
|
34
|
-
const editsOf = (tool, i) =>
|
|
35
|
-
/**
|
|
36
|
-
* 审批时的预览:带行号和上下文的 diff(读出文件、按工具的规则算出修改后的内容)。
|
|
37
|
-
* 文件读不了、替换不成立时退回到只显示参数里的新旧文本
|
|
38
|
-
*/
|
|
28
|
+
const editsOf = (tool, i) => tool === "edit_file" ? [i] : Array.isArray(i.edits) ? i.edits : [];
|
|
39
29
|
async function filePreview(cwd, tool, input) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
30
|
+
const i = args(input);
|
|
31
|
+
const file = typeof i.path === "string" ? path.resolve(cwd, i.path) : undefined;
|
|
32
|
+
const current = file ? await readFile(file, "utf8").catch(() => {
|
|
33
|
+
return;
|
|
34
|
+
}) : undefined;
|
|
35
|
+
if (tool === "write_file") {
|
|
36
|
+
const content = String(i.content ?? "");
|
|
37
|
+
if (current !== undefined)
|
|
38
|
+
return [dim(" overwrites the existing file:"), `
|
|
39
|
+
`, renderDiff(current, content)];
|
|
40
|
+
return marked(content, "+", added, 20);
|
|
41
|
+
}
|
|
42
|
+
if (tool === "multi_edit" && !Array.isArray(i.edits))
|
|
43
|
+
return dim(clip(JSON.stringify(input, null, 2), 2000));
|
|
44
|
+
const edits = editsOf(tool, i);
|
|
45
|
+
const raw = (e) => [marked(String(e.old_string ?? ""), "-", removed), `
|
|
46
|
+
`, marked(String(e.new_string ?? ""), "+", added)];
|
|
47
|
+
const fallback = () => edits.map((e) => raw(e)).flatMap((t, k) => k ? [dim(`
|
|
48
|
+
⋯
|
|
49
|
+
`), t] : [t]);
|
|
50
|
+
if (current === undefined)
|
|
51
|
+
return fallback();
|
|
52
|
+
const r = applyEditsToText(current, edits, display(cwd, file));
|
|
53
|
+
if (typeof r === "string")
|
|
54
|
+
return [fallback(), `
|
|
55
|
+
`, warn(` (this edit will fail: ${clip(r, 200)})`)];
|
|
56
|
+
return renderDiff(current, r.text);
|
|
60
57
|
}
|
|
61
|
-
/** 对话里折叠的 diff 最多显示这么多行 */
|
|
62
58
|
const MAX_DIFF_LINES = 400;
|
|
63
|
-
/** 结果文字(例如编辑后检查的输出)折叠起来最多这么多行 */
|
|
64
59
|
const MAX_RESULT_LINES = 2000;
|
|
65
|
-
/**
|
|
66
|
-
* 执行前后的差异。在工具执行之前调用(读的是修改前的文件),同步读文件:
|
|
67
|
-
* 执行前的通知是同步的,等异步读完文件可能已经改了。替换不成立时返回 undefined
|
|
68
|
-
*/
|
|
69
60
|
function fileDiff(cwd, tool, input) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const { added, removed } = diffStats(lines);
|
|
93
|
-
return { added, removed, text: renderDiff(before ?? "", after, MAX_DIFF_LINES, lines) };
|
|
61
|
+
const i = args(input);
|
|
62
|
+
if (typeof i.path !== "string")
|
|
63
|
+
return;
|
|
64
|
+
const file = path.resolve(cwd, i.path);
|
|
65
|
+
let before;
|
|
66
|
+
try {
|
|
67
|
+
before = readFileSync(file, "utf8");
|
|
68
|
+
} catch {}
|
|
69
|
+
let after;
|
|
70
|
+
if (tool === "write_file") {
|
|
71
|
+
after = String(i.content ?? "");
|
|
72
|
+
} else {
|
|
73
|
+
if (before === undefined)
|
|
74
|
+
return;
|
|
75
|
+
const r = applyEditsToText(before, editsOf(tool, i), display(cwd, file));
|
|
76
|
+
if (typeof r === "string")
|
|
77
|
+
return;
|
|
78
|
+
after = r.text;
|
|
79
|
+
}
|
|
80
|
+
const lines = diffLines(before ?? "", after);
|
|
81
|
+
const { added, removed } = diffStats(lines);
|
|
82
|
+
return { added, removed, text: renderDiff(before ?? "", after, MAX_DIFF_LINES, lines) };
|
|
94
83
|
}
|
|
95
|
-
/** 修改文件的工具:预览是 diff;结果行后面是增删的行数,展开看完整的 diff */
|
|
96
84
|
function fileRenderer(cwd, tool, summary) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
85
|
+
return {
|
|
86
|
+
tool,
|
|
87
|
+
summary,
|
|
88
|
+
preview: (input) => filePreview(cwd, tool, input),
|
|
89
|
+
before: (input) => fileDiff(cwd, tool, input),
|
|
90
|
+
result: ({ text, isError, before }) => {
|
|
91
|
+
const diff = before;
|
|
92
|
+
if (!diff || isError)
|
|
93
|
+
return;
|
|
94
|
+
const [first = "", ...rest] = text.split(`
|
|
95
|
+
`);
|
|
96
|
+
const extra = rest.length ? [capped(rest, MAX_RESULT_LINES)] : [];
|
|
97
|
+
return { line: [dim(`${clip(first, 150)} `), added(`+${diff.added}`), " ", removed(`-${diff.removed}`)], details: joinLines([...extra, diff.text]) };
|
|
98
|
+
}
|
|
99
|
+
};
|
|
111
100
|
}
|
|
112
101
|
export function fsRenderers(cwd) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
];
|
|
102
|
+
const where = (input) => pathOf(cwd, args(input));
|
|
103
|
+
const search = (input) => {
|
|
104
|
+
const i = args(input);
|
|
105
|
+
const at = pathOf(cwd, i);
|
|
106
|
+
return `${i.pattern}${at && at !== "." ? ` in ${at}` : ""}`;
|
|
107
|
+
};
|
|
108
|
+
return [
|
|
109
|
+
{ tool: "read_file", summary: where },
|
|
110
|
+
{ tool: "list_dir", summary: where },
|
|
111
|
+
fileRenderer(cwd, "edit_file", where),
|
|
112
|
+
fileRenderer(cwd, "multi_edit", (input) => {
|
|
113
|
+
const i = args(input);
|
|
114
|
+
return `${pathOf(cwd, i)}, ${Array.isArray(i.edits) ? i.edits.length : 0} edits`;
|
|
115
|
+
}),
|
|
116
|
+
fileRenderer(cwd, "write_file", where),
|
|
117
|
+
{ tool: "grep", summary: search },
|
|
118
|
+
{ tool: "glob", summary: search }
|
|
119
|
+
];
|
|
132
120
|
}
|
package/plugins-dist/fs/style.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export const
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
export const red = paint("31", "39");
|
|
13
|
-
export const green = paint("32", "39");
|
|
14
|
-
export const yellow = paint("33", "39");
|
|
15
|
-
export const bold = paint("1", "22");
|
|
1
|
+
export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
|
|
2
|
+
const styled = (style) => (text) => ({ text, style });
|
|
3
|
+
export const dim = styled("dim");
|
|
4
|
+
export const bold = styled("bold");
|
|
5
|
+
export const ok = styled("ok");
|
|
6
|
+
export const warn = styled("warn");
|
|
7
|
+
export const error = styled("error");
|
|
8
|
+
export const added = styled("added");
|
|
9
|
+
export const removed = styled("removed");
|
|
10
|
+
export const lines = (rows) => rows.flatMap((r, i) => i ? [`
|
|
11
|
+
`, r] : [r]);
|
package/plugins-dist/fs/sys.js
CHANGED
|
@@ -1,54 +1,42 @@
|
|
|
1
|
-
// 跨运行时的进程和 glob:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
1
|
import { spawn as nodeSpawn } from "node:child_process";
|
|
3
2
|
import picomatch from "picomatch";
|
|
4
|
-
const EMPTY = { async
|
|
5
|
-
/**
|
|
6
|
-
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
7
|
-
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
8
|
-
*/
|
|
3
|
+
const EMPTY = { async* [Symbol.asyncIterator]() {} };
|
|
9
4
|
export function spawn(cmd, opts = {}) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
kill: () => void child.kill(),
|
|
31
|
-
});
|
|
32
|
-
});
|
|
5
|
+
const child = nodeSpawn(cmd[0], cmd.slice(1), {
|
|
6
|
+
cwd: opts.cwd,
|
|
7
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
8
|
+
detached: opts.detached ?? false,
|
|
9
|
+
env: opts.env,
|
|
10
|
+
windowsHide: true
|
|
11
|
+
});
|
|
12
|
+
const exited = new Promise((resolve) => child.once("exit", (code) => resolve(code)));
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
child.once("error", reject);
|
|
15
|
+
child.once("spawn", () => {
|
|
16
|
+
child.off("error", reject);
|
|
17
|
+
child.on("error", () => {});
|
|
18
|
+
resolve({
|
|
19
|
+
pid: child.pid,
|
|
20
|
+
stdout: child.stdout ?? EMPTY,
|
|
21
|
+
stderr: child.stderr ?? EMPTY,
|
|
22
|
+
exited,
|
|
23
|
+
kill: () => void child.kill()
|
|
24
|
+
});
|
|
33
25
|
});
|
|
26
|
+
});
|
|
34
27
|
}
|
|
35
|
-
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
|
|
36
28
|
export async function run(cmd, opts = {}) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
30
|
+
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
31
|
+
return { code, stdout };
|
|
40
32
|
}
|
|
41
33
|
export async function text(stream) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
const decoder = new TextDecoder;
|
|
35
|
+
let out = "";
|
|
36
|
+
for await (const chunk of stream)
|
|
37
|
+
out += decoder.decode(chunk, { stream: true });
|
|
38
|
+
return out + decoder.decode();
|
|
47
39
|
}
|
|
48
|
-
/**
|
|
49
|
-
* 编译 glob,返回匹配函数。路径用正斜杠、相对于搜索根目录。
|
|
50
|
-
* 和原先的 Bun.Glob#match 一致:* 也匹配点开头的文件,反斜杠是转义符而不是分隔符
|
|
51
|
-
*/
|
|
52
40
|
export function globMatcher(pattern) {
|
|
53
|
-
|
|
41
|
+
return picomatch(pattern, { dot: true, windows: false });
|
|
54
42
|
}
|
|
@@ -1,18 +1,3 @@
|
|
|
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
1
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
17
2
|
import os from "node:os";
|
|
18
3
|
import path from "node:path";
|
|
@@ -20,126 +5,121 @@ import { definePlugin } from "@uncarved/jade-api";
|
|
|
20
5
|
const DEFAULT_TIMEOUT_SECONDS = 60;
|
|
21
6
|
const MAX_TIMEOUT_SECONDS = 600;
|
|
22
7
|
const MAX_OUTPUT_CHARS = 4000;
|
|
23
|
-
/** 检查配置里的 hooks;写错的条目说明原因,其余的照用 */
|
|
24
8
|
export function parseHooks(value) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
}
|
|
9
|
+
if (value === undefined)
|
|
10
|
+
return { hooks: [], problems: [] };
|
|
11
|
+
if (!Array.isArray(value))
|
|
12
|
+
return { hooks: [], problems: ["hooks must be a list"] };
|
|
13
|
+
const hooks = [];
|
|
14
|
+
const problems = [];
|
|
15
|
+
for (const [i, raw] of value.entries()) {
|
|
16
|
+
const h = raw ?? {};
|
|
17
|
+
const where = `hooks[${i}]`;
|
|
18
|
+
if (h.on !== "before" && h.on !== "after" && h.on !== "turn")
|
|
19
|
+
problems.push(`${where}: "on" must be "before", "after" or "turn"`);
|
|
20
|
+
else if (typeof h.command !== "string" || !h.command.trim())
|
|
21
|
+
problems.push(`${where}: "command" must be a shell command`);
|
|
22
|
+
else if (h.tool !== undefined && typeof h.tool !== "string")
|
|
23
|
+
problems.push(`${where}: "tool" must be a tool name`);
|
|
24
|
+
else if (h.timeoutSeconds !== undefined && !(typeof h.timeoutSeconds === "number" && h.timeoutSeconds > 0 && h.timeoutSeconds <= MAX_TIMEOUT_SECONDS)) {
|
|
25
|
+
problems.push(`${where}: "timeoutSeconds" must be a number up to ${MAX_TIMEOUT_SECONDS}`);
|
|
26
|
+
} else {
|
|
27
|
+
hooks.push({
|
|
28
|
+
on: h.on,
|
|
29
|
+
command: h.command,
|
|
30
|
+
timeoutMs: (h.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS) * 1000,
|
|
31
|
+
...typeof h.tool === "string" && h.tool !== "*" ? { tool: h.tool } : {}
|
|
32
|
+
});
|
|
51
33
|
}
|
|
52
|
-
|
|
34
|
+
}
|
|
35
|
+
return { hooks, problems };
|
|
53
36
|
}
|
|
54
|
-
/** 单引号括起来,给 POSIX shell 用 */
|
|
55
37
|
const quote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
56
|
-
const clip = (s) =>
|
|
38
|
+
const clip = (s) => s.length > MAX_OUTPUT_CHARS ? `${s.slice(0, MAX_OUTPUT_CHARS)}
|
|
39
|
+
… (${s.length - MAX_OUTPUT_CHARS} more characters)` : s;
|
|
57
40
|
let warned = false;
|
|
58
41
|
export default definePlugin({
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
42
|
+
session(ctx) {
|
|
43
|
+
const { hooks, problems } = parseHooks(ctx.config.hooks);
|
|
44
|
+
const ui = () => ctx.service("ui@1");
|
|
45
|
+
if (problems.length > 0 && !warned) {
|
|
46
|
+
warned = true;
|
|
47
|
+
ui()?.notify(`jade/hooks: ${problems.join("; ")}`, "warning");
|
|
48
|
+
}
|
|
49
|
+
if (hooks.length === 0)
|
|
50
|
+
return;
|
|
51
|
+
const shell = ctx.require("shell@1");
|
|
52
|
+
const sessionId = ctx.session.id;
|
|
53
|
+
const run = async (hook, call, signal) => {
|
|
54
|
+
let dir;
|
|
55
|
+
try {
|
|
56
|
+
let exports = "";
|
|
57
|
+
if (call) {
|
|
58
|
+
dir = await mkdtemp(path.join(os.tmpdir(), "jade-hook-"));
|
|
59
|
+
const file = path.join(dir, "input.json");
|
|
60
|
+
await writeFile(file, JSON.stringify(call.input ?? null));
|
|
61
|
+
const p = call.input?.path;
|
|
62
|
+
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("/") } : {} };
|
|
63
|
+
exports = `export ${Object.entries(vars).map(([k, v]) => `${k}=${quote(v)}`).join(" ")}; `;
|
|
65
64
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
finally {
|
|
88
|
-
if (dir)
|
|
89
|
-
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
const matching = (on, tool) => hooks.filter((h) => h.on === on && (h.tool === undefined || h.tool === tool));
|
|
93
|
-
if (hooks.some((h) => h.on === "before")) {
|
|
94
|
-
ctx.kernel.hook({
|
|
95
|
-
kind: "gate",
|
|
96
|
-
point: "tool.gate",
|
|
97
|
-
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
98
|
-
fn: async (call, hook) => {
|
|
99
|
-
for (const h of matching("before", call.name)) {
|
|
100
|
-
const r = await run(h, call, hook.signal);
|
|
101
|
-
if (!r.ok)
|
|
102
|
-
return { decision: "deny", reason: `A hook the user configured (${h.command}) refused this ${call.name} call${r.output ? `:\n${r.output}` : "."}` };
|
|
103
|
-
}
|
|
104
|
-
return { decision: "abstain" };
|
|
105
|
-
},
|
|
106
|
-
});
|
|
65
|
+
const r = await shell.run(exports + hook.command, { cwd: ctx.cwd, timeoutMs: hook.timeoutMs, ...signal ? { signal } : {} });
|
|
66
|
+
return { ok: r.code === 0, output: r.timedOut ? `timed out after ${hook.timeoutMs / 1000}s` : clip(r.output.trim()) };
|
|
67
|
+
} finally {
|
|
68
|
+
if (dir)
|
|
69
|
+
await rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const matching = (on, tool) => hooks.filter((h) => h.on === on && (h.tool === undefined || h.tool === tool));
|
|
73
|
+
if (hooks.some((h) => h.on === "before")) {
|
|
74
|
+
ctx.kernel.hook({
|
|
75
|
+
kind: "gate",
|
|
76
|
+
point: "tool.gate",
|
|
77
|
+
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
78
|
+
fn: async (call, hook) => {
|
|
79
|
+
for (const h of matching("before", call.name)) {
|
|
80
|
+
const r = await run(h, call, hook.signal);
|
|
81
|
+
if (!r.ok)
|
|
82
|
+
return { decision: "deny", reason: `A hook the user configured (${h.command}) refused this ${call.name} call${r.output ? `:
|
|
83
|
+
${r.output}` : "."}` };
|
|
84
|
+
}
|
|
85
|
+
return { decision: "abstain" };
|
|
107
86
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (hooks.some((h) => h.on === "after")) {
|
|
90
|
+
ctx.kernel.hook({
|
|
91
|
+
kind: "transform",
|
|
92
|
+
point: "tool.after",
|
|
93
|
+
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
94
|
+
optional: true,
|
|
95
|
+
fn: async (out) => {
|
|
96
|
+
if (out.isError)
|
|
97
|
+
return;
|
|
98
|
+
for (const h of matching("after", out.call.name)) {
|
|
99
|
+
const r = await run(h, out.call);
|
|
100
|
+
if (r.ok)
|
|
101
|
+
continue;
|
|
102
|
+
const text = `The user's hook after ${out.call.name} (${h.command}) failed${r.output ? `:
|
|
103
|
+
${r.output}` : "."}`;
|
|
104
|
+
ui()?.notify(text, "warning");
|
|
105
|
+
ctx.service("prompts@1")?.note(sessionId, text);
|
|
106
|
+
}
|
|
107
|
+
return;
|
|
129
108
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (hooks.some((h) => h.on === "turn") && !ctx.session.parent) {
|
|
112
|
+
ctx.kernel.hook({
|
|
113
|
+
kind: "observe",
|
|
114
|
+
point: "event",
|
|
115
|
+
fn: (e) => {
|
|
116
|
+
if (e.type !== "turn.ended")
|
|
117
|
+
return;
|
|
118
|
+
for (const h of matching("turn")) {
|
|
119
|
+
run(h, undefined, ctx.signal).then((r) => void (r.ok || ui()?.notify(`jade/hooks: ${h.command} failed${r.output ? `: ${r.output}` : ""}`, "warning")), () => {});
|
|
120
|
+
}
|
|
143
121
|
}
|
|
144
|
-
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
145
125
|
});
|