use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::JsonObject;
use crate::ids::{MessageId, SubagentRunId, ToolCallId};
use crate::tool::ToolCall;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum Role {
Developer,
System,
Assistant,
User,
Tool,
Activity,
Reasoning,
}
impl Role {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Developer => "developer",
Self::System => "system",
Self::Assistant => "assistant",
Self::User => "user",
Self::Tool => "tool",
Self::Activity => "activity",
Self::Reasoning => "reasoning",
}
}
}
impl std::fmt::Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum InputContentSource {
Data {
value: String,
#[serde(rename = "mimeType")]
mime_type: String,
},
Url {
value: String,
#[serde(
rename = "mimeType",
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
mime_type: Option<String>,
},
File {
value: String,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
provider: Option<String>,
#[serde(
rename = "mimeType",
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
mime_type: Option<String>,
},
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct TextInputContent {
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub id: Option<String>,
pub text: String,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "crate::serde_util::is_none_or_null"
)]
pub metadata: Option<Value>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct MediaInputContent {
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub id: Option<String>,
pub source: InputContentSource,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "crate::serde_util::is_none_or_null"
)]
pub metadata: Option<Value>,
}
impl MediaInputContent {
pub fn new(source: InputContentSource) -> Self {
Self {
id: None,
source,
metadata: None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct BinaryInputContent {
pub mime_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub filename: Option<String>,
}
impl BinaryInputContent {
pub fn has_payload(&self) -> bool {
self.id.is_some() || self.url.is_some() || self.data.is_some()
}
#[cfg(any(feature = "client", feature = "axum"))]
fn upgraded(&self) -> Option<InputContent> {
let mime_type = self.mime_type.clone();
let filled = |value: &Option<String>| value.clone().filter(|value| !value.is_empty());
let source = if let Some(value) = filled(&self.data) {
InputContentSource::Data { value, mime_type }
} else {
InputContentSource::Url {
value: filled(&self.url)?,
mime_type: Some(mime_type),
}
};
let media = MediaInputContent {
id: None,
source,
metadata: filled(&self.filename)
.map(|filename| serde_json::json!({ "filename": filename })),
};
let modality = self.mime_type.split_once('/').map(|(kind, _)| kind);
Some(match modality {
Some(kind) if kind.eq_ignore_ascii_case("image") => InputContent::Image(media),
Some(kind) if kind.eq_ignore_ascii_case("audio") => InputContent::Audio(media),
Some(kind) if kind.eq_ignore_ascii_case("video") => InputContent::Video(media),
_ => InputContent::Document(media),
})
}
}
#[cfg(feature = "client")]
pub(crate) fn upgrade_legacy_content(messages: &mut [Message]) {
for message in messages {
let parts = match message {
Message::User(user) => match &mut user.content {
UserContent::Parts(parts) => parts,
UserContent::Text(_) => continue,
},
Message::Tool(tool) => match &mut tool.content {
ToolContent::Parts(parts) => parts,
ToolContent::Text(_) => continue,
},
_ => continue,
};
if !parts
.iter()
.any(|part| matches!(part, InputContent::Binary(_)))
{
continue;
}
let original = std::mem::take(parts);
for part in &original {
let InputContent::Binary(binary) = part else {
parts.push(part.clone());
continue;
};
match binary.upgraded() {
Some(media) if original.iter().any(|other| mirrors(other, &media, binary)) => {}
Some(media) => parts.push(media),
None => parts.push(part.clone()),
}
}
}
}
#[cfg(feature = "client")]
pub(crate) fn upgrade_legacy_event_content(
event: &mut Value,
tag: &str,
warnings: &mut Vec<String>,
) {
let (messages, at) = match tag {
"TOOL_CALL_RESULT" => {
return upgrade_wire_parts(event.get_mut("content"), "/content", warnings);
}
"MESSAGES_SNAPSHOT" => (event.get_mut("messages"), "/messages"),
"RUN_STARTED" => (event.pointer_mut("/input/messages"), "/input/messages"),
_ => return,
};
upgrade_wire_messages(messages, at, warnings);
}
#[cfg(any(feature = "client", feature = "axum"))]
pub(crate) fn upgrade_wire_messages(
messages: Option<&mut Value>,
at: &str,
warnings: &mut Vec<String>,
) {
for (index, message) in messages
.and_then(Value::as_array_mut)
.into_iter()
.flatten()
.enumerate()
{
if matches!(
message.get("role").and_then(Value::as_str),
Some("user" | "tool")
) && message.get("content").is_some_and(Value::is_array)
{
let at = format!("{at}/{index}/content");
upgrade_wire_parts(message.get_mut("content"), &at, warnings);
}
}
}
#[cfg(any(feature = "client", feature = "axum"))]
fn upgrade_wire_parts(content: Option<&mut Value>, at: &str, warnings: &mut Vec<String>) {
let Some(parts) = content.and_then(Value::as_array_mut) else {
return;
};
let legacy = |part: &Value| -> Option<BinaryInputContent> {
(part.get("type").and_then(Value::as_str) == Some("binary"))
.then(|| serde_json::from_value(part.clone()).ok())
.flatten()
};
if !parts.iter().any(|part| legacy(part).is_some()) {
return;
}
let original = std::mem::take(parts);
let modern: Vec<InputContent> = original
.iter()
.filter(|part| legacy(part).is_none())
.filter_map(|part| serde_json::from_value(part.clone()).ok())
.collect();
for (index, part) in original.iter().enumerate() {
let translated = legacy(part).and_then(|binary| {
let media = binary.upgraded()?;
Some((serde_json::to_value(&media).ok()?, media, binary))
});
let Some((wire, media, binary)) = translated else {
parts.push(part.clone());
continue;
};
let modality = wire.get("type").and_then(Value::as_str).unwrap_or("media");
if modern.iter().any(|other| mirrors(other, &media, &binary)) {
warnings.push(format!(
"dropped retired binary content part at {at}/{index}: the {modality} part \
beside it carries the same payload"
));
} else {
warnings.push(format!(
"translated retired binary content part at {at}/{index} to {modality}"
));
parts.push(wire);
}
}
}
#[cfg(any(feature = "client", feature = "axum"))]
fn mirrors(part: &InputContent, media: &InputContent, legacy: &BinaryInputContent) -> bool {
let (part, media) = match (part, media) {
(InputContent::Image(part), InputContent::Image(media))
| (InputContent::Audio(part), InputContent::Audio(media))
| (InputContent::Video(part), InputContent::Video(media))
| (InputContent::Document(part), InputContent::Document(media)) => (part, media),
_ => return false,
};
let kept_name = part
.metadata
.as_ref()
.and_then(|metadata| metadata.get("filename"))
.and_then(Value::as_str);
let same_mime = |a: &str, b: &str| {
let (kind_a, params_a) = a.split_once(';').unwrap_or((a, ""));
let (kind_b, params_b) = b.split_once(';').unwrap_or((b, ""));
kind_a.eq_ignore_ascii_case(kind_b) && params_a == params_b
};
let same_source = match (&part.source, &media.source) {
(
InputContentSource::Url {
value: a,
mime_type: Some(mime_a),
},
InputContentSource::Url {
value: b,
mime_type: Some(mime_b),
},
)
| (
InputContentSource::Data {
value: a,
mime_type: mime_a,
},
InputContentSource::Data {
value: b,
mime_type: mime_b,
},
) => a == b && same_mime(mime_a, mime_b),
_ => false,
};
same_source
&& legacy
.filename
.as_deref()
.filter(|filename| !filename.is_empty())
.is_none_or(|filename| kept_name == Some(filename))
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum InputContent {
Text(TextInputContent),
Image(MediaInputContent),
Audio(MediaInputContent),
Video(MediaInputContent),
Document(MediaInputContent),
Binary(BinaryInputContent),
}
impl InputContent {
pub fn text(text: impl Into<String>) -> Self {
Self::Text(TextInputContent {
text: text.into(),
..Default::default()
})
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum UserContent {
Text(String),
Parts(Vec<InputContent>),
}
impl UserContent {
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(text) => Some(text),
Self::Parts(_) => None,
}
}
pub fn to_text(&self) -> String {
match self {
Self::Text(text) => text.clone(),
Self::Parts(parts) => parts
.iter()
.filter_map(|part| match part {
InputContent::Text(part) => Some(part.text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n"),
}
}
}
impl Default for UserContent {
fn default() -> Self {
Self::Text(String::new())
}
}
impl From<String> for UserContent {
fn from(value: String) -> Self {
Self::Text(value)
}
}
impl From<&str> for UserContent {
fn from(value: &str) -> Self {
Self::Text(value.to_owned())
}
}
impl From<Vec<InputContent>> for UserContent {
fn from(value: Vec<InputContent>) -> Self {
Self::Parts(value)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ToolContent {
Text(String),
Parts(Vec<InputContent>),
}
impl ToolContent {
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(text) => Some(text),
Self::Parts(_) => None,
}
}
pub fn to_text(&self) -> String {
match self {
Self::Text(text) => text.clone(),
Self::Parts(parts) => parts
.iter()
.filter_map(|part| match part {
InputContent::Text(part) => Some(part.text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n"),
}
}
}
impl Default for ToolContent {
fn default() -> Self {
Self::Text(String::new())
}
}
impl From<String> for ToolContent {
fn from(value: String) -> Self {
Self::Text(value)
}
}
impl From<&str> for ToolContent {
fn from(value: &str) -> Self {
Self::Text(value.to_owned())
}
}
impl From<Vec<InputContent>> for ToolContent {
fn from(value: Vec<InputContent>) -> Self {
Self::Parts(value)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct DeveloperMessage {
pub id: MessageId,
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct SystemMessage {
pub id: MessageId,
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct AssistantMessage {
pub id: MessageId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct UserMessage {
pub id: MessageId,
pub content: UserContent,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ToolMessage {
pub id: MessageId,
pub content: ToolContent,
pub tool_call_id: ToolCallId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ActivityMessage {
pub id: MessageId,
pub activity_type: String,
#[cfg_attr(
feature = "schemars",
schemars(with = "std::collections::BTreeMap<String, serde_json::Value>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Object))]
pub content: JsonObject,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningMessage {
pub id: MessageId,
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub encrypted_value: Option<String>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
pub subagent_run_id: Option<SubagentRunId>,
#[serde(
default,
deserialize_with = "crate::serde_util::reject_null",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(
feature = "schemars",
schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
)]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
pub metadata: Option<JsonObject>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "lowercase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum Message {
Developer(DeveloperMessage),
System(SystemMessage),
Assistant(AssistantMessage),
User(UserMessage),
Tool(ToolMessage),
Activity(ActivityMessage),
Reasoning(ReasoningMessage),
}
impl Message {
pub fn user(id: impl Into<MessageId>, content: impl Into<UserContent>) -> Self {
Self::User(UserMessage {
id: id.into(),
content: content.into(),
..Default::default()
})
}
pub fn assistant(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
Self::Assistant(AssistantMessage {
id: id.into(),
content: Some(content.into()),
..Default::default()
})
}
pub fn system(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
Self::System(SystemMessage {
id: id.into(),
content: content.into(),
..Default::default()
})
}
pub fn developer(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
Self::Developer(DeveloperMessage {
id: id.into(),
content: content.into(),
..Default::default()
})
}
pub fn tool(
id: impl Into<MessageId>,
tool_call_id: impl Into<ToolCallId>,
content: impl Into<ToolContent>,
) -> Self {
Self::Tool(ToolMessage {
id: id.into(),
content: content.into(),
tool_call_id: tool_call_id.into(),
..Default::default()
})
}
pub const fn id(&self) -> &MessageId {
match self {
Self::Developer(m) => &m.id,
Self::System(m) => &m.id,
Self::Assistant(m) => &m.id,
Self::User(m) => &m.id,
Self::Tool(m) => &m.id,
Self::Activity(m) => &m.id,
Self::Reasoning(m) => &m.id,
}
}
pub const fn role(&self) -> Role {
match self {
Self::Developer(_) => Role::Developer,
Self::System(_) => Role::System,
Self::Assistant(_) => Role::Assistant,
Self::User(_) => Role::User,
Self::Tool(_) => Role::Tool,
Self::Activity(_) => Role::Activity,
Self::Reasoning(_) => Role::Reasoning,
}
}
pub const fn subagent_run_id(&self) -> Option<&SubagentRunId> {
match self {
Self::Developer(m) => m.subagent_run_id.as_ref(),
Self::System(m) => m.subagent_run_id.as_ref(),
Self::Assistant(m) => m.subagent_run_id.as_ref(),
Self::User(m) => m.subagent_run_id.as_ref(),
Self::Tool(m) => m.subagent_run_id.as_ref(),
Self::Activity(m) => m.subagent_run_id.as_ref(),
Self::Reasoning(m) => m.subagent_run_id.as_ref(),
}
}
pub fn set_subagent_run_id(&mut self, subagent_run_id: Option<SubagentRunId>) {
match self {
Self::Developer(m) => m.subagent_run_id = subagent_run_id,
Self::System(m) => m.subagent_run_id = subagent_run_id,
Self::Assistant(m) => m.subagent_run_id = subagent_run_id,
Self::User(m) => m.subagent_run_id = subagent_run_id,
Self::Tool(m) => m.subagent_run_id = subagent_run_id,
Self::Activity(m) => m.subagent_run_id = subagent_run_id,
Self::Reasoning(m) => m.subagent_run_id = subagent_run_id,
}
}
pub const fn metadata(&self) -> Option<&JsonObject> {
match self {
Self::Developer(m) => m.metadata.as_ref(),
Self::System(m) => m.metadata.as_ref(),
Self::Assistant(m) => m.metadata.as_ref(),
Self::User(m) => m.metadata.as_ref(),
Self::Tool(m) => m.metadata.as_ref(),
Self::Activity(m) => m.metadata.as_ref(),
Self::Reasoning(m) => m.metadata.as_ref(),
}
}
pub fn metadata_mut(&mut self) -> &mut Option<JsonObject> {
match self {
Self::Developer(m) => &mut m.metadata,
Self::System(m) => &mut m.metadata,
Self::Assistant(m) => &mut m.metadata,
Self::User(m) => &mut m.metadata,
Self::Tool(m) => &mut m.metadata,
Self::Activity(m) => &mut m.metadata,
Self::Reasoning(m) => &mut m.metadata,
}
}
}
macro_rules! message_from {
($($ty:ident => $variant:ident),* $(,)?) => {
$(
impl From<$ty> for Message {
fn from(value: $ty) -> Self {
Self::$variant(value)
}
}
)*
};
}
message_from! {
DeveloperMessage => Developer,
SystemMessage => System,
AssistantMessage => Assistant,
UserMessage => User,
ToolMessage => Tool,
ActivityMessage => Activity,
ReasoningMessage => Reasoning,
}