use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::mpsc::UnboundedSender;
use crate::cancel::CancelToken;
use crate::session::AgentEvent;
use crate::tool::ToolSpec;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Role {
System,
User,
Assistant,
Tool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
pub id: String,
pub name: String,
#[serde(default)]
pub arguments: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Content {
Text(String),
Parts(Vec<ContentPart>),
}
impl Default for Content {
fn default() -> Self {
Content::Text(String::new())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ContentPart {
Text {
text: String,
},
ImageUrl {
image_url: ImageUrl,
},
InputAudio {
input_audio: InputAudio,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ImageUrl {
pub url: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InputAudio {
pub data: String,
pub format: String,
}
impl ContentPart {
pub fn text(text: impl Into<String>) -> Self {
ContentPart::Text { text: text.into() }
}
pub fn image(url: impl Into<String>) -> Self {
ContentPart::ImageUrl { image_url: ImageUrl { url: url.into(), detail: None } }
}
pub fn audio(data: impl Into<String>, format: impl Into<String>) -> Self {
ContentPart::InputAudio { input_audio: InputAudio { data: data.into(), format: format.into() } }
}
pub fn needs(&self) -> Modalities {
match self {
ContentPart::Text { .. } => Modalities::TEXT,
ContentPart::ImageUrl { .. } => Modalities::VISION,
ContentPart::InputAudio { .. } => Modalities::AUDIO,
}
}
}
impl Content {
pub fn text(&self) -> std::borrow::Cow<'_, str> {
match self {
Content::Text(t) => std::borrow::Cow::Borrowed(t),
Content::Parts(parts) => parts
.iter()
.filter_map(|p| match p {
ContentPart::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<String>()
.into(),
}
}
pub fn as_text(&self) -> Option<&str> {
match self {
Content::Text(t) => Some(t),
Content::Parts(_) => None,
}
}
pub fn into_text(self) -> String {
match self {
Content::Text(t) => t,
other => other.text().into_owned(),
}
}
pub fn parts(&self) -> Vec<ContentPart> {
match self {
Content::Text(t) => vec![ContentPart::text(t.clone())],
Content::Parts(p) => p.clone(),
}
}
pub fn is_empty(&self) -> bool {
match self {
Content::Text(t) => t.is_empty(),
Content::Parts(p) => p.is_empty(),
}
}
pub fn needs(&self) -> Modalities {
match self {
Content::Text(_) => Modalities::TEXT,
Content::Parts(p) => p.iter().fold(Modalities::TEXT, |m, part| m.union(part.needs())),
}
}
pub fn normalized(self) -> Self {
match self {
Content::Parts(p) if p.iter().all(|x| matches!(x, ContentPart::Text { .. })) => Content::Parts(p).into_text().into(),
other => other,
}
}
pub fn check(&self) -> Result<(), String> {
let Content::Parts(parts) = self else { return Ok(()) };
for p in parts {
match p {
ContentPart::ImageUrl { image_url } if !image_url.url.starts_with("data:image/") => {
return Err("image_url must be a data:image/... URL; remote image URLs are not fetched".into());
}
ContentPart::InputAudio { input_audio } if input_audio.format.is_empty() || input_audio.data.is_empty() => {
return Err("input_audio needs data and a format".into());
}
_ => {}
}
}
Ok(())
}
}
impl From<String> for Content {
fn from(s: String) -> Self {
Content::Text(s)
}
}
impl From<&str> for Content {
fn from(s: &str) -> Self {
Content::Text(s.to_string())
}
}
impl From<&String> for Content {
fn from(s: &String) -> Self {
Content::Text(s.clone())
}
}
impl From<Vec<ContentPart>> for Content {
fn from(p: Vec<ContentPart>) -> Self {
Content::Parts(p)
}
}
impl PartialEq<str> for Content {
fn eq(&self, other: &str) -> bool {
self.as_text() == Some(other)
}
}
impl PartialEq<&str> for Content {
fn eq(&self, other: &&str) -> bool {
self.as_text() == Some(*other)
}
}
impl std::fmt::Display for Content {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.text())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Modalities {
#[serde(default, skip_serializing_if = "is_false")]
pub vision: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub audio: bool,
}
fn is_false(b: &bool) -> bool {
!*b
}
impl Modalities {
pub const TEXT: Self = Self { vision: false, audio: false };
pub const VISION: Self = Self { vision: true, audio: false };
pub const AUDIO: Self = Self { vision: false, audio: true };
pub fn new(vision: bool, audio: bool) -> Self {
Self { vision, audio }
}
pub fn is_text(&self) -> bool {
!self.vision && !self.audio
}
pub fn union(self, other: Self) -> Self {
Self { vision: self.vision || other.vision, audio: self.audio || other.audio }
}
pub fn covers(&self, needed: Modalities) -> bool {
(self.vision || !needed.vision) && (self.audio || !needed.audio)
}
pub fn lacking(&self, needed: Modalities) -> Option<&'static str> {
match (needed.vision && !self.vision, needed.audio && !self.audio) {
(true, true) => Some("images and audio"),
(true, false) => Some("images"),
(false, true) => Some("audio"),
(false, false) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: Content,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tool_calls: Vec<ToolCall>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
}
impl Message {
pub fn system(content: impl Into<Content>) -> Self {
Self { role: Role::System, content: content.into(), tool_calls: Vec::new(), tool_call_id: None }
}
pub fn user(content: impl Into<Content>) -> Self {
Self { role: Role::User, content: content.into(), tool_calls: Vec::new(), tool_call_id: None }
}
pub fn assistant(content: impl Into<Content>, tool_calls: Vec<ToolCall>) -> Self {
Self { role: Role::Assistant, content: content.into(), tool_calls, tool_call_id: None }
}
pub fn tool(call_id: impl Into<String>, content: impl Into<Content>) -> Self {
Self { role: Role::Tool, content: content.into(), tool_calls: Vec::new(), tool_call_id: Some(call_id.into()) }
}
pub fn text(&self) -> std::borrow::Cow<'_, str> {
self.content.text()
}
pub fn needs(&self) -> Modalities {
self.content.needs()
}
}
pub fn needs(messages: &[Message]) -> Modalities {
messages.iter().fold(Modalities::TEXT, |m, msg| m.union(msg.needs()))
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ModelTurn {
pub text: String,
#[serde(default)]
pub tool_calls: Vec<ToolCall>,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ModelError {
#[error("model unavailable: {0}")]
Unavailable(String),
#[error("model failed: {0}")]
Failed(String),
#[error("cancelled")]
Cancelled,
}
#[derive(Debug, Clone)]
pub struct DeltaSink {
tx: Option<UnboundedSender<AgentEvent>>,
}
impl DeltaSink {
pub fn new(tx: Option<UnboundedSender<AgentEvent>>) -> Self {
Self { tx }
}
pub fn discard() -> Self {
Self { tx: None }
}
pub fn text(&self, delta: impl Into<String>) {
if let Some(tx) = &self.tx {
let _ = tx.send(AgentEvent::TextDelta(delta.into()));
}
}
pub fn reasoning(&self, delta: impl Into<String>) {
if let Some(tx) = &self.tx {
let _ = tx.send(AgentEvent::ReasoningDelta(delta.into()));
}
}
}
#[async_trait]
pub trait ModelBackend: Send + Sync {
fn name(&self) -> String;
async fn complete(&self, messages: &[Message], tools: &[ToolSpec], deltas: &DeltaSink, cancel: &CancelToken) -> Result<ModelTurn, ModelError>;
}
#[cfg(test)]
mod tests {
use super::*;
const PNG: &str = "data:image/png;base64,iVBORw0KGgo=";
#[test]
fn text_content_keeps_the_plain_string_wire_form() {
let m = Message::user("hi");
assert_eq!(serde_json::to_value(&m).unwrap(), serde_json::json!({"role": "user", "content": "hi"}));
let cbor = crate::remote::to_cbor(&m).unwrap();
#[derive(Deserialize)]
struct Old {
content: String,
}
assert_eq!(crate::remote::from_cbor::<Old>(&cbor).unwrap().content, "hi", "a 0.4 peer reads it");
let old = serde_json::json!({"role": "user", "content": "from 0.4"});
assert_eq!(serde_json::from_value::<Message>(old).unwrap().content, "from 0.4");
}
#[test]
fn parts_round_trip_in_json_and_cbor() {
let m = Message::user(vec![ContentPart::text("what is it?"), ContentPart::image(PNG), ContentPart::audio("UklGRg==", "wav")]);
let j = serde_json::to_value(&m).unwrap();
assert_eq!(
j["content"],
serde_json::json!([
{"type": "text", "text": "what is it?"},
{"type": "image_url", "image_url": {"url": PNG}},
{"type": "input_audio", "input_audio": {"data": "UklGRg==", "format": "wav"}}
])
);
assert_eq!(serde_json::from_value::<Message>(j).unwrap(), m);
let back: Message = crate::remote::from_cbor(&crate::remote::to_cbor(&m).unwrap()).unwrap();
assert_eq!(back, m);
assert_eq!(m.needs(), Modalities::new(true, true));
assert_eq!(m.text(), "what is it?");
}
#[test]
fn needs_covers_and_lacking() {
let msgs = [Message::system("s"), Message::user(vec![ContentPart::image(PNG)])];
let n = needs(&msgs);
assert_eq!(n, Modalities::VISION);
assert!(Modalities::VISION.covers(n) && !Modalities::TEXT.covers(n) && !Modalities::AUDIO.covers(n));
assert_eq!(Modalities::TEXT.lacking(Modalities::new(true, true)), Some("images and audio"));
assert_eq!(Modalities::VISION.lacking(n), None);
assert!(needs(&[Message::user("x")]).is_text());
}
#[test]
fn check_refuses_remote_images_and_empty_audio() {
assert!(Content::from(vec![ContentPart::image(PNG)]).check().is_ok());
assert!(Content::from(vec![ContentPart::image("https://example.com/a.png")]).check().is_err());
assert!(Content::from(vec![ContentPart::audio("", "wav")]).check().is_err());
assert!(Content::from("text").check().is_ok());
}
#[test]
fn text_parts_normalize_to_a_string() {
let c = Content::from(vec![ContentPart::text("a"), ContentPart::text("b")]).normalized();
assert_eq!(c, Content::Text("ab".into()));
let img = Content::from(vec![ContentPart::image(PNG)]);
assert_eq!(img.clone().normalized(), img);
}
}