Refactor: move model config parsing from renderer to main process via IPC

The renderer was reading config/models.json via workspace:readFile and
manually parsing the provider/model structure. This moves that logic into
a dedicated models:getConfiguredModels IPC handler backed by a new
getAllConfiguredModels() method on the model config repo, matching the
existing models:saveConfig pattern.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
tusharmagar 2026-03-16 11:37:47 +05:30
parent 16b8975b00
commit bfb6702dc9
5 changed files with 77 additions and 47 deletions

View file

@ -3,11 +3,15 @@ import { WorkDir } from "../config/config.js";
import fs from "fs/promises";
import path from "path";
import z from "zod";
import { ConfiguredModelsResult, LlmProvider } from "@x/shared/dist/models.js";
type LlmProviderFlavor = z.infer<typeof LlmProvider>["flavor"];
export interface IModelConfigRepo {
ensureConfig(): Promise<void>;
getConfig(): Promise<z.infer<typeof ModelConfig>>;
setConfig(config: z.infer<typeof ModelConfig>): Promise<void>;
getAllConfiguredModels(): Promise<z.infer<typeof ConfiguredModelsResult>>;
}
const defaultConfig: z.infer<typeof ModelConfig> = {
@ -56,4 +60,45 @@ export class FSModelConfigRepo implements IModelConfigRepo {
const toWrite = { ...config, providers: existingProviders };
await fs.writeFile(this.configPath, JSON.stringify(toWrite, null, 2));
}
async getAllConfiguredModels(): Promise<z.infer<typeof ConfiguredModelsResult>> {
const raw = await fs.readFile(this.configPath, "utf8");
const parsed = JSON.parse(raw);
const models: z.infer<typeof ConfiguredModelsResult>["models"] = [];
if (parsed?.providers) {
for (const [flavor, entry] of Object.entries(parsed.providers) as [LlmProviderFlavor, unknown][]) {
const e = entry as Record<string, unknown>;
const modelList: string[] = Array.isArray(e.models) ? e.models as string[] : [];
const singleModel = typeof e.model === "string" ? e.model : "";
const allModels = modelList.length > 0 ? modelList : singleModel ? [singleModel] : [];
for (const model of allModels) {
if (model) {
models.push({
flavor,
model,
apiKey: (e.apiKey as string) || undefined,
baseURL: (e.baseURL as string) || undefined,
headers: (e.headers as Record<string, string>) || undefined,
knowledgeGraphModel: (e.knowledgeGraphModel as string) || undefined,
});
}
}
}
}
const activeModelKey = parsed?.provider?.flavor && parsed?.model
? `${parsed.provider.flavor}/${parsed.model}`
: "";
models.sort((a, b) => {
const aKey = `${a.flavor}/${a.model}`;
const bKey = `${b.flavor}/${b.model}`;
if (aKey === activeModelKey) return -1;
if (bKey === activeModelKey) return 1;
return 0;
});
return { models, activeModelKey };
}
}