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