@trustgraph/react-state 1.4.3 → 1.4.5

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
@@ -1399,53 +1399,36 @@ const useGraphSubgraph = ({ entityUri, flow, collection, }) => {
1399
1399
  };
1400
1400
 
1401
1401
  /**
1402
- * Custom hook for managing token cost operations
1403
- * Provides functionality for fetching, deleting, and updating token costs
1404
- * for AI models
1405
- * @returns {Object} Token cost state and operations
1402
+ * Custom hook for querying graph embeddings
1403
+ * Finds graph entities similar to the provided embedding vectors
1406
1404
  */
1407
- const useGraphEmbeddings = ({ flow, vecs, limit, collection }) => {
1408
- // WebSocket connection for communicating with the configuration service
1405
+ const useGraphEmbeddings = ({ flow, vecs, limit = 10, collection }) => {
1409
1406
  const socket = reactProvider.useSocket();
1410
- // Hook for displaying user notifications
1411
1407
  const notify = useNotification();
1412
- // Settings for default collection
1413
1408
  const { settings } = useSettings();
1414
- // Session state for default flow ID
1415
1409
  const sessionFlowId = useSessionStore((state) => state.flowId);
1416
- // Use explicit param if provided, otherwise fall back to session state
1417
1410
  const effectiveFlow = flow ?? sessionFlowId;
1418
- /**
1419
- * Query for fetching graph embeddings
1420
- * Uses React Query for caching and background refetching
1421
- */
1411
+ const effectiveCollection = collection ?? settings.collection;
1422
1412
  const query = reactQuery.useQuery({
1423
- queryKey: ["graph-embeddings", { flow: effectiveFlow, vecs, limit }],
1413
+ queryKey: ["graph-embeddings", { flow: effectiveFlow, vecs, limit, collection: effectiveCollection }],
1414
+ enabled: !!vecs && vecs.length > 0 && !!effectiveFlow,
1424
1415
  queryFn: () => {
1425
1416
  return socket
1426
1417
  .flow(effectiveFlow)
1427
- .graphEmbeddingsQuery(vecs, limit, collection || settings.collection)
1428
- .then((x) => {
1429
- return x;
1430
- })
1418
+ .graphEmbeddingsQuery(vecs, limit, effectiveCollection)
1431
1419
  .catch((err) => {
1432
- console.log("Error:", err);
1433
1420
  const message = err instanceof Error ? err.message : String(err);
1434
1421
  notify.error(message);
1435
1422
  throw err;
1436
1423
  });
1437
1424
  },
1438
1425
  });
1439
- // Show loading indicators for long-running operations
1440
1426
  useActivity(query.isLoading, "Loading graph embeddings");
1441
- // Return token cost state and operations for use in components
1442
1427
  return {
1443
- // Token cost query state
1444
1428
  graphEmbeddings: query.data,
1445
1429
  isLoading: query.isLoading,
1446
1430
  isError: query.isError,
1447
1431
  error: query.error,
1448
- // Manual refetch function
1449
1432
  refetch: query.refetch,
1450
1433
  };
1451
1434
  };
@@ -2173,71 +2156,90 @@ const useStructuredQuery = () => {
2173
2156
  };
2174
2157
 
2175
2158
  // @ts-nocheck
2176
- // React Query hooks for data fetching and mutation management
2177
2159
  /**
2178
- * Custom hook for managing row embeddings query operations
2179
- * Provides functionality for executing semantic searches on structured data indexes
2180
- * First converts query text to embeddings, then searches for similar records
2160
+ * Custom hook for querying row embeddings using vectors.
2161
+ * Searches for similar records in structured data indexes.
2181
2162
  */
2182
2163
  const useRowEmbeddingsQuery = ({ flow } = {}) => {
2183
- // Socket connection for API calls
2184
2164
  const socket = reactProvider.useSocket();
2185
2165
  const connectionState = reactProvider.useConnectionState();
2186
- // Notification system for user feedback
2187
2166
  const notify = useNotification();
2188
- // Session state for current flow ID
2189
2167
  const sessionFlowId = useSessionStore((state) => state.flowId);
2190
- // Settings for default collection
2191
2168
  const { settings } = useSettings();
2192
- // Use explicit param if provided, otherwise fall back to session state
2193
2169
  const effectiveFlow = flow ?? sessionFlowId;
2194
- // Only enable operations when socket is connected and ready
2195
2170
  const isSocketReady = connectionState?.status === "authenticated" ||
2196
2171
  connectionState?.status === "unauthenticated";
2197
- // Mutation for executing row embeddings queries
2198
- const rowEmbeddingsQueryMutation = reactQuery.useMutation({
2199
- mutationFn: async ({ query, schemaName, collection, indexName, limit, }) => {
2172
+ const mutation = reactQuery.useMutation({
2173
+ mutationFn: async ({ vectors, schemaName, collection, indexName, limit = 10, }) => {
2200
2174
  if (!isSocketReady) {
2201
2175
  throw new Error("Socket connection not ready");
2202
2176
  }
2203
- const flowApi = socket.flow(effectiveFlow);
2204
- // First, get embeddings for the query text
2205
- const vectors = await flowApi.embeddings(query);
2206
- // Then query row embeddings with those vectors
2207
- return flowApi.rowEmbeddingsQuery(vectors, schemaName, collection || settings.collection, indexName, limit || 10);
2177
+ return socket.flow(effectiveFlow).rowEmbeddingsQuery(vectors, schemaName, collection ?? settings.collection, indexName, limit);
2208
2178
  },
2209
2179
  onError: (err) => {
2210
- console.log("Row embeddings query error:", err);
2211
- const errorMessage = err instanceof Error
2212
- ? err.message
2213
- : err?.toString() || "Unknown error";
2214
- notify.error(`Row embeddings query failed: ${errorMessage}`);
2180
+ const message = err instanceof Error ? err.message : String(err);
2181
+ notify.error(`Row embeddings query failed: ${message}`);
2182
+ },
2183
+ onSuccess: () => {
2184
+ },
2185
+ });
2186
+ useActivity(mutation.isPending, "Querying row embeddings");
2187
+ return {
2188
+ executeQuery: mutation.mutate,
2189
+ executeQueryAsync: mutation.mutateAsync,
2190
+ isExecuting: mutation.isPending,
2191
+ error: mutation.error,
2192
+ matches: mutation.data ?? [],
2193
+ hasResults: (mutation.data?.length ?? 0) > 0,
2194
+ reset: mutation.reset,
2195
+ isReady: isSocketReady,
2196
+ };
2197
+ };
2198
+
2199
+ // @ts-nocheck
2200
+ /**
2201
+ * Custom hook for querying document chunks using vectors.
2202
+ * Searches for document chunks with similar embeddings.
2203
+ */
2204
+ const useDocumentEmbeddingsQuery = ({ flow } = {}) => {
2205
+ const socket = reactProvider.useSocket();
2206
+ const connectionState = reactProvider.useConnectionState();
2207
+ const notify = useNotification();
2208
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2209
+ const { settings } = useSettings();
2210
+ const effectiveFlow = flow ?? sessionFlowId;
2211
+ const isSocketReady = connectionState?.status === "authenticated" ||
2212
+ connectionState?.status === "unauthenticated";
2213
+ const mutation = reactQuery.useMutation({
2214
+ mutationFn: async ({ vectors, user, collection, limit = 10, }) => {
2215
+ if (!isSocketReady) {
2216
+ throw new Error("Socket connection not ready");
2217
+ }
2218
+ return socket.flow(effectiveFlow).documentEmbeddingsQuery(vectors, user ?? settings.user, collection ?? settings.collection, limit);
2219
+ },
2220
+ onError: (err) => {
2221
+ const message = err instanceof Error ? err.message : String(err);
2222
+ notify.error(`Document embeddings query failed: ${message}`);
2215
2223
  },
2216
2224
  onSuccess: (data) => {
2217
- if (data && data.length > 0) {
2218
- notify.success(`Found ${data.length} matching record${data.length !== 1 ? 's' : ''}`);
2225
+ const count = Array.isArray(data) ? data.length : 0;
2226
+ if (count > 0) {
2227
+ notify.success(`Found ${count} matching chunk${count !== 1 ? "s" : ""}`);
2219
2228
  }
2220
2229
  else {
2221
- notify.info("No matching records found");
2230
+ notify.info("No matching document chunks found");
2222
2231
  }
2223
2232
  },
2224
2233
  });
2225
- // Show loading indicator for row embeddings query operations
2226
- useActivity(rowEmbeddingsQueryMutation.isPending, "Executing row embeddings query");
2227
- // Return the public API for the hook
2234
+ useActivity(mutation.isPending, "Searching document chunks");
2228
2235
  return {
2229
- // Query execution
2230
- executeQuery: rowEmbeddingsQueryMutation.mutate,
2231
- executeQueryAsync: rowEmbeddingsQueryMutation.mutateAsync,
2232
- // Query state
2233
- isExecuting: rowEmbeddingsQueryMutation.isPending,
2234
- error: rowEmbeddingsQueryMutation.error,
2235
- // Results
2236
- matches: rowEmbeddingsQueryMutation.data || [],
2237
- hasResults: (rowEmbeddingsQueryMutation.data?.length || 0) > 0,
2238
- // Reset function to clear previous results
2239
- reset: rowEmbeddingsQueryMutation.reset,
2240
- // Socket readiness
2236
+ executeQuery: mutation.mutate,
2237
+ executeQueryAsync: mutation.mutateAsync,
2238
+ isExecuting: mutation.isPending,
2239
+ error: mutation.error,
2240
+ results: mutation.data ?? [],
2241
+ hasResults: Array.isArray(mutation.data) && mutation.data.length > 0,
2242
+ reset: mutation.reset,
2241
2243
  isReady: isSocketReady,
2242
2244
  };
2243
2245
  };
@@ -2278,7 +2280,6 @@ const useRowsQuery = ({ flow } = {}) => {
2278
2280
  notify.error(`GraphQL query failed: ${errorMessage}`);
2279
2281
  },
2280
2282
  onSuccess: () => {
2281
- notify.success("GraphQL query executed successfully");
2282
2283
  },
2283
2284
  });
2284
2285
  // Show loading indicator for query operations
@@ -4690,6 +4691,7 @@ exports.useChat = useChat;
4690
4691
  exports.useChatSession = useChatSession;
4691
4692
  exports.useCollections = useCollections;
4692
4693
  exports.useConversation = useConversation;
4694
+ exports.useDocumentEmbeddingsQuery = useDocumentEmbeddingsQuery;
4693
4695
  exports.useEmbeddings = useEmbeddings;
4694
4696
  exports.useEntityDetail = useEntityDetail;
4695
4697
  exports.useFlowBlueprints = useFlowBlueprints;