mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-04-25 00:16:29 +02:00
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:
parent
16b8975b00
commit
bfb6702dc9
5 changed files with 77 additions and 47 deletions
|
|
@ -455,6 +455,10 @@ export function setupIpcHandlers() {
|
||||||
await repo.setConfig(args);
|
await repo.setConfig(args);
|
||||||
return { success: true };
|
return { success: true };
|
||||||
},
|
},
|
||||||
|
'models:getConfiguredModels': async () => {
|
||||||
|
const repo = container.resolve<IModelConfigRepo>('modelConfigRepo');
|
||||||
|
return repo.getAllConfiguredModels();
|
||||||
|
},
|
||||||
'oauth:connect': async (_event, args) => {
|
'oauth:connect': async (_event, args) => {
|
||||||
return await connectProvider(args.provider, args.clientId?.trim());
|
return await connectProvider(args.provider, args.clientId?.trim());
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,10 @@ import {
|
||||||
usePromptInputController,
|
usePromptInputController,
|
||||||
} from '@/components/ai-elements/prompt-input'
|
} from '@/components/ai-elements/prompt-input'
|
||||||
import { toast } from 'sonner'
|
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 = {
|
export type StagedAttachment = {
|
||||||
id: string
|
id: string
|
||||||
|
|
@ -68,15 +72,6 @@ const providerDisplayNames: Record<string, string> = {
|
||||||
'openai-compatible': 'OpenAI-Compatible',
|
'openai-compatible': 'OpenAI-Compatible',
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ConfiguredModel {
|
|
||||||
flavor: string
|
|
||||||
model: string
|
|
||||||
apiKey?: string
|
|
||||||
baseURL?: string
|
|
||||||
headers?: Record<string, string>
|
|
||||||
knowledgeGraphModel?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAttachmentIcon(kind: AttachmentIconKind) {
|
function getAttachmentIcon(kind: AttachmentIconKind) {
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case 'audio':
|
case 'audio':
|
||||||
|
|
@ -157,45 +152,13 @@ function ChatInputInner({
|
||||||
const [searchEnabled, setSearchEnabled] = useState(false)
|
const [searchEnabled, setSearchEnabled] = useState(false)
|
||||||
const [searchAvailable, setSearchAvailable] = 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 () => {
|
const loadModelConfig = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await window.ipc.invoke('workspace:readFile', { path: 'config/models.json' })
|
const result = await window.ipc.invoke('models:getConfiguredModels', null)
|
||||||
const parsed = JSON.parse(result.data)
|
setConfiguredModels(result.models)
|
||||||
const models: ConfiguredModel[] = []
|
if (result.activeModelKey) {
|
||||||
if (parsed?.providers) {
|
setActiveModelKey(result.activeModelKey)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// No config yet
|
// No config yet
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,15 @@ import { WorkDir } from "../config/config.js";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
import { ConfiguredModelsResult, LlmProvider } from "@x/shared/dist/models.js";
|
||||||
|
|
||||||
|
type LlmProviderFlavor = z.infer<typeof LlmProvider>["flavor"];
|
||||||
|
|
||||||
export interface IModelConfigRepo {
|
export interface IModelConfigRepo {
|
||||||
ensureConfig(): Promise<void>;
|
ensureConfig(): Promise<void>;
|
||||||
getConfig(): Promise<z.infer<typeof ModelConfig>>;
|
getConfig(): Promise<z.infer<typeof ModelConfig>>;
|
||||||
setConfig(config: z.infer<typeof ModelConfig>): Promise<void>;
|
setConfig(config: z.infer<typeof ModelConfig>): Promise<void>;
|
||||||
|
getAllConfiguredModels(): Promise<z.infer<typeof ConfiguredModelsResult>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultConfig: z.infer<typeof ModelConfig> = {
|
const defaultConfig: z.infer<typeof ModelConfig> = {
|
||||||
|
|
@ -56,4 +60,45 @@ export class FSModelConfigRepo implements IModelConfigRepo {
|
||||||
const toWrite = { ...config, providers: existingProviders };
|
const toWrite = { ...config, providers: existingProviders };
|
||||||
await fs.writeFile(this.configPath, JSON.stringify(toWrite, null, 2));
|
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 };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { z } from 'zod';
|
||||||
import { RelPath, Encoding, Stat, DirEntry, ReaddirOptions, ReadFileResult, WorkspaceChangeEvent, WriteFileOptions, WriteFileResult, RemoveOptions } from './workspace.js';
|
import { RelPath, Encoding, Stat, DirEntry, ReaddirOptions, ReadFileResult, WorkspaceChangeEvent, WriteFileOptions, WriteFileResult, RemoveOptions } from './workspace.js';
|
||||||
import { ListToolsResponse } from './mcp.js';
|
import { ListToolsResponse } from './mcp.js';
|
||||||
import { AskHumanResponsePayload, CreateRunOptions, Run, ListRunsResponse, ToolPermissionAuthorizePayload } from './runs.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 { AgentScheduleConfig, AgentScheduleEntry } from './agent-schedule.js';
|
||||||
import { AgentScheduleState } from './agent-schedule-state.js';
|
import { AgentScheduleState } from './agent-schedule-state.js';
|
||||||
import { ServiceEvent } from './service-events.js';
|
import { ServiceEvent } from './service-events.js';
|
||||||
|
|
@ -219,6 +219,10 @@ const ipcSchemas = {
|
||||||
success: z.literal(true),
|
success: z.literal(true),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
'models:getConfiguredModels': {
|
||||||
|
req: z.null(),
|
||||||
|
res: ConfiguredModelsResult,
|
||||||
|
},
|
||||||
'oauth:connect': {
|
'oauth:connect': {
|
||||||
req: z.object({
|
req: z.object({
|
||||||
provider: z.string(),
|
provider: z.string(),
|
||||||
|
|
|
||||||
|
|
@ -13,3 +13,17 @@ export const LlmModelConfig = z.object({
|
||||||
models: z.array(z.string()).optional(),
|
models: z.array(z.string()).optional(),
|
||||||
knowledgeGraphModel: 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(),
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue