@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
|
@@ -19,6 +19,7 @@ export function withRetry(inner, opts = {}) {
|
|
|
19
19
|
return {
|
|
20
20
|
id: `retry(${inner.id})`,
|
|
21
21
|
async complete(request, options) {
|
|
22
|
+
const failures = [];
|
|
22
23
|
for (let attempt = 1;; attempt++) {
|
|
23
24
|
let streamed = false;
|
|
24
25
|
await opts.gate?.acquire(options.signal);
|
|
@@ -32,7 +33,7 @@ export function withRetry(inner, opts = {}) {
|
|
|
32
33
|
},
|
|
33
34
|
});
|
|
34
35
|
// 实际回复的是被包装者(4.7 第 3 款)
|
|
35
|
-
return { ...res, provider: res.provider ?? inner.id };
|
|
36
|
+
return { ...res, provider: res.provider ?? inner.id, ...(failures.length > 0 ? { retries: failures } : {}) };
|
|
36
37
|
}
|
|
37
38
|
catch (err) {
|
|
38
39
|
if (!(err instanceof ProviderError) || !err.retryable || attempt > retries || options.signal.aborted)
|
|
@@ -48,6 +49,7 @@ export function withRetry(inner, opts = {}) {
|
|
|
48
49
|
// 失败的这次已经推送过增量的,先让订阅者作废它们(4.7)
|
|
49
50
|
if (streamed)
|
|
50
51
|
options.onDelta?.({ kind: "reset" });
|
|
52
|
+
failures.push({ attempt, delayMs, error: err.message, ...(err.status !== undefined ? { status: err.status } : {}), ...(streamed ? { discarded: true } : {}) });
|
|
51
53
|
opts.onRetry?.({ attempt, retries, delayMs, error: err });
|
|
52
54
|
await sleep(delayMs, options.signal);
|
|
53
55
|
}
|
|
@@ -319,7 +321,7 @@ export async function listModels(env = process.env, fetchImpl = fetch) {
|
|
|
319
321
|
return [];
|
|
320
322
|
}
|
|
321
323
|
}
|
|
322
|
-
function jsonObject(name, text) {
|
|
324
|
+
export function jsonObject(name, text) {
|
|
323
325
|
let value;
|
|
324
326
|
try {
|
|
325
327
|
value = JSON.parse(text);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// 保存过的模型:$JADE_HOME/models.json。同一个服务(类型、地址、密钥都相同)只存一份,下面是在它上面用过的模型,
|
|
2
|
-
//
|
|
2
|
+
// 每个模型带自己的设置(输出上限、思考力度、上下文窗口、openai-compat 的额外请求参数等)。.env 里始终是当前用的那一个,这里记着用过的全部,
|
|
3
3
|
// /model 可以切回去。
|
|
4
4
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
/** 每个模型自己的设置:切换模型时一起换掉 */
|
|
7
|
-
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION"];
|
|
7
|
+
export const MODEL_SETTINGS = ["JADE_MAX_TOKENS", "JADE_EFFORT", "JADE_CONTEXT_WINDOW", "JADE_RPM", "JADE_VISION", "OPENAI_COMPAT_EXTRA_BODY"];
|
|
8
8
|
const strings = (v) => typeof v === "object" && v !== null ? Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) : {};
|
|
9
9
|
/** 读不了或格式不对时返回空数组 */
|
|
10
10
|
export async function readSavedServices(file) {
|
|
@@ -16,11 +16,16 @@ export async function readSavedServices(file) {
|
|
|
16
16
|
const s = raw;
|
|
17
17
|
if (!s || !Array.isArray(s.models))
|
|
18
18
|
return [];
|
|
19
|
+
// 以前额外请求参数存在服务上:分给它下面没有自己那份的模型
|
|
20
|
+
const { OPENAI_COMPAT_EXTRA_BODY: extraBody, ...env } = strings(s.env);
|
|
19
21
|
const models = s.models.flatMap((m) => {
|
|
20
22
|
const name = m?.name;
|
|
21
|
-
|
|
23
|
+
const settings = strings(m.settings);
|
|
24
|
+
if (extraBody && settings.OPENAI_COMPAT_EXTRA_BODY === undefined)
|
|
25
|
+
settings.OPENAI_COMPAT_EXTRA_BODY = extraBody;
|
|
26
|
+
return typeof name === "string" && name ? [{ name, settings }] : [];
|
|
22
27
|
});
|
|
23
|
-
return models.length ? [{ env
|
|
28
|
+
return models.length ? [{ env, models }] : [];
|
|
24
29
|
});
|
|
25
30
|
}
|
|
26
31
|
catch {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// 用户的选择在工具结果里,会话日志里看得到
|
|
9
9
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
10
|
import path from "node:path";
|
|
11
|
-
import { definePlugin } from "
|
|
11
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
12
12
|
export const DESCRIPTIONS = {
|
|
13
13
|
ask: "ask before edits and commands",
|
|
14
14
|
auto: "edits in this directory and harmless commands run without asking; others still ask",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade/plugins:/plugins 命令。列出这次启动里每个插件的状态和原因(failed、skipped、conflicted……),
|
|
2
2
|
// 以及每个服务由谁提供。数据来自核心自己的 jade@1 服务,和救援 CLI `jade plugins list` 是同一份状态。
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
4
|
export default definePlugin({
|
|
5
5
|
process(ctx) {
|
|
6
6
|
const commands = ctx.require("commands@1");
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
// jade/print:jade -p "<prompt>" 跑一轮就退出。模型的回复写到 stdout,工具调用和提示写到 stderr。
|
|
2
2
|
// 不能向用户提问:ui@1 的 select 总是返回 undefined,要审批的调用由审批插件拒绝。
|
|
3
|
-
import { definePlugin } from "
|
|
4
|
-
const USAGE = `usage: jade -p <prompt> [--resume <session id>]
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
|
+
const USAGE = `usage: jade -p <prompt> [--resume <session id> | --continue] [--json]
|
|
5
5
|
echo <prompt> | jade -p
|
|
6
6
|
cat <file> | jade -p - <prompt>
|
|
7
7
|
|
|
8
8
|
With "-", what comes in on stdin is added after the prompt (without a prompt, stdin is the prompt).
|
|
9
9
|
|
|
10
10
|
Runs one prompt and exits. The reply goes to stdout; tool calls and notices go to stderr.
|
|
11
|
+
-c, --continue continues the most recent session in this directory.
|
|
12
|
+
--json prints one JSON object on stdout instead of the reply as it is written:
|
|
13
|
+
{"session", "result" (the final reply), "reason" (why the turn ended), "error"?, "usage"}
|
|
11
14
|
Exit code: 0 when the turn ends normally, 1 on an error, 130 when interrupted.
|
|
12
15
|
jade's own commands (plugins, update, --safe): jade help`;
|
|
13
16
|
const ui = {
|
|
@@ -18,6 +21,8 @@ const ui = {
|
|
|
18
21
|
select: async () => undefined,
|
|
19
22
|
input: async () => undefined,
|
|
20
23
|
};
|
|
24
|
+
/** --continue 用:process() 里记下的工作目录和会话索引(jade/sessions 的 sessions@1,用到的时候取) */
|
|
25
|
+
let here;
|
|
21
26
|
const frontend = {
|
|
22
27
|
async run({ argv, sessions }) {
|
|
23
28
|
const args = parse(argv);
|
|
@@ -36,6 +41,14 @@ const frontend = {
|
|
|
36
41
|
process.stderr.write(`no prompt given\n\n${USAGE}\n`);
|
|
37
42
|
return 2;
|
|
38
43
|
}
|
|
44
|
+
if (args.cont) {
|
|
45
|
+
const latest = (await here?.index()?.list(here.cwd))?.[0]?.id;
|
|
46
|
+
if (!latest) {
|
|
47
|
+
process.stderr.write("jade: there is no earlier session in this directory to continue\n");
|
|
48
|
+
return 2;
|
|
49
|
+
}
|
|
50
|
+
args.resume = latest;
|
|
51
|
+
}
|
|
39
52
|
let session;
|
|
40
53
|
try {
|
|
41
54
|
session = await sessions.open(args.resume ? { resume: args.resume } : {});
|
|
@@ -47,7 +60,12 @@ const frontend = {
|
|
|
47
60
|
}
|
|
48
61
|
let ended;
|
|
49
62
|
let midLine = false;
|
|
63
|
+
/** --json:回复不边写边打印,最后一次回复的正文和用量合计在结束时一起输出 */
|
|
64
|
+
let result = "";
|
|
65
|
+
const used = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 };
|
|
50
66
|
const out = (text) => {
|
|
67
|
+
if (args.json)
|
|
68
|
+
return;
|
|
51
69
|
process.stdout.write(text);
|
|
52
70
|
midLine = !text.endsWith("\n");
|
|
53
71
|
};
|
|
@@ -62,7 +80,12 @@ const frontend = {
|
|
|
62
80
|
return;
|
|
63
81
|
const e = n.event;
|
|
64
82
|
if (e.type === "model.responded") {
|
|
65
|
-
const content = e.data
|
|
83
|
+
const { content, usage } = e.data;
|
|
84
|
+
const text = content.flatMap((b) => (b.kind === "text" ? [b.text] : [])).join("\n");
|
|
85
|
+
if (text)
|
|
86
|
+
result = text;
|
|
87
|
+
for (const k of Object.keys(used))
|
|
88
|
+
used[k] += usage?.[k] ?? 0;
|
|
66
89
|
if (!streamed)
|
|
67
90
|
for (const b of content)
|
|
68
91
|
if (b.kind === "text")
|
|
@@ -107,6 +130,10 @@ const frontend = {
|
|
|
107
130
|
out("\n");
|
|
108
131
|
process.stderr.write(`session ${session.id}\n`);
|
|
109
132
|
const end = ended;
|
|
133
|
+
if (args.json) {
|
|
134
|
+
const error = failure !== undefined ? errorText(failure) : end?.error;
|
|
135
|
+
process.stdout.write(`${JSON.stringify({ session: session.id, result, reason: end?.reason ?? "error", ...(error ? { error } : {}), usage: used })}\n`);
|
|
136
|
+
}
|
|
110
137
|
if (end?.reason === "aborted")
|
|
111
138
|
return 130;
|
|
112
139
|
if (failure !== undefined || end?.reason === "error") {
|
|
@@ -118,6 +145,7 @@ const frontend = {
|
|
|
118
145
|
};
|
|
119
146
|
export default definePlugin({
|
|
120
147
|
process(ctx) {
|
|
148
|
+
here = { cwd: ctx.cwd, index: () => ctx.service("sessions@1") };
|
|
121
149
|
ctx.provide("ui@1", ui);
|
|
122
150
|
ctx.provide("frontend@1", frontend);
|
|
123
151
|
},
|
|
@@ -133,6 +161,10 @@ function parse(argv) {
|
|
|
133
161
|
out.help = true;
|
|
134
162
|
else if (a === "-")
|
|
135
163
|
out.stdin = true;
|
|
164
|
+
else if (a === "-c" || a === "--continue")
|
|
165
|
+
out.cont = true;
|
|
166
|
+
else if (a === "--json")
|
|
167
|
+
out.json = true;
|
|
136
168
|
else if (a === "--resume") {
|
|
137
169
|
const id = argv[++i];
|
|
138
170
|
if (!id)
|
|
@@ -144,6 +176,8 @@ function parse(argv) {
|
|
|
144
176
|
else
|
|
145
177
|
words.push(a);
|
|
146
178
|
}
|
|
179
|
+
if (out.cont && out.resume)
|
|
180
|
+
return "--continue and --resume both pick the session; use one of them";
|
|
147
181
|
if (words.length > 0)
|
|
148
182
|
out.prompt = words.join(" ");
|
|
149
183
|
return out;
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
// jade/prompts:在 context.build 里生成系统提示词。其他插件经 prompts@1 往里加段落。
|
|
2
|
-
// 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts
|
|
2
|
+
// 也负责内核不写的、给模型看的说明:内容为空的错误工具结果在投影里补一句原因(results.ts);
|
|
3
|
+
// 其他插件经 prompts@1 的 note 给模型留一句说明(notes.ts)。
|
|
4
|
+
import os from "node:os";
|
|
3
5
|
import path from "node:path";
|
|
4
|
-
import { definePlugin } from "
|
|
6
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
7
|
+
import { Notes } from "./notes.js";
|
|
5
8
|
import { allInstructions, environment, mainPrompt, realPath } from "./prompts.js";
|
|
6
9
|
import { explainEmptyResults, whyEmpty } from "./results.js";
|
|
7
10
|
const ID = "jade/prompts";
|
|
8
11
|
/** 其他插件加的段落。模块每个进程只加载一次,进程级和会话级共用 */
|
|
9
12
|
const sections = [];
|
|
13
|
+
/** 打开着的会话 → 它读到的指令文件(prompts@1.instructions) */
|
|
14
|
+
const loaded = new Map();
|
|
15
|
+
/** 其他插件留给模型的说明 */
|
|
16
|
+
const notes = new Notes();
|
|
10
17
|
function segment(text, trust = "trusted", stability = "stable", by = ID, source) {
|
|
11
18
|
return { text, stability, origin: { by: "plugin", id: by, ...(source ? { source } : {}) }, trust };
|
|
12
19
|
}
|
|
@@ -32,11 +39,21 @@ export default definePlugin({
|
|
|
32
39
|
caller.onDispose(remove);
|
|
33
40
|
return remove;
|
|
34
41
|
},
|
|
42
|
+
note(session, text) {
|
|
43
|
+
if (typeof session !== "string" || !session)
|
|
44
|
+
throw new Error("prompts.note: session must be a session id");
|
|
45
|
+
if (typeof text !== "string" || !text)
|
|
46
|
+
throw new Error("prompts.note: text must be a non-empty string");
|
|
47
|
+
notes.add(session, { plugin: caller.plugin, text, trusted: caller.layer !== "project" });
|
|
48
|
+
},
|
|
49
|
+
instructions: (session) => (loaded.get(session) ?? []).map((i) => ({ ...i, source: { ...i.source } })),
|
|
35
50
|
}));
|
|
36
51
|
},
|
|
37
52
|
async session(ctx) {
|
|
38
53
|
const models = ctx.service("models@1");
|
|
39
|
-
const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd)]);
|
|
54
|
+
const [env, instructions] = await Promise.all([environment(ctx.cwd), allInstructions(ctx.home, ctx.cwd, ctx.config.claudeInstructions === false ? null : os.homedir())]);
|
|
55
|
+
loaded.set(ctx.session.id, instructions.map(({ text, trust, source }) => ({ text, trust, source })));
|
|
56
|
+
await ctx.effect(() => () => void loaded.delete(ctx.session.id));
|
|
40
57
|
const fixed = [segment(env), ...instructions.map((i) => segment(i.text, i.trust, "stable", ID, i.source))];
|
|
41
58
|
// 告诉用户读了哪些指令文件,否则看不出它们生效了
|
|
42
59
|
if (instructions.length > 0)
|
|
@@ -67,6 +84,8 @@ export default definePlugin({
|
|
|
67
84
|
optional: true,
|
|
68
85
|
fn: (messages, hook) => explainEmptyResults(messages, { ...hook.derived[reasonsKey], ...reasons }, ID),
|
|
69
86
|
});
|
|
87
|
+
ctx.kernel.hook(notes.hook(ctx.session.id, `${ID}/notes`));
|
|
88
|
+
await ctx.effect(() => () => notes.drop(ctx.session.id));
|
|
70
89
|
ctx.kernel.hook({
|
|
71
90
|
kind: "transform",
|
|
72
91
|
point: "context.build",
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
// 给模型的说明([Note from jade]
|
|
2
|
-
//
|
|
1
|
+
// 给模型的说明([Note from jade],prompts@1 的 note):命令里做的事(例如用户删掉了一条记忆、撤销了模型改的文件)
|
|
2
|
+
// 要让模型知道,但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
|
|
3
3
|
// 附在最后一条消息后面,并用 ctx.record 写进日志(derived):之后的投影、恢复的会话里它都在同一个位置。
|
|
4
|
-
// 插件之间不共享代码,jade/undo 里也有一份。
|
|
5
4
|
export class Notes {
|
|
6
5
|
/** 会话 → 还没交给模型的说明 */
|
|
7
6
|
#pending = new Map();
|
|
8
|
-
add(sessionId,
|
|
9
|
-
this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []),
|
|
7
|
+
add(sessionId, note) {
|
|
8
|
+
this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), note]);
|
|
9
|
+
}
|
|
10
|
+
/** 会话关闭:还没交给模型的说明不再留着 */
|
|
11
|
+
drop(sessionId) {
|
|
12
|
+
this.#pending.delete(sessionId);
|
|
10
13
|
}
|
|
11
14
|
/** 会话里的 context.project hook;key 是 derived 里的键 */
|
|
12
|
-
hook(sessionId,
|
|
15
|
+
hook(sessionId, key) {
|
|
13
16
|
return {
|
|
14
17
|
kind: "transform",
|
|
15
18
|
point: "context.project",
|
|
@@ -27,7 +30,7 @@ export class Notes {
|
|
|
27
30
|
const notes = state[i];
|
|
28
31
|
if (!notes?.length || m.role !== "user")
|
|
29
32
|
return m;
|
|
30
|
-
const blocks = notes.map((
|
|
33
|
+
const blocks = notes.map((n) => ({ kind: "text", text: `[Note from jade] ${n.text}`, origin: { by: "plugin", id: n.plugin }, trust: n.trusted ? "trusted" : "untrusted" }));
|
|
31
34
|
return { ...m, content: [...m.content, ...blocks] };
|
|
32
35
|
});
|
|
33
36
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "jade/prompts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.js",
|
|
6
6
|
"provides": [
|
|
@@ -9,5 +9,11 @@
|
|
|
9
9
|
"optional": [
|
|
10
10
|
"models@1",
|
|
11
11
|
"ui@1"
|
|
12
|
-
]
|
|
12
|
+
],
|
|
13
|
+
"config": {
|
|
14
|
+
"claudeInstructions": {
|
|
15
|
+
"default": true,
|
|
16
|
+
"description": "when $JADE_HOME has no AGENTS.md or CLAUDE.md, use ~/.claude/CLAUDE.md as the user instructions; false turns this off"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
13
19
|
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// 主体和环境信息都是 stable:环境信息在会话开始时取一次,不在每次请求时更新,
|
|
4
4
|
// 否则它一变,排在后面的整段历史都无法命中提示缓存。
|
|
5
5
|
// 指令文件兼容 AGENTS.md 和 CLAUDE.md,同一目录两者都有时只读 AGENTS.md。
|
|
6
|
-
//
|
|
6
|
+
// 用户层的先找 $JADE_HOME,没有时用 ~/.claude 里的(和 jade/skills 读 ~/.claude/skills 一样,已有的全局指令不用再抄一份)。
|
|
7
|
+
// 用户层的是 trusted;项目层(随仓库分发)的是 untrusted,并记下出处(宪法 7.2)。
|
|
7
8
|
// 项目层从仓库根目录到工作目录逐级查找,上级的在前,离工作目录近的在后。
|
|
8
9
|
import { createHash } from "node:crypto";
|
|
9
10
|
import { realpathSync } from "node:fs";
|
|
@@ -84,10 +85,10 @@ export async function environment(cwd, today = localDate()) {
|
|
|
84
85
|
}
|
|
85
86
|
return lines.join("\n");
|
|
86
87
|
}
|
|
87
|
-
/** 用户层(home
|
|
88
|
-
export async function allInstructions(home, cwd) {
|
|
88
|
+
/** 用户层(home 里,没有时 userHome/.claude 里;userHome 给 null 时不找)和项目层(仓库根目录到 cwd 逐级)的指令文件,按顺序 */
|
|
89
|
+
export async function allInstructions(home, cwd, userHome = os.homedir()) {
|
|
89
90
|
const out = [];
|
|
90
|
-
const user = await instructions(home, home, "user");
|
|
91
|
+
const user = (await instructions(home, home, "user")) ?? (userHome === null ? undefined : await instructions(userHome, path.join(userHome, ".claude"), "user"));
|
|
91
92
|
if (user)
|
|
92
93
|
out.push({ text: `# User instructions (${user.source.path})\n\n${user.text}`, trust: "trusted", source: user.source, file: user.file });
|
|
93
94
|
const root = realPath(await repoRoot(cwd));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// jade/renderers:工具调用和插件事件在对话里怎么显示的注册表。前端查这里;没有登记的用前端的默认显示。
|
|
2
2
|
// 显示的文字可以带 ANSI 颜色,前端负责过滤掉颜色之外的控制序列。
|
|
3
|
-
import { definePlugin } from "
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
4
4
|
const tools = new Map();
|
|
5
5
|
const events = new Map();
|
|
6
6
|
function add(map, key, renderer, plugin, what, onDispose) {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// 从旧 jade 的 plugins/repeats.ts 移植。
|
|
14
14
|
import { createHash } from "node:crypto";
|
|
15
15
|
import path from "node:path";
|
|
16
|
-
import { definePlugin } from "
|
|
16
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
17
17
|
const ID = "jade/repeats";
|
|
18
18
|
/** jade/compact 记录压缩结果用的派生数据键(旧 jade 的约定):记一次就说明历史被压缩过 */
|
|
19
19
|
export const COMPACT_KEY = "jade/compact";
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 下次打开时错过的几次只补一次。不能提问的前端(jade -p)不投递。
|
|
4
4
|
// 管理入口:/schedule 命令和给模型的 schedule_create、schedule_list、schedule_edit 工具,共用同一份存储。
|
|
5
5
|
import path from "node:path";
|
|
6
|
-
import { definePlugin } from "
|
|
6
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
7
7
|
import { parseCron } from "./cron.js";
|
|
8
8
|
import { claimDue, nextAt, sameDir, ScheduleStore } from "./store.js";
|
|
9
9
|
const ID = "jade/schedule";
|
|
@@ -4,7 +4,7 @@ import { realpathSync } from "node:fs";
|
|
|
4
4
|
import { mkdir } from "node:fs/promises";
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { definePlugin } from "
|
|
7
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
8
|
/** 会话 id 对应的临时目录 */
|
|
9
9
|
export const scratchDir = (session) => path.join(os.tmpdir(), "jade-scratch", session);
|
|
10
10
|
export default definePlugin({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// jade/screenshot:截图工具(见 tool.ts)。本机没有能无头截图的浏览器时不注册这个工具。
|
|
2
|
-
// 模型能不能看图片由 jade/models 的 models@1 给出(JADE_VISION
|
|
2
|
+
// 模型能不能看图片由 jade/models 的 models@1 给出(JADE_VISION):不能看时请求里没有这个工具和提示,调用了也不截图。
|
|
3
3
|
// 访问网络的 URL 由 jade/approval 的关卡决定要不要问用户(effect 是 read,路径之外的 URL 会问)。
|
|
4
|
-
import { definePlugin } from "
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
5
5
|
import { findBrowser, screenshotTool } from "./tool.js";
|
|
6
6
|
/** 提示词里的一段:有截图工具时才加 */
|
|
7
7
|
const NOTE = "- When the work is visual (a page, a drawing, styles), take a screenshot of the result and look at it instead of guessing from the source.";
|
|
@@ -19,17 +19,31 @@ export default definePlugin({
|
|
|
19
19
|
if (!browser)
|
|
20
20
|
return;
|
|
21
21
|
const models = ctx.service("models@1");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// 换了模型(/model)之后也按新的配置判断
|
|
26
|
-
available: () => models?.current()?.vision !== false,
|
|
27
|
-
});
|
|
22
|
+
// 换了模型(/model)之后也按新的配置判断
|
|
23
|
+
const available = () => models?.current()?.vision !== false;
|
|
24
|
+
const tool = screenshotTool({ cwd: ctx.cwd, browser, available });
|
|
28
25
|
if (!tool)
|
|
29
26
|
return;
|
|
30
27
|
ctx.kernel.tool(tool);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
// 不能看图片的模型不给这个工具,也不加那句提示:给了它多半会试一次,白费一轮。
|
|
29
|
+
// 排在 jade/prompts 的 context.build 之前(order -1),它组装系统提示词时读到的段落和工具已经是这一次的
|
|
30
|
+
const prompts = ctx.service("prompts@1");
|
|
31
|
+
let note;
|
|
32
|
+
ctx.effect(() => () => note?.());
|
|
33
|
+
ctx.kernel.hook({
|
|
34
|
+
kind: "transform",
|
|
35
|
+
point: "context.build",
|
|
36
|
+
order: -1,
|
|
37
|
+
fn: (req) => {
|
|
38
|
+
const can = available();
|
|
39
|
+
if (can && !note)
|
|
40
|
+
note = prompts?.add({ text: NOTE, stable: false });
|
|
41
|
+
else if (!can && note) {
|
|
42
|
+
note();
|
|
43
|
+
note = undefined;
|
|
44
|
+
}
|
|
45
|
+
return can ? req : { ...req, tools: req.tools.filter((t) => t.name !== tool.name) };
|
|
46
|
+
},
|
|
47
|
+
});
|
|
34
48
|
},
|
|
35
49
|
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 读的时候按 id 合并:标题取最早的一行,时间取最晚的一行;目录按 cwdKey 比较。
|
|
4
4
|
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
5
5
|
import path from "node:path";
|
|
6
|
-
import { definePlugin } from "
|
|
6
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
7
7
|
const TITLE_CHARS = 80;
|
|
8
8
|
/** 比较目录用的键:绝对路径、去掉末尾的分隔符;Windows 上不分大小写 */
|
|
9
9
|
export function cwdKey(cwd) {
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
// jade/shell:bash 工具,以及后台命令的 bash_output、kill_shell;shell@1 给其他插件执行命令。
|
|
2
|
-
|
|
2
|
+
// 后台命令自己结束时告诉用户,并给模型留一句说明(不用它一直轮询);/shells 给用户看和停掉后台命令。
|
|
3
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
3
4
|
import { shellRenderers } from "./renderers.js";
|
|
4
5
|
import { shellService } from "./service.js";
|
|
5
6
|
import { shellTools } from "./shell.js";
|
|
7
|
+
/** 打开着的会话 → 它的后台命令(/shells 用) */
|
|
8
|
+
const sessions = new Map();
|
|
9
|
+
const firstLine = (command) => {
|
|
10
|
+
const line = command.split("\n")[0];
|
|
11
|
+
return line.length > 80 ? `${line.slice(0, 80)}…` : line;
|
|
12
|
+
};
|
|
6
13
|
export default definePlugin({
|
|
7
14
|
process(ctx) {
|
|
8
15
|
// 显示方式和会话无关,登记一次留到进程结束;没有 renderers@1(例如 print)时用前端的默认显示
|
|
@@ -12,10 +19,52 @@ export default definePlugin({
|
|
|
12
19
|
renderers.tool(r);
|
|
13
20
|
// shell@1:找 shell 和杀进程树只有一份(见 service.ts)
|
|
14
21
|
ctx.provide("shell@1", shellService(typeof ctx.config.shell === "string" ? { shell: ctx.config.shell } : {}));
|
|
22
|
+
ctx.service("commands@1")?.register({
|
|
23
|
+
name: "shells",
|
|
24
|
+
args: "[kill <id>]",
|
|
25
|
+
description: "list the background commands the agent started, or stop one",
|
|
26
|
+
instant: true,
|
|
27
|
+
async run(args, { sessions: manager }) {
|
|
28
|
+
const shells = manager.current && sessions.get(manager.current.id);
|
|
29
|
+
const [verb, id] = args.trim().split(/\s+/);
|
|
30
|
+
if (verb === "kill" && id)
|
|
31
|
+
return shells && (await shells.kill(id)) ? `Stopped ${id}.` : `There is no background command ${id}.`;
|
|
32
|
+
const all = shells?.list() ?? [];
|
|
33
|
+
if (all.length === 0)
|
|
34
|
+
return "No background commands.";
|
|
35
|
+
return all.map((b) => `${b.id} ${b.status} ${firstLine(b.command)}`).join("\n");
|
|
36
|
+
},
|
|
37
|
+
});
|
|
15
38
|
},
|
|
16
39
|
async session(ctx) {
|
|
17
40
|
const shell = typeof ctx.config.shell === "string" ? ctx.config.shell : undefined;
|
|
18
|
-
const
|
|
41
|
+
const id = ctx.session.id;
|
|
42
|
+
const { tools, executable, unavailable, shutdown, list, kill } = shellTools({
|
|
43
|
+
cwd: ctx.cwd,
|
|
44
|
+
...(shell ? { shell } : {}),
|
|
45
|
+
onExit(bg) {
|
|
46
|
+
const what = `Background shell ${bg.id} (${firstLine(bg.command)}) exited with code ${bg.exitCode}`;
|
|
47
|
+
ctx.service("ui@1")?.notify(what, bg.exitCode === 0 ? "info" : "warning");
|
|
48
|
+
ctx.service("prompts@1")?.note(id, `${what}. Read its remaining output with bash_output.`);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
sessions.set(id, { list, kill });
|
|
52
|
+
// 子 Agent(jade/task)的 bash:只收命令和时限,不能在后台跑;哪些命令能执行由 jade/task 按审批规则把关(只读的才行)
|
|
53
|
+
const bash = tools.find((t) => t.name === "bash");
|
|
54
|
+
try {
|
|
55
|
+
if (bash) {
|
|
56
|
+
ctx.service("task@1")?.tool({
|
|
57
|
+
...bash,
|
|
58
|
+
description: "Run a read-only shell command in the working directory (for example git log, git diff, git status, ls, rg) and return its exit code and output. " +
|
|
59
|
+
"Commands that could change anything, or that touch paths outside the working directory, are refused.",
|
|
60
|
+
inputSchema: { type: "object", properties: { command: { type: "string" }, timeout_ms: { type: "integer" } }, required: ["command"], additionalProperties: false },
|
|
61
|
+
execute: (input, c) => bash.execute({ command: input.command, timeout_ms: input.timeout_ms }, c),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// jade/task 被隔离时它的服务已失效:子 Agent 没有 bash,主会话的照常
|
|
67
|
+
}
|
|
19
68
|
if (unavailable)
|
|
20
69
|
ctx.service("ui@1")?.notify(unavailable, "warning");
|
|
21
70
|
for (const tool of tools)
|
|
@@ -28,6 +77,9 @@ export default definePlugin({
|
|
|
28
77
|
ctx.service("prompts@1")?.add({ text: lines.join("\n") });
|
|
29
78
|
}
|
|
30
79
|
// 会话关闭(换会话、退出)时杀掉还在运行的后台命令
|
|
31
|
-
await ctx.effect(() =>
|
|
80
|
+
await ctx.effect(() => () => {
|
|
81
|
+
sessions.delete(id);
|
|
82
|
+
shutdown();
|
|
83
|
+
});
|
|
32
84
|
},
|
|
33
85
|
});
|
|
@@ -23,7 +23,7 @@ export function shellTools(opts) {
|
|
|
23
23
|
// 找不到 shell 时不提供这几个工具:否则模型每次调用都要用户先批准,执行时才失败,模型还会反复重试
|
|
24
24
|
if (!found) {
|
|
25
25
|
const hint = process.platform === "win32" ? "install Git for Windows, or set JADE_SHELL to the path of a bash executable" : "set JADE_SHELL to the path of a shell";
|
|
26
|
-
return { tools: [], unavailable: `${shell} was not found, so the bash tool is disabled; ${hint}`, shutdown() { } };
|
|
26
|
+
return { tools: [], unavailable: `${shell} was not found, so the bash tool is disabled; ${hint}`, shutdown() { }, list: () => [], kill: async () => false };
|
|
27
27
|
}
|
|
28
28
|
const executable = found;
|
|
29
29
|
const env = shellEnv(executable);
|
|
@@ -49,11 +49,21 @@ export function shellTools(opts) {
|
|
|
49
49
|
void Promise.all([collect(bg.proc.stdout, append, false), collect(bg.proc.stderr, append, false)]).catch(() => { });
|
|
50
50
|
void bg.proc.exited.then((code) => {
|
|
51
51
|
bg.exitCode = code;
|
|
52
|
+
if (!bg.killed)
|
|
53
|
+
opts.onExit?.({ id, command, exitCode: code });
|
|
52
54
|
});
|
|
53
55
|
background.set(id, bg);
|
|
54
56
|
return id;
|
|
55
57
|
}
|
|
56
58
|
const status = (bg) => (bg.exitCode === undefined ? "running" : `exited with code ${bg.exitCode}`);
|
|
59
|
+
/** 停掉一个还在运行的后台命令,等它退出 */
|
|
60
|
+
const stop = async (bg) => {
|
|
61
|
+
if (bg.exitCode !== undefined)
|
|
62
|
+
return;
|
|
63
|
+
bg.killed = true;
|
|
64
|
+
killTree(executable, env, bg.proc.pid);
|
|
65
|
+
await bg.proc.exited;
|
|
66
|
+
};
|
|
57
67
|
const bash = {
|
|
58
68
|
name: "bash",
|
|
59
69
|
description: `Run a shell command with ${shell} -c in the working directory and return its exit code, stdout and stderr. ` +
|
|
@@ -157,10 +167,7 @@ export function shellTools(opts) {
|
|
|
157
167
|
const bg = find(id);
|
|
158
168
|
if (!bg)
|
|
159
169
|
return unknown(id);
|
|
160
|
-
|
|
161
|
-
killTree(executable, env, bg.proc.pid);
|
|
162
|
-
await bg.proc.exited;
|
|
163
|
-
}
|
|
170
|
+
await stop(bg);
|
|
164
171
|
return { content: `${id} stopped (${status(bg)})` };
|
|
165
172
|
},
|
|
166
173
|
};
|
|
@@ -170,9 +177,20 @@ export function shellTools(opts) {
|
|
|
170
177
|
executable,
|
|
171
178
|
run,
|
|
172
179
|
shutdown() {
|
|
173
|
-
for (const bg of background.values())
|
|
174
|
-
if (bg.exitCode
|
|
175
|
-
|
|
180
|
+
for (const bg of background.values()) {
|
|
181
|
+
if (bg.exitCode !== undefined)
|
|
182
|
+
continue;
|
|
183
|
+
bg.killed = true;
|
|
184
|
+
killTree(executable, env, bg.proc.pid);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
list: () => [...background.values()].map((bg) => ({ id: bg.id, command: bg.command, status: status(bg) })),
|
|
188
|
+
async kill(id) {
|
|
189
|
+
const bg = background.get(id);
|
|
190
|
+
if (!bg)
|
|
191
|
+
return false;
|
|
192
|
+
await stop(bg);
|
|
193
|
+
return true;
|
|
176
194
|
},
|
|
177
195
|
};
|
|
178
196
|
}
|