@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,160 @@
|
|
|
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
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { checkConfig, describeServer } from "./mcp.js";
|
|
10
|
+
import { display } from "./project.js";
|
|
11
|
+
/** cmd 是 "/mcp" 或 "jade mcp" */
|
|
12
|
+
export const manageUsage = (cmd) => `usage: ${cmd} add <name> [--project] [--env KEY=VALUE]… [--force] [--] <command> [args…]
|
|
13
|
+
${cmd} add <name> --url <url> [--project] [--header "Name: value"]… [--force]
|
|
14
|
+
${cmd} remove <name> [--project]
|
|
15
|
+
|
|
16
|
+
--project write <cwd>/.jade/mcp.json instead of $JADE_HOME/mcp.json
|
|
17
|
+
--env an environment variable for a stdio server; values may use \${VAR}
|
|
18
|
+
--header an HTTP header for a --url server; values may use \${VAR}
|
|
19
|
+
--force replace a server with the same name
|
|
20
|
+
Everything after <command> is passed to it unchanged, e.g. ${cmd} add fs npx -y @modelcontextprotocol/server-filesystem .`;
|
|
21
|
+
/** 不加 --project 写 $JADE_HOME/mcp.json,加了写 <工作目录>/.jade/mcp.json */
|
|
22
|
+
export const mcpConfigFile = (home, cwd, project) => (project ? path.join(cwd, ".jade", "mcp.json") : path.join(home, "mcp.json"));
|
|
23
|
+
/** add / remove,参数已经切好;返回给用户看的结果,用法错误或失败时抛错。cmd 用在提示里 */
|
|
24
|
+
export async function manageMcp(argv, env) {
|
|
25
|
+
const usage = manageUsage(env.cmd);
|
|
26
|
+
const [sub, ...tail] = argv;
|
|
27
|
+
if (sub === "-h" || sub === "--help")
|
|
28
|
+
return usage;
|
|
29
|
+
if (sub === "add") {
|
|
30
|
+
const { name, project, force, config } = parseAdd(tail, usage);
|
|
31
|
+
const file = mcpConfigFile(env.home, env.cwd, project);
|
|
32
|
+
await edit(file, (servers) => {
|
|
33
|
+
if (servers[name] !== undefined && !force)
|
|
34
|
+
throw new Error(`${display(env.cwd, file)} already has an MCP server named ${name}; add --force to replace it`);
|
|
35
|
+
servers[name] = config;
|
|
36
|
+
});
|
|
37
|
+
return [
|
|
38
|
+
`added MCP server ${name} to ${display(env.cwd, file)}: ${describeServer(config)}`,
|
|
39
|
+
`restart jade to start it${project ? "; jade asks you to confirm it the first time" : ""}`,
|
|
40
|
+
].join("\n");
|
|
41
|
+
}
|
|
42
|
+
if (sub === "remove" || sub === "rm") {
|
|
43
|
+
const project = tail.includes("--project");
|
|
44
|
+
const rest = tail.filter((a) => a !== "--project");
|
|
45
|
+
if (rest.length !== 1 || rest[0].startsWith("-"))
|
|
46
|
+
throw new Error(usage);
|
|
47
|
+
const name = rest[0];
|
|
48
|
+
const file = mcpConfigFile(env.home, env.cwd, project);
|
|
49
|
+
await edit(file, (servers) => {
|
|
50
|
+
if (servers[name] === undefined)
|
|
51
|
+
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
|
+
function parseAdd(tail, usage) {
|
|
59
|
+
let name;
|
|
60
|
+
let project = false;
|
|
61
|
+
let force = false;
|
|
62
|
+
let url;
|
|
63
|
+
const envVars = {};
|
|
64
|
+
const headers = {};
|
|
65
|
+
let command;
|
|
66
|
+
for (let i = 0; i < tail.length; i++) {
|
|
67
|
+
const a = tail[i];
|
|
68
|
+
const value = () => {
|
|
69
|
+
const v = tail[++i];
|
|
70
|
+
if (v === undefined)
|
|
71
|
+
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
|
+
}
|
|
78
|
+
else if (a === "--project")
|
|
79
|
+
project = true;
|
|
80
|
+
else if (a === "--force" || a === "-f")
|
|
81
|
+
force = true;
|
|
82
|
+
else if (a === "--url")
|
|
83
|
+
url = value();
|
|
84
|
+
else if (a === "--env" || a === "-e") {
|
|
85
|
+
const v = value();
|
|
86
|
+
const eq = v.indexOf("=");
|
|
87
|
+
if (eq <= 0)
|
|
88
|
+
throw new Error(`--env needs KEY=VALUE, got ${JSON.stringify(v)}`);
|
|
89
|
+
envVars[v.slice(0, eq)] = v.slice(eq + 1);
|
|
90
|
+
}
|
|
91
|
+
else if (a === "--header" || a === "-H") {
|
|
92
|
+
const v = value();
|
|
93
|
+
const colon = v.indexOf(":");
|
|
94
|
+
if (colon <= 0)
|
|
95
|
+
throw new Error(`--header needs "Name: value", got ${JSON.stringify(v)}`);
|
|
96
|
+
headers[v.slice(0, colon).trim()] = v.slice(colon + 1).trim();
|
|
97
|
+
}
|
|
98
|
+
else if (a.startsWith("-"))
|
|
99
|
+
throw new Error(`unknown option ${a}\n${usage}`);
|
|
100
|
+
else if (name === undefined)
|
|
101
|
+
name = a;
|
|
102
|
+
else {
|
|
103
|
+
// 名字之后第一个不是选项的参数是服务的命令,后面的都原样交给它(npx -y … 里的 -y 不是 jade 的选项)
|
|
104
|
+
command = tail.slice(i);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (name === undefined)
|
|
109
|
+
throw new Error(usage);
|
|
110
|
+
if (url !== undefined && command?.length)
|
|
111
|
+
throw new Error(`give either --url or a command, not both\n${usage}`);
|
|
112
|
+
if (url === undefined && !command?.length)
|
|
113
|
+
throw new Error(`give a command or --url\n${usage}`);
|
|
114
|
+
let config;
|
|
115
|
+
if (url !== undefined) {
|
|
116
|
+
// 含 ${VAR} 的地址要到读配置时才知道展开后是什么,那时再检查
|
|
117
|
+
if (!/^https?:\/\//.test(url) && !url.includes("${"))
|
|
118
|
+
throw new Error(`--url must start with http:// or https://`);
|
|
119
|
+
if (Object.keys(envVars).length)
|
|
120
|
+
throw new Error("--env is for servers started with a command; a --url server takes --header");
|
|
121
|
+
config = { type: "http", url, ...(Object.keys(headers).length ? { headers } : {}) };
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (Object.keys(headers).length)
|
|
125
|
+
throw new Error("--header is for --url servers; a command takes --env");
|
|
126
|
+
const [cmd, ...args] = command;
|
|
127
|
+
config = { command: cmd, ...(args.length ? { args } : {}), ...(Object.keys(envVars).length ? { env: envVars } : {}) };
|
|
128
|
+
}
|
|
129
|
+
const problem = checkConfig(name, config);
|
|
130
|
+
if (problem)
|
|
131
|
+
throw new Error(`MCP server ${JSON.stringify(name)}: ${problem}`);
|
|
132
|
+
return { name, project, force, config };
|
|
133
|
+
}
|
|
134
|
+
/** 读配置文件、改 mcpServers、写回;文件坏了时报错,不覆盖 */
|
|
135
|
+
async function edit(file, change) {
|
|
136
|
+
let json = {};
|
|
137
|
+
let text;
|
|
138
|
+
try {
|
|
139
|
+
text = await readFile(file, "utf8");
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// 还没有这个文件
|
|
143
|
+
}
|
|
144
|
+
if (text !== undefined && text.trim()) {
|
|
145
|
+
try {
|
|
146
|
+
json = JSON.parse(text);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
throw new Error(`${file}: ${e instanceof Error ? e.message : String(e)}; fix it by hand first`);
|
|
150
|
+
}
|
|
151
|
+
if (typeof json !== "object" || json === null || Array.isArray(json))
|
|
152
|
+
throw new Error(`${file}: expected { "mcpServers": { ... } }; fix it by hand first`);
|
|
153
|
+
}
|
|
154
|
+
const servers = (json.mcpServers ??= {});
|
|
155
|
+
if (typeof servers !== "object" || servers === null || Array.isArray(servers))
|
|
156
|
+
throw new Error(`${file}: "mcpServers" is not an object; fix it by hand first`);
|
|
157
|
+
change(servers);
|
|
158
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
159
|
+
await writeFile(file, JSON.stringify(json, null, 2) + "\n");
|
|
160
|
+
}
|
package/plugins-dist/mcp/mcp.js
CHANGED
|
@@ -90,7 +90,8 @@ export async function loadMcpConfig(opts) {
|
|
|
90
90
|
return { servers: [...out.values()], warnings };
|
|
91
91
|
}
|
|
92
92
|
const SERVER_NAME = /^[A-Za-z0-9_.-]+$/;
|
|
93
|
-
|
|
93
|
+
/** 一条服务配置的问题;没问题时为 undefined。/mcp add 写入前也用它检查 */
|
|
94
|
+
export function checkConfig(name, raw) {
|
|
94
95
|
if (!SERVER_NAME.test(name))
|
|
95
96
|
return "the name may only contain letters, digits, '.', '_' and '-'";
|
|
96
97
|
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 仓库目录、确认记录、显示用的小工具、斜杠命令参数的切分、token 估算。插件之间不共享代码(只能共享类型),jade/skills、jade/checks 各有一份。
|
|
2
2
|
import { existsSync, realpathSync } from "node:fs";
|
|
3
3
|
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
@@ -88,3 +88,44 @@ export const red = paint("31", "39");
|
|
|
88
88
|
export const green = paint("32", "39");
|
|
89
89
|
export const yellow = paint("33", "39");
|
|
90
90
|
export const bold = paint("1", "22");
|
|
91
|
+
/** 斜杠命令的参数按空白切开;"…" 或 '…' 里的空白不切(不处理反斜杠,Windows 路径照写) */
|
|
92
|
+
export function splitArgs(text) {
|
|
93
|
+
const out = [];
|
|
94
|
+
let cur = "";
|
|
95
|
+
let started = false;
|
|
96
|
+
let quote;
|
|
97
|
+
for (const ch of text) {
|
|
98
|
+
if (quote) {
|
|
99
|
+
if (ch === quote)
|
|
100
|
+
quote = undefined;
|
|
101
|
+
else
|
|
102
|
+
cur += ch;
|
|
103
|
+
}
|
|
104
|
+
else if (ch === '"' || ch === "'") {
|
|
105
|
+
quote = ch;
|
|
106
|
+
started = true;
|
|
107
|
+
}
|
|
108
|
+
else if (/\s/.test(ch)) {
|
|
109
|
+
if (started)
|
|
110
|
+
out.push(cur);
|
|
111
|
+
cur = "";
|
|
112
|
+
started = false;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
cur += ch;
|
|
116
|
+
started = true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (quote)
|
|
120
|
+
throw new Error(`unclosed ${quote}`);
|
|
121
|
+
if (started)
|
|
122
|
+
out.push(cur);
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
/** 中日韩文字和全角符号:大约一个字一个 token(和 jade/compact 的估算一致) */
|
|
126
|
+
const WIDE = /[ -鿿가-豈--]/g;
|
|
127
|
+
/** 粗略估算一段文字发给模型时占多少 token:英文和代码约 4 个字符 1 个,中日韩文字每字约 1 个 */
|
|
128
|
+
export function estimateTokens(text) {
|
|
129
|
+
const wide = text.match(WIDE)?.length ?? 0;
|
|
130
|
+
return Math.ceil((text.length - wide) / 4 + wide);
|
|
131
|
+
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// jade/memory:跨会话的记忆(见 memory.ts)。memory 工具给模型增删,/memory 给用户查看和管理,
|
|
2
2
|
// 会话开始时经 prompts@1 把记忆放进系统提示词(会话中途的改动下一个会话才进提示词,这样提示缓存不失效;
|
|
3
3
|
// 用户在 /memory 里改了,下一次请求时附一条说明告诉模型)。
|
|
4
|
-
import { definePlugin } from "
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
5
|
import { Memory, MEMORY_SCOPES, memoryTool, projectRoot } from "./memory.js";
|
|
6
|
-
import { Notes } from "./notes.js";
|
|
7
6
|
import { clip, dim, green, red } from "./style.js";
|
|
8
|
-
const notes = new Notes();
|
|
9
7
|
const args = (input) => (input ?? {});
|
|
10
8
|
export default definePlugin({
|
|
11
9
|
process(ctx) {
|
|
@@ -29,8 +27,9 @@ export default definePlugin({
|
|
|
29
27
|
async run(input, { ui, sessions, signal }) {
|
|
30
28
|
const note = (text) => {
|
|
31
29
|
const id = sessions.current?.id;
|
|
30
|
+
// 用到的时候再取:可选服务在 process() 开头不一定已经有了
|
|
32
31
|
if (id)
|
|
33
|
-
|
|
32
|
+
ctx.service("prompts@1")?.note(id, text);
|
|
34
33
|
};
|
|
35
34
|
let text = input.trim();
|
|
36
35
|
if (!text) {
|
|
@@ -85,7 +84,6 @@ export default definePlugin({
|
|
|
85
84
|
async session(ctx) {
|
|
86
85
|
const memory = new Memory(ctx.home, projectRoot(ctx.cwd));
|
|
87
86
|
ctx.kernel.tool(memoryTool(memory));
|
|
88
|
-
ctx.kernel.hook(notes.hook(ctx.session.id, ctx.plugin, ctx.plugin));
|
|
89
87
|
const text = await memory.prompt();
|
|
90
88
|
// 段落随会话作用域撤销
|
|
91
89
|
if (text)
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// 每条消息的附件单独记一个 key(attachedKey):derived.recorded 是整值替换,记成一张表的话,
|
|
7
7
|
// 每次有新的 @ 都要把之前附过的全部内容再写一遍,日志按平方增长。
|
|
8
8
|
// 输入框里 @ 的补全不在这里,在 jade/tui。
|
|
9
|
-
import { definePlugin } from "
|
|
9
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
10
10
|
import { attachMentions } from "./attach.js";
|
|
11
11
|
import { clip, dim, yellow } from "./style.js";
|
|
12
12
|
/** 附在第 i 条消息后面的内容记在这个 key 下 */
|