#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
clippy::all
)]
use super::super::context::{LoadContext, SaveContext};
use super::content_part::{ContentPart, ContentPartKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Role {
System,
User,
Assistant,
Developer,
Tool,
}
impl Default for Role {
fn default() -> Self {
Self::System
}
}
impl std::fmt::Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::System => write!(f, "system"),
Self::User => write!(f, "user"),
Self::Assistant => write!(f, "assistant"),
Self::Developer => write!(f, "developer"),
Self::Tool => write!(f, "tool"),
}
}
}
impl Role {
pub fn from_str_opt(s: &str) -> Option<Self> {
match s {
"system" => Some(Self::System),
"user" => Some(Self::User),
"assistant" => Some(Self::Assistant),
"developer" => Some(Self::Developer),
"tool" => Some(Self::Tool),
_ => None,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::System => "system",
Self::User => "user",
Self::Assistant => "assistant",
Self::Developer => "developer",
Self::Tool => "tool",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Message {
pub role: Role,
pub parts: Vec<ContentPart>,
pub metadata: serde_json::Value,
}
impl Message {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
Self {
role: value
.get("role")
.and_then(|v| v.as_str())
.and_then(|s| Role::from_str_opt(s))
.unwrap_or(Role::User),
parts: value
.get("parts")
.map(|v| Self::load_parts(v, ctx))
.unwrap_or_default(),
metadata: value
.get("metadata")
.cloned()
.unwrap_or(serde_json::Value::Null),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
result.insert(
"role".to_string(),
serde_json::Value::String(self.role.to_string()),
);
if !self.parts.is_empty() {
result.insert("parts".to_string(), Self::save_parts(&self.parts, ctx));
}
if !self.metadata.is_null() {
result.insert("metadata".to_string(), self.metadata.clone());
}
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
pub fn as_metadata_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
self.metadata.as_object()
}
fn load_parts(data: &serde_json::Value, ctx: &LoadContext) -> Vec<ContentPart> {
match data {
serde_json::Value::Array(arr) => arr
.iter()
.map(|v| ContentPart::load_from_value(v, ctx))
.collect(),
_ => Vec::new(),
}
}
fn save_parts(items: &[ContentPart], ctx: &SaveContext) -> serde_json::Value {
serde_json::Value::Array(
items
.iter()
.map(|item| item.to_value(ctx))
.collect::<Vec<_>>(),
)
}
pub fn assistant(text: impl Into<String>) -> Self {
Message {
role: Role::Assistant,
parts: vec![ContentPart {
kind: ContentPartKind::TextPart { value: text.into() },
..Default::default()
}],
..Default::default()
}
}
pub fn system(text: impl Into<String>) -> Self {
Message {
role: Role::System,
parts: vec![ContentPart {
kind: ContentPartKind::TextPart { value: text.into() },
..Default::default()
}],
..Default::default()
}
}
pub fn user(text: impl Into<String>) -> Self {
Message {
role: Role::User,
parts: vec![ContentPart {
kind: ContentPartKind::TextPart { value: text.into() },
..Default::default()
}],
..Default::default()
}
}
}
pub trait MessageHelpers {
fn to_text_content(&self) -> serde_json::Value;
fn text(&self) -> String;
}