@dxos/app-graph 0.8.3 → 0.8.4-main.03d5cd7b56

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 (73) hide show
  1. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  2. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  3. package/dist/lib/neutral/chunk-WJJ5KEOH.mjs +1477 -0
  4. package/dist/lib/neutral/chunk-WJJ5KEOH.mjs.map +7 -0
  5. package/dist/lib/neutral/index.mjs +40 -0
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/lib/neutral/scheduler.mjs +15 -0
  9. package/dist/lib/neutral/scheduler.mjs.map +7 -0
  10. package/dist/lib/neutral/testing/index.mjs +40 -0
  11. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  12. package/dist/types/src/atoms.d.ts +8 -0
  13. package/dist/types/src/atoms.d.ts.map +1 -0
  14. package/dist/types/src/graph-builder.d.ts +117 -60
  15. package/dist/types/src/graph-builder.d.ts.map +1 -1
  16. package/dist/types/src/graph.d.ts +188 -218
  17. package/dist/types/src/graph.d.ts.map +1 -1
  18. package/dist/types/src/index.d.ts +7 -3
  19. package/dist/types/src/index.d.ts.map +1 -1
  20. package/dist/types/src/node-matcher.d.ts +244 -0
  21. package/dist/types/src/node-matcher.d.ts.map +1 -0
  22. package/dist/types/src/node-matcher.test.d.ts +2 -0
  23. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  24. package/dist/types/src/node.d.ts +50 -5
  25. package/dist/types/src/node.d.ts.map +1 -1
  26. package/dist/types/src/scheduler.browser.d.ts +2 -0
  27. package/dist/types/src/scheduler.browser.d.ts.map +1 -0
  28. package/dist/types/src/scheduler.d.ts +8 -0
  29. package/dist/types/src/scheduler.d.ts.map +1 -0
  30. package/dist/types/src/stories/EchoGraph.stories.d.ts +6 -13
  31. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  32. package/dist/types/src/testing/index.d.ts +2 -0
  33. package/dist/types/src/testing/index.d.ts.map +1 -0
  34. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  35. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  36. package/dist/types/src/util.d.ts +40 -0
  37. package/dist/types/src/util.d.ts.map +1 -0
  38. package/dist/types/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +53 -42
  40. package/src/atoms.ts +25 -0
  41. package/src/graph-builder.test.ts +1193 -126
  42. package/src/graph-builder.ts +753 -264
  43. package/src/graph.test.ts +451 -123
  44. package/src/graph.ts +1057 -407
  45. package/src/index.ts +10 -3
  46. package/src/node-matcher.test.ts +301 -0
  47. package/src/node-matcher.ts +314 -0
  48. package/src/node.ts +83 -7
  49. package/src/scheduler.browser.ts +5 -0
  50. package/src/scheduler.ts +17 -0
  51. package/src/stories/EchoGraph.stories.tsx +178 -255
  52. package/src/stories/Tree.tsx +1 -1
  53. package/src/testing/index.ts +5 -0
  54. package/src/testing/setup-graph-builder.ts +41 -0
  55. package/src/util.ts +101 -0
  56. package/dist/lib/browser/index.mjs +0 -778
  57. package/dist/lib/browser/index.mjs.map +0 -7
  58. package/dist/lib/browser/meta.json +0 -1
  59. package/dist/lib/node/index.cjs +0 -816
  60. package/dist/lib/node/index.cjs.map +0 -7
  61. package/dist/lib/node/meta.json +0 -1
  62. package/dist/lib/node-esm/index.mjs +0 -780
  63. package/dist/lib/node-esm/index.mjs.map +0 -7
  64. package/dist/lib/node-esm/meta.json +0 -1
  65. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  66. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  67. package/dist/types/src/signals-integration.test.d.ts +0 -2
  68. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  69. package/dist/types/src/testing.d.ts +0 -5
  70. package/dist/types/src/testing.d.ts.map +0 -1
  71. package/src/experimental/graph-projections.test.ts +0 -56
  72. package/src/signals-integration.test.ts +0 -218
  73. package/src/testing.ts +0 -20
@@ -2,37 +2,548 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Registry, Rx } from '@effect-rx/rx-react';
6
- import { effect } from '@preact/signals-core';
7
- import { Array, type Option, pipe, Record } from 'effect';
8
-
9
- import { type MulticastObservable, type CleanupFn } from '@dxos/async';
5
+ import { Atom, Registry } from '@effect-atom/atom-react';
6
+ import * as Array from 'effect/Array';
7
+ import type * as Context from 'effect/Context';
8
+ import * as Effect from 'effect/Effect';
9
+ import * as Function from 'effect/Function';
10
+ import * as Option from 'effect/Option';
11
+ import * as Pipeable from 'effect/Pipeable';
12
+ import * as Record from 'effect/Record';
13
+ import type * as Schema from 'effect/Schema';
14
+
15
+ import { type CleanupFn, type Trigger } from '@dxos/async';
16
+ import { type Entity, type Type } from '@dxos/echo';
10
17
  import { log } from '@dxos/log';
11
- import { byPosition, getDebugName, isNode, isNonNullable, type MaybePromise, type Position } from '@dxos/util';
18
+ import { type MaybePromise, type Position, byPosition, getDebugName, isNonNullable } from '@dxos/util';
19
+
20
+ import { scheduleTask, yieldOrContinue } from '#scheduler';
21
+
22
+ import * as Graph from './graph';
23
+ import * as Node from './node';
24
+ import * as NodeMatcher from './node-matcher';
25
+ import {
26
+ getParentId,
27
+ nodeArgsUnchanged,
28
+ normalizeRelation,
29
+ primaryKey,
30
+ primaryParts,
31
+ qualifyId,
32
+ validateSegmentId,
33
+ } from './util';
12
34
 
13
- import { ACTION_GROUP_TYPE, ACTION_TYPE, Graph, ROOT_ID, type GraphParams, type ExpandableGraph } from './graph';
14
- import { actionGroupSymbol, type ActionData, type Node, type NodeArg, type Relation } from './node';
35
+ //
36
+ // Extension Types
37
+ //
38
+
39
+ /**
40
+ * Graph builder extension for adding nodes to the graph based on a node id.
41
+ */
42
+ export type ResolverExtension = (id: string) => Atom.Atom<Node.NodeArg<any> | null>;
15
43
 
16
44
  /**
17
45
  * Graph builder extension for adding nodes to the graph based on a connection to an existing node.
18
46
  *
19
47
  * @param params.node The existing node the returned nodes will be connected to.
20
48
  */
21
- export type ConnectorExtension = (node: Rx.Rx<Option.Option<Node>>) => Rx.Rx<NodeArg<any>[]>;
49
+ export type ConnectorExtension = (node: Atom.Atom<Option.Option<Node.Node>>) => Atom.Atom<Node.NodeArg<any>[]>;
22
50
 
23
51
  /**
24
52
  * Constrained case of the connector extension for more easily adding actions to the graph.
25
53
  */
26
54
  export type ActionsExtension = (
27
- node: Rx.Rx<Option.Option<Node>>,
28
- ) => Rx.Rx<Omit<NodeArg<ActionData>, 'type' | 'nodes' | 'edges'>[]>;
55
+ node: Atom.Atom<Option.Option<Node.Node>>,
56
+ ) => Atom.Atom<Omit<Node.NodeArg<Node.ActionData<any>>, 'type' | 'nodes' | 'edges'>[]>;
29
57
 
30
58
  /**
31
59
  * Constrained case of the connector extension for more easily adding action groups to the graph.
32
60
  */
33
61
  export type ActionGroupsExtension = (
34
- node: Rx.Rx<Option.Option<Node>>,
35
- ) => Rx.Rx<Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[]>;
62
+ node: Atom.Atom<Option.Option<Node.Node>>,
63
+ ) => Atom.Atom<Omit<Node.NodeArg<typeof Node.actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[]>;
64
+
65
+ export type BuilderExtension = Readonly<{
66
+ id: string;
67
+ position: Position;
68
+ relation?: Node.RelationInput;
69
+ resolver?: ResolverExtension;
70
+ connector?: (node: Atom.Atom<Option.Option<Node.Node>>) => Atom.Atom<Node.NodeArg<any>[]>;
71
+ }>;
72
+
73
+ export type BuilderExtensions = BuilderExtension | BuilderExtension[] | BuilderExtensions[];
74
+
75
+ //
76
+ // GraphBuilder Core
77
+ //
78
+
79
+ export type GraphBuilderTraverseOptions = {
80
+ visitor: (node: Node.Node, path: string[]) => MaybePromise<boolean | void>;
81
+ registry?: Registry.Registry;
82
+ source?: string;
83
+ relation: Node.RelationInput | Node.RelationInput[];
84
+ };
85
+
86
+ /**
87
+ * Identifier denoting a GraphBuilder.
88
+ */
89
+ export const GraphBuilderTypeId: unique symbol = Symbol.for('@dxos/app-graph/GraphBuilder');
90
+ export type GraphBuilderTypeId = typeof GraphBuilderTypeId;
91
+
92
+ /**
93
+ * GraphBuilder interface.
94
+ */
95
+ export interface GraphBuilder extends Pipeable.Pipeable {
96
+ readonly [GraphBuilderTypeId]: GraphBuilderTypeId;
97
+ readonly graph: Graph.ExpandableGraph;
98
+ readonly extensions: Atom.Atom<Record<string, BuilderExtension>>;
99
+ }
100
+
101
+ /**
102
+ * The builder provides an extensible way to compose the construction of the graph.
103
+ * @internal
104
+ */
105
+ // TODO(wittjosiah): Add api for setting subscription set and/or radius.
106
+ // Should unsubscribe from nodes that are not in the set/radius.
107
+ // Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.
108
+ class GraphBuilderImpl implements GraphBuilder {
109
+ readonly [GraphBuilderTypeId]: GraphBuilderTypeId = GraphBuilderTypeId;
110
+
111
+ pipe() {
112
+ // eslint-disable-next-line prefer-rest-params
113
+ return Pipeable.pipeArguments(this, arguments);
114
+ }
115
+
116
+ // TODO(wittjosiah): Use Context.
117
+ /** Active subscriptions keyed by composite ID, cleaned up on node removal. */
118
+ readonly _subscriptions = new Map<string, CleanupFn>();
119
+ /** Connector updates pending flush, keyed by connector key. */
120
+ readonly _dirtyConnectors = new Map<
121
+ string,
122
+ {
123
+ nodes: Node.NodeArg<any>[];
124
+ previous: string[];
125
+ }
126
+ >();
127
+ /** Last-flushed node IDs per connector key, used for edge removal on update. */
128
+ readonly _connectorPrevious = new Map<string, string[]>();
129
+ /** All inline-descendant IDs per connector key, used to remove stale inline nodes on update. */
130
+ readonly _connectorPreviousInlineIds = new Map<string, string[]>();
131
+ /** Last-flushed node args per connector key, used for change detection. */
132
+ readonly _connectorPreviousArgs = new Map<string, Node.NodeArg<any>[]>();
133
+ /** Whether a dirty-flush task is already scheduled. */
134
+ _flushScheduled = false;
135
+ /** Resolves when the current flush completes. */
136
+ _flushPromise: Promise<void> = Promise.resolve();
137
+ /** Registered builder extensions keyed by extension ID. */
138
+ readonly _extensions = Atom.make(Record.empty<string, BuilderExtension>()).pipe(
139
+ Atom.keepAlive,
140
+ Atom.withLabel('graph-builder:extensions'),
141
+ );
142
+ /** Triggers signalling that a node's resolver has fired at least once. */
143
+ readonly _initialized: Record<string, Trigger> = {};
144
+ /** Shared atom registry for reactive subscriptions. */
145
+ readonly _registry: Registry.Registry;
146
+ /** Backing graph with internal accessors for node atoms and construction. */
147
+ readonly _graph: Graph.Graph & {
148
+ _node: (id: string) => Atom.Writable<Option.Option<Node.Node>>;
149
+ _constructNode: (node: Node.NodeArg<any>) => Option.Option<Node.Node>;
150
+ };
151
+
152
+ constructor({ registry, ...params }: Pick<Graph.GraphProps, 'registry' | 'nodes' | 'edges'> = {}) {
153
+ this._registry = registry ?? Registry.make();
154
+ const graph = Graph.make({
155
+ ...params,
156
+ registry: this._registry,
157
+ onExpand: (id, relation) => this._onExpand(id, relation),
158
+ onInitialize: (id) => this._onInitialize(id),
159
+ onRemoveNode: (id) => this._onRemoveNode(id),
160
+ });
161
+ // Access internal methods via type assertion since GraphBuilder needs them
162
+ this._graph = graph as Graph.Graph & {
163
+ _node: (id: string) => Atom.Writable<Option.Option<Node.Node>>;
164
+ _constructNode: (node: Node.NodeArg<any>) => Option.Option<Node.Node>;
165
+ };
166
+ }
167
+
168
+ get graph(): Graph.ExpandableGraph {
169
+ return this._graph;
170
+ }
171
+
172
+ get extensions() {
173
+ return this._extensions;
174
+ }
175
+
176
+ /** Apply a set of node changes for a single connector key. */
177
+ private _applyConnectorUpdate(key: string, nodes: Node.NodeArg<any>[], previous: string[]): void {
178
+ const { id, relation } = relationFromConnectorKey(key);
179
+ const ids = nodes.map((node) => node.id);
180
+ const removed = previous.filter((pid) => !ids.includes(pid));
181
+ this._connectorPrevious.set(key, ids);
182
+ this._connectorPreviousArgs.set(key, nodes);
183
+
184
+ const currentInlineIds = collectAllInlineIds(nodes);
185
+ const previousInlineIds = this._connectorPreviousInlineIds.get(key) ?? [];
186
+ const staleInlineIds = previousInlineIds.filter((pid) => !currentInlineIds.includes(pid));
187
+ this._connectorPreviousInlineIds.set(key, currentInlineIds);
188
+
189
+ Graph.removeNodes(this._graph, staleInlineIds, true);
190
+ Graph.removeEdges(
191
+ this._graph,
192
+ removed.map((target) => ({ source: id, target, relation })),
193
+ true,
194
+ );
195
+ Graph.addNodes(this._graph, nodes);
196
+ Graph.addEdges(
197
+ this._graph,
198
+ nodes.map((node) => ({ source: id, target: node.id, relation })),
199
+ );
200
+ if (ids.length > 0) {
201
+ const sortedIds = [...nodes]
202
+ .sort((a, b) =>
203
+ byPosition(a.properties ?? ({} as { position?: Position }), b.properties ?? ({} as { position?: Position })),
204
+ )
205
+ .map((n) => n.id);
206
+ Graph.sortEdges(this._graph, id, relation, sortedIds);
207
+ }
208
+ }
209
+
210
+ private _scheduleDirtyFlush(): void {
211
+ if (!this._flushScheduled) {
212
+ this._flushScheduled = true;
213
+ this._flushPromise = scheduleTask(
214
+ () => {
215
+ this._flushScheduled = false;
216
+ while (this._dirtyConnectors.size > 0) {
217
+ const entries = [...this._dirtyConnectors.entries()];
218
+ this._dirtyConnectors.clear();
219
+
220
+ Atom.batch(() => {
221
+ for (const [key, { nodes, previous }] of entries) {
222
+ this._applyConnectorUpdate(key, nodes, previous);
223
+ }
224
+ });
225
+ }
226
+ },
227
+ { strategy: 'smooth' },
228
+ );
229
+ }
230
+ }
231
+
232
+ private readonly _resolvers = Atom.family<string, Atom.Atom<Option.Option<Node.NodeArg<any>>>>((id) => {
233
+ return Atom.make((get) => {
234
+ return Function.pipe(
235
+ get(this._extensions),
236
+ Record.values,
237
+ Array.sortBy(byPosition),
238
+ Array.map(({ resolver }) => resolver),
239
+ Array.filter(isNonNullable),
240
+ Array.map((resolver) => get(resolver(id))),
241
+ Array.filter(isNonNullable),
242
+ Array.head,
243
+ );
244
+ });
245
+ });
246
+
247
+ private readonly _connectors = Atom.family<string, Atom.Atom<Node.NodeArg<any>[]>>((key) => {
248
+ return Atom.make((get) => {
249
+ const { id, relation } = relationFromConnectorKey(key);
250
+ const node = this._graph.node(id);
251
+
252
+ const sourceNode = Option.getOrElse(get(node), () => undefined);
253
+ if (!sourceNode) {
254
+ return [];
255
+ }
256
+
257
+ const extensions = Function.pipe(
258
+ get(this._extensions),
259
+ Record.values,
260
+ Array.sortBy(byPosition),
261
+ Array.filter(
262
+ (ext): ext is BuilderExtension & { connector: NonNullable<BuilderExtension['connector']> } =>
263
+ Graph.relationKey(ext.relation ?? 'child') === Graph.relationKey(relation) && ext.connector != null,
264
+ ),
265
+ );
266
+
267
+ const nodes: Node.NodeArg<any>[] = [];
268
+ for (const ext of extensions) {
269
+ const result = get(ext.connector(node));
270
+ nodes.push(...result);
271
+ }
272
+
273
+ return nodes;
274
+ }).pipe(Atom.withLabel(`graph-builder:connectors:${key}`));
275
+ });
276
+
277
+ private _onExpand(id: string, relation: Node.Relation): void {
278
+ log('onExpand', { id, relation, registry: getDebugName(this._registry) });
279
+ this._expandRelation(id, relation);
280
+
281
+ // TODO(wittjosiah): Remove. This is for backwards compatibility.
282
+ if (relation.kind === 'child' && relation.direction === 'outbound') {
283
+ Graph.expand(this._graph, id, 'action');
284
+ }
285
+ }
286
+
287
+ private _expandRelation(id: string, relation: Node.RelationInput): void {
288
+ const key = connectorKey(id, relation);
289
+ const connectors = this._connectors(key);
290
+
291
+ const cancel = this._registry.subscribe(
292
+ connectors,
293
+ (rawNodes) => {
294
+ const nodes = qualifyNodeArgs(id)(rawNodes);
295
+ const previous = this._connectorPrevious.get(key) ?? [];
296
+ const ids = nodes.map((n) => n.id);
297
+
298
+ if (ids.length === previous.length && ids.every((nodeId, idx) => nodeId === previous[idx])) {
299
+ const prevArgs = this._connectorPreviousArgs.get(key);
300
+ if (prevArgs && nodeArgsUnchanged(prevArgs, nodes)) {
301
+ return;
302
+ }
303
+ }
304
+
305
+ log('update', { id, relation, ids });
306
+ this._dirtyConnectors.set(key, { nodes, previous });
307
+ this._scheduleDirtyFlush();
308
+ },
309
+ { immediate: true },
310
+ );
311
+
312
+ this._subscriptions.set(subscriptionKey(id, 'expand', key), cancel);
313
+ }
314
+
315
+ private async _onInitialize(id: string) {
316
+ log('onInitialize', { id });
317
+ const resolver = this._resolvers(id);
318
+
319
+ const cancel = this._registry.subscribe(
320
+ resolver,
321
+ (node) => {
322
+ const trigger = this._initialized[id];
323
+ const connectorOwned = [...this._connectorPrevious.values()].some((ids) => ids.includes(id));
324
+ Option.match(node, {
325
+ onSome: (node) => {
326
+ if (!connectorOwned) {
327
+ Graph.addNodes(this._graph, [node]);
328
+ // Connect resolved node to its parent via a child edge.
329
+ const parentId = getParentId(id);
330
+ if (parentId) {
331
+ Graph.addEdges(this._graph, [{ source: parentId, target: id, relation: 'child' }]);
332
+ }
333
+ }
334
+ trigger?.wake();
335
+ },
336
+ onNone: () => {
337
+ trigger?.wake();
338
+ if (!connectorOwned) {
339
+ Graph.removeNodes(this._graph, [id]);
340
+ }
341
+ },
342
+ });
343
+ },
344
+ { immediate: true },
345
+ );
346
+
347
+ this._subscriptions.set(subscriptionKey(id, 'init'), cancel);
348
+ }
349
+
350
+ private _onRemoveNode(id: string): void {
351
+ for (const [key, cleanup] of this._subscriptions) {
352
+ if (primaryParts(key)[0] === id) {
353
+ cleanup();
354
+ this._subscriptions.delete(key);
355
+ }
356
+ }
357
+ }
358
+ }
359
+
360
+ /**
361
+ * Creates a new GraphBuilder instance.
362
+ */
363
+ export const make = (params?: Pick<Graph.GraphProps, 'registry' | 'nodes' | 'edges'>): GraphBuilder => {
364
+ return new GraphBuilderImpl(params);
365
+ };
366
+
367
+ /**
368
+ * Creates a GraphBuilder from a serialized pickle string.
369
+ */
370
+ export const from = (pickle?: string, registry?: Registry.Registry): GraphBuilder => {
371
+ if (!pickle) {
372
+ return make({ registry });
373
+ }
374
+
375
+ const { nodes, edges } = JSON.parse(pickle);
376
+ return make({ nodes, edges, registry });
377
+ };
378
+
379
+ /**
380
+ * Implementation helper for addExtension.
381
+ */
382
+ const addExtensionImpl = (builder: GraphBuilder, extensions: BuilderExtensions): GraphBuilder => {
383
+ const internal = builder as GraphBuilderImpl;
384
+ flattenExtensions(extensions).forEach((extension) => {
385
+ const extensions = internal._registry.get(internal._extensions);
386
+ internal._registry.set(internal._extensions, Record.set(extensions, extension.id, extension));
387
+ });
388
+ return builder;
389
+ };
390
+
391
+ /**
392
+ * Add extensions to the graph builder.
393
+ */
394
+ export function addExtension(builder: GraphBuilder, extensions: BuilderExtensions): GraphBuilder;
395
+ export function addExtension(extensions: BuilderExtensions): (builder: GraphBuilder) => GraphBuilder;
396
+ export function addExtension(
397
+ builderOrExtensions: GraphBuilder | BuilderExtensions,
398
+ extensions?: BuilderExtensions,
399
+ ): GraphBuilder | ((builder: GraphBuilder) => GraphBuilder) {
400
+ if (extensions === undefined) {
401
+ // Curried: addExtension(extensions)
402
+ const extensions = builderOrExtensions as BuilderExtensions;
403
+ return (builder: GraphBuilder) => addExtensionImpl(builder, extensions);
404
+ } else {
405
+ // Direct: addExtension(builder, extensions)
406
+ const builder = builderOrExtensions as GraphBuilder;
407
+ return addExtensionImpl(builder, extensions);
408
+ }
409
+ }
410
+
411
+ /**
412
+ * Implementation helper for removeExtension.
413
+ */
414
+ const removeExtensionImpl = (builder: GraphBuilder, id: string): GraphBuilder => {
415
+ const internal = builder as GraphBuilderImpl;
416
+ const extensions = internal._registry.get(internal._extensions);
417
+ internal._registry.set(internal._extensions, Record.remove(extensions, id));
418
+ return builder;
419
+ };
420
+
421
+ /**
422
+ * Remove an extension from the graph builder.
423
+ */
424
+ export function removeExtension(builder: GraphBuilder, id: string): GraphBuilder;
425
+ export function removeExtension(id: string): (builder: GraphBuilder) => GraphBuilder;
426
+ export function removeExtension(
427
+ builderOrId: GraphBuilder | string,
428
+ id?: string,
429
+ ): GraphBuilder | ((builder: GraphBuilder) => GraphBuilder) {
430
+ if (typeof builderOrId === 'string') {
431
+ // Curried: removeExtension(id)
432
+ const id = builderOrId;
433
+ return (builder: GraphBuilder) => removeExtensionImpl(builder, id);
434
+ } else {
435
+ // Direct: removeExtension(builder, id)
436
+ const builder = builderOrId;
437
+ return removeExtensionImpl(builder, id!);
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Implementation helper for explore.
443
+ */
444
+ const exploreImpl = async (
445
+ builder: GraphBuilder,
446
+ options: GraphBuilderTraverseOptions,
447
+ path: string[] = [],
448
+ ): Promise<void> => {
449
+ const internal = builder as GraphBuilderImpl;
450
+ const { registry = Registry.make(), source = Node.RootId, relation, visitor } = options;
451
+ // Break cycles.
452
+ if (path.includes(source)) {
453
+ return;
454
+ }
455
+
456
+ await yieldOrContinue('idle');
457
+
458
+ const node = registry.get(internal._graph.nodeOrThrow(source));
459
+ const shouldContinue = await visitor(node, [...path, node.id]);
460
+ if (shouldContinue === false) {
461
+ return;
462
+ }
463
+
464
+ const nodes = Function.pipe(
465
+ internal._registry.get(internal._extensions),
466
+ Record.values,
467
+ Array.map((extension) => extension.connector),
468
+ Array.filter(isNonNullable),
469
+ Array.flatMap((connector) => registry.get(connector(internal._graph.node(source)))),
470
+ qualifyNodeArgs(source),
471
+ );
472
+
473
+ await Promise.all(
474
+ nodes.map((nodeArg) => {
475
+ registry.set(internal._graph._node(nodeArg.id), internal._graph._constructNode(nodeArg));
476
+ return exploreImpl(builder, { registry, source: nodeArg.id, relation, visitor }, [...path, node.id]);
477
+ }),
478
+ );
479
+
480
+ if (registry !== internal._registry) {
481
+ registry.reset();
482
+ registry.dispose();
483
+ }
484
+ };
485
+
486
+ /**
487
+ * Explore the graph by traversing it with the given options.
488
+ */
489
+ export function explore(builder: GraphBuilder, options: GraphBuilderTraverseOptions, path?: string[]): Promise<void>;
490
+ export function explore(
491
+ options: GraphBuilderTraverseOptions,
492
+ path?: string[],
493
+ ): (builder: GraphBuilder) => Promise<void>;
494
+ export function explore(
495
+ builderOrOptions: GraphBuilder | GraphBuilderTraverseOptions,
496
+ optionsOrPath?: GraphBuilderTraverseOptions | string[],
497
+ path?: string[],
498
+ ): Promise<void> | ((builder: GraphBuilder) => Promise<void>) {
499
+ if (typeof builderOrOptions === 'object' && 'visitor' in builderOrOptions) {
500
+ // Curried: explore(options, path?)
501
+ const options = builderOrOptions as GraphBuilderTraverseOptions;
502
+ const path = Array.isArray(optionsOrPath) ? optionsOrPath : undefined;
503
+ return (builder: GraphBuilder) => exploreImpl(builder, options, path);
504
+ } else {
505
+ // Direct: explore(builder, options, path?)
506
+ const builder = builderOrOptions as GraphBuilder;
507
+ const options = optionsOrPath as GraphBuilderTraverseOptions;
508
+ const pathArg = path ?? (Array.isArray(optionsOrPath) ? optionsOrPath : undefined);
509
+ return exploreImpl(builder, options, pathArg);
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Implementation helper for destroy.
515
+ */
516
+ const destroyImpl = (builder: GraphBuilder): void => {
517
+ const internal = builder as GraphBuilderImpl;
518
+ internal._subscriptions.forEach((unsubscribe) => unsubscribe());
519
+ internal._subscriptions.clear();
520
+ };
521
+
522
+ /**
523
+ * Destroy the graph builder and clean up resources.
524
+ */
525
+ export function destroy(builder: GraphBuilder): void;
526
+ export function destroy(): (builder: GraphBuilder) => void;
527
+ export function destroy(builder?: GraphBuilder): void | ((builder: GraphBuilder) => void) {
528
+ if (builder === undefined) {
529
+ // Curried: destroy()
530
+ return (builder: GraphBuilder) => destroyImpl(builder);
531
+ } else {
532
+ // Direct: destroy(builder)
533
+ return destroyImpl(builder);
534
+ }
535
+ }
536
+
537
+ /**
538
+ * Wait for all pending connector updates to be flushed.
539
+ */
540
+ export const flush = (builder: GraphBuilder): Promise<void> => {
541
+ return (builder as GraphBuilderImpl)._flushPromise;
542
+ };
543
+
544
+ //
545
+ // Extension Creation
546
+ //
36
547
 
37
548
  /**
38
549
  * A graph builder extension is used to add nodes to the graph.
@@ -45,63 +556,69 @@ export type ActionGroupsExtension = (
45
556
  * @param params.actions A function to add actions to the graph based on a connection to an existing node.
46
557
  * @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.
47
558
  */
48
- export type CreateExtensionOptions = {
559
+ export type CreateExtensionRawOptions = {
49
560
  id: string;
50
- relation?: Relation;
561
+ relation?: Node.RelationInput;
51
562
  position?: Position;
52
- // TODO(wittjosiah): On initialize to restore state from cache.
53
- // resolver?: ResolverExtension;
563
+ resolver?: ResolverExtension;
54
564
  connector?: ConnectorExtension;
55
565
  actions?: ActionsExtension;
56
566
  actionGroups?: ActionGroupsExtension;
57
567
  };
58
568
 
59
569
  /**
60
- * Create a graph builder extension.
570
+ * Create a graph builder extension (low-level API that works directly with Atoms).
61
571
  */
62
- export const createExtension = (extension: CreateExtensionOptions): BuilderExtension[] => {
572
+ export const createExtensionRaw = (extension: CreateExtensionRawOptions): BuilderExtension[] => {
63
573
  const {
64
574
  id,
65
575
  position = 'static',
66
- relation = 'outbound',
576
+ relation = 'child',
577
+ resolver: _resolver,
67
578
  connector: _connector,
68
579
  actions: _actions,
69
580
  actionGroups: _actionGroups,
70
581
  } = extension;
582
+ const normalizedRelation = normalizeRelation(relation);
71
583
  const getId = (key: string) => `${id}/${key}`;
72
584
 
585
+ const resolver =
586
+ _resolver && Atom.family((id: string) => _resolver(id).pipe(Atom.withLabel(`graph-builder:_resolver:${id}`)));
587
+
73
588
  const connector =
74
589
  _connector &&
75
- Rx.family((node: Rx.Rx<Option.Option<Node>>) =>
76
- _connector(node).pipe(Rx.withLabel(`graph-builder:_connector:${id}`)),
590
+ Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
591
+ _connector(node).pipe(Atom.withLabel(`graph-builder:_connector:${id}`)),
77
592
  );
78
593
 
79
594
  const actionGroups =
80
595
  _actionGroups &&
81
- Rx.family((node: Rx.Rx<Option.Option<Node>>) =>
82
- _actionGroups(node).pipe(Rx.withLabel(`graph-builder:_actionGroups:${id}`)),
596
+ Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
597
+ _actionGroups(node).pipe(Atom.withLabel(`graph-builder:_actionGroups:${id}`)),
83
598
  );
84
599
 
85
600
  const actions =
86
601
  _actions &&
87
- Rx.family((node: Rx.Rx<Option.Option<Node>>) => _actions(node).pipe(Rx.withLabel(`graph-builder:_actions:${id}`)));
602
+ Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
603
+ _actions(node).pipe(Atom.withLabel(`graph-builder:_actions:${id}`)),
604
+ );
88
605
 
89
606
  return [
90
- // resolver ? { id: getId('resolver'), position, resolver } : undefined,
607
+ resolver ? { id: getId('resolver'), position, resolver } : undefined,
91
608
  connector
92
609
  ? ({
93
610
  id: getId('connector'),
94
611
  position,
95
- relation,
96
- connector: Rx.family((node) =>
97
- Rx.make((get) => {
612
+ relation: normalizedRelation,
613
+ connector: Atom.family((node) =>
614
+ Atom.make((get) => {
98
615
  try {
99
616
  return get(connector(node));
100
- } catch {
101
- log.warn('Error in connector', { id: getId('connector'), node });
617
+ } catch (error) {
618
+ log.warn('Error in connector', { id: getId('connector'), node, error });
102
619
  return [];
103
620
  }
104
- }).pipe(Rx.withLabel(`graph-builder:connector:${id}`)),
621
+ }).pipe(Atom.withLabel(`graph-builder:connector:${id}`)),
105
622
  ),
106
623
  } satisfies BuilderExtension)
107
624
  : undefined,
@@ -109,20 +626,20 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
109
626
  ? ({
110
627
  id: getId('actionGroups'),
111
628
  position,
112
- relation: 'outbound',
113
- connector: Rx.family((node) =>
114
- Rx.make((get) => {
629
+ relation: Node.actionRelation(),
630
+ connector: Atom.family((node) =>
631
+ Atom.make((get) => {
115
632
  try {
116
633
  return get(actionGroups(node)).map((arg) => ({
117
634
  ...arg,
118
- data: actionGroupSymbol,
119
- type: ACTION_GROUP_TYPE,
635
+ data: Node.actionGroupSymbol,
636
+ type: Node.ActionGroupType,
120
637
  }));
121
- } catch {
122
- log.warn('Error in actionGroups', { id: getId('actionGroups'), node });
638
+ } catch (error) {
639
+ log.warn('Error in actionGroups', { id: getId('actionGroups'), node, error });
123
640
  return [];
124
641
  }
125
- }).pipe(Rx.withLabel(`graph-builder:connector:actionGroups:${id}`)),
642
+ }).pipe(Atom.withLabel(`graph-builder:connector:actionGroups:${id}`)),
126
643
  ),
127
644
  } satisfies BuilderExtension)
128
645
  : undefined,
@@ -130,261 +647,233 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
130
647
  ? ({
131
648
  id: getId('actions'),
132
649
  position,
133
- relation: 'outbound',
134
- connector: Rx.family((node) =>
135
- Rx.make((get) => {
650
+ relation: Node.actionRelation(),
651
+ connector: Atom.family((node) =>
652
+ Atom.make((get) => {
136
653
  try {
137
- return get(actions(node)).map((arg) => ({ ...arg, type: ACTION_TYPE }));
138
- } catch {
139
- log.warn('Error in actions', { id: getId('actions'), node });
654
+ return get(actions(node)).map((arg) => ({ ...arg, type: Node.ActionType }));
655
+ } catch (error) {
656
+ log.warn('Error in actions', { id: getId('actions'), node, error });
140
657
  return [];
141
658
  }
142
- }).pipe(Rx.withLabel(`graph-builder:connector:actions:${id}`)),
659
+ }).pipe(Atom.withLabel(`graph-builder:connector:actions:${id}`)),
143
660
  ),
144
661
  } satisfies BuilderExtension)
145
662
  : undefined,
146
663
  ].filter(isNonNullable);
147
664
  };
148
665
 
149
- export type GraphBuilderTraverseOptions = {
150
- visitor: (node: Node, path: string[]) => MaybePromise<boolean | void>;
151
- registry?: Registry.Registry;
152
- source?: string;
153
- relation?: Relation;
154
- };
155
-
156
- export type BuilderExtension = Readonly<{
666
+ /**
667
+ * Options for creating a graph builder extension with simplified API.
668
+ * All callbacks must return Effects for dependency injection.
669
+ * Effects may fail - errors are caught, logged, and the extension returns empty results.
670
+ */
671
+ export type CreateExtensionOptions<TMatched = Node.Node, R = never> = {
157
672
  id: string;
158
- position: Position;
159
- relation?: Relation; // Only for connector.
160
- // resolver?: ResolverExtension;
161
- connector?: (node: Rx.Rx<Option.Option<Node>>) => Rx.Rx<NodeArg<any>[]>;
162
- }>;
163
-
164
- export type BuilderExtensions = BuilderExtension | BuilderExtension[] | BuilderExtensions[];
165
-
166
- export const flattenExtensions = (extension: BuilderExtensions, acc: BuilderExtension[] = []): BuilderExtension[] => {
167
- if (Array.isArray(extension)) {
168
- return [...acc, ...extension.flatMap((ext) => flattenExtensions(ext, acc))];
169
- } else {
170
- return [...acc, extension];
171
- }
673
+ match: (node: Node.Node) => Option.Option<TMatched>;
674
+ actions?: (
675
+ matched: TMatched,
676
+ get: Atom.Context,
677
+ ) => Effect.Effect<Omit<Node.NodeArg<Node.ActionData<any>, any>, 'type'>[], Error, R>;
678
+ connector?: (matched: TMatched, get: Atom.Context) => Effect.Effect<Node.NodeArg<any, any>[], Error, R>;
679
+ resolver?: (id: string, get: Atom.Context) => Effect.Effect<Node.NodeArg<any, any> | null, Error, R>;
680
+ relation?: Node.RelationInput;
681
+ position?: Position;
172
682
  };
173
683
 
174
684
  /**
175
- * The builder provides an extensible way to compose the construction of the graph.
685
+ * Run an Effect synchronously with the provided context.
686
+ * If the effect fails, logs the error and returns the fallback value.
687
+ * @internal
176
688
  */
177
- // TODO(wittjosiah): Add api for setting subscription set and/or radius.
178
- // Should unsubscribe from nodes that are not in the set/radius.
179
- // Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.
180
- export class GraphBuilder {
181
- // TODO(wittjosiah): Use Context.
182
- private readonly _connectorSubscriptions = new Map<string, CleanupFn>();
183
- private readonly _extensions = Rx.make(Record.empty<string, BuilderExtension>()).pipe(
184
- Rx.keepAlive,
185
- Rx.withLabel('graph-builder:extensions'),
689
+ const runEffectSyncWithFallback = <T, R>(
690
+ effect: Effect.Effect<T, Error, R>,
691
+ context: Context.Context<R>,
692
+ extensionId: string,
693
+ fallback: T,
694
+ ): T => {
695
+ return Effect.runSync(
696
+ effect.pipe(
697
+ Effect.provide(context),
698
+ Effect.catchAll((error) => {
699
+ log.warn('Extension failed', { extension: extensionId, error });
700
+ return Effect.succeed(fallback);
701
+ }),
702
+ ),
186
703
  );
704
+ };
187
705
 
188
- private readonly _registry: Registry.Registry;
189
- private readonly _graph: Graph;
190
-
191
- constructor({ registry, ...params }: Pick<GraphParams, 'registry' | 'nodes' | 'edges'> = {}) {
192
- this._registry = registry ?? Registry.make();
193
- this._graph = new Graph({
194
- ...params,
195
- registry: this._registry,
196
- onExpand: (id, relation) => this._onExpand(id, relation),
197
- // onInitialize: (id) => this._onInitialize(id),
198
- onRemoveNode: (id) => this._onRemoveNode(id),
199
- });
200
- }
201
-
202
- static from(pickle?: string, registry?: Registry.Registry): GraphBuilder {
203
- if (!pickle) {
204
- return new GraphBuilder({ registry });
205
- }
206
-
207
- const { nodes, edges } = JSON.parse(pickle);
208
- return new GraphBuilder({ nodes, edges, registry });
209
- }
210
-
211
- get graph(): ExpandableGraph {
212
- return this._graph;
213
- }
214
-
215
- get extensions() {
216
- return this._extensions;
217
- }
218
-
219
- addExtension(extensions: BuilderExtensions): GraphBuilder {
220
- flattenExtensions(extensions).forEach((extension) => {
221
- const extensions = this._registry.get(this._extensions);
222
- this._registry.set(this._extensions, Record.set(extensions, extension.id, extension));
706
+ /**
707
+ * Create a graph builder extension with simplified API.
708
+ * Returns an Effect to allow callbacks to access services via dependency injection.
709
+ */
710
+ export const createExtension = <TMatched = Node.Node, R = never>(
711
+ options: CreateExtensionOptions<TMatched, R>,
712
+ ): Effect.Effect<BuilderExtension[], never, R> =>
713
+ Effect.map(Effect.context<R>(), (context) => {
714
+ const { id, match, actions, connector, resolver, relation, position } = options;
715
+
716
+ const connectorExtension = connector ? createConnectorWithRuntime(id, match, connector, context) : undefined;
717
+
718
+ const actionsExtension = actions
719
+ ? (node: Atom.Atom<Option.Option<Node.Node>>) =>
720
+ Atom.make((get) =>
721
+ Function.pipe(
722
+ get(node),
723
+ Option.flatMap(match),
724
+ Option.map((matched) =>
725
+ runEffectSyncWithFallback(actions(matched, get), context, id, []).map((action) => ({
726
+ ...action,
727
+ // Attach captured context for action execution.
728
+ _actionContext: context,
729
+ })),
730
+ ),
731
+ Option.getOrElse(() => []),
732
+ ),
733
+ )
734
+ : undefined;
735
+
736
+ const resolverExtension = resolver
737
+ ? (nodeId: string) =>
738
+ Atom.make((get) => runEffectSyncWithFallback(resolver(nodeId, get), context, id, null) ?? null)
739
+ : undefined;
740
+
741
+ return createExtensionRaw({
742
+ id,
743
+ relation,
744
+ position,
745
+ connector: connectorExtension,
746
+ actions: actionsExtension,
747
+ resolver: resolverExtension,
223
748
  });
224
- return this;
225
- }
226
-
227
- removeExtension(id: string): GraphBuilder {
228
- const extensions = this._registry.get(this._extensions);
229
- this._registry.set(this._extensions, Record.remove(extensions, id));
230
- return this;
231
- }
232
-
233
- async explore(
234
- // TODO(wittjosiah): Currently defaulting to new registry.
235
- // Currently unsure about how to handle nodes which are expanded in the background.
236
- // This seems like a good place to start.
237
- { registry = Registry.make(), source = ROOT_ID, relation = 'outbound', visitor }: GraphBuilderTraverseOptions,
238
- path: string[] = [],
239
- ): Promise<void> {
240
- // Break cycles.
241
- if (path.includes(source)) {
242
- return;
243
- }
244
-
245
- // TODO(wittjosiah): This is a workaround for esm not working in the test runner.
246
- // Switching to vitest is blocked by having node esm versions of echo-schema & echo-signals.
247
- if (!isNode()) {
248
- const { yieldOrContinue } = await import('main-thread-scheduling');
249
- await yieldOrContinue('idle');
250
- }
251
-
252
- const node = registry.get(this._graph.nodeOrThrow(source));
253
- const shouldContinue = await visitor(node, [...path, node.id]);
254
- if (shouldContinue === false) {
255
- return;
256
- }
257
-
258
- const nodes = Object.values(this._registry.get(this._extensions))
259
- .filter((extension) => relation === (extension.relation ?? 'outbound'))
260
- .map((extension) => extension.connector)
261
- .filter(isNonNullable)
262
- .flatMap((connector) => registry.get(connector(this._graph.node(source))));
263
-
264
- await Promise.all(
265
- nodes.map((nodeArg) => {
266
- registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
267
- return this.explore({ registry, source: nodeArg.id, relation, visitor }, [...path, node.id]);
268
- }),
269
- );
270
-
271
- if (registry !== this._registry) {
272
- registry.reset();
273
- registry.dispose();
274
- }
275
- }
276
-
277
- destroy(): void {
278
- this._connectorSubscriptions.forEach((unsubscribe) => unsubscribe());
279
- this._connectorSubscriptions.clear();
280
- }
281
-
282
- private readonly _connectors = Rx.family<string, Rx.Rx<NodeArg<any>[]>>((key) => {
283
- return Rx.make((get) => {
284
- const [id, relation] = key.split('+');
285
- const node = this._graph.node(id);
286
-
287
- return pipe(
288
- get(this._extensions),
289
- Record.values,
290
- // TODO(wittjosiah): Sort on write rather than read.
291
- Array.sortBy(byPosition),
292
- Array.filter(({ relation: _relation = 'outbound' }) => _relation === relation),
293
- Array.map(({ connector }) => connector?.(node)),
294
- Array.filter(isNonNullable),
295
- Array.flatMap((result) => get(result)),
296
- );
297
- }).pipe(Rx.withLabel(`graph-builder:connectors:${key}`));
298
749
  });
299
750
 
300
- private _onExpand(id: string, relation: Relation): void {
301
- log('onExpand', { id, relation, registry: getDebugName(this._registry) });
302
- const connectors = this._connectors(`${id}+${relation}`);
751
+ /**
752
+ * Create a connector extension from a matcher and factory function.
753
+ * The factory's data type is inferred from the matcher's return type.
754
+ */
755
+ export const createConnector = <TData>(
756
+ matcher: (node: Node.Node) => Option.Option<TData>,
757
+ factory: (data: TData, get: Atom.Context) => Node.NodeArg<any>[],
758
+ ): ConnectorExtension => {
759
+ return (node: Atom.Atom<Option.Option<Node.Node>>) =>
760
+ Atom.make((get) =>
761
+ Function.pipe(
762
+ get(node),
763
+ Option.flatMap(matcher),
764
+ Option.map((data) => factory(data, get)),
765
+ Option.getOrElse(() => []),
766
+ ),
767
+ );
768
+ };
303
769
 
304
- let previous: string[] = [];
305
- const cancel = this._registry.subscribe(
306
- connectors,
307
- (nodes) => {
308
- const ids = nodes.map((n) => n.id);
309
- const removed = previous.filter((id) => !ids.includes(id));
310
- previous = ids;
311
-
312
- log('update', { id, relation, ids, removed });
313
- const update = () => {
314
- Rx.batch(() => {
315
- this._graph.removeEdges(
316
- removed.map((target) => ({ source: id, target })),
317
- true,
318
- );
319
- this._graph.addNodes(nodes);
320
- this._graph.addEdges(
321
- nodes.map((node) =>
322
- relation === 'outbound' ? { source: id, target: node.id } : { source: node.id, target: id },
323
- ),
324
- );
325
- this._graph.sortEdges(
326
- id,
327
- relation,
328
- nodes.map(({ id }) => id),
329
- );
330
- });
331
- };
332
-
333
- // TODO(wittjosiah): Remove `requestAnimationFrame` once we have a better solution.
334
- // This is a workaround to avoid a race condition where the graph is updated during React render.
335
- if (typeof requestAnimationFrame === 'function') {
336
- requestAnimationFrame(update);
337
- } else {
338
- update();
339
- }
340
- },
341
- { immediate: true },
770
+ /**
771
+ * Create a connector extension from a matcher and factory function with Effect support.
772
+ * The factory must return an Effect. Errors are caught and logged.
773
+ * @internal
774
+ */
775
+ const createConnectorWithRuntime = <TData, R>(
776
+ extensionId: string,
777
+ matcher: (node: Node.Node) => Option.Option<TData>,
778
+ factory: (data: TData, get: Atom.Context) => Effect.Effect<Node.NodeArg<any>[], Error, R>,
779
+ context: Context.Context<R>,
780
+ ): ConnectorExtension => {
781
+ return (node: Atom.Atom<Option.Option<Node.Node>>) =>
782
+ Atom.make((get) =>
783
+ Function.pipe(
784
+ get(node),
785
+ Option.flatMap(matcher),
786
+ Option.map((data) => runEffectSyncWithFallback(factory(data, get), context, extensionId, [])),
787
+ Option.getOrElse(() => []),
788
+ ),
342
789
  );
790
+ };
343
791
 
344
- this._connectorSubscriptions.set(id, cancel);
345
- }
792
+ /**
793
+ * Options for creating a type-based extension.
794
+ * All callbacks must return Effects for dependency injection.
795
+ * Effects may fail - errors are caught, logged, and the extension returns empty results.
796
+ */
797
+ export type CreateTypeExtensionOptions<T extends Type.AnyEntity = Type.AnyEntity, R = never> = {
798
+ id: string;
799
+ type: T;
800
+ actions?: (
801
+ object: Entity.Entity<Schema.Schema.Type<T>>,
802
+ get: Atom.Context,
803
+ ) => Effect.Effect<Omit<Node.NodeArg<Node.ActionData<any>>, 'type'>[], Error, R>;
804
+ connector?: (
805
+ object: Entity.Entity<Schema.Schema.Type<T>>,
806
+ get: Atom.Context,
807
+ ) => Effect.Effect<Node.NodeArg<any>[], Error, R>;
808
+ relation?: Node.RelationInput;
809
+ position?: Position;
810
+ };
346
811
 
347
- // TODO(wittjosiah): On initialize to restore state from cache.
348
- // private async _onInitialize(id: string) {
349
- // log('onInitialize', { id });
350
- // }
812
+ /**
813
+ * Create an extension that matches nodes by schema type.
814
+ * The entity type is inferred from the schema type and works for both object and relation schemas.
815
+ * Returns an Effect to allow callbacks to access services via dependency injection.
816
+ */
817
+ export const createTypeExtension = <T extends Type.AnyEntity, R = never>(
818
+ options: CreateTypeExtensionOptions<T, R>,
819
+ ): Effect.Effect<BuilderExtension[], never, R> => {
820
+ const { id, type, actions, connector, relation, position } = options;
821
+ return createExtension<Entity.Entity<Schema.Schema.Type<T>>, R>({
822
+ id,
823
+ match: NodeMatcher.whenEchoType(type),
824
+ actions,
825
+ connector,
826
+ relation,
827
+ position,
828
+ });
829
+ };
351
830
 
352
- private _onRemoveNode(id: string): void {
353
- this._connectorSubscriptions.get(id)?.();
354
- this._connectorSubscriptions.delete(id);
355
- }
356
- }
831
+ //
832
+ // Extension Utilities
833
+ //
357
834
 
358
835
  /**
359
- * Creates an Rx.Rx<T> from a callback which accesses signals.
360
- * Will return a new rx instance each time.
836
+ * Qualify node IDs by prefixing with the parent path.
837
+ * Validates that segment IDs do not contain the path separator.
838
+ * Recursively qualifies inline child nodes.
361
839
  */
362
- export const rxFromSignal = <T>(cb: () => T): Rx.Rx<T> => {
363
- return Rx.make((get) => {
364
- const dispose = effect(() => {
365
- get.setSelf(cb());
840
+ const qualifyNodeArgs =
841
+ (parentId: string) =>
842
+ (nodes: Node.NodeArg<any>[]): Node.NodeArg<any>[] =>
843
+ nodes.map((node) => {
844
+ validateSegmentId(node.id);
845
+ const qualified = qualifyId(parentId, node.id);
846
+ return {
847
+ ...node,
848
+ id: qualified,
849
+ nodes: node.nodes ? qualifyNodeArgs(qualified)(node.nodes) : undefined,
850
+ };
366
851
  });
367
852
 
368
- get.addFinalizer(() => dispose());
853
+ /**
854
+ * Recursively collect all inline-descendant IDs (the `nodes` arrays at every level)
855
+ * from a list of top-level NodeArgs. Top-level IDs are excluded because they are
856
+ * already tracked via `_connectorPrevious`.
857
+ */
858
+ const collectAllInlineIds = (nodes: Node.NodeArg<any>[]): string[] =>
859
+ nodes.flatMap((node) =>
860
+ node.nodes ? [...node.nodes.map((child) => child.id), ...collectAllInlineIds(node.nodes)] : [],
861
+ );
862
+
863
+ const connectorKey = (id: string, relation: Node.RelationInput): string => primaryKey(id, Graph.relationKey(relation));
369
864
 
370
- return cb();
371
- });
865
+ const relationFromConnectorKey = (key: string): { id: string; relation: Node.Relation } => {
866
+ const [id, encodedRelation] = primaryParts(key);
867
+ return { id, relation: Graph.relationFromKey(encodedRelation) };
372
868
  };
373
869
 
374
- const observableFamily = Rx.family((observable: MulticastObservable<any>) => {
375
- return Rx.make((get) => {
376
- const subscription = observable.subscribe((value) => get.setSelf(value));
870
+ const subscriptionKey = (id: string, kind: string, detail?: string): string =>
871
+ detail != null ? primaryKey(id, kind, detail) : primaryKey(id, kind);
377
872
 
378
- get.addFinalizer(() => subscription.unsubscribe());
379
-
380
- return observable.get();
381
- });
382
- });
383
-
384
- /**
385
- * Creates an Rx.Rx<T> from a MulticastObservable<T>
386
- * Will return the same rx instance for the same observable.
387
- */
388
- export const rxFromObservable = <T>(observable: MulticastObservable<T>): Rx.Rx<T> => {
389
- return observableFamily(observable) as Rx.Rx<T>;
873
+ export const flattenExtensions = (extension: BuilderExtensions, acc: BuilderExtension[] = []): BuilderExtension[] => {
874
+ if (Array.isArray(extension)) {
875
+ return [...acc, ...extension.flatMap((ext) => flattenExtensions(ext, acc))];
876
+ } else {
877
+ return [...acc, extension];
878
+ }
390
879
  };