chore: 提交所有工作进度 — SaaS 后端增强、Admin UI、桌面端集成
包含大量 SaaS 平台改进、Admin 管理后台更新、桌面端集成完善、 文档同步、测试文件重构等内容。为 QA 测试准备干净工作树。
This commit is contained in:
104
crates/zclaw-saas/src/agent_template/handlers.rs
Normal file
104
crates/zclaw-saas/src/agent_template/handlers.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
//! Agent 配置模板 HTTP 处理器
|
||||
|
||||
use axum::{
|
||||
extract::{Extension, Path, Query, State},
|
||||
Json,
|
||||
};
|
||||
use crate::state::AppState;
|
||||
use crate::error::SaasResult;
|
||||
use crate::auth::types::AuthContext;
|
||||
use crate::auth::handlers::{log_operation, check_permission};
|
||||
use super::types::*;
|
||||
use super::service;
|
||||
|
||||
/// GET /api/v1/agent-templates — 列出 Agent 模板
|
||||
pub async fn list_templates(
|
||||
State(state): State<AppState>,
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Query(query): Query<AgentTemplateListQuery>,
|
||||
) -> SaasResult<Json<crate::common::PaginatedResponse<AgentTemplateInfo>>> {
|
||||
check_permission(&ctx, "model:read")?;
|
||||
Ok(Json(service::list_templates(&state.db, &query).await?))
|
||||
}
|
||||
|
||||
/// POST /api/v1/agent-templates — 创建 Agent 模板
|
||||
pub async fn create_template(
|
||||
State(state): State<AppState>,
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Json(req): Json<CreateAgentTemplateRequest>,
|
||||
) -> SaasResult<Json<AgentTemplateInfo>> {
|
||||
check_permission(&ctx, "model:manage")?;
|
||||
|
||||
let category = req.category.as_deref().unwrap_or("general");
|
||||
let source = req.source.as_deref().unwrap_or("custom");
|
||||
let visibility = req.visibility.as_deref().unwrap_or("public");
|
||||
let tools = req.tools.as_deref().unwrap_or(&[]);
|
||||
let capabilities = req.capabilities.as_deref().unwrap_or(&[]);
|
||||
|
||||
let result = service::create_template(
|
||||
&state.db, &req.name, req.description.as_deref(),
|
||||
category, source, req.model.as_deref(),
|
||||
req.system_prompt.as_deref(),
|
||||
tools, capabilities,
|
||||
req.temperature, req.max_tokens, visibility,
|
||||
).await?;
|
||||
|
||||
log_operation(&state.db, &ctx.account_id, "agent_template.create", "agent_template", &result.id,
|
||||
Some(serde_json::json!({"name": req.name})), ctx.client_ip.as_deref()).await?;
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
/// GET /api/v1/agent-templates/:id — 获取单个 Agent 模板
|
||||
pub async fn get_template(
|
||||
State(state): State<AppState>,
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Path(id): Path<String>,
|
||||
) -> SaasResult<Json<AgentTemplateInfo>> {
|
||||
check_permission(&ctx, "model:read")?;
|
||||
Ok(Json(service::get_template(&state.db, &id).await?))
|
||||
}
|
||||
|
||||
/// POST /api/v1/agent-templates/:id — 更新 Agent 模板
|
||||
pub async fn update_template(
|
||||
State(state): State<AppState>,
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Path(id): Path<String>,
|
||||
Json(req): Json<UpdateAgentTemplateRequest>,
|
||||
) -> SaasResult<Json<AgentTemplateInfo>> {
|
||||
check_permission(&ctx, "model:manage")?;
|
||||
|
||||
let result = service::update_template(
|
||||
&state.db, &id,
|
||||
req.description.as_deref(),
|
||||
req.model.as_deref(),
|
||||
req.system_prompt.as_deref(),
|
||||
req.tools.as_deref(),
|
||||
req.capabilities.as_deref(),
|
||||
req.temperature,
|
||||
req.max_tokens,
|
||||
req.visibility.as_deref(),
|
||||
req.status.as_deref(),
|
||||
).await?;
|
||||
|
||||
log_operation(&state.db, &ctx.account_id, "agent_template.update", "agent_template", &id,
|
||||
None, ctx.client_ip.as_deref()).await?;
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
/// DELETE /api/v1/agent-templates/:id — 归档 Agent 模板
|
||||
pub async fn archive_template(
|
||||
State(state): State<AppState>,
|
||||
Extension(ctx): Extension<AuthContext>,
|
||||
Path(id): Path<String>,
|
||||
) -> SaasResult<Json<AgentTemplateInfo>> {
|
||||
check_permission(&ctx, "model:manage")?;
|
||||
|
||||
let result = service::archive_template(&state.db, &id).await?;
|
||||
|
||||
log_operation(&state.db, &ctx.account_id, "agent_template.archive", "agent_template", &id,
|
||||
None, ctx.client_ip.as_deref()).await?;
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
Reference in New Issue
Block a user