@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.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -2
  3. package/boards/actions/create-board.d.ts +1 -0
  4. package/boards/actions/create-board.js +47 -0
  5. package/boards/actions/create-card.d.ts +1 -0
  6. package/boards/actions/create-card.js +83 -0
  7. package/boards/actions/delete-board.d.ts +1 -0
  8. package/boards/actions/delete-board.js +30 -0
  9. package/boards/actions/delete-card.d.ts +1 -0
  10. package/boards/actions/delete-card.js +24 -0
  11. package/boards/actions/move-card.d.ts +1 -0
  12. package/boards/actions/move-card.js +85 -0
  13. package/boards/actions/update-board.d.ts +1 -0
  14. package/boards/actions/update-board.js +110 -0
  15. package/boards/actions/update-card.d.ts +1 -0
  16. package/boards/actions/update-card.js +67 -0
  17. package/boards/collections/board.d.ts +71 -0
  18. package/boards/collections/board.js +32 -0
  19. package/boards/collections/card.d.ts +74 -0
  20. package/boards/collections/card.js +29 -0
  21. package/boards/contract.d.ts +67 -0
  22. package/boards/contract.js +20 -0
  23. package/boards/mod.d.ts +34 -0
  24. package/boards/mod.js +72 -0
  25. package/boards/resources/tools.d.ts +2 -0
  26. package/boards/resources/tools.js +414 -0
  27. package/boards/shared/columns.d.ts +39 -0
  28. package/boards/shared/columns.js +88 -0
  29. package/boards/shared/dates.d.ts +11 -0
  30. package/boards/shared/dates.js +22 -0
  31. package/boards/shared/errors.d.ts +24 -0
  32. package/boards/shared/errors.js +20 -0
  33. package/boards/shared/fields.d.ts +65 -0
  34. package/boards/shared/fields.js +44 -0
  35. package/boards/shared/load.d.ts +22 -0
  36. package/boards/shared/load.js +32 -0
  37. package/boards/shared/order.d.ts +30 -0
  38. package/boards/shared/order.js +47 -0
  39. package/boards/shared/people.d.ts +30 -0
  40. package/boards/shared/people.js +87 -0
  41. package/boards/shared/said.d.ts +28 -0
  42. package/boards/shared/said.js +30 -0
  43. package/boards/views/web/board-head.d.ts +62 -0
  44. package/boards/views/web/board-head.js +170 -0
  45. package/boards/views/web/board-page.d.ts +17 -0
  46. package/boards/views/web/board-page.js +97 -0
  47. package/boards/views/web/board-view.d.ts +40 -0
  48. package/boards/views/web/board-view.js +341 -0
  49. package/boards/views/web/boards-tab.d.ts +13 -0
  50. package/boards/views/web/boards-tab.js +109 -0
  51. package/boards/views/web/card-sheet.d.ts +47 -0
  52. package/boards/views/web/card-sheet.js +314 -0
  53. package/boards/views/web/card-tile.d.ts +29 -0
  54. package/boards/views/web/card-tile.js +20 -0
  55. package/boards/views/web/column.d.ts +38 -0
  56. package/boards/views/web/column.js +171 -0
  57. package/boards/views/web/drag.d.ts +36 -0
  58. package/boards/views/web/drag.js +276 -0
  59. package/boards/views/web/glyphs.d.ts +29 -0
  60. package/boards/views/web/glyphs.js +25 -0
  61. package/boards/views/web/load.d.ts +32 -0
  62. package/boards/views/web/load.js +33 -0
  63. package/boards/views/web/palette.d.ts +15 -0
  64. package/boards/views/web/palette.js +180 -0
  65. package/boards/views/web/pickers.d.ts +45 -0
  66. package/boards/views/web/pickers.js +83 -0
  67. package/boards/views/web/shown.d.ts +77 -0
  68. package/boards/views/web/shown.js +136 -0
  69. package/boards/views/web/step.d.ts +13 -0
  70. package/boards/views/web/step.js +41 -0
  71. package/boards/views/web/style.d.ts +10 -0
  72. package/boards/views/web/style.js +380 -0
  73. package/boards/views/web/toolbar.d.ts +21 -0
  74. package/boards/views/web/toolbar.js +39 -0
  75. package/boards/web.d.ts +26 -0
  76. package/boards/web.js +52 -0
  77. package/contract.d.ts +7 -0
  78. package/contract.js +7 -0
  79. package/documents/actions/create.d.ts +1 -0
  80. package/documents/actions/create.js +28 -0
  81. package/documents/actions/delete.d.ts +1 -0
  82. package/documents/actions/delete.js +32 -0
  83. package/documents/actions/presence.d.ts +1 -0
  84. package/documents/actions/presence.js +45 -0
  85. package/documents/actions/rename.d.ts +1 -0
  86. package/documents/actions/rename.js +33 -0
  87. package/documents/actions/restore.d.ts +1 -0
  88. package/documents/actions/restore.js +39 -0
  89. package/documents/actions/save.d.ts +1 -0
  90. package/documents/actions/save.js +78 -0
  91. package/documents/collections/document-presence.d.ts +79 -0
  92. package/documents/collections/document-presence.js +25 -0
  93. package/documents/collections/document-version.d.ts +166 -0
  94. package/documents/collections/document-version.js +60 -0
  95. package/documents/collections/document.d.ts +131 -0
  96. package/documents/collections/document.js +72 -0
  97. package/documents/contract.d.ts +17 -0
  98. package/documents/contract.js +1 -0
  99. package/documents/mod.d.ts +38 -0
  100. package/documents/mod.js +75 -0
  101. package/documents/resources/tools.d.ts +2 -0
  102. package/documents/resources/tools.js +253 -0
  103. package/documents/shared/diff.d.ts +37 -0
  104. package/documents/shared/diff.js +148 -0
  105. package/documents/shared/merge.d.ts +29 -0
  106. package/documents/shared/merge.js +120 -0
  107. package/documents/shared/presence.d.ts +7 -0
  108. package/documents/shared/presence.js +7 -0
  109. package/documents/shared/text.d.ts +36 -0
  110. package/documents/shared/text.js +67 -0
  111. package/documents/shared/who.d.ts +23 -0
  112. package/documents/shared/who.js +39 -0
  113. package/documents/shared/write.d.ts +54 -0
  114. package/documents/shared/write.js +123 -0
  115. package/documents/views/web/changes.d.ts +22 -0
  116. package/documents/views/web/changes.js +57 -0
  117. package/documents/views/web/conflict.d.ts +38 -0
  118. package/documents/views/web/conflict.js +96 -0
  119. package/documents/views/web/draft.d.ts +18 -0
  120. package/documents/views/web/draft.js +32 -0
  121. package/documents/views/web/editor.d.ts +57 -0
  122. package/documents/views/web/editor.js +268 -0
  123. package/documents/views/web/format.d.ts +56 -0
  124. package/documents/views/web/format.js +245 -0
  125. package/documents/views/web/history.d.ts +23 -0
  126. package/documents/views/web/history.js +118 -0
  127. package/documents/views/web/list.d.ts +37 -0
  128. package/documents/views/web/list.js +211 -0
  129. package/documents/views/web/page.d.ts +35 -0
  130. package/documents/views/web/page.js +416 -0
  131. package/documents/views/web/palette.d.ts +16 -0
  132. package/documents/views/web/palette.js +168 -0
  133. package/documents/views/web/people.d.ts +60 -0
  134. package/documents/views/web/people.js +63 -0
  135. package/documents/views/web/presence.d.ts +30 -0
  136. package/documents/views/web/presence.js +75 -0
  137. package/documents/views/web/step.d.ts +25 -0
  138. package/documents/views/web/step.js +81 -0
  139. package/documents/views/web/style.d.ts +7 -0
  140. package/documents/views/web/style.js +266 -0
  141. package/documents/web.d.ts +18 -0
  142. package/documents/web.js +46 -0
  143. package/mod.d.ts +18 -0
  144. package/mod.js +18 -0
  145. package/package.json +61 -4
  146. package/web.d.ts +13 -0
  147. package/web.js +13 -0
@@ -0,0 +1,45 @@
1
+ /**
2
+ * `document.presence`: a page says who has a document open, and whether
3
+ * they're editing it (`open`, `editing`), every little while, and that
4
+ * they've gone (`gone`). Anyone who may read the document may say so for
5
+ * themselves; it's kept in memory only, and unrecorded (no events), so
6
+ * saying it often costs the log nothing. Entries no page has spoken for in
7
+ * a while go as anyone speaks.
8
+ */
9
+ import { defineAction, people } from "copilotz";
10
+ import { presenceId, STALE_MS } from "../collections/document-presence.js";
11
+ import { keyOf } from "../shared/text.js";
12
+ export const presenceAction = defineAction({
13
+ description: "Says who has a document open, and whether they're editing it",
14
+ record: false,
15
+ input: {
16
+ type: "object",
17
+ properties: {
18
+ document: { type: "string", minLength: 1 },
19
+ /** The page saying it: one of the person's tabs. */
20
+ page: { type: "string", pattern: "^[A-Za-z0-9_-]{1,40}$" },
21
+ state: { enum: ["open", "editing", "gone"] },
22
+ },
23
+ required: ["document", "page", "state"],
24
+ additionalProperties: false,
25
+ },
26
+ allow: people,
27
+ run: async (ctx, input) => {
28
+ const doc = await ctx.db.collection("document").findOne({ _id: input.document });
29
+ if (!doc)
30
+ return { present: false };
31
+ const who = keyOf(ctx.actor);
32
+ const _id = presenceId(doc._id, who, input.page);
33
+ const presence = ctx.db.collection("documentPresence");
34
+ if (input.state === "gone")
35
+ await presence.deleteOne({ _id });
36
+ else {
37
+ await presence.replaceOne({ _id }, { document: doc._id, in: doc.in, who, state: input.state, at: new Date().toISOString() }, { upsert: true });
38
+ }
39
+ await presence.deleteMany({
40
+ document: doc._id,
41
+ at: { $lt: new Date(Date.now() - STALE_MS).toISOString() },
42
+ });
43
+ return { present: input.state !== "gone" };
44
+ },
45
+ });
@@ -0,0 +1 @@
1
+ export declare const renameAction: import("copilotz").ActionDef;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * `document.rename`: a document's new title, as its next version (its text
3
+ * as it is), by someone who may change it (the rules say).
4
+ */
5
+ import { defineAction, people } from "copilotz";
6
+ import { keyOf, TITLE } from "../shared/text.js";
7
+ import { commit, fail, load, plainly } from "../shared/write.js";
8
+ export const renameAction = defineAction({
9
+ description: "Renames a document",
10
+ input: {
11
+ type: "object",
12
+ properties: {
13
+ document: { type: "string", minLength: 1 },
14
+ title: { type: "string", maxLength: TITLE },
15
+ },
16
+ required: ["document", "title"],
17
+ additionalProperties: false,
18
+ },
19
+ allow: people,
20
+ run: async (ctx, input) => {
21
+ const title = input.title.trim();
22
+ if (!title)
23
+ throw fail("A document needs a title.", "invalid");
24
+ return await ctx.transaction(async (tx) => {
25
+ const doc = await load(tx.db, input.document);
26
+ return await commit(tx, doc, { title, text: doc.text, kind: "rename" }, {
27
+ by: keyOf(ctx.actor),
28
+ });
29
+ }).catch((error) => {
30
+ throw plainly(error);
31
+ });
32
+ },
33
+ });
@@ -0,0 +1 @@
1
+ export declare const restoreAction: import("copilotz").ActionDef;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * `document.restore`: an old version brought back, as a new one (its title
3
+ * and text over what's there now; the versions between stay in History).
4
+ */
5
+ import { defineAction, people } from "copilotz";
6
+ import { versionId } from "../collections/document-version.js";
7
+ import { keyOf } from "../shared/text.js";
8
+ import { commit, fail, load, plainly } from "../shared/write.js";
9
+ export const restoreAction = defineAction({
10
+ description: "Brings back an old version of a document, as its next version",
11
+ input: {
12
+ type: "object",
13
+ properties: {
14
+ document: { type: "string", minLength: 1 },
15
+ version: { type: "integer", minimum: 1 },
16
+ },
17
+ required: ["document", "version"],
18
+ additionalProperties: false,
19
+ },
20
+ allow: people,
21
+ run: async (ctx, input) => {
22
+ return await ctx.transaction(async (tx) => {
23
+ const doc = await load(tx.db, input.document);
24
+ const old = await tx.db.collection("documentVersion").findOne({
25
+ _id: versionId(doc._id, input.version),
26
+ });
27
+ if (!old)
28
+ throw fail(`This document has no version ${input.version}.`, "not_found");
29
+ return await commit(tx, doc, {
30
+ title: old.title,
31
+ text: old.text,
32
+ kind: "restore",
33
+ from: input.version,
34
+ }, { by: keyOf(ctx.actor) });
35
+ }).catch((error) => {
36
+ throw plainly(error);
37
+ });
38
+ },
39
+ });
@@ -0,0 +1 @@
1
+ export declare const saveAction: import("copilotz").ActionDef;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * `document.save`: a person's save, as the document's next version. It says
3
+ * the version it started from (`base`). If someone saved since, the two
4
+ * changes are merged line by line against that version: changes to other
5
+ * lines are saved together (`merged`), and the merged text comes back;
6
+ * changes to the same lines (or two new titles) are refused with
7
+ * `conflict`, saying who saved what's there now, and nothing is written.
8
+ * The same title and text as now saves nothing (`unchanged`).
9
+ */
10
+ import { defineAction, people } from "copilotz";
11
+ import { namesFor } from "copilotz/plugins/access/contract";
12
+ import { versionId } from "../collections/document-version.js";
13
+ import { merge, mergeField } from "../shared/merge.js";
14
+ import { keyOf, LIMIT, TITLE, UNTITLED } from "../shared/text.js";
15
+ import { agentNamer, nameOf, peopleOf } from "../shared/who.js";
16
+ import { commit, fail, load, plainly } from "../shared/write.js";
17
+ export const saveAction = defineAction({
18
+ description: "Saves a document's new text (and title) as its next version, " +
19
+ "merging with what others saved since the version it started from",
20
+ input: {
21
+ type: "object",
22
+ properties: {
23
+ document: { type: "string", minLength: 1 },
24
+ /** The version the person started from. */
25
+ base: { type: "integer", minimum: 1 },
26
+ text: { type: "string", maxLength: 4 * LIMIT },
27
+ title: { type: "string", maxLength: TITLE },
28
+ },
29
+ required: ["document", "base", "text"],
30
+ additionalProperties: false,
31
+ },
32
+ allow: people,
33
+ run: async (ctx, input) => {
34
+ const author = { by: keyOf(ctx.actor) };
35
+ const asked = input.title === undefined ? undefined : input.title.trim() || UNTITLED;
36
+ return await ctx.transaction(async (tx) => {
37
+ const doc = await load(tx.db, input.document);
38
+ if (input.base > doc.version) {
39
+ throw fail(`There's no version ${input.base} yet: it's at ${doc.version}.`, "invalid");
40
+ }
41
+ let text = input.text;
42
+ let title = asked ?? doc.title;
43
+ const merging = input.base < doc.version;
44
+ if (merging) {
45
+ const base = await tx.db.collection("documentVersion").findOne({
46
+ _id: versionId(doc._id, input.base),
47
+ });
48
+ const merged = base ? merge(base.text, input.text, doc.text) : undefined;
49
+ const named = asked === undefined
50
+ ? doc.title
51
+ : base
52
+ ? mergeField(base.title, asked, doc.title)
53
+ : undefined;
54
+ if (!merged || merged.conflicts || named === undefined) {
55
+ const names = await namesFor({ resources: ctx.resources, db: tx.db }, peopleOf([doc.savedBy]));
56
+ const who = nameOf(doc.savedBy, agentNamer(ctx), names);
57
+ throw fail(`${who} saved version ${doc.version} since you started from version ` +
58
+ `${input.base}, changing the same ${merged?.conflicts ? "lines" : "title"}. ` +
59
+ "Nothing was saved: keep yours over theirs, take theirs, or merge them by hand.", "conflict", { version: doc.version, by: doc.savedBy, at: doc.savedAt });
60
+ }
61
+ text = merged.text;
62
+ title = named;
63
+ }
64
+ const written = await commit(tx, doc, {
65
+ title,
66
+ text,
67
+ kind: "save",
68
+ ...(merging ? { merged: true, base: input.base } : {}),
69
+ }, author);
70
+ return {
71
+ ...written,
72
+ ...(merging ? { merged: true, text, title } : {}),
73
+ };
74
+ }).catch((error) => {
75
+ throw plainly(error);
76
+ });
77
+ },
78
+ });
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Who has a document open right now, and who's editing it: one entry per
3
+ * page open (a person's two tabs are two), kept in the owner's memory only
4
+ * (`durable: false`: no store, no events, gone on restart). In the
5
+ * document's space, so whoever reads the document sees who's there. Each
6
+ * open page says it's still there every little while (`at`); one that
7
+ * stops saying so is gone a minute later, even if it never said goodbye.
8
+ */
9
+ import { type Infer } from "copilotz";
10
+ export { BEAT_MS, presenceId, STALE_MS } from "../shared/presence.js";
11
+ declare const presenceSchema: {
12
+ readonly type: "object";
13
+ readonly properties: {
14
+ readonly document: {
15
+ readonly type: "string";
16
+ };
17
+ readonly in: {
18
+ readonly type: "string";
19
+ };
20
+ /** Who: `person:ana`. */
21
+ readonly who: {
22
+ readonly type: "string";
23
+ };
24
+ /** `open`: reading it, or with nothing unsaved; `editing`: with changes not yet saved. */
25
+ readonly state: {
26
+ readonly enum: readonly ["open", "editing"];
27
+ };
28
+ /** When the page last said so. */
29
+ readonly at: {
30
+ readonly type: "string";
31
+ };
32
+ };
33
+ readonly required: readonly ["document", "in", "who", "state", "at"];
34
+ };
35
+ export type Presence = Infer<typeof presenceSchema> & {
36
+ _id: string;
37
+ };
38
+ export declare const documentPresence: {
39
+ schema: {
40
+ readonly type: "object";
41
+ readonly properties: {
42
+ readonly document: {
43
+ readonly type: "string";
44
+ };
45
+ readonly in: {
46
+ readonly type: "string";
47
+ };
48
+ /** Who: `person:ana`. */
49
+ readonly who: {
50
+ readonly type: "string";
51
+ };
52
+ /** `open`: reading it, or with nothing unsaved; `editing`: with changes not yet saved. */
53
+ readonly state: {
54
+ readonly enum: readonly ["open", "editing"];
55
+ };
56
+ /** When the page last said so. */
57
+ readonly at: {
58
+ readonly type: "string";
59
+ };
60
+ };
61
+ readonly required: readonly ["document", "in", "who", "state", "at"];
62
+ };
63
+ events?: boolean;
64
+ records?: boolean;
65
+ durable?: false;
66
+ indexes?: string[][];
67
+ private?: string[];
68
+ queries?: Record<string, import("copilotz").QueryDef<{
69
+ [x: string]: unknown;
70
+ at: string;
71
+ in: string;
72
+ document: string;
73
+ who: string;
74
+ state: "open" | "editing";
75
+ } & {
76
+ _id: string;
77
+ }>> | undefined;
78
+ vectors?: string[];
79
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Who has a document open right now, and who's editing it: one entry per
3
+ * page open (a person's two tabs are two), kept in the owner's memory only
4
+ * (`durable: false`: no store, no events, gone on restart). In the
5
+ * document's space, so whoever reads the document sees who's there. Each
6
+ * open page says it's still there every little while (`at`); one that
7
+ * stops saying so is gone a minute later, even if it never said goodbye.
8
+ */
9
+ import { defineCollection } from "copilotz";
10
+ export { BEAT_MS, presenceId, STALE_MS } from "../shared/presence.js";
11
+ const presenceSchema = {
12
+ type: "object",
13
+ properties: {
14
+ document: { type: "string" },
15
+ in: { type: "string" },
16
+ /** Who: `person:ana`. */
17
+ who: { type: "string" },
18
+ /** `open`: reading it, or with nothing unsaved; `editing`: with changes not yet saved. */
19
+ state: { enum: ["open", "editing"] },
20
+ /** When the page last said so. */
21
+ at: { type: "string" },
22
+ },
23
+ required: ["document", "in", "who", "state", "at"],
24
+ };
25
+ export const documentPresence = defineCollection({ durable: false, schema: presenceSchema });
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Every save of a document, as it was then: its `version`, title and text,
3
+ * who saved it and when (an agent, for whom), how (`kind`: made, saved,
4
+ * renamed, restored from an older one, or an agent's write, append or edit;
5
+ * `merged` when it was saved over someone else's changes, line by line), and
6
+ * what changed (`added` and `removed` lines). It lives in the document's
7
+ * space, so it's read and written as the document is; its `_id` is
8
+ * `<document>:<version>`, so a version is had once. A long text is stored
9
+ * once by its content (the runtime's blobs), so a version that keeps it
10
+ * costs little.
11
+ *
12
+ * Its query `history` (`{ document }`) lists them newest first, without
13
+ * their text: what a document's History shows.
14
+ */
15
+ import { type Infer } from "copilotz";
16
+ import type { Without } from "../shared/text.js";
17
+ export { versionId } from "../shared/text.js";
18
+ declare const versionSchema: {
19
+ readonly type: "object";
20
+ readonly properties: {
21
+ readonly document: {
22
+ readonly type: "string";
23
+ };
24
+ /** The document's space. */
25
+ readonly in: {
26
+ readonly type: "string";
27
+ };
28
+ readonly version: {
29
+ readonly type: "integer";
30
+ readonly minimum: 1;
31
+ };
32
+ readonly title: {
33
+ readonly type: "string";
34
+ };
35
+ readonly text: {
36
+ readonly type: "string";
37
+ };
38
+ /** Who saved it: `person:ana`, `agent:north`; for an agent, the person it worked for. */
39
+ readonly by: {
40
+ readonly type: "string";
41
+ };
42
+ readonly for: {
43
+ readonly type: "string";
44
+ };
45
+ readonly at: {
46
+ readonly type: "string";
47
+ };
48
+ readonly kind: {
49
+ readonly enum: readonly ["create", "save", "rename", "restore", "write", "append", "edit"];
50
+ };
51
+ /** Saved over others' changes since `base`, the version it started from, merged by line. */
52
+ readonly merged: {
53
+ readonly type: "boolean";
54
+ };
55
+ readonly base: {
56
+ readonly type: "integer";
57
+ };
58
+ /** Restored: the version it brought back. */
59
+ readonly from: {
60
+ readonly type: "integer";
61
+ };
62
+ /** Lines added and removed since the version before. */
63
+ readonly added: {
64
+ readonly type: "integer";
65
+ };
66
+ readonly removed: {
67
+ readonly type: "integer";
68
+ };
69
+ /** The title before, when this one changed it. */
70
+ readonly renamed: {
71
+ readonly type: "string";
72
+ };
73
+ };
74
+ readonly required: readonly ["document", "in", "version", "title", "text", "by", "at", "kind", "added", "removed"];
75
+ };
76
+ export type DocumentVersion = Infer<typeof versionSchema> & {
77
+ _id: string;
78
+ };
79
+ /** A version as History lists it: without its text. */
80
+ export type Saved = Without<DocumentVersion, "text">;
81
+ export declare const documentVersion: {
82
+ schema: {
83
+ readonly type: "object";
84
+ readonly properties: {
85
+ readonly document: {
86
+ readonly type: "string";
87
+ };
88
+ /** The document's space. */
89
+ readonly in: {
90
+ readonly type: "string";
91
+ };
92
+ readonly version: {
93
+ readonly type: "integer";
94
+ readonly minimum: 1;
95
+ };
96
+ readonly title: {
97
+ readonly type: "string";
98
+ };
99
+ readonly text: {
100
+ readonly type: "string";
101
+ };
102
+ /** Who saved it: `person:ana`, `agent:north`; for an agent, the person it worked for. */
103
+ readonly by: {
104
+ readonly type: "string";
105
+ };
106
+ readonly for: {
107
+ readonly type: "string";
108
+ };
109
+ readonly at: {
110
+ readonly type: "string";
111
+ };
112
+ readonly kind: {
113
+ readonly enum: readonly ["create", "save", "rename", "restore", "write", "append", "edit"];
114
+ };
115
+ /** Saved over others' changes since `base`, the version it started from, merged by line. */
116
+ readonly merged: {
117
+ readonly type: "boolean";
118
+ };
119
+ readonly base: {
120
+ readonly type: "integer";
121
+ };
122
+ /** Restored: the version it brought back. */
123
+ readonly from: {
124
+ readonly type: "integer";
125
+ };
126
+ /** Lines added and removed since the version before. */
127
+ readonly added: {
128
+ readonly type: "integer";
129
+ };
130
+ readonly removed: {
131
+ readonly type: "integer";
132
+ };
133
+ /** The title before, when this one changed it. */
134
+ readonly renamed: {
135
+ readonly type: "string";
136
+ };
137
+ };
138
+ readonly required: readonly ["document", "in", "version", "title", "text", "by", "at", "kind", "added", "removed"];
139
+ };
140
+ events?: boolean;
141
+ records?: boolean;
142
+ durable?: false;
143
+ indexes?: string[][];
144
+ private?: string[];
145
+ queries?: Record<string, import("copilotz").QueryDef<{
146
+ [x: string]: unknown;
147
+ from?: number | undefined;
148
+ for?: string | undefined;
149
+ merged?: boolean | undefined;
150
+ base?: number | undefined;
151
+ renamed?: string | undefined;
152
+ at: string;
153
+ title: string;
154
+ in: string;
155
+ by: string;
156
+ kind: "write" | "create" | "save" | "rename" | "restore" | "append" | "edit";
157
+ text: string;
158
+ version: number;
159
+ document: string;
160
+ added: number;
161
+ removed: number;
162
+ } & {
163
+ _id: string;
164
+ }>> | undefined;
165
+ vectors?: string[];
166
+ };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Every save of a document, as it was then: its `version`, title and text,
3
+ * who saved it and when (an agent, for whom), how (`kind`: made, saved,
4
+ * renamed, restored from an older one, or an agent's write, append or edit;
5
+ * `merged` when it was saved over someone else's changes, line by line), and
6
+ * what changed (`added` and `removed` lines). It lives in the document's
7
+ * space, so it's read and written as the document is; its `_id` is
8
+ * `<document>:<version>`, so a version is had once. A long text is stored
9
+ * once by its content (the runtime's blobs), so a version that keeps it
10
+ * costs little.
11
+ *
12
+ * Its query `history` (`{ document }`) lists them newest first, without
13
+ * their text: what a document's History shows.
14
+ */
15
+ import { defineCollection, people } from "copilotz";
16
+ export { versionId } from "../shared/text.js";
17
+ const versionSchema = {
18
+ type: "object",
19
+ properties: {
20
+ document: { type: "string" },
21
+ /** The document's space. */
22
+ in: { type: "string" },
23
+ version: { type: "integer", minimum: 1 },
24
+ title: { type: "string" },
25
+ text: { type: "string" },
26
+ /** Who saved it: `person:ana`, `agent:north`; for an agent, the person it worked for. */
27
+ by: { type: "string" },
28
+ for: { type: "string" },
29
+ at: { type: "string" },
30
+ kind: {
31
+ enum: ["create", "save", "rename", "restore", "write", "append", "edit"],
32
+ },
33
+ /** Saved over others' changes since `base`, the version it started from, merged by line. */
34
+ merged: { type: "boolean" },
35
+ base: { type: "integer" },
36
+ /** Restored: the version it brought back. */
37
+ from: { type: "integer" },
38
+ /** Lines added and removed since the version before. */
39
+ added: { type: "integer" },
40
+ removed: { type: "integer" },
41
+ /** The title before, when this one changed it. */
42
+ renamed: { type: "string" },
43
+ },
44
+ required: ["document", "in", "version", "title", "text", "by", "at", "kind", "added", "removed"],
45
+ };
46
+ export const documentVersion = defineCollection({
47
+ schema: versionSchema,
48
+ indexes: [["document", "version"], ["in"]],
49
+ queries: {
50
+ history: {
51
+ allow: people,
52
+ run: async (versions, params) => {
53
+ if (typeof params?.document !== "string")
54
+ return [];
55
+ const found = await versions.find({ document: params.document }, { sort: { version: -1 }, ...(params.limit ? { limit: Number(params.limit) } : {}) });
56
+ return found.map(({ text: _, ...v }) => v);
57
+ },
58
+ },
59
+ },
60
+ });
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Documents: Markdown that lives in a space (`in`), shared like its chats,
3
+ * with its title and text as they are now and the version they're at (one
4
+ * more with each save; every version is a `documentVersion`), who saved it
5
+ * last and when, and who made it and when. Its queries, for front ends:
6
+ *
7
+ * - `list`: a space's documents (`{ in }`), latest saved first, each with
8
+ * how it opens (`excerpt`) instead of its whole text;
9
+ * - `recent`: the latest saved anywhere the asker reaches (`{ limit }`),
10
+ * said the same way: what the command palette goes to.
11
+ */
12
+ import { type Infer } from "copilotz";
13
+ import { type Without } from "../shared/text.js";
14
+ declare const documentSchema: {
15
+ readonly type: "object";
16
+ readonly properties: {
17
+ /** The space it's in. */
18
+ readonly in: {
19
+ readonly type: "string";
20
+ };
21
+ readonly title: {
22
+ readonly type: "string";
23
+ };
24
+ /** Markdown. */
25
+ readonly text: {
26
+ readonly type: "string";
27
+ };
28
+ /** Which version it's at: 1 when made, one more with each save. */
29
+ readonly version: {
30
+ readonly type: "integer";
31
+ readonly minimum: 1;
32
+ };
33
+ /** Its text's size, in bytes. */
34
+ readonly size: {
35
+ readonly type: "integer";
36
+ };
37
+ /** Who made it (`person:ana`, `agent:north`), and when. */
38
+ readonly createdBy: {
39
+ readonly type: "string";
40
+ };
41
+ readonly createdAt: {
42
+ readonly type: "string";
43
+ };
44
+ /** Who saved it last, and when; for an agent, the person it worked for (`savedFor`). */
45
+ readonly savedBy: {
46
+ readonly type: "string";
47
+ };
48
+ readonly savedAt: {
49
+ readonly type: "string";
50
+ };
51
+ readonly savedFor: {
52
+ readonly type: "string";
53
+ };
54
+ };
55
+ readonly required: readonly ["in", "title", "text", "version", "size", "createdBy", "createdAt", "savedBy", "savedAt"];
56
+ };
57
+ export type Document = Infer<typeof documentSchema> & {
58
+ _id: string;
59
+ };
60
+ /** A document as a list shows it: everything but its text, and how it opens. */
61
+ export type Listed = Without<Document, "text"> & {
62
+ excerpt: string;
63
+ };
64
+ export declare const listed: ({ text, ...doc }: Document) => Listed;
65
+ export declare const document: {
66
+ schema: {
67
+ readonly type: "object";
68
+ readonly properties: {
69
+ /** The space it's in. */
70
+ readonly in: {
71
+ readonly type: "string";
72
+ };
73
+ readonly title: {
74
+ readonly type: "string";
75
+ };
76
+ /** Markdown. */
77
+ readonly text: {
78
+ readonly type: "string";
79
+ };
80
+ /** Which version it's at: 1 when made, one more with each save. */
81
+ readonly version: {
82
+ readonly type: "integer";
83
+ readonly minimum: 1;
84
+ };
85
+ /** Its text's size, in bytes. */
86
+ readonly size: {
87
+ readonly type: "integer";
88
+ };
89
+ /** Who made it (`person:ana`, `agent:north`), and when. */
90
+ readonly createdBy: {
91
+ readonly type: "string";
92
+ };
93
+ readonly createdAt: {
94
+ readonly type: "string";
95
+ };
96
+ /** Who saved it last, and when; for an agent, the person it worked for (`savedFor`). */
97
+ readonly savedBy: {
98
+ readonly type: "string";
99
+ };
100
+ readonly savedAt: {
101
+ readonly type: "string";
102
+ };
103
+ readonly savedFor: {
104
+ readonly type: "string";
105
+ };
106
+ };
107
+ readonly required: readonly ["in", "title", "text", "version", "size", "createdBy", "createdAt", "savedBy", "savedAt"];
108
+ };
109
+ events?: boolean;
110
+ records?: boolean;
111
+ durable?: false;
112
+ indexes?: string[][];
113
+ private?: string[];
114
+ queries?: Record<string, import("copilotz").QueryDef<{
115
+ [x: string]: unknown;
116
+ savedFor?: string | undefined;
117
+ title: string;
118
+ in: string;
119
+ text: string;
120
+ version: number;
121
+ size: number;
122
+ createdBy: string;
123
+ createdAt: string;
124
+ savedBy: string;
125
+ savedAt: string;
126
+ } & {
127
+ _id: string;
128
+ }>> | undefined;
129
+ vectors?: string[];
130
+ };
131
+ export {};