use axum::{
extract::{Path, Query, State},
response::Json,
};
use serde::Deserialize;
use crate::ai::{
AgentDiscoveryQuery, AgentDiscoveryResponse, AgentMarketplace, AgentRankingsResponse,
AgentReputation,
};
use crate::error::DbError;
use crate::server::handlers::AppState;
#[derive(Debug, Deserialize)]
pub struct MarketplaceDiscoverQuery {
pub capabilities: Option<String>,
pub agent_type: Option<String>,
pub min_trust_score: Option<f64>,
pub task_type: Option<String>,
}
pub async fn discover_agents_handler(
State(state): State<AppState>,
Path(db_name): Path<String>,
Query(params): Query<MarketplaceDiscoverQuery>,
) -> Result<Json<AgentDiscoveryResponse>, DbError> {
let db = state.storage.get_database(&db_name)?;
let query = AgentDiscoveryQuery {
required_capabilities: params
.capabilities
.map(|c| c.split(',').map(|s| s.trim().to_string()).collect()),
agent_type: params
.agent_type
.and_then(|s| match s.to_lowercase().as_str() {
"analyzer" => Some(crate::ai::AgentType::Analyzer),
"coder" => Some(crate::ai::AgentType::Coder),
"tester" => Some(crate::ai::AgentType::Tester),
"reviewer" => Some(crate::ai::AgentType::Reviewer),
"integrator" => Some(crate::ai::AgentType::Integrator),
_ => None,
}),
min_trust_score: params.min_trust_score,
task_type: params.task_type,
limit: None,
idle_only: None,
};
let agents = AgentMarketplace::discover_agents(&db, &query)?;
let total = agents.len();
Ok(Json(AgentDiscoveryResponse { agents, total }))
}
pub async fn get_agent_reputation_handler(
State(state): State<AppState>,
Path((db_name, agent_id)): Path<(String, String)>,
) -> Result<Json<AgentReputation>, DbError> {
let db = state.storage.get_database(&db_name)?;
let reputation = AgentMarketplace::get_reputation(&db, &agent_id)?;
Ok(Json(reputation))
}
#[derive(Debug, Deserialize)]
pub struct SelectAgentRequest {
pub task_requirements: serde_json::Value,
}
pub async fn select_agent_handler(
State(_state): State<AppState>,
Path(_db_name): Path<String>,
Json(_request): Json<SelectAgentRequest>,
) -> Result<Json<serde_json::Value>, DbError> {
Ok(Json(serde_json::json!({
"status": "success",
"message": "Agent selection submitted for processing"
})))
}
#[derive(Debug, Deserialize)]
pub struct VerifyCapabilityRequest {
pub capability: String,
pub evidence: Option<serde_json::Value>,
}
pub async fn verify_capability_handler(
State(state): State<AppState>,
Path((db_name, agent_id)): Path<(String, String)>,
Json(_request): Json<VerifyCapabilityRequest>,
) -> Result<Json<serde_json::Value>, DbError> {
let _db = state.storage.get_database(&db_name)?;
Ok(Json(serde_json::json!({
"status": "pending",
"message": "Capability verification submitted for review",
"agent_id": agent_id
})))
}
#[derive(Debug, Deserialize)]
pub struct RankingsQuery {
pub limit: Option<usize>,
}
pub async fn get_agent_rankings_handler(
State(state): State<AppState>,
Path(db_name): Path<String>,
Query(params): Query<RankingsQuery>,
) -> Result<Json<AgentRankingsResponse>, DbError> {
let db = state.storage.get_database(&db_name)?;
let rankings = AgentMarketplace::get_rankings(&db, params.limit)?;
let total = rankings.len();
Ok(Json(AgentRankingsResponse { rankings, total }))
}