1use std::fmt;
2use std::sync::Arc;
3
4use reqwest::header::{HeaderMap, HeaderName};
5use serde_json::{Map, Value};
6
7pub use crate::normalize::Protocol as ProtocolSurface;
11use crate::normalize::SystemPlacement;
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15pub struct ProfileId(String);
16
17impl ProfileId {
18 pub fn new(id: impl Into<String>) -> Self {
20 Self(id.into())
21 }
22
23 pub fn as_str(&self) -> &str {
25 &self.0
26 }
27}
28
29impl From<&str> for ProfileId {
30 fn from(value: &str) -> Self {
31 Self::new(value)
32 }
33}
34
35impl From<String> for ProfileId {
36 fn from(value: String) -> Self {
37 Self::new(value)
38 }
39}
40
41impl fmt::Display for ProfileId {
42 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43 formatter.write_str(&self.0)
44 }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
49pub enum AuthStyle {
50 Bearer,
52 XApiKey { header: HeaderName },
54 QueryKey { parameter: String },
56 Custom {
58 header: HeaderName,
59 value_prefix: Option<String>,
60 },
61 None,
63}
64
65#[derive(Debug, Clone, Default)]
67pub enum ProfileSelector {
68 #[default]
70 Generic,
71 Custom(Arc<ProviderProfile>),
73 Builtin(ProfileId),
76}
77
78#[derive(Debug, Clone)]
83pub struct EndpointSpec {
84 pub protocol: ProtocolSurface,
86 pub base_url: String,
88 pub path: String,
90 pub stream_path: Option<String>,
92 pub auth: AuthStyle,
94 pub profile: ProfileSelector,
96}
97
98impl EndpointSpec {
99 pub fn new(
101 protocol: ProtocolSurface,
102 base_url: impl Into<String>,
103 path: impl Into<String>,
104 auth: AuthStyle,
105 ) -> Self {
106 Self {
107 protocol,
108 base_url: base_url.into(),
109 path: path.into(),
110 stream_path: None,
111 auth,
112 profile: ProfileSelector::Generic,
113 }
114 }
115
116 pub fn openai_chat(base_url: impl Into<String>) -> Self {
118 Self::new(
119 ProtocolSurface::OpenAiChat,
120 base_url,
121 "/chat/completions",
122 AuthStyle::Bearer,
123 )
124 }
125
126 pub fn openai_responses(base_url: impl Into<String>) -> Self {
128 Self::new(
129 ProtocolSurface::OpenAiResponses,
130 base_url,
131 "/responses",
132 AuthStyle::Bearer,
133 )
134 }
135
136 pub fn anthropic_messages(base_url: impl Into<String>) -> Self {
138 Self::new(
139 ProtocolSurface::AnthropicMessages,
140 base_url,
141 "/messages",
142 AuthStyle::XApiKey {
143 header: HeaderName::from_static("x-api-key"),
144 },
145 )
146 }
147
148 pub fn gemini_generate_content(base_url: impl Into<String>) -> Self {
151 let mut endpoint = Self::new(
152 ProtocolSurface::GeminiGenerateContent,
153 base_url,
154 "/models/{model}:generateContent",
155 AuthStyle::QueryKey {
156 parameter: "key".to_string(),
157 },
158 );
159 endpoint.stream_path = Some("/models/{model}:streamGenerateContent".to_string());
160 endpoint
161 }
162
163 pub fn stream_path(mut self, path: impl Into<String>) -> Self {
165 self.stream_path = Some(path.into());
166 self
167 }
168
169 pub fn profile(mut self, profile: ProviderProfile) -> Self {
171 self.profile = ProfileSelector::Custom(Arc::new(profile));
172 self
173 }
174
175 pub fn profile_selector(mut self, profile: ProfileSelector) -> Self {
178 self.profile = profile;
179 self
180 }
181
182 pub fn url(&self, model: &str) -> crate::Result<String> {
184 self.url_for(model, false)
185 }
186
187 pub fn url_for(&self, model: &str, streaming: bool) -> crate::Result<String> {
189 let path = if streaming {
190 self.stream_path.as_deref().unwrap_or(&self.path)
191 } else {
192 &self.path
193 };
194 if self.base_url.trim().is_empty() {
195 return Err(crate::Error::InvalidEndpoint(
196 "base_url cannot be empty".to_string(),
197 ));
198 }
199 if path.trim().is_empty() {
200 return Err(crate::Error::InvalidEndpoint(
201 "path cannot be empty".to_string(),
202 ));
203 }
204
205 let base = self.base_url.trim_end_matches('/');
206 let path = path.trim_start_matches('/').replace("{model}", model);
207 Ok(format!("{base}/{path}"))
208 }
209
210 pub fn provider_profile(&self) -> Option<&ProviderProfile> {
212 match &self.profile {
213 ProfileSelector::Custom(profile) => Some(profile),
214 ProfileSelector::Generic | ProfileSelector::Builtin(_) => None,
215 }
216 }
217
218 pub fn resolved_profile(&self) -> Option<ProviderProfile> {
220 match &self.profile {
221 ProfileSelector::Generic => None,
222 ProfileSelector::Custom(profile) => Some(profile.as_ref().clone()),
223 ProfileSelector::Builtin(id) => ProfileRegistry::new().resolve(id),
224 }
225 }
226}
227
228#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
230pub struct NormalizeProfile {
231 pub system_placement: SystemPlacement,
237}
238
239#[derive(Debug, Clone)]
241pub struct ProviderProfile {
242 pub id: ProfileId,
244 pub request: RequestProfile,
246 pub request_options: ProviderRequestOptions,
248 pub normalize: NormalizeProfile,
250 pub reasoning: ReasoningProfile,
252 pub tools: ToolProfile,
254 pub stream: StreamProfile,
256 pub usage: UsageProfile,
258 pub capabilities: Capabilities,
260 pub max_tokens_semantics: MaxTokensSemantics,
262}
263
264impl ProviderProfile {
265 pub fn builtin(id: &str) -> Option<Self> {
270 match id.trim().to_ascii_lowercase().as_str() {
271 "deepseek" => Some(Self::deepseek_compatible()),
272 "qwen" | "bailian" | "dashscope" => Some(Self::qwen_compatible()),
273 "kimi" | "moonshot" => Some(Self::kimi_compatible()),
274 "glm" | "zhipu" | "bigmodel" => Some(Self::glm_compatible()),
275 "doubao" | "ark" | "volcengine" => Some(Self::doubao_compatible()),
276 "openai" => Some(Self::new("openai")),
277 "anthropic" => Some(Self::new("anthropic")),
278 "gemini" | "google" => Some(Self::new("gemini")),
279 "openai-compatible" => Some(Self::new("openai-compatible")),
280 _ => None,
281 }
282 }
283
284 pub fn new(id: impl Into<ProfileId>) -> Self {
286 Self {
287 id: id.into(),
288 request: RequestProfile::default(),
289 request_options: ProviderRequestOptions::default(),
290 normalize: NormalizeProfile::default(),
291 reasoning: ReasoningProfile::default(),
292 tools: ToolProfile::default(),
293 stream: StreamProfile::default(),
294 usage: UsageProfile::default(),
295 capabilities: Capabilities::default(),
296 max_tokens_semantics: MaxTokensSemantics::MaxCompletionTokens,
297 }
298 }
299
300 pub fn deepseek_compatible() -> Self {
305 let mut profile = Self::new("deepseek");
306 profile.reasoning.aliases = ReasoningAliases::empty()
307 .text("reasoning_content")
308 .replay("reasoning_content");
309 profile.reasoning.replay = ReasoningReplayPolicy::SameProvider;
310 profile.reasoning.thinking = ThinkingRequestProfile::Composite(vec![
311 ThinkingRequestProfile::ThinkingObject,
312 ThinkingRequestProfile::MappedEffort {
313 field: "reasoning_effort".to_string(),
314 mapping: EffortMapping::deepseek(),
315 },
316 ]);
317 profile
318 }
319
320 pub fn qwen_compatible() -> Self {
322 let mut profile = Self::new("qwen");
323 profile.reasoning.aliases = ReasoningAliases::empty()
324 .text("reasoning_content")
325 .replay("reasoning_content");
326 profile.reasoning.replay = ReasoningReplayPolicy::SameProvider;
327 profile.reasoning.thinking = ThinkingRequestProfile::Composite(vec![
328 ThinkingRequestProfile::EnabledFlag("enable_thinking".to_string()),
329 ThinkingRequestProfile::BudgetTokens("thinking_budget".to_string()),
330 ]);
331 profile
332 }
333
334 pub fn kimi_compatible() -> Self {
339 let mut profile = Self::new("kimi");
340 profile.reasoning.aliases = ReasoningAliases::empty()
341 .text("reasoning_content")
342 .replay("reasoning_content");
343 profile.reasoning.replay = ReasoningReplayPolicy::SameProvider;
344 profile
345 }
346
347 pub fn glm_compatible() -> Self {
349 let mut profile = Self::new("glm");
350 profile.reasoning.aliases = ReasoningAliases::empty()
351 .text("reasoning_content")
352 .replay("reasoning_content");
353 profile.reasoning.replay = ReasoningReplayPolicy::SameProvider;
354 profile.reasoning.thinking = ThinkingRequestProfile::ThinkingObject;
355 profile
356 }
357
358 pub fn doubao_compatible() -> Self {
363 let mut profile = Self::new("doubao");
364 profile.reasoning.aliases = ReasoningAliases::empty()
365 .text("reasoning_content")
366 .replay("reasoning_content")
367 .encrypted("encrypted_content");
368 profile.reasoning.replay = ReasoningReplayPolicy::RequiredForToolCalls;
369 profile.reasoning.thinking = ThinkingRequestProfile::ThinkingObject;
370 profile
371 }
372}
373
374#[derive(Debug, Clone, Copy, Default)]
379pub struct ProfileRegistry;
380
381impl ProfileRegistry {
382 pub const fn new() -> Self {
384 Self
385 }
386
387 pub fn resolve(&self, id: &ProfileId) -> Option<ProviderProfile> {
389 ProviderProfile::builtin(id.as_str())
390 }
391
392 pub const fn builtin_ids(&self) -> &'static [&'static str] {
394 &[
395 "deepseek",
396 "qwen",
397 "bailian",
398 "dashscope",
399 "kimi",
400 "moonshot",
401 "glm",
402 "zhipu",
403 "bigmodel",
404 "doubao",
405 "ark",
406 "volcengine",
407 "openai",
408 "anthropic",
409 "gemini",
410 "google",
411 "openai-compatible",
412 ]
413 }
414}
415
416#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
424pub enum ToolCallContentMode {
425 #[default]
427 Auto,
428 Null,
430 Omit,
432 Empty,
434}
435
436#[derive(Debug, Clone, Default, PartialEq, Eq)]
441pub struct ProviderRequestOptions {
442 pub tool_call_content: Option<ToolCallContentMode>,
444 pub require_provider_parameters: Option<bool>,
446 pub do_sample: Option<bool>,
448 pub include_stream_usage: Option<bool>,
450 pub prompt_cache_key: Option<String>,
452 pub user: Option<String>,
454}
455
456#[derive(Debug, Clone, Default)]
458pub struct RequestProfile {
459 pub extra_body: Map<String, Value>,
461 pub extra_headers: HeaderMap,
463 pub extra_query: Vec<(String, String)>,
465}
466
467impl RequestProfile {
468 pub fn body(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
470 self.extra_body.insert(key.into(), value.into());
471 self
472 }
473
474 pub fn header(mut self, name: HeaderName, value: reqwest::header::HeaderValue) -> Self {
476 self.extra_headers.insert(name, value);
477 self
478 }
479
480 pub fn query(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
482 self.extra_query.push((name.into(), value.into()));
483 self
484 }
485}
486
487#[derive(Debug, Clone, PartialEq, Eq)]
489pub struct ReasoningAliases {
490 pub response_text: Vec<String>,
492 pub response_summary: Vec<String>,
494 pub request_replay: Vec<String>,
496 pub encrypted: Vec<String>,
498 pub signature: Vec<String>,
500}
501
502impl Default for ReasoningAliases {
503 fn default() -> Self {
504 Self {
505 response_text: vec!["reasoning_content".to_string(), "reasoning".to_string()],
506 response_summary: Vec::new(),
507 request_replay: vec![
508 "reasoning_content".to_string(),
509 "reasoning".to_string(),
510 "reasoning_details".to_string(),
511 ],
512 encrypted: vec!["reasoning_details".to_string()],
513 signature: Vec::new(),
514 }
515 }
516}
517
518impl ReasoningAliases {
519 pub fn empty() -> Self {
521 Self {
522 response_text: Vec::new(),
523 response_summary: Vec::new(),
524 request_replay: Vec::new(),
525 encrypted: Vec::new(),
526 signature: Vec::new(),
527 }
528 }
529
530 pub fn is_empty(&self) -> bool {
532 self.response_text.is_empty()
533 && self.response_summary.is_empty()
534 && self.request_replay.is_empty()
535 && self.encrypted.is_empty()
536 && self.signature.is_empty()
537 }
538
539 pub fn text(mut self, field: impl Into<String>) -> Self {
541 self.response_text.push(field.into());
542 self
543 }
544
545 pub fn summary(mut self, field: impl Into<String>) -> Self {
547 self.response_summary.push(field.into());
548 self
549 }
550
551 pub fn replay(mut self, field: impl Into<String>) -> Self {
553 self.request_replay.push(field.into());
554 self
555 }
556
557 pub fn encrypted(mut self, field: impl Into<String>) -> Self {
559 self.encrypted.push(field.into());
560 self
561 }
562
563 pub fn signature(mut self, field: impl Into<String>) -> Self {
565 self.signature.push(field.into());
566 self
567 }
568}
569
570#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
573pub enum ReasoningReplayPolicy {
574 Never,
576 #[default]
579 SameProvider,
580 PreserveInHistory,
582 RequiredForToolCalls,
585 EncryptedOnly,
587 ModelDefined,
589}
590
591#[derive(Debug, Clone, Default, PartialEq, Eq)]
593pub struct ReasoningProfile {
594 pub aliases: ReasoningAliases,
596 pub replay: ReasoningReplayPolicy,
598 pub thinking: ThinkingRequestProfile,
600}
601
602#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
604pub enum ToolCallIdPolicy {
605 #[default]
607 Preserve,
608 Synthesize,
610 NamePair,
612}
613
614#[derive(Debug, Clone, Copy, PartialEq, Eq)]
616pub struct ToolProfile {
617 pub call_id_policy: ToolCallIdPolicy,
619 pub parallel_tool_calls: bool,
621 pub stream_arguments: bool,
623}
624
625impl Default for ToolProfile {
626 fn default() -> Self {
627 Self {
628 call_id_policy: ToolCallIdPolicy::Preserve,
629 parallel_tool_calls: true,
630 stream_arguments: true,
631 }
632 }
633}
634
635#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
637pub enum StreamTerminal {
638 #[default]
640 DataDone,
641 ResponseCompleted,
643 MessageStop,
645 Eof,
647}
648
649#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
651pub enum UsagePlacement {
652 #[default]
654 LastChunk,
655 EveryChunk,
657 UsageEvent,
659 None,
661}
662
663#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
665pub struct StreamProfile {
666 pub terminal: StreamTerminal,
668 pub usage: UsagePlacement,
670}
671
672#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
674pub struct UsageProfile {
675 pub placement: UsagePlacement,
677}
678
679#[derive(Debug, Clone, PartialEq, Eq)]
681pub struct Capabilities {
682 pub reasoning: ReasoningCapabilities,
684 pub tools: ToolCapabilities,
686 pub streaming: StreamCapabilities,
688 pub tool_choice: bool,
690 pub structured_output: bool,
692 pub seed: bool,
694 pub stop: bool,
696 pub multimodal: bool,
698 pub server_side_state: bool,
700}
701
702impl Default for Capabilities {
703 fn default() -> Self {
704 Self {
705 reasoning: ReasoningCapabilities::default(),
706 tools: ToolCapabilities::default(),
707 streaming: StreamCapabilities::default(),
708 tool_choice: true,
709 structured_output: true,
710 seed: true,
711 stop: true,
712 multimodal: true,
713 server_side_state: false,
714 }
715 }
716}
717
718#[derive(Debug, Clone, Copy, PartialEq, Eq)]
720pub struct ReasoningCapabilities {
721 pub supported: bool,
723 pub text: bool,
725 pub summary: bool,
727 pub encrypted: bool,
729 pub replay: bool,
731}
732
733impl Default for ReasoningCapabilities {
734 fn default() -> Self {
735 Self {
736 supported: true,
737 text: true,
738 summary: true,
739 encrypted: true,
740 replay: true,
741 }
742 }
743}
744
745#[derive(Debug, Clone, Copy, PartialEq, Eq)]
747pub struct ToolCapabilities {
748 pub supported: bool,
750 pub parallel: bool,
752 pub streaming: bool,
754}
755
756impl Default for ToolCapabilities {
757 fn default() -> Self {
758 Self {
759 supported: true,
760 parallel: true,
761 streaming: true,
762 }
763 }
764}
765
766#[derive(Debug, Clone, Copy, PartialEq, Eq)]
768pub struct StreamCapabilities {
769 pub supported: bool,
771 pub usage: bool,
773}
774
775impl Default for StreamCapabilities {
776 fn default() -> Self {
777 Self {
778 supported: true,
779 usage: true,
780 }
781 }
782}
783
784#[derive(Debug, Clone, PartialEq, Eq)]
786pub struct ModelProfile {
787 pub matcher: ModelMatcher,
789 pub thinking: ThinkingRequestProfile,
791 pub reasoning_aliases: ReasoningAliases,
793 pub replay: Option<ReasoningReplayPolicy>,
795 pub max_tokens_semantics: MaxTokensSemantics,
797 pub stream_function_arguments: bool,
799 pub capabilities: Option<Capabilities>,
801}
802
803impl Default for ModelProfile {
804 fn default() -> Self {
805 Self {
806 matcher: ModelMatcher::Any,
807 thinking: ThinkingRequestProfile::None,
808 reasoning_aliases: ReasoningAliases::empty(),
809 replay: None,
810 max_tokens_semantics: MaxTokensSemantics::MaxOutputTokens,
811 stream_function_arguments: true,
812 capabilities: None,
813 }
814 }
815}
816
817impl ModelProfile {
818 pub fn kimi_k3() -> Self {
820 Self {
821 matcher: ModelMatcher::Prefix("kimi-k3".to_string()),
822 thinking: ThinkingRequestProfile::Effort("reasoning_effort".to_string()),
823 reasoning_aliases: ReasoningAliases::empty()
824 .text("reasoning_content")
825 .replay("reasoning_content"),
826 replay: Some(ReasoningReplayPolicy::SameProvider),
827 ..Self::default()
828 }
829 }
830
831 pub fn kimi_k2_6() -> Self {
833 Self {
834 matcher: ModelMatcher::Prefix("kimi-k2.6".to_string()),
835 thinking: ThinkingRequestProfile::Composite(vec![
836 ThinkingRequestProfile::ThinkingObject,
837 ThinkingRequestProfile::Field {
838 path: "thinking.keep".to_string(),
839 value: Value::String("all".to_string()),
840 },
841 ]),
842 reasoning_aliases: ReasoningAliases::empty()
843 .text("reasoning_content")
844 .replay("reasoning_content"),
845 replay: Some(ReasoningReplayPolicy::SameProvider),
846 ..Self::default()
847 }
848 }
849
850 pub fn matches(&self, model: &str) -> bool {
852 self.matcher.matches(model)
853 }
854}
855
856#[derive(Debug, Clone, Default, PartialEq, Eq)]
858pub enum ModelMatcher {
859 #[default]
861 Any,
862 Exact(String),
864 Prefix(String),
866}
867
868impl ModelMatcher {
869 pub fn matches(&self, model: &str) -> bool {
871 match self {
872 Self::Any => true,
873 Self::Exact(expected) => model == expected,
874 Self::Prefix(prefix) => model.starts_with(prefix),
875 }
876 }
877}
878
879#[derive(Debug, Clone, Default, PartialEq, Eq)]
881pub enum ThinkingRequestProfile {
882 #[default]
884 None,
885 EnabledFlag(String),
887 ThinkingObject,
889 Effort(String),
891 MappedEffort {
893 field: String,
895 mapping: EffortMapping,
897 },
898 BudgetTokens(String),
900 Field {
906 path: String,
907 value: Value,
908 },
909 Composite(Vec<ThinkingRequestProfile>),
911}
912
913#[derive(Debug, Clone, Default, PartialEq, Eq)]
915pub struct EffortMapping {
916 pub none: Option<String>,
918 pub minimal: Option<String>,
920 pub low: Option<String>,
922 pub medium: Option<String>,
924 pub high: Option<String>,
926 pub xhigh: Option<String>,
928 pub max: Option<String>,
930 pub ultra: Option<String>,
932}
933
934impl EffortMapping {
935 pub fn identity() -> Self {
937 Self {
938 none: Some("none".to_string()),
939 minimal: Some("minimal".to_string()),
940 low: Some("low".to_string()),
941 medium: Some("medium".to_string()),
942 high: Some("high".to_string()),
943 xhigh: Some("xhigh".to_string()),
944 max: Some("max".to_string()),
945 ultra: Some("ultra".to_string()),
946 }
947 }
948
949 pub fn deepseek() -> Self {
951 Self {
952 none: Some("none".to_string()),
953 minimal: Some("low".to_string()),
954 low: Some("low".to_string()),
955 medium: Some("high".to_string()),
956 high: Some("high".to_string()),
957 xhigh: Some("high".to_string()),
958 max: Some("max".to_string()),
959 ultra: Some("max".to_string()),
962 }
963 }
964
965 pub fn kimi_k3() -> Self {
967 Self {
968 low: Some("low".to_string()),
969 high: Some("high".to_string()),
970 max: Some("max".to_string()),
971 ..Self::default()
972 }
973 }
974
975 pub fn map(&self, effort: crate::types::ReasoningEffort) -> Option<&str> {
978 match effort {
979 crate::types::ReasoningEffort::None => self.none.as_deref(),
980 crate::types::ReasoningEffort::Minimal => self.minimal.as_deref(),
981 crate::types::ReasoningEffort::Low => self.low.as_deref(),
982 crate::types::ReasoningEffort::Medium => self.medium.as_deref(),
983 crate::types::ReasoningEffort::High => self.high.as_deref(),
984 crate::types::ReasoningEffort::XHigh => self.xhigh.as_deref(),
985 crate::types::ReasoningEffort::Max => self.max.as_deref(),
986 crate::types::ReasoningEffort::Ultra => self.ultra.as_deref(),
987 }
988 }
989}
990
991#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
993pub enum MaxTokensSemantics {
994 #[default]
996 MaxOutputTokens,
997 MaxTokens,
999 MaxCompletionTokens,
1001}