UNPKG

@agentman/chat-widget

Version:

Agentman Chat Widget for easy integration with web applications

267 lines (266 loc) 6.54 kB
import type { ChatConfig, ChatTheme } from './types/types'; /** * ChatWidget - Refactored component-based chat widget with welcome screen * * This simplified version delegates UI management to ViewManager and focuses on: * - Orchestrating component interactions * - Managing widget lifecycle * - Handling agent initialization * - Coordinating state and persistence */ export declare class ChatWidget { private static instances; private config; private state; private element; private containerId; private theme; private assets; private configManager; private stateManager; private styleManager; private persistenceManager; private viewManager; private conversationManager; private apiService; private messageService; private agentService; private isSwitchingConversation; private floatingPromptsTimeout; private eventBus; private errorHandler; private messageHandler; private fileHandler; private conversationOrchestrator; private logger; private eventSubscriptions; private supportedMimeTypes; private supportsAttachments; private agentCapabilities; private isInitializingAgent; private resizeTimeout; private saveDebounceTimer; constructor(config: ChatConfig & { containerId: string; }); /** * Initialize configuration */ private initializeConfig; /** * Initialize state management */ private initializeState; /** * Initialize assets and theme */ private initializeAssets; /** * Initialize core services */ private initializeServices; /** * Initialize core managers (but not agent yet) */ private initializeManagers; /** * Create the widget structure and view manager */ private createWidget; /** * Mount widget to appropriate container */ private mountToContainer; /** * Setup event listeners for EventBus communication */ private setupEventListeners; /** * Emit toggle event (called by ViewManager) */ private emitToggleEvent; /** * Handle expand button click */ private handleExpand; /** * Emit send event (called by ViewManager) */ private emitSendEvent; /** * Handle input key events */ private handleInputKey; /** * Emit prompt click event (called by ViewManager) */ private emitPromptClickEvent; /** * Handle attachment button click */ private handleAttachmentClick; /** * Handle file selection */ private handleFileSelect; /** * Handle attachment removal */ private handleAttachmentRemove; /** * Handle view transitions */ private handleViewTransition; /** * Handle window resize */ private handleResize; /** * Handle state changes */ private handleStateChange; /** * Update UI based on current state */ private updateUI; /** * Update responsive layout */ private updateResponsiveLayout; /** * Ensure FileHandler is initialized */ private ensureFileHandler; /** * Ensure a conversation exists, creating one if necessary * Used when files are uploaded from welcome screen */ private ensureConversationExists; /** * Ensure ConversationOrchestrator is initialized */ private ensureConversationOrchestrator; /** * Public API methods */ /** * Get current view type */ getCurrentView(): 'welcome' | 'conversation'; /** * Start a new conversation (reset to welcome screen) */ startNewConversation(): Promise<void>; /** * Apply theme CSS variables to the widget element */ private applyThemeToWidget; /** * Update theme colors */ updateTheme(theme: Partial<ChatTheme>): void; /** * Get the current conversation ID * Returns null if no conversation has been created yet * @returns {string | null} The current conversation ID or null */ getConversationId(): string | null; /** * Getter property for conversation ID * Provides easy access: chatWidget.conversationId */ get conversationId(): string | null; /** * Destroy the widget and clean up */ destroy(): void; /** * Event Handlers for EventBus communication */ /** * Handle user input events from EventBus */ private handleUserInputEvent; /** * Handle prompt click events from EventBus */ private handlePromptClickEvent; /** * Handle view transition events from EventBus */ private handleViewTransitionEvent; /** * Conversation management methods */ /** * Handle new conversation creation */ private handleNewConversation; /** * Handle conversation switching */ private handleSwitchConversation; /** * Handle conversation deletion */ private handleDeleteConversation; /** * Handle conversations click from welcome screen */ private handleConversationsClickFromWelcome; /** * Handle conversation list view toggle */ private handleToggleListView; /** * Setup conversation management features */ private setupConversationManagement; /** * Update conversation header buttons based on current conversation count */ private updateConversationHeaderButtons; /** * Initialize agent and fetch capabilities */ private initializeAgent; /** * Process agent capabilities from API metadata */ private processAgentCapabilities; /** * Get agent capabilities */ getAgentCapabilities(): { supportedMimeTypes: string[]; supportsAttachments: boolean; metadata: any; }; /** * Restore the current conversation from persistence */ private restoreCurrentConversation; /** * Show floating prompts when widget is closed */ private showFloatingPrompts; /** * Hide floating prompts */ private hideFloatingPrompts; /** * Escape HTML to prevent XSS */ private escapeHtml; /** * Static methods */ /** * Get existing instance by container ID */ static getInstance(containerId: string): ChatWidget | undefined; /** * Destroy all instances */ static destroyAll(): void; }