#[cfg(feature = "skills")]
use std::future::Future;
use askama::Template;
use super::source::{Block, ContextSource};
#[derive(Template)]
#[template(path = "system_reminder.txt", escape = "none")]
struct SystemReminderTemplate {
content: String,
}
fn block(tag: &str, template: &impl Template) -> Block {
Block::new(
tag,
template
.render()
.expect("context block template must render"),
)
}
#[derive(Debug)]
pub struct BackgroundJobs {
registry: aither_sandbox::JobRegistry,
}
impl BackgroundJobs {
#[must_use]
pub const fn new(registry: aither_sandbox::JobRegistry) -> Self {
Self { registry }
}
}
impl ContextSource for BackgroundJobs {
async fn render(&self) -> Option<Block> {
let running = self.registry.format_running_jobs().await;
if running.is_empty() {
return None;
}
Some(block(
"system_reminder",
&SystemReminderTemplate {
content: format!(
"Running background terminals:\n{running}Use terminal_read for incremental output, read redirected output files via terminal commands like head/tail/rg/cat when needed, terminal_input for stdin, and terminal_kill to stop tasks."
),
},
))
}
}
#[cfg(feature = "skills")]
#[derive(Template)]
#[template(path = "skill_instruction.txt", escape = "none")]
struct SkillInstructionTemplate<'a> {
name: &'a str,
description: &'a str,
instructions: &'a str,
allowed_tools: Option<&'a [String]>,
resource_paths: Option<&'a [String]>,
}
#[cfg(feature = "skills")]
#[derive(Debug, Default)]
pub struct ActiveSkills {
skills: Vec<aither_skills::Skill>,
}
#[cfg(feature = "skills")]
impl ActiveSkills {
#[must_use]
pub const fn new() -> Self {
Self { skills: Vec::new() }
}
pub fn set(&mut self, skills: Vec<aither_skills::Skill>) {
self.skills = skills;
}
#[must_use]
pub fn skills(&self) -> &[aither_skills::Skill] {
&self.skills
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.skills.is_empty()
}
}
#[cfg(feature = "skills")]
impl ContextSource for ActiveSkills {
fn render(&self) -> impl Future<Output = Option<Block>> + Send {
std::future::ready(self.render_skills())
}
}
#[cfg(feature = "skills")]
impl ActiveSkills {
fn render_skills(&self) -> Option<Block> {
if self.skills.is_empty() {
return None;
}
let mut rendered = String::new();
for skill in &self.skills {
let mut resource_paths = skill.resources.keys().cloned().collect::<Vec<_>>();
resource_paths.sort();
let instruction = SkillInstructionTemplate {
name: &skill.name,
description: &skill.description,
instructions: &skill.instructions,
allowed_tools: skill.allowed_tools.as_deref(),
resource_paths: (!resource_paths.is_empty()).then_some(resource_paths.as_slice()),
}
.render()
.expect("skill instruction template must render");
if !rendered.is_empty() {
rendered.push('\n');
}
rendered.push_str(&instruction);
}
Some(Block::new("skill_instructions", rendered))
}
}