@novu/react 3.19.1-rc.68f5589d4e → 3.19.1-rc.852194fa77

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.
@@ -23,286 +23,249 @@ __export(useAgentChat_exports, {
23
23
  useAgentChat: () => useAgentChat
24
24
  });
25
25
  module.exports = __toCommonJS(useAgentChat_exports);
26
- var import_js = require("@novu/js");
27
26
  var import_react = require("react");
28
27
  var import_useDataRef = require("./internal/useDataRef.cjs");
29
28
  var import_NovuProvider = require("./NovuProvider.cjs");
30
- function createLocalSessionKey() {
31
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
32
- return `local_${crypto.randomUUID().replace(/-/g, "").slice(0, 12)}`;
29
+ var EMPTY_SERVER_SNAPSHOT = {
30
+ key: "ssr",
31
+ status: "ready",
32
+ run: { isRunning: false },
33
+ conversationStatus: "active",
34
+ pagination: { hasMore: false, status: "idle" },
35
+ messages: [],
36
+ pendingActions: [],
37
+ isRecovering: false
38
+ };
39
+ function handlePublicationCallbacks(args) {
40
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
41
+ const {
42
+ snapshot,
43
+ meta,
44
+ conversationIdProp,
45
+ propsRef,
46
+ loadNotifiedRef,
47
+ notifiedCatchUpErrorRef,
48
+ lastReportedErrorKeyRef
49
+ } = args;
50
+ if (((meta == null ? void 0 : meta.historyLoaded) || ((_a = meta == null ? void 0 : meta.change) == null ? void 0 : _a.kind) === "history") && !loadNotifiedRef.current && conversationIdProp) {
51
+ if (snapshot.conversationId) {
52
+ loadNotifiedRef.current = true;
53
+ (_c = (_b = propsRef.current).onSuccess) == null ? void 0 : _c.call(_b, {
54
+ conversationId: snapshot.conversationId,
55
+ messages: [...snapshot.messages],
56
+ hasMore: snapshot.pagination.hasMore
57
+ });
58
+ }
59
+ }
60
+ if (snapshot.catchUpError && snapshot.catchUpError !== notifiedCatchUpErrorRef.current) {
61
+ notifiedCatchUpErrorRef.current = snapshot.catchUpError;
62
+ (_e = (_d = propsRef.current).onError) == null ? void 0 : _e.call(_d, snapshot.catchUpError);
63
+ } else if (!snapshot.catchUpError) {
64
+ notifiedCatchUpErrorRef.current = void 0;
65
+ }
66
+ if (snapshot.error) {
67
+ const originalMessage = "originalError" in snapshot.error ? ((_f = snapshot.error.originalError) == null ? void 0 : _f.message) ?? "" : "";
68
+ const errorKey = `${snapshot.error.message}:${originalMessage}`;
69
+ if (lastReportedErrorKeyRef.current !== errorKey) {
70
+ lastReportedErrorKeyRef.current = errorKey;
71
+ (_h = (_g = propsRef.current).onError) == null ? void 0 : _h.call(_g, snapshot.error);
72
+ }
73
+ } else {
74
+ lastReportedErrorKeyRef.current = void 0;
75
+ }
76
+ const change = meta == null ? void 0 : meta.change;
77
+ if ((change == null ? void 0 : change.kind) === "live") {
78
+ (_j = (_i = propsRef.current).onEvent) == null ? void 0 : _j.call(_i, change.envelope);
79
+ }
80
+ if (change && change.kind !== "history") {
81
+ for (const message of change.addedMessages) {
82
+ (_l = (_k = propsRef.current).onMessage) == null ? void 0 : _l.call(_k, message);
83
+ }
33
84
  }
34
- if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
35
- const bytes = new Uint8Array(6);
36
- crypto.getRandomValues(bytes);
37
- return `local_${Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("")}`;
85
+ if (change) {
86
+ for (const action of change.newActions) {
87
+ (_n = (_m = propsRef.current).onActionRequested) == null ? void 0 : _n.call(_m, action);
88
+ }
38
89
  }
39
- return `local_${Date.now().toString(36)}`;
40
90
  }
41
- var EMPTY_CONVERSATION = {
42
- messages: [],
43
- isRunning: false,
44
- typing: void 0,
45
- status: "active",
46
- hasMore: false
47
- };
48
- function applyConversationSnapshot(snapshot, setters) {
49
- setters.setMessages(snapshot.messages);
50
- setters.setIsRunning(snapshot.isRunning);
51
- setters.setTyping(snapshot.typing);
52
- setters.setStatus(snapshot.status);
53
- setters.setHasMore(snapshot.hasMore);
91
+ function getCreateFlowKey(agentId, agentHash) {
92
+ return `${agentId}\0${agentHash ?? ""}`;
93
+ }
94
+ function resolveOwnedRuntime(args) {
95
+ const key = getCreateFlowKey(args.agentId, args.agentHash);
96
+ const current = args.ownedRuntimeRef.current;
97
+ if ((current == null ? void 0 : current.key) === key) {
98
+ return current.runtime;
99
+ }
100
+ current == null ? void 0 : current.runtime.dispose();
101
+ const result = args.novu.agentChat.conversation({ agentId: args.agentId, agentHash: args.agentHash });
102
+ if (!result.ok) {
103
+ args.ownedRuntimeRef.current = null;
104
+ return null;
105
+ }
106
+ args.ownedRuntimeRef.current = { key, runtime: result.data };
107
+ return result.data;
54
108
  }
55
109
  var useAgentChat = (props) => {
56
- const { agentId, agentHash, conversationId: conversationIdProp } = props;
57
- const propsRef = (0, import_useDataRef.useDataRef)(props);
110
+ var _a;
58
111
  const novu = (0, import_NovuProvider.useNovu)();
59
- const [localSessionKey, setLocalSessionKey] = (0, import_react.useState)(createLocalSessionKey);
60
- const sessionKey = conversationIdProp ?? localSessionKey;
61
- const sessionKeyRef = (0, import_useDataRef.useDataRef)(sessionKey);
62
- const prevAgentIdRef = (0, import_react.useRef)(agentId);
63
- const prevConversationIdPropRef = (0, import_react.useRef)(conversationIdProp);
64
- const [assignedConversationId, setAssignedConversationId] = (0, import_react.useState)();
65
- const conversationId = conversationIdProp ?? assignedConversationId;
66
- const conversationIdRef = (0, import_useDataRef.useDataRef)(conversationId);
67
- const [messages, setMessages] = (0, import_react.useState)([]);
68
- const [isRunning, setIsRunning] = (0, import_react.useState)(false);
69
- const [typing, setTyping] = (0, import_react.useState)();
70
- const [status, setStatus] = (0, import_react.useState)("active");
71
- const [hasMore, setHasMore] = (0, import_react.useState)(false);
72
- const [error, setError] = (0, import_react.useState)();
73
- const [isLoading, setIsLoading] = (0, import_react.useState)(Boolean(conversationIdProp));
74
- const [isFetching, setIsFetching] = (0, import_react.useState)(false);
75
- const fetchGenerationRef = (0, import_react.useRef)(0);
76
- const pendingActions = (0, import_react.useMemo)(() => (0, import_js.derivePendingActions)(messages), [messages]);
77
- const snapshotSetters = (0, import_react.useMemo)(
78
- () => ({
79
- setMessages,
80
- setIsRunning,
81
- setTyping,
82
- setStatus,
83
- setHasMore
84
- }),
85
- []
86
- );
87
- (0, import_react.useEffect)(() => {
88
- const agentChanged = prevAgentIdRef.current !== agentId;
89
- const prevConversationIdProp = prevConversationIdPropRef.current;
90
- prevAgentIdRef.current = agentId;
91
- prevConversationIdPropRef.current = conversationIdProp;
92
- if (agentChanged) {
93
- setAssignedConversationId(void 0);
94
- setLocalSessionKey(createLocalSessionKey());
95
- applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
96
- setError(void 0);
97
- setIsLoading(Boolean(conversationIdProp));
98
- return;
99
- }
100
- if (conversationIdProp) {
101
- setAssignedConversationId(void 0);
102
- return;
112
+ const propsRef = (0, import_useDataRef.useDataRef)(props);
113
+ const sharedRuntime = "conversation" in props ? props.conversation : void 0;
114
+ const agentId = (sharedRuntime == null ? void 0 : sharedRuntime.agentId) ?? props.agentId;
115
+ const conversationIdProp = sharedRuntime ? void 0 : props.conversationId;
116
+ const agentHash = sharedRuntime ? void 0 : props.agentHash;
117
+ const ownedRuntimeRef = (0, import_react.useRef)(null);
118
+ const cachedRuntime = (0, import_react.useMemo)(() => {
119
+ if (sharedRuntime) {
120
+ return sharedRuntime;
103
121
  }
104
- setIsLoading(false);
105
- if (prevConversationIdProp !== void 0) {
106
- setAssignedConversationId(void 0);
107
- setLocalSessionKey(createLocalSessionKey());
108
- applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
122
+ if (!conversationIdProp) {
123
+ return null;
109
124
  }
110
- }, [agentId, conversationIdProp, snapshotSetters]);
111
- const fetchConversation = (0, import_react.useCallback)(
112
- async (targetConversationId) => {
113
- var _a, _b, _c, _d;
114
- const generation = ++fetchGenerationRef.current;
115
- setError(void 0);
116
- setIsLoading(true);
117
- setIsFetching(true);
118
- const response = await novu.agentChat.loadConversation({
119
- agentId,
120
- conversationId: targetConversationId
121
- });
122
- if (generation !== fetchGenerationRef.current) {
123
- return;
124
- }
125
- if (response.error) {
126
- setError(response.error);
127
- (_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
128
- } else if (response.data) {
129
- setMessages(response.data.messages);
130
- setHasMore(response.data.hasMore);
131
- (_d = (_c = propsRef.current).onSuccess) == null ? void 0 : _d.call(_c, response.data);
132
- }
133
- setIsLoading(false);
134
- setIsFetching(false);
135
- },
136
- [novu, agentId, propsRef]
137
- );
138
- (0, import_react.useEffect)(() => {
139
- var _a, _b;
140
- novu.agentChat.subscribe();
141
- const snapshot = novu.agentChat.getConversation({
125
+ const result = novu.agentChat.conversation({
142
126
  agentId,
143
- key: sessionKey,
144
- conversationId: conversationIdProp
127
+ conversationId: conversationIdProp,
128
+ agentHash
145
129
  });
146
- if (snapshot) {
147
- applyConversationSnapshot(
148
- {
149
- messages: snapshot.messages,
150
- isRunning: snapshot.isRunning,
151
- typing: snapshot.typing,
152
- status: snapshot.status,
153
- hasMore: snapshot.hasMore
154
- },
155
- snapshotSetters
156
- );
157
- if (snapshot.conversationId && !conversationIdProp) {
158
- setAssignedConversationId(snapshot.conversationId);
159
- }
160
- for (const action of (0, import_js.derivePendingActions)(snapshot.messages)) {
161
- (_b = (_a = propsRef.current).onActionRequested) == null ? void 0 : _b.call(_a, action);
162
- }
163
- } else if (!conversationIdProp) {
164
- applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
165
- }
166
- const cleanup = novu.on("agent_chat.messages.updated", ({ data }) => {
167
- var _a2, _b2, _c, _d, _e, _f;
168
- if (data.key !== sessionKeyRef.current) {
169
- return;
170
- }
171
- applyConversationSnapshot(
172
- {
173
- messages: data.messages,
174
- isRunning: data.isRunning,
175
- typing: data.typing,
176
- status: data.status,
177
- hasMore: data.hasMore
178
- },
179
- snapshotSetters
180
- );
181
- if (data.conversationId && !propsRef.current.conversationId) {
182
- setAssignedConversationId(data.conversationId);
183
- }
184
- const { change } = data;
185
- if (change.kind === "live") {
186
- (_b2 = (_a2 = propsRef.current).onEvent) == null ? void 0 : _b2.call(_a2, change.envelope);
130
+ return result.ok ? result.data : null;
131
+ }, [sharedRuntime, novu, agentId, conversationIdProp, agentHash]);
132
+ if (sharedRuntime || conversationIdProp) {
133
+ (_a = ownedRuntimeRef.current) == null ? void 0 : _a.runtime.dispose();
134
+ ownedRuntimeRef.current = null;
135
+ }
136
+ const ownedRuntime = sharedRuntime || conversationIdProp ? null : resolveOwnedRuntime({ novu, agentId, agentHash, ownedRuntimeRef });
137
+ const runtime = sharedRuntime ?? cachedRuntime ?? ownedRuntime;
138
+ (0, import_react.useEffect)(() => {
139
+ return () => {
140
+ var _a2;
141
+ (_a2 = ownedRuntimeRef.current) == null ? void 0 : _a2.runtime.dispose();
142
+ ownedRuntimeRef.current = null;
143
+ };
144
+ }, []);
145
+ const loadNotifiedRef = (0, import_react.useRef)(false);
146
+ const replayedActionsRef = (0, import_react.useRef)(false);
147
+ const notifiedCatchUpErrorRef = (0, import_react.useRef)();
148
+ const lastReportedErrorKeyRef = (0, import_react.useRef)();
149
+ (0, import_react.useEffect)(() => {
150
+ loadNotifiedRef.current = false;
151
+ replayedActionsRef.current = false;
152
+ notifiedCatchUpErrorRef.current = void 0;
153
+ lastReportedErrorKeyRef.current = void 0;
154
+ }, [runtime]);
155
+ const subscribe = (0, import_react.useCallback)(
156
+ (onStoreChange) => {
157
+ var _a2, _b;
158
+ if (!runtime) {
159
+ return () => {
160
+ };
187
161
  }
188
- if (change.kind !== "history") {
189
- for (const message of change.addedMessages) {
190
- (_d = (_c = propsRef.current).onMessage) == null ? void 0 : _d.call(_c, message);
162
+ if (!replayedActionsRef.current) {
163
+ replayedActionsRef.current = true;
164
+ for (const action of runtime.getSnapshot().pendingActions) {
165
+ (_b = (_a2 = propsRef.current).onActionRequested) == null ? void 0 : _b.call(_a2, action);
191
166
  }
192
167
  }
193
- for (const action of change.newActions) {
194
- (_f = (_e = propsRef.current).onActionRequested) == null ? void 0 : _f.call(_e, action);
195
- }
196
- });
197
- if (conversationIdProp) {
198
- void fetchConversation(conversationIdProp);
199
- }
200
- return () => {
201
- cleanup();
202
- novu.agentChat.unsubscribe();
203
- };
204
- }, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, propsRef, fetchConversation, snapshotSetters]);
205
- const refetch = (0, import_react.useCallback)(async () => {
206
- const id = conversationIdRef.current;
207
- if (!id) {
208
- return;
209
- }
210
- await fetchConversation(id);
211
- }, [conversationIdRef, fetchConversation]);
212
- const fetchMore = (0, import_react.useCallback)(async () => {
213
- var _a, _b;
214
- const response = await novu.agentChat.fetchMore({
215
- agentId,
216
- key: sessionKeyRef.current,
217
- conversationId: conversationIdRef.current
218
- });
219
- if (response.error) {
220
- setError(response.error);
221
- (_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
222
- } else if (response.data) {
223
- setMessages(response.data.messages);
224
- setHasMore(response.data.hasMore);
225
- }
226
- return response;
227
- }, [novu, agentId, sessionKeyRef, conversationIdRef, propsRef]);
228
- const sendMessage = (0, import_react.useCallback)(
229
- async (text) => {
230
- var _a, _b;
231
- setError(void 0);
232
- const response = await novu.agentChat.sendMessage({
233
- agentId,
234
- agentHash,
235
- text,
236
- key: sessionKeyRef.current,
237
- conversationId: conversationIdRef.current
168
+ return runtime.subscribe((snapshot2, meta) => {
169
+ onStoreChange();
170
+ handlePublicationCallbacks({
171
+ snapshot: snapshot2,
172
+ meta,
173
+ conversationIdProp,
174
+ propsRef,
175
+ loadNotifiedRef,
176
+ notifiedCatchUpErrorRef,
177
+ lastReportedErrorKeyRef
178
+ });
238
179
  });
239
- if (response.error) {
240
- setError(response.error);
241
- (_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
242
- } else if (response.data && !propsRef.current.conversationId) {
243
- setAssignedConversationId(response.data.conversationId);
244
- }
245
- return response;
246
180
  },
247
- [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
181
+ [runtime, conversationIdProp, propsRef]
248
182
  );
249
- const respondToAction = (0, import_react.useCallback)(
250
- async (args) => {
251
- var _a, _b;
252
- setError(void 0);
253
- const response = await novu.agentChat.respondToAction({
254
- agentId,
255
- agentHash,
256
- key: sessionKeyRef.current,
257
- conversationId: conversationIdRef.current,
258
- actionId: args.actionId,
259
- decision: args.decision
260
- });
183
+ const getSnapshot = (0, import_react.useCallback)(() => {
184
+ return (runtime == null ? void 0 : runtime.getSnapshot()) ?? EMPTY_SERVER_SNAPSHOT;
185
+ }, [runtime]);
186
+ const getServerSnapshot = (0, import_react.useCallback)(() => {
187
+ return (runtime == null ? void 0 : runtime.getServerSnapshot()) ?? EMPTY_SERVER_SNAPSHOT;
188
+ }, [runtime]);
189
+ const snapshot = (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
190
+ const callRuntime = (0, import_react.useCallback)(
191
+ async (action) => {
192
+ var _a2, _b;
193
+ if (!runtime) {
194
+ return { error: void 0 };
195
+ }
196
+ const response = await action(runtime);
261
197
  if (response.error) {
262
- setError(response.error);
263
- (_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
198
+ (_b = (_a2 = propsRef.current).onError) == null ? void 0 : _b.call(_a2, response.error);
264
199
  }
265
200
  return response;
266
201
  },
267
- [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
202
+ [runtime, propsRef]
268
203
  );
269
- const sendAction = (0, import_react.useCallback)(
270
- async (args) => {
271
- var _a, _b;
272
- setError(void 0);
273
- const response = await novu.agentChat.sendAction({
274
- agentId,
275
- agentHash,
276
- key: sessionKeyRef.current,
277
- conversationId: conversationIdRef.current,
278
- actionId: args.actionId,
279
- sourceMessageId: args.sourceMessageId,
280
- value: args.value
204
+ const refetch = (0, import_react.useCallback)(async () => {
205
+ var _a2, _b;
206
+ if (!runtime) {
207
+ return;
208
+ }
209
+ const response = await runtime.load();
210
+ if (response.data) {
211
+ (_b = (_a2 = propsRef.current).onSuccess) == null ? void 0 : _b.call(_a2, {
212
+ conversationId: response.data.conversationId,
213
+ messages: [...response.data.messages],
214
+ hasMore: response.data.hasMore
281
215
  });
282
- if (response.error) {
283
- setError(response.error);
284
- (_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
285
- }
286
- return response;
287
- },
288
- [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
216
+ }
217
+ }, [runtime, propsRef]);
218
+ const fetchMore = (0, import_react.useCallback)(async () => {
219
+ const response = await callRuntime((target) => target.fetchMore());
220
+ return {
221
+ ...response,
222
+ error: response.error,
223
+ data: response.data ? {
224
+ messages: [...response.data.messages],
225
+ hasMore: response.data.hasMore
226
+ } : void 0
227
+ };
228
+ }, [callRuntime]);
229
+ const paginationWithFetch = (0, import_react.useMemo)(
230
+ () => ({
231
+ status: snapshot.pagination.status,
232
+ hasMore: snapshot.pagination.hasMore,
233
+ fetchMore
234
+ }),
235
+ [snapshot.pagination.status, snapshot.pagination.hasMore, fetchMore]
236
+ );
237
+ const sendMessage = (0, import_react.useCallback)((text) => callRuntime((target) => target.sendMessage(text)), [callRuntime]);
238
+ const respondToAction = (0, import_react.useCallback)(
239
+ (args) => callRuntime((target) => target.respondToAction(args)),
240
+ [callRuntime]
241
+ );
242
+ const sendAction = (0, import_react.useCallback)(
243
+ (args) => callRuntime((target) => target.sendAction(args)),
244
+ [callRuntime]
245
+ );
246
+ const retryMessage = (0, import_react.useCallback)(
247
+ (messageId) => callRuntime((target) => target.retryMessage(messageId)),
248
+ [callRuntime]
289
249
  );
290
250
  return {
291
- messages,
292
- pendingActions,
251
+ messages: [...snapshot.messages],
252
+ pendingActions: [...snapshot.pendingActions],
253
+ conversationId: snapshot.conversationId,
254
+ error: snapshot.error,
255
+ isLoading: snapshot.status === "loading",
256
+ isRunning: snapshot.run.isRunning,
257
+ typing: snapshot.run.typing,
258
+ status: snapshot.conversationStatus,
259
+ conversationStatus: snapshot.conversationStatus,
260
+ run: snapshot.run,
261
+ pagination: paginationWithFetch,
262
+ isRecovering: snapshot.isRecovering,
263
+ catchUpError: snapshot.catchUpError,
264
+ refetch,
293
265
  sendMessage,
294
266
  respondToAction,
295
267
  sendAction,
296
- conversationId,
297
- error,
298
- isLoading,
299
- isFetching,
300
- isRunning,
301
- typing,
302
- status,
303
- hasMore,
304
- refetch,
305
- fetchMore
268
+ retryMessage
306
269
  };
307
270
  };
308
271
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/useAgentChat.ts"],"sourcesContent":["import type {\n AgentChatPlanLimitError,\n AgentConversationStatus,\n AgentConversationTyping,\n AgentEventEnvelope,\n AgentHashFields,\n AgentMessage,\n AgentPendingAction,\n AgentToolApprovalDecision,\n LoadConversationResult,\n NovuError,\n RespondToActionResult,\n SendActionResult,\n SendMessageResult,\n} from '@novu/js';\nimport { derivePendingActions } from '@novu/js';\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { useDataRef } from './internal/useDataRef';\nimport { useNovu } from './NovuProvider';\n\nexport type UseAgentChatProps = AgentHashFields & {\n agentId: string;\n /**\n * Resume this conversation. The hook loads history on mount.\n * Omit this prop to start a new chat. The first send creates a conversation.\n * Later sends pass the returned id. Remount or clear this prop to start another chat.\n */\n conversationId?: string;\n onSuccess?: (data: LoadConversationResult) => void;\n onError?: (error: NovuError | AgentChatPlanLimitError) => void;\n /**\n * Fires once per message, when the message id first appears on the conversation.\n * History pages are silent: only new activity fires.\n * An agent message can still be empty at this point, because the first envelope of a\n * turn creates the message before any text is folded into it.\n * A send that never reaches the server does not fire: the message flips to `failed` instead.\n */\n onMessage?: (message: AgentMessage) => void;\n /**\n * Fires once per pending action, including actions still pending on mount, so a\n * resumed conversation reports what it is blocked on. Paging backwards is silent.\n */\n onActionRequested?: (action: AgentPendingAction) => void;\n /**\n * Raw envelopes for this conversation, before the derived callbacks for the same fold.\n * A duplicate envelope that the store drops does not fire. Neither does an envelope that\n * arrives before a newly created conversation claims its id.\n * The store folds the envelope before this callback runs, so `messages` here is one render old.\n */\n onEvent?: (envelope: AgentEventEnvelope) => void;\n};\n\nexport type UseAgentChatResult = {\n messages: AgentMessage[];\n pendingActions: AgentPendingAction[];\n conversationId?: string;\n error?: NovuError | AgentChatPlanLimitError;\n /** True until the first history fetch completes. False when there is no `conversationId` prop. */\n isLoading: boolean;\n isFetching: boolean;\n isRunning: boolean;\n typing?: AgentConversationTyping;\n status: AgentConversationStatus;\n /** True when older history pages are available via `fetchMore`. */\n hasMore: boolean;\n refetch: () => Promise<void>;\n fetchMore: () => Promise<{\n data?: { messages: AgentMessage[]; hasMore: boolean };\n error?: NovuError;\n }>;\n sendMessage: (text: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n respondToAction: (args: { actionId: string; decision: AgentToolApprovalDecision }) => Promise<{\n data?: RespondToActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n sendAction: (args: { actionId: string; sourceMessageId: string; value?: string }) => Promise<{\n data?: SendActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n};\n\nfunction createLocalSessionKey(): string {\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n return `local_${crypto.randomUUID().replace(/-/g, '').slice(0, 12)}`;\n }\n\n if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {\n const bytes = new Uint8Array(6);\n crypto.getRandomValues(bytes);\n\n return `local_${Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')}`;\n }\n\n return `local_${Date.now().toString(36)}`;\n}\n\ntype ConversationSnapshot = {\n messages: AgentMessage[];\n isRunning: boolean;\n typing?: AgentConversationTyping;\n status: AgentConversationStatus;\n hasMore: boolean;\n};\n\nconst EMPTY_CONVERSATION: ConversationSnapshot = {\n messages: [],\n isRunning: false,\n typing: undefined,\n status: 'active',\n hasMore: false,\n};\n\nfunction applyConversationSnapshot(\n snapshot: ConversationSnapshot,\n setters: {\n setMessages: (messages: AgentMessage[]) => void;\n setIsRunning: (isRunning: boolean) => void;\n setTyping: (typing?: AgentConversationTyping) => void;\n setStatus: (status: AgentConversationStatus) => void;\n setHasMore: (hasMore: boolean) => void;\n }\n): void {\n setters.setMessages(snapshot.messages);\n setters.setIsRunning(snapshot.isRunning);\n setters.setTyping(snapshot.typing);\n setters.setStatus(snapshot.status);\n setters.setHasMore(snapshot.hasMore);\n}\n\nexport const useAgentChat = (props: UseAgentChatProps): UseAgentChatResult => {\n const { agentId, agentHash, conversationId: conversationIdProp } = props;\n const propsRef = useDataRef(props);\n const novu = useNovu();\n\n // Resume: the prop is the key on the same render (no effect lag).\n // Create: keep a `local_*` key until remount, prop clear, or agent change.\n const [localSessionKey, setLocalSessionKey] = useState(createLocalSessionKey);\n const sessionKey = conversationIdProp ?? localSessionKey;\n const sessionKeyRef = useDataRef(sessionKey);\n const prevAgentIdRef = useRef(agentId);\n const prevConversationIdPropRef = useRef(conversationIdProp);\n\n const [assignedConversationId, setAssignedConversationId] = useState<string>();\n const conversationId = conversationIdProp ?? assignedConversationId;\n const conversationIdRef = useDataRef(conversationId);\n\n const [messages, setMessages] = useState<AgentMessage[]>([]);\n const [isRunning, setIsRunning] = useState(false);\n const [typing, setTyping] = useState<AgentConversationTyping>();\n const [status, setStatus] = useState<AgentConversationStatus>('active');\n const [hasMore, setHasMore] = useState(false);\n const [error, setError] = useState<NovuError | AgentChatPlanLimitError>();\n const [isLoading, setIsLoading] = useState(Boolean(conversationIdProp));\n const [isFetching, setIsFetching] = useState(false);\n const fetchGenerationRef = useRef(0);\n\n const pendingActions = useMemo(() => derivePendingActions(messages), [messages]);\n\n const snapshotSetters = useMemo(\n () => ({\n setMessages,\n setIsRunning,\n setTyping,\n setStatus,\n setHasMore,\n }),\n []\n );\n\n useEffect(() => {\n const agentChanged = prevAgentIdRef.current !== agentId;\n const prevConversationIdProp = prevConversationIdPropRef.current;\n prevAgentIdRef.current = agentId;\n prevConversationIdPropRef.current = conversationIdProp;\n\n if (agentChanged) {\n setAssignedConversationId(undefined);\n setLocalSessionKey(createLocalSessionKey());\n applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);\n setError(undefined);\n setIsLoading(Boolean(conversationIdProp));\n\n return;\n }\n\n if (conversationIdProp) {\n setAssignedConversationId(undefined);\n\n return;\n }\n\n setIsLoading(false);\n if (prevConversationIdProp !== undefined) {\n setAssignedConversationId(undefined);\n setLocalSessionKey(createLocalSessionKey());\n applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);\n }\n }, [agentId, conversationIdProp, snapshotSetters]);\n\n const fetchConversation = useCallback(\n async (targetConversationId: string) => {\n const generation = ++fetchGenerationRef.current;\n setError(undefined);\n setIsLoading(true);\n setIsFetching(true);\n\n const response = await novu.agentChat.loadConversation({\n agentId,\n conversationId: targetConversationId,\n });\n\n if (generation !== fetchGenerationRef.current) {\n return;\n }\n\n if (response.error) {\n setError(response.error);\n propsRef.current.onError?.(response.error);\n } else if (response.data) {\n setMessages(response.data.messages);\n setHasMore(response.data.hasMore);\n propsRef.current.onSuccess?.(response.data);\n }\n\n setIsLoading(false);\n setIsFetching(false);\n },\n [novu, agentId, propsRef]\n );\n\n useEffect(() => {\n novu.agentChat.subscribe();\n\n const snapshot = novu.agentChat.getConversation({\n agentId,\n key: sessionKey,\n conversationId: conversationIdProp,\n });\n if (snapshot) {\n applyConversationSnapshot(\n {\n messages: snapshot.messages,\n isRunning: snapshot.isRunning,\n typing: snapshot.typing,\n status: snapshot.status,\n hasMore: snapshot.hasMore,\n },\n snapshotSetters\n );\n if (snapshot.conversationId && !conversationIdProp) {\n setAssignedConversationId(snapshot.conversationId);\n }\n\n // The store reports each action once per holder, and a holder outlives a mount.\n // Replay from the snapshot so a remount still learns what the run is blocked on.\n for (const action of derivePendingActions(snapshot.messages)) {\n propsRef.current.onActionRequested?.(action);\n }\n } else if (!conversationIdProp) {\n applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);\n }\n\n const cleanup = novu.on('agent_chat.messages.updated', ({ data }) => {\n if (data.key !== sessionKeyRef.current) {\n return;\n }\n\n applyConversationSnapshot(\n {\n messages: data.messages,\n isRunning: data.isRunning,\n typing: data.typing,\n status: data.status,\n hasMore: data.hasMore,\n },\n snapshotSetters\n );\n if (data.conversationId && !propsRef.current.conversationId) {\n setAssignedConversationId(data.conversationId);\n }\n\n const { change } = data;\n if (change.kind === 'live') {\n propsRef.current.onEvent?.(change.envelope);\n }\n\n if (change.kind !== 'history') {\n for (const message of change.addedMessages) {\n propsRef.current.onMessage?.(message);\n }\n }\n\n for (const action of change.newActions) {\n propsRef.current.onActionRequested?.(action);\n }\n });\n\n if (conversationIdProp) {\n void fetchConversation(conversationIdProp);\n }\n\n return () => {\n cleanup();\n novu.agentChat.unsubscribe();\n };\n }, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, propsRef, fetchConversation, snapshotSetters]);\n\n const refetch = useCallback(async () => {\n const id = conversationIdRef.current;\n if (!id) {\n return;\n }\n\n await fetchConversation(id);\n }, [conversationIdRef, fetchConversation]);\n\n const fetchMore = useCallback(async () => {\n const response = await novu.agentChat.fetchMore({\n agentId,\n key: sessionKeyRef.current,\n conversationId: conversationIdRef.current,\n });\n\n if (response.error) {\n setError(response.error);\n propsRef.current.onError?.(response.error);\n } else if (response.data) {\n setMessages(response.data.messages);\n setHasMore(response.data.hasMore);\n }\n\n return response;\n }, [novu, agentId, sessionKeyRef, conversationIdRef, propsRef]);\n\n const sendMessage = useCallback(\n async (text: string) => {\n setError(undefined);\n\n const response = await novu.agentChat.sendMessage({\n agentId,\n agentHash,\n text,\n key: sessionKeyRef.current,\n conversationId: conversationIdRef.current,\n });\n\n if (response.error) {\n setError(response.error);\n propsRef.current.onError?.(response.error);\n } else if (response.data && !propsRef.current.conversationId) {\n setAssignedConversationId(response.data.conversationId);\n }\n\n return response;\n },\n [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]\n );\n\n const respondToAction = useCallback(\n async (args: { actionId: string; decision: AgentToolApprovalDecision }) => {\n setError(undefined);\n\n const response = await novu.agentChat.respondToAction({\n agentId,\n agentHash,\n key: sessionKeyRef.current,\n conversationId: conversationIdRef.current,\n actionId: args.actionId,\n decision: args.decision,\n });\n\n if (response.error) {\n setError(response.error);\n propsRef.current.onError?.(response.error);\n }\n\n return response;\n },\n [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]\n );\n\n const sendAction = useCallback(\n async (args: { actionId: string; sourceMessageId: string; value?: string }) => {\n setError(undefined);\n\n const response = await novu.agentChat.sendAction({\n agentId,\n agentHash,\n key: sessionKeyRef.current,\n conversationId: conversationIdRef.current,\n actionId: args.actionId,\n sourceMessageId: args.sourceMessageId,\n value: args.value,\n });\n\n if (response.error) {\n setError(response.error);\n propsRef.current.onError?.(response.error);\n }\n\n return response;\n },\n [novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]\n );\n\n return {\n messages,\n pendingActions,\n sendMessage,\n respondToAction,\n sendAction,\n conversationId,\n error,\n isLoading,\n isFetching,\n isRunning,\n typing,\n status,\n hasMore,\n refetch,\n fetchMore,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,gBAAqC;AACrC,mBAAkE;AAClE,wBAA2B;AAC3B,0BAAwB;AAkExB,SAAS,wBAAgC;AACvC,MAAI,OAAO,WAAW,eAAe,OAAO,OAAO,eAAe,YAAY;AAC5E,WAAO,SAAS,OAAO,WAAW,EAAE,QAAQ,MAAM,EAAE,EAAE,MAAM,GAAG,EAAE,CAAC;AAAA,EACpE;AAEA,MAAI,OAAO,WAAW,eAAe,OAAO,OAAO,oBAAoB,YAAY;AACjF,UAAM,QAAQ,IAAI,WAAW,CAAC;AAC9B,WAAO,gBAAgB,KAAK;AAE5B,WAAO,SAAS,MAAM,KAAK,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,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S,KAAK;AACvB,6BAAS,SAAQ,YAAjB,4BAA2B,SAAS;AAAA,MACtC;AAEA,aAAO;AAAA,IACT;AAAA,IACA,CAAC,MAAM,SAAS,WAAW,eAAe,mBAAmB,QAAQ;AAAA,EACvE;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["_a","_b"]}
1
+ {"version":3,"sources":["../../../src/hooks/useAgentChat.ts"],"sourcesContent":["import type {\n AgentChatPagination,\n AgentChatPlanLimitError,\n AgentConversationPublicationMeta,\n AgentConversationRunSnapshot,\n AgentConversationRuntime,\n AgentConversationSnapshot,\n AgentConversationStatus,\n AgentEventEnvelope,\n AgentHashFields,\n AgentMessage,\n AgentPendingAction,\n AgentToolApprovalDecision,\n LoadConversationResult,\n NovuError,\n RespondToActionResult,\n SendActionResult,\n SendMessageResult,\n} from '@novu/js';\nimport { type MutableRefObject, useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';\nimport { useDataRef } from './internal/useDataRef';\nimport { useNovu } from './NovuProvider';\n\ntype UseAgentChatCallbacks = {\n onSuccess?: (data: LoadConversationResult) => void;\n onError?: (error: NovuError | AgentChatPlanLimitError) => void;\n /**\n * Fires once per message, when the message id first appears on the conversation.\n * History pages are silent: only new activity fires.\n * An agent message can still be empty at this point, because the first envelope of a\n * turn creates the message before any text is folded into it.\n * A send that never reaches the server does not fire: the message flips to `failed` instead.\n */\n onMessage?: (message: AgentMessage) => void;\n /**\n * Fires once per pending action, including actions still pending on mount, so a\n * resumed conversation reports what it is blocked on. Paging backwards is silent.\n */\n onActionRequested?: (action: AgentPendingAction) => void;\n /**\n * Raw envelopes for this conversation, before the derived callbacks for the same fold.\n * A duplicate envelope that the store drops does not fire. Neither does an envelope that\n * arrives before a newly created conversation claims its id.\n * The store folds the envelope before this callback runs, so `messages` here is one render old.\n */\n onEvent?: (envelope: AgentEventEnvelope) => void;\n};\n\nexport type UseAgentChatProps = UseAgentChatCallbacks &\n AgentHashFields &\n (\n | {\n agentId: string;\n /**\n * Resume this conversation. The hook loads history on mount.\n * Omit this prop to start a new chat. The first send creates a conversation.\n * Later sends pass the returned id. Remount or clear this prop to start another chat.\n */\n conversationId?: string;\n conversation?: never;\n }\n | {\n /** Share an existing conversation runtime across multiple hook instances. */\n conversation: AgentConversationRuntime;\n agentId?: never;\n conversationId?: never;\n agentHash?: never;\n }\n );\n\nexport type UseAgentChatResult = {\n messages: AgentMessage[];\n pendingActions: AgentPendingAction[];\n conversationId?: string;\n error?: NovuError | AgentChatPlanLimitError;\n /** True until the first history fetch completes. False when there is no `conversationId` prop. */\n isLoading: boolean;\n isRunning: boolean;\n typing?: AgentConversationRunSnapshot['typing'];\n /** Conversation lifecycle status (`active`, etc.). */\n status: AgentConversationStatus;\n /** Explicit alias for `status`. */\n conversationStatus: AgentConversationStatus;\n /** Current agent run snapshot. */\n run: AgentConversationRunSnapshot;\n pagination: AgentChatPagination & {\n fetchMore: () => Promise<{\n data?: { messages: AgentMessage[]; hasMore: boolean };\n error?: NovuError;\n }>;\n };\n /** True while reconnect catch-up is in flight for this conversation. */\n isRecovering: boolean;\n /** Set when reconnect catch-up fails. Separate from send/fetch `error`. */\n catchUpError?: NovuError;\n refetch: () => Promise<void>;\n sendMessage: (text: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n respondToAction: (args: { actionId: string; decision: AgentToolApprovalDecision }) => Promise<{\n data?: RespondToActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n sendAction: (args: { actionId: string; sourceMessageId: string; value?: string }) => Promise<{\n data?: SendActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n retryMessage: (messageId: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n};\n\nconst EMPTY_SERVER_SNAPSHOT: AgentConversationSnapshot = {\n key: 'ssr',\n status: 'ready',\n run: { isRunning: false },\n conversationStatus: 'active',\n pagination: { hasMore: false, status: 'idle' },\n messages: [],\n pendingActions: [],\n isRecovering: false,\n};\n\ntype RuntimeActionResult<T> = {\n data?: T;\n error?: NovuError | AgentChatPlanLimitError;\n};\n\nfunction handlePublicationCallbacks(args: {\n snapshot: AgentConversationSnapshot;\n meta: AgentConversationPublicationMeta | undefined;\n conversationIdProp: string | undefined;\n propsRef: ReturnType<typeof useDataRef<UseAgentChatProps>>;\n loadNotifiedRef: MutableRefObject<boolean>;\n notifiedCatchUpErrorRef: MutableRefObject<NovuError | undefined>;\n lastReportedErrorKeyRef: MutableRefObject<string | undefined>;\n}): void {\n const {\n snapshot,\n meta,\n conversationIdProp,\n propsRef,\n loadNotifiedRef,\n notifiedCatchUpErrorRef,\n lastReportedErrorKeyRef,\n } = args;\n\n if ((meta?.historyLoaded || meta?.change?.kind === 'history') && !loadNotifiedRef.current && conversationIdProp) {\n if (snapshot.conversationId) {\n loadNotifiedRef.current = true;\n propsRef.current.onSuccess?.({\n conversationId: snapshot.conversationId,\n messages: [...snapshot.messages],\n hasMore: snapshot.pagination.hasMore,\n });\n }\n }\n\n if (snapshot.catchUpError && snapshot.catchUpError !== notifiedCatchUpErrorRef.current) {\n notifiedCatchUpErrorRef.current = snapshot.catchUpError;\n propsRef.current.onError?.(snapshot.catchUpError);\n } else if (!snapshot.catchUpError) {\n notifiedCatchUpErrorRef.current = undefined;\n }\n\n if (snapshot.error) {\n const originalMessage = 'originalError' in snapshot.error ? (snapshot.error.originalError?.message ?? '') : '';\n const errorKey = `${snapshot.error.message}:${originalMessage}`;\n if (lastReportedErrorKeyRef.current !== errorKey) {\n lastReportedErrorKeyRef.current = errorKey;\n propsRef.current.onError?.(snapshot.error as NovuError | AgentChatPlanLimitError);\n }\n } else {\n lastReportedErrorKeyRef.current = undefined;\n }\n\n const change = meta?.change;\n if (change?.kind === 'live') {\n propsRef.current.onEvent?.(change.envelope);\n }\n\n if (change && change.kind !== 'history') {\n for (const message of change.addedMessages) {\n propsRef.current.onMessage?.(message);\n }\n }\n\n if (change) {\n for (const action of change.newActions) {\n propsRef.current.onActionRequested?.(action);\n }\n }\n}\n\ntype OwnedRuntimeEntry = {\n key: string;\n runtime: AgentConversationRuntime;\n};\n\nfunction getCreateFlowKey(agentId: string, agentHash?: string): string {\n return `${agentId}\\0${agentHash ?? ''}`;\n}\n\nfunction resolveOwnedRuntime(args: {\n novu: ReturnType<typeof useNovu>;\n agentId: string;\n agentHash?: string;\n ownedRuntimeRef: MutableRefObject<OwnedRuntimeEntry | null>;\n}): AgentConversationRuntime | null {\n const key = getCreateFlowKey(args.agentId, args.agentHash);\n const current = args.ownedRuntimeRef.current;\n\n if (current?.key === key) {\n return current.runtime;\n }\n\n current?.runtime.dispose();\n\n const result = args.novu.agentChat.conversation({ agentId: args.agentId, agentHash: args.agentHash });\n if (!result.ok) {\n args.ownedRuntimeRef.current = null;\n return null;\n }\n\n args.ownedRuntimeRef.current = { key, runtime: result.data };\n return result.data;\n}\n\nexport const useAgentChat = (props: UseAgentChatProps): UseAgentChatResult => {\n const novu = useNovu();\n const propsRef = useDataRef(props);\n\n const sharedRuntime = 'conversation' in props ? props.conversation : undefined;\n const agentId = sharedRuntime?.agentId ?? props.agentId!;\n const conversationIdProp = sharedRuntime ? undefined : props.conversationId;\n const agentHash = sharedRuntime ? undefined : props.agentHash;\n\n const ownedRuntimeRef = useRef<OwnedRuntimeEntry | null>(null);\n\n const cachedRuntime = useMemo(() => {\n if (sharedRuntime) {\n return sharedRuntime;\n }\n\n if (!conversationIdProp) {\n return null;\n }\n\n const result = novu.agentChat.conversation({\n agentId,\n conversationId: conversationIdProp,\n agentHash,\n });\n\n return result.ok ? result.data : null;\n }, [sharedRuntime, novu, agentId, conversationIdProp, agentHash]);\n\n if (sharedRuntime || conversationIdProp) {\n ownedRuntimeRef.current?.runtime.dispose();\n ownedRuntimeRef.current = null;\n }\n\n const ownedRuntime =\n sharedRuntime || conversationIdProp ? null : resolveOwnedRuntime({ novu, agentId, agentHash, ownedRuntimeRef });\n\n const runtime = sharedRuntime ?? cachedRuntime ?? ownedRuntime;\n\n useEffect(() => {\n return () => {\n ownedRuntimeRef.current?.runtime.dispose();\n ownedRuntimeRef.current = null;\n };\n }, []);\n\n const loadNotifiedRef = useRef(false);\n const replayedActionsRef = useRef(false);\n const notifiedCatchUpErrorRef = useRef<NovuError | undefined>();\n const lastReportedErrorKeyRef = useRef<string>();\n\n useEffect(() => {\n loadNotifiedRef.current = false;\n replayedActionsRef.current = false;\n notifiedCatchUpErrorRef.current = undefined;\n lastReportedErrorKeyRef.current = undefined;\n }, [runtime]);\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (!runtime) {\n return () => {};\n }\n\n if (!replayedActionsRef.current) {\n replayedActionsRef.current = true;\n for (const action of runtime.getSnapshot().pendingActions) {\n propsRef.current.onActionRequested?.(action);\n }\n }\n\n return runtime.subscribe((snapshot, meta) => {\n onStoreChange();\n handlePublicationCallbacks({\n snapshot,\n meta,\n conversationIdProp,\n propsRef,\n loadNotifiedRef,\n notifiedCatchUpErrorRef,\n lastReportedErrorKeyRef,\n });\n });\n },\n [runtime, conversationIdProp, propsRef]\n );\n\n const getSnapshot = useCallback(() => {\n return runtime?.getSnapshot() ?? EMPTY_SERVER_SNAPSHOT;\n }, [runtime]);\n\n const getServerSnapshot = useCallback(() => {\n return runtime?.getServerSnapshot() ?? EMPTY_SERVER_SNAPSHOT;\n }, [runtime]);\n\n const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n\n const callRuntime = useCallback(\n async <T>(action: (target: AgentConversationRuntime) => Promise<RuntimeActionResult<T>>) => {\n if (!runtime) {\n return { error: undefined };\n }\n\n const response = await action(runtime);\n if (response.error) {\n propsRef.current.onError?.(response.error);\n }\n\n return response;\n },\n [runtime, propsRef]\n );\n\n const refetch = useCallback(async () => {\n if (!runtime) {\n return;\n }\n\n const response = await runtime.load();\n if (response.data) {\n propsRef.current.onSuccess?.({\n conversationId: response.data.conversationId,\n messages: [...response.data.messages],\n hasMore: response.data.hasMore,\n });\n }\n }, [runtime, propsRef]);\n\n const fetchMore = useCallback(async () => {\n const response = await callRuntime((target) => target.fetchMore());\n\n return {\n ...response,\n error: response.error as NovuError | undefined,\n data: response.data\n ? {\n messages: [...response.data.messages],\n hasMore: response.data.hasMore,\n }\n : undefined,\n };\n }, [callRuntime]);\n\n const paginationWithFetch = useMemo(\n () => ({\n status: snapshot.pagination.status,\n hasMore: snapshot.pagination.hasMore,\n fetchMore,\n }),\n [snapshot.pagination.status, snapshot.pagination.hasMore, fetchMore]\n );\n\n const sendMessage = useCallback((text: string) => callRuntime((target) => target.sendMessage(text)), [callRuntime]);\n const respondToAction = useCallback(\n (args: { actionId: string; decision: AgentToolApprovalDecision }) =>\n callRuntime((target) => target.respondToAction(args)),\n [callRuntime]\n );\n const sendAction = useCallback(\n (args: { actionId: string; sourceMessageId: string; value?: string }) =>\n callRuntime((target) => target.sendAction(args)),\n [callRuntime]\n );\n const retryMessage = useCallback(\n (messageId: string) => callRuntime((target) => target.retryMessage(messageId)),\n [callRuntime]\n );\n\n return {\n messages: [...snapshot.messages],\n pendingActions: [...snapshot.pendingActions],\n conversationId: snapshot.conversationId,\n error: snapshot.error as UseAgentChatResult['error'],\n isLoading: snapshot.status === 'loading',\n isRunning: snapshot.run.isRunning,\n typing: snapshot.run.typing,\n status: snapshot.conversationStatus,\n conversationStatus: snapshot.conversationStatus,\n run: snapshot.run,\n pagination: paginationWithFetch,\n isRecovering: snapshot.isRecovering,\n catchUpError: snapshot.catchUpError,\n refetch,\n sendMessage,\n respondToAction,\n sendAction,\n retryMessage,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,mBAAqG;AACrG,wBAA2B;AAC3B,0BAAwB;AA6Fx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