@juspay/neurolink 9.79.2 → 9.80.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/CHANGELOG.md +12 -0
- package/dist/browser/neurolink.min.js +351 -347
- package/dist/cli/factories/commandFactory.js +61 -0
- package/dist/cli/utils/classifierRouterFlags.d.ts +19 -0
- package/dist/cli/utils/classifierRouterFlags.js +111 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +1 -1
- package/dist/lib/neurolink.d.ts +14 -0
- package/dist/lib/neurolink.js +131 -0
- package/dist/lib/providers/googleNativeGemini3.d.ts +26 -0
- package/dist/lib/providers/googleNativeGemini3.js +48 -0
- package/dist/lib/providers/googleVertex.d.ts +16 -0
- package/dist/lib/providers/googleVertex.js +200 -24
- package/dist/lib/routing/classifierRouter.d.ts +52 -0
- package/dist/lib/routing/classifierRouter.js +269 -0
- package/dist/lib/routing/classifierStrategies.d.ts +23 -0
- package/dist/lib/routing/classifierStrategies.js +156 -0
- package/dist/lib/routing/index.d.ts +2 -0
- package/dist/lib/routing/index.js +2 -0
- package/dist/lib/session/globalSessionState.d.ts +10 -1
- package/dist/lib/session/globalSessionState.js +18 -0
- package/dist/lib/types/classifierRouter.d.ts +193 -0
- package/dist/lib/types/classifierRouter.js +17 -0
- package/dist/lib/types/cli.d.ts +27 -3
- package/dist/lib/types/config.d.ts +10 -0
- package/dist/lib/types/index.d.ts +1 -0
- package/dist/lib/types/index.js +2 -0
- package/dist/neurolink.d.ts +14 -0
- package/dist/neurolink.js +131 -0
- package/dist/providers/googleNativeGemini3.d.ts +26 -0
- package/dist/providers/googleNativeGemini3.js +48 -0
- package/dist/providers/googleVertex.d.ts +16 -0
- package/dist/providers/googleVertex.js +200 -24
- package/dist/routing/classifierRouter.d.ts +52 -0
- package/dist/routing/classifierRouter.js +268 -0
- package/dist/routing/classifierStrategies.d.ts +23 -0
- package/dist/routing/classifierStrategies.js +155 -0
- package/dist/routing/index.d.ts +2 -0
- package/dist/routing/index.js +2 -0
- package/dist/session/globalSessionState.d.ts +10 -1
- package/dist/session/globalSessionState.js +18 -0
- package/dist/types/classifierRouter.d.ts +193 -0
- package/dist/types/classifierRouter.js +16 -0
- package/dist/types/cli.d.ts +27 -3
- package/dist/types/config.d.ts +10 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/package.json +2 -1
package/dist/neurolink.js
CHANGED
|
@@ -87,6 +87,7 @@ import { isNonNullObject } from "./utils/typeUtils.js";
|
|
|
87
87
|
import { getWorkflow } from "./workflow/core/workflowRegistry.js";
|
|
88
88
|
import { runWorkflow } from "./workflow/core/workflowRunner.js";
|
|
89
89
|
import { ModelPool, classifyProviderError } from "./routing/index.js";
|
|
90
|
+
import { ClassifierRouter } from "./routing/classifierRouter.js";
|
|
90
91
|
import { looksLikeModelAccessDenied as sharedLooksLikeModelAccessDenied, isNonRetryableProviderError as sharedIsNonRetryableProviderError, } from "./utils/providerErrorClassification.js";
|
|
91
92
|
/**
|
|
92
93
|
* NL-002: Classify MCP error messages into categories for AI disambiguation.
|
|
@@ -388,6 +389,9 @@ export class NeuroLink {
|
|
|
388
389
|
// RequestRouter: pluggable pre-call provider/model selector.
|
|
389
390
|
// Stored directly from config.requestRouter; null when not configured.
|
|
390
391
|
requestRouter;
|
|
392
|
+
// ClassifierRouter: opt-in "classify → pick model + tools" pre-call router.
|
|
393
|
+
// Built from config.classifierRouter; null when not configured.
|
|
394
|
+
classifierRouter;
|
|
391
395
|
/**
|
|
392
396
|
* Merge instance-level credentials with per-call credentials.
|
|
393
397
|
*
|
|
@@ -796,6 +800,17 @@ export class NeuroLink {
|
|
|
796
800
|
this.modelPool = config?.modelPool ? new ModelPool(config.modelPool) : null;
|
|
797
801
|
// RequestRouter: store the host-supplied function; null when not configured.
|
|
798
802
|
this.requestRouter = config?.requestRouter ?? null;
|
|
803
|
+
// ClassifierRouter: opt-in. The LLM strategy reuses this instance's
|
|
804
|
+
// generate() (marked so it never recursively re-routes). Fails open.
|
|
805
|
+
this.classifierRouter = config?.classifierRouter?.enabled
|
|
806
|
+
? new ClassifierRouter(config.classifierRouter, {
|
|
807
|
+
generate: (genOptions) => this.generate(genOptions),
|
|
808
|
+
logger: {
|
|
809
|
+
debug: (message, meta) => logger.debug(message, meta),
|
|
810
|
+
warn: (message, meta) => logger.warn(message, meta),
|
|
811
|
+
},
|
|
812
|
+
})
|
|
813
|
+
: null;
|
|
799
814
|
logger.setEventEmitter(this.emitter);
|
|
800
815
|
// Read tool cache duration from environment variables, with a default
|
|
801
816
|
const cacheDurationEnv = process.env.NEUROLINK_TOOL_CACHE_DURATION;
|
|
@@ -3105,6 +3120,13 @@ Current user's request: ${currentInput}`;
|
|
|
3105
3120
|
}
|
|
3106
3121
|
async runStandardGenerateRequest(options, originalPrompt, generateSpan) {
|
|
3107
3122
|
const startTime = Date.now();
|
|
3123
|
+
// Pre-call classifier router: opt-in. Classifies the request and selects a
|
|
3124
|
+
// provider/model from the configured base pool (and optionally narrows the
|
|
3125
|
+
// tool set) BEFORE orchestration/requestRouter, so it takes precedence.
|
|
3126
|
+
// Fails open.
|
|
3127
|
+
await this.applyClassifierRouting(options, options.input?.text ?? originalPrompt ?? "", !options.disableTools &&
|
|
3128
|
+
(!!(options.tools && Object.keys(options.tools).length > 0) ||
|
|
3129
|
+
this.getCustomTools().size > 0), !!(options.input?.images && options.input.images.length > 0), options.thinkingConfig?.thinkingLevel);
|
|
3108
3130
|
await this.maybeApplyGenerateOrchestration(options);
|
|
3109
3131
|
// Pre-call request router: opt-in, only when the caller did not set both
|
|
3110
3132
|
// provider+model. Fails open (any router error leaves options unchanged).
|
|
@@ -3241,6 +3263,12 @@ Current user's request: ${currentInput}`;
|
|
|
3241
3263
|
if (!this.requestRouter) {
|
|
3242
3264
|
return;
|
|
3243
3265
|
}
|
|
3266
|
+
// Stand down if the classifier router already selected a provider/model on
|
|
3267
|
+
// this turn — one explicit precedence order: classifierRouter wins.
|
|
3268
|
+
if (options.context
|
|
3269
|
+
?.__classifierRouted) {
|
|
3270
|
+
return;
|
|
3271
|
+
}
|
|
3244
3272
|
// Skip if the caller explicitly set both provider AND model — they know
|
|
3245
3273
|
// what they want; the router should not override an intentional choice.
|
|
3246
3274
|
if (options.provider && options.model) {
|
|
@@ -3307,6 +3335,103 @@ Current user's request: ${currentInput}`;
|
|
|
3307
3335
|
});
|
|
3308
3336
|
}
|
|
3309
3337
|
}
|
|
3338
|
+
/**
|
|
3339
|
+
* Applies the host-configured `classifierRouter` to `options` in place.
|
|
3340
|
+
*
|
|
3341
|
+
* Classifies the request by difficulty and selects a provider/model from the
|
|
3342
|
+
* configured base pool (cheaper/faster for easy tasks, more capable for hard
|
|
3343
|
+
* ones), and optionally narrows the tool set via `toolFilter`/`excludeTools`.
|
|
3344
|
+
*
|
|
3345
|
+
* Skipped when: no router is configured; the inbound call is the classifier's
|
|
3346
|
+
* own generate() (marked `__classifierRouted`); the caller pinned BOTH
|
|
3347
|
+
* provider and model; or a ModelPool is configured (the pool owns selection).
|
|
3348
|
+
* Fails open — any error leaves options unchanged.
|
|
3349
|
+
*/
|
|
3350
|
+
async applyClassifierRouting(options, promptText, hasTools, requiresVision, thinkingLevel) {
|
|
3351
|
+
if (!this.classifierRouter) {
|
|
3352
|
+
return;
|
|
3353
|
+
}
|
|
3354
|
+
const opt = options;
|
|
3355
|
+
// Prevent recursion: the LLM classifier itself calls generate() with this
|
|
3356
|
+
// marker set. Also respect any explicit prior routing decision.
|
|
3357
|
+
if (opt.context?.__classifierRouted) {
|
|
3358
|
+
return;
|
|
3359
|
+
}
|
|
3360
|
+
// Honor an intentional caller choice of BOTH provider and model.
|
|
3361
|
+
if (options.provider && options.model) {
|
|
3362
|
+
return;
|
|
3363
|
+
}
|
|
3364
|
+
// A ModelPool overrides provider/model per member unconditionally; let it
|
|
3365
|
+
// own selection (mirrors applyRequestRouter precedence).
|
|
3366
|
+
if (this.modelPool) {
|
|
3367
|
+
logger.debug("[NeuroLink] applyClassifierRouting: skipped — modelPool takes precedence");
|
|
3368
|
+
return;
|
|
3369
|
+
}
|
|
3370
|
+
const sessionId = typeof opt.context?.sessionId === "string"
|
|
3371
|
+
? opt.context.sessionId
|
|
3372
|
+
: undefined;
|
|
3373
|
+
try {
|
|
3374
|
+
const decision = await this.classifierRouter.route({
|
|
3375
|
+
prompt: promptText,
|
|
3376
|
+
estimatedInputTokens: Math.ceil((promptText?.length ?? 0) / 4),
|
|
3377
|
+
hasTools,
|
|
3378
|
+
requiresVision,
|
|
3379
|
+
thinkingLevel,
|
|
3380
|
+
sessionId,
|
|
3381
|
+
});
|
|
3382
|
+
if (!decision) {
|
|
3383
|
+
return;
|
|
3384
|
+
}
|
|
3385
|
+
// Apply provider/model/region as a coherent set (same discipline as
|
|
3386
|
+
// applyRequestRouter): only fill fields the caller did not pin.
|
|
3387
|
+
if (decision.provider && !options.provider) {
|
|
3388
|
+
options.provider = decision.provider;
|
|
3389
|
+
if (decision.model && !options.model) {
|
|
3390
|
+
options.model = decision.model;
|
|
3391
|
+
}
|
|
3392
|
+
if (decision.region && !options.region) {
|
|
3393
|
+
options.region = decision.region;
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
else if (!decision.provider && decision.model && !options.model) {
|
|
3397
|
+
options.model = decision.model;
|
|
3398
|
+
}
|
|
3399
|
+
// Narrow tools unless the caller disabled tools entirely.
|
|
3400
|
+
if (!opt.disableTools) {
|
|
3401
|
+
if (decision.excludeTools && decision.excludeTools.length > 0) {
|
|
3402
|
+
opt.excludeTools = Array.from(new Set([...(opt.excludeTools ?? []), ...decision.excludeTools]));
|
|
3403
|
+
}
|
|
3404
|
+
if (decision.toolFilter && decision.toolFilter.length > 0) {
|
|
3405
|
+
const allow = decision.toolFilter;
|
|
3406
|
+
opt.toolFilter =
|
|
3407
|
+
opt.toolFilter && opt.toolFilter.length > 0
|
|
3408
|
+
? opt.toolFilter.filter((t) => allow.includes(t))
|
|
3409
|
+
: [...allow];
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
// Mark routed so orchestration/requestRouter stand down this turn.
|
|
3413
|
+
if (decision.provider || decision.model) {
|
|
3414
|
+
const ctx = opt.context ?? {};
|
|
3415
|
+
ctx.__classifierRouted = true;
|
|
3416
|
+
opt.context = ctx;
|
|
3417
|
+
}
|
|
3418
|
+
if (decision.reason) {
|
|
3419
|
+
logger.debug("[NeuroLink] classifierRouter decision", {
|
|
3420
|
+
reason: decision.reason,
|
|
3421
|
+
difficulty: decision.difficulty,
|
|
3422
|
+
provider: options.provider,
|
|
3423
|
+
model: options.model,
|
|
3424
|
+
});
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
catch (classifierErr) {
|
|
3428
|
+
logger.warn("[NeuroLink] classifierRouter threw — proceeding unrouted", {
|
|
3429
|
+
error: classifierErr instanceof Error
|
|
3430
|
+
? classifierErr.message
|
|
3431
|
+
: String(classifierErr),
|
|
3432
|
+
});
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3310
3435
|
async prepareGenerateAugmentations(options) {
|
|
3311
3436
|
if (options.rag?.files?.length) {
|
|
3312
3437
|
try {
|
|
@@ -5829,6 +5954,12 @@ Current user's request: ${currentInput}`;
|
|
|
5829
5954
|
// classify correctly. After the workflow short-circuit, so workflow
|
|
5830
5955
|
// streams skip it.
|
|
5831
5956
|
await context.with(streamSpanContext, () => this.setLangfuseContextFromOptions(options, () => this.applyToolRoutingExclusions(options, originalPrompt)));
|
|
5957
|
+
// Pre-call classifier router for stream: opt-in, fails open. Runs before
|
|
5958
|
+
// the request router so it takes precedence.
|
|
5959
|
+
await this.applyClassifierRouting(options, originalPrompt, !options.disableTools &&
|
|
5960
|
+
(!!(options.tools && Object.keys(options.tools).length > 0) ||
|
|
5961
|
+
this.getCustomTools().size > 0), !!(options.input?.images && options.input.images.length > 0), options
|
|
5962
|
+
.thinking?.thinkingLevel);
|
|
5832
5963
|
// Pre-call request router for stream: opt-in, fails open.
|
|
5833
5964
|
await this.applyRequestRouter(options, originalPrompt, !options.disableTools &&
|
|
5834
5965
|
(!!(options.tools && Object.keys(options.tools).length > 0) ||
|
|
@@ -96,6 +96,32 @@ export declare function buildNativeConfig(options: {
|
|
|
96
96
|
* Compute a safe, clamped maxSteps value.
|
|
97
97
|
*/
|
|
98
98
|
export declare function computeMaxSteps(rawMaxSteps?: number): number;
|
|
99
|
+
/**
|
|
100
|
+
* Map a `@google/genai` `Candidate.finishReason` enum value onto NeuroLink's
|
|
101
|
+
* unified finish reason, mirroring anthropic.ts `mapAnthropicStopReason`.
|
|
102
|
+
*
|
|
103
|
+
* Enum values per `@google/genai` `FinishReason`: STOP, MAX_TOKENS, SAFETY,
|
|
104
|
+
* RECITATION, LANGUAGE, OTHER, BLOCKLIST, PROHIBITED_CONTENT, SPII,
|
|
105
|
+
* MALFORMED_FUNCTION_CALL, IMAGE_SAFETY, UNEXPECTED_TOOL_CALL,
|
|
106
|
+
* FINISH_REASON_UNSPECIFIED. Unknown / unset / non-terminal values default to
|
|
107
|
+
* "stop" (a clean completion is the safe assumption).
|
|
108
|
+
*
|
|
109
|
+
* Returns a plain string (not a `{ unified, raw }` object): the Vertex result
|
|
110
|
+
* builders and the consuming layer (neurolink.ts `finishReason || "unknown"`
|
|
111
|
+
* and `finishReason === "length"`) compare against plain strings.
|
|
112
|
+
*/
|
|
113
|
+
export declare function mapGeminiFinishReason(raw: string | null | undefined): "stop" | "length" | "tool-calls" | "content-filter";
|
|
114
|
+
/**
|
|
115
|
+
* Append a step's text to a running cross-step accumulator, ignoring empty
|
|
116
|
+
* steps and inserting a single newline between non-empty contributions.
|
|
117
|
+
*
|
|
118
|
+
* The native Gemini loops overwrite per-step text into `lastStepText`, so when
|
|
119
|
+
* the loop is force-terminated by the step cap the intermediate tool-step prose
|
|
120
|
+
* is lost and a canned placeholder becomes the answer. Accumulating here mirrors
|
|
121
|
+
* the Vertex-Claude loop's `aggregatedTurnText += block.text` so the gathered
|
|
122
|
+
* text can be surfaced at the maxSteps-exhaustion exit instead of the placeholder.
|
|
123
|
+
*/
|
|
124
|
+
export declare function appendStepText(accumulated: string, stepText: string): string;
|
|
99
125
|
/**
|
|
100
126
|
* Process stream chunks to extract raw response parts, function calls, and usage metadata.
|
|
101
127
|
*
|
|
@@ -450,6 +450,54 @@ export function computeMaxSteps(rawMaxSteps) {
|
|
|
450
450
|
? Math.min(Math.floor(value), GEMINI3_NATIVE_MAX_STEPS)
|
|
451
451
|
: Math.min(DEFAULT_MAX_STEPS, GEMINI3_NATIVE_MAX_STEPS);
|
|
452
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* Map a `@google/genai` `Candidate.finishReason` enum value onto NeuroLink's
|
|
455
|
+
* unified finish reason, mirroring anthropic.ts `mapAnthropicStopReason`.
|
|
456
|
+
*
|
|
457
|
+
* Enum values per `@google/genai` `FinishReason`: STOP, MAX_TOKENS, SAFETY,
|
|
458
|
+
* RECITATION, LANGUAGE, OTHER, BLOCKLIST, PROHIBITED_CONTENT, SPII,
|
|
459
|
+
* MALFORMED_FUNCTION_CALL, IMAGE_SAFETY, UNEXPECTED_TOOL_CALL,
|
|
460
|
+
* FINISH_REASON_UNSPECIFIED. Unknown / unset / non-terminal values default to
|
|
461
|
+
* "stop" (a clean completion is the safe assumption).
|
|
462
|
+
*
|
|
463
|
+
* Returns a plain string (not a `{ unified, raw }` object): the Vertex result
|
|
464
|
+
* builders and the consuming layer (neurolink.ts `finishReason || "unknown"`
|
|
465
|
+
* and `finishReason === "length"`) compare against plain strings.
|
|
466
|
+
*/
|
|
467
|
+
export function mapGeminiFinishReason(raw) {
|
|
468
|
+
switch (raw) {
|
|
469
|
+
case "MAX_TOKENS":
|
|
470
|
+
return "length";
|
|
471
|
+
case "MALFORMED_FUNCTION_CALL":
|
|
472
|
+
case "UNEXPECTED_TOOL_CALL":
|
|
473
|
+
return "tool-calls";
|
|
474
|
+
case "SAFETY":
|
|
475
|
+
case "RECITATION":
|
|
476
|
+
case "BLOCKLIST":
|
|
477
|
+
case "PROHIBITED_CONTENT":
|
|
478
|
+
case "SPII":
|
|
479
|
+
case "IMAGE_SAFETY":
|
|
480
|
+
return "content-filter";
|
|
481
|
+
default:
|
|
482
|
+
return "stop";
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Append a step's text to a running cross-step accumulator, ignoring empty
|
|
487
|
+
* steps and inserting a single newline between non-empty contributions.
|
|
488
|
+
*
|
|
489
|
+
* The native Gemini loops overwrite per-step text into `lastStepText`, so when
|
|
490
|
+
* the loop is force-terminated by the step cap the intermediate tool-step prose
|
|
491
|
+
* is lost and a canned placeholder becomes the answer. Accumulating here mirrors
|
|
492
|
+
* the Vertex-Claude loop's `aggregatedTurnText += block.text` so the gathered
|
|
493
|
+
* text can be surfaced at the maxSteps-exhaustion exit instead of the placeholder.
|
|
494
|
+
*/
|
|
495
|
+
export function appendStepText(accumulated, stepText) {
|
|
496
|
+
if (!stepText) {
|
|
497
|
+
return accumulated;
|
|
498
|
+
}
|
|
499
|
+
return accumulated ? `${accumulated}\n${stepText}` : stepText;
|
|
500
|
+
}
|
|
453
501
|
/**
|
|
454
502
|
* Process stream chunks to extract raw response parts, function calls, and usage metadata.
|
|
455
503
|
*
|
|
@@ -160,6 +160,22 @@ export declare class GoogleVertexProvider extends BaseProvider {
|
|
|
160
160
|
* This bypasses @ai-sdk/google-vertex to properly handle thought_signature
|
|
161
161
|
*/
|
|
162
162
|
private executeNativeGemini3Generate;
|
|
163
|
+
/**
|
|
164
|
+
* One-shot, tools-disabled model call used when a native Gemini agentic loop
|
|
165
|
+
* is force-terminated by the step cap with no text produced. Lets the model
|
|
166
|
+
* synthesize a final answer from the function results already in `contents`
|
|
167
|
+
* instead of returning a canned placeholder (Bug 1, part b).
|
|
168
|
+
*
|
|
169
|
+
* Tools are disabled by OMITTING `config.tools` — the codebase's established
|
|
170
|
+
* mechanism. `@google/genai`'s `FunctionCallingConfigMode.NONE` is documented
|
|
171
|
+
* as equivalent to passing no function declarations, and `functionCallingConfig`
|
|
172
|
+
* is not used anywhere in this codebase. When the structured-output
|
|
173
|
+
* (`final_result`) pattern was active, a trailing instruction countermands the
|
|
174
|
+
* earlier "you MUST call final_result" directive so the model answers in plain
|
|
175
|
+
* text. Never throws — returns empty text so the caller falls back to the
|
|
176
|
+
* placeholder, guaranteeing no new failure path.
|
|
177
|
+
*/
|
|
178
|
+
private synthesizeFinalAnswerWithoutTools;
|
|
163
179
|
/**
|
|
164
180
|
* Create native AnthropicVertex client for Claude models
|
|
165
181
|
*/
|
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
1878
|
-
|
|
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
|
|
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
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
`
|
|
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
|