@copilotz/work 0.0.0-stage → 1.0.0-rc.35
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,72 @@
|
|
|
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 { defineCollection, people } from "copilotz";
|
|
13
|
+
import { excerptOf } from "../shared/text.js";
|
|
14
|
+
const documentSchema = {
|
|
15
|
+
type: "object",
|
|
16
|
+
properties: {
|
|
17
|
+
/** The space it's in. */
|
|
18
|
+
in: { type: "string" },
|
|
19
|
+
title: { type: "string" },
|
|
20
|
+
/** Markdown. */
|
|
21
|
+
text: { type: "string" },
|
|
22
|
+
/** Which version it's at: 1 when made, one more with each save. */
|
|
23
|
+
version: { type: "integer", minimum: 1 },
|
|
24
|
+
/** Its text's size, in bytes. */
|
|
25
|
+
size: { type: "integer" },
|
|
26
|
+
/** Who made it (`person:ana`, `agent:north`), and when. */
|
|
27
|
+
createdBy: { type: "string" },
|
|
28
|
+
createdAt: { type: "string" },
|
|
29
|
+
/** Who saved it last, and when; for an agent, the person it worked for (`savedFor`). */
|
|
30
|
+
savedBy: { type: "string" },
|
|
31
|
+
savedAt: { type: "string" },
|
|
32
|
+
savedFor: { type: "string" },
|
|
33
|
+
},
|
|
34
|
+
required: [
|
|
35
|
+
"in",
|
|
36
|
+
"title",
|
|
37
|
+
"text",
|
|
38
|
+
"version",
|
|
39
|
+
"size",
|
|
40
|
+
"createdBy",
|
|
41
|
+
"createdAt",
|
|
42
|
+
"savedBy",
|
|
43
|
+
"savedAt",
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
export const listed = ({ text, ...doc }) => ({
|
|
47
|
+
...doc,
|
|
48
|
+
excerpt: excerptOf(text, doc.title),
|
|
49
|
+
});
|
|
50
|
+
export const document = defineCollection({
|
|
51
|
+
schema: documentSchema,
|
|
52
|
+
indexes: [["in"], ["in", "savedAt"], ["savedAt"]],
|
|
53
|
+
queries: {
|
|
54
|
+
list: {
|
|
55
|
+
allow: people,
|
|
56
|
+
run: async (docs, params) => {
|
|
57
|
+
if (typeof params?.in !== "string")
|
|
58
|
+
return [];
|
|
59
|
+
const found = await docs.find({ in: params.in }, { sort: { savedAt: -1 } });
|
|
60
|
+
return found.map((d) => listed(d));
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
recent: {
|
|
64
|
+
allow: people,
|
|
65
|
+
run: async (docs, params = {}) => {
|
|
66
|
+
const limit = Math.min(Math.max(Number(params?.limit) || 30, 1), 100);
|
|
67
|
+
const found = await docs.find({}, { sort: { savedAt: -1 }, limit });
|
|
68
|
+
return found.map((d) => listed(d));
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The documents plugin's contract: what other code may import, on either
|
|
3
|
+
* side: the shapes of a document (`Document`; as a list shows it,
|
|
4
|
+
* `Listed`), of each version (`DocumentVersion`; as History lists it,
|
|
5
|
+
* `Saved`) and of who has one open (`Presence`), and its addresses
|
|
6
|
+
* (`documentPath`, `documentsTab`) and limits. The rest is reached by name:
|
|
7
|
+
* its collections (`document`, `documentVersion`, `documentPresence`), its
|
|
8
|
+
* queries (`document.list`, `document.recent`, `documentVersion.history`),
|
|
9
|
+
* its Actions (`document.create`, `document.save`, `document.rename`,
|
|
10
|
+
* `document.delete`, `document.restore`, `document.presence`) and its
|
|
11
|
+
* tools (`list_documents`, `read_document`, `write_document`,
|
|
12
|
+
* `append_document`, `edit_document`).
|
|
13
|
+
*/
|
|
14
|
+
export type { Document, Listed } from "./collections/document.js";
|
|
15
|
+
export type { DocumentVersion, Saved } from "./collections/document-version.js";
|
|
16
|
+
export type { Presence } from "./collections/document-presence.js";
|
|
17
|
+
export { documentPath, documentsTab, LIMIT, TITLE } from "./shared/text.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { documentPath, documentsTab, LIMIT, TITLE } from "./shared/text.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Documents: shared Markdown in a space, for people and agents — the
|
|
3
|
+
* canonical artifact of the thing being made, and its working memory.
|
|
4
|
+
*
|
|
5
|
+
* - **Every save is a version** (`documentVersion`): who changed what and
|
|
6
|
+
* when, and how many lines; an old one comes back as a new one
|
|
7
|
+
* (`document.restore`).
|
|
8
|
+
* - **Two at once:** a save says the version it started from (`base`). If
|
|
9
|
+
* someone saved since, the changes are merged line by line against it:
|
|
10
|
+
* saved together where they touch different lines, refused with
|
|
11
|
+
* `conflict` where they touch the same ones, for the person to settle.
|
|
12
|
+
* - **Before it happens:** who has a document open, and who's editing it,
|
|
13
|
+
* is live (`documentPresence`, kept in memory only), on its page.
|
|
14
|
+
* - **People's Actions:** `document.create`, `document.save`,
|
|
15
|
+
* `document.rename`, `document.delete`, `document.restore`, and
|
|
16
|
+
* `document.presence` (open, editing, gone).
|
|
17
|
+
* - **Agents' tools,** wherever a conversation is in a space:
|
|
18
|
+
* `list_documents`, `read_document`, `write_document`,
|
|
19
|
+
* `append_document`, `edit_document`. Each change reads and writes in one
|
|
20
|
+
* transaction, so agents never conflict, and it's a version like any.
|
|
21
|
+
* - **Shared in spaces** (spaces' `shared` resource): the access plugin's
|
|
22
|
+
* rules keep every read and write to what the actor reaches; a viewer
|
|
23
|
+
* reads, an editor writes, and anyone who reads a document may say
|
|
24
|
+
* they're there. Nothing outside writes them directly (the REST API, a
|
|
25
|
+
* browser's socket): only the Actions keep every save a version, by whom
|
|
26
|
+
* it was.
|
|
27
|
+
*
|
|
28
|
+
* Its front end is `documentsWeb` (web.tsx); what other code may import,
|
|
29
|
+
* contract.ts.
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* plugins: [models, chat, ...access, documents]
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import { type Plugin } from "copilotz";
|
|
36
|
+
export * from "./contract.js";
|
|
37
|
+
export declare const documents: Plugin;
|
|
38
|
+
export default documents;
|
package/documents/mod.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Documents: shared Markdown in a space, for people and agents — the
|
|
3
|
+
* canonical artifact of the thing being made, and its working memory.
|
|
4
|
+
*
|
|
5
|
+
* - **Every save is a version** (`documentVersion`): who changed what and
|
|
6
|
+
* when, and how many lines; an old one comes back as a new one
|
|
7
|
+
* (`document.restore`).
|
|
8
|
+
* - **Two at once:** a save says the version it started from (`base`). If
|
|
9
|
+
* someone saved since, the changes are merged line by line against it:
|
|
10
|
+
* saved together where they touch different lines, refused with
|
|
11
|
+
* `conflict` where they touch the same ones, for the person to settle.
|
|
12
|
+
* - **Before it happens:** who has a document open, and who's editing it,
|
|
13
|
+
* is live (`documentPresence`, kept in memory only), on its page.
|
|
14
|
+
* - **People's Actions:** `document.create`, `document.save`,
|
|
15
|
+
* `document.rename`, `document.delete`, `document.restore`, and
|
|
16
|
+
* `document.presence` (open, editing, gone).
|
|
17
|
+
* - **Agents' tools,** wherever a conversation is in a space:
|
|
18
|
+
* `list_documents`, `read_document`, `write_document`,
|
|
19
|
+
* `append_document`, `edit_document`. Each change reads and writes in one
|
|
20
|
+
* transaction, so agents never conflict, and it's a version like any.
|
|
21
|
+
* - **Shared in spaces** (spaces' `shared` resource): the access plugin's
|
|
22
|
+
* rules keep every read and write to what the actor reaches; a viewer
|
|
23
|
+
* reads, an editor writes, and anyone who reads a document may say
|
|
24
|
+
* they're there. Nothing outside writes them directly (the REST API, a
|
|
25
|
+
* browser's socket): only the Actions keep every save a version, by whom
|
|
26
|
+
* it was.
|
|
27
|
+
*
|
|
28
|
+
* Its front end is `documentsWeb` (web.tsx); what other code may import,
|
|
29
|
+
* contract.ts.
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* plugins: [models, chat, ...access, documents]
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import { definePlugin, failure } from "copilotz";
|
|
36
|
+
import { createAction } from "./actions/create.js";
|
|
37
|
+
import { deleteAction } from "./actions/delete.js";
|
|
38
|
+
import { presenceAction } from "./actions/presence.js";
|
|
39
|
+
import { renameAction } from "./actions/rename.js";
|
|
40
|
+
import { restoreAction } from "./actions/restore.js";
|
|
41
|
+
import { saveAction } from "./actions/save.js";
|
|
42
|
+
import { document } from "./collections/document.js";
|
|
43
|
+
import { documentPresence } from "./collections/document-presence.js";
|
|
44
|
+
import { documentVersion } from "./collections/document-version.js";
|
|
45
|
+
import { tools } from "./resources/tools.js";
|
|
46
|
+
export * from "./contract.js";
|
|
47
|
+
/** Its collections. */
|
|
48
|
+
const OWN = ["document", "documentVersion", "documentPresence"];
|
|
49
|
+
export const documents = definePlugin({
|
|
50
|
+
name: "documents",
|
|
51
|
+
requires: ["chat", "spaces"],
|
|
52
|
+
collections: { document, documentVersion, documentPresence },
|
|
53
|
+
actions: {
|
|
54
|
+
"document.create": createAction,
|
|
55
|
+
"document.save": saveAction,
|
|
56
|
+
"document.rename": renameAction,
|
|
57
|
+
"document.delete": deleteAction,
|
|
58
|
+
"document.restore": restoreAction,
|
|
59
|
+
"document.presence": presenceAction,
|
|
60
|
+
},
|
|
61
|
+
resources: {
|
|
62
|
+
// In spaces: read where the space is read, changed where it's written; presence, where read.
|
|
63
|
+
shared: { document: {}, documentVersion: {}, documentPresence: { write: "read" } },
|
|
64
|
+
tools,
|
|
65
|
+
},
|
|
66
|
+
// Only the Actions change them: a write from outside (`direct`) would be a save with no
|
|
67
|
+
// version, a version by someone else, or someone else's presence.
|
|
68
|
+
guard: (_actor, { collection, access, direct }) => {
|
|
69
|
+
if (OWN.includes(collection) && direct && access === "write") {
|
|
70
|
+
throw failure("forbidden", "Documents change through their Actions.", { public: true });
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
export default documents;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Documents, for agents: the space's documents, offered wherever a
|
|
3
|
+
* conversation is in a space. `list_documents`, `read_document` (by title
|
|
4
|
+
* or id; an old version too), `write_document` (make one, or replace one
|
|
5
|
+
* whole), `append_document`, and `edit_document` (replace text that occurs
|
|
6
|
+
* exactly once). Each write reads and writes in one transaction, so an
|
|
7
|
+
* agent never conflicts; it's a version like a person's save, attributed
|
|
8
|
+
* to the agent (and the person it works for). Answers are short, and link
|
|
9
|
+
* the document (`[Title](/document/<id>)`), so agents link it too.
|
|
10
|
+
*/
|
|
11
|
+
import { namesFor } from "copilotz/plugins/access/contract";
|
|
12
|
+
import { tool } from "copilotz/plugins/agents/contract";
|
|
13
|
+
import { versionId } from "../collections/document-version.js";
|
|
14
|
+
import { excerptOf, forOf, kb, keyOf, LIMIT, TITLE } from "../shared/text.js";
|
|
15
|
+
import { agentNamer, nameOf, peopleOf } from "../shared/who.js";
|
|
16
|
+
import { commit, create } from "../shared/write.js";
|
|
17
|
+
const inSpace = (_agent, thread) => !!thread.in;
|
|
18
|
+
const author = (ctx) => {
|
|
19
|
+
const person = forOf(ctx.actor);
|
|
20
|
+
return { by: keyOf({ kind: "agent", id: ctx.agent }), ...(person ? { for: person } : {}) };
|
|
21
|
+
};
|
|
22
|
+
/** A document as a link, for an answer: `[Launch plan](/document/<id>)`. */
|
|
23
|
+
const link = (doc) => `[${doc.title.replace(/[[\]]/g, "")}](/document/${doc._id ?? doc.document})`;
|
|
24
|
+
/** "2026-10-29 21:04" from an ISO time. */
|
|
25
|
+
const when = (at) => at.slice(0, 16).replace("T", " ");
|
|
26
|
+
/** Text as it's matched by title: any case, any accents, spaces as one. */
|
|
27
|
+
const fold = (text) => text.normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/\s+/g, " ").trim().toLowerCase();
|
|
28
|
+
/**
|
|
29
|
+
* The document an agent names, by its id (anywhere it may read) or its
|
|
30
|
+
* title (in this space: exactly, else the one title that has it); or why
|
|
31
|
+
* none, in words for the agent.
|
|
32
|
+
*/
|
|
33
|
+
async function named(db, space, name) {
|
|
34
|
+
const asked = name.trim().replace(/^\/document\//, "").replace(/[?#].*$/, "");
|
|
35
|
+
const byId = await db.collection("document").findOne({ _id: asked });
|
|
36
|
+
if (byId)
|
|
37
|
+
return byId;
|
|
38
|
+
const here = await db.collection("document").find({ in: space });
|
|
39
|
+
const exact = here.filter((d) => fold(d.title) === fold(name));
|
|
40
|
+
const near = exact.length ? exact : here.filter((d) => fold(d.title).includes(fold(name)));
|
|
41
|
+
if (near.length === 1)
|
|
42
|
+
return near[0];
|
|
43
|
+
if (near.length > 1) {
|
|
44
|
+
return `More than one document here is called that: ${near.map((d) => `${link(d)} (id ${d._id})`).join(", ")}. Name it by its id.`;
|
|
45
|
+
}
|
|
46
|
+
return `No document "${name}" here.` +
|
|
47
|
+
(here.length
|
|
48
|
+
? ` These are: ${here.slice(0, 20).map((d) => `“${d.title}” (id ${d._id})`).join(", ")}.`
|
|
49
|
+
: " There are none yet: write_document makes one.");
|
|
50
|
+
}
|
|
51
|
+
/** What an error says to an agent: the runtime's words, which say what would unblock it. */
|
|
52
|
+
const said = (error) => {
|
|
53
|
+
const e = error;
|
|
54
|
+
if (e?.code === "forbidden") {
|
|
55
|
+
return "You can read documents in this space but not change them: you work here as a " +
|
|
56
|
+
"viewer. Tell the person, so a manager can make you an editor.";
|
|
57
|
+
}
|
|
58
|
+
return e instanceof Error ? e.message : String(error);
|
|
59
|
+
};
|
|
60
|
+
/** How a write ends, for the agent: what it made, and how much changed. */
|
|
61
|
+
function answer(w, made = false) {
|
|
62
|
+
if (w.unchanged)
|
|
63
|
+
return `No changes: ${link(w)} is already that (version ${w.version}).`;
|
|
64
|
+
const lines = (n) => `${n} line${n === 1 ? "" : "s"}`;
|
|
65
|
+
return made
|
|
66
|
+
? `Made ${link(w)} (version 1, ${lines(w.added)}).`
|
|
67
|
+
: `Saved ${link(w)} as version ${w.version}: +${w.added} −${w.removed} lines.`;
|
|
68
|
+
}
|
|
69
|
+
/** Changes a named document in one transaction (read, then written): `change` says how. */
|
|
70
|
+
async function change(ctx, name, how, kind) {
|
|
71
|
+
const space = ctx.in;
|
|
72
|
+
if (!space)
|
|
73
|
+
return "This conversation isn't in a space: there are no documents here.";
|
|
74
|
+
try {
|
|
75
|
+
return await ctx.transaction(async (tx) => {
|
|
76
|
+
const doc = await named(tx.db, space, name);
|
|
77
|
+
if (typeof doc === "string")
|
|
78
|
+
return doc;
|
|
79
|
+
const next = how(doc);
|
|
80
|
+
if (typeof next === "string")
|
|
81
|
+
return next;
|
|
82
|
+
return answer(await commit(tx, doc, { title: next.title ?? doc.title, text: next.text, kind }, author(ctx)));
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return said(error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const listDocuments = tool({
|
|
90
|
+
offer: inSpace,
|
|
91
|
+
description: "List this space's documents, latest saved first: each one's title (a link to " +
|
|
92
|
+
"it), version, size, who saved it last and when, and how it opens. Read one with " +
|
|
93
|
+
"read_document before you change it.",
|
|
94
|
+
input: { type: "object", properties: {} },
|
|
95
|
+
run: async (_, ctx) => {
|
|
96
|
+
const space = ctx.in;
|
|
97
|
+
if (!space)
|
|
98
|
+
return "This conversation isn't in a space: there are no documents here.";
|
|
99
|
+
const docs = await ctx.db.collection("document").find({ in: space }, {
|
|
100
|
+
sort: { savedAt: -1 },
|
|
101
|
+
});
|
|
102
|
+
if (!docs.length)
|
|
103
|
+
return "No documents here yet. write_document makes one.";
|
|
104
|
+
const name = agentNamer(ctx);
|
|
105
|
+
const names = await namesFor(ctx, peopleOf(docs.map((d) => d.savedBy)));
|
|
106
|
+
return `Documents here (${docs.length}), latest saved first:\n` + docs.map((d) => {
|
|
107
|
+
const opens = excerptOf(d.text, d.title, 160);
|
|
108
|
+
return [
|
|
109
|
+
`- ${link(d)} · id ${d._id} · version ${d.version} · ${kb(d.size)} · saved by ` +
|
|
110
|
+
`${nameOf(d.savedBy, name, names)} ${when(d.savedAt)}`,
|
|
111
|
+
...(opens ? [` opens: ${opens}`] : []),
|
|
112
|
+
].join("\n");
|
|
113
|
+
}).join("\n");
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const readDocument = tool({
|
|
117
|
+
offer: inSpace,
|
|
118
|
+
description: "Read a document of this space, by its title or id: its text (Markdown), with " +
|
|
119
|
+
"its version and who saved it. With version, an older one, as it was then.",
|
|
120
|
+
input: {
|
|
121
|
+
type: "object",
|
|
122
|
+
properties: {
|
|
123
|
+
document: { type: "string", minLength: 1, description: "Its title or id." },
|
|
124
|
+
version: { type: "integer", minimum: 1, description: "An older version (default: now)." },
|
|
125
|
+
},
|
|
126
|
+
required: ["document"],
|
|
127
|
+
},
|
|
128
|
+
run: async ({ document, version }, ctx) => {
|
|
129
|
+
const space = ctx.in;
|
|
130
|
+
if (!space)
|
|
131
|
+
return "This conversation isn't in a space: there are no documents here.";
|
|
132
|
+
const doc = await named(ctx.db, space, document);
|
|
133
|
+
if (typeof doc === "string")
|
|
134
|
+
return doc;
|
|
135
|
+
const name = agentNamer(ctx);
|
|
136
|
+
const old = version && version !== doc.version
|
|
137
|
+
? await ctx.db.collection("documentVersion").findOne({
|
|
138
|
+
_id: versionId(doc._id, version),
|
|
139
|
+
})
|
|
140
|
+
: undefined;
|
|
141
|
+
if (version && version !== doc.version && !old) {
|
|
142
|
+
return `${link(doc)} has no version ${version}: it's at version ${doc.version}.`;
|
|
143
|
+
}
|
|
144
|
+
const shown = old
|
|
145
|
+
? { title: old.title, text: old.text, by: old.by, at: old.at }
|
|
146
|
+
: { title: doc.title, text: doc.text, by: doc.savedBy, at: doc.savedAt };
|
|
147
|
+
const names = await namesFor(ctx, peopleOf([shown.by]));
|
|
148
|
+
const head = old
|
|
149
|
+
? `${link({ ...doc, title: shown.title })} · version ${version} of ${doc.version}, an old ` +
|
|
150
|
+
`one (read it without version for now) · saved by ${nameOf(shown.by, name, names)} ` +
|
|
151
|
+
when(shown.at)
|
|
152
|
+
: `${link(doc)} · version ${doc.version} · ${kb(doc.size)} · saved by ` +
|
|
153
|
+
`${nameOf(shown.by, name, names)} ${when(shown.at)}`;
|
|
154
|
+
return `${head}\n\n${shown.text.trim() ? shown.text : "(It's empty.)"}`;
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
const writeDocument = tool({
|
|
158
|
+
offer: inSpace,
|
|
159
|
+
description: "Make a new document in this space (title and text), or, naming one with " +
|
|
160
|
+
"document, replace its whole text (and title, if given). To change part of one, prefer " +
|
|
161
|
+
"edit_document or append_document: they keep what others wrote. Markdown.",
|
|
162
|
+
input: {
|
|
163
|
+
type: "object",
|
|
164
|
+
properties: {
|
|
165
|
+
title: { type: "string", maxLength: TITLE, description: "Its title." },
|
|
166
|
+
text: { type: "string", maxLength: 4 * LIMIT, description: "Its whole text, in Markdown." },
|
|
167
|
+
document: {
|
|
168
|
+
type: "string",
|
|
169
|
+
description: "An existing document to replace (its title or id). Leave out to make one.",
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
required: ["text"],
|
|
173
|
+
},
|
|
174
|
+
run: async ({ title, text, document }, ctx) => {
|
|
175
|
+
if (document) {
|
|
176
|
+
return await change(ctx, document, () => ({ title: title?.trim() || undefined, text }), "write");
|
|
177
|
+
}
|
|
178
|
+
const space = ctx.in;
|
|
179
|
+
if (!space)
|
|
180
|
+
return "This conversation isn't in a space: there's nowhere to write.";
|
|
181
|
+
const name = title?.trim();
|
|
182
|
+
if (!name)
|
|
183
|
+
return "A new document needs a title.";
|
|
184
|
+
try {
|
|
185
|
+
return await ctx.transaction(async (tx) => {
|
|
186
|
+
const taken = (await tx.db.collection("document").find({ in: space }))
|
|
187
|
+
.find((d) => fold(d.title) === fold(name));
|
|
188
|
+
if (taken) {
|
|
189
|
+
return `There's a document called that here already: ${link(taken)} (id ` +
|
|
190
|
+
`${taken._id}, version ${taken.version}). Read it, then change it with ` +
|
|
191
|
+
"edit_document or append_document, or replace it whole with write_document and " +
|
|
192
|
+
"document; or give the new one another title.";
|
|
193
|
+
}
|
|
194
|
+
return answer(await create(tx, space, name, text, author(ctx)), true);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
return said(error);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
const appendDocument = tool({
|
|
203
|
+
offer: inSpace,
|
|
204
|
+
description: "Add text to the end of a document of this space (by its title or id), after a " +
|
|
205
|
+
"blank line. Markdown.",
|
|
206
|
+
input: {
|
|
207
|
+
type: "object",
|
|
208
|
+
properties: {
|
|
209
|
+
document: { type: "string", minLength: 1, description: "Its title or id." },
|
|
210
|
+
text: { type: "string", minLength: 1, maxLength: 4 * LIMIT },
|
|
211
|
+
},
|
|
212
|
+
required: ["document", "text"],
|
|
213
|
+
},
|
|
214
|
+
run: ({ document, text }, ctx) => change(ctx, document, (doc) => {
|
|
215
|
+
const was = doc.text.replace(/\s+$/, "");
|
|
216
|
+
return { text: was ? `${was}\n\n${text.replace(/^\n+/, "")}` : text };
|
|
217
|
+
}, "append"),
|
|
218
|
+
});
|
|
219
|
+
const editDocument = tool({
|
|
220
|
+
offer: inSpace,
|
|
221
|
+
description: "Change part of a document of this space (by its title or id): replace text " +
|
|
222
|
+
"that occurs exactly once in it (old, copied exactly, with enough around it to be " +
|
|
223
|
+
"unique) with new text. Read it first.",
|
|
224
|
+
input: {
|
|
225
|
+
type: "object",
|
|
226
|
+
properties: {
|
|
227
|
+
document: { type: "string", minLength: 1, description: "Its title or id." },
|
|
228
|
+
old: { type: "string", minLength: 1, description: "The text to replace, exactly." },
|
|
229
|
+
new: { type: "string", description: "What goes in its place." },
|
|
230
|
+
},
|
|
231
|
+
required: ["document", "old", "new"],
|
|
232
|
+
},
|
|
233
|
+
run: ({ document, old, new: next }, ctx) => change(ctx, document, (doc) => {
|
|
234
|
+
const at = doc.text.indexOf(old);
|
|
235
|
+
if (at < 0) {
|
|
236
|
+
return `The text to replace isn't in ${link(doc)}: read it again (read_document) and ` +
|
|
237
|
+
"copy it exactly.";
|
|
238
|
+
}
|
|
239
|
+
if (doc.text.indexOf(old, at + 1) >= 0) {
|
|
240
|
+
const times = doc.text.split(old).length - 1;
|
|
241
|
+
return `The text to replace occurs ${times > 1 ? `${times} times` : "more than once"} ` +
|
|
242
|
+
`in ${link(doc)}: give more of what's around it.`;
|
|
243
|
+
}
|
|
244
|
+
return { text: doc.text.slice(0, at) + next + doc.text.slice(at + old.length) };
|
|
245
|
+
}, "edit"),
|
|
246
|
+
});
|
|
247
|
+
export const tools = {
|
|
248
|
+
list_documents: listDocuments,
|
|
249
|
+
read_document: readDocument,
|
|
250
|
+
write_document: writeDocument,
|
|
251
|
+
append_document: appendDocument,
|
|
252
|
+
edit_document: editDocument,
|
|
253
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Texts compared line by line: which lines stayed, which were added and
|
|
3
|
+
* which removed (Myers' algorithm, after the common start and end are set
|
|
4
|
+
* aside), how many of each, and the changes as hunks of the first text.
|
|
5
|
+
* Pure, so the server (versions, merges) and the browser (diffs, the
|
|
6
|
+
* conflict screen) say the same thing.
|
|
7
|
+
*/
|
|
8
|
+
/** A text's lines: split where it breaks, so joining them with "\n" gives it back exactly. */
|
|
9
|
+
export declare const linesOf: (text: string) => string[];
|
|
10
|
+
/** One line of a comparison: in both, only in the second (`added`), or only in the first. */
|
|
11
|
+
export type Edit = {
|
|
12
|
+
kind: "same" | "added" | "removed";
|
|
13
|
+
text: string;
|
|
14
|
+
};
|
|
15
|
+
/** A change to the first text: its lines `start` to `end` (not included) became `lines`. */
|
|
16
|
+
export type Hunk = {
|
|
17
|
+
start: number;
|
|
18
|
+
end: number;
|
|
19
|
+
lines: string[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The shortest way from `a` to `b`, line by line: the lines both keep, in
|
|
23
|
+
* order, and what goes and comes between them.
|
|
24
|
+
*/
|
|
25
|
+
export declare function diffLines(a: string[], b: string[]): Edit[];
|
|
26
|
+
/** How many lines a change added and removed. */
|
|
27
|
+
export declare function counted(edits: Edit[]): {
|
|
28
|
+
added: number;
|
|
29
|
+
removed: number;
|
|
30
|
+
};
|
|
31
|
+
/** How many lines `after` adds to `before`, and how many it takes away (an empty text has none). */
|
|
32
|
+
export declare const changes: (before: string, after: string) => {
|
|
33
|
+
added: number;
|
|
34
|
+
removed: number;
|
|
35
|
+
};
|
|
36
|
+
/** The changes from `a` to `b` as hunks of `a`, in order, none touching another. */
|
|
37
|
+
export declare function hunksOf(a: string[], b: string[]): Hunk[];
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Texts compared line by line: which lines stayed, which were added and
|
|
3
|
+
* which removed (Myers' algorithm, after the common start and end are set
|
|
4
|
+
* aside), how many of each, and the changes as hunks of the first text.
|
|
5
|
+
* Pure, so the server (versions, merges) and the browser (diffs, the
|
|
6
|
+
* conflict screen) say the same thing.
|
|
7
|
+
*/
|
|
8
|
+
/** A text's lines: split where it breaks, so joining them with "\n" gives it back exactly. */
|
|
9
|
+
export const linesOf = (text) => text.split("\n");
|
|
10
|
+
/** Past this many differences, the middle counts as replaced whole: a diff stays fast and small. */
|
|
11
|
+
const MOST = 2000;
|
|
12
|
+
/**
|
|
13
|
+
* The shortest way from `a` to `b`, line by line: the lines both keep, in
|
|
14
|
+
* order, and what goes and comes between them.
|
|
15
|
+
*/
|
|
16
|
+
export function diffLines(a, b) {
|
|
17
|
+
let start = 0;
|
|
18
|
+
while (start < a.length && start < b.length && a[start] === b[start])
|
|
19
|
+
start++;
|
|
20
|
+
let endA = a.length;
|
|
21
|
+
let endB = b.length;
|
|
22
|
+
while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
|
|
23
|
+
endA--;
|
|
24
|
+
endB--;
|
|
25
|
+
}
|
|
26
|
+
const same = (from, i, j) => from.slice(i, j).map((text) => ({ kind: "same", text }));
|
|
27
|
+
return [
|
|
28
|
+
...same(a, 0, start),
|
|
29
|
+
...middle(a.slice(start, endA), b.slice(start, endB)),
|
|
30
|
+
...same(a, endA, a.length),
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
/** Myers' greedy diff of what differs, its lines compared as numbers. */
|
|
34
|
+
function middle(a, b) {
|
|
35
|
+
const n = a.length;
|
|
36
|
+
const m = b.length;
|
|
37
|
+
if (!n)
|
|
38
|
+
return b.map((text) => ({ kind: "added", text }));
|
|
39
|
+
if (!m)
|
|
40
|
+
return a.map((text) => ({ kind: "removed", text }));
|
|
41
|
+
const ids = new Map();
|
|
42
|
+
const id = (line) => {
|
|
43
|
+
let known = ids.get(line);
|
|
44
|
+
if (known === undefined)
|
|
45
|
+
ids.set(line, known = ids.size);
|
|
46
|
+
return known;
|
|
47
|
+
};
|
|
48
|
+
const x = Int32Array.from(a, id);
|
|
49
|
+
const y = Int32Array.from(b, id);
|
|
50
|
+
const max = Math.min(n + m, MOST);
|
|
51
|
+
const offset = max + 1;
|
|
52
|
+
const v = new Int32Array(2 * max + 3);
|
|
53
|
+
// Each round's furthest points, kept to walk back from the end.
|
|
54
|
+
const trace = [];
|
|
55
|
+
let found = -1;
|
|
56
|
+
for (let d = 0; d <= max && found < 0; d++) {
|
|
57
|
+
trace.push(v.slice(offset - d - 1, offset + d + 2));
|
|
58
|
+
for (let k = -d; k <= d; k += 2) {
|
|
59
|
+
let i = k === -d || (k !== d && v[offset + k - 1] < v[offset + k + 1])
|
|
60
|
+
? v[offset + k + 1]
|
|
61
|
+
: v[offset + k - 1] + 1;
|
|
62
|
+
let j = i - k;
|
|
63
|
+
while (i < n && j < m && x[i] === y[j]) {
|
|
64
|
+
i++;
|
|
65
|
+
j++;
|
|
66
|
+
}
|
|
67
|
+
v[offset + k] = i;
|
|
68
|
+
if (i >= n && j >= m) {
|
|
69
|
+
found = d;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (found < 0) {
|
|
75
|
+
return [
|
|
76
|
+
...a.map((text) => ({ kind: "removed", text })),
|
|
77
|
+
...b.map((text) => ({ kind: "added", text })),
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
// Walk back through the rounds, from the end to the start.
|
|
81
|
+
const out = [];
|
|
82
|
+
let i = n;
|
|
83
|
+
let j = m;
|
|
84
|
+
for (let d = found; d > 0; d--) {
|
|
85
|
+
const before = trace[d];
|
|
86
|
+
// `before` holds round d's starting points: diagonals -d-1 … d+1 of the round before.
|
|
87
|
+
const at = (k) => before[k + d + 1];
|
|
88
|
+
const k = i - j;
|
|
89
|
+
const down = k === -d || (k !== d && at(k - 1) < at(k + 1));
|
|
90
|
+
const prevK = down ? k + 1 : k - 1;
|
|
91
|
+
const prevI = at(prevK);
|
|
92
|
+
const prevJ = prevI - prevK;
|
|
93
|
+
while (i > prevI + (down ? 0 : 1) && j > prevJ + (down ? 1 : 0)) {
|
|
94
|
+
out.push({ kind: "same", text: a[--i] });
|
|
95
|
+
j--;
|
|
96
|
+
}
|
|
97
|
+
if (down)
|
|
98
|
+
out.push({ kind: "added", text: b[--j] });
|
|
99
|
+
else
|
|
100
|
+
out.push({ kind: "removed", text: a[--i] });
|
|
101
|
+
}
|
|
102
|
+
while (i > 0 && j > 0) {
|
|
103
|
+
out.push({ kind: "same", text: a[--i] });
|
|
104
|
+
j--;
|
|
105
|
+
}
|
|
106
|
+
return out.reverse();
|
|
107
|
+
}
|
|
108
|
+
/** How many lines a change added and removed. */
|
|
109
|
+
export function counted(edits) {
|
|
110
|
+
let added = 0;
|
|
111
|
+
let removed = 0;
|
|
112
|
+
for (const e of edits) {
|
|
113
|
+
if (e.kind === "added")
|
|
114
|
+
added++;
|
|
115
|
+
else if (e.kind === "removed")
|
|
116
|
+
removed++;
|
|
117
|
+
}
|
|
118
|
+
return { added, removed };
|
|
119
|
+
}
|
|
120
|
+
/** How many lines `after` adds to `before`, and how many it takes away (an empty text has none). */
|
|
121
|
+
export const changes = (before, after) => before === after
|
|
122
|
+
? { added: 0, removed: 0 }
|
|
123
|
+
: counted(diffLines(before ? linesOf(before) : [], after ? linesOf(after) : []));
|
|
124
|
+
/** The changes from `a` to `b` as hunks of `a`, in order, none touching another. */
|
|
125
|
+
export function hunksOf(a, b) {
|
|
126
|
+
const hunks = [];
|
|
127
|
+
let at = 0;
|
|
128
|
+
let open;
|
|
129
|
+
for (const e of diffLines(a, b)) {
|
|
130
|
+
if (e.kind === "same") {
|
|
131
|
+
if (open)
|
|
132
|
+
hunks.push(open);
|
|
133
|
+
open = undefined;
|
|
134
|
+
at++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
open ??= { start: at, end: at, lines: [] };
|
|
138
|
+
if (e.kind === "removed") {
|
|
139
|
+
at++;
|
|
140
|
+
open.end = at;
|
|
141
|
+
}
|
|
142
|
+
else
|
|
143
|
+
open.lines.push(e.text);
|
|
144
|
+
}
|
|
145
|
+
if (open)
|
|
146
|
+
hunks.push(open);
|
|
147
|
+
return hunks;
|
|
148
|
+
}
|