agentic-documents 0.1.0 → 0.2.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.
@@ -1,67 +0,0 @@
1
- import {
2
- AgenticDoc,
3
- DocError,
4
- PRINCIPAL_KINDS
5
- } from "./chunk-4YVWD5PH.js";
6
-
7
- // src/service.ts
8
- var ATTEMPTS = 64;
9
- var ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
10
- var CATALOG = "documents";
11
- var DocsService = class {
12
- constructor(ports, options = {}) {
13
- this.options = options;
14
- this.blobs = ports.blobs;
15
- this.store = ports.store;
16
- }
17
- options;
18
- blobs;
19
- store;
20
- key(id) {
21
- if (!ID.test(id)) throw new DocError("INVALID_INPUT", `invalid document id "${id}": use 1 to 64 of a-z, 0-9, "-" and "_"`);
22
- return `${id}.ydoc`;
23
- }
24
- async create(id, markdown, author) {
25
- const key = this.key(id);
26
- const created = await this.blobs.put(key, AgenticDoc.fromMarkdown(markdown, author, this.options).encode(), { ifNoneMatch: "*" });
27
- if (!created) throw new DocError("DOCUMENT_EXISTS", `document "${id}" already exists`);
28
- await this.store.transaction(async (tx) => {
29
- if (await tx.get(CATALOG, id) === null) await tx.put(CATALOG, id, { createdBy: { kind: author.kind, name: author.name }, createdAt: Date.now() });
30
- });
31
- }
32
- /** Who created the document and when, or null when it has no catalog entry (made before the catalog, or a failed catalog write). */
33
- async info(id) {
34
- this.key(id);
35
- const entry = await this.store.get(CATALOG, id);
36
- if (entry === null) return null;
37
- const { createdBy, createdAt } = entry;
38
- if (!createdBy || !PRINCIPAL_KINDS.includes(createdBy.kind) || typeof createdBy.name !== "string" || typeof createdAt !== "number") {
39
- throw new DocError("INVALID_INPUT", `the catalog entry for "${id}" is damaged`);
40
- }
41
- return { createdBy, createdAt };
42
- }
43
- async read(id, run) {
44
- const found = await this.blobs.get(this.key(id));
45
- if (!found) throw new DocError("NO_DOCUMENT", `no document with id "${id}"`);
46
- return run(AgenticDoc.fromUpdate(found.data, this.options));
47
- }
48
- /**
49
- * Load, run the change, and save only if nobody saved in between. When someone did, load again and
50
- * run the change again, so a check such as "is it still pending" sees the new state.
51
- */
52
- async write(id, run) {
53
- const key = this.key(id);
54
- for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
55
- const found = await this.blobs.get(key);
56
- if (!found) throw new DocError("NO_DOCUMENT", `no document with id "${id}"`);
57
- const doc = AgenticDoc.fromUpdate(found.data, this.options);
58
- const result = run(doc);
59
- if (await this.blobs.put(key, doc.encode(), { ifMatch: found.version })) return result;
60
- }
61
- throw new DocError("CONFLICT", `document "${id}" kept changing; try again`);
62
- }
63
- };
64
-
65
- export {
66
- DocsService
67
- };