UNPKG

@agentman/chat-widget

Version:

Agentman Chat Widget for easy integration with web applications

81 lines (80 loc) 2.42 kB
import type { Message } from '../types/types'; export interface ProcessedResponse { newMessages: Message[]; totalCount: number; } /** * MessageService - Handles all message-related operations * * Responsibilities: * - Message validation and processing * - Response filtering and deduplication * - Message ID generation * - Message formatting and preparation * - Content validation and sanitization */ export declare class MessageService { private logger; private apiUrl; constructor(apiUrl: string, debug?: boolean | import('../types/types').DebugConfig); /** * Validate if a message object is properly formatted */ validateMessage(msg: any): boolean; /** * Extract content and attachments from message data */ private extractMessageContent; /** * Process API response and extract new messages */ processResponse(responseData: any[], lastMessageCount: number, existingMessages?: Message[]): ProcessedResponse; /** * Process initial response (for welcome messages) */ processInitialResponse(responseData: any[], lastMessageCount?: number): ProcessedResponse; /** * Create a user message object */ createUserMessage(content: string, attachments?: any[]): Message; /** * Create an error message object */ createErrorMessage(content: string): Message; /** * Generate a unique message ID */ generateMessageId(): string; /** * Filter messages by type */ filterMessagesByType(messages: Message[], type: 'user' | 'agent'): Message[]; /** * Get last N messages */ getLastMessages(messages: Message[], count: number): Message[]; /** * Sanitize message content (basic XSS prevention) */ sanitizeContent(content: string): string; /** * Check if two messages have the same content (for deduplication) */ isDuplicateContent(message1: Message, message2: Message): boolean; /** * Get message statistics */ getMessageStats(messages: Message[]): { userCount: number; agentCount: number; total: number; }; /** * Validate message content length */ validateContentLength(content: string, maxLength?: number): boolean; /** * Truncate message content if too long */ truncateContent(content: string, maxLength?: number): string; }