@trustgraph/react-state 1.5.3 → 1.5.4

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
@@ -1,7 +1,9 @@
1
1
  import { useSocket, useConnectionState } from '@trustgraph/react-provider';
2
2
  export { ConnectionStateContext, SocketContext, SocketProvider } from '@trustgraph/react-provider';
3
+ import { TG_EDGE, TG_REASONING, TG_EDGE_COUNT, TG_SELECTED_EDGE, TG_QUERY, PROV_STARTED_AT_TIME, TG_CONTENT, RDFS_LABEL as RDFS_LABEL$2, PROV_WAS_DERIVED_FROM, TG_REIFIES } from '@trustgraph/client';
4
+ export { PROV, PROV_ACTIVITY, PROV_ENTITY, PROV_STARTED_AT_TIME, PROV_WAS_DERIVED_FROM, PROV_WAS_GENERATED_BY, RDF, RDFS, RDF_TYPE, SCHEMA, SCHEMA_AUTHOR, SCHEMA_DESCRIPTION, SCHEMA_KEYWORDS, SCHEMA_NAME, SKOS, SKOS_DEFINITION, TG, TG_CONTENT, TG_DOCUMENT, TG_EDGE, TG_EDGE_COUNT, TG_QUERY, TG_REASONING, TG_REIFIES, TG_SELECTED_EDGE } from '@trustgraph/client';
3
5
  import { jsx } from 'react/jsx-runtime';
4
- import { createContext, useContext, useEffect, useState, useMemo, useRef, useCallback } from 'react';
6
+ import { createContext, useContext, useEffect, useState, useRef, useCallback, useMemo } from 'react';
5
7
  import { create } from 'zustand';
6
8
  import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
7
9
  import { v4 } from 'uuid';
@@ -155,23 +157,25 @@ const useConversation = create()((set) => ({
155
157
  setMessages: (v) => set(() => ({
156
158
  messages: v,
157
159
  })),
158
- addMessage: (role, text, type) => set((state) => ({
160
+ addMessage: (role, text, type, explainSessionId) => set((state) => ({
159
161
  messages: [
160
162
  ...state.messages,
161
163
  {
162
164
  role: role,
163
165
  text: text,
164
166
  type: type || "normal",
167
+ explainSessionId,
165
168
  },
166
169
  ],
167
170
  })),
168
- updateLastMessage: (text) => set((state) => {
171
+ updateLastMessage: (text, explainSessionId) => set((state) => {
169
172
  if (state.messages.length === 0)
170
173
  return state;
171
174
  const messages = [...state.messages];
172
175
  messages[messages.length - 1] = {
173
176
  ...messages[messages.length - 1],
174
177
  text: text,
178
+ ...(explainSessionId !== undefined && { explainSessionId }),
175
179
  };
176
180
  return { messages };
177
181
  }),
@@ -311,6 +315,7 @@ const DEFAULT_SETTINGS = {
311
315
  flowBlueprintEditor: false, // Off by default - experimental feature
312
316
  structuredQuery: false, // Off by default
313
317
  llmModels: false, // Off by default
318
+ explainability: true, // On by default
314
319
  },
315
320
  };
316
321
  const SETTINGS_STORAGE_KEY = "trustgraph-settings";
@@ -967,7 +972,7 @@ const useTriples = ({ flow, s, p, o, limit, collection }) => {
967
972
  };
968
973
 
969
974
  // Helper to get the string value from a Term (IRI or Literal)
970
- const getTermValue$2 = (term) => {
975
+ const getTermValue$3 = (term) => {
971
976
  if (term.t === "i")
972
977
  return term.i;
973
978
  if (term.t === "l")
@@ -1086,7 +1091,7 @@ const queryLabel = (socket, uri, add, remove, collection) => {
1086
1091
  // If got a result, return the label, otherwise the URI
1087
1092
  // can be its own label
1088
1093
  if (triples.length > 0)
1089
- return getTermValue$2(triples[0].o);
1094
+ return getTermValue$3(triples[0].o);
1090
1095
  else
1091
1096
  return uri;
1092
1097
  })
@@ -1103,7 +1108,7 @@ const queryLabel = (socket, uri, add, remove, collection) => {
1103
1108
  // Returns a promise
1104
1109
  const labelS = (socket, triples, add, remove, collection) => {
1105
1110
  return Promise.all(triples.map((t) => {
1106
- return queryLabel(socket, getTermValue$2(t.s), add, remove, collection).then((label) => {
1111
+ return queryLabel(socket, getTermValue$3(t.s), add, remove, collection).then((label) => {
1107
1112
  return {
1108
1113
  ...t,
1109
1114
  s: {
@@ -1118,7 +1123,7 @@ const labelS = (socket, triples, add, remove, collection) => {
1118
1123
  // Returns a promise
1119
1124
  const labelP = (socket, triples, add, remove, collection) => {
1120
1125
  return Promise.all(triples.map((t) => {
1121
- return queryLabel(socket, getTermValue$2(t.p), add, remove, collection).then((label) => {
1126
+ return queryLabel(socket, getTermValue$3(t.p), add, remove, collection).then((label) => {
1122
1127
  return {
1123
1128
  ...t,
1124
1129
  p: {
@@ -1151,7 +1156,7 @@ const labelO = (socket, triples, add, remove, collection) => {
1151
1156
  ...t,
1152
1157
  o: {
1153
1158
  ...t.o,
1154
- label: getTermValue$2(t.o),
1159
+ label: getTermValue$3(t.o),
1155
1160
  },
1156
1161
  });
1157
1162
  });
@@ -1187,7 +1192,7 @@ const getTriples = (socket, uri, add, remove, limit, collection) => {
1187
1192
  // Functionality here helps construct subgraphs for react-force-graph
1188
1193
  // visualisation
1189
1194
  // Helper to get the string value from a Term (IRI or Literal)
1190
- const getTermValue$1 = (term) => {
1195
+ const getTermValue$2 = (term) => {
1191
1196
  if (term.t === "i")
1192
1197
  return term.i;
1193
1198
  if (term.t === "l")
@@ -1215,11 +1220,11 @@ const updateSubgraphTriples = (sg, triples) => {
1215
1220
  continue;
1216
1221
  }
1217
1222
  // Source has a URI, that can be its unique ID
1218
- const sourceId = getTermValue$1(t.s);
1223
+ const sourceId = getTermValue$2(t.s);
1219
1224
  // Target is always an entity now (we filtered out literals above)
1220
- const targetId = getTermValue$1(t.o);
1225
+ const targetId = getTermValue$2(t.o);
1221
1226
  // Links have an ID so that this edge is unique
1222
- const linkId = getTermValue$1(t.s) + "@@" + getTermValue$1(t.p) + "@@" + getTermValue$1(t.o);
1227
+ const linkId = getTermValue$2(t.s) + "@@" + getTermValue$2(t.p) + "@@" + getTermValue$2(t.o);
1223
1228
  if (!nodeIds.has(sourceId)) {
1224
1229
  const sLabeled = t.s;
1225
1230
  const n = {
@@ -1730,10 +1735,14 @@ const useInference = ({ flow } = {}) => {
1730
1735
  // Use explicit param if provided, otherwise fall back to session state
1731
1736
  const effectiveFlow = flow ?? sessionFlowId;
1732
1737
  /**
1733
- * Graph RAG inference with entity discovery
1738
+ * Graph RAG inference with entity discovery and optional explainability
1739
+ * Explainability events are only tracked if callbacks.onExplain is provided
1734
1740
  */
1735
1741
  const graphRagMutation = useMutation({
1736
1742
  mutationFn: async ({ input, options, collection, callbacks, }) => {
1743
+ // Only collect explain events if caller provided onExplain callback
1744
+ const wantsExplainability = !!callbacks?.onExplain;
1745
+ const explainEvents = wantsExplainability ? [] : undefined;
1737
1746
  // If callbacks provided, use streaming API
1738
1747
  const response = callbacks
1739
1748
  ? await new Promise((resolve, reject) => {
@@ -1749,9 +1758,16 @@ const useInference = ({ flow } = {}) => {
1749
1758
  callbacks?.onError?.(error);
1750
1759
  reject(new Error(error));
1751
1760
  };
1761
+ // Only wire up onExplain if caller wants explainability
1762
+ const onExplain = wantsExplainability
1763
+ ? (event) => {
1764
+ explainEvents.push(event);
1765
+ callbacks.onExplain(event);
1766
+ }
1767
+ : undefined;
1752
1768
  socket
1753
1769
  .flow(effectiveFlow)
1754
- .graphRagStreaming(input, onChunk, onError, options, collection);
1770
+ .graphRagStreaming(input, onChunk, onError, options, collection, onExplain);
1755
1771
  })
1756
1772
  : await socket.flow(effectiveFlow).graphRag(input, options || {}, collection);
1757
1773
  // Get embeddings for entity discovery
@@ -1761,7 +1777,7 @@ const useInference = ({ flow } = {}) => {
1761
1777
  const entities = await socket
1762
1778
  .flow(effectiveFlow)
1763
1779
  .graphEmbeddingsQuery(embeddings, options?.entityLimit || 10, collection);
1764
- return { response, entities };
1780
+ return { response, entities, explainEvents };
1765
1781
  },
1766
1782
  });
1767
1783
  /**
@@ -1831,6 +1847,620 @@ const useInference = ({ flow } = {}) => {
1831
1847
  };
1832
1848
  };
1833
1849
 
1850
+ /**
1851
+ * Zustand store for managing explainability sessions
1852
+ * Sessions are keyed by ID and linked to messages via explainSessionId
1853
+ */
1854
+ const useExplainabilityStore = create()((set, get) => ({
1855
+ sessions: {},
1856
+ addSession: (id, session) => set((state) => ({
1857
+ sessions: {
1858
+ ...state.sessions,
1859
+ [id]: session,
1860
+ },
1861
+ })),
1862
+ updateSession: (id, partial) => set((state) => ({
1863
+ sessions: {
1864
+ ...state.sessions,
1865
+ [id]: {
1866
+ ...state.sessions[id],
1867
+ ...partial,
1868
+ },
1869
+ },
1870
+ })),
1871
+ getSession: (id) => get().sessions[id],
1872
+ removeSession: (id) => set((state) => {
1873
+ const { [id]: _, ...rest } = state.sessions;
1874
+ return { sessions: rest };
1875
+ }),
1876
+ clearSessions: () => set({ sessions: {} }),
1877
+ }));
1878
+
1879
+ /**
1880
+ * Explainability utilities for parsing and structuring explain events
1881
+ */
1882
+ /**
1883
+ * Extract event type from explainId URI
1884
+ * e.g., "urn:trustgraph:question:abc123" → "question"
1885
+ */
1886
+ function getEventType(explainId) {
1887
+ if (explainId.includes("question"))
1888
+ return "question";
1889
+ if (explainId.includes("exploration"))
1890
+ return "exploration";
1891
+ if (explainId.includes("focus"))
1892
+ return "focus";
1893
+ if (explainId.includes("synthesis"))
1894
+ return "synthesis";
1895
+ return "unknown";
1896
+ }
1897
+ /**
1898
+ * Get term value from a Term object
1899
+ */
1900
+ function getTermValue$1(term) {
1901
+ if (!term)
1902
+ return "";
1903
+ if (term.t === "i")
1904
+ return term.i || "";
1905
+ if (term.t === "l")
1906
+ return term.v || "";
1907
+ if (term.t === "t" && term.tr) {
1908
+ // Quoted triple - return a serialized form
1909
+ const s = getTermValue$1(term.tr.s);
1910
+ const p = getTermValue$1(term.tr.p);
1911
+ const o = getTermValue$1(term.tr.o);
1912
+ return `<<${s} ${p} ${o}>>`;
1913
+ }
1914
+ return "";
1915
+ }
1916
+ /**
1917
+ * Extract quoted triple from a Term
1918
+ */
1919
+ function extractQuotedTriple(term) {
1920
+ if (term.t === "t" && term.tr) {
1921
+ return {
1922
+ s: getTermValue$1(term.tr.s),
1923
+ p: getTermValue$1(term.tr.p),
1924
+ o: getTermValue$1(term.tr.o),
1925
+ };
1926
+ }
1927
+ return null;
1928
+ }
1929
+ /**
1930
+ * Parse triples for a question event
1931
+ */
1932
+ function parseQuestionTriples(explainId, explainGraph, triples) {
1933
+ const event = {
1934
+ type: "question",
1935
+ explainId,
1936
+ explainGraph,
1937
+ };
1938
+ for (const triple of triples) {
1939
+ const p = getTermValue$1(triple.p);
1940
+ const o = getTermValue$1(triple.o);
1941
+ if (p === TG_QUERY) {
1942
+ event.query = o;
1943
+ }
1944
+ else if (p === PROV_STARTED_AT_TIME) {
1945
+ event.timestamp = o;
1946
+ }
1947
+ }
1948
+ return event;
1949
+ }
1950
+ /**
1951
+ * Parse triples for an exploration event
1952
+ */
1953
+ function parseExplorationTriples(explainId, explainGraph, triples) {
1954
+ const event = {
1955
+ type: "exploration",
1956
+ explainId,
1957
+ explainGraph,
1958
+ };
1959
+ for (const triple of triples) {
1960
+ const p = getTermValue$1(triple.p);
1961
+ const o = getTermValue$1(triple.o);
1962
+ if (p === TG_EDGE_COUNT) {
1963
+ event.edgeCount = parseInt(o, 10);
1964
+ }
1965
+ }
1966
+ return event;
1967
+ }
1968
+ /**
1969
+ * Parse triples for a focus event
1970
+ */
1971
+ function parseFocusTriples(explainId, explainGraph, triples) {
1972
+ const event = {
1973
+ type: "focus",
1974
+ explainId,
1975
+ explainGraph,
1976
+ edgeSelectionUris: [],
1977
+ };
1978
+ for (const triple of triples) {
1979
+ const p = getTermValue$1(triple.p);
1980
+ const o = getTermValue$1(triple.o);
1981
+ if (p === TG_SELECTED_EDGE && typeof o === "string") {
1982
+ event.edgeSelectionUris.push(o);
1983
+ }
1984
+ }
1985
+ return event;
1986
+ }
1987
+ /**
1988
+ * Parse triples for a synthesis event
1989
+ */
1990
+ function parseSynthesisTriples(explainId, explainGraph, triples) {
1991
+ const event = {
1992
+ type: "synthesis",
1993
+ explainId,
1994
+ explainGraph,
1995
+ };
1996
+ for (const triple of triples) {
1997
+ const p = getTermValue$1(triple.p);
1998
+ const o = getTermValue$1(triple.o);
1999
+ if (p === TG_CONTENT) {
2000
+ event.contentLength = o.length;
2001
+ }
2002
+ }
2003
+ return event;
2004
+ }
2005
+ /**
2006
+ * Parse triples for an edge selection entity
2007
+ */
2008
+ function parseEdgeSelectionTriples(triples) {
2009
+ let edge = null;
2010
+ let reasoning = null;
2011
+ for (const triple of triples) {
2012
+ const p = getTermValue$1(triple.p);
2013
+ if (p === TG_EDGE) {
2014
+ edge = extractQuotedTriple(triple.o);
2015
+ }
2016
+ else if (p === TG_REASONING) {
2017
+ reasoning = getTermValue$1(triple.o);
2018
+ }
2019
+ }
2020
+ return { edge, reasoning };
2021
+ }
2022
+ /**
2023
+ * Parse triples based on event type
2024
+ */
2025
+ function parseExplainTriples(explainId, explainGraph, triples) {
2026
+ const eventType = getEventType(explainId);
2027
+ switch (eventType) {
2028
+ case "question":
2029
+ return parseQuestionTriples(explainId, explainGraph, triples);
2030
+ case "exploration":
2031
+ return parseExplorationTriples(explainId, explainGraph, triples);
2032
+ case "focus":
2033
+ return parseFocusTriples(explainId, explainGraph, triples);
2034
+ case "synthesis":
2035
+ return parseSynthesisTriples(explainId, explainGraph, triples);
2036
+ default:
2037
+ return null;
2038
+ }
2039
+ }
2040
+
2041
+ /**
2042
+ * Hook for tracing provenance chains in the knowledge graph
2043
+ * Follows prov:wasDerivedFrom relationships from any entity to its source documents
2044
+ */
2045
+ /**
2046
+ * Hook for tracing provenance chains
2047
+ */
2048
+ const useProvenance = (options = {}) => {
2049
+ const { flow, collection = "default", maxDepth = 10 } = options;
2050
+ const socket = useSocket();
2051
+ const connectionState = useConnectionState();
2052
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2053
+ const effectiveFlow = flow ?? sessionFlowId;
2054
+ const [isTracing, setIsTracing] = useState(false);
2055
+ // Label cache to avoid repeated queries
2056
+ const labelCacheRef = useRef(new Map());
2057
+ /**
2058
+ * Check if connected
2059
+ */
2060
+ const isConnected = useCallback(() => {
2061
+ return (connectionState?.status === "authenticated" ||
2062
+ connectionState?.status === "unauthenticated");
2063
+ }, [connectionState]);
2064
+ /**
2065
+ * Query for rdfs:label of a URI
2066
+ */
2067
+ const resolveLabel = useCallback(async (uri) => {
2068
+ // Check cache
2069
+ if (labelCacheRef.current.has(uri)) {
2070
+ return labelCacheRef.current.get(uri);
2071
+ }
2072
+ // Not an IRI - return as-is
2073
+ if (!uri.startsWith("http") && !uri.startsWith("urn:")) {
2074
+ return uri;
2075
+ }
2076
+ if (!isConnected()) {
2077
+ return uri;
2078
+ }
2079
+ try {
2080
+ const triples = await socket
2081
+ .flow(effectiveFlow)
2082
+ .triplesQuery({ t: "i", i: uri }, { t: "i", i: RDFS_LABEL$2 }, undefined, 1, collection);
2083
+ const label = triples.length > 0 ? getTermValue$1(triples[0].o) : uri;
2084
+ // Cache the result
2085
+ labelCacheRef.current.set(uri, label);
2086
+ return label;
2087
+ }
2088
+ catch {
2089
+ return uri;
2090
+ }
2091
+ }, [socket, effectiveFlow, collection, isConnected]);
2092
+ /**
2093
+ * Query for prov:wasDerivedFrom parent of a URI
2094
+ */
2095
+ const queryDerivedFrom = useCallback(async (uri) => {
2096
+ if (!isConnected())
2097
+ return null;
2098
+ try {
2099
+ const triples = await socket
2100
+ .flow(effectiveFlow)
2101
+ .triplesQuery({ t: "i", i: uri }, { t: "i", i: PROV_WAS_DERIVED_FROM }, undefined, 1, collection);
2102
+ if (triples.length > 0) {
2103
+ return getTermValue$1(triples[0].o);
2104
+ }
2105
+ return null;
2106
+ }
2107
+ catch {
2108
+ return null;
2109
+ }
2110
+ }, [socket, effectiveFlow, collection, isConnected]);
2111
+ /**
2112
+ * Trace the full provenance chain from a URI to root
2113
+ */
2114
+ const traceChain = useCallback(async (uri) => {
2115
+ setIsTracing(true);
2116
+ try {
2117
+ const chain = [];
2118
+ let current = uri;
2119
+ for (let depth = 0; depth < maxDepth && current; depth++) {
2120
+ const label = await resolveLabel(current);
2121
+ chain.push({ uri: current, label });
2122
+ const parent = await queryDerivedFrom(current);
2123
+ if (!parent || parent === current) {
2124
+ break;
2125
+ }
2126
+ current = parent;
2127
+ }
2128
+ // The last item in the chain is the root document
2129
+ const rootItem = chain.length > 0 ? chain[chain.length - 1] : undefined;
2130
+ return {
2131
+ chain,
2132
+ documentUri: rootItem?.uri,
2133
+ documentLabel: rootItem?.label,
2134
+ };
2135
+ }
2136
+ finally {
2137
+ setIsTracing(false);
2138
+ }
2139
+ }, [maxDepth, resolveLabel, queryDerivedFrom]);
2140
+ /**
2141
+ * Query for statements that reify an edge via tg:reifies
2142
+ */
2143
+ const queryReifyingStatements = useCallback(async (s, p, o) => {
2144
+ if (!isConnected())
2145
+ return [];
2146
+ try {
2147
+ // Build the quoted triple term
2148
+ const quotedTriple = {
2149
+ t: "t",
2150
+ tr: {
2151
+ s: { t: "i", i: s },
2152
+ p: { t: "i", i: p },
2153
+ o: o.startsWith("http") || o.startsWith("urn:")
2154
+ ? { t: "i", i: o }
2155
+ : { t: "l", v: o },
2156
+ },
2157
+ };
2158
+ const triples = await socket
2159
+ .flow(effectiveFlow)
2160
+ .triplesQuery(undefined, { t: "i", i: TG_REIFIES }, quotedTriple, 10, collection);
2161
+ return triples.map((t) => getTermValue$1(t.s));
2162
+ }
2163
+ catch {
2164
+ return [];
2165
+ }
2166
+ }, [socket, effectiveFlow, collection, isConnected]);
2167
+ /**
2168
+ * Trace provenance for an edge - finds reifying statements and traces each
2169
+ */
2170
+ const traceEdgeProvenance = useCallback(async (s, p, o) => {
2171
+ setIsTracing(true);
2172
+ try {
2173
+ // Find statements that reify this edge
2174
+ const stmtUris = await queryReifyingStatements(s, p, o);
2175
+ // For each reifying statement, trace its provenance chain
2176
+ const chains = [];
2177
+ for (const stmtUri of stmtUris) {
2178
+ // Get the wasDerivedFrom source for this statement
2179
+ const sourceUri = await queryDerivedFrom(stmtUri);
2180
+ if (sourceUri) {
2181
+ const chain = await traceChain(sourceUri);
2182
+ chains.push(chain);
2183
+ }
2184
+ }
2185
+ return chains;
2186
+ }
2187
+ finally {
2188
+ setIsTracing(false);
2189
+ }
2190
+ }, [queryReifyingStatements, queryDerivedFrom, traceChain]);
2191
+ /**
2192
+ * Clear the label cache
2193
+ */
2194
+ const clearCache = useCallback(() => {
2195
+ labelCacheRef.current.clear();
2196
+ }, []);
2197
+ return {
2198
+ traceChain,
2199
+ traceEdgeProvenance,
2200
+ resolveLabel,
2201
+ clearCache,
2202
+ isTracing,
2203
+ };
2204
+ };
2205
+
2206
+ /**
2207
+ * Hook for managing explainability state during GraphRAG queries
2208
+ * Unpacks explain events into structured data with provenance chains
2209
+ *
2210
+ * Processing strategy:
2211
+ * - Events are processed immediately as they arrive
2212
+ * - Main event nodes (question, exploration, focus, synthesis) use
2213
+ * stability-based retry: fetch until count > 0 AND stable
2214
+ * - Edge sub-objects use simple retry-until-non-empty (fast)
2215
+ * - All edge fetching + label resolution + provenance runs in parallel
2216
+ */
2217
+ /**
2218
+ * Hook for managing explainability during inference
2219
+ */
2220
+ const useExplainability = (options = {}) => {
2221
+ const { flow, collection = "default", traceProvenance = true, } = options;
2222
+ const socket = useSocket();
2223
+ const connectionState = useConnectionState();
2224
+ const sessionFlowId = useSessionStore((state) => state.flowId);
2225
+ const effectiveFlow = flow ?? sessionFlowId;
2226
+ const { traceEdgeProvenance, resolveLabel } = useProvenance({
2227
+ flow: effectiveFlow,
2228
+ collection,
2229
+ });
2230
+ const [events, setEvents] = useState([]);
2231
+ const [session, setSession] = useState({});
2232
+ const [isUnpacking, setIsUnpacking] = useState(false);
2233
+ const [error, setError] = useState(null);
2234
+ // Mirror session in a ref so it's always immediately readable (no render delay)
2235
+ const sessionRef = useRef({});
2236
+ // Track pending unpack operations
2237
+ const unpackQueueRef = useRef([]);
2238
+ const isProcessingRef = useRef(false);
2239
+ /**
2240
+ * Check if connected
2241
+ */
2242
+ const isConnected = useCallback(() => {
2243
+ return (connectionState?.status === "authenticated" ||
2244
+ connectionState?.status === "unauthenticated");
2245
+ }, [connectionState]);
2246
+ /**
2247
+ * Single triple query (no retry)
2248
+ */
2249
+ const fetchTriples = useCallback(async (explainId, explainGraph) => {
2250
+ return socket
2251
+ .flow(effectiveFlow)
2252
+ .triplesQuery({ t: "i", i: explainId }, undefined, undefined, 100, collection, explainGraph);
2253
+ }, [socket, effectiveFlow, collection]);
2254
+ /**
2255
+ * Stability-based retry for main event nodes.
2256
+ * Retries until: count > 0 AND count matches previous fetch.
2257
+ * Used for question, exploration, focus, synthesis nodes where the
2258
+ * backend may write multiple triples incrementally.
2259
+ */
2260
+ const queryWithStabilityRetry = useCallback(async (explainId, explainGraph, timeoutMs = 5000) => {
2261
+ if (!isConnected())
2262
+ return [];
2263
+ const retryDelay = 500;
2264
+ const maxAttempts = Math.ceil(timeoutMs / retryDelay) + 1;
2265
+ let prevCount = -1;
2266
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
2267
+ try {
2268
+ const triples = await fetchTriples(explainId, explainGraph);
2269
+ const count = triples.length;
2270
+ if (count > 0 && count === prevCount) {
2271
+ return triples;
2272
+ }
2273
+ prevCount = count;
2274
+ if (attempt < maxAttempts - 1) {
2275
+ await new Promise((r) => setTimeout(r, retryDelay));
2276
+ }
2277
+ }
2278
+ catch (err) {
2279
+ console.error("[explain] triple query failed:", explainId, err);
2280
+ return [];
2281
+ }
2282
+ }
2283
+ // Return last fetch if we got anything
2284
+ if (prevCount > 0) {
2285
+ try {
2286
+ return await fetchTriples(explainId, explainGraph);
2287
+ }
2288
+ catch {
2289
+ return [];
2290
+ }
2291
+ }
2292
+ return [];
2293
+ }, [fetchTriples, isConnected]);
2294
+ /**
2295
+ * Simple retry-until-non-empty for sub-objects (edge selections).
2296
+ * These are small atomic writes — either fully there or not yet.
2297
+ */
2298
+ const queryWithSimpleRetry = useCallback(async (explainId, explainGraph, timeoutMs = 5000) => {
2299
+ if (!isConnected())
2300
+ return [];
2301
+ const retryDelay = 300;
2302
+ const maxAttempts = Math.ceil(timeoutMs / retryDelay) + 1;
2303
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
2304
+ try {
2305
+ const triples = await fetchTriples(explainId, explainGraph);
2306
+ if (triples.length > 0)
2307
+ return triples;
2308
+ if (attempt < maxAttempts - 1) {
2309
+ await new Promise((r) => setTimeout(r, retryDelay));
2310
+ }
2311
+ }
2312
+ catch (err) {
2313
+ console.error("[explain] triple query failed:", explainId, err);
2314
+ return [];
2315
+ }
2316
+ }
2317
+ return [];
2318
+ }, [fetchTriples, isConnected]);
2319
+ /**
2320
+ * Resolve a single edge: fetch triples, labels, and provenance in parallel
2321
+ */
2322
+ const resolveEdge = useCallback(async (edgeSelUri, explainGraph) => {
2323
+ const triples = await queryWithSimpleRetry(edgeSelUri, explainGraph);
2324
+ const { edge, reasoning } = parseEdgeSelectionTriples(triples);
2325
+ if (!edge)
2326
+ return null;
2327
+ const selectedEdge = {
2328
+ edge,
2329
+ reasoning: reasoning || undefined,
2330
+ };
2331
+ // Kick off labels and provenance in parallel
2332
+ const [labels, provenanceChains] = await Promise.all([
2333
+ // Labels — all 3 in parallel
2334
+ Promise.all([
2335
+ resolveLabel(edge.s),
2336
+ resolveLabel(edge.p),
2337
+ resolveLabel(edge.o),
2338
+ ]),
2339
+ // Provenance
2340
+ traceProvenance
2341
+ ? traceEdgeProvenance(edge.s, edge.p, edge.o)
2342
+ : Promise.resolve([]),
2343
+ ]);
2344
+ selectedEdge.labels = { s: labels[0], p: labels[1], o: labels[2] };
2345
+ if (provenanceChains.length > 0) {
2346
+ selectedEdge.sources = provenanceChains.map((c) => c.chain).flat();
2347
+ }
2348
+ return selectedEdge;
2349
+ }, [queryWithSimpleRetry, resolveLabel, traceProvenance, traceEdgeProvenance]);
2350
+ /**
2351
+ * Unpack a focus event — resolve ALL edges in parallel
2352
+ */
2353
+ const unpackFocusEvent = useCallback(async (focusEvent) => {
2354
+ // Fire off all edge resolutions concurrently
2355
+ const edgePromises = focusEvent.edgeSelectionUris.map((uri) => resolveEdge(uri, focusEvent.explainGraph));
2356
+ const results = await Promise.all(edgePromises);
2357
+ const selectedEdges = results.filter((e) => e !== null);
2358
+ return {
2359
+ ...focusEvent,
2360
+ selectedEdges,
2361
+ };
2362
+ }, [resolveEdge]);
2363
+ /** Helper to update both state and ref together */
2364
+ const updateSession = useCallback((updater) => {
2365
+ sessionRef.current = updater(sessionRef.current);
2366
+ setSession(updater);
2367
+ }, []);
2368
+ /**
2369
+ * Process a single explain event
2370
+ */
2371
+ const processEvent = useCallback(async (event) => {
2372
+ // Query triples for main event node (stability retry)
2373
+ const triples = await queryWithStabilityRetry(event.explainId, event.explainGraph);
2374
+ // Parse into structured data
2375
+ const parsed = parseExplainTriples(event.explainId, event.explainGraph, triples);
2376
+ if (!parsed)
2377
+ return;
2378
+ // Update session based on event type
2379
+ updateSession((prev) => {
2380
+ const next = { ...prev };
2381
+ switch (parsed.type) {
2382
+ case "question":
2383
+ next.question = parsed;
2384
+ break;
2385
+ case "exploration":
2386
+ next.exploration = parsed;
2387
+ break;
2388
+ case "focus":
2389
+ // Will be updated again after unpacking
2390
+ next.focus = parsed;
2391
+ break;
2392
+ case "synthesis":
2393
+ next.synthesis = parsed;
2394
+ break;
2395
+ }
2396
+ return next;
2397
+ });
2398
+ // For focus events, unpack edges in parallel
2399
+ if (parsed.type === "focus") {
2400
+ const unpackedFocus = await unpackFocusEvent(parsed);
2401
+ updateSession((prev) => ({
2402
+ ...prev,
2403
+ focus: unpackedFocus,
2404
+ }));
2405
+ }
2406
+ }, [queryWithStabilityRetry, unpackFocusEvent, updateSession]);
2407
+ /**
2408
+ * Process the unpack queue
2409
+ */
2410
+ const processQueue = useCallback(async () => {
2411
+ if (isProcessingRef.current)
2412
+ return;
2413
+ if (unpackQueueRef.current.length === 0)
2414
+ return;
2415
+ isProcessingRef.current = true;
2416
+ setIsUnpacking(true);
2417
+ try {
2418
+ while (unpackQueueRef.current.length > 0) {
2419
+ const event = unpackQueueRef.current.shift();
2420
+ await processEvent(event);
2421
+ }
2422
+ }
2423
+ catch (err) {
2424
+ const msg = err instanceof Error ? err.message : String(err);
2425
+ setError(msg);
2426
+ }
2427
+ isProcessingRef.current = false;
2428
+ setIsUnpacking(false);
2429
+ // Check if more events were queued during processing (e.g., after a reset)
2430
+ if (unpackQueueRef.current.length > 0) {
2431
+ processQueue();
2432
+ }
2433
+ }, [processEvent]);
2434
+ /**
2435
+ * Add an explain event — immediately queues and starts processing
2436
+ */
2437
+ const addEvent = useCallback((event) => {
2438
+ setEvents((prev) => [...prev, event]);
2439
+ unpackQueueRef.current.push(event);
2440
+ processQueue();
2441
+ }, [processQueue]);
2442
+ /**
2443
+ * Reset the session
2444
+ */
2445
+ const reset = useCallback(() => {
2446
+ setEvents([]);
2447
+ setSession({});
2448
+ sessionRef.current = {};
2449
+ setError(null);
2450
+ unpackQueueRef.current = [];
2451
+ }, []);
2452
+ return {
2453
+ addEvent,
2454
+ session,
2455
+ sessionRef,
2456
+ events,
2457
+ isUnpacking,
2458
+ isProcessingRef,
2459
+ error,
2460
+ reset,
2461
+ };
2462
+ };
2463
+
1834
2464
  /**
1835
2465
  * High-level hook for managing chat sessions
1836
2466
  * Combines conversation state with inference services
@@ -1854,6 +2484,19 @@ const useChatSession = ({ flow } = {}) => {
1854
2484
  const effectiveFlow = flow ?? sessionFlowId;
1855
2485
  // Settings for GraphRAG configuration
1856
2486
  const { settings } = useSettings();
2487
+ // Explainability store for persisting sessions
2488
+ const addExplainSession = useExplainabilityStore((state) => state.addSession);
2489
+ // Explainability hook for processing events (processes each event immediately)
2490
+ const explainability = useExplainability({
2491
+ flow: effectiveFlow,
2492
+ collection: settings.collection,
2493
+ });
2494
+ const explainabilityRef = useRef(explainability);
2495
+ explainabilityRef.current = explainability;
2496
+ // Generate unique session IDs
2497
+ const generateSessionId = useCallback(() => {
2498
+ return `explain-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2499
+ }, []);
1857
2500
  // Inference services
1858
2501
  const inference = useInference({ flow });
1859
2502
  /**
@@ -1865,6 +2508,13 @@ const useChatSession = ({ flow } = {}) => {
1865
2508
  addActivity(ragActivity);
1866
2509
  let accumulated = "";
1867
2510
  let messageAdded = false;
2511
+ // Check if explainability is enabled
2512
+ const explainabilityEnabled = settings.featureSwitches.explainability;
2513
+ const sessionId = explainabilityEnabled ? generateSessionId() : undefined;
2514
+ // Reset explainability state for new query
2515
+ if (explainabilityEnabled) {
2516
+ explainabilityRef.current.reset();
2517
+ }
1868
2518
  try {
1869
2519
  // Execute Graph RAG with streaming and entity discovery
1870
2520
  const result = await inference.graphRag({
@@ -1880,8 +2530,8 @@ const useChatSession = ({ flow } = {}) => {
1880
2530
  onChunk: (chunk, complete) => {
1881
2531
  accumulated += chunk;
1882
2532
  if (!messageAdded) {
1883
- // Add empty message on first chunk
1884
- addMessage("ai", accumulated);
2533
+ // Add empty message on first chunk (with session ID if enabled)
2534
+ addMessage("ai", accumulated, undefined, sessionId);
1885
2535
  messageAdded = true;
1886
2536
  }
1887
2537
  else {
@@ -1889,9 +2539,32 @@ const useChatSession = ({ flow } = {}) => {
1889
2539
  updateLastMessage(accumulated);
1890
2540
  }
1891
2541
  },
2542
+ // Wire up explainability callback if feature is enabled
2543
+ ...(explainabilityEnabled && {
2544
+ onExplain: (event) => {
2545
+ console.log("[explain] event received:", event.explainId);
2546
+ explainabilityRef.current.addEvent(event);
2547
+ },
2548
+ }),
1892
2549
  },
1893
2550
  });
1894
2551
  removeActivity(ragActivity);
2552
+ // Store explainability session progressively — events are already being
2553
+ // processed as they arrive. Wait for processing to finish, then snapshot.
2554
+ if (explainabilityEnabled && sessionId) {
2555
+ const waitAndStore = async () => {
2556
+ // Poll until processing completes (events are processed as they arrive)
2557
+ const maxWait = 30000;
2558
+ let elapsed = 0;
2559
+ while (explainabilityRef.current.isProcessingRef.current && elapsed < maxWait) {
2560
+ await new Promise((r) => setTimeout(r, 500));
2561
+ elapsed += 500;
2562
+ }
2563
+ const sess = explainabilityRef.current.sessionRef.current;
2564
+ addExplainSession(sessionId, sess);
2565
+ };
2566
+ waitAndStore();
2567
+ }
1895
2568
  // Start embeddings activity
1896
2569
  addActivity(embActivity);
1897
2570
  // Get labels for each entity
@@ -1899,7 +2572,7 @@ const useChatSession = ({ flow } = {}) => {
1899
2572
  .filter((match) => match.entity !== null)
1900
2573
  .map(async (match) => {
1901
2574
  const entity = match.entity;
1902
- const labelActivity = "Label " + getTermValue$2(entity);
2575
+ const labelActivity = "Label " + getTermValue$3(entity);
1903
2576
  addActivity(labelActivity);
1904
2577
  try {
1905
2578
  const triples = await socket
@@ -1918,8 +2591,8 @@ const useChatSession = ({ flow } = {}) => {
1918
2591
  const entityList = labelResponses
1919
2592
  .filter((resp) => resp && resp.length > 0)
1920
2593
  .map((resp) => ({
1921
- label: getTermValue$2(resp[0].o),
1922
- uri: getTermValue$2(resp[0].s),
2594
+ label: getTermValue$3(resp[0].o),
2595
+ uri: getTermValue$3(resp[0].s),
1923
2596
  }));
1924
2597
  setEntities(entityList);
1925
2598
  removeActivity(embActivity);
@@ -5187,5 +5860,76 @@ const useChunkedDownload = (options = {}) => {
5187
5860
  };
5188
5861
  };
5189
5862
 
5190
- export { DEFAULT_SETTINGS, NotificationProvider, RDFS_LABEL, SETTINGS_STORAGE_KEY, createDocId, fileToBase64, generateFlowBlueprintId, getTermValue$2 as getTermValue, getTriples, prepareMetadata, textToBase64, useActivity, useAgentTools, useChat, useChatSession, useChunkedDownload, useChunkedUpload, useCollections, useConversation, useDocumentEmbeddingsQuery, useEmbeddings, useEntityDetail, useFlowBlueprints, useFlowParameters, useFlows, useGraphEmbeddings, useGraphSubgraph, useInference, useKnowledgeCores, useLLMModels, useLibrary, useLoadStateStore, useMcpTools, useNlpQuery, useNodeDetails, useNotification, useOntologies, useParameterValidation, useProcessing, useProgressStateStore, usePrompts, useRowEmbeddingsQuery, useRowsQuery, useSchemas, useSearchStateStore, useSessionStore, useSettings, useStructuredQuery, useTokenCosts, useTriples, useVectorSearch, useWorkbenchStateStore, vectorSearch };
5863
+ /**
5864
+ * Hook for fetching document metadata from the librarian service
5865
+ */
5866
+ /**
5867
+ * Hook for fetching a single document's metadata
5868
+ */
5869
+ const useDocumentMetadata = (options = {}) => {
5870
+ const { documentId, enabled } = options;
5871
+ const socket = useSocket();
5872
+ const connectionState = useConnectionState();
5873
+ const isSocketReady = connectionState?.status === "authenticated" ||
5874
+ connectionState?.status === "unauthenticated";
5875
+ const query = useQuery({
5876
+ queryKey: ["document-metadata", documentId],
5877
+ enabled: isSocketReady && !!documentId && (enabled !== false),
5878
+ queryFn: async () => {
5879
+ if (!documentId)
5880
+ return null;
5881
+ return socket.librarian().getDocumentMetadata(documentId);
5882
+ },
5883
+ });
5884
+ return {
5885
+ metadata: query.data ?? null,
5886
+ isLoading: query.isLoading,
5887
+ isError: query.isError,
5888
+ error: query.error,
5889
+ refetch: query.refetch,
5890
+ };
5891
+ };
5892
+ /**
5893
+ * Hook for fetching multiple documents' metadata
5894
+ */
5895
+ const useDocumentsMetadata = (documentIds = []) => {
5896
+ const socket = useSocket();
5897
+ const connectionState = useConnectionState();
5898
+ const queryClient = useQueryClient();
5899
+ const isSocketReady = connectionState?.status === "authenticated" ||
5900
+ connectionState?.status === "unauthenticated";
5901
+ const query = useQuery({
5902
+ queryKey: ["documents-metadata", documentIds],
5903
+ enabled: isSocketReady && documentIds.length > 0,
5904
+ queryFn: async () => {
5905
+ const results = await Promise.all(documentIds.map(async (id) => {
5906
+ // Check cache first
5907
+ const cached = queryClient.getQueryData([
5908
+ "document-metadata",
5909
+ id,
5910
+ ]);
5911
+ if (cached !== undefined) {
5912
+ return { id, metadata: cached };
5913
+ }
5914
+ // Fetch and cache
5915
+ const metadata = await socket.librarian().getDocumentMetadata(id);
5916
+ queryClient.setQueryData(["document-metadata", id], metadata);
5917
+ return { id, metadata };
5918
+ }));
5919
+ return results.reduce((acc, { id, metadata }) => {
5920
+ acc[id] = metadata;
5921
+ return acc;
5922
+ }, {});
5923
+ },
5924
+ });
5925
+ return {
5926
+ metadataMap: query.data ?? {},
5927
+ isLoading: query.isLoading,
5928
+ isError: query.isError,
5929
+ error: query.error,
5930
+ refetch: query.refetch,
5931
+ };
5932
+ };
5933
+
5934
+ export { DEFAULT_SETTINGS, NotificationProvider, RDFS_LABEL, SETTINGS_STORAGE_KEY, createDocId, extractQuotedTriple, fileToBase64, generateFlowBlueprintId, getEventType, getTermValue$1 as getExplainTermValue, getTermValue$3 as getTermValue, getTriples, parseEdgeSelectionTriples, parseExplainTriples, parseExplorationTriples, parseFocusTriples, parseQuestionTriples, parseSynthesisTriples, prepareMetadata, textToBase64, useActivity, useAgentTools, useChat, useChatSession, useChunkedDownload, useChunkedUpload, useCollections, useConversation, useDocumentEmbeddingsQuery, useDocumentMetadata, useDocumentsMetadata, useEmbeddings, useEntityDetail, useExplainability, useExplainabilityStore, useFlowBlueprints, useFlowParameters, useFlows, useGraphEmbeddings, useGraphSubgraph, useInference, useKnowledgeCores, useLLMModels, useLibrary, useLoadStateStore, useMcpTools, useNlpQuery, useNodeDetails, useNotification, useOntologies, useParameterValidation, useProcessing, useProgressStateStore, usePrompts, useProvenance, useRowEmbeddingsQuery, useRowsQuery, useSchemas, useSearchStateStore, useSessionStore, useSettings, useStructuredQuery, useTokenCosts, useTriples, useVectorSearch, useWorkbenchStateStore, vectorSearch };
5191
5935
  //# sourceMappingURL=index.esm.js.map