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,
}
impl MediaRef {
pub fn new(media_type: impl Into<String>, reference: impl Into<String>) -> Self {
Self {
media_type: media_type.into(),
reference: reference.into(),
}
}
}
#[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,
}
}
}