@juspay/neurolink 9.79.2 → 9.79.3

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.
@@ -22,7 +22,7 @@ import { convertZodToJsonSchema, inlineJsonSchema, ensureNestedSchemaTypes, } fr
22
22
  import { createNativeThinkingConfig } from "../utils/thinkingConfig.js";
23
23
  import { TimeoutError, withTimeout } from "../utils/async/index.js";
24
24
  import { parseTimeout } from "../utils/timeout.js";
25
- import { createTextChannel, extractThoughtSignature, prependConversationMessages, } from "./googleNativeGemini3.js";
25
+ import { appendStepText, createTextChannel, extractThoughtSignature, mapGeminiFinishReason, prependConversationMessages, } from "./googleNativeGemini3.js";
26
26
  import { ATTR, LANGFUSE_ATTR, spanJsonAttribute, tracers, withClientSpan, withClientStreamSpan, withSpan, } from "../telemetry/index.js";
27
27
  import { SpanKind, SpanStatusCode, context as otelContext, trace as otelTrace, } from "@opentelemetry/api";
28
28
  import { calculateCost } from "../utils/pricing.js";
@@ -786,7 +786,7 @@ export class GoogleVertexProvider extends BaseProvider {
786
786
  // lifecycle callbacks. Pipeline A gets these via the AI SDK
787
787
  // wrapStream middleware; the native path has to fire them here.
788
788
  const wrappedResult = this.wrapStreamResultWithLifecycle(options, result, streamStartTime);
789
- this.emitStreamEnd(modelName, streamStartTime, true);
789
+ this.emitStreamEnd(modelName, streamStartTime, true, undefined, result.finishReason);
790
790
  return wrappedResult;
791
791
  }
792
792
  catch (error) {
@@ -801,7 +801,7 @@ export class GoogleVertexProvider extends BaseProvider {
801
801
  * `model.generation` span for native Vertex stream traffic. Mirrors
802
802
  * `emitGenerationEnd` (used by `generate()`).
803
803
  */
804
- emitStreamEnd(modelName, startTime, success, error) {
804
+ emitStreamEnd(modelName, startTime, success, error, resolvedFinishReason) {
805
805
  const emitter = this.neurolink?.getEventEmitter();
806
806
  if (!emitter) {
807
807
  return;
@@ -815,7 +815,7 @@ export class GoogleVertexProvider extends BaseProvider {
815
815
  usage: { input: 0, output: 0, total: 0 },
816
816
  model: modelName,
817
817
  provider: this.providerName,
818
- finishReason: success ? "stop" : "error",
818
+ finishReason: success ? (resolvedFinishReason ?? "stop") : "error",
819
819
  },
820
820
  success,
821
821
  ...(error
@@ -1156,7 +1156,10 @@ export class GoogleVertexProvider extends BaseProvider {
1156
1156
  : Math.min(DEFAULT_MAX_STEPS, 100);
1157
1157
  const currentContents = [...contents];
1158
1158
  let finalText = "";
1159
- let lastStepText = ""; // Track text from last step for maxSteps termination
1159
+ // Last SDK finish reason seen across steps (Bug 2: previously never read,
1160
+ // so callers fell back to "unknown"). Last non-empty value wins — the
1161
+ // terminal chunk is authoritative.
1162
+ let lastFinishReason;
1160
1163
  const allToolCalls = [];
1161
1164
  // Mirrors the generate-path shape so StreamResult.toolExecutions can be
1162
1165
  // populated (parity with AI-SDK-driven providers) and so the storage
@@ -1200,6 +1203,12 @@ export class GoogleVertexProvider extends BaseProvider {
1200
1203
  const chunkRecord = chunk;
1201
1204
  const candidates = chunkRecord.candidates;
1202
1205
  const firstCandidate = candidates?.[0];
1206
+ // Capture the SDK finish reason (Bug 2: previously dropped). Last
1207
+ // non-empty value across chunks wins.
1208
+ const chunkFinishReason = firstCandidate?.finishReason;
1209
+ if (typeof chunkFinishReason === "string" && chunkFinishReason) {
1210
+ lastFinishReason = chunkFinishReason;
1211
+ }
1203
1212
  const chunkContent = firstCandidate?.content;
1204
1213
  if (chunkContent && Array.isArray(chunkContent.parts)) {
1205
1214
  for (const part of chunkContent.parts) {
@@ -1253,8 +1262,6 @@ export class GoogleVertexProvider extends BaseProvider {
1253
1262
  break;
1254
1263
  }
1255
1264
  }
1256
- // Track the last step text for maxSteps termination
1257
- lastStepText = stepText;
1258
1265
  // Execute function calls
1259
1266
  logger.debug(`[GoogleVertex] Executing ${stepFunctionCalls.length} function calls`);
1260
1267
  // Add model response with ALL parts (including thoughtSignature) to history
@@ -1426,14 +1433,50 @@ export class GoogleVertexProvider extends BaseProvider {
1426
1433
  throw this.handleProviderError(error);
1427
1434
  }
1428
1435
  }
1429
- // Handle maxSteps termination - if we exited the loop due to maxSteps being reached
1436
+ // Handle maxSteps termination the loop exited because the step cap was
1437
+ // reached while the model was still calling tools. Surface a real answer
1438
+ // instead of the canned placeholder (Bug 1) and a meaningful finishReason
1439
+ // (Bug 2).
1440
+ let hitStepLimit = false;
1441
+ let synthesizedFinalAnswer = false;
1430
1442
  if (step >= maxSteps && !finalText) {
1431
- logger.warn(`[GoogleVertex] Tool call loop terminated after reaching maxSteps (${maxSteps}). ` +
1432
- `Model was still calling tools. Using accumulated text from last step.`);
1433
- finalText =
1434
- lastStepText ||
1435
- `[Tool execution limit reached after ${maxSteps} steps. The model continued requesting tool calls beyond the limit.]`;
1443
+ hitStepLimit = true;
1444
+ // The consumer receives text via `incrementalTextChunks`; any text the
1445
+ // model emitted across steps is already preserved there. Only synthesize
1446
+ // when NOTHING was produced (the pure-functionCall case that otherwise
1447
+ // surfaces the placeholder) so we never waste a round-trip whose output
1448
+ // the stream would ignore.
1449
+ if (incrementalTextChunks.length === 0) {
1450
+ logger.warn(`[GoogleVertex] Tool call loop terminated after reaching maxSteps (${maxSteps}) ` +
1451
+ `with no text; synthesizing a final answer with tools disabled.`);
1452
+ const synth = await this.synthesizeFinalAnswerWithoutTools(client, modelName, config, currentContents, useFinalResultTool, parseTimeout(options.timeout) ?? 300_000);
1453
+ if (synth.text) {
1454
+ synthesizedFinalAnswer = true;
1455
+ finalText = synth.text;
1456
+ incrementalTextChunks.push(synth.text);
1457
+ totalInputTokens += synth.inputTokens;
1458
+ totalOutputTokens += synth.outputTokens;
1459
+ if (synth.finishReason) {
1460
+ lastFinishReason = synth.finishReason;
1461
+ }
1462
+ }
1463
+ else {
1464
+ finalText = `[Tool execution limit reached after ${maxSteps} steps. The model continued requesting tool calls beyond the limit.]`;
1465
+ }
1466
+ }
1467
+ else {
1468
+ logger.warn(`[GoogleVertex] Tool call loop terminated after reaching maxSteps (${maxSteps}); ` +
1469
+ `returning text already gathered from prior steps.`);
1470
+ }
1436
1471
  }
1472
+ // Unified finish reason: a step-cap exhaustion that did NOT end in a clean
1473
+ // synthesized answer is reported as "tool-calls" (the model still wanted
1474
+ // tools) — NOT "length", which neurolink.ts treats as token truncation
1475
+ // (jsonTruncated + WARNING span). A clean completion maps from the SDK
1476
+ // finish reason.
1477
+ const resolvedFinishReason = hitStepLimit && !synthesizedFinalAnswer
1478
+ ? "tool-calls"
1479
+ : mapGeminiFinishReason(lastFinishReason);
1437
1480
  const responseTime = Date.now() - startTime;
1438
1481
  // Yield each text part separately so the CLI receives multiple stream
1439
1482
  // chunks instead of a single coalesced buffer. The SDK already gave us
@@ -1460,6 +1503,7 @@ export class GoogleVertexProvider extends BaseProvider {
1460
1503
  stream: createTextStream(),
1461
1504
  provider: this.providerName,
1462
1505
  model: modelName,
1506
+ finishReason: resolvedFinishReason,
1463
1507
  usage: {
1464
1508
  input: totalInputTokens,
1465
1509
  output: totalOutputTokens,
@@ -1791,7 +1835,11 @@ export class GoogleVertexProvider extends BaseProvider {
1791
1835
  : Math.min(DEFAULT_MAX_STEPS, 100);
1792
1836
  const currentContents = [...contents];
1793
1837
  let finalText = "";
1794
- let lastStepText = ""; // Track text from last step for maxSteps termination
1838
+ // Cross-step text accumulation + last SDK finish reason, so the
1839
+ // maxSteps-exhaustion exit can surface real gathered text (Bug 1) and a
1840
+ // meaningful finishReason (Bug 2) instead of a placeholder / "unknown".
1841
+ let accumulatedText = "";
1842
+ let lastFinishReason;
1795
1843
  const allToolCalls = [];
1796
1844
  const toolExecutions = [];
1797
1845
  let step = 0;
@@ -1826,6 +1874,12 @@ export class GoogleVertexProvider extends BaseProvider {
1826
1874
  const chunkRecord = chunk;
1827
1875
  const candidates = chunkRecord.candidates;
1828
1876
  const firstCandidate = candidates?.[0];
1877
+ // Capture the SDK finish reason (Bug 2: previously dropped). Last
1878
+ // non-empty value across chunks wins.
1879
+ const chunkFinishReason = firstCandidate?.finishReason;
1880
+ if (typeof chunkFinishReason === "string" && chunkFinishReason) {
1881
+ lastFinishReason = chunkFinishReason;
1882
+ }
1829
1883
  const chunkContent = firstCandidate?.content;
1830
1884
  if (chunkContent && Array.isArray(chunkContent.parts)) {
1831
1885
  rawResponseParts.push(...chunkContent.parts);
@@ -1874,8 +1928,11 @@ export class GoogleVertexProvider extends BaseProvider {
1874
1928
  break;
1875
1929
  }
1876
1930
  }
1877
- // Track the last step text for maxSteps termination
1878
- lastStepText = stepText;
1931
+ // Accumulate non-empty step text across steps so the
1932
+ // maxSteps-exhaustion exit can surface the prose the model produced
1933
+ // instead of a canned placeholder (Bug 1). Mirrors the Vertex-Claude
1934
+ // loop's text accumulation.
1935
+ accumulatedText = appendStepText(accumulatedText, stepText);
1879
1936
  // Execute function calls
1880
1937
  logger.debug(`[GoogleVertex] Generate executing ${stepFunctionCalls.length} function calls`);
1881
1938
  // Add model response with ALL parts (including thoughtSignature) to history
@@ -2036,14 +2093,49 @@ export class GoogleVertexProvider extends BaseProvider {
2036
2093
  throw this.handleProviderError(error);
2037
2094
  }
2038
2095
  }
2039
- // Handle maxSteps termination - if we exited the loop due to maxSteps being reached
2096
+ // Handle maxSteps termination the loop exited because the step cap was
2097
+ // reached while the model was still calling tools. Surface a real answer
2098
+ // instead of the canned placeholder (Bug 1) and a meaningful finishReason
2099
+ // (Bug 2).
2100
+ let hitStepLimit = false;
2101
+ let synthesizedFinalAnswer = false;
2040
2102
  if (step >= maxSteps && !finalText) {
2041
- logger.warn(`[GoogleVertex] Generate tool call loop terminated after reaching maxSteps (${maxSteps}). ` +
2042
- `Model was still calling tools. Using accumulated text from last step.`);
2043
- finalText =
2044
- lastStepText ||
2045
- `[Tool execution limit reached after ${maxSteps} steps. The model continued requesting tool calls beyond the limit.]`;
2103
+ hitStepLimit = true;
2104
+ if (accumulatedText) {
2105
+ // Prefer the prose the model already produced across steps.
2106
+ logger.warn(`[GoogleVertex] Generate tool call loop terminated after reaching maxSteps (${maxSteps}); ` +
2107
+ `returning text already gathered from prior steps.`);
2108
+ finalText = accumulatedText;
2109
+ }
2110
+ else {
2111
+ // Pure functionCall turns leave no text — make one tools-disabled call
2112
+ // so the model answers from the gathered tool results instead of the
2113
+ // canned placeholder.
2114
+ logger.warn(`[GoogleVertex] Generate tool call loop terminated after reaching maxSteps (${maxSteps}) ` +
2115
+ `with no text; synthesizing a final answer with tools disabled.`);
2116
+ const synth = await this.synthesizeFinalAnswerWithoutTools(client, modelName, config, currentContents, useFinalResultTool, parseTimeout(options.timeout) ?? 300_000);
2117
+ if (synth.text) {
2118
+ synthesizedFinalAnswer = true;
2119
+ finalText = synth.text;
2120
+ totalInputTokens += synth.inputTokens;
2121
+ totalOutputTokens += synth.outputTokens;
2122
+ if (synth.finishReason) {
2123
+ lastFinishReason = synth.finishReason;
2124
+ }
2125
+ }
2126
+ else {
2127
+ finalText = `[Tool execution limit reached after ${maxSteps} steps. The model continued requesting tool calls beyond the limit.]`;
2128
+ }
2129
+ }
2046
2130
  }
2131
+ // Unified finish reason: a step-cap exhaustion that did NOT end in a clean
2132
+ // synthesized answer is reported as "tool-calls" (the model still wanted
2133
+ // tools) — NOT "length", which neurolink.ts treats as token truncation
2134
+ // (jsonTruncated + WARNING span). A clean completion maps from the SDK
2135
+ // finish reason.
2136
+ const resolvedFinishReason = hitStepLimit && !synthesizedFinalAnswer
2137
+ ? "tool-calls"
2138
+ : mapGeminiFinishReason(lastFinishReason);
2047
2139
  const responseTime = Date.now() - startTime;
2048
2140
  // Filter out final_result from tool calls and executions as it's an internal pattern
2049
2141
  const externalToolCalls = allToolCalls.filter((tc) => tc.toolName !== "final_result");
@@ -2053,6 +2145,7 @@ export class GoogleVertexProvider extends BaseProvider {
2053
2145
  content: finalText,
2054
2146
  provider: this.providerName,
2055
2147
  model: modelName,
2148
+ finishReason: resolvedFinishReason,
2056
2149
  usage: {
2057
2150
  input: totalInputTokens,
2058
2151
  output: totalOutputTokens,
@@ -2073,6 +2166,89 @@ export class GoogleVertexProvider extends BaseProvider {
2073
2166
  // Vertex Gemini generate path.
2074
2167
  return this.enhanceResult(result, options, startTime);
2075
2168
  }
2169
+ /**
2170
+ * One-shot, tools-disabled model call used when a native Gemini agentic loop
2171
+ * is force-terminated by the step cap with no text produced. Lets the model
2172
+ * synthesize a final answer from the function results already in `contents`
2173
+ * instead of returning a canned placeholder (Bug 1, part b).
2174
+ *
2175
+ * Tools are disabled by OMITTING `config.tools` — the codebase's established
2176
+ * mechanism. `@google/genai`'s `FunctionCallingConfigMode.NONE` is documented
2177
+ * as equivalent to passing no function declarations, and `functionCallingConfig`
2178
+ * is not used anywhere in this codebase. When the structured-output
2179
+ * (`final_result`) pattern was active, a trailing instruction countermands the
2180
+ * earlier "you MUST call final_result" directive so the model answers in plain
2181
+ * text. Never throws — returns empty text so the caller falls back to the
2182
+ * placeholder, guaranteeing no new failure path.
2183
+ */
2184
+ async synthesizeFinalAnswerWithoutTools(client, modelName, config, contents, useFinalResultTool, timeoutMs) {
2185
+ try {
2186
+ // Shallow clone so the loop's config is never mutated; dropping the
2187
+ // top-level `tools` key is sufficient (nested thinkingConfig /
2188
+ // systemInstruction are intentionally preserved).
2189
+ const synthConfig = { ...config };
2190
+ delete synthConfig.tools;
2191
+ if (useFinalResultTool) {
2192
+ const baseSystemInstruction = typeof synthConfig.systemInstruction === "string"
2193
+ ? synthConfig.systemInstruction
2194
+ : "";
2195
+ synthConfig.systemInstruction =
2196
+ baseSystemInstruction +
2197
+ "\n\nThe final_result tool is no longer available. Provide your " +
2198
+ "final answer directly as plain text now, using the information " +
2199
+ "gathered so far.";
2200
+ }
2201
+ // Bound the whole connect + drain with a timeout. The surrounding
2202
+ // try/catch only catches throws, not hangs, so without this a stalled
2203
+ // Vertex endpoint would hang the maxSteps recovery path indefinitely.
2204
+ // On timeout withTimeout rejects (TimeoutError) and the catch below
2205
+ // falls back to the placeholder — no new failure path is introduced.
2206
+ return await withTimeout((async () => {
2207
+ const stream = await client.models.generateContentStream({
2208
+ model: modelName,
2209
+ contents,
2210
+ config: synthConfig,
2211
+ });
2212
+ const parts = [];
2213
+ let finishReason;
2214
+ let inputTokens = 0;
2215
+ let outputTokens = 0;
2216
+ for await (const chunk of stream) {
2217
+ const chunkRecord = chunk;
2218
+ const candidates = chunkRecord.candidates;
2219
+ const firstCandidate = candidates?.[0];
2220
+ const chunkFinishReason = firstCandidate?.finishReason;
2221
+ if (typeof chunkFinishReason === "string" && chunkFinishReason) {
2222
+ finishReason = chunkFinishReason;
2223
+ }
2224
+ const chunkContent = firstCandidate?.content;
2225
+ if (chunkContent && Array.isArray(chunkContent.parts)) {
2226
+ parts.push(...chunkContent.parts);
2227
+ }
2228
+ const usageMetadata = chunkRecord.usageMetadata;
2229
+ if (usageMetadata) {
2230
+ if (usageMetadata.promptTokenCount !== undefined &&
2231
+ usageMetadata.promptTokenCount > 0) {
2232
+ inputTokens = usageMetadata.promptTokenCount;
2233
+ }
2234
+ if (usageMetadata.candidatesTokenCount !== undefined &&
2235
+ usageMetadata.candidatesTokenCount > 0) {
2236
+ outputTokens = usageMetadata.candidatesTokenCount;
2237
+ }
2238
+ }
2239
+ }
2240
+ const text = parts
2241
+ .filter((part) => typeof part.text === "string")
2242
+ .map((part) => part.text)
2243
+ .join("");
2244
+ return { text, finishReason, inputTokens, outputTokens };
2245
+ })(), timeoutMs, "Gemini synthesis call timed out");
2246
+ }
2247
+ catch (error) {
2248
+ logger.warn("[GoogleVertex] Tools-disabled synthesis call failed; falling back to placeholder", { error: error instanceof Error ? error.message : String(error) });
2249
+ return { text: "", inputTokens: 0, outputTokens: 0 };
2250
+ }
2251
+ }
2076
2252
  /**
2077
2253
  * Create native AnthropicVertex client for Claude models
2078
2254
  */
@@ -3685,7 +3861,7 @@ export class GoogleVertexProvider extends BaseProvider {
3685
3861
  }
3686
3862
  : undefined,
3687
3863
  duration: Date.now() - startTime,
3688
- finishReason: "stop",
3864
+ finishReason: result?.finishReason ?? "stop",
3689
3865
  });
3690
3866
  Promise.resolve(callbackResult).catch((err) => logger.warn(`[GoogleVertex] onFinish callback rejected: ${err instanceof Error ? err.message : String(err)}`));
3691
3867
  }
@@ -3890,7 +4066,7 @@ export class GoogleVertexProvider extends BaseProvider {
3890
4066
  usage,
3891
4067
  model: modelName,
3892
4068
  provider: this.providerName,
3893
- finishReason: success ? "stop" : "error",
4069
+ finishReason: success ? (result?.finishReason ?? "stop") : "error",
3894
4070
  },
3895
4071
  success,
3896
4072
  ...(error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "9.79.2",
3
+ "version": "9.79.3",
4
4
  "packageManager": "pnpm@10.15.1",
5
5
  "description": "Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applications with 21+ providers: OpenAI, Anthropic, Google AI Studio, Google Vertex, AWS Bedrock, Azure OpenAI, Mistral, LiteLLM, SageMaker, Hugging Face, Ollama, OpenAI-compatible, OpenRouter, DeepSeek, NVIDIA NIM, LM Studio, llama.cpp, plus voice (OpenAI TTS, ElevenLabs, Deepgram, Azure Speech).",
6
6
  "author": {