File size: 2,510 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { ServerGenerationParams } from './types';
import type { ProviderId } from '@/lib/llm/providers/types';

interface SessionCost {
  totalCost: number;
  requestCount: number;
  totalPromptTokens: number;
  totalCompletionTokens: number;
}

export class ServerConfigManager {
  private session: SessionCost = {
    totalCost: 0,
    requestCount: 0,
    totalPromptTokens: 0,
    totalCompletionTokens: 0,
  };

  constructor(
    private readonly params: ServerGenerationParams,
    private readonly taskId?: string,
  ) {}

  getSelectedProvider(): ProviderId {
    return this.params.provider;
  }

  getProviderApiKey(provider: ProviderId): string | null {
    return provider === this.params.provider ? this.params.apiKey : null;
  }

  getProviderModel(provider: ProviderId): string | null {
    return provider === this.params.provider ? this.params.model : null;
  }

  getCachedModels(
    provider: ProviderId,
  ): { models: Array<{ id: string; name: string; context_length?: number }>; timestamp: number } | null {
    if (provider !== this.params.provider || !this.params.cachedModels) return null;
    return { models: this.params.cachedModels, timestamp: Date.now() };
  }

  getModelPricing(_provider: ProviderId, model: string): { prompt: number; completion: number } | null {
    return this.params.modelPricing?.[model] ?? null;
  }

  getReasoningEnabled(_model: string): boolean {
    return this.params.reasoningEnabled ?? false;
  }

  getDebugStreamEnabled(): boolean {
    return this.params.debugStreamEnabled ?? false;
  }

  isCompactionEnabled(_provider: ProviderId): boolean {
    return this.params.compactionEnabled ?? true;
  }

  getCompactionLimit(_provider: ProviderId): number | undefined {
    return this.params.compactionLimit;
  }

  getModelContextLengthFromCache(_provider: ProviderId, modelId: string): number | undefined {
    return this.params.cachedModels?.find((m) => m.id === modelId)?.context_length;
  }

  updateSessionCost(usage: { promptTokens?: number; completionTokens?: number }, cost: number): void {
    this.session.totalCost += cost;
    this.session.requestCount += 1;
    this.session.totalPromptTokens += usage.promptTokens ?? 0;
    this.session.totalCompletionTokens += usage.completionTokens ?? 0;
  }

  getCurrentSession(): { sessionId?: string; totalCost: number; requestCount: number } | null {
    return { sessionId: this.taskId, ...this.session };
  }

  getSessionCost(): SessionCost {
    return { ...this.session };
  }
}