File size: 2,523 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { configManager, type ProviderPricingEntry } from '@/lib/config/storage';
import type { ProviderId, ProviderModel } from '@/lib/llm/providers/types';
import type { OpenRouterModel } from '@/lib/llm/models-api';

const OPENROUTER_PROVIDER: ProviderId = 'openrouter';

function toPerMillion(value?: string | null): number | undefined {
  if (!value) {
    return undefined;
  }

  const parsed = Number(value);
  if (!Number.isFinite(parsed) || parsed <= 0) {
    return undefined;
  }

  if (parsed < 0.01) {
    return parsed * 1_000_000;
  }

  return parsed;
}

export function registerPricingFromProviderModels(
  provider: ProviderId,
  models: ProviderModel[]
): void {
  if (!Array.isArray(models) || models.length === 0) {
    return;
  }

  const pricingMap: Record<string, ProviderPricingEntry> = {};

  for (const model of models) {
    if (!model?.pricing) {
      continue;
    }

    const entry: ProviderPricingEntry = {
      input: model.pricing.input,
      output: model.pricing.output,
      reasoning: model.pricing.reasoning
    };

    if (!Number.isFinite(entry.input) || !Number.isFinite(entry.output)) {
      continue;
    }

    pricingMap[model.id] = entry;
    pricingMap[`${provider}/${model.id}`] = entry;
  }

  if (provider === OPENROUTER_PROVIDER) {
    for (const [modelId, entry] of Object.entries(pricingMap)) {
      const slug = modelId.split('/').pop();
      if (slug && !pricingMap[slug]) {
        pricingMap[slug] = entry;
      }
    }
  }

  if (Object.keys(pricingMap).length > 0) {
    configManager.setProviderPricing(provider, pricingMap);
  }
}

export function registerOpenRouterPricingFromApi(models: OpenRouterModel[]): void {
  if (!Array.isArray(models) || models.length === 0) {
    return;
  }

  const pricingMap: Record<string, ProviderPricingEntry> = {};

  for (const model of models) {
    const input = toPerMillion(model.pricing?.prompt);
    const output = toPerMillion(model.pricing?.completion);
    const reasoning = toPerMillion(model.pricing?.internal_reasoning);

    if (input === undefined || output === undefined) {
      continue;
    }

    const entry: ProviderPricingEntry = {
      input,
      output,
      reasoning
    };

    pricingMap[model.id] = entry;
    pricingMap[`${OPENROUTER_PROVIDER}/${model.id}`] = entry;

    if (model.canonical_slug) {
      pricingMap[model.canonical_slug] = entry;
    }
  }

  if (Object.keys(pricingMap).length > 0) {
    configManager.setProviderPricing(OPENROUTER_PROVIDER, pricingMap);
  }
}