use anyhow::Result;
use tokio::sync::mpsc;
use crate::api::{ApiEvent, Message, Provider, ToolDefinition};
use crate::permissions::PermissionMode;
use crate::query::{Engine, SteeringQueue};
pub struct ScriptedProvider {
pub calls: std::sync::atomic::AtomicUsize,
pub first_round_text: Option<String>,
pub first_round: Vec<(String, String, serde_json::Value)>,
pub push_on_first_call: Option<(SteeringQueue, String)>,
}
#[async_trait::async_trait]
impl Provider for ScriptedProvider {
fn name(&self) -> &str {
"scripted-mock"
}
fn model(&self) -> &str {
"test-model"
}
fn set_model(&mut self, _model: &str) {}
async fn stream(
&self,
_messages: &[Message],
_system: &str,
_tools: &[ToolDefinition],
_max_tokens: u32,
) -> Result<mpsc::Receiver<ApiEvent>> {
let call = self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let (tx, rx) = mpsc::channel(10);
if call == 0 {
if let Some((queue, text)) = &self.push_on_first_call {
queue.lock().unwrap().push_back(text.clone());
}
if let Some(text) = &self.first_round_text {
let _ = tx.send(ApiEvent::Text(text.clone())).await;
}
for (id, name, input) in &self.first_round {
let _ = tx
.send(ApiEvent::ToolUse {
id: id.clone(),
name: name.clone(),
input: input.clone(),
})
.await;
}
}
let _ = tx.send(ApiEvent::Done).await;
Ok(rx)
}
}
pub fn scripted_engine(
first_round: Vec<(String, String, serde_json::Value)>,
push_on_first_call: Option<String>,
mode: PermissionMode,
) -> Engine {
let steering = SteeringQueue::default();
let provider = Box::new(ScriptedProvider {
calls: std::sync::atomic::AtomicUsize::new(0),
first_round_text: Some("working on it".to_string()),
first_round,
push_on_first_call: push_on_first_call.map(|t| (steering.clone(), t)),
});
Engine::for_tests(provider, steering, mode)
}
pub fn tool_use(
id: &str,
name: &str,
input: serde_json::Value,
) -> (String, String, serde_json::Value) {
(id.to_string(), name.to_string(), input)
}