feat(ai): AnalysisService 核心编排 + PromptService + UsageService
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
45
crates/erp-ai/src/service/usage.rs
Normal file
45
crates/erp-ai/src/service/usage.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use sea_orm::ActiveModelTrait;
|
||||
use sea_orm::Set;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::entity::ai_usage;
|
||||
use crate::error::AiResult;
|
||||
|
||||
pub struct UsageService {
|
||||
pub db: sea_orm::DatabaseConnection,
|
||||
}
|
||||
|
||||
impl UsageService {
|
||||
pub fn new(db: sea_orm::DatabaseConnection) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub async fn log_usage(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
provider: &str,
|
||||
model: &str,
|
||||
analysis_type: &str,
|
||||
input_tokens: u32,
|
||||
output_tokens: u32,
|
||||
duration_ms: u64,
|
||||
cost_cents: i32,
|
||||
is_cache_hit: bool,
|
||||
) -> AiResult<ai_usage::Model> {
|
||||
let id = Uuid::now_v7();
|
||||
let active = ai_usage::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
provider: Set(provider.into()),
|
||||
model: Set(model.into()),
|
||||
analysis_type: Set(analysis_type.into()),
|
||||
input_tokens: Set(input_tokens as i32),
|
||||
output_tokens: Set(output_tokens as i32),
|
||||
duration_ms: Set(duration_ms as i32),
|
||||
cost_cents: Set(cost_cents),
|
||||
is_cache_hit: Set(is_cache_hit),
|
||||
created_at: Set(chrono::Utc::now()),
|
||||
};
|
||||
Ok(active.insert(&self.db).await?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user