2026-02-19 15:25:22 +00:00
|
|
|
use crate::http::context::{AppContext, OpenApiResult, bad_request};
|
|
|
|
|
use poem_openapi::{Object, OpenApi, Tags, payload::Json};
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
#[derive(Tags)]
|
|
|
|
|
enum AgentsTags {
|
|
|
|
|
Agents,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Object)]
|
2026-02-19 17:58:53 +00:00
|
|
|
struct StoryIdPayload {
|
|
|
|
|
story_id: String,
|
2026-02-19 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Object, Serialize)]
|
|
|
|
|
struct AgentInfoResponse {
|
2026-02-19 17:58:53 +00:00
|
|
|
story_id: String,
|
2026-02-19 15:25:22 +00:00
|
|
|
status: String,
|
|
|
|
|
session_id: Option<String>,
|
2026-02-19 17:58:53 +00:00
|
|
|
worktree_path: Option<String>,
|
2026-02-19 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct AgentsApi {
|
|
|
|
|
pub ctx: Arc<AppContext>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[OpenApi(tag = "AgentsTags::Agents")]
|
|
|
|
|
impl AgentsApi {
|
2026-02-19 17:58:53 +00:00
|
|
|
/// Start an agent for a given story (creates worktree, runs setup, spawns agent).
|
|
|
|
|
#[oai(path = "/agents/start", method = "post")]
|
|
|
|
|
async fn start_agent(
|
2026-02-19 15:25:22 +00:00
|
|
|
&self,
|
2026-02-19 17:58:53 +00:00
|
|
|
payload: Json<StoryIdPayload>,
|
2026-02-19 15:25:22 +00:00
|
|
|
) -> OpenApiResult<Json<AgentInfoResponse>> {
|
2026-02-19 17:58:53 +00:00
|
|
|
let project_root = self
|
|
|
|
|
.ctx
|
|
|
|
|
.agents
|
|
|
|
|
.get_project_root(&self.ctx.state)
|
|
|
|
|
.map_err(bad_request)?;
|
2026-02-19 15:25:22 +00:00
|
|
|
|
2026-02-19 17:58:53 +00:00
|
|
|
let info = self
|
|
|
|
|
.ctx
|
|
|
|
|
.agents
|
|
|
|
|
.start_agent(&project_root, &payload.0.story_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(bad_request)?;
|
2026-02-19 15:25:22 +00:00
|
|
|
|
|
|
|
|
Ok(Json(AgentInfoResponse {
|
2026-02-19 17:58:53 +00:00
|
|
|
story_id: info.story_id,
|
|
|
|
|
status: info.status.to_string(),
|
2026-02-19 15:25:22 +00:00
|
|
|
session_id: info.session_id,
|
2026-02-19 17:58:53 +00:00
|
|
|
worktree_path: info.worktree_path,
|
2026-02-19 15:25:22 +00:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 17:58:53 +00:00
|
|
|
/// Stop a running agent and clean up its worktree.
|
|
|
|
|
#[oai(path = "/agents/stop", method = "post")]
|
|
|
|
|
async fn stop_agent(&self, payload: Json<StoryIdPayload>) -> OpenApiResult<Json<bool>> {
|
|
|
|
|
let project_root = self
|
|
|
|
|
.ctx
|
|
|
|
|
.agents
|
|
|
|
|
.get_project_root(&self.ctx.state)
|
|
|
|
|
.map_err(bad_request)?;
|
|
|
|
|
|
|
|
|
|
self.ctx
|
|
|
|
|
.agents
|
|
|
|
|
.stop_agent(&project_root, &payload.0.story_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(bad_request)?;
|
|
|
|
|
|
|
|
|
|
Ok(Json(true))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// List all agents with their status.
|
2026-02-19 15:25:22 +00:00
|
|
|
#[oai(path = "/agents", method = "get")]
|
|
|
|
|
async fn list_agents(&self) -> OpenApiResult<Json<Vec<AgentInfoResponse>>> {
|
|
|
|
|
let agents = self.ctx.agents.list_agents().map_err(bad_request)?;
|
|
|
|
|
|
|
|
|
|
Ok(Json(
|
|
|
|
|
agents
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|info| AgentInfoResponse {
|
2026-02-19 17:58:53 +00:00
|
|
|
story_id: info.story_id,
|
|
|
|
|
status: info.status.to_string(),
|
2026-02-19 15:25:22 +00:00
|
|
|
session_id: info.session_id,
|
2026-02-19 17:58:53 +00:00
|
|
|
worktree_path: info.worktree_path,
|
2026-02-19 15:25:22 +00:00
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|