1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::JsonObject;
7use crate::ids::{MessageId, SubagentRunId, ToolCallId};
8use crate::tool::ToolCall;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
14#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
15pub enum Role {
16 Developer,
18 System,
20 Assistant,
22 User,
24 Tool,
26 Activity,
28 Reasoning,
30}
31
32impl Role {
33 pub const fn as_str(&self) -> &'static str {
35 match self {
36 Self::Developer => "developer",
37 Self::System => "system",
38 Self::Assistant => "assistant",
39 Self::User => "user",
40 Self::Tool => "tool",
41 Self::Activity => "activity",
42 Self::Reasoning => "reasoning",
43 }
44 }
45}
46
47impl std::fmt::Display for Role {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 f.write_str(self.as_str())
50 }
51}
52
53#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(tag = "type", rename_all = "lowercase")]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
58pub enum InputContentSource {
59 Data {
61 value: String,
63 #[serde(rename = "mimeType")]
65 mime_type: String,
66 },
67 Url {
69 value: String,
71 #[serde(
73 rename = "mimeType",
74 default,
75 deserialize_with = "crate::serde_util::reject_null",
76 skip_serializing_if = "Option::is_none"
77 )]
78 mime_type: Option<String>,
79 },
80 File {
82 value: String,
84 #[serde(
86 default,
87 deserialize_with = "crate::serde_util::reject_null",
88 skip_serializing_if = "Option::is_none"
89 )]
90 provider: Option<String>,
91 #[serde(
93 rename = "mimeType",
94 default,
95 deserialize_with = "crate::serde_util::reject_null",
96 skip_serializing_if = "Option::is_none"
97 )]
98 mime_type: Option<String>,
99 },
100}
101
102#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
104#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
105#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
106pub struct TextInputContent {
107 #[serde(
109 default,
110 deserialize_with = "crate::serde_util::reject_null",
111 skip_serializing_if = "Option::is_none"
112 )]
113 pub id: Option<String>,
114 pub text: String,
116 #[serde(
118 default,
119 deserialize_with = "crate::serde_util::reject_null",
120 skip_serializing_if = "crate::serde_util::is_none_or_null"
121 )]
122 pub metadata: Option<Value>,
123}
124
125#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
130#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
131#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
132pub struct MediaInputContent {
133 #[serde(
135 default,
136 deserialize_with = "crate::serde_util::reject_null",
137 skip_serializing_if = "Option::is_none"
138 )]
139 pub id: Option<String>,
140 pub source: InputContentSource,
142 #[serde(
144 default,
145 deserialize_with = "crate::serde_util::reject_null",
146 skip_serializing_if = "crate::serde_util::is_none_or_null"
147 )]
148 pub metadata: Option<Value>,
149}
150
151impl MediaInputContent {
152 pub fn new(source: InputContentSource) -> Self {
154 Self {
155 id: None,
156 source,
157 metadata: None,
158 }
159 }
160}
161
162#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
171#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
172pub struct BinaryInputContent {
173 pub mime_type: String,
175 #[serde(default, skip_serializing_if = "Option::is_none")]
177 pub id: Option<String>,
178 #[serde(default, skip_serializing_if = "Option::is_none")]
180 pub url: Option<String>,
181 #[serde(default, skip_serializing_if = "Option::is_none")]
183 pub data: Option<String>,
184 #[serde(default, skip_serializing_if = "Option::is_none")]
186 pub filename: Option<String>,
187}
188
189impl BinaryInputContent {
190 pub fn has_payload(&self) -> bool {
192 self.id.is_some() || self.url.is_some() || self.data.is_some()
193 }
194}
195
196#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
198#[serde(tag = "type", rename_all = "lowercase")]
199#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
200#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
201pub enum InputContent {
202 Text(TextInputContent),
204 Image(MediaInputContent),
206 Audio(MediaInputContent),
208 Video(MediaInputContent),
210 Document(MediaInputContent),
212 Binary(BinaryInputContent),
214}
215
216impl InputContent {
217 pub fn text(text: impl Into<String>) -> Self {
219 Self::Text(TextInputContent {
220 text: text.into(),
221 ..Default::default()
222 })
223 }
224}
225
226#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
229#[serde(untagged)]
230#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
231#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
232pub enum UserContent {
233 Text(String),
235 Parts(Vec<InputContent>),
237}
238
239impl UserContent {
240 pub fn as_text(&self) -> Option<&str> {
246 match self {
247 Self::Text(text) => Some(text),
248 Self::Parts(_) => None,
249 }
250 }
251
252 pub fn to_text(&self) -> String {
277 match self {
278 Self::Text(text) => text.clone(),
279 Self::Parts(parts) => parts
280 .iter()
281 .filter_map(|part| match part {
282 InputContent::Text(part) => Some(part.text.as_str()),
283 _ => None,
284 })
285 .collect::<Vec<_>>()
286 .join("\n"),
287 }
288 }
289}
290
291impl Default for UserContent {
292 fn default() -> Self {
293 Self::Text(String::new())
294 }
295}
296
297impl From<String> for UserContent {
298 fn from(value: String) -> Self {
299 Self::Text(value)
300 }
301}
302
303impl From<&str> for UserContent {
304 fn from(value: &str) -> Self {
305 Self::Text(value.to_owned())
306 }
307}
308
309impl From<Vec<InputContent>> for UserContent {
310 fn from(value: Vec<InputContent>) -> Self {
311 Self::Parts(value)
312 }
313}
314
315#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
317#[serde(untagged)]
318#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
319#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
320pub enum ToolContent {
321 Text(String),
323 Parts(Vec<InputContent>),
325}
326
327impl ToolContent {
328 pub fn as_text(&self) -> Option<&str> {
330 match self {
331 Self::Text(text) => Some(text),
332 Self::Parts(_) => None,
333 }
334 }
335
336 pub fn to_text(&self) -> String {
338 match self {
339 Self::Text(text) => text.clone(),
340 Self::Parts(parts) => parts
341 .iter()
342 .filter_map(|part| match part {
343 InputContent::Text(part) => Some(part.text.as_str()),
344 _ => None,
345 })
346 .collect::<Vec<_>>()
347 .join("\n"),
348 }
349 }
350}
351
352impl Default for ToolContent {
353 fn default() -> Self {
354 Self::Text(String::new())
355 }
356}
357
358impl From<String> for ToolContent {
359 fn from(value: String) -> Self {
360 Self::Text(value)
361 }
362}
363
364impl From<&str> for ToolContent {
365 fn from(value: &str) -> Self {
366 Self::Text(value.to_owned())
367 }
368}
369
370impl From<Vec<InputContent>> for ToolContent {
371 fn from(value: Vec<InputContent>) -> Self {
372 Self::Parts(value)
373 }
374}
375
376#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
378#[serde(rename_all = "camelCase")]
379#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
380#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
381pub struct DeveloperMessage {
382 pub id: MessageId,
384 pub content: String,
386 #[serde(default, skip_serializing_if = "Option::is_none")]
388 pub name: Option<String>,
389 #[serde(default, skip_serializing_if = "Option::is_none")]
391 pub encrypted_value: Option<String>,
392 #[serde(
395 default,
396 deserialize_with = "crate::serde_util::reject_null",
397 skip_serializing_if = "Option::is_none"
398 )]
399 pub subagent_run_id: Option<SubagentRunId>,
400 #[serde(
403 default,
404 deserialize_with = "crate::serde_util::reject_null",
405 skip_serializing_if = "Option::is_none"
406 )]
407 #[cfg_attr(
408 feature = "schemars",
409 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
410 )]
411 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
412 pub metadata: Option<JsonObject>,
413}
414
415#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
417#[serde(rename_all = "camelCase")]
418#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
419#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
420pub struct SystemMessage {
421 pub id: MessageId,
423 pub content: String,
425 #[serde(default, skip_serializing_if = "Option::is_none")]
427 pub name: Option<String>,
428 #[serde(default, skip_serializing_if = "Option::is_none")]
430 pub encrypted_value: Option<String>,
431 #[serde(
434 default,
435 deserialize_with = "crate::serde_util::reject_null",
436 skip_serializing_if = "Option::is_none"
437 )]
438 pub subagent_run_id: Option<SubagentRunId>,
439 #[serde(
442 default,
443 deserialize_with = "crate::serde_util::reject_null",
444 skip_serializing_if = "Option::is_none"
445 )]
446 #[cfg_attr(
447 feature = "schemars",
448 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
449 )]
450 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
451 pub metadata: Option<JsonObject>,
452}
453
454#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
456#[serde(rename_all = "camelCase")]
457#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
458#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
459pub struct AssistantMessage {
460 pub id: MessageId,
462 #[serde(default, skip_serializing_if = "Option::is_none")]
464 pub content: Option<String>,
465 #[serde(default, skip_serializing_if = "Option::is_none")]
467 pub name: Option<String>,
468 #[serde(default, skip_serializing_if = "Option::is_none")]
470 pub encrypted_value: Option<String>,
471 #[serde(default, skip_serializing_if = "Option::is_none")]
473 pub tool_calls: Option<Vec<ToolCall>>,
474 #[serde(
477 default,
478 deserialize_with = "crate::serde_util::reject_null",
479 skip_serializing_if = "Option::is_none"
480 )]
481 pub subagent_run_id: Option<SubagentRunId>,
482 #[serde(
485 default,
486 deserialize_with = "crate::serde_util::reject_null",
487 skip_serializing_if = "Option::is_none"
488 )]
489 #[cfg_attr(
490 feature = "schemars",
491 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
492 )]
493 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
494 pub metadata: Option<JsonObject>,
495}
496
497#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
499#[serde(rename_all = "camelCase")]
500#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
501#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
502pub struct UserMessage {
503 pub id: MessageId,
505 pub content: UserContent,
507 #[serde(default, skip_serializing_if = "Option::is_none")]
509 pub name: Option<String>,
510 #[serde(default, skip_serializing_if = "Option::is_none")]
512 pub encrypted_value: Option<String>,
513 #[serde(
516 default,
517 deserialize_with = "crate::serde_util::reject_null",
518 skip_serializing_if = "Option::is_none"
519 )]
520 pub subagent_run_id: Option<SubagentRunId>,
521 #[serde(
524 default,
525 deserialize_with = "crate::serde_util::reject_null",
526 skip_serializing_if = "Option::is_none"
527 )]
528 #[cfg_attr(
529 feature = "schemars",
530 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
531 )]
532 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
533 pub metadata: Option<JsonObject>,
534}
535
536#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
538#[serde(rename_all = "camelCase")]
539#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
540#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
541pub struct ToolMessage {
542 pub id: MessageId,
544 pub content: ToolContent,
546 pub tool_call_id: ToolCallId,
548 #[serde(default, skip_serializing_if = "Option::is_none")]
551 pub error: Option<String>,
552 #[serde(default, skip_serializing_if = "Option::is_none")]
554 pub encrypted_value: Option<String>,
555 #[serde(
560 default,
561 deserialize_with = "crate::serde_util::reject_null",
562 skip_serializing_if = "Option::is_none"
563 )]
564 pub subagent_run_id: Option<SubagentRunId>,
565 #[serde(
568 default,
569 deserialize_with = "crate::serde_util::reject_null",
570 skip_serializing_if = "Option::is_none"
571 )]
572 #[cfg_attr(
573 feature = "schemars",
574 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
575 )]
576 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
577 pub metadata: Option<JsonObject>,
578}
579
580#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
582#[serde(rename_all = "camelCase")]
583#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
584#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
585pub struct ActivityMessage {
586 pub id: MessageId,
588 pub activity_type: String,
590 #[cfg_attr(
592 feature = "schemars",
593 schemars(with = "std::collections::BTreeMap<String, serde_json::Value>")
594 )]
595 #[cfg_attr(feature = "utoipa", schema(value_type = Object))]
596 pub content: JsonObject,
597 #[serde(
600 default,
601 deserialize_with = "crate::serde_util::reject_null",
602 skip_serializing_if = "Option::is_none"
603 )]
604 pub subagent_run_id: Option<SubagentRunId>,
605 #[serde(
608 default,
609 deserialize_with = "crate::serde_util::reject_null",
610 skip_serializing_if = "Option::is_none"
611 )]
612 #[cfg_attr(
613 feature = "schemars",
614 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
615 )]
616 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
617 pub metadata: Option<JsonObject>,
618}
619
620#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
622#[serde(rename_all = "camelCase")]
623#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
624#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
625pub struct ReasoningMessage {
626 pub id: MessageId,
628 pub content: String,
631 #[serde(default, skip_serializing_if = "Option::is_none")]
633 pub encrypted_value: Option<String>,
634 #[serde(
637 default,
638 deserialize_with = "crate::serde_util::reject_null",
639 skip_serializing_if = "Option::is_none"
640 )]
641 pub subagent_run_id: Option<SubagentRunId>,
642 #[serde(
645 default,
646 deserialize_with = "crate::serde_util::reject_null",
647 skip_serializing_if = "Option::is_none"
648 )]
649 #[cfg_attr(
650 feature = "schemars",
651 schemars(with = "Option<std::collections::BTreeMap<String, serde_json::Value>>")
652 )]
653 #[cfg_attr(feature = "utoipa", schema(value_type = Option<Object>))]
654 pub metadata: Option<JsonObject>,
655}
656
657#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
659#[serde(tag = "role", rename_all = "lowercase")]
660#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
661#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
662pub enum Message {
663 Developer(DeveloperMessage),
665 System(SystemMessage),
667 Assistant(AssistantMessage),
669 User(UserMessage),
671 Tool(ToolMessage),
673 Activity(ActivityMessage),
675 Reasoning(ReasoningMessage),
677}
678
679impl Message {
680 pub fn user(id: impl Into<MessageId>, content: impl Into<UserContent>) -> Self {
682 Self::User(UserMessage {
683 id: id.into(),
684 content: content.into(),
685 ..Default::default()
686 })
687 }
688
689 pub fn assistant(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
691 Self::Assistant(AssistantMessage {
692 id: id.into(),
693 content: Some(content.into()),
694 ..Default::default()
695 })
696 }
697
698 pub fn system(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
700 Self::System(SystemMessage {
701 id: id.into(),
702 content: content.into(),
703 ..Default::default()
704 })
705 }
706
707 pub fn developer(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
709 Self::Developer(DeveloperMessage {
710 id: id.into(),
711 content: content.into(),
712 ..Default::default()
713 })
714 }
715
716 pub fn tool(
718 id: impl Into<MessageId>,
719 tool_call_id: impl Into<ToolCallId>,
720 content: impl Into<ToolContent>,
721 ) -> Self {
722 Self::Tool(ToolMessage {
723 id: id.into(),
724 content: content.into(),
725 tool_call_id: tool_call_id.into(),
726 ..Default::default()
727 })
728 }
729
730 pub const fn id(&self) -> &MessageId {
732 match self {
733 Self::Developer(m) => &m.id,
734 Self::System(m) => &m.id,
735 Self::Assistant(m) => &m.id,
736 Self::User(m) => &m.id,
737 Self::Tool(m) => &m.id,
738 Self::Activity(m) => &m.id,
739 Self::Reasoning(m) => &m.id,
740 }
741 }
742
743 pub const fn role(&self) -> Role {
745 match self {
746 Self::Developer(_) => Role::Developer,
747 Self::System(_) => Role::System,
748 Self::Assistant(_) => Role::Assistant,
749 Self::User(_) => Role::User,
750 Self::Tool(_) => Role::Tool,
751 Self::Activity(_) => Role::Activity,
752 Self::Reasoning(_) => Role::Reasoning,
753 }
754 }
755
756 pub const fn subagent_run_id(&self) -> Option<&SubagentRunId> {
759 match self {
760 Self::Developer(m) => m.subagent_run_id.as_ref(),
761 Self::System(m) => m.subagent_run_id.as_ref(),
762 Self::Assistant(m) => m.subagent_run_id.as_ref(),
763 Self::User(m) => m.subagent_run_id.as_ref(),
764 Self::Tool(m) => m.subagent_run_id.as_ref(),
765 Self::Activity(m) => m.subagent_run_id.as_ref(),
766 Self::Reasoning(m) => m.subagent_run_id.as_ref(),
767 }
768 }
769
770 pub fn set_subagent_run_id(&mut self, subagent_run_id: Option<SubagentRunId>) {
772 match self {
773 Self::Developer(m) => m.subagent_run_id = subagent_run_id,
774 Self::System(m) => m.subagent_run_id = subagent_run_id,
775 Self::Assistant(m) => m.subagent_run_id = subagent_run_id,
776 Self::User(m) => m.subagent_run_id = subagent_run_id,
777 Self::Tool(m) => m.subagent_run_id = subagent_run_id,
778 Self::Activity(m) => m.subagent_run_id = subagent_run_id,
779 Self::Reasoning(m) => m.subagent_run_id = subagent_run_id,
780 }
781 }
782
783 pub const fn metadata(&self) -> Option<&JsonObject> {
785 match self {
786 Self::Developer(m) => m.metadata.as_ref(),
787 Self::System(m) => m.metadata.as_ref(),
788 Self::Assistant(m) => m.metadata.as_ref(),
789 Self::User(m) => m.metadata.as_ref(),
790 Self::Tool(m) => m.metadata.as_ref(),
791 Self::Activity(m) => m.metadata.as_ref(),
792 Self::Reasoning(m) => m.metadata.as_ref(),
793 }
794 }
795
796 pub fn metadata_mut(&mut self) -> &mut Option<JsonObject> {
800 match self {
801 Self::Developer(m) => &mut m.metadata,
802 Self::System(m) => &mut m.metadata,
803 Self::Assistant(m) => &mut m.metadata,
804 Self::User(m) => &mut m.metadata,
805 Self::Tool(m) => &mut m.metadata,
806 Self::Activity(m) => &mut m.metadata,
807 Self::Reasoning(m) => &mut m.metadata,
808 }
809 }
810}
811
812macro_rules! message_from {
813 ($($ty:ident => $variant:ident),* $(,)?) => {
814 $(
815 impl From<$ty> for Message {
816 fn from(value: $ty) -> Self {
817 Self::$variant(value)
818 }
819 }
820 )*
821 };
822}
823
824message_from! {
825 DeveloperMessage => Developer,
826 SystemMessage => System,
827 AssistantMessage => Assistant,
828 UserMessage => User,
829 ToolMessage => Tool,
830 ActivityMessage => Activity,
831 ReasoningMessage => Reasoning,
832}