Skip to main content

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