feat: add cache token tracking and cache-aware cost calculation
Some checks failed
CI / Check (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Formatting (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Release Build (push) Has been cancelled

Track cache_read_tokens and cache_write_tokens end-to-end: parse from
provider responses (OpenAI, DeepSeek, Grok, Gemini), persist to SQLite,
apply cache-aware pricing from the model registry, and surface in API
responses and the dashboard.

- Add cache fields to ProviderResponse, StreamUsage, RequestLog structs
- Parse cached_tokens (OpenAI/Grok), prompt_cache_hit/miss (DeepSeek),
  cachedContentTokenCount (Gemini) from provider responses
- Send stream_options.include_usage for streaming; capture real usage
  from final SSE chunk in AggregatingStream
- ALTER TABLE migration for cache_read_tokens/cache_write_tokens columns
- Cache-aware cost formula using registry cache_read/cache_write rates
- Update Provider trait calculate_cost signature across all providers
- Add cache_read_tokens/cache_write_tokens to Usage API response
- Dashboard: cache hit rate card, cache columns in pricing and usage
  tables, cache token aggregation in SQL queries
- Remove API debug panel and verbose console logging from api.js
- Bump static asset cache-bust to v5
This commit is contained in:
2026-03-02 14:45:21 -05:00
parent 232f092f27
commit db5824f0fb
19 changed files with 352 additions and 109 deletions

View File

@@ -17,61 +17,25 @@ class ApiClient {
headers['Authorization'] = `Bearer ${window.authManager.token}`;
}
const response = await fetch(url, {
...options,
headers
});
const text = await response.text();
let result;
try {
console.log(`[API] Fetching ${url}...`);
const response = await fetch(url, {
...options,
headers
});
console.log(`[API] ${url} → status=${response.status} ok=${response.ok} type=${response.headers.get('content-type')}`);
const text = await response.text();
console.log(`[API] ${url} → body length=${text.length}, first 200 chars:`, text.substring(0, 200));
let result;
try {
result = JSON.parse(text);
} catch (parseErr) {
const msg = `JSON parse failed for ${url}: ${parseErr.message}. Body: ${text.substring(0, 300)}`;
console.error(`[API] ${msg}`);
this._addDebugEntry(url, 'JSON_PARSE_ERROR', msg);
throw new Error(msg);
}
if (!response.ok || !result.success) {
const msg = `API error for ${url}: ok=${response.ok} success=${result.success} error=${result.error} status=${response.status}`;
console.error(`[API] ${msg}`);
this._addDebugEntry(url, 'API_ERROR', msg);
throw new Error(result.error || `HTTP error! status: ${response.status}`);
}
console.log(`[API] ${url} → SUCCESS, data keys:`, result.data ? Object.keys(result.data) : 'null');
return result.data;
} catch (error) {
console.error(`[API] Request failed (${path}):`, error);
this._addDebugEntry(url, 'EXCEPTION', error.message);
throw error;
result = JSON.parse(text);
} catch (parseErr) {
throw new Error(`JSON parse failed for ${url}: ${parseErr.message}`);
}
}
// Visible on-page debug panel for diagnosing fetch failures
_addDebugEntry(url, status, detail) {
let panel = document.getElementById('api-debug-panel');
if (!panel) {
panel = document.createElement('div');
panel.id = 'api-debug-panel';
panel.style.cssText = 'position:fixed;bottom:0;left:0;right:0;max-height:200px;overflow-y:auto;background:#1d2021;color:#fbf1c7;font-family:monospace;font-size:11px;padding:8px;z-index:99999;border-top:2px solid #cc241d;';
const title = document.createElement('div');
title.style.cssText = 'font-weight:bold;margin-bottom:4px;color:#fb4934;';
title.textContent = 'API Debug Panel (remove after fixing)';
panel.appendChild(title);
document.body.appendChild(panel);
if (!response.ok || !result.success) {
throw new Error(result.error || `HTTP error! status: ${response.status}`);
}
const entry = document.createElement('div');
entry.style.cssText = 'margin:2px 0;padding:2px 4px;background:#282828;border-left:3px solid ' + (status === 'EXCEPTION' ? '#fb4934' : '#fabd2f') + ';';
entry.textContent = `[${status}] ${url}: ${detail}`;
panel.appendChild(entry);
return result.data;
}
async get(path) {