@mcowger/pi-plexus 1.3.9 → 1.4.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.
- package/dist/extension.js +77 -96
- package/package.json +5 -5
package/dist/extension.js
CHANGED
|
@@ -307,23 +307,32 @@ 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, signal) {
|
|
311
311
|
const controller = new AbortController;
|
|
312
312
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
313
|
+
const requestSignal = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal;
|
|
313
314
|
try {
|
|
314
315
|
const headers = { Accept: "application/json" };
|
|
315
316
|
if (apiKey)
|
|
316
317
|
headers.Authorization = `Bearer ${apiKey}`;
|
|
318
|
+
if (etag)
|
|
319
|
+
headers["If-None-Match"] = etag;
|
|
317
320
|
const res = await fetch(modelsUrl, {
|
|
318
321
|
headers,
|
|
319
|
-
signal:
|
|
322
|
+
signal: requestSignal
|
|
320
323
|
});
|
|
324
|
+
if (res.status === 304) {
|
|
325
|
+
return { models: [], notModified: true };
|
|
326
|
+
}
|
|
321
327
|
if (!res.ok) {
|
|
322
328
|
throw new Error(`Plexus models fetch failed: ${res.status} ${res.statusText}`);
|
|
323
329
|
}
|
|
324
330
|
const raw = await res.json();
|
|
325
|
-
|
|
331
|
+
const responseEtag = res.headers.get("etag") ?? undefined;
|
|
332
|
+
return { models: raw.data ?? [], raw, etag: responseEtag };
|
|
326
333
|
} catch (err) {
|
|
334
|
+
if (signal?.aborted)
|
|
335
|
+
throw err;
|
|
327
336
|
if (err instanceof Error && err.name === "AbortError") {
|
|
328
337
|
throw new Error(`Plexus models fetch timed out after ${timeoutMs}ms`);
|
|
329
338
|
}
|
|
@@ -464,16 +473,36 @@ function getSuppressedModels() {
|
|
|
464
473
|
}
|
|
465
474
|
|
|
466
475
|
// src/cache.ts
|
|
467
|
-
import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
|
|
476
|
+
import { mkdir as mkdir2, writeFile as writeFile2, readFile } from "fs/promises";
|
|
477
|
+
import { existsSync as existsSync2 } from "fs";
|
|
468
478
|
import { join as join2 } from "path";
|
|
469
479
|
import { getAgentDir as getAgentDir2 } from "@earendil-works/pi-coding-agent";
|
|
470
480
|
var getCacheDir = () => join2(getAgentDir2(), "extensions", "plexus");
|
|
471
481
|
var getRawResponsePath = () => join2(getCacheDir(), "plexus-models-response.json");
|
|
482
|
+
var getEtagPath = () => join2(getCacheDir(), "plexus-models-etag.txt");
|
|
472
483
|
async function writeRawResponse(data) {
|
|
473
484
|
await mkdir2(getCacheDir(), { recursive: true });
|
|
474
485
|
await writeFile2(getRawResponsePath(), `${JSON.stringify(data, null, 2)}
|
|
475
486
|
`, "utf8");
|
|
476
487
|
}
|
|
488
|
+
async function readCachedEtag() {
|
|
489
|
+
try {
|
|
490
|
+
const p = getEtagPath();
|
|
491
|
+
if (!existsSync2(p))
|
|
492
|
+
return;
|
|
493
|
+
return (await readFile(p, "utf8")).trim();
|
|
494
|
+
} catch {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
async function writeCachedEtag(etag) {
|
|
499
|
+
if (!etag)
|
|
500
|
+
return;
|
|
501
|
+
try {
|
|
502
|
+
await mkdir2(getCacheDir(), { recursive: true });
|
|
503
|
+
await writeFile2(getEtagPath(), etag, "utf8");
|
|
504
|
+
} catch {}
|
|
505
|
+
}
|
|
477
506
|
|
|
478
507
|
// src/log.ts
|
|
479
508
|
import { mkdir as mkdir3, appendFile } from "fs/promises";
|
|
@@ -553,80 +582,6 @@ function descriptorToPiModel(descriptor) {
|
|
|
553
582
|
};
|
|
554
583
|
}
|
|
555
584
|
|
|
556
|
-
// src/gemini-toolcall-id.ts
|
|
557
|
-
var GEMINI_3_PATTERN = /gemini-(?:live-)?3(?:\.\d+)?[-.]/i;
|
|
558
|
-
var GEMINI_3_ALIASES = new Set(["gemini-flash-latest", "gemini-flash-lite-latest"]);
|
|
559
|
-
function isGemini3Model(modelId) {
|
|
560
|
-
return GEMINI_3_ALIASES.has(modelId) || GEMINI_3_PATTERN.test(modelId);
|
|
561
|
-
}
|
|
562
|
-
function createGeminiToolCallIdFixer() {
|
|
563
|
-
let functionCallIds = [];
|
|
564
|
-
let functionResponseIds = [];
|
|
565
|
-
function onContext(messages) {
|
|
566
|
-
const calls = [];
|
|
567
|
-
const responses = [];
|
|
568
|
-
for (const msg of messages) {
|
|
569
|
-
if (msg.role === "assistant" && Array.isArray(msg.content)) {
|
|
570
|
-
for (const block of msg.content) {
|
|
571
|
-
if (block?.type === "toolCall" && typeof block.id === "string") {
|
|
572
|
-
calls.push(block.id);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
} else if (msg.role === "toolResult" && typeof msg.toolCallId === "string") {
|
|
576
|
-
responses.push({ id: msg.toolCallId, name: msg.toolName ?? "" });
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
functionCallIds = calls;
|
|
580
|
-
functionResponseIds = responses;
|
|
581
|
-
}
|
|
582
|
-
function onBeforeProviderRequest(payload) {
|
|
583
|
-
const google = payload;
|
|
584
|
-
if (!google || typeof google !== "object" || !Array.isArray(google.contents)) {
|
|
585
|
-
return payload;
|
|
586
|
-
}
|
|
587
|
-
if (typeof google.model !== "string" || !isGemini3Model(google.model)) {
|
|
588
|
-
return payload;
|
|
589
|
-
}
|
|
590
|
-
let callCursor = 0;
|
|
591
|
-
let responseCursor = 0;
|
|
592
|
-
let missing = 0;
|
|
593
|
-
for (const content of google.contents) {
|
|
594
|
-
if (!Array.isArray(content?.parts))
|
|
595
|
-
continue;
|
|
596
|
-
for (const part of content.parts) {
|
|
597
|
-
if (part.functionCall && part.functionCall.id === undefined) {
|
|
598
|
-
const id = functionCallIds[callCursor++];
|
|
599
|
-
if (id !== undefined) {
|
|
600
|
-
part.functionCall.id = id;
|
|
601
|
-
} else {
|
|
602
|
-
missing++;
|
|
603
|
-
}
|
|
604
|
-
} else if (part.functionResponse && part.functionResponse.id === undefined) {
|
|
605
|
-
const entry = functionResponseIds[responseCursor++];
|
|
606
|
-
if (entry !== undefined) {
|
|
607
|
-
part.functionResponse.id = entry.id;
|
|
608
|
-
if (part.functionResponse.name === undefined && entry.name) {
|
|
609
|
-
part.functionResponse.name = entry.name;
|
|
610
|
-
}
|
|
611
|
-
} else {
|
|
612
|
-
missing++;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
if (missing > 0) {
|
|
618
|
-
log("gemini-toolcall-id: fewer captured ids than parts", {
|
|
619
|
-
model: google.model,
|
|
620
|
-
missing,
|
|
621
|
-
capturedCalls: functionCallIds.length,
|
|
622
|
-
capturedResponses: functionResponseIds.length
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
return payload;
|
|
626
|
-
}
|
|
627
|
-
return { onContext, onBeforeProviderRequest };
|
|
628
|
-
}
|
|
629
|
-
|
|
630
585
|
// src/gemini-malformed-retry.ts
|
|
631
586
|
var MALFORMED_LEAK_PATTERN = /(?:print\()?call:\s*default_api[.:]|default_api\.\w+\s*\(/;
|
|
632
587
|
var MALFORMED_DIAGNOSTIC_PATTERN = /\bmalformed[\s_-]?function[\s_-]?call\b/i;
|
|
@@ -674,13 +629,6 @@ function plexusExtension(pi) {
|
|
|
674
629
|
const envApiKey = getEnvApiKey();
|
|
675
630
|
const startupBaseUrl = getBaseUrl();
|
|
676
631
|
log("startup", { baseUrl: startupBaseUrl, hasEnvApiKey: !!envApiKey });
|
|
677
|
-
const geminiToolCallIdFixer = createGeminiToolCallIdFixer();
|
|
678
|
-
pi.on("context", (event) => {
|
|
679
|
-
geminiToolCallIdFixer.onContext(event.messages);
|
|
680
|
-
});
|
|
681
|
-
pi.on("before_provider_request", (event) => {
|
|
682
|
-
return geminiToolCallIdFixer.onBeforeProviderRequest(event.payload);
|
|
683
|
-
});
|
|
684
632
|
pi.on("message_end", (event) => normalizeMalformedFunctionCall(event.message, PROVIDER_NAME));
|
|
685
633
|
pi.registerProvider(PROVIDER_NAME, {
|
|
686
634
|
api: "openai-completions",
|
|
@@ -735,23 +683,42 @@ async function refreshPlexusModels(context) {
|
|
|
735
683
|
if (!context.allowNetwork || !apiKey || !modelsUrl || !baseUrl) {
|
|
736
684
|
if (currentModels.length > 0) {
|
|
737
685
|
const filteredCurrent = currentModels.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppress));
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
686
|
+
await context.publish({
|
|
687
|
+
persist: { models: filteredCurrent, checkedAt: Date.now() },
|
|
688
|
+
update: () => {
|
|
689
|
+
currentModels = filteredCurrent;
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
return filteredCurrent;
|
|
741
693
|
}
|
|
742
694
|
const restored = await restoreStoredModels(context);
|
|
743
695
|
if (restored)
|
|
744
696
|
return restored;
|
|
745
697
|
throw new Error(!modelsUrl || !baseUrl ? "Plexus base URL not configured. Run /login plexus first." : "No Plexus API key configured. Run /login plexus first.");
|
|
746
698
|
}
|
|
699
|
+
const storedEtag = await readCachedEtag();
|
|
747
700
|
try {
|
|
748
|
-
const { models: apiModels, raw } = await fetchPlexusModels(apiKey, modelsUrl);
|
|
701
|
+
const { models: apiModels, raw, etag, notModified } = await fetchPlexusModels(apiKey, modelsUrl, undefined, storedEtag, context.signal);
|
|
702
|
+
if (notModified) {
|
|
703
|
+
log("refreshModels: not modified", { etag: storedEtag });
|
|
704
|
+
const restored = await restoreStoredModels(context);
|
|
705
|
+
if (restored)
|
|
706
|
+
return restored;
|
|
707
|
+
}
|
|
749
708
|
const piModels = convertDescriptors(apiModels, baseUrl, suppress).map(descriptorToPiModel);
|
|
709
|
+
const published = await context.publish({
|
|
710
|
+
persist: { models: piModels, checkedAt: Date.now() },
|
|
711
|
+
update: () => {
|
|
712
|
+
currentModels = piModels;
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
if (!published) {
|
|
716
|
+
log("refreshModels: publication superseded by a newer refresh", {});
|
|
717
|
+
}
|
|
750
718
|
await Promise.all([
|
|
751
|
-
|
|
752
|
-
writeRawResponse(raw)
|
|
719
|
+
writeCachedEtag(etag),
|
|
720
|
+
raw ? writeRawResponse(raw) : Promise.resolve()
|
|
753
721
|
]);
|
|
754
|
-
currentModels = piModels;
|
|
755
722
|
log("refreshModels: fetched", { count: piModels.length });
|
|
756
723
|
return piModels;
|
|
757
724
|
} catch (error) {
|
|
@@ -760,12 +727,16 @@ async function refreshPlexusModels(context) {
|
|
|
760
727
|
}
|
|
761
728
|
}
|
|
762
729
|
async function restoreStoredModels(context) {
|
|
763
|
-
const stored =
|
|
730
|
+
const stored = context.stored;
|
|
764
731
|
if (!stored || stored.models.length === 0)
|
|
765
732
|
return;
|
|
766
733
|
const suppress = getSuppressedModels();
|
|
767
734
|
const models = stored.models.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppress));
|
|
768
|
-
|
|
735
|
+
await context.publish({
|
|
736
|
+
update: () => {
|
|
737
|
+
currentModels = models;
|
|
738
|
+
}
|
|
739
|
+
});
|
|
769
740
|
log("refreshModels: restored from store", { count: models.length });
|
|
770
741
|
return models;
|
|
771
742
|
}
|
|
@@ -797,7 +768,8 @@ function createPlexusLoginProvider() {
|
|
|
797
768
|
plexusBaseUrl: baseUrl
|
|
798
769
|
};
|
|
799
770
|
},
|
|
800
|
-
async refreshToken(credentials) {
|
|
771
|
+
async refreshToken(credentials, signal) {
|
|
772
|
+
signal.throwIfAborted();
|
|
801
773
|
return { ...credentials, expires: PLEXUS_CREDENTIAL_EXPIRES_AT };
|
|
802
774
|
},
|
|
803
775
|
getApiKey(credentials) {
|
|
@@ -819,7 +791,16 @@ async function handleRefresh(ctx) {
|
|
|
819
791
|
return;
|
|
820
792
|
}
|
|
821
793
|
ctx.ui.notify("Refreshing Plexus models\u2026", "info");
|
|
822
|
-
await ctx.modelRegistry.refresh();
|
|
794
|
+
const result = await ctx.modelRegistry.refresh({ providers: [PROVIDER_NAME], force: true });
|
|
795
|
+
if (result.aborted) {
|
|
796
|
+
ctx.ui.notify("Plexus model refresh was cancelled.", "warning");
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
const refreshError = result.errors.get(PROVIDER_NAME);
|
|
800
|
+
if (refreshError) {
|
|
801
|
+
ctx.ui.notify(`Plexus model refresh failed: ${refreshError.message}`, "error");
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
823
804
|
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");
|
|
824
805
|
}
|
|
825
806
|
async function handleSetDefaultModel(pi, ctx, requestedModelId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcowger/pi-plexus",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
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.
|
|
29
|
-
"@earendil-works/pi-coding-agent": ">=0.
|
|
28
|
+
"@earendil-works/pi-ai": ">=0.84.0",
|
|
29
|
+
"@earendil-works/pi-coding-agent": ">=0.84.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@earendil-works/pi-ai": "^0.
|
|
33
|
-
"@earendil-works/pi-coding-agent": "^0.
|
|
32
|
+
"@earendil-works/pi-ai": "^0.84.0",
|
|
33
|
+
"@earendil-works/pi-coding-agent": "^0.84.0",
|
|
34
34
|
"@types/bun": "latest"
|
|
35
35
|
},
|
|
36
36
|
"repository": {
|