@celljs/ta-changes 4.8.0 → 4.9.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/package.json +13 -4
- package/web/index.js +16 -13
- package/web-src/blob-store.spec.ts +0 -33
- package/web-src/blob-store.ts +0 -68
- package/web-src/card.tsx +0 -164
- package/web-src/changes-protocol.ts +0 -85
- package/web-src/changes-service.spec.ts +0 -61
- package/web-src/changes-service.ts +0 -75
- package/web-src/changes-util.spec.ts +0 -33
- package/web-src/changes-util.ts +0 -44
- package/web-src/diff-tab.tsx +0 -60
- package/web-src/extension.ts +0 -143
- package/web-src/host.ts +0 -14
- package/web-src/i18n.ts +0 -59
- package/web-src/index.tsx +0 -26
- package/web-src/line-diff.spec.ts +0 -25
- package/web-src/line-diff.ts +0 -65
- package/web-src/page.ts +0 -107
- package/web-src/react.ts +0 -11
- package/web-src/recorder.spec.ts +0 -124
- package/web-src/recorder.ts +0 -114
- package/web-src/style.ts +0 -40
package/web-src/recorder.spec.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import type { TaSessionFiles } from '@celljs/ta-protocol';
|
|
3
|
-
import { ChangeRecorder, hash, revert } from './recorder';
|
|
4
|
-
|
|
5
|
-
const encode = (text: string) => new TextEncoder().encode(text);
|
|
6
|
-
const decode = (data: Uint8Array | undefined) => (data ? new TextDecoder().decode(data) : undefined);
|
|
7
|
-
|
|
8
|
-
/** Files in memory, and blobs kept by hash. */
|
|
9
|
-
function fixture(initial: Record<string, string> = {}) {
|
|
10
|
-
const disk = new Map<string, Uint8Array>(Object.entries(initial).map(([path, text]) => [path, encode(text)]));
|
|
11
|
-
const blobs = new Map<string, Uint8Array>();
|
|
12
|
-
const files: TaSessionFiles = {
|
|
13
|
-
resolve: path => (path.startsWith('/') ? path : `/work/${path}`),
|
|
14
|
-
read: async path => disk.get(path),
|
|
15
|
-
write: async (path, data) => {
|
|
16
|
-
disk.set(path, data);
|
|
17
|
-
},
|
|
18
|
-
remove: async path => {
|
|
19
|
-
disk.delete(path);
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
const service = {
|
|
23
|
-
store: async (data: Uint8Array) => {
|
|
24
|
-
const name = hash(data);
|
|
25
|
-
blobs.set(name, data);
|
|
26
|
-
return name;
|
|
27
|
-
},
|
|
28
|
-
load: async (name: string) => blobs.get(name),
|
|
29
|
-
};
|
|
30
|
-
return { disk, blobs, files, service, text: (path: string) => decode(disk.get(path)) };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
describe('ChangeRecorder', () => {
|
|
34
|
-
it('keeps each file as it was before its first write in a reply, and lists what changed when the reply ends', async () => {
|
|
35
|
-
const { disk, files, service, blobs } = fixture({ '/work/a.ts': 'one\ntwo\n', '/work/same.ts': 'same\n' });
|
|
36
|
-
const recorder = new ChangeRecorder(files, service);
|
|
37
|
-
await recorder.before('/work/a.ts');
|
|
38
|
-
disk.set('/work/a.ts', encode('one\n2\n'));
|
|
39
|
-
// A second write in the same reply does not move what it was before.
|
|
40
|
-
await recorder.before('/work/a.ts');
|
|
41
|
-
disk.set('/work/a.ts', encode('one\n2\nthree\n'));
|
|
42
|
-
await recorder.before('/work/new.ts');
|
|
43
|
-
disk.set('/work/new.ts', encode('hello\n'));
|
|
44
|
-
// Written with what was there: no change.
|
|
45
|
-
await recorder.before('/work/same.ts');
|
|
46
|
-
const set = await recorder.finish('/work');
|
|
47
|
-
expect(set?.root).toBe('/work');
|
|
48
|
-
expect(set?.files).toEqual([
|
|
49
|
-
{ path: '/work/a.ts', added: 2, removed: 1, before: hash(encode('one\ntwo\n')), after: hash(encode('one\n2\nthree\n')) },
|
|
50
|
-
{ path: '/work/new.ts', added: 1, removed: 0, after: hash(encode('hello\n')) },
|
|
51
|
-
]);
|
|
52
|
-
expect(blobs.size).toBe(3);
|
|
53
|
-
// The next reply starts afresh.
|
|
54
|
-
expect(await recorder.finish('/work')).toBeUndefined();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('takes what a file held from whoever writes it, when they say', async () => {
|
|
58
|
-
const { disk, files, service } = fixture({ '/work/a.ts': 'already written\n' });
|
|
59
|
-
const recorder = new ChangeRecorder(files, service);
|
|
60
|
-
await recorder.before('/work/a.ts', 'before\n');
|
|
61
|
-
await recorder.before('/work/new.ts', null);
|
|
62
|
-
disk.set('/work/new.ts', encode('new\n'));
|
|
63
|
-
// Only the first word of a reply counts.
|
|
64
|
-
await recorder.before('/work/a.ts', 'later\n');
|
|
65
|
-
const set = await recorder.finish('/work');
|
|
66
|
-
expect(set?.files.map(file => [file.path, file.added, file.removed, file.before === hash(encode('before\n')), file.before])).toEqual([
|
|
67
|
-
['/work/a.ts', 1, 1, true, hash(encode('before\n'))],
|
|
68
|
-
['/work/new.ts', 1, 0, false, undefined],
|
|
69
|
-
]);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('counts no lines of binary files and keeps no contents of large ones', async () => {
|
|
73
|
-
const { disk, files, service, blobs } = fixture({ '/work/logo.png': '\u0000png' });
|
|
74
|
-
const recorder = new ChangeRecorder(files, service);
|
|
75
|
-
await recorder.before('/work/logo.png');
|
|
76
|
-
disk.set('/work/logo.png', new Uint8Array([0, 1, 2]));
|
|
77
|
-
await recorder.before('/work/big.txt');
|
|
78
|
-
disk.set('/work/big.txt', new Uint8Array(5 * 1024 * 1024).fill(97));
|
|
79
|
-
const set = await recorder.finish('/work');
|
|
80
|
-
expect(set?.files[0]).toMatchObject({ path: '/work/logo.png', added: 0, removed: 0, binary: true });
|
|
81
|
-
expect(set?.files[1]).toEqual({ path: '/work/big.txt', added: 1, removed: 0, large: true });
|
|
82
|
-
expect(blobs.size).toBe(2);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('revert', () => {
|
|
87
|
-
async function changed() {
|
|
88
|
-
const fx = fixture({ '/work/a.ts': 'before\n' });
|
|
89
|
-
const recorder = new ChangeRecorder(fx.files, fx.service);
|
|
90
|
-
await recorder.before('/work/a.ts');
|
|
91
|
-
fx.disk.set('/work/a.ts', encode('after\n'));
|
|
92
|
-
await recorder.before('/work/new.ts');
|
|
93
|
-
fx.disk.set('/work/new.ts', encode('new\n'));
|
|
94
|
-
const set = (await recorder.finish('/work'))!;
|
|
95
|
-
return { ...fx, set };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
it('writes the files back and removes those the reply created', async () => {
|
|
99
|
-
const { set, files, service, text, disk } = await changed();
|
|
100
|
-
expect(await revert(set, files, service, false)).toEqual({ reverted: ['/work/a.ts', '/work/new.ts'], conflicts: [], skipped: [] });
|
|
101
|
-
expect(text('/work/a.ts')).toBe('before\n');
|
|
102
|
-
expect(disk.has('/work/new.ts')).toBe(false);
|
|
103
|
-
// Taken back already: nothing to do.
|
|
104
|
-
expect(await revert(set, files, service, false)).toEqual({ reverted: [], conflicts: [], skipped: [] });
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('touches nothing when a file changed again since, unless forced', async () => {
|
|
108
|
-
const { set, files, service, text, disk } = await changed();
|
|
109
|
-
disk.set('/work/a.ts', encode('edited by hand\n'));
|
|
110
|
-
expect(await revert(set, files, service, false)).toEqual({ reverted: [], conflicts: ['/work/a.ts'], skipped: [] });
|
|
111
|
-
expect(text('/work/a.ts')).toBe('edited by hand\n');
|
|
112
|
-
expect(disk.has('/work/new.ts')).toBe(true);
|
|
113
|
-
expect((await revert(set, files, service, true)).reverted).toEqual(['/work/a.ts', '/work/new.ts']);
|
|
114
|
-
expect(text('/work/a.ts')).toBe('before\n');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('skips files whose contents are gone or were never kept', async () => {
|
|
118
|
-
const { set, files, service, blobs, text } = await changed();
|
|
119
|
-
blobs.delete(set.files[0].before!);
|
|
120
|
-
const large = { ...set, files: [...set.files, { path: '/work/big.txt', added: 1, removed: 0, large: true }] };
|
|
121
|
-
expect(await revert(large, files, service, false)).toEqual({ reverted: ['/work/new.ts'], conflicts: [], skipped: ['/work/big.txt', '/work/a.ts'] });
|
|
122
|
-
expect(text('/work/a.ts')).toBe('after\n');
|
|
123
|
-
});
|
|
124
|
-
});
|
package/web-src/recorder.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
-
import type { TaSessionFiles } from '@celljs/ta-protocol';
|
|
3
|
-
import { type ChangedFile, type ChangeSet, type ChangesService, MAX_KEPT_BYTES, type RevertResult } from './changes-protocol';
|
|
4
|
-
import { ChangesUtil } from './changes-util';
|
|
5
|
-
import { LineDiff } from './line-diff';
|
|
6
|
-
|
|
7
|
-
/** The SHA-256 of bytes, in hex: the name of what the host keeps. */
|
|
8
|
-
export function hash(data: Uint8Array): string {
|
|
9
|
-
return createHash('sha256').update(data).digest('hex');
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The files a reply changes: before a tool writes a file the first time in the reply, what is there;
|
|
14
|
-
* when the reply ends, what changed, its contents before and after kept by the host.
|
|
15
|
-
*/
|
|
16
|
-
export class ChangeRecorder {
|
|
17
|
-
protected befores = new Map<string, Promise<Uint8Array | undefined>>();
|
|
18
|
-
|
|
19
|
-
constructor(
|
|
20
|
-
protected readonly files: Pick<TaSessionFiles, 'read'>,
|
|
21
|
-
protected readonly service: Pick<ChangesService, 'store'>,
|
|
22
|
-
) {}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Before a tool writes `path` (resolved): its contents, the first time in this reply. `known`: what it
|
|
26
|
-
* held, when whoever writes said (a diff of another agent; null: no file), instead of reading it.
|
|
27
|
-
*/
|
|
28
|
-
async before(path: string, known?: string | null): Promise<void> {
|
|
29
|
-
if (!this.befores.has(path)) {
|
|
30
|
-
this.befores.set(path, known === undefined ? this.files.read(path) : Promise.resolve(known === null ? undefined : new TextEncoder().encode(known)));
|
|
31
|
-
}
|
|
32
|
-
await this.befores.get(path)?.catch(() => undefined);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** The reply ended: what it changed, if anything; the next one starts afresh. */
|
|
36
|
-
async finish(root: string): Promise<ChangeSet | undefined> {
|
|
37
|
-
const befores = this.befores;
|
|
38
|
-
this.befores = new Map();
|
|
39
|
-
const files: ChangedFile[] = [];
|
|
40
|
-
for (const [path, read] of befores) {
|
|
41
|
-
try {
|
|
42
|
-
const file = await this.compare(path, await read, await this.files.read(path));
|
|
43
|
-
if (file) {
|
|
44
|
-
files.push(file);
|
|
45
|
-
}
|
|
46
|
-
} catch {
|
|
47
|
-
// A file that cannot be read is left out: nothing about it would be right.
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return files.length ? { id: randomUUID(), root, files } : undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
protected async compare(path: string, before: Uint8Array | undefined, after: Uint8Array | undefined): Promise<ChangedFile | undefined> {
|
|
54
|
-
if (before === after || (before && after && Buffer.compare(before, after) === 0)) {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
const binary = !ChangesUtil.isText(before) || !ChangesUtil.isText(after);
|
|
58
|
-
const decoder = new TextDecoder();
|
|
59
|
-
const counts = binary ? { added: 0, removed: 0 } : LineDiff.count(before ? decoder.decode(before) : '', after ? decoder.decode(after) : '');
|
|
60
|
-
const file: ChangedFile = { path, ...counts };
|
|
61
|
-
if (binary) {
|
|
62
|
-
file.binary = true;
|
|
63
|
-
}
|
|
64
|
-
if ((before?.length ?? 0) > MAX_KEPT_BYTES || (after?.length ?? 0) > MAX_KEPT_BYTES) {
|
|
65
|
-
file.large = true;
|
|
66
|
-
return file;
|
|
67
|
-
}
|
|
68
|
-
if (before) {
|
|
69
|
-
file.before = await this.service.store(before);
|
|
70
|
-
}
|
|
71
|
-
if (after) {
|
|
72
|
-
file.after = await this.service.store(after);
|
|
73
|
-
}
|
|
74
|
-
return file;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Takes the changes of a reply back: each file as it was before it, unless it changed again since. */
|
|
79
|
-
export async function revert(set: ChangeSet, files: TaSessionFiles, service: Pick<ChangesService, 'load'>, force: boolean): Promise<RevertResult> {
|
|
80
|
-
const result: RevertResult = { reverted: [], conflicts: [], skipped: [] };
|
|
81
|
-
const pending: ChangedFile[] = [];
|
|
82
|
-
for (const file of set.files) {
|
|
83
|
-
if (file.large) {
|
|
84
|
-
result.skipped.push(file.path);
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
const current = await files.read(file.path);
|
|
88
|
-
const now = current && hash(current);
|
|
89
|
-
if (now === file.before) {
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (now !== file.after) {
|
|
93
|
-
result.conflicts.push(file.path);
|
|
94
|
-
}
|
|
95
|
-
pending.push(file);
|
|
96
|
-
}
|
|
97
|
-
if (result.conflicts.length && !force) {
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
for (const file of pending) {
|
|
101
|
-
if (file.before === undefined) {
|
|
102
|
-
await files.remove(file.path);
|
|
103
|
-
} else {
|
|
104
|
-
const data = await service.load(file.before);
|
|
105
|
-
if (!data) {
|
|
106
|
-
result.skipped.push(file.path);
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
await files.write(file.path, data);
|
|
110
|
-
}
|
|
111
|
-
result.reverted.push(file.path);
|
|
112
|
-
}
|
|
113
|
-
return result;
|
|
114
|
-
}
|
package/web-src/style.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/** The styles of the package; only the --ta-* variables of the page, so they follow its theme. */
|
|
2
|
-
export const STYLE = `
|
|
3
|
-
.tcg { position: relative; display: flex; justify-content: center; }
|
|
4
|
-
.tcg-pill { display: inline-flex; align-items: center; gap: 8px; height: 32px; padding: 0 14px; border: 1px solid var(--ta-border-muted); border-radius: 999px; background: var(--ta-raised); color: var(--ta-text); font: inherit; font-size: 13px; cursor: pointer; transition: background 120ms, border-color 120ms, transform 120ms var(--ta-ease); }
|
|
5
|
-
.tcg-pill:hover { background: var(--ta-hover); border-color: var(--ta-border); }
|
|
6
|
-
.tcg-pill:active { transform: scale(.98); }
|
|
7
|
-
.tcg-pill[aria-expanded='true'] { border-color: var(--ta-border); }
|
|
8
|
-
.tcg-stat { display: inline-flex; gap: 6px; font-variant-numeric: tabular-nums; font-size: 12.5px; }
|
|
9
|
-
.tcg-add { color: var(--ta-green); }
|
|
10
|
-
.tcg-del { color: var(--ta-red); }
|
|
11
|
-
.tcg-card { position: absolute; bottom: calc(100% + 8px); left: 50%; z-index: 20; width: min(100%, 560px); transform: translateX(-50%); border: 1px solid var(--ta-border-muted); border-radius: 14px; background: var(--ta-card); box-shadow: var(--ta-shadow-pop); overflow: hidden; animation: tcg-in 160ms var(--ta-ease); }
|
|
12
|
-
@keyframes tcg-in { from { opacity: 0; transform: translate(-50%, 4px); } }
|
|
13
|
-
.tcg-files { max-height: 280px; overflow: auto; padding: 4px 0; }
|
|
14
|
-
.tcg-file { display: flex; align-items: baseline; gap: 8px; width: 100%; padding: 8px 16px; border: 0; background: none; color: inherit; font: inherit; font-size: 13.5px; text-align: left; cursor: pointer; }
|
|
15
|
-
.tcg-file:hover { background: var(--ta-hover); }
|
|
16
|
-
.tcg-file:disabled { cursor: default; }
|
|
17
|
-
.tcg-file:disabled:hover { background: none; }
|
|
18
|
-
.tcg-name { flex: none; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
19
|
-
.tcg-dir { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
|
|
20
|
-
.tcg-tag { flex: none; padding: 0 6px; border-radius: 6px; background: var(--ta-chip); color: var(--ta-muted); font-size: 11px; line-height: 17px; }
|
|
21
|
-
.tcg-file .tcg-stat { flex: none; margin-left: auto; }
|
|
22
|
-
.tcg-foot { border-top: 1px solid var(--ta-border-muted); padding: 4px; }
|
|
23
|
-
.tcg-undo { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 12px; border: 0; border-radius: 10px; background: none; color: var(--ta-secondary); font: inherit; font-size: 13.5px; text-align: left; cursor: pointer; }
|
|
24
|
-
.tcg-undo:hover { background: var(--ta-hover); color: var(--ta-text); }
|
|
25
|
-
.tcg-undo:disabled { color: var(--ta-muted); cursor: default; background: none; }
|
|
26
|
-
.tcg-note { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 2px 0; color: var(--ta-muted); font-size: 12.5px; }
|
|
27
|
-
.tcg-diff { display: flex; flex-direction: column; height: 100%; min-height: 0; }
|
|
28
|
-
.tcg-diff-head { display: flex; align-items: center; gap: 10px; flex: none; height: 32px; padding: 0 12px; border-bottom: 1px solid var(--ta-border-muted); color: var(--ta-muted); font-size: 12px; }
|
|
29
|
-
.tcg-diff-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
30
|
-
.tcg-diff-body { flex: 1; min-height: 0; }
|
|
31
|
-
.tcg-diff-empty { padding: 24px; color: var(--ta-muted); font-size: 13px; text-align: center; }
|
|
32
|
-
`;
|
|
33
|
-
|
|
34
|
-
export function injectStyle(): () => void {
|
|
35
|
-
const element = document.createElement('style');
|
|
36
|
-
element.dataset.taPackage = 'ta-changes';
|
|
37
|
-
element.textContent = STYLE;
|
|
38
|
-
document.head.appendChild(element);
|
|
39
|
-
return () => element.remove();
|
|
40
|
-
}
|