use std::collections::BTreeMap;
use ito_config::types::{MemoryConfig, MemoryOpConfig};
use serde_json::Value;
mod rendering;
#[cfg(test)]
mod rendering_tests;
pub use rendering::shell_quote;
pub const DEFAULT_SEARCH_LIMIT: u64 = 10;
#[derive(Debug, Clone, Default)]
pub struct CaptureInputs {
pub context: Option<String>,
pub files: Vec<String>,
pub folders: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct SearchInputs {
pub query: String,
pub limit: Option<u64>,
pub scope: Option<String>,
}
#[derive(Debug, Clone)]
pub struct QueryInputs {
pub query: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
Capture,
Search,
Query,
}
impl Operation {
#[must_use]
pub fn as_key(self) -> &'static str {
match self {
Self::Capture => "capture",
Self::Search => "search",
Self::Query => "query",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RenderedInstruction {
Command {
line: String,
},
Skill {
skill_id: String,
inputs: BTreeMap<String, Value>,
options: Option<Value>,
},
NotConfigured {
operation: Operation,
},
}
#[must_use]
pub fn render_capture(
config: Option<&MemoryConfig>,
inputs: &CaptureInputs,
) -> RenderedInstruction {
let op_cfg = config.and_then(|c| c.capture.as_ref());
let Some(op_cfg) = op_cfg else {
return RenderedInstruction::NotConfigured {
operation: Operation::Capture,
};
};
match op_cfg {
MemoryOpConfig::Command { command } => RenderedInstruction::Command {
line: rendering::render_capture_command(command, inputs),
},
MemoryOpConfig::Skill { skill, options } => RenderedInstruction::Skill {
skill_id: skill.clone(),
inputs: rendering::capture_inputs_as_structured(inputs),
options: options.clone(),
},
}
}
#[must_use]
pub fn render_search(config: Option<&MemoryConfig>, inputs: &SearchInputs) -> RenderedInstruction {
let op_cfg = config.and_then(|c| c.search.as_ref());
let Some(op_cfg) = op_cfg else {
return RenderedInstruction::NotConfigured {
operation: Operation::Search,
};
};
match op_cfg {
MemoryOpConfig::Command { command } => RenderedInstruction::Command {
line: rendering::render_search_command(command, inputs),
},
MemoryOpConfig::Skill { skill, options } => RenderedInstruction::Skill {
skill_id: skill.clone(),
inputs: rendering::search_inputs_as_structured(inputs),
options: options.clone(),
},
}
}
#[must_use]
pub fn render_query(config: Option<&MemoryConfig>, inputs: &QueryInputs) -> RenderedInstruction {
let op_cfg = config.and_then(|c| c.query.as_ref());
let Some(op_cfg) = op_cfg else {
return RenderedInstruction::NotConfigured {
operation: Operation::Query,
};
};
match op_cfg {
MemoryOpConfig::Command { command } => RenderedInstruction::Command {
line: rendering::render_query_command(command, inputs),
},
MemoryOpConfig::Skill { skill, options } => RenderedInstruction::Skill {
skill_id: skill.clone(),
inputs: rendering::query_inputs_as_structured(inputs),
options: options.clone(),
},
}
}