@kehto/paja 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14138,8 +14138,9 @@ async function* readLlmSseData(body, signal) {
14138
14138
  var PRESET_ROWS = [
14139
14139
  { id: "openai", label: "OpenAI", protocol: "openai-chat", baseUrl: "https://api.openai.com/v1", requiresCredential: true, local: false, docsUrl: "https://platform.openai.com/docs/api-reference/chat", exampleModels: ["gpt-4.1-mini", "gpt-4o-mini"] },
14140
14140
  { id: "anthropic", label: "Anthropic", protocol: "anthropic-messages", baseUrl: "https://api.anthropic.com/v1", requiresCredential: true, local: false, docsUrl: "https://docs.anthropic.com/en/api/messages", exampleModels: ["claude-sonnet-4-5", "claude-haiku-4-5"] },
14141
- { id: "gemini", label: "Google Gemini", protocol: "gemini", baseUrl: "https://generativelanguage.googleapis.com/v1beta", requiresCredential: true, local: false, docsUrl: "https://ai.google.dev/api/generate-content", exampleModels: ["gemini-2.5-flash", "gemini-2.5-pro"] },
14142
14141
  { id: "openrouter", label: "OpenRouter", protocol: "openai-chat", baseUrl: "https://openrouter.ai/api/v1", requiresCredential: true, local: false, docsUrl: "https://openrouter.ai/docs/api-reference/overview", exampleModels: ["openai/gpt-4.1-mini", "anthropic/claude-sonnet-4.5"] },
14142
+ { id: "routstr", label: "Routstr", protocol: "openai-chat", baseUrl: "http://127.0.0.1:8008/v1", requiresCredential: false, local: true, docsUrl: "https://routstr.com", exampleModels: [], modelsPath: "/models" },
14143
+ { id: "gemini", label: "Google Gemini", protocol: "gemini", baseUrl: "https://generativelanguage.googleapis.com/v1beta", requiresCredential: true, local: false, docsUrl: "https://ai.google.dev/api/generate-content", exampleModels: ["gemini-2.5-flash", "gemini-2.5-pro"] },
14143
14144
  { id: "groq", label: "Groq", protocol: "openai-chat", baseUrl: "https://api.groq.com/openai/v1", requiresCredential: true, local: false, docsUrl: "https://console.groq.com/docs/openai", exampleModels: ["llama-3.3-70b-versatile"] },
14144
14145
  { id: "mistral", label: "Mistral", protocol: "openai-chat", baseUrl: "https://api.mistral.ai/v1", requiresCredential: true, local: false, docsUrl: "https://docs.mistral.ai/api/", exampleModels: ["mistral-small-latest"] },
14145
14146
  { id: "deepseek", label: "DeepSeek", protocol: "openai-chat", baseUrl: "https://api.deepseek.com/v1", requiresCredential: true, local: false, docsUrl: "https://api-docs.deepseek.com/", exampleModels: ["deepseek-chat"] },
@@ -14786,6 +14787,76 @@ function installPajaResourceSettings() {
14786
14787
  return { getServers: settings.getServers, dispose: () => form?.removeEventListener("submit", submit) };
14787
14788
  }
14788
14789
 
14790
+ // src/browser-llm-models.ts
14791
+ var PajaLlmModelsError = class extends Error {
14792
+ constructor(failure4, message3) {
14793
+ super(message3);
14794
+ this.failure = failure4;
14795
+ this.name = "PajaLlmModelsError";
14796
+ }
14797
+ failure;
14798
+ };
14799
+ var PAJA_LLM_MODELS_TIMEOUT_MS = 8e3;
14800
+ function llmModelsUrl(baseUrl, modelsPath) {
14801
+ return `${baseUrl.trim().replace(/\/+$/, "")}/${modelsPath.trim().replace(/^\/+/, "")}`;
14802
+ }
14803
+ function readModelIds(body) {
14804
+ const rows = Array.isArray(body) ? body : body && typeof body === "object" ? body.data ?? body.models : void 0;
14805
+ if (!Array.isArray(rows)) throw new PajaLlmModelsError("shape", "The server did not return a model list.");
14806
+ const ids = [];
14807
+ const seen = /* @__PURE__ */ new Set();
14808
+ for (const row of rows) {
14809
+ const id = typeof row === "string" ? row : row && typeof row === "object" && typeof row.id === "string" ? row.id : void 0;
14810
+ const trimmed = id?.trim();
14811
+ if (!trimmed || seen.has(trimmed)) continue;
14812
+ seen.add(trimmed);
14813
+ ids.push(trimmed);
14814
+ }
14815
+ if (ids.length === 0) throw new PajaLlmModelsError("shape", "The server reported no models.");
14816
+ return ids.sort((a, b) => a.localeCompare(b));
14817
+ }
14818
+ async function fetchPajaLlmModels(options) {
14819
+ const fetchImpl = options.fetch ?? globalThis.fetch;
14820
+ const controller = new AbortController();
14821
+ const timeoutMs = options.timeoutMs ?? PAJA_LLM_MODELS_TIMEOUT_MS;
14822
+ let timedOut = false;
14823
+ const timer = setTimeout(() => {
14824
+ timedOut = true;
14825
+ controller.abort();
14826
+ }, timeoutMs);
14827
+ const abortFromCaller = () => controller.abort();
14828
+ options.signal?.addEventListener("abort", abortFromCaller, { once: true });
14829
+ const reason = () => timedOut ? "timeout" : options.signal?.aborted ? "aborted" : "network";
14830
+ try {
14831
+ const headers = { accept: "application/json" };
14832
+ if (options.credential) headers.authorization = `Bearer ${options.credential}`;
14833
+ const response = await fetchImpl(llmModelsUrl(options.baseUrl, options.modelsPath), {
14834
+ method: "GET",
14835
+ headers,
14836
+ signal: controller.signal
14837
+ });
14838
+ if (!response.ok) {
14839
+ throw new PajaLlmModelsError("http", `The server answered ${String(response.status)} for ${options.modelsPath}.`);
14840
+ }
14841
+ let body;
14842
+ try {
14843
+ body = await response.json();
14844
+ } catch {
14845
+ throw new PajaLlmModelsError("shape", "The server did not return JSON for the model list.");
14846
+ }
14847
+ return readModelIds(body);
14848
+ } catch (error) {
14849
+ if (error instanceof PajaLlmModelsError) throw error;
14850
+ if (timedOut || options.signal?.aborted) {
14851
+ throw new PajaLlmModelsError(reason(), timedOut ? `No answer from the server within ${String(Math.round(timeoutMs / 1e3))}s.` : "The model listing was cancelled.");
14852
+ }
14853
+ throw new PajaLlmModelsError("network", "Could not read the model list \u2014 the server is down, or it does not allow this page cross-origin (CORS).");
14854
+ } finally {
14855
+ clearTimeout(timer);
14856
+ options.signal?.removeEventListener("abort", abortFromCaller);
14857
+ }
14858
+ }
14859
+
14789
14860
  // src/browser-llm-connections.ts
14790
14861
  var PAJA_LLM_CONNECTIONS_STORAGE_KEY = "kehto:paja:llm-connections:v1";
14791
14862
  var MAX_CONNECTIONS = 32;
@@ -15308,6 +15379,10 @@ function readForm() {
15308
15379
  const name = byId("paja-llm-name", HTMLInputElement);
15309
15380
  const baseUrl = byId("paja-llm-base-url", HTMLInputElement);
15310
15381
  const model = byId("paja-llm-model", HTMLInputElement);
15382
+ const modelField = byId("paja-llm-model-field", HTMLElement);
15383
+ const modelSelect = byId("paja-llm-model-select", HTMLSelectElement);
15384
+ const modelSelectField = byId("paja-llm-model-select-field", HTMLElement);
15385
+ const modelStatus = byId("paja-llm-model-status", HTMLElement);
15311
15386
  const examples = byId("paja-llm-model-examples", HTMLDataListElement);
15312
15387
  const credential = byId("paja-llm-credential", HTMLInputElement);
15313
15388
  const remember = byId("paja-llm-remember", HTMLInputElement);
@@ -15316,8 +15391,27 @@ function readForm() {
15316
15391
  const list = byId("paja-llm-connections", HTMLElement);
15317
15392
  const status = byId("paja-llm-status", HTMLElement);
15318
15393
  const lock = byId("paja-llm-vault-lock", HTMLButtonElement);
15319
- if (!form || !preset || !name || !baseUrl || !model || !examples || !credential || !remember || !images || !json || !list || !status || !lock) return null;
15320
- return { form, preset, name, baseUrl, model, examples, credential, remember, images, json, list, status, lock, section: byId("paja-section-llm", HTMLDetailsElement) };
15394
+ if (!form || !preset || !name || !baseUrl || !model || !modelField || !modelSelect || !modelSelectField || !modelStatus || !examples || !credential || !remember || !images || !json || !list || !status || !lock) return null;
15395
+ return {
15396
+ form,
15397
+ preset,
15398
+ name,
15399
+ baseUrl,
15400
+ model,
15401
+ modelField,
15402
+ modelSelect,
15403
+ modelSelectField,
15404
+ modelStatus,
15405
+ examples,
15406
+ credential,
15407
+ remember,
15408
+ images,
15409
+ json,
15410
+ list,
15411
+ status,
15412
+ lock,
15413
+ section: byId("paja-section-llm", HTMLDetailsElement)
15414
+ };
15321
15415
  }
15322
15416
  function installPajaLlmPanel(options) {
15323
15417
  const { store: store2, vault } = options;
@@ -15350,14 +15444,89 @@ function installPajaLlmPanel(options) {
15350
15444
  setStatus2("Wrong vault password. Try again or clear the vault to start over.", true);
15351
15445
  }
15352
15446
  }
15447
+ let modelListing = null;
15448
+ function setModelStatus(text2, state = "idle") {
15449
+ if (!elements) return;
15450
+ elements.modelStatus.textContent = text2;
15451
+ if (state === "idle") delete elements.modelStatus.dataset.state;
15452
+ else elements.modelStatus.dataset.state = state;
15453
+ }
15454
+ function setModelMode(mode) {
15455
+ if (!elements) return;
15456
+ const listing = mode === "select";
15457
+ elements.modelField.hidden = listing;
15458
+ elements.modelSelectField.hidden = !listing;
15459
+ elements.model.disabled = listing;
15460
+ elements.modelSelect.disabled = !listing;
15461
+ }
15462
+ function currentModel() {
15463
+ if (!elements) return "";
15464
+ return (elements.modelSelectField.hidden ? elements.model.value : elements.modelSelect.value).trim();
15465
+ }
15466
+ async function refreshModels() {
15467
+ if (!elements) return;
15468
+ modelListing?.abort();
15469
+ modelListing = null;
15470
+ const preset = presetById(elements.preset.value);
15471
+ const modelsPath = preset?.modelsPath;
15472
+ if (!modelsPath) {
15473
+ setModelMode("text");
15474
+ setModelStatus("");
15475
+ return;
15476
+ }
15477
+ setModelMode("select");
15478
+ elements.modelSelect.replaceChildren();
15479
+ let baseUrl;
15480
+ try {
15481
+ baseUrl = normalizePajaLlmBaseUrl(elements.baseUrl.value);
15482
+ } catch (error) {
15483
+ setModelMode("text");
15484
+ setModelStatus(error instanceof Error ? error.message : "Enter an absolute API base URL.", "error");
15485
+ return;
15486
+ }
15487
+ const controller = new AbortController();
15488
+ modelListing = controller;
15489
+ setModelStatus("Connecting\u2026", "busy");
15490
+ try {
15491
+ const models = await fetchPajaLlmModels({
15492
+ baseUrl,
15493
+ modelsPath,
15494
+ ...elements.credential.value.trim() ? { credential: elements.credential.value.trim() } : {},
15495
+ signal: controller.signal
15496
+ });
15497
+ if (modelListing !== controller || !elements) return;
15498
+ elements.modelSelect.replaceChildren(...models.map((id) => Object.assign(document.createElement("option"), { value: id, textContent: id })));
15499
+ setModelStatus(`Connected \u2713 \u2014 ${String(models.length)} model${models.length === 1 ? "" : "s"}`, "ready");
15500
+ } catch (error) {
15501
+ if (modelListing !== controller || !elements) return;
15502
+ if (error instanceof PajaLlmModelsError && error.failure === "aborted") return;
15503
+ setModelMode("text");
15504
+ setModelStatus(`${error instanceof Error ? error.message : "Could not list models."} Type the model id instead.`, "error");
15505
+ if (preset.id === "routstr") {
15506
+ const install = Object.assign(document.createElement("a"), {
15507
+ href: "https://routstr.com/routstrd",
15508
+ textContent: "Install routstrd",
15509
+ target: "_blank",
15510
+ rel: "noopener noreferrer"
15511
+ });
15512
+ elements.modelStatus.append(document.createElement("br"), install);
15513
+ }
15514
+ } finally {
15515
+ if (modelListing === controller) modelListing = null;
15516
+ }
15517
+ }
15518
+ let appliedPreset;
15353
15519
  function applyPreset() {
15354
15520
  if (!elements) return;
15355
15521
  const preset = presetById(elements.preset.value);
15356
15522
  if (!preset) return;
15523
+ if (appliedPreset && appliedPreset.id !== preset.id) elements.credential.value = "";
15524
+ if (!elements.name.value || elements.name.value === appliedPreset?.label) elements.name.value = preset.label;
15525
+ appliedPreset = preset;
15357
15526
  if (preset.baseUrl) elements.baseUrl.value = preset.baseUrl;
15358
15527
  elements.examples.replaceChildren(...preset.exampleModels.map((value) => Object.assign(document.createElement("option"), { value })));
15359
- if (!elements.name.value) elements.name.value = preset.label;
15360
15528
  elements.credential.placeholder = preset.requiresCredential ? "API key (sent only to this base URL)" : "Optional for local servers";
15529
+ void refreshModels();
15361
15530
  }
15362
15531
  function renderList() {
15363
15532
  if (!elements) return;
@@ -15403,6 +15572,11 @@ function installPajaLlmPanel(options) {
15403
15572
  const credential = elements.credential.value;
15404
15573
  const storage = credential ? elements.remember.checked ? "vault" : "session" : "none";
15405
15574
  const preset = presetById(elements.preset.value);
15575
+ const model = currentModel();
15576
+ if (!model) {
15577
+ setModelStatus("Choose or type a model id.", "error");
15578
+ return;
15579
+ }
15406
15580
  let record3;
15407
15581
  try {
15408
15582
  record3 = {
@@ -15411,7 +15585,7 @@ function installPajaLlmPanel(options) {
15411
15585
  presetId: preset?.id ?? "custom",
15412
15586
  protocol: preset?.protocol ?? "openai-chat",
15413
15587
  baseUrl: normalizePajaLlmBaseUrl(elements.baseUrl.value),
15414
- model: elements.model.value,
15588
+ model,
15415
15589
  images: elements.images.checked,
15416
15590
  json: elements.json.checked,
15417
15591
  credentialStorage: storage,
@@ -15438,6 +15612,10 @@ function installPajaLlmPanel(options) {
15438
15612
  elements.preset.replaceChildren(...LLM_PROVIDER_PRESETS.map((preset) => Object.assign(document.createElement("option"), { value: preset.id, textContent: preset.label })));
15439
15613
  applyPreset();
15440
15614
  const onPreset = () => applyPreset();
15615
+ const onBaseUrl = () => {
15616
+ if (!presetById(elements.preset.value)?.modelsPath) return;
15617
+ void refreshModels();
15618
+ };
15441
15619
  const onSubmit = (event) => {
15442
15620
  void submit(event);
15443
15621
  };
@@ -15448,10 +15626,13 @@ function installPajaLlmPanel(options) {
15448
15626
  options.onChange?.();
15449
15627
  };
15450
15628
  elements.preset.addEventListener("change", onPreset);
15629
+ elements.baseUrl.addEventListener("change", onBaseUrl);
15451
15630
  elements.form.addEventListener("submit", onSubmit);
15452
15631
  elements.lock.addEventListener("click", onLock);
15453
15632
  disposers.push(() => {
15633
+ modelListing?.abort();
15454
15634
  elements.preset.removeEventListener("change", onPreset);
15635
+ elements.baseUrl.removeEventListener("change", onBaseUrl);
15455
15636
  elements.form.removeEventListener("submit", onSubmit);
15456
15637
  elements.lock.removeEventListener("click", onLock);
15457
15638
  });