import { LoadConversationResult, NovuError, WebChatPlanLimitError, AgentMessage, AgentPendingAction, AgentEventEnvelope, AgentHashFields, AgentConversationRuntime, AgentConversationRunSnapshot, AgentConversationStatus, WebChatPagination, SendMessageInput, SendMessageResult, AgentToolApprovalDecision, RespondToActionResult, SendActionResult, StopResult } from '@novu/js'; type UseWebChatCallbacks = { onSuccess?: (data: LoadConversationResult) => void; onError?: (error: NovuError | WebChatPlanLimitError) => void; /** * Fires once when a message id first appears. History pages do not fire. * The first event of a turn can create an empty assistant message before text arrives. * A send that never reaches the server does not fire. The message status becomes `failed` instead. */ onMessage?: (message: AgentMessage) => void; /** * Fires once per pending action, including actions still pending on mount. * Paging backwards does not fire. */ onActionRequested?: (action: AgentPendingAction) => void; /** * Live event envelopes for this conversation. * Duplicates that the client drops do not fire. * Envelopes that arrive before the conversation id exists do not fire. * The message list in this render does not include this envelope yet. */ onEvent?: (envelope: AgentEventEnvelope) => void; }; /** Arguments for {@link useWebChat}. Pass `agentId`, or pass `conversation`. Do not mix the two. */ type UseWebChatProps = UseWebChatCallbacks & AgentHashFields & ({ agentId: string; /** * Resume this conversation. The hook loads history on mount. * Omit this prop to start a new chat. The first send creates a conversation. */ conversationId?: string; conversation?: never; } | { /** Share an existing conversation runtime across multiple hook instances. */ conversation: AgentConversationRuntime; agentId?: never; conversationId?: never; agentHash?: never; }); /** * State and actions returned by {@link useWebChat}. * `sendMessage`, `respondToAction`, `sendAction`, and `retryMessage` resolve `{ data, error }` and never reject. * Inspect `error` on the result, or show hook `error`. */ type UseWebChatResult = { /** Conversation timeline. */ messages: AgentMessage[]; /** Tool approvals and MCP connect items that are still pending. */ pendingActions: AgentPendingAction[]; /** Server conversation id after create or resume. */ conversationId?: string; /** Last error from load, send, retry, or action. Mutations also return `{ error }` for that call. */ error?: NovuError | WebChatPlanLimitError; /** True while Web Chat loads and, for an existing conversation, until the first history fetch completes. */ isLoading: boolean; /** True while the agent turn is in progress. Same as `run.isRunning`. */ isRunning: boolean; /** True from `stop()` until the run ends. Same as `run.isStopping`. */ isStopping: boolean; /** Typing indicator. Same as `run.typing`. Absent when the agent is not typing. */ typing?: AgentConversationRunSnapshot['typing']; /** `'active'` or `'resolved'`. The agent sets `resolved` with `ctx.resolve()`. Not a loading flag. */ conversationStatus: AgentConversationStatus; /** Current agent-run snapshot. */ run: AgentConversationRunSnapshot; /** Older-history control. Not a top-level `hasMore` or `fetchMore`. */ pagination: WebChatPagination & { fetchMore: () => Promise<{ data?: { messages: AgentMessage[]; hasMore: boolean; }; error?: NovuError; }>; }; /** True while reconnect recovery is in progress. */ isRecovering: boolean; /** Set when reconnect recovery fails. Separate from send and fetch `error`. */ catchUpError?: NovuError; /** Reload the newest history page. No-op when there is no conversation id. */ refetch: () => Promise; /** * Send a user message. `input` is a string, or `{ text, metadata }`. Creates a conversation when `conversationId` is omitted. * Does not throw. Resolves `{ data, error }`. Inspect `error` on the result, or show hook `error`. */ sendMessage: (input: SendMessageInput) => Promise<{ data?: SendMessageResult; error?: NovuError | WebChatPlanLimitError; }>; /** * Resolve a pending `approval`. Pass `approvalId` from `pendingActions` or message parts. * Does not throw. Resolves `{ data, error }`. Inspect `error` on the result, or show hook `error`. */ respondToAction: (args: { approvalId: string; decision: AgentToolApprovalDecision; }) => Promise<{ data?: RespondToActionResult; error?: NovuError | WebChatPlanLimitError; }>; /** * Click a Card button. Do not use this for tool approval. * Does not throw. Resolves `{ data, error }`. Inspect `error` on the result, or show hook `error`. */ sendAction: (args: { actionId: string; sourceMessageId: string; value?: string; }) => Promise<{ data?: SendActionResult; error?: NovuError | WebChatPlanLimitError; }>; /** * Resend a message whose `status` is `failed`. Reuses the original idempotency key. * Does not throw. Resolves `{ data, error }`. Inspect `error` on the result, or show hook `error`. */ retryMessage: (messageId: string) => Promise<{ data?: SendMessageResult; error?: NovuError | WebChatPlanLimitError; }>; /** * Stop the agent turn that is running. Does nothing when no turn runs. Resolves an `error` when no message was * sent yet. `isStopping` is true until the run ends, and the last assistant message then has `stopped: true`. * Does not throw. Resolves `{ data, error }`. Inspect `error` on the result, or show hook `error`. * * @experimental Not every agent runtime can stop a turn mid-step yet. The agent finishes its current step first. */ stop: () => Promise<{ data?: StopResult; error?: NovuError | WebChatPlanLimitError; }>; /** * Start a new empty chat for the current `agentId`. The next `sendMessage` creates the server conversation. * No-op when `conversationId` or `conversation` is provided. */ startNewConversation: () => void; }; /** * Headless Web Chat client. Use it inside `NovuProvider`. * * @example * ```tsx * const { messages, sendMessage, isRunning, isLoading, error } = useWebChat({ * agentId: 'YOUR_AGENT_IDENTIFIER', * }); * ``` */ declare const useWebChat: (props: UseWebChatProps) => UseWebChatResult; export { useWebChat }; export type { UseWebChatProps, UseWebChatResult };