UNPKG

@unified-llm/core

Version:

Unified LLM interface (in-memory).

142 lines 4.25 kB
import { v4 as uuidv4 } from 'uuid'; /** * Thread-based chat session * Multiple LLM clients can join an in-memory conversation thread * Note: v0.4.0 removed persistence support - threads are now in-memory only */ export class Thread { constructor(config = {}) { this.clients = new Map(); this.messages = []; this.id = config.threadId || `thread_${uuidv4()}`; this.title = config.title; this.description = config.description; } /** * Add an LLM client to the thread */ addAssistant(assistant, name) { this.clients.set(name, assistant); } /** * Remove an LLM client from the thread */ removeAssistant(name) { return this.clients.delete(name); } /** * Send a message to all clients in the thread */ async broadcast(message) { const responses = new Map(); // Add user message to thread this.messages.push({ id: `msg_${uuidv4()}`, role: 'user', content: [{ type: 'text', text: message }], createdAt: new Date(), metadata: { timestamp: new Date(), } }); // Get responses from all clients for (const [name, client] of this.clients) { try { const response = await client.chat(this.messages); responses.set(name, response); // Add assistant response to thread this.messages.push({ id: response.id, role: 'assistant', content: response.message.content, createdAt: response.createdAt, metadata: { ...response.message.metadata, clientName: name, } }); } catch (error) { console.error(`Failed to get response from ${name}:`, error); } } return responses; } /** * Send a message to a specific client in the thread */ async sendTo(clientName, message) { const client = this.clients.get(clientName); if (!client) { console.error(`Client ${clientName} not found in thread`); return null; } // Add user message to thread this.messages.push({ id: `msg_${uuidv4()}`, role: 'user', content: [{ type: 'text', text: message }], createdAt: new Date(), metadata: { timestamp: new Date(), directedTo: clientName, } }); try { const response = await client.chat(this.messages); // Add assistant response to thread this.messages.push({ id: response.id, role: 'assistant', content: response.message.content, createdAt: response.createdAt, metadata: { ...response.message.metadata, clientName: clientName, } }); return response; } catch (error) { console.error(`Failed to get response from ${clientName}:`, error); return null; } } /** * Get the conversation thread as a structured object */ getConversation() { return { id: this.id, title: this.title, messages: this.messages, createdAt: new Date(), updatedAt: new Date(), metadata: { description: this.description, clientCount: this.clients.size, messageCount: this.messages.length, } }; } /** * Clear all messages from the thread */ clearMessages() { this.messages = []; } /** * Get all client names in the thread */ getClientNames() { return Array.from(this.clients.keys()); } } export default Thread; //# sourceMappingURL=thread.js.map