huskies: merge 770
This commit is contained in:
@@ -262,36 +262,6 @@ impl AgentsApi {
|
||||
Ok(Json(true))
|
||||
}
|
||||
|
||||
/// List all agents with their status.
|
||||
///
|
||||
/// Agents for stories that have been completed (`work/5_done/` or `work/6_archived/`) are
|
||||
/// excluded so the agents panel is not cluttered with old completed items
|
||||
/// on frontend startup.
|
||||
#[oai(path = "/agents", method = "get")]
|
||||
async fn list_agents(&self) -> OpenApiResult<Json<Vec<AgentInfoResponse>>> {
|
||||
let project_root = self
|
||||
.ctx
|
||||
.services
|
||||
.agents
|
||||
.get_project_root(&self.ctx.state)
|
||||
.ok();
|
||||
let agents = svc::list_agents(&self.ctx.services.agents, project_root.as_deref())
|
||||
.map_err(map_svc_error)?;
|
||||
|
||||
Ok(Json(
|
||||
agents
|
||||
.into_iter()
|
||||
.map(|info| AgentInfoResponse {
|
||||
story_id: info.story_id,
|
||||
agent_name: info.agent_name,
|
||||
status: info.status.to_string(),
|
||||
session_id: info.session_id,
|
||||
worktree_path: info.worktree_path,
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Get the configured agent roster from project.toml.
|
||||
#[oai(path = "/agents/config", method = "get")]
|
||||
async fn get_agent_config(&self) -> OpenApiResult<Json<Vec<AgentConfigInfoResponse>>> {
|
||||
|
||||
@@ -42,58 +42,6 @@ fn story_is_archived_true_when_file_in_6_archived() {
|
||||
assert!(svc::is_archived(&root, "79_story_foo"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_agents_excludes_archived_stories() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let root = make_work_dirs(&tmp);
|
||||
|
||||
// Place an archived story file in 6_archived
|
||||
std::fs::write(
|
||||
root.join(".huskies/work/6_archived/79_story_archived.md"),
|
||||
"---\nname: archived story\n---\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let ctx = AppContext::new_test(root);
|
||||
// Inject an agent for the archived story (completed) and one for an active story
|
||||
ctx.services
|
||||
.agents
|
||||
.inject_test_agent("79_story_archived", "coder-1", AgentStatus::Completed);
|
||||
ctx.services
|
||||
.agents
|
||||
.inject_test_agent("80_story_active", "coder-1", AgentStatus::Running);
|
||||
|
||||
let api = AgentsApi { ctx: Arc::new(ctx) };
|
||||
let result = api.list_agents().await.unwrap().0;
|
||||
|
||||
// Archived story's agent should not appear
|
||||
assert!(
|
||||
!result.iter().any(|a| a.story_id == "79_story_archived"),
|
||||
"archived story agent should be excluded from list_agents"
|
||||
);
|
||||
// Active story's agent should still appear
|
||||
assert!(
|
||||
result.iter().any(|a| a.story_id == "80_story_active"),
|
||||
"active story agent should be included in list_agents"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_agents_includes_all_when_no_project_root() {
|
||||
// When no project root is configured, all agents are returned (safe default).
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let ctx = AppContext::new_test(tmp.path().to_path_buf());
|
||||
// Clear the project_root so get_project_root returns Err
|
||||
*ctx.state.project_root.lock().unwrap() = None;
|
||||
ctx.services
|
||||
.agents
|
||||
.inject_test_agent("42_story_whatever", "coder-1", AgentStatus::Completed);
|
||||
|
||||
let api = AgentsApi { ctx: Arc::new(ctx) };
|
||||
let result = api.list_agents().await.unwrap().0;
|
||||
assert!(result.iter().any(|a| a.story_id == "42_story_whatever"));
|
||||
}
|
||||
|
||||
fn make_project_toml(root: &path::Path, content: &str) {
|
||||
let sk_dir = root.join(".huskies");
|
||||
std::fs::create_dir_all(&sk_dir).unwrap();
|
||||
|
||||
@@ -82,6 +82,7 @@ pub fn build_routes(
|
||||
.nest("/docs", docs_service.swagger_ui())
|
||||
.at("/ws", get(ws::ws_handler))
|
||||
.at("/crdt-sync", get(crate::crdt_sync::crdt_sync_handler))
|
||||
.at("/rpc", post(rpc_http_handler))
|
||||
.at(
|
||||
"/agents/:story_id/:agent_name/stream",
|
||||
get(agents_sse::agent_stream),
|
||||
@@ -133,6 +134,28 @@ pub fn build_routes(
|
||||
route.data(ctx_arc)
|
||||
}
|
||||
|
||||
/// HTTP bridge for the read-RPC protocol.
|
||||
///
|
||||
/// Accepts a JSON [`RpcFrame::RpcRequest`] body and returns the corresponding
|
||||
/// [`RpcFrame::RpcResponse`]. This allows HTTP clients (e.g. the frontend) to
|
||||
/// call read-RPC methods without maintaining a `/crdt-sync` WebSocket connection.
|
||||
#[poem::handler]
|
||||
pub async fn rpc_http_handler(body: poem::web::Json<serde_json::Value>) -> poem::Response {
|
||||
let text = serde_json::to_string(&body.0).unwrap_or_default();
|
||||
match crate::crdt_sync::try_handle_rpc_text(&text) {
|
||||
Some(response) => {
|
||||
let json = serde_json::to_string(&response).unwrap_or_default();
|
||||
poem::Response::builder()
|
||||
.status(poem::http::StatusCode::OK)
|
||||
.header(poem::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(json)
|
||||
}
|
||||
None => poem::Response::builder()
|
||||
.status(poem::http::StatusCode::BAD_REQUEST)
|
||||
.body("Invalid RPC request"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Debug HTTP endpoint: `GET /debug/crdt[?story_id=<id>]`
|
||||
///
|
||||
/// Returns the raw in-memory CRDT state as JSON. Accepts an optional
|
||||
|
||||
+37
-5
@@ -29,13 +29,30 @@ pub async fn ws_handler(ws: WebSocket, ctx: Data<&Arc<AppContext>>) -> impl poem
|
||||
ws.on_upgrade(move |socket| async move {
|
||||
let (mut sink, mut stream) = socket.split();
|
||||
let (tx, mut rx) = mpsc::unbounded_channel::<WsResponse>();
|
||||
// Separate channel for pre-serialized messages (e.g. RPC responses).
|
||||
let (raw_tx, mut raw_rx) = mpsc::unbounded_channel::<String>();
|
||||
|
||||
let forward = tokio::spawn(async move {
|
||||
while let Some(msg) = rx.recv().await {
|
||||
if let Ok(text) = serde_json::to_string(&msg)
|
||||
&& sink.send(WsMessage::Text(text)).await.is_err()
|
||||
{
|
||||
break;
|
||||
loop {
|
||||
tokio::select! {
|
||||
msg = rx.recv() => match msg {
|
||||
Some(msg) => {
|
||||
if let Ok(text) = serde_json::to_string(&msg)
|
||||
&& sink.send(WsMessage::Text(text)).await.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
},
|
||||
raw = raw_rx.recv() => match raw {
|
||||
Some(text) => {
|
||||
if sink.send(WsMessage::Text(text)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -79,6 +96,14 @@ pub async fn ws_handler(ws: WebSocket, ctx: Data<&Arc<AppContext>>) -> impl poem
|
||||
break;
|
||||
};
|
||||
|
||||
// Handle read-RPC frames (discriminated by "kind", not "type").
|
||||
if let Some(rpc_resp) = crate::crdt_sync::try_handle_rpc_text(&text) {
|
||||
if let Ok(resp_text) = serde_json::to_string(&rpc_resp) {
|
||||
let _ = raw_tx.send(resp_text);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match ws::dispatch_outer(&text) {
|
||||
ws::DispatchResult::StartChat { messages, config } => {
|
||||
let tx_updates = tx.clone();
|
||||
@@ -134,6 +159,13 @@ pub async fn ws_handler(ws: WebSocket, ctx: Data<&Arc<AppContext>>) -> impl poem
|
||||
}
|
||||
|
||||
Some(Ok(WsMessage::Text(inner_text))) = stream.next() => {
|
||||
// Handle read-RPC frames during active chat.
|
||||
if let Some(rpc_resp) = crate::crdt_sync::try_handle_rpc_text(&inner_text) {
|
||||
if let Ok(resp_text) = serde_json::to_string(&rpc_resp) {
|
||||
let _ = raw_tx.send(resp_text);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match ws::dispatch_inner(&inner_text, &mut pending_perms) {
|
||||
ws::InnerDispatchResult::CancelChat => {
|
||||
let _ = chat::cancel_chat(&ctx.state);
|
||||
|
||||
Reference in New Issue
Block a user