@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
|
@@ -3,18 +3,27 @@
|
|
|
3
3
|
// jade/fs 不再保存检查点,这里自己观察:tool.before 在 write_file / edit_file / multi_edit 执行前记下文件原来的内容,
|
|
4
4
|
// tool.after 在执行成功后记下新内容的哈希,存成快照(checkpoint.ts)。tool.before 在审批之前执行,
|
|
5
5
|
// 所以等用户批准期间别人对文件的改动会算进"原来的内容"。
|
|
6
|
+
// shell 命令改的文件:在 git 仓库里,每轮第一次 bash 之前给工作区拍一张快照(snapshot.ts),撤销时把之后变了的文件恢复回去。
|
|
6
7
|
import { readFile } from "node:fs/promises";
|
|
7
8
|
import { realpathSync } from "node:fs";
|
|
8
9
|
import path from "node:path";
|
|
9
|
-
import { definePlugin } from "
|
|
10
|
-
import { Checkpoints, lastTurnChanges, sha256, turnToolNames } from "./checkpoint.js";
|
|
11
|
-
import {
|
|
10
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
11
|
+
import { Checkpoints, lastTurnChanges, safe, sha256, turnToolNames } from "./checkpoint.js";
|
|
12
|
+
import { Snapshots } from "./snapshot.js";
|
|
12
13
|
import { dim, green } from "./style.js";
|
|
13
14
|
/** 会改文件、参数里有 path 的工具 */
|
|
14
15
|
export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
15
|
-
/** shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
|
|
16
|
+
/** 没有工作区快照(不在 git 仓库里、配置关掉了)时,shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
|
|
16
17
|
const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
|
|
17
|
-
|
|
18
|
+
/** 拍工作区快照的时限;过了这一轮就没有快照(不影响命令执行) */
|
|
19
|
+
const SNAPSHOT_TIMEOUT_MS = 60_000;
|
|
20
|
+
/** 对话框和结果里最多列出这么多个文件 */
|
|
21
|
+
const MAX_LISTED = 30;
|
|
22
|
+
/** 比较两个路径是不是同一个文件用的写法:git 给的和工具参数里的大小写、斜杠、短文件名可能不同 */
|
|
23
|
+
function same(p) {
|
|
24
|
+
const full = path.join(realPath(path.dirname(p)), path.basename(p));
|
|
25
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
26
|
+
}
|
|
18
27
|
/** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
|
|
19
28
|
function display(cwd, full) {
|
|
20
29
|
const rel = path.relative(cwd, full);
|
|
@@ -46,38 +55,70 @@ export default definePlugin({
|
|
|
46
55
|
const cwd = realPath(ctx.cwd);
|
|
47
56
|
ctx.service("commands@1")?.register({
|
|
48
57
|
name: "undo",
|
|
49
|
-
description: "restore the files the agent
|
|
58
|
+
description: "restore the files the agent changed in its last turn; in a git repository that includes what its shell commands changed (run again to go further back)",
|
|
50
59
|
async run(_args, { sessions, ui, signal }) {
|
|
51
60
|
const s = sessions.current;
|
|
52
61
|
if (!s)
|
|
53
62
|
return "No session is open.";
|
|
63
|
+
const snapshots = new Snapshots(path.join(ctx.home, "checkpoints", safe(s.id)), ctx.cwd);
|
|
54
64
|
const undone = await checkpoints.undone(s.id);
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
const snaps = await snapshots.list();
|
|
66
|
+
const entries = [...(await checkpoints.list(s.id)), ...snaps].sort((x, y) => y.at - x.at);
|
|
67
|
+
/** 这一轮文件工具的快照(最新的在前)、命令之前的工作区快照、工作区在那之后变了的文件 */
|
|
68
|
+
let cps;
|
|
69
|
+
let snap;
|
|
70
|
+
let tree;
|
|
71
|
+
for (;;) {
|
|
72
|
+
const hits = lastTurnChanges(s.state.messages, entries, undone);
|
|
73
|
+
cps = hits.filter((h) => "path" in h);
|
|
74
|
+
snap = hits.find((h) => "tree" in h);
|
|
75
|
+
tree = snap ? await snapshots.changed(snap.tree) : [];
|
|
76
|
+
if (!snap || cps.length > 0 || tree.length > 0)
|
|
77
|
+
break;
|
|
78
|
+
// 命令什么都没改(或者已经改回去了)的一轮:没有可撤销的,看更早的一轮
|
|
79
|
+
undone.add(snap.toolUseId);
|
|
80
|
+
await checkpoints.markUndone(s.id, [snap.toolUseId]);
|
|
81
|
+
}
|
|
82
|
+
// 这个会话里拍到过快照,说明命令的改动撤销得了,不用提醒
|
|
83
|
+
if (cps.length === 0 && !snap)
|
|
84
|
+
return dim(snaps.length === 0 && turnToolNames(s.state.messages).has("bash") ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
|
|
85
|
+
// 没拍到快照(不在 git 仓库里、配置关掉了)的那一轮跑过命令时提醒一句
|
|
86
|
+
const shellToo = !snap && turnToolNames(s.state.messages, cps[0].toolUseId).has("bash");
|
|
87
|
+
// 工作区快照覆盖到的文件按快照恢复(那是命令动它之前的内容):拍快照之后文件工具对它的修改不再单独恢复
|
|
88
|
+
const inTree = new Set(tree.map((c) => same(c.path)));
|
|
89
|
+
const fileCps = cps.filter((cp) => !(snap && cp.at > snap.at && inTree.has(same(cp.path))));
|
|
59
90
|
/** 文件 → 这一轮最早的快照(恢复成它的 before);cps 是最新的在前 */
|
|
60
91
|
const files = new Map();
|
|
61
|
-
for (const cp of
|
|
92
|
+
for (const cp of fileCps)
|
|
62
93
|
files.set(cp.path, cp);
|
|
94
|
+
/** 要恢复的全部文件:路径 → 恢复之后还在不在。先按工作区快照,再按文件快照(它更早,说了算) */
|
|
95
|
+
const plan = new Map();
|
|
96
|
+
for (const c of tree)
|
|
97
|
+
plan.set(same(c.path), { path: c.path, gone: c.added });
|
|
98
|
+
for (const cp of files.values())
|
|
99
|
+
plan.set(same(cp.path), { path: cp.path, gone: cp.before === null });
|
|
63
100
|
// 恢复前就算好显示的名字:恢复时会删掉 agent 新建的文件。文件可能已经不在了(agent 删掉的):解析它所在的目录
|
|
64
|
-
const shown = new Map([...
|
|
101
|
+
const shown = new Map([...plan.values()].map((f) => [f.path, display(cwd, path.join(realPath(path.dirname(f.path)), path.basename(f.path)))]));
|
|
65
102
|
const show = (p) => shown.get(p) ?? display(cwd, p);
|
|
66
103
|
// 只有最新一次写入之后的内容能和文件现在的样子比较
|
|
67
104
|
const latest = new Map();
|
|
68
|
-
for (const cp of
|
|
105
|
+
for (const cp of fileCps)
|
|
69
106
|
if (!latest.has(cp.path))
|
|
70
107
|
latest.set(cp.path, cp);
|
|
71
108
|
const changed = [];
|
|
109
|
+
// 工作区快照之后变了的文件多半是命令改的,下面的问题里一起说明,不算"别人改过"
|
|
72
110
|
for (const cp of latest.values())
|
|
73
|
-
if (!(await checkpoints.unchangedSince(cp)))
|
|
111
|
+
if (!inTree.has(same(cp.path)) && !(await checkpoints.unchangedSince(cp)))
|
|
74
112
|
changed.push(show(cp.path));
|
|
75
|
-
const
|
|
76
|
-
|
|
113
|
+
const rows = [...plan.values()].map((f) => ` ${f.gone ? "delete " : "restore"} ${show(f.path)}`);
|
|
114
|
+
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join("\n") : rows.join("\n");
|
|
115
|
+
if (plan.size > 1 || changed.length > 0 || tree.length > 0) {
|
|
77
116
|
const ok = await ui.select({
|
|
78
117
|
title: changed.length > 0
|
|
79
118
|
? `${changed.join(", ")} changed since the agent wrote ${changed.length > 1 ? "them" : "it"}. Undo anyway and discard those changes too?`
|
|
80
|
-
:
|
|
119
|
+
: tree.length > 0
|
|
120
|
+
? `Undo the agent's last turn (${plan.size} file${plan.size > 1 ? "s" : ""})? It ran shell commands: these files go back to how they were before the first command, and edits you made to them since then are lost too.`
|
|
121
|
+
: `Undo the agent's last turn of changes (${plan.size} files)?`,
|
|
81
122
|
detail: list,
|
|
82
123
|
options: [
|
|
83
124
|
{ label: "No", value: "no" },
|
|
@@ -87,13 +128,18 @@ export default definePlugin({
|
|
|
87
128
|
if (ok !== "yes")
|
|
88
129
|
return;
|
|
89
130
|
}
|
|
131
|
+
if (snap)
|
|
132
|
+
await snapshots.restore(snap.tree, tree);
|
|
90
133
|
// 最新的先恢复:同一个文件最后恢复成这一轮最早的 before
|
|
91
|
-
for (const cp of
|
|
134
|
+
for (const cp of fileCps)
|
|
92
135
|
await checkpoints.restore(cp);
|
|
93
|
-
await checkpoints.markUndone(s.id, cps.map((c) => c.toolUseId));
|
|
94
|
-
const what = [...
|
|
95
|
-
|
|
96
|
-
|
|
136
|
+
await checkpoints.markUndone(s.id, [...cps.map((c) => c.toolUseId), ...(snap ? [snap.toolUseId] : [])]);
|
|
137
|
+
const what = [...plan.values()].slice(0, MAX_LISTED).map((f) => (f.gone ? `deleted ${show(f.path)} (you had created it)` : `restored ${show(f.path)} to its content before your changes`));
|
|
138
|
+
if (plan.size > MAX_LISTED)
|
|
139
|
+
what.push(`and ${plan.size - MAX_LISTED} more files`);
|
|
140
|
+
ctx.service("prompts@1")?.note(s.id, `The user undid your changes from your last turn: ${what.join("; ")}. Read these files again before changing them.`);
|
|
141
|
+
const title = snap ? `Undid the last turn (${plan.size} file${plan.size > 1 ? "s" : ""}):` : `Undid ${cps.length} change${cps.length > 1 ? "s" : ""} from the last turn:`;
|
|
142
|
+
return green(title) + "\n" + dim(list) + (shellToo ? `\n${dim(SHELL_NOTE)}` : "");
|
|
97
143
|
},
|
|
98
144
|
});
|
|
99
145
|
},
|
|
@@ -102,13 +148,26 @@ export default definePlugin({
|
|
|
102
148
|
const sessionId = ctx.session.id;
|
|
103
149
|
/** toolUseId → 执行前的内容,执行成功后存成快照 */
|
|
104
150
|
const before = new Map();
|
|
151
|
+
const snapshots = new Snapshots(path.join(ctx.home, "checkpoints", safe(sessionId)), ctx.cwd);
|
|
152
|
+
/** 这一轮已经拍过工作区快照 */
|
|
153
|
+
let snapped = false;
|
|
105
154
|
ctx.kernel.hook({
|
|
106
155
|
kind: "transform",
|
|
107
156
|
point: "tool.before",
|
|
108
157
|
// 排在 jade/approval 把路径换成绝对路径之后;出错时只是没有快照,不影响工具执行
|
|
109
158
|
order: 50,
|
|
110
159
|
optional: true,
|
|
160
|
+
// 第一次给大仓库拍快照要把每个文件算一遍哈希
|
|
161
|
+
timeoutMs: SNAPSHOT_TIMEOUT_MS,
|
|
111
162
|
fn: async (call) => {
|
|
163
|
+
// 每轮第一次跑命令之前给工作区拍一张快照:命令改了什么,/undo 时和它一比就知道
|
|
164
|
+
if (call.name === "bash" && ctx.config.shell !== false && !snapped) {
|
|
165
|
+
snapped = true;
|
|
166
|
+
const tree = await snapshots.take();
|
|
167
|
+
if (tree)
|
|
168
|
+
await snapshots.save({ toolUseId: call.toolUseId, tree, at: performance.timeOrigin + performance.now() });
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
112
171
|
if (!WRITE_TOOLS.has(call.name))
|
|
113
172
|
return;
|
|
114
173
|
const p = call.input?.path;
|
|
@@ -137,7 +196,15 @@ export default definePlugin({
|
|
|
137
196
|
},
|
|
138
197
|
});
|
|
139
198
|
// 被拒绝、被中断的调用不经过 tool.after:一轮结束时还留着的都不会再用到,不然整个文件的内容一直占着内存
|
|
140
|
-
ctx.kernel.hook({
|
|
141
|
-
|
|
199
|
+
ctx.kernel.hook({
|
|
200
|
+
kind: "observe",
|
|
201
|
+
point: "event",
|
|
202
|
+
fn: (e) => {
|
|
203
|
+
if (e.type !== "turn.ended")
|
|
204
|
+
return;
|
|
205
|
+
before.clear();
|
|
206
|
+
snapped = false;
|
|
207
|
+
},
|
|
208
|
+
});
|
|
142
209
|
},
|
|
143
210
|
});
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "jade/undo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.js",
|
|
6
6
|
"optional": [
|
|
7
|
-
"commands@1"
|
|
8
|
-
|
|
7
|
+
"commands@1",
|
|
8
|
+
"prompts@1"
|
|
9
|
+
],
|
|
10
|
+
"config": {
|
|
11
|
+
"shell": {
|
|
12
|
+
"default": true,
|
|
13
|
+
"description": "in a git repository, snapshot the working tree before the first shell command of each turn so that /undo also restores what the commands changed; false turns this off"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
9
16
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// 工作区快照:shell 命令改了什么无从得知,所以每轮第一次跑命令之前给整个工作区拍一张快照,/undo 时把之后变了的文件恢复回去。
|
|
2
|
+
//
|
|
3
|
+
// 快照存在 $JADE_HOME 里的一个影子 git 仓库(<dir>/shadow/.git),工作区指向项目的仓库根目录:
|
|
4
|
+
// 项目自己的 .git(暂存区、提交、stash)一点不碰。只在 git 仓库里做:要靠 .gitignore 跳过 node_modules 这类目录,
|
|
5
|
+
// 对象库借用项目的(alternates),已经提交过的内容不再存一份。被忽略的文件不在快照里,撤销不了。
|
|
6
|
+
// 快照是 tree 对象,不建提交;哪一轮对应哪个 tree 记在 snapshots.json。
|
|
7
|
+
import { execFile } from "node:child_process";
|
|
8
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
const FILE = "snapshots.json";
|
|
11
|
+
/** 一次 git 命令最多带这么多路径:Windows 的命令行长度有限 */
|
|
12
|
+
const CHUNK = 100;
|
|
13
|
+
export class Snapshots {
|
|
14
|
+
#dir;
|
|
15
|
+
#cwd;
|
|
16
|
+
/** 影子仓库准备好之后的 git 环境;不在 git 仓库里(或没有 git)时 undefined */
|
|
17
|
+
#ready;
|
|
18
|
+
/** dir:这个会话的快照目录;cwd:会话的工作目录 */
|
|
19
|
+
constructor(dir, cwd) {
|
|
20
|
+
this.#dir = dir;
|
|
21
|
+
this.#cwd = cwd;
|
|
22
|
+
}
|
|
23
|
+
#prepare() {
|
|
24
|
+
return (this.#ready ??= (async () => {
|
|
25
|
+
let root;
|
|
26
|
+
let objects;
|
|
27
|
+
try {
|
|
28
|
+
root = path.resolve((await git(["rev-parse", "--show-toplevel"], { cwd: this.#cwd })).trim());
|
|
29
|
+
objects = path.resolve(this.#cwd, (await git(["rev-parse", "--git-common-dir"], { cwd: this.#cwd })).trim(), "objects");
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const shadow = path.join(this.#dir, "shadow");
|
|
35
|
+
const gitDir = path.join(shadow, ".git");
|
|
36
|
+
const env = { ...process.env, GIT_DIR: gitDir, GIT_WORK_TREE: root };
|
|
37
|
+
delete env.GIT_INDEX_FILE;
|
|
38
|
+
try {
|
|
39
|
+
await readFile(path.join(gitDir, "HEAD"));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
await mkdir(shadow, { recursive: true });
|
|
43
|
+
await git(["init", "--quiet", shadow], { cwd: shadow });
|
|
44
|
+
// 原样存、原样取:不做换行转换,不跑 LFS 之类的 filter。info/attributes 比工作区里的 .gitattributes 优先
|
|
45
|
+
await mkdir(path.join(gitDir, "info"), { recursive: true });
|
|
46
|
+
await writeFile(path.join(gitDir, "info", "attributes"), "* -text -eol -filter -ident -working-tree-encoding\n");
|
|
47
|
+
await writeFile(path.join(gitDir, "objects", "info", "alternates"), `${objects.split(path.sep).join("/")}\n`);
|
|
48
|
+
for (const [k, v] of [["core.autocrlf", "false"], ["core.safecrlf", "false"], ["core.longpaths", "true"], ["gc.auto", "0"]]) {
|
|
49
|
+
await git(["config", k, v], { cwd: root, env });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { env, root };
|
|
53
|
+
})());
|
|
54
|
+
}
|
|
55
|
+
/** 给工作区拍一张快照,返回 tree;不在 git 仓库里时 undefined */
|
|
56
|
+
async take() {
|
|
57
|
+
const r = await this.#prepare();
|
|
58
|
+
if (!r)
|
|
59
|
+
return undefined;
|
|
60
|
+
await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {
|
|
61
|
+
// 有的文件加不进去(被别的程序锁着、嵌套的仓库):其余的照样拍
|
|
62
|
+
});
|
|
63
|
+
return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
|
|
64
|
+
}
|
|
65
|
+
/** 工作区现在和快照 tree 相比变了的文件 */
|
|
66
|
+
async changed(tree) {
|
|
67
|
+
const r = await this.#prepare();
|
|
68
|
+
const now = await this.take();
|
|
69
|
+
if (!r || !now || now === tree)
|
|
70
|
+
return [];
|
|
71
|
+
const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", tree, now], { cwd: r.root, env: r.env });
|
|
72
|
+
const parts = out.split("\0");
|
|
73
|
+
const changes = [];
|
|
74
|
+
for (let i = 0; i + 1 < parts.length; i += 2)
|
|
75
|
+
changes.push({ path: path.join(r.root, parts[i + 1]), added: parts[i] === "A" });
|
|
76
|
+
return changes;
|
|
77
|
+
}
|
|
78
|
+
/** 把这些文件恢复成快照 tree 里的样子:之后新出现的删掉,其余的取回 */
|
|
79
|
+
async restore(tree, changes) {
|
|
80
|
+
const r = await this.#prepare();
|
|
81
|
+
if (!r)
|
|
82
|
+
return;
|
|
83
|
+
for (const c of changes)
|
|
84
|
+
if (c.added)
|
|
85
|
+
await rm(c.path, { force: true });
|
|
86
|
+
const back = changes.filter((c) => !c.added).map((c) => path.relative(r.root, c.path).split(path.sep).join("/"));
|
|
87
|
+
for (let i = 0; i < back.length; i += CHUNK) {
|
|
88
|
+
await git(["--literal-pathspecs", "checkout", tree, "--", ...back.slice(i, i + CHUNK)], { cwd: r.root, env: r.env });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** 本会话记下的快照,最新的在前 */
|
|
92
|
+
async list() {
|
|
93
|
+
try {
|
|
94
|
+
const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8"));
|
|
95
|
+
return Array.isArray(all) ? all.sort((a, b) => b.at - a.at) : [];
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async save(snapshot) {
|
|
102
|
+
const all = await this.list();
|
|
103
|
+
await mkdir(this.#dir, { recursive: true });
|
|
104
|
+
await writeFile(path.join(this.#dir, FILE), JSON.stringify([snapshot, ...all]));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function git(args, opts) {
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
execFile("git", args, { cwd: opts.cwd, env: opts.env, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (err, stdout) => (err ? reject(err) : resolve(stdout)));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
// jade/update:启动时(每天最多一次)查 npm 上有没有更新的 jade,有就在后台升级,下次启动生效;
|
|
2
2
|
// 配置 auto 为 false 或 JADE_AUTO_UPDATE=off 时只提示。仓库里的开发版和 npm link 既不提示也不升级。
|
|
3
|
-
//
|
|
4
|
-
import { definePlugin } from "
|
|
5
|
-
import { autoUpdate, autoUpdateEnabled, detectInstall, newerVersion, VERSION } from "../../dist/update.js";
|
|
3
|
+
// 查询和升级经核心的 update@1 服务,和 jade update 是同一套;不想要它就 jade plugins disable jade/update。
|
|
4
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
6
5
|
/** 查一次,按结果提示;出什么错都不打扰用户 */
|
|
7
6
|
export async function checkForUpdate(o) {
|
|
8
|
-
const
|
|
9
|
-
if (install
|
|
7
|
+
const { update } = o;
|
|
8
|
+
if (update.install === "dev")
|
|
10
9
|
return;
|
|
11
|
-
const latest = await
|
|
10
|
+
const latest = await update.newer().catch(() => undefined);
|
|
12
11
|
if (!latest)
|
|
13
12
|
return;
|
|
14
|
-
const manual = () => o.notify(`jade ${latest} is available (you have ${
|
|
15
|
-
if (!o.auto || !
|
|
13
|
+
const manual = () => o.notify(`jade ${latest} is available (you have ${update.version}); run jade update`);
|
|
14
|
+
if (!o.auto || !update.autoEnabled())
|
|
16
15
|
return manual();
|
|
17
|
-
const result = await
|
|
16
|
+
const result = await update.upgrade(latest).catch(() => undefined);
|
|
18
17
|
if (result?.status === "installed")
|
|
19
18
|
o.notify(`jade ${latest} was installed in the background; restart jade to use it`);
|
|
20
19
|
else if (result?.status === "failed")
|
|
@@ -29,6 +28,6 @@ export default definePlugin({
|
|
|
29
28
|
if (!ui?.interactive)
|
|
30
29
|
return;
|
|
31
30
|
// 作用域关闭后 ui 失效,再提示会抛错:那时也没人看了
|
|
32
|
-
void checkForUpdate({
|
|
31
|
+
void checkForUpdate({ update: ctx.require("update@1"), auto: ctx.config.auto !== false, notify: (m, level) => ui.notify(m, level) }).catch(() => { });
|
|
33
32
|
},
|
|
34
33
|
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// web_search 总是注册,但只在配置了搜索服务时发给模型(context.build 里去掉):这样 /search 设置之后当前会话立即能用,
|
|
5
5
|
// 不用重开会话;会话的工具清单在创建时就定了。
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { definePlugin } from "
|
|
7
|
+
import { definePlugin } from "@uncarved/jade-api";
|
|
8
8
|
import { envWithFile, updateEnvFile } from "./env.js";
|
|
9
9
|
import { webRenderers } from "./renderers.js";
|
|
10
10
|
import { parseOrder, parseResults, SEARCH_PROVIDERS, webTools } from "./web.js";
|
|
Binary file
|
|
@@ -189,6 +189,14 @@ class Context:
|
|
|
189
189
|
"""本轮完全结束后执行一次(会话级)"""
|
|
190
190
|
self._conn.call(self.scope, "$session", "afterTurn", [fn])
|
|
191
191
|
|
|
192
|
+
def state(self) -> dict:
|
|
193
|
+
"""会话现在的状态(阶段、消息、还没做完的工具调用、derived)。scope 打开期间(session 函数里)还没有"""
|
|
194
|
+
return self._conn.call(self.scope, "$session", "state", [])
|
|
195
|
+
|
|
196
|
+
def subscribe(self, fn: Callable[[dict], Any]) -> None:
|
|
197
|
+
"""订阅运行时的通知(事件、增量、工具进度、awaiting),作用域关闭时自动取消(会话级)"""
|
|
198
|
+
self._conn.call(self.scope, "$session", "subscribe", [fn])
|
|
199
|
+
|
|
192
200
|
def on_close(self, fn: Callable[[], Any]) -> None:
|
|
193
201
|
"""作用域关闭时执行,逆序"""
|
|
194
202
|
self._on_close.append(fn)
|