@uncarved/jade 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +187 -2
- package/bin/jade.js +20 -0
- package/dist/commands.d.ts +30 -0
- package/dist/commands.js +106 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +34 -0
- package/dist/config.js +130 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +17 -0
- package/dist/context.js +53 -0
- package/dist/context.js.map +1 -0
- package/dist/distro.d.ts +93 -0
- package/dist/distro.js +277 -0
- package/dist/distro.js.map +1 -0
- package/dist/history.d.ts +10 -0
- package/dist/history.js +47 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +1124 -0
- package/dist/main.js.map +1 -0
- package/dist/mcp.d.ts +114 -0
- package/dist/mcp.js +640 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory.d.ts +19 -0
- package/dist/memory.js +129 -0
- package/dist/memory.js.map +1 -0
- package/dist/mentions.d.ts +35 -0
- package/dist/mentions.js +130 -0
- package/dist/mentions.js.map +1 -0
- package/dist/plugin-cli.d.ts +16 -0
- package/dist/plugin-cli.js +251 -0
- package/dist/plugin-cli.js.map +1 -0
- package/dist/plugins/budget.d.ts +9 -0
- package/dist/plugins/budget.js +30 -0
- package/dist/plugins/budget.js.map +1 -0
- package/dist/plugins/builtins.d.ts +10 -0
- package/dist/plugins/builtins.js +22 -0
- package/dist/plugins/builtins.js.map +1 -0
- package/dist/plugins/checks.d.ts +70 -0
- package/dist/plugins/checks.js +335 -0
- package/dist/plugins/checks.js.map +1 -0
- package/dist/plugins/compact.d.ts +26 -0
- package/dist/plugins/compact.js +216 -0
- package/dist/plugins/compact.js.map +1 -0
- package/dist/plugins/core.d.ts +42 -0
- package/dist/plugins/core.js +190 -0
- package/dist/plugins/core.js.map +1 -0
- package/dist/plugins/install.d.ts +68 -0
- package/dist/plugins/install.js +377 -0
- package/dist/plugins/install.js.map +1 -0
- package/dist/plugins/loader.d.ts +182 -0
- package/dist/plugins/loader.js +253 -0
- package/dist/plugins/loader.js.map +1 -0
- package/dist/plugins/policy.d.ts +64 -0
- package/dist/plugins/policy.js +269 -0
- package/dist/plugins/policy.js.map +1 -0
- package/dist/plugins/prompts.d.ts +28 -0
- package/dist/plugins/prompts.js +169 -0
- package/dist/plugins/prompts.js.map +1 -0
- package/dist/plugins/repeats.d.ts +9 -0
- package/dist/plugins/repeats.js +55 -0
- package/dist/plugins/repeats.js.map +1 -0
- package/dist/plugins/schema.d.ts +2 -0
- package/dist/plugins/schema.js +64 -0
- package/dist/plugins/schema.js.map +1 -0
- package/dist/provider.d.ts +52 -0
- package/dist/provider.js +187 -0
- package/dist/provider.js.map +1 -0
- package/dist/sessions.d.ts +13 -0
- package/dist/sessions.js +40 -0
- package/dist/sessions.js.map +1 -0
- package/dist/skills.d.ts +37 -0
- package/dist/skills.js +179 -0
- package/dist/skills.js.map +1 -0
- package/dist/sys.d.ts +44 -0
- package/dist/sys.js +115 -0
- package/dist/sys.js.map +1 -0
- package/dist/tools/checkpoint.d.ts +43 -0
- package/dist/tools/checkpoint.js +144 -0
- package/dist/tools/checkpoint.js.map +1 -0
- package/dist/tools/fs.d.ts +35 -0
- package/dist/tools/fs.js +417 -0
- package/dist/tools/fs.js.map +1 -0
- package/dist/tools/grep.d.ts +6 -0
- package/dist/tools/grep.js +243 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/shell.d.ts +49 -0
- package/dist/tools/shell.js +295 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/task.d.ts +31 -0
- package/dist/tools/task.js +86 -0
- package/dist/tools/task.js.map +1 -0
- package/dist/tools/todo.d.ts +10 -0
- package/dist/tools/todo.js +55 -0
- package/dist/tools/todo.js.map +1 -0
- package/dist/tools/web.d.ts +23 -0
- package/dist/tools/web.js +426 -0
- package/dist/tools/web.js.map +1 -0
- package/dist/ui/configure.d.ts +20 -0
- package/dist/ui/configure.js +285 -0
- package/dist/ui/configure.js.map +1 -0
- package/dist/ui/diff.d.ts +20 -0
- package/dist/ui/diff.js +142 -0
- package/dist/ui/diff.js.map +1 -0
- package/dist/ui/editor.d.ts +33 -0
- package/dist/ui/editor.js +128 -0
- package/dist/ui/editor.js.map +1 -0
- package/dist/ui/render.d.ts +13 -0
- package/dist/ui/render.js +84 -0
- package/dist/ui/render.js.map +1 -0
- package/dist/ui/screen.d.ts +90 -0
- package/dist/ui/screen.js +105 -0
- package/dist/ui/screen.js.map +1 -0
- package/dist/ui/transcript.d.ts +33 -0
- package/dist/ui/transcript.js +336 -0
- package/dist/ui/transcript.js.map +1 -0
- package/dist/ui/tui.d.ts +28 -0
- package/dist/ui/tui.js +884 -0
- package/dist/ui/tui.js.map +1 -0
- package/package.json +59 -4
- package/src/commands.ts +120 -0
- package/src/config.ts +146 -0
- package/src/context.ts +64 -0
- package/src/distro.ts +358 -0
- package/src/history.ts +55 -0
- package/src/index.ts +26 -0
- package/src/main.ts +1180 -0
- package/src/mcp.ts +693 -0
- package/src/memory.ts +135 -0
- package/src/mentions.ts +135 -0
- package/src/plugin-cli.ts +239 -0
- package/src/plugins/budget.ts +37 -0
- package/src/plugins/builtins.ts +31 -0
- package/src/plugins/checks.ts +390 -0
- package/src/plugins/compact.ts +257 -0
- package/src/plugins/core.ts +217 -0
- package/src/plugins/install.ts +429 -0
- package/src/plugins/loader.ts +386 -0
- package/src/plugins/policy.ts +304 -0
- package/src/plugins/prompts.ts +200 -0
- package/src/plugins/repeats.ts +62 -0
- package/src/plugins/schema.ts +67 -0
- package/src/provider.ts +208 -0
- package/src/sessions.ts +48 -0
- package/src/skills.ts +197 -0
- package/src/sys.ts +133 -0
- package/src/tools/checkpoint.ts +164 -0
- package/src/tools/fs.ts +432 -0
- package/src/tools/grep.ts +268 -0
- package/src/tools/shell.ts +339 -0
- package/src/tools/task.ts +105 -0
- package/src/tools/todo.ts +67 -0
- package/src/tools/web.ts +451 -0
- package/src/ui/configure.ts +310 -0
- package/src/ui/diff.ts +136 -0
- package/src/ui/editor.ts +136 -0
- package/src/ui/render.ts +74 -0
- package/src/ui/screen.ts +164 -0
- package/src/ui/transcript.ts +345 -0
- package/src/ui/tui.tsx +1374 -0
package/src/distro.ts
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// 组装发行版:内核之外的功能都是插件。内置插件(见 plugins/builtins.ts)和第三方插件用同一套接口,
|
|
2
|
+
// 在这里拼成一份插件清单。宿主(main.ts)和测试都通过这里创建,内核本身不知道这些插件的存在。
|
|
3
|
+
//
|
|
4
|
+
// 用户可以停用内置插件($JADE_HOME/plugins.json),第三方插件可以用 replaces 代替内置插件;jade/policy 两者都不行。
|
|
5
|
+
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { checkCompat, definePlugin, type LogStore, type Plugin, type Provider, type Tool } from "uncarved";
|
|
8
|
+
import { budget } from "./plugins/budget.ts";
|
|
9
|
+
import { BUILTINS, BUILTIN_IDS, REQUIRED_IDS } from "./plugins/builtins.ts";
|
|
10
|
+
import { checks as checksPlugin, type ApproveCheck, type Check, type Checks } from "./plugins/checks.ts";
|
|
11
|
+
import { checksCommand, compactCommand, mcpCommand, mcpInstructions, memoryCommand, skillsCommand, withCounter, withSystem } from "./plugins/core.ts";
|
|
12
|
+
import { Memory, memoryTool } from "./memory.ts";
|
|
13
|
+
import { mcpTools, type McpClient } from "./mcp.ts";
|
|
14
|
+
import { skillTool, skillsPrompt, type Skill } from "./skills.ts";
|
|
15
|
+
import { compactor, type Compactor } from "./plugins/compact.ts";
|
|
16
|
+
import { COMMAND_NAME, JADE_VERSION, type HostContext, type JadePlugin, type LoadedPlugin, type PluginCommand } from "./plugins/loader.ts";
|
|
17
|
+
import { policy, type AllowRule, type Mode, type Policy } from "./plugins/policy.ts";
|
|
18
|
+
import { prompts, realPath, repoRoot } from "./plugins/prompts.ts";
|
|
19
|
+
import { repeats } from "./plugins/repeats.ts";
|
|
20
|
+
import { fsTools } from "./tools/fs.ts";
|
|
21
|
+
import { Checkpoints } from "./tools/checkpoint.ts";
|
|
22
|
+
import { shellTools } from "./tools/shell.ts";
|
|
23
|
+
import { taskTool, type ApproveSubagent } from "./tools/task.ts";
|
|
24
|
+
import { todoTool } from "./tools/todo.ts";
|
|
25
|
+
import { webTools } from "./tools/web.ts";
|
|
26
|
+
|
|
27
|
+
const READ_ONLY = new Set(["read_file", "list_dir", "glob", "grep"]);
|
|
28
|
+
|
|
29
|
+
export interface JadeOptions {
|
|
30
|
+
cwd: string;
|
|
31
|
+
/** 用户层配置目录 */
|
|
32
|
+
home: string;
|
|
33
|
+
store: LogStore;
|
|
34
|
+
provider: Provider;
|
|
35
|
+
mode?: Mode;
|
|
36
|
+
/** 已经通过 loadProjectPlugin 确认过的项目层插件,原样加入 */
|
|
37
|
+
projectPlugins?: Plugin[];
|
|
38
|
+
/** 用 loadPlugin 加载的第三方插件(用户层和项目层),每次 createJade 创建一套新实例 */
|
|
39
|
+
extensions?: LoadedPlugin[];
|
|
40
|
+
/** 停用的插件:内置插件的 id,或者第三方插件的 id(第三方插件按目录名停用由宿主在加载前处理) */
|
|
41
|
+
disabled?: Iterable<string>;
|
|
42
|
+
/** 压缩阈值;传函数时每次投影前取值 */
|
|
43
|
+
compactTokens?: number | (() => number);
|
|
44
|
+
maxSteps?: number;
|
|
45
|
+
/** 测试用 */
|
|
46
|
+
today?: string;
|
|
47
|
+
/** 读取搜索服务配置用,默认 process.env;测试用 */
|
|
48
|
+
env?: Record<string, string | undefined>;
|
|
49
|
+
/** web 工具用的 fetch;测试用 */
|
|
50
|
+
fetch?: typeof fetch;
|
|
51
|
+
/** web 工具解析主机名用;测试用 */
|
|
52
|
+
lookup?: (hostname: string) => Promise<string[]>;
|
|
53
|
+
/** 子 Agent 要用户批准的调用(例如 web_fetch)交给宿主;不给就拒绝 */
|
|
54
|
+
approveSubagent?: ApproveSubagent;
|
|
55
|
+
/** 子 Agent 也遵守的"总是允许"规则(主会话的),每次判断时取 */
|
|
56
|
+
sharedRules?: () => AllowRule[];
|
|
57
|
+
/**
|
|
58
|
+
* 编辑后检查的命令第一次运行前由宿主确认;不给就不做编辑后检查。
|
|
59
|
+
* 检查会执行仓库里的代码,所以不能默认放行
|
|
60
|
+
*/
|
|
61
|
+
approveCheck?: ApproveCheck;
|
|
62
|
+
/** /checks 显示一条检查是否已经确认过 */
|
|
63
|
+
checkConfirmed?: (check: Check) => Promise<boolean>;
|
|
64
|
+
/** 没有 approveCheck 时 /checks 给出的原因 */
|
|
65
|
+
checksUnavailable?: string;
|
|
66
|
+
/** 已连接的 MCP 服务,它们的工具加进主会话(声明只读的也给子 Agent) */
|
|
67
|
+
mcp?: McpClient[];
|
|
68
|
+
/** loadSkills 找到的 skill */
|
|
69
|
+
skills?: Skill[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface BuiltinState {
|
|
73
|
+
id: string;
|
|
74
|
+
about: string;
|
|
75
|
+
required?: boolean;
|
|
76
|
+
/** on:加载了;disabled:用户停用;replaced:被第三方插件代替 */
|
|
77
|
+
state: "on" | "disabled" | "replaced";
|
|
78
|
+
/** 代替它的插件 */
|
|
79
|
+
by?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface Jade {
|
|
83
|
+
plugins: Plugin[];
|
|
84
|
+
policy: Policy;
|
|
85
|
+
/** jade/compact 停用或被代替时没有 */
|
|
86
|
+
compact?: Compactor;
|
|
87
|
+
/** write_file / edit_file 写入前的快照,供 /undo */
|
|
88
|
+
checkpoints: Checkpoints;
|
|
89
|
+
/** 跨会话的记忆;jade/memory 停用或被代替时没有 */
|
|
90
|
+
memory?: Memory;
|
|
91
|
+
/** 编辑后检查;没有 shell、宿主没给 approveCheck、jade/checks 停用时为 undefined */
|
|
92
|
+
checks?: Checks;
|
|
93
|
+
/** 每个内置插件的状态 */
|
|
94
|
+
builtins: BuiltinState[];
|
|
95
|
+
/** 成功加入本会话的第三方插件实例 */
|
|
96
|
+
extensions: { source: LoadedPlugin; plugin: JadePlugin }[];
|
|
97
|
+
/** 插件(内置的和第三方的)提供的斜杠命令,按小写的名字 */
|
|
98
|
+
commands: Map<string, { plugin: string; command: PluginCommand; builtin: boolean }>;
|
|
99
|
+
/** 主会话的工具;宿主用 read_file / list_dir 读取 @ 引用的文件 */
|
|
100
|
+
tools: Tool[];
|
|
101
|
+
/** 宿主退出时调用:杀掉后台 shell */
|
|
102
|
+
shutdown(): void;
|
|
103
|
+
/** 启动时要告诉用户的问题,例如找不到 shell */
|
|
104
|
+
warnings: string[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function createJade(opts: JadeOptions): Promise<Jade> {
|
|
108
|
+
const { cwd, store, provider } = opts;
|
|
109
|
+
const skills = opts.skills ?? [];
|
|
110
|
+
const mcp = opts.mcp ?? [];
|
|
111
|
+
const warnings: string[] = [];
|
|
112
|
+
|
|
113
|
+
const disabled = new Set(opts.disabled ?? []);
|
|
114
|
+
for (const id of REQUIRED_IDS) {
|
|
115
|
+
if (disabled.has(id)) {
|
|
116
|
+
warnings.push(`${id} cannot be disabled (it decides which tool calls need your approval); it stays on`);
|
|
117
|
+
disabled.delete(id);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const off = (id: string) => disabled.has(id);
|
|
121
|
+
|
|
122
|
+
const checkpoints = new Checkpoints(path.join(opts.home, "checkpoints"));
|
|
123
|
+
// run 也给插件和编辑后检查用,所以 jade/shell 停用时也创建,只是不把工具交给模型
|
|
124
|
+
const shell = shellTools({ cwd });
|
|
125
|
+
const root = await repoRoot(cwd);
|
|
126
|
+
const host: HostContext = { jade: JADE_VERSION, cwd: realPath(cwd), root: realPath(root), home: opts.home, definePlugin, run: shell.run };
|
|
127
|
+
|
|
128
|
+
// 第三方插件先创建:要先知道它们代替哪些内置插件
|
|
129
|
+
const made = await createExtensions(opts.extensions ?? [], host, disabled, warnings);
|
|
130
|
+
|
|
131
|
+
// ---------- 内置插件(jade/prompts 最后创建:它要知道最终有哪些工具) ----------
|
|
132
|
+
const built = new Map<string, JadePlugin>();
|
|
133
|
+
const add = (p: JadePlugin) => built.set(p.id, p);
|
|
134
|
+
|
|
135
|
+
const pol = policy({ cwd, mode: opts.mode, readRoots: off("jade/skills") ? [] : skills.map((x) => x.dir) });
|
|
136
|
+
add(pol.plugin);
|
|
137
|
+
// 占位,组装完成后换成真正的提示词插件
|
|
138
|
+
if (!off("jade/prompts")) add({ id: "jade/prompts", version: "0.3.0", kernel: "^0.1" });
|
|
139
|
+
|
|
140
|
+
// 文件工具据此判断读取和修改是不是同一次回复里发出的
|
|
141
|
+
const responses = { calls: 0 };
|
|
142
|
+
if (!off("jade/fs")) add(withCounter({ id: "jade/fs", version: "0.1.0", kernel: "^0.1", tools: fsTools(cwd, { checkpoints, responses }) }, responses));
|
|
143
|
+
if (!off("jade/shell")) add({ id: "jade/shell", version: "0.1.0", kernel: "^0.1", tools: shell.tools });
|
|
144
|
+
let web: Tool[] = [];
|
|
145
|
+
if (!off("jade/web")) {
|
|
146
|
+
try {
|
|
147
|
+
web = webTools({ env: opts.env, fetch: opts.fetch, lookup: opts.lookup }).tools;
|
|
148
|
+
} catch (err) {
|
|
149
|
+
warnings.push(`web tools are disabled: ${err instanceof Error ? err.message : String(err)}`);
|
|
150
|
+
}
|
|
151
|
+
add({ id: "jade/web", version: "0.1.0", kernel: "^0.1", tools: web });
|
|
152
|
+
}
|
|
153
|
+
if (!off("jade/todo")) add({ id: "jade/todo", version: "0.1.0", kernel: "^0.1", tools: [todoTool()] });
|
|
154
|
+
|
|
155
|
+
let memory: Memory | undefined;
|
|
156
|
+
if (!off("jade/memory")) {
|
|
157
|
+
memory = new Memory(opts.home, root);
|
|
158
|
+
const text = await memory.prompt();
|
|
159
|
+
add(withSystem({ id: "jade/memory", version: "0.1.0", kernel: "^0.1", tools: [memoryTool(memory)], commands: [memoryCommand(memory)] }, text ? [{ text, trust: "trusted" }] : []));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** 最终加载了的内置插件,子 Agent 据此决定给哪些工具;组装完成后才填上 */
|
|
163
|
+
const kept = new Set<string>();
|
|
164
|
+
/** 子 Agent 的提示词,组装完成后生成 */
|
|
165
|
+
const subSystem: { plugin?: Plugin } = {};
|
|
166
|
+
if (!off("jade/task")) {
|
|
167
|
+
const subagentPlugins = subagent(opts, subSystem, () => ({ fs: kept.has("jade/fs"), web: kept.has("jade/web") ? web : [], mcp: kept.has("jade/mcp") ? mcp : [] }));
|
|
168
|
+
add({ id: "jade/task", version: "0.1.0", kernel: "^0.1", tools: [taskTool({ store, provider, plugins: subagentPlugins, approve: opts.approveSubagent })] });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!off("jade/skills")) {
|
|
172
|
+
const list = skillsPrompt(skills);
|
|
173
|
+
// 有随仓库分发的 skill 时这段是 untrusted
|
|
174
|
+
const trust = skills.every((x) => x.layer === "user") ? "trusted" : "untrusted";
|
|
175
|
+
add(
|
|
176
|
+
withSystem(
|
|
177
|
+
{ id: "jade/skills", version: "0.1.0", kernel: "^0.1", tools: skills.length ? [skillTool(skills)] : [], commands: [skillsCommand(skills, { cwd, home: opts.home })] },
|
|
178
|
+
list ? [{ text: list, trust }] : [],
|
|
179
|
+
),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
if (!off("jade/mcp")) {
|
|
183
|
+
add(withSystem({ id: "jade/mcp", version: "0.1.0", kernel: "^0.1", tools: mcpTools(mcp), commands: [mcpCommand(mcp, { cwd, home: opts.home })] }, mcpInstructions(mcp)));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let checks: Checks | undefined;
|
|
187
|
+
if (!off("jade/checks")) {
|
|
188
|
+
checks =
|
|
189
|
+
shell.run && opts.approveCheck
|
|
190
|
+
? checksPlugin({ cwd: realPath(cwd), root: realPath(root), home: opts.home, run: shell.run, approve: opts.approveCheck })
|
|
191
|
+
: undefined;
|
|
192
|
+
const command = checksCommand(checks, {
|
|
193
|
+
cwd,
|
|
194
|
+
home: opts.home,
|
|
195
|
+
changed: async (sessionId) => [...new Set((await checkpoints.list(sessionId)).map((c) => c.path))],
|
|
196
|
+
confirmed: opts.checkConfirmed,
|
|
197
|
+
unavailable: opts.checksUnavailable ?? (shell.run ? "Edit-time checks are turned off." : "Edit-time checks need a shell; see the warning at startup."),
|
|
198
|
+
});
|
|
199
|
+
add(checks ? { ...checks.plugin, commands: [command] } : { id: "jade/checks", version: "0.1.0", kernel: "^0.1", commands: [command] });
|
|
200
|
+
}
|
|
201
|
+
let compact: Compactor | undefined;
|
|
202
|
+
if (!off("jade/compact")) {
|
|
203
|
+
compact = compactor({ provider, thresholdTokens: opts.compactTokens });
|
|
204
|
+
add({ ...compact.plugin, commands: [compactCommand(compact)] });
|
|
205
|
+
}
|
|
206
|
+
if (!off("jade/budget")) add(budget({ maxSteps: opts.maxSteps }));
|
|
207
|
+
if (!off("jade/repeats")) {
|
|
208
|
+
// 自己数回复次数:不依赖 jade/fs 是否加载
|
|
209
|
+
const own = { calls: 0 };
|
|
210
|
+
add(withCounter(repeats({ responses: own }), own));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---------- 代替关系:被代替的内置插件让位,代替者的工具名可以和它重复 ----------
|
|
214
|
+
const claims = (p: JadePlugin) => (p.replaces ?? []).filter((id) => BUILTIN_IDS.has(id) && !REQUIRED_IDS.has(id) && built.has(id));
|
|
215
|
+
for (const { source, plugin } of made) {
|
|
216
|
+
for (const id of plugin.replaces ?? []) {
|
|
217
|
+
if (REQUIRED_IDS.has(id)) warnings.push(`plugin ${plugin.id} (${source.layer}): cannot replace ${id}; add a gate hook to make approvals stricter instead`);
|
|
218
|
+
else if (!BUILTIN_IDS.has(id)) warnings.push(`plugin ${plugin.id} (${source.layer}): replaces unknown built-in plugin ${id}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// 从"全部声明都生效"开始;被拒绝的插件声明的代替撤销,内置插件回来之后可能又拒绝别的插件,直到不再变化
|
|
222
|
+
let replaced = new Set(made.flatMap((m) => claims(m.plugin)));
|
|
223
|
+
let result: ReturnType<typeof accept>;
|
|
224
|
+
for (;;) {
|
|
225
|
+
result = accept(made, [...built.values()].filter((p) => !replaced.has(p.id)));
|
|
226
|
+
const next = new Set(result.accepted.flatMap((m) => claims(m.plugin)));
|
|
227
|
+
if (next.size === replaced.size) break;
|
|
228
|
+
replaced = next;
|
|
229
|
+
}
|
|
230
|
+
warnings.push(...result.rejected);
|
|
231
|
+
const added = result.accepted;
|
|
232
|
+
const builtin = [...built.values()].filter((p) => !replaced.has(p.id));
|
|
233
|
+
for (const p of builtin) kept.add(p.id);
|
|
234
|
+
|
|
235
|
+
const allTools = [...builtin, ...added.map((a) => a.plugin)].flatMap((p) => p.tools ?? []);
|
|
236
|
+
const toolNames = new Set(allTools.map((t) => t.name));
|
|
237
|
+
const webNames = kept.has("jade/web") ? web.map((t) => t.name) : [];
|
|
238
|
+
const at = builtin.findIndex((p) => p.id === "jade/prompts");
|
|
239
|
+
if (at >= 0) {
|
|
240
|
+
builtin[at] = await prompts({
|
|
241
|
+
cwd,
|
|
242
|
+
home: opts.home,
|
|
243
|
+
today: opts.today,
|
|
244
|
+
// bash 由别的插件提供时不知道它用什么 shell,不写
|
|
245
|
+
shell: toolNames.has("bash") ? (kept.has("jade/shell") ? (shell.executable ?? null) : undefined) : null,
|
|
246
|
+
web: webNames,
|
|
247
|
+
checks: kept.has("jade/checks") && Boolean(checks),
|
|
248
|
+
tools: toolNames,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
if (kept.has("jade/task")) subSystem.plugin = await prompts({ cwd, home: opts.home, role: "subagent", today: opts.today, web: webNames });
|
|
252
|
+
|
|
253
|
+
const builtins: BuiltinState[] = BUILTINS.map((b) => {
|
|
254
|
+
if (kept.has(b.id)) return { ...b, state: "on" as const };
|
|
255
|
+
const by = added.find((a) => claims(a.plugin).includes(b.id))?.plugin.id;
|
|
256
|
+
return by ? { ...b, state: "replaced" as const, by } : { ...b, state: "disabled" as const };
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const plugins = [...builtin, ...(opts.projectPlugins ?? []), ...added.map((a) => a.plugin)];
|
|
260
|
+
const commands = new Map<string, { plugin: string; command: PluginCommand; builtin: boolean }>();
|
|
261
|
+
for (const p of [...builtin, ...added.map((a) => a.plugin)] as JadePlugin[]) {
|
|
262
|
+
const isBuiltin = BUILTIN_IDS.has(p.id) && builtin.includes(p);
|
|
263
|
+
for (const c of p.commands ?? []) {
|
|
264
|
+
const taken = commands.get(c.name.toLowerCase());
|
|
265
|
+
if (taken) warnings.push(`plugin ${p.id}: command /${c.name} is ignored: plugin ${taken.plugin} already provides it`);
|
|
266
|
+
else commands.set(c.name.toLowerCase(), { plugin: p.id, command: c, builtin: isBuiltin });
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (shell.unavailable && kept.has("jade/shell")) warnings.push(shell.unavailable);
|
|
270
|
+
return {
|
|
271
|
+
plugins,
|
|
272
|
+
policy: pol,
|
|
273
|
+
compact: kept.has("jade/compact") ? compact : undefined,
|
|
274
|
+
checkpoints,
|
|
275
|
+
memory: kept.has("jade/memory") ? memory : undefined,
|
|
276
|
+
checks: kept.has("jade/checks") ? checks : undefined,
|
|
277
|
+
builtins,
|
|
278
|
+
extensions: added,
|
|
279
|
+
commands,
|
|
280
|
+
tools: allTools,
|
|
281
|
+
shutdown: shell.shutdown,
|
|
282
|
+
warnings,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 创建第三方插件的实例。有问题的插件跳过并给出警告,而不是让整个 jade 起不来:工厂出错、内核不兼容、命令格式不对。
|
|
288
|
+
* 只带内容(skills、命令、MCP 配置)的插件没有实例,由宿主处理
|
|
289
|
+
*/
|
|
290
|
+
async function createExtensions(sources: LoadedPlugin[], host: HostContext, disabled: Set<string>, warnings: string[]) {
|
|
291
|
+
const out: { source: LoadedPlugin; plugin: JadePlugin }[] = [];
|
|
292
|
+
for (const source of sources) {
|
|
293
|
+
if (!source.create) continue;
|
|
294
|
+
const where = `plugin ${source.name} (${source.layer})`;
|
|
295
|
+
let plugin: JadePlugin;
|
|
296
|
+
try {
|
|
297
|
+
plugin = await source.create(host);
|
|
298
|
+
checkCompat(plugin);
|
|
299
|
+
} catch (err) {
|
|
300
|
+
warnings.push(`${where} is skipped: ${err instanceof Error ? err.message : String(err)}`);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (disabled.has(plugin.id)) continue;
|
|
304
|
+
if (plugin.replaces !== undefined && !(Array.isArray(plugin.replaces) && plugin.replaces.every((x) => typeof x === "string"))) {
|
|
305
|
+
warnings.push(`${where} is skipped: "replaces" must be an array of plugin ids`);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const commands = (plugin.commands ?? []).filter((c) => {
|
|
309
|
+
const ok = c && typeof c.name === "string" && COMMAND_NAME.test(c.name) && typeof c.run === "function";
|
|
310
|
+
if (!ok) warnings.push(`${where}: command ${JSON.stringify(c?.name)} is ignored: it needs a name of letters, digits, - and _, and a run function`);
|
|
311
|
+
return ok;
|
|
312
|
+
});
|
|
313
|
+
// 原地修改而不是复制:内核只认加载器返回的那个项目层插件对象(宪法 7.1)
|
|
314
|
+
if (plugin.commands) plugin.commands = commands;
|
|
315
|
+
out.push({ source, plugin });
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** 按顺序检查第三方插件的 id 和工具名不和已有的(留下的内置插件、排在前面的第三方插件)重复 */
|
|
321
|
+
function accept(made: { source: LoadedPlugin; plugin: JadePlugin }[], existing: Plugin[]) {
|
|
322
|
+
const ids = new Set(existing.map((p) => p.id));
|
|
323
|
+
const toolNames = new Set(existing.flatMap((p) => (p.tools ?? []).map((t) => t.name)));
|
|
324
|
+
const accepted: typeof made = [];
|
|
325
|
+
const rejected: string[] = [];
|
|
326
|
+
for (const m of made) {
|
|
327
|
+
const where = `plugin ${m.source.name} (${m.source.layer})`;
|
|
328
|
+
if (ids.has(m.plugin.id)) {
|
|
329
|
+
rejected.push(`${where} is skipped: another plugin already has the id ${m.plugin.id}`);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const clash = (m.plugin.tools ?? []).find((t) => toolNames.has(t.name));
|
|
333
|
+
if (clash) {
|
|
334
|
+
rejected.push(`${where} is skipped: tool ${clash.name} already exists`);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
ids.add(m.plugin.id);
|
|
338
|
+
for (const t of m.plugin.tools ?? []) toolNames.add(t.name);
|
|
339
|
+
accepted.push(m);
|
|
340
|
+
}
|
|
341
|
+
return { accepted, rejected };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** 子 Agent 的插件:只读工具(含 web 工具)、子 Agent 提示词、只读策略(加上主会话的规则)、步数上限。每次调用返回新实例 */
|
|
345
|
+
function subagent(opts: JadeOptions, system: { plugin?: Plugin }, available: () => { fs: boolean; web: Tool[]; mcp: McpClient[] }): () => Plugin[] {
|
|
346
|
+
// 有哪些工具要到组装完成才知道,所以每次调用时再取
|
|
347
|
+
return () => {
|
|
348
|
+
const { fs, web, mcp } = available();
|
|
349
|
+
const tools: Plugin = {
|
|
350
|
+
id: "jade/tools-readonly",
|
|
351
|
+
version: "0.1.0",
|
|
352
|
+
kernel: "^0.1",
|
|
353
|
+
tools: [...(fs ? fsTools(opts.cwd).filter((t) => READ_ONLY.has(t.name)) : []), ...web, ...mcpTools(mcp, { readOnly: true })],
|
|
354
|
+
};
|
|
355
|
+
const pol = policy({ cwd: opts.cwd, mode: "readonly", id: "jade/policy-subagent", rules: opts.sharedRules });
|
|
356
|
+
return [tools, ...(system.plugin ? [system.plugin] : []), pol.plugin, budget({ maxSteps: 40, id: "jade/budget-subagent" })];
|
|
357
|
+
};
|
|
358
|
+
}
|
package/src/history.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// 输入历史:输入框里提交过的内容,跨会话保留,↑/↓ 和 Ctrl+R 用。
|
|
2
|
+
// 所有目录共用 $JADE_HOME/history.jsonl,每条记下目录;读的时候只取当前目录的。
|
|
3
|
+
|
|
4
|
+
import { appendFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
interface HistoryEntry {
|
|
8
|
+
text: string;
|
|
9
|
+
cwd: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** 每个目录最多留多少条 */
|
|
13
|
+
export const HISTORY_LIMIT = 500;
|
|
14
|
+
/** 文件超过这么多行时,下次读取顺便删掉旧的 */
|
|
15
|
+
const FILE_LIMIT = 5000;
|
|
16
|
+
|
|
17
|
+
export class InputHistory {
|
|
18
|
+
readonly file: string;
|
|
19
|
+
readonly cwd: string;
|
|
20
|
+
|
|
21
|
+
constructor(file: string, cwd: string) {
|
|
22
|
+
this.file = file;
|
|
23
|
+
this.cwd = path.resolve(cwd);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 当前目录的历史,旧的在前,重复的只留最后一次 */
|
|
27
|
+
async load(): Promise<string[]> {
|
|
28
|
+
let text: string;
|
|
29
|
+
try {
|
|
30
|
+
text = await readFile(this.file, "utf8");
|
|
31
|
+
} catch {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const lines = text.split("\n").filter(Boolean);
|
|
35
|
+
const entries: HistoryEntry[] = [];
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
try {
|
|
38
|
+
const e = JSON.parse(line) as HistoryEntry;
|
|
39
|
+
if (typeof e.text === "string" && typeof e.cwd === "string") entries.push(e);
|
|
40
|
+
} catch {}
|
|
41
|
+
}
|
|
42
|
+
if (lines.length > FILE_LIMIT) {
|
|
43
|
+
await writeFile(this.file, entries.slice(-FILE_LIMIT / 2).map((e) => JSON.stringify(e) + "\n").join("")).catch(() => {});
|
|
44
|
+
}
|
|
45
|
+
const mine = entries.filter((e) => samePath(e.cwd, this.cwd)).map((e) => e.text);
|
|
46
|
+
return mine.filter((t, i) => mine.lastIndexOf(t) === i).slice(-HISTORY_LIMIT);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async add(text: string): Promise<void> {
|
|
50
|
+
await mkdir(path.dirname(this.file), { recursive: true });
|
|
51
|
+
await appendFile(this.file, JSON.stringify({ text, cwd: this.cwd } satisfies HistoryEntry) + "\n");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const samePath = (a: string, b: string) => (process.platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from "./distro.ts";
|
|
2
|
+
export * from "./provider.ts";
|
|
3
|
+
export * from "./sessions.ts";
|
|
4
|
+
export * from "./memory.ts";
|
|
5
|
+
export * from "./mentions.ts";
|
|
6
|
+
export * from "./plugins/budget.ts";
|
|
7
|
+
export * from "./plugins/compact.ts";
|
|
8
|
+
export * from "./plugins/checks.ts";
|
|
9
|
+
export * from "./plugins/policy.ts";
|
|
10
|
+
export * from "./plugins/prompts.ts";
|
|
11
|
+
export * from "./plugins/schema.ts";
|
|
12
|
+
export * from "./tools/fs.ts";
|
|
13
|
+
export * from "./tools/grep.ts";
|
|
14
|
+
export * from "./tools/web.ts";
|
|
15
|
+
export * from "./tools/shell.ts";
|
|
16
|
+
export * from "./tools/task.ts";
|
|
17
|
+
export * from "./tools/checkpoint.ts";
|
|
18
|
+
export * from "./tools/todo.ts";
|
|
19
|
+
export * from "./config.ts";
|
|
20
|
+
export * from "./context.ts";
|
|
21
|
+
export * from "./commands.ts";
|
|
22
|
+
export * from "./plugins/loader.ts";
|
|
23
|
+
export * from "./plugins/install.ts";
|
|
24
|
+
export * from "./plugins/builtins.ts";
|
|
25
|
+
export * from "./mcp.ts";
|
|
26
|
+
export * from "./skills.ts";
|