@novu/react 3.19.1-rc.852194fa77 → 3.19.1
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/index.cjs +2 -2
- package/dist/cjs/hooks/index.cjs.map +1 -1
- package/dist/cjs/hooks/index.d.cts +1 -1
- package/dist/cjs/hooks/{useAgentChat.cjs → useWebChat.cjs} +79 -28
- package/dist/cjs/hooks/useWebChat.cjs.map +1 -0
- package/dist/cjs/hooks/useWebChat.d.cts +117 -0
- package/dist/cjs/index.cjs +4 -4
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/server/index.cjs +6 -7
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/cjs/server/index.d.cts +3 -3
- package/dist/esm/hooks/index.d.ts +1 -1
- package/dist/esm/hooks/index.js +1 -1
- package/dist/esm/hooks/index.js.map +1 -1
- package/dist/esm/hooks/useWebChat.d.ts +117 -0
- package/dist/esm/hooks/{useAgentChat.js → useWebChat.js} +73 -23
- package/dist/esm/hooks/useWebChat.js.map +1 -0
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +3 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server/index.d.ts +3 -3
- package/dist/esm/server/index.js +4 -5
- package/dist/esm/server/index.js.map +1 -1
- package/package.json +4 -3
- package/dist/cjs/hooks/useAgentChat.cjs.map +0 -1
- package/dist/cjs/hooks/useAgentChat.d.cts +0 -98
- package/dist/esm/hooks/useAgentChat.d.ts +0 -98
- package/dist/esm/hooks/useAgentChat.js.map +0 -1
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { LoadConversationResult, NovuError, AgentChatPlanLimitError, AgentMessage, AgentPendingAction, AgentEventEnvelope, AgentHashFields, AgentConversationRuntime, AgentConversationRunSnapshot, AgentConversationStatus, AgentChatPagination, SendMessageResult, AgentToolApprovalDecision, RespondToActionResult, SendActionResult } from '@novu/js';
|
|
2
|
-
|
|
3
|
-
type UseAgentChatCallbacks = {
|
|
4
|
-
onSuccess?: (data: LoadConversationResult) => void;
|
|
5
|
-
onError?: (error: NovuError | AgentChatPlanLimitError) => void;
|
|
6
|
-
/**
|
|
7
|
-
* Fires once per message, when the message id first appears on the conversation.
|
|
8
|
-
* History pages are silent: only new activity fires.
|
|
9
|
-
* An agent message can still be empty at this point, because the first envelope of a
|
|
10
|
-
* turn creates the message before any text is folded into it.
|
|
11
|
-
* A send that never reaches the server does not fire: the message flips to `failed` instead.
|
|
12
|
-
*/
|
|
13
|
-
onMessage?: (message: AgentMessage) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Fires once per pending action, including actions still pending on mount, so a
|
|
16
|
-
* resumed conversation reports what it is blocked on. Paging backwards is silent.
|
|
17
|
-
*/
|
|
18
|
-
onActionRequested?: (action: AgentPendingAction) => void;
|
|
19
|
-
/**
|
|
20
|
-
* Raw envelopes for this conversation, before the derived callbacks for the same fold.
|
|
21
|
-
* A duplicate envelope that the store drops does not fire. Neither does an envelope that
|
|
22
|
-
* arrives before a newly created conversation claims its id.
|
|
23
|
-
* The store folds the envelope before this callback runs, so `messages` here is one render old.
|
|
24
|
-
*/
|
|
25
|
-
onEvent?: (envelope: AgentEventEnvelope) => void;
|
|
26
|
-
};
|
|
27
|
-
type UseAgentChatProps = UseAgentChatCallbacks & AgentHashFields & ({
|
|
28
|
-
agentId: string;
|
|
29
|
-
/**
|
|
30
|
-
* Resume this conversation. The hook loads history on mount.
|
|
31
|
-
* Omit this prop to start a new chat. The first send creates a conversation.
|
|
32
|
-
* Later sends pass the returned id. Remount or clear this prop to start another chat.
|
|
33
|
-
*/
|
|
34
|
-
conversationId?: string;
|
|
35
|
-
conversation?: never;
|
|
36
|
-
} | {
|
|
37
|
-
/** Share an existing conversation runtime across multiple hook instances. */
|
|
38
|
-
conversation: AgentConversationRuntime;
|
|
39
|
-
agentId?: never;
|
|
40
|
-
conversationId?: never;
|
|
41
|
-
agentHash?: never;
|
|
42
|
-
});
|
|
43
|
-
type UseAgentChatResult = {
|
|
44
|
-
messages: AgentMessage[];
|
|
45
|
-
pendingActions: AgentPendingAction[];
|
|
46
|
-
conversationId?: string;
|
|
47
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
48
|
-
/** True until the first history fetch completes. False when there is no `conversationId` prop. */
|
|
49
|
-
isLoading: boolean;
|
|
50
|
-
isRunning: boolean;
|
|
51
|
-
typing?: AgentConversationRunSnapshot['typing'];
|
|
52
|
-
/** Conversation lifecycle status (`active`, etc.). */
|
|
53
|
-
status: AgentConversationStatus;
|
|
54
|
-
/** Explicit alias for `status`. */
|
|
55
|
-
conversationStatus: AgentConversationStatus;
|
|
56
|
-
/** Current agent run snapshot. */
|
|
57
|
-
run: AgentConversationRunSnapshot;
|
|
58
|
-
pagination: AgentChatPagination & {
|
|
59
|
-
fetchMore: () => Promise<{
|
|
60
|
-
data?: {
|
|
61
|
-
messages: AgentMessage[];
|
|
62
|
-
hasMore: boolean;
|
|
63
|
-
};
|
|
64
|
-
error?: NovuError;
|
|
65
|
-
}>;
|
|
66
|
-
};
|
|
67
|
-
/** True while reconnect catch-up is in flight for this conversation. */
|
|
68
|
-
isRecovering: boolean;
|
|
69
|
-
/** Set when reconnect catch-up fails. Separate from send/fetch `error`. */
|
|
70
|
-
catchUpError?: NovuError;
|
|
71
|
-
refetch: () => Promise<void>;
|
|
72
|
-
sendMessage: (text: string) => Promise<{
|
|
73
|
-
data?: SendMessageResult;
|
|
74
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
75
|
-
}>;
|
|
76
|
-
respondToAction: (args: {
|
|
77
|
-
actionId: string;
|
|
78
|
-
decision: AgentToolApprovalDecision;
|
|
79
|
-
}) => Promise<{
|
|
80
|
-
data?: RespondToActionResult;
|
|
81
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
82
|
-
}>;
|
|
83
|
-
sendAction: (args: {
|
|
84
|
-
actionId: string;
|
|
85
|
-
sourceMessageId: string;
|
|
86
|
-
value?: string;
|
|
87
|
-
}) => Promise<{
|
|
88
|
-
data?: SendActionResult;
|
|
89
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
90
|
-
}>;
|
|
91
|
-
retryMessage: (messageId: string) => Promise<{
|
|
92
|
-
data?: SendMessageResult;
|
|
93
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
94
|
-
}>;
|
|
95
|
-
};
|
|
96
|
-
declare const useAgentChat: (props: UseAgentChatProps) => UseAgentChatResult;
|
|
97
|
-
|
|
98
|
-
export { type UseAgentChatProps, type UseAgentChatResult, useAgentChat };
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { LoadConversationResult, NovuError, AgentChatPlanLimitError, AgentMessage, AgentPendingAction, AgentEventEnvelope, AgentHashFields, AgentConversationRuntime, AgentConversationRunSnapshot, AgentConversationStatus, AgentChatPagination, SendMessageResult, AgentToolApprovalDecision, RespondToActionResult, SendActionResult } from '@novu/js';
|
|
2
|
-
|
|
3
|
-
type UseAgentChatCallbacks = {
|
|
4
|
-
onSuccess?: (data: LoadConversationResult) => void;
|
|
5
|
-
onError?: (error: NovuError | AgentChatPlanLimitError) => void;
|
|
6
|
-
/**
|
|
7
|
-
* Fires once per message, when the message id first appears on the conversation.
|
|
8
|
-
* History pages are silent: only new activity fires.
|
|
9
|
-
* An agent message can still be empty at this point, because the first envelope of a
|
|
10
|
-
* turn creates the message before any text is folded into it.
|
|
11
|
-
* A send that never reaches the server does not fire: the message flips to `failed` instead.
|
|
12
|
-
*/
|
|
13
|
-
onMessage?: (message: AgentMessage) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Fires once per pending action, including actions still pending on mount, so a
|
|
16
|
-
* resumed conversation reports what it is blocked on. Paging backwards is silent.
|
|
17
|
-
*/
|
|
18
|
-
onActionRequested?: (action: AgentPendingAction) => void;
|
|
19
|
-
/**
|
|
20
|
-
* Raw envelopes for this conversation, before the derived callbacks for the same fold.
|
|
21
|
-
* A duplicate envelope that the store drops does not fire. Neither does an envelope that
|
|
22
|
-
* arrives before a newly created conversation claims its id.
|
|
23
|
-
* The store folds the envelope before this callback runs, so `messages` here is one render old.
|
|
24
|
-
*/
|
|
25
|
-
onEvent?: (envelope: AgentEventEnvelope) => void;
|
|
26
|
-
};
|
|
27
|
-
type UseAgentChatProps = UseAgentChatCallbacks & AgentHashFields & ({
|
|
28
|
-
agentId: string;
|
|
29
|
-
/**
|
|
30
|
-
* Resume this conversation. The hook loads history on mount.
|
|
31
|
-
* Omit this prop to start a new chat. The first send creates a conversation.
|
|
32
|
-
* Later sends pass the returned id. Remount or clear this prop to start another chat.
|
|
33
|
-
*/
|
|
34
|
-
conversationId?: string;
|
|
35
|
-
conversation?: never;
|
|
36
|
-
} | {
|
|
37
|
-
/** Share an existing conversation runtime across multiple hook instances. */
|
|
38
|
-
conversation: AgentConversationRuntime;
|
|
39
|
-
agentId?: never;
|
|
40
|
-
conversationId?: never;
|
|
41
|
-
agentHash?: never;
|
|
42
|
-
});
|
|
43
|
-
type UseAgentChatResult = {
|
|
44
|
-
messages: AgentMessage[];
|
|
45
|
-
pendingActions: AgentPendingAction[];
|
|
46
|
-
conversationId?: string;
|
|
47
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
48
|
-
/** True until the first history fetch completes. False when there is no `conversationId` prop. */
|
|
49
|
-
isLoading: boolean;
|
|
50
|
-
isRunning: boolean;
|
|
51
|
-
typing?: AgentConversationRunSnapshot['typing'];
|
|
52
|
-
/** Conversation lifecycle status (`active`, etc.). */
|
|
53
|
-
status: AgentConversationStatus;
|
|
54
|
-
/** Explicit alias for `status`. */
|
|
55
|
-
conversationStatus: AgentConversationStatus;
|
|
56
|
-
/** Current agent run snapshot. */
|
|
57
|
-
run: AgentConversationRunSnapshot;
|
|
58
|
-
pagination: AgentChatPagination & {
|
|
59
|
-
fetchMore: () => Promise<{
|
|
60
|
-
data?: {
|
|
61
|
-
messages: AgentMessage[];
|
|
62
|
-
hasMore: boolean;
|
|
63
|
-
};
|
|
64
|
-
error?: NovuError;
|
|
65
|
-
}>;
|
|
66
|
-
};
|
|
67
|
-
/** True while reconnect catch-up is in flight for this conversation. */
|
|
68
|
-
isRecovering: boolean;
|
|
69
|
-
/** Set when reconnect catch-up fails. Separate from send/fetch `error`. */
|
|
70
|
-
catchUpError?: NovuError;
|
|
71
|
-
refetch: () => Promise<void>;
|
|
72
|
-
sendMessage: (text: string) => Promise<{
|
|
73
|
-
data?: SendMessageResult;
|
|
74
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
75
|
-
}>;
|
|
76
|
-
respondToAction: (args: {
|
|
77
|
-
actionId: string;
|
|
78
|
-
decision: AgentToolApprovalDecision;
|
|
79
|
-
}) => Promise<{
|
|
80
|
-
data?: RespondToActionResult;
|
|
81
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
82
|
-
}>;
|
|
83
|
-
sendAction: (args: {
|
|
84
|
-
actionId: string;
|
|
85
|
-
sourceMessageId: string;
|
|
86
|
-
value?: string;
|
|
87
|
-
}) => Promise<{
|
|
88
|
-
data?: SendActionResult;
|
|
89
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
90
|
-
}>;
|
|
91
|
-
retryMessage: (messageId: string) => Promise<{
|
|
92
|
-
data?: SendMessageResult;
|
|
93
|
-
error?: NovuError | AgentChatPlanLimitError;
|
|
94
|
-
}>;
|
|
95
|
-
};
|
|
96
|
-
declare const useAgentChat: (props: UseAgentChatProps) => UseAgentChatResult;
|
|
97
|
-
|
|
98
|
-
export { type UseAgentChatProps, type UseAgentChatResult, useAgentChat };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/hooks/useAgentChat.ts"],"sourcesContent":["import type {\n AgentChatPagination,\n AgentChatPlanLimitError,\n AgentConversationPublicationMeta,\n AgentConversationRunSnapshot,\n AgentConversationRuntime,\n AgentConversationSnapshot,\n AgentConversationStatus,\n AgentEventEnvelope,\n AgentHashFields,\n AgentMessage,\n AgentPendingAction,\n AgentToolApprovalDecision,\n LoadConversationResult,\n NovuError,\n RespondToActionResult,\n SendActionResult,\n SendMessageResult,\n} from '@novu/js';\nimport { type MutableRefObject, useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';\nimport { useDataRef } from './internal/useDataRef';\nimport { useNovu } from './NovuProvider';\n\ntype UseAgentChatCallbacks = {\n onSuccess?: (data: LoadConversationResult) => void;\n onError?: (error: NovuError | AgentChatPlanLimitError) => void;\n /**\n * Fires once per message, when the message id first appears on the conversation.\n * History pages are silent: only new activity fires.\n * An agent message can still be empty at this point, because the first envelope of a\n * turn creates the message before any text is folded into it.\n * A send that never reaches the server does not fire: the message flips to `failed` instead.\n */\n onMessage?: (message: AgentMessage) => void;\n /**\n * Fires once per pending action, including actions still pending on mount, so a\n * resumed conversation reports what it is blocked on. Paging backwards is silent.\n */\n onActionRequested?: (action: AgentPendingAction) => void;\n /**\n * Raw envelopes for this conversation, before the derived callbacks for the same fold.\n * A duplicate envelope that the store drops does not fire. Neither does an envelope that\n * arrives before a newly created conversation claims its id.\n * The store folds the envelope before this callback runs, so `messages` here is one render old.\n */\n onEvent?: (envelope: AgentEventEnvelope) => void;\n};\n\nexport type UseAgentChatProps = UseAgentChatCallbacks &\n AgentHashFields &\n (\n | {\n agentId: string;\n /**\n * Resume this conversation. The hook loads history on mount.\n * Omit this prop to start a new chat. The first send creates a conversation.\n * Later sends pass the returned id. Remount or clear this prop to start another chat.\n */\n conversationId?: string;\n conversation?: never;\n }\n | {\n /** Share an existing conversation runtime across multiple hook instances. */\n conversation: AgentConversationRuntime;\n agentId?: never;\n conversationId?: never;\n agentHash?: never;\n }\n );\n\nexport type UseAgentChatResult = {\n messages: AgentMessage[];\n pendingActions: AgentPendingAction[];\n conversationId?: string;\n error?: NovuError | AgentChatPlanLimitError;\n /** True until the first history fetch completes. False when there is no `conversationId` prop. */\n isLoading: boolean;\n isRunning: boolean;\n typing?: AgentConversationRunSnapshot['typing'];\n /** Conversation lifecycle status (`active`, etc.). */\n status: AgentConversationStatus;\n /** Explicit alias for `status`. */\n conversationStatus: AgentConversationStatus;\n /** Current agent run snapshot. */\n run: AgentConversationRunSnapshot;\n pagination: AgentChatPagination & {\n fetchMore: () => Promise<{\n data?: { messages: AgentMessage[]; hasMore: boolean };\n error?: NovuError;\n }>;\n };\n /** True while reconnect catch-up is in flight for this conversation. */\n isRecovering: boolean;\n /** Set when reconnect catch-up fails. Separate from send/fetch `error`. */\n catchUpError?: NovuError;\n refetch: () => Promise<void>;\n sendMessage: (text: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n respondToAction: (args: { actionId: string; decision: AgentToolApprovalDecision }) => Promise<{\n data?: RespondToActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n sendAction: (args: { actionId: string; sourceMessageId: string; value?: string }) => Promise<{\n data?: SendActionResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n retryMessage: (messageId: string) => Promise<{\n data?: SendMessageResult;\n error?: NovuError | AgentChatPlanLimitError;\n }>;\n};\n\nconst EMPTY_SERVER_SNAPSHOT: AgentConversationSnapshot = {\n key: 'ssr',\n status: 'ready',\n run: { isRunning: false },\n conversationStatus: 'active',\n pagination: { hasMore: false, status: 'idle' },\n messages: [],\n pendingActions: [],\n isRecovering: false,\n};\n\ntype RuntimeActionResult<T> = {\n data?: T;\n error?: NovuError | AgentChatPlanLimitError;\n};\n\nfunction handlePublicationCallbacks(args: {\n snapshot: AgentConversationSnapshot;\n meta: AgentConversationPublicationMeta | undefined;\n conversationIdProp: string | undefined;\n propsRef: ReturnType<typeof useDataRef<UseAgentChatProps>>;\n loadNotifiedRef: MutableRefObject<boolean>;\n notifiedCatchUpErrorRef: MutableRefObject<NovuError | undefined>;\n lastReportedErrorKeyRef: MutableRefObject<string | undefined>;\n}): void {\n const {\n snapshot,\n meta,\n conversationIdProp,\n propsRef,\n loadNotifiedRef,\n notifiedCatchUpErrorRef,\n lastReportedErrorKeyRef,\n } = args;\n\n if ((meta?.historyLoaded || meta?.change?.kind === 'history') && !loadNotifiedRef.current && conversationIdProp) {\n if (snapshot.conversationId) {\n loadNotifiedRef.current = true;\n propsRef.current.onSuccess?.({\n conversationId: snapshot.conversationId,\n messages: [...snapshot.messages],\n hasMore: snapshot.pagination.hasMore,\n });\n }\n }\n\n if (snapshot.catchUpError && snapshot.catchUpError !== notifiedCatchUpErrorRef.current) {\n notifiedCatchUpErrorRef.current = snapshot.catchUpError;\n propsRef.current.onError?.(snapshot.catchUpError);\n } else if (!snapshot.catchUpError) {\n notifiedCatchUpErrorRef.current = undefined;\n }\n\n if (snapshot.error) {\n const originalMessage = 'originalError' in snapshot.error ? (snapshot.error.originalError?.message ?? '') : '';\n const errorKey = `${snapshot.error.message}:${originalMessage}`;\n if (lastReportedErrorKeyRef.current !== errorKey) {\n lastReportedErrorKeyRef.current = errorKey;\n propsRef.current.onError?.(snapshot.error as NovuError | AgentChatPlanLimitError);\n }\n } else {\n lastReportedErrorKeyRef.current = undefined;\n }\n\n const change = meta?.change;\n if (change?.kind === 'live') {\n propsRef.current.onEvent?.(change.envelope);\n }\n\n if (change && change.kind !== 'history') {\n for (const message of change.addedMessages) {\n propsRef.current.onMessage?.(message);\n }\n }\n\n if (change) {\n for (const action of change.newActions) {\n propsRef.current.onActionRequested?.(action);\n }\n }\n}\n\ntype OwnedRuntimeEntry = {\n key: string;\n runtime: AgentConversationRuntime;\n};\n\nfunction getCreateFlowKey(agentId: string, agentHash?: string): string {\n return `${agentId}\\0${agentHash ?? ''}`;\n}\n\nfunction resolveOwnedRuntime(args: {\n novu: ReturnType<typeof useNovu>;\n agentId: string;\n agentHash?: string;\n ownedRuntimeRef: MutableRefObject<OwnedRuntimeEntry | null>;\n}): AgentConversationRuntime | null {\n const key = getCreateFlowKey(args.agentId, args.agentHash);\n const current = args.ownedRuntimeRef.current;\n\n if (current?.key === key) {\n return current.runtime;\n }\n\n current?.runtime.dispose();\n\n const result = args.novu.agentChat.conversation({ agentId: args.agentId, agentHash: args.agentHash });\n if (!result.ok) {\n args.ownedRuntimeRef.current = null;\n return null;\n }\n\n args.ownedRuntimeRef.current = { key, runtime: result.data };\n return result.data;\n}\n\nexport const useAgentChat = (props: UseAgentChatProps): UseAgentChatResult => {\n const novu = useNovu();\n const propsRef = useDataRef(props);\n\n const sharedRuntime = 'conversation' in props ? props.conversation : undefined;\n const agentId = sharedRuntime?.agentId ?? props.agentId!;\n const conversationIdProp = sharedRuntime ? undefined : props.conversationId;\n const agentHash = sharedRuntime ? undefined : props.agentHash;\n\n const ownedRuntimeRef = useRef<OwnedRuntimeEntry | null>(null);\n\n const cachedRuntime = useMemo(() => {\n if (sharedRuntime) {\n return sharedRuntime;\n }\n\n if (!conversationIdProp) {\n return null;\n }\n\n const result = novu.agentChat.conversation({\n agentId,\n conversationId: conversationIdProp,\n agentHash,\n });\n\n return result.ok ? result.data : null;\n }, [sharedRuntime, novu, agentId, conversationIdProp, agentHash]);\n\n if (sharedRuntime || conversationIdProp) {\n ownedRuntimeRef.current?.runtime.dispose();\n ownedRuntimeRef.current = null;\n }\n\n const ownedRuntime =\n sharedRuntime || conversationIdProp ? null : resolveOwnedRuntime({ novu, agentId, agentHash, ownedRuntimeRef });\n\n const runtime = sharedRuntime ?? cachedRuntime ?? ownedRuntime;\n\n useEffect(() => {\n return () => {\n ownedRuntimeRef.current?.runtime.dispose();\n ownedRuntimeRef.current = null;\n };\n }, []);\n\n const loadNotifiedRef = useRef(false);\n const replayedActionsRef = useRef(false);\n const notifiedCatchUpErrorRef = useRef<NovuError | undefined>();\n const lastReportedErrorKeyRef = useRef<string>();\n\n useEffect(() => {\n loadNotifiedRef.current = false;\n replayedActionsRef.current = false;\n notifiedCatchUpErrorRef.current = undefined;\n lastReportedErrorKeyRef.current = undefined;\n }, [runtime]);\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (!runtime) {\n return () => {};\n }\n\n if (!replayedActionsRef.current) {\n replayedActionsRef.current = true;\n for (const action of runtime.getSnapshot().pendingActions) {\n propsRef.current.onActionRequested?.(action);\n }\n }\n\n return runtime.subscribe((snapshot, meta) => {\n onStoreChange();\n handlePublicationCallbacks({\n snapshot,\n meta,\n conversationIdProp,\n propsRef,\n loadNotifiedRef,\n notifiedCatchUpErrorRef,\n lastReportedErrorKeyRef,\n });\n });\n },\n [runtime, conversationIdProp, propsRef]\n );\n\n const getSnapshot = useCallback(() => {\n return runtime?.getSnapshot() ?? EMPTY_SERVER_SNAPSHOT;\n }, [runtime]);\n\n const getServerSnapshot = useCallback(() => {\n return runtime?.getServerSnapshot() ?? EMPTY_SERVER_SNAPSHOT;\n }, [runtime]);\n\n const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n\n const callRuntime = useCallback(\n async <T>(action: (target: AgentConversationRuntime) => Promise<RuntimeActionResult<T>>) => {\n if (!runtime) {\n return { error: undefined };\n }\n\n const response = await action(runtime);\n if (response.error) {\n propsRef.current.onError?.(response.error);\n }\n\n return response;\n },\n [runtime, propsRef]\n );\n\n const refetch = useCallback(async () => {\n if (!runtime) {\n return;\n }\n\n const response = await runtime.load();\n if (response.data) {\n propsRef.current.onSuccess?.({\n conversationId: response.data.conversationId,\n messages: [...response.data.messages],\n hasMore: response.data.hasMore,\n });\n }\n }, [runtime, propsRef]);\n\n const fetchMore = useCallback(async () => {\n const response = await callRuntime((target) => target.fetchMore());\n\n return {\n ...response,\n error: response.error as NovuError | undefined,\n data: response.data\n ? {\n messages: [...response.data.messages],\n hasMore: response.data.hasMore,\n }\n : undefined,\n };\n }, [callRuntime]);\n\n const paginationWithFetch = useMemo(\n () => ({\n status: snapshot.pagination.status,\n hasMore: snapshot.pagination.hasMore,\n fetchMore,\n }),\n [snapshot.pagination.status, snapshot.pagination.hasMore, fetchMore]\n );\n\n const sendMessage = useCallback((text: string) => callRuntime((target) => target.sendMessage(text)), [callRuntime]);\n const respondToAction = useCallback(\n (args: { actionId: string; decision: AgentToolApprovalDecision }) =>\n callRuntime((target) => target.respondToAction(args)),\n [callRuntime]\n );\n const sendAction = useCallback(\n (args: { actionId: string; sourceMessageId: string; value?: string }) =>\n callRuntime((target) => target.sendAction(args)),\n [callRuntime]\n );\n const retryMessage = useCallback(\n (messageId: string) => callRuntime((target) => target.retryMessage(messageId)),\n [callRuntime]\n );\n\n return {\n messages: [...snapshot.messages],\n pendingActions: [...snapshot.pendingActions],\n conversationId: snapshot.conversationId,\n error: snapshot.error as UseAgentChatResult['error'],\n isLoading: snapshot.status === 'loading',\n isRunning: snapshot.run.isRunning,\n typing: snapshot.run.typing,\n status: snapshot.conversationStatus,\n conversationStatus: snapshot.conversationStatus,\n run: snapshot.run,\n pagination: paginationWithFetch,\n isRecovering: snapshot.isRecovering,\n catchUpError: snapshot.catchUpError,\n refetch,\n sendMessage,\n respondToAction,\n sendAction,\n retryMessage,\n };\n};\n"],"mappings":";AAmBA,SAAgC,aAAa,WAAW,SAAS,QAAQ,4BAA4B;AACrG,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AA6FxB,IAAM,wBAAmD;AAAA,EACvD,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,KAAK,EAAE,WAAW,MAAM;AAAA,EACxB,oBAAoB;AAAA,EACpB,YAAY,EAAE,SAAS,OAAO,QAAQ,OAAO;AAAA,EAC7C,UAAU,CAAC;AAAA,EACX,gBAAgB,CAAC;AAAA,EACjB,cAAc;AAChB;AAOA,SAAS,2BAA2B,MAQ3B;AACP,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,OAAK,MAAM,iBAAiB,MAAM,QAAQ,SAAS,cAAc,CAAC,gBAAgB,WAAW,oBAAoB;AAC/G,QAAI,SAAS,gBAAgB;AAC3B,sBAAgB,UAAU;AAC1B,eAAS,QAAQ,YAAY;AAAA,QAC3B,gBAAgB,SAAS;AAAA,QACzB,UAAU,CAAC,GAAG,SAAS,QAAQ;AAAA,QAC/B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