| 1 | //! Responses dialect profiles loaded from `assets/responses_dialects.json`. |
| 2 | //! |
| 3 | //! Known limits: selection does not mint a route or build a request. A |
| 4 | //! DeepSeek Responses URL outside the binding's exact list does not take the |
| 5 | //! DeepSeek profile. Custom DeepSeek endpoints are forced to Chat by the route |
| 6 | //! resolver before selection. Replay API scope strings and account identity |
| 7 | //! stay with the caller. All four profiles share one usage policy. |
| 8 | |
| 9 | use serde::Serialize; |
| 10 | |
| 11 | use crate::ProviderKind; |
| 12 | use crate::provider::WireFormat; |
| 13 | |
| 14 | #[cfg(test)] |
| 15 | #[path = "responses_dialect_data.rs"] |
| 16 | mod data; |
| 17 | |
| 18 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] |
| 19 | #[serde(transparent)] |
| 20 | pub struct ResponsesProfileId(&'static str); |
| 21 | |
| 22 | impl ResponsesProfileId { |
| 23 | const fn from_static(id: &'static str) -> Self { |
| 24 | Self(id) |
| 25 | } |
| 26 | |
| 27 | #[must_use] |
| 28 | pub const fn as_str(self) -> &'static str { |
| 29 | self.0 |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 34 | pub struct ResponsesDialectProfile { |
| 35 | pub id: ResponsesProfileId, |
| 36 | pub endpoint: ResponsesEndpointPolicy, |
| 37 | pub headers: ResponsesHeaderPolicy, |
| 38 | pub request: ResponsesRequestPolicy, |
| 39 | pub reasoning: ResponsesReasoningPolicy, |
| 40 | pub replay: ResponsesReplayPolicy, |
| 41 | pub tools: ResponsesToolPolicy, |
| 42 | pub stream: ResponsesStreamPolicy, |
| 43 | pub usage: ResponsesUsagePolicy, |
| 44 | pub capabilities: ResponsesCapabilityPolicy, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 48 | pub struct ResponsesEndpointPolicy { |
| 49 | pub path: ResponsesPathPolicy, |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 53 | pub enum ResponsesPathPolicy { |
| 54 | VersionedResponses, |
| 55 | UnversionedResponses, |
| 56 | } |
| 57 | |
| 58 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 59 | pub struct ResponsesHeaderPolicy { |
| 60 | pub protocol_headers: ProtocolHeaderPolicy, |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 64 | pub enum ProtocolHeaderPolicy { |
| 65 | StandardJson, |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 69 | pub struct ResponsesRequestPolicy { |
| 70 | pub store: StorePolicy, |
| 71 | pub include: IncludePolicy, |
| 72 | pub instructions: InstructionsPlacement, |
| 73 | pub max_output_tokens: OutputTokenFieldPolicy, |
| 74 | pub sampling: SamplingPolicy, |
| 75 | } |
| 76 | |
| 77 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 78 | pub enum StorePolicy { |
| 79 | ExplicitFalse, |
| 80 | Omit, |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 84 | pub enum IncludePolicy { |
| 85 | EncryptedReasoning, |
| 86 | Omit, |
| 87 | } |
| 88 | |
| 89 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 90 | pub enum InstructionsPlacement { |
| 91 | TopLevel, |
| 92 | LeadingSystemInput, |
| 93 | } |
| 94 | |
| 95 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 96 | pub enum OutputTokenFieldPolicy { |
| 97 | WhenPositive, |
| 98 | Omit, |
| 99 | } |
| 100 | |
| 101 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 102 | pub enum SamplingPolicy { |
| 103 | TemperatureAndTopP, |
| 104 | Omit, |
| 105 | } |
| 106 | |
| 107 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 108 | pub struct ResponsesReasoningPolicy { |
| 109 | pub effort_mapping: ReasoningEffortMapping, |
| 110 | pub shape: ReasoningRequestShape, |
| 111 | pub summary: ReasoningSummaryPolicy, |
| 112 | } |
| 113 | |
| 114 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 115 | pub enum ReasoningEffortMapping { |
| 116 | OpenaiCompatible, |
| 117 | Codex, |
| 118 | Deepseek, |
| 119 | } |
| 120 | |
| 121 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 122 | pub enum ReasoningRequestShape { |
| 123 | EffortWithSummary, |
| 124 | EffortOnly, |
| 125 | } |
| 126 | |
| 127 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 128 | pub enum ReasoningSummaryPolicy { |
| 129 | Auto, |
| 130 | Omit, |
| 131 | } |
| 132 | |
| 133 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 134 | pub struct ResponsesReplayPolicy { |
| 135 | pub encrypted_state: EncryptedReasoningReplayPolicy, |
| 136 | pub plain_reasoning: PlainReasoningReplayPolicy, |
| 137 | pub system_role: SystemRoleReplayPolicy, |
| 138 | } |
| 139 | |
| 140 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 141 | pub enum EncryptedReasoningReplayPolicy { |
| 142 | ProviderApiModelScoped, |
| 143 | AccountApiModelScoped, |
| 144 | Disabled, |
| 145 | } |
| 146 | |
| 147 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 148 | pub enum PlainReasoningReplayPolicy { |
| 149 | Ignore, |
| 150 | PlainText, |
| 151 | PlainTextWithToolPlaceholder, |
| 152 | } |
| 153 | |
| 154 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 155 | pub enum SystemRoleReplayPolicy { |
| 156 | Preserve, |
| 157 | SystemToDeveloper, |
| 158 | } |
| 159 | |
| 160 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 161 | pub struct ResponsesToolPolicy { |
| 162 | pub envelope: ToolEnvelopePolicy, |
| 163 | pub namespace: Option<&'static str>, |
| 164 | pub parallel_calls: ParallelToolCallPolicy, |
| 165 | } |
| 166 | |
| 167 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 168 | pub enum ToolEnvelopePolicy { |
| 169 | FunctionArray, |
| 170 | NamespaceWrapped, |
| 171 | } |
| 172 | |
| 173 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 174 | pub enum ParallelToolCallPolicy { |
| 175 | Allow, |
| 176 | Disallow, |
| 177 | } |
| 178 | |
| 179 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 180 | pub struct ResponsesStreamPolicy { |
| 181 | pub malformed_json: MalformedEventPolicy, |
| 182 | pub unknown_event: UnknownEventPolicy, |
| 183 | pub done_marker: DoneMarkerPolicy, |
| 184 | pub completion: CompletionPolicy, |
| 185 | pub incomplete: IncompleteResponsePolicy, |
| 186 | } |
| 187 | |
| 188 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 189 | pub enum MalformedEventPolicy { |
| 190 | Fail, |
| 191 | SkipWithDiagnostic, |
| 192 | } |
| 193 | |
| 194 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 195 | pub enum UnknownEventPolicy { |
| 196 | Ignore, |
| 197 | Fail, |
| 198 | } |
| 199 | |
| 200 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 201 | pub enum DoneMarkerPolicy { |
| 202 | Terminal, |
| 203 | Ignore, |
| 204 | } |
| 205 | |
| 206 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 207 | pub enum CompletionPolicy { |
| 208 | AcceptDocumentedTerminalEvents, |
| 209 | RequireSuccessfulResponseCompleted, |
| 210 | } |
| 211 | |
| 212 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 213 | pub enum IncompleteResponsePolicy { |
| 214 | PreserveProviderIncompleteResult, |
| 215 | Fail, |
| 216 | } |
| 217 | |
| 218 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 219 | pub struct ResponsesUsagePolicy { |
| 220 | pub cache_fields: CacheUsageFieldPolicy, |
| 221 | pub reasoning_fields: ReasoningUsageFieldPolicy, |
| 222 | pub audio_fields: AudioUsageFieldPolicy, |
| 223 | } |
| 224 | |
| 225 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 226 | pub enum CacheUsageFieldPolicy { |
| 227 | TopLevelThenNestedFallback, |
| 228 | } |
| 229 | |
| 230 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 231 | pub enum ReasoningUsageFieldPolicy { |
| 232 | OutputTokensDetails, |
| 233 | } |
| 234 | |
| 235 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 236 | pub enum AudioUsageFieldPolicy { |
| 237 | NestedDetails, |
| 238 | } |
| 239 | |
| 240 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 241 | pub struct ResponsesCapabilityPolicy { |
| 242 | pub function_tools: FunctionToolCapability, |
| 243 | pub server_side_tools: ServerSideToolCapability, |
| 244 | } |
| 245 | |
| 246 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 247 | pub enum FunctionToolCapability { |
| 248 | Supported, |
| 249 | Unsupported, |
| 250 | } |
| 251 | |
| 252 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 253 | pub enum ServerSideToolCapability { |
| 254 | RouteAndAdapterGated, |
| 255 | Unsupported, |
| 256 | } |
| 257 | |
| 258 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 259 | pub struct ResponsesProfileBinding { |
| 260 | pub provider_kind: ProviderKind, |
| 261 | pub endpoint_key: Option<&'static str>, |
| 262 | pub base_urls: Option<&'static [&'static str]>, |
| 263 | pub profile_id: ResponsesProfileId, |
| 264 | } |
| 265 | |
| 266 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 267 | pub struct ResponsesProfileQuery<'a> { |
| 268 | pub protocol: WireFormat, |
| 269 | pub provider: ProviderKind, |
| 270 | pub endpoint_key: &'a str, |
| 271 | pub base_url: &'a str, |
| 272 | pub explicit_profile: Option<&'a str>, |
| 273 | } |
| 274 | |
| 275 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 276 | pub enum ResponsesProfileError { |
| 277 | NotResponses { profile: String }, |
| 278 | UnknownProfile { profile: String, allowed: String }, |
| 279 | ProfileNotAllowed { profile: String, allowed: String }, |
| 280 | BuiltinOverride { provider: ProviderKind }, |
| 281 | AmbiguousBinding { provider: ProviderKind }, |
| 282 | } |
| 283 | |
| 284 | impl std::fmt::Display for ResponsesProfileError { |
| 285 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 286 | match self { |
| 287 | Self::NotResponses { profile } => write!( |
| 288 | formatter, |
| 289 | "responses profile `{profile}` cannot be set when the wire is not responses" |
| 290 | ), |
| 291 | Self::UnknownProfile { profile, allowed } => write!( |
| 292 | formatter, |
| 293 | "unknown responses profile `{profile}`; allowed: {allowed}" |
| 294 | ), |
| 295 | Self::ProfileNotAllowed { profile, allowed } => write!( |
| 296 | formatter, |
| 297 | "responses profile `{profile}` is not allowed for a custom provider; allowed: {allowed}" |
| 298 | ), |
| 299 | Self::BuiltinOverride { provider } => write!( |
| 300 | formatter, |
| 301 | "built-in provider {} cannot set a responses profile", |
| 302 | provider.as_str() |
| 303 | ), |
| 304 | Self::AmbiguousBinding { provider } => write!( |
| 305 | formatter, |
| 306 | "responses profile bindings overlap for {}", |
| 307 | provider.as_str() |
| 308 | ), |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | impl std::error::Error for ResponsesProfileError {} |
| 314 | |
| 315 | include!(concat!(env!("OUT_DIR"), "/responses_dialects.rs")); |
| 316 | |
| 317 | #[must_use] |
| 318 | pub fn builtin_profiles() -> &'static [ResponsesDialectProfile] { |
| 319 | BUILTIN_PROFILES |
| 320 | } |
| 321 | |
| 322 | pub fn profile(id: &str) -> Option<&'static ResponsesDialectProfile> { |
| 323 | find_profile(id) |
| 324 | } |
| 325 | |
| 326 | /// Path policy for a base URL that matches an exact binding. Unmatched URLs |
| 327 | /// stay on the versioned Responses path. Matching strips a trailing slash and |
| 328 | /// lowercases; it does not trim surrounding whitespace. |
| 329 | pub fn bound_responses_path(provider: ProviderKind, base_url: &str) -> ResponsesPathPolicy { |
| 330 | let normalized = base_url.trim_end_matches('/').to_ascii_lowercase(); |
| 331 | for binding in BUILTIN_BINDINGS { |
| 332 | if binding.provider_kind != provider { |
| 333 | continue; |
| 334 | } |
| 335 | let Some(urls) = binding.base_urls else { |
| 336 | continue; |
| 337 | }; |
| 338 | if !urls.iter().any(|url| *url == normalized) { |
| 339 | continue; |
| 340 | } |
| 341 | if let Some(profile) = find_profile(binding.profile_id.as_str()) { |
| 342 | return profile.endpoint.path; |
| 343 | } |
| 344 | } |
| 345 | ResponsesPathPolicy::VersionedResponses |
| 346 | } |
| 347 | |
| 348 | pub fn accept_custom_responses_profile( |
| 349 | profile: &str, |
| 350 | ) -> Result<&'static ResponsesDialectProfile, ResponsesProfileError> { |
| 351 | let Some(found) = find_profile(profile) else { |
| 352 | return Err(ResponsesProfileError::UnknownProfile { |
| 353 | profile: profile.to_string(), |
| 354 | allowed: allowed_list(), |
| 355 | }); |
| 356 | }; |
| 357 | if !CUSTOM_ALLOWED_PROFILES.contains(&found.id) { |
| 358 | return Err(ResponsesProfileError::ProfileNotAllowed { |
| 359 | profile: profile.to_string(), |
| 360 | allowed: allowed_list(), |
| 361 | }); |
| 362 | } |
| 363 | Ok(found) |
| 364 | } |
| 365 | |
| 366 | pub fn select_responses_profile( |
| 367 | query: ResponsesProfileQuery<'_>, |
| 368 | ) -> Result<Option<&'static ResponsesDialectProfile>, ResponsesProfileError> { |
| 369 | if query.protocol != WireFormat::Responses { |
| 370 | return match query.explicit_profile { |
| 371 | Some(profile) => Err(ResponsesProfileError::NotResponses { |
| 372 | profile: profile.to_string(), |
| 373 | }), |
| 374 | None => Ok(None), |
| 375 | }; |
| 376 | } |
| 377 | if let Some(explicit) = query.explicit_profile { |
| 378 | if query.provider != ProviderKind::Custom { |
| 379 | return Err(ResponsesProfileError::BuiltinOverride { |
| 380 | provider: query.provider, |
| 381 | }); |
| 382 | } |
| 383 | let Some(profile) = find_profile(explicit) else { |
| 384 | return Err(ResponsesProfileError::UnknownProfile { |
| 385 | profile: explicit.to_string(), |
| 386 | allowed: allowed_list(), |
| 387 | }); |
| 388 | }; |
| 389 | if !CUSTOM_ALLOWED_PROFILES.contains(&profile.id) { |
| 390 | return Err(ResponsesProfileError::ProfileNotAllowed { |
| 391 | profile: explicit.to_string(), |
| 392 | allowed: allowed_list(), |
| 393 | }); |
| 394 | } |
| 395 | return Ok(Some(profile)); |
| 396 | } |
| 397 | let normalized = normalize_base_url(query.base_url); |
| 398 | let mut best: Option<&ResponsesProfileBinding> = None; |
| 399 | for binding in BUILTIN_BINDINGS { |
| 400 | if !binding_matches(binding, query.provider, query.endpoint_key, &normalized) { |
| 401 | continue; |
| 402 | } |
| 403 | match best { |
| 404 | None => best = Some(binding), |
| 405 | Some(current) if specificity(binding) > specificity(current) => best = Some(binding), |
| 406 | Some(current) if specificity(binding) == specificity(current) => { |
| 407 | return Err(ResponsesProfileError::AmbiguousBinding { |
| 408 | provider: query.provider, |
| 409 | }); |
| 410 | } |
| 411 | Some(_) => {} |
| 412 | } |
| 413 | } |
| 414 | let id = best |
| 415 | .map(|binding| binding.profile_id) |
| 416 | .unwrap_or(DEFAULT_PROFILE_ID); |
| 417 | Ok(Some( |
| 418 | find_profile(id.as_str()).expect("responses profile binding"), |
| 419 | )) |
| 420 | } |
| 421 | |
| 422 | fn find_profile(id: &str) -> Option<&'static ResponsesDialectProfile> { |
| 423 | BUILTIN_PROFILES |
| 424 | .iter() |
| 425 | .find(|profile| profile.id.as_str() == id) |
| 426 | } |
| 427 | |
| 428 | fn allowed_list() -> String { |
| 429 | CUSTOM_ALLOWED_PROFILES |
| 430 | .iter() |
| 431 | .map(|id| id.as_str()) |
| 432 | .collect::<Vec<_>>() |
| 433 | .join(", ") |
| 434 | } |
| 435 | |
| 436 | fn binding_matches( |
| 437 | binding: &ResponsesProfileBinding, |
| 438 | provider: ProviderKind, |
| 439 | endpoint_key: &str, |
| 440 | base_url: &str, |
| 441 | ) -> bool { |
| 442 | if binding.provider_kind != provider { |
| 443 | return false; |
| 444 | } |
| 445 | if let Some(expected) = binding.endpoint_key |
| 446 | && expected != endpoint_key |
| 447 | { |
| 448 | return false; |
| 449 | } |
| 450 | if let Some(urls) = binding.base_urls |
| 451 | && !urls.contains(&base_url) |
| 452 | { |
| 453 | return false; |
| 454 | } |
| 455 | true |
| 456 | } |
| 457 | |
| 458 | fn specificity(binding: &ResponsesProfileBinding) -> u8 { |
| 459 | u8::from(binding.endpoint_key.is_some()) + u8::from(binding.base_urls.is_some()) |
| 460 | } |
| 461 | |
| 462 | fn normalize_base_url(url: &str) -> String { |
| 463 | url.trim_end_matches('/').to_ascii_lowercase() |
| 464 | } |
| 465 | |
| 466 | #[cfg(test)] |
| 467 | mod tests { |
| 468 | use super::*; |
| 469 | |
| 470 | #[test] |
| 471 | fn embedded_asset_matches_the_generated_table() { |
| 472 | let parsed = data::parse(include_str!("../assets/responses_dialects.json")) |
| 473 | .expect("responses dialect asset"); |
| 474 | let generated = data::codegen(&parsed); |
| 475 | assert_eq!(parsed.profiles.len(), BUILTIN_PROFILES.len()); |
| 476 | assert_eq!(parsed.bindings.len(), BUILTIN_BINDINGS.len()); |
| 477 | for profile in &parsed.profiles { |
| 478 | assert!( |
| 479 | BUILTIN_PROFILES |
| 480 | .iter() |
| 481 | .any(|item| item.id.as_str() == profile.id), |
| 482 | "missing generated profile {}", |
| 483 | profile.id |
| 484 | ); |
| 485 | assert!(generated.contains(&profile.id)); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | const DEEPSEEK_URLS: &[&str] = &[ |
| 490 | "https://api.deepseek.com", |
| 491 | "https://api.deepseek.com/v1", |
| 492 | "https://api.deepseek.com/beta", |
| 493 | "https://api.deepseeki.com", |
| 494 | "https://api.deepseeki.com/v1", |
| 495 | "https://api.deepseeki.com/beta", |
| 496 | ]; |
| 497 | |
| 498 | fn query<'a>( |
| 499 | protocol: WireFormat, |
| 500 | provider: ProviderKind, |
| 501 | endpoint_key: &'a str, |
| 502 | base_url: &'a str, |
| 503 | explicit_profile: Option<&'a str>, |
| 504 | ) -> ResponsesProfileQuery<'a> { |
| 505 | ResponsesProfileQuery { |
| 506 | protocol, |
| 507 | provider, |
| 508 | endpoint_key, |
| 509 | base_url, |
| 510 | explicit_profile, |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | fn selected(query: ResponsesProfileQuery<'_>) -> &'static str { |
| 515 | select_responses_profile(query) |
| 516 | .expect("profile") |
| 517 | .expect("responses profile") |
| 518 | .id |
| 519 | .as_str() |
| 520 | } |
| 521 | |
| 522 | fn asset() -> serde_json::Value { |
| 523 | serde_json::from_str(include_str!("../assets/responses_dialects.json")).expect("asset") |
| 524 | } |
| 525 | |
| 526 | #[test] |
| 527 | fn builtin_table_has_four_profiles_and_one_usage_policy() { |
| 528 | let profiles = builtin_profiles(); |
| 529 | assert_eq!(profiles.len(), 4); |
| 530 | let usage = profiles[0].usage; |
| 531 | assert!(profiles.iter().all(|profile| profile.usage == usage)); |
| 532 | assert_eq!( |
| 533 | profiles |
| 534 | .iter() |
| 535 | .find(|profile| profile.id.as_str() == "codex") |
| 536 | .expect("codex") |
| 537 | .tools |
| 538 | .namespace, |
| 539 | Some("codewhale") |
| 540 | ); |
| 541 | } |
| 542 | |
| 543 | #[test] |
| 544 | fn deepseek_binding_is_the_responses_url_allowlist() { |
| 545 | let binding = BUILTIN_BINDINGS |
| 546 | .iter() |
| 547 | .find(|binding| binding.provider_kind == ProviderKind::Deepseek) |
| 548 | .expect("deepseek binding"); |
| 549 | assert_eq!(binding.endpoint_key, Some("responses")); |
| 550 | assert_eq!(binding.base_urls, Some(DEEPSEEK_URLS)); |
| 551 | assert!( |
| 552 | !binding |
| 553 | .base_urls |
| 554 | .expect("urls") |
| 555 | .contains(&"https://api.deepseek.com/anthropic") |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn selection_follows_protocol_then_binding() { |
| 561 | assert!( |
| 562 | select_responses_profile(query( |
| 563 | WireFormat::ChatCompletions, |
| 564 | ProviderKind::OpenaiCodex, |
| 565 | "responses", |
| 566 | "https://api.openai.com/v1", |
| 567 | None, |
| 568 | )) |
| 569 | .expect("chat") |
| 570 | .is_none() |
| 571 | ); |
| 572 | assert_eq!( |
| 573 | selected(query( |
| 574 | WireFormat::Responses, |
| 575 | ProviderKind::Openai, |
| 576 | "responses", |
| 577 | "https://api.openai.com/v1", |
| 578 | None, |
| 579 | )), |
| 580 | "standard" |
| 581 | ); |
| 582 | assert_eq!( |
| 583 | selected(query( |
| 584 | WireFormat::Responses, |
| 585 | ProviderKind::OpenaiCodex, |
| 586 | "chat", |
| 587 | "https://api.openai.com/v1", |
| 588 | None, |
| 589 | )), |
| 590 | "codex" |
| 591 | ); |
| 592 | assert_eq!( |
| 593 | selected(query( |
| 594 | WireFormat::Responses, |
| 595 | ProviderKind::Concentrate, |
| 596 | "models", |
| 597 | "https://api.concentrate.ai/v1", |
| 598 | None, |
| 599 | )), |
| 600 | "concentrate" |
| 601 | ); |
| 602 | for url in DEEPSEEK_URLS { |
| 603 | assert_eq!( |
| 604 | selected(query( |
| 605 | WireFormat::Responses, |
| 606 | ProviderKind::Deepseek, |
| 607 | "responses", |
| 608 | url, |
| 609 | None, |
| 610 | )), |
| 611 | "deepseek_stateless_plain_reasoning" |
| 612 | ); |
| 613 | } |
| 614 | assert_eq!( |
| 615 | selected(query( |
| 616 | WireFormat::Responses, |
| 617 | ProviderKind::Deepseek, |
| 618 | "responses", |
| 619 | "https://API.DEEPSEEK.COM/v1/", |
| 620 | None, |
| 621 | )), |
| 622 | "deepseek_stateless_plain_reasoning" |
| 623 | ); |
| 624 | assert_eq!( |
| 625 | selected(query( |
| 626 | WireFormat::Responses, |
| 627 | ProviderKind::Deepseek, |
| 628 | "responses", |
| 629 | "https://api.deepseek.com/anthropic", |
| 630 | None, |
| 631 | )), |
| 632 | "standard" |
| 633 | ); |
| 634 | assert_eq!( |
| 635 | selected(query( |
| 636 | WireFormat::Responses, |
| 637 | ProviderKind::Deepseek, |
| 638 | "Responses", |
| 639 | "https://api.deepseek.com", |
| 640 | None, |
| 641 | )), |
| 642 | "standard" |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn custom_override_is_allowlisted_and_built_ins_cannot_set_one() { |
| 648 | assert_eq!( |
| 649 | selected(query( |
| 650 | WireFormat::Responses, |
| 651 | ProviderKind::Custom, |
| 652 | "responses", |
| 653 | "https://gateway.example/v1", |
| 654 | None, |
| 655 | )), |
| 656 | "standard" |
| 657 | ); |
| 658 | assert_eq!( |
| 659 | selected(query( |
| 660 | WireFormat::Responses, |
| 661 | ProviderKind::Custom, |
| 662 | "responses", |
| 663 | "https://gateway.example/v1", |
| 664 | Some("standard"), |
| 665 | )), |
| 666 | "standard" |
| 667 | ); |
| 668 | let denied = select_responses_profile(query( |
| 669 | WireFormat::Responses, |
| 670 | ProviderKind::Custom, |
| 671 | "responses", |
| 672 | "https://gateway.example/v1", |
| 673 | Some("codex"), |
| 674 | )) |
| 675 | .expect_err("codex"); |
| 676 | assert!(denied.to_string().contains("allowed: standard"), "{denied}"); |
| 677 | let unknown = select_responses_profile(query( |
| 678 | WireFormat::Responses, |
| 679 | ProviderKind::Custom, |
| 680 | "responses", |
| 681 | "https://gateway.example/v1", |
| 682 | Some("missing"), |
| 683 | )) |
| 684 | .expect_err("missing"); |
| 685 | assert!(unknown.to_string().contains("unknown responses profile")); |
| 686 | assert!( |
| 687 | select_responses_profile(query( |
| 688 | WireFormat::ChatCompletions, |
| 689 | ProviderKind::Custom, |
| 690 | "chat", |
| 691 | "https://gateway.example/v1", |
| 692 | Some("standard"), |
| 693 | )) |
| 694 | .expect_err("wire") |
| 695 | .to_string() |
| 696 | .contains("not responses") |
| 697 | ); |
| 698 | assert!( |
| 699 | select_responses_profile(query( |
| 700 | WireFormat::Responses, |
| 701 | ProviderKind::Openai, |
| 702 | "responses", |
| 703 | "https://api.openai.com/v1", |
| 704 | Some("standard"), |
| 705 | )) |
| 706 | .expect_err("built-in") |
| 707 | .to_string() |
| 708 | .contains("openai") |
| 709 | ); |
| 710 | } |
| 711 | |
| 712 | #[test] |
| 713 | fn parser_rejects_unknown_fields_conflicts_and_unsafe_custom_profiles() { |
| 714 | let mut unknown = asset(); |
| 715 | unknown["profiles"][0]["request"]["store"] = serde_json::json!("false"); |
| 716 | assert!(super::data::parse(&unknown.to_string()).is_err()); |
| 717 | |
| 718 | let mut extra = asset(); |
| 719 | extra["profiles"][0]["request"]["patch"] = serde_json::json!("x"); |
| 720 | assert!(super::data::parse(&extra.to_string()).is_err()); |
| 721 | |
| 722 | let mut duplicate = asset(); |
| 723 | duplicate["profiles"][1]["id"] = duplicate["profiles"][0]["id"].clone(); |
| 724 | assert!(super::data::parse(&duplicate.to_string()).is_err()); |
| 725 | |
| 726 | let mut overlap = asset(); |
| 727 | let binding = overlap["bindings"][0].clone(); |
| 728 | overlap["bindings"] |
| 729 | .as_array_mut() |
| 730 | .expect("bindings") |
| 731 | .push(binding); |
| 732 | assert!(super::data::parse(&overlap.to_string()).is_err()); |
| 733 | |
| 734 | let mut custom = asset(); |
| 735 | custom["custom_allowed_profiles"] = serde_json::json!(["standard", "codex"]); |
| 736 | assert!(super::data::parse(&custom.to_string()).is_err()); |
| 737 | |
| 738 | let mut secret = asset(); |
| 739 | secret["profiles"][0]["request"]["api_key"] = serde_json::json!("sk-test"); |
| 740 | assert!(super::data::parse(&secret.to_string()).is_err()); |
| 741 | |
| 742 | let mut urls = asset(); |
| 743 | let deepseek = urls["bindings"] |
| 744 | .as_array_mut() |
| 745 | .expect("bindings") |
| 746 | .iter_mut() |
| 747 | .find(|binding| binding["provider_kind"] == "Deepseek") |
| 748 | .expect("deepseek"); |
| 749 | deepseek["base_url_match"]["urls"] |
| 750 | .as_array_mut() |
| 751 | .expect("urls") |
| 752 | .push(serde_json::json!("https://api.deepseek.com/")); |
| 753 | assert!(super::data::parse(&urls.to_string()).is_err()); |
| 754 | } |
| 755 | } |
| 756 |