agentic-documents 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/README.md +64 -4
- package/dist/chunk-FZA5LQV3.js +150 -0
- package/dist/chunk-GYQFTF2E.js +389 -0
- package/dist/chunk-IU3YIQ4Y.js +232 -0
- package/dist/chunk-R5XUTBC3.js +177 -0
- package/dist/{chunk-BSXUNQDR.js → chunk-TFCRPEEM.js} +1 -1
- package/dist/chunk-YBBQNCMC.js +744 -0
- package/dist/cli.js +47 -11
- package/dist/index.d.ts +4 -4
- package/dist/index.js +10 -8
- package/dist/live-worker.d.ts +20 -0
- package/dist/live-worker.js +27 -0
- package/dist/live.d.ts +73 -0
- package/dist/live.js +12 -0
- package/dist/{mcp-2VZIGHIX.js → mcp-P2C75IIG.js} +4 -2
- package/dist/{service-eYISo6-b.d.ts → service-BKkxEnaJ.d.ts} +24 -2
- package/dist/worker.d.ts +19 -6
- package/dist/worker.js +11 -158
- package/package.json +13 -2
- package/dist/chunk-4YVWD5PH.js +0 -1123
- package/dist/chunk-LRPLNXOF.js +0 -67
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Breaking changes
|
|
6
|
+
|
|
7
|
+
The API of this package does not change. The change is in the dependency.
|
|
8
|
+
|
|
9
|
+
- **Dependencies.** `agentic-identity` moves from `^0.3.0` to `^0.4.0`. In 0.4 a grant is keyed on the
|
|
10
|
+
principal kind and the id, so a person and an agent with the same id hold separate roles. Grants
|
|
11
|
+
written by 0.3 are not honored until you run `migrateGrants`. A host that shares documents through
|
|
12
|
+
`createSharing` must pass `{ kind, id }` to `grant` and `revokeGrant`.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- Tests that a person and an agent with the same id get separate roles, and that `deleteResource`
|
|
17
|
+
removes the grants.
|
|
18
|
+
- This package has no grant call and no command that deletes a whole document or erases a person. A
|
|
19
|
+
host that deletes a document must call `deleteResource` for it, and must delete the document and its
|
|
20
|
+
catalog entry itself. If `deleteResource` fails, run it again. If it throws `EraseRefusedError`,
|
|
21
|
+
remove the child resources or the extra owners first. Do not reuse the document id until it
|
|
22
|
+
succeeds. The `agentic-identity` person erase does not remove document content, and author names
|
|
23
|
+
stay in the edit log, the threads, and the suggestions.
|
|
24
|
+
|
|
25
|
+
## 0.2.0
|
|
26
|
+
|
|
27
|
+
### Breaking changes
|
|
28
|
+
|
|
29
|
+
- **Worker deployments from the repo template.** `wrangler.jsonc` now sets `main` to
|
|
30
|
+
`src/live-worker.ts`. To host live rooms, add the `SYNC_ROOM` Durable Object binding and the `v1`
|
|
31
|
+
migration (`new_sqlite_classes`). Without the binding, the room routes fail with `MISCONFIGURED`.
|
|
32
|
+
The published package does not include `wrangler.jsonc`.
|
|
33
|
+
- **Token table.** An `AGENTIC_DOCS_TOKENS` entry in object form may now carry a `role`. A `role` that
|
|
34
|
+
is not `editor` or `viewer` refuses the whole token table, so a typo never leaves a token live with
|
|
35
|
+
the wrong role. Entries with no `role` stay editors, as before.
|
|
36
|
+
- **Dependencies.** `agentic-identity` moves from `^0.2.0` to `^0.3.0`.
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
|
|
40
|
+
- Live rooms. Several people and agents edit one document at the same time. Import `LiveDoc` from
|
|
41
|
+
`agentic-documents/live`. Deploy `agentic-documents/live-worker` to host the rooms.
|
|
42
|
+
- The `agentic-realtime` dependency (`^0.1.0`, the version on npm) for the live rooms.
|
|
43
|
+
- `--live <url>` on the CLI and the MCP server, or `AGENTIC_DOCS_LIVE_URL`. Set `AGENTIC_DOCS_TOKEN`
|
|
44
|
+
to join a room. The server sets the author kind and the role from the token.
|
|
45
|
+
- With `--live`, the CLI refuses `--as`, `--scopes`, and `--dir`, and exits 2. The server sets these
|
|
46
|
+
from the token.
|
|
47
|
+
- A `viewer` role. A viewer reads and cannot write, over HTTP or in a live room. The default role is
|
|
48
|
+
`editor`.
|
|
49
|
+
- Worker routes for live rooms: `GET /api/v1/me`, `GET /api/v1/docs/:id/room`,
|
|
50
|
+
`GET /api/v1/docs/:id/ws`, `GET /api/v1/docs/:id/state`, and `POST /api/v1/docs/:id/checkpoint`.
|
|
51
|
+
- Live rooms save through the shared `Store` and `Blobs` ports. A stored document too big for a room
|
|
52
|
+
fails the join with a clear error.
|
|
53
|
+
- `npm pack` and `npm publish` build `dist/` first (`prepack`).
|
|
54
|
+
|
|
55
|
+
## 0.1.0
|
|
56
|
+
|
|
57
|
+
- First release. The document model, the Markdown and ODT export, the text operations, the CLI, the
|
|
58
|
+
MCP server, and the Worker HTTP API.
|
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
Write documents with people and agents in real time.
|
|
9
9
|
|
|
10
|
-
> Status: early, version 0.
|
|
11
|
-
>
|
|
10
|
+
> Status: early, version 0.3.0. The document model, the text operations, the CLI, the MCP server,
|
|
11
|
+
> the Worker API, and live editing in a room work. The web UI is not built yet.
|
|
12
12
|
|
|
13
13
|
## Install
|
|
14
14
|
|
|
@@ -130,7 +130,60 @@ curl "$URL/api/v1/docs/plan/export?format=html" -H "authorization: Bearer $TOKEN
|
|
|
130
130
|
its alt text, because a remote image tells its host who opened the file. `remoteImages` turns
|
|
131
131
|
that back on.
|
|
132
132
|
- Every edit, message, and suggestion needs an author. The edit log keeps the kind and the name, so
|
|
133
|
-
agent work stays labeled. The log holds no content.
|
|
133
|
+
agent work stays labeled. The log holds no content. Over the CLI, MCP, and HTTP the server or
|
|
134
|
+
the operator sets the author. In a live room a client writes its own log entries, suggestion decisions, and
|
|
135
|
+
threads. A save merges them into the stored document. So after a live client saves, treat all three
|
|
136
|
+
as advisory. The room's own record of who sent updates in the room is at `GET /api/v1/docs/:id/room`. It does not cover
|
|
137
|
+
text written over HTTP, the CLI, or MCP before the room opened: the client that seeds the room appears there.
|
|
138
|
+
|
|
139
|
+
## Live editing
|
|
140
|
+
|
|
141
|
+
A person and an agent can edit one document at the same time. Each sees the other's changes. The Worker
|
|
142
|
+
hosts one room per document, on a Durable Object, through [agentic-realtime](https://www.npmjs.com/package/agentic-realtime).
|
|
143
|
+
Deploy `agentic-documents/live-worker`. The sample `wrangler.jsonc` binds `SYNC_ROOM` and sets the migration.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { LiveDoc } from "agentic-documents/live";
|
|
147
|
+
|
|
148
|
+
const live = await LiveDoc.join({ server: "https://docs.example.com", id: "plan", token });
|
|
149
|
+
await live.edit((doc, author) => doc.append("Ship on Friday.", author));
|
|
150
|
+
console.log(live.peers); // the other people and agents. `machine` is true for an agent.
|
|
151
|
+
await live.save(); // write the room state to the stored document
|
|
152
|
+
await live.leave();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The CLI and the MCP server join rooms too. Every command then runs in the room and saves:
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
export AGENTIC_DOCS_TOKEN=... # a bearer token. The server sets the author kind and the role.
|
|
159
|
+
export AGENTIC_DOCS_LIVE_URL=https://docs.example.com # or pass --live <url>
|
|
160
|
+
npx agentic-documents append plan --content "Ship on Friday." --live https://docs.example.com
|
|
161
|
+
npx agentic-documents mcp --live https://docs.example.com
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
- The server reads your author kind and role from the token (`GET /api/v1/me`). An agent token writes
|
|
165
|
+
agent edits. `--as`, `--scopes`, and `--dir` do not apply with `--live`: the CLI refuses them and exits 2. This
|
|
166
|
+
holds when `AGENTIC_DOCS_LIVE_URL` is set too, so unset it for a local call.
|
|
167
|
+
- A room starts from the stored document. `save` merges the room back into the stored document, so
|
|
168
|
+
`read`, `export`, and the other commands see live edits. A save also creates a document that has none.
|
|
169
|
+
The CLI and MCP server save after each change. If a save fails, the room keeps the change and the error says so: do not repeat it. `new` makes the document in the store, and a room picks it up on the next join. A document over 1,000,000 bytes cannot open in a room or be saved from one. A save refuses an update with a wrong
|
|
170
|
+
kind of content or a malformed thread, suggestion, or log entry. HTTP edits reach an open room only when a client joins and seeds it.
|
|
171
|
+
- A `viewer` token joins and reads. The room drops its updates, and `edit` and `save` fail with `FORBIDDEN`.
|
|
172
|
+
Set a role in `AGENTIC_DOCS_TOKENS` with `{ "as": "human:Vic", "role": "viewer" }`. A token with no role is an editor.
|
|
173
|
+
- `GET /api/v1/docs/:id/room` lists who sent updates in the room, and whether each is machine-made.
|
|
174
|
+
- Pass `reconnect: {}` to `join` to keep trying after a drop. Edits made offline merge on return.
|
|
175
|
+
A room that restarts recovers its state from the Durable Object.
|
|
176
|
+
|
|
177
|
+
Limits of live editing:
|
|
178
|
+
|
|
179
|
+
- The edit log, the suggestion decisions, and the threads live in the document, and a client writes them.
|
|
180
|
+
A modified client can write a log entry with another author, or accept its own suggestion. The room's
|
|
181
|
+
list at `/room` is the record to trust for who sent updates. It does not cover text written before the
|
|
182
|
+
room opened.
|
|
183
|
+
- Until a client saves, a command over HTTP reads the stored copy, not the room.
|
|
184
|
+
- After the room refuses an edit, the local copy of `LiveDoc` still holds it. Join again.
|
|
185
|
+
- Durable Objects have no US jurisdiction. With the region `us` a room sits near the first client that opens it.
|
|
186
|
+
With `eu` it stays in the EU. The local `wrangler dev` cannot place rooms.
|
|
134
187
|
|
|
135
188
|
## Rules for text matching
|
|
136
189
|
|
|
@@ -142,12 +195,19 @@ curl "$URL/api/v1/docs/plan/export?format=html" -H "authorization: Bearer $TOKEN
|
|
|
142
195
|
## Limits
|
|
143
196
|
|
|
144
197
|
- No web UI yet.
|
|
198
|
+
- A document holds no roles of its own. The token table gives `editor` or `viewer`. To share a document
|
|
199
|
+
by person, use `createSharing` from `agentic-identity` 0.4. A grant names its subject by kind
|
|
200
|
+
(`human` or `agent`) and id, so a person and an agent with the same id hold separate roles. When you
|
|
201
|
+
delete a document, call `deleteResource` on its resource so its grants do not outlive it. If
|
|
202
|
+
`deleteResource` fails, treat the delete as failed and run it again. If it throws
|
|
203
|
+
`EraseRefusedError`, remove the child resources or the extra owners first. Do not reuse the id
|
|
204
|
+
until it succeeds. This package has no command that deletes a whole document.
|
|
145
205
|
- The Worker keeps each document in R2 and a catalog (who made it, and when) in D1. Both come from the
|
|
146
206
|
shared `agentic-identity` ports. A save is conditional on the version it read, so two writers cannot
|
|
147
207
|
overwrite each other. The CLI and MCP server keep the same data in local files.
|
|
148
208
|
- The CLI and MCP server keep files under `<dir>/blobs` and `<dir>/store`. Files from an older release that sit
|
|
149
209
|
directly in `<dir>` are not read.
|
|
150
|
-
- Token auth is a stopgap until the `Auth` port exists. There is no per-document sharing
|
|
210
|
+
- Token auth is a stopgap until the `Auth` port exists. A token has one role for the whole server: `editor` (the default) or `viewer`. A viewer cannot call a write command or write in a live room. There is no per-document sharing yet.
|
|
151
211
|
- A save is a compare-and-set. The file store takes a lock file, checks the version, and renames a
|
|
152
212
|
uniquely named temp file into place. A writer that loses loads the document again and runs
|
|
153
213
|
its change again. A lock older than 10 seconds counts as dead and is taken over. A lock that
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgenticDoc,
|
|
3
|
+
DocError,
|
|
4
|
+
PRINCIPAL_KINDS,
|
|
5
|
+
Suggestions,
|
|
6
|
+
Threads
|
|
7
|
+
} from "./chunk-YBBQNCMC.js";
|
|
8
|
+
|
|
9
|
+
// src/service.ts
|
|
10
|
+
import * as Y from "yjs";
|
|
11
|
+
var ATTEMPTS = 64;
|
|
12
|
+
var ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
13
|
+
var CATALOG = "documents";
|
|
14
|
+
var MAX_LIVE_BYTES = 1e6;
|
|
15
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
16
|
+
var isString = (v) => typeof v === "string";
|
|
17
|
+
var isWho = (v) => isObject(v) && PRINCIPAL_KINDS.includes(v.kind) && isString(v.name) && v.name !== "";
|
|
18
|
+
var isAnchor = (v) => isObject(v) && isString(v.text) && isObject(v.start) && isObject(v.end);
|
|
19
|
+
var ROOTS = /* @__PURE__ */ new Set(["default", "edits", "suggestions", "threads", "applied"]);
|
|
20
|
+
var DocsService = class {
|
|
21
|
+
constructor(ports, options = {}) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.blobs = ports.blobs;
|
|
24
|
+
this.store = ports.store;
|
|
25
|
+
}
|
|
26
|
+
options;
|
|
27
|
+
blobs;
|
|
28
|
+
store;
|
|
29
|
+
key(id) {
|
|
30
|
+
if (!ID.test(id)) throw new DocError("INVALID_INPUT", `invalid document id "${id}": use 1 to 64 of a-z, 0-9, "-" and "_"`);
|
|
31
|
+
return `${id}.ydoc`;
|
|
32
|
+
}
|
|
33
|
+
async create(id, markdown, author) {
|
|
34
|
+
const key = this.key(id);
|
|
35
|
+
const created = await this.blobs.put(key, AgenticDoc.fromMarkdown(markdown, author, this.options).encode(), { ifNoneMatch: "*" });
|
|
36
|
+
if (!created) throw new DocError("DOCUMENT_EXISTS", `document "${id}" already exists`);
|
|
37
|
+
await this.catalog(id, author);
|
|
38
|
+
}
|
|
39
|
+
/** The stored document as a Yjs update. A live client seeds its room from it. */
|
|
40
|
+
async state(id) {
|
|
41
|
+
const found = await this.blobs.get(this.key(id));
|
|
42
|
+
if (!found) throw new DocError("NO_DOCUMENT", `no document with id "${id}"`);
|
|
43
|
+
return found.data;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Merge a Yjs update from a live room into the stored document, and create the document when it
|
|
47
|
+
* has none. A merge is safe to repeat and to run in any order. The save is conditional, so two
|
|
48
|
+
* checkpoints at once both land.
|
|
49
|
+
*/
|
|
50
|
+
async checkpoint(id, update, author) {
|
|
51
|
+
const key = this.key(id);
|
|
52
|
+
const scratch = new Y.Doc();
|
|
53
|
+
try {
|
|
54
|
+
Y.applyUpdate(scratch, update);
|
|
55
|
+
} catch {
|
|
56
|
+
throw new DocError("INVALID_INPUT", "the update is not a Yjs update");
|
|
57
|
+
}
|
|
58
|
+
if (scratch.store.clients.size === 0 || scratch.store.pendingStructs || scratch.store.pendingDs) {
|
|
59
|
+
throw new DocError("INVALID_INPUT", "the update is empty or lacks the changes it depends on");
|
|
60
|
+
}
|
|
61
|
+
for (const root of scratch.share.keys()) if (!ROOTS.has(root)) throw new DocError("INVALID_INPUT", `the update holds a part this package does not use: "${root}"`);
|
|
62
|
+
for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
|
|
63
|
+
const found = await this.blobs.get(key);
|
|
64
|
+
if (!found) {
|
|
65
|
+
const created = AgenticDoc.fromUpdate(update, this.options);
|
|
66
|
+
const fresh = this.checked(id, created);
|
|
67
|
+
this.fits(id, fresh);
|
|
68
|
+
if (!await this.blobs.put(key, fresh, { ifNoneMatch: "*" })) continue;
|
|
69
|
+
await this.catalog(id, author);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const doc = AgenticDoc.fromUpdate(found.data, this.options);
|
|
73
|
+
doc.applyUpdate(update);
|
|
74
|
+
const merged = this.checked(id, doc);
|
|
75
|
+
this.fits(id, merged);
|
|
76
|
+
if (await this.blobs.put(key, merged, { ifMatch: found.version })) return;
|
|
77
|
+
}
|
|
78
|
+
throw new DocError("CONFLICT", `document "${id}" kept changing; try again`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The encoded document, once it reads back and every stored entry has the shape the commands rely on. Yjs does not
|
|
82
|
+
* check types, so an update can put text where the editor expects blocks, or a thread with no messages, and then a
|
|
83
|
+
* command fails on every later call.
|
|
84
|
+
*/
|
|
85
|
+
checked(id, doc) {
|
|
86
|
+
const refuse = (what) => new DocError("INVALID_INPUT", `the update would leave document "${id}" unreadable: ${what}`);
|
|
87
|
+
try {
|
|
88
|
+
doc.toMarkdown();
|
|
89
|
+
if (!doc.edits().every((e) => isObject(e) && isWho(e.author) && isString(e.at) && isString(e.op))) throw refuse("an edit log entry is malformed");
|
|
90
|
+
for (const [, t] of doc.ydoc.getMap("threads")) {
|
|
91
|
+
const messages = t instanceof Y.Map ? t.get("messages") : void 0;
|
|
92
|
+
const ok = t instanceof Y.Map && isAnchor(t.get("anchor")) && ["open", "resolved"].includes(t.get("status")) && messages instanceof Y.Array && messages.length > 0 && messages.toArray().every((m) => isObject(m) && isString(m.id) && isString(m.at) && isString(m.body) && isWho(m.author));
|
|
93
|
+
if (!ok) throw refuse("a thread is malformed");
|
|
94
|
+
}
|
|
95
|
+
for (const [, f] of doc.ydoc.getMap("suggestions")) {
|
|
96
|
+
const ok = f instanceof Y.Map && ["insert", "replace", "delete"].includes(f.get("kind")) && ["pending", "accepted", "rejected"].includes(f.get("status")) && isAnchor(f.get("anchor")) && isString(f.get("replacement")) && isString(f.get("createdAt")) && isWho(f.get("author"));
|
|
97
|
+
if (!ok) throw refuse("a suggestion is malformed");
|
|
98
|
+
}
|
|
99
|
+
new Suggestions(doc).list();
|
|
100
|
+
new Threads(doc).list();
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw error instanceof DocError ? error : refuse("a part holds the wrong kind of content");
|
|
103
|
+
}
|
|
104
|
+
return doc.encode();
|
|
105
|
+
}
|
|
106
|
+
fits(id, state) {
|
|
107
|
+
if (state.length > MAX_LIVE_BYTES) throw new DocError("INVALID_INPUT", `document "${id}" would pass ${MAX_LIVE_BYTES} bytes, the most a live room takes`);
|
|
108
|
+
}
|
|
109
|
+
async catalog(id, author) {
|
|
110
|
+
await this.store.transaction(async (tx) => {
|
|
111
|
+
if (await tx.get(CATALOG, id) === null) await tx.put(CATALOG, id, { createdBy: { kind: author.kind, name: author.name }, createdAt: Date.now() });
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** Who created the document and when, or null when it has no catalog entry (made before the catalog, or a failed catalog write). */
|
|
115
|
+
async info(id) {
|
|
116
|
+
this.key(id);
|
|
117
|
+
const entry = await this.store.get(CATALOG, id);
|
|
118
|
+
if (entry === null) return null;
|
|
119
|
+
const { createdBy, createdAt } = entry;
|
|
120
|
+
if (!createdBy || !PRINCIPAL_KINDS.includes(createdBy.kind) || typeof createdBy.name !== "string" || typeof createdAt !== "number") {
|
|
121
|
+
throw new DocError("INVALID_INPUT", `the catalog entry for "${id}" is damaged`);
|
|
122
|
+
}
|
|
123
|
+
return { createdBy, createdAt };
|
|
124
|
+
}
|
|
125
|
+
async read(id, run) {
|
|
126
|
+
const found = await this.blobs.get(this.key(id));
|
|
127
|
+
if (!found) throw new DocError("NO_DOCUMENT", `no document with id "${id}"`);
|
|
128
|
+
return run(AgenticDoc.fromUpdate(found.data, this.options));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Load, run the change, and save only if nobody saved in between. When someone did, load again and
|
|
132
|
+
* run the change again, so a check such as "is it still pending" sees the new state.
|
|
133
|
+
*/
|
|
134
|
+
async write(id, run) {
|
|
135
|
+
const key = this.key(id);
|
|
136
|
+
for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
|
|
137
|
+
const found = await this.blobs.get(key);
|
|
138
|
+
if (!found) throw new DocError("NO_DOCUMENT", `no document with id "${id}"`);
|
|
139
|
+
const doc = AgenticDoc.fromUpdate(found.data, this.options);
|
|
140
|
+
const result = run(doc);
|
|
141
|
+
if (await this.blobs.put(key, doc.encode(), { ifMatch: found.version })) return result;
|
|
142
|
+
}
|
|
143
|
+
throw new DocError("CONFLICT", `document "${id}" kept changing; try again`);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export {
|
|
148
|
+
MAX_LIVE_BYTES,
|
|
149
|
+
DocsService
|
|
150
|
+
};
|