xonecas.io
64875
Mc

mini-coder / commit 2f6f851

feat: add CustomProvider type and settings parsing

Adds a CustomProvider interface and customProviders field to UserSettings.
Settings parsing validates entries (non-empty name/baseUrl, deduplication)
and drops invalid ones.

Andy Nutter-Upham2026-04-10 2f6f8518eca329ce5d35e588d0b461c959897966 parent 155412b

2 files changed

src/settings.test.ts+87−0
@@ -202,4 +202,91 @@ describe("settings", () => {
202202 verbose: false,
203203 });
204204 });
205+
206+ test("loadSettings parses valid customProviders entries", () => {
207+ const dir = createTempDir();
208+ const path = join(dir, "settings.json");
209+ writeFileSync(
210+ path,
211+ JSON.stringify({
212+ customProviders: [
213+ { name: "ollama", baseUrl: "http://localhost:11434/v1" },
214+ {
215+ name: "lm-studio",
216+ baseUrl: "http://localhost:1234/v1",
217+ apiKey: "lm-studio",
218+ },
219+ ],
220+ }),
221+ "utf-8",
222+ );
223+
224+ const settings = loadSettings(path);
225+ expect(settings.customProviders).toEqual([
226+ { name: "ollama", baseUrl: "http://localhost:11434/v1" },
227+ {
228+ name: "lm-studio",
229+ baseUrl: "http://localhost:1234/v1",
230+ apiKey: "lm-studio",
231+ },
232+ ]);
233+ });
234+
235+ test("loadSettings drops customProviders entries with missing or invalid fields", () => {
236+ const dir = createTempDir();
237+ const path = join(dir, "settings.json");
238+ writeFileSync(
239+ path,
240+ JSON.stringify({
241+ customProviders: [
242+ { name: "good", baseUrl: "http://localhost:11434/v1" },
243+ { name: "", baseUrl: "http://localhost:1234/v1" },
244+ { name: "no-url" },
245+ { baseUrl: "http://localhost:5678/v1" },
246+ "not-an-object",
247+ 42,
248+ null,
249+ ],
250+ }),
251+ "utf-8",
252+ );
253+
254+ const settings = loadSettings(path);
255+ expect(settings.customProviders).toEqual([
256+ { name: "good", baseUrl: "http://localhost:11434/v1" },
257+ ]);
258+ });
259+
260+ test("loadSettings drops duplicate customProviders names keeping the first", () => {
261+ const dir = createTempDir();
262+ const path = join(dir, "settings.json");
263+ writeFileSync(
264+ path,
265+ JSON.stringify({
266+ customProviders: [
267+ { name: "ollama", baseUrl: "http://localhost:11434/v1" },
268+ { name: "ollama", baseUrl: "http://localhost:9999/v1" },
269+ ],
270+ }),
271+ "utf-8",
272+ );
273+
274+ const settings = loadSettings(path);
275+ expect(settings.customProviders).toEqual([
276+ { name: "ollama", baseUrl: "http://localhost:11434/v1" },
277+ ]);
278+ });
279+
280+ test("loadSettings omits customProviders when the field is not an array", () => {
281+ const dir = createTempDir();
282+ const path = join(dir, "settings.json");
283+ writeFileSync(
284+ path,
285+ JSON.stringify({ customProviders: "not-an-array" }),
286+ "utf-8",
287+ );
288+
289+ const settings = loadSettings(path);
290+ expect(settings.customProviders).toBeUndefined();
291+ });
205292 });
src/settings.ts+62−0
@@ -12,6 +12,16 @@ import { dirname } from "node:path";
1212 import type { ThinkingLevel } from "@mariozechner/pi-ai";
1313 import { getErrorMessage } from "./errors.ts";
1414
15+/** A user-configured OpenAI-compatible provider endpoint. */
16+export interface CustomProvider {
17+ /** Provider identifier, e.g. "ollama". Shown as the provider prefix in model names. */
18+ name: string;
19+ /** OpenAI-compatible API base URL, e.g. "http://localhost:11434/v1". */
20+ baseUrl: string;
21+ /** Optional API key. Defaults to "no-key" at discovery time. */
22+ apiKey?: string;
23+}
24+
1525 /** Default reasoning effort when no saved setting exists. */
1626 const DEFAULT_EFFORT: ThinkingLevel = "medium";
1727
@@ -31,6 +41,8 @@ export interface UserSettings {
3141 showReasoning?: boolean;
3242 /** Whether full tool output is shown in the UI. */
3343 verbose?: boolean;
44+ /** Custom OpenAI-compatible provider endpoints. */
45+ customProviders?: CustomProvider[];
3446 }
3547
3648 /** Resolved startup settings after applying defaults and availability checks. */
@@ -172,9 +184,59 @@ function sanitizeSettings(value: unknown): UserSettings {
172184 settings.verbose = candidate.verbose;
173185 }
174186
187+ const customProviders = sanitizeCustomProviders(candidate.customProviders);
188+ if (customProviders) {
189+ settings.customProviders = customProviders;
190+ }
191+
175192 return settings;
176193 }
177194
195+/**
196+ * Validate and normalize custom provider entries.
197+ *
198+ * Drops entries with missing/empty name or baseUrl, and deduplicates by name
199+ * (first entry wins).
200+ */
201+function sanitizeCustomProviders(value: unknown): CustomProvider[] | undefined {
202+ if (!Array.isArray(value)) {
203+ return undefined;
204+ }
205+
206+ const result: CustomProvider[] = [];
207+ const seen = new Set<string>();
208+
209+ for (const item of value) {
210+ if (item == null || typeof item !== "object" || Array.isArray(item)) {
211+ continue;
212+ }
213+
214+ const candidate = item as Record<string, unknown>;
215+ const name =
216+ typeof candidate.name === "string" ? candidate.name.trim() : "";
217+ const baseUrl =
218+ typeof candidate.baseUrl === "string" ? candidate.baseUrl.trim() : "";
219+
220+ if (!name || !baseUrl) {
221+ continue;
222+ }
223+
224+ if (seen.has(name)) {
225+ continue;
226+ }
227+ seen.add(name);
228+
229+ const entry: CustomProvider = { name, baseUrl };
230+ if (typeof candidate.apiKey === "string") {
231+ entry.apiKey = candidate.apiKey;
232+ }
233+
234+ result.push(entry);
235+ }
236+
237+ return result.length > 0 ? result : undefined;
238+}
239+
178240 /**
179241 * Check whether a value is a valid thinking level.
180242 *