Elffuss BraidHub

kikocis/continue public

Merge pull request #6797 from su0as/5763-huggingface-router-openai-compat

feat: add HuggingFace OpenAI-compatible router detection
Changesetb8017d56403e42b04ffa94d411547ed59bdbc6ec797d51cb3240ea6249c4a2bf
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-12 15:18 -06:00
CommitterGitHub <noreply@github.com> · 2025-09-12 15:18 -06:00
Parents370bd05956f8, 99fe51849b68

Browse the files at this changeset

2 files changed

modified packages/openai-adapters/src/index.ts +35 −0
@@ -31,6 +31,33 @@
3131 });
3232 }
3333
34+/**
35+ * Detects if a HuggingFace API URL is using an OpenAI-compatible router
36+ * @param url The URL to check
37+ * @returns true if the URL appears to be using an OpenAI-compatible router
38+ */
39+function isHuggingFaceOpenAICompatible(url: string): boolean {
40+ if (!url) {
41+ return false;
42+ }
43+
44+ // Normalize the URL to lowercase for case-insensitive matching
45+ const normalizedUrl = url.toLowerCase();
46+
47+ // Check for common OpenAI-compatible patterns
48+ const openAIPatterns = [
49+ "/v1/", // Standard OpenAI v1 API pattern
50+ "/openai/", // Explicit OpenAI compatibility path
51+ "/v1/chat/completions", // Specific OpenAI chat completions endpoint
52+ "/v1/completions", // OpenAI completions endpoint
53+ "/v1/embeddings", // OpenAI embeddings endpoint
54+ "/v1/models", // OpenAI models endpoint
55+ ];
56+
57+ // Check if the URL contains any of the OpenAI-compatible patterns
58+ return openAIPatterns.some((pattern) => normalizedUrl.includes(pattern));
59+}
60+
3461 export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
3562 switch (config.provider) {
3663 case "openai":
@@ -117,6 +144,14 @@
117144 return openAICompatible("http://localhost:11434/v1/", config);
118145 case "mock":
119146 return new MockApi();
147+ case "huggingface-inference-api":
148+ // Check if it's an OpenAI-compatible router
149+ if (config.apiBase && isHuggingFaceOpenAICompatible(config.apiBase)) {
150+ return openAICompatible(config.apiBase, config);
151+ }
152+ // Return undefined for native HuggingFace endpoints
153+ // (handled by HuggingFaceInferenceAPI class in core)
154+ return undefined;
120155 default:
121156 return undefined;
122157 }
modified packages/openai-adapters/src/types.ts +1 −0
@@ -58,6 +58,7 @@
5858 z.literal("scaleway"),
5959 z.literal("ncompass"),
6060 z.literal("relace"),
61+ z.literal("huggingface-inference-api"),
6162 ]),
6263 });
6364 export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>;