@mcowger/pi-plexus 1.3.5 → 1.3.7

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 +119 -0
  2. package/package.json +1 -1
package/dist/extension.js CHANGED
@@ -441,6 +441,117 @@ function descriptorToPiModel(descriptor) {
441
441
  };
442
442
  }
443
443
 
444
+ // src/gemini-toolcall-id.ts
445
+ var GEMINI_3_PATTERN = /gemini-(?:live-)?3(?:\.\d+)?[-.]/i;
446
+ var GEMINI_3_ALIASES = new Set(["gemini-flash-latest", "gemini-flash-lite-latest"]);
447
+ function isGemini3Model(modelId) {
448
+ return GEMINI_3_ALIASES.has(modelId) || GEMINI_3_PATTERN.test(modelId);
449
+ }
450
+ function createGeminiToolCallIdFixer() {
451
+ let functionCallIds = [];
452
+ let functionResponseIds = [];
453
+ function onContext(messages) {
454
+ const calls = [];
455
+ const responses = [];
456
+ for (const msg of messages) {
457
+ if (msg.role === "assistant" && Array.isArray(msg.content)) {
458
+ for (const block of msg.content) {
459
+ if (block?.type === "toolCall" && typeof block.id === "string") {
460
+ calls.push(block.id);
461
+ }
462
+ }
463
+ } else if (msg.role === "toolResult" && typeof msg.toolCallId === "string") {
464
+ responses.push({ id: msg.toolCallId, name: msg.toolName ?? "" });
465
+ }
466
+ }
467
+ functionCallIds = calls;
468
+ functionResponseIds = responses;
469
+ }
470
+ function onBeforeProviderRequest(payload) {
471
+ const google = payload;
472
+ if (!google || typeof google !== "object" || !Array.isArray(google.contents)) {
473
+ return payload;
474
+ }
475
+ if (typeof google.model !== "string" || !isGemini3Model(google.model)) {
476
+ return payload;
477
+ }
478
+ let callCursor = 0;
479
+ let responseCursor = 0;
480
+ let missing = 0;
481
+ for (const content of google.contents) {
482
+ if (!Array.isArray(content?.parts))
483
+ continue;
484
+ for (const part of content.parts) {
485
+ if (part.functionCall && part.functionCall.id === undefined) {
486
+ const id = functionCallIds[callCursor++];
487
+ if (id !== undefined) {
488
+ part.functionCall.id = id;
489
+ } else {
490
+ missing++;
491
+ }
492
+ } else if (part.functionResponse && part.functionResponse.id === undefined) {
493
+ const entry = functionResponseIds[responseCursor++];
494
+ if (entry !== undefined) {
495
+ part.functionResponse.id = entry.id;
496
+ if (part.functionResponse.name === undefined && entry.name) {
497
+ part.functionResponse.name = entry.name;
498
+ }
499
+ } else {
500
+ missing++;
501
+ }
502
+ }
503
+ }
504
+ }
505
+ if (missing > 0) {
506
+ log("gemini-toolcall-id: fewer captured ids than parts", {
507
+ model: google.model,
508
+ missing,
509
+ capturedCalls: functionCallIds.length,
510
+ capturedResponses: functionResponseIds.length
511
+ });
512
+ }
513
+ return payload;
514
+ }
515
+ return { onContext, onBeforeProviderRequest };
516
+ }
517
+
518
+ // src/gemini-malformed-retry.ts
519
+ var MALFORMED_LEAK_PATTERN = /(?:print\()?call:\s*default_api[.:]|default_api\.\w+\s*\(/;
520
+ var MALFORMED_DIAGNOSTIC_PATTERN = /\bmalformed[\s_-]?function[\s_-]?call\b/i;
521
+ var NORMALIZED_PREFIX = "MALFORMED_FUNCTION_CALL:";
522
+ var NORMALIZED_MESSAGE = `${NORMALIZED_PREFIX} Gemini emitted a malformed tool call (its internal ` + `function-call syntax leaked as text). This is a transient model failure \u2014 ` + `please retry your request.`;
523
+ function hasLeakedFunctionCall(content) {
524
+ if (!Array.isArray(content))
525
+ return false;
526
+ for (const block of content) {
527
+ if (block?.type === "text" && typeof block.text === "string" && MALFORMED_LEAK_PATTERN.test(block.text)) {
528
+ return true;
529
+ }
530
+ }
531
+ return false;
532
+ }
533
+ function hasToolCall(content) {
534
+ return Array.isArray(content) && content.some((block) => block?.type === "toolCall");
535
+ }
536
+ function normalizeMalformedFunctionCall(message, providerName) {
537
+ if (!message || message.role !== "assistant" || message.provider !== providerName || message.stopReason !== "error") {
538
+ return;
539
+ }
540
+ if (typeof message.errorMessage === "string" && message.errorMessage.startsWith(NORMALIZED_PREFIX)) {
541
+ return;
542
+ }
543
+ if (hasToolCall(message.content))
544
+ return;
545
+ const via = hasLeakedFunctionCall(message.content) ? "leak" : typeof message.errorMessage === "string" && MALFORMED_DIAGNOSTIC_PATTERN.test(message.errorMessage) ? "diagnostic" : undefined;
546
+ if (!via)
547
+ return;
548
+ log("gemini-malformed-retry: retagged MALFORMED_FUNCTION_CALL for retry", {
549
+ model: message.model,
550
+ via
551
+ });
552
+ return { message: { ...message, errorMessage: NORMALIZED_MESSAGE } };
553
+ }
554
+
444
555
  // src/extension.ts
445
556
  var PROVIDER_NAME = "plexus";
446
557
  var PROVIDER_API_KEY_TEMPLATE = "${PLEXUS_API_KEY}";
@@ -451,6 +562,14 @@ function plexusExtension(pi) {
451
562
  const envApiKey = getEnvApiKey();
452
563
  const startupBaseUrl = getBaseUrl();
453
564
  log("startup", { baseUrl: startupBaseUrl, hasEnvApiKey: !!envApiKey });
565
+ const geminiToolCallIdFixer = createGeminiToolCallIdFixer();
566
+ pi.on("context", (event) => {
567
+ geminiToolCallIdFixer.onContext(event.messages);
568
+ });
569
+ pi.on("before_provider_request", (event) => {
570
+ return geminiToolCallIdFixer.onBeforeProviderRequest(event.payload);
571
+ });
572
+ pi.on("message_end", (event) => normalizeMalformedFunctionCall(event.message, PROVIDER_NAME));
454
573
  pi.registerProvider(PROVIDER_NAME, {
455
574
  api: "openai-completions",
456
575
  ...envApiKey ? { apiKey: PROVIDER_API_KEY_TEMPLATE } : {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcowger/pi-plexus",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "Plexus AI proxy plugin for the pi coding agent",
5
5
  "type": "module",
6
6
  "files": [