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

@ -455,6 +455,10 @@ export function setupIpcHandlers() {
await repo.setConfig(args);
return { success: true };
},
'models:getConfiguredModels': async () => {
const repo = container.resolve<IModelConfigRepo>('modelConfigRepo');
return repo.getAllConfiguredModels();
},
'oauth:connect': async (_event, args) => {
return await connectProvider(args.provider, args.clientId?.trim());
},

View file

@ -44,6 +44,10 @@ import {
usePromptInputController,
} from '@/components/ai-elements/prompt-input'
import { toast } from 'sonner'
import { z } from 'zod'
import { ConfiguredModelEntry } from '@x/shared/src/models.js'
type ConfiguredModel = z.infer<typeof ConfiguredModelEntry>
export type StagedAttachment = {
id: string
@ -68,15 +72,6 @@ const providerDisplayNames: Record<string, string> = {
'openai-compatible': 'OpenAI-Compatible',
}
interface ConfiguredModel {
flavor: string
model: string
apiKey?: string
baseURL?: string
headers?: Record<string, string>
knowledgeGraphModel?: string
}
function getAttachmentIcon(kind: AttachmentIconKind) {
switch (kind) {
case 'audio':
@ -157,45 +152,13 @@ function ChatInputInner({
const [searchEnabled, setSearchEnabled] = useState(false)
const [searchAvailable, setSearchAvailable] = useState(false)
// Load model config from disk (on mount and whenever tab becomes active)
// Load model config via IPC (on mount and whenever tab becomes active)
const loadModelConfig = useCallback(async () => {
try {
const result = await window.ipc.invoke('workspace:readFile', { path: 'config/models.json' })
const parsed = JSON.parse(result.data)
const models: ConfiguredModel[] = []
if (parsed?.providers) {
for (const [flavor, entry] of Object.entries(parsed.providers)) {
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 defaultKey = 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 === defaultKey) return -1
if (bKey === defaultKey) return 1
return 0
})
setConfiguredModels(models)
if (defaultKey) {
setActiveModelKey(defaultKey)
const result = await window.ipc.invoke('models:getConfiguredModels', null)
setConfiguredModels(result.models)
if (result.activeModelKey) {
setActiveModelKey(result.activeModelKey)
}
} catch {
// No config yet

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 };
}
}

View file

@ -2,7 +2,7 @@ import { z } from 'zod';
import { RelPath, Encoding, Stat, DirEntry, ReaddirOptions, ReadFileResult, WorkspaceChangeEvent, WriteFileOptions, WriteFileResult, RemoveOptions } from './workspace.js';
import { ListToolsResponse } from './mcp.js';
import { AskHumanResponsePayload, CreateRunOptions, Run, ListRunsResponse, ToolPermissionAuthorizePayload } from './runs.js';
import { LlmModelConfig } from './models.js';
import { LlmModelConfig, ConfiguredModelsResult } from './models.js';
import { AgentScheduleConfig, AgentScheduleEntry } from './agent-schedule.js';
import { AgentScheduleState } from './agent-schedule-state.js';
import { ServiceEvent } from './service-events.js';
@ -219,6 +219,10 @@ const ipcSchemas = {
success: z.literal(true),
}),
},
'models:getConfiguredModels': {
req: z.null(),
res: ConfiguredModelsResult,
},
'oauth:connect': {
req: z.object({
provider: z.string(),

View file

@ -13,3 +13,17 @@ export const LlmModelConfig = z.object({
models: z.array(z.string()).optional(),
knowledgeGraphModel: z.string().optional(),
});
export const ConfiguredModelEntry = z.object({
flavor: LlmProvider.shape.flavor,
model: z.string(),
apiKey: z.string().optional(),
baseURL: z.string().optional(),
headers: z.record(z.string(), z.string()).optional(),
knowledgeGraphModel: z.string().optional(),
});
export const ConfiguredModelsResult = z.object({
models: z.array(ConfiguredModelEntry),
activeModelKey: z.string(),
});