|
5 | 5 | type ServerSettings as ServerSettingsValue, |
6 | 6 | type UsageProviderKind, |
7 | 7 | type UsageSource, |
| 8 | + type UsagePriceInput, |
| 9 | + type UsagePriceResult, |
8 | 10 | type UsagePricing, |
9 | 11 | type UsageSummary, |
10 | 12 | type UsageSummaryInput, |
@@ -40,7 +42,12 @@ import type { |
40 | 42 | UsageRecord, |
41 | 43 | } from "@t3tools/provider-core/server/usage"; |
42 | 44 | import { canonicalModelFilter, resolveModelAliases, UsageAggregator } from "./usageAggregation.ts"; |
43 | | -import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts"; |
| 45 | +import { |
| 46 | + createOverrideRateTable, |
| 47 | + parseRateTable, |
| 48 | + priceUsage, |
| 49 | + type RateTable, |
| 50 | +} from "./usagePricing.ts"; |
44 | 51 | import { |
45 | 52 | listTranscriptFiles, |
46 | 53 | readDirectoryVolumeId, |
@@ -144,6 +151,7 @@ export class UsageService extends Context.Service< |
144 | 151 | { |
145 | 152 | readonly readSummary: (input: UsageSummaryInput) => Effect.Effect<UsageSummary, UsageReadError>; |
146 | 153 | readonly refreshRates: Effect.Effect<UsagePricing>; |
| 154 | + readonly priceUsage: (input: UsagePriceInput) => Effect.Effect<UsagePriceResult>; |
147 | 155 | } |
148 | 156 | >()("t3/usage/UsageService") {} |
149 | 157 |
|
@@ -935,7 +943,35 @@ export const make = Effect.gen(function* () { |
935 | 943 | return yield* Deferred.await(deferred); |
936 | 944 | }); |
937 | 945 |
|
938 | | - return { readSummary, refreshRates, awaitPersisted } as const; |
| 946 | + const priceUsageItems = Effect.fn("UsageService.priceUsage")(function* (input: UsagePriceInput) { |
| 947 | + const settings = yield* readSettings; |
| 948 | + yield* ensureRates(false); |
| 949 | + const overrides = createOverrideRateTable(settings.usagePriceOverrides); |
| 950 | + const aliases = resolveModelAliases(settings.usageModelAliases); |
| 951 | + return { |
| 952 | + items: input.items.map((item) => { |
| 953 | + const model = aliases.get(item.model) ?? item.model; |
| 954 | + const priced = priceUsage( |
| 955 | + rates, |
| 956 | + { |
| 957 | + model, |
| 958 | + totals: item.totals, |
| 959 | + speed: "standard", |
| 960 | + reportedCostUsd: null, |
| 961 | + }, |
| 962 | + overrides, |
| 963 | + ); |
| 964 | + return { |
| 965 | + id: item.id, |
| 966 | + costUsd: priced.costUsd, |
| 967 | + costSource: priced.costSource, |
| 968 | + }; |
| 969 | + }), |
| 970 | + pricing: pricing(), |
| 971 | + } satisfies UsagePriceResult; |
| 972 | + }); |
| 973 | + |
| 974 | + return { readSummary, refreshRates, priceUsage: priceUsageItems, awaitPersisted } as const; |
939 | 975 | }); |
940 | 976 |
|
941 | 977 | export const layer = Layer.effect(UsageService, make); |
0 commit comments