@copilotz/work 0.0.0-stage → 1.0.0-rc.34
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 +56 -2
- package/boards/actions/create-board.d.ts +1 -0
- package/boards/actions/create-board.js +47 -0
- package/boards/actions/create-card.d.ts +1 -0
- package/boards/actions/create-card.js +83 -0
- package/boards/actions/delete-board.d.ts +1 -0
- package/boards/actions/delete-board.js +30 -0
- package/boards/actions/delete-card.d.ts +1 -0
- package/boards/actions/delete-card.js +24 -0
- package/boards/actions/move-card.d.ts +1 -0
- package/boards/actions/move-card.js +85 -0
- package/boards/actions/update-board.d.ts +1 -0
- package/boards/actions/update-board.js +110 -0
- package/boards/actions/update-card.d.ts +1 -0
- package/boards/actions/update-card.js +67 -0
- package/boards/collections/board.d.ts +71 -0
- package/boards/collections/board.js +32 -0
- package/boards/collections/card.d.ts +74 -0
- package/boards/collections/card.js +29 -0
- package/boards/contract.d.ts +67 -0
- package/boards/contract.js +20 -0
- package/boards/mod.d.ts +34 -0
- package/boards/mod.js +72 -0
- package/boards/resources/tools.d.ts +2 -0
- package/boards/resources/tools.js +414 -0
- package/boards/shared/columns.d.ts +39 -0
- package/boards/shared/columns.js +88 -0
- package/boards/shared/dates.d.ts +11 -0
- package/boards/shared/dates.js +22 -0
- package/boards/shared/errors.d.ts +24 -0
- package/boards/shared/errors.js +20 -0
- package/boards/shared/fields.d.ts +65 -0
- package/boards/shared/fields.js +44 -0
- package/boards/shared/load.d.ts +22 -0
- package/boards/shared/load.js +32 -0
- package/boards/shared/order.d.ts +30 -0
- package/boards/shared/order.js +47 -0
- package/boards/shared/people.d.ts +30 -0
- package/boards/shared/people.js +87 -0
- package/boards/shared/said.d.ts +28 -0
- package/boards/shared/said.js +30 -0
- package/boards/views/web/board-head.d.ts +62 -0
- package/boards/views/web/board-head.js +170 -0
- package/boards/views/web/board-page.d.ts +17 -0
- package/boards/views/web/board-page.js +97 -0
- package/boards/views/web/board-view.d.ts +40 -0
- package/boards/views/web/board-view.js +341 -0
- package/boards/views/web/boards-tab.d.ts +13 -0
- package/boards/views/web/boards-tab.js +109 -0
- package/boards/views/web/card-sheet.d.ts +47 -0
- package/boards/views/web/card-sheet.js +314 -0
- package/boards/views/web/card-tile.d.ts +29 -0
- package/boards/views/web/card-tile.js +20 -0
- package/boards/views/web/column.d.ts +38 -0
- package/boards/views/web/column.js +171 -0
- package/boards/views/web/drag.d.ts +36 -0
- package/boards/views/web/drag.js +276 -0
- package/boards/views/web/glyphs.d.ts +29 -0
- package/boards/views/web/glyphs.js +25 -0
- package/boards/views/web/load.d.ts +32 -0
- package/boards/views/web/load.js +33 -0
- package/boards/views/web/palette.d.ts +15 -0
- package/boards/views/web/palette.js +180 -0
- package/boards/views/web/pickers.d.ts +45 -0
- package/boards/views/web/pickers.js +83 -0
- package/boards/views/web/shown.d.ts +77 -0
- package/boards/views/web/shown.js +136 -0
- package/boards/views/web/step.d.ts +13 -0
- package/boards/views/web/step.js +41 -0
- package/boards/views/web/style.d.ts +10 -0
- package/boards/views/web/style.js +380 -0
- package/boards/views/web/toolbar.d.ts +21 -0
- package/boards/views/web/toolbar.js +39 -0
- package/boards/web.d.ts +26 -0
- package/boards/web.js +52 -0
- package/contract.d.ts +7 -0
- package/contract.js +7 -0
- package/documents/actions/create.d.ts +1 -0
- package/documents/actions/create.js +28 -0
- package/documents/actions/delete.d.ts +1 -0
- package/documents/actions/delete.js +32 -0
- package/documents/actions/presence.d.ts +1 -0
- package/documents/actions/presence.js +45 -0
- package/documents/actions/rename.d.ts +1 -0
- package/documents/actions/rename.js +33 -0
- package/documents/actions/restore.d.ts +1 -0
- package/documents/actions/restore.js +39 -0
- package/documents/actions/save.d.ts +1 -0
- package/documents/actions/save.js +78 -0
- package/documents/collections/document-presence.d.ts +79 -0
- package/documents/collections/document-presence.js +25 -0
- package/documents/collections/document-version.d.ts +166 -0
- package/documents/collections/document-version.js +60 -0
- package/documents/collections/document.d.ts +131 -0
- package/documents/collections/document.js +72 -0
- package/documents/contract.d.ts +17 -0
- package/documents/contract.js +1 -0
- package/documents/mod.d.ts +38 -0
- package/documents/mod.js +75 -0
- package/documents/resources/tools.d.ts +2 -0
- package/documents/resources/tools.js +253 -0
- package/documents/shared/diff.d.ts +37 -0
- package/documents/shared/diff.js +148 -0
- package/documents/shared/merge.d.ts +29 -0
- package/documents/shared/merge.js +120 -0
- package/documents/shared/presence.d.ts +7 -0
- package/documents/shared/presence.js +7 -0
- package/documents/shared/text.d.ts +36 -0
- package/documents/shared/text.js +67 -0
- package/documents/shared/who.d.ts +23 -0
- package/documents/shared/who.js +39 -0
- package/documents/shared/write.d.ts +54 -0
- package/documents/shared/write.js +123 -0
- package/documents/views/web/changes.d.ts +22 -0
- package/documents/views/web/changes.js +57 -0
- package/documents/views/web/conflict.d.ts +38 -0
- package/documents/views/web/conflict.js +96 -0
- package/documents/views/web/draft.d.ts +18 -0
- package/documents/views/web/draft.js +32 -0
- package/documents/views/web/editor.d.ts +57 -0
- package/documents/views/web/editor.js +268 -0
- package/documents/views/web/format.d.ts +56 -0
- package/documents/views/web/format.js +245 -0
- package/documents/views/web/history.d.ts +23 -0
- package/documents/views/web/history.js +118 -0
- package/documents/views/web/list.d.ts +37 -0
- package/documents/views/web/list.js +211 -0
- package/documents/views/web/page.d.ts +35 -0
- package/documents/views/web/page.js +416 -0
- package/documents/views/web/palette.d.ts +16 -0
- package/documents/views/web/palette.js +168 -0
- package/documents/views/web/people.d.ts +60 -0
- package/documents/views/web/people.js +63 -0
- package/documents/views/web/presence.d.ts +30 -0
- package/documents/views/web/presence.js +75 -0
- package/documents/views/web/step.d.ts +25 -0
- package/documents/views/web/step.js +81 -0
- package/documents/views/web/style.d.ts +7 -0
- package/documents/views/web/style.js +266 -0
- package/documents/web.d.ts +18 -0
- package/documents/web.js +46 -0
- package/mod.d.ts +18 -0
- package/mod.js +18 -0
- package/package.json +61 -4
- package/web.d.ts +13 -0
- package/web.js +13 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** A part of the merge: lines both agree on, or a conflict between them. */
|
|
2
|
+
export type Chunk = {
|
|
3
|
+
kind: "same";
|
|
4
|
+
lines: string[];
|
|
5
|
+
} | {
|
|
6
|
+
kind: "conflict";
|
|
7
|
+
base: string[];
|
|
8
|
+
mine: string[];
|
|
9
|
+
theirs: string[];
|
|
10
|
+
};
|
|
11
|
+
/** What merging came to: its parts, and, when nothing conflicts, the merged text. */
|
|
12
|
+
export type Merged = {
|
|
13
|
+
chunks: Chunk[];
|
|
14
|
+
conflicts: number;
|
|
15
|
+
text?: string;
|
|
16
|
+
};
|
|
17
|
+
/** Merges `mine` and `theirs`, both changed from `base`. */
|
|
18
|
+
export declare function merge(base: string, mine: string, theirs: string): Merged;
|
|
19
|
+
/** How a conflict is settled: one side, both (theirs first, then mine), or lines of one's own. */
|
|
20
|
+
export type Pick = "mine" | "theirs" | "both" | {
|
|
21
|
+
lines: string[];
|
|
22
|
+
};
|
|
23
|
+
/** The merged text, with each conflict settled as `picks` says (by its place among them). */
|
|
24
|
+
export declare function textOf(chunks: Chunk[], picks?: Pick[]): string;
|
|
25
|
+
/**
|
|
26
|
+
* A field merged the same way, whole: whichever side changed it wins; both,
|
|
27
|
+
* to the same value, that value; both, differently, a conflict (undefined).
|
|
28
|
+
*/
|
|
29
|
+
export declare function mergeField<T>(base: T, mine: T, theirs: T): T | undefined;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two people's changes to the same version, merged line by line (a
|
|
3
|
+
* three-way merge): what each changed since the version both started from
|
|
4
|
+
* (`base`), applied together where they don't touch the same lines. Where
|
|
5
|
+
* they do (both changed a line, or added lines in the same place, or one
|
|
6
|
+
* changed what the other took out), that part is a conflict: the base's
|
|
7
|
+
* lines and each side's, for a person to choose between. Changes next to
|
|
8
|
+
* each other, not over each other, merge. Pure, so the server merges a save
|
|
9
|
+
* and the browser shows the same conflicts.
|
|
10
|
+
*/
|
|
11
|
+
import { hunksOf, linesOf } from "./diff.js";
|
|
12
|
+
/** `base`'s lines `start` to `end`, with one side's hunks there applied. */
|
|
13
|
+
function applied(base, start, end, hunks) {
|
|
14
|
+
const out = [];
|
|
15
|
+
let at = start;
|
|
16
|
+
for (const h of hunks) {
|
|
17
|
+
out.push(...base.slice(at, h.start), ...h.lines);
|
|
18
|
+
at = h.end;
|
|
19
|
+
}
|
|
20
|
+
out.push(...base.slice(at, end));
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
const equal = (a, b) => a.length === b.length && a.every((l, i) => l === b[i]);
|
|
24
|
+
/** Merges `mine` and `theirs`, both changed from `base`. */
|
|
25
|
+
export function merge(base, mine, theirs) {
|
|
26
|
+
if (mine === theirs || theirs === base)
|
|
27
|
+
return whole(mine);
|
|
28
|
+
if (mine === base)
|
|
29
|
+
return whole(theirs);
|
|
30
|
+
const lines = linesOf(base);
|
|
31
|
+
const all = [
|
|
32
|
+
...hunksOf(lines, linesOf(mine)).map((h) => ({ ...h, side: "mine" })),
|
|
33
|
+
...hunksOf(lines, linesOf(theirs)).map((h) => ({ ...h, side: "theirs" })),
|
|
34
|
+
].sort((a, b) => a.start - b.start || (a.end - a.start) - (b.end - b.start));
|
|
35
|
+
// Changes that overlap go together: each group is a stretch of the base both may have changed.
|
|
36
|
+
const groups = [];
|
|
37
|
+
for (const h of all) {
|
|
38
|
+
const last = groups.at(-1);
|
|
39
|
+
const insertion = h.start === h.end;
|
|
40
|
+
const over = last && (h.start < last.end ||
|
|
41
|
+
// Two sides adding lines at the same place: which goes first is a choice.
|
|
42
|
+
(insertion && last.start === last.end && h.start === last.start));
|
|
43
|
+
if (over) {
|
|
44
|
+
last.end = Math.max(last.end, h.end);
|
|
45
|
+
last.hunks.push(h);
|
|
46
|
+
}
|
|
47
|
+
else
|
|
48
|
+
groups.push({ start: h.start, end: h.end, hunks: [h] });
|
|
49
|
+
}
|
|
50
|
+
const chunks = [];
|
|
51
|
+
const same = (more) => {
|
|
52
|
+
if (!more.length)
|
|
53
|
+
return;
|
|
54
|
+
const last = chunks.at(-1);
|
|
55
|
+
if (last?.kind === "same")
|
|
56
|
+
last.lines.push(...more);
|
|
57
|
+
else
|
|
58
|
+
chunks.push({ kind: "same", lines: [...more] });
|
|
59
|
+
};
|
|
60
|
+
let at = 0;
|
|
61
|
+
let conflicts = 0;
|
|
62
|
+
for (const g of groups) {
|
|
63
|
+
same(lines.slice(at, g.start));
|
|
64
|
+
const of = (side) => g.hunks.filter((h) => h.side === side);
|
|
65
|
+
const ours = applied(lines, g.start, g.end, of("mine"));
|
|
66
|
+
const others = applied(lines, g.start, g.end, of("theirs"));
|
|
67
|
+
const was = lines.slice(g.start, g.end);
|
|
68
|
+
if (!of("theirs").length || equal(ours, others))
|
|
69
|
+
same(ours);
|
|
70
|
+
else if (!of("mine").length)
|
|
71
|
+
same(others);
|
|
72
|
+
else {
|
|
73
|
+
chunks.push({ kind: "conflict", base: was, mine: ours, theirs: others });
|
|
74
|
+
conflicts++;
|
|
75
|
+
}
|
|
76
|
+
at = g.end;
|
|
77
|
+
}
|
|
78
|
+
same(lines.slice(at));
|
|
79
|
+
return {
|
|
80
|
+
chunks,
|
|
81
|
+
conflicts,
|
|
82
|
+
...(conflicts ? {} : { text: textOf(chunks) }),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const whole = (text) => ({
|
|
86
|
+
chunks: [{ kind: "same", lines: linesOf(text) }],
|
|
87
|
+
conflicts: 0,
|
|
88
|
+
text,
|
|
89
|
+
});
|
|
90
|
+
/** The merged text, with each conflict settled as `picks` says (by its place among them). */
|
|
91
|
+
export function textOf(chunks, picks = []) {
|
|
92
|
+
const out = [];
|
|
93
|
+
let n = 0;
|
|
94
|
+
for (const c of chunks) {
|
|
95
|
+
if (c.kind === "same") {
|
|
96
|
+
out.push(...c.lines);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const pick = picks[n++] ?? "mine";
|
|
100
|
+
out.push(...(pick === "mine"
|
|
101
|
+
? c.mine
|
|
102
|
+
: pick === "theirs"
|
|
103
|
+
? c.theirs
|
|
104
|
+
: pick === "both"
|
|
105
|
+
? [...c.theirs, ...c.mine]
|
|
106
|
+
: pick.lines));
|
|
107
|
+
}
|
|
108
|
+
return out.join("\n");
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A field merged the same way, whole: whichever side changed it wins; both,
|
|
112
|
+
* to the same value, that value; both, differently, a conflict (undefined).
|
|
113
|
+
*/
|
|
114
|
+
export function mergeField(base, mine, theirs) {
|
|
115
|
+
if (mine === theirs || theirs === base)
|
|
116
|
+
return mine;
|
|
117
|
+
if (mine === base)
|
|
118
|
+
return theirs;
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Who has a document open: how long an entry lasts without word from its page, and its `_id`. */
|
|
2
|
+
/** How long an entry lasts without word from its page (a hidden tab's timers slow to a minute). */
|
|
3
|
+
export declare const STALE_MS = 90000;
|
|
4
|
+
/** How often an open page says it's still there. */
|
|
5
|
+
export declare const BEAT_MS = 25000;
|
|
6
|
+
/** An entry's `_id`: the document, who, and their page. */
|
|
7
|
+
export declare const presenceId: (document: string, who: string, page: string) => string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Who has a document open: how long an entry lasts without word from its page, and its `_id`. */
|
|
2
|
+
/** How long an entry lasts without word from its page (a hidden tab's timers slow to a minute). */
|
|
3
|
+
export const STALE_MS = 90_000;
|
|
4
|
+
/** How often an open page says it's still there. */
|
|
5
|
+
export const BEAT_MS = 25_000;
|
|
6
|
+
/** An entry's `_id`: the document, who, and their page. */
|
|
7
|
+
export const presenceId = (document, who, page) => `${document}|${who}|${page}`;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What documents hold and how they read in a few words: the most a text may
|
|
3
|
+
* be, its size, who someone is from the actor doing something (`person:ana`,
|
|
4
|
+
* `agent:north`), a document's address, and its first lines as plain text.
|
|
5
|
+
*/
|
|
6
|
+
import type { Actor } from "copilotz";
|
|
7
|
+
/** `T` without `K`, nor the index signature a schema's type has (its fields stay). */
|
|
8
|
+
export type Without<T, K extends string> = {
|
|
9
|
+
[P in keyof T as string extends P ? never : number extends P ? never : P extends K ? never : P]: T[P];
|
|
10
|
+
};
|
|
11
|
+
/** The most a document's text may be, in bytes of UTF-8: 100 KB. */
|
|
12
|
+
export declare const LIMIT: number;
|
|
13
|
+
/** The most a title may be, in characters. */
|
|
14
|
+
export declare const TITLE = 200;
|
|
15
|
+
/** What a new document is called until someone names it. */
|
|
16
|
+
export declare const UNTITLED = "Untitled";
|
|
17
|
+
/** A text's size, in bytes of UTF-8. */
|
|
18
|
+
export declare const sizeOf: (text: string) => number;
|
|
19
|
+
/** Who did it, as a member of a space is keyed: `person:ana`, `agent:north`. */
|
|
20
|
+
export declare const keyOf: (actor: Pick<Actor, "kind" | "id">) => string;
|
|
21
|
+
/** The person an agent works for, from its actor, if it says. */
|
|
22
|
+
export declare function forOf(actor: Actor | undefined): string | undefined;
|
|
23
|
+
/** A document's page (where the person is, `in`, if known). */
|
|
24
|
+
export declare const documentPath: (id: string, space?: string) => string;
|
|
25
|
+
/** A version's `_id`: `<document>:<version>`. */
|
|
26
|
+
export declare const versionId: (document: string, version: number) => string;
|
|
27
|
+
/** A space's Documents tab. */
|
|
28
|
+
export declare const documentsTab: (space: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* How a document opens, as plain text (`most` characters at most): its first
|
|
31
|
+
* lines that say something, headings left out (they're its outline), lines
|
|
32
|
+
* that don't end a sentence kept apart with a dot (a list's items).
|
|
33
|
+
*/
|
|
34
|
+
export declare function excerptOf(text: string, title?: string, most?: number): string;
|
|
35
|
+
/** "2.1 KB", for a size in bytes. */
|
|
36
|
+
export declare const kb: (bytes: number) => string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** The most a document's text may be, in bytes of UTF-8: 100 KB. */
|
|
2
|
+
export const LIMIT = 100 * 1024;
|
|
3
|
+
/** The most a title may be, in characters. */
|
|
4
|
+
export const TITLE = 200;
|
|
5
|
+
/** What a new document is called until someone names it. */
|
|
6
|
+
export const UNTITLED = "Untitled";
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
/** A text's size, in bytes of UTF-8. */
|
|
9
|
+
export const sizeOf = (text) => encoder.encode(text).length;
|
|
10
|
+
/** Who did it, as a member of a space is keyed: `person:ana`, `agent:north`. */
|
|
11
|
+
export const keyOf = (actor) => `${actor.kind}:${actor.id}`;
|
|
12
|
+
/** The person an agent works for, from its actor, if it says. */
|
|
13
|
+
export function forOf(actor) {
|
|
14
|
+
const person = actor?.for;
|
|
15
|
+
return person && typeof person === "object" && typeof person.id === "string"
|
|
16
|
+
? keyOf(person)
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
/** A document's page (where the person is, `in`, if known). */
|
|
20
|
+
export const documentPath = (id, space) => `/document/${encodeURIComponent(id)}${space ? `?in=${encodeURIComponent(space)}` : ""}`;
|
|
21
|
+
/** A version's `_id`: `<document>:<version>`. */
|
|
22
|
+
export const versionId = (document, version) => `${document}:${version}`;
|
|
23
|
+
/** A space's Documents tab. */
|
|
24
|
+
export const documentsTab = (space) => `/space/${encodeURIComponent(space)}?tab=documents`;
|
|
25
|
+
/** A line of Markdown as plain words: no heading marks, emphasis, list marks or link targets. */
|
|
26
|
+
const plain = (line) => line
|
|
27
|
+
.replace(/^\s{0,3}(#{1,6}\s+|>\s?|[-*+]\s+(\[[ xX]\]\s+)?|\d+[.)]\s+)/, "")
|
|
28
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
29
|
+
.replace(/(\*\*|__|~~|`+)/g, "")
|
|
30
|
+
.replace(/(^|\W)[*_](\S[^*_]*)[*_](?=\W|$)/g, "$1$2")
|
|
31
|
+
.trim();
|
|
32
|
+
/**
|
|
33
|
+
* How a document opens, as plain text (`most` characters at most): its first
|
|
34
|
+
* lines that say something, headings left out (they're its outline), lines
|
|
35
|
+
* that don't end a sentence kept apart with a dot (a list's items).
|
|
36
|
+
*/
|
|
37
|
+
export function excerptOf(text, title = "", most = 220) {
|
|
38
|
+
const said = [];
|
|
39
|
+
const headings = [];
|
|
40
|
+
let length = 0;
|
|
41
|
+
let fenced = false;
|
|
42
|
+
for (const raw of text.split("\n")) {
|
|
43
|
+
if (/^\s*(```|~~~)/.test(raw)) {
|
|
44
|
+
fenced = !fenced;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (fenced || /^\s*([-*_]\s*){3,}$/.test(raw) || /^\s*\|?\s*:?-{3,}/.test(raw))
|
|
48
|
+
continue;
|
|
49
|
+
const line = plain(raw.replace(/\|/g, " "));
|
|
50
|
+
if (!line)
|
|
51
|
+
continue;
|
|
52
|
+
if (/^\s{0,3}#{1,6}\s/.test(raw)) {
|
|
53
|
+
if (line.toLowerCase() !== title.trim().toLowerCase())
|
|
54
|
+
headings.push(line);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
said.push(line);
|
|
58
|
+
length += line.length + 3;
|
|
59
|
+
if (length > most)
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
const lines = said.length ? said : headings;
|
|
63
|
+
const flat = lines.reduce((all, line) => !all ? line : /[.!?:;,…]$/.test(all) ? `${all} ${line}` : `${all} · ${line}`, "").replace(/\s+/g, " ").trim();
|
|
64
|
+
return flat.length > most ? `${flat.slice(0, most - 1).trimEnd()}…` : flat;
|
|
65
|
+
}
|
|
66
|
+
/** "2.1 KB", for a size in bytes. */
|
|
67
|
+
export const kb = (bytes) => bytes < 1000 ? `${bytes} bytes` : `${(bytes / 1024).toFixed(bytes < 10240 ? 1 : 0)} KB`;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Who = {
|
|
2
|
+
kind: string;
|
|
3
|
+
id: string;
|
|
4
|
+
};
|
|
5
|
+
/** Who a key is. */
|
|
6
|
+
export declare function whoOf(key: string): Who;
|
|
7
|
+
/**
|
|
8
|
+
* A person, as a sentence names them: their profile's name (`names`, by id);
|
|
9
|
+
* else "Ana" for ana@example.com, "Ana Lima" for ana.lima@…; a sign-in's id
|
|
10
|
+
* (`google:1139…`) says nothing, so "someone".
|
|
11
|
+
*/
|
|
12
|
+
export declare function personName(id: string, names?: Record<string, string>): string;
|
|
13
|
+
/**
|
|
14
|
+
* Someone, by key; `agentName` says what an agent is shown as, when it
|
|
15
|
+
* knows, and `names` what people are.
|
|
16
|
+
*/
|
|
17
|
+
export declare function nameOf(key: string, agentName?: (id: string) => string | undefined, names?: Record<string, string>): string;
|
|
18
|
+
/** The people among keys, for `namesFor`/`namesIn`: their ids. */
|
|
19
|
+
export declare const peopleOf: (keys: (string | undefined)[]) => string[];
|
|
20
|
+
/** What the app's agents are shown as (the `agents` resource): "North" for `north`. */
|
|
21
|
+
export declare const agentNamer: (ctx: {
|
|
22
|
+
resources: Record<string, unknown>;
|
|
23
|
+
}) => (id: string) => string | undefined;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who changed a document, as words say them: from a key (`person:ana`,
|
|
3
|
+
* `agent:north`), a person by their name (their profile's, from Copilotz's
|
|
4
|
+
* `namesIn`/`namesFor`; else an email's first part), an agent by what it's
|
|
5
|
+
* shown as when that's known ("North"), else by what it's called.
|
|
6
|
+
*/
|
|
7
|
+
import { nameOf as shownName } from "copilotz/plugins/access/contract";
|
|
8
|
+
/** Who a key is. */
|
|
9
|
+
export function whoOf(key) {
|
|
10
|
+
const at = key.indexOf(":");
|
|
11
|
+
return at < 0 ? { kind: "person", id: key } : { kind: key.slice(0, at), id: key.slice(at + 1) };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A person, as a sentence names them: their profile's name (`names`, by id);
|
|
15
|
+
* else "Ana" for ana@example.com, "Ana Lima" for ana.lima@…; a sign-in's id
|
|
16
|
+
* (`google:1139…`) says nothing, so "someone".
|
|
17
|
+
*/
|
|
18
|
+
export function personName(id, names = {}) {
|
|
19
|
+
if (names[id])
|
|
20
|
+
return names[id];
|
|
21
|
+
if (/^[^:@\s]+:/.test(id))
|
|
22
|
+
return shownName(id, names);
|
|
23
|
+
return (id.includes("@") ? id.split("@")[0] : id).split(/[._]+/).filter(Boolean)
|
|
24
|
+
.map((w) => w[0].toLocaleUpperCase() + w.slice(1)).join(" ") || id;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Someone, by key; `agentName` says what an agent is shown as, when it
|
|
28
|
+
* knows, and `names` what people are.
|
|
29
|
+
*/
|
|
30
|
+
export function nameOf(key, agentName, names) {
|
|
31
|
+
const who = whoOf(key);
|
|
32
|
+
if (who.kind === "agent")
|
|
33
|
+
return agentName?.(who.id) || who.id;
|
|
34
|
+
return personName(who.id, names);
|
|
35
|
+
}
|
|
36
|
+
/** The people among keys, for `namesFor`/`namesIn`: their ids. */
|
|
37
|
+
export const peopleOf = (keys) => keys.flatMap((k) => k && whoOf(k).kind === "person" ? [whoOf(k).id] : []);
|
|
38
|
+
/** What the app's agents are shown as (the `agents` resource): "North" for `north`. */
|
|
39
|
+
export const agentNamer = (ctx) => (id) => ctx.resources.agents?.[id]?.name;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a document changes, for people's Actions and agents' tools alike:
|
|
3
|
+
* each change is its next version, written with the document in one
|
|
4
|
+
* transaction (read, then written, so nothing comes between), attributed
|
|
5
|
+
* to who made it. The rules decide who may: a document someone can read
|
|
6
|
+
* but not change doesn't match their write, and they're told so.
|
|
7
|
+
*/
|
|
8
|
+
import type { ReadContext, TransactionScope } from "copilotz";
|
|
9
|
+
import type { Document } from "../collections/document.js";
|
|
10
|
+
import { type DocumentVersion } from "../collections/document-version.js";
|
|
11
|
+
type Db = TransactionScope["db"];
|
|
12
|
+
/** An error with a code, as the runtime's are. */
|
|
13
|
+
export declare const fail: (message: string, code: string, more?: Record<string, unknown>) => Error & {
|
|
14
|
+
code: string;
|
|
15
|
+
};
|
|
16
|
+
/** Who makes a change: `person:ana`, or `agent:north` working for `person:ana`. */
|
|
17
|
+
export type Author = {
|
|
18
|
+
by: string;
|
|
19
|
+
for?: string;
|
|
20
|
+
};
|
|
21
|
+
/** A change, as the version it makes: the title and text after it, and how it was made. */
|
|
22
|
+
export type Change = {
|
|
23
|
+
title: string;
|
|
24
|
+
text: string;
|
|
25
|
+
kind: DocumentVersion["kind"];
|
|
26
|
+
/** Saved over others' changes since version `base`, merged. */
|
|
27
|
+
merged?: boolean;
|
|
28
|
+
base?: number;
|
|
29
|
+
/** Restored from this version. */
|
|
30
|
+
from?: number;
|
|
31
|
+
};
|
|
32
|
+
/** What a change came to: the version it made (or that nothing changed), and how much. */
|
|
33
|
+
export type Written = {
|
|
34
|
+
document: string;
|
|
35
|
+
title: string;
|
|
36
|
+
version: number;
|
|
37
|
+
added: number;
|
|
38
|
+
removed: number;
|
|
39
|
+
unchanged?: true;
|
|
40
|
+
};
|
|
41
|
+
/** A title and text a document may have, or why not. */
|
|
42
|
+
export declare function checked(title: string, text: string): void;
|
|
43
|
+
/** The document, as whoever asks may read it; not there, or not theirs to see, it's `not_found`. */
|
|
44
|
+
export declare function load(db: Db | ReadContext["db"], id: string): Promise<Document>;
|
|
45
|
+
/**
|
|
46
|
+
* Writes `change` as `doc`'s next version, in the transaction that read
|
|
47
|
+
* `doc`. Nothing changed (the same title and text), nothing is written.
|
|
48
|
+
*/
|
|
49
|
+
export declare function commit(tx: TransactionScope, doc: Document, change: Change, author: Author): Promise<Written>;
|
|
50
|
+
/** Makes a document in `space`, as its first version. */
|
|
51
|
+
export declare function create(tx: TransactionScope, space: string, title: string, text: string, author: Author): Promise<Written>;
|
|
52
|
+
/** A rule's refusal to write somewhere ("out of your reach"), said plainly; others as they are. */
|
|
53
|
+
export declare function plainly(error: unknown): Error;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { versionId } from "../collections/document-version.js";
|
|
2
|
+
import { changes } from "./diff.js";
|
|
3
|
+
import { LIMIT, sizeOf, TITLE } from "./text.js";
|
|
4
|
+
/** An error with a code, as the runtime's are. */
|
|
5
|
+
export const fail = (message, code, more = {}) => Object.assign(new Error(message), { code, ...more });
|
|
6
|
+
/** A title and text a document may have, or why not. */
|
|
7
|
+
export function checked(title, text) {
|
|
8
|
+
if (!title.trim())
|
|
9
|
+
throw fail("A document needs a title.", "invalid");
|
|
10
|
+
if (title.length > TITLE)
|
|
11
|
+
throw fail(`A title is at most ${TITLE} characters.`, "invalid");
|
|
12
|
+
const size = sizeOf(text);
|
|
13
|
+
if (size > LIMIT) {
|
|
14
|
+
throw fail(`That's ${Math.ceil(size / 1024)} KB: a document holds at most ${LIMIT / 1024} KB. ` +
|
|
15
|
+
"Split it into more than one.", "too_large");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** The document, as whoever asks may read it; not there, or not theirs to see, it's `not_found`. */
|
|
19
|
+
export async function load(db, id) {
|
|
20
|
+
const doc = await db.collection("document").findOne({ _id: id });
|
|
21
|
+
if (!doc) {
|
|
22
|
+
throw fail("No such document: it may have been deleted, or it's in a space you're not in.", "not_found");
|
|
23
|
+
}
|
|
24
|
+
return doc;
|
|
25
|
+
}
|
|
26
|
+
/** Someone who may read a document but not change it. */
|
|
27
|
+
const readOnly = () => fail("You can read this document but not change it: an editor or a manager of its space can.", "forbidden");
|
|
28
|
+
/**
|
|
29
|
+
* Writes `change` as `doc`'s next version, in the transaction that read
|
|
30
|
+
* `doc`. Nothing changed (the same title and text), nothing is written.
|
|
31
|
+
*/
|
|
32
|
+
export async function commit(tx, doc, change, author) {
|
|
33
|
+
const { title, text } = change;
|
|
34
|
+
if (title === doc.title && text === doc.text) {
|
|
35
|
+
return {
|
|
36
|
+
document: doc._id,
|
|
37
|
+
title,
|
|
38
|
+
version: doc.version,
|
|
39
|
+
added: 0,
|
|
40
|
+
removed: 0,
|
|
41
|
+
unchanged: true,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
checked(title, text);
|
|
45
|
+
const version = doc.version + 1;
|
|
46
|
+
const at = new Date().toISOString();
|
|
47
|
+
const { added, removed } = changes(doc.text, text);
|
|
48
|
+
const saved = await tx.db.collection("document").updateOne({ _id: doc._id, version: doc.version }, {
|
|
49
|
+
$set: {
|
|
50
|
+
title,
|
|
51
|
+
text,
|
|
52
|
+
version,
|
|
53
|
+
size: sizeOf(text),
|
|
54
|
+
savedBy: author.by,
|
|
55
|
+
savedAt: at,
|
|
56
|
+
...(author.for ? { savedFor: author.for } : {}),
|
|
57
|
+
},
|
|
58
|
+
...(author.for || !doc.savedFor ? {} : { $unset: { savedFor: true } }),
|
|
59
|
+
});
|
|
60
|
+
if (!saved.matchedCount)
|
|
61
|
+
throw readOnly();
|
|
62
|
+
await tx.db.collection("documentVersion").insertOne({
|
|
63
|
+
_id: versionId(doc._id, version),
|
|
64
|
+
document: doc._id,
|
|
65
|
+
in: doc.in,
|
|
66
|
+
version,
|
|
67
|
+
title,
|
|
68
|
+
text,
|
|
69
|
+
by: author.by,
|
|
70
|
+
...(author.for ? { for: author.for } : {}),
|
|
71
|
+
at,
|
|
72
|
+
kind: change.kind,
|
|
73
|
+
...(change.merged ? { merged: true, base: change.base } : {}),
|
|
74
|
+
...(change.from !== undefined ? { from: change.from } : {}),
|
|
75
|
+
...(title !== doc.title ? { renamed: doc.title } : {}),
|
|
76
|
+
added,
|
|
77
|
+
removed,
|
|
78
|
+
});
|
|
79
|
+
return { document: doc._id, title, version, added, removed };
|
|
80
|
+
}
|
|
81
|
+
/** Makes a document in `space`, as its first version. */
|
|
82
|
+
export async function create(tx, space, title, text, author) {
|
|
83
|
+
checked(title, text);
|
|
84
|
+
const id = crypto.randomUUID();
|
|
85
|
+
const at = new Date().toISOString();
|
|
86
|
+
const { added } = changes("", text);
|
|
87
|
+
await tx.db.collection("document").insertOne({
|
|
88
|
+
_id: id,
|
|
89
|
+
in: space,
|
|
90
|
+
title,
|
|
91
|
+
text,
|
|
92
|
+
version: 1,
|
|
93
|
+
size: sizeOf(text),
|
|
94
|
+
createdBy: author.by,
|
|
95
|
+
createdAt: at,
|
|
96
|
+
savedBy: author.by,
|
|
97
|
+
savedAt: at,
|
|
98
|
+
...(author.for ? { savedFor: author.for } : {}),
|
|
99
|
+
});
|
|
100
|
+
await tx.db.collection("documentVersion").insertOne({
|
|
101
|
+
_id: versionId(id, 1),
|
|
102
|
+
document: id,
|
|
103
|
+
in: space,
|
|
104
|
+
version: 1,
|
|
105
|
+
title,
|
|
106
|
+
text,
|
|
107
|
+
by: author.by,
|
|
108
|
+
...(author.for ? { for: author.for } : {}),
|
|
109
|
+
at,
|
|
110
|
+
kind: "create",
|
|
111
|
+
added,
|
|
112
|
+
removed: 0,
|
|
113
|
+
});
|
|
114
|
+
return { document: id, title, version: 1, added, removed: 0 };
|
|
115
|
+
}
|
|
116
|
+
/** A rule's refusal to write somewhere ("out of your reach"), said plainly; others as they are. */
|
|
117
|
+
export function plainly(error) {
|
|
118
|
+
const e = error;
|
|
119
|
+
if (e?.code === "forbidden" && /out of (your )?reach/.test(String(e.message))) {
|
|
120
|
+
return fail("You can read what's in this space but not add to it: an editor or a manager of it can.", "forbidden");
|
|
121
|
+
}
|
|
122
|
+
return e;
|
|
123
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A change, as people read it: the lines taken out and put in, in the
|
|
3
|
+
* toolkit's `CodeBlock` tones (− removed, + added), with a few lines of
|
|
4
|
+
* what stayed around each and the rest folded ("12 unchanged lines").
|
|
5
|
+
*/
|
|
6
|
+
import type { ReactNode } from "react";
|
|
7
|
+
import { type CodePart } from "copilotz/web";
|
|
8
|
+
/** The change from `before` to `after`, as a code block's parts, `context` lines around each. */
|
|
9
|
+
export declare function diffParts(before: string, after: string, context?: number): CodePart[];
|
|
10
|
+
/** "+12 −3", in the tones of what was added and removed. */
|
|
11
|
+
export declare function Counts({ added, removed }: {
|
|
12
|
+
added: number;
|
|
13
|
+
removed: number;
|
|
14
|
+
}): ReactNode;
|
|
15
|
+
/** The change from `before` to `after`, in a code block titled `title`. */
|
|
16
|
+
export declare function Diff({ before, after, title, empty, height }: {
|
|
17
|
+
before: string;
|
|
18
|
+
after: string;
|
|
19
|
+
title?: ReactNode;
|
|
20
|
+
empty?: string;
|
|
21
|
+
height?: number;
|
|
22
|
+
}): ReactNode;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { CodeBlock } from "copilotz/web";
|
|
3
|
+
import { counted, diffLines, linesOf } from "../../shared/diff.js";
|
|
4
|
+
/** The change from `before` to `after`, as a code block's parts, `context` lines around each. */
|
|
5
|
+
export function diffParts(before, after, context = 3) {
|
|
6
|
+
const edits = diffLines(before ? linesOf(before) : [], after ? linesOf(after) : []);
|
|
7
|
+
const parts = [];
|
|
8
|
+
const line = (text, tone) => {
|
|
9
|
+
if (parts.length)
|
|
10
|
+
parts.push({ text: "\n" });
|
|
11
|
+
// Lines that stayed line up with the changed ones, past where their + and − go.
|
|
12
|
+
parts.push(tone === "added" || tone === "removed" ? { text, tone } : {
|
|
13
|
+
text: ` ${text}`,
|
|
14
|
+
...(tone ? { tone } : {}),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
let i = 0;
|
|
18
|
+
while (i < edits.length) {
|
|
19
|
+
const e = edits[i];
|
|
20
|
+
if (e.kind !== "same") {
|
|
21
|
+
line(e.text, e.kind);
|
|
22
|
+
i++;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
let j = i;
|
|
26
|
+
while (j < edits.length && edits[j].kind === "same")
|
|
27
|
+
j++;
|
|
28
|
+
const head = i === 0 ? 0 : context;
|
|
29
|
+
const tail = j === edits.length ? 0 : context;
|
|
30
|
+
if (j - i > head + tail + 1) {
|
|
31
|
+
for (let k = i; k < i + head; k++)
|
|
32
|
+
line(edits[k].text);
|
|
33
|
+
const folded = j - i - head - tail;
|
|
34
|
+
line(`⋯ ${folded} unchanged line${folded === 1 ? "" : "s"}`, "muted");
|
|
35
|
+
for (let k = j - tail; k < j; k++)
|
|
36
|
+
line(edits[k].text);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
for (let k = i; k < j; k++)
|
|
40
|
+
line(edits[k].text);
|
|
41
|
+
}
|
|
42
|
+
i = j;
|
|
43
|
+
}
|
|
44
|
+
return parts;
|
|
45
|
+
}
|
|
46
|
+
/** "+12 −3", in the tones of what was added and removed. */
|
|
47
|
+
export function Counts({ added, removed }) {
|
|
48
|
+
return (_jsxs("span", { className: "doc-counts", "aria-label": `${added} lines added, ${removed} removed`, children: [_jsxs("span", { className: "doc-added", children: ["+", added] }), _jsxs("span", { className: "doc-removed", children: ["\u2212", removed] })] }));
|
|
49
|
+
}
|
|
50
|
+
/** The change from `before` to `after`, in a code block titled `title`. */
|
|
51
|
+
export function Diff({ before, after, title, empty = "No changes to the text.", height }) {
|
|
52
|
+
const parts = before === after ? [] : diffParts(before, after);
|
|
53
|
+
const { added, removed } = before === after
|
|
54
|
+
? { added: 0, removed: 0 }
|
|
55
|
+
: counted(diffLines(before ? linesOf(before) : [], after ? linesOf(after) : []));
|
|
56
|
+
return (_jsx(CodeBlock, { className: "doc-diff", title: title, actions: before === after || !title ? undefined : _jsx(Counts, { added: added, removed: removed }), code: parts, empty: empty, label: typeof title === "string" ? title : "The changes", ...(height ? { height } : {}) }));
|
|
57
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When two saves changed the same lines: who saved what's there now, and
|
|
3
|
+
* when, what yours would change in theirs (a diff), and three ways on:
|
|
4
|
+
* keep yours (saved as a new version over theirs, which stays in History),
|
|
5
|
+
* take theirs (yours put aside, to bring back), or merge by hand: each
|
|
6
|
+
* place you both changed, side by side, to keep theirs, yours, both, or
|
|
7
|
+
* lines of your own; the rest is merged already. Nothing of yours is lost
|
|
8
|
+
* on the way: until it's saved, it's still your draft.
|
|
9
|
+
*/
|
|
10
|
+
import { type ReactNode } from "react";
|
|
11
|
+
import { type Shown } from "./people.js";
|
|
12
|
+
/** What's there now, as the conflict screen shows it. */
|
|
13
|
+
export type Theirs = {
|
|
14
|
+
text: string;
|
|
15
|
+
title: string;
|
|
16
|
+
version: number;
|
|
17
|
+
at: string;
|
|
18
|
+
by: Shown;
|
|
19
|
+
};
|
|
20
|
+
/** A version of the text and title. */
|
|
21
|
+
export type Sides = {
|
|
22
|
+
text: string;
|
|
23
|
+
title: string;
|
|
24
|
+
};
|
|
25
|
+
export declare function Conflict({ base, mine, theirs, busy, error, onKeep, onTake, onSave, onEdit }: {
|
|
26
|
+
/** What both started from. */
|
|
27
|
+
base: Sides & {
|
|
28
|
+
version: number;
|
|
29
|
+
};
|
|
30
|
+
mine: Sides;
|
|
31
|
+
theirs: Theirs;
|
|
32
|
+
busy: boolean;
|
|
33
|
+
error?: string;
|
|
34
|
+
onKeep(): void;
|
|
35
|
+
onTake(): void;
|
|
36
|
+
onSave(merged: Sides): void;
|
|
37
|
+
onEdit(merged: Sides): void;
|
|
38
|
+
}): ReactNode;
|