1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum Role {
12 User,
14 Assistant,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Message {
33 pub role: Role,
35 pub content: MessageContent,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum MessageContent {
43 Text(String),
45 ToolResult {
47 tool_use_id: String,
48 content: String,
49 },
50 Parts(Vec<ContentPart>),
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(tag = "type")]
57pub enum ContentPart {
58 #[serde(rename = "text")]
60 Text { text: String },
61 #[serde(rename = "tool_use")]
63 ToolUse {
64 id: String,
65 name: String,
66 input: serde_json::Value,
67 },
68 #[serde(rename = "tool_result")]
70 ToolResult {
71 tool_use_id: String,
72 content: String,
73 },
74}
75
76impl Message {
77 pub fn user(content: impl Into<String>) -> Self {
86 Self {
87 role: Role::User,
88 content: MessageContent::Text(content.into()),
89 }
90 }
91
92 pub fn assistant(content: impl Into<String>) -> Self {
97 Self {
98 role: Role::Assistant,
99 content: MessageContent::Text(content.into()),
100 }
101 }
102
103 pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
108 Self {
109 role: Role::User,
110 content: MessageContent::ToolResult {
111 tool_use_id: tool_use_id.into(),
112 content: content.into(),
113 },
114 }
115 }
116}