The services provide the core business logic for the application. They are implemented using the singleton pattern for global access.
Path: src/services/chat-service.ts
Description: Manages chat conversations and message handling.
Key Methods:
getInstance(): Returns the singleton instanceinitialize(): Initializes the serviceloadConversations(): Loads all conversations from the databaseloadConversation(conversationId): Loads a specific conversationcreateConversation(title): Creates a new conversationsendMessage(content, conversationId, isStreaming, conversationUpdate): Sends a message with streaming supporteditMessage(messageId, conversationId, newContent, isStreaming, conversationUpdate): Edits a messageregenerateAiMessage(messageId, conversationId, isStreaming, conversationUpdate): Regenerates an AI responsestopStreaming(conversationId): Stops the current streaming messageisCurrentlyStreaming(conversationId): Checks if streaming is activegetCurrentProviderModelCapabilities(): Gets capabilities of current provider/model combination
Usage Example:
const chatService = ChatService.getInstance();
await chatService.initialize();
const conversations = await chatService.loadConversations();Path: src/services/ai-service.ts
Description: Provides integration with AI providers.
Key Methods:
getInstance(): Returns the singleton instancegetProvider(name): Gets a provider by namegetAllProviders(): Gets all registered providersgetChatCompletion(messages, options, streamController): Gets a streaming chat completiongenerateImage(prompt, options): Generates an image from a promptgetCachedAllModels(): Gets all models from cachegetModelsForProvider(providerName): Gets models for a specific providerrefreshModels(): Refreshes the model cache
Usage Example:
const aiService = AIService.getInstance();
const provider = aiService.getProvider('openai');
const models = await aiService.getModelsForProvider('openai');Path: src/services/settings-service.ts
Description: Manages application settings and user preferences.
Key Methods:
getInstance(): Returns the singleton instancegetSettings(): Gets all settingssaveSettings(settings): Saves settingsgetSelectedProvider(): Gets the currently selected providersetSelectedProvider(provider): Sets the selected providergetSelectedModel(): Gets the currently selected modelsetSelectedModel(model): Sets the selected modelgetProviderSettings(provider): Gets settings for a specific providersetProviderApiKey(provider, apiKey): Sets the API key for a providergetWebSearchActive(): Gets the web search statussetWebSearchEnabled(enabled): Enables/disables web search
Usage Example:
const settingsService = SettingsService.getInstance();
const selectedProvider = settingsService.getSelectedProvider();
await settingsService.setSelectedModel('gpt-4');Path: src/services/database-integration.ts
Description: Provides database integration for persistent storage.
Key Methods:
getInstance(): Returns the singleton instanceinitialize(): Initializes the database connectionloadConversationsList(): Loads the list of all conversationsloadConversation(conversationId): Loads a specific conversation with messagescreateConversation(title): Creates a new conversationupdateConversation(conversation): Updates a conversationdeleteConversation(conversationId): Deletes a conversationupdateChatMessage(messageId, message, conversationId): Updates a messageloadFoldersList(): Loads all conversation folderscreateFolder(folder): Creates a new folderupdateFolder(folderId, folder): Updates a folderdeleteFolder(folderId): Deletes a folder
Usage Example:
const dbService = DatabaseIntegrationService.getInstance();
await dbService.initialize();
const conversations = await dbService.loadConversationsList();Path: src/services/message-helper.ts
Description: Provides utility functions for message handling.
Key Methods:
mapMessagesTreeToList(conversation, includeSystemMessages): Maps message tree to flat listaddUserMessageToConversation(content, conversation): Adds a user message to conversationinsertAssistantMessageToConversation(userMessage, aiResponse, conversation): Inserts AI responsegetPlaceholderMessage(model, provider, conversationId): Creates a placeholder message for streaming
Usage Example:
const messages = MessageHelper.mapMessagesTreeToList(conversation, false);
const { conversation: updated, message: userMsg } = await MessageHelper.addUserMessageToConversation(content, conversation);Path: src/services/streaming-control.ts
Description: Handles streaming control for AI responses.
Key Methods:
constructor(conversation, placeholderMessage, onChunk, onFinish): Initializes a handlergetAbortSignal(): Gets the abort signal for cancellationabortStreaming(): Aborts the current streaminghandleChunk(chunk): Handles a streaming chunkfinalizeStreaming(): Finalizes the streaming process
Usage Example:
const streamController = new StreamControlHandler(
conversation,
placeholderMessage,
(updated) => { /* handle chunk */ },
(aiResponse) => { /* handle final response */ }
);Path: src/services/providers/
Description: Provider implementations for different AI services.
The provider system uses a factory pattern to create provider instances:
ProviderFactory:
getNewProvider(providerName): Creates a new provider instance
Provider Interface:
getChatCompletion(messages, options, streamController): Gets chat completiongetAvailableModels(): Gets available models for the provider
Implemented Providers:
- OpenAI
- Anthropic
- Custom providers