import { ChatMessage, ChatOptions, ProviderResponse } from '../../../types/chat';
import { Tool } from '../../../types/tool';

interface CacheKey {
  messages: ChatMessage[];
  options: ChatOptions;
  tools?: Tool[];
}

export class OpenAICache {
  private cache: Map<string, ProviderResponse> = new Map();

  private generateCacheKey(key: CacheKey): string {
    return JSON.stringify({
      messages: key.messages,
      options: key.options,
      tools: key.tools?.map(tool => tool.name)
    });
  }

  set(key: CacheKey, value: ProviderResponse): void {
    const cacheKey = this.generateCacheKey(key);
    this.cache.set(cacheKey, value);
  }

  get(key: CacheKey): ProviderResponse | undefined {
    const cacheKey = this.generateCacheKey(key);
    return this.cache.get(cacheKey);
  }

  has(key: CacheKey): boolean {
    const cacheKey = this.generateCacheKey(key);
    return this.cache.has(cacheKey);
  }

  clear(): void {
    this.cache.clear();
  }
}