yaml-flow 8.6.4 → 8.7.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.
Files changed (146) hide show
  1. package/browser/adapters/firebase-storage.js +3 -0
  2. package/browser/adapters/firestore-storage.js +3 -0
  3. package/browser/adapters/localstorage-storage.js +4 -0
  4. package/browser/asset-integrity.json +20 -4
  5. package/browser/server-runtime-controlface.js +8 -0
  6. package/examples/ARCHITECTURE.md +5 -32
  7. package/examples/board/demo-shell-with-server.html +2 -2
  8. package/examples/board/doc.html +2 -2
  9. package/examples/board/server/board-server.js +4 -2
  10. package/examples/board-firestore/README.md +81 -0
  11. package/examples/board-firestore/browser/board-runtime.js +263 -0
  12. package/examples/board-firestore/firestore.indexes.json +29 -0
  13. package/examples/board-firestore/package.json +14 -0
  14. package/examples/board-firestore/server/adapters/firestore-archive-factory.js +59 -0
  15. package/examples/board-firestore/server/adapters/firestore-blob-storage.js +82 -0
  16. package/examples/board-firestore/server/adapters/firestore-board-adapter.js +127 -0
  17. package/examples/board-firestore/server/adapters/firestore-journal-storage.js +54 -0
  18. package/examples/board-firestore/server/adapters/firestore-kv-storage.js +47 -0
  19. package/examples/board-firestore/server/adapters/firestore-lock.js +62 -0
  20. package/examples/board-firestore/server/adapters/firestore-queue-storage.js +186 -0
  21. package/examples/board-firestore/server/adapters/firestore-scratch-storage.js +50 -0
  22. package/examples/board-firestore/server/worker.js +146 -0
  23. package/lib/{artifacts-store-lib-BR-Samty.d.cts → artifacts-store-lib-D9nMkVcE.d.cts} +1 -1
  24. package/lib/{artifacts-store-lib-DT7XlWUL.d.ts → artifacts-store-lib-DSSMqVL2.d.ts} +1 -1
  25. package/lib/artifacts-store-public.d.cts +2 -2
  26. package/lib/artifacts-store-public.d.ts +2 -2
  27. package/lib/board-live-cards-mcp.cjs +1 -1
  28. package/lib/board-live-cards-mcp.d.cts +50 -3
  29. package/lib/board-live-cards-mcp.d.ts +50 -3
  30. package/lib/board-live-cards-mcp.js +1 -1
  31. package/lib/board-live-cards-node.cjs +8 -8
  32. package/lib/board-live-cards-node.d.cts +13 -9
  33. package/lib/board-live-cards-node.d.ts +13 -9
  34. package/lib/board-live-cards-node.js +8 -8
  35. package/lib/{board-live-cards-public-BMUIPOrc.d.ts → board-live-cards-public-D-DJek3X.d.ts} +1 -1
  36. package/lib/{board-live-cards-public-wkNmBIRC.d.cts → board-live-cards-public-DQzPe7A9.d.cts} +1 -1
  37. package/lib/board-live-cards-public-async-3hUuHxDx.d.ts +55 -0
  38. package/lib/board-live-cards-public-async-CgMCYYft.d.cts +55 -0
  39. package/lib/board-live-cards-public.d.cts +1 -1
  40. package/lib/board-live-cards-public.d.ts +1 -1
  41. package/lib/board-live-cards-server-runtime.cjs +1 -1
  42. package/lib/board-live-cards-server-runtime.d.cts +10 -6
  43. package/lib/board-live-cards-server-runtime.d.ts +10 -6
  44. package/lib/board-live-cards-server-runtime.js +1 -1
  45. package/lib/board-platform-adapter-async-DOfEq_HC.d.cts +129 -0
  46. package/lib/board-platform-adapter-async-JZPCzZnH.d.ts +129 -0
  47. package/lib/board-worker-adapter.cjs +3 -3
  48. package/lib/board-worker-adapter.js +3 -3
  49. package/lib/card-store-public.d.cts +1 -1
  50. package/lib/card-store-public.d.ts +1 -1
  51. package/lib/{chat-storage-lib-BIUbE-fM.d.cts → chat-storage-lib-B9Q34Dyv.d.cts} +1 -1
  52. package/lib/{chat-storage-lib-BlG-sobS.d.ts → chat-storage-lib-DB9iSai2.d.ts} +1 -1
  53. package/lib/chat-store-public.d.cts +2 -2
  54. package/lib/chat-store-public.d.ts +2 -2
  55. package/lib/chunk-272IYUKT.cjs +2 -0
  56. package/lib/chunk-5XHOHTLZ.cjs +2 -0
  57. package/lib/chunk-6APH25VI.js +2 -0
  58. package/lib/chunk-7FGPOGRV.cjs +2 -0
  59. package/lib/chunk-7ICPAABP.cjs +7 -0
  60. package/lib/chunk-CPAXTVBQ.cjs +2 -0
  61. package/lib/chunk-DDYSXP2A.cjs +3 -0
  62. package/lib/chunk-EGRHWZRV.js +2 -0
  63. package/lib/chunk-GL2OHR2E.cjs +2 -0
  64. package/lib/chunk-IPLSRN6P.cjs +4 -0
  65. package/lib/chunk-J5J6BG7B.js +2 -0
  66. package/lib/chunk-KAWQPLIE.cjs +2 -0
  67. package/lib/chunk-LPXVVMQT.cjs +2 -0
  68. package/lib/chunk-M3OU6IS5.cjs +2 -0
  69. package/lib/chunk-M6STQR5F.cjs +2 -0
  70. package/lib/chunk-NJJ7WEDT.cjs +2 -0
  71. package/lib/chunk-NKIQRCOM.cjs +2 -0
  72. package/lib/chunk-NM6O35RY.cjs +2 -0
  73. package/lib/chunk-NTICU4OK.js +2 -0
  74. package/lib/chunk-O7NOHKVR.js +2 -0
  75. package/lib/chunk-PRHQBGPT.js +2 -0
  76. package/lib/chunk-S44QZUDX.js +2 -0
  77. package/lib/chunk-SGV7PU4H.js +2 -0
  78. package/lib/chunk-TSN3RTXT.js +4 -0
  79. package/lib/chunk-VXJHBWK3.js +2 -0
  80. package/lib/chunk-WHDEBJLT.js +7 -0
  81. package/lib/chunk-XYN5D3GL.js +2 -0
  82. package/lib/chunk-YGALANRO.js +2 -0
  83. package/lib/chunk-ZJ5M5COT.js +2 -0
  84. package/lib/chunk-ZXQR7GHT.js +3 -0
  85. package/lib/cloud-storage.cjs +1 -1
  86. package/lib/cloud-storage.d.cts +5 -3
  87. package/lib/cloud-storage.d.ts +5 -3
  88. package/lib/cloud-storage.js +1 -1
  89. package/lib/firebase-storage/index.cjs +3 -0
  90. package/lib/firebase-storage/index.d.cts +57 -0
  91. package/lib/firebase-storage/index.d.ts +57 -0
  92. package/lib/firebase-storage/index.js +3 -0
  93. package/lib/firestore-storage/index.cjs +3 -0
  94. package/lib/firestore-storage/index.d.cts +98 -0
  95. package/lib/firestore-storage/index.d.ts +98 -0
  96. package/lib/firestore-storage/index.js +3 -0
  97. package/lib/localstorage-storage/index.cjs +2 -0
  98. package/lib/localstorage-storage/index.d.cts +39 -0
  99. package/lib/localstorage-storage/index.d.ts +39 -0
  100. package/lib/localstorage-storage/index.js +2 -0
  101. package/lib/mcp-tool-registries-BBObLYga.d.ts +41 -0
  102. package/lib/mcp-tool-registries-W3TRj6O5.d.cts +41 -0
  103. package/lib/queue-lane-registry-PaZuFpwp.d.cts +30 -0
  104. package/lib/queue-lane-registry-PaZuFpwp.d.ts +30 -0
  105. package/lib/server-jobs-queue-runner/index.cjs +2 -0
  106. package/lib/server-jobs-queue-runner/index.d.cts +22 -0
  107. package/lib/server-jobs-queue-runner/index.d.ts +22 -0
  108. package/lib/server-jobs-queue-runner/index.js +2 -0
  109. package/lib/server-runtime/index.cjs +1 -1
  110. package/lib/server-runtime/index.d.cts +11 -17
  111. package/lib/server-runtime/index.d.ts +11 -17
  112. package/lib/server-runtime/index.js +1 -1
  113. package/lib/server-runtime-agentface/index.cjs +2 -0
  114. package/lib/server-runtime-agentface/index.d.cts +53 -0
  115. package/lib/server-runtime-agentface/index.d.ts +53 -0
  116. package/lib/server-runtime-agentface/index.js +2 -0
  117. package/lib/server-runtime-controlface/index.cjs +2 -0
  118. package/lib/server-runtime-controlface/index.d.cts +80 -0
  119. package/lib/server-runtime-controlface/index.d.ts +80 -0
  120. package/lib/server-runtime-controlface/index.js +2 -0
  121. package/lib/server-runtime-core/index.cjs +2 -0
  122. package/lib/server-runtime-core/index.d.cts +376 -0
  123. package/lib/server-runtime-core/index.d.ts +376 -0
  124. package/lib/server-runtime-core/index.js +2 -0
  125. package/lib/server-runtime-watchers/index.cjs +2 -0
  126. package/lib/server-runtime-watchers/index.d.cts +127 -0
  127. package/lib/server-runtime-watchers/index.d.ts +127 -0
  128. package/lib/server-runtime-watchers/index.js +2 -0
  129. package/lib/server-runtime-webhooks/index.cjs +2 -0
  130. package/lib/server-runtime-webhooks/index.d.cts +34 -0
  131. package/lib/server-runtime-webhooks/index.d.ts +34 -0
  132. package/lib/server-runtime-webhooks/index.js +2 -0
  133. package/lib/storage-async-interface-BRR4eBjx.d.cts +81 -0
  134. package/lib/storage-async-interface-DhlOVPSp.d.ts +81 -0
  135. package/lib/{queue-lane-registry-BPKWWgd4.d.cts → types-BzQY45dH.d.cts} +8 -34
  136. package/lib/{queue-lane-registry-Be6c0ftj.d.ts → types-CF2xUcZW.d.ts} +8 -34
  137. package/package.json +46 -2
  138. package/examples/board-local/demo-shell-localstorage.html +0 -843
  139. package/lib/board-live-cards-public-async-DKZqbJVU.d.ts +0 -256
  140. package/lib/board-live-cards-public-async-dMWNbWq6.d.cts +0 -256
  141. package/lib/chunk-KXWT3CY6.cjs +0 -8
  142. package/lib/chunk-MLVTJASJ.js +0 -2
  143. package/lib/chunk-OJLA6NLU.js +0 -8
  144. package/lib/chunk-R5L5WUKN.js +0 -2
  145. package/lib/chunk-WOALA3V5.cjs +0 -2
  146. package/lib/chunk-YEB5QHGE.cjs +0 -2
@@ -0,0 +1,127 @@
1
+ /**
2
+ * firestore-board-adapter.js
3
+ *
4
+ * Composes all Firestore-backed storage primitives into an AsyncBoardPlatformAdapter
5
+ * using createHostedAsyncBoardPlatformAdapter from yaml-flow/cloud-storage.
6
+ *
7
+ * Firestore collection layout under boards/{boardId}/:
8
+ * kv-{namespace}/ AsyncKVStorage per namespace
9
+ * journal/ single board journal
10
+ * worker-queue/ board worker task queue
11
+ * chat-queue/ chat agent dispatch queue
12
+ * process-queue/ processAccumulated trigger queue
13
+ * blobs-{namespace}/ AsyncBlobStorage per namespace
14
+ * scratch/ ephemeral scratch storage
15
+ * archive-stream-{name}/ archive journal streams
16
+ * archive-blob-{name}/ archive blob collections
17
+ * archive-config/ archive config KV
18
+ * locks/board-lock AtomicRelayLock document
19
+ *
20
+ * @param {import('@google-cloud/firestore').Firestore} db
21
+ * @param {string} boardId
22
+ * @param {{ holderId?: string, requestProcessAccumulated?: () => void }} [options]
23
+ * @returns {import('yaml-flow/cloud-storage').AsyncBoardPlatformAdapter}
24
+ */
25
+
26
+ import { createHash, randomUUID } from 'node:crypto';
27
+ import { createHostedAsyncBoardPlatformAdapter } from 'yaml-flow/cloud-storage';
28
+
29
+ import { createFirestoreKvStorage } from './firestore-kv-storage.js';
30
+ import { createFirestoreJournalStorage } from './firestore-journal-storage.js';
31
+ import { createFirestoreQueueStorage } from './firestore-queue-storage.js';
32
+ import { createFirestoreBlobStorage } from './firestore-blob-storage.js';
33
+ import { createFirestoreScratchStorage } from './firestore-scratch-storage.js';
34
+ import { createFirestoreArchiveFactory } from './firestore-archive-factory.js';
35
+ import { createFirestoreLock } from './firestore-lock.js';
36
+
37
+ function parseKindValueRef(ref) {
38
+ const PREFIX = 'b64:';
39
+ if (!ref.startsWith(PREFIX)) return { kind: 'unknown', value: ref };
40
+ const b64 = ref.slice(PREFIX.length)
41
+ .replace(/-/g, '+').replace(/_/g, '/')
42
+ + '='.repeat((4 - (ref.length - PREFIX.length) % 4) % 4);
43
+ return JSON.parse(Buffer.from(b64, 'base64').toString());
44
+ }
45
+
46
+ function lexicalId() {
47
+ const ts = String(Date.now()).padStart(13, '0');
48
+ const rand = Math.random().toString(36).slice(2, 10).padEnd(8, '0');
49
+ return `${ts}-${rand}`;
50
+ }
51
+
52
+ export function createFirestoreBoardAdapter(db, boardId, options = {}) {
53
+ const boardDoc = db.collection('boards').doc(boardId);
54
+
55
+ return createHostedAsyncBoardPlatformAdapter({
56
+ boardId,
57
+
58
+ // ── KV storage ────────────────────────────────────────────────────────
59
+ kvStorage(namespace) {
60
+ return createFirestoreKvStorage(boardDoc.collection(`kv-${namespace || 'root'}`));
61
+ },
62
+ kvStorageForRef(ref) {
63
+ const parsed = parseKindValueRef(ref);
64
+ // ref.value is treated as a full Firestore collection path when kind='firestore'
65
+ const col = parsed.kind === 'firestore'
66
+ ? db.collection(parsed.value)
67
+ : boardDoc.collection(`kv-ref-${Buffer.from(ref).toString('base64url').slice(0, 16)}`);
68
+ return createFirestoreKvStorage(col);
69
+ },
70
+
71
+ // ── Blob storage ──────────────────────────────────────────────────────
72
+ blobStorage(namespace) {
73
+ return createFirestoreBlobStorage(boardDoc.collection(`blobs-${namespace || 'root'}`));
74
+ },
75
+
76
+ // ── Scratch storage ───────────────────────────────────────────────────
77
+ scratchStorage() {
78
+ return createFirestoreScratchStorage(boardDoc.collection('scratch'));
79
+ },
80
+ scratchStorageForRef(ref) {
81
+ const parsed = parseKindValueRef(ref);
82
+ const col = parsed.kind === 'firestore'
83
+ ? db.collection(parsed.value)
84
+ : boardDoc.collection('scratch');
85
+ return createFirestoreScratchStorage(col);
86
+ },
87
+
88
+ // ── Archive factory ───────────────────────────────────────────────────
89
+ archiveFactory() {
90
+ return createFirestoreArchiveFactory(db, boardId);
91
+ },
92
+ archiveFactoryForRef(ref) {
93
+ const parsed = parseKindValueRef(ref);
94
+ const altBoardId = parsed.kind === 'firestore' ? parsed.value : `${boardId}-archive`;
95
+ return createFirestoreArchiveFactory(db, altBoardId);
96
+ },
97
+
98
+ // ── Journal storage ───────────────────────────────────────────────────
99
+ journalStorage() {
100
+ return createFirestoreJournalStorage(boardDoc.collection('journal'));
101
+ },
102
+
103
+ // ── Queue storage ─────────────────────────────────────────────────────
104
+ queueStorage: createFirestoreQueueStorage(boardDoc.collection('worker-queue')),
105
+ chatAgentQueueStorage: createFirestoreQueueStorage(boardDoc.collection('chat-queue')),
106
+ processAccumulatedQueueStorage: createFirestoreQueueStorage(boardDoc.collection('process-queue')),
107
+
108
+ // ── Lock ──────────────────────────────────────────────────────────────
109
+ lock: createFirestoreLock(
110
+ boardDoc.collection('locks').doc('board-lock'),
111
+ { holderId: options.holderId ?? randomUUID() },
112
+ ),
113
+
114
+ // ── Utilities ─────────────────────────────────────────────────────────
115
+ hashFn(value) {
116
+ return createHash('sha256').update(JSON.stringify(value)).digest('hex').slice(0, 16);
117
+ },
118
+
119
+ genId() {
120
+ return lexicalId();
121
+ },
122
+
123
+ requestProcessAccumulated: options.requestProcessAccumulated,
124
+ publishBoardChangeNotifications: options.publishBoardChangeNotifications,
125
+ onWarn: (msg) => console.warn(`[firestore-board-adapter:${boardId}] ${msg}`),
126
+ });
127
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * firestore-journal-storage.js
3
+ *
4
+ * AsyncJournalStorage backed by a Firestore CollectionReference.
5
+ * Each entry is stored as: { id, createdAt, payload }
6
+ * IDs are lexicographically sortable (timestamp prefix) so ORDER BY id
7
+ * gives insertion order.
8
+ *
9
+ * @param {import('@google-cloud/firestore').CollectionReference} col
10
+ * @returns {import('yaml-flow/cloud-storage').AsyncJournalStorage}
11
+ */
12
+
13
+ function lexicalId() {
14
+ const ts = String(Date.now()).padStart(13, '0');
15
+ const rand = Math.random().toString(36).slice(2, 10).padEnd(8, '0');
16
+ return `${ts}-${rand}`;
17
+ }
18
+
19
+ export function createFirestoreJournalStorage(col) {
20
+ return {
21
+ async append(payload) {
22
+ const id = lexicalId();
23
+ const doc = { id, createdAt: new Date().toISOString(), payload };
24
+ await col.doc(id).set(doc);
25
+ return { id, payload };
26
+ },
27
+
28
+ async readAll() {
29
+ const snap = await col.orderBy('id').get();
30
+ return snap.docs.map((d) => ({ id: d.data().id, payload: d.data().payload }));
31
+ },
32
+
33
+ async readAfter(cursor) {
34
+ let q = col.orderBy('id');
35
+ if (cursor) q = col.where('id', '>', cursor).orderBy('id');
36
+ const snap = await q.get();
37
+ const entries = snap.docs.map((d) => ({ id: d.data().id, payload: d.data().payload }));
38
+ return {
39
+ entries,
40
+ newCursor: entries.length > 0 ? entries[entries.length - 1].id : cursor,
41
+ };
42
+ },
43
+
44
+ async clear() {
45
+ const snap = await col.get();
46
+ const batchSize = 500;
47
+ for (let i = 0; i < snap.docs.length; i += batchSize) {
48
+ const batch = col.firestore.batch();
49
+ for (const doc of snap.docs.slice(i, i + batchSize)) batch.delete(doc.ref);
50
+ await batch.commit();
51
+ }
52
+ },
53
+ };
54
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * firestore-kv-storage.js
3
+ *
4
+ * AsyncKVStorage backed by a Firestore CollectionReference.
5
+ * Each key is stored as a document with fields: { k, value }
6
+ * where k = original key string, value = arbitrary JSON value.
7
+ *
8
+ * Document ID: base64url(key) — avoids '/' and other Firestore-forbidden chars.
9
+ *
10
+ * @param {import('@google-cloud/firestore').CollectionReference} col
11
+ * @returns {import('yaml-flow/cloud-storage').AsyncKVStorage}
12
+ */
13
+
14
+ function encodeDocId(key) {
15
+ return Buffer.from(key).toString('base64url');
16
+ }
17
+
18
+ function decodeDocId(docId) {
19
+ return Buffer.from(docId, 'base64url').toString();
20
+ }
21
+
22
+ export function createFirestoreKvStorage(col) {
23
+ return {
24
+ async read(key) {
25
+ const snap = await col.doc(encodeDocId(key)).get();
26
+ if (!snap.exists) return null;
27
+ return snap.data()?.value ?? null;
28
+ },
29
+
30
+ async write(key, value) {
31
+ await col.doc(encodeDocId(key)).set({ k: key, value });
32
+ },
33
+
34
+ async delete(key) {
35
+ await col.doc(encodeDocId(key)).delete();
36
+ },
37
+
38
+ async listKeys(prefix = '') {
39
+ let q = col;
40
+ if (prefix) {
41
+ q = col.where('k', '>=', prefix).where('k', '<', prefix + '\uf8ff');
42
+ }
43
+ const snap = await q.orderBy('k').get();
44
+ return snap.docs.map((d) => d.data().k ?? decodeDocId(d.id));
45
+ },
46
+ };
47
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * firestore-lock.js
3
+ *
4
+ * AsyncAtomicRelayLock backed by a Firestore document + runTransaction.
5
+ *
6
+ * Document shape: { held, holderId, expiresAt, acquiredAt }
7
+ * Guarantees at-most-one holder via Firestore transaction atomicity.
8
+ *
9
+ * @param {import('@google-cloud/firestore').DocumentReference} lockDoc
10
+ * @param {{ holderId?: string, ttlMs?: number }} [options]
11
+ * @returns {import('yaml-flow/cloud-storage').AsyncAtomicRelayLock}
12
+ */
13
+
14
+ import { randomUUID } from 'node:crypto';
15
+
16
+ export function createFirestoreLock(lockDoc, options = {}) {
17
+ const holderId = options.holderId ?? randomUUID();
18
+ const ttlMs = options.ttlMs ?? 30_000;
19
+
20
+ return {
21
+ async tryAcquire() {
22
+ let released = false;
23
+
24
+ try {
25
+ await lockDoc.firestore.runTransaction(async (tx) => {
26
+ const snap = await tx.get(lockDoc);
27
+ const now = new Date();
28
+ if (snap.exists) {
29
+ const d = snap.data();
30
+ if (d.held && d.expiresAt > now.toISOString()) {
31
+ throw Object.assign(new Error('locked'), { code: 'locked' });
32
+ }
33
+ }
34
+ tx.set(lockDoc, {
35
+ held: true,
36
+ holderId,
37
+ expiresAt: new Date(now.getTime() + ttlMs).toISOString(),
38
+ acquiredAt: now.toISOString(),
39
+ });
40
+ });
41
+ } catch (err) {
42
+ if (err?.code === 'locked') return null;
43
+ throw err;
44
+ }
45
+
46
+ return async () => {
47
+ if (released) return;
48
+ released = true;
49
+ try {
50
+ await lockDoc.firestore.runTransaction(async (tx) => {
51
+ const snap = await tx.get(lockDoc);
52
+ if (snap.exists && snap.data().holderId === holderId) {
53
+ tx.update(lockDoc, { held: false, holderId: null });
54
+ }
55
+ });
56
+ } catch {
57
+ // Best-effort release — lock will expire via TTL
58
+ }
59
+ };
60
+ },
61
+ };
62
+ }
@@ -0,0 +1,186 @@
1
+ /**
2
+ * firestore-queue-storage.js
3
+ *
4
+ * AsyncQueueStorage backed by Firestore with visibility-timeout leasing.
5
+ *
6
+ * Document shape:
7
+ * { id, body, enqueuedAt, attempt, visibleAfter, leaseToken, leaseExpiresAt,
8
+ * dead, deadReason, dedupKey? }
9
+ *
10
+ * Leasing is done via Firestore transactions: find visible messages,
11
+ * then atomically claim each one (optimistic concurrency — skip on race).
12
+ *
13
+ * Required Firestore composite index for the lease query:
14
+ * Collection group: <your collection>
15
+ * Fields: dead ASC, visibleAfter ASC
16
+ *
17
+ * @param {import('@google-cloud/firestore').CollectionReference} col
18
+ * @param {{ defaultVisibilityMs?: number }} [options]
19
+ * @returns {import('yaml-flow/cloud-storage').AsyncQueueStorage}
20
+ */
21
+
22
+ import { randomUUID } from 'node:crypto';
23
+
24
+ function lexicalId() {
25
+ const ts = String(Date.now()).padStart(13, '0');
26
+ const rand = Math.random().toString(36).slice(2, 10).padEnd(8, '0');
27
+ return `${ts}-${rand}`;
28
+ }
29
+
30
+ export function createFirestoreQueueStorage(col, options = {}) {
31
+ const defaultVisibilityMs = options.defaultVisibilityMs ?? 30_000;
32
+
33
+ return {
34
+ async enqueue(body) {
35
+ const id = lexicalId();
36
+ const now = new Date().toISOString();
37
+ await col.doc(id).set({
38
+ id,
39
+ body,
40
+ enqueuedAt: now,
41
+ attempt: 0,
42
+ visibleAfter: now,
43
+ leaseToken: null,
44
+ leaseExpiresAt: null,
45
+ dead: false,
46
+ deadReason: null,
47
+ });
48
+ return { id, body, enqueuedAt: now };
49
+ },
50
+
51
+ async enqueueIfAbsent(body, dedupKey) {
52
+ const existing = await col
53
+ .where('dedupKey', '==', dedupKey)
54
+ .where('dead', '==', false)
55
+ .limit(1)
56
+ .get();
57
+ if (!existing.empty) return null;
58
+ const id = lexicalId();
59
+ const now = new Date().toISOString();
60
+ await col.doc(id).set({
61
+ id,
62
+ body,
63
+ enqueuedAt: now,
64
+ attempt: 0,
65
+ visibleAfter: now,
66
+ leaseToken: null,
67
+ leaseExpiresAt: null,
68
+ dead: false,
69
+ deadReason: null,
70
+ dedupKey,
71
+ });
72
+ return { id, body, enqueuedAt: now };
73
+ },
74
+
75
+ async lease(opts = {}) {
76
+ const max = opts.max ?? 1;
77
+ const visibilityMs = opts.visibilityMs ?? defaultVisibilityMs;
78
+ const nowIso = new Date().toISOString();
79
+
80
+ // Find visible, non-dead messages — fetch extra to tolerate races
81
+ const snap = await col
82
+ .where('dead', '==', false)
83
+ .where('visibleAfter', '<=', nowIso)
84
+ .orderBy('visibleAfter')
85
+ .limit(max * 4)
86
+ .get();
87
+
88
+ const leased = [];
89
+ for (const doc of snap.docs) {
90
+ if (leased.length >= max) break;
91
+
92
+ const data = doc.data();
93
+ // Skip if currently leased and lease hasn't expired
94
+ if (data.leaseToken && data.leaseExpiresAt > nowIso) continue;
95
+
96
+ const leaseToken = randomUUID();
97
+ const leaseExpiresAt = new Date(Date.now() + visibilityMs).toISOString();
98
+
99
+ try {
100
+ let claimedAttempt = 0;
101
+ await col.firestore.runTransaction(async (tx) => {
102
+ const fresh = await tx.get(doc.ref);
103
+ if (!fresh.exists) throw Object.assign(new Error('gone'), { code: 'gone' });
104
+ const d = fresh.data();
105
+ if (d.dead) throw Object.assign(new Error('dead'), { code: 'dead' });
106
+ if (d.leaseToken && d.leaseExpiresAt > new Date().toISOString()) {
107
+ throw Object.assign(new Error('taken'), { code: 'taken' });
108
+ }
109
+ claimedAttempt = (d.attempt ?? 0) + 1;
110
+ tx.update(doc.ref, { leaseToken, leaseExpiresAt, attempt: claimedAttempt });
111
+ });
112
+ leased.push({
113
+ id: data.id,
114
+ body: data.body,
115
+ enqueuedAt: data.enqueuedAt,
116
+ attempt: claimedAttempt,
117
+ leaseToken,
118
+ leaseExpiresAt,
119
+ });
120
+ } catch {
121
+ // Race condition — skip this message
122
+ }
123
+ }
124
+ return leased;
125
+ },
126
+
127
+ async ack(messageId, leaseToken) {
128
+ try {
129
+ await col.firestore.runTransaction(async (tx) => {
130
+ const ref = col.doc(messageId);
131
+ const snap = await tx.get(ref);
132
+ if (!snap.exists) return;
133
+ if (snap.data().leaseToken !== leaseToken) throw new Error('token mismatch');
134
+ tx.delete(ref);
135
+ });
136
+ return true;
137
+ } catch {
138
+ return false;
139
+ }
140
+ },
141
+
142
+ async nack(messageId, leaseToken, opts = {}) {
143
+ try {
144
+ await col.firestore.runTransaction(async (tx) => {
145
+ const ref = col.doc(messageId);
146
+ const snap = await tx.get(ref);
147
+ if (!snap.exists) return;
148
+ if (snap.data().leaseToken !== leaseToken) throw new Error('token mismatch');
149
+ if (opts.dead) {
150
+ tx.update(ref, {
151
+ dead: true,
152
+ deadReason: opts.reason ?? 'nacked',
153
+ leaseToken: null,
154
+ leaseExpiresAt: null,
155
+ });
156
+ } else {
157
+ tx.update(ref, {
158
+ leaseToken: null,
159
+ leaseExpiresAt: null,
160
+ visibleAfter: new Date().toISOString(),
161
+ });
162
+ }
163
+ });
164
+ return true;
165
+ } catch {
166
+ return false;
167
+ }
168
+ },
169
+
170
+ async peekActive(prefix) {
171
+ const snap = await col.where('dead', '==', false).orderBy('enqueuedAt').get();
172
+ return snap.docs
173
+ .map((d) => d.data())
174
+ .filter((d) => !prefix || String(d.id).startsWith(prefix))
175
+ .map((d) => ({ id: d.id, body: d.body, enqueuedAt: d.enqueuedAt }));
176
+ },
177
+
178
+ async peekDeadLetter(prefix) {
179
+ const snap = await col.where('dead', '==', true).orderBy('enqueuedAt').get();
180
+ return snap.docs
181
+ .map((d) => d.data())
182
+ .filter((d) => !prefix || String(d.id).startsWith(prefix))
183
+ .map((d) => ({ id: d.id, body: d.body, enqueuedAt: d.enqueuedAt, reason: d.deadReason }));
184
+ },
185
+ };
186
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * firestore-scratch-storage.js
3
+ *
4
+ * AsyncScratchStorage backed by Firestore.
5
+ * Extends AsyncBlobStorage with ephemeral-key creation and a config sub-map.
6
+ *
7
+ * @param {import('@google-cloud/firestore').CollectionReference} col
8
+ * @returns {import('yaml-flow/cloud-storage').AsyncScratchStorage}
9
+ */
10
+
11
+ import { createFirestoreBlobStorage } from './firestore-blob-storage.js';
12
+
13
+ function lexicalId() {
14
+ const ts = String(Date.now()).padStart(13, '0');
15
+ const rand = Math.random().toString(36).slice(2, 10).padEnd(8, '0');
16
+ return `${ts}-${rand}`;
17
+ }
18
+
19
+ export function createFirestoreScratchStorage(col) {
20
+ const blob = createFirestoreBlobStorage(col);
21
+
22
+ return {
23
+ ...blob,
24
+
25
+ async getUniqueKey(prefix = 'scratch-', suffix = '') {
26
+ return `${prefix}${lexicalId()}${suffix}`;
27
+ },
28
+
29
+ async create(data, prefix = 'scratch-', suffix = '') {
30
+ const key = `${prefix}${lexicalId()}${suffix}`;
31
+ await blob.write(key, data);
32
+ return key;
33
+ },
34
+
35
+ keyRef(key) {
36
+ return { kind: 'firestore-blob', value: `${col.path}/${Buffer.from(key).toString('base64url')}` };
37
+ },
38
+
39
+ config: {
40
+ async get(k) {
41
+ const content = await blob.read(`__config__/${k}`);
42
+ if (content == null) return null;
43
+ try { return JSON.parse(content); } catch { return content; }
44
+ },
45
+ async set(k, v) {
46
+ await blob.write(`__config__/${k}`, JSON.stringify(v));
47
+ },
48
+ },
49
+ };
50
+ }
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * board-firestore/server/worker.js
4
+ *
5
+ * Long-running worker server for the Firestore-backed board.
6
+ * Runs three things in one Node.js process:
7
+ *
8
+ * 1. Queue lane runners — drain worker-queue, chat-queue, process-queue
9
+ * 2. Board control-plane HTTP — GET/POST /api/board/* for browser SPA
10
+ * 3. Callback endpoint — POST /api/board/callback/* for execution webhooks
11
+ *
12
+ * Environment variables:
13
+ * GOOGLE_APPLICATION_CREDENTIALS Path to Firebase service account JSON key
14
+ * FIREBASE_PROJECT_ID Firebase project ID
15
+ * BOARD_ID Firestore board document ID (default: "default")
16
+ * WORKER_PORT HTTP listen port (default: 7900)
17
+ */
18
+
19
+ import http from 'node:http';
20
+ import { Buffer } from 'node:buffer';
21
+ import { createSingleBoardServerRuntime } from 'yaml-flow/server-runtime-controlface';
22
+ import { createHostedBoardQueueLaneRegistry } from 'yaml-flow/server-jobs-queue-runner';
23
+ import { startQueueLaneRunners } from 'yaml-flow/board-live-cards-node';
24
+ import { initializeApp, cert, getApps } from 'firebase-admin/app';
25
+ import { getFirestore } from 'firebase-admin/firestore';
26
+ import { createFirestoreBoardAdapter } from './adapters/firestore-board-adapter.js';
27
+
28
+ // ── Firebase Admin SDK init ────────────────────────────────────────────────────
29
+ if (getApps().length === 0) {
30
+ initializeApp({
31
+ credential: cert(process.env.GOOGLE_APPLICATION_CREDENTIALS ?? './service-account.json'),
32
+ projectId: process.env.FIREBASE_PROJECT_ID,
33
+ });
34
+ }
35
+
36
+ const db = getFirestore();
37
+
38
+ // ── Config ─────────────────────────────────────────────────────────────────────
39
+ const BOARD_ID = process.env.BOARD_ID ?? 'default';
40
+ const PORT = Number(process.env.WORKER_PORT ?? 7900);
41
+
42
+ // ── Helper: build a b64 ref string (matches serializeRef in yaml-flow) ──────────
43
+ function makeRef(kind, value) {
44
+ return `b64:${Buffer.from(JSON.stringify({ kind, value })).toString('base64url')}`;
45
+ }
46
+
47
+ // ── Firestore board adapter ────────────────────────────────────────────────────
48
+ const boardAdapter = createFirestoreBoardAdapter(db, BOARD_ID);
49
+
50
+ // ── Single-board runtime ───────────────────────────────────────────────────────
51
+ const runtime = createSingleBoardServerRuntime({
52
+ boardId: BOARD_ID,
53
+ boards: [
54
+ {
55
+ label: `Board — ${BOARD_ID}`,
56
+ boardAdapter,
57
+ baseRef: { kind: 'firestore', value: `boards/${BOARD_ID}` },
58
+ cardStoreRef: makeRef('firestore', `boards/${BOARD_ID}/cards`),
59
+ outputsStoreRef: makeRef('firestore', `boards/${BOARD_ID}/runtime-out`),
60
+ },
61
+ ],
62
+ invocationAdapter: {
63
+ /**
64
+ * Route execution requests to the appropriate handler.
65
+ * For worker queue dispatch, yaml-flow uses boardAdapter.dispatchExecution internally.
66
+ * This adapter is the last-resort fallback for refs not handled by boardAdapter.
67
+ */
68
+ async invoke(ref, _args) {
69
+ return { dispatched: false, error: `No invocation handler for ${ref?.howToRun ?? '?'}` };
70
+ },
71
+ },
72
+ logger: {
73
+ info: (msg, ...args) => console.log(`[worker:${BOARD_ID}] ${msg}`, ...args),
74
+ warn: (msg, ...args) => console.warn(`[worker:${BOARD_ID}] ${msg}`, ...args),
75
+ error: (msg, ...args) => console.error(`[worker:${BOARD_ID}] ${msg}`, ...args),
76
+ },
77
+ });
78
+
79
+ // ── Queue lane runners ─────────────────────────────────────────────────────────
80
+ const stopRunners = startQueueLaneRunners(
81
+ createHostedBoardQueueLaneRegistry({
82
+ boardId: BOARD_ID,
83
+ runtime,
84
+ boardAdapter,
85
+ logger: {
86
+ info: (msg, ...args) => console.log(`[queue:${BOARD_ID}] ${msg}`, ...args),
87
+ warn: (msg, ...args) => console.warn(`[queue:${BOARD_ID}] ${msg}`, ...args),
88
+ error: (msg, ...args) => console.error(`[queue:${BOARD_ID}] ${msg}`, ...args),
89
+ },
90
+ }),
91
+ );
92
+
93
+ // ── HTTP server ────────────────────────────────────────────────────────────────
94
+ function sendJson(res, status, payload) {
95
+ const body = JSON.stringify(payload);
96
+ res.writeHead(status, {
97
+ 'Content-Type': 'application/json',
98
+ 'Content-Length': Buffer.byteLength(body),
99
+ 'Access-Control-Allow-Origin': '*',
100
+ });
101
+ res.end(body);
102
+ }
103
+
104
+ const server = http.createServer(async (req, res) => {
105
+ // Handle CORS preflight
106
+ if (req.method === 'OPTIONS') {
107
+ res.writeHead(204, {
108
+ 'Access-Control-Allow-Origin': '*',
109
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
110
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
111
+ });
112
+ res.end();
113
+ return;
114
+ }
115
+
116
+ const url = new URL(req.url ?? '/', `http://localhost:${PORT}`);
117
+
118
+ try {
119
+ const handled = await runtime.handleRuntimeApi(req, res, url);
120
+ if (!handled) sendJson(res, 404, { error: 'not found', path: url.pathname });
121
+ } catch (err) {
122
+ console.error(`[worker:${BOARD_ID}] unhandled error for ${req.method} ${req.url}:`, err);
123
+ if (!res.headersSent) sendJson(res, 500, { error: 'internal error' });
124
+ }
125
+ });
126
+
127
+ server.listen(PORT, () => {
128
+ console.log(`[board-firestore/worker] Listening on http://localhost:${PORT}`);
129
+ console.log(`[board-firestore/worker] Board ID : ${BOARD_ID}`);
130
+ console.log(`[board-firestore/worker] Firebase project: ${process.env.FIREBASE_PROJECT_ID ?? '(auto-detect)'}`);
131
+ });
132
+
133
+ // ── Graceful shutdown ───────────────────────────────────────────────────────────
134
+ function shutdown() {
135
+ console.log('[board-firestore/worker] Shutting down...');
136
+ stopRunners();
137
+ server.close(() => {
138
+ console.log('[board-firestore/worker] HTTP server closed.');
139
+ process.exit(0);
140
+ });
141
+ // Force exit after 10 s if connections linger
142
+ setTimeout(() => process.exit(0), 10_000).unref();
143
+ }
144
+
145
+ process.on('SIGTERM', shutdown);
146
+ process.on('SIGINT', shutdown);
@@ -58,4 +58,4 @@ declare function createArtifactsStore(blob: BlobStorage): ArtifactsStore;
58
58
  declare function createFileArtifactsStore(store: ArtifactsStore): FileArtifactsStore;
59
59
  declare function createCardFileMetadataStore(): CardFileMetadataStore;
60
60
 
61
- export { type ArtifactInfo as A, type ArtifactsStore as a, createCardFileMetadataStore as b, createArtifactsStore as c, createFileArtifactsStore as d };
61
+ export { type ArtifactInfo as A, type CardFileMetadataStore as C, type ArtifactsStore as a, createCardFileMetadataStore as b, createArtifactsStore as c, createFileArtifactsStore as d };
@@ -58,4 +58,4 @@ declare function createArtifactsStore(blob: BlobStorage): ArtifactsStore;
58
58
  declare function createFileArtifactsStore(store: ArtifactsStore): FileArtifactsStore;
59
59
  declare function createCardFileMetadataStore(): CardFileMetadataStore;
60
60
 
61
- export { type ArtifactInfo as A, type ArtifactsStore as a, createCardFileMetadataStore as b, createArtifactsStore as c, createFileArtifactsStore as d };
61
+ export { type ArtifactInfo as A, type CardFileMetadataStore as C, type ArtifactsStore as a, createCardFileMetadataStore as b, createArtifactsStore as c, createFileArtifactsStore as d };