@@ -40,7 +40,12 @@ import type { |
40 | 40 | UsageRecord, |
41 | 41 | } from "@t3tools/provider-core/server/usage"; |
42 | 42 | import { canonicalModelFilter, resolveModelAliases, UsageAggregator } from "./usageAggregation.ts"; |
43 | | -import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts"; |
| 43 | +import { |
| 44 | + createOverrideRateTable, |
| 45 | + isUnlistedModel, |
| 46 | + parseRateTable, |
| 47 | + type RateTable, |
| 48 | +} from "./usagePricing.ts"; |
44 | 49 | import { |
45 | 50 | DESKTOP_HOST_SCOPE_ENV, |
46 | 51 | listTranscriptFiles, |
@@ -312,6 +317,24 @@ export const make = Effect.gen(function* () { |
312 | 317 |
|
313 | 318 | const ensureRates = (force: boolean) => ratesLock.withPermit(loadRates(force)); |
314 | 319 |
|
| 320 | + // Rates are kept for RATES_TTL_MS, so a model released after the table was fetched would stay unpriced |
| 321 | + // that long. A scan that meets a model the table does not list refetches early: once per model against |
| 322 | + // a fresh table, with loadRates keeping RATES_REFRESH_FLOOR_MS between attempts. |
| 323 | + const checkedUnlistedModels = new Set<string>(); |
| 324 | + const refreshForUnlistedModels = Effect.fn("UsageService.refreshForUnlistedModels")(function* ( |
| 325 | + models: ReadonlySet<string>, |
| 326 | + ) { |
| 327 | + const unlisted = [...models].filter( |
| 328 | + (model) => !checkedUnlistedModels.has(model) && isUnlistedModel(rates, model), |
| 329 | + ); |
| 330 | + if (unlisted.length === 0) return; |
| 331 | + yield* ensureRates(true); |
| 332 | + const now = yield* Clock.currentTimeMillis; |
| 333 | + // A failed fetch leaves the models unchecked, so a later scan tries again. |
| 334 | + if (ratesFetchedAtMs === null || now - ratesFetchedAtMs >= RATES_REFRESH_FLOOR_MS) return; |
| 335 | + for (const model of unlisted) checkedUnlistedModels.add(model); |
| 336 | + }); |
| 337 | + |
315 | 338 | const refreshRates = ensureRates(true).pipe( |
316 | 339 | Effect.map(pricing), |
317 | 340 | Effect.withSpan("UsageService.refreshRates"), |
@@ -881,19 +904,8 @@ export const make = Effect.gen(function* () { |
881 | 904 | { concurrency: 2 }, |
882 | 905 | ); |
883 | 906 |
|
884 | | - const summaryPricing = pricing(); |
885 | | - const aggregator = new UsageAggregator({ |
886 | | - timeZone: input.timeZone, |
887 | | - sinceDay: input.sinceDay, |
888 | | - untilDay: input.untilDay, |
889 | | - resolution: input.resolution ?? "day", |
890 | | - ...hourlyWindow, |
891 | | - rates, |
892 | | - priceOverrides: createOverrideRateTable(settings.usagePriceOverrides), |
893 | | - modelAliases: resolveModelAliases(settings.usageModelAliases), |
894 | | - ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }), |
895 | | - }); |
896 | | - |
| 907 | + const priceOverrides = createOverrideRateTable(settings.usagePriceOverrides); |
| 908 | + const modelAliases = resolveModelAliases(settings.usageModelAliases); |
897 | 909 | const sources: UsageSource[] = []; |
898 | 910 |
|
899 | 911 | // Deleted transcripts' saved usage still aggregates and deduplicates normally. |
@@ -959,6 +971,36 @@ export const make = Effect.gen(function* () { |
959 | 971 | for (const filePath of ordered) fileCache.set(filePath, entries.get(filePath)!); |
960 | 972 | cacheDirty = true; |
961 | 973 | } |
| 974 | + |
| 975 | + // The models priced below, as UsageAggregator resolves them: an alias replaces rateModel. |
| 976 | + const rateModels = new Set<string>(); |
| 977 | + for (const files of filesByDir) { |
| 978 | + for (const file of files) { |
| 979 | + for (const record of file.records) { |
| 980 | + const alias = modelAliases.get(record.model); |
| 981 | + const model = alias ?? record.model; |
| 982 | + if (priceOverrides.has(model.trim())) continue; |
| 983 | + rateModels.add(alias === undefined ? (record.rateModel ?? model) : model); |
| 984 | + } |
| 985 | + } |
| 986 | + } |
| 987 | + // A saved answer prices with saved rates and the full scan after it refetches. |
| 988 | + if (!fromMemory) yield* refreshForUnlistedModels(rateModels); |
| 989 | + |
| 990 | + const summaryPricing = pricing(); |
| 991 | + |
| 992 | + const aggregator = new UsageAggregator({ |
| 993 | + timeZone: input.timeZone, |
| 994 | + sinceDay: input.sinceDay, |
| 995 | + untilDay: input.untilDay, |
| 996 | + resolution: input.resolution ?? "day", |
| 997 | + ...hourlyWindow, |
| 998 | + rates, |
| 999 | + priceOverrides, |
| 1000 | + modelAliases, |
| 1001 | + ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }), |
| 1002 | + }); |
| 1003 | + |
962 | 1004 | const windowEndMs = |
963 | 1005 | hourlyWindow?.untilTimeMs ?? |
964 | 1006 | Date.parse(`${input.untilDay}T00:00:00Z`) + 86400000 + MTIME_SLACK_MS; |
|