@pydantic/genai-prices 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/matcher.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Provider, ModelInfo } from './types.js';
2
- export declare function matchProvider(providers: Provider[], modelRef: string, providerId?: string, providerApiUrl?: string): Provider | undefined;
3
- export declare function matchModel(models: ModelInfo[], modelRef: string): ModelInfo | undefined;
package/dist/matcher.js DELETED
@@ -1,43 +0,0 @@
1
- function matchLogic(logic, text) {
2
- if ('or' in logic) {
3
- return logic.or.some((clause) => matchLogic(clause, text));
4
- }
5
- if ('and' in logic) {
6
- return logic.and.every((clause) => matchLogic(clause, text));
7
- }
8
- if ('equals' in logic) {
9
- return text === logic.equals;
10
- }
11
- if ('starts_with' in logic) {
12
- return text.startsWith(logic.starts_with);
13
- }
14
- if ('ends_with' in logic) {
15
- return text.endsWith(logic.ends_with);
16
- }
17
- if ('contains' in logic) {
18
- return text.includes(logic.contains);
19
- }
20
- if ('regex' in logic) {
21
- return new RegExp(logic.regex).test(text);
22
- }
23
- return false;
24
- }
25
- function findProviderById(providers, providerId) {
26
- const normalizedProviderId = providerId.toLowerCase().trim();
27
- const exactMatch = providers.find((p) => p.id === normalizedProviderId);
28
- if (exactMatch)
29
- return exactMatch;
30
- return providers.find((p) => p.provider_match && matchLogic(p.provider_match, normalizedProviderId));
31
- }
32
- export function matchProvider(providers, modelRef, providerId, providerApiUrl) {
33
- if (providerId) {
34
- return findProviderById(providers, providerId);
35
- }
36
- if (providerApiUrl) {
37
- return providers.find((p) => new RegExp(p.api_pattern).test(providerApiUrl));
38
- }
39
- return providers.find((p) => p.model_match && matchLogic(p.model_match, modelRef));
40
- }
41
- export function matchModel(models, modelRef) {
42
- return models.find((m) => matchLogic(m.match, modelRef));
43
- }
@@ -1,3 +0,0 @@
1
- import { Usage, ModelPrice, ModelInfo, CalcPrice } from './types.js';
2
- export declare function calcPrice(usage: Usage, modelPrice: ModelPrice): CalcPrice;
3
- export declare function getActiveModelPrice(model: ModelInfo, timestamp: Date): ModelPrice;
package/dist/priceCalc.js DELETED
@@ -1,90 +0,0 @@
1
- function calcTieredPrice(tiered, tokens) {
2
- if (tokens <= 0)
3
- return 0;
4
- let price = 0;
5
- // Sort tiers by start ascending
6
- const tiers = [...tiered.tiers].sort((a, b) => a.start - b.start);
7
- // Base price for tokens up to the first tier start
8
- const firstTierStart = tiers[0]?.start ?? tokens;
9
- const baseTokens = Math.min(tokens, firstTierStart);
10
- price += (baseTokens * tiered.base) / 1_000_000;
11
- // Price for each tier
12
- for (let i = 0; i < tiers.length; i++) {
13
- const tier = tiers[i];
14
- const nextTierStart = tiers[i + 1]?.start ?? Infinity;
15
- // Tokens in this tier: from tier.start up to nextTierStart or tokens
16
- const tierTokenCount = Math.max(0, Math.min(tokens, nextTierStart) - tier.start);
17
- if (tierTokenCount > 0) {
18
- price += (tierTokenCount * tier.price) / 1_000_000;
19
- }
20
- }
21
- return price;
22
- }
23
- function calcMtokPrice(price, tokens, _field) {
24
- if (price === undefined || tokens === undefined)
25
- return 0;
26
- if (typeof price === 'number') {
27
- return (price * tokens) / 1_000_000;
28
- }
29
- return calcTieredPrice(price, tokens);
30
- }
31
- export function calcPrice(usage, modelPrice) {
32
- let input_price = 0;
33
- let output_price = 0;
34
- // Input-related prices
35
- input_price += calcMtokPrice(modelPrice.input_mtok, usage.input_tokens, 'input_mtok');
36
- input_price += calcMtokPrice(modelPrice.cache_write_mtok, usage.cache_write_tokens, 'cache_write_mtok');
37
- input_price += calcMtokPrice(modelPrice.cache_read_mtok, usage.cache_read_tokens, 'cache_read_mtok');
38
- input_price += calcMtokPrice(modelPrice.input_audio_mtok, usage.input_audio_tokens, 'input_audio_mtok');
39
- input_price += calcMtokPrice(modelPrice.cache_audio_read_mtok, usage.cache_audio_read_tokens, 'cache_audio_read_mtok');
40
- // Output-related prices
41
- output_price += calcMtokPrice(modelPrice.output_mtok, usage.output_tokens, 'output_mtok');
42
- output_price += calcMtokPrice(modelPrice.output_audio_mtok, usage.output_audio_tokens, 'output_audio_mtok');
43
- // Requests price (counted as input cost)
44
- if (modelPrice.requests_kcount !== undefined) {
45
- input_price += modelPrice.requests_kcount / 1000;
46
- }
47
- const total_price = input_price + output_price;
48
- return {
49
- input_price,
50
- output_price,
51
- total_price,
52
- };
53
- }
54
- export function getActiveModelPrice(model, timestamp) {
55
- if (!Array.isArray(model.prices)) {
56
- return model.prices;
57
- }
58
- // Conditional prices: last active wins
59
- for (let i = model.prices.length - 1; i >= 0; i--) {
60
- const cond = model.prices[i];
61
- if (!cond.constraint)
62
- return cond.prices;
63
- if (cond.constraint.type === 'start_date') {
64
- if (timestamp >= new Date(cond.constraint.start_date)) {
65
- return cond.prices;
66
- }
67
- }
68
- else if (cond.constraint.type === 'time_of_date') {
69
- // Extract UTC time to match constraint times which are in UTC (with 'Z' suffix)
70
- const t = timestamp.toISOString().slice(11, 19); // Get "HH:MM:SS" from ISO string
71
- const startTime = cond.constraint.start_time;
72
- const endTime = cond.constraint.end_time;
73
- // Handle time ranges that span midnight (end time < start time)
74
- if (endTime < startTime) {
75
- // Time is in range if it's >= start OR < end
76
- if (t >= startTime || t < endTime) {
77
- return cond.prices;
78
- }
79
- }
80
- else {
81
- // Normal time range (start <= time < end)
82
- if (t >= startTime && t < endTime) {
83
- return cond.prices;
84
- }
85
- }
86
- }
87
- }
88
- // Fallback to first
89
- return model.prices[0].prices;
90
- }
@@ -1,8 +0,0 @@
1
- import { Usage, PriceCalculationResult } from '../types.js';
2
- export type { Usage, PriceCalculation, PriceCalculationResult } from '../types.js';
3
- export interface CalcPriceOptions {
4
- providerId?: string;
5
- providerApiUrl?: string;
6
- timestamp?: Date;
7
- }
8
- export declare function calcPriceSync(usage: Usage, modelRef: string, options?: CalcPriceOptions): PriceCalculationResult;
@@ -1,24 +0,0 @@
1
- import { getProvidersSync } from '../dataLoader.js';
2
- import { matchProvider, matchModel } from '../matcher.js';
3
- import { calcPrice as calcModelPrice, getActiveModelPrice } from '../priceCalc.js';
4
- export function calcPriceSync(usage, modelRef, options = {}) {
5
- const providers = getProvidersSync();
6
- const provider = matchProvider(providers, modelRef, options.providerId, options.providerApiUrl);
7
- if (!provider)
8
- return null;
9
- const model = matchModel(provider.models, modelRef);
10
- if (!model)
11
- return null;
12
- const timestamp = options.timestamp || new Date();
13
- const model_price = getActiveModelPrice(model, timestamp);
14
- const priceResult = calcModelPrice(usage, model_price);
15
- return {
16
- input_price: priceResult.input_price,
17
- output_price: priceResult.output_price,
18
- total_price: priceResult.total_price,
19
- provider,
20
- model,
21
- model_price,
22
- auto_update_timestamp: undefined,
23
- };
24
- }
package/dist/types.d.ts DELETED
@@ -1,95 +0,0 @@
1
- export interface Usage {
2
- input_tokens?: number;
3
- cache_write_tokens?: number;
4
- cache_read_tokens?: number;
5
- output_tokens?: number;
6
- input_audio_tokens?: number;
7
- cache_audio_read_tokens?: number;
8
- output_audio_tokens?: number;
9
- }
10
- export interface Tier {
11
- start: number;
12
- price: number;
13
- }
14
- export interface TieredPrices {
15
- base: number;
16
- tiers: Tier[];
17
- }
18
- export interface ModelPrice {
19
- input_mtok?: number | TieredPrices;
20
- cache_write_mtok?: number | TieredPrices;
21
- cache_read_mtok?: number | TieredPrices;
22
- output_mtok?: number | TieredPrices;
23
- input_audio_mtok?: number | TieredPrices;
24
- cache_audio_read_mtok?: number | TieredPrices;
25
- output_audio_mtok?: number | TieredPrices;
26
- requests_kcount?: number;
27
- }
28
- export interface ConditionalPrice {
29
- constraint?: StartDateConstraint | TimeOfDateConstraint;
30
- prices: ModelPrice;
31
- }
32
- export interface StartDateConstraint {
33
- start_date: string;
34
- type: 'start_date';
35
- }
36
- export interface TimeOfDateConstraint {
37
- start_time: string;
38
- end_time: string;
39
- type: 'time_of_date';
40
- }
41
- export type MatchLogic = {
42
- starts_with: string;
43
- } | {
44
- ends_with: string;
45
- } | {
46
- contains: string;
47
- } | {
48
- equals: string;
49
- } | {
50
- regex: string;
51
- } | {
52
- or: MatchLogic[];
53
- } | {
54
- and: MatchLogic[];
55
- };
56
- export interface ModelInfo {
57
- id: string;
58
- match: MatchLogic;
59
- name?: string;
60
- description?: string;
61
- context_window?: number;
62
- price_comments?: string;
63
- prices: ModelPrice | ConditionalPrice[];
64
- }
65
- export interface Provider {
66
- id: string;
67
- name: string;
68
- api_pattern: string;
69
- pricing_urls?: string[];
70
- description?: string;
71
- price_comments?: string;
72
- model_match?: MatchLogic;
73
- provider_match?: MatchLogic;
74
- models: ModelInfo[];
75
- }
76
- export interface CalcPrice {
77
- input_price: number;
78
- output_price: number;
79
- total_price: number;
80
- }
81
- export interface PriceCalculation {
82
- input_price: number;
83
- output_price: number;
84
- total_price: number;
85
- provider: Provider;
86
- model: ModelInfo;
87
- model_price: ModelPrice;
88
- auto_update_timestamp?: string;
89
- }
90
- export type PriceCalculationResult = PriceCalculation | null;
91
- export type PriceDataStorage = {
92
- get: () => Promise<string | null>;
93
- set: (data: string) => Promise<void>;
94
- get_last_modified?: () => Promise<number | null>;
95
- };
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};