68 lines
1.5 KiB
Rust
68 lines
1.5 KiB
Rust
//! Agent HTTP server for browser automation
|
|
//!
|
|
//! Port of `agent_server.c` / `agent_server.h` from the C project.
|
|
|
|
use std::sync::Mutex;
|
|
use once_cell::sync::Lazy;
|
|
|
|
use crate::agent_mcp;
|
|
|
|
/// Agent server state.
|
|
static G_SERVER_STATE: Lazy<Mutex<ServerState>> = Lazy::new(|| Mutex::new(ServerState::default()));
|
|
|
|
struct ServerState {
|
|
running: bool,
|
|
port: u16,
|
|
}
|
|
|
|
impl Default for ServerState {
|
|
fn default() -> Self {
|
|
ServerState {
|
|
running: false,
|
|
port: 17777,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Initialize the agent server.
|
|
pub fn agent_server_init() {
|
|
let mut state = G_SERVER_STATE.lock().unwrap();
|
|
state.port = 17777;
|
|
}
|
|
|
|
/// Start the agent server.
|
|
pub fn agent_server_start() -> Result<(), Box<dyn std::error::Error>> {
|
|
let mut state = G_SERVER_STATE.lock().unwrap();
|
|
if state.running {
|
|
return Ok(());
|
|
}
|
|
|
|
state.running = true;
|
|
println!("[server] Agent server starting on port {}", state.port);
|
|
|
|
// Start MCP server
|
|
agent_mcp::agent_mcp_start()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Stop the agent server.
|
|
pub fn agent_server_stop() {
|
|
let mut state = G_SERVER_STATE.lock().unwrap();
|
|
state.running = false;
|
|
agent_mcp::agent_mcp_stop();
|
|
println!("[server] Agent server stopped");
|
|
}
|
|
|
|
/// Check if the agent server is running.
|
|
pub fn agent_server_is_running() -> bool {
|
|
let state = G_SERVER_STATE.lock().unwrap();
|
|
state.running
|
|
}
|
|
|
|
/// Get the agent server port.
|
|
pub fn agent_server_get_port() -> u16 {
|
|
let state = G_SERVER_STATE.lock().unwrap();
|
|
state.port
|
|
}
|