Show proxy status in UI

This commit is contained in:
Gregory Schier
2026-03-11 15:09:21 -07:00
parent 90365f0723
commit f51f72a332
13 changed files with 289 additions and 194 deletions

View File

@@ -33,6 +33,22 @@ impl ProxyCtx {
}
}
// -- Proxy state --
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "gen_rpc.ts")]
pub enum ProxyState {
Running,
Stopped,
}
#[derive(Serialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct ProxyStatePayload {
pub state: ProxyState,
}
// -- Request/response types --
#[derive(Deserialize, TS)]
@@ -56,6 +72,16 @@ pub struct ListModelsResponse {
pub http_exchanges: Vec<HttpExchange>,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct GetProxyStateRequest {}
#[derive(Serialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct GetProxyStateResponse {
pub state: ProxyState,
}
// -- Handlers --
fn execute_action(ctx: &ProxyCtx, invocation: ActionInvocation) -> Result<bool, RpcError> {
@@ -81,6 +107,9 @@ fn execute_action(ctx: &ProxyCtx, invocation: ActionInvocation) -> Result<bool,
}
*handle = Some(proxy_handle);
ctx.events.emit("proxy_state_changed", &ProxyStatePayload {
state: ProxyState::Running,
});
Ok(true)
}
GlobalAction::ProxyStop => {
@@ -89,12 +118,28 @@ fn execute_action(ctx: &ProxyCtx, invocation: ActionInvocation) -> Result<bool,
.lock()
.map_err(|_| RpcError { message: "lock poisoned".into() })?;
handle.take();
ctx.events.emit("proxy_state_changed", &ProxyStatePayload {
state: ProxyState::Stopped,
});
Ok(true)
}
},
}
}
fn get_proxy_state(ctx: &ProxyCtx, _req: GetProxyStateRequest) -> Result<GetProxyStateResponse, RpcError> {
let handle = ctx
.handle
.lock()
.map_err(|_| RpcError { message: "lock poisoned".into() })?;
let state = if handle.is_some() {
ProxyState::Running
} else {
ProxyState::Stopped
};
Ok(GetProxyStateResponse { state })
}
fn list_actions(_ctx: &ProxyCtx, _req: ListActionsRequest) -> Result<ListActionsResponse, RpcError> {
Ok(ListActionsResponse {
actions: crate::actions::all_global_actions(),
@@ -216,10 +261,12 @@ define_rpc! {
ProxyCtx;
commands {
execute_action(ActionInvocation) -> bool,
get_proxy_state(GetProxyStateRequest) -> GetProxyStateResponse,
list_actions(ListActionsRequest) -> ListActionsResponse,
list_models(ListModelsRequest) -> ListModelsResponse,
}
events {
model_write(ModelPayload),
proxy_state_changed(ProxyStatePayload),
}
}