feat: complete Epic 1 production foundation
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
use axum::{
|
||||
Json,
|
||||
Extension, Json,
|
||||
extract::{Path, State},
|
||||
http::{HeaderMap, header},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::{
|
||||
auth::AuthenticatedSession,
|
||||
error::ApiError,
|
||||
request_context::RequestContext,
|
||||
service::{
|
||||
AgentCatalogPayload, AgentPayload, PlatformApiKeyPayload, PublishPayload,
|
||||
ToolSearchPreviewPayload, UpdateAgentPayload,
|
||||
AdminAuditContext, AgentCatalogPayload, AgentPayload, PlatformApiKeyPayload,
|
||||
PublishPayload, ToolSearchPreviewPayload, UpdateAgentPayload,
|
||||
},
|
||||
state::AppState,
|
||||
};
|
||||
@@ -77,7 +81,7 @@ pub async fn create_agent(
|
||||
pub async fn get_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let agent = state
|
||||
.service
|
||||
.get_agent(
|
||||
@@ -85,20 +89,30 @@ pub async fn get_agent(
|
||||
&path.agent_id.as_str().into(),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(agent)))
|
||||
let etag = crate::service::AdminService::agent_state_etag(&agent);
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
header::ETAG,
|
||||
header::HeaderValue::from_str(&etag)
|
||||
.map_err(|_| ApiError::internal("invalid agent etag"))?,
|
||||
);
|
||||
Ok((headers, Json(json!(agent))))
|
||||
}
|
||||
|
||||
pub async fn update_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Json(payload): Json<UpdateAgentPayload>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let updated = state
|
||||
.service
|
||||
.update_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
payload,
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(updated)))
|
||||
@@ -107,12 +121,15 @@ pub async fn update_agent(
|
||||
pub async fn delete_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let deleted = state
|
||||
.service
|
||||
.delete_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(deleted)))
|
||||
@@ -136,30 +153,79 @@ pub async fn get_agent_version(
|
||||
pub async fn save_agent_bindings(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Json(payload): Json<AgentCatalogPayload>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let record = state
|
||||
.service
|
||||
.save_agent_bindings(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
payload,
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(record)))
|
||||
}
|
||||
|
||||
async fn require_agent_precondition(
|
||||
state: &AppState,
|
||||
path: &WorkspaceAgentPath,
|
||||
headers: &HeaderMap,
|
||||
) -> Result<crank_registry::AgentStateExpectation, ApiError> {
|
||||
let agent = state
|
||||
.service
|
||||
.get_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
)
|
||||
.await?;
|
||||
let expected = crate::service::AdminService::agent_state_etag(&agent);
|
||||
let provided = headers
|
||||
.get(header::IF_MATCH)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.ok_or_else(|| {
|
||||
ApiError::precondition_required_with_context(
|
||||
"If-Match is required for Agent mutation",
|
||||
json!({
|
||||
"error_code": "agent_precondition_required",
|
||||
"current_version": agent.current_draft_version,
|
||||
"latest_published_version": agent.latest_published_version,
|
||||
"catalog_revision": agent.catalog_revision,
|
||||
"recovery": "reload"
|
||||
}),
|
||||
)
|
||||
})?;
|
||||
if provided != expected {
|
||||
return Err(ApiError::conflict_with_context(
|
||||
"Agent state changed; reload before retrying",
|
||||
json!({
|
||||
"error_code": "agent_stale_revision",
|
||||
"current_version": agent.current_draft_version,
|
||||
"latest_published_version": agent.latest_published_version,
|
||||
"catalog_revision": agent.catalog_revision,
|
||||
"recovery": "reload"
|
||||
}),
|
||||
));
|
||||
}
|
||||
crate::service::AdminService::agent_state_expectation(&agent)
|
||||
}
|
||||
|
||||
pub async fn publish_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Json(payload): Json<PublishPayload>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let published = state
|
||||
.service
|
||||
.publish_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
payload.version,
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(published)))
|
||||
@@ -168,12 +234,15 @@ pub async fn publish_agent(
|
||||
pub async fn unpublish_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let updated = state
|
||||
.service
|
||||
.unpublish_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(updated)))
|
||||
@@ -182,12 +251,15 @@ pub async fn unpublish_agent(
|
||||
pub async fn archive_agent(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let expected_state = require_agent_precondition(&state, &path, &headers).await?;
|
||||
let updated = state
|
||||
.service
|
||||
.archive_agent(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
Some(&expected_state),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(updated)))
|
||||
@@ -210,14 +282,19 @@ pub async fn list_agent_platform_api_keys(
|
||||
pub async fn create_agent_platform_api_key(
|
||||
Path(path): Path<WorkspaceAgentPath>,
|
||||
State(state): State<AppState>,
|
||||
Extension(session): Extension<AuthenticatedSession>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Json(payload): Json<PlatformApiKeyPayload>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let audit_context =
|
||||
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
|
||||
let created = state
|
||||
.service
|
||||
.create_agent_platform_api_key(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
payload,
|
||||
Some(&audit_context),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(created)))
|
||||
@@ -226,13 +303,18 @@ pub async fn create_agent_platform_api_key(
|
||||
pub async fn revoke_agent_platform_api_key(
|
||||
Path(path): Path<WorkspaceAgentPlatformApiKeyPath>,
|
||||
State(state): State<AppState>,
|
||||
Extension(session): Extension<AuthenticatedSession>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
) -> Result<axum::http::StatusCode, ApiError> {
|
||||
let audit_context =
|
||||
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
|
||||
state
|
||||
.service
|
||||
.revoke_agent_platform_api_key(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
&path.key_id.as_str().into(),
|
||||
Some(&audit_context),
|
||||
)
|
||||
.await?;
|
||||
Ok(axum::http::StatusCode::NO_CONTENT)
|
||||
@@ -241,13 +323,18 @@ pub async fn revoke_agent_platform_api_key(
|
||||
pub async fn delete_agent_platform_api_key(
|
||||
Path(path): Path<WorkspaceAgentPlatformApiKeyPath>,
|
||||
State(state): State<AppState>,
|
||||
Extension(session): Extension<AuthenticatedSession>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
) -> Result<axum::http::StatusCode, ApiError> {
|
||||
let audit_context =
|
||||
AdminAuditContext::from_session_and_correlation(&session, &request_context.correlation);
|
||||
state
|
||||
.service
|
||||
.delete_agent_platform_api_key(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.agent_id.as_str().into(),
|
||||
&path.key_id.as_str().into(),
|
||||
Some(&audit_context),
|
||||
)
|
||||
.await?;
|
||||
Ok(axum::http::StatusCode::NO_CONTENT)
|
||||
|
||||
Reference in New Issue
Block a user