xonecas.io
64875
Mc

mini-coder / commit 0023906

feat: add discoverCustomProviders() for OpenAI-compatible endpoints

Queries each configured custom provider's /v1/models endpoint at startup,
constructs pi-ai Model objects, and collects warnings for unreachable
endpoints.

Andy Nutter-Upham2026-04-10 002390619fb149c65aca95313bab624b68bbe3ca parent 2f6f851

2 files changed

src/index.test.ts+117−0
@@ -6,6 +6,7 @@ import { pathToFileURL } from "node:url";
66 import {
77 type AppState,
88 didOAuthCredentialsChange,
9+ discoverCustomProviders,
910 loadOAuthCredentials,
1011 loadPromptContext,
1112 reloadPromptContext,
@@ -295,3 +296,119 @@ test("reloadPromptContext swaps in the new context before destroying the old plu
295296 state.db.close();
296297 }
297298 });
299+
300+// ---------------------------------------------------------------------------
301+// discoverCustomProviders
302+// ---------------------------------------------------------------------------
303+
304+test("discoverCustomProviders returns models from a mock endpoint", async () => {
305+ const server = Bun.serve({
306+ port: 0,
307+ fetch() {
308+ return Response.json({
309+ data: [
310+ { id: "gemma4:31b", object: "model" },
311+ { id: "qwen3:8b", object: "model" },
312+ ],
313+ });
314+ },
315+ });
316+
317+ try {
318+ const result = await discoverCustomProviders(
319+ [{ name: "test-local", baseUrl: `http://localhost:${server.port}` }],
320+ new Set(),
321+ );
322+
323+ expect(result.warnings).toEqual([]);
324+ expect(result.models).toHaveLength(2);
325+ expect(result.models[0]).toMatchObject({
326+ id: "gemma4:31b",
327+ name: "gemma4:31b",
328+ api: "openai-completions",
329+ provider: "test-local",
330+ baseUrl: `http://localhost:${server.port}`,
331+ reasoning: false,
332+ input: ["text"],
333+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
334+ contextWindow: 131072,
335+ maxTokens: 8192,
336+ });
337+ expect(result.providers.get("test-local")).toBe("no-key");
338+ } finally {
339+ server.stop(true);
340+ }
341+});
342+
343+test("discoverCustomProviders uses configured apiKey", async () => {
344+ const server = Bun.serve({
345+ port: 0,
346+ fetch() {
347+ return Response.json({ data: [{ id: "model-1", object: "model" }] });
348+ },
349+ });
350+
351+ try {
352+ const result = await discoverCustomProviders(
353+ [
354+ {
355+ name: "keyed",
356+ baseUrl: `http://localhost:${server.port}`,
357+ apiKey: "my-secret-key",
358+ },
359+ ],
360+ new Set(),
361+ );
362+
363+ expect(result.providers.get("keyed")).toBe("my-secret-key");
364+ } finally {
365+ server.stop(true);
366+ }
367+});
368+
369+test("discoverCustomProviders returns a warning when the endpoint is unreachable", async () => {
370+ const result = await discoverCustomProviders(
371+ [{ name: "dead", baseUrl: "http://localhost:1" }],
372+ new Set(),
373+ );
374+
375+ expect(result.models).toEqual([]);
376+ expect(result.providers.size).toBe(0);
377+ expect(result.warnings).toHaveLength(1);
378+ expect(result.warnings[0]).toContain('Custom provider "dead"');
379+ expect(result.warnings[0]).toContain("http://localhost:1/models");
380+});
381+
382+test("discoverCustomProviders skips providers that collide with built-in names", async () => {
383+ const result = await discoverCustomProviders(
384+ [{ name: "openai", baseUrl: "http://localhost:11434/v1" }],
385+ new Set(["openai"]),
386+ );
387+
388+ expect(result.models).toEqual([]);
389+ expect(result.warnings).toHaveLength(1);
390+ expect(result.warnings[0]).toContain('"openai"');
391+ expect(result.warnings[0]).toContain("built-in");
392+});
393+
394+test("discoverCustomProviders handles empty model list from endpoint", async () => {
395+ const server = Bun.serve({
396+ port: 0,
397+ fetch() {
398+ return Response.json({ data: [] });
399+ },
400+ });
401+
402+ try {
403+ const result = await discoverCustomProviders(
404+ [{ name: "empty", baseUrl: `http://localhost:${server.port}` }],
405+ new Set(),
406+ );
407+
408+ expect(result.models).toEqual([]);
409+ expect(result.providers.get("empty")).toBe("no-key");
410+ expect(result.warnings).toEqual([]);
411+ } finally {
412+ server.stop(true);
413+ }
414+});
src/index.ts+96−0
@@ -57,6 +57,7 @@ import {
5757 truncateSessions,
5858 } from "./session.ts";
5959 import {
60+ type CustomProvider,
6061 loadSettings,
6162 resolveStartupSettings,
6263 type UserSettings,
@@ -208,6 +209,101 @@ async function discoverProviders(): Promise<DiscoveryResult> {
208209 return { providers, oauthCredentials };
209210 }
210211
212+// ---------------------------------------------------------------------------
213+// Custom provider discovery
214+// ---------------------------------------------------------------------------
215+
216+/** Timeout for custom provider model discovery requests. */
217+const CUSTOM_PROVIDER_TIMEOUT_MS = 3_000;
218+
219+/** Default API key for custom providers that don't require authentication. */
220+const CUSTOM_PROVIDER_DEFAULT_KEY = "no-key";
221+
222+/** Result of custom provider discovery. */
223+interface CustomDiscoveryResult {
224+ /** Discovered models from all reachable custom providers. */
225+ models: Model<"openai-completions">[];
226+ /** Provider name → API key for discovered providers. */
227+ providers: Map<string, string>;
228+ /** Warning messages for unreachable or invalid providers. */
229+ warnings: string[];
230+}
231+
232+/**
233+ * Discover models from user-configured OpenAI-compatible endpoints.
234+ *
235+ * Queries each provider's `/models` endpoint and constructs pi-ai Model
236+ * objects from the response. Unreachable endpoints produce a warning
237+ * instead of failing startup.
238+ *
239+ * @param customProviders - Configured custom provider entries.
240+ * @param builtInProviderNames - Names of built-in providers (to detect collisions).
241+ * @returns Discovered models, provider credentials, and warnings.
242+ */
243+export async function discoverCustomProviders(
244+ customProviders: readonly CustomProvider[],
245+ builtInProviderNames: ReadonlySet<string>,
246+): Promise<CustomDiscoveryResult> {
247+ const models: Model<"openai-completions">[] = [];
248+ const providers = new Map<string, string>();
249+ const warnings: string[] = [];
250+
251+ for (const entry of customProviders) {
252+ if (builtInProviderNames.has(entry.name)) {
253+ warnings.push(
254+ `Custom provider "${entry.name}" skipped: name conflicts with a built-in provider.`,
255+ );
256+ continue;
257+ }
258+
259+ const apiKey = entry.apiKey ?? CUSTOM_PROVIDER_DEFAULT_KEY;
260+ const modelsUrl = `${entry.baseUrl}/models`;
261+
262+ try {
263+ const response = await fetch(modelsUrl, {
264+ signal: AbortSignal.timeout(CUSTOM_PROVIDER_TIMEOUT_MS),
265+ });
266+
267+ if (!response.ok) {
268+ warnings.push(
269+ `Custom provider "${entry.name}": ${response.status} ${response.statusText} (${modelsUrl})`,
270+ );
271+ continue;
272+ }
273+
274+ const body = (await response.json()) as {
275+ data?: { id: string }[];
276+ };
277+ const modelList = body.data ?? [];
278+
279+ for (const item of modelList) {
280+ if (typeof item.id !== "string" || !item.id) continue;
281+
282+ models.push({
283+ id: item.id,
284+ name: item.id,
285+ api: "openai-completions",
286+ provider: entry.name,
287+ baseUrl: entry.baseUrl,
288+ reasoning: false,
289+ input: ["text"],
290+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
291+ contextWindow: 131072,
292+ maxTokens: 8192,
293+ });
294+ }
295+
296+ providers.set(entry.name, apiKey);
297+ } catch (error) {
298+ warnings.push(
299+ `Custom provider "${entry.name}": ${getErrorMessage(error)} (${modelsUrl})`,
300+ );
301+ }
302+ }
303+
304+ return { models, providers, warnings };
305+}
306+
211307 // ---------------------------------------------------------------------------
212308 // Model selection
213309 // ---------------------------------------------------------------------------