use std::sync::atomic::{AtomicU64, Ordering};
use agent_client_protocol::v1::schema::{
PermissionOption, PermissionOptionKind, RequestPermissionOutcome, RequestPermissionRequest,
SessionId, SessionModeId, ToolCallContent, ToolCallId, ToolCallUpdate, ToolCallUpdateFields,
ToolKind,
};
use rho_protocol::{self as wire, ApprovalAsk, InputAnswer, Operation, PlanApprovalAsk};
use crate::{permission::PermissionMode, tools::plan_exit};
const OPTION_ALLOW_ONCE: &str = "allow_once";
const OPTION_ALLOW_ALWAYS: &str = "allow_always";
const OPTION_REJECT_ONCE: &str = "reject_once";
const OPTION_KEEP_PLANNING: &str = "keep_planning";
pub(super) fn mode_id(mode: PermissionMode) -> SessionModeId {
SessionModeId::new(mode.as_str())
}
pub(super) fn parse_mode_id(id: &str) -> Option<PermissionMode> {
PermissionMode::ALL
.into_iter()
.find(|mode| mode.as_str() == id)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Card<'a> {
Call(&'a wire::ToolCallId),
Placeholder(String),
}
impl<'a> Card<'a> {
pub(super) fn of(tool_call: Option<&'a wire::ToolCallId>, counter: &AtomicU64) -> Self {
match tool_call {
Some(id) => Self::Call(id),
None => Self::Placeholder(format!(
"pending-{}",
counter.fetch_add(1, Ordering::Relaxed) + 1
)),
}
}
fn id(&self) -> ToolCallId {
match self {
Self::Call(id) => ToolCallId::new(id.as_str()),
Self::Placeholder(id) => ToolCallId::new(id.as_str()),
}
}
}
pub(super) fn permission_request(
session_id: &SessionId,
ask: &ApprovalAsk,
card: &Card<'_>,
) -> RequestPermissionRequest {
let fields = ToolCallUpdateFields::new();
let fields = match ask.reason.as_str() {
"" => fields,
reason => fields.title(reason.to_owned()),
};
let fields = match card {
Card::Call(_) => fields,
Card::Placeholder(_) => fields.kind(tool_kind_for_operation(&ask.operation)),
};
RequestPermissionRequest::new(
session_id.clone(),
ToolCallUpdate::new(card.id(), fields),
permission_options(),
)
}
pub(super) fn approval_answer(outcome: &RequestPermissionOutcome) -> Option<InputAnswer> {
let selected = match outcome {
RequestPermissionOutcome::Cancelled => return None,
RequestPermissionOutcome::Selected(selected) => selected,
_ => {
return Some(InputAnswer::Deny {
reason: Some("unknown permission outcome".into()),
});
}
};
Some(match selected.option_id.0.as_ref() {
OPTION_ALLOW_ONCE => InputAnswer::AllowOnce,
OPTION_ALLOW_ALWAYS => InputAnswer::AllowForSession,
OPTION_REJECT_ONCE => InputAnswer::Deny { reason: None },
_ => InputAnswer::Deny {
reason: Some("unknown permission option".into()),
},
})
}
fn permission_options() -> Vec<PermissionOption> {
vec![
PermissionOption::new(
OPTION_ALLOW_ONCE,
"Allow once",
PermissionOptionKind::AllowOnce,
),
PermissionOption::new(
OPTION_ALLOW_ALWAYS,
"Allow for this session",
PermissionOptionKind::AllowAlways,
),
PermissionOption::new(
OPTION_REJECT_ONCE,
"Reject once",
PermissionOptionKind::RejectOnce,
),
]
}
fn tool_kind_for_operation(operation: &Operation) -> ToolKind {
match operation {
Operation::ReadPath { .. } | Operation::DiscoverInstructions { .. } => ToolKind::Read,
Operation::WritePath { .. } => ToolKind::Edit,
Operation::RunProcess { .. } => ToolKind::Execute,
Operation::Network { .. } => ToolKind::Fetch,
Operation::LoadSkill { .. } | Operation::ControlSession { .. } => ToolKind::Other,
_ => ToolKind::Other,
}
}
pub(super) fn plan_approval_request(
session_id: &SessionId,
ask: &PlanApprovalAsk,
card: &Card<'_>,
) -> RequestPermissionRequest {
let mut options = ask
.modes
.iter()
.filter_map(|&mode| PermissionMode::try_from(mode).ok())
.map(|mode| {
let kind = match mode {
PermissionMode::Supervised => PermissionOptionKind::AllowOnce,
PermissionMode::AllowEdits | PermissionMode::Auto | PermissionMode::Bypass => {
PermissionOptionKind::AllowAlways
}
PermissionMode::Plan => PermissionOptionKind::RejectOnce,
};
PermissionOption::new(mode.as_str(), plan_exit::approval_label(mode), kind)
})
.collect::<Vec<_>>();
options.push(PermissionOption::new(
OPTION_KEEP_PLANNING,
"Keep planning",
PermissionOptionKind::RejectOnce,
));
RequestPermissionRequest::new(
session_id.clone(),
ToolCallUpdate::new(
card.id(),
ToolCallUpdateFields::new()
.title("Plan ready. How should Rho continue?")
.kind(ToolKind::SwitchMode)
.content(vec![ToolCallContent::from(ask.plan.clone())]),
),
options,
)
}
pub(super) fn plan_answer(outcome: &RequestPermissionOutcome) -> Option<InputAnswer> {
let selected = match outcome {
RequestPermissionOutcome::Cancelled => return None,
RequestPermissionOutcome::Selected(selected) => selected,
_ => return Some(InputAnswer::KeepPlanning { feedback: None }),
};
Some(match parse_mode_id(selected.option_id.0.as_ref()) {
Some(mode) => InputAnswer::ApprovePlan {
mode: mode.into(),
feedback: None,
},
None => InputAnswer::KeepPlanning { feedback: None },
})
}
#[cfg(test)]
#[path = "permission_tests.rs"]
mod tests;