use std::path::PathBuf;
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::perf::TokenUsage;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HistoryMessage {
pub role: String,
pub content: String,
}
impl HistoryMessage {
#[allow(dead_code)]
pub fn user(content: impl Into<String>) -> Self {
Self {
role: "user".to_string(),
content: content.into(),
}
}
#[allow(dead_code)]
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: "assistant".to_string(),
content: content.into(),
}
}
}
#[derive(Debug, Default, Clone)]
pub struct RunContext {
pub assigned_file: Option<PathBuf>,
pub max_turns_override: Option<u32>,
pub working_dir: Option<PathBuf>,
pub model: Option<String>,
}
#[derive(Debug, Clone)]
pub struct AgentOutput {
pub content: String,
pub summary: Option<String>,
pub usage: TokenUsage,
}
impl AgentOutput {
#[allow(dead_code)]
pub fn from_content(content: impl Into<String>) -> Self {
Self {
content: content.into(),
summary: None,
usage: TokenUsage::default(),
}
}
#[allow(dead_code)]
pub fn summary_or_content(&self) -> &str {
self.summary.as_deref().unwrap_or(&self.content)
}
}
#[async_trait]
pub trait AgentRunner: Send + Sync {
async fn run(&self, agent_name: &str, task: &str) -> Result<AgentOutput>;
async fn run_with_history(
&self,
agent_name: &str,
task: &str,
_history: &[HistoryMessage],
ctx: &RunContext,
) -> Result<AgentOutput> {
self.run_with_context(agent_name, task, ctx).await
}
async fn run_with_context(
&self,
agent_name: &str,
task: &str,
_ctx: &RunContext,
) -> Result<AgentOutput> {
self.run(agent_name, task).await
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use anyhow::Result;
use async_trait::async_trait;
use super::{AgentOutput, AgentRunner, HistoryMessage, RunContext, TokenUsage};
struct CtxCapture {
captured: Arc<Mutex<Vec<RunContext>>>,
}
#[async_trait]
impl AgentRunner for CtxCapture {
async fn run(&self, _agent_name: &str, _task: &str) -> Result<AgentOutput> {
Ok(AgentOutput {
content: "run".into(),
summary: None,
usage: TokenUsage::default(),
})
}
async fn run_with_context(
&self,
_agent_name: &str,
_task: &str,
ctx: &RunContext,
) -> Result<AgentOutput> {
self.captured.lock().unwrap().push(ctx.clone());
Ok(AgentOutput {
content: "run_with_context".into(),
summary: None,
usage: TokenUsage::default(),
})
}
}
#[tokio::test]
async fn test_run_with_history_forwards_ctx() {
let captured = Arc::new(Mutex::new(Vec::new()));
let runner = CtxCapture {
captured: captured.clone(),
};
let ctx = RunContext {
working_dir: Some(PathBuf::from("/tmp/test-wd")),
model: Some("anthropic/claude-opus-4-5".to_string()),
max_turns_override: Some(5),
assigned_file: None,
};
let out = runner
.run_with_history("test-agent", "do something", &[], &ctx)
.await
.expect("run_with_history should succeed");
assert_eq!(
out.content, "run_with_context",
"default run_with_history must call run_with_context, not run()"
);
let snaps = captured.lock().unwrap();
assert_eq!(
snaps.len(),
1,
"run_with_context should be called exactly once"
);
let recorded = &snaps[0];
assert_eq!(
recorded.working_dir,
Some(PathBuf::from("/tmp/test-wd")),
"working_dir must be forwarded through run_with_history"
);
assert_eq!(
recorded.model,
Some("anthropic/claude-opus-4-5".to_string()),
"model must be forwarded through run_with_history"
);
assert_eq!(
recorded.max_turns_override,
Some(5),
"max_turns_override must be forwarded through run_with_history"
);
}
#[test]
fn history_message_constructors() {
let u = HistoryMessage::user("hello");
assert_eq!(u.role, "user");
assert_eq!(u.content, "hello");
let a = HistoryMessage::assistant("world");
assert_eq!(a.role, "assistant");
assert_eq!(a.content, "world");
}
#[test]
fn agent_output_from_content() {
let out = AgentOutput::from_content("test output");
assert_eq!(out.content, "test output");
assert!(out.summary.is_none());
assert_eq!(out.usage, TokenUsage::default());
}
#[test]
fn agent_output_summary_or_content_fallback() {
let out = AgentOutput::from_content("fallback content");
assert_eq!(out.summary_or_content(), "fallback content");
let out_with_summary = AgentOutput {
content: "full content".into(),
summary: Some("short summary".into()),
usage: TokenUsage::default(),
};
assert_eq!(out_with_summary.summary_or_content(), "short summary");
}
}