agent ui: live event panel via SSE + stream-json
This commit is contained in:
parent
3c9d42b2a7
commit
9eab28a716
8 changed files with 277 additions and 33 deletions
|
|
@ -1,12 +1,15 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
use hive_ag3nt::events::{Bus, LiveEvent};
|
||||
use hive_ag3nt::login::{self, LoginState};
|
||||
use hive_ag3nt::{DEFAULT_SOCKET, DEFAULT_WEB_PORT, client, mcp, web_ui};
|
||||
use hive_sh4re::{AgentRequest, AgentResponse};
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::process::Command;
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
@ -61,14 +64,16 @@ async fn main() -> Result<()> {
|
|||
tracing::info!(state = ?initial, claude_dir = %claude_dir.display(), "harness boot");
|
||||
let login_state = Arc::new(Mutex::new(initial));
|
||||
let ui_state = login_state.clone();
|
||||
let bus = Bus::new();
|
||||
let ui_bus = bus.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = web_ui::serve(label, port, ui_state).await {
|
||||
if let Err(e) = web_ui::serve(label, port, ui_state, ui_bus).await {
|
||||
tracing::error!(error = ?e, "web ui failed");
|
||||
}
|
||||
});
|
||||
match initial {
|
||||
LoginState::Online => {
|
||||
serve(&cli.socket, Duration::from_millis(poll_ms), login_state).await
|
||||
serve(&cli.socket, Duration::from_millis(poll_ms), login_state, bus).await
|
||||
}
|
||||
LoginState::NeedsLogin => {
|
||||
// Partial-run mode: keep the harness alive (so the web UI
|
||||
|
|
@ -77,7 +82,7 @@ async fn main() -> Result<()> {
|
|||
// from the dashboard PTY path in step 4 or via
|
||||
// `root-login` + `claude auth login` in the meantime)
|
||||
// transitions us into the turn loop without a restart.
|
||||
needs_login_loop(&cli.socket, &claude_dir, login_state, poll_ms).await
|
||||
needs_login_loop(&cli.socket, &claude_dir, login_state, poll_ms, bus).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -103,6 +108,7 @@ async fn needs_login_loop(
|
|||
claude_dir: &Path,
|
||||
state: Arc<Mutex<LoginState>>,
|
||||
poll_ms: u64,
|
||||
bus: Bus,
|
||||
) -> Result<()> {
|
||||
tracing::warn!(
|
||||
claude_dir = %claude_dir.display(),
|
||||
|
|
@ -114,12 +120,17 @@ async fn needs_login_loop(
|
|||
if login::has_session(claude_dir) {
|
||||
tracing::info!("claude session detected — entering turn loop");
|
||||
*state.lock().unwrap() = LoginState::Online;
|
||||
return serve(socket, Duration::from_millis(poll_ms), state).await;
|
||||
return serve(socket, Duration::from_millis(poll_ms), state, bus).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve(socket: &Path, interval: Duration, state: Arc<Mutex<LoginState>>) -> Result<()> {
|
||||
async fn serve(
|
||||
socket: &Path,
|
||||
interval: Duration,
|
||||
state: Arc<Mutex<LoginState>>,
|
||||
bus: Bus,
|
||||
) -> Result<()> {
|
||||
tracing::info!(socket = %socket.display(), "hive-ag3nt serve");
|
||||
let _ = state; // reserved for future state transitions (turn-loop -> needs-login)
|
||||
let mcp_config = write_mcp_config(socket).await?;
|
||||
|
|
@ -129,10 +140,28 @@ async fn serve(socket: &Path, interval: Duration, state: Arc<Mutex<LoginState>>)
|
|||
match recv {
|
||||
Ok(AgentResponse::Message { from, body }) => {
|
||||
tracing::info!(%from, %body, "inbox");
|
||||
bus.emit(LiveEvent::TurnStart {
|
||||
from: from.clone(),
|
||||
body: body.clone(),
|
||||
});
|
||||
let prompt = format_wake_prompt(&label, &from, &body);
|
||||
match invoke_claude(&prompt, &mcp_config).await {
|
||||
Ok(out) => tracing::info!(stdout = %out.trim(), "claude turn finished"),
|
||||
Err(e) => tracing::warn!(error = %format!("{e:#}"), "claude turn failed"),
|
||||
let outcome = run_turn(&prompt, &mcp_config, &bus).await;
|
||||
match outcome {
|
||||
Ok(()) => {
|
||||
bus.emit(LiveEvent::TurnEnd {
|
||||
ok: true,
|
||||
note: None,
|
||||
});
|
||||
tracing::info!("claude turn finished");
|
||||
}
|
||||
Err(e) => {
|
||||
let note = format!("{e:#}");
|
||||
bus.emit(LiveEvent::TurnEnd {
|
||||
ok: false,
|
||||
note: Some(note.clone()),
|
||||
});
|
||||
tracing::warn!(error = %note, "claude turn failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(AgentResponse::Empty) => {}
|
||||
|
|
@ -173,15 +202,20 @@ fn format_wake_prompt(label: &str, from: &str, body: &str) -> String {
|
|||
)
|
||||
}
|
||||
|
||||
async fn invoke_claude(prompt: &str, mcp_config: &Path) -> Result<String> {
|
||||
// Whitelist model: `--tools` restricts which built-ins exist in the
|
||||
// session (omitting WebFetch/WebSearch/Task means claude literally
|
||||
// can't invoke them); `--allowedTools` auto-approves the same set
|
||||
// plus the hyperhive MCP surface so there's no permission prompt
|
||||
// mid-turn. A finer-grained allow-list system for Bash command
|
||||
// patterns is on the backlog (PLAN.md polish).
|
||||
let out = Command::new("claude")
|
||||
/// Spawn `claude` for one turn and stream its `stream-json` stdout into
|
||||
/// the live event bus. `--verbose` is required by claude-code when pairing
|
||||
/// `--print` with `--output-format stream-json`. Each stdout line is one
|
||||
/// JSON event; we broadcast the parsed value (or a `Note` fallback on
|
||||
/// parse error so the UI doesn't silently lose information). The tool
|
||||
/// whitelist is the same as before: omit WebFetch/WebSearch/Task; allow
|
||||
/// the hyperhive MCP surface auto-approved. Bash pattern allow-list is on
|
||||
/// the backlog (CLAUDE.md).
|
||||
async fn run_turn(prompt: &str, mcp_config: &Path, bus: &Bus) -> Result<()> {
|
||||
let mut child = Command::new("claude")
|
||||
.arg("--print")
|
||||
.arg("--verbose")
|
||||
.arg("--output-format")
|
||||
.arg("stream-json")
|
||||
.arg("--model")
|
||||
.arg("haiku")
|
||||
.arg("--mcp-config")
|
||||
|
|
@ -191,20 +225,38 @@ async fn invoke_claude(prompt: &str, mcp_config: &Path) -> Result<String> {
|
|||
.arg("--allowedTools")
|
||||
.arg(mcp::allowed_tools_arg())
|
||||
.arg(prompt)
|
||||
.output()
|
||||
.await?;
|
||||
if !out.status.success() {
|
||||
bail!(
|
||||
"claude exited {}: {}",
|
||||
out.status,
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
);
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let stdout = child.stdout.take().expect("piped stdout");
|
||||
let stderr = child.stderr.take().expect("piped stderr");
|
||||
|
||||
let bus_out = bus.clone();
|
||||
let bus_err = bus.clone();
|
||||
let pump_stdout = tokio::spawn(async move {
|
||||
let mut reader = BufReader::new(stdout).lines();
|
||||
while let Ok(Some(line)) = reader.next_line().await {
|
||||
match serde_json::from_str::<serde_json::Value>(&line) {
|
||||
Ok(v) => bus_out.emit(LiveEvent::Stream(v)),
|
||||
Err(_) => bus_out.emit(LiveEvent::Note(format!("(non-json) {line}"))),
|
||||
}
|
||||
}
|
||||
});
|
||||
let pump_stderr = tokio::spawn(async move {
|
||||
let mut reader = BufReader::new(stderr).lines();
|
||||
while let Ok(Some(line)) = reader.next_line().await {
|
||||
bus_err.emit(LiveEvent::Note(format!("stderr: {line}")));
|
||||
}
|
||||
});
|
||||
|
||||
let status = child.wait().await?;
|
||||
let _ = pump_stdout.await;
|
||||
let _ = pump_stderr.await;
|
||||
if !status.success() {
|
||||
bail!("claude exited {status}");
|
||||
}
|
||||
let text = String::from_utf8_lossy(&out.stdout).trim().to_owned();
|
||||
if text.is_empty() {
|
||||
bail!("claude produced empty output");
|
||||
}
|
||||
Ok(text)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Drop the per-agent MCP config on disk so the turn loop can hand its path
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use std::time::Duration;
|
|||
|
||||
use anyhow::{Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
use hive_ag3nt::events::Bus;
|
||||
use hive_ag3nt::login::{self, LoginState};
|
||||
use hive_ag3nt::{DEFAULT_SOCKET, DEFAULT_WEB_PORT, client, web_ui};
|
||||
use hive_sh4re::{HelperEvent, ManagerRequest, ManagerResponse, SYSTEM_SENDER};
|
||||
|
|
@ -66,11 +67,14 @@ async fn main() -> Result<()> {
|
|||
tracing::info!(state = ?initial, claude_dir = %claude_dir.display(), "hm1nd boot");
|
||||
let login_state = Arc::new(Mutex::new(initial));
|
||||
let ui_state = login_state.clone();
|
||||
let bus = Bus::new();
|
||||
let ui_bus = bus.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = web_ui::serve(label, port, ui_state).await {
|
||||
if let Err(e) = web_ui::serve(label, port, ui_state, ui_bus).await {
|
||||
tracing::error!(error = ?e, "web ui failed");
|
||||
}
|
||||
});
|
||||
let _ = bus; // manager turn loop not wired to events yet
|
||||
match initial {
|
||||
LoginState::Online => serve(&cli.socket, Duration::from_millis(poll_ms)).await,
|
||||
LoginState::NeedsLogin => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue