5875
Os
ollama-space-molt-player / files
ollama-space-molt-player / packages / client / src / model-config.ts
2.4 KB
1import { readFileSync } from "node:fs"; 2import { join } from "node:path"; 3 4export interface ModelConfig { 5 displayName: string; 6 description: string; 7 ollama: { 8 model: string; 9 options: Record<string, unknown>; 10 }; 11 contextWindow: number; 12 recommendedMessages?: number; 13 recommended: boolean; 14} 15 16export interface ModelConfigFile { 17 models: Record<string, ModelConfig>; 18 default: string; 19} 20 21let cachedConfig: ModelConfigFile | null = null; 22 23function getRepoRoot(): string { 24 return join(__dirname, "../../.."); 25} 26 27function loadConfigFile(): ModelConfigFile { 28 if (cachedConfig) { 29 return cachedConfig; 30 } 31 32 const configPath = join(getRepoRoot(), "player-models.json"); 33 34 try { 35 const content = readFileSync(configPath, "utf-8"); 36 cachedConfig = JSON.parse(content) as ModelConfigFile; 37 38 if (!cachedConfig.models || typeof cachedConfig.models !== "object") { 39 throw new Error("Invalid player-models.json: missing 'models' object"); 40 } 41 42 if (!cachedConfig.default || typeof cachedConfig.default !== "string") { 43 throw new Error("Invalid player-models.json: missing 'default' field"); 44 } 45 46 return cachedConfig; 47 } catch (error) { 48 if (error instanceof Error) { 49 if (error.message.includes("ENOENT")) { 50 throw new Error( 51 `Could not load player-models.json from repo root.\n\nExpected location: ${configPath}\n\nThe file must contain valid JSON with a "models" object and "default" field.\nSee documentation for schema details.`, 52 ); 53 } 54 throw error; 55 } 56 throw new Error("Unknown error loading player-models.json"); 57 } 58} 59 60export function loadModelConfig(configName?: string): ModelConfig { 61 const configFile = loadConfigFile(); 62 63 const name = configName || configFile.default; 64 65 if (!configFile.models[name]) { 66 const available = Object.keys(configFile.models); 67 throw new Error( 68 `Model configuration "${name}" not found.\n\nAvailable models:\n${available.map((m) => ` - ${m}${configFile.models[m].recommended ? " (recommended)" : ""}`).join("\n")}\n\nCheck player-models.json for configuration details.`, 69 ); 70 } 71 72 return configFile.models[name]; 73} 74 75export function listAvailableModels(): string[] { 76 const configFile = loadConfigFile(); 77 return Object.keys(configFile.models); 78} 79 80export function getModelListText(): string { 81 const configFile = loadConfigFile(); 82 return Object.entries(configFile.models) 83 .map( 84 ([name, config]) => 85 ` ${name.padEnd(15)} ${config.displayName.padEnd(30)} ${config.recommended ? "(Recommended)" : ""}`, 86 ) 87 .join("\n"); 88}