Forked from will-lms/openai-compat-endpoint
src / schema.ts
// src/schema.ts
// ΠΠ±ΡΠ°Ρ ΡΡ
Π΅ΠΌΠ° ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ Π΄Π»Ρ Π²ΡΠ΅Π³ΠΎ ΠΏΠ»Π°Π³ΠΈΠ½Π°
import { createConfigSchematics } from "@lmstudio/sdk";
import { loadCache as loadCacheFromFile, saveCache as saveCacheToFile } from './file-cache';
export interface ModelCache {
timestamp: number;
allModels: string[];
freeModels: string[];
}
/**
* ΠΠ°Π³ΡΡΠΆΠ°Π΅Ρ ΠΊΡΡ ΠΈΠ· ΡΠ°ΠΉΠ»Π° (ΠΎΠ±ΡΡΡΠΊΠ° Π½Π°Π΄ file-cache)
*/
export function loadCache(): ModelCache | null {
return loadCacheFromFile();
}
/**
* Π‘ΠΎΡ
ΡΠ°Π½ΡΠ΅Ρ ΠΊΡΡ Π² ΡΠ°ΠΉΠ» (ΠΎΠ±ΡΡΡΠΊΠ° Π½Π°Π΄ file-cache)
*/
export function saveCache(allModels: string[], freeModels: string[]): void {
saveCacheToFile(allModels, freeModels);
}
/**
* ΠΠ΅Π½Π΅ΡΠΈΡΡΠ΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°Π΅ΠΌΠΎΠ΅ ΠΈΠΌΡ Π΄Π»Ρ ΠΌΠΎΠ΄Π΅Π»ΠΈ
*/
export function generateDisplayName(model: string): string {
let name = model.replace(/^[^\/]+\/(.+)/, "$1");
const isFree = name.includes(":free");
name = name.replace(":free", "");
name = name.replace(/-/g, " ");
name = name.replace(/(\d+)\.(\d+)/g, "$1.$2");
name = name.replace(/\b\w/g, char => char.toUpperCase());
if (isFree) {
name += " (free)";
}
return name;
}
/**
* Π‘ΠΎΠ·Π΄Π°ΡΡ ΡΡ
Π΅ΠΌΡ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ Ρ Π·Π°Π΄Π°Π½Π½ΡΠΌ ΡΠΏΠΈΡΠΊΠΎΠΌ ΠΌΠΎΠ΄Π΅Π»Π΅ΠΉ
*/
export function createConfigSchema(modelOptions: Array<{ value: string; displayName: string }>) {
const modelCount = modelOptions.length - 1; // minus "auto"
const statusText = modelCount > 0
? `${modelCount} model${modelCount > 1 ? 's' : ''} available`
: 'No models loaded β restart plugin to refresh';
return createConfigSchematics()
.field(
"model",
"select",
{
displayName: "Model",
subtitle: `Model selection (auto = first available free model) β’ ${statusText}`,
options: modelOptions
},
"auto"
)
.field(
"customModel",
"string",
{
displayName: "Custom Model ID",
subtitle: "Enter model ID manually (overrides selection above)",
placeholder: "provider/model-name:free"
},
""
)
.field(
"onlyFreeModels",
"boolean",
{
displayName: "Only Free Models",
subtitle: "Filter to show only models with ':free' suffix"
},
true
)
.build();
}
/**
* ΠΠ»ΠΎΠ±Π°Π»ΡΠ½Π°Ρ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΡ (ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Π°Ρ Π²Π΅Π·Π΄Π΅)
*/
export const globalConfigSchematics = createConfigSchematics()
.field(
"apiKey",
"string",
{
displayName: "API Key",
subtitle: "OpenRouter API key (optional for free models)",
isProtected: true,
placeholder: "sk-or-v1-..."
},
""
)
.field(
"baseUrl",
"string",
{
displayName: "Base URL",
subtitle: "Base URL for API calls.",
placeholder: "https://openrouter.ai/api/v1"
},
"https://openrouter.ai/api/v1"
)
.build();