@uncarved/jade 0.2.1 → 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 +64 -2
- package/assets/logo.svg +1 -0
- package/bin/jade.js +4 -0
- package/dist/api.d.ts +5 -250
- package/dist/api.js +5 -7
- package/dist/api.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.js +19 -17
- package/dist/fetch.js.map +1 -1
- package/dist/host.d.ts +12 -1
- package/dist/host.js +49 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +26 -4
- package/dist/main.js.map +1 -1
- package/dist/remote.js +4 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +2 -0
- package/dist/session.js +133 -13
- package/dist/session.js.map +1 -1
- package/dist/source.d.ts +2 -10
- package/dist/source.js.map +1 -1
- package/dist/update.d.ts +8 -20
- package/dist/update.js +12 -1
- package/dist/update.js.map +1 -1
- package/package.json +4 -2
- package/plugins/approval/index.ts +233 -85
- package/plugins/approval/plugin.json +1 -1
- package/plugins/approval/policy.ts +2 -1
- package/plugins/approval/review.ts +1 -1
- package/plugins/ask/index.ts +2 -2
- package/plugins/budget/index.ts +1 -1
- package/plugins/checks/checks.ts +3 -2
- package/plugins/checks/index.ts +2 -5
- package/plugins/commands/index.ts +2 -44
- package/plugins/compact/compact.ts +8 -3
- package/plugins/compact/index.ts +13 -16
- package/plugins/continue/index.ts +1 -1
- package/plugins/cost/index.ts +143 -0
- package/plugins/cost/plugin.json +13 -0
- package/plugins/custom-commands/index.ts +85 -0
- package/plugins/custom-commands/plugin.json +8 -0
- package/plugins/editor/index.ts +88 -0
- package/plugins/editor/plugin.json +17 -0
- package/plugins/fs/fs.ts +33 -6
- package/plugins/fs/grep.ts +6 -4
- package/plugins/fs/index.ts +2 -3
- 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 +2 -2
- package/plugins/models/configure.ts +258 -150
- package/plugins/models/context.ts +2 -2
- package/plugins/models/index.ts +35 -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 +36 -6
- package/plugins/print/plugin.json +1 -0
- package/plugins/prompts/index.ts +22 -14
- package/plugins/{memory → prompts}/notes.ts +21 -9
- package/plugins/prompts/plugin.json +8 -2
- package/plugins/prompts/prompts.ts +5 -4
- package/plugins/renderers/index.ts +2 -43
- package/plugins/repeats/index.ts +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 +7 -16
- package/plugins/task/plugin.json +9 -2
- package/plugins/task/subagent.ts +23 -5
- package/plugins/todo/index.ts +2 -2
- package/plugins/tui/editor.ts +10 -2
- package/plugins/tui/frontend.ts +103 -21
- package/plugins/tui/index.ts +3 -8
- package/plugins/tui/markdown.ts +51 -6
- package/plugins/tui/mascot.ts +505 -0
- package/plugins/tui/plugin.json +2 -2
- package/plugins/tui/screen.ts +6 -0
- package/plugins/tui/transcript.ts +17 -2
- package/plugins/tui/tui.ts +321 -90
- package/plugins/undo/checkpoint.ts +2 -2
- package/plugins/undo/index.ts +84 -26
- package/plugins/undo/plugin.json +8 -2
- package/plugins/undo/snapshot.ts +129 -0
- package/plugins/update/index.ts +11 -15
- package/plugins/update/plugin.json +2 -1
- package/plugins/web/index.ts +2 -5
- package/plugins/web/renderers.ts +1 -1
- package/plugins-dist/approval/index.js +217 -51
- package/plugins-dist/approval/plugin.json +1 -1
- package/plugins-dist/ask/index.js +1 -1
- package/plugins-dist/budget/index.js +1 -1
- package/plugins-dist/checks/checks.js +2 -1
- package/plugins-dist/checks/index.js +1 -1
- package/plugins-dist/commands/index.js +1 -1
- package/plugins-dist/compact/compact.js +6 -1
- package/plugins-dist/compact/index.js +12 -8
- package/plugins-dist/continue/index.js +1 -1
- package/plugins-dist/cost/index.js +135 -0
- package/plugins-dist/cost/plugin.json +16 -0
- package/plugins-dist/custom-commands/index.js +86 -0
- package/plugins-dist/custom-commands/plugin.json +12 -0
- package/plugins-dist/editor/index.js +89 -0
- package/plugins-dist/editor/plugin.json +21 -0
- package/plugins-dist/fs/fs.js +34 -6
- package/plugins-dist/fs/grep.js +5 -3
- package/plugins-dist/fs/index.js +1 -1
- package/plugins-dist/hooks/index.js +145 -0
- package/plugins-dist/hooks/plugin.json +19 -0
- package/plugins-dist/keys/index.js +1 -1
- package/plugins-dist/mcp/index.js +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 +1 -1
- package/plugins-dist/models/configure.js +252 -151
- package/plugins-dist/models/context.js +2 -2
- package/plugins-dist/models/index.js +37 -14
- 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 +37 -3
- package/plugins-dist/print/plugin.json +3 -0
- package/plugins-dist/prompts/index.js +22 -3
- package/plugins-dist/{memory → prompts}/notes.js +10 -7
- package/plugins-dist/prompts/plugin.json +8 -2
- package/plugins-dist/prompts/prompts.js +5 -4
- package/plugins-dist/renderers/index.js +1 -1
- package/plugins-dist/repeats/index.js +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 +6 -4
- package/plugins-dist/task/plugin.json +10 -2
- package/plugins-dist/task/subagent.js +20 -5
- package/plugins-dist/todo/index.js +1 -1
- package/plugins-dist/tui/editor.js +7 -2
- package/plugins-dist/tui/frontend.js +107 -13
- 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/plugin.json +3 -2
- package/plugins-dist/tui/screen.js +2 -0
- package/plugins-dist/tui/transcript.js +16 -1
- package/plugins-dist/tui/tui.js +302 -81
- package/plugins-dist/undo/checkpoint.js +1 -1
- package/plugins-dist/undo/index.js +91 -24
- package/plugins-dist/undo/plugin.json +10 -3
- package/plugins-dist/undo/snapshot.js +111 -0
- package/plugins-dist/update/index.js +9 -10
- package/plugins-dist/update/plugin.json +4 -1
- package/plugins-dist/web/index.js +1 -1
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +8 -0
- package/src/api.ts +7 -262
- package/src/fetch.ts +18 -16
- package/src/host.ts +49 -1
- package/src/index.ts +1 -1
- package/src/main.ts +23 -5
- package/src/remote.ts +2 -0
- package/src/session.ts +134 -15
- package/src/source.ts +2 -11
- package/src/update.ts +16 -10
- package/plugins/undo/notes.ts +0 -43
- package/plugins-dist/undo/notes.js +0 -36
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/cost",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"optional": [
|
|
7
|
+
"commands@1",
|
|
8
|
+
"models@1"
|
|
9
|
+
],
|
|
10
|
+
"config": {
|
|
11
|
+
"prices": {
|
|
12
|
+
"default": {},
|
|
13
|
+
"description": "prices in $ per million tokens, by model name or name prefix: { \"<model>\": { \"input\": 3, \"output\": 15, \"cacheRead\": 0.3, \"cacheWrite\": 3.75 } }; models without a price are counted in tokens only"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// jade/custom-commands:把 Markdown 文件变成斜杠命令。常用的提示词写一次,之后 /名字 [参数] 就能发出去。
|
|
2
|
+
//
|
|
3
|
+
// 文件放在 $JADE_HOME/commands/<名字>.md(用户层)或 <工作目录>/.jade/commands/<名字>.md(项目层,随仓库分发)。
|
|
4
|
+
// 文件内容就是要发给模型的消息;$ARGUMENTS 换成命令后面的文字,没有这个占位符时参数接在最后。
|
|
5
|
+
// 开头可以有一段 frontmatter,description 显示在命令菜单里:
|
|
6
|
+
//
|
|
7
|
+
// ---
|
|
8
|
+
// description: review the staged changes
|
|
9
|
+
// ---
|
|
10
|
+
// Review the staged changes ($ARGUMENTS) ...
|
|
11
|
+
//
|
|
12
|
+
// 命令在启动时登记(新加的文件重启后生效),内容每次执行时重新读(改了马上生效)。
|
|
13
|
+
// 同名时用户层的优先;名字被别的插件的命令占用时不登记,告诉用户一次。
|
|
14
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
17
|
+
const MAX_DESCRIPTION = 80;
|
|
18
|
+
/** 拆出 frontmatter 里的 description 和正文 */
|
|
19
|
+
export function parseCommand(text) {
|
|
20
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(text);
|
|
21
|
+
if (!m)
|
|
22
|
+
return { body: text.trim() };
|
|
23
|
+
const description = /^description:\s*(.+?)\s*$/m.exec(m[1])?.[1]?.replace(/^(["'])(.*)\1$/, "$2");
|
|
24
|
+
return { ...(description ? { description } : {}), body: text.slice(m[0].length).trim() };
|
|
25
|
+
}
|
|
26
|
+
/** 把参数放进正文:有 $ARGUMENTS 就替换,没有就接在最后 */
|
|
27
|
+
export function fill(body, args) {
|
|
28
|
+
const given = args.trim();
|
|
29
|
+
if (body.includes("$ARGUMENTS"))
|
|
30
|
+
return body.replaceAll("$ARGUMENTS", given);
|
|
31
|
+
return given ? `${body}\n\n${given}` : body;
|
|
32
|
+
}
|
|
33
|
+
async function markdownFiles(dir) {
|
|
34
|
+
try {
|
|
35
|
+
return (await readdir(dir)).filter((n) => n.toLowerCase().endsWith(".md")).sort();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export default definePlugin({
|
|
42
|
+
async process(ctx) {
|
|
43
|
+
const commands = ctx.require("commands@1");
|
|
44
|
+
const skipped = [];
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
for (const [dir, layer] of [
|
|
47
|
+
[path.join(ctx.home, "commands"), "user"],
|
|
48
|
+
[path.join(ctx.cwd, ".jade", "commands"), "project"],
|
|
49
|
+
]) {
|
|
50
|
+
for (const file of await markdownFiles(dir)) {
|
|
51
|
+
const name = file.slice(0, -3);
|
|
52
|
+
// 用户层的已经登记了同名的:项目层的不盖过它
|
|
53
|
+
if (seen.has(name.toLowerCase()))
|
|
54
|
+
continue;
|
|
55
|
+
const full = path.join(dir, file);
|
|
56
|
+
let description;
|
|
57
|
+
try {
|
|
58
|
+
description = parseCommand(await readFile(full, "utf8")).description;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const about = (description ?? `send ${layer === "user" ? full : path.join(".jade", "commands", file)}`).replace(/\s+/g, " ");
|
|
64
|
+
try {
|
|
65
|
+
commands.register({
|
|
66
|
+
name,
|
|
67
|
+
args: "[arguments]",
|
|
68
|
+
description: `${about.length > MAX_DESCRIPTION ? `${about.slice(0, MAX_DESCRIPTION)}…` : about} (${layer} command)`,
|
|
69
|
+
async run(args) {
|
|
70
|
+
const { body } = parseCommand(await readFile(full, "utf8"));
|
|
71
|
+
if (!body)
|
|
72
|
+
return `${full} is empty.`;
|
|
73
|
+
return { prompt: fill(body, args) };
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
seen.add(name.toLowerCase());
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
skipped.push(`/${name} (${e instanceof Error ? e.message : String(e)})`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (skipped.length > 0)
|
|
84
|
+
ctx.service("ui@1")?.notify(`jade/custom-commands: not registered: ${skipped.join("; ")}`, "warning");
|
|
85
|
+
},
|
|
86
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// jade/editor:Ctrl+G 把输入框里的文字交给 $EDITOR 编辑,存盘退出后换回输入框。
|
|
2
|
+
//
|
|
3
|
+
// 终端编辑器(vim、nano)要独占终端:经 ui@1 的 suspend 让前端把终端让出来,编辑器退出后前端重新接管。
|
|
4
|
+
// 图形编辑器("code --wait")不需要终端,但走同一条路也没有坏处。前端不提供 suspend 或没有输入框时做不了,提示一句。
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
10
|
+
const DEFAULT_KEY = "ctrl+g";
|
|
11
|
+
/** 用哪个编辑器:配置的 editor,否则 $VISUAL、$EDITOR,都没有时 vi(Windows 上是 notepad) */
|
|
12
|
+
export function editorCommand(configured, env = process.env) {
|
|
13
|
+
if (typeof configured === "string" && configured.trim())
|
|
14
|
+
return configured.trim();
|
|
15
|
+
return env.VISUAL?.trim() || env.EDITOR?.trim() || (process.platform === "win32" ? "notepad" : "vi");
|
|
16
|
+
}
|
|
17
|
+
/** 经 shell 启动编辑器(命令里可以带参数,例如 "code --wait"),等它退出;返回退出码 */
|
|
18
|
+
function run(command, file) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const child = spawn(`${command} "${file}"`, { stdio: "inherit", shell: true });
|
|
21
|
+
child.on("error", reject);
|
|
22
|
+
child.on("exit", (code) => resolve(code));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** 正在显示的会话的输入框:会话级的 ctx 才有,按键是进程级注册的 */
|
|
26
|
+
let input;
|
|
27
|
+
/** 编辑器开着的时候再按一次不再开一个 */
|
|
28
|
+
let editing = false;
|
|
29
|
+
/** 按键没注册上的原因(被别的插件占了、配置写错了):第一个会话打开时告诉用户一次 */
|
|
30
|
+
let noKey;
|
|
31
|
+
export default definePlugin({
|
|
32
|
+
process(ctx) {
|
|
33
|
+
const key = typeof ctx.config.key === "string" && ctx.config.key ? ctx.config.key : DEFAULT_KEY;
|
|
34
|
+
const binding = {
|
|
35
|
+
key,
|
|
36
|
+
description: "edit the input in $EDITOR",
|
|
37
|
+
async run() {
|
|
38
|
+
const ui = ctx.service("ui@1");
|
|
39
|
+
const draft = input;
|
|
40
|
+
if (!ui?.suspend || !draft)
|
|
41
|
+
return ui?.notify("this front end cannot hand the terminal to an editor", "warning");
|
|
42
|
+
if (editing)
|
|
43
|
+
return;
|
|
44
|
+
editing = true;
|
|
45
|
+
const command = editorCommand(ctx.config.editor);
|
|
46
|
+
const dir = await mkdtemp(path.join(os.tmpdir(), "jade-edit-"));
|
|
47
|
+
// .md:编辑器按 Markdown 高亮,消息多半就是 Markdown
|
|
48
|
+
const file = path.join(dir, "message.md");
|
|
49
|
+
try {
|
|
50
|
+
await writeFile(file, draft.text());
|
|
51
|
+
const code = await ui.suspend(() => run(command, file));
|
|
52
|
+
if (code !== 0)
|
|
53
|
+
return ui.notify(`${command} exited with code ${code}; the input was left as it was`, "warning");
|
|
54
|
+
// 编辑器存盘时加的最后一个换行不算
|
|
55
|
+
draft.set((await readFile(file, "utf8")).replace(/\r?\n$/, ""));
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
ui.notify(`could not edit the input with ${command}: ${e instanceof Error ? e.message : String(e)}`, "error");
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
editing = false;
|
|
62
|
+
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
// 键被别的插件占了不算这个插件坏了:只是没有这个键,换一个就行
|
|
67
|
+
noKey = undefined;
|
|
68
|
+
try {
|
|
69
|
+
ctx.require("keys@1").register(binding);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
noKey = `${e instanceof Error ? e.message : String(e)}; set {"jade/editor": {"key": "ctrl+<letter>"}} in config.json to use another key`;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
async session(ctx) {
|
|
76
|
+
if (noKey) {
|
|
77
|
+
ctx.service("ui@1")?.notify(`jade/editor: ${noKey}`, "warning");
|
|
78
|
+
noKey = undefined;
|
|
79
|
+
}
|
|
80
|
+
const draft = ctx.input;
|
|
81
|
+
if (!draft)
|
|
82
|
+
return;
|
|
83
|
+
input = draft;
|
|
84
|
+
await ctx.effect(() => () => {
|
|
85
|
+
if (input === draft)
|
|
86
|
+
input = undefined;
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/editor",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"requires": [
|
|
7
|
+
"keys@1"
|
|
8
|
+
],
|
|
9
|
+
"optional": [
|
|
10
|
+
"ui@1"
|
|
11
|
+
],
|
|
12
|
+
"config": {
|
|
13
|
+
"key": {
|
|
14
|
+
"default": "ctrl+g",
|
|
15
|
+
"description": "the key that opens the editor: ctrl+<letter> or alt+<letter or digit>"
|
|
16
|
+
},
|
|
17
|
+
"editor": {
|
|
18
|
+
"description": "the editor command ctrl+g opens the input in, for example \"vim\" or \"code --wait\"; default: $VISUAL, then $EDITOR, then vi (notepad on Windows)"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
package/plugins-dist/fs/fs.js
CHANGED
|
@@ -146,8 +146,12 @@ export function fsTools(cwd, opts = {}) {
|
|
|
146
146
|
if (buf.subarray(0, 8000).includes(0)) {
|
|
147
147
|
return { content: `${display(cwd, full)} is a binary file (${buf.length} bytes) and cannot be shown as text`, isError: true };
|
|
148
148
|
}
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
// 不是 UTF-8 的文件(多半是 GBK)照样给模型看,但不算读过:改它会把原来的字节写坏
|
|
150
|
+
const strict = utf8(buf);
|
|
151
|
+
const text = strict ?? legacyText(buf);
|
|
152
|
+
const note = strict === undefined ? `\n(${notUtf8(display(cwd, full))})` : "";
|
|
153
|
+
if (strict !== undefined)
|
|
154
|
+
remember(full, info, opts.responses?.calls ?? -1, ctx?.toolUseId);
|
|
151
155
|
if (text.length === 0)
|
|
152
156
|
return { content: "(empty file)" };
|
|
153
157
|
const lines = text.split(/\r?\n/);
|
|
@@ -170,9 +174,9 @@ export function fsTools(cwd, opts = {}) {
|
|
|
170
174
|
const body = out.join("\n");
|
|
171
175
|
const end = start + out.length - 1;
|
|
172
176
|
if (out.length < slice.length) {
|
|
173
|
-
return { content: `${body}\n(output capped at about ${MAX_READ_CHARS} characters: showing lines ${start}-${end} of ${lines.length}; continue with offset ${end + 1})` };
|
|
177
|
+
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}` };
|
|
174
178
|
}
|
|
175
|
-
return { content: end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body };
|
|
179
|
+
return { content: (end < lines.length ? `${body}\n(showing lines ${start}-${end} of ${lines.length})` : body) + note };
|
|
176
180
|
},
|
|
177
181
|
};
|
|
178
182
|
const writeFileTool = {
|
|
@@ -196,7 +200,9 @@ export function fsTools(cwd, opts = {}) {
|
|
|
196
200
|
if (info?.isDirectory())
|
|
197
201
|
return { content: `${display(cwd, full)} is a directory`, isError: true };
|
|
198
202
|
// 同样内容重复写入(崩溃后重跑)不算覆盖别人的修改
|
|
199
|
-
const before = info ? await readFile(full
|
|
203
|
+
const before = info ? utf8(await readFile(full)) ?? undefined : null;
|
|
204
|
+
if (before === undefined)
|
|
205
|
+
return { content: notUtf8(display(cwd, full)), isError: true };
|
|
200
206
|
// read_file 不显示换行风格和 BOM:覆盖已有文件时沿用原来的,否则整个文件每一行都算改了
|
|
201
207
|
const content = before === null ? given : keepStyle(before, given);
|
|
202
208
|
const problem = info && before !== null && before !== content ? staleness(full, info) : undefined;
|
|
@@ -218,7 +224,9 @@ export function fsTools(cwd, opts = {}) {
|
|
|
218
224
|
if (!info) {
|
|
219
225
|
return { content: seen.has(full) ? `${shown} no longer exists` : `${shown} does not exist; use write_file to create it`, isError: true };
|
|
220
226
|
}
|
|
221
|
-
const text = await readFile(full
|
|
227
|
+
const text = utf8(await readFile(full));
|
|
228
|
+
if (text === undefined)
|
|
229
|
+
return { content: notUtf8(shown), isError: true };
|
|
222
230
|
const problem = staleness(full, info);
|
|
223
231
|
if (problem)
|
|
224
232
|
return { content: problem, isError: true };
|
|
@@ -353,6 +361,26 @@ export function applyEditsToText(text, edits, shown) {
|
|
|
353
361
|
}
|
|
354
362
|
return { text: next };
|
|
355
363
|
}
|
|
364
|
+
/** 按 UTF-8 解码(BOM 留着);不是合法的 UTF-8 时 undefined */
|
|
365
|
+
export function utf8(buf) {
|
|
366
|
+
try {
|
|
367
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buf);
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/** 不是 UTF-8 的文本只用来显示:按 GB18030(含 GBK)解码,运行时不支持时按 UTF-8 解码、坏的字节显示成 U+FFFD */
|
|
374
|
+
function legacyText(buf) {
|
|
375
|
+
try {
|
|
376
|
+
return new TextDecoder("gb18030").decode(buf);
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
return buf.toString("utf8");
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const notUtf8 = (shown) => `${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: ` +
|
|
383
|
+
"change it with a command that keeps its encoding, or ask the user before converting it to UTF-8.";
|
|
356
384
|
/** text 里多数换行是 CRLF 时返回 "\r\n",多数是 LF 时返回 "\n",一样多或没有换行时返回 fallback */
|
|
357
385
|
function eolOf(text, fallback) {
|
|
358
386
|
const crlf = text.split("\r\n").length - 1;
|
package/plugins-dist/fs/grep.js
CHANGED
|
@@ -29,7 +29,8 @@ export function grepTool(cwd, opts = {}) {
|
|
|
29
29
|
path: { type: "string" },
|
|
30
30
|
include: { type: "string" },
|
|
31
31
|
ignore_case: { type: "boolean" },
|
|
32
|
-
|
|
32
|
+
// files_with_matches 是 ripgrep / Claude Code 的叫法,很多模型会这样传,当作 files
|
|
33
|
+
output_mode: { type: "string", enum: ["content", "files", "count", "files_with_matches"] },
|
|
33
34
|
context: { type: "integer" },
|
|
34
35
|
},
|
|
35
36
|
required: ["pattern"],
|
|
@@ -40,6 +41,7 @@ export function grepTool(cwd, opts = {}) {
|
|
|
40
41
|
idempotent: true,
|
|
41
42
|
async execute(input, { signal }) {
|
|
42
43
|
const { pattern, path: p = ".", include, ignore_case = false, output_mode = "content", context = 0 } = input;
|
|
44
|
+
const mode = output_mode === "files_with_matches" ? "files" : output_mode;
|
|
43
45
|
let re;
|
|
44
46
|
try {
|
|
45
47
|
re = new RegExp(pattern, ignore_case ? "i" : "");
|
|
@@ -53,8 +55,8 @@ export function grepTool(cwd, opts = {}) {
|
|
|
53
55
|
pattern,
|
|
54
56
|
ignoreCase: ignore_case,
|
|
55
57
|
include,
|
|
56
|
-
context:
|
|
57
|
-
mode
|
|
58
|
+
context: mode === "content" ? Math.min(Math.max(context, 0), MAX_CONTEXT) : 0,
|
|
59
|
+
mode,
|
|
58
60
|
signal,
|
|
59
61
|
};
|
|
60
62
|
let found;
|
package/plugins-dist/fs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// jade/fs:文件工具 read_file、write_file、edit_file、multi_edit、list_dir、glob、grep。
|
|
2
|
-
import { definePlugin } from "
|
|
2
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
3
3
|
import { fsTools, SeenFiles } from "./fs.js";
|
|
4
4
|
import { fsRenderers } from "./renderers.js";
|
|
5
5
|
export default definePlugin({
|
|
@@ -0,0 +1,145 @@
|
|
|
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
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
17
|
+
import os from "node:os";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
20
|
+
const DEFAULT_TIMEOUT_SECONDS = 60;
|
|
21
|
+
const MAX_TIMEOUT_SECONDS = 600;
|
|
22
|
+
const MAX_OUTPUT_CHARS = 4000;
|
|
23
|
+
/** 检查配置里的 hooks;写错的条目说明原因,其余的照用 */
|
|
24
|
+
export function parseHooks(value) {
|
|
25
|
+
if (value === undefined)
|
|
26
|
+
return { hooks: [], problems: [] };
|
|
27
|
+
if (!Array.isArray(value))
|
|
28
|
+
return { hooks: [], problems: ["hooks must be a list"] };
|
|
29
|
+
const hooks = [];
|
|
30
|
+
const problems = [];
|
|
31
|
+
for (const [i, raw] of value.entries()) {
|
|
32
|
+
const h = (raw ?? {});
|
|
33
|
+
const where = `hooks[${i}]`;
|
|
34
|
+
if (h.on !== "before" && h.on !== "after" && h.on !== "turn")
|
|
35
|
+
problems.push(`${where}: "on" must be "before", "after" or "turn"`);
|
|
36
|
+
else if (typeof h.command !== "string" || !h.command.trim())
|
|
37
|
+
problems.push(`${where}: "command" must be a shell command`);
|
|
38
|
+
else if (h.tool !== undefined && typeof h.tool !== "string")
|
|
39
|
+
problems.push(`${where}: "tool" must be a tool name`);
|
|
40
|
+
else if (h.timeoutSeconds !== undefined && !(typeof h.timeoutSeconds === "number" && h.timeoutSeconds > 0 && h.timeoutSeconds <= MAX_TIMEOUT_SECONDS)) {
|
|
41
|
+
problems.push(`${where}: "timeoutSeconds" must be a number up to ${MAX_TIMEOUT_SECONDS}`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
hooks.push({
|
|
45
|
+
on: h.on,
|
|
46
|
+
command: h.command,
|
|
47
|
+
timeoutMs: (h.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS) * 1000,
|
|
48
|
+
...(typeof h.tool === "string" && h.tool !== "*" ? { tool: h.tool } : {}),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { hooks, problems };
|
|
53
|
+
}
|
|
54
|
+
/** 单引号括起来,给 POSIX shell 用 */
|
|
55
|
+
const quote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
56
|
+
const clip = (s) => (s.length > MAX_OUTPUT_CHARS ? `${s.slice(0, MAX_OUTPUT_CHARS)}\n… (${s.length - MAX_OUTPUT_CHARS} more characters)` : s);
|
|
57
|
+
let warned = false;
|
|
58
|
+
export default definePlugin({
|
|
59
|
+
session(ctx) {
|
|
60
|
+
const { hooks, problems } = parseHooks(ctx.config.hooks);
|
|
61
|
+
const ui = () => ctx.service("ui@1");
|
|
62
|
+
if (problems.length > 0 && !warned) {
|
|
63
|
+
warned = true;
|
|
64
|
+
ui()?.notify(`jade/hooks: ${problems.join("; ")}`, "warning");
|
|
65
|
+
}
|
|
66
|
+
if (hooks.length === 0)
|
|
67
|
+
return;
|
|
68
|
+
const shell = ctx.require("shell@1");
|
|
69
|
+
const sessionId = ctx.session.id;
|
|
70
|
+
/** 运行一条 hook;call 给了就把工具名和参数经环境变量交给它 */
|
|
71
|
+
const run = async (hook, call, signal) => {
|
|
72
|
+
let dir;
|
|
73
|
+
try {
|
|
74
|
+
let exports = "";
|
|
75
|
+
if (call) {
|
|
76
|
+
dir = await mkdtemp(path.join(os.tmpdir(), "jade-hook-"));
|
|
77
|
+
const file = path.join(dir, "input.json");
|
|
78
|
+
await writeFile(file, JSON.stringify(call.input ?? null));
|
|
79
|
+
const p = call.input?.path;
|
|
80
|
+
// 路径用正斜杠:Git Bash 里反斜杠是转义符
|
|
81
|
+
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("/") } : {}) };
|
|
82
|
+
exports = `export ${Object.entries(vars).map(([k, v]) => `${k}=${quote(v)}`).join(" ")}; `;
|
|
83
|
+
}
|
|
84
|
+
const r = await shell.run(exports + hook.command, { cwd: ctx.cwd, timeoutMs: hook.timeoutMs, ...(signal ? { signal } : {}) });
|
|
85
|
+
return { ok: r.code === 0, output: r.timedOut ? `timed out after ${hook.timeoutMs / 1000}s` : clip(r.output.trim()) };
|
|
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
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (hooks.some((h) => h.on === "after")) {
|
|
109
|
+
ctx.kernel.hook({
|
|
110
|
+
kind: "transform",
|
|
111
|
+
point: "tool.after",
|
|
112
|
+
timeoutMs: MAX_TIMEOUT_SECONDS * 1000,
|
|
113
|
+
// 出错时只是这条 hook 没跑,不影响工具的结果
|
|
114
|
+
optional: true,
|
|
115
|
+
fn: async (out) => {
|
|
116
|
+
if (out.isError)
|
|
117
|
+
return undefined;
|
|
118
|
+
for (const h of matching("after", out.call.name)) {
|
|
119
|
+
const r = await run(h, out.call);
|
|
120
|
+
if (r.ok)
|
|
121
|
+
continue;
|
|
122
|
+
const text = `The user's hook after ${out.call.name} (${h.command}) failed${r.output ? `:\n${r.output}` : "."}`;
|
|
123
|
+
ui()?.notify(text, "warning");
|
|
124
|
+
ctx.service("prompts@1")?.note(sessionId, text);
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (hooks.some((h) => h.on === "turn")) {
|
|
131
|
+
ctx.kernel.hook({
|
|
132
|
+
kind: "observe",
|
|
133
|
+
point: "event",
|
|
134
|
+
fn: (e) => {
|
|
135
|
+
if (e.type !== "turn.ended")
|
|
136
|
+
return;
|
|
137
|
+
// 不等它:一轮结束不该被用户的命令拖住
|
|
138
|
+
for (const h of matching("turn")) {
|
|
139
|
+
void run(h, undefined, ctx.signal).then((r) => void (r.ok || ui()?.notify(`jade/hooks: ${h.command} failed${r.output ? `: ${r.output}` : ""}`, "warning")), () => { });
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.js",
|
|
6
|
+
"requires": [
|
|
7
|
+
"shell@1"
|
|
8
|
+
],
|
|
9
|
+
"optional": [
|
|
10
|
+
"ui@1",
|
|
11
|
+
"prompts@1"
|
|
12
|
+
],
|
|
13
|
+
"config": {
|
|
14
|
+
"hooks": {
|
|
15
|
+
"default": [],
|
|
16
|
+
"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"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// jade/keys:快捷键的注册表。前端在输入框有焦点时按下自己不用的键,查这里找到插件的处理函数。
|
|
2
|
-
import { definePlugin } from "
|
|
2
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
3
3
|
/** "ctrl+<字母>"、"alt+<字母或数字>" 或 "shift+tab" */
|
|
4
4
|
export const KEY_SPEC = /^(ctrl\+[a-z]|alt\+[a-z0-9]|shift\+tab)$/;
|
|
5
5
|
/**
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
// jade/mcp:配置的 MCP 服务的工具交给模型(声明只读的也经 task@1 交给子 Agent);/mcp
|
|
1
|
+
// jade/mcp:配置的 MCP 服务的工具交给模型(声明只读的也经 task@1 交给子 Agent);/mcp 查看状态、重连,
|
|
2
|
+
// /mcp add、/mcp remove 改配置文件(见 install.ts;终端里是 jade mcp add|remove)。
|
|
2
3
|
//
|
|
3
4
|
// 服务是进程级的资源:第一个会话打开时读配置、确认项目层的服务、连接,之后各会话共用;进程退出时(进程作用域关闭)全部关掉。
|
|
4
5
|
// 一个会话里的工具列表不变(否则提示缓存失效):第一个会话最多等 STARTUP_WAIT_MS,没连上的在后台接着连,
|
|
5
6
|
// 它们的工具从下一个会话(/clear)开始提供。不能交互时(-p)只有这一个会话,照常等到每个服务连上或超时。
|
|
6
7
|
// 服务进程退出后,下一次调用它的工具时重新连接一次。
|
|
7
8
|
import path from "node:path";
|
|
8
|
-
import { definePlugin } from "
|
|
9
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
9
10
|
import { describeServer, loadMcpConfig, McpClient, mcpTools, mcpTrustHash } from "./mcp.js";
|
|
10
|
-
import {
|
|
11
|
+
import { manageMcp, manageUsage } from "./install.js";
|
|
12
|
+
import { bold, clip, dim, display, estimateTokens, green, realPath, red, repoRoot, splitArgs, TrustFile, yellow } from "./project.js";
|
|
11
13
|
const ID = "jade/mcp";
|
|
12
14
|
/** 第一个会话最多等 MCP 服务连接这么久(能交互时),没连上的在后台接着连 */
|
|
13
15
|
const STARTUP_WAIT_MS = 5000;
|
|
@@ -117,6 +119,11 @@ function instructions(clients) {
|
|
|
117
119
|
|
|
118
120
|
${c.instructions}`);
|
|
119
121
|
}
|
|
122
|
+
/** 一个服务每次请求都带着的大约多少 token:发给模型的工具定义,加上它的使用说明。没连上时为 0 */
|
|
123
|
+
function serverTokens(c) {
|
|
124
|
+
const tools = mcpTools([c]).reduce((n, t) => n + estimateTokens(JSON.stringify({ name: t.name, description: t.description, input_schema: t.inputSchema })), 0);
|
|
125
|
+
return tools + instructions([c]).reduce((n, text) => n + estimateTokens(text), 0);
|
|
126
|
+
}
|
|
120
127
|
/** 当前进程的服务;process() 里创建,session() 里用 */
|
|
121
128
|
let current;
|
|
122
129
|
export default definePlugin({
|
|
@@ -127,13 +134,28 @@ export default definePlugin({
|
|
|
127
134
|
await ctx.effect(() => () => servers.close());
|
|
128
135
|
ctx.service("commands@1")?.register({
|
|
129
136
|
name: "mcp",
|
|
130
|
-
args: "[reconnect <name>]",
|
|
131
|
-
description: "MCP servers: status and tools",
|
|
137
|
+
args: "[reconnect <name> | add <name> … | remove <name>] [--project]",
|
|
138
|
+
description: "MCP servers: status and tools; add or remove one",
|
|
132
139
|
instant: true,
|
|
133
140
|
async run(args) {
|
|
141
|
+
let words;
|
|
142
|
+
try {
|
|
143
|
+
words = splitArgs(args);
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
return red(errorText(e));
|
|
147
|
+
}
|
|
148
|
+
// 只改配置文件,不用先连接
|
|
149
|
+
if (words[0] === "add" || words[0] === "remove" || words[0] === "rm") {
|
|
150
|
+
try {
|
|
151
|
+
return await manageMcp(words, { home: ctx.home, cwd: ctx.cwd, cmd: "/mcp" });
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
return red(errorText(e));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
134
157
|
await servers.start();
|
|
135
158
|
const clients = servers.clients;
|
|
136
|
-
const words = args.trim().split(/\s+/).filter(Boolean);
|
|
137
159
|
if (words[0] === "reconnect") {
|
|
138
160
|
const c = clients.find((x) => x.name === words[1]);
|
|
139
161
|
if (!c)
|
|
@@ -148,15 +170,18 @@ export default definePlugin({
|
|
|
148
170
|
}
|
|
149
171
|
}
|
|
150
172
|
if (words.length > 0)
|
|
151
|
-
return red(
|
|
173
|
+
return red(`usage: /mcp [reconnect <name>]\n${manageUsage("/mcp")}`);
|
|
152
174
|
if (clients.length === 0) {
|
|
153
|
-
return dim(`(no MCP servers) configure them in ${display(ctx.cwd, path.join(ctx.home, "mcp.json"))}, .mcp.json or .jade/mcp.json`);
|
|
175
|
+
return dim(`(no MCP servers) add one with /mcp add, or configure them in ${display(ctx.cwd, path.join(ctx.home, "mcp.json"))}, .mcp.json or .jade/mcp.json`);
|
|
154
176
|
}
|
|
155
177
|
const lines = [];
|
|
178
|
+
let total = 0;
|
|
156
179
|
for (const c of clients) {
|
|
157
180
|
const state = c.status === "connected" ? green("connected") : c.status === "failed" ? red("failed") : yellow(c.status);
|
|
158
181
|
const info = c.serverInfo?.name ? ` · ${c.serverInfo.name}${c.serverInfo.version ? ` ${c.serverInfo.version}` : ""}` : "";
|
|
159
|
-
|
|
182
|
+
const tokens = serverTokens(c);
|
|
183
|
+
total += tokens;
|
|
184
|
+
lines.push(`${bold(c.name)} ${state} ${dim(`${c.entry.layer} · ${display(ctx.cwd, c.entry.file)} · ${describeServer(c.entry.config)}${info}${tokens ? ` · ≈${tokens} tokens` : ""}`)}`);
|
|
160
185
|
if (c.error)
|
|
161
186
|
lines.push(red(` ${c.error}`));
|
|
162
187
|
if (c.tools.length)
|
|
@@ -164,9 +189,16 @@ export default definePlugin({
|
|
|
164
189
|
if (c.status === "failed" && c.stderr.length)
|
|
165
190
|
lines.push(dim(c.stderr.map((l) => ` │ ${clip(l, 200)}`).join("\n")));
|
|
166
191
|
}
|
|
192
|
+
if (total)
|
|
193
|
+
lines.push(dim(`≈${total} tokens in every request (tool definitions and server instructions); remove or disable servers you do not use to save them`));
|
|
167
194
|
return lines.join("\n");
|
|
168
195
|
},
|
|
169
196
|
});
|
|
197
|
+
// 终端里加、删服务:jade mcp add|remove …(cli@1,核心提供)。查看状态要连接服务,只在 /mcp 里有
|
|
198
|
+
ctx.service("cli@1")?.register({
|
|
199
|
+
name: "mcp",
|
|
200
|
+
run: (argv) => manageMcp(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "jade mcp" }),
|
|
201
|
+
});
|
|
170
202
|
},
|
|
171
203
|
async session(ctx) {
|
|
172
204
|
const servers = current;
|