@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/mcp/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
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,没连上的在后台接着连,
|
|
@@ -6,12 +7,11 @@
|
|
|
6
7
|
// 服务进程退出后,下一次调用它的工具时重新连接一次。
|
|
7
8
|
|
|
8
9
|
import path from "node:path";
|
|
9
|
-
import { definePlugin, type ProcessContext, type UI } from "
|
|
10
|
-
import type { Commands } from "
|
|
11
|
-
import type { Prompts } from "../prompts/index.ts";
|
|
12
|
-
import type { Task } from "../task/index.ts";
|
|
10
|
+
import { definePlugin, type Cli, type ProcessContext, type UI } from "@uncarved/jade-api";
|
|
11
|
+
import type { Commands, Prompts, Task } from "@uncarved/jade-api/services";
|
|
13
12
|
import { describeServer, loadMcpConfig, McpClient, mcpTools, mcpTrustHash } from "./mcp.ts";
|
|
14
|
-
import {
|
|
13
|
+
import { manageMcp, manageUsage } from "./install.ts";
|
|
14
|
+
import { bold, clip, dim, display, estimateTokens, green, realPath, red, repoRoot, splitArgs, TrustFile, yellow } from "./project.ts";
|
|
15
15
|
|
|
16
16
|
const ID = "jade/mcp";
|
|
17
17
|
/** 第一个会话最多等 MCP 服务连接这么久(能交互时),没连上的在后台接着连 */
|
|
@@ -130,6 +130,12 @@ function instructions(clients: McpClient[]): string[] {
|
|
|
130
130
|
${c.instructions}`);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
/** 一个服务每次请求都带着的大约多少 token:发给模型的工具定义,加上它的使用说明。没连上时为 0 */
|
|
134
|
+
function serverTokens(c: McpClient): number {
|
|
135
|
+
const tools = mcpTools([c]).reduce((n, t) => n + estimateTokens(JSON.stringify({ name: t.name, description: t.description, input_schema: t.inputSchema })), 0);
|
|
136
|
+
return tools + instructions([c]).reduce((n, text) => n + estimateTokens(text), 0);
|
|
137
|
+
}
|
|
138
|
+
|
|
133
139
|
/** 当前进程的服务;process() 里创建,session() 里用 */
|
|
134
140
|
let current: Servers | undefined;
|
|
135
141
|
|
|
@@ -142,13 +148,26 @@ export default definePlugin({
|
|
|
142
148
|
|
|
143
149
|
ctx.service<Commands>("commands@1")?.register({
|
|
144
150
|
name: "mcp",
|
|
145
|
-
args: "[reconnect <name>]",
|
|
146
|
-
description: "MCP servers: status and tools",
|
|
151
|
+
args: "[reconnect <name> | add <name> … | remove <name>] [--project]",
|
|
152
|
+
description: "MCP servers: status and tools; add or remove one",
|
|
147
153
|
instant: true,
|
|
148
154
|
async run(args) {
|
|
155
|
+
let words: string[];
|
|
156
|
+
try {
|
|
157
|
+
words = splitArgs(args);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return red(errorText(e));
|
|
160
|
+
}
|
|
161
|
+
// 只改配置文件,不用先连接
|
|
162
|
+
if (words[0] === "add" || words[0] === "remove" || words[0] === "rm") {
|
|
163
|
+
try {
|
|
164
|
+
return await manageMcp(words, { home: ctx.home, cwd: ctx.cwd, cmd: "/mcp" });
|
|
165
|
+
} catch (e) {
|
|
166
|
+
return red(errorText(e));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
149
169
|
await servers.start();
|
|
150
170
|
const clients = servers.clients;
|
|
151
|
-
const words = args.trim().split(/\s+/).filter(Boolean);
|
|
152
171
|
if (words[0] === "reconnect") {
|
|
153
172
|
const c = clients.find((x) => x.name === words[1]);
|
|
154
173
|
if (!c) return red(`usage: /mcp reconnect <name>; servers: ${clients.map((x) => x.name).join(", ") || "(none)"}`);
|
|
@@ -160,22 +179,31 @@ export default definePlugin({
|
|
|
160
179
|
return red(`${c.name}: ${errorText(err)}`);
|
|
161
180
|
}
|
|
162
181
|
}
|
|
163
|
-
if (words.length > 0) return red(
|
|
182
|
+
if (words.length > 0) return red(`usage: /mcp [reconnect <name>]\n${manageUsage("/mcp")}`);
|
|
164
183
|
if (clients.length === 0) {
|
|
165
|
-
return dim(`(no MCP servers) configure them in ${display(ctx.cwd, path.join(ctx.home, "mcp.json"))}, .mcp.json or .jade/mcp.json`);
|
|
184
|
+
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`);
|
|
166
185
|
}
|
|
167
186
|
const lines: string[] = [];
|
|
187
|
+
let total = 0;
|
|
168
188
|
for (const c of clients) {
|
|
169
189
|
const state = c.status === "connected" ? green("connected") : c.status === "failed" ? red("failed") : yellow(c.status);
|
|
170
190
|
const info = c.serverInfo?.name ? ` · ${c.serverInfo.name}${c.serverInfo.version ? ` ${c.serverInfo.version}` : ""}` : "";
|
|
171
|
-
|
|
191
|
+
const tokens = serverTokens(c);
|
|
192
|
+
total += tokens;
|
|
193
|
+
lines.push(`${bold(c.name)} ${state} ${dim(`${c.entry.layer} · ${display(ctx.cwd, c.entry.file)} · ${describeServer(c.entry.config)}${info}${tokens ? ` · ≈${tokens} tokens` : ""}`)}`);
|
|
172
194
|
if (c.error) lines.push(red(` ${c.error}`));
|
|
173
195
|
if (c.tools.length) lines.push(` tools ${c.tools.map((t) => `${t.name}${t.annotations?.readOnlyHint ? dim(" (read)") : ""}`).join(", ")}`);
|
|
174
196
|
if (c.status === "failed" && c.stderr.length) lines.push(dim(c.stderr.map((l) => ` │ ${clip(l, 200)}`).join("\n")));
|
|
175
197
|
}
|
|
198
|
+
if (total) lines.push(dim(`≈${total} tokens in every request (tool definitions and server instructions); remove or disable servers you do not use to save them`));
|
|
176
199
|
return lines.join("\n");
|
|
177
200
|
},
|
|
178
201
|
});
|
|
202
|
+
// 终端里加、删服务:jade mcp add|remove …(cli@1,核心提供)。查看状态要连接服务,只在 /mcp 里有
|
|
203
|
+
ctx.service<Cli>("cli@1")?.register({
|
|
204
|
+
name: "mcp",
|
|
205
|
+
run: (argv) => manageMcp(argv, { home: ctx.home, cwd: ctx.cwd, cmd: "jade mcp" }),
|
|
206
|
+
});
|
|
179
207
|
},
|
|
180
208
|
|
|
181
209
|
async session(ctx) {
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// /mcp add|remove(终端里是 jade mcp add|remove):往配置文件里加一个 MCP 服务或删掉它,不用手改 JSON。
|
|
2
|
+
//
|
|
3
|
+
// 不加 --project 写 $JADE_HOME/mcp.json,加了写 <工作目录>/.jade/mcp.json;文件里其他的内容原样保留。
|
|
4
|
+
// 这里只写配置,不启动服务:进程里的服务只在第一个会话打开时连接,新加的重启 jade 后生效。
|
|
5
|
+
// 项目层的服务照旧在第一次启动前请用户确认:命令不顺带记下确认。这个命令也有命令行的写法(jade mcp add),
|
|
6
|
+
// 模型能经 shell 跑它;如果加了就算确认,模型就能绕过确认。
|
|
7
|
+
|
|
8
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { checkConfig, describeServer, type McpServerConfig } from "./mcp.ts";
|
|
11
|
+
import { display } from "./project.ts";
|
|
12
|
+
|
|
13
|
+
/** cmd 是 "/mcp" 或 "jade mcp" */
|
|
14
|
+
export const manageUsage = (cmd: string) => `usage: ${cmd} add <name> [--project] [--env KEY=VALUE]… [--force] [--] <command> [args…]
|
|
15
|
+
${cmd} add <name> --url <url> [--project] [--header "Name: value"]… [--force]
|
|
16
|
+
${cmd} remove <name> [--project]
|
|
17
|
+
|
|
18
|
+
--project write <cwd>/.jade/mcp.json instead of $JADE_HOME/mcp.json
|
|
19
|
+
--env an environment variable for a stdio server; values may use \${VAR}
|
|
20
|
+
--header an HTTP header for a --url server; values may use \${VAR}
|
|
21
|
+
--force replace a server with the same name
|
|
22
|
+
Everything after <command> is passed to it unchanged, e.g. ${cmd} add fs npx -y @modelcontextprotocol/server-filesystem .`;
|
|
23
|
+
|
|
24
|
+
/** 不加 --project 写 $JADE_HOME/mcp.json,加了写 <工作目录>/.jade/mcp.json */
|
|
25
|
+
export const mcpConfigFile = (home: string, cwd: string, project: boolean) => (project ? path.join(cwd, ".jade", "mcp.json") : path.join(home, "mcp.json"));
|
|
26
|
+
|
|
27
|
+
/** add / remove,参数已经切好;返回给用户看的结果,用法错误或失败时抛错。cmd 用在提示里 */
|
|
28
|
+
export async function manageMcp(argv: readonly string[], env: { home: string; cwd: string; cmd: string }): Promise<string> {
|
|
29
|
+
const usage = manageUsage(env.cmd);
|
|
30
|
+
const [sub, ...tail] = argv;
|
|
31
|
+
if (sub === "-h" || sub === "--help") return usage;
|
|
32
|
+
if (sub === "add") {
|
|
33
|
+
const { name, project, force, config } = parseAdd(tail, usage);
|
|
34
|
+
const file = mcpConfigFile(env.home, env.cwd, project);
|
|
35
|
+
await edit(file, (servers) => {
|
|
36
|
+
if (servers[name] !== undefined && !force) throw new Error(`${display(env.cwd, file)} already has an MCP server named ${name}; add --force to replace it`);
|
|
37
|
+
servers[name] = config;
|
|
38
|
+
});
|
|
39
|
+
return [
|
|
40
|
+
`added MCP server ${name} to ${display(env.cwd, file)}: ${describeServer(config)}`,
|
|
41
|
+
`restart jade to start it${project ? "; jade asks you to confirm it the first time" : ""}`,
|
|
42
|
+
].join("\n");
|
|
43
|
+
}
|
|
44
|
+
if (sub === "remove" || sub === "rm") {
|
|
45
|
+
const project = tail.includes("--project");
|
|
46
|
+
const rest = tail.filter((a) => a !== "--project");
|
|
47
|
+
if (rest.length !== 1 || rest[0]!.startsWith("-")) throw new Error(usage);
|
|
48
|
+
const name = rest[0]!;
|
|
49
|
+
const file = mcpConfigFile(env.home, env.cwd, project);
|
|
50
|
+
await edit(file, (servers) => {
|
|
51
|
+
if (servers[name] === undefined) throw new Error(`${display(env.cwd, file)} has no MCP server named ${name}`);
|
|
52
|
+
delete servers[name];
|
|
53
|
+
});
|
|
54
|
+
return `removed MCP server ${name} from ${display(env.cwd, file)}; restart jade to stop it`;
|
|
55
|
+
}
|
|
56
|
+
throw new Error(usage);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function parseAdd(tail: readonly string[], usage: string): { name: string; project: boolean; force: boolean; config: McpServerConfig } {
|
|
60
|
+
let name: string | undefined;
|
|
61
|
+
let project = false;
|
|
62
|
+
let force = false;
|
|
63
|
+
let url: string | undefined;
|
|
64
|
+
const envVars: Record<string, string> = {};
|
|
65
|
+
const headers: Record<string, string> = {};
|
|
66
|
+
let command: string[] | undefined;
|
|
67
|
+
for (let i = 0; i < tail.length; i++) {
|
|
68
|
+
const a = tail[i]!;
|
|
69
|
+
const value = () => {
|
|
70
|
+
const v = tail[++i];
|
|
71
|
+
if (v === undefined) throw new Error(`${a} needs a value\n${usage}`);
|
|
72
|
+
return v;
|
|
73
|
+
};
|
|
74
|
+
if (a === "--") {
|
|
75
|
+
command = tail.slice(i + 1);
|
|
76
|
+
break;
|
|
77
|
+
} else if (a === "--project") project = true;
|
|
78
|
+
else if (a === "--force" || a === "-f") force = true;
|
|
79
|
+
else if (a === "--url") url = value();
|
|
80
|
+
else if (a === "--env" || a === "-e") {
|
|
81
|
+
const v = value();
|
|
82
|
+
const eq = v.indexOf("=");
|
|
83
|
+
if (eq <= 0) throw new Error(`--env needs KEY=VALUE, got ${JSON.stringify(v)}`);
|
|
84
|
+
envVars[v.slice(0, eq)] = v.slice(eq + 1);
|
|
85
|
+
} else if (a === "--header" || a === "-H") {
|
|
86
|
+
const v = value();
|
|
87
|
+
const colon = v.indexOf(":");
|
|
88
|
+
if (colon <= 0) throw new Error(`--header needs "Name: value", got ${JSON.stringify(v)}`);
|
|
89
|
+
headers[v.slice(0, colon).trim()] = v.slice(colon + 1).trim();
|
|
90
|
+
} else if (a.startsWith("-")) throw new Error(`unknown option ${a}\n${usage}`);
|
|
91
|
+
else if (name === undefined) name = a;
|
|
92
|
+
else {
|
|
93
|
+
// 名字之后第一个不是选项的参数是服务的命令,后面的都原样交给它(npx -y … 里的 -y 不是 jade 的选项)
|
|
94
|
+
command = tail.slice(i);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (name === undefined) throw new Error(usage);
|
|
99
|
+
if (url !== undefined && command?.length) throw new Error(`give either --url or a command, not both\n${usage}`);
|
|
100
|
+
if (url === undefined && !command?.length) throw new Error(`give a command or --url\n${usage}`);
|
|
101
|
+
let config: McpServerConfig;
|
|
102
|
+
if (url !== undefined) {
|
|
103
|
+
// 含 ${VAR} 的地址要到读配置时才知道展开后是什么,那时再检查
|
|
104
|
+
if (!/^https?:\/\//.test(url) && !url.includes("${")) throw new Error(`--url must start with http:// or https://`);
|
|
105
|
+
if (Object.keys(envVars).length) throw new Error("--env is for servers started with a command; a --url server takes --header");
|
|
106
|
+
config = { type: "http", url, ...(Object.keys(headers).length ? { headers } : {}) };
|
|
107
|
+
} else {
|
|
108
|
+
if (Object.keys(headers).length) throw new Error("--header is for --url servers; a command takes --env");
|
|
109
|
+
const [cmd, ...args] = command!;
|
|
110
|
+
config = { command: cmd!, ...(args.length ? { args } : {}), ...(Object.keys(envVars).length ? { env: envVars } : {}) };
|
|
111
|
+
}
|
|
112
|
+
const problem = checkConfig(name, config);
|
|
113
|
+
if (problem) throw new Error(`MCP server ${JSON.stringify(name)}: ${problem}`);
|
|
114
|
+
return { name, project, force, config };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 读配置文件、改 mcpServers、写回;文件坏了时报错,不覆盖 */
|
|
118
|
+
async function edit(file: string, change: (servers: Record<string, unknown>) => void): Promise<void> {
|
|
119
|
+
let json: Record<string, unknown> = {};
|
|
120
|
+
let text: string | undefined;
|
|
121
|
+
try {
|
|
122
|
+
text = await readFile(file, "utf8");
|
|
123
|
+
} catch {
|
|
124
|
+
// 还没有这个文件
|
|
125
|
+
}
|
|
126
|
+
if (text !== undefined && text.trim()) {
|
|
127
|
+
try {
|
|
128
|
+
json = JSON.parse(text);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
throw new Error(`${file}: ${e instanceof Error ? e.message : String(e)}; fix it by hand first`);
|
|
131
|
+
}
|
|
132
|
+
if (typeof json !== "object" || json === null || Array.isArray(json)) throw new Error(`${file}: expected { "mcpServers": { ... } }; fix it by hand first`);
|
|
133
|
+
}
|
|
134
|
+
const servers = (json.mcpServers ??= {}) as Record<string, unknown>;
|
|
135
|
+
if (typeof servers !== "object" || servers === null || Array.isArray(servers)) throw new Error(`${file}: "mcpServers" is not an object; fix it by hand first`);
|
|
136
|
+
change(servers);
|
|
137
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
138
|
+
await writeFile(file, JSON.stringify(json, null, 2) + "\n");
|
|
139
|
+
}
|
package/plugins/mcp/mcp.ts
CHANGED
|
@@ -135,7 +135,8 @@ export async function loadMcpConfig(opts: {
|
|
|
135
135
|
|
|
136
136
|
const SERVER_NAME = /^[A-Za-z0-9_.-]+$/;
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
/** 一条服务配置的问题;没问题时为 undefined。/mcp add 写入前也用它检查 */
|
|
139
|
+
export function checkConfig(name: string, raw: unknown): string | undefined {
|
|
139
140
|
if (!SERVER_NAME.test(name)) return "the name may only contain letters, digits, '.', '_' and '-'";
|
|
140
141
|
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return "must be an object";
|
|
141
142
|
const c = raw as McpServerConfig;
|
package/plugins/mcp/plugin.json
CHANGED
package/plugins/mcp/project.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 仓库目录、确认记录、显示用的小工具、斜杠命令参数的切分、token 估算。插件之间不共享代码(只能共享类型),jade/skills、jade/checks 各有一份。
|
|
2
2
|
|
|
3
3
|
import { existsSync, realpathSync } from "node:fs";
|
|
4
4
|
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
@@ -91,3 +91,39 @@ export const red = paint("31", "39");
|
|
|
91
91
|
export const green = paint("32", "39");
|
|
92
92
|
export const yellow = paint("33", "39");
|
|
93
93
|
export const bold = paint("1", "22");
|
|
94
|
+
|
|
95
|
+
/** 斜杠命令的参数按空白切开;"…" 或 '…' 里的空白不切(不处理反斜杠,Windows 路径照写) */
|
|
96
|
+
export function splitArgs(text: string): string[] {
|
|
97
|
+
const out: string[] = [];
|
|
98
|
+
let cur = "";
|
|
99
|
+
let started = false;
|
|
100
|
+
let quote: string | undefined;
|
|
101
|
+
for (const ch of text) {
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (ch === quote) quote = undefined;
|
|
104
|
+
else cur += ch;
|
|
105
|
+
} else if (ch === '"' || ch === "'") {
|
|
106
|
+
quote = ch;
|
|
107
|
+
started = true;
|
|
108
|
+
} else if (/\s/.test(ch)) {
|
|
109
|
+
if (started) out.push(cur);
|
|
110
|
+
cur = "";
|
|
111
|
+
started = false;
|
|
112
|
+
} else {
|
|
113
|
+
cur += ch;
|
|
114
|
+
started = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (quote) throw new Error(`unclosed ${quote}`);
|
|
118
|
+
if (started) out.push(cur);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 中日韩文字和全角符号:大约一个字一个 token(和 jade/compact 的估算一致) */
|
|
123
|
+
const WIDE = /[ -鿿가-豈--]/g;
|
|
124
|
+
|
|
125
|
+
/** 粗略估算一段文字发给模型时占多少 token:英文和代码约 4 个字符 1 个,中日韩文字每字约 1 个 */
|
|
126
|
+
export function estimateTokens(text: string): number {
|
|
127
|
+
const wide = text.match(WIDE)?.length ?? 0;
|
|
128
|
+
return Math.ceil((text.length - wide) / 4 + wide);
|
|
129
|
+
}
|
package/plugins/memory/index.ts
CHANGED
|
@@ -2,16 +2,11 @@
|
|
|
2
2
|
// 会话开始时经 prompts@1 把记忆放进系统提示词(会话中途的改动下一个会话才进提示词,这样提示缓存不失效;
|
|
3
3
|
// 用户在 /memory 里改了,下一次请求时附一条说明告诉模型)。
|
|
4
4
|
|
|
5
|
-
import { definePlugin } from "
|
|
6
|
-
import type { Commands } from "
|
|
7
|
-
import type { Prompts } from "../prompts/index.ts";
|
|
8
|
-
import type { Renderers } from "../renderers/index.ts";
|
|
5
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
6
|
+
import type { Commands, Prompts, Renderers } from "@uncarved/jade-api/services";
|
|
9
7
|
import { Memory, MEMORY_SCOPES, memoryTool, projectRoot, type MemoryScope } from "./memory.ts";
|
|
10
|
-
import { Notes } from "./notes.ts";
|
|
11
8
|
import { clip, dim, green, red } from "./style.ts";
|
|
12
9
|
|
|
13
|
-
const notes = new Notes();
|
|
14
|
-
|
|
15
10
|
const args = (input: unknown) => (input ?? {}) as { action?: string; scope?: string; text?: string };
|
|
16
11
|
|
|
17
12
|
export default definePlugin({
|
|
@@ -38,7 +33,8 @@ export default definePlugin({
|
|
|
38
33
|
async run(input, { ui, sessions, signal }) {
|
|
39
34
|
const note = (text: string) => {
|
|
40
35
|
const id = sessions.current?.id;
|
|
41
|
-
|
|
36
|
+
// 用到的时候再取:可选服务在 process() 开头不一定已经有了
|
|
37
|
+
if (id) ctx.service<Prompts>("prompts@1")?.note(id, text);
|
|
42
38
|
};
|
|
43
39
|
let text = input.trim();
|
|
44
40
|
if (!text) {
|
|
@@ -97,7 +93,6 @@ export default definePlugin({
|
|
|
97
93
|
async session(ctx) {
|
|
98
94
|
const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
|
|
99
95
|
ctx.kernel.tool(memoryTool(memory));
|
|
100
|
-
ctx.kernel.hook(notes.hook(ctx.session.id, ctx.plugin, ctx.plugin));
|
|
101
96
|
const text = await memory.prompt();
|
|
102
97
|
// 段落随会话作用域撤销
|
|
103
98
|
if (text) ctx.service<Prompts>("prompts@1")?.add({ text });
|
|
@@ -3,21 +3,26 @@
|
|
|
3
3
|
// 内核没有改写用户输入的扩展点:input.received 原样进日志。所以在 context.project 里把附件加到引用它的那条用户消息后面。
|
|
4
4
|
// 附件在消息之后第一次投影时读取,先用 ctx.record 写进日志(derived)再使用(I-5):之后每次投影都从 derived 里取,
|
|
5
5
|
// 文件之后变了也不影响已经发出的消息(提示缓存不失效),恢复会话后也一样。
|
|
6
|
+
// 每条消息的附件单独记一个 key(attachedKey):derived.recorded 是整值替换,记成一张表的话,
|
|
7
|
+
// 每次有新的 @ 都要把之前附过的全部内容再写一遍,日志按平方增长。
|
|
6
8
|
// 输入框里 @ 的补全不在这里,在 jade/tui。
|
|
7
9
|
|
|
8
10
|
import type { Block, Message } from "uncarved";
|
|
9
|
-
import { definePlugin } from "
|
|
10
|
-
import type { Renderers } from "
|
|
11
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
12
|
+
import type { Renderers } from "@uncarved/jade-api/services";
|
|
11
13
|
import { attachMentions } from "./attach.ts";
|
|
12
14
|
import { clip, dim, yellow } from "./style.ts";
|
|
13
15
|
|
|
14
16
|
export interface MentionsState {
|
|
15
17
|
/** state.messages 的前 upTo 条已经处理过 */
|
|
16
18
|
upTo: number;
|
|
17
|
-
/**
|
|
18
|
-
attached
|
|
19
|
+
/** 旧格式:消息的下标 → 附在它后面的内容。现在每条消息单独记在 attachedKey 下,这里只为读旧会话的日志 */
|
|
20
|
+
attached?: Record<string, Block[]>;
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
/** 附在第 i 条消息后面的内容记在这个 key 下 */
|
|
24
|
+
export const attachedKey = (plugin: string, i: number) => `${plugin}/${i}`;
|
|
25
|
+
|
|
21
26
|
/** 读文件的时限 */
|
|
22
27
|
const READ_TIMEOUT_MS = 30_000;
|
|
23
28
|
|
|
@@ -53,26 +58,41 @@ export default definePlugin({
|
|
|
53
58
|
optional: true,
|
|
54
59
|
timeoutMs: READ_TIMEOUT_MS + 5_000,
|
|
55
60
|
fn: async (messages, hook) => {
|
|
56
|
-
|
|
57
|
-
const start = Math.max(state
|
|
58
|
-
|
|
61
|
+
const state = hook.derived[key] as MentionsState | undefined;
|
|
62
|
+
const start = Math.max(state?.upTo ?? 0, scanned);
|
|
63
|
+
/** 这次记下的;hook.derived 是调用时的快照,刚记下的不在里面 */
|
|
64
|
+
const fresh = new Map<number, Block[]>();
|
|
65
|
+
if (state?.attached) {
|
|
66
|
+
// 旧格式的会话:先拆成每条消息一个 key,进度里去掉整表,之后不再随新的 @ 重写
|
|
67
|
+
for (const [i, blocks] of Object.entries(state.attached)) {
|
|
68
|
+
await hook.record(attachedKey(key, Number(i)), blocks);
|
|
69
|
+
fresh.set(Number(i), blocks);
|
|
70
|
+
}
|
|
71
|
+
await hook.record(key, { upTo: state.upTo } satisfies MentionsState);
|
|
72
|
+
}
|
|
59
73
|
if (messages.length > start) {
|
|
60
|
-
|
|
74
|
+
let changed = false;
|
|
61
75
|
const signal = AbortSignal.any([hook.signal, AbortSignal.timeout(READ_TIMEOUT_MS)]);
|
|
62
76
|
for (let i = start; i < messages.length; i++) {
|
|
63
77
|
const text = userText(messages[i]!);
|
|
64
78
|
if (!text.includes("@")) continue;
|
|
65
79
|
changed = true;
|
|
66
80
|
const refs = await attachMentions(text, { cwd: ctx.cwd, plugin: ctx.plugin, signal });
|
|
67
|
-
if (refs.blocks.length)
|
|
81
|
+
if (refs.blocks.length) {
|
|
82
|
+
await hook.record(attachedKey(key, i), refs.blocks);
|
|
83
|
+
fresh.set(i, refs.blocks);
|
|
84
|
+
}
|
|
68
85
|
if (refs.attached.length || refs.failed.length) await hook.emit("attached", { attached: refs.attached, failed: refs.failed });
|
|
69
86
|
}
|
|
70
|
-
|
|
71
|
-
//
|
|
72
|
-
if (changed) await hook.record(key,
|
|
87
|
+
// 处理过带 @ 的消息才记:恢复的会话不再重新读这些消息引用的文件(文件可能已经变了)
|
|
88
|
+
// 附件先于进度记下:两次记录之间崩溃时请求还没发出,恢复后重新处理这条消息即可
|
|
89
|
+
if (changed) await hook.record(key, { upTo: messages.length } satisfies MentionsState);
|
|
73
90
|
scanned = messages.length;
|
|
74
91
|
}
|
|
75
|
-
return messages.map((m, i) =>
|
|
92
|
+
return messages.map((m, i) => {
|
|
93
|
+
const extra = fresh.get(i) ?? (hook.derived[attachedKey(key, i)] as Block[] | undefined);
|
|
94
|
+
return extra ? { ...m, content: [...m.content, ...extra] } : m;
|
|
95
|
+
});
|
|
76
96
|
},
|
|
77
97
|
});
|
|
78
98
|
},
|