/** * @fileoverview Message sending and streaming response handling. * Handles the complete flow of sending a chat message, receiving streamed * responses, and updating the UI state accordingly. * @module components/ResearchAgent/state/chat/sendMessage */ import { Message } from "../../components/ChatConversation/ChatWindow"; import { ChatModelProvider, ChatFile } from "../../types/chat"; /** * Parameters for sending a chat message. */ export interface SendMessageParams { /** The message content to send */ message: string; /** Optional custom message ID (used when rewriting) */ messageId?: string; /** Whether this is a rewrite of a previous response */ rewrite?: boolean; } /** * Dependencies required by the sendMessage function. * These are passed from the ChatProvider to allow the function * to access and modify chat state. */ export interface SendMessageDeps { /** Current chat session ID */ chatId: string; /** Whether a message is currently being sent */ loading: boolean; /** Current messages in the chat */ messages: Message[]; /** IDs of files attached to the chat */ fileIds: string[]; /** Full metadata of files attached to the chat (for inline display). */ files: ChatFile[]; /** Current search/focus mode */ focusMode: string; /** Current category for search filtering */ category: string; /** Response optimization mode */ optimizationMode: string; /** Conversation history for context */ chatHistory: [string, string][]; /** AI model provider configuration */ chatModelProvider: ChatModelProvider; /** Whether the user is authenticated */ isAuthenticated: boolean; /** Ref to current messages (avoids stale closures) */ messagesRef: React.MutableRefObject; /** Ref to the AbortController for cancelling the stream */ abortControllerRef: React.MutableRefObject; /** Setter for loading state */ setLoading: (loading: boolean) => void; /** Setter for message appeared state */ setMessageAppeared: (appeared: boolean) => void; /** Setter for messages array */ setMessages: React.Dispatch>; /** Setter for chat history */ setChatHistory: React.Dispatch>; /** Setter for the selected chat model provider (optional — used to reset on model errors) */ setChatModelProvider?: (provider: ChatModelProvider) => void; } /** * Sends a message to the chat API and handles the streaming response. * * This function: * 1. Adds the user message to the chat immediately * 2. Sends the request to `/api/chat` * 3. Processes the streaming response (sources, message chunks, completion) * 4. Updates the URL to include the chat ID * 5. Triggers auto media search if enabled * 6. Fetches follow-up suggestions after the response completes * * The streaming response is expected in newline-delimited JSON format with * message types: 'sources', 'message', 'messageEnd', 'error'. * * @param params - The message parameters * @param deps - State and setter dependencies from ChatProvider * * @example * ```typescript * await sendMessage( * { message: 'What is TypeScript?', rewrite: false }, * { * chatId: 'abc123', * loading: false, * messages: [], * // ... other dependencies * } * ); * ``` */ export declare function sendMessage(params: SendMessageParams, deps: SendMessageDeps): Promise;