use chrono::{Local, Utc};
use crate::loader::PromptStore;
use crate::ProviderFamily;
#[derive(Debug, Clone)]
enum PromptLayer {
Base,
Provider(ProviderFamily),
Verification,
Environment(EnvironmentContext),
Custom(String),
}
#[derive(Debug, Clone)]
pub struct EnvironmentContext {
pub working_dir: String,
pub platform: String,
pub os_version: Option<String>,
pub is_git_repo: bool,
pub git_branch: Option<String>,
}
impl EnvironmentContext {
pub fn from_current() -> Self {
let working_dir = std::env::current_dir()
.map(|p| p.display().to_string())
.unwrap_or_else(|_| ".".to_string());
Self {
working_dir,
platform: std::env::consts::OS.to_string(),
os_version: None,
is_git_repo: std::path::Path::new(".git").exists(),
git_branch: None,
}
}
pub fn with_working_dir(mut self, dir: impl Into<String>) -> Self {
self.working_dir = dir.into();
self
}
pub fn with_git_repo(mut self, is_repo: bool) -> Self {
self.is_git_repo = is_repo;
self
}
pub fn with_git_branch(mut self, branch: impl Into<String>) -> Self {
self.git_branch = Some(branch.into());
self
}
pub fn with_os_version(mut self, version: impl Into<String>) -> Self {
self.os_version = Some(version.into());
self
}
fn format(&self) -> String {
let mut lines = vec![
"# Environment".to_string(),
format!("Working directory: {}", self.working_dir),
format!("Platform: {}", self.platform),
];
if let Some(ref version) = self.os_version {
lines.push(format!("OS Version: {}", version));
}
lines.push(format!("Is git repo: {}", if self.is_git_repo { "Yes" } else { "No" }));
if let Some(ref branch) = self.git_branch {
lines.push(format!("Git branch: {}", branch));
}
lines.push(format!("Today's date: {}", Local::now().format("%Y-%m-%d")));
lines.push(format!("Current time (UTC): {}", Utc::now().format("%H:%M")));
lines.join("\n")
}
}
impl Default for EnvironmentContext {
fn default() -> Self {
Self::from_current()
}
}
#[derive(Debug)]
pub struct PromptComposer<'a> {
store: &'a PromptStore,
layers: Vec<PromptLayer>,
}
impl<'a> PromptComposer<'a> {
pub fn new(store: &'a PromptStore) -> Self {
Self {
store,
layers: Vec::new(),
}
}
pub fn with_base(mut self) -> Self {
self.layers.push(PromptLayer::Base);
self
}
pub fn with_provider(mut self, family: ProviderFamily) -> Self {
self.layers.push(PromptLayer::Provider(family));
self
}
pub fn with_provider_auto(self, model_id: &str) -> Self {
let family = ProviderFamily::from_model_id(model_id);
self.with_provider(family)
}
pub fn with_verification(mut self) -> Self {
self.layers.push(PromptLayer::Verification);
self
}
pub fn with_verification_if(self, enabled: bool) -> Self {
if enabled {
self.with_verification()
} else {
self
}
}
pub fn with_environment(mut self, context: EnvironmentContext) -> Self {
self.layers.push(PromptLayer::Environment(context));
self
}
pub fn with_current_environment(self) -> Self {
self.with_environment(EnvironmentContext::from_current())
}
pub fn with_custom(mut self, content: impl Into<String>) -> Self {
self.layers.push(PromptLayer::Custom(content.into()));
self
}
pub fn compose(&self) -> String {
let sections: Vec<String> = self
.layers
.iter()
.map(|layer| self.render_layer(layer))
.collect();
sections.join("\n\n")
}
fn render_layer(&self, layer: &PromptLayer) -> String {
match layer {
PromptLayer::Base => self.store.base().to_string(),
PromptLayer::Provider(family) => self.store.provider(*family).to_string(),
PromptLayer::Verification => self.store.verification().to_string(),
PromptLayer::Environment(ctx) => ctx.format(),
PromptLayer::Custom(content) => content.clone(),
}
}
}
pub fn compose_standard(store: &PromptStore, model_id: &str, verification: bool) -> String {
PromptComposer::new(store)
.with_base()
.with_provider_auto(model_id)
.with_current_environment()
.with_verification_if(verification)
.compose()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_basic() {
let store = PromptStore::default();
let prompt = PromptComposer::new(&store)
.with_base()
.compose();
assert!(!prompt.is_empty());
assert!(prompt.contains("CLI tool"));
}
#[test]
fn test_compose_with_provider() {
let store = PromptStore::default();
let prompt = PromptComposer::new(&store)
.with_base()
.with_provider(ProviderFamily::Anthropic)
.compose();
assert!(prompt.contains("AI"));
assert!(prompt.len() > store.base().len());
}
#[test]
fn test_compose_auto_provider() {
let store = PromptStore::default();
let claude_prompt = PromptComposer::new(&store)
.with_base()
.with_provider_auto("claude-3-opus")
.compose();
let gpt_prompt = PromptComposer::new(&store)
.with_base()
.with_provider_auto("gpt-4")
.compose();
assert_ne!(claude_prompt, gpt_prompt);
}
#[test]
fn test_compose_with_environment() {
let store = PromptStore::default();
let ctx = EnvironmentContext::default()
.with_working_dir("/test/project")
.with_git_repo(true)
.with_git_branch("main");
let prompt = PromptComposer::new(&store)
.with_base()
.with_environment(ctx)
.compose();
assert!(prompt.contains("/test/project"));
assert!(prompt.contains("main"));
}
#[test]
fn test_compose_with_verification() {
let store = PromptStore::default();
let with_verify = PromptComposer::new(&store)
.with_base()
.with_verification()
.compose();
let without_verify = PromptComposer::new(&store)
.with_base()
.with_verification_if(false)
.compose();
assert!(with_verify.len() > without_verify.len());
assert!(with_verify.contains("verification") || with_verify.contains("Verification"));
}
#[test]
fn test_compose_with_custom() {
let store = PromptStore::default();
let prompt = PromptComposer::new(&store)
.with_base()
.with_custom("# Custom Section\nThis is custom content.")
.compose();
assert!(prompt.contains("Custom Section"));
assert!(prompt.contains("custom content"));
}
#[test]
fn test_compose_standard() {
let store = PromptStore::default();
let prompt = compose_standard(&store, "claude-3-sonnet", true);
assert!(!prompt.is_empty());
assert!(prompt.contains("Environment"));
}
#[test]
fn test_environment_context() {
let ctx = EnvironmentContext::default()
.with_working_dir("/home/user/project")
.with_git_repo(true)
.with_git_branch("feature-branch")
.with_os_version("Ubuntu 22.04");
let formatted = ctx.format();
assert!(formatted.contains("/home/user/project"));
assert!(formatted.contains("feature-branch"));
assert!(formatted.contains("Ubuntu 22.04"));
assert!(formatted.contains("Yes")); }
}