async_openai/types/responses/
impls.rs

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