async_openai/types/responses/
impls.rs

1use crate::types::responses::{
2    ApplyPatchToolCallItemParam, ApplyPatchToolCallOutputItemParam, CodeInterpreterContainerAuto,
3    CodeInterpreterTool, CodeInterpreterToolCall, CodeInterpreterToolContainer,
4    ComputerCallOutputItemParam, ComputerToolCall, ComputerUsePreviewTool, ConversationParam,
5    CustomToolCall, CustomToolCallOutput, CustomToolParam, EasyInputContent, EasyInputMessage,
6    FileSearchTool, FileSearchToolCall, FunctionCallOutput, FunctionCallOutputItemParam,
7    FunctionShellCallItemParam, FunctionShellCallOutputItemParam, FunctionTool, FunctionToolCall,
8    ImageGenTool, ImageGenToolCall, InputContent, InputFileContent, InputImageContent, InputItem,
9    InputMessage, InputParam, InputTextContent, Item, ItemReference, ItemReferenceType,
10    LocalShellToolCall, LocalShellToolCallOutput, MCPApprovalRequest, MCPApprovalResponse,
11    MCPListTools, MCPToolCall, MessageItem, MessageType, OutputMessage, OutputMessageContent,
12    OutputTextContent, Prompt, Reasoning, ReasoningEffort, ReasoningItem, ReasoningSummary,
13    RefusalContent, ResponsePromptVariables, ResponseStreamOptions, ResponseTextParam, Role,
14    TextResponseFormatConfiguration, Tool, ToolChoiceCustom, ToolChoiceFunction, ToolChoiceMCP,
15    ToolChoiceOptions, ToolChoiceParam, ToolChoiceTypes, WebSearchTool, WebSearchToolCall,
16};
17use crate::types::{chat::ResponseFormatJsonSchema, MCPTool};
18
19impl<S: Into<String>> From<S> for EasyInputMessage {
20    fn from(value: S) -> Self {
21        EasyInputMessage {
22            r#type: MessageType::Message,
23            role: Role::User,
24            content: EasyInputContent::Text(value.into()),
25        }
26    }
27}
28
29impl From<EasyInputMessage> for InputItem {
30    fn from(msg: EasyInputMessage) -> Self {
31        InputItem::EasyMessage(msg)
32    }
33}
34
35// InputItem ergonomics
36
37impl From<InputMessage> for InputItem {
38    fn from(msg: InputMessage) -> Self {
39        InputItem::Item(Item::Message(MessageItem::Input(msg)))
40    }
41}
42
43impl From<Item> for InputItem {
44    fn from(item: Item) -> Self {
45        InputItem::Item(item)
46    }
47}
48
49impl From<ItemReference> for InputItem {
50    fn from(item: ItemReference) -> Self {
51        InputItem::ItemReference(item)
52    }
53}
54
55// InputParam ergonomics: from InputItem
56
57impl From<InputItem> for InputParam {
58    fn from(item: InputItem) -> Self {
59        InputParam::Items(vec![item])
60    }
61}
62
63impl From<Item> for InputParam {
64    fn from(item: Item) -> Self {
65        InputParam::Items(vec![InputItem::Item(item)])
66    }
67}
68
69impl From<MessageItem> for InputParam {
70    fn from(item: MessageItem) -> Self {
71        InputParam::Items(vec![InputItem::Item(Item::Message(item))])
72    }
73}
74
75impl From<InputMessage> for InputParam {
76    fn from(msg: InputMessage) -> Self {
77        InputParam::Items(vec![InputItem::Item(Item::Message(MessageItem::Input(
78            msg,
79        )))])
80    }
81}
82
83impl<I: Into<InputItem>> From<Vec<I>> for InputParam {
84    fn from(items: Vec<I>) -> Self {
85        InputParam::Items(items.into_iter().map(|item| item.into()).collect())
86    }
87}
88
89impl<I: Into<InputItem>, const N: usize> From<[I; N]> for InputParam {
90    fn from(items: [I; N]) -> Self {
91        InputParam::Items(items.into_iter().map(|item| item.into()).collect())
92    }
93}
94
95// InputParam ergonomics: from string "family"
96
97impl From<&str> for InputParam {
98    fn from(value: &str) -> Self {
99        InputParam::Text(value.into())
100    }
101}
102
103impl From<String> for InputParam {
104    fn from(value: String) -> Self {
105        InputParam::Text(value)
106    }
107}
108
109impl From<&String> for InputParam {
110    fn from(value: &String) -> Self {
111        InputParam::Text(value.clone())
112    }
113}
114
115// InputParam ergonomics: from vector family
116
117macro_rules! impl_inputparam_easy_from_collection {
118    // Vec<T>
119    ($t:ty, $map:expr, $clone:expr) => {
120        impl From<Vec<$t>> for InputParam {
121            fn from(values: Vec<$t>) -> Self {
122                InputParam::Items(
123                    values
124                        .into_iter()
125                        .map(|value| {
126                            InputItem::EasyMessage(EasyInputMessage {
127                                r#type: MessageType::Message,
128                                role: Role::User,
129                                content: EasyInputContent::Text($map(value)),
130                            })
131                        })
132                        .collect(),
133                )
134            }
135        }
136        // &[T; N]
137        impl<const N: usize> From<[$t; N]> for InputParam {
138            fn from(values: [$t; N]) -> Self {
139                InputParam::Items(
140                    values
141                        .into_iter()
142                        .map(|value| {
143                            InputItem::EasyMessage(EasyInputMessage {
144                                r#type: MessageType::Message,
145                                role: Role::User,
146                                content: EasyInputContent::Text($map(value)),
147                            })
148                        })
149                        .collect(),
150                )
151            }
152        }
153        // &Vec<T>
154        impl From<&Vec<$t>> for InputParam {
155            fn from(values: &Vec<$t>) -> Self {
156                InputParam::Items(
157                    values
158                        .iter()
159                        .map(|value| {
160                            InputItem::EasyMessage(EasyInputMessage {
161                                r#type: MessageType::Message,
162                                role: Role::User,
163                                content: EasyInputContent::Text($clone(value)),
164                            })
165                        })
166                        .collect(),
167                )
168            }
169        }
170    };
171}
172
173// Apply for &str
174impl_inputparam_easy_from_collection!(&str, |v: &str| v.to_string(), |v: &str| v.to_string());
175// Apply for String
176impl_inputparam_easy_from_collection!(String, |v: String| v, |v: &String| v.clone());
177// Apply for &String
178impl_inputparam_easy_from_collection!(&String, |v: &String| v.clone(), |v: &String| v.clone());
179
180// ConversationParam ergonomics
181
182impl<S: Into<String>> From<S> for ConversationParam {
183    fn from(id: S) -> Self {
184        ConversationParam::ConversationID(id.into())
185    }
186}
187
188// ToolChoiceParam ergonomics
189
190impl From<ToolChoiceOptions> for ToolChoiceParam {
191    fn from(mode: ToolChoiceOptions) -> Self {
192        ToolChoiceParam::Mode(mode)
193    }
194}
195
196impl From<ToolChoiceTypes> for ToolChoiceParam {
197    fn from(tool_type: ToolChoiceTypes) -> Self {
198        ToolChoiceParam::Hosted(tool_type)
199    }
200}
201
202impl<S: Into<String>> From<S> for ToolChoiceParam {
203    fn from(name: S) -> Self {
204        ToolChoiceParam::Function(ToolChoiceFunction { name: name.into() })
205    }
206}
207
208impl From<ToolChoiceFunction> for ToolChoiceParam {
209    fn from(function: ToolChoiceFunction) -> Self {
210        ToolChoiceParam::Function(function)
211    }
212}
213
214impl From<ToolChoiceMCP> for ToolChoiceParam {
215    fn from(mcp: ToolChoiceMCP) -> Self {
216        ToolChoiceParam::Mcp(mcp)
217    }
218}
219
220impl From<ToolChoiceCustom> for ToolChoiceParam {
221    fn from(custom: ToolChoiceCustom) -> Self {
222        ToolChoiceParam::Custom(custom)
223    }
224}
225
226// ResponseTextParam ergonomics
227
228impl From<TextResponseFormatConfiguration> for ResponseTextParam {
229    fn from(format: TextResponseFormatConfiguration) -> Self {
230        ResponseTextParam {
231            format,
232            verbosity: None,
233        }
234    }
235}
236
237impl From<ResponseFormatJsonSchema> for ResponseTextParam {
238    fn from(schema: ResponseFormatJsonSchema) -> Self {
239        ResponseTextParam {
240            format: TextResponseFormatConfiguration::JsonSchema(schema),
241            verbosity: None,
242        }
243    }
244}
245
246// ResponseStreamOptions ergonomics
247
248impl From<bool> for ResponseStreamOptions {
249    fn from(include_obfuscation: bool) -> Self {
250        ResponseStreamOptions {
251            include_obfuscation: Some(include_obfuscation),
252        }
253    }
254}
255
256// Reasoning ergonomics
257
258impl From<ReasoningEffort> for Reasoning {
259    fn from(effort: ReasoningEffort) -> Self {
260        Reasoning {
261            effort: Some(effort),
262            summary: None,
263        }
264    }
265}
266
267impl From<ReasoningSummary> for Reasoning {
268    fn from(summary: ReasoningSummary) -> Self {
269        Reasoning {
270            effort: None,
271            summary: Some(summary),
272        }
273    }
274}
275
276// Prompt ergonomics
277
278impl<S: Into<String>> From<S> for Prompt {
279    fn from(id: S) -> Self {
280        Prompt {
281            id: id.into(),
282            version: None,
283            variables: None,
284        }
285    }
286}
287
288// InputTextContent ergonomics
289
290impl<S: Into<String>> From<S> for InputTextContent {
291    fn from(text: S) -> Self {
292        InputTextContent { text: text.into() }
293    }
294}
295
296// InputContent ergonomics
297
298impl From<InputTextContent> for InputContent {
299    fn from(content: InputTextContent) -> Self {
300        InputContent::InputText(content)
301    }
302}
303
304impl From<InputImageContent> for InputContent {
305    fn from(content: InputImageContent) -> Self {
306        InputContent::InputImage(content)
307    }
308}
309
310impl From<InputFileContent> for InputContent {
311    fn from(content: InputFileContent) -> Self {
312        InputContent::InputFile(content)
313    }
314}
315
316impl<S: Into<String>> From<S> for InputContent {
317    fn from(text: S) -> Self {
318        InputContent::InputText(InputTextContent { text: text.into() })
319    }
320}
321
322// ResponsePromptVariables ergonomics
323
324impl From<InputContent> for ResponsePromptVariables {
325    fn from(content: InputContent) -> Self {
326        ResponsePromptVariables::Content(content)
327    }
328}
329
330impl<S: Into<String>> From<S> for ResponsePromptVariables {
331    fn from(text: S) -> Self {
332        ResponsePromptVariables::String(text.into())
333    }
334}
335
336// MessageItem ergonomics
337
338impl From<InputMessage> for MessageItem {
339    fn from(msg: InputMessage) -> Self {
340        MessageItem::Input(msg)
341    }
342}
343
344impl From<OutputMessage> for MessageItem {
345    fn from(msg: OutputMessage) -> Self {
346        MessageItem::Output(msg)
347    }
348}
349
350// FunctionCallOutput ergonomics
351
352impl From<&str> for FunctionCallOutput {
353    fn from(text: &str) -> Self {
354        FunctionCallOutput::Text(text.to_string())
355    }
356}
357
358impl From<String> for FunctionCallOutput {
359    fn from(text: String) -> Self {
360        FunctionCallOutput::Text(text)
361    }
362}
363
364impl From<Vec<InputContent>> for FunctionCallOutput {
365    fn from(content: Vec<InputContent>) -> Self {
366        FunctionCallOutput::Content(content)
367    }
368}
369
370// RefusalContent ergonomics
371
372impl<S: Into<String>> From<S> for RefusalContent {
373    fn from(refusal: S) -> Self {
374        RefusalContent {
375            refusal: refusal.into(),
376        }
377    }
378}
379
380// OutputMessageContent ergonomics
381
382impl From<OutputTextContent> for OutputMessageContent {
383    fn from(content: OutputTextContent) -> Self {
384        OutputMessageContent::OutputText(content)
385    }
386}
387
388impl From<RefusalContent> for OutputMessageContent {
389    fn from(content: RefusalContent) -> Self {
390        OutputMessageContent::Refusal(content)
391    }
392}
393
394// Item ergonomics
395
396impl From<MessageItem> for Item {
397    fn from(item: MessageItem) -> Self {
398        Item::Message(item)
399    }
400}
401
402impl From<FileSearchToolCall> for Item {
403    fn from(call: FileSearchToolCall) -> Self {
404        Item::FileSearchCall(call)
405    }
406}
407
408impl From<ComputerToolCall> for Item {
409    fn from(call: ComputerToolCall) -> Self {
410        Item::ComputerCall(call)
411    }
412}
413
414impl From<ComputerCallOutputItemParam> for Item {
415    fn from(output: ComputerCallOutputItemParam) -> Self {
416        Item::ComputerCallOutput(output)
417    }
418}
419
420impl From<WebSearchToolCall> for Item {
421    fn from(call: WebSearchToolCall) -> Self {
422        Item::WebSearchCall(call)
423    }
424}
425
426impl From<FunctionToolCall> for Item {
427    fn from(call: FunctionToolCall) -> Self {
428        Item::FunctionCall(call)
429    }
430}
431
432impl From<FunctionCallOutputItemParam> for Item {
433    fn from(output: FunctionCallOutputItemParam) -> Self {
434        Item::FunctionCallOutput(output)
435    }
436}
437
438impl From<ReasoningItem> for Item {
439    fn from(item: ReasoningItem) -> Self {
440        Item::Reasoning(item)
441    }
442}
443
444impl From<ImageGenToolCall> for Item {
445    fn from(call: ImageGenToolCall) -> Self {
446        Item::ImageGenerationCall(call)
447    }
448}
449
450impl From<CodeInterpreterToolCall> for Item {
451    fn from(call: CodeInterpreterToolCall) -> Self {
452        Item::CodeInterpreterCall(call)
453    }
454}
455
456impl From<LocalShellToolCall> for Item {
457    fn from(call: LocalShellToolCall) -> Self {
458        Item::LocalShellCall(call)
459    }
460}
461
462impl From<LocalShellToolCallOutput> for Item {
463    fn from(output: LocalShellToolCallOutput) -> Self {
464        Item::LocalShellCallOutput(output)
465    }
466}
467
468impl From<FunctionShellCallItemParam> for Item {
469    fn from(call: FunctionShellCallItemParam) -> Self {
470        Item::FunctionShellCall(call)
471    }
472}
473
474impl From<FunctionShellCallOutputItemParam> for Item {
475    fn from(output: FunctionShellCallOutputItemParam) -> Self {
476        Item::FunctionShellCallOutput(output)
477    }
478}
479
480impl From<ApplyPatchToolCallItemParam> for Item {
481    fn from(call: ApplyPatchToolCallItemParam) -> Self {
482        Item::ApplyPatchCall(call)
483    }
484}
485
486impl From<ApplyPatchToolCallOutputItemParam> for Item {
487    fn from(output: ApplyPatchToolCallOutputItemParam) -> Self {
488        Item::ApplyPatchCallOutput(output)
489    }
490}
491
492impl From<MCPListTools> for Item {
493    fn from(tools: MCPListTools) -> Self {
494        Item::McpListTools(tools)
495    }
496}
497
498impl From<MCPApprovalRequest> for Item {
499    fn from(request: MCPApprovalRequest) -> Self {
500        Item::McpApprovalRequest(request)
501    }
502}
503
504impl From<MCPApprovalResponse> for Item {
505    fn from(response: MCPApprovalResponse) -> Self {
506        Item::McpApprovalResponse(response)
507    }
508}
509
510impl From<MCPToolCall> for Item {
511    fn from(call: MCPToolCall) -> Self {
512        Item::McpCall(call)
513    }
514}
515
516impl From<CustomToolCallOutput> for Item {
517    fn from(output: CustomToolCallOutput) -> Self {
518        Item::CustomToolCallOutput(output)
519    }
520}
521
522impl From<CustomToolCall> for Item {
523    fn from(call: CustomToolCall) -> Self {
524        Item::CustomToolCall(call)
525    }
526}
527
528// Tool ergonomics
529
530impl From<FunctionTool> for Tool {
531    fn from(tool: FunctionTool) -> Self {
532        Tool::Function(tool)
533    }
534}
535
536impl From<FileSearchTool> for Tool {
537    fn from(tool: FileSearchTool) -> Self {
538        Tool::FileSearch(tool)
539    }
540}
541
542impl From<ComputerUsePreviewTool> for Tool {
543    fn from(tool: ComputerUsePreviewTool) -> Self {
544        Tool::ComputerUsePreview(tool)
545    }
546}
547
548impl From<WebSearchTool> for Tool {
549    fn from(tool: WebSearchTool) -> Self {
550        Tool::WebSearch(tool)
551    }
552}
553
554impl From<MCPTool> for Tool {
555    fn from(tool: MCPTool) -> Self {
556        Tool::Mcp(tool)
557    }
558}
559
560impl From<CodeInterpreterTool> for Tool {
561    fn from(tool: CodeInterpreterTool) -> Self {
562        Tool::CodeInterpreter(tool)
563    }
564}
565
566impl From<ImageGenTool> for Tool {
567    fn from(tool: ImageGenTool) -> Self {
568        Tool::ImageGeneration(tool)
569    }
570}
571
572impl From<CustomToolParam> for Tool {
573    fn from(tool: CustomToolParam) -> Self {
574        Tool::Custom(tool)
575    }
576}
577
578// Vec<Tool> ergonomics
579
580impl From<Tool> for Vec<Tool> {
581    fn from(tool: Tool) -> Self {
582        vec![tool]
583    }
584}
585
586impl From<FunctionTool> for Vec<Tool> {
587    fn from(tool: FunctionTool) -> Self {
588        vec![Tool::Function(tool)]
589    }
590}
591
592impl From<FileSearchTool> for Vec<Tool> {
593    fn from(tool: FileSearchTool) -> Self {
594        vec![Tool::FileSearch(tool)]
595    }
596}
597
598impl From<ComputerUsePreviewTool> for Vec<Tool> {
599    fn from(tool: ComputerUsePreviewTool) -> Self {
600        vec![Tool::ComputerUsePreview(tool)]
601    }
602}
603
604impl From<WebSearchTool> for Vec<Tool> {
605    fn from(tool: WebSearchTool) -> Self {
606        vec![Tool::WebSearch(tool)]
607    }
608}
609
610impl From<MCPTool> for Vec<Tool> {
611    fn from(tool: MCPTool) -> Self {
612        vec![Tool::Mcp(tool)]
613    }
614}
615
616impl From<CodeInterpreterTool> for Vec<Tool> {
617    fn from(tool: CodeInterpreterTool) -> Self {
618        vec![Tool::CodeInterpreter(tool)]
619    }
620}
621
622impl From<ImageGenTool> for Vec<Tool> {
623    fn from(tool: ImageGenTool) -> Self {
624        vec![Tool::ImageGeneration(tool)]
625    }
626}
627
628impl From<CustomToolParam> for Vec<Tool> {
629    fn from(tool: CustomToolParam) -> Self {
630        vec![Tool::Custom(tool)]
631    }
632}
633
634// EasyInputContent ergonomics
635
636impl Default for EasyInputContent {
637    fn default() -> Self {
638        Self::Text("".to_string())
639    }
640}
641
642impl From<String> for EasyInputContent {
643    fn from(value: String) -> Self {
644        Self::Text(value)
645    }
646}
647
648impl From<&str> for EasyInputContent {
649    fn from(value: &str) -> Self {
650        Self::Text(value.to_owned())
651    }
652}
653
654// Defaults
655
656impl Default for CodeInterpreterToolContainer {
657    fn default() -> Self {
658        Self::Auto(CodeInterpreterContainerAuto::default())
659    }
660}
661
662impl Default for InputParam {
663    fn default() -> Self {
664        Self::Text(String::new())
665    }
666}
667
668impl ItemReference {
669    /// Create a new item reference with the given ID.
670    pub fn new(id: impl Into<String>) -> Self {
671        Self {
672            r#type: Some(ItemReferenceType::ItemReference),
673            id: id.into(),
674        }
675    }
676}