feat: implement provider credit tracking and balance management
- Added 'credit_balance' and 'low_credit_threshold' to 'provider_configs' table. - Updated dashboard backend to support reading and updating provider credits. - Implemented real-time credit deduction from provider balances on successful requests. - Added visual balance indicators and configuration modal to the 'Providers' dashboard tab.
This commit is contained in:
@@ -56,6 +56,8 @@ impl RequestLogger {
|
||||
|
||||
/// Insert a log entry into the database
|
||||
async fn insert_log(pool: &SqlitePool, log: RequestLog) -> Result<(), sqlx::Error> {
|
||||
let mut tx = pool.begin().await?;
|
||||
|
||||
sqlx::query(
|
||||
r#"
|
||||
INSERT INTO llm_requests
|
||||
@@ -65,7 +67,7 @@ impl RequestLogger {
|
||||
)
|
||||
.bind(log.timestamp)
|
||||
.bind(log.client_id)
|
||||
.bind(log.provider)
|
||||
.bind(&log.provider)
|
||||
.bind(log.model)
|
||||
.bind(log.prompt_tokens as i64)
|
||||
.bind(log.completion_tokens as i64)
|
||||
@@ -77,9 +79,22 @@ impl RequestLogger {
|
||||
.bind(log.duration_ms as i64)
|
||||
.bind(None::<String>) // request_body - TODO: store serialized request
|
||||
.bind(None::<String>) // response_body - TODO: store serialized response or error
|
||||
.execute(pool)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
// Deduct from provider balance if successful
|
||||
if log.cost > 0.0 {
|
||||
sqlx::query(
|
||||
"UPDATE provider_configs SET credit_balance = credit_balance - ? WHERE id = ?"
|
||||
)
|
||||
.bind(log.cost)
|
||||
.bind(&log.provider)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user