@trustgraph/react-state 1.4.2 → 1.4.3

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/dist/index.cjs CHANGED
@@ -926,17 +926,19 @@ const useTriples = ({ flow, s, p, o, limit, collection }) => {
926
926
  const notify = useNotification();
927
927
  // Settings for default collection
928
928
  const { settings } = useSettings();
929
- if (!flow)
930
- flow = "default";
929
+ // Session state for default flow ID
930
+ const sessionFlowId = useSessionStore((state) => state.flowId);
931
+ // Use explicit param if provided, otherwise fall back to session state
932
+ const effectiveFlow = flow ?? sessionFlowId;
931
933
  /**
932
934
  * Query for fetching all token costs
933
935
  * Uses React Query for caching and background refetching
934
936
  */
935
937
  const query = reactQuery.useQuery({
936
- queryKey: ["triples", { flow, s, p, o, limit }],
938
+ queryKey: ["triples", { flow: effectiveFlow, s, p, o, limit }],
937
939
  queryFn: () => {
938
940
  return socket
939
- .flow(flow)
941
+ .flow(effectiveFlow)
940
942
  .triplesQuery(s, p, o, limit, collection || settings.collection)
941
943
  .then((x) => {
942
944
  if (x["error"]) {
@@ -1302,13 +1304,17 @@ const updateSubgraphByRelationship = (socket, selectedNodeId, relationshipUri, d
1302
1304
  * Custom hook for managing graph visualization operations using React Query
1303
1305
  * Provides functionality for fetching and updating graph subgraphs
1304
1306
  * @param entityUri - The URI of the entity to build the graph around
1305
- * @param flowId - The flow ID to use for the query
1307
+ * @param flow - Optional flow ID to use for the query (defaults to session state)
1306
1308
  * @param collection - The collection to query
1307
1309
  * @returns {Object} Graph state and operations
1308
1310
  */
1309
- const useGraphSubgraph = (entityUri, flowId, collection) => {
1311
+ const useGraphSubgraph = ({ entityUri, flow, collection, }) => {
1310
1312
  // WebSocket connection for communicating with the graph service
1311
1313
  const socket = reactProvider.useSocket();
1314
+ // Session state for default flow ID
1315
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1316
+ // Use explicit param if provided, otherwise fall back to session state
1317
+ const effectiveFlow = flow ?? sessionFlowId;
1312
1318
  const addActivity = useProgressStateStore((state) => state.addActivity);
1313
1319
  const removeActivity = useProgressStateStore((state) => state.removeActivity);
1314
1320
  // Hook for displaying user notifications
@@ -1320,29 +1326,29 @@ const useGraphSubgraph = (entityUri, flowId, collection) => {
1320
1326
  * Uses React Query for caching and background refetching
1321
1327
  */
1322
1328
  const query = reactQuery.useQuery({
1323
- queryKey: ["graph-subgraph", { entityUri, flowId, collection }],
1329
+ queryKey: ["graph-subgraph", { entityUri, flow: effectiveFlow, collection }],
1324
1330
  queryFn: async () => {
1325
1331
  if (!entityUri) {
1326
1332
  throw new Error("Entity URI is required");
1327
1333
  }
1328
1334
  const sg = createSubgraph();
1329
1335
  // Use the existing updateSubgraph utility function for initial load
1330
- const api = socket.flow(flowId);
1336
+ const api = socket.flow(effectiveFlow);
1331
1337
  return updateSubgraph(api, entityUri, sg, addActivity, removeActivity, collection);
1332
1338
  },
1333
- enabled: !!entityUri && !!flowId, // Only run query if both entityUri and flowId are available
1339
+ enabled: !!entityUri && !!effectiveFlow, // Only run query if both entityUri and effectiveFlow are available
1334
1340
  });
1335
1341
  /**
1336
1342
  * Mutation for updating the graph subgraph when nodes are clicked
1337
1343
  */
1338
1344
  const updateMutation = reactQuery.useMutation({
1339
1345
  mutationFn: async ({ nodeId, currentGraph, }) => {
1340
- const api = socket.flow(flowId);
1346
+ const api = socket.flow(effectiveFlow);
1341
1347
  return updateSubgraph(api, nodeId, currentGraph, addActivity, removeActivity);
1342
1348
  },
1343
1349
  onSuccess: (newGraph) => {
1344
1350
  // Update the cache with the new graph data
1345
- queryClient.setQueryData(["graph-subgraph", { entityUri, flowId, collection }], newGraph);
1351
+ queryClient.setQueryData(["graph-subgraph", { entityUri, flow: effectiveFlow, collection }], newGraph);
1346
1352
  },
1347
1353
  onError: (err) => {
1348
1354
  console.log("Graph update error:", err);
@@ -1354,12 +1360,12 @@ const useGraphSubgraph = (entityUri, flowId, collection) => {
1354
1360
  */
1355
1361
  const relationshipNavigationMutation = reactQuery.useMutation({
1356
1362
  mutationFn: async ({ selectedNodeId, relationshipUri, direction, currentGraph, }) => {
1357
- const api = socket.flow(flowId);
1363
+ const api = socket.flow(effectiveFlow);
1358
1364
  return updateSubgraphByRelationship(api, selectedNodeId, relationshipUri, direction, currentGraph, addActivity, removeActivity, collection);
1359
1365
  },
1360
1366
  onSuccess: (newGraph) => {
1361
1367
  // Update the cache with the new graph data
1362
- queryClient.setQueryData(["graph-subgraph", { entityUri, flowId, collection }], newGraph);
1368
+ queryClient.setQueryData(["graph-subgraph", { entityUri, flow: effectiveFlow, collection }], newGraph);
1363
1369
  },
1364
1370
  onError: (err) => {
1365
1371
  console.log("Relationship navigation error:", err);
@@ -1405,17 +1411,19 @@ const useGraphEmbeddings = ({ flow, vecs, limit, collection }) => {
1405
1411
  const notify = useNotification();
1406
1412
  // Settings for default collection
1407
1413
  const { settings } = useSettings();
1408
- if (!flow)
1409
- flow = "default";
1414
+ // Session state for default flow ID
1415
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1416
+ // Use explicit param if provided, otherwise fall back to session state
1417
+ const effectiveFlow = flow ?? sessionFlowId;
1410
1418
  /**
1411
1419
  * Query for fetching graph embeddings
1412
1420
  * Uses React Query for caching and background refetching
1413
1421
  */
1414
1422
  const query = reactQuery.useQuery({
1415
- queryKey: ["graph-embeddings", { vecs, limit }],
1423
+ queryKey: ["graph-embeddings", { flow: effectiveFlow, vecs, limit }],
1416
1424
  queryFn: () => {
1417
1425
  return socket
1418
- .flow(flow)
1426
+ .flow(effectiveFlow)
1419
1427
  .graphEmbeddingsQuery(vecs, limit, collection || settings.collection)
1420
1428
  .then((x) => {
1421
1429
  return x;
@@ -1621,6 +1629,8 @@ const useVectorSearch = () => {
1621
1629
  const removeActivity = useProgressStateStore((state) => state.removeActivity);
1622
1630
  // Hook for displaying user notifications
1623
1631
  const notify = useNotification();
1632
+ // Session state for default flow ID
1633
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1624
1634
  // State to track current search parameters
1625
1635
  const [searchParams, setSearchParams] = react.useState(null);
1626
1636
  /**
@@ -1632,7 +1642,7 @@ const useVectorSearch = () => {
1632
1642
  enabled: !!searchParams?.term,
1633
1643
  queryFn: () => {
1634
1644
  const { flow, term, limit, collection } = searchParams;
1635
- return vectorSearch(socket, flow || "default", addActivity, removeActivity, term, collection, limit)
1645
+ return vectorSearch(socket, flow ?? sessionFlowId, addActivity, removeActivity, term, collection, limit)
1636
1646
  .then((x) => {
1637
1647
  if (x["error"]) {
1638
1648
  console.log("Error:", x);
@@ -1654,7 +1664,7 @@ const useVectorSearch = () => {
1654
1664
  return;
1655
1665
  }
1656
1666
  setSearchParams({
1657
- flow: flow || "default",
1667
+ flow: flow ?? sessionFlowId,
1658
1668
  term,
1659
1669
  limit: limit || 10,
1660
1670
  collection,
@@ -1678,13 +1688,17 @@ const useVectorSearch = () => {
1678
1688
  * Custom hook for managing entity detail operations using React Query
1679
1689
  * Provides functionality for fetching entity details and related triples
1680
1690
  * @param entityUri - The URI of the entity to fetch details for
1681
- * @param flowId - The flow ID to use for the query
1691
+ * @param flow - Optional flow ID to use for the query (defaults to session state)
1682
1692
  * @param collection - The collection to query
1683
1693
  * @returns {Object} Entity detail state and operations
1684
1694
  */
1685
- const useEntityDetail = (entityUri, flowId, collection) => {
1695
+ const useEntityDetail = ({ entityUri, flow, collection, }) => {
1686
1696
  // WebSocket connection for communicating with the graph service
1687
1697
  const socket = reactProvider.useSocket();
1698
+ // Session state for default flow ID
1699
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1700
+ // Use explicit param if provided, otherwise fall back to session state
1701
+ const effectiveFlow = flow ?? sessionFlowId;
1688
1702
  const addActivity = useProgressStateStore((state) => state.addActivity);
1689
1703
  const removeActivity = useProgressStateStore((state) => state.removeActivity);
1690
1704
  // Hook for displaying user notifications
@@ -1694,17 +1708,17 @@ const useEntityDetail = (entityUri, flowId, collection) => {
1694
1708
  * Uses React Query for caching and background refetching
1695
1709
  */
1696
1710
  const query = reactQuery.useQuery({
1697
- queryKey: ["entity-detail", { entityUri, flowId, collection }],
1711
+ queryKey: ["entity-detail", { entityUri, flow: effectiveFlow, collection }],
1698
1712
  queryFn: async () => {
1699
1713
  if (!entityUri) {
1700
1714
  throw new Error("Entity URI is required");
1701
1715
  }
1702
1716
  // Use the existing getTriples utility function
1703
- const api = socket.flow(flowId);
1717
+ const api = socket.flow(effectiveFlow);
1704
1718
  return getTriples(api, entityUri, addActivity, removeActivity, undefined, collection);
1705
1719
  },
1706
- // Only run query if both entityUri and flowId are available
1707
- enabled: !!entityUri && !!flowId,
1720
+ // Only run query if both entityUri and effectiveFlow are available
1721
+ enabled: !!entityUri && !!effectiveFlow,
1708
1722
  });
1709
1723
  // Show loading indicators for long-running operations
1710
1724
  useActivity(query.isLoading, entityUri ? `Knowledge graph search: ${entityUri}` : "Loading entity");
@@ -1728,9 +1742,11 @@ const useEntityDetail = (entityUri, flowId, collection) => {
1728
1742
  * Hook providing low-level access to LLM inference services
1729
1743
  * No conversation state or side effects - just the API calls
1730
1744
  */
1731
- const useInference = () => {
1745
+ const useInference = ({ flow } = {}) => {
1732
1746
  const socket = reactProvider.useSocket();
1733
- const flowId = useSessionStore((state) => state.flowId);
1747
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1748
+ // Use explicit param if provided, otherwise fall back to session state
1749
+ const effectiveFlow = flow ?? sessionFlowId;
1734
1750
  /**
1735
1751
  * Graph RAG inference with entity discovery
1736
1752
  */
@@ -1752,15 +1768,15 @@ const useInference = () => {
1752
1768
  reject(new Error(error));
1753
1769
  };
1754
1770
  socket
1755
- .flow(flowId)
1771
+ .flow(effectiveFlow)
1756
1772
  .graphRagStreaming(input, onChunk, onError, options, collection);
1757
1773
  })
1758
- : await socket.flow(flowId).graphRag(input, options || {}, collection);
1774
+ : await socket.flow(effectiveFlow).graphRag(input, options || {}, collection);
1759
1775
  // Get embeddings for entity discovery
1760
- const embeddings = await socket.flow(flowId).embeddings(input);
1776
+ const embeddings = await socket.flow(effectiveFlow).embeddings(input);
1761
1777
  // Query graph embeddings to find entities
1762
1778
  const entities = await socket
1763
- .flow(flowId)
1779
+ .flow(effectiveFlow)
1764
1780
  .graphEmbeddingsQuery(embeddings, options?.entityLimit || 10, collection);
1765
1781
  return { response, entities };
1766
1782
  },
@@ -1786,10 +1802,10 @@ const useInference = () => {
1786
1802
  reject(new Error(error));
1787
1803
  };
1788
1804
  socket
1789
- .flow(flowId)
1805
+ .flow(effectiveFlow)
1790
1806
  .textCompletionStreaming(systemPrompt, input, onChunk, onError);
1791
1807
  })
1792
- : await socket.flow(flowId).textCompletion(systemPrompt, input);
1808
+ : await socket.flow(effectiveFlow).textCompletion(systemPrompt, input);
1793
1809
  },
1794
1810
  });
1795
1811
  /**
@@ -1817,7 +1833,7 @@ const useInference = () => {
1817
1833
  reject(new Error(error));
1818
1834
  };
1819
1835
  socket
1820
- .flow(flowId)
1836
+ .flow(effectiveFlow)
1821
1837
  .agent(input, onThink, onObserve, onAnswer, onError);
1822
1838
  });
1823
1839
  },
@@ -1837,7 +1853,7 @@ const useInference = () => {
1837
1853
  * Combines conversation state with inference services
1838
1854
  * Handles routing, progress tracking, entity management, and notifications
1839
1855
  */
1840
- const useChatSession = () => {
1856
+ const useChatSession = ({ flow } = {}) => {
1841
1857
  const socket = reactProvider.useSocket();
1842
1858
  const notify = useNotification();
1843
1859
  // Conversation state
@@ -1849,12 +1865,14 @@ const useChatSession = () => {
1849
1865
  const addActivity = useProgressStateStore((state) => state.addActivity);
1850
1866
  const removeActivity = useProgressStateStore((state) => state.removeActivity);
1851
1867
  // Session and workbench state
1852
- const flowId = useSessionStore((state) => state.flowId);
1868
+ const sessionFlowId = useSessionStore((state) => state.flowId);
1853
1869
  const setEntities = useWorkbenchStateStore((state) => state.setEntities);
1870
+ // Use explicit param if provided, otherwise fall back to session state
1871
+ const effectiveFlow = flow ?? sessionFlowId;
1854
1872
  // Settings for GraphRAG configuration
1855
1873
  const { settings } = useSettings();
1856
1874
  // Inference services
1857
- const inference = useInference();
1875
+ const inference = useInference({ flow });
1858
1876
  /**
1859
1877
  * Graph RAG chat handling with entity discovery
1860
1878
  */
@@ -1899,7 +1917,7 @@ const useChatSession = () => {
1899
1917
  addActivity(labelActivity);
1900
1918
  try {
1901
1919
  const triples = await socket
1902
- .flow(flowId)
1920
+ .flow(effectiveFlow)
1903
1921
  .triplesQuery(entity, { t: "i", i: RDFS_LABEL }, undefined, 1, settings.collection);
1904
1922
  removeActivity(labelActivity);
1905
1923
  return triples;
@@ -2161,16 +2179,18 @@ const useStructuredQuery = () => {
2161
2179
  * Provides functionality for executing semantic searches on structured data indexes
2162
2180
  * First converts query text to embeddings, then searches for similar records
2163
2181
  */
2164
- const useRowEmbeddingsQuery = () => {
2182
+ const useRowEmbeddingsQuery = ({ flow } = {}) => {
2165
2183
  // Socket connection for API calls
2166
2184
  const socket = reactProvider.useSocket();
2167
2185
  const connectionState = reactProvider.useConnectionState();
2168
2186
  // Notification system for user feedback
2169
2187
  const notify = useNotification();
2170
2188
  // Session state for current flow ID
2171
- const flowId = useSessionStore((state) => state.flowId);
2189
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2172
2190
  // Settings for default collection
2173
2191
  const { settings } = useSettings();
2192
+ // Use explicit param if provided, otherwise fall back to session state
2193
+ const effectiveFlow = flow ?? sessionFlowId;
2174
2194
  // Only enable operations when socket is connected and ready
2175
2195
  const isSocketReady = connectionState?.status === "authenticated" ||
2176
2196
  connectionState?.status === "unauthenticated";
@@ -2180,11 +2200,11 @@ const useRowEmbeddingsQuery = () => {
2180
2200
  if (!isSocketReady) {
2181
2201
  throw new Error("Socket connection not ready");
2182
2202
  }
2183
- const flow = socket.flow(flowId);
2203
+ const flowApi = socket.flow(effectiveFlow);
2184
2204
  // First, get embeddings for the query text
2185
- const vectors = await flow.embeddings(query);
2205
+ const vectors = await flowApi.embeddings(query);
2186
2206
  // Then query row embeddings with those vectors
2187
- return flow.rowEmbeddingsQuery(vectors, schemaName, collection || settings.collection, indexName, limit || 10);
2207
+ return flowApi.rowEmbeddingsQuery(vectors, schemaName, collection || settings.collection, indexName, limit || 10);
2188
2208
  },
2189
2209
  onError: (err) => {
2190
2210
  console.log("Row embeddings query error:", err);
@@ -2227,14 +2247,16 @@ const useRowEmbeddingsQuery = () => {
2227
2247
  * Custom hook for managing GraphQL rows queries
2228
2248
  * Provides functionality for executing GraphQL queries against structured row data
2229
2249
  */
2230
- const useRowsQuery = () => {
2250
+ const useRowsQuery = ({ flow } = {}) => {
2231
2251
  // Socket connection for API calls
2232
2252
  const socket = reactProvider.useSocket();
2233
2253
  const connectionState = reactProvider.useConnectionState();
2234
2254
  // Notification system for user feedback
2235
2255
  const notify = useNotification();
2236
2256
  // Session state for current flow ID
2237
- const flowId = useSessionStore((state) => state.flowId);
2257
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2258
+ // Use explicit param if provided, otherwise fall back to session state
2259
+ const effectiveFlow = flow ?? sessionFlowId;
2238
2260
  // Settings for default collection
2239
2261
  const { settings } = useSettings();
2240
2262
  // Only enable operations when socket is connected and ready
@@ -2247,7 +2269,7 @@ const useRowsQuery = () => {
2247
2269
  throw new Error("Socket connection not ready");
2248
2270
  }
2249
2271
  return socket
2250
- .flow(flowId)
2272
+ .flow(effectiveFlow)
2251
2273
  .rowsQuery(query, collection || settings.collection, variables, operationName);
2252
2274
  },
2253
2275
  onError: (err) => {
@@ -2293,18 +2315,20 @@ const useEmbeddings = ({ flow, term }) => {
2293
2315
  connectionState?.status === "unauthenticated";
2294
2316
  // Hook for displaying user notifications
2295
2317
  const notify = useNotification();
2296
- if (!flow)
2297
- flow = "default";
2318
+ // Session state for default flow ID
2319
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2320
+ // Use explicit param if provided, otherwise fall back to session state
2321
+ const effectiveFlow = flow ?? sessionFlowId;
2298
2322
  /**
2299
2323
  * Query for fetching all token costs
2300
2324
  * Uses React Query for caching and background refetching
2301
2325
  */
2302
2326
  const query = reactQuery.useQuery({
2303
- queryKey: ["embeddings", { flow, term }],
2304
- enabled: isSocketReady && !!term && !!flow,
2327
+ queryKey: ["embeddings", { flow: effectiveFlow, term }],
2328
+ enabled: isSocketReady && !!term && !!effectiveFlow,
2305
2329
  queryFn: () => {
2306
2330
  return socket
2307
- .flow(flow)
2331
+ .flow(effectiveFlow)
2308
2332
  .embeddings(term)
2309
2333
  .then((x) => {
2310
2334
  if (x["error"]) {
@@ -2442,14 +2466,16 @@ const useCollections = () => {
2442
2466
  * Custom hook for managing NLP query operations
2443
2467
  * Provides functionality for converting natural language questions to GraphQL queries
2444
2468
  */
2445
- const useNlpQuery = () => {
2469
+ const useNlpQuery = ({ flow } = {}) => {
2446
2470
  // Socket connection for API calls
2447
2471
  const socket = reactProvider.useSocket();
2448
2472
  const connectionState = reactProvider.useConnectionState();
2449
2473
  // Notification system for user feedback
2450
2474
  const notify = useNotification();
2451
2475
  // Session state for current flow ID
2452
- const flowId = useSessionStore((state) => state.flowId);
2476
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2477
+ // Use explicit param if provided, otherwise fall back to session state
2478
+ const effectiveFlow = flow ?? sessionFlowId;
2453
2479
  // Only enable operations when socket is connected and ready
2454
2480
  const isSocketReady = connectionState?.status === "authenticated" ||
2455
2481
  connectionState?.status === "unauthenticated";
@@ -2459,7 +2485,7 @@ const useNlpQuery = () => {
2459
2485
  if (!isSocketReady) {
2460
2486
  throw new Error("Socket connection not ready");
2461
2487
  }
2462
- return socket.flow(flowId).nlpQuery(question, maxResults);
2488
+ return socket.flow(effectiveFlow).nlpQuery(question, maxResults);
2463
2489
  },
2464
2490
  onError: (err) => {
2465
2491
  console.log("NLP query error:", err);