chat-send-utils
v1.0.0
Published
A utility library for chat message sending and processing with SSE support
Maintainers
Readme
Chat Send Utils
A comprehensive utility library for chat message sending, SSE processing, and message management.
Installation
npm install chat-send-utilsUsage
import {
scrollToBottom,
processSSEResponse,
pushUserMessage,
pushThinkingMessage,
validateBeforeSend,
saveMessagesToLocal,
saveFeedbackToLocal
} from 'chat-send-utils';
// Scroll to bottom of chat container
scrollToBottom(messagesContainer);
// Process SSE response from server
await processSSEResponse(
timeout,
response,
messages,
messagesContainer,
conversationId,
isGenerating,
currentReader,
currentTaskId
);
// Push user message to chat
pushUserMessage(messages, newMessage, messagesContainer, isGenerating);
// Validate before sending message
const isValid = validateBeforeSend(isGenerating, newMessage);API Reference
Core Functions
scrollToBottom(element)- Scroll to the bottom of the specified elementstopSSEResponse(currentReader)- Stop an ongoing SSE responseprocessSSEResponse(timeout, response, messages, messagesContainer, conversationId, isGenerating, currentReader, currentTaskId)- Process Server-Sent Events responsehandleTimeoutResponse(messages, isGenerating)- Handle timeout for chat responses
Message Management
pushThinkingMessage(messages, messagesContainer)- Add "thinking" message to chatpushUserMessage(messages, newMessage, messagesContainer, isGenerating)- Add user message to chatpushErrorMessage(messages, messagesContainer, isGenerating)- Add error message to chatvalidateBeforeSend(isGenerating, newMessage)- Validate if message can be sent
Local Storage
saveFeedbackToLocal(conversationId, messageId, feedbackType)- Save feedback to localStoragesaveMessagesToLocal(conversationId, messages)- Save messages to localStorage
Features
- SSE Processing: Full support for Server-Sent Events with proper error handling
- Auto-scrolling: Intelligent scrolling that respects user interaction
- Message Validation: Comprehensive validation before sending messages
- Local Storage: Persistent storage for messages and feedback
- Error Handling: Robust error handling for network and server issues
- Timeout Management: Automatic timeout handling for long-running requests
Browser Support
This library requires modern browser features:
- ES6 Modules
- Fetch API
- localStorage
- TextDecoder
License
MIT
