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