llm_sdk/
types_ext.rs

1use crate::{
2    AssistantMessage, AudioPart, AudioPartDelta, CitationDelta, ImagePart, ImagePartDelta, Message,
3    Part, ReasoningPart, ReasoningPartDelta, SourcePart, TextPart, TextPartDelta, ToolCallPart,
4    ToolCallPartDelta, ToolMessage, ToolResultPart, UserMessage,
5};
6
7impl TextPart {
8    pub fn new(text: impl Into<String>) -> Self {
9        Self {
10            text: text.into(),
11            citations: None,
12        }
13    }
14
15    #[must_use]
16    pub fn with_citation(mut self, citation: Vec<crate::Citation>) -> Self {
17        self.citations = Some(citation);
18        self
19    }
20}
21
22impl From<&str> for TextPart {
23    fn from(value: &str) -> Self {
24        Self {
25            text: value.to_string(),
26            citations: None,
27        }
28    }
29}
30
31impl From<String> for TextPart {
32    fn from(value: String) -> Self {
33        Self {
34            text: value,
35            citations: None,
36        }
37    }
38}
39
40impl ImagePart {
41    pub fn new(data: impl Into<String>, mime_type: impl Into<String>) -> Self {
42        Self {
43            mime_type: mime_type.into(),
44            data: data.into(),
45            width: None,
46            height: None,
47            id: None,
48        }
49    }
50
51    #[must_use]
52    pub fn with_width(mut self, w: u32) -> Self {
53        self.width = Some(w);
54        self
55    }
56
57    #[must_use]
58    pub fn with_height(mut self, h: u32) -> Self {
59        self.height = Some(h);
60        self
61    }
62
63    #[must_use]
64    pub fn with_id(mut self, id: impl Into<String>) -> Self {
65        self.id = Some(id.into());
66        self
67    }
68}
69
70impl AudioPart {
71    pub fn new(data: impl Into<String>, format: crate::AudioFormat) -> Self {
72        Self {
73            data: data.into(),
74            format,
75            sample_rate: None,
76            channels: None,
77            transcript: None,
78            id: None,
79        }
80    }
81
82    #[must_use]
83    pub fn with_sample_rate(mut self, rate: u32) -> Self {
84        self.sample_rate = Some(rate);
85        self
86    }
87
88    #[must_use]
89    pub fn with_channels(mut self, channels: u32) -> Self {
90        self.channels = Some(channels);
91        self
92    }
93
94    #[must_use]
95    pub fn with_transcript(mut self, transcript: impl Into<String>) -> Self {
96        self.transcript = Some(transcript.into());
97        self
98    }
99
100    #[must_use]
101    pub fn with_id(mut self, id: impl Into<String>) -> Self {
102        self.id = Some(id.into());
103        self
104    }
105}
106
107impl SourcePart {
108    pub fn new(source: impl Into<String>, title: impl Into<String>, content: Vec<Part>) -> Self {
109        Self {
110            source: source.into(),
111            title: title.into(),
112            content,
113        }
114    }
115}
116
117impl ReasoningPart {
118    pub fn new(text: impl Into<String>) -> Self {
119        Self {
120            text: text.into(),
121            id: None,
122            signature: None,
123        }
124    }
125
126    #[must_use]
127    pub fn with_signature(mut self, signature: impl Into<String>) -> Self {
128        self.signature = Some(signature.into());
129        self
130    }
131
132    #[must_use]
133    pub fn with_id(mut self, id: impl Into<String>) -> Self {
134        self.id = Some(id.into());
135        self
136    }
137}
138
139impl ToolCallPart {
140    pub fn new(
141        tool_call_id: impl Into<String>,
142        tool_name: impl Into<String>,
143        args: serde_json::Value,
144    ) -> Self {
145        Self {
146            tool_call_id: tool_call_id.into(),
147            tool_name: tool_name.into(),
148            args,
149            id: None,
150        }
151    }
152
153    #[must_use]
154    pub fn with_id(mut self, id: impl Into<String>) -> Self {
155        self.id = Some(id.into());
156        self
157    }
158}
159
160impl ToolResultPart {
161    pub fn new(
162        tool_call_id: impl Into<String>,
163        tool_name: impl Into<String>,
164        content: Vec<Part>,
165    ) -> Self {
166        Self {
167            tool_call_id: tool_call_id.into(),
168            tool_name: tool_name.into(),
169            content,
170            is_error: None,
171        }
172    }
173
174    #[must_use]
175    pub fn with_is_error(mut self, is_error: bool) -> Self {
176        self.is_error = Some(is_error);
177        self
178    }
179}
180
181impl From<TextPart> for Part {
182    fn from(value: TextPart) -> Self {
183        Self::Text(value)
184    }
185}
186
187impl From<ImagePart> for Part {
188    fn from(value: ImagePart) -> Self {
189        Self::Image(value)
190    }
191}
192
193impl From<AudioPart> for Part {
194    fn from(value: AudioPart) -> Self {
195        Self::Audio(value)
196    }
197}
198
199impl From<ToolCallPart> for Part {
200    fn from(value: ToolCallPart) -> Self {
201        Self::ToolCall(value)
202    }
203}
204
205impl From<ToolResultPart> for Part {
206    fn from(value: ToolResultPart) -> Self {
207        Self::ToolResult(value)
208    }
209}
210
211impl From<SourcePart> for Part {
212    fn from(value: SourcePart) -> Self {
213        Self::Source(value)
214    }
215}
216
217impl From<ReasoningPart> for Part {
218    fn from(value: ReasoningPart) -> Self {
219        Self::Reasoning(value)
220    }
221}
222
223impl Part {
224    pub fn text(text: impl Into<String>) -> Self {
225        Self::Text(TextPart::new(text))
226    }
227
228    pub fn image(data: impl Into<String>, mime_type: impl Into<String>) -> Self {
229        Self::Image(ImagePart::new(data, mime_type))
230    }
231
232    pub fn audio(data: impl Into<String>, format: crate::AudioFormat) -> Self {
233        Self::Audio(AudioPart::new(data, format))
234    }
235
236    pub fn source(source: impl Into<String>, title: impl Into<String>, content: Vec<Self>) -> Self {
237        Self::Source(SourcePart::new(source, title, content))
238    }
239
240    pub fn reasoning(text: impl Into<String>) -> Self {
241        Self::Reasoning(ReasoningPart::new(text))
242    }
243
244    pub fn tool_call(
245        tool_call_id: impl Into<String>,
246        tool_name: impl Into<String>,
247        args: serde_json::Value,
248    ) -> Self {
249        Self::ToolCall(ToolCallPart::new(tool_call_id, tool_name, args))
250    }
251
252    pub fn tool_result(
253        tool_call_id: impl Into<String>,
254        tool_name: impl Into<String>,
255        content: Vec<Self>,
256    ) -> Self {
257        Self::ToolResult(ToolResultPart::new(tool_call_id, tool_name, content))
258    }
259}
260
261impl UserMessage {
262    pub fn new<I, P>(parts: I) -> Self
263    where
264        I: IntoIterator<Item = P>,
265        P: Into<Part>,
266    {
267        Self {
268            content: parts.into_iter().map(Into::into).collect(),
269        }
270    }
271}
272
273impl AssistantMessage {
274    pub fn new<I, P>(parts: I) -> Self
275    where
276        I: IntoIterator<Item = P>,
277        P: Into<Part>,
278    {
279        Self {
280            content: parts.into_iter().map(Into::into).collect(),
281        }
282    }
283}
284
285impl ToolMessage {
286    pub fn new<I, P>(parts: I) -> Self
287    where
288        I: IntoIterator<Item = P>,
289        P: Into<Part>,
290    {
291        Self {
292            content: parts.into_iter().map(Into::into).collect(),
293        }
294    }
295}
296
297impl Message {
298    pub fn user<I, P>(parts: I) -> Self
299    where
300        I: IntoIterator<Item = P>,
301        P: Into<Part>,
302    {
303        Self::User(UserMessage::new(parts))
304    }
305    pub fn assistant<I, P>(parts: I) -> Self
306    where
307        I: IntoIterator<Item = P>,
308        P: Into<Part>,
309    {
310        Self::Assistant(AssistantMessage::new(parts))
311    }
312
313    pub fn tool<I, P>(parts: I) -> Self
314    where
315        I: IntoIterator<Item = P>,
316        P: Into<Part>,
317    {
318        Self::Tool(ToolMessage::new(parts))
319    }
320}
321
322impl From<UserMessage> for Message {
323    fn from(value: UserMessage) -> Self {
324        Self::User(value)
325    }
326}
327
328impl From<AssistantMessage> for Message {
329    fn from(value: AssistantMessage) -> Self {
330        Self::Assistant(value)
331    }
332}
333
334impl From<ToolMessage> for Message {
335    fn from(value: ToolMessage) -> Self {
336        Self::Tool(value)
337    }
338}
339
340impl TextPartDelta {
341    pub fn new(text: impl Into<String>) -> Self {
342        Self {
343            text: text.into(),
344            citation: None,
345        }
346    }
347
348    #[must_use]
349    pub fn with_citation_delta(mut self, citation: CitationDelta) -> Self {
350        self.citation = Some(citation);
351        self
352    }
353}
354
355impl CitationDelta {
356    #[must_use]
357    pub fn with_source(mut self, source: impl Into<String>) -> Self {
358        self.source = Some(source.into());
359        self
360    }
361
362    #[must_use]
363    pub fn with_title(mut self, title: impl Into<String>) -> Self {
364        self.title = Some(title.into());
365        self
366    }
367
368    #[must_use]
369    pub fn with_cited_text(mut self, cited_text: impl Into<String>) -> Self {
370        self.cited_text = Some(cited_text.into());
371        self
372    }
373
374    #[must_use]
375    pub fn with_start_index(mut self, start_index: usize) -> Self {
376        self.start_index = Some(start_index);
377        self
378    }
379
380    #[must_use]
381    pub fn with_end_index(mut self, end_index: usize) -> Self {
382        self.end_index = Some(end_index);
383        self
384    }
385}
386
387impl ToolCallPartDelta {
388    #[must_use]
389    pub fn with_tool_call_id(mut self, tool_call_id: impl Into<String>) -> Self {
390        self.tool_call_id = Some(tool_call_id.into());
391        self
392    }
393
394    #[must_use]
395    pub fn with_tool_name(mut self, tool_name: impl Into<String>) -> Self {
396        self.tool_name = Some(tool_name.into());
397        self
398    }
399
400    #[must_use]
401    pub fn with_args(mut self, args: impl Into<String>) -> Self {
402        self.args = Some(args.into());
403        self
404    }
405
406    #[must_use]
407    pub fn with_id(mut self, id: impl Into<String>) -> Self {
408        self.id = Some(id.into());
409        self
410    }
411}
412
413impl ImagePartDelta {
414    #[must_use]
415    pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
416        self.mime_type = Some(mime_type.into());
417        self
418    }
419
420    #[must_use]
421    pub fn with_data(mut self, data: impl Into<String>) -> Self {
422        self.data = Some(data.into());
423        self
424    }
425
426    #[must_use]
427    pub fn with_width(mut self, width: u32) -> Self {
428        self.width = Some(width);
429        self
430    }
431
432    #[must_use]
433    pub fn with_height(mut self, height: u32) -> Self {
434        self.height = Some(height);
435        self
436    }
437
438    #[must_use]
439    pub fn with_id(mut self, id: impl Into<String>) -> Self {
440        self.id = Some(id.into());
441        self
442    }
443}
444
445impl AudioPartDelta {
446    #[must_use]
447    pub fn with_format(mut self, format: crate::AudioFormat) -> Self {
448        self.format = Some(format);
449        self
450    }
451
452    #[must_use]
453    pub fn with_data(mut self, data: impl Into<String>) -> Self {
454        self.data = Some(data.into());
455        self
456    }
457
458    #[must_use]
459    pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
460        self.sample_rate = Some(sample_rate);
461        self
462    }
463
464    #[must_use]
465    pub fn with_channels(mut self, channels: u32) -> Self {
466        self.channels = Some(channels);
467        self
468    }
469
470    #[must_use]
471    pub fn with_transcript(mut self, transcript: impl Into<String>) -> Self {
472        self.transcript = Some(transcript.into());
473        self
474    }
475
476    #[must_use]
477    pub fn with_id(mut self, id: impl Into<String>) -> Self {
478        self.id = Some(id.into());
479        self
480    }
481}
482
483impl ReasoningPartDelta {
484    #[must_use]
485    pub fn with_text(mut self, text: impl Into<String>) -> Self {
486        self.text = Some(text.into());
487        self
488    }
489
490    #[must_use]
491    pub fn with_id(mut self, id: impl Into<String>) -> Self {
492        self.id = Some(id.into());
493        self
494    }
495
496    #[must_use]
497    pub fn with_signature(mut self, signature: impl Into<String>) -> Self {
498        self.signature = Some(signature.into());
499        self
500    }
501}