use crate::macros::open_string_union;
use serde_json::Value;
open_string_union! {
AdapterKind {
REMOTE_IO => "remote_io",
APP_SERVER_TCP_WS => "app_server_tcp_ws",
BRIDGE_CCR => "bridge_ccr",
APP_SERVER_UDS => "app_server_uds",
STDIO_STREAM_JSON => "stdio_stream_json",
TAURI_MANAGED_APP_SERVER => "tauri_managed_app_server",
}
}
open_string_union! {
RunnerKind {
CLOUD => "cloud",
DESKTOP => "desktop",
LOCAL_EMBEDDED => "local_embedded",
}
}
#[derive(Debug, Clone, Default)]
pub struct WorkspacePolicy {
pub read_only: Option<bool>,
pub allowed_paths: Option<Vec<String>>,
pub denied_paths: Option<Vec<String>>,
pub max_bytes: Option<i64>,
}
#[derive(Debug, Clone, Default)]
pub struct PermissionPolicy {
pub shell_allowed: Option<bool>,
pub shell_deny_list: Option<Vec<String>>,
pub network_allowed: Option<bool>,
pub write_allowed: Option<bool>,
pub approval_timeout_ms: Option<i64>,
pub required_actors: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
pub enum RemoteControlEvent {
TextDelta {
index: i64,
text: String,
},
ReasoningDelta {
index: i64,
text: String,
},
ToolCall {
tool_call_id: String,
name: String,
input: Option<Value>,
source: Option<String>,
},
ToolResult {
tool_call_id: String,
ok: bool,
output: Option<Value>,
error: Option<String>,
},
PermissionRequest {
request_id: String,
kind: String,
payload: Option<Value>,
deadline_ms: Option<i64>,
},
PermissionResult {
request_id: String,
decision: String,
actor: Option<String>,
decided_at: Option<String>,
},
Usage {
input_tokens: Option<i64>,
output_tokens: Option<i64>,
cache_read: Option<i64>,
cache_create: Option<i64>,
exact: Option<bool>,
},
Settle {
status: String,
billed: Option<bool>,
},
Status {
phase: String,
message: Option<String>,
},
Error {
code: String,
message: String,
retryable: Option<bool>,
kind: Option<String>,
},
Done {
reason: String,
run_id: String,
final_status: String,
},
}
impl RemoteControlEvent {
pub fn type_str(&self) -> &'static str {
match self {
Self::TextDelta { .. } => "text_delta",
Self::ReasoningDelta { .. } => "reasoning_delta",
Self::ToolCall { .. } => "tool_call",
Self::ToolResult { .. } => "tool_result",
Self::PermissionRequest { .. } => "permission_request",
Self::PermissionResult { .. } => "permission_result",
Self::Usage { .. } => "usage",
Self::Settle { .. } => "settle",
Self::Status { .. } => "status",
Self::Error { .. } => "error",
Self::Done { .. } => "done",
}
}
}
fn as_record(v: &Value) -> Option<&serde_json::Map<String, Value>> {
v.as_object()
}
fn str_field(rec: &serde_json::Map<String, Value>, key: &str) -> Option<String> {
rec.get(key).and_then(|v| v.as_str()).map(|s| s.to_string())
}
fn num_field(rec: &serde_json::Map<String, Value>, key: &str) -> Option<i64> {
rec.get(key).and_then(|v| {
if let Some(i) = v.as_i64() {
Some(i)
} else {
v.as_f64().filter(|f| f.is_finite()).map(|f| f as i64)
}
})
}
fn bool_field(rec: &serde_json::Map<String, Value>, key: &str) -> Option<bool> {
rec.get(key).and_then(|v| v.as_bool())
}
pub fn parse_remote_control_event(raw: &Value) -> Option<RemoteControlEvent> {
let rec = as_record(raw)?;
let r#type = str_field(rec, "type")?;
match r#type.as_str() {
"text_delta" => {
let index = num_field(rec, "index")?;
let text = str_field(rec, "text")?;
Some(RemoteControlEvent::TextDelta { index, text })
}
"reasoning_delta" => {
let index = num_field(rec, "index")?;
let text = str_field(rec, "text")?;
Some(RemoteControlEvent::ReasoningDelta { index, text })
}
"tool_call" => {
let tool_call_id =
str_field(rec, "tool_call_id").or_else(|| str_field(rec, "toolCallId"))?;
let name = str_field(rec, "name")?;
Some(RemoteControlEvent::ToolCall {
tool_call_id,
name,
input: rec.get("input").cloned(),
source: str_field(rec, "source"),
})
}
"tool_result" => {
let tool_call_id =
str_field(rec, "tool_call_id").or_else(|| str_field(rec, "toolCallId"))?;
let ok = bool_field(rec, "ok")?;
Some(RemoteControlEvent::ToolResult {
tool_call_id,
ok,
output: rec.get("output").cloned(),
error: str_field(rec, "error"),
})
}
"permission_request" => {
let request_id =
str_field(rec, "request_id").or_else(|| str_field(rec, "requestId"))?;
let kind = str_field(rec, "kind")?;
Some(RemoteControlEvent::PermissionRequest {
request_id,
kind,
payload: rec.get("payload").cloned(),
deadline_ms: num_field(rec, "deadline_ms").or_else(|| num_field(rec, "deadlineMs")),
})
}
"permission_result" => {
let request_id =
str_field(rec, "request_id").or_else(|| str_field(rec, "requestId"))?;
let decision = str_field(rec, "decision")?;
Some(RemoteControlEvent::PermissionResult {
request_id,
decision,
actor: str_field(rec, "actor"),
decided_at: str_field(rec, "decided_at").or_else(|| str_field(rec, "decidedAt")),
})
}
"usage" => Some(RemoteControlEvent::Usage {
input_tokens: num_field(rec, "input_tokens").or_else(|| num_field(rec, "inputTokens")),
output_tokens: num_field(rec, "output_tokens")
.or_else(|| num_field(rec, "outputTokens")),
cache_read: num_field(rec, "cache_read").or_else(|| num_field(rec, "cacheRead")),
cache_create: num_field(rec, "cache_create").or_else(|| num_field(rec, "cacheCreate")),
exact: bool_field(rec, "exact"),
}),
"settle" => {
let status = str_field(rec, "status")?;
Some(RemoteControlEvent::Settle {
status,
billed: bool_field(rec, "billed"),
})
}
"status" => {
let phase = str_field(rec, "phase")?;
Some(RemoteControlEvent::Status {
phase,
message: str_field(rec, "message"),
})
}
"error" => {
let code = str_field(rec, "code")?;
let message = str_field(rec, "message")?;
Some(RemoteControlEvent::Error {
code,
message,
retryable: bool_field(rec, "retryable"),
kind: str_field(rec, "kind"),
})
}
"done" => {
let reason = str_field(rec, "reason")?;
let run_id = str_field(rec, "run_id").or_else(|| str_field(rec, "runId"))?;
let final_status =
str_field(rec, "final_status").or_else(|| str_field(rec, "finalStatus"))?;
Some(RemoteControlEvent::Done {
reason,
run_id,
final_status,
})
}
_ => None,
}
}
pub fn is_terminal_remote_event(ev: &RemoteControlEvent) -> bool {
matches!(
ev,
RemoteControlEvent::Done { .. } | RemoteControlEvent::Settle { .. }
)
}
pub const REMOTE_PERMISSION_DECISION_APPROVED: &str = "approved";
pub const REMOTE_PERMISSION_DECISION_REJECTED: &str = "rejected";
#[derive(Debug, Clone, Default)]
pub struct RemotePermissionResultRequest {
pub request_id: String,
pub decision: String,
pub reason: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct RemoteUserMessageRequest {
pub content: String,
pub request_id: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct RemoteUserMessageAck {
pub ok: bool,
pub request_id: String,
}
#[derive(Debug, Clone, Default)]
pub struct RemoteSessionTokenGrant {
pub access_token: String,
pub session_url: String,
pub tenant_id: String,
pub workspace: Option<String>,
}