@agentman/chat-widget
Version:
Agentman Chat Widget for easy integration with web applications
150 lines (149 loc) • 5.42 kB
TypeScript
import type { Message } from '../types/types';
import type { IFileHandler } from '../types/FileTypes';
import type { StateManager } from '../StateManager';
import type { ViewManager } from '../components/ViewManager';
import type { PersistenceManager } from '../PersistenceManager';
import type { ApiService } from '../services/ApiService';
import type { MessageService } from '../services/MessageService';
import type { AgentService } from '../services/AgentService';
import type { ErrorHandler } from './ErrorHandler';
import type { EventBus } from '../utils/EventBus';
/**
* MessageHandler - Orchestrates message flow and UI updates
*
* This handler manages:
* - Message sending flow
* - Response processing
* - Loading state management
* - Message view updates
* - Error handling for message operations
*/
export declare class MessageHandler {
private stateManager;
private viewManager;
private apiService;
private messageService;
private agentService;
private errorHandler;
private eventBus;
private persistenceManager;
private config;
private logger;
private lastMessageCount;
private isProcessingMessage;
private loadingManager;
private unifiedLoadingManager;
private currentLoadingOperation;
private fileHandler?;
constructor(stateManager: StateManager, viewManager: ViewManager, apiService: ApiService, messageService: MessageService, agentService: AgentService, errorHandler: ErrorHandler, eventBus: EventBus, persistenceManager: PersistenceManager | null, config: {
features?: {
streaming?: {
enabled?: boolean;
};
};
debug?: boolean;
}, debug?: boolean);
/**
* Send a message through the complete flow
* @param message - The text message to send
* @param conversationId - ID of the current conversation
* @param config - Configuration including agent token and attachments
* @param config.agentToken - Authentication token for the agent
* @param config.clientMetadata - Optional metadata to include
* @param config.attachmentFileIds - Array of uploaded file IDs to attach
* @param config.attachmentUrls - Deprecated, use attachmentFileIds
* @returns Promise that resolves when message is sent
* @throws Error if API call fails or response format is invalid
*/
sendMessage(message: string, conversationId: string, config: {
agentToken: string;
clientMetadata?: any;
attachmentFileIds?: string[];
attachmentUrls?: string[];
}): Promise<void>;
/**
* Wait for file uploads to complete with timeout
* @param timeout - Maximum time to wait in milliseconds
* @returns Promise that resolves when uploads complete or timeout is reached
*/
private waitForUploads;
/**
* Process initial response (e.g., from agent initialization)
* @param responseData - Array of message data from API
* @returns Promise that resolves when messages are processed
*/
handleInitialResponse(responseData: any[]): Promise<void>;
/**
* Handle API response with messages
* @param responseData - Array of message data from API
* @param conversationId - ID of the current conversation
* @returns Promise that resolves when messages are processed
*/
handleApiResponse(responseData: any[], conversationId: string): Promise<void>;
/**
* Add a message to the view and state
* @param message - Message object to add
* @returns Promise that resolves when message is added to UI
*/
addMessageToView(message: Message): Promise<void>;
/**
* Show loading indicator (delegated to LoadingManager)
*/
showLoadingIndicator(): void;
/**
* Hide loading indicator (delegated to LoadingManager)
*/
hideLoadingIndicator(): void;
/**
* Reset message count (e.g., when switching conversations)
*/
resetMessageCount(): void;
/**
* Update message count (e.g., when loading conversation)
*/
setMessageCount(count: number): void;
/**
* Get current message count
*/
getMessageCount(): number;
/**
* Check if currently processing a message
*/
isProcessing(): boolean;
/**
* Clear all messages from view
*/
clearMessages(): void;
/**
* Load messages into view (e.g., when switching conversations)
*/
loadMessages(messages: Message[]): void;
/**
* Handle prompt click from welcome screen
*/
handlePromptClick(prompt: string, conversationId: string, config: any): Promise<void>;
/**
* Save conversation state
*/
private saveConversation;
/**
* Schedule conversation save with debouncing
*/
private saveDebounceTimer;
private scheduleConversationSave;
/**
* Setup unified loading event listeners
*/
private setupUnifiedLoadingListeners;
/**
* Set the FileHandler (optional dependency)
* @param fileHandler - The file handler instance for managing attachments
* @description Allows late binding of FileHandler to avoid circular dependencies
*/
setFileHandler(fileHandler: IFileHandler): void;
/**
* Clean up resources and cancel pending operations
* @description Cleans up timers, loading operations, and resets state
*/
destroy(): void;
}