nymia-ai-assistant
Version:
Package for integrating an AI assistant chat into any web application easily and customizable.
428 lines (422 loc) • 11.9 kB
text/typescript
/**
* Types of notifications that can be sent
*/
type NotificationType = "info" | "warning" | "error" | "success";
/**
* Options for configuring notifications
*/
interface NotificationOptions {
/** Notification type */
type: NotificationType;
/** Message to display */
message: string;
/** Notification duration in ms (default: 3000) */
duration?: number;
}
/**
* Class to handle notifications in the user interface
*/
declare class Notification {
private readonly type;
private readonly colors;
/**
* Creates a new notification instance
* @param type Notification type (info, warning, error, success)
*/
constructor(type?: NotificationType);
/**
* Sends a notification
* @param message Message to display
* @param options Additional options (duration)
*/
send(message: string, options?: {
duration?: number;
}): Promise<void>;
}
/**
* Possible positions for the floating button
*/
type FloatingButtonPosition = "bottom-right" | "bottom-left" | "top-right" | "top-left";
/**
* Available sizes for the floating button
*/
type ButtonSize = "small" | "medium" | "large";
/**
* Configuration options for the floating button
*/
interface FloatingButtonOptions {
/** Position of the button on the screen */
position?: FloatingButtonPosition;
/** Background color of the button */
backgroundColor?: string;
/** Color of the icon/text */
color?: string;
/** Content of the button (icon or text) */
icon?: string;
/** Size of the button */
size?: ButtonSize;
/** Function to execute when the button is clicked */
onClick?: () => void;
/** CSS selector or element where to mount the button */
container?: HTMLElement | string;
/** Additional CSS classes */
className?: string;
/** Whether to hide the button initially */
hidden?: boolean;
}
/**
* FloatingButton Component - Creates a customizable floating button
* that can be positioned in any of the 4 corners of the screen
*/
declare class FloatingButton {
private button;
private options;
private mounted;
/**
* Default dimensions for each button size
*/
private readonly sizes;
/**
* Creates a new instance of FloatingButton
* @param options Configuration options
*/
constructor(options?: FloatingButtonOptions);
/**
* Creates the floating button element and applies the styles
*/
private createButton;
/**
* Applies the CSS styles to the button
*/
private applyStyles;
/**
* Mounts the button to the DOM
* @param container Element or selector where to mount the button
*/
mount(container?: HTMLElement | string): void;
/**
* Unmounts the button from the DOM
*/
unmount(): void;
/**
* Updates the button options
* @param options New options to apply
*/
update(options: Partial<FloatingButtonOptions>): void;
/**
* Shows the button
*/
show(): void;
/**
* Hides the button
*/
hide(): void;
/**
* Change the onClick event
* @param handler New handler function
*/
setOnClick(handler: () => void): void;
}
/**
* Possible position for the chat window
*/
type ChatPosition = "bottom-right" | "bottom-left" | "top-right" | "top-left";
/**
* Theme options for the chat
*/
interface ChatTheme {
/** Primary color for headers and user messages */
primaryColor?: string;
/** Text color */
textColor?: string;
/** Window background color */
backgroundColor?: string;
/** Background color of user messages */
userMessageBgColor?: string;
/** Text color of user messages */
userMessageTextColor?: string;
/** Background color of assistant messages */
assistantMessageBgColor?: string;
/** Text color of assistant messages */
assistantMessageTextColor?: string;
/** Input border color */
inputBorderColor?: string;
/** Input background color */
inputBgColor?: string;
/** Input text color */
inputTextColor?: string;
}
/**
* Configuration options for the chat
*/
interface ChatOptions {
/** Chat window title */
title?: string;
/** Placeholder text for the text area */
placeholder?: string;
/** Chat window position */
position?: ChatPosition;
/** Window width in pixels */
width?: number;
/** Window height in pixels */
height?: number;
/** Function to execute when a message is sent */
onSend?: (message: any) => Promise<string | {
content: string;
isHtml: boolean;
}> | string | {
content: string;
isHtml: boolean;
};
/** Initial assistant message */
initialMessage?: string;
/** Theme options */
theme?: ChatTheme;
/** Whether it should be open on startup */
isOpen?: boolean;
/** Show images option (checkbox) */
showImagesOption?: boolean;
/** Enable audio answers (hide text, show audio player) */
audioAnswers?: boolean;
}
/**
* Chat Component - Implements a complete chat interface with message handling,
* input auto-adjustment, animations, and customizable styles
*/
declare class Chat {
private container;
private chatWindow;
private messageList;
private inputArea;
private isOpen;
private options;
private onNewConversationCallback?;
private newConvButton?;
private mediaRecorder?;
private audioChunks;
private isRecording;
private recordingTimer?;
/**
* Creates a new Chat instance
* @param options Configuration options
*/
constructor(options?: ChatOptions);
/**
* Creates the DOM elements for the chat
*/
private createChatElements;
/**
* Adds the necessary event listeners
*/
private addEventListeners;
/**
* Sets up audio recording functionality for the record button
*/
private setupAudioRecording;
/**
* Starts the recording timer and begins audio recording
*/
private startRecordingTimer;
/**
* Starts audio recording
*/
private startAudioRecording;
/**
* Stops recording and processes the audio
*/
private stopRecording;
/**
* Processes the recorded audio and sends it
*/
private processRecordedAudio;
/**
* Converts audio blob to WAV format
*/
private convertToWav;
/**
* Converts AudioBuffer to WAV Blob
*/
private audioBufferToWav;
/**
* Sends an audio message with optional text
*/
private sendAudioMessage;
/**
* Automatically adjusts the height of the textarea according to its content
*/
private autoResizeTextarea;
/**
* Resets the textarea to its initial state
*/
private resetTextarea;
/**
* Sends a message and processes the response
*/
private sendMessage;
/**
* Adds a message to the chat
* @param text Message text
* @param sender Message sender (user, assistant, error)
* @param isHtml Whether the text contains HTML (only for assistant messages)
*/
private addMessage;
/**
* Sanitizes the text to prevent XSS
*/
private sanitizeText;
/**
* Sanitizes HTML to allow only safe tags for assistant messages
*/
private sanitizeHtml;
/**
* Recursively clean HTML elements
*/
private cleanElement;
/**
* Formats the text by detecting links, titles, and lists
*/
private formatText;
/**
* Formats the text by detecting titles and lists, but skipping URL conversion to links
* Used when audioAnswers is enabled to avoid converting links to audio
*/
private formatTextWithoutLinks;
/**
* Shows the typing indicator
*/
private showTypingIndicator;
/**
* Hides the typing indicator
*/
private hideTypingIndicator;
/**
* Scrolls to the end of the message list
*/
private scrollToBottom;
/**
* Adds the initial assistant message
*/
private addInitialMessage;
/**
* Toggles between showing and hiding the chat
*/
toggle(): void;
/**
* Opens the chat
*/
open(): void;
/**
* Closes the chat
*/
close(): void;
/**
* Mounts the chat in the DOM
* @param container Element or selector where to mount the chat
*/
mount(container?: HTMLElement | string): void;
/**
* Unmounts the chat from the DOM
*/
unmount(): void;
/**
* Gets the state of the show images checkbox
* @returns boolean indicating if images should be shown
*/
getShowImages(): boolean;
/**
* Gets the audio answers setting from options
* @returns boolean indicating if audio answers are enabled
*/
getAudioAnswers(): boolean;
/**
* Sets the callback for the new conversation button
*/
setOnNewConversation(callback: () => void): void;
/**
* Clears all chat messages and resets the visual state
*/
clearMessages(): void;
/**
* Loads the necessary CSS styles
*/
private loadStyles;
/**
* Darkens a color by a certain percentage
*/
private darkenColor;
/**
* Converts an RGB color to hexadecimal format
*/
private rgbToHex;
}
/**
* Configuration options for the assistant
*/
interface AssistantOptions {
/** Required API Key for authentication */
apiKey: string;
/** Required API Base URL for authentication */
apiBaseUrl: string;
/** Title of the chat window */
title?: string;
/** Placeholder text for the text area */
placeholder?: string;
/** Position of the button and chat window */
position?: ChatPosition;
/** Initial message from the assistant */
initialMessage?: string;
/** Whether to search for images in the context */
searchImages?: boolean;
/** Enable audio responses (replaces text with audio player) */
audioAnswers?: boolean;
/** Specific options for the floating button */
buttonOptions?: {
/** Background color of the button */
backgroundColor?: string;
/** Color of the icon/text */
color?: string;
/** Content of the button (icon or text) */
icon?: string;
/** Size of the button */
size?: ButtonSize;
/** Selector of the container where to mount the button */
container?: HTMLElement | string;
};
/** Theme options for the chat */
theme?: ChatTheme;
/** Selector of the container where to mount the chat */
container?: HTMLElement | string;
/** Whether to show the chat automatically on startup */
autoOpen?: boolean;
}
/**
* Assistant interface
*/
interface Assistant {
/** Show the chat */
open: () => void;
/** Hide the chat */
close: () => void;
/** Toggle between showing/hiding the chat */
toggle: () => void;
/** Unmount the assistant (button and chat) */
unmount: () => void;
/** Check if the chat is open */
isOpen: () => boolean;
/** Hide the floating button */
hideButton: () => void;
/** Show the floating button */
showButton: () => void;
}
/**
* Creates a complete assistant with floating button and chat
* @param options Configuration options for the assistant
* @returns Assistant instance
*/
declare function createAssistant(options: AssistantOptions): Assistant;
/**
* Assistant AI - Library to integrate an AI assistant chat into any web application
*
* @module assistant-ia
*/
export { type AssistantOptions, type ButtonSize, Chat, type ChatOptions, type ChatPosition, type ChatTheme, FloatingButton, type FloatingButtonOptions, type FloatingButtonPosition, Notification, type NotificationOptions, type NotificationType, createAssistant, createAssistant as default };