codex-mobile-bridge 0.2.6

Remote bridge and service manager for codex-mobile.
Documentation
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(&params, "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(&params, "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(&params, "threadId")?;
    state.emit_event(
        "model/rerouted",
        Some(runtime_id),
        Some(thread_id),
        json!({
            "runtimeId": runtime_id,
            "threadId": thread_id,
            "turnId": required_string(&params, "turnId")?,
            "fromModel": required_string(&params, "fromModel")?,
            "toModel": required_string(&params, "toModel")?,
            "reason": required_string(&params, "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(())
}