@mcowger/pi-plexus 1.3.10 → 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.
Files changed (2) hide show
  1. package/dist/extension.js +41 -94
  2. package/package.json +5 -5
package/dist/extension.js CHANGED
@@ -307,9 +307,10 @@ 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, etag) {
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)
@@ -318,7 +319,7 @@ async function fetchPlexusModels(apiKey, modelsUrl, timeoutMs = DEFAULT_MODELS_F
318
319
  headers["If-None-Match"] = etag;
319
320
  const res = await fetch(modelsUrl, {
320
321
  headers,
321
- signal: controller.signal
322
+ signal: requestSignal
322
323
  });
323
324
  if (res.status === 304) {
324
325
  return { models: [], notModified: true };
@@ -330,6 +331,8 @@ async function fetchPlexusModels(apiKey, modelsUrl, timeoutMs = DEFAULT_MODELS_F
330
331
  const responseEtag = res.headers.get("etag") ?? undefined;
331
332
  return { models: raw.data ?? [], raw, etag: responseEtag };
332
333
  } catch (err) {
334
+ if (signal?.aborted)
335
+ throw err;
333
336
  if (err instanceof Error && err.name === "AbortError") {
334
337
  throw new Error(`Plexus models fetch timed out after ${timeoutMs}ms`);
335
338
  }
@@ -579,80 +582,6 @@ function descriptorToPiModel(descriptor) {
579
582
  };
580
583
  }
581
584
 
582
- // src/gemini-toolcall-id.ts
583
- var GEMINI_3_PATTERN = /gemini-(?:live-)?3(?:\.\d+)?[-.]/i;
584
- var GEMINI_3_ALIASES = new Set(["gemini-flash-latest", "gemini-flash-lite-latest"]);
585
- function isGemini3Model(modelId) {
586
- return GEMINI_3_ALIASES.has(modelId) || GEMINI_3_PATTERN.test(modelId);
587
- }
588
- function createGeminiToolCallIdFixer() {
589
- let functionCallIds = [];
590
- let functionResponseIds = [];
591
- function onContext(messages) {
592
- const calls = [];
593
- const responses = [];
594
- for (const msg of messages) {
595
- if (msg.role === "assistant" && Array.isArray(msg.content)) {
596
- for (const block of msg.content) {
597
- if (block?.type === "toolCall" && typeof block.id === "string") {
598
- calls.push(block.id);
599
- }
600
- }
601
- } else if (msg.role === "toolResult" && typeof msg.toolCallId === "string") {
602
- responses.push({ id: msg.toolCallId, name: msg.toolName ?? "" });
603
- }
604
- }
605
- functionCallIds = calls;
606
- functionResponseIds = responses;
607
- }
608
- function onBeforeProviderRequest(payload) {
609
- const google = payload;
610
- if (!google || typeof google !== "object" || !Array.isArray(google.contents)) {
611
- return payload;
612
- }
613
- if (typeof google.model !== "string" || !isGemini3Model(google.model)) {
614
- return payload;
615
- }
616
- let callCursor = 0;
617
- let responseCursor = 0;
618
- let missing = 0;
619
- for (const content of google.contents) {
620
- if (!Array.isArray(content?.parts))
621
- continue;
622
- for (const part of content.parts) {
623
- if (part.functionCall && part.functionCall.id === undefined) {
624
- const id = functionCallIds[callCursor++];
625
- if (id !== undefined) {
626
- part.functionCall.id = id;
627
- } else {
628
- missing++;
629
- }
630
- } else if (part.functionResponse && part.functionResponse.id === undefined) {
631
- const entry = functionResponseIds[responseCursor++];
632
- if (entry !== undefined) {
633
- part.functionResponse.id = entry.id;
634
- if (part.functionResponse.name === undefined && entry.name) {
635
- part.functionResponse.name = entry.name;
636
- }
637
- } else {
638
- missing++;
639
- }
640
- }
641
- }
642
- }
643
- if (missing > 0) {
644
- log("gemini-toolcall-id: fewer captured ids than parts", {
645
- model: google.model,
646
- missing,
647
- capturedCalls: functionCallIds.length,
648
- capturedResponses: functionResponseIds.length
649
- });
650
- }
651
- return payload;
652
- }
653
- return { onContext, onBeforeProviderRequest };
654
- }
655
-
656
585
  // src/gemini-malformed-retry.ts
657
586
  var MALFORMED_LEAK_PATTERN = /(?:print\()?call:\s*default_api[.:]|default_api\.\w+\s*\(/;
658
587
  var MALFORMED_DIAGNOSTIC_PATTERN = /\bmalformed[\s_-]?function[\s_-]?call\b/i;
@@ -700,13 +629,6 @@ function plexusExtension(pi) {
700
629
  const envApiKey = getEnvApiKey();
701
630
  const startupBaseUrl = getBaseUrl();
702
631
  log("startup", { baseUrl: startupBaseUrl, hasEnvApiKey: !!envApiKey });
703
- const geminiToolCallIdFixer = createGeminiToolCallIdFixer();
704
- pi.on("context", (event) => {
705
- geminiToolCallIdFixer.onContext(event.messages);
706
- });
707
- pi.on("before_provider_request", (event) => {
708
- return geminiToolCallIdFixer.onBeforeProviderRequest(event.payload);
709
- });
710
632
  pi.on("message_end", (event) => normalizeMalformedFunctionCall(event.message, PROVIDER_NAME));
711
633
  pi.registerProvider(PROVIDER_NAME, {
712
634
  api: "openai-completions",
@@ -758,21 +680,25 @@ async function refreshPlexusModels(context) {
758
680
  const modelsUrl = getModelsUrl();
759
681
  const apiKey = credentialApiKey(context.credential) ?? getEnvApiKey() ?? undefined;
760
682
  const suppress = getSuppressedModels();
761
- const storedEtag = await readCachedEtag();
762
683
  if (!context.allowNetwork || !apiKey || !modelsUrl || !baseUrl) {
763
684
  if (currentModels.length > 0) {
764
685
  const filteredCurrent = currentModels.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppress));
765
- currentModels = filteredCurrent;
766
- await context.store.write({ models: currentModels, checkedAt: Date.now() });
767
- return currentModels;
686
+ await context.publish({
687
+ persist: { models: filteredCurrent, checkedAt: Date.now() },
688
+ update: () => {
689
+ currentModels = filteredCurrent;
690
+ }
691
+ });
692
+ return filteredCurrent;
768
693
  }
769
694
  const restored = await restoreStoredModels(context);
770
695
  if (restored)
771
696
  return restored;
772
697
  throw new Error(!modelsUrl || !baseUrl ? "Plexus base URL not configured. Run /login plexus first." : "No Plexus API key configured. Run /login plexus first.");
773
698
  }
699
+ const storedEtag = await readCachedEtag();
774
700
  try {
775
- const { models: apiModels, raw, etag, notModified } = await fetchPlexusModels(apiKey, modelsUrl, undefined, storedEtag);
701
+ const { models: apiModels, raw, etag, notModified } = await fetchPlexusModels(apiKey, modelsUrl, undefined, storedEtag, context.signal);
776
702
  if (notModified) {
777
703
  log("refreshModels: not modified", { etag: storedEtag });
778
704
  const restored = await restoreStoredModels(context);
@@ -780,12 +706,19 @@ async function refreshPlexusModels(context) {
780
706
  return restored;
781
707
  }
782
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
+ }
783
718
  await Promise.all([
784
- context.store.write({ models: piModels, checkedAt: Date.now() }),
785
719
  writeCachedEtag(etag),
786
720
  raw ? writeRawResponse(raw) : Promise.resolve()
787
721
  ]);
788
- currentModels = piModels;
789
722
  log("refreshModels: fetched", { count: piModels.length });
790
723
  return piModels;
791
724
  } catch (error) {
@@ -794,12 +727,16 @@ async function refreshPlexusModels(context) {
794
727
  }
795
728
  }
796
729
  async function restoreStoredModels(context) {
797
- const stored = await context.store.read();
730
+ const stored = context.stored;
798
731
  if (!stored || stored.models.length === 0)
799
732
  return;
800
733
  const suppress = getSuppressedModels();
801
734
  const models = stored.models.filter((m) => !isModelSuppressed({ id: m.id, name: m.name }, suppress));
802
- currentModels = models;
735
+ await context.publish({
736
+ update: () => {
737
+ currentModels = models;
738
+ }
739
+ });
803
740
  log("refreshModels: restored from store", { count: models.length });
804
741
  return models;
805
742
  }
@@ -831,7 +768,8 @@ function createPlexusLoginProvider() {
831
768
  plexusBaseUrl: baseUrl
832
769
  };
833
770
  },
834
- async refreshToken(credentials) {
771
+ async refreshToken(credentials, signal) {
772
+ signal.throwIfAborted();
835
773
  return { ...credentials, expires: PLEXUS_CREDENTIAL_EXPIRES_AT };
836
774
  },
837
775
  getApiKey(credentials) {
@@ -853,7 +791,16 @@ async function handleRefresh(ctx) {
853
791
  return;
854
792
  }
855
793
  ctx.ui.notify("Refreshing Plexus models\u2026", "info");
856
- 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
+ }
857
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");
858
805
  }
859
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.10",
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.81.0",
29
- "@earendil-works/pi-coding-agent": ">=0.81.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.81.0",
33
- "@earendil-works/pi-coding-agent": "^0.81.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": {