@celljs/ta-changes 0.0.0-stage → 4.8.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.en.md +21 -0
- package/README.md +20 -2
- package/extension/index.mjs +296 -0
- package/host/index.mjs +309 -0
- package/package.json +44 -4
- package/web/index.js +460 -0
- package/web-src/blob-store.spec.ts +33 -0
- package/web-src/blob-store.ts +68 -0
- package/web-src/card.tsx +164 -0
- package/web-src/changes-protocol.ts +85 -0
- package/web-src/changes-service.spec.ts +61 -0
- package/web-src/changes-service.ts +75 -0
- package/web-src/changes-util.spec.ts +33 -0
- package/web-src/changes-util.ts +44 -0
- package/web-src/diff-tab.tsx +60 -0
- package/web-src/extension.ts +143 -0
- package/web-src/host.ts +14 -0
- package/web-src/i18n.ts +59 -0
- package/web-src/index.tsx +26 -0
- package/web-src/line-diff.spec.ts +25 -0
- package/web-src/line-diff.ts +65 -0
- package/web-src/page.ts +107 -0
- package/web-src/react.ts +11 -0
- package/web-src/recorder.spec.ts +124 -0
- package/web-src/recorder.ts +114 -0
- package/web-src/style.ts +40 -0
package/README.en.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Cell - Ta Changes
|
|
2
|
+
|
|
3
|
+
A pi package that comes with Ta (on by default; turn it off in Settings → Extensions of the web client): which files each reply changed. When a reply ends, a pill stays above the message box, "N files changed +added −removed" (for the last reply of the conversation that changed files; it goes once they are taken back). It opens to a list above it of the files (name, directory, lines added and removed; files Ta created are marked "new"); a click on a file opens its diff in the workbench on the right (before and after, read-only). "Undo the changes of this reply" at the bottom of the list puts the files back as they were before the reply, and removes those Ta created.
|
|
4
|
+
|
|
5
|
+
It has three parts, none of which changes the core of Ta:
|
|
6
|
+
|
|
7
|
+
- **pi extension** (`pi.extensions`, `web-src/extension.ts` → `extension/index.mjs`): before the `edit` or `write` tool (also a nested call in a codemode script) writes a file the first time in a reply, it reads what the file holds. When the reply ends (`agent_settled`, also when it is stopped), it compares each file before and after and sends a `ta-changes` custom message that the conversation does not show (`display: false`), whose `details` hold the files, their lines added and removed, and the hashes of their contents before and after. The message is for people: the extension leaves it out of what the model reads, in the `context` event. Files are read and written through `files` of the answer to `TA_HOST_EVENT` (the files of the session's execution environment), so `ssh://` directories work as well.
|
|
8
|
+
- **Host** (`pi.host`, `web-src/host.ts` → `host/index.mjs`): keeps the contents before and after by their hash (`<agentDir>/changes/<first two>/<hash>`; the same contents once; what was not used for 30 days is removed when ta serve starts), and serves the page: `latest` for the last change set on the branch of a conversation, `read` for contents, `revert` to undo. Undoing is done by the pi extension of the session; the change sets taken back are listed under the `reverted` key of the package's data, the same in every browser.
|
|
9
|
+
- **Web extension** (`pi.web`, `web-src/index.tsx` → `web/index.js`): puts the pill and the list above the message box with `ui.setWidget` (placement `aboveEditor`): it asks the host for the last change set when a conversation is shown, then follows `message_end` events; the note of an undo is one line of the conversation (`registerMessageRenderer`); the diff is `pi.ui.DiffEditor`.
|
|
10
|
+
|
|
11
|
+
## Undo
|
|
12
|
+
|
|
13
|
+
- Only while Ta is not working in the conversation, and the conversation is open in the Ta service (a conversation the page shows is).
|
|
14
|
+
- When a file changed again after the reply (by hand, by a later reply or by another program), it asks first; only then does it overwrite them, and the later changes are lost. Files already as they were before the reply are skipped.
|
|
15
|
+
- After undoing, a line "Took back the changes to N files" is added to the conversation. That message reaches the model: it tells Ta the files are back, and to read them again before changing them.
|
|
16
|
+
|
|
17
|
+
## Limits
|
|
18
|
+
|
|
19
|
+
- Files written by the `edit` and `write` tools, and, with another coding agent as the model (CodeBuddy Code, Claude Code, Codex; see ta-agents), those it writes with its own editing tools: ta-agents emits `TA_FILE_WRITE_EVENT` on `pi.events` as soon as it knows which file a call changes (mostly before it runs), with the text before when the agent sends it (an ACP diff), which then counts; otherwise the file is read then. Files changed by `bash` and other commands are not listed, and not taken back.
|
|
20
|
+
- Lines added and removed are counted as `git diff --stat` counts them. Binary files count no lines and show no diff, but can be taken back. Files larger than 4 MB are counted, but their contents are not kept: they cannot be taken back.
|
|
21
|
+
- Only in the Ta service (ta serve); `ta` alone (in a terminal) records nothing.
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - Ta Changes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ta 自带的 pi 包(默认开启,可在 Web 客户端的 设置 → 扩展 关闭):每轮回复改动了哪些文件。回复结束后,输入框上方固定显示一个胶囊按钮"N 个文件已更改 +增 −删"(对话里最近一轮改动了文件的回复,撤销后消失),点开是在它上方弹出的改动文件列表(文件名、所在目录、增删行数,Ta 新建的文件标"新建"),点文件在右侧工作台以差异视图打开(改动前后对比,只读);列表底部"撤销本轮改动"把这些文件恢复成这轮回复之前的样子,Ta 新建的文件会被删除。
|
|
4
|
+
|
|
5
|
+
由三部分组成,都不改 Ta 的核心:
|
|
6
|
+
|
|
7
|
+
- **pi 扩展**(`pi.extensions`,`web-src/extension.ts` → `extension/index.mjs`):`edit`、`write` 工具(包括 codemode 脚本里的嵌套调用)第一次在本轮写某个文件之前,读下文件当时的内容;回复结束(`agent_settled`,中途停止也算)时比较每个文件的前后内容,发一条不在对话里显示的 `ta-changes` 自定义消息(`display: false`),`details` 里是改动的文件、增删行数和前后内容的哈希。这条消息只给人看:扩展在 `context` 事件里把它从发给模型的上下文中去掉。文件经 `TA_HOST_EVENT` 应答里的 `files`(会话执行环境里的文件)读写,所以 `ssh://` 远程目录同样适用。
|
|
8
|
+
- **服务端**(`pi.host`,`web-src/host.ts` → `host/index.mjs`):按内容哈希保存改动前后的文件内容(`<agentDir>/changes/<前两位>/<哈希>`,相同内容只存一份,30 天没有再用到的在 ta serve 启动时清理);给页面取对话当前分支上最近的一次改动(`latest`)、读内容(`read`)和撤销(`revert`)。撤销交给该会话的 pi 扩展执行,已撤销的改动记在包数据的 `reverted` 键里,所有浏览器看到的都一样。
|
|
9
|
+
- **Web 扩展**(`pi.web`,`web-src/index.tsx` → `web/index.js`):用 `ui.setWidget`(默认位置 `aboveEditor`)把胶囊和列表放在输入框上方:切到一个对话时向服务端要最近的一次改动,之后随 `message_end` 事件更新;撤销说明用 `registerMessageRenderer` 画成对话里的一行;差异视图用 `pi.ui.DiffEditor`。
|
|
10
|
+
|
|
11
|
+
## 撤销
|
|
12
|
+
|
|
13
|
+
- 只在 Ta 没有在这个对话里工作时才能撤销;对话需要在 Ta 服务里开着(页面上正显示的对话都是开着的)。
|
|
14
|
+
- 文件在这轮之后又被改过(用户手改、之后的回复或别的程序)时先询问:确认后才覆盖,之后的修改会丢失。已经是这轮之前样子的文件跳过。
|
|
15
|
+
- 撤销后,对话里加一行"已撤销 N 个文件的改动",这条消息会进入模型的上下文,告诉 Ta 这些文件已恢复,改之前要重新读。
|
|
16
|
+
|
|
17
|
+
## 限制
|
|
18
|
+
|
|
19
|
+
- 记录 `edit`、`write` 工具写的文件,以及选了别的 coding agent(CodeBuddy Code、Claude Code、Codex,见 ta-agents)时它用自己的编辑工具写的文件:ta-agents 一知道某次调用要改哪个文件(多在执行之前),就经 `pi.events` 发 `TA_FILE_WRITE_EVENT`,带上 Agent 给出的改动前文本(ACP 的 diff)时以它为准,否则这时读下文件。`bash` 等命令改动的文件不在列表里,也不会被撤销。
|
|
20
|
+
- 增删行数照 `git diff --stat` 的算法;二进制文件不计行数、不显示差异,但能撤销。超过 4 MB 的文件只计数,不保存内容,不能撤销。
|
|
21
|
+
- 只在 Ta 服务(ta serve)里工作;单独运行 `ta`(终端)时不记录。
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// web-src/changes-protocol.ts
|
|
2
|
+
var NAME = "@celljs/ta-changes";
|
|
3
|
+
var TA_HOST_EVENT = "ta:host";
|
|
4
|
+
var TA_FILE_WRITE_EVENT = "ta:file-write";
|
|
5
|
+
var CHANGES_TYPE = "ta-changes";
|
|
6
|
+
var REVERT_TYPE = "ta-changes-revert";
|
|
7
|
+
var MAX_KEPT_BYTES = 4 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
// web-src/changes-util.ts
|
|
10
|
+
var ChangesUtil;
|
|
11
|
+
((ChangesUtil) => {
|
|
12
|
+
function isText(data) {
|
|
13
|
+
return !data?.subarray(0, 8000).includes(0);
|
|
14
|
+
}
|
|
15
|
+
ChangesUtil.isText = isText;
|
|
16
|
+
function totals(files) {
|
|
17
|
+
return files.reduce((sum, file) => ({ added: sum.added + file.added, removed: sum.removed + file.removed }), { added: 0, removed: 0 });
|
|
18
|
+
}
|
|
19
|
+
ChangesUtil.totals = totals;
|
|
20
|
+
function relative(path, root) {
|
|
21
|
+
const base = root.replace(/[\\/]+$/, "");
|
|
22
|
+
if (base && path.length > base.length && path.startsWith(base) && /[\\/]/.test(path[base.length])) {
|
|
23
|
+
return path.slice(base.length + 1);
|
|
24
|
+
}
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
ChangesUtil.relative = relative;
|
|
28
|
+
function place(path, root) {
|
|
29
|
+
const shown = relative(path, root);
|
|
30
|
+
const index = Math.max(shown.lastIndexOf("/"), shown.lastIndexOf("\\"));
|
|
31
|
+
return index < 0 ? { name: shown, dir: "" } : { name: shown.slice(index + 1), dir: shown.slice(0, index) || shown.slice(0, 1) };
|
|
32
|
+
}
|
|
33
|
+
ChangesUtil.place = place;
|
|
34
|
+
function summary(set) {
|
|
35
|
+
const files = set.files.map((file) => `${relative(file.path, set.root)} (${file.before === undefined && !file.large ? "new, " : ""}+${file.added} -${file.removed})`);
|
|
36
|
+
return `Changed ${set.files.length === 1 ? "1 file" : `${set.files.length} files`}: ${files.join(", ")}`;
|
|
37
|
+
}
|
|
38
|
+
ChangesUtil.summary = summary;
|
|
39
|
+
function revertNote(paths, root) {
|
|
40
|
+
return "The user took back the changes you made to these files in one of your earlier replies; they are as they were before that reply: " + `${paths.map((path) => relative(path, root)).join(", ")}. Read them again before you change them.`;
|
|
41
|
+
}
|
|
42
|
+
ChangesUtil.revertNote = revertNote;
|
|
43
|
+
})(ChangesUtil ||= {});
|
|
44
|
+
|
|
45
|
+
// web-src/recorder.ts
|
|
46
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
47
|
+
|
|
48
|
+
// web-src/line-diff.ts
|
|
49
|
+
var LineDiff;
|
|
50
|
+
((LineDiff) => {
|
|
51
|
+
LineDiff.MAX_EDITS = 4000;
|
|
52
|
+
function lines(text) {
|
|
53
|
+
if (!text) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
const lines2 = text.split(`
|
|
57
|
+
`);
|
|
58
|
+
if (lines2[lines2.length - 1] === "") {
|
|
59
|
+
lines2.pop();
|
|
60
|
+
}
|
|
61
|
+
return lines2;
|
|
62
|
+
}
|
|
63
|
+
LineDiff.lines = lines;
|
|
64
|
+
function count(before, after) {
|
|
65
|
+
const a = lines(before);
|
|
66
|
+
const b = lines(after);
|
|
67
|
+
let start = 0;
|
|
68
|
+
while (start < a.length && start < b.length && a[start] === b[start]) {
|
|
69
|
+
start++;
|
|
70
|
+
}
|
|
71
|
+
let endA = a.length;
|
|
72
|
+
let endB = b.length;
|
|
73
|
+
while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
|
|
74
|
+
endA--;
|
|
75
|
+
endB--;
|
|
76
|
+
}
|
|
77
|
+
const n = endA - start;
|
|
78
|
+
const m = endB - start;
|
|
79
|
+
if (!n || !m) {
|
|
80
|
+
return { added: m, removed: n };
|
|
81
|
+
}
|
|
82
|
+
const edits = distance(a.slice(start, endA), b.slice(start, endB), Math.min(n + m, LineDiff.MAX_EDITS));
|
|
83
|
+
if (edits === undefined) {
|
|
84
|
+
return { added: m, removed: n };
|
|
85
|
+
}
|
|
86
|
+
return { added: (edits + m - n) / 2, removed: (edits - m + n) / 2 };
|
|
87
|
+
}
|
|
88
|
+
LineDiff.count = count;
|
|
89
|
+
function distance(a, b, max) {
|
|
90
|
+
const offset = max + 1;
|
|
91
|
+
const furthest = new Int32Array(2 * max + 3);
|
|
92
|
+
for (let edits = 0;edits <= max; edits++) {
|
|
93
|
+
for (let diagonal = -edits;diagonal <= edits; diagonal += 2) {
|
|
94
|
+
const down = diagonal === -edits || diagonal !== edits && furthest[offset + diagonal - 1] < furthest[offset + diagonal + 1];
|
|
95
|
+
let x = down ? furthest[offset + diagonal + 1] : furthest[offset + diagonal - 1] + 1;
|
|
96
|
+
let y = x - diagonal;
|
|
97
|
+
while (x < a.length && y < b.length && a[x] === b[y]) {
|
|
98
|
+
x++;
|
|
99
|
+
y++;
|
|
100
|
+
}
|
|
101
|
+
furthest[offset + diagonal] = x;
|
|
102
|
+
if (x >= a.length && y >= b.length) {
|
|
103
|
+
return edits;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
})(LineDiff ||= {});
|
|
110
|
+
|
|
111
|
+
// web-src/recorder.ts
|
|
112
|
+
function hash(data) {
|
|
113
|
+
return createHash("sha256").update(data).digest("hex");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
class ChangeRecorder {
|
|
117
|
+
files;
|
|
118
|
+
service;
|
|
119
|
+
befores = new Map;
|
|
120
|
+
constructor(files, service) {
|
|
121
|
+
this.files = files;
|
|
122
|
+
this.service = service;
|
|
123
|
+
}
|
|
124
|
+
async before(path, known) {
|
|
125
|
+
if (!this.befores.has(path)) {
|
|
126
|
+
this.befores.set(path, known === undefined ? this.files.read(path) : Promise.resolve(known === null ? undefined : new TextEncoder().encode(known)));
|
|
127
|
+
}
|
|
128
|
+
await this.befores.get(path)?.catch(() => {
|
|
129
|
+
return;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async finish(root) {
|
|
133
|
+
const befores = this.befores;
|
|
134
|
+
this.befores = new Map;
|
|
135
|
+
const files = [];
|
|
136
|
+
for (const [path, read] of befores) {
|
|
137
|
+
try {
|
|
138
|
+
const file = await this.compare(path, await read, await this.files.read(path));
|
|
139
|
+
if (file) {
|
|
140
|
+
files.push(file);
|
|
141
|
+
}
|
|
142
|
+
} catch {}
|
|
143
|
+
}
|
|
144
|
+
return files.length ? { id: randomUUID(), root, files } : undefined;
|
|
145
|
+
}
|
|
146
|
+
async compare(path, before, after) {
|
|
147
|
+
if (before === after || before && after && Buffer.compare(before, after) === 0) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const binary = !ChangesUtil.isText(before) || !ChangesUtil.isText(after);
|
|
151
|
+
const decoder = new TextDecoder;
|
|
152
|
+
const counts = binary ? { added: 0, removed: 0 } : LineDiff.count(before ? decoder.decode(before) : "", after ? decoder.decode(after) : "");
|
|
153
|
+
const file = { path, ...counts };
|
|
154
|
+
if (binary) {
|
|
155
|
+
file.binary = true;
|
|
156
|
+
}
|
|
157
|
+
if ((before?.length ?? 0) > MAX_KEPT_BYTES || (after?.length ?? 0) > MAX_KEPT_BYTES) {
|
|
158
|
+
file.large = true;
|
|
159
|
+
return file;
|
|
160
|
+
}
|
|
161
|
+
if (before) {
|
|
162
|
+
file.before = await this.service.store(before);
|
|
163
|
+
}
|
|
164
|
+
if (after) {
|
|
165
|
+
file.after = await this.service.store(after);
|
|
166
|
+
}
|
|
167
|
+
return file;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async function revert(set, files, service, force) {
|
|
171
|
+
const result = { reverted: [], conflicts: [], skipped: [] };
|
|
172
|
+
const pending = [];
|
|
173
|
+
for (const file of set.files) {
|
|
174
|
+
if (file.large) {
|
|
175
|
+
result.skipped.push(file.path);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const current = await files.read(file.path);
|
|
179
|
+
const now = current && hash(current);
|
|
180
|
+
if (now === file.before) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (now !== file.after) {
|
|
184
|
+
result.conflicts.push(file.path);
|
|
185
|
+
}
|
|
186
|
+
pending.push(file);
|
|
187
|
+
}
|
|
188
|
+
if (result.conflicts.length && !force) {
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
for (const file of pending) {
|
|
192
|
+
if (file.before === undefined) {
|
|
193
|
+
await files.remove(file.path);
|
|
194
|
+
} else {
|
|
195
|
+
const data = await service.load(file.before);
|
|
196
|
+
if (!data) {
|
|
197
|
+
result.skipped.push(file.path);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
await files.write(file.path, data);
|
|
201
|
+
}
|
|
202
|
+
result.reverted.push(file.path);
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// web-src/extension.ts
|
|
208
|
+
var HOST_WAIT_MS = 1000;
|
|
209
|
+
var WRITING_TOOLS = new Set(["edit", "write"]);
|
|
210
|
+
function activate(pi) {
|
|
211
|
+
let reached;
|
|
212
|
+
let recorder;
|
|
213
|
+
let detach;
|
|
214
|
+
let reachedFiles;
|
|
215
|
+
const reach = () => reached ??= new Promise((resolve) => {
|
|
216
|
+
const timer = setTimeout(() => resolve(undefined), HOST_WAIT_MS);
|
|
217
|
+
pi.events.emit(TA_HOST_EVENT, {
|
|
218
|
+
name: NAME,
|
|
219
|
+
resolve: (answer) => {
|
|
220
|
+
clearTimeout(timer);
|
|
221
|
+
resolve(answer.service && answer.files ? { service: answer.service, files: answer.files } : undefined);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
226
|
+
const found = await reach();
|
|
227
|
+
if (!found) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
recorder = new ChangeRecorder(found.files, found.service);
|
|
231
|
+
reachedFiles = found.files;
|
|
232
|
+
detach?.();
|
|
233
|
+
detach = found.service.attach(ctx.sessionManager.getSessionId(), {
|
|
234
|
+
revert: (id, force) => takeBack(ctx, found, id, force),
|
|
235
|
+
latest: () => changeSets(ctx.sessionManager.getBranch()).at(-1)
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
pi.on("session_shutdown", () => {
|
|
239
|
+
detach?.();
|
|
240
|
+
detach = undefined;
|
|
241
|
+
});
|
|
242
|
+
pi.on("tool_call", async (event) => {
|
|
243
|
+
const path = event.input?.path;
|
|
244
|
+
if (recorder && WRITING_TOOLS.has(event.toolName) && typeof path === "string" && path) {
|
|
245
|
+
const found = await reach();
|
|
246
|
+
if (found) {
|
|
247
|
+
await recorder.before(found.files.resolve(path));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
pi.events.on(TA_FILE_WRITE_EVENT, (data) => {
|
|
252
|
+
const file = data;
|
|
253
|
+
if (recorder && reachedFiles && typeof file?.path === "string" && file.path) {
|
|
254
|
+
const before = typeof file.before === "string" || file.before === null ? file.before : undefined;
|
|
255
|
+
recorder.before(reachedFiles.resolve(file.path), before);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
pi.on("agent_settled", async () => {
|
|
259
|
+
const found = recorder && await reach();
|
|
260
|
+
const set = found && await recorder?.finish(found.files.resolve("."));
|
|
261
|
+
if (set) {
|
|
262
|
+
pi.sendMessage({ customType: CHANGES_TYPE, content: ChangesUtil.summary(set), display: false, details: set });
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
pi.on("context", (event) => {
|
|
266
|
+
const messages = event.messages.filter((message) => !(message.role === "custom" && message.customType === CHANGES_TYPE));
|
|
267
|
+
return messages.length === event.messages.length ? undefined : { messages };
|
|
268
|
+
});
|
|
269
|
+
async function takeBack(ctx, found, id, force) {
|
|
270
|
+
if (!ctx.isIdle()) {
|
|
271
|
+
return { reverted: [], conflicts: [], skipped: [], refused: "busy" };
|
|
272
|
+
}
|
|
273
|
+
const set = find(ctx, id);
|
|
274
|
+
if (!set) {
|
|
275
|
+
return { reverted: [], conflicts: [], skipped: [], refused: "missing" };
|
|
276
|
+
}
|
|
277
|
+
const result = await revert(set, found.files, found.service, force);
|
|
278
|
+
if (result.reverted.length) {
|
|
279
|
+
const details = { id, files: result.reverted };
|
|
280
|
+
pi.sendMessage({ customType: REVERT_TYPE, content: ChangesUtil.revertNote(result.reverted, set.root), display: true, details });
|
|
281
|
+
}
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function find(ctx, id) {
|
|
286
|
+
return changeSets(ctx.sessionManager.getEntries()).find((set) => set.id === id);
|
|
287
|
+
}
|
|
288
|
+
function changeSets(entries) {
|
|
289
|
+
return entries.flatMap((entry) => {
|
|
290
|
+
const details = entry.details;
|
|
291
|
+
return entry.type === "custom_message" && entry.customType === CHANGES_TYPE && typeof details?.id === "string" && Array.isArray(details.files) ? [details] : [];
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
export {
|
|
295
|
+
activate as default
|
|
296
|
+
};
|
package/host/index.mjs
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// web-src/host.ts
|
|
2
|
+
import { join as join2 } from "node:path";
|
|
3
|
+
|
|
4
|
+
// web-src/blob-store.ts
|
|
5
|
+
import { mkdir, readdir, readFile, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
// web-src/recorder.ts
|
|
9
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
10
|
+
|
|
11
|
+
// web-src/changes-protocol.ts
|
|
12
|
+
var NAME = "@celljs/ta-changes";
|
|
13
|
+
var REVERTED_KEY = "reverted";
|
|
14
|
+
var REVERTED_KEPT = 1000;
|
|
15
|
+
var MAX_KEPT_BYTES = 4 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
// web-src/changes-util.ts
|
|
18
|
+
var ChangesUtil;
|
|
19
|
+
((ChangesUtil) => {
|
|
20
|
+
function isText(data) {
|
|
21
|
+
return !data?.subarray(0, 8000).includes(0);
|
|
22
|
+
}
|
|
23
|
+
ChangesUtil.isText = isText;
|
|
24
|
+
function totals(files) {
|
|
25
|
+
return files.reduce((sum, file) => ({ added: sum.added + file.added, removed: sum.removed + file.removed }), { added: 0, removed: 0 });
|
|
26
|
+
}
|
|
27
|
+
ChangesUtil.totals = totals;
|
|
28
|
+
function relative(path, root) {
|
|
29
|
+
const base = root.replace(/[\\/]+$/, "");
|
|
30
|
+
if (base && path.length > base.length && path.startsWith(base) && /[\\/]/.test(path[base.length])) {
|
|
31
|
+
return path.slice(base.length + 1);
|
|
32
|
+
}
|
|
33
|
+
return path;
|
|
34
|
+
}
|
|
35
|
+
ChangesUtil.relative = relative;
|
|
36
|
+
function place(path, root) {
|
|
37
|
+
const shown = relative(path, root);
|
|
38
|
+
const index = Math.max(shown.lastIndexOf("/"), shown.lastIndexOf("\\"));
|
|
39
|
+
return index < 0 ? { name: shown, dir: "" } : { name: shown.slice(index + 1), dir: shown.slice(0, index) || shown.slice(0, 1) };
|
|
40
|
+
}
|
|
41
|
+
ChangesUtil.place = place;
|
|
42
|
+
function summary(set) {
|
|
43
|
+
const files = set.files.map((file) => `${relative(file.path, set.root)} (${file.before === undefined && !file.large ? "new, " : ""}+${file.added} -${file.removed})`);
|
|
44
|
+
return `Changed ${set.files.length === 1 ? "1 file" : `${set.files.length} files`}: ${files.join(", ")}`;
|
|
45
|
+
}
|
|
46
|
+
ChangesUtil.summary = summary;
|
|
47
|
+
function revertNote(paths, root) {
|
|
48
|
+
return "The user took back the changes you made to these files in one of your earlier replies; they are as they were before that reply: " + `${paths.map((path) => relative(path, root)).join(", ")}. Read them again before you change them.`;
|
|
49
|
+
}
|
|
50
|
+
ChangesUtil.revertNote = revertNote;
|
|
51
|
+
})(ChangesUtil ||= {});
|
|
52
|
+
|
|
53
|
+
// web-src/line-diff.ts
|
|
54
|
+
var LineDiff;
|
|
55
|
+
((LineDiff) => {
|
|
56
|
+
LineDiff.MAX_EDITS = 4000;
|
|
57
|
+
function lines(text) {
|
|
58
|
+
if (!text) {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const lines2 = text.split(`
|
|
62
|
+
`);
|
|
63
|
+
if (lines2[lines2.length - 1] === "") {
|
|
64
|
+
lines2.pop();
|
|
65
|
+
}
|
|
66
|
+
return lines2;
|
|
67
|
+
}
|
|
68
|
+
LineDiff.lines = lines;
|
|
69
|
+
function count(before, after) {
|
|
70
|
+
const a = lines(before);
|
|
71
|
+
const b = lines(after);
|
|
72
|
+
let start = 0;
|
|
73
|
+
while (start < a.length && start < b.length && a[start] === b[start]) {
|
|
74
|
+
start++;
|
|
75
|
+
}
|
|
76
|
+
let endA = a.length;
|
|
77
|
+
let endB = b.length;
|
|
78
|
+
while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
|
|
79
|
+
endA--;
|
|
80
|
+
endB--;
|
|
81
|
+
}
|
|
82
|
+
const n = endA - start;
|
|
83
|
+
const m = endB - start;
|
|
84
|
+
if (!n || !m) {
|
|
85
|
+
return { added: m, removed: n };
|
|
86
|
+
}
|
|
87
|
+
const edits = distance(a.slice(start, endA), b.slice(start, endB), Math.min(n + m, LineDiff.MAX_EDITS));
|
|
88
|
+
if (edits === undefined) {
|
|
89
|
+
return { added: m, removed: n };
|
|
90
|
+
}
|
|
91
|
+
return { added: (edits + m - n) / 2, removed: (edits - m + n) / 2 };
|
|
92
|
+
}
|
|
93
|
+
LineDiff.count = count;
|
|
94
|
+
function distance(a, b, max) {
|
|
95
|
+
const offset = max + 1;
|
|
96
|
+
const furthest = new Int32Array(2 * max + 3);
|
|
97
|
+
for (let edits = 0;edits <= max; edits++) {
|
|
98
|
+
for (let diagonal = -edits;diagonal <= edits; diagonal += 2) {
|
|
99
|
+
const down = diagonal === -edits || diagonal !== edits && furthest[offset + diagonal - 1] < furthest[offset + diagonal + 1];
|
|
100
|
+
let x = down ? furthest[offset + diagonal + 1] : furthest[offset + diagonal - 1] + 1;
|
|
101
|
+
let y = x - diagonal;
|
|
102
|
+
while (x < a.length && y < b.length && a[x] === b[y]) {
|
|
103
|
+
x++;
|
|
104
|
+
y++;
|
|
105
|
+
}
|
|
106
|
+
furthest[offset + diagonal] = x;
|
|
107
|
+
if (x >= a.length && y >= b.length) {
|
|
108
|
+
return edits;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
})(LineDiff ||= {});
|
|
115
|
+
|
|
116
|
+
// web-src/recorder.ts
|
|
117
|
+
function hash(data) {
|
|
118
|
+
return createHash("sha256").update(data).digest("hex");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class ChangeRecorder {
|
|
122
|
+
files;
|
|
123
|
+
service;
|
|
124
|
+
befores = new Map;
|
|
125
|
+
constructor(files, service) {
|
|
126
|
+
this.files = files;
|
|
127
|
+
this.service = service;
|
|
128
|
+
}
|
|
129
|
+
async before(path, known) {
|
|
130
|
+
if (!this.befores.has(path)) {
|
|
131
|
+
this.befores.set(path, known === undefined ? this.files.read(path) : Promise.resolve(known === null ? undefined : new TextEncoder().encode(known)));
|
|
132
|
+
}
|
|
133
|
+
await this.befores.get(path)?.catch(() => {
|
|
134
|
+
return;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async finish(root) {
|
|
138
|
+
const befores = this.befores;
|
|
139
|
+
this.befores = new Map;
|
|
140
|
+
const files = [];
|
|
141
|
+
for (const [path, read] of befores) {
|
|
142
|
+
try {
|
|
143
|
+
const file = await this.compare(path, await read, await this.files.read(path));
|
|
144
|
+
if (file) {
|
|
145
|
+
files.push(file);
|
|
146
|
+
}
|
|
147
|
+
} catch {}
|
|
148
|
+
}
|
|
149
|
+
return files.length ? { id: randomUUID(), root, files } : undefined;
|
|
150
|
+
}
|
|
151
|
+
async compare(path, before, after) {
|
|
152
|
+
if (before === after || before && after && Buffer.compare(before, after) === 0) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const binary = !ChangesUtil.isText(before) || !ChangesUtil.isText(after);
|
|
156
|
+
const decoder = new TextDecoder;
|
|
157
|
+
const counts = binary ? { added: 0, removed: 0 } : LineDiff.count(before ? decoder.decode(before) : "", after ? decoder.decode(after) : "");
|
|
158
|
+
const file = { path, ...counts };
|
|
159
|
+
if (binary) {
|
|
160
|
+
file.binary = true;
|
|
161
|
+
}
|
|
162
|
+
if ((before?.length ?? 0) > MAX_KEPT_BYTES || (after?.length ?? 0) > MAX_KEPT_BYTES) {
|
|
163
|
+
file.large = true;
|
|
164
|
+
return file;
|
|
165
|
+
}
|
|
166
|
+
if (before) {
|
|
167
|
+
file.before = await this.service.store(before);
|
|
168
|
+
}
|
|
169
|
+
if (after) {
|
|
170
|
+
file.after = await this.service.store(after);
|
|
171
|
+
}
|
|
172
|
+
return file;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// web-src/blob-store.ts
|
|
177
|
+
var HASH = /^[0-9a-f]{64}$/;
|
|
178
|
+
|
|
179
|
+
class BlobStore {
|
|
180
|
+
dir;
|
|
181
|
+
static KEEP_DAYS = 30;
|
|
182
|
+
constructor(dir) {
|
|
183
|
+
this.dir = dir;
|
|
184
|
+
}
|
|
185
|
+
static isHash(value) {
|
|
186
|
+
return typeof value === "string" && HASH.test(value);
|
|
187
|
+
}
|
|
188
|
+
async store(data) {
|
|
189
|
+
const name = hash(data);
|
|
190
|
+
const file = this.file(name);
|
|
191
|
+
const now = new Date;
|
|
192
|
+
try {
|
|
193
|
+
await utimes(file, now, now);
|
|
194
|
+
} catch {
|
|
195
|
+
await mkdir(join(this.dir, name.slice(0, 2)), { recursive: true });
|
|
196
|
+
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
197
|
+
await writeFile(temporary, data);
|
|
198
|
+
await rename(temporary, file);
|
|
199
|
+
}
|
|
200
|
+
return name;
|
|
201
|
+
}
|
|
202
|
+
async load(name) {
|
|
203
|
+
if (!BlobStore.isHash(name)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
return await readFile(this.file(name));
|
|
208
|
+
} catch {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async prune(days = BlobStore.KEEP_DAYS, now = Date.now()) {
|
|
213
|
+
const before = now - days * 24 * 60 * 60 * 1000;
|
|
214
|
+
let removed = 0;
|
|
215
|
+
for (const group of await readdir(this.dir).catch(() => [])) {
|
|
216
|
+
const dir = join(this.dir, group);
|
|
217
|
+
for (const name of await readdir(dir).catch(() => [])) {
|
|
218
|
+
const file = join(dir, name);
|
|
219
|
+
const info = await stat(file).catch(() => {
|
|
220
|
+
return;
|
|
221
|
+
});
|
|
222
|
+
if (info && info.mtimeMs < before) {
|
|
223
|
+
await rm(file, { force: true });
|
|
224
|
+
removed++;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return removed;
|
|
229
|
+
}
|
|
230
|
+
file(name) {
|
|
231
|
+
return join(this.dir, name.slice(0, 2), name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// web-src/changes-service.ts
|
|
236
|
+
class DefaultChangesService {
|
|
237
|
+
host;
|
|
238
|
+
blobs;
|
|
239
|
+
sessions = new Map;
|
|
240
|
+
constructor(host, blobs) {
|
|
241
|
+
this.host = host;
|
|
242
|
+
this.blobs = blobs;
|
|
243
|
+
}
|
|
244
|
+
dispose() {
|
|
245
|
+
this.sessions.clear();
|
|
246
|
+
}
|
|
247
|
+
store(data) {
|
|
248
|
+
return this.blobs.store(data);
|
|
249
|
+
}
|
|
250
|
+
load(hash2) {
|
|
251
|
+
return this.blobs.load(hash2);
|
|
252
|
+
}
|
|
253
|
+
attach(piSessionId, session) {
|
|
254
|
+
this.sessions.set(piSessionId, session);
|
|
255
|
+
return () => {
|
|
256
|
+
if (this.sessions.get(piSessionId) === session) {
|
|
257
|
+
this.sessions.delete(piSessionId);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
async read(hash2) {
|
|
262
|
+
if (!BlobStore.isHash(hash2)) {
|
|
263
|
+
throw new Error(`${String(hash2)} is not the hash of kept contents.`);
|
|
264
|
+
}
|
|
265
|
+
const data = await this.blobs.load(hash2);
|
|
266
|
+
return data ? new TextDecoder().decode(data) : null;
|
|
267
|
+
}
|
|
268
|
+
async latest(sessionId) {
|
|
269
|
+
return (await this.session(sessionId))?.latest() ?? null;
|
|
270
|
+
}
|
|
271
|
+
async revert(sessionId, id, force = false) {
|
|
272
|
+
if (typeof id !== "string") {
|
|
273
|
+
throw new Error("revert takes the id of a session and the id of a change set.");
|
|
274
|
+
}
|
|
275
|
+
const session = await this.session(sessionId);
|
|
276
|
+
if (!session) {
|
|
277
|
+
return { reverted: [], conflicts: [], skipped: [], refused: "closed" };
|
|
278
|
+
}
|
|
279
|
+
const result = await session.revert(id, force === true);
|
|
280
|
+
if (!result.refused && (force || !result.conflicts.length)) {
|
|
281
|
+
await this.markReverted(id);
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
async session(sessionId) {
|
|
286
|
+
if (typeof sessionId !== "string") {
|
|
287
|
+
throw new Error(`${String(sessionId)} is not the id of a session.`);
|
|
288
|
+
}
|
|
289
|
+
const info = (await this.host.listSessions()).find((candidate) => candidate.sessionId === sessionId);
|
|
290
|
+
return info && this.sessions.get(info.piSessionId);
|
|
291
|
+
}
|
|
292
|
+
async markReverted(id) {
|
|
293
|
+
const value = await this.host.getExtensionData(NAME, REVERTED_KEY);
|
|
294
|
+
const ids = Array.isArray(value) ? value.filter((item) => typeof item === "string" && item !== id) : [];
|
|
295
|
+
await this.host.setExtensionData(NAME, REVERTED_KEY, [...ids, id].slice(-REVERTED_KEPT));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// web-src/host.ts
|
|
300
|
+
function activate(host) {
|
|
301
|
+
const blobs = new BlobStore(join2(host.agentDir, "changes"));
|
|
302
|
+
blobs.prune().catch(() => {
|
|
303
|
+
return;
|
|
304
|
+
});
|
|
305
|
+
return new DefaultChangesService(host, blobs);
|
|
306
|
+
}
|
|
307
|
+
export {
|
|
308
|
+
activate as default
|
|
309
|
+
};
|