Skip to content

Commit 29b1664

Browse files
authored
fix(huggingface): honor available provider capabilities (#129797)
* fix(huggingface): honor available provider capabilities * fix(huggingface): preserve canonical model definition typing * fix(huggingface): bound context to every available route * fix(huggingface): require tool support across eligible routes
1 parent d2360e4 commit 29b1664

2 files changed

Lines changed: 196 additions & 25 deletions

File tree

‎extensions/huggingface/models.test.ts‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,179 @@ describe("huggingface models", () => {
3737
const models = await discoverHuggingfaceModels("");
3838
expect(models).toHaveLength(HUGGINGFACE_MODEL_CATALOG.length);
3939
expect(models.map((m) => m.id)).toEqual(HUGGINGFACE_MODEL_CATALOG.map((m) => m.id));
40+
expect(models[0]?.contextWindow).toBe(131072);
41+
expect(models[0]?.compat).toBeUndefined();
42+
});
43+
44+
it("uses the live route context for bundled models while preserving their catalog metadata", async () => {
45+
const bundledModel = HUGGINGFACE_MODEL_CATALOG[0]!;
46+
vi.stubGlobal(
47+
"fetch",
48+
vi.fn(async () =>
49+
Response.json({
50+
data: [
51+
{
52+
id: bundledModel.id,
53+
name: "Upstream name must not replace bundled metadata",
54+
architecture: { input_modalities: ["text", "image"] },
55+
providers: [{ context_length: 64000, supports_tools: true }],
56+
},
57+
],
58+
}),
59+
),
60+
);
61+
62+
const models = await discoverHuggingfaceModels("hf_test_token");
63+
64+
expect(models).toEqual([{ ...bundledModel, contextWindow: 64000 }]);
65+
});
66+
67+
it("limits discovered models to every available route regardless of provider order", async () => {
68+
const bundledModel = HUGGINGFACE_MODEL_CATALOG[0]!;
69+
vi.stubGlobal(
70+
"fetch",
71+
vi.fn(async () =>
72+
Response.json({
73+
data: [
74+
{
75+
id: "Qwen/Qwen3.8-2.4T-A95B",
76+
providers: [{ context_length: 1010000 }, { context_length: 262144 }],
77+
},
78+
{
79+
id: "test/reversed-provider-order",
80+
providers: [{ context_length: 262144 }, { context_length: 1010000 }],
81+
},
82+
{
83+
id: bundledModel.id,
84+
providers: [
85+
{ status: "error", context_length: 16000 },
86+
{ context_length: 96000 },
87+
{ context_length: 0 },
88+
{ context_length: 64000 },
89+
],
90+
},
91+
],
92+
}),
93+
),
94+
);
95+
96+
const models = await discoverHuggingfaceModels("hf_test_token");
97+
98+
expect(models.map(({ id, contextWindow }) => ({ id, contextWindow }))).toEqual([
99+
{ id: "Qwen/Qwen3.8-2.4T-A95B", contextWindow: 262144 },
100+
{ id: "test/reversed-provider-order", contextWindow: 262144 },
101+
{ id: bundledModel.id, contextWindow: 64000 },
102+
]);
103+
});
104+
105+
it("disables tools whenever an available route explicitly rejects them", async () => {
106+
vi.stubGlobal(
107+
"fetch",
108+
vi.fn(async () =>
109+
Response.json({
110+
data: [
111+
{
112+
id: "test/no-tools-vision",
113+
architecture: { input_modalities: ["text", "image"] },
114+
providers: [
115+
{ context_length: 96000, supports_tools: false },
116+
{ context_length: 64000, supports_tools: false },
117+
],
118+
},
119+
{
120+
id: "test/mixed-routes",
121+
providers: [{ supports_tools: false }, { supports_tools: true }],
122+
},
123+
{
124+
id: "test/reversed-mixed-routes",
125+
providers: [{ supports_tools: true }, { supports_tools: false }],
126+
},
127+
{
128+
id: "test/unknown-route",
129+
providers: [{ supports_tools: false }, { context_length: 48000 }],
130+
},
131+
{
132+
id: "test/errored-route",
133+
providers: [
134+
{ status: "error", context_length: 262144, supports_tools: true },
135+
{ status: "live", context_length: 32000, supports_tools: false },
136+
],
137+
},
138+
{
139+
id: "test/errored-unsupported-route",
140+
providers: [
141+
{ status: "error", supports_tools: false },
142+
{ status: "live", supports_tools: true },
143+
],
144+
},
145+
{ id: "test/no-routes" },
146+
{ id: "test/unknown-only", providers: [{}] },
147+
{ id: "test/tools", providers: [{ supports_tools: true }] },
148+
],
149+
}),
150+
),
151+
);
152+
153+
const models = await discoverHuggingfaceModels("hf_test_token");
154+
155+
expect(
156+
models.map(({ id, input, contextWindow, compat }) => ({ id, input, contextWindow, compat })),
157+
).toEqual([
158+
{
159+
id: "test/no-tools-vision",
160+
input: ["text", "image"],
161+
contextWindow: 64000,
162+
compat: { supportsTools: false },
163+
},
164+
{
165+
id: "test/mixed-routes",
166+
input: ["text"],
167+
contextWindow: 131072,
168+
compat: { supportsTools: false },
169+
},
170+
{
171+
id: "test/reversed-mixed-routes",
172+
input: ["text"],
173+
contextWindow: 131072,
174+
compat: { supportsTools: false },
175+
},
176+
{
177+
id: "test/unknown-route",
178+
input: ["text"],
179+
contextWindow: 48000,
180+
compat: { supportsTools: false },
181+
},
182+
{
183+
id: "test/errored-route",
184+
input: ["text"],
185+
contextWindow: 32000,
186+
compat: { supportsTools: false },
187+
},
188+
{
189+
id: "test/errored-unsupported-route",
190+
input: ["text"],
191+
contextWindow: 131072,
192+
compat: undefined,
193+
},
194+
{
195+
id: "test/no-routes",
196+
input: ["text"],
197+
contextWindow: 131072,
198+
compat: undefined,
199+
},
200+
{
201+
id: "test/unknown-only",
202+
input: ["text"],
203+
contextWindow: 131072,
204+
compat: undefined,
205+
},
206+
{
207+
id: "test/tools",
208+
input: ["text"],
209+
contextWindow: 131072,
210+
compat: undefined,
211+
},
212+
]);
40213
});
41214

42215
it("uses the default discovery timeout for live Hugging Face fetches", async () => {

‎extensions/huggingface/models.ts‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type HFModelEntry = {
3333
input_modalities?: string[];
3434
};
3535
providers?: Array<{
36+
status?: string;
3637
context_length?: number;
38+
supports_tools?: boolean;
3739
}>;
3840
};
3941

@@ -88,26 +90,19 @@ function isReasoningModelHeuristic(modelId: string): boolean {
8890
);
8991
}
9092

91-
function inferredMetaFromModelId(id: string): { name: string; reasoning: boolean } {
92-
const base = id.split("/").pop() ?? id;
93-
const reasoning = isReasoningModelHeuristic(id);
94-
const name = base.replace(/-/g, " ").replace(/\b(\w)/g, (c) => c.toUpperCase());
95-
return { name, reasoning };
96-
}
97-
98-
function displayNameFromApiEntry(entry: HFModelEntry, inferredName: string): string {
93+
function displayNameFromApiEntry(entry: HFModelEntry): string {
9994
const fromApi =
10095
(typeof entry.name === "string" && entry.name.trim()) ||
10196
(typeof entry.title === "string" && entry.title.trim()) ||
10297
(typeof entry.display_name === "string" && entry.display_name.trim());
10398
if (fromApi) {
10499
return fromApi;
105100
}
101+
const base = entry.id.split("/").pop() ?? entry.id;
106102
if (typeof entry.owned_by === "string" && entry.owned_by.trim()) {
107-
const base = entry.id.split("/").pop() ?? entry.id;
108103
return `${entry.owned_by.trim()}/${base}`;
109104
}
110-
return inferredName;
105+
return base.replace(/-/g, " ").replace(/\b(\w)/g, (c) => c.toUpperCase());
111106
}
112107

113108
function readHuggingfaceModelRows(body: unknown): readonly unknown[] {
@@ -130,27 +125,30 @@ function projectHuggingfaceModels(rows: readonly unknown[]): ModelDefinitionConf
130125
}
131126
seen.add(id);
132127

133-
const catalogEntry = catalogById.get(id);
134-
if (catalogEntry) {
135-
models.push(Object.assign({}, catalogEntry));
136-
continue;
137-
}
138-
139-
const inferred = inferredMetaFromModelId(id);
140128
const modalities = entry?.architecture?.input_modalities;
141-
const providers = Array.isArray(entry?.providers) ? entry.providers : [];
142-
const providerWithContext = providers.find(
143-
(provider) => typeof provider?.context_length === "number" && provider.context_length > 0,
144-
);
145-
models.push({
129+
const providers = Array.isArray(entry?.providers)
130+
? entry.providers.filter((provider) => provider?.status !== "error")
131+
: [];
132+
const providerContexts = providers
133+
.map((provider) => provider?.context_length)
134+
.filter((context): context is number => typeof context === "number" && context > 0);
135+
const model: ModelDefinitionConfig = catalogById.get(id) ?? {
146136
id,
147-
name: displayNameFromApiEntry(entry, inferred.name),
148-
reasoning: inferred.reasoning,
137+
name: displayNameFromApiEntry(entry),
138+
reasoning: isReasoningModelHeuristic(id),
149139
input:
150140
Array.isArray(modalities) && modalities.includes("image") ? ["text", "image"] : ["text"],
151141
cost: HUGGINGFACE_DEFAULT_COST,
152-
contextWindow: providerWithContext?.context_length ?? HUGGINGFACE_DEFAULT_CONTEXT_WINDOW,
142+
contextWindow: HUGGINGFACE_DEFAULT_CONTEXT_WINDOW,
153143
maxTokens: HUGGINGFACE_DEFAULT_MAX_TOKENS,
144+
};
145+
models.push({
146+
...model,
147+
contextWindow:
148+
providerContexts.length > 0 ? Math.min(...providerContexts) : model.contextWindow,
149+
...(providers.some((provider) => provider?.supports_tools === false)
150+
? { compat: { ...model.compat, supportsTools: false } }
151+
: {}),
154152
});
155153
}
156154
return models;

0 commit comments

Comments
 (0)