use serde::{Deserialize, Serialize};
use crate::context::ids::BlockId;
use crate::context::tool_data::ToolResultPayload;
use crate::context::tools::ToolChange;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
System,
User,
Assistant,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct BlockMeta {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_call_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wire: Option<WireMeta>,
}
impl BlockMeta {
pub fn from_source(source: impl Into<String>) -> Self {
Self {
source: Some(source.into()),
..Self::default()
}
}
pub fn with_provider_call_id(mut self, provider_call_id: impl Into<String>) -> Self {
self.provider_call_id = Some(provider_call_id.into());
self
}
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
pub fn with_wire(mut self, wire: WireMeta) -> Self {
self.wire = Some(wire);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WireMeta {
pub format: String,
pub fields: serde_json::Value,
}
impl WireMeta {
pub fn new(format: impl Into<String>, fields: serde_json::Value) -> Self {
Self {
format: format.into(),
fields,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TextPayload(pub String);
impl TextPayload {
pub fn new(s: impl Into<String>) -> Self {
Self(s.into())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolCallPayload {
pub tool_name: String,
pub arguments: serde_json::Value,
}
impl ToolCallPayload {
pub fn new(tool_name: impl Into<String>, arguments: serde_json::Value) -> Self {
Self {
tool_name: tool_name.into(),
arguments,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContextBlock {
id: BlockId,
content: BlockContent,
meta: BlockMeta,
}
impl ContextBlock {
pub fn new(id: BlockId, content: BlockContent, meta: BlockMeta) -> Self {
Self { id, content, meta }
}
pub fn text(id: BlockId, role: Role, text: impl Into<String>) -> Self {
Self::new(id, BlockContent::text(role, text), BlockMeta::default())
}
pub const fn id(&self) -> BlockId {
self.id
}
pub const fn content(&self) -> &BlockContent {
&self.content
}
pub const fn meta(&self) -> &BlockMeta {
&self.meta
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MediaRef {
pub media_type: String,
pub reference: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
impl MediaRef {
pub fn new(media_type: impl Into<String>, reference: impl Into<String>) -> Self {
Self {
media_type: media_type.into(),
reference: reference.into(),
name: None,
}
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "shape", content = "value", rename_all = "snake_case")]
pub enum BlockContent {
Message {
role: Role,
parts: Vec<ContentPart>,
},
ToolCall(ToolCallPayload),
ToolResult(ToolResultPayload),
ToolChange(ToolChange),
}
impl BlockContent {
pub fn text(role: Role, text: impl Into<String>) -> Self {
Self::Message {
role,
parts: vec![ContentPart::Text(TextPayload::new(text))],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "part", content = "value", rename_all = "snake_case")]
pub enum ContentPart {
Text(TextPayload),
Media(MediaRef),
Sealed(SealedPart),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SealedPart {
pub format: String,
pub item: serde_json::Value,
}
impl SealedPart {
pub fn new(format: impl Into<String>, item: serde_json::Value) -> Self {
Self {
format: format.into(),
item,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn named_media_ref_round_trips_through_serde() {
let media = MediaRef::new("application/pdf", "asset://doc").with_name("memo.pdf");
let serialized = serde_json::to_value(&media).expect("serialization succeeds");
assert_eq!(
serialized,
json!({
"media_type": "application/pdf",
"reference": "asset://doc",
"name": "memo.pdf",
})
);
let deserialized: MediaRef =
serde_json::from_value(serialized).expect("deserialization succeeds");
assert_eq!(deserialized, media);
assert_eq!(deserialized.name.as_deref(), Some("memo.pdf"));
}
#[test]
fn unnamed_media_ref_serializes_with_no_name_key() {
let media = MediaRef::new("image/png", "asset://img");
let serialized = serde_json::to_value(&media).expect("serialization succeeds");
assert_eq!(
serialized,
json!({
"media_type": "image/png",
"reference": "asset://img",
})
);
assert!(!serialized.as_object().unwrap().contains_key("name"));
let deserialized: MediaRef =
serde_json::from_value(serialized).expect("deserialization succeeds");
assert_eq!(deserialized, media);
assert_eq!(deserialized.name, None);
}
}