|
| 1 | +import { SOLAR_PRICE_CHANGES } from './freebuff-solar-promo' |
| 2 | + |
| 3 | +/** |
| 4 | + * Spans in which a model is free to the user, and whose provider cost must |
| 5 | + * therefore not be charged against that user's daily dollar allowance. |
| 6 | + * |
| 7 | + * ## Why this exists |
| 8 | + * |
| 9 | + * A promotion that makes a row free, while its spend still consumes the one |
| 10 | + * budget deciding whether the account can start anything at all, is an offer |
| 11 | + * working against itself: the user is handed a model at no price and then |
| 12 | + * locked out of the rest of the catalog for the day by using it. Reported by |
| 13 | + * users on Solar Pro 4's 2026-09-05 weekend, which is what this was built for. |
| 14 | + * |
| 15 | + * ## Derived, not written down |
| 16 | + * |
| 17 | + * The windows come from the price schedule the offer is actually made from |
| 18 | + * (`SOLAR_PRICE_CHANGES`), so the span that is free to the user and the span |
| 19 | + * that is exempt from their allowance cannot drift apart. Hand-copying the |
| 20 | + * dates would put the two facts in two files and guarantee that a future |
| 21 | + * extension moves one and not the other. |
| 22 | + * |
| 23 | + * A window opens at every transition to price 0 and closes at the next |
| 24 | + * transition to any other price. An offer left open-ended closes at the end of |
| 25 | + * time, which is deliberate: while the price is zero the exemption holds. |
| 26 | + * |
| 27 | + * ## What it must not be used for |
| 28 | + * |
| 29 | + * ONLY the per-user allowance. `message.cost` is untouched, because we really |
| 30 | + * do pay for these tokens — the ledger, the dashboards and the provider |
| 31 | + * accounting all have to keep saying so. This decides whose budget it comes |
| 32 | + * out of, not whether it was spent. |
| 33 | + */ |
| 34 | +export interface FreePromotionSpendWindow { |
| 35 | + readonly modelId: string |
| 36 | + /** Inclusive ISO instant the model became free. */ |
| 37 | + readonly from: string |
| 38 | + /** Exclusive ISO instant it stopped being free. */ |
| 39 | + readonly to: string |
| 40 | +} |
| 41 | + |
| 42 | +/** Far enough out to mean "still running", and a real date so the SQL |
| 43 | + * comparison stays a plain range check rather than a null branch. */ |
| 44 | +const OPEN_ENDED = '2099-01-01T00:00:00.000Z' |
| 45 | + |
| 46 | +function windowsFromPriceSchedule( |
| 47 | + changes: ReadonlyArray<{ |
| 48 | + readonly at: string |
| 49 | + readonly modelId: string |
| 50 | + readonly price: number |
| 51 | + }>, |
| 52 | +): FreePromotionSpendWindow[] { |
| 53 | + const byModel = new Map<string, typeof changes>() |
| 54 | + for (const change of changes) { |
| 55 | + byModel.set(change.modelId, [ |
| 56 | + ...(byModel.get(change.modelId) ?? []), |
| 57 | + change, |
| 58 | + ]) |
| 59 | + } |
| 60 | + const windows: FreePromotionSpendWindow[] = [] |
| 61 | + for (const [modelId, entries] of byModel) { |
| 62 | + const ordered = [...entries].sort( |
| 63 | + (a, b) => Date.parse(a.at) - Date.parse(b.at), |
| 64 | + ) |
| 65 | + ordered.forEach((change, index) => { |
| 66 | + if (change.price !== 0) return |
| 67 | + // The next transition at a DIFFERENT price closes it. A second free |
| 68 | + // transition (a re-announcement, or an extension) extends rather than |
| 69 | + // splits, so a window is never cut short by a restatement of itself. |
| 70 | + const end = ordered |
| 71 | + .slice(index + 1) |
| 72 | + .find((later) => later.price !== 0) |
| 73 | + const from = new Date(Date.parse(change.at)).toISOString() |
| 74 | + const to = end ? new Date(Date.parse(end.at)).toISOString() : OPEN_ENDED |
| 75 | + // Merge into the previous window when this free transition sits inside |
| 76 | + // one already open, so the restatement case emits one span. |
| 77 | + const previous = windows[windows.length - 1] |
| 78 | + if ( |
| 79 | + previous && |
| 80 | + previous.modelId === modelId && |
| 81 | + Date.parse(from) <= Date.parse(previous.to) |
| 82 | + ) { |
| 83 | + windows[windows.length - 1] = { |
| 84 | + modelId, |
| 85 | + from: previous.from, |
| 86 | + to: Date.parse(to) > Date.parse(previous.to) ? to : previous.to, |
| 87 | + } |
| 88 | + return |
| 89 | + } |
| 90 | + windows.push({ modelId, from, to }) |
| 91 | + }) |
| 92 | + } |
| 93 | + return windows |
| 94 | +} |
| 95 | + |
| 96 | +export const FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS: readonly FreePromotionSpendWindow[] = |
| 97 | + Object.freeze(windowsFromPriceSchedule(SOLAR_PRICE_CHANGES)) |
| 98 | + |
| 99 | +/** Whether `modelId` was free to the user at `at` — the same question the SQL |
| 100 | + * predicate asks, for callers that have a row rather than a query. */ |
| 101 | +export function isFreePromotionSpendAt( |
| 102 | + modelId: string, |
| 103 | + at: number | Date, |
| 104 | +): boolean { |
| 105 | + const t = typeof at === 'number' ? at : at.getTime() |
| 106 | + return FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS.some( |
| 107 | + (window) => |
| 108 | + window.modelId === modelId && |
| 109 | + t >= Date.parse(window.from) && |
| 110 | + t < Date.parse(window.to), |
| 111 | + ) |
| 112 | +} |
0 commit comments