@uncarved/jade 0.3.1 → 0.4.1
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 +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +86 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +496 -36
package/plugins/tui/vendor.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// ink 和 react 只经这里导入。构建时 scripts/build-plugins.
|
|
1
|
+
// ink 和 react 只经这里导入。构建时 scripts/build-plugins.ts 把编译出的 vendor.js 连同依赖打成一个文件:
|
|
2
2
|
// 逐个解析 ink 和它拉进来的整个 es-toolkit/compat 要约 470ms,打包后约 120ms(docs/review.md #28)。
|
|
3
3
|
// 两者必须打在同一个文件里,否则 ink 和界面各用一份 react,hooks 会失效。
|
|
4
4
|
// 只用到类型的从 "ink"/"react" 直接 import type,不经这里。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// 文件快照:write_file / edit_file / multi_edit 执行前保存原内容,/undo 用它恢复。从旧 jade 的 tools/checkpoint.ts 移植。
|
|
2
2
|
//
|
|
3
|
-
// 快照放在 <dir>/<sessionId>/<toolUseId>.json
|
|
3
|
+
// 快照放在 <dir>/<sessionId>/<toolUseId>.json(子 Agent 写的放在根会话的目录里,via 是根会话里打开它的调用),不进日志:文件可能很大,而撤销是用户的操作,不影响插件决策。
|
|
4
4
|
// 哪些修改已经撤销记在同一个目录的 undone.json 里(命令拿不到会话日志),恢复会话后仍然有效。
|
|
5
5
|
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
@@ -19,6 +19,8 @@ export interface Checkpoint {
|
|
|
19
19
|
afterHash: string;
|
|
20
20
|
/** 写入完成的时间(毫秒,带小数),决定撤销顺序 */
|
|
21
21
|
at: number;
|
|
22
|
+
/** 子 Agent(子会话)写的:根会话里打开它的那次工具调用(例如 task)。/undo 按它找到是哪一轮 */
|
|
23
|
+
via?: string;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export const sha256 = (s: string) => createHash("sha256").update(s).digest("hex");
|
|
@@ -50,7 +52,9 @@ export class Checkpoints {
|
|
|
50
52
|
for (const n of names) {
|
|
51
53
|
if (!n.endsWith(".json") || n === UNDONE) continue;
|
|
52
54
|
try {
|
|
53
|
-
|
|
55
|
+
const cp = JSON.parse(await readFile(path.join(dir, n), "utf8")) as Partial<Checkpoint> | null;
|
|
56
|
+
// 同一个目录里还有别的文件(工作区快照的 snapshots.json):只认快照的样子
|
|
57
|
+
if (typeof cp?.toolUseId === "string" && typeof cp.path === "string" && typeof cp.at === "number") out.push(cp as Checkpoint);
|
|
54
58
|
} catch {
|
|
55
59
|
// 写到一半的快照,忽略
|
|
56
60
|
}
|
|
@@ -101,7 +105,7 @@ const UNDONE = "undone.json";
|
|
|
101
105
|
* 最近一轮还有没撤销的修改的快照(最新的在前)。一轮从一条用户自己写的消息开始;
|
|
102
106
|
* 只有工具结果、或者只有插件、宿主加的说明(例如自动续写的提示)的消息不算新的一轮。没有可撤销的返回空数组
|
|
103
107
|
*/
|
|
104
|
-
export function lastTurnChanges<T extends { toolUseId: string }>(messages: readonly Message[], checkpoints: readonly T[], undone: ReadonlySet<string>): T[] {
|
|
108
|
+
export function lastTurnChanges<T extends { toolUseId: string; via?: string }>(messages: readonly Message[], checkpoints: readonly T[], undone: ReadonlySet<string>): T[] {
|
|
105
109
|
const pending = checkpoints.filter((c) => !undone.has(c.toolUseId));
|
|
106
110
|
if (pending.length === 0) return [];
|
|
107
111
|
let ids = new Set<string>();
|
|
@@ -113,7 +117,7 @@ export function lastTurnChanges<T extends { toolUseId: string }>(messages: reado
|
|
|
113
117
|
}
|
|
114
118
|
const startsTurn = m.content.some((b) => b.kind !== "tool_result" && b.origin.by === "user");
|
|
115
119
|
if (!startsTurn) continue;
|
|
116
|
-
const hits = pending.filter((c) => ids.has(c.toolUseId));
|
|
120
|
+
const hits = pending.filter((c) => ids.has(c.via ?? c.toolUseId));
|
|
117
121
|
if (hits.length > 0) return hits;
|
|
118
122
|
ids = new Set();
|
|
119
123
|
}
|
package/plugins/undo/index.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
// jade/undo:/undo 把模型上一轮改过的文件恢复原样(再执行一次撤销更早的一轮)。
|
|
2
2
|
//
|
|
3
|
-
// jade/fs 不再保存检查点,这里自己观察:tool.before
|
|
3
|
+
// jade/fs 不再保存检查点,这里自己观察:tool.before 在写文件的工具(meta.writes,例如 write_file)执行前记下文件原来的内容,
|
|
4
4
|
// tool.after 在执行成功后记下新内容的哈希,存成快照(checkpoint.ts)。tool.before 在审批之前执行,
|
|
5
5
|
// 所以等用户批准期间别人对文件的改动会算进"原来的内容"。
|
|
6
|
-
// shell 命令改的文件:在 git
|
|
6
|
+
// shell 命令改的文件:在 git 仓库里,每轮第一次执行命令(meta.shell,例如 bash)之前给工作区拍一张快照(snapshot.ts),一轮结束时再拍一张,
|
|
7
|
+
// 撤销时把这两张之间变了的文件恢复回去。子 Agent 的命令算在根会话的这一轮里:整棵会话树一轮只在第一次跑命令之前拍一张。
|
|
8
|
+
// undo@1:用户自己执行的命令(/sh)前后各拍一张,单独算一步撤销。
|
|
9
|
+
// 认工具靠它注册时给的 meta,不认工具名:换掉提供文件工具的插件照样能撤销。
|
|
7
10
|
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
8
12
|
import { readFile } from "node:fs/promises";
|
|
9
13
|
import { realpathSync } from "node:fs";
|
|
10
14
|
import path from "node:path";
|
|
11
|
-
import { definePlugin } from "@uncarved/jade-api";
|
|
12
|
-
import type { Commands, Prompts } from "@uncarved/jade-api/services";
|
|
15
|
+
import { definePlugin, type UI } from "@uncarved/jade-api";
|
|
16
|
+
import type { Commands, Prompts, Undo } from "@uncarved/jade-api/services";
|
|
13
17
|
import { Checkpoints, lastTurnChanges, safe, sha256, turnToolNames, type Checkpoint } from "./checkpoint.ts";
|
|
14
|
-
import {
|
|
15
|
-
import { dim,
|
|
16
|
-
|
|
17
|
-
/** 会改文件、参数里有 path 的工具 */
|
|
18
|
-
export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
18
|
+
import { snapshotsAt, type Change, type Snapshot, type Snapshots } from "./snapshot.ts";
|
|
19
|
+
import { dim, ok } from "./style.ts";
|
|
19
20
|
|
|
20
21
|
/** 没有工作区快照(不在 git 仓库里、配置关掉了)时,shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
|
|
21
22
|
const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
|
|
@@ -45,6 +46,33 @@ function realPath(p: string): string {
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/** 打开着的会话:会话 id → 它的 tool.decided 事件 id → toolUseId(子会话的 parent 只给事件 id) */
|
|
50
|
+
const decidedBy = new Map<string, Map<string, string>>();
|
|
51
|
+
/** 打开着的会话:会话 id → 它的改动记在哪个根会话、算作根会话里哪次调用的 */
|
|
52
|
+
const links = new Map<string, () => { root: string; via?: string }>();
|
|
53
|
+
/** 根会话 id → 这一轮的工作区快照:整棵会话树(包括并行的子 Agent)第一次跑命令之前拍的那张。根会话这一轮结束时清掉 */
|
|
54
|
+
const turnSnaps = new Map<string, Promise<{ snapshots: Snapshots; toolUseId: string } | undefined>>();
|
|
55
|
+
/** 根会话 id → 正在给这一轮的快照补拍结束时的 tree:/undo、下一张快照先等它 */
|
|
56
|
+
const closing = new Map<string, Promise<void>>();
|
|
57
|
+
|
|
58
|
+
const now = () => performance.timeOrigin + performance.now();
|
|
59
|
+
/** 会话的工作区快照(子 Agent 的记在根会话里) */
|
|
60
|
+
const snapshotsOf = (home: string, sessionId: string, cwd: string) => snapshotsAt(path.join(home, "checkpoints", safe(sessionId)), cwd);
|
|
61
|
+
|
|
62
|
+
/** 根会话这一轮结束:拍过快照的话补上结束时的 tree。返回补拍完成 */
|
|
63
|
+
function closeTurn(root: string): Promise<void> | undefined {
|
|
64
|
+
const p = turnSnaps.get(root);
|
|
65
|
+
if (!p) return undefined;
|
|
66
|
+
turnSnaps.delete(root);
|
|
67
|
+
const done = p
|
|
68
|
+
.then((r) => r?.snapshots.close(r.toolUseId))
|
|
69
|
+
.catch(() => {
|
|
70
|
+
// 补拍不了:撤销时退回到和工作区现在的样子比
|
|
71
|
+
});
|
|
72
|
+
closing.set(root, done);
|
|
73
|
+
return done;
|
|
74
|
+
}
|
|
75
|
+
|
|
48
76
|
/** 文件现在的内容;不存在时 null */
|
|
49
77
|
async function contentOf(file: string): Promise<string | null> {
|
|
50
78
|
try {
|
|
@@ -59,34 +87,102 @@ export default definePlugin({
|
|
|
59
87
|
process(ctx) {
|
|
60
88
|
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
61
89
|
const cwd = realPath(ctx.cwd);
|
|
90
|
+
ctx.provide("undo@1", {
|
|
91
|
+
async track(session, label, fn) {
|
|
92
|
+
if (ctx.config.shell === false) return fn();
|
|
93
|
+
const snapshots = snapshotsOf(ctx.home, session, ctx.cwd);
|
|
94
|
+
await closing.get(session);
|
|
95
|
+
let id: string | undefined;
|
|
96
|
+
try {
|
|
97
|
+
const tree = await snapshots.take();
|
|
98
|
+
if (tree) {
|
|
99
|
+
id = `user-${randomUUID()}`;
|
|
100
|
+
await snapshots.save({ toolUseId: id, tree, label, at: now() });
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
// 拍不了快照:照样执行,只是撤销不了
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
return await fn();
|
|
107
|
+
} finally {
|
|
108
|
+
if (id) await snapshots.close(id).catch(() => {});
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
} satisfies Undo);
|
|
112
|
+
/** 文件的显示名:恢复前算好(恢复时会删掉新建的文件)。文件可能已经不在了:解析它所在的目录 */
|
|
113
|
+
const shownName = (p: string) => display(cwd, path.join(realPath(path.dirname(p)), path.basename(p)));
|
|
114
|
+
/** 撤销用户自己执行的命令(undo@1.track 记下的一步) */
|
|
115
|
+
const undoRun = async (sessionId: string, snapshots: Snapshots, run: Snapshot, { changes, conflicts }: { changes: Change[]; conflicts: Set<string> }, ui: UI, signal: AbortSignal) => {
|
|
116
|
+
const shown = new Map(changes.map((c) => [c.path, shownName(c.path)]));
|
|
117
|
+
const rows = changes.map((c) => ` ${c.added ? "delete " : "restore"} ${shown.get(c.path)}`);
|
|
118
|
+
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join("\n") : rows.join("\n");
|
|
119
|
+
const changed = changes.filter((c) => conflicts.has(c.path)).map((c) => shown.get(c.path)!);
|
|
120
|
+
const files = `${changes.length} file${changes.length > 1 ? "s" : ""}`;
|
|
121
|
+
const answer = await ui.select(
|
|
122
|
+
{
|
|
123
|
+
title:
|
|
124
|
+
changed.length > 0
|
|
125
|
+
? `${changed.join(", ")} changed since your command ${run.label} ran. Undo it anyway and discard those changes too?`
|
|
126
|
+
: `Undo your command ${run.label} (${files})?`,
|
|
127
|
+
detail: list,
|
|
128
|
+
options: [
|
|
129
|
+
{ label: "No", value: "no" },
|
|
130
|
+
{ label: changed.length > 0 ? "Yes, undo anyway" : "Yes, undo", value: "yes" },
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
signal,
|
|
134
|
+
);
|
|
135
|
+
if (answer !== "yes") return;
|
|
136
|
+
await snapshots.restore(run.tree, changes);
|
|
137
|
+
await checkpoints.markUndone(sessionId, [run.toolUseId]);
|
|
138
|
+
const what = changes.slice(0, MAX_LISTED).map((c) => (c.added ? `deleted ${shown.get(c.path)} (the command had created it)` : `restored ${shown.get(c.path)}`));
|
|
139
|
+
if (changes.length > MAX_LISTED) what.push(`and ${changes.length - MAX_LISTED} more files`);
|
|
140
|
+
ctx.service<Prompts>("prompts@1")?.note(sessionId, `The user undid what their own shell command ${run.label} changed: ${what.join("; ")}. Read these files again before changing them.`);
|
|
141
|
+
return [ok(`Undid your command ${run.label} (${files}):`), "\n", dim(list)];
|
|
142
|
+
};
|
|
62
143
|
ctx.service<Commands>("commands@1")?.register({
|
|
63
144
|
name: "undo",
|
|
64
145
|
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)",
|
|
65
146
|
async run(_args, { sessions, ui, signal }) {
|
|
66
147
|
const s = sessions.current;
|
|
67
148
|
if (!s) return "No session is open.";
|
|
68
|
-
|
|
149
|
+
await closing.get(s.id);
|
|
150
|
+
const snapshots = snapshotsOf(ctx.home, s.id, ctx.cwd);
|
|
69
151
|
const undone = await checkpoints.undone(s.id);
|
|
70
152
|
const snaps = await snapshots.list();
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
153
|
+
/** 用户自己执行的命令,最新的在前:不属于哪一轮,按时间和 Agent 的轮次排先后 */
|
|
154
|
+
const runs = snaps.filter((x) => x.label !== undefined);
|
|
155
|
+
const entries = [...(await checkpoints.list(s.id)), ...snaps.filter((x) => x.label === undefined)].sort((x, y) => y.at - x.at);
|
|
156
|
+
/** 这一轮文件工具的快照(最新的在前)、命令之前的工作区快照、撤销它要动的文件、其中之后又被改过的 */
|
|
157
|
+
let cps: Checkpoint[] = [];
|
|
74
158
|
let snap: Snapshot | undefined;
|
|
75
|
-
let tree: Change[];
|
|
159
|
+
let tree: Change[] = [];
|
|
160
|
+
let conflicts = new Set<string>();
|
|
76
161
|
for (;;) {
|
|
77
162
|
const hits = lastTurnChanges<Checkpoint | Snapshot>(s.state.messages, entries, undone);
|
|
163
|
+
// 用户的命令比这一轮的改动新(例如这一轮之后跑的 !npm i):先撤销它。它引出的那一轮比它晚,先撤那一轮
|
|
164
|
+
const run = runs.find((r) => !undone.has(r.toolUseId));
|
|
165
|
+
if (run && (hits.length === 0 || run.at > Math.min(...hits.map((h) => h.at)))) {
|
|
166
|
+
const found = await snapshots.changes(run);
|
|
167
|
+
if (found.changes.length > 0) return undoRun(s.id, snapshots, run, found, ui, signal);
|
|
168
|
+
// 命令什么都没改(或者已经改回去了):看更早的
|
|
169
|
+
undone.add(run.toolUseId);
|
|
170
|
+
await checkpoints.markUndone(s.id, [run.toolUseId]);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
78
173
|
cps = hits.filter((h): h is Checkpoint => "path" in h);
|
|
79
174
|
snap = hits.find((h): h is Snapshot => "tree" in h);
|
|
80
|
-
tree = snap ? await snapshots.
|
|
175
|
+
({ changes: tree, conflicts } = snap ? await snapshots.changes(snap) : { changes: [], conflicts: new Set<string>() });
|
|
81
176
|
if (!snap || cps.length > 0 || tree.length > 0) break;
|
|
82
177
|
// 命令什么都没改(或者已经改回去了)的一轮:没有可撤销的,看更早的一轮
|
|
83
178
|
undone.add(snap.toolUseId);
|
|
84
179
|
await checkpoints.markUndone(s.id, [snap.toolUseId]);
|
|
85
180
|
}
|
|
86
181
|
// 这个会话里拍到过快照,说明命令的改动撤销得了,不用提醒
|
|
87
|
-
|
|
182
|
+
const ranShell = (names: Set<string>) => [...names].some((n) => s.tool(n)?.meta?.shell !== undefined);
|
|
183
|
+
if (cps.length === 0 && !snap) return dim(snaps.length === 0 && ranShell(turnToolNames(s.state.messages)) ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
|
|
88
184
|
// 没拍到快照(不在 git 仓库里、配置关掉了)的那一轮跑过命令时提醒一句
|
|
89
|
-
const shellToo = !snap && turnToolNames(s.state.messages, cps[0]!.toolUseId)
|
|
185
|
+
const shellToo = !snap && ranShell(turnToolNames(s.state.messages, cps[0]!.toolUseId));
|
|
90
186
|
// 工作区快照覆盖到的文件按快照恢复(那是命令动它之前的内容):拍快照之后文件工具对它的修改不再单独恢复
|
|
91
187
|
const inTree = new Set(tree.map((c) => same(c.path)));
|
|
92
188
|
const fileCps = cps.filter((cp) => !(snap && cp.at > snap.at && inTree.has(same(cp.path))));
|
|
@@ -104,8 +200,9 @@ export default definePlugin({
|
|
|
104
200
|
const latest = new Map<string, Checkpoint>();
|
|
105
201
|
for (const cp of fileCps) if (!latest.has(cp.path)) latest.set(cp.path, cp);
|
|
106
202
|
const changed: string[] = [];
|
|
107
|
-
//
|
|
203
|
+
// 工作区快照覆盖到的文件按快照判断:这一轮结束之后又变了的才算"别人改过"
|
|
108
204
|
for (const cp of latest.values()) if (!inTree.has(same(cp.path)) && !(await checkpoints.unchangedSince(cp))) changed.push(show(cp.path));
|
|
205
|
+
for (const c of tree) if (conflicts.has(c.path)) changed.push(show(c.path));
|
|
109
206
|
const rows = [...plan.values()].map((f) => ` ${f.gone ? "delete " : "restore"} ${show(f.path)}`);
|
|
110
207
|
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join("\n") : rows.join("\n");
|
|
111
208
|
if (plan.size > 1 || changed.length > 0 || tree.length > 0) {
|
|
@@ -115,7 +212,7 @@ export default definePlugin({
|
|
|
115
212
|
changed.length > 0
|
|
116
213
|
? `${changed.join(", ")} changed since the agent wrote ${changed.length > 1 ? "them" : "it"}. Undo anyway and discard those changes too?`
|
|
117
214
|
: tree.length > 0
|
|
118
|
-
? `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
|
|
215
|
+
? `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${snap?.after ? "." : ", and edits you made to them since then are lost too."}`
|
|
119
216
|
: `Undo the agent's last turn of changes (${plan.size} files)?`,
|
|
120
217
|
detail: list,
|
|
121
218
|
options: [
|
|
@@ -135,7 +232,7 @@ export default definePlugin({
|
|
|
135
232
|
if (plan.size > MAX_LISTED) what.push(`and ${plan.size - MAX_LISTED} more files`);
|
|
136
233
|
ctx.service<Prompts>("prompts@1")?.note(s.id, `The user undid your changes from your last turn: ${what.join("; ")}. Read these files again before changing them.`);
|
|
137
234
|
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:`;
|
|
138
|
-
return
|
|
235
|
+
return [ok(title), "\n", dim(list), ...(shellToo ? ["\n", dim(SHELL_NOTE)] : [])];
|
|
139
236
|
},
|
|
140
237
|
});
|
|
141
238
|
},
|
|
@@ -143,11 +240,48 @@ export default definePlugin({
|
|
|
143
240
|
session(ctx) {
|
|
144
241
|
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
145
242
|
const sessionId = ctx.session.id;
|
|
243
|
+
const decided = new Map<string, string>();
|
|
244
|
+
decidedBy.set(sessionId, decided);
|
|
245
|
+
/**
|
|
246
|
+
* 子会话(子 Agent)的改动记在根会话里,算作根会话里打开它的那次调用的:父会话 /undo 时和那一轮的改动一起撤销。
|
|
247
|
+
* 父会话的 tool.decided 在工具执行之前就记下了,所以子会话开好时查得到
|
|
248
|
+
*/
|
|
249
|
+
const link = (): { root: string; via?: string } => {
|
|
250
|
+
const p = ctx.session.parent;
|
|
251
|
+
if (!p) return { root: sessionId };
|
|
252
|
+
const up = links.get(p.sessionId)?.();
|
|
253
|
+
if (up?.via) return up;
|
|
254
|
+
const via = decidedBy.get(p.sessionId)?.get(p.eventId);
|
|
255
|
+
return { root: up?.root ?? p.sessionId, ...(via ? { via } : {}) };
|
|
256
|
+
};
|
|
257
|
+
links.set(sessionId, link);
|
|
258
|
+
void ctx.effect(() => () => {
|
|
259
|
+
if (decidedBy.get(sessionId) === decided) decidedBy.delete(sessionId);
|
|
260
|
+
if (links.get(sessionId) === link) links.delete(sessionId);
|
|
261
|
+
if (!ctx.session.parent) closeTurn(sessionId);
|
|
262
|
+
});
|
|
146
263
|
/** toolUseId → 执行前的内容,执行成功后存成快照 */
|
|
147
264
|
const before = new Map<string, { tool: string; path: string; before: string | null }>();
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
265
|
+
/**
|
|
266
|
+
* 这一轮第一次跑命令之前给工作区拍快照:整棵会话树(根会话和它这一轮开的子 Agent)共用一张,记在根会话里。
|
|
267
|
+
* 并行的子 Agent 同时要跑命令时等同一张拍完
|
|
268
|
+
*/
|
|
269
|
+
const beforeCommand = (toolUseId: string) => {
|
|
270
|
+
const { root, via } = link();
|
|
271
|
+
let p = turnSnaps.get(root);
|
|
272
|
+
if (!p) {
|
|
273
|
+
const snapshots = snapshotsOf(ctx.home, root, ctx.cwd);
|
|
274
|
+
p = (async () => {
|
|
275
|
+
await closing.get(root);
|
|
276
|
+
const tree = await snapshots.take();
|
|
277
|
+
if (!tree) return undefined;
|
|
278
|
+
await snapshots.save({ toolUseId, tree, at: now(), ...(via ? { via } : {}) });
|
|
279
|
+
return { snapshots, toolUseId };
|
|
280
|
+
})().catch(() => undefined);
|
|
281
|
+
turnSnaps.set(root, p);
|
|
282
|
+
}
|
|
283
|
+
return p;
|
|
284
|
+
};
|
|
151
285
|
|
|
152
286
|
ctx.kernel.hook({
|
|
153
287
|
kind: "transform",
|
|
@@ -158,15 +292,14 @@ export default definePlugin({
|
|
|
158
292
|
// 第一次给大仓库拍快照要把每个文件算一遍哈希
|
|
159
293
|
timeoutMs: SNAPSHOT_TIMEOUT_MS,
|
|
160
294
|
fn: async (call) => {
|
|
295
|
+
const meta = ctx.session.tool(call.name)?.meta;
|
|
161
296
|
// 每轮第一次跑命令之前给工作区拍一张快照:命令改了什么,/undo 时和它一比就知道
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
const tree = await snapshots.take();
|
|
165
|
-
if (tree) await snapshots.save({ toolUseId: call.toolUseId, tree, at: performance.timeOrigin + performance.now() });
|
|
297
|
+
if (meta?.shell !== undefined && ctx.config.shell !== false) {
|
|
298
|
+
await beforeCommand(call.toolUseId);
|
|
166
299
|
return;
|
|
167
300
|
}
|
|
168
|
-
if (
|
|
169
|
-
const p = (call.input as
|
|
301
|
+
if (meta?.writes === undefined) return;
|
|
302
|
+
const p = (call.input as Record<string, unknown> | null)?.[meta.writes];
|
|
170
303
|
if (typeof p !== "string") return;
|
|
171
304
|
const full = path.resolve(ctx.cwd, p);
|
|
172
305
|
before.set(call.toolUseId, { tool: call.name, path: full, before: await contentOf(full) });
|
|
@@ -184,7 +317,8 @@ export default definePlugin({
|
|
|
184
317
|
const after = await contentOf(snap.path);
|
|
185
318
|
// 内容没变(例如同样的内容重复写入)不算修改
|
|
186
319
|
if (after === snap.before) return;
|
|
187
|
-
|
|
320
|
+
const { root, via } = link();
|
|
321
|
+
await checkpoints.save(root, { toolUseId: out.call.toolUseId, ...snap, afterHash: sha256(after ?? ""), ...(via ? { via } : {}) });
|
|
188
322
|
},
|
|
189
323
|
});
|
|
190
324
|
// 被拒绝、被中断的调用不经过 tool.after:一轮结束时还留着的都不会再用到,不然整个文件的内容一直占着内存
|
|
@@ -192,9 +326,14 @@ export default definePlugin({
|
|
|
192
326
|
kind: "observe",
|
|
193
327
|
point: "event",
|
|
194
328
|
fn: (e) => {
|
|
329
|
+
if (e.type === "tool.decided") decided.set(e.id, (e.data as { toolUseId: string }).toolUseId);
|
|
195
330
|
if (e.type !== "turn.ended") return;
|
|
196
331
|
before.clear();
|
|
197
|
-
|
|
332
|
+
// 只有这一轮里开的子会话查它(子会话在打开它的那次调用里结束)
|
|
333
|
+
decided.clear();
|
|
334
|
+
// 子会话结束不算:它的命令属于根会话这一轮。
|
|
335
|
+
// 返回补拍:内核等 observe 完成才交还这一轮(5.1),之后用户再动的文件不会被拍进这一轮
|
|
336
|
+
if (!ctx.session.parent) return closeTurn(sessionId);
|
|
198
337
|
},
|
|
199
338
|
});
|
|
200
339
|
},
|
package/plugins/undo/plugin.json
CHANGED
package/plugins/undo/snapshot.ts
CHANGED
|
@@ -4,16 +4,24 @@
|
|
|
4
4
|
// 项目自己的 .git(暂存区、提交、stash)一点不碰。只在 git 仓库里做:要靠 .gitignore 跳过 node_modules 这类目录,
|
|
5
5
|
// 对象库借用项目的(alternates),已经提交过的内容不再存一份。被忽略的文件不在快照里,撤销不了。
|
|
6
6
|
// 快照是 tree 对象,不建提交;哪一轮对应哪个 tree 记在 snapshots.json。
|
|
7
|
+
// 一轮结束时再拍一张(after):撤销时只动这一轮之内变了的文件,之后别人(用户的 /sh、手改)动过的不会被一起冲掉,而是先问。
|
|
8
|
+
// 同一个影子仓库的操作在进程内排队:并行的子 Agent、/undo 都会用它,git 的 index.lock 不能并发。
|
|
7
9
|
|
|
8
10
|
import { execFile } from "node:child_process";
|
|
9
11
|
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
10
12
|
import path from "node:path";
|
|
11
13
|
|
|
12
14
|
export interface Snapshot {
|
|
13
|
-
/** 这一轮第一次 bash 调用的 toolUseId:/undo
|
|
15
|
+
/** 这一轮第一次 bash 调用的 toolUseId:/undo 按它找到是哪一轮。用户自己执行的命令(label)是生成的 id */
|
|
14
16
|
toolUseId: string;
|
|
15
|
-
/** 影子仓库里的 tree */
|
|
17
|
+
/** 影子仓库里的 tree:命令之前 */
|
|
16
18
|
tree: string;
|
|
19
|
+
/** 一轮(或用户的命令)结束时的 tree;没有时(旧记录、进程中途退出)撤销时和工作区现在的样子比 */
|
|
20
|
+
after?: string;
|
|
21
|
+
/** 子 Agent 拍的:根会话里打开它的那次调用(同 Checkpoint.via) */
|
|
22
|
+
via?: string;
|
|
23
|
+
/** 有它的是用户自己执行的命令(例如 /sh 的 "$ npm i"),单独算一步撤销,不属于哪一轮 */
|
|
24
|
+
label?: string;
|
|
17
25
|
/** 拍下的时间,和 Checkpoint.at 同一个钟 */
|
|
18
26
|
at: number;
|
|
19
27
|
}
|
|
@@ -29,6 +37,29 @@ const FILE = "snapshots.json";
|
|
|
29
37
|
/** 一次 git 命令最多带这么多路径:Windows 的命令行长度有限 */
|
|
30
38
|
const CHUNK = 100;
|
|
31
39
|
|
|
40
|
+
/** 影子仓库目录 → 排在最后的操作 */
|
|
41
|
+
const queues = new Map<string, Promise<unknown>>();
|
|
42
|
+
/** 同一个目录的操作一个接一个执行(前一个失败不影响后一个) */
|
|
43
|
+
function serial<T>(key: string, fn: () => Promise<T>): Promise<T> {
|
|
44
|
+
const next = (queues.get(key) ?? Promise.resolve()).then(fn, fn);
|
|
45
|
+
const tail = next.catch(() => {});
|
|
46
|
+
queues.set(key, tail);
|
|
47
|
+
void tail.then(() => {
|
|
48
|
+
if (queues.get(key) === tail) queues.delete(key);
|
|
49
|
+
});
|
|
50
|
+
return next;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 同一个目录共用一个实例:影子仓库只准备一次 */
|
|
54
|
+
const instances = new Map<string, Snapshots>();
|
|
55
|
+
/** dir:会话的快照目录;cwd:会话的工作目录 */
|
|
56
|
+
export function snapshotsAt(dir: string, cwd: string): Snapshots {
|
|
57
|
+
const key = `${dir}|${cwd}`;
|
|
58
|
+
let s = instances.get(key);
|
|
59
|
+
if (!s) instances.set(key, (s = new Snapshots(dir, cwd)));
|
|
60
|
+
return s;
|
|
61
|
+
}
|
|
62
|
+
|
|
32
63
|
export class Snapshots {
|
|
33
64
|
readonly #dir: string;
|
|
34
65
|
readonly #cwd: string;
|
|
@@ -72,8 +103,16 @@ export class Snapshots {
|
|
|
72
103
|
})());
|
|
73
104
|
}
|
|
74
105
|
|
|
106
|
+
#serial<T>(fn: () => Promise<T>): Promise<T> {
|
|
107
|
+
return serial(this.#dir, fn);
|
|
108
|
+
}
|
|
109
|
+
|
|
75
110
|
/** 给工作区拍一张快照,返回 tree;不在 git 仓库里时 undefined */
|
|
76
|
-
|
|
111
|
+
take(): Promise<string | undefined> {
|
|
112
|
+
return this.#serial(() => this.#take());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async #take(): Promise<string | undefined> {
|
|
77
116
|
const r = await this.#prepare();
|
|
78
117
|
if (!r) return undefined;
|
|
79
118
|
await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {
|
|
@@ -82,12 +121,26 @@ export class Snapshots {
|
|
|
82
121
|
return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
|
|
83
122
|
}
|
|
84
123
|
|
|
85
|
-
/**
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
124
|
+
/**
|
|
125
|
+
* 撤销这张快照要动的文件,和其中之后又被改过的(conflicts,绝对路径)。
|
|
126
|
+
* 有 after 时是 tree 到 after 之间变了的文件,工作区现在和 after 不一样的算之后改过;
|
|
127
|
+
* 没有时(旧记录、进程中途退出)是 tree 到现在变了的文件,分不出哪些是之后改的
|
|
128
|
+
*/
|
|
129
|
+
changes(snap: Pick<Snapshot, "tree" | "after">): Promise<{ changes: Change[]; conflicts: Set<string> }> {
|
|
130
|
+
return this.#serial(async () => {
|
|
131
|
+
const r = await this.#prepare();
|
|
132
|
+
const now = await this.#take();
|
|
133
|
+
if (!r || !now) return { changes: [], conflicts: new Set<string>() };
|
|
134
|
+
const end = snap.after ?? now;
|
|
135
|
+
const changes = await this.#diff(r, snap.tree, end);
|
|
136
|
+
const later = new Set((await this.#diff(r, end, now)).map((c) => c.path));
|
|
137
|
+
return { changes, conflicts: new Set(changes.map((c) => c.path).filter((p) => later.has(p))) };
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async #diff(r: { env: NodeJS.ProcessEnv; root: string }, from: string, to: string): Promise<Change[]> {
|
|
142
|
+
if (from === to) return [];
|
|
143
|
+
const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", from, to], { cwd: r.root, env: r.env });
|
|
91
144
|
const parts = out.split("\0");
|
|
92
145
|
const changes: Change[] = [];
|
|
93
146
|
for (let i = 0; i + 1 < parts.length; i += 2) changes.push({ path: path.join(r.root, parts[i + 1]!), added: parts[i] === "A" });
|
|
@@ -95,7 +148,11 @@ export class Snapshots {
|
|
|
95
148
|
}
|
|
96
149
|
|
|
97
150
|
/** 把这些文件恢复成快照 tree 里的样子:之后新出现的删掉,其余的取回 */
|
|
98
|
-
|
|
151
|
+
restore(tree: string, changes: readonly Change[]): Promise<void> {
|
|
152
|
+
return this.#serial(() => this.#restore(tree, changes));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async #restore(tree: string, changes: readonly Change[]): Promise<void> {
|
|
99
156
|
const r = await this.#prepare();
|
|
100
157
|
if (!r) return;
|
|
101
158
|
for (const c of changes) if (c.added) await rm(c.path, { force: true });
|
|
@@ -106,7 +163,11 @@ export class Snapshots {
|
|
|
106
163
|
}
|
|
107
164
|
|
|
108
165
|
/** 本会话记下的快照,最新的在前 */
|
|
109
|
-
|
|
166
|
+
list(): Promise<Snapshot[]> {
|
|
167
|
+
return this.#serial(() => this.#list());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async #list(): Promise<Snapshot[]> {
|
|
110
171
|
try {
|
|
111
172
|
const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8")) as unknown;
|
|
112
173
|
return Array.isArray(all) ? (all as Snapshot[]).sort((a, b) => b.at - a.at) : [];
|
|
@@ -115,10 +176,22 @@ export class Snapshots {
|
|
|
115
176
|
}
|
|
116
177
|
}
|
|
117
178
|
|
|
118
|
-
|
|
119
|
-
|
|
179
|
+
save(snapshot: Snapshot): Promise<void> {
|
|
180
|
+
return this.#serial(async () => this.#write([snapshot, ...(await this.#list())]));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 给这张快照补上结束时的 tree(现在工作区的样子) */
|
|
184
|
+
close(toolUseId: string): Promise<void> {
|
|
185
|
+
return this.#serial(async () => {
|
|
186
|
+
const after = await this.#take();
|
|
187
|
+
if (!after) return;
|
|
188
|
+
await this.#write((await this.#list()).map((s) => (s.toolUseId === toolUseId ? { ...s, after } : s)));
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async #write(all: Snapshot[]): Promise<void> {
|
|
120
193
|
await mkdir(this.#dir, { recursive: true });
|
|
121
|
-
await writeFile(path.join(this.#dir, FILE), JSON.stringify(
|
|
194
|
+
await writeFile(path.join(this.#dir, FILE), JSON.stringify(all));
|
|
122
195
|
}
|
|
123
196
|
}
|
|
124
197
|
|
package/plugins/undo/style.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 显示用的小工具:截断,和带语义样式的文字(jade-api 的 Text)。插件只说这段是次要的、成功、警告……,
|
|
2
|
+
// 怎么画由前端决定(终端界面画成颜色,-p 只取文字)。插件之间不共享代码(只能共享类型),所以每个插件有一份。
|
|
3
|
+
|
|
4
|
+
import type { Span, Text, TextStyle } from "@uncarved/jade-api";
|
|
2
5
|
|
|
3
6
|
export const clip = (s: string, n: number) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export const
|
|
14
|
-
export const red = paint("31", "39");
|
|
15
|
-
export const green = paint("32", "39");
|
|
16
|
-
export const yellow = paint("33", "39");
|
|
17
|
-
export const bold = paint("1", "22");
|
|
8
|
+
const styled = (style: TextStyle) => (text: string): Span => ({ text, style });
|
|
9
|
+
export const dim = styled("dim");
|
|
10
|
+
export const bold = styled("bold");
|
|
11
|
+
export const ok = styled("ok");
|
|
12
|
+
export const warn = styled("warn");
|
|
13
|
+
export const error = styled("error");
|
|
14
|
+
|
|
15
|
+
/** 几行接成一段,行之间换行 */
|
|
16
|
+
export const lines = (rows: readonly Text[]): Text => rows.flatMap((r, i) => (i ? ["\n", r] : [r]));
|
package/plugins/web/index.ts
CHANGED
|
@@ -5,16 +5,15 @@
|
|
|
5
5
|
// 不用重开会话;会话的工具清单在创建时就定了。
|
|
6
6
|
|
|
7
7
|
import path from "node:path";
|
|
8
|
-
import { definePlugin, type ScopeContext, type UI } from "@uncarved/jade-api";
|
|
9
|
-
import type { Commands, Prompts, Renderers
|
|
8
|
+
import { definePlugin, type ScopeContext, type Span, type UI } from "@uncarved/jade-api";
|
|
9
|
+
import type { Commands, Prompts, Renderers } from "@uncarved/jade-api/services";
|
|
10
10
|
import { envWithFile, updateEnvFile, type Env } from "./env.ts";
|
|
11
11
|
import { webRenderers } from "./renderers.ts";
|
|
12
12
|
import { parseOrder, parseResults, SEARCH_PROVIDERS, webTools } from "./web.ts";
|
|
13
13
|
|
|
14
|
-
//
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const green = (s: string) => (colors ? `\x1b[32m${s}\x1b[39m` : s);
|
|
14
|
+
// 带语义样式的文字(jade-api 的 Text):怎么画由前端决定
|
|
15
|
+
const dim = (text: string): Span => ({ text, style: "dim" });
|
|
16
|
+
const ok = (text: string): Span => ({ text, style: "ok" });
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
19
|
* API key 所在的环境:每次开会话时重新读,/search 改了之后原地更新。模块每个进程只加载一次,进程级的命令和会话级的工具共用
|
|
@@ -79,7 +78,7 @@ export default definePlugin({
|
|
|
79
78
|
await updateEnvFile(file, { [p.env]: key }, env);
|
|
80
79
|
offNote?.();
|
|
81
80
|
offNote = undefined;
|
|
82
|
-
return
|
|
81
|
+
return ok(`Web search is on with ${p.name}: web_search uses it, and web_fetch falls back to it when a page cannot be fetched directly.`);
|
|
83
82
|
},
|
|
84
83
|
});
|
|
85
84
|
},
|
|
@@ -98,17 +97,7 @@ export default definePlugin({
|
|
|
98
97
|
const web = webTools({ ...opts, env: () => keys ?? env });
|
|
99
98
|
for (const tool of web.tools) ctx.kernel.tool(tool);
|
|
100
99
|
|
|
101
|
-
//
|
|
102
|
-
// web_fetch 带 url 参数,jade/task 的子 Agent 每次都要问用户(URL 能把读到的内容带出去)。
|
|
103
|
-
// jade/tools 被隔离时它的服务已失效:嵌套 Agent 没有 web 工具,web 工具本身照常
|
|
104
|
-
try {
|
|
105
|
-
const shared = ctx.service<Tools>("tools@1");
|
|
106
|
-
if (shared) {
|
|
107
|
-
const sub = webTools({ ...opts, env: () => keys ?? env });
|
|
108
|
-
for (const tool of sub.tools) shared.register(tool, tool.name === "web_search" ? { when: () => sub.search().length > 0 } : {});
|
|
109
|
-
}
|
|
110
|
-
} catch {}
|
|
111
|
-
|
|
100
|
+
// 子 Agent 是核心开的子会话,本插件在里面另有一份,web_fetch 经过和主会话同一套审批
|
|
112
101
|
ctx.kernel.hook({
|
|
113
102
|
kind: "transform",
|
|
114
103
|
point: "context.build",
|
package/plugins/web/plugin.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"version": "0.3.0",
|
|
4
4
|
"api": "^1",
|
|
5
5
|
"entry": "./index.ts",
|
|
6
|
-
"optional": ["ui@1", "commands@1", "prompts@1", "renderers@1"
|
|
6
|
+
"optional": ["ui@1", "commands@1", "prompts@1", "renderers@1"],
|
|
7
7
|
"config": {
|
|
8
8
|
"search": {
|
|
9
9
|
"description": "search services to use, in order: a list or comma-separated names (tinyfish, anysearch, tavily, exa, firecrawl, jina); default: every service with an API key. Keys are read from the environment and $JADE_HOME/.env (/search sets them). $JADE_SEARCH still works"
|