@mcowger/pi-plexus 1.3.3 → 1.3.5

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.
package/README.md CHANGED
@@ -13,6 +13,7 @@ Exposes models from a self-hosted [Plexus](https://github.com/mcowger/plexus) AI
13
13
  ## Prerequisites
14
14
 
15
15
  - A running Plexus instance
16
+ - pi 0.81.0 or later (for `plexus-pi`)
16
17
 
17
18
  ## Installation
18
19
 
package/dist/extension.js CHANGED
@@ -352,45 +352,11 @@ function getBaseUrl() {
352
352
  }
353
353
 
354
354
  // src/cache.ts
355
- import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
356
355
  import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
357
356
  import { join as join2 } from "path";
358
357
  import { getAgentDir as getAgentDir2 } from "@earendil-works/pi-coding-agent";
359
358
  var getCacheDir = () => join2(getAgentDir2(), "extensions", "plexus");
360
- var getModelsCachePath = () => join2(getCacheDir(), "plexus-models-cache.json");
361
359
  var getRawResponsePath = () => join2(getCacheDir(), "plexus-models-response.json");
362
- function parseCacheData(raw) {
363
- try {
364
- const parsed = JSON.parse(raw);
365
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
366
- return null;
367
- const obj = parsed;
368
- if (!Array.isArray(obj["models"]))
369
- return null;
370
- return {
371
- models: obj["models"],
372
- timestamp: typeof obj["timestamp"] === "number" ? obj["timestamp"] : 0
373
- };
374
- } catch {
375
- return null;
376
- }
377
- }
378
- function readCachedModelsSync() {
379
- try {
380
- const p = getModelsCachePath();
381
- if (!existsSync2(p))
382
- return null;
383
- return parseCacheData(readFileSync2(p, "utf8"));
384
- } catch {
385
- return null;
386
- }
387
- }
388
- async function writeCachedModels(models) {
389
- await mkdir2(getCacheDir(), { recursive: true });
390
- const payload = { models, timestamp: Date.now() };
391
- await writeFile2(getModelsCachePath(), `${JSON.stringify(payload, null, 2)}
392
- `, "utf8");
393
- }
394
360
  async function writeRawResponse(data) {
395
361
  await mkdir2(getCacheDir(), { recursive: true });
396
362
  await writeFile2(getRawResponsePath(), `${JSON.stringify(data, null, 2)}
@@ -479,33 +445,20 @@ function descriptorToPiModel(descriptor) {
479
445
  var PROVIDER_NAME = "plexus";
480
446
  var PROVIDER_API_KEY_TEMPLATE = "${PLEXUS_API_KEY}";
481
447
  var PLEXUS_CREDENTIAL_EXPIRES_AT = 253402300799000;
448
+ var PLACEHOLDER_BASE_URL = "http://localhost/v1";
482
449
  var currentModels = [];
483
450
  function plexusExtension(pi) {
484
- const cached = readCachedModelsSync();
485
- const startupBaseUrl = getBaseUrl() ?? "http://localhost/v1";
486
- const startupModels = cached?.models.map(descriptorToPiModel) ?? [];
487
- log("startup", {
488
- cachedModelCount: startupModels.length,
489
- startupBaseUrl
490
- });
451
+ const envApiKey = getEnvApiKey();
452
+ const startupBaseUrl = getBaseUrl();
453
+ log("startup", { baseUrl: startupBaseUrl, hasEnvApiKey: !!envApiKey });
491
454
  pi.registerProvider(PROVIDER_NAME, {
492
455
  api: "openai-completions",
493
- apiKey: PROVIDER_API_KEY_TEMPLATE,
456
+ ...envApiKey ? { apiKey: PROVIDER_API_KEY_TEMPLATE } : {},
494
457
  authHeader: true,
495
- baseUrl: startupBaseUrl,
496
- models: startupModels,
497
- oauth: createPlexusLoginProvider(pi)
498
- });
499
- currentModels = startupModels;
500
- pi.on("session_start", async (_event, ctx) => {
501
- const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME) ?? getEnvApiKey();
502
- const baseUrl = getBaseUrl();
503
- log("session_start", { hasApiKey: !!apiKey, baseUrl });
504
- if (!apiKey || !baseUrl) {
505
- log("session_start: no auth configured, skipping refresh");
506
- return;
507
- }
508
- await doRefresh(pi, apiKey, ctx);
458
+ baseUrl: startupBaseUrl ?? PLACEHOLDER_BASE_URL,
459
+ models: [],
460
+ refreshModels: refreshPlexusModels,
461
+ oauth: createPlexusLoginProvider()
509
462
  });
510
463
  pi.registerCommand("plexus", {
511
464
  description: "Plexus provider commands: refresh, set-default-model (setup: /login plexus)",
@@ -532,7 +485,7 @@ function plexusExtension(pi) {
532
485
  const trimmed = args.trim();
533
486
  const sub = trimmed.toLowerCase();
534
487
  if (sub === "refresh" || sub === "") {
535
- await handleRefresh(pi, ctx);
488
+ await handleRefresh(ctx);
536
489
  return;
537
490
  }
538
491
  if (sub === "set-default-model" || sub.startsWith("set-default-model ")) {
@@ -543,7 +496,52 @@ function plexusExtension(pi) {
543
496
  }
544
497
  });
545
498
  }
546
- function createPlexusLoginProvider(pi) {
499
+ async function refreshPlexusModels(context) {
500
+ const baseUrl = getBaseUrl();
501
+ const modelsUrl = getModelsUrl();
502
+ const apiKey = credentialApiKey(context.credential) ?? getEnvApiKey() ?? undefined;
503
+ if (!context.allowNetwork || !apiKey || !modelsUrl || !baseUrl) {
504
+ if (currentModels.length > 0) {
505
+ await context.store.write({ models: currentModels, checkedAt: Date.now() });
506
+ return currentModels;
507
+ }
508
+ const restored = await restoreStoredModels(context);
509
+ if (restored)
510
+ return restored;
511
+ throw new Error(!modelsUrl || !baseUrl ? "Plexus base URL not configured. Run /login plexus first." : "No Plexus API key configured. Run /login plexus first.");
512
+ }
513
+ try {
514
+ const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
515
+ const piModels = convertDescriptors(apiModels, baseUrl).map(descriptorToPiModel);
516
+ await Promise.all([
517
+ context.store.write({ models: piModels, checkedAt: Date.now() }),
518
+ writeRawResponse(raw)
519
+ ]);
520
+ currentModels = piModels;
521
+ log("refreshModels: fetched", { count: piModels.length });
522
+ return piModels;
523
+ } catch (error) {
524
+ log("refreshModels: fetch failed", { error: String(error) });
525
+ throw error;
526
+ }
527
+ }
528
+ async function restoreStoredModels(context) {
529
+ const stored = await context.store.read();
530
+ if (!stored || stored.models.length === 0)
531
+ return;
532
+ const models = stored.models;
533
+ currentModels = models;
534
+ log("refreshModels: restored from store", { count: models.length });
535
+ return models;
536
+ }
537
+ function credentialApiKey(credential) {
538
+ if (!credential)
539
+ return;
540
+ if (credential.type === "api_key")
541
+ return credential.key || undefined;
542
+ return String(credential.access || credential.refresh || "") || undefined;
543
+ }
544
+ function createPlexusLoginProvider() {
547
545
  return {
548
546
  name: "Plexus",
549
547
  async login(callbacks) {
@@ -557,8 +555,6 @@ function createPlexusLoginProvider(pi) {
557
555
  if (!apiKey)
558
556
  throw new Error("Plexus API key is required.");
559
557
  await saveBaseUrl(baseUrl);
560
- callbacks.onProgress?.("Refreshing Plexus models...");
561
- await doRefresh(pi, apiKey, null);
562
558
  return {
563
559
  access: apiKey,
564
560
  refresh: apiKey,
@@ -581,14 +577,15 @@ function createPlexusLoginProvider(pi) {
581
577
  }
582
578
  };
583
579
  }
584
- async function handleRefresh(pi, ctx) {
585
- const apiKey = await ctx.modelRegistry.authStorage.getApiKey(PROVIDER_NAME) ?? getEnvApiKey();
580
+ async function handleRefresh(ctx) {
581
+ const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_NAME);
586
582
  if (!apiKey) {
587
583
  ctx.ui.notify("No Plexus API key configured. Run /login plexus first.", "error");
588
584
  return;
589
585
  }
590
586
  ctx.ui.notify("Refreshing Plexus models\u2026", "info");
591
- await doRefresh(pi, apiKey, ctx);
587
+ await ctx.modelRegistry.refresh();
588
+ ctx.ui.notify(currentModels.length > 0 ? `Refreshed ${currentModels.length} Plexus models` : "Refresh finished but no Plexus models are available. Check the Plexus server and /login plexus.", currentModels.length > 0 ? "info" : "warning");
592
589
  }
593
590
  async function handleSetDefaultModel(pi, ctx, requestedModelId) {
594
591
  let modelId = requestedModelId;
@@ -614,39 +611,6 @@ async function handleSetDefaultModel(pi, ctx, requestedModelId) {
614
611
  const active = await pi.setModel(registryModel);
615
612
  ctx.ui.notify(active ? `Plexus model selected: ${model.id}.` : `Plexus model ${model.id} was saved but could not be selected in this session.`, active ? "info" : "warning");
616
613
  }
617
- async function doRefresh(pi, apiKey, ctx) {
618
- const modelsUrl = getModelsUrl();
619
- const baseUrl = getBaseUrl();
620
- if (!modelsUrl || !baseUrl) {
621
- if (ctx)
622
- ctx.ui.notify("Plexus base URL not configured. Run /login plexus first.", "warning");
623
- log("doRefresh: no base URL configured");
624
- return;
625
- }
626
- try {
627
- const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
628
- const descriptors = convertDescriptors(apiModels, baseUrl);
629
- const piModels = descriptors.map(descriptorToPiModel);
630
- await Promise.all([writeCachedModels(descriptors), writeRawResponse(raw)]);
631
- currentModels = piModels;
632
- pi.registerProvider(PROVIDER_NAME, {
633
- api: "openai-completions",
634
- apiKey: PROVIDER_API_KEY_TEMPLATE,
635
- authHeader: true,
636
- baseUrl,
637
- models: piModels,
638
- oauth: createPlexusLoginProvider(pi)
639
- });
640
- log("doRefresh: registered", { count: piModels.length });
641
- if (ctx)
642
- ctx.ui.notify(`Refreshed ${piModels.length} Plexus models`, "info");
643
- } catch (error) {
644
- log("doRefresh: failed", { error: String(error) });
645
- if (ctx) {
646
- ctx.ui.notify(`Refresh failed: ${error instanceof Error ? error.message : String(error)}`, "error");
647
- }
648
- }
649
- }
650
614
  export {
651
615
  plexusExtension as default
652
616
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcowger/pi-plexus",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Plexus AI proxy plugin for the pi coding agent",
5
5
  "type": "module",
6
6
  "files": [
@@ -25,12 +25,12 @@
25
25
  "build": "bun run build.ts"
26
26
  },
27
27
  "peerDependencies": {
28
- "@earendil-works/pi-ai": ">=0.80.6",
29
- "@earendil-works/pi-coding-agent": ">=0.80.6"
28
+ "@earendil-works/pi-ai": ">=0.81.0",
29
+ "@earendil-works/pi-coding-agent": ">=0.81.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@earendil-works/pi-ai": "^0.80.6",
33
- "@earendil-works/pi-coding-agent": "^0.80.6",
32
+ "@earendil-works/pi-ai": "^0.81.0",
33
+ "@earendil-works/pi-coding-agent": "^0.81.0",
34
34
  "@types/bun": "latest"
35
35
  },
36
36
  "repository": {