CoolFace
Apppublic

Leon4gr45/builder

sourceHugging Facemitupdated 2d agoView on Hugging Face
0likes
server-config-manager.ts82 linesDownload Raw Back to server-generate
1import type { ServerGenerationParams } from './types';2import type { ProviderId } from '@/lib/llm/providers/types';3 4interface SessionCost {5  totalCost: number;6  requestCount: number;7  totalPromptTokens: number;8  totalCompletionTokens: number;9}10 11export class ServerConfigManager {12  private session: SessionCost = {13    totalCost: 0,14    requestCount: 0,15    totalPromptTokens: 0,16    totalCompletionTokens: 0,17  };18 19  constructor(20    private readonly params: ServerGenerationParams,21    private readonly taskId?: string,22  ) {}23 24  getSelectedProvider(): ProviderId {25    return this.params.provider;26  }27 28  getProviderApiKey(provider: ProviderId): string | null {29    return provider === this.params.provider ? this.params.apiKey : null;30  }31 32  getProviderModel(provider: ProviderId): string | null {33    return provider === this.params.provider ? this.params.model : null;34  }35 36  getCachedModels(37    provider: ProviderId,38  ): { models: Array<{ id: string; name: string; context_length?: number }>; timestamp: number } | null {39    if (provider !== this.params.provider || !this.params.cachedModels) return null;40    return { models: this.params.cachedModels, timestamp: Date.now() };41  }42 43  getModelPricing(_provider: ProviderId, model: string): { prompt: number; completion: number } | null {44    return this.params.modelPricing?.[model] ?? null;45  }46 47  getReasoningEnabled(_model: string): boolean {48    return this.params.reasoningEnabled ?? false;49  }50 51  getDebugStreamEnabled(): boolean {52    return this.params.debugStreamEnabled ?? false;53  }54 55  isCompactionEnabled(_provider: ProviderId): boolean {56    return this.params.compactionEnabled ?? true;57  }58 59  getCompactionLimit(_provider: ProviderId): number | undefined {60    return this.params.compactionLimit;61  }62 63  getModelContextLengthFromCache(_provider: ProviderId, modelId: string): number | undefined {64    return this.params.cachedModels?.find((m) => m.id === modelId)?.context_length;65  }66 67  updateSessionCost(usage: { promptTokens?: number; completionTokens?: number }, cost: number): void {68    this.session.totalCost += cost;69    this.session.requestCount += 1;70    this.session.totalPromptTokens += usage.promptTokens ?? 0;71    this.session.totalCompletionTokens += usage.completionTokens ?? 0;72  }73 74  getCurrentSession(): { sessionId?: string; totalCost: number; requestCount: number } | null {75    return { sessionId: this.taskId, ...this.session };76  }77 78  getSessionCost(): SessionCost {79    return { ...this.session };80  }81}82