use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use serde_json::Value;
use crate::message::Message;
#[derive(Clone, Debug, PartialEq)]
pub struct ToolSchema {
pub name: String,
pub description: String,
pub parameters: Value,
}
impl ToolSchema {
pub fn new(name: impl Into<String>, description: impl Into<String>, parameters: Value) -> Self {
Self {
name: name.into(),
description: description.into(),
parameters,
}
}
}
pub type Cancel = Arc<AtomicBool>;
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct Request {
pub instructions: String,
pub messages: Vec<Message>,
pub tools: Vec<ToolSchema>,
pub max_output_tokens: Option<u32>,
pub temperature: Option<f32>,
pub side_call: bool,
pub model: Option<String>,
pub cancel: Option<Cancel>,
}
impl Request {
pub fn new(instructions: impl Into<String>) -> Self {
Self {
instructions: instructions.into(),
..Self::default()
}
}
pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
self.instructions = instructions.into();
self
}
pub fn message(mut self, message: Message) -> Self {
self.messages.push(message);
self
}
pub fn user(self, text: impl Into<String>) -> Self {
self.message(Message::user(text))
}
pub fn assistant(self, text: impl Into<String>) -> Self {
self.message(Message::assistant(text))
}
pub fn messages(mut self, messages: impl IntoIterator<Item = Message>) -> Self {
self.messages.extend(messages);
self
}
pub fn tool(mut self, tool: ToolSchema) -> Self {
self.tools.push(tool);
self
}
pub fn tools(mut self, tools: impl IntoIterator<Item = ToolSchema>) -> Self {
self.tools.extend(tools);
self
}
pub fn max_output_tokens(mut self, tokens: u32) -> Self {
self.max_output_tokens = Some(tokens);
self
}
pub fn temperature(mut self, temperature: f32) -> Self {
self.temperature = Some(temperature);
self
}
pub fn side_call(mut self, side: bool) -> Self {
self.side_call = side;
self
}
pub fn model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn cancel(mut self, cancel: Cancel) -> Self {
self.cancel = Some(cancel);
self
}
pub fn cancelled(&self) -> bool {
self.cancel
.as_ref()
.is_some_and(|flag| flag.load(Ordering::Relaxed))
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn a_request_reads_back_what_was_chained_onto_it() {
let request = Request::new("Be brief")
.user("hello")
.assistant("hi")
.tool(ToolSchema::new(
"now",
"the time",
json!({"type": "object"}),
))
.max_output_tokens(64)
.temperature(0.2)
.model("some-model")
.side_call(true);
assert_eq!(request.instructions, "Be brief");
assert_eq!(request.messages.len(), 2);
assert_eq!(request.tools[0].name, "now");
assert_eq!(request.max_output_tokens, Some(64));
assert_eq!(request.temperature, Some(0.2));
assert_eq!(request.model.as_deref(), Some("some-model"));
assert!(request.side_call);
}
#[test]
fn cancellation_is_off_until_the_caller_raises_it() {
let flag = Cancel::default();
let request = Request::new("x").cancel(flag.clone());
assert!(!request.cancelled());
flag.store(true, Ordering::Relaxed);
assert!(request.cancelled());
assert!(!Request::new("x").cancelled());
}
}