1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::JsonObject;
7use crate::ids::{MessageId, 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(rename = "mimeType", default, skip_serializing_if = "Option::is_none")]
73 mime_type: Option<String>,
74 },
75}
76
77#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
79#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
80#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
81pub struct TextInputContent {
82 pub text: String,
84}
85
86#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
91#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
92#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
93pub struct MediaInputContent {
94 pub source: InputContentSource,
96 #[serde(default, skip_serializing_if = "Option::is_none")]
98 pub metadata: Option<Value>,
99}
100
101impl MediaInputContent {
102 pub fn new(source: InputContentSource) -> Self {
104 Self {
105 source,
106 metadata: None,
107 }
108 }
109}
110
111#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
118#[serde(rename_all = "camelCase")]
119#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
120#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
121pub struct BinaryInputContent {
122 pub mime_type: String,
124 #[serde(default, skip_serializing_if = "Option::is_none")]
126 pub id: Option<String>,
127 #[serde(default, skip_serializing_if = "Option::is_none")]
129 pub url: Option<String>,
130 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub data: Option<String>,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub filename: Option<String>,
136}
137
138impl BinaryInputContent {
139 pub fn has_payload(&self) -> bool {
141 self.id.is_some() || self.url.is_some() || self.data.is_some()
142 }
143}
144
145#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
147#[serde(tag = "type", rename_all = "lowercase")]
148#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
149#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
150pub enum InputContent {
151 Text(TextInputContent),
153 Image(MediaInputContent),
155 Audio(MediaInputContent),
157 Video(MediaInputContent),
159 Document(MediaInputContent),
161 Binary(BinaryInputContent),
163}
164
165impl InputContent {
166 pub fn text(text: impl Into<String>) -> Self {
168 Self::Text(TextInputContent { text: text.into() })
169 }
170}
171
172#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
175#[serde(untagged)]
176#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
177#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
178pub enum UserContent {
179 Text(String),
181 Parts(Vec<InputContent>),
183}
184
185impl UserContent {
186 pub fn as_text(&self) -> Option<&str> {
192 match self {
193 Self::Text(text) => Some(text),
194 Self::Parts(_) => None,
195 }
196 }
197
198 pub fn to_text(&self) -> String {
223 match self {
224 Self::Text(text) => text.clone(),
225 Self::Parts(parts) => parts
226 .iter()
227 .filter_map(|part| match part {
228 InputContent::Text(part) => Some(part.text.as_str()),
229 _ => None,
230 })
231 .collect::<Vec<_>>()
232 .join("\n"),
233 }
234 }
235}
236
237impl Default for UserContent {
238 fn default() -> Self {
239 Self::Text(String::new())
240 }
241}
242
243impl From<String> for UserContent {
244 fn from(value: String) -> Self {
245 Self::Text(value)
246 }
247}
248
249impl From<&str> for UserContent {
250 fn from(value: &str) -> Self {
251 Self::Text(value.to_owned())
252 }
253}
254
255impl From<Vec<InputContent>> for UserContent {
256 fn from(value: Vec<InputContent>) -> Self {
257 Self::Parts(value)
258 }
259}
260
261#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
263#[serde(rename_all = "camelCase")]
264#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
265#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
266pub struct DeveloperMessage {
267 pub id: MessageId,
269 pub content: String,
271 #[serde(default, skip_serializing_if = "Option::is_none")]
273 pub name: Option<String>,
274 #[serde(default, skip_serializing_if = "Option::is_none")]
276 pub encrypted_value: Option<String>,
277}
278
279#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
281#[serde(rename_all = "camelCase")]
282#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
283#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
284pub struct SystemMessage {
285 pub id: MessageId,
287 pub content: String,
289 #[serde(default, skip_serializing_if = "Option::is_none")]
291 pub name: Option<String>,
292 #[serde(default, skip_serializing_if = "Option::is_none")]
294 pub encrypted_value: Option<String>,
295}
296
297#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
299#[serde(rename_all = "camelCase")]
300#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
301#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
302pub struct AssistantMessage {
303 pub id: MessageId,
305 #[serde(default, skip_serializing_if = "Option::is_none")]
307 pub content: Option<String>,
308 #[serde(default, skip_serializing_if = "Option::is_none")]
310 pub name: Option<String>,
311 #[serde(default, skip_serializing_if = "Option::is_none")]
313 pub encrypted_value: Option<String>,
314 #[serde(default, skip_serializing_if = "Option::is_none")]
316 pub tool_calls: Option<Vec<ToolCall>>,
317}
318
319#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
321#[serde(rename_all = "camelCase")]
322#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
323#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
324pub struct UserMessage {
325 pub id: MessageId,
327 pub content: UserContent,
329 #[serde(default, skip_serializing_if = "Option::is_none")]
331 pub name: Option<String>,
332 #[serde(default, skip_serializing_if = "Option::is_none")]
334 pub encrypted_value: Option<String>,
335}
336
337#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
339#[serde(rename_all = "camelCase")]
340#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
341#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
342pub struct ToolMessage {
343 pub id: MessageId,
345 pub content: String,
347 pub tool_call_id: ToolCallId,
349 #[serde(default, skip_serializing_if = "Option::is_none")]
352 pub error: Option<String>,
353 #[serde(default, skip_serializing_if = "Option::is_none")]
355 pub encrypted_value: Option<String>,
356}
357
358#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
360#[serde(rename_all = "camelCase")]
361#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
362#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
363pub struct ActivityMessage {
364 pub id: MessageId,
366 pub activity_type: String,
368 #[cfg_attr(
370 feature = "schemars",
371 schemars(with = "std::collections::BTreeMap<String, serde_json::Value>")
372 )]
373 #[cfg_attr(feature = "utoipa", schema(value_type = Object))]
374 pub content: JsonObject,
375}
376
377#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
379#[serde(rename_all = "camelCase")]
380#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
381#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
382pub struct ReasoningMessage {
383 pub id: MessageId,
385 pub content: String,
388 #[serde(default, skip_serializing_if = "Option::is_none")]
390 pub encrypted_value: Option<String>,
391}
392
393#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
395#[serde(tag = "role", rename_all = "lowercase")]
396#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
397#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
398pub enum Message {
399 Developer(DeveloperMessage),
401 System(SystemMessage),
403 Assistant(AssistantMessage),
405 User(UserMessage),
407 Tool(ToolMessage),
409 Activity(ActivityMessage),
411 Reasoning(ReasoningMessage),
413}
414
415impl Message {
416 pub fn user(id: impl Into<MessageId>, content: impl Into<UserContent>) -> Self {
418 Self::User(UserMessage {
419 id: id.into(),
420 content: content.into(),
421 ..Default::default()
422 })
423 }
424
425 pub fn assistant(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
427 Self::Assistant(AssistantMessage {
428 id: id.into(),
429 content: Some(content.into()),
430 ..Default::default()
431 })
432 }
433
434 pub fn system(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
436 Self::System(SystemMessage {
437 id: id.into(),
438 content: content.into(),
439 ..Default::default()
440 })
441 }
442
443 pub fn developer(id: impl Into<MessageId>, content: impl Into<String>) -> Self {
445 Self::Developer(DeveloperMessage {
446 id: id.into(),
447 content: content.into(),
448 ..Default::default()
449 })
450 }
451
452 pub fn tool(
454 id: impl Into<MessageId>,
455 tool_call_id: impl Into<ToolCallId>,
456 content: impl Into<String>,
457 ) -> Self {
458 Self::Tool(ToolMessage {
459 id: id.into(),
460 content: content.into(),
461 tool_call_id: tool_call_id.into(),
462 ..Default::default()
463 })
464 }
465
466 pub const fn id(&self) -> &MessageId {
468 match self {
469 Self::Developer(m) => &m.id,
470 Self::System(m) => &m.id,
471 Self::Assistant(m) => &m.id,
472 Self::User(m) => &m.id,
473 Self::Tool(m) => &m.id,
474 Self::Activity(m) => &m.id,
475 Self::Reasoning(m) => &m.id,
476 }
477 }
478
479 pub const fn role(&self) -> Role {
481 match self {
482 Self::Developer(_) => Role::Developer,
483 Self::System(_) => Role::System,
484 Self::Assistant(_) => Role::Assistant,
485 Self::User(_) => Role::User,
486 Self::Tool(_) => Role::Tool,
487 Self::Activity(_) => Role::Activity,
488 Self::Reasoning(_) => Role::Reasoning,
489 }
490 }
491}
492
493macro_rules! message_from {
494 ($($ty:ident => $variant:ident),* $(,)?) => {
495 $(
496 impl From<$ty> for Message {
497 fn from(value: $ty) -> Self {
498 Self::$variant(value)
499 }
500 }
501 )*
502 };
503}
504
505message_from! {
506 DeveloperMessage => Developer,
507 SystemMessage => System,
508 AssistantMessage => Assistant,
509 UserMessage => User,
510 ToolMessage => Tool,
511 ActivityMessage => Activity,
512 ReasoningMessage => Reasoning,
513}