/* =========================================================================
pricing.js — fetch /api/pricing, render the rate table + cost estimator
• populates the model dropdown for the calculator
• recomputes credits on any input change
• live filter on the rate table
========================================================================= */
(function () {
'use strict';
var API = '/api/pricing';
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function esc(s) {
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
});
}
function fmtPrice(n) {
if (n == null) return '—';
if (n < 0.01) return '$' + n.toFixed(4);
if (n < 1) return '$' + n.toFixed(3);
return '$' + n.toFixed(2);
}
function fmtCredits(n) {
return Number(n || 0).toLocaleString();
}
// strip the leading "free:" or "anthropic/" prefix for a friendlier display,
// but keep the full id as a title tooltip
function shortName(full) {
var n = full.replace(/^free:/, '');
var slash = n.indexOf('/');
return slash >= 0 ? n.slice(slash + 1) : n;
}
function providerOf(full) {
var n = full.replace(/^free:/, '');
var slash = n.indexOf('/');
return slash >= 0 ? n.slice(0, slash) : '';
}
var STATE = { models: [], rate: null };
async function load() {
var body = document.getElementById('rates-body');
try {
var res = await fetch(API);
if (!res.ok) throw new Error('status ' + res.status);
var data = await res.json();
STATE.models = data.models || [];
STATE.rate = data.rate || { credits_per_usd: 10000, input_ratio: 1 / 3 };
renderTable(STATE.models);
populateCalculator(STATE.models);
} catch (e) {
if (body) body.innerHTML = '
Couldn\u2019t load rates. Please try again.
';
}
}
function renderTable(models) {
var body = document.getElementById('rates-body');
if (!body) return;
if (!models.length) {
body.innerHTML = '
No models match your filter.
';
return;
}
var html = '';
for (var i = 0; i < models.length; i++) {
var m = models[i];
var prov = providerOf(m.model);
var badge = m.free ? 'free' : '';
html +=
'