Project Files
src / index.ts
import { LMStudioClient, type PluginContext } from "@lmstudio/sdk";
import { rebuildConfigSchematics, type ModelOption } from "./configSchematics";
import { toolsProvider } from "./toolsProvider";
declare var process: any;
// Au démarrage on interroge LM Studio pour la liste des modèles LLM
// téléchargés, et on injecte cette liste dans le select localModel du
// panneau de config. Si l'appel échoue (perms, version SDK, …), on
// retombe sur la liste fallback définie dans configSchematics.ts.
async function fetchLocalModels(): Promise<ModelOption[]> {
try {
const client = new LMStudioClient({
clientIdentifier: process.env.LMS_PLUGIN_CLIENT_IDENTIFIER,
clientPasskey: process.env.LMS_PLUGIN_CLIENT_PASSKEY,
baseUrl: process.env.LMS_PLUGIN_BASE_URL,
});
const llms = await client.system.listDownloadedModels("llm");
if (!Array.isArray(llms) || llms.length === 0) return [];
// displayName parfois redondant avec modelKey → on évite le doublon
return llms
.map((m): ModelOption => {
const value = m.modelKey;
const label = m.displayName && m.displayName !== m.modelKey
? `${m.modelKey} — ${m.displayName}`
: m.modelKey;
return { value, displayName: label };
})
// Tri alpha pour un ordre déterministe dans le dropdown
.sort((a, b) => a.value.localeCompare(b.value));
} catch (e) {
console.warn("livre-heros-bac: impossible de lister les modèles LM Studio (fallback statique utilisé) :", e);
return [];
}
}
export async function main(context: PluginContext) {
const localModels = await fetchLocalModels();
context.withConfigSchematics(rebuildConfigSchematics(localModels));
context.withToolsProvider(toolsProvider);
}