@dxos/app-graph 0.8.4-main.ae835ea → 0.8.4-main.bc674ce

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 (43) hide show
  1. package/dist/lib/browser/index.mjs +1014 -553
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +1013 -553
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/atoms.d.ts +8 -0
  8. package/dist/types/src/atoms.d.ts.map +1 -0
  9. package/dist/types/src/graph-builder.d.ts +108 -66
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +182 -212
  12. package/dist/types/src/graph.d.ts.map +1 -1
  13. package/dist/types/src/index.d.ts +6 -3
  14. package/dist/types/src/index.d.ts.map +1 -1
  15. package/dist/types/src/node-matcher.d.ts +218 -0
  16. package/dist/types/src/node-matcher.d.ts.map +1 -0
  17. package/dist/types/src/node-matcher.test.d.ts +2 -0
  18. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  19. package/dist/types/src/node.d.ts +32 -3
  20. package/dist/types/src/node.d.ts.map +1 -1
  21. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  22. package/dist/types/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +35 -33
  24. package/src/atoms.ts +25 -0
  25. package/src/graph-builder.test.ts +520 -104
  26. package/src/graph-builder.ts +550 -255
  27. package/src/graph.test.ts +299 -106
  28. package/src/graph.ts +964 -394
  29. package/src/index.ts +9 -3
  30. package/src/node-matcher.test.ts +301 -0
  31. package/src/node-matcher.ts +284 -0
  32. package/src/node.ts +39 -6
  33. package/src/stories/EchoGraph.stories.tsx +104 -95
  34. package/src/stories/Tree.tsx +2 -2
  35. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  36. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  37. package/dist/types/src/signals-integration.test.d.ts +0 -2
  38. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  39. package/dist/types/src/testing.d.ts +0 -5
  40. package/dist/types/src/testing.d.ts.map +0 -1
  41. package/src/experimental/graph-projections.test.ts +0 -56
  42. package/src/signals-integration.test.ts +0 -218
  43. package/src/testing.ts +0 -20
package/src/index.ts CHANGED
@@ -2,6 +2,12 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './graph';
6
- export * from './graph-builder';
7
- export * from './node';
5
+ // TODO(wittjosiah): What is a good name for this module?
6
+ export * as CreateAtom from './atoms';
7
+ export * as Graph from './graph';
8
+ export * as GraphBuilder from './graph-builder';
9
+ export * as Node from './node';
10
+ export * as NodeMatcher from './node-matcher';
11
+
12
+ // TODO(wittjosiah): Direct re-export needed for portable type references.
13
+ export type { BuilderExtensions } from './graph-builder';
@@ -0,0 +1,301 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Option from 'effect/Option';
6
+ import { describe, expect, test } from 'vitest';
7
+
8
+ import { Obj } from '@dxos/echo';
9
+ import { TestSchema } from '@dxos/echo/testing';
10
+
11
+ import * as Node from './node';
12
+ import * as NodeMatcher from './node-matcher';
13
+
14
+ describe('NodeMatcher', () => {
15
+ describe('whenRoot', () => {
16
+ test('matches root node', () => {
17
+ const rootNode: Node.Node = {
18
+ id: Node.RootId,
19
+ type: Node.RootType,
20
+ properties: {},
21
+ data: null,
22
+ };
23
+ const result = NodeMatcher.whenRoot(rootNode);
24
+ expect(Option.isSome(result)).to.be.true;
25
+ expect(Option.getOrNull(result)).to.equal(rootNode);
26
+ });
27
+
28
+ test('does not match non-root node', () => {
29
+ const node: Node.Node = {
30
+ id: 'other',
31
+ type: 'test',
32
+ properties: {},
33
+ data: null,
34
+ };
35
+ const result = NodeMatcher.whenRoot(node);
36
+ expect(Option.isNone(result)).to.be.true;
37
+ });
38
+ });
39
+
40
+ describe('whenId', () => {
41
+ test('matches node by ID', () => {
42
+ const node: Node.Node = {
43
+ id: 'test-id',
44
+ type: 'test',
45
+ properties: {},
46
+ data: null,
47
+ };
48
+ const matcher = NodeMatcher.whenId('test-id');
49
+ const result = matcher(node);
50
+ expect(Option.isSome(result)).to.be.true;
51
+ expect(Option.getOrNull(result)).to.equal(node);
52
+ });
53
+
54
+ test('does not match different ID', () => {
55
+ const node: Node.Node = {
56
+ id: 'test-id',
57
+ type: 'test',
58
+ properties: {},
59
+ data: null,
60
+ };
61
+ const matcher = NodeMatcher.whenId('other-id');
62
+ const result = matcher(node);
63
+ expect(Option.isNone(result)).to.be.true;
64
+ });
65
+ });
66
+
67
+ describe('whenNodeType', () => {
68
+ test('matches node by type', () => {
69
+ const node: Node.Node = {
70
+ id: 'test',
71
+ type: 'test-type',
72
+ properties: {},
73
+ data: null,
74
+ };
75
+ const matcher = NodeMatcher.whenNodeType('test-type');
76
+ const result = matcher(node);
77
+ expect(Option.isSome(result)).to.be.true;
78
+ expect(Option.getOrNull(result)).to.equal(node);
79
+ });
80
+
81
+ test('does not match different type', () => {
82
+ const node: Node.Node = {
83
+ id: 'test',
84
+ type: 'test-type',
85
+ properties: {},
86
+ data: null,
87
+ };
88
+ const matcher = NodeMatcher.whenNodeType('other-type');
89
+ const result = matcher(node);
90
+ expect(Option.isNone(result)).to.be.true;
91
+ });
92
+ });
93
+
94
+ describe('whenEchoType', () => {
95
+ test('creates matcher function', () => {
96
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
97
+ expect(typeof matcher).to.equal('function');
98
+ });
99
+
100
+ test('returns none for non-instance', () => {
101
+ const node: Node.Node = {
102
+ id: 'test',
103
+ type: 'test',
104
+ properties: {},
105
+ data: { name: 'Test' },
106
+ };
107
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
108
+ const result = matcher(node);
109
+ expect(Option.isNone(result)).to.be.true;
110
+ });
111
+
112
+ test('returns some for instance of type', () => {
113
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
114
+ const node: Node.Node = {
115
+ id: 'test',
116
+ type: 'test',
117
+ properties: {},
118
+ data: testObject,
119
+ };
120
+ const matcher = NodeMatcher.whenEchoType(TestSchema.Person);
121
+ const result = matcher(node);
122
+ expect(Option.isSome(result)).to.be.true;
123
+ expect(Option.getOrNull(result)).to.equal(testObject);
124
+ });
125
+ });
126
+
127
+ describe('whenEchoObject', () => {
128
+ test('creates matcher function', () => {
129
+ const node: Node.Node = {
130
+ id: 'test',
131
+ type: 'test',
132
+ properties: {},
133
+ data: { name: 'Test' },
134
+ };
135
+ const result = NodeMatcher.whenEchoObject(node);
136
+ // Just verify the function works - actual object matching depends on Obj.isObject implementation.
137
+ expect(Option.isNone(result) || Option.isSome(result)).to.be.true;
138
+ });
139
+
140
+ test('does not match non-object data', () => {
141
+ const node: Node.Node = {
142
+ id: 'test',
143
+ type: 'test',
144
+ properties: {},
145
+ data: 'string',
146
+ };
147
+ const result = NodeMatcher.whenEchoObject(node);
148
+ expect(Option.isNone(result)).to.be.true;
149
+ });
150
+ });
151
+
152
+ describe('whenEchoTypeMatches', () => {
153
+ test('returns node for matching type', () => {
154
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
155
+ const node: Node.Node = {
156
+ id: 'test',
157
+ type: 'test',
158
+ properties: {},
159
+ data: testObject,
160
+ };
161
+ const matcher = NodeMatcher.whenEchoTypeMatches(TestSchema.Person);
162
+ const result = matcher(node);
163
+ expect(Option.isSome(result)).to.be.true;
164
+ expect(Option.getOrNull(result)).to.equal(node);
165
+ });
166
+
167
+ test('returns none for non-matching type', () => {
168
+ const node: Node.Node = {
169
+ id: 'test',
170
+ type: 'test',
171
+ properties: {},
172
+ data: { name: 'Test' },
173
+ };
174
+ const matcher = NodeMatcher.whenEchoTypeMatches(TestSchema.Person);
175
+ const result = matcher(node);
176
+ expect(Option.isNone(result)).to.be.true;
177
+ });
178
+ });
179
+
180
+ describe('whenEchoObjectMatches', () => {
181
+ test('returns node for ECHO object', () => {
182
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
183
+ const node: Node.Node = {
184
+ id: 'test',
185
+ type: 'test',
186
+ properties: {},
187
+ data: testObject,
188
+ };
189
+ const result = NodeMatcher.whenEchoObjectMatches(node);
190
+ expect(Option.isSome(result)).to.be.true;
191
+ expect(Option.getOrNull(result)).to.equal(node);
192
+ });
193
+
194
+ test('returns none for non-ECHO object', () => {
195
+ const node: Node.Node = {
196
+ id: 'test',
197
+ type: 'test',
198
+ properties: {},
199
+ data: 'string',
200
+ };
201
+ const result = NodeMatcher.whenEchoObjectMatches(node);
202
+ expect(Option.isNone(result)).to.be.true;
203
+ });
204
+ });
205
+
206
+ describe('whenNot', () => {
207
+ test('negates a matching matcher', () => {
208
+ const rootNode: Node.Node = {
209
+ id: Node.RootId,
210
+ type: Node.RootType,
211
+ properties: {},
212
+ data: null,
213
+ };
214
+ const matcher = NodeMatcher.whenNot(NodeMatcher.whenRoot);
215
+ const result = matcher(rootNode);
216
+ expect(Option.isNone(result)).to.be.true;
217
+ });
218
+
219
+ test('returns node when matcher does not match', () => {
220
+ const node: Node.Node = {
221
+ id: 'other',
222
+ type: 'test',
223
+ properties: {},
224
+ data: null,
225
+ };
226
+ const matcher = NodeMatcher.whenNot(NodeMatcher.whenRoot);
227
+ const result = matcher(node);
228
+ expect(Option.isSome(result)).to.be.true;
229
+ expect(Option.getOrNull(result)).to.equal(node);
230
+ });
231
+
232
+ test('works with whenAll for complex patterns', () => {
233
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
234
+ const node: Node.Node = {
235
+ id: 'test',
236
+ type: 'test',
237
+ properties: {},
238
+ data: testObject,
239
+ };
240
+ // Match ECHO objects that are NOT Person type.
241
+ const matcher = NodeMatcher.whenAll(
242
+ NodeMatcher.whenEchoObjectMatches,
243
+ NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(TestSchema.Person)),
244
+ );
245
+ const result = matcher(node);
246
+ expect(Option.isNone(result)).to.be.true;
247
+ });
248
+ });
249
+
250
+ describe('whenAll', () => {
251
+ test('matches when all matchers match', () => {
252
+ const node: Node.Node = {
253
+ id: 'test-id',
254
+ type: 'test-type',
255
+ properties: {},
256
+ data: null,
257
+ };
258
+ const matcher = NodeMatcher.whenAll(NodeMatcher.whenId('test-id'), NodeMatcher.whenNodeType('test-type'));
259
+ const result = matcher(node);
260
+ expect(Option.isSome(result)).to.be.true;
261
+ });
262
+
263
+ test('does not match when any matcher fails', () => {
264
+ const node: Node.Node = {
265
+ id: 'test-id',
266
+ type: 'test-type',
267
+ properties: {},
268
+ data: null,
269
+ };
270
+ const matcher = NodeMatcher.whenAll(NodeMatcher.whenId('test-id'), NodeMatcher.whenNodeType('other-type'));
271
+ const result = matcher(node);
272
+ expect(Option.isNone(result)).to.be.true;
273
+ });
274
+ });
275
+
276
+ describe('whenAny', () => {
277
+ test('matches when any matcher matches', () => {
278
+ const node: Node.Node = {
279
+ id: 'test-id',
280
+ type: 'test-type',
281
+ properties: {},
282
+ data: null,
283
+ };
284
+ const matcher = NodeMatcher.whenAny(NodeMatcher.whenId('other-id'), NodeMatcher.whenNodeType('test-type'));
285
+ const result = matcher(node);
286
+ expect(Option.isSome(result)).to.be.true;
287
+ });
288
+
289
+ test('does not match when all matchers fail', () => {
290
+ const node: Node.Node = {
291
+ id: 'test-id',
292
+ type: 'test-type',
293
+ properties: {},
294
+ data: null,
295
+ };
296
+ const matcher = NodeMatcher.whenAny(NodeMatcher.whenId('other-id'), NodeMatcher.whenNodeType('other-type'));
297
+ const result = matcher(node);
298
+ expect(Option.isNone(result)).to.be.true;
299
+ });
300
+ });
301
+ });
@@ -0,0 +1,284 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Option from 'effect/Option';
6
+ import type * as Schema from 'effect/Schema';
7
+
8
+ import { type Entity, Obj, type Type } from '@dxos/echo';
9
+
10
+ import * as Node from './node';
11
+
12
+ /**
13
+ * Type for a node matcher function that returns an Option of the matched data.
14
+ * Matchers are used to filter and transform nodes in the app graph.
15
+ *
16
+ * @template TData - The type of data returned when the matcher succeeds.
17
+ * Defaults to Node.Node, but can be a more specific type (e.g., an ECHO entity).
18
+ */
19
+ export type NodeMatcher<TData = Node.Node> = (node: Node.Node) => Option.Option<TData>;
20
+
21
+ //
22
+ // Basic Node Matchers
23
+ //
24
+
25
+ /**
26
+ * Matches the root node of the graph.
27
+ *
28
+ * @returns Option.some(node) if the node is the root, Option.none() otherwise.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * GraphBuilder.createExtension({
33
+ * id: 'my-extension',
34
+ * match: NodeMatcher.whenRoot,
35
+ * connector: (node) => Effect.succeed([...]),
36
+ * });
37
+ * ```
38
+ */
39
+ export const whenRoot = (node: Node.Node): Option.Option<Node.Node> =>
40
+ node.id === Node.RootId ? Option.some(node) : Option.none();
41
+
42
+ /**
43
+ * Matches a node by its exact ID.
44
+ *
45
+ * @param id - The node ID to match against.
46
+ * @returns A matcher that returns Option.some(node) if IDs match, Option.none() otherwise.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * GraphBuilder.createExtension({
51
+ * id: 'spaces-extension',
52
+ * match: NodeMatcher.whenId('spaces'),
53
+ * connector: (node) => Effect.succeed([...]),
54
+ * });
55
+ * ```
56
+ */
57
+ export const whenId =
58
+ (id: string) =>
59
+ (node: Node.Node): Option.Option<Node.Node> =>
60
+ node.id === id ? Option.some(node) : Option.none();
61
+
62
+ /**
63
+ * Matches a node by its type string (the `node.type` property).
64
+ *
65
+ * @param type - The node type string to match against.
66
+ * @returns A matcher that returns Option.some(node) if types match, Option.none() otherwise.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * GraphBuilder.createExtension({
71
+ * id: 'space-settings-extension',
72
+ * match: NodeMatcher.whenNodeType('dxos.org/plugin/space/settings'),
73
+ * connector: (node) => Effect.succeed([...]),
74
+ * });
75
+ * ```
76
+ */
77
+ export const whenNodeType =
78
+ (type: string) =>
79
+ (node: Node.Node): Option.Option<Node.Node> =>
80
+ node.type === type ? Option.some(node) : Option.none();
81
+
82
+ //
83
+ // ECHO Data Matchers
84
+ //
85
+
86
+ /**
87
+ * Matches a node whose data is an instance of the given ECHO schema type.
88
+ * Returns the **typed entity data** (not the node) for direct use in callbacks.
89
+ *
90
+ * Use this when you need to work directly with the typed ECHO entity in your
91
+ * connector or actions callback.
92
+ *
93
+ * @template T - The ECHO schema type to match against.
94
+ * @param type - The ECHO schema (e.g., `Collection.Collection`, `Document.Document`).
95
+ * @returns A matcher that returns Option.some(entity) if the data matches, Option.none() otherwise.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * GraphBuilder.createExtension({
100
+ * id: 'collection-extension',
101
+ * match: NodeMatcher.whenEchoType(Collection.Collection),
102
+ * connector: (collection) => {
103
+ * // `collection` is typed as Collection.Collection
104
+ * return Effect.succeed(collection.objects.map(...));
105
+ * },
106
+ * });
107
+ * ```
108
+ *
109
+ * @see {@link whenEchoTypeMatches} - Use instead when composing with whenAll/whenAny.
110
+ */
111
+ export const whenEchoType =
112
+ <T extends Type.Entity.Any>(type: T): NodeMatcher<Entity.Entity<Schema.Schema.Type<T>>> =>
113
+ (node: Node.Node): Option.Option<Entity.Entity<Schema.Schema.Type<T>>> =>
114
+ Obj.instanceOf(type, node.data) ? Option.some(node.data) : Option.none();
115
+
116
+ /**
117
+ * Matches a node whose data is any ECHO object.
118
+ * Returns the **object data** (not the node) for direct use in callbacks.
119
+ *
120
+ * Use this when you need to work with any ECHO object regardless of its specific type.
121
+ *
122
+ * @returns Option.some(object) if the node's data is an ECHO object, Option.none() otherwise.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * GraphBuilder.createExtension({
127
+ * id: 'object-settings',
128
+ * match: NodeMatcher.whenEchoObject,
129
+ * connector: (object) => {
130
+ * // `object` is typed as Obj.Unknown
131
+ * const id = Obj.getDXN(object).toString();
132
+ * return Effect.succeed([{ id: `${id}/settings`, ... }]);
133
+ * },
134
+ * });
135
+ * ```
136
+ *
137
+ * @see {@link whenEchoObjectMatches} - Use instead when composing with whenAll/whenAny.
138
+ */
139
+ export const whenEchoObject = (node: Node.Node): Option.Option<Obj.Unknown> =>
140
+ Obj.isObject(node.data) ? Option.some(node.data) : Option.none();
141
+
142
+ //
143
+ // Composition Matchers
144
+ //
145
+
146
+ /**
147
+ * Composes multiple matchers with AND logic - all matchers must match for success.
148
+ * Returns the **node** (not the matched data) to enable further composition.
149
+ *
150
+ * @param matchers - The matchers to combine. All must return Option.some for success.
151
+ * @returns A matcher that returns Option.some(node) if all match, Option.none() otherwise.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * // Match ECHO objects that are NOT Channels
156
+ * const whenCommentable = NodeMatcher.whenAll(
157
+ * NodeMatcher.whenEchoObjectMatches,
158
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
159
+ * );
160
+ * ```
161
+ */
162
+ export const whenAll =
163
+ (...matchers: NodeMatcher[]): NodeMatcher =>
164
+ (node: Node.Node): Option.Option<Node.Node> => {
165
+ for (const matcher of matchers) {
166
+ const result = matcher(node);
167
+ if (Option.isNone(result)) {
168
+ return Option.none();
169
+ }
170
+ }
171
+ return Option.some(node);
172
+ };
173
+
174
+ /**
175
+ * Composes multiple matchers with OR logic - at least one matcher must match.
176
+ * Returns the **node** (not the matched data) to enable further composition.
177
+ *
178
+ * @param matchers - The matchers to combine. At least one must return Option.some.
179
+ * @returns A matcher that returns Option.some(node) if any match, Option.none() otherwise.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * // Match nodes that are either Sequences or Prompts
184
+ * const whenInvocable = NodeMatcher.whenAny(
185
+ * NodeMatcher.whenEchoTypeMatches(Sequence),
186
+ * NodeMatcher.whenEchoTypeMatches(Prompt.Prompt),
187
+ * );
188
+ * ```
189
+ */
190
+ export const whenAny =
191
+ (...matchers: NodeMatcher[]): NodeMatcher =>
192
+ (node: Node.Node): Option.Option<Node.Node> => {
193
+ for (const matcher of matchers) {
194
+ const result = matcher(node);
195
+ if (Option.isSome(result)) {
196
+ return Option.some(node);
197
+ }
198
+ }
199
+ return Option.none();
200
+ };
201
+
202
+ /**
203
+ * Matches a node whose data is an instance of the given ECHO schema type.
204
+ * Returns the **node** (not the data) to enable composition with whenAll/whenAny/whenNot.
205
+ *
206
+ * Use this instead of {@link whenEchoType} when you need to combine matchers.
207
+ * The difference is what's returned:
208
+ * - `whenEchoType` returns the typed entity (for direct use)
209
+ * - `whenEchoTypeMatches` returns the node (for composition)
210
+ *
211
+ * @template T - The ECHO schema type to match against.
212
+ * @param type - The ECHO schema (e.g., `Channel.Channel`, `Document.Document`).
213
+ * @returns A matcher that returns Option.some(node) if the data matches, Option.none() otherwise.
214
+ *
215
+ * @example
216
+ * ```ts
217
+ * // Use with whenAny for OR logic
218
+ * const whenPresentable = NodeMatcher.whenAny(
219
+ * NodeMatcher.whenEchoTypeMatches(Collection.Collection),
220
+ * NodeMatcher.whenEchoTypeMatches(Markdown.Document),
221
+ * );
222
+ *
223
+ * // Use with whenNot for exclusion
224
+ * const whenNotChannel = NodeMatcher.whenNot(
225
+ * NodeMatcher.whenEchoTypeMatches(Channel.Channel),
226
+ * );
227
+ * ```
228
+ *
229
+ * @see {@link whenEchoType} - Use instead when you need the typed entity directly.
230
+ */
231
+ export const whenEchoTypeMatches =
232
+ <T extends Type.Entity.Any>(type: T): NodeMatcher =>
233
+ (node: Node.Node): Option.Option<Node.Node> =>
234
+ Obj.instanceOf(type, node.data) ? Option.some(node) : Option.none();
235
+
236
+ /**
237
+ * Matches a node whose data is any ECHO object.
238
+ * Returns the **node** (not the data) to enable composition with whenAll/whenAny/whenNot.
239
+ *
240
+ * Use this instead of {@link whenEchoObject} when you need to combine matchers.
241
+ * The difference is what's returned:
242
+ * - `whenEchoObject` returns the object data (for direct use)
243
+ * - `whenEchoObjectMatches` returns the node (for composition)
244
+ *
245
+ * @returns Option.some(node) if the node's data is an ECHO object, Option.none() otherwise.
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * // Match ECHO objects that are not system types
250
+ * const whenUserObject = NodeMatcher.whenAll(
251
+ * NodeMatcher.whenEchoObjectMatches,
252
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(SystemType)),
253
+ * );
254
+ * ```
255
+ *
256
+ * @see {@link whenEchoObject} - Use instead when you need the object data directly.
257
+ */
258
+ export const whenEchoObjectMatches = (node: Node.Node): Option.Option<Node.Node> =>
259
+ Obj.isObject(node.data) ? Option.some(node) : Option.none();
260
+
261
+ /**
262
+ * Negates a matcher - matches when the given matcher does NOT match.
263
+ * Useful for exclusion patterns like "any object EXCEPT type X".
264
+ *
265
+ * @param matcher - The matcher to negate.
266
+ * @returns A matcher that returns Option.some(node) if the input matcher returns none,
267
+ * and Option.none() if the input matcher returns some.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * // Match any ECHO object that is NOT a Channel
272
+ * const whenCommentable = NodeMatcher.whenAll(
273
+ * NodeMatcher.whenEchoObjectMatches,
274
+ * NodeMatcher.whenNot(NodeMatcher.whenEchoTypeMatches(Channel.Channel)),
275
+ * );
276
+ *
277
+ * // Match any node that is NOT the root
278
+ * const whenNotRoot = NodeMatcher.whenNot(NodeMatcher.whenRoot);
279
+ * ```
280
+ */
281
+ export const whenNot =
282
+ (matcher: NodeMatcher): NodeMatcher =>
283
+ (node: Node.Node): Option.Option<Node.Node> =>
284
+ Option.isNone(matcher(node)) ? Option.some(node) : Option.none();
package/src/node.ts CHANGED
@@ -2,9 +2,30 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type MakeOptional, type MaybePromise } from '@dxos/util';
5
+ import type * as Context from 'effect/Context';
6
+ import type * as Effect from 'effect/Effect';
6
7
 
7
- import { ACTION_GROUP_TYPE, ACTION_TYPE } from './graph';
8
+ import { type MakeOptional } from '@dxos/util';
9
+
10
+ /**
11
+ * Root node ID.
12
+ */
13
+ export const RootId = 'root';
14
+
15
+ /**
16
+ * Root node type.
17
+ */
18
+ export const RootType = 'dxos.org/type/GraphRoot';
19
+
20
+ /**
21
+ * Action node type.
22
+ */
23
+ export const ActionType = 'dxos.org/type/GraphAction';
24
+
25
+ /**
26
+ * Action group node type.
27
+ */
28
+ export const ActionGroupType = 'dxos.org/type/GraphActionGroup';
8
29
 
9
30
  /**
10
31
  * Represents a node in the graph.
@@ -70,23 +91,35 @@ export type NodeArg<TData, TProperties extends Record<string, any> = Record<stri
70
91
  // Actions
71
92
  //
72
93
 
73
- export type InvokeParams = {
94
+ export type InvokeProps = {
74
95
  /** Node the invoked action is connected to. */
75
96
  parent?: Node;
76
97
 
77
98
  caller?: string;
78
99
  };
79
100
 
80
- export type ActionData = (params?: InvokeParams) => MaybePromise<void>;
101
+ /**
102
+ * Action data is an Effect-returning function.
103
+ * The Effect is provided with captured context at execution time.
104
+ */
105
+ export type ActionData<R = never> = (params?: InvokeProps) => Effect.Effect<any, Error, R>;
106
+
107
+ /**
108
+ * Context captured at extension creation time.
109
+ * Automatically provided to action Effects at execution.
110
+ */
111
+ export type ActionContext = Context.Context<any>;
81
112
 
82
113
  export type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<
83
114
  Omit<Node<ActionData, TProperties>, 'properties'> & {
84
115
  properties: Readonly<TProperties>;
116
+ /** Captured context from extension creation. Provided automatically at action execution. */
117
+ _actionContext?: ActionContext;
85
118
  }
86
119
  >;
87
120
 
88
121
  export const isAction = (data: unknown): data is Action =>
89
- isGraphNode(data) ? typeof data.data === 'function' && data.type === ACTION_TYPE : false;
122
+ isGraphNode(data) ? typeof data.data === 'function' && data.type === ActionType : false;
90
123
 
91
124
  export const actionGroupSymbol = Symbol('ActionGroup');
92
125
 
@@ -97,7 +130,7 @@ export type ActionGroup<TProperties extends Record<string, any> = Record<string,
97
130
  >;
98
131
 
99
132
  export const isActionGroup = (data: unknown): data is ActionGroup =>
100
- isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ACTION_GROUP_TYPE : false;
133
+ isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ActionGroupType : false;
101
134
 
102
135
  export type ActionLike = Action | ActionGroup;
103
136