@uncarved/jade 0.1.1 → 0.1.3
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 +18 -6
- package/bin/jade.js +23 -0
- package/dist/commands.d.ts +20 -0
- package/dist/commands.js +20 -0
- package/dist/commands.js.map +1 -1
- package/dist/distro.d.ts +51 -7
- package/dist/distro.js +199 -48
- package/dist/distro.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js +479 -303
- package/dist/main.js.map +1 -1
- package/dist/mcp.js +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/plugin-cli.d.ts +1 -1
- package/dist/plugin-cli.js +79 -36
- package/dist/plugin-cli.js.map +1 -1
- package/dist/plugins/budget.d.ts +2 -2
- package/dist/plugins/budget.js +2 -2
- package/dist/plugins/budget.js.map +1 -1
- package/dist/plugins/builtins.js +6 -3
- package/dist/plugins/builtins.js.map +1 -1
- package/dist/plugins/checks.d.ts +2 -1
- package/dist/plugins/checks.js +25 -5
- package/dist/plugins/checks.js.map +1 -1
- package/dist/plugins/compact.js +45 -1
- package/dist/plugins/compact.js.map +1 -1
- package/dist/plugins/core.d.ts +5 -0
- package/dist/plugins/core.js +39 -0
- package/dist/plugins/core.js.map +1 -1
- package/dist/plugins/host.d.ts +52 -0
- package/dist/plugins/host.js +115 -0
- package/dist/plugins/host.js.map +1 -0
- package/dist/plugins/install.d.ts +1 -1
- package/dist/plugins/loader.d.ts +238 -10
- package/dist/plugins/loader.js +175 -21
- package/dist/plugins/loader.js.map +1 -1
- package/dist/plugins/policy.d.ts +23 -5
- package/dist/plugins/policy.js +63 -10
- package/dist/plugins/policy.js.map +1 -1
- package/dist/plugins/prompts.d.ts +14 -1
- package/dist/plugins/prompts.js +118 -16
- package/dist/plugins/prompts.js.map +1 -1
- package/dist/plugins/repeats.d.ts +7 -0
- package/dist/plugins/repeats.js +74 -13
- package/dist/plugins/repeats.js.map +1 -1
- package/dist/plugins/review.d.ts +18 -0
- package/dist/plugins/review.js +130 -0
- package/dist/plugins/review.js.map +1 -0
- package/dist/plugins/sessions.d.ts +7 -0
- package/dist/plugins/sessions.js +110 -0
- package/dist/plugins/sessions.js.map +1 -0
- package/dist/provider.d.ts +57 -3
- package/dist/provider.js +80 -10
- package/dist/provider.js.map +1 -1
- package/dist/session-lifecycle.d.ts +61 -0
- package/dist/session-lifecycle.js +167 -0
- package/dist/session-lifecycle.js.map +1 -0
- package/dist/session-scope.d.ts +53 -0
- package/dist/session-scope.js +116 -0
- package/dist/session-scope.js.map +1 -0
- package/dist/sessions.d.ts +6 -0
- package/dist/sessions.js +20 -0
- package/dist/sessions.js.map +1 -1
- package/dist/sys.d.ts +4 -1
- package/dist/sys.js +18 -1
- package/dist/sys.js.map +1 -1
- package/dist/tools/ask.d.ts +11 -0
- package/dist/tools/ask.js +62 -0
- package/dist/tools/ask.js.map +1 -0
- package/dist/tools/fs.d.ts +4 -0
- package/dist/tools/fs.js +53 -12
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/grep.js +3 -2
- package/dist/tools/grep.js.map +1 -1
- package/dist/tools/shell.js +5 -1
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/task.d.ts +10 -2
- package/dist/tools/task.js +40 -9
- package/dist/tools/task.js.map +1 -1
- package/dist/tools/web.d.ts +33 -3
- package/dist/tools/web.js +351 -143
- package/dist/tools/web.js.map +1 -1
- package/dist/ui/configure.js +144 -66
- package/dist/ui/configure.js.map +1 -1
- package/dist/ui/diff.d.ts +1 -1
- package/dist/ui/diff.js +1 -2
- package/dist/ui/diff.js.map +1 -1
- package/dist/ui/editor.d.ts +8 -0
- package/dist/ui/editor.js +50 -1
- package/dist/ui/editor.js.map +1 -1
- package/dist/ui/markdown.d.ts +22 -0
- package/dist/ui/markdown.js +242 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/render.d.ts +9 -8
- package/dist/ui/render.js +35 -62
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/renderers.d.ts +8 -0
- package/dist/ui/renderers.js +169 -0
- package/dist/ui/renderers.js.map +1 -0
- package/dist/ui/screen.d.ts +25 -2
- package/dist/ui/screen.js +104 -12
- package/dist/ui/screen.js.map +1 -1
- package/dist/ui/transcript.d.ts +13 -0
- package/dist/ui/transcript.js +157 -31
- package/dist/ui/transcript.js.map +1 -1
- package/dist/ui/tui.d.ts +2 -0
- package/dist/ui/tui.js +383 -81
- package/dist/ui/tui.js.map +1 -1
- package/dist/update.d.ts +53 -0
- package/dist/update.js +255 -0
- package/dist/update.js.map +1 -0
- package/package.json +7 -6
- package/src/commands.ts +24 -0
- package/src/distro.ts +234 -52
- package/src/index.ts +3 -0
- package/src/main.ts +459 -300
- package/src/mcp.ts +1 -1
- package/src/plugin-cli.ts +73 -34
- package/src/plugins/budget.ts +4 -4
- package/src/plugins/builtins.ts +6 -3
- package/src/plugins/checks.ts +25 -6
- package/src/plugins/compact.ts +40 -1
- package/src/plugins/core.ts +40 -0
- package/src/plugins/host.ts +146 -0
- package/src/plugins/install.ts +1 -1
- package/src/plugins/loader.ts +379 -23
- package/src/plugins/policy.ts +70 -13
- package/src/plugins/prompts.ts +128 -16
- package/src/plugins/repeats.ts +75 -14
- package/src/plugins/review.ts +146 -0
- package/src/plugins/sessions.ts +109 -0
- package/src/provider.ts +117 -14
- package/src/session-lifecycle.ts +194 -0
- package/src/session-scope.ts +135 -0
- package/src/sessions.ts +17 -0
- package/src/sys.ts +18 -1
- package/src/tools/ask.ts +72 -0
- package/src/tools/fs.ts +53 -12
- package/src/tools/grep.ts +3 -2
- package/src/tools/shell.ts +5 -1
- package/src/tools/task.ts +56 -12
- package/src/tools/web.ts +370 -140
- package/src/ui/configure.ts +141 -67
- package/src/ui/diff.ts +1 -2
- package/src/ui/editor.ts +50 -1
- package/src/ui/markdown.ts +258 -0
- package/src/ui/render.ts +35 -48
- package/src/ui/renderers.ts +183 -0
- package/src/ui/screen.ts +113 -16
- package/src/ui/transcript.ts +164 -35
- package/src/ui/tui.tsx +424 -79
- package/src/update.ts +262 -0
package/src/mcp.ts
CHANGED
|
@@ -627,7 +627,7 @@ export function mcpTools(clients: McpClient[], opts: { readOnly?: boolean } = {}
|
|
|
627
627
|
name,
|
|
628
628
|
description: `${(info.description ?? info.title ?? info.annotations?.title ?? info.name).trim()}\n\n(Tool "${info.name}" from the MCP server "${client.name}".)`,
|
|
629
629
|
inputSchema: { type: "object", ...schema },
|
|
630
|
-
// 服务没有声明只读的,都按有外部副作用处理:ask
|
|
630
|
+
// 服务没有声明只读的,都按有外部副作用处理:ask 模式下先问,auto 模式下先交给审查
|
|
631
631
|
effect: readOnly ? "read" : "external",
|
|
632
632
|
concurrency: readOnly ? "safe" : "exclusive",
|
|
633
633
|
idempotent: readOnly || info.annotations?.idempotentHint === true,
|
package/src/plugin-cli.ts
CHANGED
|
@@ -14,8 +14,8 @@ import {
|
|
|
14
14
|
updatePlugins,
|
|
15
15
|
type InstallRecord,
|
|
16
16
|
} from "./plugins/install.ts";
|
|
17
|
-
import { inspectPlugin, type PluginInfo } from "./plugins/loader.ts";
|
|
18
|
-
import { bold, dim, green, red, yellow } from "./ui/screen.ts";
|
|
17
|
+
import { inspectPlugin, pluginId, type PluginInfo } from "./plugins/loader.ts";
|
|
18
|
+
import { bold, dim, green, red, terminalSafe, yellow } from "./ui/screen.ts";
|
|
19
19
|
|
|
20
20
|
export const PLUGIN_HELP = `Usage: jade plugin <command>
|
|
21
21
|
|
|
@@ -31,8 +31,9 @@ export const PLUGIN_HELP = `Usage: jade plugin <command>
|
|
|
31
31
|
--force replace a plugin with the same name
|
|
32
32
|
update [name…] [--project] fetch again what jade plugin add installed (all of them when no name is given)
|
|
33
33
|
remove <name> [--project] delete an installed plugin
|
|
34
|
-
disable <name
|
|
35
|
-
|
|
34
|
+
disable <id|name> stop loading a plugin (by its id, or the name of its directory),
|
|
35
|
+
or a built-in plugin such as jade/web
|
|
36
|
+
enable <id|name> undo disable
|
|
36
37
|
|
|
37
38
|
Plugins are loaded when jade starts; restart running sessions to pick up changes.`;
|
|
38
39
|
|
|
@@ -49,8 +50,9 @@ export interface PluginCliOptions {
|
|
|
49
50
|
|
|
50
51
|
/** 返回退出码 */
|
|
51
52
|
export async function pluginCli(argv: string[], o: PluginCliOptions): Promise<number> {
|
|
52
|
-
|
|
53
|
-
const
|
|
53
|
+
// 插件的名字、说明来自插件目录(可能是刚下载的):只留颜色,去掉能搅乱终端的控制序列
|
|
54
|
+
const out = (t: string) => (o.out ?? console.log)(terminalSafe(t));
|
|
55
|
+
const err = (t: string) => (o.err ?? console.error)(terminalSafe(t));
|
|
54
56
|
const [sub, ...rest] = argv;
|
|
55
57
|
const flags = { project: false, link: false, force: false } as { project: boolean; link: boolean; force: boolean; name?: string; path?: string };
|
|
56
58
|
const args: string[] = [];
|
|
@@ -93,7 +95,7 @@ export async function pluginCli(argv: string[], o: PluginCliOptions): Promise<nu
|
|
|
93
95
|
if (args.length !== 1) throw new UsageError("usage: jade plugin add <source> [--project] [--name <name>] [--path <dir>] [--link] [--force]");
|
|
94
96
|
const r = await addPlugin(args[0]!, { pluginsDir: dir, cwd: o.cwd, name: flags.name, subdir: flags.path, link: flags.link, force: flags.force, log });
|
|
95
97
|
if (flags.project && r.info.kind === "module") await trustProject(r.dir, o.trust);
|
|
96
|
-
out(green(`Installed ${bold(r.name)}${r.info.version ? ` ${r.info.version}` : ""} into ${r.dir}`) + dim(` (${origin(r.record)})`));
|
|
98
|
+
out(green(`Installed ${bold(pluginId({ name: r.name, dir: r.dir, layer: "user" }, r.info))}${r.info.version ? ` ${r.info.version}` : ""} into ${r.dir}`) + dim(` (${origin(r.record)})`));
|
|
97
99
|
for (const line of await provides(r.dir, r.info)) out(` ${line}`);
|
|
98
100
|
if (r.info.bundle.unsupported.length) out(yellow(` jade does not support the plugin's ${r.info.bundle.unsupported.join(" and ")} yet; they are ignored`));
|
|
99
101
|
if (flags.project) out(dim(" Commit .jade/plugins to share it; others confirm it the first time jade loads it."));
|
|
@@ -127,24 +129,37 @@ export async function pluginCli(argv: string[], o: PluginCliOptions): Promise<nu
|
|
|
127
129
|
case "rm":
|
|
128
130
|
case "uninstall": {
|
|
129
131
|
if (args.length !== 1) throw new UsageError("usage: jade plugin remove <name> [--project]");
|
|
132
|
+
// 停用记录按 id:删掉之前读出来
|
|
133
|
+
const info = await inspectPlugin(path.join(dir, args[0]!)).catch(() => undefined);
|
|
130
134
|
if (!(await removePlugin(args[0]!, dir))) {
|
|
131
135
|
err(red(`plugin ${args[0]} is not installed in ${dir}`));
|
|
132
136
|
return 1;
|
|
133
137
|
}
|
|
134
|
-
await setDisabled(o.home, args[0]!, false);
|
|
138
|
+
if (info) await setDisabled(o.home, pluginId({ name: args[0]!, dir, layer: "user" }, info), false);
|
|
135
139
|
out(green(`Removed ${args[0]}.`));
|
|
136
140
|
return 0;
|
|
137
141
|
}
|
|
138
142
|
case "disable":
|
|
139
143
|
case "enable": {
|
|
140
|
-
if (args.length !== 1) throw new UsageError(`usage: jade plugin ${sub} <name
|
|
141
|
-
const
|
|
142
|
-
if (sub === "disable" && REQUIRED_IDS.has(
|
|
143
|
-
err(red(`${
|
|
144
|
+
if (args.length !== 1) throw new UsageError(`usage: jade plugin ${sub} <id|name>`);
|
|
145
|
+
const arg = args[0]!;
|
|
146
|
+
if (sub === "disable" && REQUIRED_IDS.has(arg)) {
|
|
147
|
+
err(red(`${arg} cannot be disabled: it decides which tool calls need your approval. Plugins can only make it stricter, with gate hooks.`));
|
|
144
148
|
return 1;
|
|
145
149
|
}
|
|
146
|
-
|
|
147
|
-
|
|
150
|
+
// plugins.json 只记 id:给的是目录名时换成那个插件的 id
|
|
151
|
+
let name = arg;
|
|
152
|
+
if (!BUILTIN_IDS.has(arg)) {
|
|
153
|
+
const installed = await installedPlugins(o);
|
|
154
|
+
const byId = installed.find((p) => p.id === arg);
|
|
155
|
+
const byDir = installed.filter((p) => p.name === arg && p.id !== undefined);
|
|
156
|
+
if (!byId && byDir.length === 1) name = byDir[0]!.id!;
|
|
157
|
+
else if (!byId && byDir.length > 1) {
|
|
158
|
+
err(red(`several plugins are in a directory called ${arg} (${byDir.map((p) => p.id).join(", ")}); give the id instead`));
|
|
159
|
+
return 1;
|
|
160
|
+
} else if (!byId && sub === "disable") {
|
|
161
|
+
err(yellow(`note: no built-in or installed plugin has the id ${arg}; it is disabled anyway, in case a plugin with that id shows up`));
|
|
162
|
+
}
|
|
148
163
|
}
|
|
149
164
|
const changed = await setDisabled(o.home, name, sub === "disable");
|
|
150
165
|
out(changed ? green(`${name} ${sub}d.`) + dim(" Restart jade to apply it.") : dim(`${name} is already ${sub}d.`));
|
|
@@ -160,26 +175,15 @@ export class UsageError extends Error {}
|
|
|
160
175
|
async function list(o: PluginCliOptions, out: (t: string) => void) {
|
|
161
176
|
const { disabled } = await readPluginSettings(o.home);
|
|
162
177
|
const off = new Set(disabled);
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
for (const b of BUILTINS) {
|
|
166
|
-
const state = b.required ? dim(" required") : off.has(b.id) ? yellow(" disabled") : "";
|
|
167
|
-
out(` ${b.id.padEnd(width)}${dim(b.about)}${state}`);
|
|
168
|
-
}
|
|
178
|
+
// 先读清单(不执行代码):内置插件那一栏要显示被谁代替
|
|
179
|
+
const layers = [];
|
|
169
180
|
for (const [title, dir] of [
|
|
170
181
|
["User", path.join(o.home, "plugins")],
|
|
171
182
|
["Project", path.join(o.root, ".jade", "plugins")],
|
|
172
183
|
] as const) {
|
|
173
|
-
const names = await installedNames(dir);
|
|
174
|
-
out("");
|
|
175
|
-
out(bold(title) + dim(` ${dir}`));
|
|
176
|
-
if (names.length === 0) {
|
|
177
|
-
out(dim(" (none)"));
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
184
|
const records = await readRecords(dir);
|
|
181
|
-
|
|
182
|
-
|
|
185
|
+
const entries = [];
|
|
186
|
+
for (const name of await installedNames(dir)) {
|
|
183
187
|
let info: PluginInfo | undefined;
|
|
184
188
|
let problem: string | undefined;
|
|
185
189
|
try {
|
|
@@ -187,9 +191,36 @@ async function list(o: PluginCliOptions, out: (t: string) => void) {
|
|
|
187
191
|
} catch (e) {
|
|
188
192
|
problem = e instanceof Error ? e.message : String(e);
|
|
189
193
|
}
|
|
194
|
+
const id = info ? pluginId({ name, dir, layer: "user" }, info) : undefined;
|
|
195
|
+
entries.push({ name, info, problem, id, rec: records[name] });
|
|
196
|
+
}
|
|
197
|
+
layers.push({ title, dir, entries });
|
|
198
|
+
}
|
|
199
|
+
const replacedBy = new Map<string, string>();
|
|
200
|
+
for (const e of layers.flatMap((l) => l.entries)) {
|
|
201
|
+
if (e.id === undefined || off.has(e.id)) continue;
|
|
202
|
+
for (const b of e.info?.replaces ?? []) if (!replacedBy.has(b)) replacedBy.set(b, e.id);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
out(bold("Built-in") + dim(" (jade plugin disable <id> turns one off)"));
|
|
206
|
+
const width = Math.max(...BUILTINS.map((b) => b.id.length)) + 2;
|
|
207
|
+
for (const b of BUILTINS) {
|
|
208
|
+
const by = replacedBy.get(b.id);
|
|
209
|
+
const state = b.required ? dim(" required") : off.has(b.id) ? yellow(" disabled") : by ? yellow(` replaced by ${by}`) : "";
|
|
210
|
+
out(` ${b.id.padEnd(width)}${dim(b.about)}${state}`);
|
|
211
|
+
}
|
|
212
|
+
for (const { title, dir, entries } of layers) {
|
|
213
|
+
out("");
|
|
214
|
+
out(bold(title) + dim(` ${dir}`));
|
|
215
|
+
if (entries.length === 0) {
|
|
216
|
+
out(dim(" (none)"));
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
for (const { name, info, problem, id, rec } of entries) {
|
|
190
220
|
const version = info?.version ?? rec?.version;
|
|
191
|
-
const state = off.has(
|
|
192
|
-
|
|
221
|
+
const state = id !== undefined && off.has(id) ? yellow(" disabled") : problem ? red(` broken: ${problem}`) : "";
|
|
222
|
+
const where = id === name ? "" : dim(` (${name}/)`);
|
|
223
|
+
out(` ${bold(id ?? name)}${where}${version ? ` ${version}` : ""}${info ? dim(` ${info.kind}`) : ""}${rec ? dim(` · ${origin(rec)}`) : dim(" · copied by hand")}${state}`);
|
|
193
224
|
if (info?.description) out(dim(` ${info.description}`));
|
|
194
225
|
if (info) for (const line of await provides(path.join(dir, name), info)) out(dim(` ${line}`));
|
|
195
226
|
}
|
|
@@ -207,6 +238,7 @@ async function provides(dir: string, info: PluginInfo): Promise<string[]> {
|
|
|
207
238
|
const lines: string[] = [];
|
|
208
239
|
if (info.kind === "module") lines.push(`code: ${info.entry}`);
|
|
209
240
|
if (info.kind === "process") lines.push(`process: ${[info.json.command, ...(info.json.args ?? [])].join(" ")}`);
|
|
241
|
+
if (info.replaces?.length) lines.push(`replaces: ${info.replaces.join(", ")}`);
|
|
210
242
|
const skills: string[] = [];
|
|
211
243
|
for (const d of info.bundle.skills) {
|
|
212
244
|
for (const n of await readdir(d).catch(() => [] as string[])) {
|
|
@@ -226,9 +258,16 @@ async function provides(dir: string, info: PluginInfo): Promise<string[]> {
|
|
|
226
258
|
return lines;
|
|
227
259
|
}
|
|
228
260
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
261
|
+
/** 用户层和项目层装了的插件:目录名和 id(清单读不了的没有 id) */
|
|
262
|
+
async function installedPlugins(o: PluginCliOptions): Promise<{ name: string; id?: string }[]> {
|
|
263
|
+
const out: { name: string; id?: string }[] = [];
|
|
264
|
+
for (const dir of [path.join(o.home, "plugins"), path.join(o.root, ".jade", "plugins")]) {
|
|
265
|
+
for (const name of await installedNames(dir)) {
|
|
266
|
+
const info = await inspectPlugin(path.join(dir, name)).catch(() => undefined);
|
|
267
|
+
out.push({ name, id: info && pluginId({ name, dir, layer: "user" }, info) });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return out;
|
|
232
271
|
}
|
|
233
272
|
|
|
234
273
|
/** 自己用 --project 装的插件不用在启动时再确认一次(别人拉取仓库后第一次加载时仍要确认) */
|
package/src/plugins/budget.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
import type { Plugin, State } from "uncarved";
|
|
4
4
|
|
|
5
5
|
export interface BudgetOptions {
|
|
6
|
-
/** 一轮最多调用模型的次数,默认 100 */
|
|
7
|
-
maxSteps?: number;
|
|
6
|
+
/** 一轮最多调用模型的次数,默认 100;传函数时每次判断时取值 */
|
|
7
|
+
maxSteps?: number | (() => number);
|
|
8
8
|
id?: string;
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -20,7 +20,7 @@ export function stepsThisTurn(s: State): number {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function budget(opts: BudgetOptions = {}): Plugin {
|
|
23
|
-
const max = opts.maxSteps ?? 100;
|
|
23
|
+
const max = typeof opts.maxSteps === "function" ? opts.maxSteps : () => (opts.maxSteps as number | undefined) ?? 100;
|
|
24
24
|
return {
|
|
25
25
|
id: opts.id ?? "jade/budget",
|
|
26
26
|
version: "0.1.0",
|
|
@@ -30,7 +30,7 @@ export function budget(opts: BudgetOptions = {}): Plugin {
|
|
|
30
30
|
{
|
|
31
31
|
kind: "gate",
|
|
32
32
|
point: "turn.continue",
|
|
33
|
-
fn: (s) => (stepsThisTurn(s) >= max ? { decision: "deny" } : { decision: "abstain" }),
|
|
33
|
+
fn: (s) => (stepsThisTurn(s) >= max() ? { decision: "deny" } : { decision: "abstain" }),
|
|
34
34
|
},
|
|
35
35
|
],
|
|
36
36
|
};
|
package/src/plugins/builtins.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade 自带的插件。内核之外的功能都以插件的形式提供,和第三方插件用同一套接口:
|
|
2
2
|
// 每个都可以在 $JADE_HOME/plugins.json 里停用(jade plugin disable <id>),
|
|
3
|
-
//
|
|
3
|
+
// 或者由第三方插件在 plugin.json 里用 replaces 代替。jade/policy 负责审批,不能停用或代替,第三方插件只能用 gate hook 让它更严格。
|
|
4
4
|
|
|
5
5
|
export interface BuiltinInfo {
|
|
6
6
|
id: string;
|
|
@@ -15,8 +15,9 @@ export const BUILTINS: readonly BuiltinInfo[] = [
|
|
|
15
15
|
{ id: "jade/prompts", about: "system prompt, environment, AGENTS.md / CLAUDE.md instructions" },
|
|
16
16
|
{ id: "jade/fs", about: "read_file, write_file, edit_file, multi_edit, list_dir, glob, grep" },
|
|
17
17
|
{ id: "jade/shell", about: "bash, bash_output, kill_shell" },
|
|
18
|
-
{ id: "jade/web", about: "web_fetch, web_search" },
|
|
18
|
+
{ id: "jade/web", about: "web_fetch, web_search, /search" },
|
|
19
19
|
{ id: "jade/todo", about: "todo_write" },
|
|
20
|
+
{ id: "jade/ask", about: "ask_user (the agent asks you clarifying questions)" },
|
|
20
21
|
{ id: "jade/memory", about: "memory tool, memories in the system prompt, /memory" },
|
|
21
22
|
{ id: "jade/task", about: "task tool (read-only sub-agents)" },
|
|
22
23
|
{ id: "jade/skills", about: "skill tool, skill list in the system prompt, /skills" },
|
|
@@ -24,7 +25,9 @@ export const BUILTINS: readonly BuiltinInfo[] = [
|
|
|
24
25
|
{ id: "jade/checks", about: "project checks after edits, /checks" },
|
|
25
26
|
{ id: "jade/compact", about: "compacts the history near the context limit, /compact" },
|
|
26
27
|
{ id: "jade/budget", about: "limits the steps of a turn" },
|
|
27
|
-
{ id: "jade/
|
|
28
|
+
{ id: "jade/sessions", about: "/clear, /resume, /retry" },
|
|
29
|
+
{ id: "jade/undo", about: "/undo (restores the files changed in the last turn)" },
|
|
30
|
+
{ id: "jade/repeats", about: "rejects calls repeated in one response and re-opening an unchanged file, warns about repeated failures and read-backs" },
|
|
28
31
|
];
|
|
29
32
|
|
|
30
33
|
export const BUILTIN_IDS: ReadonlySet<string> = new Set(BUILTINS.map((b) => b.id));
|
package/src/plugins/checks.ts
CHANGED
|
@@ -20,7 +20,11 @@ import type { Block, HookContext, Message, Plugin } from "uncarved";
|
|
|
20
20
|
import type { RunCommand } from "../tools/shell.ts";
|
|
21
21
|
import { which as findExecutable } from "../sys.ts";
|
|
22
22
|
|
|
23
|
+
/** 老版本把整个 ChecksState 记在这一个 key 下、每次检查整份重记;恢复老会话时还要读它 */
|
|
23
24
|
export const CHECKS_KEY = "jade/checks";
|
|
25
|
+
/** 每份报告单独一个 key:每次检查日志里只多记这一份,不随会话变长而整份重记 */
|
|
26
|
+
const RESULT_PREFIX = `${CHECKS_KEY}/result/`;
|
|
27
|
+
const OTHERS_KEY = `${CHECKS_KEY}/others`;
|
|
24
28
|
|
|
25
29
|
/** 一个具体的检查:在 cwd 里执行 command;command 里的 {files} 换成改过的文件,{dirs} 换成它们所在的目录 */
|
|
26
30
|
export interface Check {
|
|
@@ -41,7 +45,7 @@ export interface CheckConfigFile {
|
|
|
41
45
|
checks?: { name: string; command: string; extensions?: string[]; cwd?: string; timeout_ms?: number }[];
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
/** derived
|
|
48
|
+
/** 从 derived 里还原的状态 */
|
|
45
49
|
export interface ChecksState {
|
|
46
50
|
/** 一批修改的最后一个调用 → 附在它结果后面的文字;空串表示检查过、没有要说的 */
|
|
47
51
|
results: Record<string, string>;
|
|
@@ -49,6 +53,13 @@ export interface ChecksState {
|
|
|
49
53
|
others: Record<string, number>;
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
function stateOf(derived: Readonly<Record<string, unknown>>): ChecksState {
|
|
57
|
+
const legacy = derived[CHECKS_KEY] as ChecksState | undefined;
|
|
58
|
+
const results: Record<string, string> = { ...legacy?.results };
|
|
59
|
+
for (const [k, v] of Object.entries(derived)) if (k.startsWith(RESULT_PREFIX)) results[k.slice(RESULT_PREFIX.length)] = v as string;
|
|
60
|
+
return { results, others: (derived[OTHERS_KEY] as Record<string, number> | undefined) ?? legacy?.others ?? {} };
|
|
61
|
+
}
|
|
62
|
+
|
|
52
63
|
/** 宿主确认一条检查命令能否运行;同意后由宿主记住 */
|
|
53
64
|
export type ApproveCheck = (check: Check, signal: AbortSignal) => Promise<boolean>;
|
|
54
65
|
|
|
@@ -83,6 +94,8 @@ const DEFAULT_TIMEOUT_MS = 120_000;
|
|
|
83
94
|
const MAX_REPORT_CHARS = 6000;
|
|
84
95
|
/** 每个检查最多列出的问题数 */
|
|
85
96
|
const MAX_PROBLEMS = 30;
|
|
97
|
+
/** 第一次运行时列出的其他文件里的问题条数 */
|
|
98
|
+
const FIRST_RUN_OTHERS = 10;
|
|
86
99
|
const HEADER = "[Checks jade ran after your edits; this is tool output, not a message from the user]";
|
|
87
100
|
|
|
88
101
|
const TS_EXT = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
|
|
@@ -303,7 +316,12 @@ export function checks(opts: ChecksOptions): Checks {
|
|
|
303
316
|
lines.push(`${check.name}: ${elsewhere.length - before} more problem${elsewhere.length - before > 1 ? "s" : ""} in other files than before your edits (${elsewhere.length} in total), possibly caused by them:`);
|
|
304
317
|
lines.push(...elsewhere.slice(0, 10).map((p) => p.text));
|
|
305
318
|
} else if (before === undefined && elsewhere.length > 0) {
|
|
306
|
-
|
|
319
|
+
// 本会话第一次运行,没有基线:列出几条让模型自己判断,改了函数签名弄坏的调用方也在这里
|
|
320
|
+
lines.push(
|
|
321
|
+
`${check.name}: also ${elsewhere.length} problem${elsewhere.length > 1 ? "s" : ""} in other files. This is the first check in this session, so jade cannot tell which predate your edits; check whether your changes caused any of these:`,
|
|
322
|
+
);
|
|
323
|
+
lines.push(...elsewhere.slice(0, FIRST_RUN_OTHERS).map((p) => p.text));
|
|
324
|
+
if (elsewhere.length > FIRST_RUN_OTHERS) lines.push(`… ${elsewhere.length - FIRST_RUN_OTHERS} more`);
|
|
307
325
|
}
|
|
308
326
|
parts.push(lines.join("\n"));
|
|
309
327
|
summary.push({ name: check.name, problems: mine.length, others: before !== undefined && elsewhere.length > before ? elsewhere.length - before : undefined });
|
|
@@ -368,16 +386,17 @@ export function checks(opts: ChecksOptions): Checks {
|
|
|
368
386
|
timeoutMs: 900_000,
|
|
369
387
|
optional: true,
|
|
370
388
|
fn: async (messages, ctx) => {
|
|
371
|
-
const state = (ctx.derived
|
|
389
|
+
const state = stateOf(ctx.derived);
|
|
372
390
|
const batch = self.enabled ? lastBatch(messages) : undefined;
|
|
373
391
|
const last = batch?.ids.at(-1);
|
|
374
392
|
if (batch && last && !(last in state.results)) {
|
|
375
393
|
const { text, summary, others } = await runAll(batch.files, state, ctx);
|
|
376
394
|
if (summary.length > 0 || text) {
|
|
377
|
-
|
|
378
|
-
|
|
395
|
+
await ctx.record(`${RESULT_PREFIX}${last}`, text);
|
|
396
|
+
// others 是 state.others 加上这次的数,有变化才记
|
|
397
|
+
if (Object.keys(others).some((k) => others[k] !== state.others[k])) await ctx.record(OTHERS_KEY, others);
|
|
379
398
|
await ctx.emit("checked", { toolUseId: last, files: batch.files, summary, report: text });
|
|
380
|
-
return inject(messages,
|
|
399
|
+
return inject(messages, { ...state.results, [last]: text });
|
|
381
400
|
}
|
|
382
401
|
}
|
|
383
402
|
return inject(messages, state.results);
|
package/src/plugins/compact.ts
CHANGED
|
@@ -182,6 +182,8 @@ export function compactor(opts: CompactOptions): Compactor {
|
|
|
182
182
|
// 最新的任务清单在被压缩的部分里时,原样带上,模型不必凭摘要重建
|
|
183
183
|
const todos = latestTodos(messages.slice(cut)) ? undefined : latestTodos(messages.slice(0, cut));
|
|
184
184
|
if (todos?.length) summary += `\n\nTodo list at the time of compaction:\n${renderTodos(todos)}`;
|
|
185
|
+
// 用户的原话和改过的文件也原样带上:只靠总结,压缩几轮后要求和约束容易走样
|
|
186
|
+
summary += appendix(messages.slice(0, cut));
|
|
185
187
|
|
|
186
188
|
// 保留部分里已经清理过的输出继续保持清理,否则投影反而变大
|
|
187
189
|
const next: CompactState = { upTo: cut, summary, ...(prev?.trimUpTo && prev.trimUpTo > cut ? { trimUpTo: prev.trimUpTo } : {}) };
|
|
@@ -197,7 +199,7 @@ export function compactor(opts: CompactOptions): Compactor {
|
|
|
197
199
|
},
|
|
198
200
|
plugin: {
|
|
199
201
|
id,
|
|
200
|
-
version: "0.2.
|
|
202
|
+
version: "0.2.1",
|
|
201
203
|
kernel: "^0.1",
|
|
202
204
|
needs: ["transform:context.project", "observe:event", "log:record", "log:emit"],
|
|
203
205
|
hooks: [
|
|
@@ -240,6 +242,43 @@ function cutPoint(messages: Message[], from: number, keep: number): number | und
|
|
|
240
242
|
return best;
|
|
241
243
|
}
|
|
242
244
|
|
|
245
|
+
const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
246
|
+
/** 摘要后面附的用户原话的总长度上限 */
|
|
247
|
+
const MAX_USER_CHARS = 6000;
|
|
248
|
+
const MAX_USER_MESSAGE_CHARS = 1500;
|
|
249
|
+
|
|
250
|
+
/** 被压缩部分里用户的原话(第一条和最近的几条)和成功修改过的文件 */
|
|
251
|
+
function appendix(messages: Message[]): string {
|
|
252
|
+
const said: string[] = [];
|
|
253
|
+
const ok = new Set<string>();
|
|
254
|
+
const files: string[] = [];
|
|
255
|
+
for (const m of messages) {
|
|
256
|
+
for (const b of m.content) if (b.kind === "tool_result" && !b.isError) ok.add(b.toolUseId);
|
|
257
|
+
}
|
|
258
|
+
for (const m of messages) {
|
|
259
|
+
const text = m.content.flatMap((b) => (b.kind === "text" && b.origin.by === "user" ? [b.text.trim()] : [])).join("\n");
|
|
260
|
+
if (text) said.push(text.length > MAX_USER_MESSAGE_CHARS ? `${text.slice(0, MAX_USER_MESSAGE_CHARS)}…` : text);
|
|
261
|
+
for (const b of m.content) {
|
|
262
|
+
if (b.kind !== "tool_use" || !WRITE_TOOLS.has(b.name) || !ok.has(b.id)) continue;
|
|
263
|
+
const p = (b.input as { path?: unknown } | null)?.path;
|
|
264
|
+
if (typeof p === "string" && !files.includes(p)) files.push(p);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// 超出上限时保留第一条(通常是任务本身)和尽量多的最近几条
|
|
268
|
+
const kept: string[] = [];
|
|
269
|
+
let size = said[0]?.length ?? 0;
|
|
270
|
+
for (let i = said.length - 1; i > 0 && size + said[i]!.length <= MAX_USER_CHARS; i--) {
|
|
271
|
+
kept.unshift(said[i]!);
|
|
272
|
+
size += said[i]!.length;
|
|
273
|
+
}
|
|
274
|
+
const skipped = said.length - 1 - kept.length;
|
|
275
|
+
const quoted = said.length ? [said[0]!, ...(skipped > 0 ? [`(${skipped} more message${skipped > 1 ? "s" : ""} omitted)`] : []), ...kept] : [];
|
|
276
|
+
let out = "";
|
|
277
|
+
if (quoted.length) out += `\n\nThe user's messages in the compacted part, verbatim:\n${quoted.map((t) => `> ${t.replaceAll("\n", "\n> ")}`).join("\n")}`;
|
|
278
|
+
if (files.length) out += `\n\nFiles changed in the compacted part:\n${files.map((f) => `- ${f}`).join("\n")}`;
|
|
279
|
+
return `${out}\n\nFile contents from the compacted part are no longer in context: read a file again before editing it.`;
|
|
280
|
+
}
|
|
281
|
+
|
|
243
282
|
function transcript(messages: Message[]): string {
|
|
244
283
|
const clip = (s: string, n = 2000) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
245
284
|
const lines: string[] = [];
|
package/src/plugins/core.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { MEMORY_SCOPES, type Memory, type MemoryScope } from "../memory.ts";
|
|
|
11
11
|
import { describeServer, type McpClient } from "../mcp.ts";
|
|
12
12
|
import type { Skill } from "../skills.ts";
|
|
13
13
|
import { display } from "../tools/fs.ts";
|
|
14
|
+
import { SEARCH_PROVIDERS } from "../tools/web.ts";
|
|
15
|
+
import { updateEnvFile } from "../config.ts";
|
|
14
16
|
import { clip } from "../ui/render.ts";
|
|
15
17
|
import { bold, dim, green, red, yellow } from "../ui/screen.ts";
|
|
16
18
|
|
|
@@ -215,3 +217,41 @@ export function compactCommand(compact: Compactor): PluginCommand {
|
|
|
215
217
|
},
|
|
216
218
|
};
|
|
217
219
|
}
|
|
220
|
+
|
|
221
|
+
// ---------- /search ----------
|
|
222
|
+
|
|
223
|
+
/** /search:设置或去掉某个搜索服务的 API key(写进 $JADE_HOME/.env),用 session.reload 让当前会话立即用上 */
|
|
224
|
+
export function searchCommand(opts: { envFile: string; env?: Record<string, string | undefined> }): PluginCommand {
|
|
225
|
+
return {
|
|
226
|
+
name: "search",
|
|
227
|
+
description: "set up web search services (TinyFish, AnySearch, Tavily, Exa, Firecrawl, Jina)",
|
|
228
|
+
async run(_args, { ui, session }) {
|
|
229
|
+
if (!session) throw new Error("/search needs session control, which this host does not provide here");
|
|
230
|
+
const env = opts.env ?? process.env;
|
|
231
|
+
const on = SEARCH_PROVIDERS.filter((p) => env[p.env]);
|
|
232
|
+
const name = await ui.select(
|
|
233
|
+
on.length ? `Web search is on (${on.map((p) => p.name).join(", ")}; tried in order, the next one takes over on failure)` : "Web search is off",
|
|
234
|
+
SEARCH_PROVIDERS.map((p) => ({ label: `${p.name}${env[p.env] ? " ✓" : ""}`, value: p.name, hint: p.signup })),
|
|
235
|
+
);
|
|
236
|
+
const p = SEARCH_PROVIDERS.find((x) => x.name === name);
|
|
237
|
+
if (!p) return;
|
|
238
|
+
if (env[p.env]) {
|
|
239
|
+
const choice = await ui.select(p.name, [
|
|
240
|
+
{ label: "Change the API key…", value: "set" as const },
|
|
241
|
+
{ label: "Remove", value: "off" as const, hint: `removes ${p.env} from ${opts.envFile}` },
|
|
242
|
+
]);
|
|
243
|
+
if (!choice) return;
|
|
244
|
+
if (choice === "off") {
|
|
245
|
+
await updateEnvFile(opts.envFile, { [p.env]: undefined }, env);
|
|
246
|
+
await session.reload();
|
|
247
|
+
return dim(`${p.name} removed.${on.length > 1 ? "" : " Web search is off."}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const key = (await ui.input(`${p.name} API key`, { mask: true }))?.trim();
|
|
251
|
+
if (!key) return;
|
|
252
|
+
await updateEnvFile(opts.envFile, { [p.env]: key }, env);
|
|
253
|
+
await session.reload();
|
|
254
|
+
return green(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// 进程级的插件管理(PluginHost):发现、读清单、加载、注册模型服务、退出时结束进程外插件。
|
|
2
|
+
//
|
|
3
|
+
// 整个 jade 进程只有一个。进程级的东西(插件模块、进程外插件的进程、模型服务)只在这里创建、只在这里销毁;
|
|
4
|
+
// 会话级的插件实例由 createJade 每个会话创建一套。见 docs/design/jade-plugin-lifecycle.md 第 3.1 节。
|
|
5
|
+
|
|
6
|
+
import { UntrustedPlugin, type TrustStore } from "uncarved";
|
|
7
|
+
import { PROVIDER_KIND, registerProviders, SETTING_ENV, type ProviderSetting } from "../provider.ts";
|
|
8
|
+
import { discoverPlugins, loadPlugin, resolvePlugins, type ConfirmPlugin, type LoadedPlugin, type PluginBundle, type PluginLayer, type PluginProvider, type ResolvedPlugin } from "./loader.ts";
|
|
9
|
+
|
|
10
|
+
export interface PluginHostOptions {
|
|
11
|
+
/** $JADE_HOME */
|
|
12
|
+
home: string;
|
|
13
|
+
/** 仓库根目录和工作目录(都已解析符号链接) */
|
|
14
|
+
root: string;
|
|
15
|
+
cwd: string;
|
|
16
|
+
/** 停用的插件 id($JADE_HOME/plugins.json) */
|
|
17
|
+
disabled: ReadonlySet<string>;
|
|
18
|
+
trust: TrustStore;
|
|
19
|
+
/** 项目层插件新出现或内容变化时请用户确认 */
|
|
20
|
+
confirm: ConfirmPlugin;
|
|
21
|
+
/** 要告诉用户的事:skipped 没加载的插件,note 提示,warning 问题 */
|
|
22
|
+
report(kind: "skipped" | "note" | "warning", text: string): void;
|
|
23
|
+
/** 显示插件目录,默认原样 */
|
|
24
|
+
display?(dir: string): string;
|
|
25
|
+
/** 项目层插件没确认而跳过时,附在原因后面的话(例如非交互运行时教用户怎么确认) */
|
|
26
|
+
untrustedHint?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PluginHost {
|
|
30
|
+
/** 读过清单、没停用的插件,还没执行代码。它们带的 skills、命令、MCP 配置从这里取,不用等加载 */
|
|
31
|
+
readonly plugins: readonly ResolvedPlugin[];
|
|
32
|
+
/** 插件带的某种内容的目录,标上是哪个插件、哪一层的 */
|
|
33
|
+
bundled(pick: (b: PluginBundle) => string[]): { plugin: string; dir: string; layer: PluginLayer }[];
|
|
34
|
+
/**
|
|
35
|
+
* 依次加载插件(项目层的先请用户确认),然后注册它们导出的模型服务。只调用一次;
|
|
36
|
+
* 加载失败的跳过并报告,返回加载成功的
|
|
37
|
+
*/
|
|
38
|
+
load(): Promise<LoadedPlugin[]>;
|
|
39
|
+
/** 加载成功的插件;load 完成之前是空的 */
|
|
40
|
+
readonly loaded: readonly LoadedPlugin[];
|
|
41
|
+
/** 结束进程外插件;之后它们退出不再报告。可以多次调用 */
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 发现插件并读清单(不执行代码);按 id 停用的、id 重复的在这里去掉 */
|
|
46
|
+
export async function openPluginHost(opts: PluginHostOptions): Promise<PluginHost> {
|
|
47
|
+
const show = opts.display ?? ((dir: string) => dir);
|
|
48
|
+
const errorText = (err: unknown) => (err instanceof Error ? err.message : String(err));
|
|
49
|
+
const resolved = await resolvePlugins(await discoverPlugins({ home: opts.home, root: opts.root, cwd: opts.cwd }), opts.disabled);
|
|
50
|
+
const loaded: LoadedPlugin[] = [];
|
|
51
|
+
let loading: Promise<LoadedPlugin[]> | undefined;
|
|
52
|
+
let closing = false;
|
|
53
|
+
|
|
54
|
+
async function load(): Promise<LoadedPlugin[]> {
|
|
55
|
+
for (const { found, error } of resolved.broken) opts.report("skipped", `plugin ${show(found.dir)}: ${errorText(error)}`);
|
|
56
|
+
for (const w of resolved.warnings) opts.report("skipped", w);
|
|
57
|
+
for (const p of resolved.plugins) {
|
|
58
|
+
if (p.info.bundle.unsupported.length) opts.report("note", `plugin ${p.id}: jade does not support its ${p.info.bundle.unsupported.join(" and ")} yet; they are ignored`);
|
|
59
|
+
try {
|
|
60
|
+
loaded.push(
|
|
61
|
+
await loadPlugin(p, {
|
|
62
|
+
trust: opts.trust,
|
|
63
|
+
confirm: opts.confirm,
|
|
64
|
+
onExit: (plugin, { code, signal }) => {
|
|
65
|
+
if (!closing) opts.report("warning", `plugin ${plugin.id} exited (${signal ?? `code ${code}`}); its hooks fail until jade restarts. /plugins shows its stderr`);
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
const hint = p.layer === "project" && err instanceof UntrustedPlugin && opts.untrustedHint ? ` ${opts.untrustedHint}` : "";
|
|
71
|
+
opts.report("skipped", `plugin ${show(p.dir)}: ${errorText(err)}${hint}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// 模型服务是插件模块导出的,不属于哪套插件实例:加载完注册一次,所有会话共用
|
|
75
|
+
const collected = collectProviders(loaded);
|
|
76
|
+
for (const w of [...collected.warnings, ...registerProviders(collected.providers)]) opts.report("warning", w);
|
|
77
|
+
return loaded;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
plugins: resolved.plugins,
|
|
82
|
+
bundled: (pick) => resolved.plugins.flatMap((p) => pick(p.info.bundle).map((dir) => ({ plugin: p.id, dir, layer: p.layer }))),
|
|
83
|
+
load: () => (loading ??= load()),
|
|
84
|
+
loaded,
|
|
85
|
+
async close() {
|
|
86
|
+
closing = true;
|
|
87
|
+
await Promise.all(loaded.map((p) => p.close?.().catch(() => {})));
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 插件模块导出的模型服务(LoadedPlugin.providers):检查后返回要注册的服务和要告诉用户的问题。
|
|
94
|
+
* 只认用户层插件:服务能看到全部对话和 API key。不用创建插件实例,所以宿主可以先注册服务、配置好模型,再创建插件
|
|
95
|
+
*/
|
|
96
|
+
export function collectProviders(sources: readonly LoadedPlugin[]): { providers: { plugin: string; def: PluginProvider }[]; warnings: string[] } {
|
|
97
|
+
const warnings: string[] = [];
|
|
98
|
+
const providers = sources.flatMap((source) => {
|
|
99
|
+
if (source.providers === undefined) return [];
|
|
100
|
+
const where = `plugin ${source.id} (${source.layer})`;
|
|
101
|
+
if (source.layer === "project") {
|
|
102
|
+
warnings.push(`${where}: providers are ignored: only user plugins can provide model services`);
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
if (!Array.isArray(source.providers)) {
|
|
106
|
+
warnings.push(`${where}: providers is ignored: it must be an array`);
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
return (source.providers as PluginProvider[]).flatMap((d) => {
|
|
110
|
+
const ok = d && typeof d.kind === "string" && PROVIDER_KIND.test(d.kind) && typeof d.model === "function" && typeof d.create === "function";
|
|
111
|
+
if (!ok) {
|
|
112
|
+
warnings.push(`${where}: provider ${JSON.stringify(d?.kind)} is ignored: it needs a kind of letters, digits, - and _, and model and create functions`);
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
// 不改插件导出的对象:用检查过的字段另做一个,方法绑定到原对象(插件的方法里可能用 this)
|
|
116
|
+
const def: PluginProvider = {
|
|
117
|
+
kind: d.kind,
|
|
118
|
+
model: d.model.bind(d),
|
|
119
|
+
create: d.create.bind(d),
|
|
120
|
+
...(typeof d.via === "function" ? { via: d.via.bind(d) } : {}),
|
|
121
|
+
...(typeof d.listModels === "function" ? { listModels: d.listModels.bind(d) } : {}),
|
|
122
|
+
...(d.settings !== undefined ? { settings: providerSettings(d.settings, `${where}: provider ${d.kind}`, warnings) } : {}),
|
|
123
|
+
};
|
|
124
|
+
return [{ plugin: source.id, def }];
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
return { providers, warnings };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 插件服务的配置项:格式不对、用了 jade 自己的变量、重复、第二个 model 的跳过并警告 */
|
|
131
|
+
function providerSettings(raw: unknown, where: string, warnings: string[]): ProviderSetting[] {
|
|
132
|
+
if (!Array.isArray(raw)) {
|
|
133
|
+
warnings.push(`${where}: settings are ignored: they must be an array`);
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
const out: ProviderSetting[] = [];
|
|
137
|
+
for (const x of raw as Partial<ProviderSetting>[]) {
|
|
138
|
+
const env = x?.env;
|
|
139
|
+
if (typeof env !== "string" || !SETTING_ENV.test(env) || env.startsWith("JADE_") || typeof x.label !== "string") {
|
|
140
|
+
warnings.push(`${where}: setting ${JSON.stringify(env)} is ignored: it needs an env name of capital letters, digits and _ (not JADE_…) and a label`);
|
|
141
|
+
} else if (out.some((o) => o.env === env)) warnings.push(`${where}: setting ${env} is ignored: it is listed twice`);
|
|
142
|
+
else if (x.model && out.some((o) => o.model)) warnings.push(`${where}: setting ${env} is ignored: only one setting can be the model`);
|
|
143
|
+
else out.push(x as ProviderSetting);
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
package/src/plugins/install.ts
CHANGED
|
@@ -386,7 +386,7 @@ function run(cmd: string, args: string[], cwd: string): Promise<string> {
|
|
|
386
386
|
|
|
387
387
|
/** $JADE_HOME/plugins.json:只有用户层能停用插件,仓库不能停用用户的插件或内置插件 */
|
|
388
388
|
export interface PluginSettings {
|
|
389
|
-
/**
|
|
389
|
+
/** 停用的插件的 id:内置插件(jade/web)或第三方插件(plugin.json 的 id) */
|
|
390
390
|
disabled: string[];
|
|
391
391
|
}
|
|
392
392
|
|