feat(billing): add billing_mode to providers (postpaid support) & UI/migration
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

This commit is contained in:
2026-03-03 15:37:19 -05:00
parent 75b68521c8
commit 1453e64d4b
4 changed files with 77 additions and 35 deletions

View File

@@ -0,0 +1,13 @@
-- Migration: add billing_mode to provider_configs
-- Adds a billing_mode TEXT column with default 'prepaid'
-- After applying, set Gemini to postpaid with:
-- UPDATE provider_configs SET billing_mode = 'postpaid' WHERE id = 'gemini';
BEGIN TRANSACTION;
ALTER TABLE provider_configs ADD COLUMN billing_mode TEXT DEFAULT 'prepaid';
COMMIT;
-- NOTE: If you use a production SQLite file, run the following to set Gemini to postpaid:
-- sqlite3 /path/to/db.sqlite "UPDATE provider_configs SET billing_mode='postpaid' WHERE id='gemini';"

View File

@@ -17,6 +17,7 @@ pub(super) struct UpdateProviderRequest {
pub(super) api_key: Option<String>,
pub(super) credit_balance: Option<f64>,
pub(super) low_credit_threshold: Option<f64>,
pub(super) billing_mode: Option<String>,
}
pub(super) async fn handle_get_providers(State(state): State<DashboardState>) -> Json<ApiResponse<serde_json::Value>> {
@@ -24,9 +25,10 @@ pub(super) async fn handle_get_providers(State(state): State<DashboardState>) ->
let config = &state.app_state.config;
let pool = &state.app_state.db_pool;
// Load all overrides from database
let db_configs_result =
sqlx::query("SELECT id, enabled, base_url, credit_balance, low_credit_threshold FROM provider_configs")
// Load all overrides from database (including billing_mode)
let db_configs_result = sqlx::query(
"SELECT id, enabled, base_url, credit_balance, low_credit_threshold, billing_mode FROM provider_configs",
)
.fetch_all(pool)
.await;
@@ -38,7 +40,8 @@ pub(super) async fn handle_get_providers(State(state): State<DashboardState>) ->
let base_url: Option<String> = row.get("base_url");
let balance: f64 = row.get("credit_balance");
let threshold: f64 = row.get("low_credit_threshold");
db_configs.insert(id, (enabled, base_url, balance, threshold));
let billing_mode: Option<String> = row.get("billing_mode");
db_configs.insert(id, (enabled, base_url, balance, threshold, billing_mode));
}
}
@@ -80,15 +83,17 @@ pub(super) async fn handle_get_providers(State(state): State<DashboardState>) ->
let mut balance = 0.0;
let mut threshold = 5.0;
let mut billing_mode: Option<String> = None;
// Apply database overrides
if let Some((db_enabled, db_url, db_balance, db_threshold)) = db_configs.get(id) {
if let Some((db_enabled, db_url, db_balance, db_threshold, db_billing)) = db_configs.get(id) {
enabled = *db_enabled;
if let Some(url) = db_url {
base_url = url.clone();
}
balance = *db_balance;
threshold = *db_threshold;
billing_mode = db_billing.clone();
}
// Find models for this provider in registry
@@ -138,6 +143,7 @@ pub(super) async fn handle_get_providers(State(state): State<DashboardState>) ->
"base_url": base_url,
"credit_balance": balance,
"low_credit_threshold": threshold,
"billing_mode": billing_mode,
"last_used": None::<String>,
}));
}
@@ -185,10 +191,11 @@ pub(super) async fn handle_get_provider(
let mut balance = 0.0;
let mut threshold = 5.0;
let mut billing_mode: Option<String> = None;
// Apply database overrides
let db_config = sqlx::query(
"SELECT enabled, base_url, credit_balance, low_credit_threshold FROM provider_configs WHERE id = ?",
"SELECT enabled, base_url, credit_balance, low_credit_threshold, billing_mode FROM provider_configs WHERE id = ?",
)
.bind(&name)
.fetch_optional(pool)
@@ -201,6 +208,7 @@ pub(super) async fn handle_get_provider(
}
balance = row.get::<f64, _>("credit_balance");
threshold = row.get::<f64, _>("low_credit_threshold");
billing_mode = row.get::<Option<String>, _>("billing_mode");
}
// Find models for this provider
@@ -246,6 +254,7 @@ pub(super) async fn handle_get_provider(
"base_url": base_url,
"credit_balance": balance,
"low_credit_threshold": threshold,
"billing_mode": billing_mode,
"last_used": None::<String>,
})))
}
@@ -262,19 +271,20 @@ pub(super) async fn handle_update_provider(
let pool = &state.app_state.db_pool;
// Update or insert into database
// Update or insert into database (include billing_mode)
let result = sqlx::query(
r#"
INSERT INTO provider_configs (id, display_name, enabled, base_url, api_key, credit_balance, low_credit_threshold)
VALUES (?, ?, ?, ?, ?, ?, ?)
INSERT INTO provider_configs (id, display_name, enabled, base_url, api_key, credit_balance, low_credit_threshold, billing_mode)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
enabled = excluded.enabled,
base_url = excluded.base_url,
api_key = COALESCE(excluded.api_key, provider_configs.api_key),
credit_balance = COALESCE(excluded.credit_balance, provider_configs.credit_balance),
low_credit_threshold = COALESCE(excluded.low_credit_threshold, provider_configs.low_credit_threshold),
billing_mode = COALESCE(excluded.billing_mode, provider_configs.billing_mode),
updated_at = CURRENT_TIMESTAMP
"#
"#,
)
.bind(&name)
.bind(name.to_uppercase())
@@ -283,6 +293,7 @@ pub(super) async fn handle_update_provider(
.bind(&payload.api_key)
.bind(payload.credit_balance)
.bind(payload.low_credit_threshold)
.bind(payload.billing_mode)
.execute(pool)
.await;

View File

@@ -100,9 +100,14 @@ impl RequestLogger {
.execute(&mut *tx)
.await?;
// Deduct from provider balance if successful (skip postpaid like Gemini)
if log.cost > 0.0 && log.provider != "gemini" {
sqlx::query("UPDATE provider_configs SET credit_balance = credit_balance - ? WHERE id = ?")
// Deduct from provider balance if successful.
// Providers configured with billing_mode = 'postpaid' will not have their
// credit_balance decremented. Use a conditional UPDATE so we don't need
// a prior SELECT and avoid extra round-trips.
if log.cost > 0.0 {
sqlx::query(
"UPDATE provider_configs SET credit_balance = credit_balance - ? WHERE id = ? AND (billing_mode IS NULL OR billing_mode != 'postpaid')",
)
.bind(log.cost)
.bind(&log.provider)
.execute(&mut *tx)

View File

@@ -70,6 +70,10 @@ class ProvidersPage {
<span>Balance: ${provider.id === 'ollama' ? 'FREE' : window.api.formatCurrency(provider.credit_balance)}</span>
${isLowBalance ? '<i class="fas fa-exclamation-triangle" title="Low Balance"></i>' : ''}
</div>
<div class="meta-item">
<i class="fas fa-exchange-alt"></i>
<span>Billing: ${provider.billing_mode ? provider.billing_mode.toUpperCase() : 'PREPAID'}</span>
</div>
<div class="meta-item">
<i class="fas fa-clock"></i>
<span>Last used: ${provider.last_used ? window.api.formatTimeAgo(provider.last_used) : 'Never'}</span>
@@ -173,6 +177,13 @@ class ProvidersPage {
<input type="number" id="provider-threshold" value="${provider.low_credit_threshold}" step="0.50">
</div>
</div>
<div class="form-control">
<label for="provider-billing-mode">Billing Mode</label>
<select id="provider-billing-mode">
<option value="prepaid" ${!provider.billing_mode || provider.billing_mode === 'prepaid' ? 'selected' : ''}>Prepaid</option>
<option value="postpaid" ${provider.billing_mode === 'postpaid' ? 'selected' : ''}>Postpaid</option>
</select>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" onclick="this.closest('.modal').remove()">Cancel</button>
@@ -189,6 +200,7 @@ class ProvidersPage {
const apiKey = modal.querySelector('#provider-api-key').value;
const balance = parseFloat(modal.querySelector('#provider-balance').value);
const threshold = parseFloat(modal.querySelector('#provider-threshold').value);
const billingMode = modal.querySelector('#provider-billing-mode').value;
try {
await window.api.put(`/providers/${id}`, {
@@ -196,7 +208,8 @@ class ProvidersPage {
base_url: baseUrl || null,
api_key: apiKey || null,
credit_balance: isNaN(balance) ? null : balance,
low_credit_threshold: isNaN(threshold) ? null : threshold
low_credit_threshold: isNaN(threshold) ? null : threshold,
billing_mode: billingMode || null,
});
window.authManager.showToast(`${provider.name} configuration saved`, 'success');