@lucern/graph-primitives 1.0.18 → 1.0.19

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.
@@ -2081,7 +2081,7 @@ function scopeFromTopicAnchor(topicNode) {
2081
2081
  }
2082
2082
  async function createRequiredBeliefTopicEdge(ctx, args) {
2083
2083
  const topicGlobalId = args.topicNode.globalId;
2084
- const edgeGlobalId = `edge:${args.beliefGlobalId}:${topicGlobalId}:scoped_by`;
2084
+ const edgeGlobalId = `edge:${args.beliefGlobalId}:${topicGlobalId}:belongs_to`;
2085
2085
  const now = Date.now();
2086
2086
  const existing = await ctx.db.query("epistemicEdges").withIndex("by_globalId", (q) => q.eq("globalId", edgeGlobalId)).first();
2087
2087
  if (!existing) {
@@ -2091,7 +2091,7 @@ async function createRequiredBeliefTopicEdge(ctx, args) {
2091
2091
  toNodeId: String(args.topicNode._id),
2092
2092
  sourceGlobalId: args.beliefGlobalId,
2093
2093
  targetGlobalId: topicGlobalId,
2094
- edgeType: "scoped_by",
2094
+ edgeType: "belongs_to",
2095
2095
  weight: 1,
2096
2096
  confidence: 1,
2097
2097
  context: "Belief creation topic anchor invariant.",
@@ -2115,7 +2115,7 @@ async function createRequiredBeliefTopicEdge(ctx, args) {
2115
2115
  globalId: edgeGlobalId,
2116
2116
  fromGlobalId: args.beliefGlobalId,
2117
2117
  toGlobalId: topicGlobalId,
2118
- edgeType: "scoped_by",
2118
+ edgeType: "belongs_to",
2119
2119
  weight: 1,
2120
2120
  confidence: 1,
2121
2121
  context: "Belief creation topic anchor invariant.",
@@ -3663,6 +3663,39 @@ var getWithEvidence = query({
3663
3663
  };
3664
3664
  }
3665
3665
  });
3666
+ async function collectRelationshipNodeRefs(ctx, nodeId) {
3667
+ const refs = /* @__PURE__ */ new Set([String(nodeId)]);
3668
+ const node = await ctx.db.get(nodeId);
3669
+ const globalId = typeof node?.globalId === "string" ? node.globalId.trim() : "";
3670
+ if (globalId.length > 0) {
3671
+ refs.add(globalId);
3672
+ }
3673
+ return [...refs];
3674
+ }
3675
+ function endpointMatches(q, fields, refs) {
3676
+ return q.or(
3677
+ ...fields.flatMap(
3678
+ (field) => refs.map((ref) => q.eq(q.field(field), ref))
3679
+ )
3680
+ );
3681
+ }
3682
+ async function resolveRelationshipEndpoint(ctx, endpointId, globalEndpointId) {
3683
+ const candidates = [endpointId, globalEndpointId].map((value) => typeof value === "string" ? value.trim() : "").filter((value, index, values) => value.length > 0 && values.indexOf(value) === index);
3684
+ for (const candidate of candidates) {
3685
+ try {
3686
+ const direct = await ctx.db.get(candidate);
3687
+ if (direct) {
3688
+ return direct;
3689
+ }
3690
+ } catch {
3691
+ }
3692
+ const byGlobalId = await ctx.db.query("epistemicNodes").withIndex("by_globalId", (q) => q.eq("globalId", candidate)).first();
3693
+ if (byGlobalId) {
3694
+ return byGlobalId;
3695
+ }
3696
+ }
3697
+ return null;
3698
+ }
3666
3699
  var getRelationships = query({
3667
3700
  args: {
3668
3701
  nodeId: v.id("epistemicNodes"),
@@ -3677,17 +3710,38 @@ var getRelationships = query({
3677
3710
  incoming: [],
3678
3711
  outgoing: []
3679
3712
  };
3713
+ const nodeRefs = await collectRelationshipNodeRefs(ctx, args.nodeId);
3680
3714
  if (direction === "in" || direction === "both") {
3681
- const inEdges = await ctx.db.query("epistemicEdges").filter((q) => q.eq(q.field("toNodeId"), args.nodeId)).collect();
3715
+ const inEdges = await ctx.db.query("epistemicEdges").filter(
3716
+ (q) => endpointMatches(
3717
+ q,
3718
+ ["toNodeId", "targetGlobalId", "toGlobalId", "toUuid"],
3719
+ nodeRefs
3720
+ )
3721
+ ).collect();
3682
3722
  for (const edge of inEdges) {
3683
- const sourceNode = await ctx.db.get(edge.fromNodeId);
3723
+ const sourceNode = await resolveRelationshipEndpoint(
3724
+ ctx,
3725
+ edge.fromNodeId,
3726
+ edge.sourceGlobalId ?? edge.fromGlobalId ?? edge.fromUuid
3727
+ );
3684
3728
  results.incoming.push({ edge, node: sourceNode });
3685
3729
  }
3686
3730
  }
3687
3731
  if (direction === "out" || direction === "both") {
3688
- const outEdges = await ctx.db.query("epistemicEdges").filter((q) => q.eq(q.field("fromNodeId"), args.nodeId)).collect();
3732
+ const outEdges = await ctx.db.query("epistemicEdges").filter(
3733
+ (q) => endpointMatches(
3734
+ q,
3735
+ ["fromNodeId", "sourceGlobalId", "fromGlobalId", "fromUuid"],
3736
+ nodeRefs
3737
+ )
3738
+ ).collect();
3689
3739
  for (const edge of outEdges) {
3690
- const targetNode = edge.toNodeId ? await ctx.db.get(edge.toNodeId) : null;
3740
+ const targetNode = await resolveRelationshipEndpoint(
3741
+ ctx,
3742
+ edge.toNodeId,
3743
+ edge.targetGlobalId ?? edge.toGlobalId ?? edge.toUuid
3744
+ );
3691
3745
  results.outgoing.push({ edge, node: targetNode });
3692
3746
  }
3693
3747
  }