Skip to content

desktop/src-tauri/crates/core/src/pricing.rs

Comment 1, line 1

desktop/src-tauri/crates/core/src/pricing.rs:1

//! F2 price snapshots and cost estimates: see the [pricing specification](../../../docs/design/desktop-usage/pricing.md)

Comment 2, line 2

desktop/src-tauri/crates/core/src/pricing.rs:2

//! and [cost calculation rules](../../../docs/design/desktop-usage/data-contract.md#pricing).

Comment 3, line 3

desktop/src-tauri/crates/core/src/pricing.rs:3

//!

Comment 4, line 4

desktop/src-tauri/crates/core/src/pricing.rs:4

//! Rates use hundredths of a minor currency unit per million tokens (i64; $0.075/M = 750).

Comment 5, line 5

desktop/src-tauri/crates/core/src/pricing.rs:5

//! Per-event components use round_half_up(tokens * rate / 100_000_000), with i128 arithmetic.

Comment 6, line 6

desktop/src-tauri/crates/core/src/pricing.rs:6

//! Also retain exact numerators so aggregate callers can sum before rounding; no binary floats.

Comment 7, line 7

desktop/src-tauri/crates/core/src/pricing.rs:7

//!

Comment 8, line 8

desktop/src-tauri/crates/core/src/pricing.rs:8

//! Unknown billing channels permit an unambiguous official API reference for the same model.

Comment 9, line 9

desktop/src-tauri/crates/core/src/pricing.rs:9

//! Do not price reasoning twice, fill unknown tokens with zero, or price anomalous tokens.

Comment 10, line 16

desktop/src-tauri/crates/core/src/pricing.rs:16

/// Repository seed snapshot maintained with releases; imports skip duplicates in storage::pricing.

Comment 11, line 20

desktop/src-tauri/crates/core/src/pricing.rs:20

/// Currencies checked for the seed and current application; verify price references before adding one.

Comment 12, line 23

desktop/src-tauri/crates/core/src/pricing.rs:23

/// Service tiers; automatic estimation selects standard without mixing batch/flex/fast rows.

Comment 13, line 26

desktop/src-tauri/crates/core/src/pricing.rs:26

/// Cache-write TTL tiers in minutes.

Comment 14, line 30

desktop/src-tauri/crates/core/src/pricing.rs:30

/// Denominator converting tokens * hundredths of a minor unit/M tokens to minor units.

Comment 15, line 33

desktop/src-tauri/crates/core/src/pricing.rs:33

// ---------------------------------------------------------------------------

Comment 16, line 34

desktop/src-tauri/crates/core/src/pricing.rs:34

// Snapshot file format.

Comment 17, line 35

desktop/src-tauri/crates/core/src/pricing.rs:35

// ---------------------------------------------------------------------------

Comment 18, line 37

desktop/src-tauri/crates/core/src/pricing.rs:37

/// Snapshot JSON format shared by seed and manually imported files.

Comment 19, line 48

desktop/src-tauri/crates/core/src/pricing.rs:48

/// Source type: seed, manual, or community.

Comment 20, line 52

desktop/src-tauri/crates/core/src/pricing.rs:52

/// ISO date in UTC (YYYY-MM-DD).

Comment 21, line 75

desktop/src-tauri/crates/core/src/pricing.rs:75

/// ISO date at UTC midnight (YYYY-MM-DD).

Comment 22, line 92

desktop/src-tauri/crates/core/src/pricing.rs:92

/// Official supplier pay-as-you-go flag (v11). When absent, source type supplies the default:

Comment 23, line 93

desktop/src-tauri/crates/core/src/pricing.rs:93

/// true for seed/community, false for manual.

Comment 24, line 104

desktop/src-tauri/crates/core/src/pricing.rs:104

/// Parsed snapshot passed to storage.

Comment 25, line 128

desktop/src-tauri/crates/core/src/pricing.rs:128

/// Normalize a NULL threshold to zero.

Comment 26, line 130

desktop/src-tauri/crates/core/src/pricing.rs:130

/// Half-open [from, to) interval; None for to means still effective.

Comment 27, line 140

desktop/src-tauri/crates/core/src/pricing.rs:140

/// Official supplier flag identifying fallback candidates when exact matching fails.

Comment 28, line 145

desktop/src-tauri/crates/core/src/pricing.rs:145

/// Convert an ISO date (YYYY-MM-DD) to UTC midnight milliseconds.

Comment 29, line 159

desktop/src-tauri/crates/core/src/pricing.rs:159

/// Stable FNV-1a content hash for duplicate snapshot import checks.

Comment 30, line 170

desktop/src-tauri/crates/core/src/pricing.rs:170

/// Hexadecimal content hash as stored in a TEXT column.

Comment 31, line 175

desktop/src-tauri/crates/core/src/pricing.rs:175

/// Validate and normalize the format, currency/tier enums, nonnegative rates, valid intervals,

Comment 32, line 176

desktop/src-tauri/crates/core/src/pricing.rs:176

/// nonoverlapping intervals for each key, and unique price_id values within the snapshot.

Comment 33, line 310

desktop/src-tauri/crates/core/src/pricing.rs:310

// Same-key intervals are half-open; an open-ended row excludes later rows with that key.

Comment 34, line 342

desktop/src-tauri/crates/core/src/pricing.rs:342

// Content changes require a new snapshot ID for A10 duplicate import checks.

Comment 35, line 389

desktop/src-tauri/crates/core/src/pricing.rs:389

// ---------------------------------------------------------------------------

Comment 36, line 390

desktop/src-tauri/crates/core/src/pricing.rs:390

// Pure estimation functions.

Comment 37, line 391

desktop/src-tauri/crates/core/src/pricing.rs:391

// ---------------------------------------------------------------------------

Comment 38, line 393

desktop/src-tauri/crates/core/src/pricing.rs:393

/// Price-related fields of a model_call or usage_observation event.

Comment 39, line 394

desktop/src-tauri/crates/core/src/pricing.rs:394

/// Callers map fields with quality other than reported/derived to None using quality_json.

Comment 40, line 408

desktop/src-tauri/crates/core/src/pricing.rs:408

/// Prefer exact channel prices; unknown channels require an unambiguous official model reference.

Comment 41, line 411

desktop/src-tauri/crates/core/src/pricing.rs:411

/// Case-folded provider keys mapped to user-selected (region, channel) pairs.

Comment 42, line 413

desktop/src-tauri/crates/core/src/pricing.rs:413

/// Case-folded provider keys mapped to default cache-write TTL tiers (5/60 minutes).

Comment 43, line 417

desktop/src-tauri/crates/core/src/pricing.rs:417

/// All price rows loaded from storage for estimation.

Comment 44, line 423

desktop/src-tauri/crates/core/src/pricing.rs:423

/// Estimate for one event.

Comment 45, line 426

desktop/src-tauri/crates/core/src/pricing.rs:426

/// At least one component was priced; other components may remain unpriced.

Comment 46, line 427

desktop/src-tauri/crates/core/src/pricing.rs:427

/// See known/priced token counts and the component Option values.

Comment 47, line 429

desktop/src-tauri/crates/core/src/pricing.rs:429

/// No component was priced; leave the amount absent instead of writing zero.

Comment 48, line 435

desktop/src-tauri/crates/core/src/pricing.rs:435

/// Exact tokens * rate numerators retained until the reporting scope has been summed.

Comment 49, line 437

desktop/src-tauri/crates/core/src/pricing.rs:437

/// Upper bound for priced components when archived request context tiers are unknown.

Comment 50, line 440

desktop/src-tauri/crates/core/src/pricing.rs:440

/// Component amounts in minor units; None means the component was not priced.

Comment 51, line 445

desktop/src-tauri/crates/core/src/pricing.rs:445

/// Sum only the priced component amounts.

Comment 52, line 447

desktop/src-tauri/crates/core/src/pricing.rs:447

/// Tokens in components that can be priced.

Comment 53, line 449

desktop/src-tauri/crates/core/src/pricing.rs:449

/// Known tokens including unpriced components; coverage is priced/known.

Comment 54, line 451

desktop/src-tauri/crates/core/src/pricing.rs:451

/// Unknown output or uncached input limits the estimate to known components (A2).

Comment 55, line 453

desktop/src-tauri/crates/core/src/pricing.rs:453

/// The event lacks cache-write TTL, so estimation uses the user default.

Comment 56, line 455

desktop/src-tauri/crates/core/src/pricing.rs:455

/// An official pay-as-you-go row supplies a reference after exact provider/model matching fails;

Comment 57, line 456

desktop/src-tauri/crates/core/src/pricing.rs:456

/// see the online price refresh specification in pricing.md.

Comment 58, line 458

desktop/src-tauri/crates/core/src/pricing.rs:458

/// Price model used for an explicitly authorized reference to a different model.

Comment 59, line 460

desktop/src-tauri/crates/core/src/pricing.rs:460

/// IDs of the price rows used in this estimate.

Comment 60, line 466

desktop/src-tauri/crates/core/src/pricing.rs:466

/// The source reported no token components usable for pricing.

Comment 61, line 470

desktop/src-tauri/crates/core/src/pricing.rs:470

/// Official reference channels/regions/currencies remain ambiguous or lack required configuration.

Comment 62, line 472

desktop/src-tauri/crates/core/src/pricing.rs:472

/// No applicable price row, including events before its effective start.

Comment 63, line 474

desktop/src-tauri/crates/core/src/pricing.rs:474

/// Separator normalization found different catalog IDs without an exact match.

Comment 64, line 476

desktop/src-tauri/crates/core/src/pricing.rs:476

/// Multiple context tiers exist but input size is unknown; leave the tier unselected.

Comment 65, line 478

desktop/src-tauri/crates/core/src/pricing.rs:478

/// Negative tokens or cache tokens exceeding known total input prevent pricing.

Comment 66, line 480

desktop/src-tauri/crates/core/src/pricing.rs:480

/// Intermediate overflow rejects pricing for this event and records a diagnostic.

Comment 67, line 500

desktop/src-tauri/crates/core/src/pricing.rs:500

/// Price one component: tokens * hundredths of a minor unit/M tokens / 1e8, rounded half up.

Comment 68, line 512

desktop/src-tauri/crates/core/src/pricing.rs:512

/// Component slot pairing an amount column with its token field.

Comment 69, line 538

desktop/src-tauri/crates/core/src/pricing.rs:538

/// Filter once per model, retaining all intervals, tiers, and snapshot priorities.

Comment 70, line 569

desktop/src-tauri/crates/core/src/pricing.rs:569

/// Estimate using the price interval containing the event occurred_at timestamp.

Comment 71, line 578

desktop/src-tauri/crates/core/src/pricing.rs:578

/// Estimate at at_ms; passing now selects the current price reference.

Comment 72, line 588

desktop/src-tauri/crates/core/src/pricing.rs:588

/// Archived sums do not identify individual request context tiers. Return component bounds

Comment 73, line 589

desktop/src-tauri/crates/core/src/pricing.rs:589

/// within one selected tariff; do not select a tier from a whole day's input sum.

Comment 74, line 630

desktop/src-tauri/crates/core/src/pricing.rs:630

// Reject negative tokens or cache exceeding known total input; do not replace them with zero.

Comment 75, line 668

desktop/src-tauri/crates/core/src/pricing.rs:668

// Select intervals where from <= at_ms < to; NULL to means open-ended.

Comment 76, line 694

desktop/src-tauri/crates/core/src/pricing.rs:694

// Unknown billing channels/providers can still show a reference without inferring actual billing.

Comment 77, line 695

desktop/src-tauri/crates/core/src/pricing.rs:695

// Vendor families restrict fallback; the model release must still match exactly.

Comment 78, line 716

desktop/src-tauri/crates/core/src/pricing.rs:716

// Reuse channel, currency, and tier checks; only official rows for this explicitly

Comment 79, line 717

desktop/src-tauri/crates/core/src/pricing.rs:717

// authorized substitute model may participate.

Comment 80, line 796

desktop/src-tauri/crates/core/src/pricing.rs:796

// Select one vendor/channel before its context tier; mixing vendors or currencies

Comment 81, line 797

desktop/src-tauri/crates/core/src/pricing.rs:797

// could otherwise select an unrelated high tier.

Comment 82, line 813

desktop/src-tauri/crates/core/src/pricing.rs:813

// Resolve catalog spelling before context tiers; a higher tier under a different

Comment 83, line 814

desktop/src-tauri/crates/core/src/pricing.rs:814

// spelling must not override an exact model ID.

Comment 84, line 838

desktop/src-tauri/crates/core/src/pricing.rs:838

// Take all tariff tiers from one prioritized snapshot; mixing a new base rate

Comment 85, line 839

desktop/src-tauri/crates/core/src/pricing.rs:839

// with an older long-context rate would invent a tariff.

Comment 86, line 875

desktop/src-tauri/crates/core/src/pricing.rs:875

// Even a lone high-tier row requires input at or above its threshold.

Comment 87, line 876

desktop/src-tauri/crates/core/src/pricing.rs:876

// Unknown input size cannot select among tiers by price-book order.

Comment 88, line 897

desktop/src-tauri/crates/core/src/pricing.rs:897

// Candidates with the same tier are already ordered by snapshot priority.

Comment 89, line 908

desktop/src-tauri/crates/core/src/pricing.rs:908

/// Price components after selecting a price row.

Comment 90, line 915

desktop/src-tauri/crates/core/src/pricing.rs:915

// Use the event provider's configured TTL default even when the fallback provider differs.

Comment 91, line 921

desktop/src-tauri/crates/core/src/pricing.rs:921

// Prefer explicit uncached input; otherwise derive total - read - write only if all are known.

Comment 92, line 966

desktop/src-tauri/crates/core/src/pricing.rs:966

// The component is unpriced because its rate is absent.

Comment 93, line 1005

desktop/src-tauri/crates/core/src/pricing.rs:1005

// Use a user-selected TTL when the event lacks one; without a default, leave writes unpriced.

Comment 94, line 1014

desktop/src-tauri/crates/core/src/pricing.rs:1014

// Known zero writes require no TTL choice and incur no write amount.

Comment 95, line 1041

desktop/src-tauri/crates/core/src/pricing.rs:1041

// No default TTL or no rate for that tier leaves writes unpriced.

Comment 96, line 1055

desktop/src-tauri/crates/core/src/pricing.rs:1055

// With no priced components, return an unpriced event instead of a zero amount.

Comment 97, line 1069

desktop/src-tauri/crates/core/src/pricing.rs:1069

/// Parse snapshot JSON from a repository seed or a user file.

Comment 98, line 1109

desktop/src-tauri/crates/core/src/pricing.rs:1109

// The seed retains both nonoverlapping glm-5.1 context tiers.

Comment 99, line 1122

desktop/src-tauri/crates/core/src/pricing.rs:1122

// Adjacent same-key intervals do not overlap; equal starts do.

Comment 100, line 1132

desktop/src-tauri/crates/core/src/pricing.rs:1132

// $10/M = 100000 hundredths of a cent; 1 * 100000 / 1e8 = 0.001 cent, rounded to 0.

Comment 101, line 1134

desktop/src-tauri/crates/core/src/pricing.rs:1134

// E1 input: 1,234,567 * 80000 / 1e8 = 987.6536, rounded to 988.

Comment 102, line 1136

desktop/src-tauri/crates/core/src/pricing.rs:1136

// E6 output: 1,000 * 280000 / 1e8 = 2.8, rounded half up to 3.

Comment 103, line 1138

desktop/src-tauri/crates/core/src/pricing.rs:1138

// E6 input: 32,768 * 80000 / 1e8 = 26.2144, rounded to 26.

Comment 104, line 1169

desktop/src-tauri/crates/core/src/pricing.rs:1169

// Configuring a channel enables pricing; a small known amount can round to zero.

Comment 105, line 1192

desktop/src-tauri/crates/core/src/pricing.rs:1192

// Cache sum 110 exceeds total input 100.

Comment 106, line 1204

desktop/src-tauri/crates/core/src/pricing.rs:1204

// P5/P6 rates: glm-5.1 [0,32K) at CNY 6/24 and [32K,+inf) at CNY 8/28 per M tokens.

Comment 107, line 1232

desktop/src-tauri/crates/core/src/pricing.rs:1232

// Unknown input size leaves the context tier unselected.

Comment 108, line 1237

desktop/src-tauri/crates/core/src/pricing.rs:1237

// E6: input 32,768 selects the high tier; 26 + 3 = 29 minor units.

Comment 109, line 1253

desktop/src-tauri/crates/core/src/pricing.rs:1253

// E7: input 32,767 selects the low tier; 20 + 2 = 22 minor units.

Comment 110, line 1310

desktop/src-tauri/crates/core/src/pricing.rs:1310

// Synthetic rates: $0.10/$0.01/$0.03/$0.50 per M tokens (1000/100/300/5000 hundredths of a cent).

Comment 111, line 1331

desktop/src-tauri/crates/core/src/pricing.rs:1331

// No default TTL leaves writes unpriced; input/output still total 10 + 50 = 60 cents.

Comment 112, line 1340

desktop/src-tauri/crates/core/src/pricing.rs:1340

// Default 5m TTL prices writes at 1M * 300 / 1e8 = 3 cents and sets defaulted.

Comment 113, line 1366

desktop/src-tauri/crates/core/src/pricing.rs:1366

// No explicit uncached input: total 3_000_000 - read 1_000_000 - write 1_000_000 = 1M.

Comment 114, line 1381

desktop/src-tauri/crates/core/src/pricing.rs:1381

// Divide each 1M * rate product by 1e8: 10 + 1 + 3 + 50 = 64 cents.

Comment 115, line 1400

desktop/src-tauri/crates/core/src/pricing.rs:1400

// Year 2001 precedes the effective start 2026-01-01.

Comment 116, line 1411

desktop/src-tauri/crates/core/src/pricing.rs:1411

// ------------------------------------------------------------------

Comment 117, line 1412

desktop/src-tauri/crates/core/src/pricing.rs:1412

// Official supplier fallback matching; see the online price refresh specification.

Comment 118, line 1413

desktop/src-tauri/crates/core/src/pricing.rs:1413

// ------------------------------------------------------------------

Comment 119, line 1415

desktop/src-tauri/crates/core/src/pricing.rs:1415

/// Row JSON with official_vendor; seed/community default true and manual defaults false.

Comment 120, line 1428

desktop/src-tauri/crates/core/src/pricing.rs:1428

// Synthetic community snapshot: vendorA/m-one with the default official flag true.

Comment 121, line 1455

desktop/src-tauri/crates/core/src/pricing.rs:1455

// relay-x has no exact price row; select the flagged vendorA reference and record fallback.

Comment 122, line 1463

desktop/src-tauri/crates/core/src/pricing.rs:1463

// 1M * $10/M + 1M * $50/M = $60.

Comment 123, line 1468

desktop/src-tauri/crates/core/src/pricing.rs:1468

// Exact provider vendorA matches the same row without a fallback flag.

Comment 124, line 1482

desktop/src-tauri/crates/core/src/pricing.rs:1482

// An unambiguous official reference needs no channel configuration or actual billing inference.

Comment 125, line 1491

desktop/src-tauri/crates/core/src/pricing.rs:1491

// A model with no official row returns no_price_row.

Comment 126, line 1500

desktop/src-tauri/crates/core/src/pricing.rs:1500

// Manual rows default to nonofficial and are excluded from fallback candidates.

Comment 127, line 1516

desktop/src-tauri/crates/core/src/pricing.rs:1516

// Explicit official_vendor: true allows a manual row to participate in fallback.

Comment 128, line 1535

desktop/src-tauri/crates/core/src/pricing.rs:1535

// Prefer the official row with region/channel matching the configured values.

Comment 129, line 1573

desktop/src-tauri/crates/core/src/pricing.rs:1573

// Fallback also leaves unknown request tiers unselected when two context tiers exist.