@mcowger/pi-plexus 1.4.0 → 1.4.1
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/dist/extension.js +98 -4
- package/package.json +1 -1
package/dist/extension.js
CHANGED
|
@@ -474,12 +474,88 @@ function getSuppressedModels() {
|
|
|
474
474
|
|
|
475
475
|
// src/cache.ts
|
|
476
476
|
import { mkdir as mkdir2, writeFile as writeFile2, readFile } from "fs/promises";
|
|
477
|
-
import { existsSync as existsSync2 } from "fs";
|
|
477
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
478
478
|
import { join as join2 } from "path";
|
|
479
479
|
import { getAgentDir as getAgentDir2 } from "@earendil-works/pi-coding-agent";
|
|
480
480
|
var getCacheDir = () => join2(getAgentDir2(), "extensions", "plexus");
|
|
481
|
+
var getModelsCachePath = () => join2(getCacheDir(), "plexus-models-cache.json");
|
|
481
482
|
var getRawResponsePath = () => join2(getCacheDir(), "plexus-models-response.json");
|
|
482
483
|
var getEtagPath = () => join2(getCacheDir(), "plexus-models-etag.txt");
|
|
484
|
+
var getModelsStorePath = () => join2(getAgentDir2(), "models-store.json");
|
|
485
|
+
function parseCacheData(raw) {
|
|
486
|
+
try {
|
|
487
|
+
const parsed = JSON.parse(raw);
|
|
488
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
489
|
+
return null;
|
|
490
|
+
const obj = parsed;
|
|
491
|
+
if (!Array.isArray(obj["models"]))
|
|
492
|
+
return null;
|
|
493
|
+
return {
|
|
494
|
+
models: obj["models"],
|
|
495
|
+
timestamp: typeof obj["timestamp"] === "number" ? obj["timestamp"] : 0,
|
|
496
|
+
etag: typeof obj["etag"] === "string" ? obj["etag"] : undefined
|
|
497
|
+
};
|
|
498
|
+
} catch {
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function parseModelsStoreData(raw) {
|
|
503
|
+
try {
|
|
504
|
+
const parsed = JSON.parse(raw);
|
|
505
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
506
|
+
return null;
|
|
507
|
+
const obj = parsed;
|
|
508
|
+
const plexusObj = obj["plexus"];
|
|
509
|
+
if (!plexusObj)
|
|
510
|
+
return null;
|
|
511
|
+
let modelsList = null;
|
|
512
|
+
let checkedAt = 0;
|
|
513
|
+
if (Array.isArray(plexusObj)) {
|
|
514
|
+
modelsList = plexusObj;
|
|
515
|
+
} else if (typeof plexusObj === "object" && plexusObj !== null) {
|
|
516
|
+
const pObj = plexusObj;
|
|
517
|
+
if (Array.isArray(pObj["models"])) {
|
|
518
|
+
modelsList = pObj["models"];
|
|
519
|
+
}
|
|
520
|
+
if (typeof pObj["checkedAt"] === "number") {
|
|
521
|
+
checkedAt = pObj["checkedAt"];
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (!modelsList)
|
|
525
|
+
return null;
|
|
526
|
+
return {
|
|
527
|
+
piModels: modelsList,
|
|
528
|
+
timestamp: checkedAt
|
|
529
|
+
};
|
|
530
|
+
} catch {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
function readCachedModelsSync() {
|
|
535
|
+
try {
|
|
536
|
+
const cachePath = getModelsCachePath();
|
|
537
|
+
if (existsSync2(cachePath)) {
|
|
538
|
+
const cache = parseCacheData(readFileSync2(cachePath, "utf8"));
|
|
539
|
+
if (cache)
|
|
540
|
+
return cache;
|
|
541
|
+
}
|
|
542
|
+
const storePath = getModelsStorePath();
|
|
543
|
+
if (existsSync2(storePath)) {
|
|
544
|
+
const storeCache = parseModelsStoreData(readFileSync2(storePath, "utf8"));
|
|
545
|
+
if (storeCache)
|
|
546
|
+
return storeCache;
|
|
547
|
+
}
|
|
548
|
+
return null;
|
|
549
|
+
} catch {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
async function writeCachedModels(models, etag) {
|
|
554
|
+
await mkdir2(getCacheDir(), { recursive: true });
|
|
555
|
+
const payload = { models, timestamp: Date.now(), etag };
|
|
556
|
+
await writeFile2(getModelsCachePath(), `${JSON.stringify(payload, null, 2)}
|
|
557
|
+
`, "utf8");
|
|
558
|
+
}
|
|
483
559
|
async function writeRawResponse(data) {
|
|
484
560
|
await mkdir2(getCacheDir(), { recursive: true });
|
|
485
561
|
await writeFile2(getRawResponsePath(), `${JSON.stringify(data, null, 2)}
|
|
@@ -487,6 +563,9 @@ async function writeRawResponse(data) {
|
|
|
487
563
|
}
|
|
488
564
|
async function readCachedEtag() {
|
|
489
565
|
try {
|
|
566
|
+
const cache = readCachedModelsSync();
|
|
567
|
+
if (cache?.etag)
|
|
568
|
+
return cache.etag;
|
|
490
569
|
const p = getEtagPath();
|
|
491
570
|
if (!existsSync2(p))
|
|
492
571
|
return;
|
|
@@ -628,14 +707,27 @@ var currentModels = [];
|
|
|
628
707
|
function plexusExtension(pi) {
|
|
629
708
|
const envApiKey = getEnvApiKey();
|
|
630
709
|
const startupBaseUrl = getBaseUrl();
|
|
631
|
-
|
|
710
|
+
const suppressPatterns = getSuppressedModels();
|
|
711
|
+
const cached = readCachedModelsSync();
|
|
712
|
+
let startupModels = [];
|
|
713
|
+
if (cached?.models && cached.models.length > 0) {
|
|
714
|
+
startupModels = convertDescriptors(cached.models, startupBaseUrl ?? PLACEHOLDER_BASE_URL, suppressPatterns).map(descriptorToPiModel);
|
|
715
|
+
} else if (cached?.piModels && cached.piModels.length > 0) {
|
|
716
|
+
startupModels = cached.piModels.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppressPatterns));
|
|
717
|
+
}
|
|
718
|
+
currentModels = startupModels;
|
|
719
|
+
log("startup", {
|
|
720
|
+
baseUrl: startupBaseUrl,
|
|
721
|
+
hasEnvApiKey: !!envApiKey,
|
|
722
|
+
cachedModelCount: startupModels.length
|
|
723
|
+
});
|
|
632
724
|
pi.on("message_end", (event) => normalizeMalformedFunctionCall(event.message, PROVIDER_NAME));
|
|
633
725
|
pi.registerProvider(PROVIDER_NAME, {
|
|
634
726
|
api: "openai-completions",
|
|
635
727
|
...envApiKey ? { apiKey: PROVIDER_API_KEY_TEMPLATE } : {},
|
|
636
728
|
authHeader: true,
|
|
637
729
|
baseUrl: startupBaseUrl ?? PLACEHOLDER_BASE_URL,
|
|
638
|
-
models:
|
|
730
|
+
models: startupModels,
|
|
639
731
|
refreshModels: refreshPlexusModels,
|
|
640
732
|
oauth: createPlexusLoginProvider()
|
|
641
733
|
});
|
|
@@ -705,7 +797,8 @@ async function refreshPlexusModels(context) {
|
|
|
705
797
|
if (restored)
|
|
706
798
|
return restored;
|
|
707
799
|
}
|
|
708
|
-
const
|
|
800
|
+
const descriptors = convertDescriptors(apiModels, baseUrl, suppress);
|
|
801
|
+
const piModels = descriptors.map(descriptorToPiModel);
|
|
709
802
|
const published = await context.publish({
|
|
710
803
|
persist: { models: piModels, checkedAt: Date.now() },
|
|
711
804
|
update: () => {
|
|
@@ -716,6 +809,7 @@ async function refreshPlexusModels(context) {
|
|
|
716
809
|
log("refreshModels: publication superseded by a newer refresh", {});
|
|
717
810
|
}
|
|
718
811
|
await Promise.all([
|
|
812
|
+
writeCachedModels(descriptors, etag),
|
|
719
813
|
writeCachedEtag(etag),
|
|
720
814
|
raw ? writeRawResponse(raw) : Promise.resolve()
|
|
721
815
|
]);
|