@mcowger/pi-plexus 1.3.9 → 1.3.10
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 +39 -5
- package/package.json +1 -1
package/dist/extension.js
CHANGED
|
@@ -307,22 +307,28 @@ function detectOpenAICompletionsCompat(providerName, baseUrl) {
|
|
|
307
307
|
return compat;
|
|
308
308
|
}
|
|
309
309
|
var DEFAULT_MODELS_FETCH_TIMEOUT_MS = 1e4;
|
|
310
|
-
async function fetchPlexusModels(apiKey, modelsUrl, timeoutMs = DEFAULT_MODELS_FETCH_TIMEOUT_MS) {
|
|
310
|
+
async function fetchPlexusModels(apiKey, modelsUrl, timeoutMs = DEFAULT_MODELS_FETCH_TIMEOUT_MS, etag) {
|
|
311
311
|
const controller = new AbortController;
|
|
312
312
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
313
313
|
try {
|
|
314
314
|
const headers = { Accept: "application/json" };
|
|
315
315
|
if (apiKey)
|
|
316
316
|
headers.Authorization = `Bearer ${apiKey}`;
|
|
317
|
+
if (etag)
|
|
318
|
+
headers["If-None-Match"] = etag;
|
|
317
319
|
const res = await fetch(modelsUrl, {
|
|
318
320
|
headers,
|
|
319
321
|
signal: controller.signal
|
|
320
322
|
});
|
|
323
|
+
if (res.status === 304) {
|
|
324
|
+
return { models: [], notModified: true };
|
|
325
|
+
}
|
|
321
326
|
if (!res.ok) {
|
|
322
327
|
throw new Error(`Plexus models fetch failed: ${res.status} ${res.statusText}`);
|
|
323
328
|
}
|
|
324
329
|
const raw = await res.json();
|
|
325
|
-
|
|
330
|
+
const responseEtag = res.headers.get("etag") ?? undefined;
|
|
331
|
+
return { models: raw.data ?? [], raw, etag: responseEtag };
|
|
326
332
|
} catch (err) {
|
|
327
333
|
if (err instanceof Error && err.name === "AbortError") {
|
|
328
334
|
throw new Error(`Plexus models fetch timed out after ${timeoutMs}ms`);
|
|
@@ -464,16 +470,36 @@ function getSuppressedModels() {
|
|
|
464
470
|
}
|
|
465
471
|
|
|
466
472
|
// src/cache.ts
|
|
467
|
-
import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
|
|
473
|
+
import { mkdir as mkdir2, writeFile as writeFile2, readFile } from "fs/promises";
|
|
474
|
+
import { existsSync as existsSync2 } from "fs";
|
|
468
475
|
import { join as join2 } from "path";
|
|
469
476
|
import { getAgentDir as getAgentDir2 } from "@earendil-works/pi-coding-agent";
|
|
470
477
|
var getCacheDir = () => join2(getAgentDir2(), "extensions", "plexus");
|
|
471
478
|
var getRawResponsePath = () => join2(getCacheDir(), "plexus-models-response.json");
|
|
479
|
+
var getEtagPath = () => join2(getCacheDir(), "plexus-models-etag.txt");
|
|
472
480
|
async function writeRawResponse(data) {
|
|
473
481
|
await mkdir2(getCacheDir(), { recursive: true });
|
|
474
482
|
await writeFile2(getRawResponsePath(), `${JSON.stringify(data, null, 2)}
|
|
475
483
|
`, "utf8");
|
|
476
484
|
}
|
|
485
|
+
async function readCachedEtag() {
|
|
486
|
+
try {
|
|
487
|
+
const p = getEtagPath();
|
|
488
|
+
if (!existsSync2(p))
|
|
489
|
+
return;
|
|
490
|
+
return (await readFile(p, "utf8")).trim();
|
|
491
|
+
} catch {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
async function writeCachedEtag(etag) {
|
|
496
|
+
if (!etag)
|
|
497
|
+
return;
|
|
498
|
+
try {
|
|
499
|
+
await mkdir2(getCacheDir(), { recursive: true });
|
|
500
|
+
await writeFile2(getEtagPath(), etag, "utf8");
|
|
501
|
+
} catch {}
|
|
502
|
+
}
|
|
477
503
|
|
|
478
504
|
// src/log.ts
|
|
479
505
|
import { mkdir as mkdir3, appendFile } from "fs/promises";
|
|
@@ -732,6 +758,7 @@ async function refreshPlexusModels(context) {
|
|
|
732
758
|
const modelsUrl = getModelsUrl();
|
|
733
759
|
const apiKey = credentialApiKey(context.credential) ?? getEnvApiKey() ?? undefined;
|
|
734
760
|
const suppress = getSuppressedModels();
|
|
761
|
+
const storedEtag = await readCachedEtag();
|
|
735
762
|
if (!context.allowNetwork || !apiKey || !modelsUrl || !baseUrl) {
|
|
736
763
|
if (currentModels.length > 0) {
|
|
737
764
|
const filteredCurrent = currentModels.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppress));
|
|
@@ -745,11 +772,18 @@ async function refreshPlexusModels(context) {
|
|
|
745
772
|
throw new Error(!modelsUrl || !baseUrl ? "Plexus base URL not configured. Run /login plexus first." : "No Plexus API key configured. Run /login plexus first.");
|
|
746
773
|
}
|
|
747
774
|
try {
|
|
748
|
-
const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
|
|
775
|
+
const { models: apiModels, raw, etag, notModified } = await fetchPlexusModels(apiKey, modelsUrl, undefined, storedEtag);
|
|
776
|
+
if (notModified) {
|
|
777
|
+
log("refreshModels: not modified", { etag: storedEtag });
|
|
778
|
+
const restored = await restoreStoredModels(context);
|
|
779
|
+
if (restored)
|
|
780
|
+
return restored;
|
|
781
|
+
}
|
|
749
782
|
const piModels = convertDescriptors(apiModels, baseUrl, suppress).map(descriptorToPiModel);
|
|
750
783
|
await Promise.all([
|
|
751
784
|
context.store.write({ models: piModels, checkedAt: Date.now() }),
|
|
752
|
-
|
|
785
|
+
writeCachedEtag(etag),
|
|
786
|
+
raw ? writeRawResponse(raw) : Promise.resolve()
|
|
753
787
|
]);
|
|
754
788
|
currentModels = piModels;
|
|
755
789
|
log("refreshModels: fetched", { count: piModels.length });
|