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();