@uncarved/jade 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +191 -2
- package/bin/jade.js +20 -0
- package/dist/commands.d.ts +30 -0
- package/dist/commands.js +106 -0
- package/dist/commands.js.map +1 -0
- package/dist/config.d.ts +34 -0
- package/dist/config.js +130 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +17 -0
- package/dist/context.js +53 -0
- package/dist/context.js.map +1 -0
- package/dist/distro.d.ts +97 -0
- package/dist/distro.js +311 -0
- package/dist/distro.js.map +1 -0
- package/dist/history.d.ts +10 -0
- package/dist/history.js +47 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +1214 -0
- package/dist/main.js.map +1 -0
- package/dist/mcp.d.ts +114 -0
- package/dist/mcp.js +640 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory.d.ts +19 -0
- package/dist/memory.js +129 -0
- package/dist/memory.js.map +1 -0
- package/dist/mentions.d.ts +35 -0
- package/dist/mentions.js +130 -0
- package/dist/mentions.js.map +1 -0
- package/dist/plugin-cli.d.ts +16 -0
- package/dist/plugin-cli.js +251 -0
- package/dist/plugin-cli.js.map +1 -0
- package/dist/plugins/budget.d.ts +9 -0
- package/dist/plugins/budget.js +30 -0
- package/dist/plugins/budget.js.map +1 -0
- package/dist/plugins/builtins.d.ts +10 -0
- package/dist/plugins/builtins.js +22 -0
- package/dist/plugins/builtins.js.map +1 -0
- package/dist/plugins/checks.d.ts +70 -0
- package/dist/plugins/checks.js +335 -0
- package/dist/plugins/checks.js.map +1 -0
- package/dist/plugins/compact.d.ts +26 -0
- package/dist/plugins/compact.js +216 -0
- package/dist/plugins/compact.js.map +1 -0
- package/dist/plugins/core.d.ts +42 -0
- package/dist/plugins/core.js +190 -0
- package/dist/plugins/core.js.map +1 -0
- package/dist/plugins/install.d.ts +68 -0
- package/dist/plugins/install.js +377 -0
- package/dist/plugins/install.js.map +1 -0
- package/dist/plugins/loader.d.ts +182 -0
- package/dist/plugins/loader.js +253 -0
- package/dist/plugins/loader.js.map +1 -0
- package/dist/plugins/policy.d.ts +80 -0
- package/dist/plugins/policy.js +541 -0
- package/dist/plugins/policy.js.map +1 -0
- package/dist/plugins/prompts.d.ts +30 -0
- package/dist/plugins/prompts.js +182 -0
- package/dist/plugins/prompts.js.map +1 -0
- package/dist/plugins/repeats.d.ts +9 -0
- package/dist/plugins/repeats.js +90 -0
- package/dist/plugins/repeats.js.map +1 -0
- package/dist/plugins/schema.d.ts +2 -0
- package/dist/plugins/schema.js +64 -0
- package/dist/plugins/schema.js.map +1 -0
- package/dist/provider.d.ts +111 -0
- package/dist/provider.js +326 -0
- package/dist/provider.js.map +1 -0
- package/dist/sessions.d.ts +22 -0
- package/dist/sessions.js +53 -0
- package/dist/sessions.js.map +1 -0
- package/dist/skills.d.ts +37 -0
- package/dist/skills.js +179 -0
- package/dist/skills.js.map +1 -0
- package/dist/sys.d.ts +48 -0
- package/dist/sys.js +116 -0
- package/dist/sys.js.map +1 -0
- package/dist/tools/checkpoint.d.ts +43 -0
- package/dist/tools/checkpoint.js +144 -0
- package/dist/tools/checkpoint.js.map +1 -0
- package/dist/tools/fs.d.ts +35 -0
- package/dist/tools/fs.js +417 -0
- package/dist/tools/fs.js.map +1 -0
- package/dist/tools/grep.d.ts +6 -0
- package/dist/tools/grep.js +243 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/screenshot.d.ts +15 -0
- package/dist/tools/screenshot.js +162 -0
- package/dist/tools/screenshot.js.map +1 -0
- package/dist/tools/shell.d.ts +55 -0
- package/dist/tools/shell.js +314 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/task.d.ts +31 -0
- package/dist/tools/task.js +86 -0
- package/dist/tools/task.js.map +1 -0
- package/dist/tools/todo.d.ts +10 -0
- package/dist/tools/todo.js +55 -0
- package/dist/tools/todo.js.map +1 -0
- package/dist/tools/web.d.ts +23 -0
- package/dist/tools/web.js +426 -0
- package/dist/tools/web.js.map +1 -0
- package/dist/ui/configure.d.ts +20 -0
- package/dist/ui/configure.js +369 -0
- package/dist/ui/configure.js.map +1 -0
- package/dist/ui/diff.d.ts +20 -0
- package/dist/ui/diff.js +142 -0
- package/dist/ui/diff.js.map +1 -0
- package/dist/ui/editor.d.ts +33 -0
- package/dist/ui/editor.js +128 -0
- package/dist/ui/editor.js.map +1 -0
- package/dist/ui/render.d.ts +13 -0
- package/dist/ui/render.js +84 -0
- package/dist/ui/render.js.map +1 -0
- package/dist/ui/screen.d.ts +95 -0
- package/dist/ui/screen.js +105 -0
- package/dist/ui/screen.js.map +1 -0
- package/dist/ui/transcript.d.ts +33 -0
- package/dist/ui/transcript.js +372 -0
- package/dist/ui/transcript.js.map +1 -0
- package/dist/ui/tui.d.ts +45 -0
- package/dist/ui/tui.js +1107 -0
- package/dist/ui/tui.js.map +1 -0
- package/package.json +59 -4
- package/src/commands.ts +120 -0
- package/src/config.ts +146 -0
- package/src/context.ts +64 -0
- package/src/distro.ts +395 -0
- package/src/history.ts +55 -0
- package/src/index.ts +27 -0
- package/src/main.ts +1267 -0
- package/src/mcp.ts +693 -0
- package/src/memory.ts +135 -0
- package/src/mentions.ts +135 -0
- package/src/plugin-cli.ts +239 -0
- package/src/plugins/budget.ts +37 -0
- package/src/plugins/builtins.ts +31 -0
- package/src/plugins/checks.ts +390 -0
- package/src/plugins/compact.ts +257 -0
- package/src/plugins/core.ts +217 -0
- package/src/plugins/install.ts +429 -0
- package/src/plugins/loader.ts +386 -0
- package/src/plugins/policy.ts +578 -0
- package/src/plugins/prompts.ts +215 -0
- package/src/plugins/repeats.ts +96 -0
- package/src/plugins/schema.ts +67 -0
- package/src/provider.ts +363 -0
- package/src/sessions.ts +61 -0
- package/src/skills.ts +197 -0
- package/src/sys.ts +136 -0
- package/src/tools/checkpoint.ts +164 -0
- package/src/tools/fs.ts +432 -0
- package/src/tools/grep.ts +268 -0
- package/src/tools/screenshot.ts +171 -0
- package/src/tools/shell.ts +357 -0
- package/src/tools/task.ts +105 -0
- package/src/tools/todo.ts +67 -0
- package/src/tools/web.ts +451 -0
- package/src/ui/configure.ts +397 -0
- package/src/ui/diff.ts +136 -0
- package/src/ui/editor.ts +136 -0
- package/src/ui/render.ts +74 -0
- package/src/ui/screen.ts +166 -0
- package/src/ui/transcript.ts +377 -0
- package/src/ui/tui.tsx +1662 -0
package/src/sys.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// 跨运行时的进程、可执行文件查找和 glob:只用 Node 的标准库,Bun 和 Node ≥22.18 下走同一套代码。
|
|
2
|
+
|
|
3
|
+
import { spawn as nodeSpawn, spawnSync as nodeSpawnSync } from "node:child_process";
|
|
4
|
+
import { accessSync, constants, existsSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import picomatch from "picomatch";
|
|
7
|
+
|
|
8
|
+
/** jade 自己的版本号,src 和 dist 下都从包根目录的 package.json 读 */
|
|
9
|
+
export const VERSION: string = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
10
|
+
|
|
11
|
+
type Stdio = "pipe" | "ignore";
|
|
12
|
+
|
|
13
|
+
export interface Proc {
|
|
14
|
+
pid: number;
|
|
15
|
+
/** stdout 为 "pipe" 时才有;Node 的 Readable 可以直接 for await 读出 Uint8Array */
|
|
16
|
+
stdout: AsyncIterable<Uint8Array>;
|
|
17
|
+
stderr: AsyncIterable<Uint8Array>;
|
|
18
|
+
/** 进程退出时 resolve 退出码;被信号杀掉时为 null */
|
|
19
|
+
exited: Promise<number | null>;
|
|
20
|
+
kill(): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SpawnOptions {
|
|
24
|
+
cwd?: string;
|
|
25
|
+
stdout?: Stdio;
|
|
26
|
+
stderr?: Stdio;
|
|
27
|
+
/** POSIX 上自成进程组,便于连同子进程一起杀掉 */
|
|
28
|
+
detached?: boolean;
|
|
29
|
+
/** 子进程的环境变量,默认继承 process.env */
|
|
30
|
+
env?: NodeJS.ProcessEnv;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const EMPTY: AsyncIterable<Uint8Array> = { async *[Symbol.asyncIterator]() {} };
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 启动进程,等到确实启动了才返回。程序不存在(ENOENT)等启动错误会 reject,
|
|
37
|
+
* 调用方用 try/catch 判断"没装",和 Bun.spawn 同步抛错的用法一致
|
|
38
|
+
*/
|
|
39
|
+
export function spawn(cmd: string[], opts: SpawnOptions = {}): Promise<Proc> {
|
|
40
|
+
const child = nodeSpawn(cmd[0]!, cmd.slice(1), {
|
|
41
|
+
cwd: opts.cwd,
|
|
42
|
+
stdio: ["ignore", opts.stdout ?? "pipe", opts.stderr ?? "pipe"],
|
|
43
|
+
detached: opts.detached ?? false,
|
|
44
|
+
env: opts.env,
|
|
45
|
+
// Windows 上不弹出控制台窗口
|
|
46
|
+
windowsHide: true,
|
|
47
|
+
});
|
|
48
|
+
const exited = new Promise<number | null>((resolve) => child.once("exit", (code) => resolve(code)));
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
child.once("error", reject);
|
|
51
|
+
child.once("spawn", () => {
|
|
52
|
+
child.off("error", reject);
|
|
53
|
+
// 启动之后的错误(例如 kill 一个已经退出的进程)不应该变成未处理的异常
|
|
54
|
+
child.on("error", () => {});
|
|
55
|
+
resolve({
|
|
56
|
+
pid: child.pid!,
|
|
57
|
+
stdout: child.stdout ?? EMPTY,
|
|
58
|
+
stderr: child.stderr ?? EMPTY,
|
|
59
|
+
exited,
|
|
60
|
+
kill: () => void child.kill(),
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 运行一条命令,返回 stdout 文本和退出码;启动失败时 reject */
|
|
67
|
+
export async function run(cmd: string[], opts: { cwd?: string } = {}): Promise<{ code: number | null; stdout: string }> {
|
|
68
|
+
const proc = await spawn(cmd, { cwd: opts.cwd, stderr: "ignore" });
|
|
69
|
+
const [stdout, code] = await Promise.all([text(proc.stdout), proc.exited]);
|
|
70
|
+
return { code, stdout };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 同步运行,忽略输出和错误;用于退出前的清理 */
|
|
74
|
+
export function runSync(cmd: string[], opts: { env?: NodeJS.ProcessEnv } = {}): void {
|
|
75
|
+
nodeSpawnSync(cmd[0]!, cmd.slice(1), { stdio: "ignore", windowsHide: true, env: opts.env });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function text(stream: AsyncIterable<Uint8Array>): Promise<string> {
|
|
79
|
+
const decoder = new TextDecoder();
|
|
80
|
+
let out = "";
|
|
81
|
+
for await (const chunk of stream) out += decoder.decode(chunk, { stream: true });
|
|
82
|
+
return out + decoder.decode();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 在 PATH 里找可执行文件,找不到返回 undefined。Windows 上按 PATHEXT 补扩展名;带路径分隔符时只检查那个文件 */
|
|
86
|
+
export function which(bin: string): string | undefined {
|
|
87
|
+
const win = process.platform === "win32";
|
|
88
|
+
const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
89
|
+
const ok = (file: string) => {
|
|
90
|
+
try {
|
|
91
|
+
if (!statSync(file).isFile()) return false;
|
|
92
|
+
if (!win) accessSync(file, constants.X_OK);
|
|
93
|
+
return true;
|
|
94
|
+
} catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const tryAll = (base: string) => {
|
|
99
|
+
for (const ext of exts) {
|
|
100
|
+
// Windows 上没有扩展名的文件不能直接执行(例如 npm 旁边那个给 Git Bash 用的 sh 脚本)
|
|
101
|
+
if (win && ext === "" && !path.extname(base)) continue;
|
|
102
|
+
if (ok(base + ext)) return base + ext;
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
};
|
|
106
|
+
if (bin.includes("/") || (win && bin.includes("\\"))) return tryAll(path.resolve(bin));
|
|
107
|
+
const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter).filter(Boolean);
|
|
108
|
+
for (const dir of dirs) {
|
|
109
|
+
const found = tryAll(path.join(dir.replace(/^"(.*)"$/, "$1"), bin));
|
|
110
|
+
if (found) return found;
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 编译 glob,返回匹配函数。路径用正斜杠、相对于搜索根目录。
|
|
117
|
+
* 和原先的 Bun.Glob#match 一致:* 也匹配点开头的文件,反斜杠是转义符而不是分隔符
|
|
118
|
+
*/
|
|
119
|
+
export function globMatcher(pattern: string): (rel: string) => boolean {
|
|
120
|
+
return picomatch(pattern, { dot: true, windows: false });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 找 npm 的 CLI 脚本,用当前的 node 运行它:Windows 上 npm 是 .cmd,不经过 shell 不能直接执行 */
|
|
124
|
+
export function npmCommand(): string[] | undefined {
|
|
125
|
+
const dirs = [path.dirname(process.execPath)];
|
|
126
|
+
const onPath = which(process.platform === "win32" ? "npm.cmd" : "npm");
|
|
127
|
+
if (onPath) dirs.push(path.dirname(onPath));
|
|
128
|
+
for (const dir of dirs) {
|
|
129
|
+
for (const cli of [path.join(dir, "node_modules", "npm", "bin", "npm-cli.js"), path.join(dir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js")]) {
|
|
130
|
+
if (existsSync(cli)) return [process.execPath, cli];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// POSIX 上 npm 本身就是可执行的脚本
|
|
134
|
+
if (onPath && process.platform !== "win32") return [onPath];
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// 文件快照:write_file / edit_file 写入前保存原内容,宿主的 /undo 用它恢复。
|
|
2
|
+
//
|
|
3
|
+
// 快照放在 <dir>/<sessionId>/<toolUseId>.json,不进日志:文件可能很大,而撤销是宿主的操作,不影响插件决策。
|
|
4
|
+
// 哪些修改已经撤销由宿主用 Runtime.record 记在日志里(UNDO_KEY),恢复会话后仍然有效。
|
|
5
|
+
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { appendFile, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import type { Message } from "uncarved";
|
|
10
|
+
|
|
11
|
+
/** derived 中保存已撤销的 toolUseId 的键 */
|
|
12
|
+
export const UNDO_KEY = "jade/undo";
|
|
13
|
+
|
|
14
|
+
export interface UndoState {
|
|
15
|
+
undone: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Checkpoint {
|
|
19
|
+
toolUseId: string;
|
|
20
|
+
tool: string;
|
|
21
|
+
/** 绝对路径 */
|
|
22
|
+
path: string;
|
|
23
|
+
/** 写入前的内容;null 表示文件原本不存在 */
|
|
24
|
+
before: string | null;
|
|
25
|
+
/** 写入后内容的 sha256,撤销前用来判断文件之后有没有被改过 */
|
|
26
|
+
afterHash: string;
|
|
27
|
+
/** 写入完成的时间(毫秒,带小数),决定撤销顺序 */
|
|
28
|
+
at: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const sha256 = (s: string) => createHash("sha256").update(s).digest("hex");
|
|
32
|
+
|
|
33
|
+
export class Checkpoints {
|
|
34
|
+
readonly #dir: string;
|
|
35
|
+
/** 会话 → 文件 → markSeen 记下的 sha256;第一次用到时从 seen.jsonl 读入 */
|
|
36
|
+
readonly #seen = new Map<string, Promise<Map<string, string>>>();
|
|
37
|
+
|
|
38
|
+
constructor(dir: string) {
|
|
39
|
+
this.#dir = dir;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async save(sessionId: string, cp: Omit<Checkpoint, "at">): Promise<void> {
|
|
43
|
+
const dir = path.join(this.#dir, safe(sessionId));
|
|
44
|
+
await mkdir(dir, { recursive: true });
|
|
45
|
+
const full: Checkpoint = { ...cp, at: performance.timeOrigin + performance.now() };
|
|
46
|
+
await writeFile(path.join(dir, `${safe(cp.toolUseId)}.json`), JSON.stringify(full));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 本会话的所有快照,最新的在前 */
|
|
50
|
+
async list(sessionId: string): Promise<Checkpoint[]> {
|
|
51
|
+
const dir = path.join(this.#dir, safe(sessionId));
|
|
52
|
+
let names: string[];
|
|
53
|
+
try {
|
|
54
|
+
names = await readdir(dir);
|
|
55
|
+
} catch {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
const out: Checkpoint[] = [];
|
|
59
|
+
for (const n of names) {
|
|
60
|
+
if (!n.endsWith(".json")) continue;
|
|
61
|
+
try {
|
|
62
|
+
out.push(JSON.parse(await readFile(path.join(dir, n), "utf8")) as Checkpoint);
|
|
63
|
+
} catch {
|
|
64
|
+
// 写到一半的快照,忽略
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return out.sort((a, b) => b.at - a.at);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 记下本会话读过或写过的文件当时内容的 sha256。fs 工具在内存里也记一份,
|
|
72
|
+
* 这里的是给恢复的会话用的:之前的进程里读过、写过的文件,内容没变就不用再读一遍。
|
|
73
|
+
* 追加到每个会话一个的 seen.jsonl,后面的记录覆盖前面的;内容没变的重复读取不再写
|
|
74
|
+
*/
|
|
75
|
+
async markSeen(sessionId: string, file: string, hash: string): Promise<void> {
|
|
76
|
+
const seen = await this.#seenOf(sessionId);
|
|
77
|
+
if (seen.get(file) === hash) return;
|
|
78
|
+
seen.set(file, hash);
|
|
79
|
+
const dir = path.join(this.#dir, safe(sessionId));
|
|
80
|
+
await mkdir(dir, { recursive: true });
|
|
81
|
+
await appendFile(path.join(dir, "seen.jsonl"), JSON.stringify({ path: file, hash }) + "\n");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** markSeen 记下的 sha256;没有记录返回 undefined */
|
|
85
|
+
async seenHash(sessionId: string, file: string): Promise<string | undefined> {
|
|
86
|
+
const hash = (await this.#seenOf(sessionId)).get(file);
|
|
87
|
+
if (hash !== undefined) return hash;
|
|
88
|
+
// 旧版本每个文件一个 seen/<sha256(path)>.json
|
|
89
|
+
try {
|
|
90
|
+
const rec = JSON.parse(await readFile(path.join(this.#dir, safe(sessionId), "seen", `${sha256(file)}.json`), "utf8")) as { path: string; hash: string };
|
|
91
|
+
return rec.path === file ? rec.hash : undefined;
|
|
92
|
+
} catch {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#seenOf(sessionId: string): Promise<Map<string, string>> {
|
|
98
|
+
let seen = this.#seen.get(sessionId);
|
|
99
|
+
if (!seen) {
|
|
100
|
+
seen = readFile(path.join(this.#dir, safe(sessionId), "seen.jsonl"), "utf8").then(
|
|
101
|
+
(text) => {
|
|
102
|
+
const map = new Map<string, string>();
|
|
103
|
+
for (const line of text.split("\n")) {
|
|
104
|
+
try {
|
|
105
|
+
const rec = JSON.parse(line) as { path: string; hash: string };
|
|
106
|
+
map.set(rec.path, rec.hash);
|
|
107
|
+
} catch {
|
|
108
|
+
// 空行或写到一半的一行
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return map;
|
|
112
|
+
},
|
|
113
|
+
() => new Map<string, string>(),
|
|
114
|
+
);
|
|
115
|
+
this.#seen.set(sessionId, seen);
|
|
116
|
+
}
|
|
117
|
+
return seen;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 文件现在的内容是否还是这次写入的结果 */
|
|
121
|
+
async unchangedSince(cp: Checkpoint): Promise<boolean> {
|
|
122
|
+
try {
|
|
123
|
+
return sha256(await readFile(cp.path, "utf8")) === cp.afterHash;
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 把文件恢复成写入前的样子;原本不存在的文件删掉 */
|
|
130
|
+
async restore(cp: Checkpoint): Promise<void> {
|
|
131
|
+
if (cp.before === null) await rm(cp.path, { force: true });
|
|
132
|
+
else {
|
|
133
|
+
await mkdir(path.dirname(cp.path), { recursive: true });
|
|
134
|
+
await writeFile(cp.path, cp.before);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 最近一轮还有没撤销的修改的快照(最新的在前)。一轮从一条用户消息开始;
|
|
141
|
+
* 只有工具结果、或者只有宿主说明(例如自动续写的提示)的消息不算新的一轮。没有可撤销的返回空数组
|
|
142
|
+
*/
|
|
143
|
+
export function lastTurnChanges(messages: readonly Message[], checkpoints: readonly Checkpoint[], undone: ReadonlySet<string>): Checkpoint[] {
|
|
144
|
+
const pending = checkpoints.filter((c) => !undone.has(c.toolUseId));
|
|
145
|
+
if (pending.length === 0) return [];
|
|
146
|
+
let ids = new Set<string>();
|
|
147
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
148
|
+
const m = messages[i]!;
|
|
149
|
+
if (m.role === "assistant") {
|
|
150
|
+
for (const b of m.content) if (b.kind === "tool_use") ids.add(b.id);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const startsTurn = m.content.some((b) => b.kind !== "tool_result" && b.origin.by !== "host");
|
|
154
|
+
if (!startsTurn) continue;
|
|
155
|
+
const hits = pending.filter((c) => ids.has(c.toolUseId));
|
|
156
|
+
if (hits.length > 0) return hits;
|
|
157
|
+
ids = new Set();
|
|
158
|
+
}
|
|
159
|
+
// 找不到对应的消息(不应该发生):退回到只撤销最近的一次写入
|
|
160
|
+
return pending.slice(0, 1);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 会话和工具调用的 id 用作文件名 */
|
|
164
|
+
const safe = (id: string) => id.replace(/[^A-Za-z0-9_.-]/g, "_");
|