1use std::fmt;
2
3use reqwest::header::HeaderMap;
4use serde::{Deserialize, Serialize};
5
6use crate::report::CompletionReport;
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum Role {
16 System,
18 Developer,
21 User,
23 Assistant,
25 Tool,
27 Custom(String),
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33#[serde(tag = "type", rename_all = "snake_case")]
34pub enum Part {
35 Text {
37 text: String,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 provider_state: Option<ProviderState>,
41 },
42 Reasoning(Reasoning),
44 ImageUrl { image_url: ImageUrl },
46 Image { image: ImagePart },
48 ToolCall(ToolCall),
50 NativeToolCall(NativeToolCall),
52 ToolResult(ToolResult),
54 ProviderItem(ServerToolItem),
56}
57
58impl Part {
59 pub fn text(text: impl Into<String>) -> Self {
61 Self::Text {
62 text: text.into(),
63 provider_state: None,
64 }
65 }
66
67 pub fn text_with_provider_state(
69 text: impl Into<String>,
70 format: ProviderStateFormat,
71 data: serde_json::Value,
72 ) -> Self {
73 Self::Text {
74 text: text.into(),
75 provider_state: Some(ProviderState::new(format, data)),
76 }
77 }
78
79 pub fn reasoning(reasoning: Reasoning) -> Self {
81 Self::Reasoning(reasoning)
82 }
83
84 pub fn image_url(url: impl Into<String>) -> Self {
86 Self::ImageUrl {
87 image_url: ImageUrl {
88 url: url.into(),
89 detail: None,
90 },
91 }
92 }
93
94 pub fn image(image: ImagePart) -> Self {
96 Self::Image { image }
97 }
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum ReasoningKind {
108 Summary,
110 Text,
112 Encrypted,
114 Redacted,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
120#[serde(rename_all = "snake_case")]
121pub enum ProviderStateFormat {
122 OpenAiChat,
124 OpenAiResponses,
126 AnthropicMessages,
128 GeminiGenerateContent,
130 Custom(String),
132}
133
134#[derive(Clone, PartialEq, Serialize, Deserialize)]
140pub struct ProviderState {
141 pub format: ProviderStateFormat,
143 pub data: serde_json::Value,
145}
146
147impl fmt::Debug for ProviderState {
148 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
149 let bytes = serde_json::to_vec(&self.data)
150 .map(|data| data.len())
151 .unwrap_or_default();
152 formatter
153 .debug_struct("ProviderState")
154 .field("format", &self.format)
155 .field("data_bytes", &bytes)
156 .finish_non_exhaustive()
157 }
158}
159
160impl ProviderState {
161 pub fn new(format: ProviderStateFormat, data: serde_json::Value) -> Self {
163 Self { format, data }
164 }
165}
166
167#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
174pub struct Reasoning {
175 pub kind: ReasoningKind,
177 pub summary: Option<String>,
179 pub text: Option<String>,
181 pub state: Option<ProviderState>,
183}
184
185impl Reasoning {
186 pub fn summary(text: impl Into<String>) -> Self {
188 Self {
189 kind: ReasoningKind::Summary,
190 summary: Some(text.into()),
191 text: None,
192 state: None,
193 }
194 }
195
196 pub fn text(text: impl Into<String>) -> Self {
198 Self {
199 kind: ReasoningKind::Text,
200 summary: None,
201 text: Some(text.into()),
202 state: None,
203 }
204 }
205
206 pub fn encrypted() -> Self {
208 Self {
209 kind: ReasoningKind::Encrypted,
210 summary: None,
211 text: None,
212 state: None,
213 }
214 }
215
216 pub fn redacted() -> Self {
218 Self {
219 kind: ReasoningKind::Redacted,
220 summary: None,
221 text: None,
222 state: None,
223 }
224 }
225
226 pub fn with_state(mut self, format: ProviderStateFormat, data: serde_json::Value) -> Self {
228 self.state = Some(ProviderState::new(format, data));
229 self
230 }
231
232 pub fn display_text(&self) -> Option<&str> {
235 self.text.as_deref().or(self.summary.as_deref())
236 }
237}
238
239#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
241pub struct ImageUrl {
242 pub url: String,
244 pub detail: Option<ImageDetail>,
246}
247
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
250#[serde(rename_all = "snake_case")]
251pub enum ImageDetail {
252 Auto,
254 Low,
256 High,
258}
259
260#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
262#[serde(tag = "type", rename_all = "snake_case")]
263pub enum ServerTool {
264 WebSearch,
266 UrlContext,
268 FileSearch,
270 Custom {
272 name: String,
274 config: serde_json::Value,
276 },
277}
278
279#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
281#[serde(rename_all = "snake_case")]
282pub enum ServerToolState {
283 InProgress,
285 Completed,
287 Failed,
289 Unknown,
291}
292
293#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
295pub struct ServerToolItem {
296 pub tool: Option<String>,
298 pub call_id: Option<String>,
300 pub state: Option<ServerToolState>,
302 pub provider_state: ProviderState,
304}
305
306impl ServerToolItem {
307 pub fn new(provider_state: ProviderState) -> Self {
309 Self {
310 tool: None,
311 call_id: None,
312 state: None,
313 provider_state,
314 }
315 }
316}
317
318#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
320#[serde(tag = "type", rename_all = "snake_case")]
321pub enum ImageSource {
322 Url {
324 url: String,
326 },
327 Base64 {
329 media_type: String,
331 data: String,
333 },
334 FileRef {
336 uri: String,
338 #[serde(default, skip_serializing_if = "Option::is_none")]
340 media_type: Option<String>,
341 },
342}
343
344#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
346pub struct ImagePart {
347 pub source: ImageSource,
349 #[serde(default, skip_serializing_if = "Option::is_none")]
351 pub detail: Option<ImageDetail>,
352}
353
354impl ImagePart {
355 pub fn url(url: impl Into<String>) -> Self {
357 Self {
358 source: ImageSource::Url { url: url.into() },
359 detail: None,
360 }
361 }
362
363 pub fn base64(media_type: impl Into<String>, data: impl Into<String>) -> Self {
365 Self {
366 source: ImageSource::Base64 {
367 media_type: media_type.into(),
368 data: data.into(),
369 },
370 detail: None,
371 }
372 }
373
374 pub fn file_ref(uri: impl Into<String>, media_type: Option<String>) -> Self {
376 Self {
377 source: ImageSource::FileRef {
378 uri: uri.into(),
379 media_type,
380 },
381 detail: None,
382 }
383 }
384
385 pub fn detail(mut self, detail: ImageDetail) -> Self {
387 self.detail = Some(detail);
388 self
389 }
390}
391
392#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
394pub struct ToolCall {
395 pub id: Option<String>,
398 pub name: String,
400 pub arguments: serde_json::Value,
402 #[serde(default, skip_serializing_if = "Option::is_none")]
407 pub provider_state: Option<ProviderState>,
408}
409
410impl ToolCall {
411 pub fn new(name: impl Into<String>, arguments: serde_json::Value) -> Self {
413 Self {
414 id: None,
415 name: name.into(),
416 arguments,
417 provider_state: None,
418 }
419 }
420
421 pub fn with_id(mut self, id: impl Into<String>) -> Self {
423 self.id = Some(id.into());
424 self
425 }
426
427 pub fn with_provider_state(
429 mut self,
430 format: ProviderStateFormat,
431 data: serde_json::Value,
432 ) -> Self {
433 self.provider_state = Some(ProviderState::new(format, data));
434 self
435 }
436}
437
438#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
440#[serde(tag = "type", rename_all = "snake_case")]
441pub enum ToolResultPart {
442 Text { text: String },
444 Json { value: serde_json::Value },
446 Image { image: ImagePart },
448}
449
450#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
452pub struct ToolResult {
453 pub call_id: Option<String>,
455 pub name: String,
457 pub content: String,
459 #[serde(default, skip_serializing_if = "Vec::is_empty")]
461 pub parts: Vec<ToolResultPart>,
462 pub is_error: bool,
464 #[serde(default, skip_serializing_if = "Option::is_none")]
466 pub status: Option<String>,
467 #[serde(default, skip_serializing_if = "Option::is_none")]
469 pub metadata: Option<serde_json::Value>,
470}
471
472impl ToolResult {
473 pub fn new(
476 call_id: Option<String>,
477 name: impl Into<String>,
478 content: impl Into<String>,
479 ) -> Self {
480 let content = content.into();
481 Self {
482 call_id,
483 name: name.into(),
484 parts: if content.is_empty() {
485 Vec::new()
486 } else {
487 vec![ToolResultPart::Text {
488 text: content.clone(),
489 }]
490 },
491 content,
492 is_error: false,
493 status: None,
494 metadata: None,
495 }
496 }
497
498 pub fn error(
500 call_id: Option<String>,
501 name: impl Into<String>,
502 content: impl Into<String>,
503 ) -> Self {
504 let mut result = Self::new(call_id, name, content);
505 result.is_error = true;
506 result
507 }
508
509 pub fn from_parts(
512 call_id: Option<String>,
513 name: impl Into<String>,
514 parts: Vec<ToolResultPart>,
515 ) -> Self {
516 Self {
517 call_id,
518 name: name.into(),
519 content: tool_result_parts_text(&parts),
520 parts,
521 is_error: false,
522 status: None,
523 metadata: None,
524 }
525 }
526
527 pub fn json(
529 call_id: Option<String>,
530 name: impl Into<String>,
531 value: serde_json::Value,
532 ) -> Self {
533 Self::from_parts(call_id, name, vec![ToolResultPart::Json { value }])
534 }
535
536 pub fn image(call_id: Option<String>, name: impl Into<String>, image: ImagePart) -> Self {
538 Self::from_parts(call_id, name, vec![ToolResultPart::Image { image }])
539 }
540
541 pub fn status(mut self, status: impl Into<String>) -> Self {
543 self.status = Some(status.into());
544 self
545 }
546
547 pub fn metadata(mut self, metadata: serde_json::Value) -> Self {
549 self.metadata = Some(metadata);
550 self
551 }
552}
553
554fn tool_result_parts_text(parts: &[ToolResultPart]) -> String {
555 let mut output = String::new();
556 for part in parts {
557 if !output.is_empty() {
558 output.push('\n');
559 }
560 match part {
561 ToolResultPart::Text { text } => output.push_str(text),
562 ToolResultPart::Json { value } => {
563 output
564 .push_str(&serde_json::to_string(value).unwrap_or_else(|_| "null".to_string()));
565 }
566 ToolResultPart::Image { image } => {
567 output.push_str(&format!("[image: {}]", image_source_label(&image.source)));
568 }
569 }
570 }
571 output
572}
573
574fn image_source_label(source: &ImageSource) -> String {
575 match source {
576 ImageSource::Url { url } => url.clone(),
577 ImageSource::Base64 { media_type, .. } => format!("base64:{media_type}"),
578 ImageSource::FileRef { uri, .. } => uri.clone(),
579 }
580}
581
582#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
584pub struct Message {
585 pub role: Role,
587 pub parts: Vec<Part>,
589}
590
591impl Message {
592 pub fn new(role: Role, parts: Vec<Part>) -> Self {
594 Self { role, parts }
595 }
596
597 pub fn system(text: impl Into<String>) -> Self {
599 Self::text(Role::System, text)
600 }
601
602 pub fn developer(text: impl Into<String>) -> Self {
604 Self::text(Role::Developer, text)
605 }
606
607 pub fn user(text: impl Into<String>) -> Self {
609 Self::text(Role::User, text)
610 }
611
612 pub fn assistant(text: impl Into<String>) -> Self {
614 Self::text(Role::Assistant, text)
615 }
616
617 pub fn custom(role: impl Into<String>, text: impl Into<String>) -> Self {
619 Self::text(Role::Custom(role.into()), text)
620 }
621
622 pub fn text(role: Role, text: impl Into<String>) -> Self {
624 Self {
625 role,
626 parts: vec![Part::text(text)],
627 }
628 }
629
630 pub fn assistant_with_tools(
633 text: impl Into<String>,
634 tool_calls: impl IntoIterator<Item = ToolCall>,
635 ) -> Self {
636 let text = text.into();
637 let mut parts = Vec::new();
638 if !text.is_empty() {
639 parts.push(Part::text(text));
640 }
641 parts.extend(tool_calls.into_iter().map(Part::ToolCall));
642 Self {
643 role: Role::Assistant,
644 parts,
645 }
646 }
647
648 pub fn assistant_reasoning(reasoning: Reasoning) -> Self {
650 Self {
651 role: Role::Assistant,
652 parts: vec![Part::Reasoning(reasoning)],
653 }
654 }
655
656 pub fn tool_result(result: ToolResult) -> Self {
658 Self {
659 role: Role::Tool,
660 parts: vec![Part::ToolResult(result)],
661 }
662 }
663
664 pub fn tool_results(results: impl IntoIterator<Item = ToolResult>) -> Self {
666 Self {
667 role: Role::Tool,
668 parts: results.into_iter().map(Part::ToolResult).collect(),
669 }
670 }
671
672 pub fn text_content(&self) -> String {
675 let mut output = String::new();
676 for part in &self.parts {
677 match part {
678 Part::Text { text, .. } => {
679 if !output.is_empty() {
680 output.push('\n');
681 }
682 output.push_str(text);
683 }
684 Part::ToolResult(result) => {
685 if !output.is_empty() {
686 output.push('\n');
687 }
688 output.push_str(&result.content);
689 }
690 _ => {}
691 }
692 }
693 output
694 }
695
696 pub fn tool_calls(&self) -> impl Iterator<Item = &ToolCall> {
698 self.parts.iter().filter_map(|part| match part {
699 Part::ToolCall(call) => Some(call),
700 _ => None,
701 })
702 }
703
704 pub fn tool_result_parts(&self) -> impl Iterator<Item = &ToolResult> {
707 self.parts.iter().filter_map(|part| match part {
708 Part::ToolResult(result) => Some(result),
709 _ => None,
710 })
711 }
712
713 pub fn reasoning(&self) -> impl Iterator<Item = &Reasoning> {
715 self.parts.iter().filter_map(|part| match part {
716 Part::Reasoning(reasoning) => Some(reasoning),
717 _ => None,
718 })
719 }
720
721 pub fn reasoning_text(&self) -> String {
723 self.reasoning()
724 .filter_map(Reasoning::display_text)
725 .collect::<Vec<_>>()
726 .join("\n")
727 }
728}
729
730#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
732pub struct ToolSpec {
733 pub name: String,
735 pub description: String,
737 pub parameters: serde_json::Value,
739 #[serde(default)]
744 pub strict: bool,
745}
746
747impl ToolSpec {
748 pub fn new(
750 name: impl Into<String>,
751 description: impl Into<String>,
752 parameters: serde_json::Value,
753 ) -> Self {
754 Self {
755 name: name.into(),
756 description: description.into(),
757 parameters,
758 strict: false,
759 }
760 }
761
762 pub fn strict(mut self, strict: bool) -> Self {
764 self.strict = strict;
765 self
766 }
767}
768
769#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
773#[serde(tag = "type", rename_all = "snake_case")]
774pub enum CustomToolFormat {
775 Text,
777 Grammar {
779 syntax: String,
781 definition: String,
783 },
784}
785
786#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
791#[serde(tag = "type", rename_all = "snake_case")]
792pub enum ScopedToolDeclaration {
793 Function { tool: ToolSpec },
795 Custom {
797 name: String,
798 description: String,
799 format: CustomToolFormat,
800 },
801}
802
803impl ScopedToolDeclaration {
804 pub fn name(&self) -> &str {
806 match self {
807 Self::Function { tool } => &tool.name,
808 Self::Custom { name, .. } => name,
809 }
810 }
811}
812
813impl From<ToolSpec> for ScopedToolDeclaration {
814 fn from(tool: ToolSpec) -> Self {
815 Self::Function { tool }
816 }
817}
818
819#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
824#[serde(tag = "type", rename_all = "snake_case")]
825pub enum ToolDeclaration {
826 Tool { tool: ScopedToolDeclaration },
828 Namespace {
830 name: String,
831 description: String,
832 tools: Vec<ScopedToolDeclaration>,
833 },
834}
835
836pub fn validate_tool_declarations(declarations: &[ToolDeclaration]) -> crate::Result<()> {
841 fn insert<'a>(
842 names: &mut std::collections::HashSet<&'a str>,
843 name: &'a str,
844 ) -> crate::Result<()> {
845 if name.is_empty() || !names.insert(name) {
846 return Err(crate::Error::InvalidRequest(format!(
847 "tool_declaration_conflict: empty or duplicate name `{name}`"
848 )));
849 }
850 Ok(())
851 }
852
853 let mut top_level = std::collections::HashSet::new();
854 for declaration in declarations {
855 match declaration {
856 ToolDeclaration::Tool { tool } => insert(&mut top_level, tool.name())?,
857 ToolDeclaration::Namespace { name, tools, .. } => {
858 insert(&mut top_level, name)?;
859 let mut scoped = std::collections::HashSet::new();
860 for tool in tools {
861 insert(&mut scoped, tool.name())?;
862 }
863 }
864 }
865 }
866 Ok(())
867}
868
869#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
871#[serde(rename_all = "snake_case")]
872pub enum NativeToolKind {
873 Function,
874 Custom,
875}
876
877#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
879#[serde(tag = "type", content = "value", rename_all = "snake_case")]
880pub enum NativeToolPayload {
881 JsonArguments(String),
883 RawText(String),
885}
886
887#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
889#[serde(rename_all = "snake_case")]
890pub enum NativeToolCompletion {
891 Receiving,
892 Completed,
893 Invalid,
894 Interrupted,
895}
896
897#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
902pub struct NativeToolCall {
903 pub kind: NativeToolKind,
904 pub namespace: Option<String>,
905 pub name: String,
906 pub call_id: String,
908 pub item_id: Option<String>,
910 pub payload: NativeToolPayload,
911 pub completion: NativeToolCompletion,
912 #[serde(default, skip_serializing_if = "Option::is_none")]
913 pub provider_state: Option<ProviderState>,
914}
915
916impl NativeToolCall {
917 pub fn validate_completed(&self) -> crate::Result<()> {
919 if self.completion != NativeToolCompletion::Completed {
920 return Err(crate::Error::InvalidRequest("tool_call_incomplete".into()));
921 }
922 if self.call_id.is_empty()
923 || self.name.is_empty()
924 || self.namespace.as_deref() == Some("")
925 || self.item_id.as_deref() == Some("")
926 {
927 return Err(crate::Error::InvalidRequest(
928 "tool_identity_conflict".into(),
929 ));
930 }
931 if !matches!(
932 (&self.kind, &self.payload),
933 (
934 NativeToolKind::Function,
935 NativeToolPayload::JsonArguments(_)
936 ) | (NativeToolKind::Custom, NativeToolPayload::RawText(_))
937 ) {
938 return Err(crate::Error::InvalidRequest("tool_payload_conflict".into()));
939 }
940 Ok(())
941 }
942}
943
944#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
946#[serde(rename_all = "snake_case")]
947pub enum ReasoningEffort {
948 None,
950 Minimal,
952 Low,
953 Medium,
954 High,
955 #[serde(rename = "xhigh")]
957 XHigh,
958 Max,
960 Ultra,
963}
964
965#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
967#[serde(rename_all = "snake_case")]
968pub enum ReasoningSummary {
969 Auto,
971 Concise,
973 Detailed,
975}
976
977#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
979#[serde(rename_all = "snake_case")]
980pub enum ReasoningMode {
981 Enabled,
983 Adaptive,
985 Disabled,
987}
988
989#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
994pub struct ReasoningConfig {
995 pub mode: Option<ReasoningMode>,
997 pub effort: Option<ReasoningEffort>,
999 pub budget_tokens: Option<u32>,
1001 pub summary: Option<ReasoningSummary>,
1003 pub include_text: Option<bool>,
1005 pub include_encrypted: Option<bool>,
1007}
1008
1009impl ReasoningConfig {
1010 pub fn new() -> Self {
1012 Self::default()
1013 }
1014
1015 pub fn mode(mut self, mode: ReasoningMode) -> Self {
1017 self.mode = Some(mode);
1018 self
1019 }
1020
1021 pub fn effort(mut self, effort: ReasoningEffort) -> Self {
1023 self.effort = Some(effort);
1024 self
1025 }
1026
1027 pub fn budget_tokens(mut self, budget_tokens: u32) -> Self {
1029 self.budget_tokens = Some(budget_tokens);
1030 self
1031 }
1032
1033 pub fn summary(mut self, summary: ReasoningSummary) -> Self {
1035 self.summary = Some(summary);
1036 self
1037 }
1038
1039 pub fn include_text(mut self, include_text: bool) -> Self {
1041 self.include_text = Some(include_text);
1042 self
1043 }
1044
1045 pub fn include_encrypted(mut self, include_encrypted: bool) -> Self {
1048 self.include_encrypted = Some(include_encrypted);
1049 self
1050 }
1051}
1052
1053#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1055#[serde(rename_all = "snake_case")]
1056pub enum ToolChoice {
1057 Auto,
1059 None,
1061 Required,
1063 Tool(String),
1065}
1066
1067#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1069#[serde(tag = "type", rename_all = "snake_case")]
1070pub enum ResponseFormat {
1071 Text,
1073 JsonObject,
1075 JsonSchema {
1077 name: String,
1079 schema: serde_json::Value,
1081 strict: Option<bool>,
1083 },
1084}
1085
1086#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
1092pub struct ChatRequest {
1093 pub messages: Vec<Message>,
1095 pub tools: Vec<ToolSpec>,
1097 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1100 pub tool_declarations: Vec<ToolDeclaration>,
1101 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1103 pub server_tools: Vec<ServerTool>,
1104 pub tool_choice: Option<ToolChoice>,
1106 pub response_format: Option<ResponseFormat>,
1108 pub stop: Vec<String>,
1110 pub seed: Option<u64>,
1112 pub temperature: Option<f32>,
1114 pub max_output_tokens: Option<u32>,
1116 pub reasoning: Option<ReasoningConfig>,
1118 #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
1121 pub extra_body: serde_json::Map<String, serde_json::Value>,
1122}
1123
1124impl ChatRequest {
1125 pub fn new(messages: impl IntoIterator<Item = Message>) -> Self {
1127 Self {
1128 messages: messages.into_iter().collect(),
1129 ..Self::default()
1130 }
1131 }
1132
1133 pub fn user(text: impl Into<String>) -> Self {
1135 Self::new([Message::user(text)])
1136 }
1137
1138 pub fn push(mut self, message: Message) -> Self {
1140 self.messages.push(message);
1141 self
1142 }
1143
1144 pub fn tools(mut self, tools: impl IntoIterator<Item = ToolSpec>) -> Self {
1146 self.tools = tools.into_iter().collect();
1147 self
1148 }
1149
1150 pub fn tool_declarations(mut self, tools: impl IntoIterator<Item = ToolDeclaration>) -> Self {
1153 self.tool_declarations = tools.into_iter().collect();
1154 self
1155 }
1156
1157 pub fn server_tools(mut self, tools: impl IntoIterator<Item = ServerTool>) -> Self {
1159 self.server_tools = tools.into_iter().collect();
1160 self
1161 }
1162
1163 pub fn tool_choice(mut self, tool_choice: ToolChoice) -> Self {
1165 self.tool_choice = Some(tool_choice);
1166 self
1167 }
1168
1169 pub fn response_format(mut self, response_format: ResponseFormat) -> Self {
1171 self.response_format = Some(response_format);
1172 self
1173 }
1174
1175 pub fn stop<I, S>(mut self, stop: I) -> Self
1177 where
1178 I: IntoIterator<Item = S>,
1179 S: Into<String>,
1180 {
1181 self.stop = stop.into_iter().map(Into::into).collect();
1182 self
1183 }
1184
1185 pub fn seed(mut self, seed: u64) -> Self {
1187 self.seed = Some(seed);
1188 self
1189 }
1190
1191 pub fn temperature(mut self, temperature: f32) -> Self {
1193 self.temperature = Some(temperature);
1194 self
1195 }
1196
1197 pub fn max_output_tokens(mut self, max_output_tokens: u32) -> Self {
1199 self.max_output_tokens = Some(max_output_tokens);
1200 self
1201 }
1202
1203 pub fn reasoning(mut self, reasoning: ReasoningConfig) -> Self {
1205 self.reasoning = Some(reasoning);
1206 self
1207 }
1208
1209 pub fn extra_body(
1214 mut self,
1215 key: impl Into<String>,
1216 value: impl Into<serde_json::Value>,
1217 ) -> Self {
1218 self.extra_body.insert(key.into(), value.into());
1219 self
1220 }
1221}
1222
1223#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
1225pub struct Usage {
1226 pub input_tokens: Option<u64>,
1228 pub output_tokens: Option<u64>,
1230 pub total_tokens: Option<u64>,
1232 pub cache_read_tokens: Option<u64>,
1234 pub cache_write_tokens: Option<u64>,
1236 pub reasoning_tokens: Option<u64>,
1238 pub raw: Option<serde_json::Value>,
1240}
1241
1242impl Usage {
1243 pub(crate) fn without_raw(mut self) -> Self {
1245 self.raw = None;
1246 self
1247 }
1248}
1249
1250#[derive(Debug, Clone, PartialEq)]
1252pub struct ResponseMetadata {
1253 pub status: u16,
1255 pub request_id: Option<String>,
1257 pub headers: HeaderMap,
1259}
1260
1261impl ResponseMetadata {
1262 pub fn header(&self, name: impl AsRef<str>) -> Option<&reqwest::header::HeaderValue> {
1265 reqwest::header::HeaderName::from_bytes(name.as_ref().as_bytes())
1266 .ok()
1267 .and_then(|name| self.headers.get(name))
1268 }
1269
1270 pub fn request_id(&self) -> Option<&str> {
1272 self.request_id.as_deref()
1273 }
1274
1275 pub fn from_response(response: &reqwest::Response) -> Self {
1279 let request_id = ["x-request-id", "request-id", "x-ms-request-id", "cf-ray"]
1280 .into_iter()
1281 .find_map(|name| {
1282 response
1283 .headers()
1284 .get(name)
1285 .and_then(|value| value.to_str().ok())
1286 .map(str::to_string)
1287 });
1288
1289 Self {
1290 status: response.status().as_u16(),
1291 request_id,
1292 headers: response.headers().clone(),
1293 }
1294 }
1295}
1296
1297#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1299pub struct ChatResponse {
1300 pub message: Message,
1302 pub usage: Option<Usage>,
1304 pub raw: serde_json::Value,
1308 #[serde(skip)]
1310 pub metadata: Option<ResponseMetadata>,
1311 #[serde(skip)]
1313 pub report: CompletionReport,
1314}
1315
1316impl ChatResponse {
1317 pub fn new(message: Message) -> Self {
1319 Self {
1320 message,
1321 usage: None,
1322 raw: serde_json::Value::Null,
1323 metadata: None,
1324 report: CompletionReport::default(),
1325 }
1326 }
1327
1328 pub fn text(&self) -> String {
1330 self.message.text_content()
1331 }
1332
1333 pub fn tool_calls(&self) -> impl Iterator<Item = &ToolCall> {
1335 self.message.tool_calls()
1336 }
1337
1338 pub fn reasoning(&self) -> impl Iterator<Item = &Reasoning> {
1340 self.message.reasoning()
1341 }
1342
1343 pub fn reasoning_text(&self) -> String {
1345 self.message.reasoning_text()
1346 }
1347}