use std::path::{Path, PathBuf};
use rho_protocol::{
self as wire, ApprovalAsk, ApprovalSource, Ask, Choice, DefaultAnswer, DefaultStyle, Operation,
PlanApprovalAsk, Question, QuestionAsk, Selection, SessionControlAction,
};
use rho_sdk::{
ApprovalRequest, CapabilityOperation, CapabilitySource, DefaultSelection, HostInputRequest,
HostQuestion, PathScope, SelectionMode, SessionControlAction as SdkAction,
};
use serde_json::Value;
use crate::app::session_runtime::PlanApprovalRequest;
pub(super) fn approval(request: &ApprovalRequest) -> Ask {
let capability = request.capability();
let source = match capability.source() {
CapabilitySource::BuiltInTool { name } => {
ApprovalSource::BuiltInTool { name: name.clone() }
}
CapabilitySource::HostProvidedTool { name } => {
ApprovalSource::HostTool { name: name.clone() }
}
CapabilitySource::PromptConstruction => ApprovalSource::Rho,
_ => ApprovalSource::Unknown,
};
let operation = match capability.operation() {
CapabilityOperation::ReadPath { path, scope } => Operation::ReadPath {
path: wire_path(path),
scope: path_scope(scope),
},
CapabilityOperation::WritePath { path, scope } => Operation::WritePath {
path: wire_path(path),
scope: path_scope(scope),
},
CapabilityOperation::DiscoverInstructions { path, scope } => {
Operation::DiscoverInstructions {
path: wire_path(path),
scope: path_scope(scope),
}
}
CapabilityOperation::ExecuteProcess(execution) => {
let invocation = execution.invocation();
Operation::RunProcess {
program: wire_path(invocation.executable_path()),
args: invocation.arguments().to_vec(),
shell_command: invocation.shell_command().map(str::to_owned),
cwd: wire_path(execution.working_directory()),
}
}
CapabilityOperation::NetworkAccess(target) => Operation::Network {
url: target.url().map(str::to_owned),
},
CapabilityOperation::LoadSkill { name, path } => Operation::LoadSkill {
name: name.clone(),
path: path.as_deref().map(wire_path),
},
CapabilityOperation::ControlSession(target) => Operation::ControlSession {
action: match target.action {
SdkAction::New => SessionControlAction::New,
SdkAction::Prompt => SessionControlAction::Prompt,
SdkAction::Steer => SessionControlAction::Steer,
SdkAction::Cancel => SessionControlAction::Cancel,
SdkAction::Answer => SessionControlAction::Answer,
_ => SessionControlAction::Unknown,
},
session: target
.session
.as_ref()
.map(|session| rho_protocol::SessionId::new(session.as_str())),
workspace: wire_path(&target.workspace),
text: target.text.clone(),
},
_ => Operation::Unknown,
};
Ask::Approval(ApprovalAsk {
source,
operation,
reason: request.reason().to_owned(),
})
}
fn wire_path(path: &Path) -> PathBuf {
PathBuf::from(path.to_string_lossy().into_owned())
}
fn path_scope(scope: &PathScope) -> wire::PathScope {
match scope {
PathScope::PrimaryWorkspace => wire::PathScope::Workspace,
PathScope::GrantedRoot { root } => wire::PathScope::GrantedRoot {
root: wire_path(root),
},
PathScope::UnrestrictedFilesystem => wire::PathScope::Unrestricted,
_ => wire::PathScope::Unknown,
}
}
pub(super) fn plan_approval(request: &PlanApprovalRequest) -> Ask {
Ask::PlanApproval(PlanApprovalAsk {
plan: request.plan.clone(),
modes: request
.targets
.iter()
.copied()
.map(wire::PermissionMode::from)
.collect(),
})
}
pub(super) fn timeout(request: &HostInputRequest, deadline_ms: u64) -> Option<wire::InputTimeout> {
let fallback = request.timeout_fallback()?;
let assumption = request.timeout_reason().map(str::to_owned);
let reason = assumption.clone().unwrap_or_else(|| {
crate::questionnaire::fallback_summary(request.questions(), fallback.answers())
});
Some(wire::InputTimeout {
deadline_ms,
answer: wire::InputAnswer::Answers {
answers: fallback.answers().clone(),
},
reason,
assumption,
})
}
pub(super) fn question(request: &HostInputRequest) -> Ask {
Ask::Question(QuestionAsk {
title: request.title().to_owned(),
reason: request.reason().map(str::to_owned),
questions: request.questions().iter().map(question_to_wire).collect(),
})
}
fn question_to_wire(question: &HostQuestion) -> Question {
Question {
id: question.id().to_owned(),
prompt: question.prompt().to_owned(),
header: question.header().map(str::to_owned),
choices: question
.choices()
.iter()
.map(|choice| Choice {
value: choice.value().to_owned(),
label: choice.label().to_owned(),
description: choice.description().map(str::to_owned),
})
.collect(),
selection: match question.selection() {
SelectionMode::One => Selection::One,
SelectionMode::Many => Selection::Many,
_ => Selection::Unknown,
},
allow_other: question.permits_other(),
help: question.help().map(str::to_owned),
default: default_answer(question),
required: question.is_required(),
}
}
fn default_answer(question: &HostQuestion) -> Option<DefaultAnswer> {
let values: Vec<String> = match question.default_value()? {
Value::Array(values) => values.iter().filter_map(scalar_text).collect(),
value => scalar_text(value).into_iter().collect(),
};
let values: Vec<String> = values
.into_iter()
.map(|text| {
question
.choices()
.iter()
.find(|choice| choice.value() == text || choice.label() == text)
.map_or(text, |choice| choice.value().to_owned())
})
.collect();
if values.is_empty() {
return None;
}
let style = match question.default_selection() {
DefaultSelection::Selected => DefaultStyle::Selected,
DefaultSelection::Focused => DefaultStyle::Focused,
_ => DefaultStyle::Unknown,
};
Some(DefaultAnswer { values, style })
}
fn scalar_text(value: &Value) -> Option<String> {
match value {
Value::String(text) if !text.is_empty() => Some(text.clone()),
Value::Bool(flag) => Some(flag.to_string()),
Value::Number(number) => Some(number.to_string()),
Value::String(_) | Value::Null | Value::Array(_) | Value::Object(_) => None,
}
}
#[cfg(test)]
#[path = "asks_tests.rs"]
mod tests;