use super::*;
pub(in crate::tool_host) fn engine(
root: &Path,
reply: &Value,
) -> (JoinHandle<()>, Receiver<Value>) {
let root = root.to_path_buf();
let reply = reply.clone();
let (tx, rx) = std::sync::mpsc::channel();
let handle = std::thread::spawn(move || {
for _ in 0..4000 {
if let Some((id, path)) = deposit::pending(&root).into_iter().next() {
let request = std::fs::read(&path)
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or(Value::Null);
let _ = deposit::claim(&root, &id);
let _ = deposit::write_reply(&root, &id, &reply);
let _ = tx.send(request);
return;
}
std::thread::sleep(Duration::from_millis(1));
}
});
(handle, rx)
}
pub(in crate::tool_host) fn budget() -> ask::Budget {
ask::Budget {
waits: 4000,
tick: Duration::from_millis(1),
}
}
pub(in crate::tool_host) fn impatient() -> ask::Budget {
ask::Budget {
waits: 1,
tick: Duration::ZERO,
}
}
pub(in crate::tool_host) fn front_door(dir: &Path, body: &str) -> PathBuf {
let path = dir.join("litany");
crate::test_support::write_exec(&path, &format!("#!/bin/sh\n{body}\n"));
path
}
pub(in crate::tool_host) fn tool(name: &str) -> Tool {
Tool {
name: name.to_owned(),
description: format!("what {name} does"),
input_schema: json!({"type": "object"}),
subject_cwd: false,
}
}
pub(in crate::tool_host) fn site(root: &Path, budget: ask::Budget) -> Site {
Site {
state_root: root.to_path_buf(),
workspace: "home".to_owned(),
agent: "dulcet-mongoose".to_owned(),
budget,
patience: budget,
clock: FakeClock::new().arc(),
}
}
pub(in crate::tool_host) fn scripted(
root: &Path,
replies: &[Value],
) -> (JoinHandle<()>, Receiver<Value>) {
let root = root.to_path_buf();
let replies = replies.to_vec();
let (tx, rx) = std::sync::mpsc::channel();
let handle = std::thread::spawn(move || {
let mut said = 0;
for _ in 0..40_000 {
let Some(reply) = replies.get(said) else {
return;
};
if let Some((id, path)) = deposit::pending(&root).into_iter().next() {
let request = std::fs::read(&path)
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or(Value::Null);
let _ = deposit::claim(&root, &id);
let _ = deposit::write_reply(&root, &id, reply);
let _ = tx.send(request);
said += 1;
}
std::thread::sleep(Duration::from_millis(1));
}
});
(handle, rx)
}