#![allow(dead_code)]
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, MutexGuard};
use jiff::{SignedDuration, Timestamp};
use vpf::Plan;
use vpf_runner::{AgentAnswer, AgentRequest, Ask, Clock, Person, Role, Verdict};
pub fn plan(text: &str) -> Plan {
let report = vpf::check(text);
assert!(report.problems.is_empty(), "{:?}", report.problems);
report.plan.expect("a plan")
}
#[derive(Default)]
pub struct Scripted {
pub replies: VecDeque<String>,
pub heard: Vec<String>,
pub asks: Vec<Ask>,
}
impl Scripted {
pub fn new(replies: &[&str]) -> Self {
Self {
replies: replies.iter().map(|&reply| reply.to_owned()).collect(),
..Self::default()
}
}
pub fn all(&self) -> String {
self.heard.join("\n")
}
}
impl Person for Scripted {
fn tell(&mut self, text: &str) {
self.heard.push(text.to_owned());
}
fn answer(&mut self, ask: &Ask) -> Option<String> {
self.heard.push(ask.question.clone());
self.asks.push(ask.clone());
self.replies.pop_front()
}
fn approve(&mut self, ask: &Ask) -> Option<Verdict> {
self.heard.push(ask.question.clone());
self.asks.push(ask.clone());
let reply = self.replies.pop_front()?;
let Some(rest) = reply.strip_prefix("no") else {
return Some(Verdict::Yes);
};
let (back, points) = match rest.strip_prefix('>') {
Some(rest) => {
let (ids, points) = rest.split_once(':').unwrap_or((rest, ""));
(
ids.split(',').map(|id| id.trim().to_owned()).collect(),
points,
)
}
None => (Vec::new(), rest.trim_start_matches(':')),
};
Some(Verdict::No {
points: points.trim().to_owned(),
back,
})
}
fn choose(&mut self, question: &str, options: &[String]) -> Option<usize> {
self.heard.push(question.to_owned());
let reply = self.replies.pop_front()?;
options.iter().position(|option| *option == reply)
}
}
#[derive(Clone, Default)]
pub struct Requests(Arc<Mutex<Vec<Request>>>);
impl Requests {
pub fn borrow(&self) -> MutexGuard<'_, Vec<Request>> {
self.0.lock().expect("the requests")
}
}
#[derive(Clone, Debug)]
pub struct Request {
pub role: Role,
pub prompt: String,
pub agent: Option<String>,
pub tools: Vec<&'static str>,
pub step: String,
pub key: String,
pub conversation: Option<String>,
pub cost_left: Option<f64>,
pub keep: Option<std::path::PathBuf>,
}
pub fn agent(
answer: impl FnMut(&AgentRequest<'_>, usize) -> AgentAnswer + Send,
) -> (
impl Fn(&AgentRequest<'_>) -> Result<AgentAnswer, String> + Sync,
Requests,
) {
let requests = Requests::default();
let kept = requests.clone();
let answer = Mutex::new(answer);
let agent = move |request: &AgentRequest<'_>| {
let mut answer = answer.lock().expect("the agent");
let count = {
let mut kept = kept.borrow();
kept.push(Request {
role: request.role,
prompt: request.prompt.clone(),
agent: request.agent.map(str::to_owned),
tools: request.tools.clone(),
step: request.step.to_owned(),
key: request.key.to_owned(),
conversation: request.conversation.map(str::to_owned),
cost_left: request.cost_left,
keep: request.keep.map(std::path::Path::to_path_buf),
});
kept.len() - 1
};
Ok(answer(request, count))
};
(agent, requests)
}
pub fn said(text: &str) -> AgentAnswer {
AgentAnswer::new(text).with_cost(0.0)
}
pub fn no_agent() -> impl Fn(&AgentRequest<'_>) -> Result<AgentAnswer, String> + Sync {
|_: &AgentRequest<'_>| Err("no agent in this test".to_owned())
}
#[derive(Default)]
pub struct Shared<T>(Arc<Mutex<T>>);
impl<T> Clone for Shared<T> {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}
impl<T> Shared<T> {
pub fn new(value: T) -> Self {
Self(Arc::new(Mutex::new(value)))
}
pub fn borrow(&self) -> MutexGuard<'_, T> {
self.0.lock().expect("the value")
}
pub fn borrow_mut(&self) -> MutexGuard<'_, T> {
self.borrow()
}
}
#[derive(Clone)]
pub struct TestClock {
pub now: Shared<Timestamp>,
pub waits: Shared<Vec<Timestamp>>,
}
impl TestClock {
pub fn at(text: &str) -> Self {
Self {
now: Shared::new(text.parse().expect("a time")),
waits: Shared::default(),
}
}
pub fn pass(&self, duration: SignedDuration) {
let now = *self.now.borrow();
*self.now.borrow_mut() = now + duration;
}
}
impl Clock for TestClock {
fn now(&self) -> Timestamp {
*self.now.borrow()
}
fn wait_until(&mut self, at: Timestamp) {
self.waits.borrow_mut().push(at);
let mut now = self.now.borrow_mut();
if at > *now {
*now = at;
}
}
}
pub fn at(text: &str) -> Timestamp {
text.parse().expect("a time")
}