@makolabs/ripple
Version:
Simple Svelte 5 powered component library ✨
116 lines (115 loc) • 3.07 kB
TypeScript
import type { ChatResponse, ChatMessage, StreamingCallback } from '../../index.js';
import type { AIAdapter, AIContext } from './types.js';
/**
* Configuration for OpenAI adapter
*/
export interface OpenAIAdapterConfig {
/**
* OpenAI API key
*/
apiKey: string;
/**
* Model to use (defaults to gpt-4-turbo-preview)
*/
model?: string;
/**
* Base URL for API calls (defaults to OpenAI's API)
*/
baseUrl?: string;
/**
* System prompt for the AI
*/
systemPrompt?: string;
/**
* Temperature for responses (0-2, defaults to 0.7)
*/
temperature?: number;
/**
* Maximum tokens in response (defaults to 4000)
*/
maxTokens?: number;
/**
* Whether to maintain conversation history (defaults to true)
*/
maintainHistory?: boolean;
/**
* Maximum number of messages to keep in history (defaults to 20)
*/
maxHistoryLength?: number;
}
/**
* OpenAI adapter with simple sliding window memory
* Handles communication with OpenAI's GPT models with AI-generated summaries
*/
export declare class OpenAIAdapter implements AIAdapter {
private config;
private messages;
private summary;
private userProfile;
private maxMessages;
constructor(config: OpenAIAdapterConfig);
getName(): string;
isConfigured(): boolean;
private getDefaultSystemPrompt;
private getCodeRules;
private getMermaidRules;
private buildConditionalSystemPrompt;
sendMessage(message: string, context?: AIContext): Promise<ChatResponse>;
sendMessageStream(message: string, onStream: StreamingCallback, context?: AIContext): Promise<ChatResponse>;
getHistory(): ChatMessage[];
clearHistory(): void;
setSystemPrompt(prompt: string): void;
getSystemPrompt(): string;
/**
* Update configuration
*/
updateConfig(newConfig: Partial<OpenAIAdapterConfig>): void;
/**
* Get current configuration (without API key for security)
*/
getConfig(): Omit<OpenAIAdapterConfig, 'apiKey'>;
/**
* Add message to memory with sliding window + AI summary logic
*/
private addToMemory;
/**
* Create AI summary and truncate old messages
*/
private createSummaryAndTruncate;
/**
* Generate summary using OpenAI
*/
private generateSummaryWithAI;
/**
* Build memory context from summary + recent messages
*/
private buildMemoryContext;
/**
* Extract user information
*/
private extractUserInfo;
/**
* Save to localStorage
*/
private saveToStorage;
/**
* Load from localStorage
*/
private loadFromStorage;
private generateId;
/**
* Build enhanced system prompt with memory context
*/
private buildEnhancedSystemPrompt;
/**
* Get memory statistics
*/
getMemoryStats(): {
totalMessages: number;
hasSummary: boolean;
};
/**
* Get the context that will be sent to AI
*/
getContextForAI(): string;
}