tandem-kit-core 0.0.0 → 0.1.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/LICENSE +21 -0
- package/README.md +7 -1
- package/dist/src/browser.d.ts +19 -0
- package/dist/src/browser.js +19 -0
- package/dist/src/client-tools.d.ts +31 -0
- package/dist/src/client-tools.js +82 -0
- package/dist/src/client.d.ts +179 -0
- package/dist/src/client.js +213 -0
- package/dist/src/delta.d.ts +16 -0
- package/dist/src/delta.js +99 -0
- package/dist/src/durable.d.ts +39 -0
- package/dist/src/durable.js +104 -0
- package/dist/src/errors.d.ts +5 -0
- package/dist/src/errors.js +10 -0
- package/dist/src/eventlog-file.d.ts +12 -0
- package/dist/src/eventlog-file.js +39 -0
- package/dist/src/eventlog.d.ts +31 -0
- package/dist/src/eventlog.js +63 -0
- package/dist/src/events.d.ts +15 -0
- package/dist/src/events.js +79 -0
- package/dist/src/events.json +80 -0
- package/dist/src/files.d.ts +34 -0
- package/dist/src/files.js +122 -0
- package/dist/src/fsutil.d.ts +8 -0
- package/dist/src/fsutil.js +68 -0
- package/dist/src/hash.d.ts +3 -0
- package/dist/src/hash.js +65 -0
- package/dist/src/images.d.ts +24 -0
- package/dist/src/images.js +70 -0
- package/dist/src/index.d.ts +25 -0
- package/dist/src/index.js +25 -0
- package/dist/src/json.d.ts +10 -0
- package/dist/src/json.js +68 -0
- package/dist/src/merge.d.ts +34 -0
- package/dist/src/merge.js +111 -0
- package/dist/src/persist.d.ts +49 -0
- package/dist/src/persist.js +29 -0
- package/dist/src/proposals-file.d.ts +12 -0
- package/dist/src/proposals-file.js +36 -0
- package/dist/src/proposals.d.ts +149 -0
- package/dist/src/proposals.js +296 -0
- package/dist/src/reconcile.d.ts +63 -0
- package/dist/src/reconcile.js +61 -0
- package/dist/src/replica.d.ts +147 -0
- package/dist/src/replica.js +320 -0
- package/dist/src/revisions-file.d.ts +14 -0
- package/dist/src/revisions-file.js +28 -0
- package/dist/src/revisions.d.ts +78 -0
- package/dist/src/revisions.js +152 -0
- package/dist/src/revisions.json +19 -0
- package/dist/src/schema.d.ts +12 -0
- package/dist/src/schema.js +49 -0
- package/dist/src/session-state.d.ts +36 -0
- package/dist/src/session-state.js +95 -0
- package/dist/src/sse.d.ts +17 -0
- package/dist/src/sse.js +59 -0
- package/dist/src/workcopy.d.ts +145 -0
- package/dist/src/workcopy.js +556 -0
- package/package.json +34 -4
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { TandemError } from "./errors.js";
|
|
2
|
+
import { clip, equal, isObject } from "./json.js";
|
|
3
|
+
function child(container, key) {
|
|
4
|
+
if (Array.isArray(container))
|
|
5
|
+
return typeof key === 'number' ? container[key] : undefined;
|
|
6
|
+
if (isObject(container))
|
|
7
|
+
return container[String(key)];
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
function badPath(path, why) {
|
|
11
|
+
throw new TandemError(400, 'bad_path', `${why}: /${path.join('/')}`);
|
|
12
|
+
}
|
|
13
|
+
function setAt(node, path, depth, value) {
|
|
14
|
+
if (depth === path.length)
|
|
15
|
+
return value;
|
|
16
|
+
const key = path[depth];
|
|
17
|
+
if (Array.isArray(node)) {
|
|
18
|
+
if (typeof key !== 'number' || !Number.isInteger(key) || key < 0 || key > node.length)
|
|
19
|
+
badPath(path, 'no such array index');
|
|
20
|
+
const next = setAt(node[key], path, depth + 1, value);
|
|
21
|
+
const copy = node.slice();
|
|
22
|
+
if (next === undefined)
|
|
23
|
+
copy.splice(key, 1);
|
|
24
|
+
else
|
|
25
|
+
copy[key] = next;
|
|
26
|
+
return copy;
|
|
27
|
+
}
|
|
28
|
+
if (isObject(node)) {
|
|
29
|
+
const next = setAt(node[String(key)], path, depth + 1, value);
|
|
30
|
+
const copy = { ...node };
|
|
31
|
+
if (next === undefined)
|
|
32
|
+
delete copy[String(key)];
|
|
33
|
+
else
|
|
34
|
+
copy[String(key)] = next;
|
|
35
|
+
return copy;
|
|
36
|
+
}
|
|
37
|
+
return badPath(path, 'parent is not a container');
|
|
38
|
+
}
|
|
39
|
+
export function applyOp(state, op) {
|
|
40
|
+
if (!Array.isArray(op.path))
|
|
41
|
+
badPath([], 'op has no path');
|
|
42
|
+
if (op.op === 'set') {
|
|
43
|
+
if (op.value === undefined)
|
|
44
|
+
badPath(op.path, 'set has no value');
|
|
45
|
+
return setAt(state, op.path, 0, op.value);
|
|
46
|
+
}
|
|
47
|
+
if (op.op === 'delete') {
|
|
48
|
+
if (op.path.length === 0)
|
|
49
|
+
badPath(op.path, 'cannot delete the root');
|
|
50
|
+
if (child(resolve(state, op.path.slice(0, -1)), op.path[op.path.length - 1]) === undefined)
|
|
51
|
+
return state;
|
|
52
|
+
return setAt(state, op.path, 0, undefined);
|
|
53
|
+
}
|
|
54
|
+
throw new TandemError(400, 'bad_op', `unknown op: ${String(op.op)}`);
|
|
55
|
+
}
|
|
56
|
+
function resolve(state, path) {
|
|
57
|
+
let node = state;
|
|
58
|
+
for (const key of path) {
|
|
59
|
+
node = child(node, key);
|
|
60
|
+
if (node === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return node;
|
|
64
|
+
}
|
|
65
|
+
export function apply(state, delta) {
|
|
66
|
+
return delta.reduce((acc, op) => applyOp(acc, op), state);
|
|
67
|
+
}
|
|
68
|
+
function collect(before, after, prefix, ops) {
|
|
69
|
+
if (before === after)
|
|
70
|
+
return;
|
|
71
|
+
if (isObject(before) && isObject(after)) {
|
|
72
|
+
for (const key of Object.keys(before)) {
|
|
73
|
+
if (!(key in after))
|
|
74
|
+
ops.push({ op: 'delete', path: [...prefix, key] });
|
|
75
|
+
}
|
|
76
|
+
for (const key of Object.keys(after)) {
|
|
77
|
+
if (!(key in before))
|
|
78
|
+
ops.push({ op: 'set', path: [...prefix, key], value: after[key] });
|
|
79
|
+
else
|
|
80
|
+
collect(before[key], after[key], [...prefix, key], ops);
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (!equal(before, after))
|
|
85
|
+
ops.push({ op: 'set', path: prefix, value: after });
|
|
86
|
+
}
|
|
87
|
+
export function diff(before, after, prefix = []) {
|
|
88
|
+
const ops = [];
|
|
89
|
+
collect(before, after, prefix, ops);
|
|
90
|
+
return ops;
|
|
91
|
+
}
|
|
92
|
+
export function formatPath(path) {
|
|
93
|
+
return path.length ? path.map(String).join('.') : '(root)';
|
|
94
|
+
}
|
|
95
|
+
export function formatDelta(delta, maxChars = 2500) {
|
|
96
|
+
const lines = delta.map((op) => op.op === 'delete' ? `delete ${formatPath(op.path)}` : `set ${formatPath(op.path)} = ${clip(JSON.stringify(op.value), 400)}`);
|
|
97
|
+
const text = lines.join('\n');
|
|
98
|
+
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}\n[${text.length - maxChars} more characters]`;
|
|
99
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type FileOptions } from './fsutil.js';
|
|
2
|
+
import { type ClashState, type ClashStore, type DocSnapshot, type SnapshotStore } from './persist.js';
|
|
3
|
+
import type { Link, LinkStore } from './replica.js';
|
|
4
|
+
import { Workcopy, type WorkcopyOptions } from './workcopy.js';
|
|
5
|
+
export declare const LAYOUT: {
|
|
6
|
+
readonly doc: "doc.json";
|
|
7
|
+
readonly revisions: "revisions.jsonl";
|
|
8
|
+
readonly clashes: "clashes.json";
|
|
9
|
+
readonly events: "events.jsonl";
|
|
10
|
+
readonly replicas: "replicas";
|
|
11
|
+
};
|
|
12
|
+
export declare const DOC_FORMAT = "tandem-doc/1";
|
|
13
|
+
export declare const CLASHES_FORMAT = "tandem-clashes/1";
|
|
14
|
+
export declare const LINK_FORMAT = "tandem-link/1";
|
|
15
|
+
export declare class FileSnapshotStore implements SnapshotStore {
|
|
16
|
+
private readonly path;
|
|
17
|
+
private readonly name;
|
|
18
|
+
private readonly options;
|
|
19
|
+
constructor(path: string, name: string, options?: FileOptions);
|
|
20
|
+
load(): DocSnapshot | null;
|
|
21
|
+
save(snapshot: DocSnapshot): void;
|
|
22
|
+
}
|
|
23
|
+
export declare class FileClashStore implements ClashStore {
|
|
24
|
+
private readonly path;
|
|
25
|
+
private readonly options;
|
|
26
|
+
constructor(path: string, options?: FileOptions);
|
|
27
|
+
load(): ClashState;
|
|
28
|
+
save(state: ClashState): void;
|
|
29
|
+
}
|
|
30
|
+
export declare class FileLinkStore implements LinkStore {
|
|
31
|
+
private readonly path;
|
|
32
|
+
private readonly options;
|
|
33
|
+
constructor(path: string, options?: FileOptions);
|
|
34
|
+
load(): Link | null;
|
|
35
|
+
save(link: Link): void;
|
|
36
|
+
}
|
|
37
|
+
export declare function replicaLinkPath(dir: string, peer: string): string;
|
|
38
|
+
export type DurableOptions = Omit<WorkcopyOptions, 'store' | 'log' | 'clashes' | 'snapshot'> & FileOptions;
|
|
39
|
+
export declare function durableWorkcopy(dir: string, options: DurableOptions): Workcopy;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { EventLog } from "./eventlog.js";
|
|
4
|
+
import { FileEventStore } from "./eventlog-file.js";
|
|
5
|
+
import { removeStaleTemps, writeFileAtomic } from "./fsutil.js";
|
|
6
|
+
import { isObject } from "./json.js";
|
|
7
|
+
import { MAX_CLOSED_CLASHES } from "./persist.js";
|
|
8
|
+
import { FileRevisionStore } from "./revisions-file.js";
|
|
9
|
+
import { Workcopy } from "./workcopy.js";
|
|
10
|
+
export const LAYOUT = {
|
|
11
|
+
doc: 'doc.json',
|
|
12
|
+
revisions: 'revisions.jsonl',
|
|
13
|
+
clashes: 'clashes.json',
|
|
14
|
+
events: 'events.jsonl',
|
|
15
|
+
replicas: 'replicas',
|
|
16
|
+
};
|
|
17
|
+
export const DOC_FORMAT = 'tandem-doc/1';
|
|
18
|
+
export const CLASHES_FORMAT = 'tandem-clashes/1';
|
|
19
|
+
export const LINK_FORMAT = 'tandem-link/1';
|
|
20
|
+
function readJson(path) {
|
|
21
|
+
try {
|
|
22
|
+
const value = JSON.parse(readFileSync(path, 'utf8'));
|
|
23
|
+
return isObject(value) ? value : null;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class FileSnapshotStore {
|
|
30
|
+
path;
|
|
31
|
+
name;
|
|
32
|
+
options;
|
|
33
|
+
constructor(path, name, options = {}) {
|
|
34
|
+
this.path = path;
|
|
35
|
+
this.name = name;
|
|
36
|
+
this.options = options;
|
|
37
|
+
}
|
|
38
|
+
load() {
|
|
39
|
+
const file = readJson(this.path);
|
|
40
|
+
if (!file || file.format !== DOC_FORMAT || typeof file.version !== 'number' || !('state' in file))
|
|
41
|
+
return null;
|
|
42
|
+
return { version: file.version, state: file.state, last_writer: typeof file.last_writer === 'string' ? file.last_writer : 'system', pending: file.pending ?? null };
|
|
43
|
+
}
|
|
44
|
+
save(snapshot) {
|
|
45
|
+
writeFileAtomic(this.path, `${JSON.stringify({ format: DOC_FORMAT, name: this.name, ...snapshot }, null, 1)}\n`, this.options);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class FileClashStore {
|
|
49
|
+
path;
|
|
50
|
+
options;
|
|
51
|
+
constructor(path, options = {}) {
|
|
52
|
+
this.path = path;
|
|
53
|
+
this.options = options;
|
|
54
|
+
}
|
|
55
|
+
load() {
|
|
56
|
+
const file = readJson(this.path);
|
|
57
|
+
if (!file || file.format !== CLASHES_FORMAT)
|
|
58
|
+
return { open: [], closed: [], count: 0 };
|
|
59
|
+
return {
|
|
60
|
+
open: Array.isArray(file.open) ? file.open : [],
|
|
61
|
+
closed: Array.isArray(file.closed) ? file.closed.slice(-MAX_CLOSED_CLASHES) : [],
|
|
62
|
+
count: typeof file.count === 'number' ? file.count : 0,
|
|
63
|
+
...(file.ids === 'content' ? { ids: 'content' } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
save(state) {
|
|
67
|
+
writeFileAtomic(this.path, `${JSON.stringify({ format: CLASHES_FORMAT, ...state }, null, 1)}\n`, this.options);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export class FileLinkStore {
|
|
71
|
+
path;
|
|
72
|
+
options;
|
|
73
|
+
constructor(path, options = {}) {
|
|
74
|
+
this.path = path;
|
|
75
|
+
this.options = options;
|
|
76
|
+
}
|
|
77
|
+
load() {
|
|
78
|
+
const file = readJson(this.path);
|
|
79
|
+
if (!file || file.format !== LINK_FORMAT || !('base' in file) || !('mark' in file))
|
|
80
|
+
return null;
|
|
81
|
+
return { base: file.base, mark: file.mark, peer_mark: typeof file.peer_mark === 'number' ? file.peer_mark : undefined, synced_at: typeof file.synced_at === 'string' ? file.synced_at : '' };
|
|
82
|
+
}
|
|
83
|
+
save(link) {
|
|
84
|
+
writeFileAtomic(this.path, `${JSON.stringify({ format: LINK_FORMAT, ...link })}\n`, this.options);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export function replicaLinkPath(dir, peer) {
|
|
88
|
+
return join(dir, LAYOUT.replicas, `${encodeURIComponent(peer)}.json`);
|
|
89
|
+
}
|
|
90
|
+
export function durableWorkcopy(dir, options) {
|
|
91
|
+
mkdirSync(dir, { recursive: true });
|
|
92
|
+
removeStaleTemps(dir);
|
|
93
|
+
const { fsync = true, ...rest } = options;
|
|
94
|
+
const file = { fsync };
|
|
95
|
+
const name = rest.name ?? 'doc';
|
|
96
|
+
return new Workcopy({
|
|
97
|
+
...rest,
|
|
98
|
+
name,
|
|
99
|
+
store: new FileRevisionStore(join(dir, LAYOUT.revisions), file),
|
|
100
|
+
log: new EventLog(new FileEventStore(join(dir, LAYOUT.events), file), rest.clock),
|
|
101
|
+
clashes: new FileClashStore(join(dir, LAYOUT.clashes), file),
|
|
102
|
+
snapshot: new FileSnapshotStore(join(dir, LAYOUT.doc), name, file),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TandemEvent } from './events.js';
|
|
2
|
+
import type { EventStore } from './eventlog.js';
|
|
3
|
+
import { type FileOptions } from './fsutil.js';
|
|
4
|
+
export declare class FileEventStore implements EventStore {
|
|
5
|
+
private readonly path;
|
|
6
|
+
private readonly options;
|
|
7
|
+
private cache;
|
|
8
|
+
constructor(path: string, options?: FileOptions);
|
|
9
|
+
append(event: TandemEvent): void;
|
|
10
|
+
list(after: number): TandemEvent[];
|
|
11
|
+
lastSeq(): number;
|
|
12
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { appendLine, repairLines } from "./fsutil.js";
|
|
4
|
+
export class FileEventStore {
|
|
5
|
+
path;
|
|
6
|
+
options;
|
|
7
|
+
cache;
|
|
8
|
+
constructor(path, options = {}) {
|
|
9
|
+
this.path = path;
|
|
10
|
+
this.options = options;
|
|
11
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
12
|
+
repairLines(path);
|
|
13
|
+
this.cache = existsSync(path) ? parseLines(readFileSync(path, 'utf8')) : [];
|
|
14
|
+
}
|
|
15
|
+
append(event) {
|
|
16
|
+
appendLine(this.path, JSON.stringify(event), this.options);
|
|
17
|
+
this.cache.push(event);
|
|
18
|
+
}
|
|
19
|
+
list(after) {
|
|
20
|
+
return this.cache.filter((e) => (e.seq ?? 0) > after);
|
|
21
|
+
}
|
|
22
|
+
lastSeq() {
|
|
23
|
+
return this.cache.length ? (this.cache[this.cache.length - 1].seq ?? 0) : 0;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function parseLines(text) {
|
|
27
|
+
const out = [];
|
|
28
|
+
for (const line of text.split('\n')) {
|
|
29
|
+
if (!line.trim())
|
|
30
|
+
continue;
|
|
31
|
+
try {
|
|
32
|
+
out.push(JSON.parse(line));
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TandemEvent } from './events.js';
|
|
2
|
+
export interface EventStore {
|
|
3
|
+
append(event: TandemEvent): void;
|
|
4
|
+
list(after: number): TandemEvent[];
|
|
5
|
+
lastSeq(): number;
|
|
6
|
+
}
|
|
7
|
+
export declare class MemoryEventStore implements EventStore {
|
|
8
|
+
private events;
|
|
9
|
+
private readonly max;
|
|
10
|
+
constructor(max?: number);
|
|
11
|
+
append(event: TandemEvent): void;
|
|
12
|
+
list(after: number): TandemEvent[];
|
|
13
|
+
lastSeq(): number;
|
|
14
|
+
}
|
|
15
|
+
export type Listener = (event: TandemEvent) => void;
|
|
16
|
+
export declare class EventLog {
|
|
17
|
+
private readonly store;
|
|
18
|
+
private seq;
|
|
19
|
+
private readonly listeners;
|
|
20
|
+
private readonly clock;
|
|
21
|
+
constructor(store?: EventStore, clock?: () => string);
|
|
22
|
+
get lastSeq(): number;
|
|
23
|
+
get subscribers(): number;
|
|
24
|
+
publish(input: {
|
|
25
|
+
type: string;
|
|
26
|
+
ts?: string;
|
|
27
|
+
[field: string]: unknown;
|
|
28
|
+
}): TandemEvent;
|
|
29
|
+
events(after?: number): TandemEvent[];
|
|
30
|
+
subscribe(after: number, listener: Listener): () => void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isEphemeral, nowIso } from "./events.js";
|
|
2
|
+
export class MemoryEventStore {
|
|
3
|
+
events = [];
|
|
4
|
+
max;
|
|
5
|
+
constructor(max = 10_000) {
|
|
6
|
+
this.max = max;
|
|
7
|
+
}
|
|
8
|
+
append(event) {
|
|
9
|
+
this.events.push(event);
|
|
10
|
+
if (this.events.length > this.max)
|
|
11
|
+
this.events.splice(0, this.events.length - this.max);
|
|
12
|
+
}
|
|
13
|
+
list(after) {
|
|
14
|
+
return this.events.filter((e) => (e.seq ?? 0) > after);
|
|
15
|
+
}
|
|
16
|
+
lastSeq() {
|
|
17
|
+
return this.events.length ? (this.events[this.events.length - 1].seq ?? 0) : 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class EventLog {
|
|
21
|
+
store;
|
|
22
|
+
seq;
|
|
23
|
+
listeners = new Set();
|
|
24
|
+
clock;
|
|
25
|
+
constructor(store = new MemoryEventStore(), clock = nowIso) {
|
|
26
|
+
this.store = store;
|
|
27
|
+
this.clock = clock;
|
|
28
|
+
this.seq = store.lastSeq();
|
|
29
|
+
}
|
|
30
|
+
get lastSeq() {
|
|
31
|
+
return this.seq;
|
|
32
|
+
}
|
|
33
|
+
get subscribers() {
|
|
34
|
+
return this.listeners.size;
|
|
35
|
+
}
|
|
36
|
+
publish(input) {
|
|
37
|
+
const event = { ...input, ts: input.ts ?? this.clock() };
|
|
38
|
+
if (!isEphemeral(event.type)) {
|
|
39
|
+
event.seq = ++this.seq;
|
|
40
|
+
this.store.append(event);
|
|
41
|
+
}
|
|
42
|
+
for (const listener of [...this.listeners]) {
|
|
43
|
+
try {
|
|
44
|
+
listener(event);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return event;
|
|
51
|
+
}
|
|
52
|
+
events(after = 0) {
|
|
53
|
+
return this.store.list(after);
|
|
54
|
+
}
|
|
55
|
+
subscribe(after, listener) {
|
|
56
|
+
for (const event of this.store.list(after))
|
|
57
|
+
listener(event);
|
|
58
|
+
this.listeners.add(listener);
|
|
59
|
+
return () => {
|
|
60
|
+
this.listeners.delete(listener);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const PROTOCOL: string;
|
|
2
|
+
export declare const EPHEMERAL: ReadonlySet<string>;
|
|
3
|
+
export declare const EXTENSION_PREFIX: string;
|
|
4
|
+
export interface TandemEvent {
|
|
5
|
+
type: string;
|
|
6
|
+
ts: string;
|
|
7
|
+
seq?: number;
|
|
8
|
+
[field: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
export declare function isEphemeral(type: string): boolean;
|
|
11
|
+
export declare function eventTypes(): string[];
|
|
12
|
+
export declare function fieldOk(rule: string, value: unknown): boolean;
|
|
13
|
+
export declare function validateEvent(event: unknown): string[];
|
|
14
|
+
export declare function isTimestamp(value: unknown): boolean;
|
|
15
|
+
export declare function nowIso(): string;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import spec from './events.json' with { type: 'json' };
|
|
2
|
+
export const PROTOCOL = spec.protocol;
|
|
3
|
+
export const EPHEMERAL = new Set(spec.ephemeral);
|
|
4
|
+
export const EXTENSION_PREFIX = spec.extension_prefix;
|
|
5
|
+
const TYPES = spec.types;
|
|
6
|
+
const TS_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
|
|
7
|
+
export function isEphemeral(type) {
|
|
8
|
+
return EPHEMERAL.has(type);
|
|
9
|
+
}
|
|
10
|
+
export function eventTypes() {
|
|
11
|
+
return Object.keys(TYPES);
|
|
12
|
+
}
|
|
13
|
+
function matches(kind, value) {
|
|
14
|
+
switch (kind) {
|
|
15
|
+
case 'string':
|
|
16
|
+
return typeof value === 'string';
|
|
17
|
+
case 'number':
|
|
18
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
19
|
+
case 'integer':
|
|
20
|
+
return Number.isInteger(value);
|
|
21
|
+
case 'boolean':
|
|
22
|
+
return typeof value === 'boolean';
|
|
23
|
+
case 'object':
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
case 'array':
|
|
26
|
+
return Array.isArray(value);
|
|
27
|
+
case 'null':
|
|
28
|
+
return value === null;
|
|
29
|
+
case 'any':
|
|
30
|
+
return true;
|
|
31
|
+
default:
|
|
32
|
+
if (kind.startsWith('enum:'))
|
|
33
|
+
return typeof value === 'string' && kind.slice(5).split(',').includes(value);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function fieldOk(rule, value) {
|
|
38
|
+
const kinds = rule.replace(/\?$/, '').split('|');
|
|
39
|
+
return kinds.some((kind) => matches(kind, value));
|
|
40
|
+
}
|
|
41
|
+
export function validateEvent(event) {
|
|
42
|
+
if (typeof event !== 'object' || event === null || Array.isArray(event))
|
|
43
|
+
return ['not_object'];
|
|
44
|
+
const ev = event;
|
|
45
|
+
const errors = [];
|
|
46
|
+
if (typeof ev.type !== 'string' || !ev.type)
|
|
47
|
+
return ['missing_type'];
|
|
48
|
+
if (typeof ev.ts !== 'string' || !TS_PATTERN.test(ev.ts))
|
|
49
|
+
errors.push('bad_ts');
|
|
50
|
+
const fields = TYPES[ev.type];
|
|
51
|
+
if (!fields && !ev.type.startsWith(EXTENSION_PREFIX))
|
|
52
|
+
errors.push('unknown_type');
|
|
53
|
+
const ephemeral = isEphemeral(ev.type);
|
|
54
|
+
if (ephemeral && 'seq' in ev)
|
|
55
|
+
errors.push('unexpected_seq');
|
|
56
|
+
if (!ephemeral) {
|
|
57
|
+
if (!('seq' in ev))
|
|
58
|
+
errors.push('missing_seq');
|
|
59
|
+
else if (!Number.isInteger(ev.seq) || ev.seq < 1)
|
|
60
|
+
errors.push('bad_seq');
|
|
61
|
+
}
|
|
62
|
+
for (const [name, rule] of Object.entries(fields ?? {})) {
|
|
63
|
+
const optional = rule.endsWith('?');
|
|
64
|
+
if (!(name in ev) || ev[name] === undefined) {
|
|
65
|
+
if (!optional)
|
|
66
|
+
errors.push(`missing_field:${name}`);
|
|
67
|
+
}
|
|
68
|
+
else if (!fieldOk(rule, ev[name])) {
|
|
69
|
+
errors.push(`bad_field:${name}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return errors;
|
|
73
|
+
}
|
|
74
|
+
export function isTimestamp(value) {
|
|
75
|
+
return typeof value === 'string' && TS_PATTERN.test(value);
|
|
76
|
+
}
|
|
77
|
+
export function nowIso() {
|
|
78
|
+
return new Date().toISOString();
|
|
79
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"protocol": "tandem/0",
|
|
3
|
+
"ephemeral": ["text_delta", "thinking", "context", "usage"],
|
|
4
|
+
"extension_prefix": "app.",
|
|
5
|
+
"types": {
|
|
6
|
+
"session_started": { "session": "string", "harness": "string", "model": "string|null?", "resumed": "boolean" },
|
|
7
|
+
"user": { "text": "string", "note": "string?", "images": "array?" },
|
|
8
|
+
"turn_started": { "turn": "integer" },
|
|
9
|
+
"text_delta": { "message_id": "string", "index": "integer|null?", "text": "string" },
|
|
10
|
+
"thinking": { "message_id": "string" },
|
|
11
|
+
"message": { "message_id": "string", "text": "string", "index": "integer|null?" },
|
|
12
|
+
"tool_call": { "id": "string", "message_id": "string|null?", "name": "string", "input": "object", "summary": "string?", "reason": "string?" },
|
|
13
|
+
"tool_result": { "id": "string", "is_error": "boolean", "text": "string", "summary": "string?", "images": "array?" },
|
|
14
|
+
"approval": {
|
|
15
|
+
"id": "string",
|
|
16
|
+
"tool": "string",
|
|
17
|
+
"input": "object",
|
|
18
|
+
"tool_use_id": "string|null?",
|
|
19
|
+
"display_name": "string|null?",
|
|
20
|
+
"summary": "string?",
|
|
21
|
+
"description": "string|null?",
|
|
22
|
+
"blocked_path": "string|null?",
|
|
23
|
+
"plan": "string|null?",
|
|
24
|
+
"always": "string|null?"
|
|
25
|
+
},
|
|
26
|
+
"approval_resolved": { "id": "string", "allow": "boolean", "always": "boolean?", "rules": "array?", "message": "string?", "mode": "string?" },
|
|
27
|
+
"question": { "id": "string", "questions": "array", "tool_use_id": "string|null?" },
|
|
28
|
+
"question_answered": { "id": "string", "answers": "object|null" },
|
|
29
|
+
"prompt_closed": { "id": "string", "reason": "enum:interrupted,ended,cancelled" },
|
|
30
|
+
"client_call": { "id": "string", "name": "string", "input": "object" },
|
|
31
|
+
"client_call_done": { "id": "string", "is_error": "boolean", "reason": "enum:timeout,cancelled?" },
|
|
32
|
+
"change": {
|
|
33
|
+
"version": "integer",
|
|
34
|
+
"actor": "string",
|
|
35
|
+
"delta": "array",
|
|
36
|
+
"reason": "string|null?",
|
|
37
|
+
"revision": "string|null?",
|
|
38
|
+
"summary": "string?",
|
|
39
|
+
"findings": "array?",
|
|
40
|
+
"doc": "string?"
|
|
41
|
+
},
|
|
42
|
+
"rejected": { "actor": "string", "findings": "array", "reason": "string|null?", "doc": "string?" },
|
|
43
|
+
"clash": { "id": "string", "path": "array", "kept_actor": "string", "dropped_actor": "string", "doc": "string?" },
|
|
44
|
+
"clash_resolved": { "id": "string", "choice": "enum:kept,dropped" },
|
|
45
|
+
"proposal": {
|
|
46
|
+
"id": "string",
|
|
47
|
+
"kind": "string",
|
|
48
|
+
"state": "enum:proposed,applied,discarded,refused",
|
|
49
|
+
"session": "string|null?",
|
|
50
|
+
"title": "string|null?",
|
|
51
|
+
"reason": "string|null?",
|
|
52
|
+
"doc": "string|null?",
|
|
53
|
+
"version": "integer|null?",
|
|
54
|
+
"delta": "array|null?",
|
|
55
|
+
"data": "any?",
|
|
56
|
+
"problems": "array?",
|
|
57
|
+
"edited": "boolean?",
|
|
58
|
+
"by": "string|null?",
|
|
59
|
+
"at": "string?",
|
|
60
|
+
"resolved_at": "string|null?",
|
|
61
|
+
"revision": "string|null?",
|
|
62
|
+
"result": "any?"
|
|
63
|
+
},
|
|
64
|
+
"compaction": { "phase": "enum:started,done,failed", "trigger": "string|null?", "pre_tokens": "integer|null?", "post_tokens": "integer|null?", "result": "string|null?" },
|
|
65
|
+
"context": { "tokens": "integer|null", "max": "integer|null", "percent": "number|null?" },
|
|
66
|
+
"usage": { "steps": "integer", "output_tokens": "integer", "elapsed_s": "number", "cost_usd": "number?" },
|
|
67
|
+
"settings": { "model": "string|null", "effort": "string|null", "permission_mode": "enum:bypass,ask,accept_edits,plan" },
|
|
68
|
+
"notice": { "level": "enum:info,warn,error", "text": "string" },
|
|
69
|
+
"turn_finished": {
|
|
70
|
+
"turn": "integer",
|
|
71
|
+
"is_error": "boolean",
|
|
72
|
+
"interrupted": "boolean",
|
|
73
|
+
"error": "string|null?",
|
|
74
|
+
"usage": "object?",
|
|
75
|
+
"limit": "object?",
|
|
76
|
+
"cost_usd": "number|null?"
|
|
77
|
+
},
|
|
78
|
+
"ended": { "reason": "string", "code": "integer|null?" }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { DocAdapter } from './workcopy.js';
|
|
2
|
+
import { type Json } from './json.js';
|
|
3
|
+
import type { Finding } from './revisions.js';
|
|
4
|
+
/** a document that is a set of text files, keyed by path relative to a root the app chooses */
|
|
5
|
+
export type FilesState = {
|
|
6
|
+
files: Record<string, string>;
|
|
7
|
+
};
|
|
8
|
+
export interface FilesPolicy {
|
|
9
|
+
/** extensions a file may have, with the dot (`['.ts', '.json']`). Default: any */
|
|
10
|
+
extensions?: string[];
|
|
11
|
+
/** extra rule for a path; return a message to refuse it */
|
|
12
|
+
allow?: (path: string) => string | null | undefined;
|
|
13
|
+
/** most files the set may hold. Default 64 */
|
|
14
|
+
maxFiles?: number;
|
|
15
|
+
/** largest file in characters. Default 200000 */
|
|
16
|
+
maxFileChars?: number;
|
|
17
|
+
/** largest set in characters. Default 1000000 */
|
|
18
|
+
maxTotalChars?: number;
|
|
19
|
+
/** app rules over the whole set (a json file that does not parse, a forbidden call). `block` rejects the write, `warn` rides on the revision */
|
|
20
|
+
check?: (files: Readonly<Record<string, string>>, ctx: {
|
|
21
|
+
actor: string;
|
|
22
|
+
}) => Finding[];
|
|
23
|
+
}
|
|
24
|
+
export declare const DEFAULT_MAX_FILES = 64;
|
|
25
|
+
export declare const DEFAULT_MAX_FILE_CHARS = 200000;
|
|
26
|
+
export declare const DEFAULT_MAX_TOTAL_CHARS = 1000000;
|
|
27
|
+
export declare function emptyFiles(files?: Record<string, string>): FilesState;
|
|
28
|
+
export declare function filesOf(state: Json): Record<string, string>;
|
|
29
|
+
/** why a path cannot be a file in the set, or null. Paths are relative, use `/`, and never climb out of the root or start a segment with a dot */
|
|
30
|
+
export declare function pathProblem(path: string): string | null;
|
|
31
|
+
export declare function lineCount(text: string): number;
|
|
32
|
+
export declare function summarizeFiles(before: Json, after: Json): string;
|
|
33
|
+
/** the adapter for a files document: checks paths, kinds and sizes, then the app's own `check`. Merges per file (two writers on different files never clash) */
|
|
34
|
+
export declare function filesAdapter(policy?: FilesPolicy): DocAdapter;
|