import { ProviderResponse } from '../../../types/chat';
import { VertexCacheKey } from './types';

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

  set(key: VertexCacheKey, value: ProviderResponse): void {
    this.cache.set(this.stringifyKey(key), value);
  }

  get(key: VertexCacheKey): ProviderResponse | undefined {
    return this.cache.get(this.stringifyKey(key));
  }

  has(key: VertexCacheKey): boolean {
    return this.cache.has(this.stringifyKey(key));
  }

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

  private stringifyKey(key: VertexCacheKey): string {
    return JSON.stringify({
      messages: key.messages.map(m => ({ role: m.role, content: m.content })),
      options: key.options,
      tools: key.tools?.map(t => t.name)
    });
  }
}