@typicalday/firegraph 0.1.0 → 0.3.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > **Warning:** This library is experimental. APIs may change without notice between releases.
4
4
 
5
- A typed graph data layer for Firebase Cloud Firestore. Store nodes and edges in a single collection with smart query planning, sharded document IDs, optional schema validation, and multi-hop traversal.
5
+ A typed graph data layer for Firebase Cloud Firestore. Store nodes and edges as triples in a Firestore collection with smart query planning, sharded document IDs, optional schema validation, multi-hop traversal, and nested subgraphs.
6
6
 
7
7
  ## Install
8
8
 
@@ -57,7 +57,7 @@ const departures = await g.findEdges({ aUid: tourId, axbType: 'hasDeparture' });
57
57
 
58
58
  ### Graph Model
59
59
 
60
- Firegraph stores everything as **triples** in a single Firestore collection:
60
+ Firegraph stores everything as **triples** in a Firestore collection (with optional nested subcollections for [subgraphs](#subgraphs)):
61
61
 
62
62
  ```
63
63
  (aType, aUid) -[axbType]-> (bType, bUid)
@@ -193,7 +193,7 @@ await batch.commit();
193
193
 
194
194
  ### Graph Traversal
195
195
 
196
- Multi-hop traversal with budget enforcement, concurrency control, and in-memory filtering:
196
+ Multi-hop traversal with budget enforcement, concurrency control, in-memory filtering, and cross-graph hops:
197
197
 
198
198
  ```typescript
199
199
  import { createTraversal } from 'firegraph';
@@ -213,6 +213,8 @@ result.totalReads; // number — Firestore reads consumed
213
213
  result.truncated; // boolean — true if budget was hit
214
214
  ```
215
215
 
216
+ `createTraversal` accepts a `GraphClient` or `GraphReader`. When passed a `GraphClient`, cross-graph hops via `targetGraph` are supported (see [Cross-Graph Edges](#cross-graph-edges)).
217
+
216
218
  #### Reverse Traversal
217
219
 
218
220
  Walk edges backwards to find parents:
@@ -249,6 +251,7 @@ await g.runTransaction(async (tx) => {
249
251
  | `limit` | `number` | `10` | Max edges per source node |
250
252
  | `orderBy` | `{ field, direction? }` | — | Firestore-level ordering |
251
253
  | `filter` | `(edge) => boolean` | — | In-memory post-filter |
254
+ | `targetGraph` | `string` | — | Subgraph to cross into (forward only). See [Cross-Graph Edges](#cross-graph-edges) |
252
255
 
253
256
  #### Run Options
254
257
 
@@ -341,6 +344,241 @@ Key behaviors:
341
344
 
342
345
  Dynamic registry returns a `DynamicGraphClient` which extends `GraphClient` with `defineNodeType()`, `defineEdgeType()`, and `reloadRegistry()`. Transactions and batches also validate against the compiled dynamic registry.
343
346
 
347
+ ### Subgraphs
348
+
349
+ Create isolated graph namespaces inside a parent node's Firestore document as subcollections. Each subgraph is a full `GraphClient` scoped to its own collection path.
350
+
351
+ ```typescript
352
+ const agentId = generateId();
353
+ await g.putNode('agent', agentId, { name: 'ResearchBot' });
354
+
355
+ // Create a subgraph under the agent's document
356
+ const memories = g.subgraph(agentId, 'memories');
357
+
358
+ // CRUD works exactly like the parent client
359
+ const memId = generateId();
360
+ await memories.putNode('memory', memId, { text: 'The sky is blue' });
361
+ const mem = await memories.getNode(memId);
362
+
363
+ // Subgraph data is isolated — parent can't see it
364
+ const parentNodes = await g.findNodes({ aType: 'memory', allowCollectionScan: true });
365
+ // → [] (empty — memories live in the subcollection)
366
+ ```
367
+
368
+ #### Nested Subgraphs
369
+
370
+ Subgraphs can be nested to any depth:
371
+
372
+ ```typescript
373
+ const workspace = g.subgraph(agentId, 'workspace');
374
+ const taskId = generateId();
375
+ await workspace.putNode('task', taskId, { name: 'Analyze data' });
376
+
377
+ // Nest further
378
+ const subtasks = workspace.subgraph(taskId, 'subtasks');
379
+ await subtasks.putNode('subtask', generateId(), { name: 'Parse CSV' });
380
+ ```
381
+
382
+ #### Scope Constraints (`allowedIn`)
383
+
384
+ Registry entries support `allowedIn` patterns that restrict where a type can be used:
385
+
386
+ ```typescript
387
+ const registry = createRegistry([
388
+ { aType: 'agent', axbType: 'is', bType: 'agent', allowedIn: ['root'] },
389
+ { aType: 'memory', axbType: 'is', bType: 'memory', allowedIn: ['**/memories'] },
390
+ { aType: 'task', axbType: 'is', bType: 'task', allowedIn: ['workspace', '**/workspace'] },
391
+ ]);
392
+
393
+ const g = createGraphClient(db, 'graph', { registry });
394
+
395
+ // Agent only at root
396
+ await g.putNode('agent', agentId, {}); // OK
397
+ await memories.putNode('agent', generateId(), {}); // throws RegistryScopeError
398
+
399
+ // Memory only in 'memories' subgraphs
400
+ await memories.putNode('memory', generateId(), {}); // OK
401
+ await g.putNode('memory', generateId(), {}); // throws RegistryScopeError
402
+ ```
403
+
404
+ **Pattern syntax:**
405
+
406
+ | Pattern | Matches |
407
+ |---------|---------|
408
+ | `root` | Top-level collection only |
409
+ | `memories` | Exact subgraph name |
410
+ | `workspace/tasks` | Exact path |
411
+ | `*/memories` | `*` matches one segment |
412
+ | `**/memories` | `**` matches zero or more segments |
413
+ | `**` | Everything including root |
414
+
415
+ Omitting `allowedIn` (or passing an empty array) means the type is allowed everywhere.
416
+
417
+ #### Transactions & Batches in Subgraphs
418
+
419
+ ```typescript
420
+ const sub = g.subgraph(agentId, 'memories');
421
+
422
+ // Transaction
423
+ await sub.runTransaction(async (tx) => {
424
+ const node = await tx.getNode(memId);
425
+ await tx.putNode('memory', memId, { text: 'updated' });
426
+ });
427
+
428
+ // Batch
429
+ const batch = sub.batch();
430
+ await batch.putNode('memory', generateId(), { text: 'first' });
431
+ await batch.putNode('memory', generateId(), { text: 'second' });
432
+ await batch.commit();
433
+ ```
434
+
435
+ #### Cascade Delete
436
+
437
+ `removeNodeCascade` recursively deletes subcollections by default:
438
+
439
+ ```typescript
440
+ // Deletes the agent node, all its edges, and all subgraph data
441
+ await g.removeNodeCascade(agentId);
442
+
443
+ // To preserve subgraph data:
444
+ await g.removeNodeCascade(agentId, { deleteSubcollections: false });
445
+ ```
446
+
447
+ #### Firestore Path Layout
448
+
449
+ ```
450
+ graph/ ← root collection
451
+ {agentId} ← agent node document
452
+ {agentId}/memories/ ← subgraph subcollection
453
+ {memId} ← memory node document
454
+ {shard:aUid:rel:bUid} ← edge document
455
+ {agentId}/workspace/ ← another subgraph
456
+ {taskId} ← task node document
457
+ {taskId}/subtasks/ ← nested subgraph
458
+ {subtaskId} ← subtask node document
459
+ ```
460
+
461
+ ### Cross-Graph Edges
462
+
463
+ Edges that connect nodes across different subgraphs. The key rule: **edges live with the target node**. A cross-graph edge is stored in the same collection as its target (bUid), while the source (aUid) may be a parent node in an ancestor graph.
464
+
465
+ ```typescript
466
+ import { createGraphClient, createRegistry, createTraversal, generateId } from 'firegraph';
467
+
468
+ // Registry declares that 'assignedTo' edges live in the 'workflow' subgraph
469
+ const registry = createRegistry([
470
+ { aType: 'task', axbType: 'is', bType: 'task', jsonSchema: taskSchema },
471
+ { aType: 'agent', axbType: 'is', bType: 'agent', jsonSchema: agentSchema },
472
+ { aType: 'task', axbType: 'assignedTo', bType: 'agent', targetGraph: 'workflow' },
473
+ ]);
474
+
475
+ const g = createGraphClient(db, 'graph', { registry });
476
+
477
+ // Create a task in the root graph
478
+ const taskId = generateId();
479
+ await g.putNode('task', taskId, { title: 'Build API' });
480
+
481
+ // Create agents in a workflow subgraph under the task
482
+ const workflow = g.subgraph(taskId, 'workflow');
483
+ const agentId = generateId();
484
+ await workflow.putNode('agent', agentId, { name: 'Backend Dev' });
485
+
486
+ // Create the cross-graph edge in the workflow subgraph
487
+ // The edge lives alongside the target (agent), source (task) is an ancestor
488
+ await workflow.putEdge('task', taskId, 'assignedTo', 'agent', agentId, { role: 'lead' });
489
+
490
+ // Forward traversal: task → agents (automatically crosses into workflow subgraph)
491
+ const result = await createTraversal(g, taskId, registry)
492
+ .follow('assignedTo')
493
+ .run();
494
+ // result.nodes contains the agent edges from the workflow subgraph
495
+ ```
496
+
497
+ #### How It Works
498
+
499
+ 1. **Writing**: You explicitly call `putEdge` on the subgraph client where the target node lives. The caller decides where the edge goes.
500
+
501
+ 2. **Reverse traversal is free**: Since the edge lives with the target, querying from the agent's perspective (`findEdges({ bUid: agentId })` on the workflow client) finds it locally.
502
+
503
+ 3. **Forward traversal uses `targetGraph`**: When traversing from the task, the engine sees `targetGraph: 'workflow'` on the registry entry and queries `g.subgraph(taskId, 'workflow')` automatically.
504
+
505
+ 4. **Path-scanning resolution**: To determine if an edge's `aUid` is an ancestor node, firegraph parses the Firestore collection path. The path `graph/taskId/workflow` reveals that `taskId` is a document in the `graph` collection.
506
+
507
+ #### Registry `targetGraph`
508
+
509
+ The `targetGraph` field on a `RegistryEntry` tells forward traversal which subgraph to query under each source node:
510
+
511
+ ```typescript
512
+ createRegistry([
513
+ // When traversing forward from a task along 'assignedTo',
514
+ // look in the 'workflow' subgraph under each task
515
+ { aType: 'task', axbType: 'assignedTo', bType: 'agent', targetGraph: 'workflow' },
516
+ ]);
517
+ ```
518
+
519
+ `targetGraph` must be a single segment (no `/`). It can also be set in entity discovery via `edge.json`:
520
+
521
+ ```json
522
+ { "from": "task", "to": "agent", "targetGraph": "workflow" }
523
+ ```
524
+
525
+ #### Explicit Hop Override
526
+
527
+ You can override the registry's `targetGraph` on a per-hop basis:
528
+
529
+ ```typescript
530
+ // Use 'team' subgraph instead of registry's default
531
+ const result = await createTraversal(g, taskId)
532
+ .follow('assignedTo', { targetGraph: 'team' })
533
+ .run();
534
+ ```
535
+
536
+ Resolution priority: explicit hop `targetGraph` > registry `targetGraph` > no cross-graph.
537
+
538
+ #### `findEdgesGlobal` — Collection Group Queries
539
+
540
+ For cross-cutting reads across all subgraphs, use `findEdgesGlobal`:
541
+
542
+ ```typescript
543
+ // Find all 'assignedTo' edges across all 'workflow' subgraphs in the database
544
+ const allAssignments = await g.findEdgesGlobal(
545
+ { axbType: 'assignedTo', allowCollectionScan: true },
546
+ 'workflow', // collection name to query across
547
+ );
548
+ ```
549
+
550
+ This uses Firestore collection group queries and requires collection group indexes. The collection name defaults to the last segment of the client's collection path if omitted.
551
+
552
+ #### Multi-Hop Limitation
553
+
554
+ Each hop resolves its reader from the root client. If hop 1 crosses into a subgraph, hop 2 does **not** stay in that subgraph — it reverts to the root. To chain hops within a subgraph, create a separate traversal from the subgraph client:
555
+
556
+ ```typescript
557
+ // This traversal finds agents in the workflow subgraph
558
+ const agents = await createTraversal(g, taskId, registry)
559
+ .follow('assignedTo')
560
+ .run();
561
+
562
+ // To continue traversing within the workflow subgraph,
563
+ // create a new traversal from the subgraph client
564
+ const workflow = g.subgraph(taskId, 'workflow');
565
+ for (const agent of agents.nodes) {
566
+ const mentees = await createTraversal(workflow, agent.bUid)
567
+ .follow('mentors')
568
+ .run();
569
+ }
570
+ ```
571
+
572
+ #### Firestore Path Layout
573
+
574
+ ```
575
+ graph/ <- root collection
576
+ {taskId} <- task node
577
+ {taskId}/workflow/ <- workflow subgraph
578
+ {agentId} <- agent node
579
+ {shard:taskId:assignedTo:agentId} <- cross-graph edge
580
+ ```
581
+
344
582
  ### ID Generation
345
583
 
346
584
  ```typescript
@@ -360,8 +598,10 @@ All errors extend `FiregraphError` with a `code` property:
360
598
  | `EdgeNotFoundError` | `EDGE_NOT_FOUND` | Edge lookup fails |
361
599
  | `ValidationError` | `VALIDATION_ERROR` | Schema validation fails (registry + Zod) |
362
600
  | `RegistryViolationError` | `REGISTRY_VIOLATION` | Triple not registered |
601
+ | `RegistryScopeError` | `REGISTRY_SCOPE` | Type not allowed at this subgraph scope |
363
602
  | `DynamicRegistryError` | `DYNAMIC_REGISTRY_ERROR` | Dynamic registry misconfiguration or misuse |
364
603
  | `InvalidQueryError` | `INVALID_QUERY` | `findEdges` called with no filters |
604
+ | `QuerySafetyError` | `QUERY_SAFETY` | Query would cause a full collection scan |
365
605
  | `TraversalError` | `TRAVERSAL_ERROR` | `run()` called with zero hops |
366
606
 
367
607
  ```typescript
@@ -403,8 +643,9 @@ import type {
403
643
  GraphClientOptions,
404
644
 
405
645
  // Registry
406
- RegistryEntry,
407
- GraphRegistry,
646
+ RegistryEntry, // includes targetGraph, allowedIn
647
+ GraphRegistry, // includes lookupByAxbType
648
+ EdgeTopology, // includes targetGraph
408
649
 
409
650
  // Dynamic Registry
410
651
  DynamicGraphClient,
@@ -413,11 +654,15 @@ import type {
413
654
  EdgeTypeData,
414
655
 
415
656
  // Traversal
416
- HopDefinition,
657
+ HopDefinition, // includes targetGraph
417
658
  TraversalOptions,
418
659
  HopResult,
419
660
  TraversalResult,
420
661
  TraversalBuilder,
662
+
663
+ // Entity Discovery
664
+ DiscoveredEntity,
665
+ DiscoveryResult,
421
666
  } from 'firegraph';
422
667
  ```
423
668
 
@@ -449,6 +694,8 @@ When you call `findEdges`, the query planner decides the strategy:
449
694
 
450
695
  1. Start with `sourceUids = [startUid]`
451
696
  2. For each hop in sequence:
697
+ - Resolve `targetGraph`: check hop override, then registry, then none
698
+ - If cross-graph (forward + `targetGraph` + `GraphClient` reader): create a subgraph reader via `reader.subgraph(sourceUid, targetGraph)` for each source
452
699
  - Fan out: query edges for each source UID (parallel, bounded by semaphore)
453
700
  - Each `findEdges` call counts as 1 read against the budget
454
701
  - Apply in-memory `filter` if specified, then apply `limit`
package/bin/firegraph.mjs CHANGED
@@ -86,6 +86,45 @@ if (subcommand === 'editor') {
86
86
  console.error(`Error: ${err.message}`);
87
87
  process.exit(1);
88
88
  }
89
+ } else if (subcommand === 'indexes') {
90
+ const args = parseArgs(process.argv.slice(3));
91
+ const entitiesDir = args.entities ? path.resolve(args.entities) : null;
92
+ const collection = args.collection || 'graph';
93
+ const outPath = args.out || null;
94
+
95
+ const distIndex = path.join(__dirname, '..', 'dist', 'index.js');
96
+ const { generateIndexConfig, discoverEntities } = await import(distIndex);
97
+
98
+ try {
99
+ let entities = undefined;
100
+ if (entitiesDir) {
101
+ const { result, warnings } = discoverEntities(entitiesDir);
102
+ for (const w of warnings) {
103
+ console.warn(` warning: ${w.message}`);
104
+ }
105
+ entities = result;
106
+ const nodeCount = result.nodes.size;
107
+ const edgeCount = result.edges.size;
108
+ if (nodeCount > 0 || edgeCount > 0) {
109
+ console.error(`Discovered ${nodeCount} node type(s) + ${edgeCount} edge type(s)`);
110
+ }
111
+ }
112
+
113
+ const config = generateIndexConfig(collection, entities);
114
+ const output = JSON.stringify(config, null, 2) + '\n';
115
+
116
+ if (outPath) {
117
+ const resolved = path.resolve(outPath);
118
+ fs.mkdirSync(path.dirname(resolved), { recursive: true });
119
+ fs.writeFileSync(resolved, output, 'utf-8');
120
+ console.log(`Generated ${config.indexes.length} index(es) → ${resolved}`);
121
+ } else {
122
+ process.stdout.write(output);
123
+ }
124
+ } catch (err) {
125
+ console.error(`Error: ${err.message}`);
126
+ process.exit(1);
127
+ }
89
128
  } else if (subcommand === '--help' || subcommand === '-h' || !subcommand) {
90
129
  console.log('');
91
130
  console.log(' Usage: firegraph <command> [options]');
@@ -94,6 +133,7 @@ if (subcommand === 'editor') {
94
133
  console.log(' editor Launch the Firegraph Editor UI');
95
134
  console.log(' query Query the graph via the editor API');
96
135
  console.log(' codegen Generate TypeScript types from entity schemas');
136
+ console.log(' indexes Generate recommended Firestore index definitions');
97
137
  console.log('');
98
138
  console.log(' Editor options:');
99
139
  console.log(' --config <path> Path to firegraph.config.ts (default: auto-discover in cwd)');
@@ -111,6 +151,11 @@ if (subcommand === 'editor') {
111
151
  console.log(' --entities <path> Path to entities directory (default: ./entities)');
112
152
  console.log(' --out <path> Output file path (default: stdout)');
113
153
  console.log('');
154
+ console.log(' Indexes options:');
155
+ console.log(' --entities <path> Path to entities directory (adds per-entity data field indexes)');
156
+ console.log(' --collection <name> Firestore collection name (default: graph)');
157
+ console.log(' --out <path> Output file path (default: stdout)');
158
+ console.log('');
114
159
  console.log(' Config file:');
115
160
  console.log(' Create a firegraph.config.ts in your project root to avoid passing');
116
161
  console.log(' flags every time. CLI flags override config file values.');
@@ -121,6 +166,8 @@ if (subcommand === 'editor') {
121
166
  console.log(' npx firegraph editor --entities ./entities # per-entity convention');
122
167
  console.log(' npx firegraph codegen --entities ./entities # types to stdout');
123
168
  console.log(' npx firegraph codegen --entities ./entities --out src/generated/types.ts');
169
+ console.log(' npx firegraph indexes # 4 base indexes to stdout');
170
+ console.log(' npx firegraph indexes --entities ./entities --out firestore.indexes.json');
124
171
  console.log('');
125
172
  } else {
126
173
  console.error(`Unknown command: ${subcommand}`);
@@ -1,2 +1,2 @@
1
- export { k as CodegenOptions, I as generateTypes } from '../index-CG3R68Hu.cjs';
1
+ export { l as CodegenOptions, J as generateTypes } from '../index-CQkofEC_.cjs';
2
2
  import '@google-cloud/firestore';
@@ -1,2 +1,2 @@
1
- export { k as CodegenOptions, I as generateTypes } from '../index-CG3R68Hu.js';
1
+ export { l as CodegenOptions, J as generateTypes } from '../index-CQkofEC_.js';
2
2
  import '@google-cloud/firestore';