use anyhow::{Context, Result};
use serde_json::{Value, json};
use crate::state::BridgeState;
use crate::state::helpers::{request_key, required_string};
pub(super) async fn handle_config_warning(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
state.emit_event(
"configWarning",
Some(runtime_id),
None,
json!({
"runtimeId": runtime_id,
"summary": required_string(¶ms, "summary")?,
"details": params.get("details"),
"path": params.get("path"),
"range": params.get("range"),
"raw": params,
}),
)?;
Ok(())
}
pub(super) async fn handle_deprecation_notice(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
state.emit_event(
"deprecationNotice",
Some(runtime_id),
None,
json!({
"runtimeId": runtime_id,
"summary": required_string(¶ms, "summary")?,
"details": params.get("details"),
"raw": params,
}),
)?;
Ok(())
}
pub(super) async fn handle_model_rerouted(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
let thread_id = required_string(¶ms, "threadId")?;
state.emit_event(
"model/rerouted",
Some(runtime_id),
Some(thread_id),
json!({
"runtimeId": runtime_id,
"threadId": thread_id,
"turnId": required_string(¶ms, "turnId")?,
"fromModel": required_string(¶ms, "fromModel")?,
"toModel": required_string(¶ms, "toModel")?,
"reason": required_string(¶ms, "reason")?,
"raw": params,
}),
)?;
Ok(())
}
pub(super) async fn handle_command_exec_output_delta(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
state.emit_event(
"command/exec/outputDelta",
Some(runtime_id),
None,
json!({
"runtimeId": runtime_id,
"processId": params.get("processId"),
"stream": params.get("stream"),
"command": params.get("command"),
"cwd": params.get("cwd"),
"status": params.get("status"),
"exitCode": params.get("exitCode"),
"delta": params.get("delta"),
"raw": params,
}),
)?;
Ok(())
}
pub(super) async fn handle_server_request_resolved(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
let request_id = params
.get("requestId")
.map(request_key)
.context("serverRequest/resolved 缺少 requestId")?;
state.storage.remove_pending_request(&request_id)?;
state.emit_event(
"serverRequest/resolved",
Some(runtime_id),
params.get("threadId").and_then(Value::as_str),
json!({
"runtimeId": runtime_id,
"threadId": params.get("threadId"),
"requestId": request_id,
"raw": params,
}),
)?;
Ok(())
}
pub(super) async fn handle_error_notification(
state: &BridgeState,
runtime_id: &str,
params: Value,
) -> Result<()> {
state.emit_event(
"error",
Some(runtime_id),
params.get("threadId").and_then(Value::as_str),
json!({
"runtimeId": runtime_id,
"message": params,
}),
)?;
Ok(())
}