@cossistant/next
Version:
Next.js-ready SDK for building AI-powered support/chat widgets. Hooks + primitives, WS-driven, TypeScript-first. Next.js-ready, Tailwind optional.
86 lines (85 loc) • 2.67 kB
TypeScript
import { UseMultimodalInputOptions } from "./use-multimodal-input.js";
//#region ../react/src/hooks/use-message-composer.d.ts
type UseMessageComposerOptions = {
/**
* The Cossistant client instance.
*/
client: CossistantClient;
/**
* Current conversation ID. Can be null if no real conversation exists yet.
* Pass null when showing default timeline items before user sends first message.
*/
conversationId: string | null;
/**
* Default timeline items to include when creating a new conversation.
*/
defaultTimelineItems?: TimelineItem[];
/**
* Visitor ID to associate with messages.
*/
visitorId?: string;
/**
* Callback when a message is successfully sent.
* @param conversationId - The conversation ID (may be newly created)
* @param messageId - The sent message ID
*/
onMessageSent?: (conversationId: string, messageId: string) => void;
/**
* Callback when message sending fails.
*/
onError?: (error: Error) => void;
/**
* File upload options (max size, allowed types, etc.)
*/
fileOptions?: Pick<UseMultimodalInputOptions, "maxFileSize" | "maxFiles" | "allowedFileTypes">;
};
type UseMessageComposerReturn = {
message: string;
files: File[];
error: Error | null;
isSubmitting: boolean;
canSubmit: boolean;
setMessage: (message: string) => void;
addFiles: (files: File[]) => void;
removeFile: (index: number) => void;
clearFiles: () => void;
submit: () => void;
reset: () => void;
};
/**
* Combines message input, typing indicators, and message sending into
* a single, cohesive hook for building message composers.
*
* This hook:
* - Manages text input and file attachments via useMultimodalInput
* - Sends typing indicators while user is composing
* - Handles message submission with proper error handling
* - Automatically resets input after successful send
* - Works with both pending and real conversations
*
* @example
* ```tsx
* const composer = useMessageComposer({
* client,
* conversationId: realConversationId, // null if pending
* defaultMessages,
* visitorId: visitor?.id,
* onMessageSent: (convId) => {
* // Update conversation ID if it was created
* },
* });
*
* return (
* <MessageInput
* value={composer.message}
* onChange={composer.setMessage}
* onSubmit={composer.submit}
* disabled={composer.isSubmitting}
* />
* );
* ```
*/
declare function useMessageComposer(options: UseMessageComposerOptions): UseMessageComposerReturn;
//#endregion
export { UseMessageComposerOptions, UseMessageComposerReturn, useMessageComposer };
//# sourceMappingURL=use-message-composer.d.ts.map