@celljs/ta-changes 4.8.0 → 4.9.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/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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-changes",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.9.0",
|
|
4
4
|
"description": "A pi package for Ta: the files each reply changed, with their diffs, and a button that takes the changes of a reply back",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"web",
|
|
19
19
|
"host",
|
|
20
20
|
"extension",
|
|
21
|
-
"
|
|
21
|
+
"!**/*.map"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "tsc -b && bun build web-src/index.tsx --format esm --target browser --outfile web/index.js && bun build web-src/host.ts --format esm --target node --outfile host/index.mjs && bun build web-src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
@@ -27,14 +27,23 @@
|
|
|
27
27
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@celljs/ta-protocol": "4.
|
|
31
|
-
"@celljs/ta-web": "4.
|
|
30
|
+
"@celljs/ta-protocol": "4.9.0",
|
|
31
|
+
"@celljs/ta-web": "4.9.0",
|
|
32
32
|
"@types/react": "^19.3.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"pi": {
|
|
38
|
+
"title": {
|
|
39
|
+
"en": "Changed files",
|
|
40
|
+
"zh-CN": "文件改动"
|
|
41
|
+
},
|
|
42
|
+
"summary": {
|
|
43
|
+
"en": "After each reply, the files it changed, with their diffs and a button to undo them.",
|
|
44
|
+
"zh-CN": "每轮回复后显示改了哪些文件,可以查看差异、一键撤销。"
|
|
45
|
+
},
|
|
46
|
+
"icon": "FileDiff",
|
|
38
47
|
"web": [
|
|
39
48
|
"./web/index.js"
|
|
40
49
|
],
|
package/web/index.js
CHANGED
|
@@ -237,9 +237,10 @@ function DiffTab({ set, file }) {
|
|
|
237
237
|
className: "tcg-del"
|
|
238
238
|
}, "−", file.removed))), /* @__PURE__ */ React.createElement("div", {
|
|
239
239
|
className: "tcg-diff-body"
|
|
240
|
-
}, empty !== undefined || !texts || "error" in texts ? /* @__PURE__ */ React.createElement(
|
|
241
|
-
|
|
242
|
-
|
|
240
|
+
}, empty !== undefined || !texts || "error" in texts ? /* @__PURE__ */ React.createElement(pi.ui.Empty, {
|
|
241
|
+
title: empty,
|
|
242
|
+
tone: texts && "error" in texts ? "error" : undefined
|
|
243
|
+
}) : /* @__PURE__ */ React.createElement(pi.ui.DiffEditor, {
|
|
243
244
|
path: file.path,
|
|
244
245
|
original: texts.before,
|
|
245
246
|
modified: texts.after
|
|
@@ -403,34 +404,36 @@ function RevertNote({ message: message2 }) {
|
|
|
403
404
|
// web-src/style.ts
|
|
404
405
|
var STYLE = `
|
|
405
406
|
.tcg { position: relative; display: flex; justify-content: center; }
|
|
406
|
-
.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); }
|
|
407
|
+
.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: calc(13px * var(--ta-font-scale)); cursor: pointer; transition: background 120ms, border-color 120ms, transform 120ms var(--ta-ease); }
|
|
407
408
|
.tcg-pill:hover { background: var(--ta-hover); border-color: var(--ta-border); }
|
|
408
409
|
.tcg-pill:active { transform: scale(.98); }
|
|
409
410
|
.tcg-pill[aria-expanded='true'] { border-color: var(--ta-border); }
|
|
410
|
-
.tcg-stat { display: inline-flex; gap: 6px; font-variant-numeric: tabular-nums; font-size: 12.5px; }
|
|
411
|
+
.tcg-stat { display: inline-flex; gap: 6px; font-variant-numeric: tabular-nums; font-size: calc(12.5px * var(--ta-font-scale)); }
|
|
411
412
|
.tcg-add { color: var(--ta-green); }
|
|
412
413
|
.tcg-del { color: var(--ta-red); }
|
|
413
414
|
.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); }
|
|
414
415
|
@keyframes tcg-in { from { opacity: 0; transform: translate(-50%, 4px); } }
|
|
415
416
|
.tcg-files { max-height: 280px; overflow: auto; padding: 4px 0; }
|
|
416
|
-
.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; }
|
|
417
|
+
.tcg-file { display: flex; align-items: baseline; gap: 8px; width: 100%; padding: 8px 16px; border: 0; background: none; color: inherit; font: inherit; font-size: calc(13.5px * var(--ta-font-scale)); text-align: left; cursor: pointer; }
|
|
417
418
|
.tcg-file:hover { background: var(--ta-hover); }
|
|
418
419
|
.tcg-file:disabled { cursor: default; }
|
|
419
420
|
.tcg-file:disabled:hover { background: none; }
|
|
420
421
|
.tcg-name { flex: none; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
421
|
-
.tcg-dir { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
|
|
422
|
-
.tcg-tag { flex: none; padding: 0 6px; border-radius: 6px; background: var(--ta-chip); color: var(--ta-muted); font-size: 11px; line-height: 17px; }
|
|
422
|
+
.tcg-dir { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: calc(12.5px * var(--ta-font-scale)); text-overflow: ellipsis; white-space: nowrap; }
|
|
423
|
+
.tcg-tag { flex: none; padding: 0 6px; border-radius: 6px; background: var(--ta-chip); color: var(--ta-muted); font-size: calc(11px * var(--ta-font-scale)); line-height: calc(17px * var(--ta-font-scale)); }
|
|
423
424
|
.tcg-file .tcg-stat { flex: none; margin-left: auto; }
|
|
424
425
|
.tcg-foot { border-top: 1px solid var(--ta-border-muted); padding: 4px; }
|
|
425
|
-
.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; }
|
|
426
|
+
.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: calc(13.5px * var(--ta-font-scale)); text-align: left; cursor: pointer; }
|
|
426
427
|
.tcg-undo:hover { background: var(--ta-hover); color: var(--ta-text); }
|
|
427
428
|
.tcg-undo:disabled { color: var(--ta-muted); cursor: default; background: none; }
|
|
428
|
-
.tcg-note { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 2px 0; color: var(--ta-muted); font-size: 12.5px; }
|
|
429
|
+
.tcg-note { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 2px 0; color: var(--ta-muted); font-size: calc(12.5px * var(--ta-font-scale)); }
|
|
429
430
|
.tcg-diff { display: flex; flex-direction: column; height: 100%; min-height: 0; }
|
|
430
|
-
.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; }
|
|
431
|
+
.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: calc(12px * var(--ta-font-scale)); }
|
|
431
432
|
.tcg-diff-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
432
|
-
.tcg-diff-body { flex: 1; min-height: 0; }
|
|
433
|
-
|
|
433
|
+
.tcg-diff-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
|
434
|
+
/* A finger (the page's data-touch): the pill takes touches over a finger's height */
|
|
435
|
+
:root[data-touch] .tcg-pill { position: relative; }
|
|
436
|
+
:root[data-touch] .tcg-pill::after { content: ''; position: absolute; inset: -6px 0; }
|
|
434
437
|
`;
|
|
435
438
|
function injectStyle() {
|
|
436
439
|
const element = document.createElement("style");
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { afterAll, describe, expect, it } from 'bun:test';
|
|
2
|
-
import { mkdtempSync, readdirSync, rmSync, utimesSync } from 'node:fs';
|
|
3
|
-
import { tmpdir } from 'node:os';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { BlobStore } from './blob-store';
|
|
6
|
-
|
|
7
|
-
describe('BlobStore', () => {
|
|
8
|
-
const dir = mkdtempSync(join(tmpdir(), 'ta-changes-blobs-'));
|
|
9
|
-
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
10
|
-
|
|
11
|
-
it('keeps the same contents once, by their hash', async () => {
|
|
12
|
-
const store = new BlobStore(dir);
|
|
13
|
-
const data = new TextEncoder().encode('hello');
|
|
14
|
-
const name = await store.store(data);
|
|
15
|
-
expect(name).toBe('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824');
|
|
16
|
-
expect(await store.store(data)).toBe(name);
|
|
17
|
-
expect(readdirSync(join(dir, '2c'))).toEqual([name]);
|
|
18
|
-
expect(new TextDecoder().decode(await store.load(name))).toBe('hello');
|
|
19
|
-
expect(await store.load('0'.repeat(64))).toBeUndefined();
|
|
20
|
-
expect(await store.load('../../etc/passwd')).toBeUndefined();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('removes what was not kept again for long', async () => {
|
|
24
|
-
const store = new BlobStore(dir);
|
|
25
|
-
const old = await store.store(new TextEncoder().encode('old'));
|
|
26
|
-
const fresh = await store.store(new TextEncoder().encode('fresh'));
|
|
27
|
-
const longAgo = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000);
|
|
28
|
-
utimesSync(join(dir, old.slice(0, 2), old), longAgo, longAgo);
|
|
29
|
-
expect(await store.prune()).toBe(1);
|
|
30
|
-
expect(await store.load(old)).toBeUndefined();
|
|
31
|
-
expect(await store.load(fresh)).toBeDefined();
|
|
32
|
-
});
|
|
33
|
-
});
|
package/web-src/blob-store.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { mkdir, readdir, readFile, rename, rm, stat, utimes, writeFile } from 'node:fs/promises';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { hash } from './recorder';
|
|
4
|
-
|
|
5
|
-
const HASH = /^[0-9a-f]{64}$/;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The contents of changed files, by their hash, on the machine of ta serve: the same contents are kept
|
|
9
|
-
* once, however many replies and sessions have them. What nobody kept again for {@link KEEP_DAYS} days goes.
|
|
10
|
-
*/
|
|
11
|
-
export class BlobStore {
|
|
12
|
-
static readonly KEEP_DAYS = 30;
|
|
13
|
-
|
|
14
|
-
constructor(protected readonly dir: string) {}
|
|
15
|
-
|
|
16
|
-
static isHash(value: unknown): value is string {
|
|
17
|
-
return typeof value === 'string' && HASH.test(value);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async store(data: Uint8Array): Promise<string> {
|
|
21
|
-
const name = hash(data);
|
|
22
|
-
const file = this.file(name);
|
|
23
|
-
const now = new Date();
|
|
24
|
-
try {
|
|
25
|
-
// Kept again: it stays for as long again.
|
|
26
|
-
await utimes(file, now, now);
|
|
27
|
-
} catch {
|
|
28
|
-
await mkdir(join(this.dir, name.slice(0, 2)), { recursive: true });
|
|
29
|
-
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
30
|
-
await writeFile(temporary, data);
|
|
31
|
-
await rename(temporary, file);
|
|
32
|
-
}
|
|
33
|
-
return name;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async load(name: string): Promise<Uint8Array | undefined> {
|
|
37
|
-
if (!BlobStore.isHash(name)) {
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
return await readFile(this.file(name));
|
|
42
|
-
} catch {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Removes what was last kept more than `days` ago. */
|
|
48
|
-
async prune(days = BlobStore.KEEP_DAYS, now = Date.now()): Promise<number> {
|
|
49
|
-
const before = now - days * 24 * 60 * 60 * 1000;
|
|
50
|
-
let removed = 0;
|
|
51
|
-
for (const group of await readdir(this.dir).catch(() => [])) {
|
|
52
|
-
const dir = join(this.dir, group);
|
|
53
|
-
for (const name of await readdir(dir).catch(() => [])) {
|
|
54
|
-
const file = join(dir, name);
|
|
55
|
-
const info = await stat(file).catch(() => undefined);
|
|
56
|
-
if (info && info.mtimeMs < before) {
|
|
57
|
-
await rm(file, { force: true });
|
|
58
|
-
removed++;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return removed;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
protected file(name: string): string {
|
|
66
|
-
return join(this.dir, name.slice(0, 2), name);
|
|
67
|
-
}
|
|
68
|
-
}
|
package/web-src/card.tsx
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import type { WebCustomMessage } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
-
import type { ChangeSet, RevertDetails, RevertResult } from './changes-protocol';
|
|
3
|
-
import { ChangesUtil } from './changes-util';
|
|
4
|
-
import { openDiff } from './diff-tab';
|
|
5
|
-
import { message as errorMessage, icon, Latest, pi, Reverted, t } from './page';
|
|
6
|
-
import { React } from './react';
|
|
7
|
-
|
|
8
|
-
function Stat({ added, removed }: { added: number; removed: number }) {
|
|
9
|
-
return (
|
|
10
|
-
<span className="tcg-stat">
|
|
11
|
-
<span className="tcg-add">+{added}</span>
|
|
12
|
-
<span className="tcg-del">−{removed}</span>
|
|
13
|
-
</span>
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function names(paths: string[], root: string): string {
|
|
18
|
-
return paths.map(path => ChangesUtil.relative(path, root)).join(', ');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Takes the changes back, asking first when files changed again since; says what came of it. */
|
|
22
|
-
async function undo(sessionId: string, set: ChangeSet): Promise<boolean> {
|
|
23
|
-
let result = await pi.callHost<RevertResult>('revert', sessionId, set.id, false);
|
|
24
|
-
if (result.conflicts.length && !result.refused) {
|
|
25
|
-
const go = await pi.ui.confirm(t('conflictTitle'), t('conflictMessage', { files: names(result.conflicts, set.root) }), {
|
|
26
|
-
confirmLabel: t('overwrite'),
|
|
27
|
-
danger: true,
|
|
28
|
-
});
|
|
29
|
-
if (!go) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
result = await pi.callHost<RevertResult>('revert', sessionId, set.id, true);
|
|
33
|
-
}
|
|
34
|
-
if (result.refused) {
|
|
35
|
-
pi.ui.notify(t(result.refused), 'warning');
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
if (result.skipped.length) {
|
|
39
|
-
pi.ui.notify(t('skipped', { files: names(result.skipped, set.root) }), 'warning');
|
|
40
|
-
} else if (!result.reverted.length) {
|
|
41
|
-
pi.ui.notify(t('nothing'));
|
|
42
|
-
}
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The last change set of the conversation shown: asked of the host when it is shown, then as Ta sends them. */
|
|
47
|
-
function useLatest(sessionId: string | undefined): ChangeSet | undefined {
|
|
48
|
-
const [latest, setLatest] = React.useState<{ sessionId: string; set?: ChangeSet }>();
|
|
49
|
-
React.useEffect(() => {
|
|
50
|
-
if (!sessionId) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
let live = true;
|
|
54
|
-
const ask = () =>
|
|
55
|
-
pi.callHost<ChangeSet | null>('latest', sessionId).then(
|
|
56
|
-
set => live && setLatest({ sessionId, set: set ?? undefined }),
|
|
57
|
-
() => live && setLatest({ sessionId }),
|
|
58
|
-
);
|
|
59
|
-
// Events come from the conversation shown, which is this one while the effect lasts.
|
|
60
|
-
const stop = Latest.subscribe(value => (value === 'replaced' ? void ask() : setLatest({ sessionId, set: value })));
|
|
61
|
-
void ask();
|
|
62
|
-
return () => {
|
|
63
|
-
live = false;
|
|
64
|
-
stop();
|
|
65
|
-
};
|
|
66
|
-
}, [sessionId]);
|
|
67
|
-
return latest && latest.sessionId === sessionId ? latest.set : undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Above the message box, the files the last reply that changed any changed: a pill with how many and the
|
|
72
|
-
* lines added and removed; it opens a card above it with each file (a click shows its diff in the
|
|
73
|
-
* workbench) and a button that takes them back. Gone once they were taken back.
|
|
74
|
-
*/
|
|
75
|
-
export function ChangesBar() {
|
|
76
|
-
const sessionId = pi.ui.useSessionId();
|
|
77
|
-
const set = useLatest(sessionId);
|
|
78
|
-
const reverted = Reverted.use(set?.id ?? '');
|
|
79
|
-
const [open, setOpen] = React.useState(false);
|
|
80
|
-
const [busy, setBusy] = React.useState(false);
|
|
81
|
-
const root = React.useRef<HTMLDivElement>(null);
|
|
82
|
-
React.useEffect(() => setOpen(false), [set?.id]);
|
|
83
|
-
React.useEffect(() => {
|
|
84
|
-
if (!open) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const close = (event: Event) => {
|
|
88
|
-
if (event instanceof KeyboardEvent ? event.key === 'Escape' : !root.current?.contains(event.target as Node)) {
|
|
89
|
-
setOpen(false);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
document.addEventListener('pointerdown', close, true);
|
|
93
|
-
document.addEventListener('keydown', close);
|
|
94
|
-
return () => {
|
|
95
|
-
document.removeEventListener('pointerdown', close, true);
|
|
96
|
-
document.removeEventListener('keydown', close);
|
|
97
|
-
};
|
|
98
|
-
}, [open]);
|
|
99
|
-
if (!sessionId || !set?.files?.length || reverted) {
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
const totals = ChangesUtil.totals(set.files);
|
|
103
|
-
const Undo = icon('Undo2');
|
|
104
|
-
const run = async () => {
|
|
105
|
-
if (busy) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
setBusy(true);
|
|
109
|
-
try {
|
|
110
|
-
if (await undo(sessionId, set)) {
|
|
111
|
-
Reverted.add(set.id);
|
|
112
|
-
setOpen(false);
|
|
113
|
-
}
|
|
114
|
-
} catch (error) {
|
|
115
|
-
pi.ui.notify(t('failed', { message: errorMessage(error) }), 'error');
|
|
116
|
-
} finally {
|
|
117
|
-
setBusy(false);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
return (
|
|
121
|
-
<div className="tcg" ref={root}>
|
|
122
|
-
{open && (
|
|
123
|
-
<div className="tcg-card" role="dialog">
|
|
124
|
-
<div className="tcg-files">
|
|
125
|
-
{set.files.map(file => {
|
|
126
|
-
const { name, dir } = ChangesUtil.place(file.path, set.root);
|
|
127
|
-
return (
|
|
128
|
-
<button key={file.path} type="button" className="tcg-file" title={file.path} onClick={() => openDiff(set, file)}>
|
|
129
|
-
<span className="tcg-name">{name}</span>
|
|
130
|
-
{dir && <span className="tcg-dir">{dir}</span>}
|
|
131
|
-
{file.before === undefined && !file.large && <span className="tcg-tag">{t('created')}</span>}
|
|
132
|
-
<Stat added={file.added} removed={file.removed} />
|
|
133
|
-
</button>
|
|
134
|
-
);
|
|
135
|
-
})}
|
|
136
|
-
</div>
|
|
137
|
-
<div className="tcg-foot">
|
|
138
|
-
<button type="button" className="tcg-undo" disabled={busy} onClick={() => void run()}>
|
|
139
|
-
<Undo size={15} />
|
|
140
|
-
{busy ? t('undoing') : t('undo')}
|
|
141
|
-
</button>
|
|
142
|
-
</div>
|
|
143
|
-
</div>
|
|
144
|
-
)}
|
|
145
|
-
<button type="button" className="tcg-pill" aria-expanded={open} onClick={() => setOpen(!open)}>
|
|
146
|
-
<span>{set.files.length === 1 ? t('fileChanged') : t('filesChanged', { count: set.files.length })}</span>
|
|
147
|
-
<Stat added={totals.added} removed={totals.removed} />
|
|
148
|
-
</button>
|
|
149
|
-
</div>
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** After the user took the changes of a reply back: one quiet line. */
|
|
154
|
-
export function RevertNote({ message }: { message: WebCustomMessage }) {
|
|
155
|
-
const details = message.details as RevertDetails | undefined;
|
|
156
|
-
const count = details?.files?.length ?? 0;
|
|
157
|
-
const Undo = icon('Undo2');
|
|
158
|
-
return (
|
|
159
|
-
<div className="tcg-note">
|
|
160
|
-
<Undo size={13} />
|
|
161
|
-
{count === 1 ? t('revertNoteOne') : t('revertNote', { count })}
|
|
162
|
-
</div>
|
|
163
|
-
);
|
|
164
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/** The `name` of this package's package.json: how its pi extension asks for its host. */
|
|
2
|
-
export const NAME = '@celljs/ta-changes';
|
|
3
|
-
|
|
4
|
-
/** TA_HOST_EVENT of @celljs/ta-protocol, which the bundles of this package do not import. */
|
|
5
|
-
export const TA_HOST_EVENT = 'ta:host';
|
|
6
|
-
/** TA_FILE_WRITE_EVENT of @celljs/ta-protocol: another agent's tools are about to change a file. */
|
|
7
|
-
export const TA_FILE_WRITE_EVENT = 'ta:file-write';
|
|
8
|
-
|
|
9
|
-
/** The custom message that follows a reply that changed files: what it changed. Not shown in the conversation: the latest is above the message box. */
|
|
10
|
-
export const CHANGES_TYPE = 'ta-changes';
|
|
11
|
-
/** The custom message that tells Ta the user took the changes of a reply back. */
|
|
12
|
-
export const REVERT_TYPE = 'ta-changes-revert';
|
|
13
|
-
|
|
14
|
-
/** The key of the package's data that lists the change sets taken back. */
|
|
15
|
-
export const REVERTED_KEY = 'reverted';
|
|
16
|
-
/** How many of them the list keeps, the latest. */
|
|
17
|
-
export const REVERTED_KEPT = 1000;
|
|
18
|
-
|
|
19
|
-
/** Files larger than this are counted, but their contents are not kept: no diff, no taking back. */
|
|
20
|
-
export const MAX_KEPT_BYTES = 4 * 1024 * 1024;
|
|
21
|
-
|
|
22
|
-
/** A file a reply changed. */
|
|
23
|
-
export interface ChangedFile {
|
|
24
|
-
/** Where the tools wrote it, in the session's execution environment. */
|
|
25
|
-
path: string;
|
|
26
|
-
added: number;
|
|
27
|
-
removed: number;
|
|
28
|
-
/** The hash of its contents before the reply; none: the reply created it. */
|
|
29
|
-
before?: string;
|
|
30
|
-
/** The hash of its contents after the reply. */
|
|
31
|
-
after?: string;
|
|
32
|
-
/** Not text: no lines are counted, no diff is shown. */
|
|
33
|
-
binary?: boolean;
|
|
34
|
-
/** Too large to keep ({@link MAX_KEPT_BYTES}): it cannot be taken back. */
|
|
35
|
-
large?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** The `details` of a {@link CHANGES_TYPE} message. */
|
|
39
|
-
export interface ChangeSet {
|
|
40
|
-
id: string;
|
|
41
|
-
/** The directory of the session, in its execution environment: paths show relative to it. */
|
|
42
|
-
root: string;
|
|
43
|
-
files: ChangedFile[];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The `details` of a {@link REVERT_TYPE} message. */
|
|
47
|
-
export interface RevertDetails {
|
|
48
|
-
id: string;
|
|
49
|
-
files: string[];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** What taking a change set back did. */
|
|
53
|
-
export interface RevertResult {
|
|
54
|
-
/** Files written back, or removed when the reply created them. */
|
|
55
|
-
reverted: string[];
|
|
56
|
-
/** Files changed again since the reply: nothing was written while there are some, unless forced. */
|
|
57
|
-
conflicts: string[];
|
|
58
|
-
/** Files that cannot be taken back: too large to have been kept, or their contents are gone. */
|
|
59
|
-
skipped: string[];
|
|
60
|
-
/** Nothing was done: Ta still works in the conversation, it is not open in ta serve, or the reply is not in it. */
|
|
61
|
-
refused?: 'busy' | 'closed' | 'missing';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** What the pi extension of a session gives the host, to take back the changes of its replies. */
|
|
65
|
-
export interface ChangesSession {
|
|
66
|
-
revert(id: string, force: boolean): Promise<RevertResult>;
|
|
67
|
-
/** The last change set on the branch of the conversation. */
|
|
68
|
-
latest(): ChangeSet | undefined;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** The host of the package (`pi.host`): it keeps the contents of changed files, and finds the session of a change set. */
|
|
72
|
-
export interface ChangesService {
|
|
73
|
-
/** Keeps the bytes of a file; answers their hash. */
|
|
74
|
-
store(data: Uint8Array): Promise<string>;
|
|
75
|
-
/** The bytes kept under a hash; undefined when they are gone. */
|
|
76
|
-
load(hash: string): Promise<Uint8Array | undefined>;
|
|
77
|
-
/** A session of `ta serve` (its pi session id) that can take its change sets back, until it ends. */
|
|
78
|
-
attach(piSessionId: string, session: ChangesSession): () => void;
|
|
79
|
-
/** For the page: the text of a file kept under a hash; null when it is gone. */
|
|
80
|
-
read(hash: string): Promise<string | null>;
|
|
81
|
-
/** For the page: the last change set of the session the page shows (TaSessionInfo.sessionId); null when there is none, or it is not open. */
|
|
82
|
-
latest(sessionId: string): Promise<ChangeSet | null>;
|
|
83
|
-
/** For the page: takes back a change set of the session the page shows (TaSessionInfo.sessionId). */
|
|
84
|
-
revert(sessionId: string, id: string, force?: boolean): Promise<RevertResult>;
|
|
85
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { mkdtempSync } from 'node:fs';
|
|
3
|
-
import { tmpdir } from 'node:os';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import type { TaSessionInfo } from '@celljs/ta-protocol';
|
|
6
|
-
import { BlobStore } from './blob-store';
|
|
7
|
-
import { NAME, REVERTED_KEY, type RevertResult } from './changes-protocol';
|
|
8
|
-
import { DefaultChangesService } from './changes-service';
|
|
9
|
-
|
|
10
|
-
function service(result: RevertResult = { reverted: ['/a'], conflicts: [], skipped: [] }) {
|
|
11
|
-
const data = new Map<string, unknown>();
|
|
12
|
-
const host = {
|
|
13
|
-
listSessions: async () => [{ sessionId: 'ta-1', piSessionId: 'pi-1' } as TaSessionInfo],
|
|
14
|
-
getExtensionData: async (name: string, key: string) => data.get(`${name}:${key}`),
|
|
15
|
-
setExtensionData: async (name: string, key: string, value: unknown) => {
|
|
16
|
-
data.set(`${name}:${key}`, value);
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
const calls: [string, boolean][] = [];
|
|
20
|
-
const changes = new DefaultChangesService(host, new BlobStore(mkdtempSync(join(tmpdir(), 'ta-changes-service-'))));
|
|
21
|
-
const detach = changes.attach('pi-1', {
|
|
22
|
-
revert: async (id, force) => {
|
|
23
|
-
calls.push([id, force]);
|
|
24
|
-
return result;
|
|
25
|
-
},
|
|
26
|
-
latest: () => ({ id: 'set-2', root: '/', files: [] }),
|
|
27
|
-
});
|
|
28
|
-
return { changes, calls, detach, reverted: () => data.get(`${NAME}:${REVERTED_KEY}`) };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
describe('DefaultChangesService', () => {
|
|
32
|
-
it('takes back the changes of the session the page shows, and remembers it', async () => {
|
|
33
|
-
const { changes, calls, reverted } = service();
|
|
34
|
-
expect((await changes.revert('ta-1', 'set-1')).reverted).toEqual(['/a']);
|
|
35
|
-
expect(calls).toEqual([['set-1', false]]);
|
|
36
|
-
expect(reverted()).toEqual(['set-1']);
|
|
37
|
-
await changes.revert('ta-1', 'set-1');
|
|
38
|
-
expect(reverted()).toEqual(['set-1']);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('remembers nothing while files conflict, and refuses for a session it does not have', async () => {
|
|
42
|
-
const { changes, reverted, detach } = service({ reverted: [], conflicts: ['/a'], skipped: [] });
|
|
43
|
-
expect((await changes.revert('ta-1', 'set-1')).conflicts).toEqual(['/a']);
|
|
44
|
-
expect(reverted()).toBeUndefined();
|
|
45
|
-
await changes.revert('ta-1', 'set-1', true);
|
|
46
|
-
expect(reverted()).toEqual(['set-1']);
|
|
47
|
-
expect((await changes.revert('ta-2', 'set-1')).refused).toBe('closed');
|
|
48
|
-
expect((await changes.latest('ta-1'))?.id).toBe('set-2');
|
|
49
|
-
expect(await changes.latest('ta-2')).toBeNull();
|
|
50
|
-
detach();
|
|
51
|
-
expect((await changes.revert('ta-1', 'set-1')).refused).toBe('closed');
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('reads kept contents for the page by their hash only', async () => {
|
|
55
|
-
const { changes } = service();
|
|
56
|
-
const name = await changes.store(new TextEncoder().encode('text'));
|
|
57
|
-
expect(await changes.read(name)).toBe('text');
|
|
58
|
-
expect(await changes.read('f'.repeat(64))).toBeNull();
|
|
59
|
-
await expect(changes.read('../x')).rejects.toThrow('is not the hash');
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { TaExtensionHost } from '@celljs/ta-protocol';
|
|
2
|
-
import { BlobStore } from './blob-store';
|
|
3
|
-
import { type ChangeSet, type ChangesService, type ChangesSession, NAME, REVERTED_KEPT, REVERTED_KEY, type RevertResult } from './changes-protocol';
|
|
4
|
-
|
|
5
|
-
/** The host of the package under ta serve: see {@link ChangesService}. */
|
|
6
|
-
export class DefaultChangesService implements ChangesService {
|
|
7
|
-
protected readonly sessions = new Map<string, ChangesSession>();
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
protected readonly host: Pick<TaExtensionHost, 'listSessions' | 'getExtensionData' | 'setExtensionData'>,
|
|
11
|
-
protected readonly blobs: BlobStore,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
dispose(): void {
|
|
15
|
-
this.sessions.clear();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
store(data: Uint8Array): Promise<string> {
|
|
19
|
-
return this.blobs.store(data);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
load(hash: string): Promise<Uint8Array | undefined> {
|
|
23
|
-
return this.blobs.load(hash);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
attach(piSessionId: string, session: ChangesSession): () => void {
|
|
27
|
-
this.sessions.set(piSessionId, session);
|
|
28
|
-
return () => {
|
|
29
|
-
if (this.sessions.get(piSessionId) === session) {
|
|
30
|
-
this.sessions.delete(piSessionId);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async read(hash: string): Promise<string | null> {
|
|
36
|
-
if (!BlobStore.isHash(hash)) {
|
|
37
|
-
throw new Error(`${String(hash)} is not the hash of kept contents.`);
|
|
38
|
-
}
|
|
39
|
-
const data = await this.blobs.load(hash);
|
|
40
|
-
return data ? new TextDecoder().decode(data) : null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async latest(sessionId: string): Promise<ChangeSet | null> {
|
|
44
|
-
return (await this.session(sessionId))?.latest() ?? null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async revert(sessionId: string, id: string, force = false): Promise<RevertResult> {
|
|
48
|
-
if (typeof id !== 'string') {
|
|
49
|
-
throw new Error('revert takes the id of a session and the id of a change set.');
|
|
50
|
-
}
|
|
51
|
-
const session = await this.session(sessionId);
|
|
52
|
-
if (!session) {
|
|
53
|
-
return { reverted: [], conflicts: [], skipped: [], refused: 'closed' };
|
|
54
|
-
}
|
|
55
|
-
const result = await session.revert(id, force === true);
|
|
56
|
-
if (!result.refused && (force || !result.conflicts.length)) {
|
|
57
|
-
await this.markReverted(id);
|
|
58
|
-
}
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
protected async session(sessionId: string): Promise<ChangesSession | undefined> {
|
|
63
|
-
if (typeof sessionId !== 'string') {
|
|
64
|
-
throw new Error(`${String(sessionId)} is not the id of a session.`);
|
|
65
|
-
}
|
|
66
|
-
const info = (await this.host.listSessions()).find(candidate => candidate.sessionId === sessionId);
|
|
67
|
-
return info && this.sessions.get(info.piSessionId);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
protected async markReverted(id: string): Promise<void> {
|
|
71
|
-
const value = await this.host.getExtensionData(NAME, REVERTED_KEY);
|
|
72
|
-
const ids = Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string' && item !== id) : [];
|
|
73
|
-
await this.host.setExtensionData(NAME, REVERTED_KEY, [...ids, id].slice(-REVERTED_KEPT));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { ChangesUtil } from './changes-util';
|
|
3
|
-
|
|
4
|
-
describe('ChangesUtil', () => {
|
|
5
|
-
it('places a file by its name and its directory, relative to the session', () => {
|
|
6
|
-
expect(ChangesUtil.place('/work/app/src/a.ts', '/work/app')).toEqual({ name: 'a.ts', dir: 'src' });
|
|
7
|
-
expect(ChangesUtil.place('/work/app/a.ts', '/work/app/')).toEqual({ name: 'a.ts', dir: '' });
|
|
8
|
-
expect(ChangesUtil.place('/tmp/report.html', '/work/app')).toEqual({ name: 'report.html', dir: '/tmp' });
|
|
9
|
-
expect(ChangesUtil.place('/a.ts', '/work')).toEqual({ name: 'a.ts', dir: '/' });
|
|
10
|
-
expect(ChangesUtil.place('/work/apple/a.ts', '/work/app')).toEqual({ name: 'a.ts', dir: '/work/apple' });
|
|
11
|
-
expect(ChangesUtil.place('C:\\work\\src\\a.ts', 'C:\\work')).toEqual({ name: 'a.ts', dir: 'src' });
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('tells text from binary as git does', () => {
|
|
15
|
-
expect(ChangesUtil.isText(new TextEncoder().encode('hello'))).toBe(true);
|
|
16
|
-
expect(ChangesUtil.isText(undefined)).toBe(true);
|
|
17
|
-
expect(ChangesUtil.isText(new Uint8Array([137, 80, 0, 1]))).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('sums the lines and says what changed in words', () => {
|
|
21
|
-
const set = {
|
|
22
|
-
id: 'x',
|
|
23
|
-
root: '/work',
|
|
24
|
-
files: [
|
|
25
|
-
{ path: '/work/a.ts', added: 3, removed: 1, before: 'h1', after: 'h2' },
|
|
26
|
-
{ path: '/work/b.ts', added: 2, removed: 0, after: 'h3' },
|
|
27
|
-
],
|
|
28
|
-
};
|
|
29
|
-
expect(ChangesUtil.totals(set.files)).toEqual({ added: 5, removed: 1 });
|
|
30
|
-
expect(ChangesUtil.summary(set)).toBe('Changed 2 files: a.ts (+3 -1), b.ts (new, +2 -0)');
|
|
31
|
-
expect(ChangesUtil.revertNote(['/work/a.ts'], '/work')).toContain(': a.ts. Read them again');
|
|
32
|
-
});
|
|
33
|
-
});
|