@graphrefly/ts 0.0.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 (210) hide show
  1. package/LICENSE +21 -0
  2. package/dist/adapters/index.cjs +9015 -0
  3. package/dist/adapters/index.cjs.map +1 -0
  4. package/dist/adapters/index.d.cts +678 -0
  5. package/dist/adapters/index.d.ts +678 -0
  6. package/dist/adapters/index.js +2894 -0
  7. package/dist/adapters/index.js.map +1 -0
  8. package/dist/adapters/observe-storage.cjs +260 -0
  9. package/dist/adapters/observe-storage.cjs.map +1 -0
  10. package/dist/adapters/observe-storage.d.cts +77 -0
  11. package/dist/adapters/observe-storage.d.ts +77 -0
  12. package/dist/adapters/observe-storage.js +161 -0
  13. package/dist/adapters/observe-storage.js.map +1 -0
  14. package/dist/append-log-DisvEReb.d.cts +189 -0
  15. package/dist/append-log-DzKMmjxx.d.ts +189 -0
  16. package/dist/chunk-2ILL7HQI.js +1189 -0
  17. package/dist/chunk-2ILL7HQI.js.map +1 -0
  18. package/dist/chunk-4VLAQHZI.js +330 -0
  19. package/dist/chunk-4VLAQHZI.js.map +1 -0
  20. package/dist/chunk-6PHB5CJS.js +229 -0
  21. package/dist/chunk-6PHB5CJS.js.map +1 -0
  22. package/dist/chunk-COHO25CZ.js +2449 -0
  23. package/dist/chunk-COHO25CZ.js.map +1 -0
  24. package/dist/chunk-D6BG7U6A.js +161 -0
  25. package/dist/chunk-D6BG7U6A.js.map +1 -0
  26. package/dist/chunk-DJC4CHLQ.js +818 -0
  27. package/dist/chunk-DJC4CHLQ.js.map +1 -0
  28. package/dist/chunk-DK554R7P.js +67 -0
  29. package/dist/chunk-DK554R7P.js.map +1 -0
  30. package/dist/chunk-DKP56CSW.js +322 -0
  31. package/dist/chunk-DKP56CSW.js.map +1 -0
  32. package/dist/chunk-G4M3NHRJ.js +688 -0
  33. package/dist/chunk-G4M3NHRJ.js.map +1 -0
  34. package/dist/chunk-GAJW5I5P.js +1 -0
  35. package/dist/chunk-GAJW5I5P.js.map +1 -0
  36. package/dist/chunk-GDL26N2T.js +282 -0
  37. package/dist/chunk-GDL26N2T.js.map +1 -0
  38. package/dist/chunk-GTPCOEOZ.js +741 -0
  39. package/dist/chunk-GTPCOEOZ.js.map +1 -0
  40. package/dist/chunk-H2HTHFVN.js +1 -0
  41. package/dist/chunk-H2HTHFVN.js.map +1 -0
  42. package/dist/chunk-HDRL4VQQ.js +218 -0
  43. package/dist/chunk-HDRL4VQQ.js.map +1 -0
  44. package/dist/chunk-I3IHOHCJ.js +2267 -0
  45. package/dist/chunk-I3IHOHCJ.js.map +1 -0
  46. package/dist/chunk-JIMUDQN3.js +965 -0
  47. package/dist/chunk-JIMUDQN3.js.map +1 -0
  48. package/dist/chunk-JR4IKCR2.js +462 -0
  49. package/dist/chunk-JR4IKCR2.js.map +1 -0
  50. package/dist/chunk-K3FXTC3I.js +165 -0
  51. package/dist/chunk-K3FXTC3I.js.map +1 -0
  52. package/dist/chunk-OOEN33FB.js +65 -0
  53. package/dist/chunk-OOEN33FB.js.map +1 -0
  54. package/dist/chunk-PKVBHGBM.js +554 -0
  55. package/dist/chunk-PKVBHGBM.js.map +1 -0
  56. package/dist/chunk-RIIOUTGM.js +1857 -0
  57. package/dist/chunk-RIIOUTGM.js.map +1 -0
  58. package/dist/chunk-SCRFRCGR.js +33 -0
  59. package/dist/chunk-SCRFRCGR.js.map +1 -0
  60. package/dist/chunk-SWTLEQL6.js +53 -0
  61. package/dist/chunk-SWTLEQL6.js.map +1 -0
  62. package/dist/chunk-SZAETNNA.js +1 -0
  63. package/dist/chunk-SZAETNNA.js.map +1 -0
  64. package/dist/chunk-TEW47IVR.js +49 -0
  65. package/dist/chunk-TEW47IVR.js.map +1 -0
  66. package/dist/chunk-Y3ZLRGHC.js +1482 -0
  67. package/dist/chunk-Y3ZLRGHC.js.map +1 -0
  68. package/dist/chunk-Z2ANQ6JW.js +149 -0
  69. package/dist/chunk-Z2ANQ6JW.js.map +1 -0
  70. package/dist/codec-BB7p8fJc.d.cts +38 -0
  71. package/dist/codec-BB7p8fJc.d.ts +38 -0
  72. package/dist/composition/index.cjs +2505 -0
  73. package/dist/composition/index.cjs.map +1 -0
  74. package/dist/composition/index.d.cts +95 -0
  75. package/dist/composition/index.d.ts +95 -0
  76. package/dist/composition/index.js +19 -0
  77. package/dist/composition/index.js.map +1 -0
  78. package/dist/core/index.cjs +2475 -0
  79. package/dist/core/index.cjs.map +1 -0
  80. package/dist/core/index.d.cts +25 -0
  81. package/dist/core/index.d.ts +25 -0
  82. package/dist/core/index.js +74 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/cqrs/index.cjs +595 -0
  85. package/dist/cqrs/index.cjs.map +1 -0
  86. package/dist/cqrs/index.d.cts +200 -0
  87. package/dist/cqrs/index.d.ts +200 -0
  88. package/dist/cqrs/index.js +557 -0
  89. package/dist/cqrs/index.js.map +1 -0
  90. package/dist/data-structures/index.cjs +4277 -0
  91. package/dist/data-structures/index.cjs.map +1 -0
  92. package/dist/data-structures/index.d.cts +22 -0
  93. package/dist/data-structures/index.d.ts +22 -0
  94. package/dist/data-structures/index.js +31 -0
  95. package/dist/data-structures/index.js.map +1 -0
  96. package/dist/describe-BZb7AS2y.d.ts +57 -0
  97. package/dist/describe-Ciu8SUnN.d.cts +57 -0
  98. package/dist/graph/index.cjs +7832 -0
  99. package/dist/graph/index.cjs.map +1 -0
  100. package/dist/graph/index.d.cts +251 -0
  101. package/dist/graph/index.d.ts +251 -0
  102. package/dist/graph/index.js +131 -0
  103. package/dist/graph/index.js.map +1 -0
  104. package/dist/graph-DfdF9zy4.d.ts +666 -0
  105. package/dist/graph-s23RbbFO.d.cts +666 -0
  106. package/dist/index-CAEmLCYF.d.cts +166 -0
  107. package/dist/index-SnzZu3x7.d.ts +166 -0
  108. package/dist/index.cjs +15409 -0
  109. package/dist/index.cjs.map +1 -0
  110. package/dist/index.d.cts +23 -0
  111. package/dist/index.d.ts +23 -0
  112. package/dist/index.js +582 -0
  113. package/dist/index.js.map +1 -0
  114. package/dist/messages-BZqrGGm1.d.cts +55 -0
  115. package/dist/messages-BZqrGGm1.d.ts +55 -0
  116. package/dist/messaging/index.cjs +818 -0
  117. package/dist/messaging/index.cjs.map +1 -0
  118. package/dist/messaging/index.d.cts +235 -0
  119. package/dist/messaging/index.d.ts +235 -0
  120. package/dist/messaging/index.js +767 -0
  121. package/dist/messaging/index.js.map +1 -0
  122. package/dist/observe-event-log-0SVsx7JG.d.cts +88 -0
  123. package/dist/observe-event-log-Dnm76owp.d.ts +88 -0
  124. package/dist/operators/index.cjs +3774 -0
  125. package/dist/operators/index.cjs.map +1 -0
  126. package/dist/operators/index.d.cts +308 -0
  127. package/dist/operators/index.d.ts +308 -0
  128. package/dist/operators/index.js +112 -0
  129. package/dist/operators/index.js.map +1 -0
  130. package/dist/orchestration/index.cjs +3541 -0
  131. package/dist/orchestration/index.cjs.map +1 -0
  132. package/dist/orchestration/index.d.cts +302 -0
  133. package/dist/orchestration/index.d.ts +302 -0
  134. package/dist/orchestration/index.js +1004 -0
  135. package/dist/orchestration/index.js.map +1 -0
  136. package/dist/patterns/index.cjs +1045 -0
  137. package/dist/patterns/index.cjs.map +1 -0
  138. package/dist/patterns/index.d.cts +39 -0
  139. package/dist/patterns/index.d.ts +39 -0
  140. package/dist/patterns/index.js +33 -0
  141. package/dist/patterns/index.js.map +1 -0
  142. package/dist/reactive-collection-BxBqtIOE.d.cts +344 -0
  143. package/dist/reactive-collection-Cb1II1Fi.d.ts +344 -0
  144. package/dist/reactive-collection-storage-BNiEc-Gd.d.ts +217 -0
  145. package/dist/reactive-collection-storage-D-KmjE0J.d.cts +217 -0
  146. package/dist/reactive-map-BcB4KsQR.d.ts +256 -0
  147. package/dist/reactive-map-D-g3dv8I.d.cts +256 -0
  148. package/dist/read-through-B1CYPfq_.d.cts +82 -0
  149. package/dist/read-through-DwyJhGs1.d.ts +82 -0
  150. package/dist/render/index.cjs +250 -0
  151. package/dist/render/index.cjs.map +1 -0
  152. package/dist/render/index.d.cts +58 -0
  153. package/dist/render/index.d.ts +58 -0
  154. package/dist/render/index.js +20 -0
  155. package/dist/render/index.js.map +1 -0
  156. package/dist/resilience-Dig3Q7n0.d.cts +45 -0
  157. package/dist/resilience-Dig3Q7n0.d.ts +45 -0
  158. package/dist/semantic-memory-graph-BYoIlP35.d.cts +356 -0
  159. package/dist/semantic-memory-graph-CxyXLms3.d.ts +356 -0
  160. package/dist/solutions/index.cjs +3249 -0
  161. package/dist/solutions/index.cjs.map +1 -0
  162. package/dist/solutions/index.d.cts +520 -0
  163. package/dist/solutions/index.d.ts +520 -0
  164. package/dist/solutions/index.js +31 -0
  165. package/dist/solutions/index.js.map +1 -0
  166. package/dist/sources/browser.cjs +3095 -0
  167. package/dist/sources/browser.cjs.map +1 -0
  168. package/dist/sources/browser.d.cts +39 -0
  169. package/dist/sources/browser.d.ts +39 -0
  170. package/dist/sources/browser.js +137 -0
  171. package/dist/sources/browser.js.map +1 -0
  172. package/dist/sources/index.cjs +3021 -0
  173. package/dist/sources/index.cjs.map +1 -0
  174. package/dist/sources/index.d.cts +7 -0
  175. package/dist/sources/index.d.ts +7 -0
  176. package/dist/sources/index.js +63 -0
  177. package/dist/sources/index.js.map +1 -0
  178. package/dist/sources/node.cjs +675 -0
  179. package/dist/sources/node.cjs.map +1 -0
  180. package/dist/sources/node.d.cts +109 -0
  181. package/dist/sources/node.d.ts +109 -0
  182. package/dist/sources/node.js +639 -0
  183. package/dist/sources/node.js.map +1 -0
  184. package/dist/storage/browser.cjs +777 -0
  185. package/dist/storage/browser.cjs.map +1 -0
  186. package/dist/storage/browser.d.cts +40 -0
  187. package/dist/storage/browser.d.ts +40 -0
  188. package/dist/storage/browser.js +275 -0
  189. package/dist/storage/browser.js.map +1 -0
  190. package/dist/storage/index.cjs +1989 -0
  191. package/dist/storage/index.cjs.map +1 -0
  192. package/dist/storage/index.d.cts +92 -0
  193. package/dist/storage/index.d.ts +92 -0
  194. package/dist/storage/index.js +161 -0
  195. package/dist/storage/index.js.map +1 -0
  196. package/dist/storage/node.cjs +898 -0
  197. package/dist/storage/node.cjs.map +1 -0
  198. package/dist/storage/node.d.cts +66 -0
  199. package/dist/storage/node.d.ts +66 -0
  200. package/dist/storage/node.js +402 -0
  201. package/dist/storage/node.js.map +1 -0
  202. package/dist/testing/index.cjs +95 -0
  203. package/dist/testing/index.cjs.map +1 -0
  204. package/dist/testing/index.d.cts +20 -0
  205. package/dist/testing/index.d.ts +20 -0
  206. package/dist/testing/index.js +8 -0
  207. package/dist/testing/index.js.map +1 -0
  208. package/dist/types-bNMSWKqF.d.cts +732 -0
  209. package/dist/types-h12mNYbq.d.ts +732 -0
  210. package/package.json +250 -0
@@ -0,0 +1,818 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/messaging/index.ts
21
+ var messaging_exports = {};
22
+ __export(messaging_exports, {
23
+ CONTEXT_TOPIC: () => CONTEXT_TOPIC,
24
+ DEFERRED_TOPIC: () => DEFERRED_TOPIC,
25
+ INJECTIONS_TOPIC: () => INJECTIONS_TOPIC,
26
+ PROMPTS_TOPIC: () => PROMPTS_TOPIC,
27
+ RESPONSES_TOPIC: () => RESPONSES_TOPIC,
28
+ SPAWNS_TOPIC: () => SPAWNS_TOPIC,
29
+ STANDARD_TOPICS: () => STANDARD_TOPICS,
30
+ TODOS_TOPIC: () => TODOS_TOPIC,
31
+ dynamicHub: () => dynamicHub,
32
+ fromHubTopic: () => fromHubTopic,
33
+ fromTopic: () => fromTopic,
34
+ messageBus: () => messageBus,
35
+ toHubTopic: () => toHubTopic,
36
+ toTopic: () => toTopic
37
+ });
38
+ module.exports = __toCommonJS(messaging_exports);
39
+
40
+ // src/protocol/messages.ts
41
+ var SENTINEL = void 0;
42
+
43
+ // src/ctx/types.ts
44
+ function depWaves(ctx, depIndex) {
45
+ return ctx.waveData[depIndex] ?? [];
46
+ }
47
+ function depBatch(ctx, depIndex) {
48
+ const waves = depWaves(ctx, depIndex);
49
+ if (waves.length === 0) return null;
50
+ const flattened = waves.flat().filter((v) => v !== SENTINEL);
51
+ return flattened.length === 0 ? [] : flattened;
52
+ }
53
+
54
+ // src/graph/graph.ts
55
+ var lifecycleRegistrars = /* @__PURE__ */ new WeakMap();
56
+ function releaseGraphNodes(graph, nodes, opts = {}) {
57
+ const registrar = lifecycleRegistrars.get(graph);
58
+ if (registrar === void 0) throw new Error("graph: unknown lifecycle registrar");
59
+ registrar.releaseNodes(nodes, opts);
60
+ }
61
+
62
+ // src/messaging/index.ts
63
+ var PROMPTS_TOPIC = "prompts";
64
+ var RESPONSES_TOPIC = "responses";
65
+ var INJECTIONS_TOPIC = "injections";
66
+ var DEFERRED_TOPIC = "deferred";
67
+ var SPAWNS_TOPIC = "spawns";
68
+ var CONTEXT_TOPIC = "context";
69
+ var TODOS_TOPIC = "todos";
70
+ var STANDARD_TOPICS = Object.freeze([
71
+ PROMPTS_TOPIC,
72
+ RESPONSES_TOPIC,
73
+ INJECTIONS_TOPIC,
74
+ DEFERRED_TOPIC,
75
+ SPAWNS_TOPIC,
76
+ CONTEXT_TOPIC,
77
+ TODOS_TOPIC
78
+ ]);
79
+ var busStates = /* @__PURE__ */ new WeakMap();
80
+ var dynamicHubStates = /* @__PURE__ */ new WeakMap();
81
+ var DEFAULT_DYNAMIC_HUB_MAX_TOPICS = 1024;
82
+ var DEFAULT_DYNAMIC_HUB_MAX_TOPIC_LENGTH = 256;
83
+ function messageBus(graph, opts) {
84
+ const unique = [...new Set(opts.topics)];
85
+ if (unique.length !== opts.topics.length) throw new Error("messageBus: duplicate topic");
86
+ if (unique.length === 0) throw new Error("messageBus: at least one topic is required");
87
+ const name = opts.name ?? "messageBus";
88
+ const state = {
89
+ graph,
90
+ name,
91
+ now: opts.now ?? Date.now,
92
+ topics: Object.freeze([...unique]),
93
+ records: /* @__PURE__ */ new Map(),
94
+ seq: 0
95
+ };
96
+ for (const topic of unique) {
97
+ const producers = [];
98
+ const body = (ctx) => {
99
+ for (let i = 0; i < producers.length; i++) {
100
+ for (const produced of depBatch(ctx, i) ?? []) {
101
+ if (produced.kind === "publish") {
102
+ ctx.down([
103
+ [
104
+ "DATA",
105
+ produced.envelope
106
+ ]
107
+ ]);
108
+ }
109
+ }
110
+ }
111
+ };
112
+ const node = graph.node([], body, {
113
+ name: `${name}/${topic}`,
114
+ factory: "messageTopic",
115
+ meta: { topic },
116
+ completeWhenDepsComplete: false,
117
+ errorWhenDepsError: false
118
+ });
119
+ state.records.set(topic, { node, producers, body });
120
+ }
121
+ const bus = {
122
+ topics: state.topics,
123
+ topic(topic) {
124
+ return requireTopic(state, topic);
125
+ },
126
+ publish(topic, payload, publishOpts = {}) {
127
+ const node = requireTopic(state, topic);
128
+ const envelope = {
129
+ topic,
130
+ seq: ++state.seq,
131
+ payload,
132
+ timestampMs: state.now(),
133
+ ...publishOpts.key === void 0 ? {} : { key: publishOpts.key }
134
+ };
135
+ node.down([["DATA", envelope]]);
136
+ return envelope;
137
+ },
138
+ has(topic) {
139
+ return state.records.has(topic);
140
+ }
141
+ };
142
+ busStates.set(bus, state);
143
+ return bus;
144
+ }
145
+ function dynamicHub(graph, opts = {}) {
146
+ const name = opts.name ?? "dynamicHub";
147
+ const unknownTopic = normalizeUnknownTopicPolicy(opts.unknownTopic);
148
+ const maxTopics = normalizeHubLimit(
149
+ opts.maxTopics,
150
+ DEFAULT_DYNAMIC_HUB_MAX_TOPICS,
151
+ "dynamicHub: maxTopics"
152
+ );
153
+ const maxTopicLength = normalizeHubLimit(
154
+ opts.maxTopicLength,
155
+ DEFAULT_DYNAMIC_HUB_MAX_TOPIC_LENGTH,
156
+ "dynamicHub: maxTopicLength"
157
+ );
158
+ const now = opts.now ?? Date.now;
159
+ const initialTopics = uniqueTopics(opts.topics ?? [], maxTopicLength);
160
+ if (initialTopics.length > maxTopics) {
161
+ throw new Error("dynamicHub: topics exceed maxTopics");
162
+ }
163
+ const commandSources = [];
164
+ const commandBody = (ctx) => {
165
+ for (let i = 0; i < commandSources.length; i++) {
166
+ for (const command2 of depBatch(ctx, i) ?? []) ctx.down([["DATA", command2]]);
167
+ }
168
+ };
169
+ const command = graph.node([], commandBody, {
170
+ name: `${name}/command`,
171
+ factory: "dynamicHubCommand",
172
+ completeWhenDepsComplete: false,
173
+ errorWhenDepsError: false
174
+ });
175
+ const events = graph.node(
176
+ [command],
177
+ (ctx) => {
178
+ const runtime = ctx.state.get() ?? {
179
+ topics: [...initialTopics],
180
+ open: true,
181
+ seq: 0,
182
+ cursor: 0
183
+ };
184
+ ctx.state.persist(true);
185
+ for (const commandFact of depBatch(ctx, 0) ?? []) {
186
+ const parsed = parseDynamicHubCommand(commandFact);
187
+ if (typeof parsed === "string") {
188
+ ctx.down([["DATA", hubError(runtime, commandFact, parsed, timestampOrFallback(now))]]);
189
+ continue;
190
+ }
191
+ const reduced = reduceDynamicHubCommand(
192
+ runtime,
193
+ parsed,
194
+ unknownTopic,
195
+ maxTopics,
196
+ maxTopicLength,
197
+ now
198
+ );
199
+ for (const event of reduced) ctx.down([["DATA", event]]);
200
+ }
201
+ ctx.state.set(runtime);
202
+ },
203
+ {
204
+ name: `${name}/events`,
205
+ factory: "dynamicHubEvents",
206
+ meta: { unknownTopic },
207
+ completeWhenDepsComplete: false,
208
+ errorWhenDepsError: false
209
+ }
210
+ );
211
+ const status = graph.node(
212
+ [events],
213
+ (ctx) => {
214
+ for (const event of depBatch(ctx, 0) ?? []) {
215
+ const typed = event;
216
+ ctx.down([["DATA", typed.status]]);
217
+ }
218
+ },
219
+ {
220
+ name: `${name}/status`,
221
+ factory: "dynamicHubStatus",
222
+ completeWhenDepsComplete: false,
223
+ errorWhenDepsError: false
224
+ }
225
+ );
226
+ const errors = graph.node(
227
+ [events],
228
+ (ctx) => {
229
+ for (const event of depBatch(ctx, 0) ?? []) {
230
+ const typed = event;
231
+ if (typed.kind !== "error") continue;
232
+ ctx.down([
233
+ [
234
+ "DATA",
235
+ {
236
+ ...typed.topic === void 0 ? {} : { topic: typed.topic },
237
+ error: typed.error,
238
+ command: typed.command,
239
+ meta: typed.meta
240
+ }
241
+ ]
242
+ ]);
243
+ }
244
+ },
245
+ {
246
+ name: `${name}/errors`,
247
+ factory: "dynamicHubErrors",
248
+ completeWhenDepsComplete: false,
249
+ errorWhenDepsError: false
250
+ }
251
+ );
252
+ const deadLetter = opts.deadLetter === true || unknownTopic === "dead-letter" ? graph.node(
253
+ [events],
254
+ (ctx) => {
255
+ for (const event of depBatch(ctx, 0) ?? []) {
256
+ const typed = event;
257
+ if (typed.kind !== "dead-letter") continue;
258
+ ctx.down([
259
+ [
260
+ "DATA",
261
+ {
262
+ ...typed.topic === void 0 ? {} : { topic: typed.topic },
263
+ reason: typed.reason,
264
+ command: typed.command,
265
+ meta: typed.meta
266
+ }
267
+ ]
268
+ ]);
269
+ }
270
+ },
271
+ {
272
+ name: `${name}/deadLetter`,
273
+ factory: "dynamicHubDeadLetter",
274
+ completeWhenDepsComplete: false,
275
+ errorWhenDepsError: false
276
+ }
277
+ ) : void 0;
278
+ const releaseEventsRetain = graph.retain(events, { reason: `${name}.dynamicHub.events` });
279
+ const hub = {
280
+ command,
281
+ events,
282
+ status,
283
+ errors,
284
+ ...deadLetter === void 0 ? {} : { deadLetter },
285
+ create(topic, commandOpts = {}) {
286
+ return publishHubCommand(command, {
287
+ kind: "create",
288
+ topic,
289
+ ...commandOpts.key === void 0 ? {} : { key: commandOpts.key },
290
+ ...commandOpts.payload === void 0 ? {} : { payload: commandOpts.payload }
291
+ });
292
+ },
293
+ delete(topic, commandOpts = {}) {
294
+ return publishHubCommand(command, {
295
+ kind: "delete",
296
+ topic,
297
+ ...commandOpts.key === void 0 ? {} : { key: commandOpts.key },
298
+ ...commandOpts.payload === void 0 ? {} : { payload: commandOpts.payload }
299
+ });
300
+ },
301
+ publish(topic, payload, commandOpts = {}) {
302
+ return publishHubCommand(command, {
303
+ kind: "publish",
304
+ topic,
305
+ payload,
306
+ ...commandOpts.key === void 0 ? {} : { key: commandOpts.key }
307
+ });
308
+ },
309
+ subscribeTopic(topic, commandOpts = {}) {
310
+ return publishHubCommand(command, {
311
+ kind: "subscribe",
312
+ topic,
313
+ ...commandOpts.key === void 0 ? {} : { key: commandOpts.key },
314
+ ...commandOpts.payload === void 0 ? {} : { payload: commandOpts.payload }
315
+ });
316
+ },
317
+ close(commandOpts = {}) {
318
+ return publishHubCommand(command, {
319
+ kind: "close",
320
+ ...commandOpts.key === void 0 ? {} : { key: commandOpts.key },
321
+ ...commandOpts.payload === void 0 ? {} : { payload: commandOpts.payload }
322
+ });
323
+ }
324
+ };
325
+ dynamicHubStates.set(hub, {
326
+ graph,
327
+ name,
328
+ maxTopicLength,
329
+ commandSources,
330
+ commandBody,
331
+ releaseEventsRetain
332
+ });
333
+ return hub;
334
+ }
335
+ function fromHubTopic(hub, topic, opts = {}) {
336
+ const state = requireDynamicHubState(hub);
337
+ assertTopicKey(topic, "fromHubTopic", state.maxTopicLength);
338
+ return state.graph.node(
339
+ [hub.events],
340
+ (ctx) => {
341
+ for (const event of depBatch(ctx, 0) ?? []) {
342
+ const typed = event;
343
+ if (typed.kind !== "message" || typed.topic !== topic) continue;
344
+ ctx.down([
345
+ [
346
+ "DATA",
347
+ {
348
+ topic,
349
+ seq: typed.meta.seq,
350
+ payload: typed.payload,
351
+ timestampMs: typed.meta.timestampMs,
352
+ ...typed.key === void 0 ? {} : { key: typed.key }
353
+ }
354
+ ]
355
+ ]);
356
+ }
357
+ },
358
+ {
359
+ name: opts.name ?? `${state.name}/${topic}/fromHubTopic`,
360
+ factory: "fromHubTopic",
361
+ meta: { topic },
362
+ completeWhenDepsComplete: false,
363
+ errorWhenDepsError: false
364
+ }
365
+ );
366
+ }
367
+ function toHubTopic(graph, source, hub, topic, opts = {}) {
368
+ const state = requireDynamicHubState(hub);
369
+ assertTopicKey(topic, "toHubTopic", state.maxTopicLength);
370
+ if (state.graph !== graph) throw new Error("toHubTopic: hub and source graph must match");
371
+ if (isReachableUpstream(source, hub.command)) {
372
+ throw new Error("toHubTopic: source already depends on hub command path");
373
+ }
374
+ const commands = graph.node(
375
+ [source],
376
+ (ctx) => {
377
+ for (const value of depBatch(ctx, 0) ?? []) {
378
+ try {
379
+ const key = opts.keyOf?.(value);
380
+ ctx.down([
381
+ [
382
+ "DATA",
383
+ {
384
+ kind: "publish",
385
+ topic,
386
+ payload: value,
387
+ ...key === void 0 ? {} : { key }
388
+ }
389
+ ]
390
+ ]);
391
+ } catch (error) {
392
+ ctx.down([
393
+ [
394
+ "DATA",
395
+ {
396
+ kind: "invalid",
397
+ topic,
398
+ error: error instanceof Error ? error.message : String(error)
399
+ }
400
+ ]
401
+ ]);
402
+ }
403
+ }
404
+ },
405
+ {
406
+ name: opts.name ?? `${state.name}/${topic}/toHubTopic`,
407
+ factory: "toHubTopic",
408
+ meta: { topic },
409
+ completeWhenDepsComplete: false,
410
+ errorWhenDepsError: false
411
+ }
412
+ );
413
+ const commandSource = commands;
414
+ state.commandSources.push(commandSource);
415
+ try {
416
+ hub.command.replaceDeps([...state.commandSources], state.commandBody);
417
+ } catch (error) {
418
+ state.commandSources.splice(state.commandSources.indexOf(commandSource), 1);
419
+ releaseGraphNodes(graph, [commands], {
420
+ reason: "toHubTopic failed command wiring"
421
+ });
422
+ throw error;
423
+ }
424
+ return { commands };
425
+ }
426
+ function fromTopic(bus, topic) {
427
+ return bus.topic(topic);
428
+ }
429
+ function toTopic(graph, source, bus, topic, opts = {}) {
430
+ const state = busStates.get(bus);
431
+ if (state === void 0) throw new Error("toTopic: unknown message bus implementation");
432
+ if (state.graph !== graph) throw new Error("toTopic: bus and source graph must match");
433
+ const record = requireTopicRecord(state, topic);
434
+ const name = opts.name ?? `toTopic/${topic}`;
435
+ const producer = graph.node(
436
+ [source],
437
+ (ctx) => {
438
+ for (const value of depBatch(ctx, 0) ?? []) {
439
+ try {
440
+ const key = opts.keyOf?.(value);
441
+ const envelope = {
442
+ topic,
443
+ seq: ++state.seq,
444
+ payload: value,
445
+ timestampMs: state.now(),
446
+ ...key === void 0 ? {} : { key }
447
+ };
448
+ ctx.down([
449
+ [
450
+ "DATA",
451
+ {
452
+ kind: "publish",
453
+ envelope
454
+ }
455
+ ]
456
+ ]);
457
+ } catch (error) {
458
+ ctx.down([["DATA", { kind: "error", topic, error }]]);
459
+ }
460
+ }
461
+ },
462
+ {
463
+ name,
464
+ factory: "toTopic",
465
+ meta: { topic },
466
+ completeWhenDepsComplete: false,
467
+ errorWhenDepsError: false
468
+ }
469
+ );
470
+ record.producers.push(producer);
471
+ record.node.replaceDeps([...record.producers], record.body);
472
+ const events = graph.node(
473
+ [producer, source],
474
+ (ctx) => {
475
+ for (const produced of depBatch(ctx, 0) ?? []) {
476
+ const typed = produced;
477
+ if (typed.kind === "publish") {
478
+ ctx.down([
479
+ [
480
+ "DATA",
481
+ {
482
+ kind: "publish",
483
+ topic,
484
+ seq: typed.envelope.seq,
485
+ payload: typed.envelope.payload
486
+ }
487
+ ]
488
+ ]);
489
+ } else {
490
+ ctx.down([
491
+ ["DATA", { kind: "error", topic, error: typed.error }]
492
+ ]);
493
+ }
494
+ }
495
+ const terminal = ctx.terminal[0];
496
+ if (terminal !== false && terminal !== void 0 && terminal !== true) {
497
+ ctx.down([
498
+ ["DATA", { kind: "error", topic, error: terminal }]
499
+ ]);
500
+ }
501
+ const sourceTerminal = ctx.terminal[1];
502
+ if (sourceTerminal === true) {
503
+ ctx.down([["DATA", { kind: "complete", topic }]]);
504
+ } else if (sourceTerminal !== false && sourceTerminal !== void 0) {
505
+ ctx.down([
506
+ ["DATA", { kind: "error", topic, error: sourceTerminal }]
507
+ ]);
508
+ }
509
+ },
510
+ {
511
+ name: `${name}/events`,
512
+ factory: "toTopicEvents",
513
+ meta: { topic },
514
+ completeWhenDepsComplete: false,
515
+ errorWhenDepsError: false,
516
+ terminalAsRealInput: true
517
+ }
518
+ );
519
+ return { events };
520
+ }
521
+ function requireTopic(state, topic) {
522
+ return requireTopicRecord(state, topic).node;
523
+ }
524
+ function requireTopicRecord(state, topic) {
525
+ const record = state.records.get(topic);
526
+ if (record === void 0) throw new Error(`messageBus: unknown topic '${topic}'`);
527
+ return record;
528
+ }
529
+ function uniqueTopics(topics, maxTopicLength) {
530
+ const unique = [...new Set(topics)];
531
+ if (unique.length !== topics.length) throw new Error("dynamicHub: duplicate topic");
532
+ for (const topic of unique) assertTopicKey(topic, "dynamicHub", maxTopicLength);
533
+ return Object.freeze(unique.sort());
534
+ }
535
+ function normalizeUnknownTopicPolicy(policy) {
536
+ if (policy === void 0) return "error";
537
+ if (policy === "drop" || policy === "error" || policy === "dead-letter" || policy === "create-as-fact") {
538
+ return policy;
539
+ }
540
+ throw new Error("dynamicHub: unknownTopic policy is not recognized");
541
+ }
542
+ function normalizeHubLimit(value, fallback, owner) {
543
+ if (value === void 0) return fallback;
544
+ if (!Number.isInteger(value) || value < 1) {
545
+ throw new Error(`${owner} must be a positive integer`);
546
+ }
547
+ return value;
548
+ }
549
+ function publishHubCommand(commandNode, command) {
550
+ commandNode.down([["DATA", command]]);
551
+ return command;
552
+ }
553
+ function parseDynamicHubCommand(value) {
554
+ if (!isObjectRecord(value)) return "dynamicHub: command fact must be an object";
555
+ const kind = value.kind;
556
+ if (kind !== "create" && kind !== "delete" && kind !== "publish" && kind !== "subscribe" && kind !== "close") {
557
+ return "dynamicHub: command kind is not recognized";
558
+ }
559
+ if (kind === "close") {
560
+ return {
561
+ kind,
562
+ ...typeof value.key === "string" ? { key: value.key } : {},
563
+ ...value.payload === void 0 ? {} : { payload: value.payload }
564
+ };
565
+ }
566
+ const topic = value.topic;
567
+ if (typeof topic !== "string" || topic.length === 0) {
568
+ return "dynamicHub: command topic must be a non-empty string";
569
+ }
570
+ if (kind === "publish") {
571
+ if (value.payload === void 0) return "dynamicHub: publish command payload is required";
572
+ return {
573
+ kind,
574
+ topic,
575
+ payload: value.payload,
576
+ ...typeof value.key === "string" ? { key: value.key } : {}
577
+ };
578
+ }
579
+ return {
580
+ kind,
581
+ topic,
582
+ ...typeof value.key === "string" ? { key: value.key } : {},
583
+ ...value.payload === void 0 ? {} : { payload: value.payload }
584
+ };
585
+ }
586
+ function isObjectRecord(value) {
587
+ return typeof value === "object" && value !== null && !Array.isArray(value);
588
+ }
589
+ function readHubTimestamp(now) {
590
+ try {
591
+ const timestampMs = now();
592
+ return Number.isFinite(timestampMs) ? timestampMs : "dynamicHub: now() must return a finite number";
593
+ } catch (error) {
594
+ return `dynamicHub: now() threw: ${error instanceof Error ? error.message : String(error)}`;
595
+ }
596
+ }
597
+ function timestampOrFallback(now) {
598
+ const timestampMs = readHubTimestamp(now);
599
+ return typeof timestampMs === "number" ? timestampMs : Date.now();
600
+ }
601
+ function reduceDynamicHubCommand(runtime, command, unknownTopic, maxTopics, maxTopicLength, now) {
602
+ const topic = "topic" in command ? command.topic : void 0;
603
+ if (topic !== void 0) {
604
+ const topicError = validateTopicKey(topic, "dynamicHub", maxTopicLength);
605
+ if (topicError !== void 0) {
606
+ return [hubError(runtime, command, topicError, timestampOrFallback(now))];
607
+ }
608
+ }
609
+ if (!runtime.open && command.kind !== "close") {
610
+ return [hubError(runtime, command, "dynamicHub: hub is closed", timestampOrFallback(now))];
611
+ }
612
+ switch (command.kind) {
613
+ case "create": {
614
+ const capacityError = canAddHubTopic(runtime, command.topic, maxTopics);
615
+ if (capacityError !== void 0) {
616
+ return [hubError(runtime, command, capacityError, timestampOrFallback(now))];
617
+ }
618
+ const timestampMs = readHubTimestamp(now);
619
+ if (typeof timestampMs === "string")
620
+ return [hubError(runtime, command, timestampMs, Date.now())];
621
+ addHubTopic(runtime, command.topic);
622
+ return [hubEvent(runtime, timestampMs, command)];
623
+ }
624
+ case "delete": {
625
+ if (!hasHubTopic(runtime, command.topic)) {
626
+ return unknownHubTopic(runtime, command, unknownTopic, now);
627
+ }
628
+ const timestampMs = readHubTimestamp(now);
629
+ if (typeof timestampMs === "string")
630
+ return [hubError(runtime, command, timestampMs, Date.now())];
631
+ deleteHubTopic(runtime, command.topic);
632
+ return [hubEvent(runtime, timestampMs, command)];
633
+ }
634
+ case "publish": {
635
+ if (!hasHubTopic(runtime, command.topic)) {
636
+ if (unknownTopic === "create-as-fact") {
637
+ const capacityError = canAddHubTopic(runtime, command.topic, maxTopics);
638
+ if (capacityError !== void 0) {
639
+ return [hubError(runtime, command, capacityError, timestampOrFallback(now))];
640
+ }
641
+ const createTimestampMs = readHubTimestamp(now);
642
+ if (typeof createTimestampMs === "string") {
643
+ return [hubError(runtime, command, createTimestampMs, Date.now())];
644
+ }
645
+ const messageTimestampMs = readHubTimestamp(now);
646
+ if (typeof messageTimestampMs === "string") {
647
+ return [hubError(runtime, command, messageTimestampMs, Date.now())];
648
+ }
649
+ addHubTopic(runtime, command.topic);
650
+ return [
651
+ hubEvent(runtime, createTimestampMs, { kind: "create", topic: command.topic }),
652
+ hubEvent(runtime, messageTimestampMs, {
653
+ kind: "message",
654
+ topic: command.topic,
655
+ payload: command.payload,
656
+ ...command.key === void 0 ? {} : { key: command.key }
657
+ })
658
+ ];
659
+ }
660
+ return unknownHubTopic(runtime, command, unknownTopic, now);
661
+ }
662
+ const timestampMs = readHubTimestamp(now);
663
+ if (typeof timestampMs === "string")
664
+ return [hubError(runtime, command, timestampMs, Date.now())];
665
+ return [
666
+ hubEvent(runtime, timestampMs, {
667
+ kind: "message",
668
+ topic: command.topic,
669
+ payload: command.payload,
670
+ ...command.key === void 0 ? {} : { key: command.key }
671
+ })
672
+ ];
673
+ }
674
+ case "subscribe": {
675
+ if (!hasHubTopic(runtime, command.topic)) {
676
+ if (unknownTopic === "create-as-fact") {
677
+ const capacityError = canAddHubTopic(runtime, command.topic, maxTopics);
678
+ if (capacityError !== void 0) {
679
+ return [hubError(runtime, command, capacityError, timestampOrFallback(now))];
680
+ }
681
+ const createTimestampMs = readHubTimestamp(now);
682
+ if (typeof createTimestampMs === "string") {
683
+ return [hubError(runtime, command, createTimestampMs, Date.now())];
684
+ }
685
+ const subscribeTimestampMs = readHubTimestamp(now);
686
+ if (typeof subscribeTimestampMs === "string") {
687
+ return [hubError(runtime, command, subscribeTimestampMs, Date.now())];
688
+ }
689
+ addHubTopic(runtime, command.topic);
690
+ return [
691
+ hubEvent(runtime, createTimestampMs, { kind: "create", topic: command.topic }),
692
+ hubEvent(runtime, subscribeTimestampMs, command)
693
+ ];
694
+ }
695
+ return unknownHubTopic(runtime, command, unknownTopic, now);
696
+ }
697
+ const timestampMs = readHubTimestamp(now);
698
+ if (typeof timestampMs === "string")
699
+ return [hubError(runtime, command, timestampMs, Date.now())];
700
+ return [hubEvent(runtime, timestampMs, command)];
701
+ }
702
+ case "close": {
703
+ const timestampMs = readHubTimestamp(now);
704
+ if (typeof timestampMs === "string")
705
+ return [hubError(runtime, command, timestampMs, Date.now())];
706
+ runtime.open = false;
707
+ return [hubEvent(runtime, timestampMs, command)];
708
+ }
709
+ }
710
+ }
711
+ function hubEvent(runtime, timestampMs, event) {
712
+ const meta = nextHubMetadata(runtime, timestampMs);
713
+ return {
714
+ ...event,
715
+ meta,
716
+ status: snapshotHubStatus(runtime, meta, event.kind)
717
+ };
718
+ }
719
+ function hubError(runtime, command, error, timestampMs) {
720
+ return hubEvent(runtime, timestampMs, {
721
+ kind: "error",
722
+ ...isObjectRecord(command) && typeof command.topic === "string" ? { topic: command.topic } : {},
723
+ error,
724
+ command
725
+ });
726
+ }
727
+ function unknownHubTopic(runtime, command, policy, now) {
728
+ const topic = "topic" in command ? command.topic : void 0;
729
+ const reason = `dynamicHub: unknown topic '${topic ?? ""}'`;
730
+ if (policy === "drop") return [];
731
+ if (policy === "dead-letter") {
732
+ return [
733
+ hubEvent(runtime, timestampOrFallback(now), {
734
+ kind: "dead-letter",
735
+ ...topic === void 0 ? {} : { topic },
736
+ reason,
737
+ command
738
+ })
739
+ ];
740
+ }
741
+ return [hubError(runtime, command, reason, timestampOrFallback(now))];
742
+ }
743
+ function nextHubMetadata(runtime, timestampMs) {
744
+ runtime.seq += 1;
745
+ runtime.cursor = runtime.seq;
746
+ return { seq: runtime.seq, cursor: runtime.cursor, timestampMs };
747
+ }
748
+ function snapshotHubStatus(runtime, meta, kind) {
749
+ return {
750
+ open: runtime.open,
751
+ topics: Object.freeze([...runtime.topics].sort()),
752
+ seq: meta.seq,
753
+ cursor: meta.cursor,
754
+ lastEventKind: kind
755
+ };
756
+ }
757
+ function assertTopicKey(topic, owner, maxTopicLength) {
758
+ const error = validateTopicKey(topic, owner, maxTopicLength);
759
+ if (error !== void 0) throw new Error(error);
760
+ }
761
+ function validateTopicKey(topic, owner, maxTopicLength) {
762
+ if (typeof topic !== "string" || topic.length === 0) {
763
+ return `${owner}: topic must be a non-empty string`;
764
+ }
765
+ if (topic.length > maxTopicLength) {
766
+ return `${owner}: topic exceeds maxTopicLength`;
767
+ }
768
+ return void 0;
769
+ }
770
+ function hasHubTopic(runtime, topic) {
771
+ return runtime.topics.includes(topic);
772
+ }
773
+ function addHubTopic(runtime, topic) {
774
+ if (!hasHubTopic(runtime, topic)) runtime.topics = [...runtime.topics, topic];
775
+ }
776
+ function deleteHubTopic(runtime, topic) {
777
+ runtime.topics = runtime.topics.filter((existing) => existing !== topic);
778
+ }
779
+ function canAddHubTopic(runtime, topic, maxTopics) {
780
+ if (hasHubTopic(runtime, topic)) return void 0;
781
+ return runtime.topics.length >= maxTopics ? "dynamicHub: topic count exceeds maxTopics" : void 0;
782
+ }
783
+ function isReachableUpstream(from, target) {
784
+ const seen = /* @__PURE__ */ new Set();
785
+ const stack = [from];
786
+ while (stack.length > 0) {
787
+ const node = stack.pop();
788
+ if (node === void 0) continue;
789
+ if (node === target) return true;
790
+ if (seen.has(node)) continue;
791
+ seen.add(node);
792
+ for (const dep of node.deps) stack.push(dep);
793
+ }
794
+ return false;
795
+ }
796
+ function requireDynamicHubState(hub) {
797
+ const state = dynamicHubStates.get(hub);
798
+ if (state === void 0) throw new Error("dynamicHub: unknown hub implementation");
799
+ return state;
800
+ }
801
+ // Annotate the CommonJS export names for ESM import in node:
802
+ 0 && (module.exports = {
803
+ CONTEXT_TOPIC,
804
+ DEFERRED_TOPIC,
805
+ INJECTIONS_TOPIC,
806
+ PROMPTS_TOPIC,
807
+ RESPONSES_TOPIC,
808
+ SPAWNS_TOPIC,
809
+ STANDARD_TOPICS,
810
+ TODOS_TOPIC,
811
+ dynamicHub,
812
+ fromHubTopic,
813
+ fromTopic,
814
+ messageBus,
815
+ toHubTopic,
816
+ toTopic
817
+ });
818
+ //# sourceMappingURL=index.cjs.map