@novu/react 3.19.0 → 3.19.1-rc.68f5589d4e
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/cjs/hooks/useAgentChat.cjs +147 -11
- package/dist/cjs/hooks/useAgentChat.cjs.map +1 -1
- package/dist/cjs/hooks/useAgentChat.d.cts +53 -5
- package/dist/cjs/server/index.cjs +9 -1
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/esm/hooks/useAgentChat.d.ts +53 -5
- package/dist/esm/hooks/useAgentChat.js +143 -12
- package/dist/esm/hooks/useAgentChat.js.map +1 -1
- package/dist/esm/server/index.js +9 -1
- package/dist/esm/server/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -23,6 +23,7 @@ __export(useAgentChat_exports, {
|
|
|
23
23
|
useAgentChat: () => useAgentChat
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(useAgentChat_exports);
|
|
26
|
+
var import_js = require("@novu/js");
|
|
26
27
|
var import_react = require("react");
|
|
27
28
|
var import_useDataRef = require("./internal/useDataRef.cjs");
|
|
28
29
|
var import_NovuProvider = require("./NovuProvider.cjs");
|
|
@@ -37,8 +38,22 @@ function createLocalSessionKey() {
|
|
|
37
38
|
}
|
|
38
39
|
return `local_${Date.now().toString(36)}`;
|
|
39
40
|
}
|
|
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);
|
|
54
|
+
}
|
|
40
55
|
var useAgentChat = (props) => {
|
|
41
|
-
const { agentId, conversationId: conversationIdProp } = props;
|
|
56
|
+
const { agentId, agentHash, conversationId: conversationIdProp } = props;
|
|
42
57
|
const propsRef = (0, import_useDataRef.useDataRef)(props);
|
|
43
58
|
const novu = (0, import_NovuProvider.useNovu)();
|
|
44
59
|
const [localSessionKey, setLocalSessionKey] = (0, import_react.useState)(createLocalSessionKey);
|
|
@@ -50,10 +65,25 @@ var useAgentChat = (props) => {
|
|
|
50
65
|
const conversationId = conversationIdProp ?? assignedConversationId;
|
|
51
66
|
const conversationIdRef = (0, import_useDataRef.useDataRef)(conversationId);
|
|
52
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);
|
|
53
72
|
const [error, setError] = (0, import_react.useState)();
|
|
54
73
|
const [isLoading, setIsLoading] = (0, import_react.useState)(Boolean(conversationIdProp));
|
|
55
74
|
const [isFetching, setIsFetching] = (0, import_react.useState)(false);
|
|
56
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
|
+
);
|
|
57
87
|
(0, import_react.useEffect)(() => {
|
|
58
88
|
const agentChanged = prevAgentIdRef.current !== agentId;
|
|
59
89
|
const prevConversationIdProp = prevConversationIdPropRef.current;
|
|
@@ -62,7 +92,7 @@ var useAgentChat = (props) => {
|
|
|
62
92
|
if (agentChanged) {
|
|
63
93
|
setAssignedConversationId(void 0);
|
|
64
94
|
setLocalSessionKey(createLocalSessionKey());
|
|
65
|
-
|
|
95
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
66
96
|
setError(void 0);
|
|
67
97
|
setIsLoading(Boolean(conversationIdProp));
|
|
68
98
|
return;
|
|
@@ -75,9 +105,9 @@ var useAgentChat = (props) => {
|
|
|
75
105
|
if (prevConversationIdProp !== void 0) {
|
|
76
106
|
setAssignedConversationId(void 0);
|
|
77
107
|
setLocalSessionKey(createLocalSessionKey());
|
|
78
|
-
|
|
108
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
79
109
|
}
|
|
80
|
-
}, [agentId, conversationIdProp]);
|
|
110
|
+
}, [agentId, conversationIdProp, snapshotSetters]);
|
|
81
111
|
const fetchConversation = (0, import_react.useCallback)(
|
|
82
112
|
async (targetConversationId) => {
|
|
83
113
|
var _a, _b, _c, _d;
|
|
@@ -97,6 +127,7 @@ var useAgentChat = (props) => {
|
|
|
97
127
|
(_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
|
|
98
128
|
} else if (response.data) {
|
|
99
129
|
setMessages(response.data.messages);
|
|
130
|
+
setHasMore(response.data.hasMore);
|
|
100
131
|
(_d = (_c = propsRef.current).onSuccess) == null ? void 0 : _d.call(_c, response.data);
|
|
101
132
|
}
|
|
102
133
|
setIsLoading(false);
|
|
@@ -105,33 +136,72 @@ var useAgentChat = (props) => {
|
|
|
105
136
|
[novu, agentId, propsRef]
|
|
106
137
|
);
|
|
107
138
|
(0, import_react.useEffect)(() => {
|
|
139
|
+
var _a, _b;
|
|
140
|
+
novu.agentChat.subscribe();
|
|
108
141
|
const snapshot = novu.agentChat.getConversation({
|
|
109
142
|
agentId,
|
|
110
143
|
key: sessionKey,
|
|
111
144
|
conversationId: conversationIdProp
|
|
112
145
|
});
|
|
113
146
|
if (snapshot) {
|
|
114
|
-
|
|
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
|
+
);
|
|
115
157
|
if (snapshot.conversationId && !conversationIdProp) {
|
|
116
158
|
setAssignedConversationId(snapshot.conversationId);
|
|
117
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
|
+
}
|
|
118
163
|
} else if (!conversationIdProp) {
|
|
119
|
-
|
|
164
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
120
165
|
}
|
|
121
166
|
const cleanup = novu.on("agent_chat.messages.updated", ({ data }) => {
|
|
167
|
+
var _a2, _b2, _c, _d, _e, _f;
|
|
122
168
|
if (data.key !== sessionKeyRef.current) {
|
|
123
169
|
return;
|
|
124
170
|
}
|
|
125
|
-
|
|
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
|
+
);
|
|
126
181
|
if (data.conversationId && !propsRef.current.conversationId) {
|
|
127
182
|
setAssignedConversationId(data.conversationId);
|
|
128
183
|
}
|
|
184
|
+
const { change } = data;
|
|
185
|
+
if (change.kind === "live") {
|
|
186
|
+
(_b2 = (_a2 = propsRef.current).onEvent) == null ? void 0 : _b2.call(_a2, change.envelope);
|
|
187
|
+
}
|
|
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);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const action of change.newActions) {
|
|
194
|
+
(_f = (_e = propsRef.current).onActionRequested) == null ? void 0 : _f.call(_e, action);
|
|
195
|
+
}
|
|
129
196
|
});
|
|
130
197
|
if (conversationIdProp) {
|
|
131
198
|
void fetchConversation(conversationIdProp);
|
|
132
199
|
}
|
|
133
|
-
return
|
|
134
|
-
|
|
200
|
+
return () => {
|
|
201
|
+
cleanup();
|
|
202
|
+
novu.agentChat.unsubscribe();
|
|
203
|
+
};
|
|
204
|
+
}, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, propsRef, fetchConversation, snapshotSetters]);
|
|
135
205
|
const refetch = (0, import_react.useCallback)(async () => {
|
|
136
206
|
const id = conversationIdRef.current;
|
|
137
207
|
if (!id) {
|
|
@@ -139,12 +209,29 @@ var useAgentChat = (props) => {
|
|
|
139
209
|
}
|
|
140
210
|
await fetchConversation(id);
|
|
141
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]);
|
|
142
228
|
const sendMessage = (0, import_react.useCallback)(
|
|
143
229
|
async (text) => {
|
|
144
230
|
var _a, _b;
|
|
145
231
|
setError(void 0);
|
|
146
232
|
const response = await novu.agentChat.sendMessage({
|
|
147
233
|
agentId,
|
|
234
|
+
agentHash,
|
|
148
235
|
text,
|
|
149
236
|
key: sessionKeyRef.current,
|
|
150
237
|
conversationId: conversationIdRef.current
|
|
@@ -157,16 +244,65 @@ var useAgentChat = (props) => {
|
|
|
157
244
|
}
|
|
158
245
|
return response;
|
|
159
246
|
},
|
|
160
|
-
[novu, agentId, sessionKeyRef, conversationIdRef, propsRef]
|
|
247
|
+
[novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
|
|
248
|
+
);
|
|
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
|
+
});
|
|
261
|
+
if (response.error) {
|
|
262
|
+
setError(response.error);
|
|
263
|
+
(_b = (_a = propsRef.current).onError) == null ? void 0 : _b.call(_a, response.error);
|
|
264
|
+
}
|
|
265
|
+
return response;
|
|
266
|
+
},
|
|
267
|
+
[novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
|
|
268
|
+
);
|
|
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
|
|
281
|
+
});
|
|
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]
|
|
161
289
|
);
|
|
162
290
|
return {
|
|
163
291
|
messages,
|
|
292
|
+
pendingActions,
|
|
164
293
|
sendMessage,
|
|
294
|
+
respondToAction,
|
|
295
|
+
sendAction,
|
|
165
296
|
conversationId,
|
|
166
297
|
error,
|
|
167
298
|
isLoading,
|
|
168
299
|
isFetching,
|
|
169
|
-
|
|
300
|
+
isRunning,
|
|
301
|
+
typing,
|
|
302
|
+
status,
|
|
303
|
+
hasMore,
|
|
304
|
+
refetch,
|
|
305
|
+
fetchMore
|
|
170
306
|
};
|
|
171
307
|
};
|
|
172
308
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useAgentChat.ts"],"sourcesContent":["import type { AgentMessage, LoadConversationResult, NovuError, SendMessageResult } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useDataRef } from './internal/useDataRef';\nimport { useNovu } from './NovuProvider';\n\nexport type UseAgentChatProps = {\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) => void;\n};\n\nexport type UseAgentChatResult = {\n messages: AgentMessage[];\n conversationId?: string;\n error?: NovuError;\n /** True until the first history fetch completes. False when there is no `conversationId` prop. */\n isLoading: boolean;\n isFetching: boolean;\n refetch: () => Promise<void>;\n sendMessage: (text: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError;\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\nexport const useAgentChat = (props: UseAgentChatProps): UseAgentChatResult => {\n const { agentId, 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 [error, setError] = useState<NovuError>();\n const [isLoading, setIsLoading] = useState(Boolean(conversationIdProp));\n const [isFetching, setIsFetching] = useState(false);\n const fetchGenerationRef = useRef(0);\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 setMessages([]);\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 setMessages([]);\n }\n }, [agentId, conversationIdProp]);\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 propsRef.current.onSuccess?.(response.data);\n }\n\n setIsLoading(false);\n setIsFetching(false);\n },\n [novu, agentId, propsRef]\n );\n\n useEffect(() => {\n const snapshot = novu.agentChat.getConversation({\n agentId,\n key: sessionKey,\n conversationId: conversationIdProp,\n });\n if (snapshot) {\n setMessages(snapshot.messages);\n if (snapshot.conversationId && !conversationIdProp) {\n setAssignedConversationId(snapshot.conversationId);\n }\n } else if (!conversationIdProp) {\n setMessages([]);\n }\n\n const cleanup = novu.on('agent_chat.messages.updated', ({ data }) => {\n if (data.key !== sessionKeyRef.current) {\n return;\n }\n\n setMessages(data.messages);\n if (data.conversationId && !propsRef.current.conversationId) {\n setAssignedConversationId(data.conversationId);\n }\n });\n\n if (conversationIdProp) {\n void fetchConversation(conversationIdProp);\n }\n\n return cleanup;\n }, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, conversationIdRef, propsRef, fetchConversation]);\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 sendMessage = useCallback(\n async (text: string) => {\n setError(undefined);\n\n const response = await novu.agentChat.sendMessage({\n agentId,\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, sessionKeyRef, conversationIdRef, propsRef]\n );\n\n return {\n messages,\n sendMessage,\n conversationId,\n error,\n isLoading,\n isFetching,\n refetch,\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AAS,KAAK,cAAc;AAAA,MACxD;AAEA,aAAO;AAAA,IACT;AAAA,IACA,CAAC,MAAM,SAAS,eAAe,mBAAmB,QAAQ;AAAA,EAC5D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
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,KAAK,EAAE,CAAC;AAAA,EAC1F;AAEA,SAAO,SAAS,KAAK,IAAI,EAAE,SAAS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O;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,6 +1,6 @@
|
|
|
1
|
-
import { LoadConversationResult, NovuError, AgentMessage, SendMessageResult } from '@novu/js';
|
|
1
|
+
import { AgentHashFields, LoadConversationResult, NovuError, AgentChatPlanLimitError, AgentMessage, AgentPendingAction, AgentEventEnvelope, AgentConversationTyping, AgentConversationStatus, SendMessageResult, AgentToolApprovalDecision, RespondToActionResult, SendActionResult } from '@novu/js';
|
|
2
2
|
|
|
3
|
-
type UseAgentChatProps = {
|
|
3
|
+
type UseAgentChatProps = AgentHashFields & {
|
|
4
4
|
agentId: string;
|
|
5
5
|
/**
|
|
6
6
|
* Resume this conversation. The hook loads history on mount.
|
|
@@ -9,19 +9,67 @@ type UseAgentChatProps = {
|
|
|
9
9
|
*/
|
|
10
10
|
conversationId?: string;
|
|
11
11
|
onSuccess?: (data: LoadConversationResult) => void;
|
|
12
|
-
onError?: (error: NovuError) => void;
|
|
12
|
+
onError?: (error: NovuError | AgentChatPlanLimitError) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Fires once per message, when the message id first appears on the conversation.
|
|
15
|
+
* History pages are silent: only new activity fires.
|
|
16
|
+
* An agent message can still be empty at this point, because the first envelope of a
|
|
17
|
+
* turn creates the message before any text is folded into it.
|
|
18
|
+
* A send that never reaches the server does not fire: the message flips to `failed` instead.
|
|
19
|
+
*/
|
|
20
|
+
onMessage?: (message: AgentMessage) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Fires once per pending action, including actions still pending on mount, so a
|
|
23
|
+
* resumed conversation reports what it is blocked on. Paging backwards is silent.
|
|
24
|
+
*/
|
|
25
|
+
onActionRequested?: (action: AgentPendingAction) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Raw envelopes for this conversation, before the derived callbacks for the same fold.
|
|
28
|
+
* A duplicate envelope that the store drops does not fire. Neither does an envelope that
|
|
29
|
+
* arrives before a newly created conversation claims its id.
|
|
30
|
+
* The store folds the envelope before this callback runs, so `messages` here is one render old.
|
|
31
|
+
*/
|
|
32
|
+
onEvent?: (envelope: AgentEventEnvelope) => void;
|
|
13
33
|
};
|
|
14
34
|
type UseAgentChatResult = {
|
|
15
35
|
messages: AgentMessage[];
|
|
36
|
+
pendingActions: AgentPendingAction[];
|
|
16
37
|
conversationId?: string;
|
|
17
|
-
error?: NovuError;
|
|
38
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
18
39
|
/** True until the first history fetch completes. False when there is no `conversationId` prop. */
|
|
19
40
|
isLoading: boolean;
|
|
20
41
|
isFetching: boolean;
|
|
42
|
+
isRunning: boolean;
|
|
43
|
+
typing?: AgentConversationTyping;
|
|
44
|
+
status: AgentConversationStatus;
|
|
45
|
+
/** True when older history pages are available via `fetchMore`. */
|
|
46
|
+
hasMore: boolean;
|
|
21
47
|
refetch: () => Promise<void>;
|
|
48
|
+
fetchMore: () => Promise<{
|
|
49
|
+
data?: {
|
|
50
|
+
messages: AgentMessage[];
|
|
51
|
+
hasMore: boolean;
|
|
52
|
+
};
|
|
53
|
+
error?: NovuError;
|
|
54
|
+
}>;
|
|
22
55
|
sendMessage: (text: string) => Promise<{
|
|
23
56
|
data?: SendMessageResult;
|
|
24
|
-
error?: NovuError;
|
|
57
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
58
|
+
}>;
|
|
59
|
+
respondToAction: (args: {
|
|
60
|
+
actionId: string;
|
|
61
|
+
decision: AgentToolApprovalDecision;
|
|
62
|
+
}) => Promise<{
|
|
63
|
+
data?: RespondToActionResult;
|
|
64
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
65
|
+
}>;
|
|
66
|
+
sendAction: (args: {
|
|
67
|
+
actionId: string;
|
|
68
|
+
sourceMessageId: string;
|
|
69
|
+
value?: string;
|
|
70
|
+
}) => Promise<{
|
|
71
|
+
data?: SendActionResult;
|
|
72
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
25
73
|
}>;
|
|
26
74
|
};
|
|
27
75
|
declare const useAgentChat: (props: UseAgentChatProps) => UseAgentChatResult;
|
|
@@ -95,10 +95,18 @@ function useNovu() {
|
|
|
95
95
|
function useAgentChat(_) {
|
|
96
96
|
return {
|
|
97
97
|
messages: [],
|
|
98
|
+
pendingActions: [],
|
|
98
99
|
isLoading: false,
|
|
99
100
|
isFetching: false,
|
|
101
|
+
isRunning: false,
|
|
102
|
+
typing: void 0,
|
|
103
|
+
status: "active",
|
|
104
|
+
hasMore: false,
|
|
100
105
|
refetch: () => Promise.resolve(),
|
|
101
|
-
|
|
106
|
+
fetchMore: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
107
|
+
sendMessage: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
108
|
+
respondToAction: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
109
|
+
sendAction: () => Promise.resolve({ data: void 0, error: void 0 })
|
|
102
110
|
};
|
|
103
111
|
}
|
|
104
112
|
function useCounts(_) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.tsx"],"sourcesContent":["import type { InboxProps } from '../components/Inbox';\nimport { ShadowRootDetector } from '../components/ShadowRootDetector';\nimport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCreateSubscriptionProps,\n UseCreateSubscriptionResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesProps,\n UsePreferencesResult,\n UseRemoveSubscriptionProps,\n UseRemoveSubscriptionResult,\n UseScheduleProps,\n UseScheduleResult,\n UseSubscriptionProps,\n UseSubscriptionResult,\n UseSubscriptionsProps,\n UseSubscriptionsResult,\n UseUpdateSubscriptionProps,\n UseUpdateSubscriptionResult,\n} from '../hooks';\nimport type { NovuProviderProps } from '../hooks/NovuProvider';\nimport type { UseCountsProps, UseCountsResult } from '../hooks/useCounts';\n\n/**\n * Exporting all components from the components folder\n * as empty functions to fix build errors in SSR\n * This will be replaced with actual components\n * when we implement the SSR components in @novu/js/ui\n */\nexport function Inbox(props: InboxProps) {\n return <ShadowRootDetector />;\n}\n\nexport function InboxContent() {}\n\nexport function Notifications() {}\n\nexport function Preferences() {}\n\nexport function Bell() {}\n\nexport function NovuProvider(props: NovuProviderProps) {\n return <>{props.children}</>;\n}\n\nexport function Subscription() {\n return <ShadowRootDetector />;\n}\n\nexport function SubscriptionButton() {}\n\nexport function SubscriptionPreferences() {}\n\nexport function SlackConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function SlackLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function TelegramConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function useNovu() {\n return null;\n}\n\nexport function useAgentChat(_: UseAgentChatProps): UseAgentChatResult {\n return {\n messages: [],\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n sendMessage: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useCounts(_: UseCountsProps): UseCountsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useNotifications(_: UseNotificationsProps): UseNotificationsResult {\n return {\n isLoading: false,\n isFetching: false,\n hasMore: false,\n readAll: () => Promise.resolve({ data: undefined, error: undefined }),\n seenAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAllRead: () => Promise.resolve({ data: undefined, error: undefined }),\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve(),\n };\n}\n\nexport function usePreferences(_: UsePreferencesProps): UsePreferencesResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSchedule(_: UseScheduleProps): UseScheduleResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSubscription(_: UseSubscriptionProps): UseSubscriptionResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useCreateSubscription(_: UseCreateSubscriptionProps = {}): UseCreateSubscriptionResult {\n return {\n isCreating: false,\n error: undefined,\n create: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useUpdateSubscription(_: UseUpdateSubscriptionProps = {}): UseUpdateSubscriptionResult {\n return {\n isUpdating: false,\n error: undefined,\n update: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useRemoveSubscription(_: UseRemoveSubscriptionProps = {}): UseRemoveSubscriptionResult {\n return {\n isRemoving: false,\n error: undefined,\n remove: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useSubscriptions(_: UseSubscriptionsProps): UseSubscriptionsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport type * from '@novu/js';\nexport { PreferenceLevel, SeverityLevelEnum, WorkflowCriticalityEnum } from '@novu/js';\n\nexport type {\n AllLocalization,\n AllLocalizationKey,\n ElementStyles,\n InboxAppearance,\n InboxAppearanceCallback,\n InboxAppearanceCallbackFunction,\n InboxAppearanceCallbackKeys,\n InboxAppearanceKey,\n InboxElements,\n InboxLocalization,\n InboxLocalizationKey,\n InboxTheme,\n NotificationActionClickHandler,\n NotificationClickHandler,\n NotificationRenderer,\n PreferenceGroups,\n PreferencesFilter,\n RouterPush,\n SubscriptionAppearance,\n SubscriptionAppearanceCallback,\n SubscriptionAppearanceCallbackFunction,\n SubscriptionAppearanceCallbackKeys,\n SubscriptionAppearanceKey,\n SubscriptionElements,\n SubscriptionLocalization,\n SubscriptionLocalizationKey,\n SubscriptionTheme,\n Tab,\n Variables,\n} from '@novu/js/ui';\n\nexport type { BellProps, InboxContentProps, InboxProps, NotificationProps, NovuProviderProps } from '../components';\n\nexport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCountsProps,\n UseCountsResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesResult,\n UseScheduleProps as UsePreferencesProps,\n} from '../hooks';\n\nexport type {\n BaseProps,\n BellRenderer,\n BodyRenderer,\n DefaultInboxProps,\n DefaultProps,\n NoRendererProps,\n NotificationRendererProps,\n NotificationsRenderer,\n SubjectBodyRendererProps,\n SubjectRenderer,\n WithChildrenProps,\n} from '../utils/types';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,gCAAmC;
|
|
1
|
+
{"version":3,"sources":["../../../src/server/index.tsx"],"sourcesContent":["import type { InboxProps } from '../components/Inbox';\nimport { ShadowRootDetector } from '../components/ShadowRootDetector';\nimport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCreateSubscriptionProps,\n UseCreateSubscriptionResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesProps,\n UsePreferencesResult,\n UseRemoveSubscriptionProps,\n UseRemoveSubscriptionResult,\n UseScheduleProps,\n UseScheduleResult,\n UseSubscriptionProps,\n UseSubscriptionResult,\n UseSubscriptionsProps,\n UseSubscriptionsResult,\n UseUpdateSubscriptionProps,\n UseUpdateSubscriptionResult,\n} from '../hooks';\nimport type { NovuProviderProps } from '../hooks/NovuProvider';\nimport type { UseCountsProps, UseCountsResult } from '../hooks/useCounts';\n\n/**\n * Exporting all components from the components folder\n * as empty functions to fix build errors in SSR\n * This will be replaced with actual components\n * when we implement the SSR components in @novu/js/ui\n */\nexport function Inbox(props: InboxProps) {\n return <ShadowRootDetector />;\n}\n\nexport function InboxContent() {}\n\nexport function Notifications() {}\n\nexport function Preferences() {}\n\nexport function Bell() {}\n\nexport function NovuProvider(props: NovuProviderProps) {\n return <>{props.children}</>;\n}\n\nexport function Subscription() {\n return <ShadowRootDetector />;\n}\n\nexport function SubscriptionButton() {}\n\nexport function SubscriptionPreferences() {}\n\nexport function SlackConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function SlackLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function TelegramConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function useNovu() {\n return null;\n}\n\nexport function useAgentChat(_: UseAgentChatProps): UseAgentChatResult {\n return {\n messages: [],\n pendingActions: [],\n isLoading: false,\n isFetching: false,\n isRunning: false,\n typing: undefined,\n status: 'active',\n hasMore: false,\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve({ data: undefined, error: undefined }),\n sendMessage: () => Promise.resolve({ data: undefined, error: undefined }),\n respondToAction: () => Promise.resolve({ data: undefined, error: undefined }),\n sendAction: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useCounts(_: UseCountsProps): UseCountsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useNotifications(_: UseNotificationsProps): UseNotificationsResult {\n return {\n isLoading: false,\n isFetching: false,\n hasMore: false,\n readAll: () => Promise.resolve({ data: undefined, error: undefined }),\n seenAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAllRead: () => Promise.resolve({ data: undefined, error: undefined }),\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve(),\n };\n}\n\nexport function usePreferences(_: UsePreferencesProps): UsePreferencesResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSchedule(_: UseScheduleProps): UseScheduleResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSubscription(_: UseSubscriptionProps): UseSubscriptionResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useCreateSubscription(_: UseCreateSubscriptionProps = {}): UseCreateSubscriptionResult {\n return {\n isCreating: false,\n error: undefined,\n create: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useUpdateSubscription(_: UseUpdateSubscriptionProps = {}): UseUpdateSubscriptionResult {\n return {\n isUpdating: false,\n error: undefined,\n update: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useRemoveSubscription(_: UseRemoveSubscriptionProps = {}): UseRemoveSubscriptionResult {\n return {\n isRemoving: false,\n error: undefined,\n remove: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useSubscriptions(_: UseSubscriptionsProps): UseSubscriptionsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport type * from '@novu/js';\nexport { PreferenceLevel, SeverityLevelEnum, WorkflowCriticalityEnum } from '@novu/js';\n\nexport type {\n AllLocalization,\n AllLocalizationKey,\n ElementStyles,\n InboxAppearance,\n InboxAppearanceCallback,\n InboxAppearanceCallbackFunction,\n InboxAppearanceCallbackKeys,\n InboxAppearanceKey,\n InboxElements,\n InboxLocalization,\n InboxLocalizationKey,\n InboxTheme,\n NotificationActionClickHandler,\n NotificationClickHandler,\n NotificationRenderer,\n PreferenceGroups,\n PreferencesFilter,\n RouterPush,\n SubscriptionAppearance,\n SubscriptionAppearanceCallback,\n SubscriptionAppearanceCallbackFunction,\n SubscriptionAppearanceCallbackKeys,\n SubscriptionAppearanceKey,\n SubscriptionElements,\n SubscriptionLocalization,\n SubscriptionLocalizationKey,\n SubscriptionTheme,\n Tab,\n Variables,\n} from '@novu/js/ui';\n\nexport type { BellProps, InboxContentProps, InboxProps, NotificationProps, NovuProviderProps } from '../components';\n\nexport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCountsProps,\n UseCountsResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesResult,\n UseScheduleProps as UsePreferencesProps,\n} from '../hooks';\n\nexport type {\n BaseProps,\n BellRenderer,\n BodyRenderer,\n DefaultInboxProps,\n DefaultProps,\n NoRendererProps,\n NotificationRendererProps,\n NotificationsRenderer,\n SubjectBodyRendererProps,\n SubjectRenderer,\n WithChildrenProps,\n} from '../utils/types';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,gCAAmC;AA+KnC,gBAA4E;AAhJnE;AADF,SAAS,MAAM,OAAmB;AACvC,SAAO,4CAAC,gDAAmB;AAC7B;AAEO,SAAS,eAAe;AAAC;AAEzB,SAAS,gBAAgB;AAAC;AAE1B,SAAS,cAAc;AAAC;AAExB,SAAS,OAAO;AAAC;AAEjB,SAAS,aAAa,OAA0B;AACrD,SAAO,2EAAG,gBAAM,UAAS;AAC3B;AAEO,SAAS,eAAe;AAC7B,SAAO,4CAAC,gDAAmB;AAC7B;AAEO,SAAS,qBAAqB;AAAC;AAE/B,SAAS,0BAA0B;AAAC;AAEpC,SAAS,qBAAqB;AACnC,SAAO,4CAAC,gDAAmB;AAC7B;AAEO,SAAS,gBAAgB;AAC9B,SAAO,4CAAC,gDAAmB;AAC7B;AAEO,SAAS,uBAAuB;AACrC,SAAO,4CAAC,gDAAmB;AAC7B;AAEO,SAAS,kBAAkB;AAChC,SAAO,4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WAAW,MAAM,QAAQ,QAAQ;AAAA,EACnC;AACF;AAEO,SAAS,eAAe,GAA8C;AAC3E,SAAO;AAAA,IACL,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,SAAS,MAAM,QAAQ,QAAQ;AAAA,EACjC;AACF;AAEO,SAAS,YAAY,GAAwC;AAClE,SAAO;AAAA,IACL,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,SAAS,MAAM,QAAQ,QAAQ;AAAA,EACjC;AACF;AAEO,SAAS,gBAAgB,GAAgD;AAC9E,SAAO;AAAA,IACL,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,SAAS,MAAM,QAAQ,QAAQ;AAAA,EACjC;AACF;AAEO,SAAS,sBAAsB,IAAgC,CAAC,GAAgC;AACrG,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,QAAQ,MAAM,QAAQ,QAAQ,EAAE,MAAM,QAAW,OAAO,OAAU,CAAC;AAAA,EACrE;AACF;AAEO,SAAS,sBAAsB,IAAgC,CAAC,GAAgC;AACrG,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,QAAQ,MAAM,QAAQ,QAAQ,EAAE,MAAM,QAAW,OAAO,OAAU,CAAC;AAAA,EACrE;AACF;AAEO,SAAS,sBAAsB,IAAgC,CAAC,GAAgC;AACrG,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,QAAQ,MAAM,QAAQ,QAAQ,EAAE,MAAM,QAAW,OAAO,OAAU,CAAC;AAAA,EACrE;AACF;AAEO,SAAS,iBAAiB,GAAkD;AACjF,SAAO;AAAA,IACL,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,SAAS,MAAM,QAAQ,QAAQ;AAAA,EACjC;AACF;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { LoadConversationResult, NovuError, AgentMessage, SendMessageResult } from '@novu/js';
|
|
1
|
+
import { AgentHashFields, LoadConversationResult, NovuError, AgentChatPlanLimitError, AgentMessage, AgentPendingAction, AgentEventEnvelope, AgentConversationTyping, AgentConversationStatus, SendMessageResult, AgentToolApprovalDecision, RespondToActionResult, SendActionResult } from '@novu/js';
|
|
2
2
|
|
|
3
|
-
type UseAgentChatProps = {
|
|
3
|
+
type UseAgentChatProps = AgentHashFields & {
|
|
4
4
|
agentId: string;
|
|
5
5
|
/**
|
|
6
6
|
* Resume this conversation. The hook loads history on mount.
|
|
@@ -9,19 +9,67 @@ type UseAgentChatProps = {
|
|
|
9
9
|
*/
|
|
10
10
|
conversationId?: string;
|
|
11
11
|
onSuccess?: (data: LoadConversationResult) => void;
|
|
12
|
-
onError?: (error: NovuError) => void;
|
|
12
|
+
onError?: (error: NovuError | AgentChatPlanLimitError) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Fires once per message, when the message id first appears on the conversation.
|
|
15
|
+
* History pages are silent: only new activity fires.
|
|
16
|
+
* An agent message can still be empty at this point, because the first envelope of a
|
|
17
|
+
* turn creates the message before any text is folded into it.
|
|
18
|
+
* A send that never reaches the server does not fire: the message flips to `failed` instead.
|
|
19
|
+
*/
|
|
20
|
+
onMessage?: (message: AgentMessage) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Fires once per pending action, including actions still pending on mount, so a
|
|
23
|
+
* resumed conversation reports what it is blocked on. Paging backwards is silent.
|
|
24
|
+
*/
|
|
25
|
+
onActionRequested?: (action: AgentPendingAction) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Raw envelopes for this conversation, before the derived callbacks for the same fold.
|
|
28
|
+
* A duplicate envelope that the store drops does not fire. Neither does an envelope that
|
|
29
|
+
* arrives before a newly created conversation claims its id.
|
|
30
|
+
* The store folds the envelope before this callback runs, so `messages` here is one render old.
|
|
31
|
+
*/
|
|
32
|
+
onEvent?: (envelope: AgentEventEnvelope) => void;
|
|
13
33
|
};
|
|
14
34
|
type UseAgentChatResult = {
|
|
15
35
|
messages: AgentMessage[];
|
|
36
|
+
pendingActions: AgentPendingAction[];
|
|
16
37
|
conversationId?: string;
|
|
17
|
-
error?: NovuError;
|
|
38
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
18
39
|
/** True until the first history fetch completes. False when there is no `conversationId` prop. */
|
|
19
40
|
isLoading: boolean;
|
|
20
41
|
isFetching: boolean;
|
|
42
|
+
isRunning: boolean;
|
|
43
|
+
typing?: AgentConversationTyping;
|
|
44
|
+
status: AgentConversationStatus;
|
|
45
|
+
/** True when older history pages are available via `fetchMore`. */
|
|
46
|
+
hasMore: boolean;
|
|
21
47
|
refetch: () => Promise<void>;
|
|
48
|
+
fetchMore: () => Promise<{
|
|
49
|
+
data?: {
|
|
50
|
+
messages: AgentMessage[];
|
|
51
|
+
hasMore: boolean;
|
|
52
|
+
};
|
|
53
|
+
error?: NovuError;
|
|
54
|
+
}>;
|
|
22
55
|
sendMessage: (text: string) => Promise<{
|
|
23
56
|
data?: SendMessageResult;
|
|
24
|
-
error?: NovuError;
|
|
57
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
58
|
+
}>;
|
|
59
|
+
respondToAction: (args: {
|
|
60
|
+
actionId: string;
|
|
61
|
+
decision: AgentToolApprovalDecision;
|
|
62
|
+
}) => Promise<{
|
|
63
|
+
data?: RespondToActionResult;
|
|
64
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
65
|
+
}>;
|
|
66
|
+
sendAction: (args: {
|
|
67
|
+
actionId: string;
|
|
68
|
+
sourceMessageId: string;
|
|
69
|
+
value?: string;
|
|
70
|
+
}) => Promise<{
|
|
71
|
+
data?: SendActionResult;
|
|
72
|
+
error?: NovuError | AgentChatPlanLimitError;
|
|
25
73
|
}>;
|
|
26
74
|
};
|
|
27
75
|
declare const useAgentChat: (props: UseAgentChatProps) => UseAgentChatResult;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/hooks/useAgentChat.ts
|
|
2
|
-
import {
|
|
2
|
+
import { derivePendingActions } from "@novu/js";
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
4
|
import { useDataRef } from "./internal/useDataRef.js";
|
|
4
5
|
import { useNovu } from "./NovuProvider.js";
|
|
5
6
|
function createLocalSessionKey() {
|
|
@@ -13,8 +14,22 @@ function createLocalSessionKey() {
|
|
|
13
14
|
}
|
|
14
15
|
return `local_${Date.now().toString(36)}`;
|
|
15
16
|
}
|
|
17
|
+
var EMPTY_CONVERSATION = {
|
|
18
|
+
messages: [],
|
|
19
|
+
isRunning: false,
|
|
20
|
+
typing: void 0,
|
|
21
|
+
status: "active",
|
|
22
|
+
hasMore: false
|
|
23
|
+
};
|
|
24
|
+
function applyConversationSnapshot(snapshot, setters) {
|
|
25
|
+
setters.setMessages(snapshot.messages);
|
|
26
|
+
setters.setIsRunning(snapshot.isRunning);
|
|
27
|
+
setters.setTyping(snapshot.typing);
|
|
28
|
+
setters.setStatus(snapshot.status);
|
|
29
|
+
setters.setHasMore(snapshot.hasMore);
|
|
30
|
+
}
|
|
16
31
|
var useAgentChat = (props) => {
|
|
17
|
-
const { agentId, conversationId: conversationIdProp } = props;
|
|
32
|
+
const { agentId, agentHash, conversationId: conversationIdProp } = props;
|
|
18
33
|
const propsRef = useDataRef(props);
|
|
19
34
|
const novu = useNovu();
|
|
20
35
|
const [localSessionKey, setLocalSessionKey] = useState(createLocalSessionKey);
|
|
@@ -26,10 +41,25 @@ var useAgentChat = (props) => {
|
|
|
26
41
|
const conversationId = conversationIdProp ?? assignedConversationId;
|
|
27
42
|
const conversationIdRef = useDataRef(conversationId);
|
|
28
43
|
const [messages, setMessages] = useState([]);
|
|
44
|
+
const [isRunning, setIsRunning] = useState(false);
|
|
45
|
+
const [typing, setTyping] = useState();
|
|
46
|
+
const [status, setStatus] = useState("active");
|
|
47
|
+
const [hasMore, setHasMore] = useState(false);
|
|
29
48
|
const [error, setError] = useState();
|
|
30
49
|
const [isLoading, setIsLoading] = useState(Boolean(conversationIdProp));
|
|
31
50
|
const [isFetching, setIsFetching] = useState(false);
|
|
32
51
|
const fetchGenerationRef = useRef(0);
|
|
52
|
+
const pendingActions = useMemo(() => derivePendingActions(messages), [messages]);
|
|
53
|
+
const snapshotSetters = useMemo(
|
|
54
|
+
() => ({
|
|
55
|
+
setMessages,
|
|
56
|
+
setIsRunning,
|
|
57
|
+
setTyping,
|
|
58
|
+
setStatus,
|
|
59
|
+
setHasMore
|
|
60
|
+
}),
|
|
61
|
+
[]
|
|
62
|
+
);
|
|
33
63
|
useEffect(() => {
|
|
34
64
|
const agentChanged = prevAgentIdRef.current !== agentId;
|
|
35
65
|
const prevConversationIdProp = prevConversationIdPropRef.current;
|
|
@@ -38,7 +68,7 @@ var useAgentChat = (props) => {
|
|
|
38
68
|
if (agentChanged) {
|
|
39
69
|
setAssignedConversationId(void 0);
|
|
40
70
|
setLocalSessionKey(createLocalSessionKey());
|
|
41
|
-
|
|
71
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
42
72
|
setError(void 0);
|
|
43
73
|
setIsLoading(Boolean(conversationIdProp));
|
|
44
74
|
return;
|
|
@@ -51,9 +81,9 @@ var useAgentChat = (props) => {
|
|
|
51
81
|
if (prevConversationIdProp !== void 0) {
|
|
52
82
|
setAssignedConversationId(void 0);
|
|
53
83
|
setLocalSessionKey(createLocalSessionKey());
|
|
54
|
-
|
|
84
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
55
85
|
}
|
|
56
|
-
}, [agentId, conversationIdProp]);
|
|
86
|
+
}, [agentId, conversationIdProp, snapshotSetters]);
|
|
57
87
|
const fetchConversation = useCallback(
|
|
58
88
|
async (targetConversationId) => {
|
|
59
89
|
const generation = ++fetchGenerationRef.current;
|
|
@@ -72,6 +102,7 @@ var useAgentChat = (props) => {
|
|
|
72
102
|
propsRef.current.onError?.(response.error);
|
|
73
103
|
} else if (response.data) {
|
|
74
104
|
setMessages(response.data.messages);
|
|
105
|
+
setHasMore(response.data.hasMore);
|
|
75
106
|
propsRef.current.onSuccess?.(response.data);
|
|
76
107
|
}
|
|
77
108
|
setIsLoading(false);
|
|
@@ -80,33 +111,70 @@ var useAgentChat = (props) => {
|
|
|
80
111
|
[novu, agentId, propsRef]
|
|
81
112
|
);
|
|
82
113
|
useEffect(() => {
|
|
114
|
+
novu.agentChat.subscribe();
|
|
83
115
|
const snapshot = novu.agentChat.getConversation({
|
|
84
116
|
agentId,
|
|
85
117
|
key: sessionKey,
|
|
86
118
|
conversationId: conversationIdProp
|
|
87
119
|
});
|
|
88
120
|
if (snapshot) {
|
|
89
|
-
|
|
121
|
+
applyConversationSnapshot(
|
|
122
|
+
{
|
|
123
|
+
messages: snapshot.messages,
|
|
124
|
+
isRunning: snapshot.isRunning,
|
|
125
|
+
typing: snapshot.typing,
|
|
126
|
+
status: snapshot.status,
|
|
127
|
+
hasMore: snapshot.hasMore
|
|
128
|
+
},
|
|
129
|
+
snapshotSetters
|
|
130
|
+
);
|
|
90
131
|
if (snapshot.conversationId && !conversationIdProp) {
|
|
91
132
|
setAssignedConversationId(snapshot.conversationId);
|
|
92
133
|
}
|
|
134
|
+
for (const action of derivePendingActions(snapshot.messages)) {
|
|
135
|
+
propsRef.current.onActionRequested?.(action);
|
|
136
|
+
}
|
|
93
137
|
} else if (!conversationIdProp) {
|
|
94
|
-
|
|
138
|
+
applyConversationSnapshot(EMPTY_CONVERSATION, snapshotSetters);
|
|
95
139
|
}
|
|
96
140
|
const cleanup = novu.on("agent_chat.messages.updated", ({ data }) => {
|
|
97
141
|
if (data.key !== sessionKeyRef.current) {
|
|
98
142
|
return;
|
|
99
143
|
}
|
|
100
|
-
|
|
144
|
+
applyConversationSnapshot(
|
|
145
|
+
{
|
|
146
|
+
messages: data.messages,
|
|
147
|
+
isRunning: data.isRunning,
|
|
148
|
+
typing: data.typing,
|
|
149
|
+
status: data.status,
|
|
150
|
+
hasMore: data.hasMore
|
|
151
|
+
},
|
|
152
|
+
snapshotSetters
|
|
153
|
+
);
|
|
101
154
|
if (data.conversationId && !propsRef.current.conversationId) {
|
|
102
155
|
setAssignedConversationId(data.conversationId);
|
|
103
156
|
}
|
|
157
|
+
const { change } = data;
|
|
158
|
+
if (change.kind === "live") {
|
|
159
|
+
propsRef.current.onEvent?.(change.envelope);
|
|
160
|
+
}
|
|
161
|
+
if (change.kind !== "history") {
|
|
162
|
+
for (const message of change.addedMessages) {
|
|
163
|
+
propsRef.current.onMessage?.(message);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (const action of change.newActions) {
|
|
167
|
+
propsRef.current.onActionRequested?.(action);
|
|
168
|
+
}
|
|
104
169
|
});
|
|
105
170
|
if (conversationIdProp) {
|
|
106
171
|
void fetchConversation(conversationIdProp);
|
|
107
172
|
}
|
|
108
|
-
return
|
|
109
|
-
|
|
173
|
+
return () => {
|
|
174
|
+
cleanup();
|
|
175
|
+
novu.agentChat.unsubscribe();
|
|
176
|
+
};
|
|
177
|
+
}, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, propsRef, fetchConversation, snapshotSetters]);
|
|
110
178
|
const refetch = useCallback(async () => {
|
|
111
179
|
const id = conversationIdRef.current;
|
|
112
180
|
if (!id) {
|
|
@@ -114,11 +182,27 @@ var useAgentChat = (props) => {
|
|
|
114
182
|
}
|
|
115
183
|
await fetchConversation(id);
|
|
116
184
|
}, [conversationIdRef, fetchConversation]);
|
|
185
|
+
const fetchMore = useCallback(async () => {
|
|
186
|
+
const response = await novu.agentChat.fetchMore({
|
|
187
|
+
agentId,
|
|
188
|
+
key: sessionKeyRef.current,
|
|
189
|
+
conversationId: conversationIdRef.current
|
|
190
|
+
});
|
|
191
|
+
if (response.error) {
|
|
192
|
+
setError(response.error);
|
|
193
|
+
propsRef.current.onError?.(response.error);
|
|
194
|
+
} else if (response.data) {
|
|
195
|
+
setMessages(response.data.messages);
|
|
196
|
+
setHasMore(response.data.hasMore);
|
|
197
|
+
}
|
|
198
|
+
return response;
|
|
199
|
+
}, [novu, agentId, sessionKeyRef, conversationIdRef, propsRef]);
|
|
117
200
|
const sendMessage = useCallback(
|
|
118
201
|
async (text) => {
|
|
119
202
|
setError(void 0);
|
|
120
203
|
const response = await novu.agentChat.sendMessage({
|
|
121
204
|
agentId,
|
|
205
|
+
agentHash,
|
|
122
206
|
text,
|
|
123
207
|
key: sessionKeyRef.current,
|
|
124
208
|
conversationId: conversationIdRef.current
|
|
@@ -131,16 +215,63 @@ var useAgentChat = (props) => {
|
|
|
131
215
|
}
|
|
132
216
|
return response;
|
|
133
217
|
},
|
|
134
|
-
[novu, agentId, sessionKeyRef, conversationIdRef, propsRef]
|
|
218
|
+
[novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
|
|
219
|
+
);
|
|
220
|
+
const respondToAction = useCallback(
|
|
221
|
+
async (args) => {
|
|
222
|
+
setError(void 0);
|
|
223
|
+
const response = await novu.agentChat.respondToAction({
|
|
224
|
+
agentId,
|
|
225
|
+
agentHash,
|
|
226
|
+
key: sessionKeyRef.current,
|
|
227
|
+
conversationId: conversationIdRef.current,
|
|
228
|
+
actionId: args.actionId,
|
|
229
|
+
decision: args.decision
|
|
230
|
+
});
|
|
231
|
+
if (response.error) {
|
|
232
|
+
setError(response.error);
|
|
233
|
+
propsRef.current.onError?.(response.error);
|
|
234
|
+
}
|
|
235
|
+
return response;
|
|
236
|
+
},
|
|
237
|
+
[novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
|
|
238
|
+
);
|
|
239
|
+
const sendAction = useCallback(
|
|
240
|
+
async (args) => {
|
|
241
|
+
setError(void 0);
|
|
242
|
+
const response = await novu.agentChat.sendAction({
|
|
243
|
+
agentId,
|
|
244
|
+
agentHash,
|
|
245
|
+
key: sessionKeyRef.current,
|
|
246
|
+
conversationId: conversationIdRef.current,
|
|
247
|
+
actionId: args.actionId,
|
|
248
|
+
sourceMessageId: args.sourceMessageId,
|
|
249
|
+
value: args.value
|
|
250
|
+
});
|
|
251
|
+
if (response.error) {
|
|
252
|
+
setError(response.error);
|
|
253
|
+
propsRef.current.onError?.(response.error);
|
|
254
|
+
}
|
|
255
|
+
return response;
|
|
256
|
+
},
|
|
257
|
+
[novu, agentId, agentHash, sessionKeyRef, conversationIdRef, propsRef]
|
|
135
258
|
);
|
|
136
259
|
return {
|
|
137
260
|
messages,
|
|
261
|
+
pendingActions,
|
|
138
262
|
sendMessage,
|
|
263
|
+
respondToAction,
|
|
264
|
+
sendAction,
|
|
139
265
|
conversationId,
|
|
140
266
|
error,
|
|
141
267
|
isLoading,
|
|
142
268
|
isFetching,
|
|
143
|
-
|
|
269
|
+
isRunning,
|
|
270
|
+
typing,
|
|
271
|
+
status,
|
|
272
|
+
hasMore,
|
|
273
|
+
refetch,
|
|
274
|
+
fetchMore
|
|
144
275
|
};
|
|
145
276
|
};
|
|
146
277
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useAgentChat.ts"],"sourcesContent":["import type { AgentMessage, LoadConversationResult, NovuError, SendMessageResult } from '@novu/js';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { useDataRef } from './internal/useDataRef';\nimport { useNovu } from './NovuProvider';\n\nexport type UseAgentChatProps = {\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) => void;\n};\n\nexport type UseAgentChatResult = {\n messages: AgentMessage[];\n conversationId?: string;\n error?: NovuError;\n /** True until the first history fetch completes. False when there is no `conversationId` prop. */\n isLoading: boolean;\n isFetching: boolean;\n refetch: () => Promise<void>;\n sendMessage: (text: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError;\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\nexport const useAgentChat = (props: UseAgentChatProps): UseAgentChatResult => {\n const { agentId, 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 [error, setError] = useState<NovuError>();\n const [isLoading, setIsLoading] = useState(Boolean(conversationIdProp));\n const [isFetching, setIsFetching] = useState(false);\n const fetchGenerationRef = useRef(0);\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 setMessages([]);\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 setMessages([]);\n }\n }, [agentId, conversationIdProp]);\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 propsRef.current.onSuccess?.(response.data);\n }\n\n setIsLoading(false);\n setIsFetching(false);\n },\n [novu, agentId, propsRef]\n );\n\n useEffect(() => {\n const snapshot = novu.agentChat.getConversation({\n agentId,\n key: sessionKey,\n conversationId: conversationIdProp,\n });\n if (snapshot) {\n setMessages(snapshot.messages);\n if (snapshot.conversationId && !conversationIdProp) {\n setAssignedConversationId(snapshot.conversationId);\n }\n } else if (!conversationIdProp) {\n setMessages([]);\n }\n\n const cleanup = novu.on('agent_chat.messages.updated', ({ data }) => {\n if (data.key !== sessionKeyRef.current) {\n return;\n }\n\n setMessages(data.messages);\n if (data.conversationId && !propsRef.current.conversationId) {\n setAssignedConversationId(data.conversationId);\n }\n });\n\n if (conversationIdProp) {\n void fetchConversation(conversationIdProp);\n }\n\n return cleanup;\n }, [novu, agentId, conversationIdProp, sessionKey, sessionKeyRef, conversationIdRef, propsRef, fetchConversation]);\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 sendMessage = useCallback(\n async (text: string) => {\n setError(undefined);\n\n const response = await novu.agentChat.sendMessage({\n agentId,\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, sessionKeyRef, conversationIdRef, propsRef]\n );\n\n return {\n messages,\n sendMessage,\n conversationId,\n error,\n isLoading,\n isFetching,\n refetch,\n };\n};\n"],"mappings":";AACA,SAAS,aAAa,WAAW,QAAQ,gBAA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AEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
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":";AAeA,SAAS,4BAA4B;AACrC,SAAS,aAAa,WAAW,SAAS,QAAQ,gBAAgB;AAClE,SAAS,kBAAkB;AAC3B,SAAS,eAAe;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,KAAK,EAAE,CAAC;AAAA,EAC1F;AAEA,SAAO,SAAS,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC;AACzC;AAUA,IAAM,qBAA2C;AAAA,EAC/C,UAAU,CAAC;AAAA,EACX,WAAW;AAAA,EACX,QAAQ;AAAA,EACR,QAAQ;AAAA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ED
|
@@ -44,10 +44,18 @@ function useNovu() {
|
|
|
44
44
|
function useAgentChat(_) {
|
|
45
45
|
return {
|
|
46
46
|
messages: [],
|
|
47
|
+
pendingActions: [],
|
|
47
48
|
isLoading: false,
|
|
48
49
|
isFetching: false,
|
|
50
|
+
isRunning: false,
|
|
51
|
+
typing: void 0,
|
|
52
|
+
status: "active",
|
|
53
|
+
hasMore: false,
|
|
49
54
|
refetch: () => Promise.resolve(),
|
|
50
|
-
|
|
55
|
+
fetchMore: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
56
|
+
sendMessage: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
57
|
+
respondToAction: () => Promise.resolve({ data: void 0, error: void 0 }),
|
|
58
|
+
sendAction: () => Promise.resolve({ data: void 0, error: void 0 })
|
|
51
59
|
};
|
|
52
60
|
}
|
|
53
61
|
function useCounts(_) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.tsx"],"sourcesContent":["import type { InboxProps } from '../components/Inbox';\nimport { ShadowRootDetector } from '../components/ShadowRootDetector';\nimport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCreateSubscriptionProps,\n UseCreateSubscriptionResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesProps,\n UsePreferencesResult,\n UseRemoveSubscriptionProps,\n UseRemoveSubscriptionResult,\n UseScheduleProps,\n UseScheduleResult,\n UseSubscriptionProps,\n UseSubscriptionResult,\n UseSubscriptionsProps,\n UseSubscriptionsResult,\n UseUpdateSubscriptionProps,\n UseUpdateSubscriptionResult,\n} from '../hooks';\nimport type { NovuProviderProps } from '../hooks/NovuProvider';\nimport type { UseCountsProps, UseCountsResult } from '../hooks/useCounts';\n\n/**\n * Exporting all components from the components folder\n * as empty functions to fix build errors in SSR\n * This will be replaced with actual components\n * when we implement the SSR components in @novu/js/ui\n */\nexport function Inbox(props: InboxProps) {\n return <ShadowRootDetector />;\n}\n\nexport function InboxContent() {}\n\nexport function Notifications() {}\n\nexport function Preferences() {}\n\nexport function Bell() {}\n\nexport function NovuProvider(props: NovuProviderProps) {\n return <>{props.children}</>;\n}\n\nexport function Subscription() {\n return <ShadowRootDetector />;\n}\n\nexport function SubscriptionButton() {}\n\nexport function SubscriptionPreferences() {}\n\nexport function SlackConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function SlackLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function TelegramConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function useNovu() {\n return null;\n}\n\nexport function useAgentChat(_: UseAgentChatProps): UseAgentChatResult {\n return {\n messages: [],\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n sendMessage: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useCounts(_: UseCountsProps): UseCountsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useNotifications(_: UseNotificationsProps): UseNotificationsResult {\n return {\n isLoading: false,\n isFetching: false,\n hasMore: false,\n readAll: () => Promise.resolve({ data: undefined, error: undefined }),\n seenAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAllRead: () => Promise.resolve({ data: undefined, error: undefined }),\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve(),\n };\n}\n\nexport function usePreferences(_: UsePreferencesProps): UsePreferencesResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSchedule(_: UseScheduleProps): UseScheduleResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSubscription(_: UseSubscriptionProps): UseSubscriptionResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useCreateSubscription(_: UseCreateSubscriptionProps = {}): UseCreateSubscriptionResult {\n return {\n isCreating: false,\n error: undefined,\n create: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useUpdateSubscription(_: UseUpdateSubscriptionProps = {}): UseUpdateSubscriptionResult {\n return {\n isUpdating: false,\n error: undefined,\n update: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useRemoveSubscription(_: UseRemoveSubscriptionProps = {}): UseRemoveSubscriptionResult {\n return {\n isRemoving: false,\n error: undefined,\n remove: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useSubscriptions(_: UseSubscriptionsProps): UseSubscriptionsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport type * from '@novu/js';\nexport { PreferenceLevel, SeverityLevelEnum, WorkflowCriticalityEnum } from '@novu/js';\n\nexport type {\n AllLocalization,\n AllLocalizationKey,\n ElementStyles,\n InboxAppearance,\n InboxAppearanceCallback,\n InboxAppearanceCallbackFunction,\n InboxAppearanceCallbackKeys,\n InboxAppearanceKey,\n InboxElements,\n InboxLocalization,\n InboxLocalizationKey,\n InboxTheme,\n NotificationActionClickHandler,\n NotificationClickHandler,\n NotificationRenderer,\n PreferenceGroups,\n PreferencesFilter,\n RouterPush,\n SubscriptionAppearance,\n SubscriptionAppearanceCallback,\n SubscriptionAppearanceCallbackFunction,\n SubscriptionAppearanceCallbackKeys,\n SubscriptionAppearanceKey,\n SubscriptionElements,\n SubscriptionLocalization,\n SubscriptionLocalizationKey,\n SubscriptionTheme,\n Tab,\n Variables,\n} from '@novu/js/ui';\n\nexport type { BellProps, InboxContentProps, InboxProps, NotificationProps, NovuProviderProps } from '../components';\n\nexport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCountsProps,\n UseCountsResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesResult,\n UseScheduleProps as UsePreferencesProps,\n} from '../hooks';\n\nexport type {\n BaseProps,\n BellRenderer,\n BodyRenderer,\n DefaultInboxProps,\n DefaultProps,\n NoRendererProps,\n NotificationRendererProps,\n NotificationsRenderer,\n SubjectBodyRendererProps,\n SubjectRenderer,\n WithChildrenProps,\n} from '../utils/types';\n"],"mappings":";AACA,SAAS,0BAA0B;
|
|
1
|
+
{"version":3,"sources":["../../../src/server/index.tsx"],"sourcesContent":["import type { InboxProps } from '../components/Inbox';\nimport { ShadowRootDetector } from '../components/ShadowRootDetector';\nimport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCreateSubscriptionProps,\n UseCreateSubscriptionResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesProps,\n UsePreferencesResult,\n UseRemoveSubscriptionProps,\n UseRemoveSubscriptionResult,\n UseScheduleProps,\n UseScheduleResult,\n UseSubscriptionProps,\n UseSubscriptionResult,\n UseSubscriptionsProps,\n UseSubscriptionsResult,\n UseUpdateSubscriptionProps,\n UseUpdateSubscriptionResult,\n} from '../hooks';\nimport type { NovuProviderProps } from '../hooks/NovuProvider';\nimport type { UseCountsProps, UseCountsResult } from '../hooks/useCounts';\n\n/**\n * Exporting all components from the components folder\n * as empty functions to fix build errors in SSR\n * This will be replaced with actual components\n * when we implement the SSR components in @novu/js/ui\n */\nexport function Inbox(props: InboxProps) {\n return <ShadowRootDetector />;\n}\n\nexport function InboxContent() {}\n\nexport function Notifications() {}\n\nexport function Preferences() {}\n\nexport function Bell() {}\n\nexport function NovuProvider(props: NovuProviderProps) {\n return <>{props.children}</>;\n}\n\nexport function Subscription() {\n return <ShadowRootDetector />;\n}\n\nexport function SubscriptionButton() {}\n\nexport function SubscriptionPreferences() {}\n\nexport function SlackConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function SlackLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function MsTeamsLinkUser() {\n return <ShadowRootDetector />;\n}\n\nexport function TelegramConnectButton() {\n return <ShadowRootDetector />;\n}\n\nexport function useNovu() {\n return null;\n}\n\nexport function useAgentChat(_: UseAgentChatProps): UseAgentChatResult {\n return {\n messages: [],\n pendingActions: [],\n isLoading: false,\n isFetching: false,\n isRunning: false,\n typing: undefined,\n status: 'active',\n hasMore: false,\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve({ data: undefined, error: undefined }),\n sendMessage: () => Promise.resolve({ data: undefined, error: undefined }),\n respondToAction: () => Promise.resolve({ data: undefined, error: undefined }),\n sendAction: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useCounts(_: UseCountsProps): UseCountsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useNotifications(_: UseNotificationsProps): UseNotificationsResult {\n return {\n isLoading: false,\n isFetching: false,\n hasMore: false,\n readAll: () => Promise.resolve({ data: undefined, error: undefined }),\n seenAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAll: () => Promise.resolve({ data: undefined, error: undefined }),\n archiveAllRead: () => Promise.resolve({ data: undefined, error: undefined }),\n refetch: () => Promise.resolve(),\n fetchMore: () => Promise.resolve(),\n };\n}\n\nexport function usePreferences(_: UsePreferencesProps): UsePreferencesResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSchedule(_: UseScheduleProps): UseScheduleResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useSubscription(_: UseSubscriptionProps): UseSubscriptionResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport function useCreateSubscription(_: UseCreateSubscriptionProps = {}): UseCreateSubscriptionResult {\n return {\n isCreating: false,\n error: undefined,\n create: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useUpdateSubscription(_: UseUpdateSubscriptionProps = {}): UseUpdateSubscriptionResult {\n return {\n isUpdating: false,\n error: undefined,\n update: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useRemoveSubscription(_: UseRemoveSubscriptionProps = {}): UseRemoveSubscriptionResult {\n return {\n isRemoving: false,\n error: undefined,\n remove: () => Promise.resolve({ data: undefined, error: undefined }),\n };\n}\n\nexport function useSubscriptions(_: UseSubscriptionsProps): UseSubscriptionsResult {\n return {\n isLoading: false,\n isFetching: false,\n refetch: () => Promise.resolve(),\n };\n}\n\nexport type * from '@novu/js';\nexport { PreferenceLevel, SeverityLevelEnum, WorkflowCriticalityEnum } from '@novu/js';\n\nexport type {\n AllLocalization,\n AllLocalizationKey,\n ElementStyles,\n InboxAppearance,\n InboxAppearanceCallback,\n InboxAppearanceCallbackFunction,\n InboxAppearanceCallbackKeys,\n InboxAppearanceKey,\n InboxElements,\n InboxLocalization,\n InboxLocalizationKey,\n InboxTheme,\n NotificationActionClickHandler,\n NotificationClickHandler,\n NotificationRenderer,\n PreferenceGroups,\n PreferencesFilter,\n RouterPush,\n SubscriptionAppearance,\n SubscriptionAppearanceCallback,\n SubscriptionAppearanceCallbackFunction,\n SubscriptionAppearanceCallbackKeys,\n SubscriptionAppearanceKey,\n SubscriptionElements,\n SubscriptionLocalization,\n SubscriptionLocalizationKey,\n SubscriptionTheme,\n Tab,\n Variables,\n} from '@novu/js/ui';\n\nexport type { BellProps, InboxContentProps, InboxProps, NotificationProps, NovuProviderProps } from '../components';\n\nexport type {\n UseAgentChatProps,\n UseAgentChatResult,\n UseCountsProps,\n UseCountsResult,\n UseNotificationsProps,\n UseNotificationsResult,\n UsePreferencesResult,\n UseScheduleProps as UsePreferencesProps,\n} from '../hooks';\n\nexport type {\n BaseProps,\n BellRenderer,\n BodyRenderer,\n DefaultInboxProps,\n DefaultProps,\n NoRendererProps,\n NotificationRendererProps,\n NotificationsRenderer,\n SubjectBodyRendererProps,\n SubjectRenderer,\n WithChildrenProps,\n} from '../utils/types';\n"],"mappings":";AACA,SAAS,0BAA0B;AA+KnC,SAAS,iBAAiB,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;AAAA,IACL,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,QAAQ,MAAM,QAAQ,QAAQ,EAAE,MAAM,QAAW,OAAO,OAAU,CAAC;AAAA,EACrE;AACF;AAEO,SAAS,sBAAsB,IAAgC,CAAC,GAAgC;AACrG,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,OAAO;AAAA,IACP,QAAQ,MAAM,QAAQ,QAAQ,EAAE,MAAM,QAAW,OAAO,OAAU,CAAC;AAAA,EACrE;AACF;AAEO,SAAS,iBAAiB,GAAkD;AACjF,SAAO;AAAA,IACL,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,SAAS,MAAM,QAAQ,QAAQ;AAAA,EACjC;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novu/react",
|
|
3
|
-
"version": "3.19.
|
|
3
|
+
"version": "3.19.1-rc.68f5589d4e",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/novuhq/novu",
|
|
@@ -121,7 +121,7 @@
|
|
|
121
121
|
}
|
|
122
122
|
},
|
|
123
123
|
"dependencies": {
|
|
124
|
-
"@novu/js": "3.19.
|
|
124
|
+
"@novu/js": "3.19.1-rc.68f5589d4e"
|
|
125
125
|
},
|
|
126
126
|
"nx": {
|
|
127
127
|
"tags": [
|