use std::path::Path;
use super::{
agent_instruction_paths, project_instructions::AGENTS_MD_HEADER, push_context_file,
push_delegation_guidance, read_existing_files, skill_listing, RENDERING_GUIDANCE,
};
use crate::{
acp_runtime::agent_tools::McpToolNaming,
skills::{Skill, SkillSource},
tools::sdk_registry::IdleDelivery,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ProjectInstructionFiles {
ReadByAgent,
FromRho,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SkillFiles {
LooseReadByAgent,
FromRho,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ServedToolGuide {
pub(crate) tools: Vec<String>,
pub(crate) skills: Vec<Skill>,
pub(crate) idle: IdleDelivery,
pub(crate) naming: McpToolNaming,
}
impl ServedToolGuide {
fn serves(&self, name: &str) -> bool {
self.tools.iter().any(|tool| tool == name)
}
}
fn served_tools(naming: McpToolNaming) -> String {
format!(
"
# rho's tools
rho serves you tools of its own through the MCP server `rho`, which lists them as `{}`, such as `{}`. Each call runs in this rho session under its permission mode: an approval a call needs, or a question a tool asks, goes to the person through rho, and the call waits for the answer.
Work you start with them in the background, such as a `process` task, an `agent` run or a `workflow` run, outlives your turn, and its result comes as a message saying it is rho's background results. ",
naming.name_of("<name>"),
naming.name_of("questionnaire"),
)
}
fn ui_tool(naming: McpToolNaming) -> String {
format!(
"
# User interface
`{}` shows the person a form, a choice or a result card in rho's transcript. With `wait: true` it returns what they do on it. Use `wait: true` whenever you need their answer now: an action taken after a call returned reaches you later, as a background result naming the surface. Write the call's `express` block in A2UI Express:
",
naming.name_of("ui"),
)
}
const SESSION_COMPLETIONS: &str = "A child's result comes as one of rho's background results. When you need a child's result before you answer, keep working on what does not depend on it, so the result can arrive during this turn, or check on it with `agents` (action `status`), which hands over the result once the run finished. Do not declare the task complete while required children are still running. Subagents share the workspace, so avoid overlapping edits.";
pub(crate) fn agent_session_instructions(
definition: &str,
cwd: &Path,
home: Option<&Path>,
project: ProjectInstructionFiles,
served: Option<&ServedToolGuide>,
skills: SkillFiles,
) -> String {
let mut text = definition.trim_end().to_owned();
text.push('\n');
text.push_str(RENDERING_GUIDANCE);
if let Some(served) = served {
text.push_str(&served_tools(served.naming));
text.push_str(served.idle.clause());
text.push('\n');
if served.serves("ui") {
text.push_str(&ui_tool(served.naming));
text.push_str(crate::tools::ui::express_notation().trim_end());
text.push('\n');
}
if served.serves("agent") {
push_delegation_guidance(&mut text, SESSION_COMPLETIONS);
}
}
let paths = match project {
ProjectInstructionFiles::FromRho => agent_instruction_paths(cwd, &[], home),
ProjectInstructionFiles::ReadByAgent => {
home.map(crate::paths::user_agents_md).into_iter().collect()
}
};
let files = read_existing_files(paths);
if !files.is_empty() {
text.push_str(AGENTS_MD_HEADER);
for (path, contents) in &files {
push_context_file(&mut text, "agents_instructions", path, contents);
}
}
let listed: Vec<Skill> = served
.filter(|served| served.serves("skill"))
.map(|served| listed_skills(&served.skills, skills))
.unwrap_or_default();
if !listed.is_empty() {
text.push_str(&skill_listing(&listed));
}
text
}
fn listed_skills(skills: &[Skill], files: SkillFiles) -> Vec<Skill> {
skills
.iter()
.filter(|skill| !skill.disable_model_invocation)
.filter(|skill| match (files, &skill.source) {
(SkillFiles::FromRho, _) => true,
(SkillFiles::LooseReadByAgent, SkillSource::BuiltIn) => true,
(SkillFiles::LooseReadByAgent, SkillSource::Filesystem { owner, .. }) => {
owner.is_some()
}
})
.cloned()
.collect()
}
#[cfg(test)]
#[path = "agent_session_tests.rs"]
mod tests;