gitdataai/libs/service/agent/billing.rs
ZhenYi c7cee8c344 misc: polish git hooks, billing services, fctool, and API/WebSocket
- git: clean up hook pool worker, commit sync, HTTP rate limiting
- billing: tighten workspace/project/agent billing logic
- fctool: add project boards and issues management tools
- api/ws: minor room WebSocket protocol adjustments
- frontend: add RoomSettingsPanel component
2026-04-30 19:16:57 +08:00

49 lines
1.5 KiB
Rust

//! Billing — delegates to agent crate.
use crate::AppService;
use crate::error::AppError;
use sea_orm::*;
use uuid::Uuid;
impl AppService {
/// Record AI usage against a project or workspace.
///
/// `model_id` is an `ai_model.id`. The active/default model version is resolved
/// internally so callers do not need to distinguish ModelId from ModelVersionId.
pub async fn record_ai_usage(
&self,
project_uid: Uuid,
model_id: Uuid,
input_tokens: i64,
output_tokens: i64,
) -> Result<agent::billing::BillingRecord, AppError> {
use agent::billing::BillingResult;
use models::agents::model_version;
let version_id = model_version::Entity::find()
.filter(model_version::Column::ModelId.eq(model_id))
.filter(model_version::Column::Status.eq("active"))
.order_by_desc(model_version::Column::IsDefault)
.order_by_desc(model_version::Column::ReleaseDate)
.one(&self.db)
.await?
.map(|v| v.id)
.unwrap_or(model_id);
match agent::billing::record_ai_usage(
&self.db,
project_uid,
version_id,
input_tokens,
output_tokens,
)
.await?
{
BillingResult::Success(record) => Ok(record),
BillingResult::InsufficientBalance { message } => {
Err(AppError::BadRequest(message))
}
}
}
}