use super::{PermissionRequest, PermissionedAction, RequestedCapability};
use serde_json::{Value, json};
pub(crate) fn requested_capabilities_json(requested: &[RequestedCapability]) -> Value {
let mut network = false;
let mut paths = Vec::new();
let mut integrations = Vec::new();
for capability in requested {
match capability {
RequestedCapability::Network => network = true,
RequestedCapability::Path(path) => {
paths.push(json!({
"path": path.path(),
"access": path.access().as_str(),
}));
}
RequestedCapability::HostIntegration(integration) => {
integrations.push(integration.as_str());
}
}
}
let mut payload = json!({});
if network {
payload["network"] = json!(true);
}
if !paths.is_empty() {
payload["paths"] = Value::Array(paths);
}
if !integrations.is_empty() {
payload["host_integrations"] = json!(integrations);
}
payload
}
pub(crate) fn permissioned_action_json(action: &PermissionedAction) -> Value {
match action {
PermissionedAction::Process(intent) => json!({
"kind": "process",
"command": crate::shell_command_for_argv(intent.argv()),
"cwd": intent.cwd(),
"summary": intent.summary(),
}),
}
}
pub(crate) fn permission_request_json(request: &PermissionRequest) -> Value {
json!({
"tool_call_id": request.tool_call_id().as_str(),
"tool_name": request.tool_name().as_str(),
"reason": request.reason(),
"review_only": request.is_action_review(),
"requested": requested_capabilities_json(request.requested()),
"action": permissioned_action_json(request.action()),
})
}
pub(crate) fn permission_request_fingerprint_json(request: &PermissionRequest) -> Value {
permission_request_json(request)
}