use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::message::MessagePart;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Artifact {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub parts: Vec<MessagePart>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<serde_json::Value>,
}
impl Artifact {
pub fn text(name: impl Into<String>, content: impl Into<String>) -> Self {
Self {
id: Uuid::new_v4().to_string(),
name: Some(name.into()),
description: None,
parts: vec![MessagePart::text(content)],
metadata: None,
}
}
pub fn data(name: impl Into<String>, value: serde_json::Value) -> Self {
Self {
id: Uuid::new_v4().to_string(),
name: Some(name.into()),
description: None,
parts: vec![MessagePart::data(value, Some("application/json".into()))],
metadata: None,
}
}
pub fn text_content(&self) -> String {
self.parts
.iter()
.filter_map(|p| match p {
MessagePart::Text { text, .. } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n")
}
}