| 1 | package openai |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "reasonix/internal/provider" |
| 7 | ) |
| 8 | |
| 9 | // canonicalKnownVendorChatURL rewrites official-vendor bases whose documented |
| 10 | // form differs from the OpenAI-compatible shape (Token Rhythm, StepFun step_plan). |
| 11 | func canonicalKnownVendorChatURL(raw string) (string, bool) { |
| 12 | if canonical, ok := canonicalTokenRhythmChatURL(raw); ok { |
| 13 | return canonical, true |
| 14 | } |
| 15 | return canonicalStepFunPlanChatURL(raw) |
| 16 | } |
| 17 | |
| 18 | func resolveOpenAIChatURL(baseURL string, extra map[string]any) string { |
| 19 | requestURL, _ := extra["request_url"].(string) |
| 20 | requestURL = strings.TrimSpace(requestURL) |
| 21 | if canonical, ok := canonicalKnownVendorChatURL(requestURL); ok { |
| 22 | return canonical |
| 23 | } |
| 24 | if requestURL != "" && !provider.EndpointOverrideRepeatsBase(requestURL, baseURL) { |
| 25 | return requestURL |
| 26 | } |
| 27 | legacyChatURL, _ := extra["chat_url"].(string) |
| 28 | return normalizeChatURL(baseURL, legacyChatURL) |
| 29 | } |
| 30 | |
| 31 | func normalizeChatURL(baseURL, chatURL string) string { |
| 32 | legacy := strings.TrimRight(strings.TrimSpace(chatURL), "/") |
| 33 | if legacy != "" && !provider.EndpointOverrideRepeatsBase(legacy, baseURL) { |
| 34 | if canonical, ok := canonicalKnownVendorChatURL(legacy); ok { |
| 35 | return canonical |
| 36 | } |
| 37 | return legacy |
| 38 | } |
| 39 | if canonical, ok := canonicalKnownVendorChatURL(baseURL); ok { |
| 40 | return canonical |
| 41 | } |
| 42 | return strings.TrimRight(strings.TrimSpace(baseURL), "/") + "/chat/completions" |
| 43 | } |
| 44 |