starweaver_model/message/
request_parts.rs1use serde::{Deserialize, Serialize};
4use serde_json::Map;
5
6use super::{Metadata, ToolReturnPart};
7
8#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
10pub enum CachePointTtl {
11 #[serde(rename = "5m")]
13 FiveMinutes,
14 #[serde(rename = "1h")]
16 OneHour,
17}
18
19impl CachePointTtl {
20 #[must_use]
22 pub const fn as_str(self) -> &'static str {
23 match self {
24 Self::FiveMinutes => "5m",
25 Self::OneHour => "1h",
26 }
27 }
28}
29
30#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
32#[serde(tag = "kind", rename_all = "snake_case")]
33pub enum ModelRequestPart {
34 SystemPrompt {
36 text: String,
38 #[serde(default, skip_serializing_if = "Map::is_empty")]
40 metadata: Metadata,
41 },
42 UserPrompt {
44 content: Vec<ContentPart>,
46 #[serde(default, skip_serializing_if = "Option::is_none")]
48 name: Option<String>,
49 #[serde(default, skip_serializing_if = "Map::is_empty")]
51 metadata: Metadata,
52 },
53 ToolReturn(ToolReturnPart),
55 RetryPrompt {
57 text: String,
59 #[serde(default, skip_serializing_if = "Option::is_none")]
61 tool_call_id: Option<String>,
62 #[serde(default, skip_serializing_if = "Map::is_empty")]
64 metadata: Metadata,
65 },
66 Instruction {
68 text: String,
70 #[serde(default, skip_serializing_if = "Map::is_empty")]
72 metadata: Metadata,
73 },
74}
75
76#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
78#[serde(tag = "kind", rename_all = "snake_case")]
79pub enum ContentPart {
80 CachePoint {
86 #[serde(default, skip_serializing_if = "Option::is_none")]
88 ttl: Option<CachePointTtl>,
89 },
90 Text {
92 text: String,
94 },
95 ImageUrl {
97 url: String,
99 },
100 FileUrl {
102 url: String,
104 media_type: String,
106 },
107 Binary {
109 data: Vec<u8>,
111 media_type: String,
113 },
114 ResourceRef {
116 uri: String,
118 media_type: String,
120 resource_type: String,
122 #[serde(default, skip_serializing_if = "Map::is_empty")]
124 metadata: Metadata,
125 },
126 DataUrl {
128 data_url: String,
130 media_type: String,
132 },
133}
134
135impl ContentPart {
136 #[must_use]
138 pub const fn cache_point() -> Self {
139 Self::CachePoint { ttl: None }
140 }
141
142 #[must_use]
144 pub const fn cache_point_with_ttl(ttl: CachePointTtl) -> Self {
145 Self::CachePoint { ttl: Some(ttl) }
146 }
147
148 #[must_use]
150 pub fn text(text: impl Into<String>) -> Self {
151 Self::Text { text: text.into() }
152 }
153
154 #[must_use]
156 pub fn image_url(url: impl Into<String>) -> Self {
157 Self::ImageUrl { url: url.into() }
158 }
159
160 #[must_use]
162 pub fn file_url(url: impl Into<String>, media_type: impl Into<String>) -> Self {
163 Self::FileUrl {
164 url: url.into(),
165 media_type: media_type.into(),
166 }
167 }
168
169 #[must_use]
171 pub fn binary(data: impl Into<Vec<u8>>, media_type: impl Into<String>) -> Self {
172 Self::Binary {
173 data: data.into(),
174 media_type: media_type.into(),
175 }
176 }
177
178 #[must_use]
180 pub fn image_bytes(data: impl Into<Vec<u8>>, media_type: impl Into<String>) -> Self {
181 Self::binary(data, media_type)
182 }
183
184 #[must_use]
186 pub fn audio_bytes(data: impl Into<Vec<u8>>, media_type: impl Into<String>) -> Self {
187 Self::binary(data, media_type)
188 }
189
190 #[must_use]
192 pub fn video_bytes(data: impl Into<Vec<u8>>, media_type: impl Into<String>) -> Self {
193 Self::binary(data, media_type)
194 }
195
196 #[must_use]
198 pub fn data_url(data_url: impl Into<String>, media_type: impl Into<String>) -> Self {
199 Self::DataUrl {
200 data_url: data_url.into(),
201 media_type: media_type.into(),
202 }
203 }
204
205 #[must_use]
207 pub fn resource_ref(
208 uri: impl Into<String>,
209 media_type: impl Into<String>,
210 resource_type: impl Into<String>,
211 ) -> Self {
212 Self::ResourceRef {
213 uri: uri.into(),
214 media_type: media_type.into(),
215 resource_type: resource_type.into(),
216 metadata: Metadata::new(),
217 }
218 }
219
220 #[must_use]
222 pub fn with_resource_metadata(mut self, metadata: Metadata) -> Self {
223 if let Self::ResourceRef {
224 metadata: existing, ..
225 } = &mut self
226 {
227 *existing = metadata;
228 }
229 self
230 }
231}