use std::sync::Arc;
use cordis::Context;
#[path = "v1/chat.rs"] pub mod chat;
#[path = "v1/stream.rs"] pub mod stream;
#[path = "v1/agents.rs"] pub mod agents;
#[path = "v1/shared.rs"] pub mod shared;
pub use shared::*;
pub use chat::*;
pub use stream::*;
pub use agents::*;
#[derive(Debug, Serialize)]
pub struct V1Agent {
pub id: String,
pub name: String,
pub agent_type: String,
pub status: V1AgentStatus,
pub config: serde_json::Value,
pub created_at: DateTime<Utc>,
pub last_run: Option<DateTime<Utc>>,
pub total_runs: u64,
pub success_rate: f64,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum V1AgentStatus {
Active,
Idle,
Error,
Disabled,
}
impl From<TenantAgent> for V1Agent {
fn from(a: TenantAgent) -> Self {
let status = if a.enabled {
V1AgentStatus::Active
} else {
V1AgentStatus::Disabled
};
Self {
id: a.id,
name: a.agent_name,
agent_type: "custom".to_string(),
status,
config: a.config,
created_at: shared::ts_to_dt(a.created_at),
last_run: None,
total_runs: 0,
success_rate: 0.0,
}
}
}
#[derive(Debug, Serialize)]
pub struct V1AgentRun {
pub id: String,
pub agent_id: String,
pub status: String,
pub input: serde_json::Value,
pub output: Option<serde_json::Value>,
pub error: Option<String>,
pub started_at: DateTime<Utc>,
pub finished_at: Option<DateTime<Utc>>,
pub duration_ms: Option<u64>,
pub tokens_used: Option<u64>,
}
#[derive(Debug, Serialize)]
pub struct V1AgentLog {
pub id: String,
pub agent_id: String,
pub run_id: Option<String>,
pub level: String,
pub message: String,
pub metadata: Option<serde_json::Value>,
pub timestamp: DateTime<Utc>,
}
#[derive(Debug, Serialize)]
pub struct Paginated<T> {
pub items: Vec<T>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
}
impl<T> Paginated<T> {
pub fn empty(page: u32, per_page: u32) -> Self {
Self {
items: vec![],
total: 0,
page,
per_page,
total_pages: 0,
}
}
}
#[derive(Debug, Serialize)]
pub struct V1Usage {
pub period_start: DateTime<Utc>,
pub period_end: DateTime<Utc>,
pub total_runs: u64,
pub total_tokens: u64,
pub total_api_calls: u64,
pub quota_runs: Option<u64>,
pub quota_tokens: Option<u64>,
pub daily_usage: Vec<DailyUsage>,
}
#[derive(Debug, Serialize)]
pub struct DailyUsage {
pub date: String,
pub runs: u64,
pub tokens: u64,
pub api_calls: u64,
}
#[derive(Debug, Serialize)]
pub struct V1ApiKey {
pub id: String,
pub name: String,
pub prefix: String,
pub created_at: DateTime<Utc>,
pub last_used: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Deserialize)]
pub struct CreateApiKeyRequest {
pub name: String,
pub expires_in_days: Option<u32>,
}
#[derive(Debug, Serialize)]
pub struct CreateApiKeyResponse {
pub key: V1ApiKey,
pub secret: String,
}
#[derive(Debug, Deserialize)]
pub struct PaginationQuery {
pub page: Option<u32>,
pub per_page: Option<u32>,
}
pub fn v1_routes() -> axum::Router<Arc<Context>> {
axum::Router::new()
.merge(chat::routes())
.merge(stream::routes())
.merge(agents::routes())
}