@juspay/neurolink 13.16.1 → 13.17.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 +3 -3
- package/dist/adapters/providerImageAdapter.js +29 -495
- package/dist/browser/neurolink.min.js +464 -470
- package/dist/constants/contextWindows.js +13 -328
- package/dist/constants/enums.d.ts +1 -0
- package/dist/constants/enums.js +2 -1
- package/dist/context/anthropicLoopGuard.d.ts +1 -1
- package/dist/context/anthropicLoopGuard.js +2 -1
- package/dist/core/constants.js +6 -53
- package/dist/core/geminiLoopAdapter.js +8 -6
- package/dist/core/modules/structuredOutputPolicy.d.ts +0 -21
- package/dist/core/modules/structuredOutputPolicy.js +4 -54
- package/dist/factories/providerDescriptors.js +3 -2
- package/dist/factories/providerRegistry.js +3 -2
- package/dist/models/manifests/anthropic.d.ts +1 -1
- package/dist/models/manifests/anthropic.js +1 -1
- package/dist/models/manifests/azure.js +9 -9
- package/dist/models/manifests/ollama.js +13 -0
- package/dist/models/manifests/openai.d.ts +1 -1
- package/dist/models/manifests/openai.js +1 -1
- package/dist/models/modelRegistry.js +17 -42
- package/dist/providers/amazonBedrock/client.js +9 -37
- package/dist/providers/amazonBedrock/loopAdapter.js +3 -11
- package/dist/providers/anthropic/client.js +42 -97
- package/dist/providers/anthropic/loopAdapter.js +24 -22
- package/dist/providers/anthropic/structuredOutput.d.ts +0 -2
- package/dist/providers/anthropic/structuredOutput.js +3 -5
- package/dist/providers/azureOpenai.js +4 -22
- package/dist/providers/catalog/loader.js +12 -25
- package/dist/providers/cohere.js +5 -30
- package/dist/providers/googleAiStudio/client.js +12 -61
- package/dist/providers/googleNativeGemini3/utils.js +43 -102
- package/dist/providers/googleVertex/client.js +123 -184
- package/dist/providers/litellm/client.js +10 -50
- package/dist/providers/llamaCpp.js +4 -15
- package/dist/providers/lmStudio.js +9 -14
- package/dist/providers/nvidiaNim/client.js +4 -62
- package/dist/providers/ollama/client.js +14 -54
- package/dist/providers/ollama/constants.d.ts +1 -3
- package/dist/providers/ollama/constants.js +2 -3
- package/dist/providers/openAI/client.js +5 -57
- package/dist/providers/openRouter/client.js +4 -65
- package/dist/providers/openaiChatCompletionsBase.js +34 -58
- package/dist/providers/openaiChatCompletionsClient.js +17 -57
- package/dist/providers/openaiCompatible/client.js +9 -36
- package/dist/providers/rules/README.md +100 -0
- package/dist/providers/rules/anthropic.json +104 -0
- package/dist/providers/rules/azure.json +13 -0
- package/dist/providers/rules/bedrock.json +48 -0
- package/dist/providers/rules/cohere.json +32 -0
- package/dist/providers/rules/credentials.json +34 -0
- package/dist/providers/rules/errorRules.d.ts +17 -0
- package/dist/providers/rules/errorRules.js +197 -0
- package/dist/providers/rules/errorSignals.d.ts +2 -0
- package/dist/providers/rules/errorSignals.js +28 -0
- package/dist/providers/rules/fallbackModel.d.ts +4 -0
- package/dist/providers/rules/fallbackModel.js +21 -0
- package/dist/providers/rules/finalResultTool.d.ts +11 -0
- package/dist/providers/rules/finalResultTool.js +17 -0
- package/dist/providers/rules/finishReasons.d.ts +10 -0
- package/dist/providers/rules/finishReasons.js +28 -0
- package/dist/providers/rules/gemini.json +92 -0
- package/dist/providers/rules/google-ai.json +55 -0
- package/dist/providers/rules/limits.json +338 -0
- package/dist/providers/rules/litellm.json +36 -0
- package/dist/providers/rules/llamacpp.json +23 -0
- package/dist/providers/rules/lm-studio.json +21 -0
- package/dist/providers/rules/model-families.json +109 -0
- package/dist/providers/rules/modelFamilies.d.ts +10 -0
- package/dist/providers/rules/modelFamilies.js +45 -0
- package/dist/providers/rules/nvidia-nim.json +43 -0
- package/dist/providers/rules/ollama.json +37 -0
- package/dist/providers/rules/openai-chat.json +66 -0
- package/dist/providers/rules/openai-compatible.json +31 -0
- package/dist/providers/rules/openai.json +38 -0
- package/dist/providers/rules/openrouter.json +57 -0
- package/dist/providers/rules/proxy.json +44 -0
- package/dist/providers/rules/registry.d.ts +7 -0
- package/dist/providers/rules/registry.js +76 -0
- package/dist/providers/rules/requestDefaults.d.ts +7 -0
- package/dist/providers/rules/requestDefaults.js +9 -0
- package/dist/providers/rules/requestFields.d.ts +18 -0
- package/dist/providers/rules/requestFields.js +38 -0
- package/dist/providers/rules/sagemaker.json +88 -0
- package/dist/providers/rules/schema.d.ts +234 -0
- package/dist/providers/rules/schema.js +352 -0
- package/dist/providers/rules/structured-output.json +36 -0
- package/dist/providers/rules/thinkingLimits.d.ts +7 -0
- package/dist/providers/rules/thinkingLimits.js +9 -0
- package/dist/providers/rules/toolRules.d.ts +16 -0
- package/dist/providers/rules/toolRules.js +95 -0
- package/dist/providers/rules/usage.json +51 -0
- package/dist/providers/rules/vertex.json +65 -0
- package/dist/providers/rules/vision.json +505 -0
- package/dist/providers/rules/wireUsage.d.ts +38 -0
- package/dist/providers/rules/wireUsage.js +82 -0
- package/dist/providers/sagemaker/language-model.js +6 -33
- package/dist/providers/sagemaker/parsers.js +13 -48
- package/dist/providers/sagemaker/streaming.js +9 -16
- package/dist/proxy/claudeFormat.js +3 -16
- package/dist/proxy/geminiFormat.js +3 -9
- package/dist/proxy/openaiFormat.js +3 -16
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/providerRules.d.ts +407 -0
- package/dist/types/providerRules.js +10 -0
- package/dist/utils/errorClassifier.d.ts +2 -2
- package/dist/utils/errorClassifier.js +3 -2
- package/dist/utils/modelDetection.d.ts +1 -1
- package/dist/utils/modelDetection.js +30 -71
- package/dist/utils/providerConfig.d.ts +10 -9
- package/dist/utils/providerConfig.js +26 -25
- package/dist/utils/providerHealth.js +1 -0
- package/dist/utils/thinkingConfig.d.ts +1 -1
- package/dist/utils/thinkingConfig.js +8 -8
- package/dist/utils/tokenUtils.d.ts +12 -38
- package/dist/utils/tokenUtils.js +44 -127
- package/docs-site/static/search-index.json +5 -5
- package/package.json +2 -1
- package/dist/providers/localRuntimeOpenAICompat.d.ts +0 -16
- package/dist/providers/localRuntimeOpenAICompat.js +0 -26
|
@@ -15,8 +15,7 @@ import { ModelConfigurationManager } from "../../core/modelConfiguration.js";
|
|
|
15
15
|
import { isSchemaComplexityError } from "../../core/modules/structuredOutputPolicy.js";
|
|
16
16
|
import { redactUrlForError, stringifyContentSafe, } from "../../utils/logSanitize.js";
|
|
17
17
|
import { googleSdkProxyHttpOptions } from "../../proxy/proxyFetch.js";
|
|
18
|
-
import { AuthenticationError,
|
|
19
|
-
import { classifyProviderError, messageNamesStatus, } from "../../utils/errorClassifier.js";
|
|
18
|
+
import { AuthenticationError, ProviderError } from "../../types/index.js";
|
|
20
19
|
import { ERROR_CODES, NeuroLinkError } from "../../utils/errorHandling.js";
|
|
21
20
|
import { applyVertexAnthropicCacheBreakpoints } from "../../utils/anthropicCacheBreakpoints.js";
|
|
22
21
|
import { FileDetector } from "../../utils/fileDetector.js";
|
|
@@ -55,6 +54,20 @@ import { resolveToolExecutionRecords } from "../../core/toolExecutionRecorder.js
|
|
|
55
54
|
import { modelSupportsForcedToolChoice, resolveSamplingParams, } from "../../models/modelRegistry.js";
|
|
56
55
|
import { sanitizeAnthropicMessagesForTrace } from "../../utils/anthropicTraceSanitizer.js";
|
|
57
56
|
import { extractToolFailureText } from "../../utils/mcpErrorText.js";
|
|
57
|
+
import { classifyWithProviderRules } from "../rules/errorRules.js";
|
|
58
|
+
import { getProviderErrorRules } from "../rules/registry.js";
|
|
59
|
+
import { getThinkingLimits } from "../rules/thinkingLimits.js";
|
|
60
|
+
import { getRequestDefault } from "../rules/requestDefaults.js";
|
|
61
|
+
import { toWireParams } from "../rules/requestFields.js";
|
|
62
|
+
import { getFinishReasonMap, translateFinishReason, } from "../rules/finishReasons.js";
|
|
63
|
+
import { FINAL_RESULT_TOOL_DESCRIPTION, FINAL_RESULT_TOOL_NAME, finalResultToolInstruction, } from "../rules/finalResultTool.js";
|
|
64
|
+
import { getWireUsage, outputWithReasoning, splitIncludedCount, } from "../rules/wireUsage.js";
|
|
65
|
+
const ANTHROPIC_MESSAGES_FINISH = getFinishReasonMap("anthropic", "messages");
|
|
66
|
+
// Sizing of the forced finalization call after a Claude-on-Vertex loop stops.
|
|
67
|
+
const FINALIZATION_MIN_TOKENS = getRequestDefault("vertex", "finalizationMinTokens");
|
|
68
|
+
const FINALIZATION_HEADROOM_TOKENS = getRequestDefault("vertex", "finalizationHeadroomTokens");
|
|
69
|
+
const GEMINI_USAGE = getWireUsage("gemini");
|
|
70
|
+
const VERTEX_ERROR_RULES = getProviderErrorRules("vertex");
|
|
58
71
|
// Import proper types for multimodal message handling
|
|
59
72
|
// Dynamic import helper for native Anthropic Vertex SDK
|
|
60
73
|
let anthropicVertexModule = null;
|
|
@@ -1299,8 +1312,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
1299
1312
|
}
|
|
1300
1313
|
const typedSchema = ensureNestedSchemaTypes(inlinedSchema);
|
|
1301
1314
|
return {
|
|
1302
|
-
name:
|
|
1303
|
-
description:
|
|
1315
|
+
name: FINAL_RESULT_TOOL_NAME,
|
|
1316
|
+
description: FINAL_RESULT_TOOL_DESCRIPTION,
|
|
1304
1317
|
input_schema: {
|
|
1305
1318
|
type: "object",
|
|
1306
1319
|
properties: typedSchema.properties || typedSchema,
|
|
@@ -1501,8 +1514,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
1501
1514
|
// Add final_result tool to the existing function declarations
|
|
1502
1515
|
const existingDeclarations = tools[0]?.functionDeclarations || [];
|
|
1503
1516
|
existingDeclarations.push({
|
|
1504
|
-
name:
|
|
1505
|
-
description:
|
|
1517
|
+
name: FINAL_RESULT_TOOL_NAME,
|
|
1518
|
+
description: FINAL_RESULT_TOOL_DESCRIPTION,
|
|
1506
1519
|
parametersJsonSchema: typedSchema,
|
|
1507
1520
|
});
|
|
1508
1521
|
tools = [{ functionDeclarations: existingDeclarations }];
|
|
@@ -1552,7 +1565,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
1552
1565
|
// Build system prompt, adding final_result instruction if needed
|
|
1553
1566
|
let effectiveSystemPrompt = options.systemPrompt || "";
|
|
1554
1567
|
if (useFinalResultTool) {
|
|
1555
|
-
const finalResultInstruction = "
|
|
1568
|
+
const finalResultInstruction = finalResultToolInstruction("callWhenReady");
|
|
1556
1569
|
effectiveSystemPrompt = effectiveSystemPrompt + finalResultInstruction;
|
|
1557
1570
|
}
|
|
1558
1571
|
if (effectiveSystemPrompt) {
|
|
@@ -1722,7 +1735,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
1722
1735
|
...(declarations ? { declarations } : {}),
|
|
1723
1736
|
...(useFinalResultTool
|
|
1724
1737
|
? {
|
|
1725
|
-
finalResultToolName:
|
|
1738
|
+
finalResultToolName: FINAL_RESULT_TOOL_NAME,
|
|
1726
1739
|
onTerminalResult: (text) => {
|
|
1727
1740
|
try {
|
|
1728
1741
|
finalResultStructuredOutput = JSON.parse(text);
|
|
@@ -1955,7 +1968,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
1955
1968
|
if (!finalText &&
|
|
1956
1969
|
(stepsTaken >= maxSteps || wasAborted || hitContextLimit)) {
|
|
1957
1970
|
hitStepLimit = stepsTaken >= maxSteps && !wasAborted;
|
|
1958
|
-
const toolCallCount = allToolCalls.filter((tc) => tc.toolName !==
|
|
1971
|
+
const toolCallCount = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
1959
1972
|
// The consumer receives text via `incrementalTextChunks`; any text the
|
|
1960
1973
|
// model emitted across steps is already preserved there. Only produce a
|
|
1961
1974
|
// terminal message when NOTHING was produced (the pure-functionCall /
|
|
@@ -2042,7 +2055,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2042
2055
|
phase: stopReason,
|
|
2043
2056
|
step: stepsTaken,
|
|
2044
2057
|
maxSteps,
|
|
2045
|
-
toolCallCount: allToolCalls.filter((tc) => tc.toolName !==
|
|
2058
|
+
toolCallCount: allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length,
|
|
2046
2059
|
elapsedMs: turnClock.elapsedMs(),
|
|
2047
2060
|
});
|
|
2048
2061
|
}
|
|
@@ -2066,12 +2079,12 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2066
2079
|
}
|
|
2067
2080
|
}
|
|
2068
2081
|
// Filter out final_result from tool calls as it's an internal pattern
|
|
2069
|
-
const externalToolCalls = allToolCalls.filter((tc) => tc.toolName !==
|
|
2070
|
-
const externalToolExecutions = toolExecutions.filter((te) => te.name !==
|
|
2082
|
+
const externalToolCalls = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME);
|
|
2083
|
+
const externalToolExecutions = toolExecutions.filter((te) => te.name !== FINAL_RESULT_TOOL_NAME);
|
|
2071
2084
|
// Gemini promptTokenCount is OVERLAPPING (already includes
|
|
2072
2085
|
// cachedContentTokenCount). Subtract once so the cached portion is billed at
|
|
2073
2086
|
// the cheaper cacheRead rate without double-counting; total is conserved.
|
|
2074
|
-
const adjustedInputTokens =
|
|
2087
|
+
const adjustedInputTokens = splitIncludedCount(totalInputTokens, totalCacheReadTokens).rest;
|
|
2075
2088
|
const result = {
|
|
2076
2089
|
stream: createTextStream(),
|
|
2077
2090
|
provider: this.providerName,
|
|
@@ -2086,7 +2099,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2086
2099
|
// candidates + thoughts), so they are folded into `output` — that is
|
|
2087
2100
|
// what calculateCost bills at the output rate — with `reasoning`
|
|
2088
2101
|
// reporting the thinking subset.
|
|
2089
|
-
output: totalOutputTokens
|
|
2102
|
+
output: outputWithReasoning(GEMINI_USAGE, totalOutputTokens, totalReasoningTokens),
|
|
2090
2103
|
total: adjustedInputTokens +
|
|
2091
2104
|
totalCacheReadTokens +
|
|
2092
2105
|
totalOutputTokens +
|
|
@@ -2358,8 +2371,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2358
2371
|
// Add final_result tool to the existing function declarations
|
|
2359
2372
|
const existingDeclarations = tools[0]?.functionDeclarations || [];
|
|
2360
2373
|
existingDeclarations.push({
|
|
2361
|
-
name:
|
|
2362
|
-
description:
|
|
2374
|
+
name: FINAL_RESULT_TOOL_NAME,
|
|
2375
|
+
description: FINAL_RESULT_TOOL_DESCRIPTION,
|
|
2363
2376
|
parametersJsonSchema: typedSchema,
|
|
2364
2377
|
});
|
|
2365
2378
|
tools = [{ functionDeclarations: existingDeclarations }];
|
|
@@ -2409,7 +2422,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2409
2422
|
// Build system prompt, adding final_result instruction if needed
|
|
2410
2423
|
let effectiveSystemPrompt = options.systemPrompt || "";
|
|
2411
2424
|
if (useFinalResultTool) {
|
|
2412
|
-
const finalResultInstruction = "
|
|
2425
|
+
const finalResultInstruction = finalResultToolInstruction("callWhenReady");
|
|
2413
2426
|
effectiveSystemPrompt = effectiveSystemPrompt + finalResultInstruction;
|
|
2414
2427
|
}
|
|
2415
2428
|
if (effectiveSystemPrompt) {
|
|
@@ -2568,7 +2581,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2568
2581
|
...(declarations ? { declarations } : {}),
|
|
2569
2582
|
...(useFinalResultTool
|
|
2570
2583
|
? {
|
|
2571
|
-
finalResultToolName:
|
|
2584
|
+
finalResultToolName: FINAL_RESULT_TOOL_NAME,
|
|
2572
2585
|
onTerminalResult: (text) => {
|
|
2573
2586
|
try {
|
|
2574
2587
|
finalResultStructuredOutput = JSON.parse(text);
|
|
@@ -2761,7 +2774,13 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2761
2774
|
throw this.handleProviderError(error);
|
|
2762
2775
|
}
|
|
2763
2776
|
}
|
|
2764
|
-
|
|
2777
|
+
// On the abort path the catch above already drained the pump. Awaiting
|
|
2778
|
+
// it again would rethrow the AbortError that branch chose to absorb, so
|
|
2779
|
+
// a turn-time-limit exit reached the caller as a bare "This operation
|
|
2780
|
+
// was aborted" instead of the turn's terminal result.
|
|
2781
|
+
if (!wasAborted) {
|
|
2782
|
+
await pump;
|
|
2783
|
+
}
|
|
2765
2784
|
if (engineResult) {
|
|
2766
2785
|
finalText = engineResult.text;
|
|
2767
2786
|
lastFinishReason = engineResult.rawStopReason ?? lastFinishReason;
|
|
@@ -2794,7 +2813,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2794
2813
|
if (!finalText &&
|
|
2795
2814
|
(stepsTaken >= maxSteps || wasAborted || hitContextLimit)) {
|
|
2796
2815
|
hitStepLimit = stepsTaken >= maxSteps && !wasAborted;
|
|
2797
|
-
const toolCallCount = allToolCalls.filter((tc) => tc.toolName !==
|
|
2816
|
+
const toolCallCount = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
2798
2817
|
if (accumulatedText) {
|
|
2799
2818
|
// Prefer the prose the model already produced across steps.
|
|
2800
2819
|
logger.warn(hitContextLimit
|
|
@@ -2878,19 +2897,19 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2878
2897
|
phase: stopReason,
|
|
2879
2898
|
step: stepsTaken,
|
|
2880
2899
|
maxSteps,
|
|
2881
|
-
toolCallCount: allToolCalls.filter((tc) => tc.toolName !==
|
|
2900
|
+
toolCallCount: allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length,
|
|
2882
2901
|
elapsedMs: turnClock.elapsedMs(),
|
|
2883
2902
|
});
|
|
2884
2903
|
}
|
|
2885
2904
|
const responseTime = Date.now() - startTime;
|
|
2886
2905
|
// Filter out final_result from tool calls and executions as it's an internal pattern
|
|
2887
|
-
const externalToolCalls = allToolCalls.filter((tc) => tc.toolName !==
|
|
2888
|
-
const externalToolExecutions = toolExecutions.filter((te) => te.name !==
|
|
2906
|
+
const externalToolCalls = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME);
|
|
2907
|
+
const externalToolExecutions = toolExecutions.filter((te) => te.name !== FINAL_RESULT_TOOL_NAME);
|
|
2889
2908
|
// Build EnhancedGenerateResult
|
|
2890
2909
|
// Gemini promptTokenCount is OVERLAPPING (already includes
|
|
2891
2910
|
// cachedContentTokenCount). Subtract once so the cached portion is billed at
|
|
2892
2911
|
// the cheaper cacheRead rate without double-counting; total is conserved.
|
|
2893
|
-
const adjustedInputTokens =
|
|
2912
|
+
const adjustedInputTokens = splitIncludedCount(totalInputTokens, totalCacheReadTokens).rest;
|
|
2894
2913
|
const result = {
|
|
2895
2914
|
content: finalText,
|
|
2896
2915
|
provider: this.providerName,
|
|
@@ -2906,7 +2925,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2906
2925
|
// candidates + thoughts), so they are folded into `output` — that is
|
|
2907
2926
|
// what calculateCost bills at the output rate — with `reasoning`
|
|
2908
2927
|
// reporting the thinking subset.
|
|
2909
|
-
output: totalOutputTokens
|
|
2928
|
+
output: outputWithReasoning(GEMINI_USAGE, totalOutputTokens, totalReasoningTokens),
|
|
2910
2929
|
total: adjustedInputTokens +
|
|
2911
2930
|
totalCacheReadTokens +
|
|
2912
2931
|
totalOutputTokens +
|
|
@@ -2986,9 +3005,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
2986
3005
|
: "";
|
|
2987
3006
|
synthConfig.systemInstruction =
|
|
2988
3007
|
baseSystemInstruction +
|
|
2989
|
-
"
|
|
2990
|
-
"final answer directly as plain text now, using the information " +
|
|
2991
|
-
"gathered so far.";
|
|
3008
|
+
finalResultToolInstruction("noLongerAvailable");
|
|
2992
3009
|
}
|
|
2993
3010
|
// Bound the whole connect + drain with a timeout. The surrounding
|
|
2994
3011
|
// try/catch only catches throws, not hangs, so without this a stalled
|
|
@@ -3169,18 +3186,19 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3169
3186
|
if (!options.thinkingConfig?.enabled || !budget) {
|
|
3170
3187
|
return undefined;
|
|
3171
3188
|
}
|
|
3172
|
-
// Two independent floors, checked
|
|
3173
|
-
// Math.max/min expression:
|
|
3174
|
-
//
|
|
3175
|
-
// thinking. When maxTokens leaves no room for both
|
|
3176
|
-
//
|
|
3177
|
-
// is omitted rather than silently coerced to
|
|
3189
|
+
// Two independent floors (anthropic.json `thinkingLimits`), checked
|
|
3190
|
+
// explicitly rather than folded into one Math.max/min expression:
|
|
3191
|
+
// Anthropic's own minimum on budget_tokens, and the reservation for the
|
|
3192
|
+
// answer that follows thinking. When maxTokens leaves no room for both
|
|
3193
|
+
// (1025..2047 with today's values) there is no valid budget, so thinking
|
|
3194
|
+
// is omitted rather than silently coerced to the minimum regardless of
|
|
3178
3195
|
// what the caller asked for.
|
|
3179
|
-
|
|
3196
|
+
const { minBudgetTokens, answerReserveTokens } = getThinkingLimits("anthropic");
|
|
3197
|
+
if (maxTokens <= answerReserveTokens) {
|
|
3180
3198
|
return undefined;
|
|
3181
3199
|
}
|
|
3182
|
-
const clamped = Math.min(budget, maxTokens -
|
|
3183
|
-
if (clamped <
|
|
3200
|
+
const clamped = Math.min(budget, maxTokens - answerReserveTokens);
|
|
3201
|
+
if (clamped < minBudgetTokens) {
|
|
3184
3202
|
return undefined;
|
|
3185
3203
|
}
|
|
3186
3204
|
return { type: "enabled", budget_tokens: clamped };
|
|
@@ -3381,8 +3399,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3381
3399
|
}
|
|
3382
3400
|
tools.push(finalResultTool);
|
|
3383
3401
|
// Add instruction to system prompt
|
|
3384
|
-
schemaSystemPromptSuffix =
|
|
3385
|
-
"\n\nIMPORTANT: You MUST call the 'final_result' tool to return your response in the required structured format. Do not respond with plain text - always use the final_result tool.";
|
|
3402
|
+
schemaSystemPromptSuffix = finalResultToolInstruction("mustCall");
|
|
3386
3403
|
logger.debug("[GoogleVertex] Added final_result tool for Anthropic structured output (stream)", {
|
|
3387
3404
|
schemaKeys: Object.keys(finalResultTool.input_schema.properties ?? {}),
|
|
3388
3405
|
totalTools: tools.length,
|
|
@@ -3411,25 +3428,20 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3411
3428
|
const streamThinking = this.buildClaudeThinkingParam(options, streamMaxTokens, forceFinalResult);
|
|
3412
3429
|
const requestParams = {
|
|
3413
3430
|
model: modelName,
|
|
3414
|
-
// Default to the model's real output ceiling (e.g. 64K for Sonnet 4.x)
|
|
3415
|
-
// instead of the legacy 4096, which silently truncated large structured
|
|
3416
|
-
// responses mid-JSON. resolveClaudeMaxTokens also clamps over-large
|
|
3417
|
-
// caller values so the native Vertex path never 400s.
|
|
3418
|
-
max_tokens: streamMaxTokens,
|
|
3419
3431
|
...(streamThinking && { thinking: streamThinking }),
|
|
3420
3432
|
messages: messages,
|
|
3421
3433
|
...(tools && tools.length > 0 && { tools }),
|
|
3422
3434
|
...(forceFinalResult && { tool_choice: { type: "any" } }),
|
|
3423
3435
|
...(systemPromptWithSchema && { system: systemPromptWithSchema }),
|
|
3424
|
-
|
|
3436
|
+
// max_tokens defaults to the model's real output ceiling (e.g. 64K for
|
|
3437
|
+
// Sonnet 4.x) instead of the legacy 4096, which silently truncated large
|
|
3438
|
+
// structured responses mid-JSON. resolveClaudeMaxTokens also clamps
|
|
3439
|
+
// over-large caller values so the native Vertex path never 400s.
|
|
3440
|
+
...toWireParams("anthropic", {
|
|
3441
|
+
maxTokens: streamMaxTokens,
|
|
3425
3442
|
temperature: streamSampling.temperature,
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
top_p: streamSampling.topP,
|
|
3429
|
-
}),
|
|
3430
|
-
...(options.stopSequences &&
|
|
3431
|
-
options.stopSequences.length > 0 && {
|
|
3432
|
-
stop_sequences: options.stopSequences,
|
|
3443
|
+
topP: streamSampling.topP,
|
|
3444
|
+
stopSequences: options.stopSequences,
|
|
3433
3445
|
}),
|
|
3434
3446
|
};
|
|
3435
3447
|
// ── Real-time streaming via stream.on('text', ...) ────────────────────
|
|
@@ -3676,7 +3688,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3676
3688
|
},
|
|
3677
3689
|
...(useFinalResultTool
|
|
3678
3690
|
? {
|
|
3679
|
-
finalResultToolName:
|
|
3691
|
+
finalResultToolName: FINAL_RESULT_TOOL_NAME,
|
|
3680
3692
|
onTerminalResult: (text) => {
|
|
3681
3693
|
// The engine ends the turn on a terminal call and hands back
|
|
3682
3694
|
// the payload as text; this loop also streams it, so the push
|
|
@@ -3878,16 +3890,16 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3878
3890
|
if (internalAbort.signal.aborted) {
|
|
3879
3891
|
wasAborted = true;
|
|
3880
3892
|
}
|
|
3881
|
-
const externalToolCallCount = allToolCalls.filter((tc) => tc.toolName !==
|
|
3893
|
+
const externalToolCallCount = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
3882
3894
|
// Clamp the terminal calls' max_tokens so prompt + output stays
|
|
3883
3895
|
// inside the model window: pre-4.5 Claude models 400 on
|
|
3884
3896
|
// input + max_tokens > window, which would defeat the very
|
|
3885
3897
|
// synthesis these calls exist for on context-capped turns. Only
|
|
3886
3898
|
// bites when the prompt is near the window (min() is a no-op on
|
|
3887
3899
|
// ordinary step-cap turns).
|
|
3888
|
-
const terminalMaxTokens = Math.max(
|
|
3900
|
+
const terminalMaxTokens = Math.max(FINALIZATION_MIN_TOKENS, Math.min(streamMaxTokens, getContextWindowSize("vertex", modelName) -
|
|
3889
3901
|
contextGuard.projectedNextPromptTokens -
|
|
3890
|
-
|
|
3902
|
+
FINALIZATION_HEADROOM_TOKENS));
|
|
3891
3903
|
if (wasAborted) {
|
|
3892
3904
|
// Budget already blown — never issue another model call. Deliver
|
|
3893
3905
|
// exactly one HONEST terminal chunk matching the actual exit
|
|
@@ -3933,21 +3945,23 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3933
3945
|
stepNumber: maxSteps + 1,
|
|
3934
3946
|
model: requestParams.model,
|
|
3935
3947
|
maxTokens: terminalMaxTokens,
|
|
3936
|
-
temperature:
|
|
3937
|
-
topP:
|
|
3948
|
+
temperature: streamSampling.temperature,
|
|
3949
|
+
topP: streamSampling.topP,
|
|
3938
3950
|
system: cachedFinal.system,
|
|
3939
3951
|
messages: cachedFinal.messages,
|
|
3940
3952
|
}, async () => {
|
|
3941
3953
|
const finalizationStream = await client.messages.stream({
|
|
3942
3954
|
...requestParams,
|
|
3943
|
-
|
|
3955
|
+
...toWireParams("anthropic", {
|
|
3956
|
+
maxTokens: terminalMaxTokens,
|
|
3957
|
+
}),
|
|
3944
3958
|
// Resized, not inherited: terminalMaxTokens can sit below the
|
|
3945
3959
|
// budget requestParams' thinking was sized for.
|
|
3946
3960
|
thinking: this.buildClaudeThinkingParam(options, terminalMaxTokens, forceFinalResult),
|
|
3947
3961
|
...(forceFinalResult && {
|
|
3948
3962
|
tool_choice: {
|
|
3949
3963
|
type: "tool",
|
|
3950
|
-
name:
|
|
3964
|
+
name: FINAL_RESULT_TOOL_NAME,
|
|
3951
3965
|
},
|
|
3952
3966
|
}),
|
|
3953
3967
|
...(cachedFinal.system !== undefined && {
|
|
@@ -3985,7 +3999,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
3985
3999
|
turnCacheUsage.read +
|
|
3986
4000
|
turnCacheUsage.creation;
|
|
3987
4001
|
lastStopReason = response.stop_reason;
|
|
3988
|
-
const forcedFinalResult = response.content.find((block) => block.type === "tool_use" &&
|
|
4002
|
+
const forcedFinalResult = response.content.find((block) => block.type === "tool_use" &&
|
|
4003
|
+
block.name === FINAL_RESULT_TOOL_NAME);
|
|
3989
4004
|
if (forcedFinalResult) {
|
|
3990
4005
|
structuredOutputRef.value = forcedFinalResult.input;
|
|
3991
4006
|
channel.push({
|
|
@@ -4078,14 +4093,16 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4078
4093
|
stepNumber: maxSteps + 1,
|
|
4079
4094
|
model: requestParams.model,
|
|
4080
4095
|
maxTokens: terminalMaxTokens,
|
|
4081
|
-
temperature:
|
|
4082
|
-
topP:
|
|
4096
|
+
temperature: streamSampling.temperature,
|
|
4097
|
+
topP: streamSampling.topP,
|
|
4083
4098
|
system: cachedBackstop.system,
|
|
4084
4099
|
messages: cachedBackstop.messages,
|
|
4085
4100
|
}, async () => {
|
|
4086
4101
|
const backstopStream = await client.messages.stream({
|
|
4087
4102
|
...requestParams,
|
|
4088
|
-
|
|
4103
|
+
...toWireParams("anthropic", {
|
|
4104
|
+
maxTokens: terminalMaxTokens,
|
|
4105
|
+
}),
|
|
4089
4106
|
// Assigned outright, never a conditional spread: spreading
|
|
4090
4107
|
// requestParams above may already carry a `thinking` sized
|
|
4091
4108
|
// for the original max_tokens, and only adding a key when
|
|
@@ -4200,7 +4217,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4200
4217
|
const deliveredNothing = aggregatedTurnText.length === 0 &&
|
|
4201
4218
|
liveTextPushedLength === 0 &&
|
|
4202
4219
|
!structuredOutputRef.value;
|
|
4203
|
-
const externalToolCallTotal = allToolCalls.filter((tc) => tc.toolName !==
|
|
4220
|
+
const externalToolCallTotal = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
4204
4221
|
if (deliveredNothing && externalToolCallTotal > 0) {
|
|
4205
4222
|
const exitMessage = hitContextLimit && !wasAborted
|
|
4206
4223
|
? buildContextCapMessage(externalToolCallTotal)
|
|
@@ -4220,14 +4237,15 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4220
4237
|
// (a mutable reference) because wrapper spreads snapshot the
|
|
4221
4238
|
// top-level getter before this line runs.
|
|
4222
4239
|
finishReasonRef.value =
|
|
4223
|
-
lastStopReason ===
|
|
4240
|
+
translateFinishReason(ANTHROPIC_MESSAGES_FINISH, lastStopReason) ===
|
|
4241
|
+
"length"
|
|
4224
4242
|
? "length"
|
|
4225
4243
|
: hitStepLimit && !synthesizedFinalAnswer
|
|
4226
4244
|
? "tool-calls"
|
|
4227
4245
|
: "stop";
|
|
4228
4246
|
metadata.finishReason = finishReasonRef.value;
|
|
4229
4247
|
metadata.responseTime = Date.now() - startTime;
|
|
4230
|
-
metadata.totalToolExecutions = allToolCalls.filter((tc) => tc.toolName !==
|
|
4248
|
+
metadata.totalToolExecutions = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
4231
4249
|
// Turn-exit discriminator + raw provider stop reason + step count —
|
|
4232
4250
|
// mirrored onto metadata (mutable reference) for wrapper spreads,
|
|
4233
4251
|
// like finishReason above.
|
|
@@ -4279,7 +4297,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4279
4297
|
maxSteps,
|
|
4280
4298
|
steps: step,
|
|
4281
4299
|
toolCallCount: metadata.totalToolExecutions,
|
|
4282
|
-
toolsCalled: toolsUsedRef.filter((name) => name !==
|
|
4300
|
+
toolsCalled: toolsUsedRef.filter((name) => name !== FINAL_RESULT_TOOL_NAME),
|
|
4283
4301
|
structuredOutput: useFinalResultTool,
|
|
4284
4302
|
...(stopReasonRef.value ? { stopReason: stopReasonRef.value } : {}),
|
|
4285
4303
|
usage: {
|
|
@@ -4352,17 +4370,17 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4352
4370
|
Object.defineProperty(result, "toolCalls", {
|
|
4353
4371
|
enumerable: true,
|
|
4354
4372
|
configurable: true,
|
|
4355
|
-
get: () => allToolCalls.filter((tc) => tc.toolName !==
|
|
4373
|
+
get: () => allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME),
|
|
4356
4374
|
});
|
|
4357
4375
|
Object.defineProperty(result, "toolsUsed", {
|
|
4358
4376
|
enumerable: true,
|
|
4359
4377
|
configurable: true,
|
|
4360
|
-
get: () => toolsUsedRef.filter((name) => name !==
|
|
4378
|
+
get: () => toolsUsedRef.filter((name) => name !== FINAL_RESULT_TOOL_NAME),
|
|
4361
4379
|
});
|
|
4362
4380
|
Object.defineProperty(result, "toolExecutions", {
|
|
4363
4381
|
enumerable: true,
|
|
4364
4382
|
configurable: true,
|
|
4365
|
-
get: () => transformToolExecutions(toolExecutions.filter((te) => te.name !==
|
|
4383
|
+
get: () => transformToolExecutions(toolExecutions.filter((te) => te.name !== FINAL_RESULT_TOOL_NAME)),
|
|
4366
4384
|
});
|
|
4367
4385
|
Object.defineProperty(result, "structuredOutput", {
|
|
4368
4386
|
enumerable: true,
|
|
@@ -4596,8 +4614,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4596
4614
|
}
|
|
4597
4615
|
tools.push(finalResultTool);
|
|
4598
4616
|
// Add instruction to system prompt
|
|
4599
|
-
schemaSystemPromptSuffix =
|
|
4600
|
-
"\n\nIMPORTANT: You MUST call the 'final_result' tool to return your response in the required structured format. Do not respond with plain text - always use the final_result tool.";
|
|
4617
|
+
schemaSystemPromptSuffix = finalResultToolInstruction("mustCall");
|
|
4601
4618
|
logger.debug("[GoogleVertex] Added final_result tool for Anthropic structured output (generate)", {
|
|
4602
4619
|
schemaKeys: Object.keys(finalResultTool.input_schema.properties ?? {}),
|
|
4603
4620
|
totalTools: tools.length,
|
|
@@ -4626,22 +4643,18 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4626
4643
|
const generateThinking = this.buildClaudeThinkingParam(options, generateMaxTokens, forceFinalResult);
|
|
4627
4644
|
const requestParams = {
|
|
4628
4645
|
model: modelName,
|
|
4629
|
-
// Default to the model's real output ceiling (see stream path note).
|
|
4630
|
-
max_tokens: generateMaxTokens,
|
|
4631
4646
|
...(generateThinking && { thinking: generateThinking }),
|
|
4632
4647
|
messages,
|
|
4633
4648
|
...(tools && tools.length > 0 && { tools }),
|
|
4634
4649
|
...(forceFinalResult && { tool_choice: { type: "any" } }),
|
|
4635
4650
|
...(systemPromptWithSchema && { system: systemPromptWithSchema }),
|
|
4636
|
-
|
|
4651
|
+
// max_tokens defaults to the model's real output ceiling (see the
|
|
4652
|
+
// stream path note).
|
|
4653
|
+
...toWireParams("anthropic", {
|
|
4654
|
+
maxTokens: generateMaxTokens,
|
|
4637
4655
|
temperature: generateSampling.temperature,
|
|
4638
|
-
|
|
4639
|
-
|
|
4640
|
-
top_p: generateSampling.topP,
|
|
4641
|
-
}),
|
|
4642
|
-
...(options.stopSequences &&
|
|
4643
|
-
options.stopSequences.length > 0 && {
|
|
4644
|
-
stop_sequences: options.stopSequences,
|
|
4656
|
+
topP: generateSampling.topP,
|
|
4657
|
+
stopSequences: options.stopSequences,
|
|
4645
4658
|
}),
|
|
4646
4659
|
};
|
|
4647
4660
|
// Handle tool calling loop with max steps
|
|
@@ -4813,7 +4826,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4813
4826
|
},
|
|
4814
4827
|
...(useFinalResultTool
|
|
4815
4828
|
? {
|
|
4816
|
-
finalResultToolName:
|
|
4829
|
+
finalResultToolName: FINAL_RESULT_TOOL_NAME,
|
|
4817
4830
|
onTerminalResult: (text) => {
|
|
4818
4831
|
try {
|
|
4819
4832
|
structuredOutput = JSON.parse(text);
|
|
@@ -4991,15 +5004,15 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
4991
5004
|
if (internalAbort.signal.aborted) {
|
|
4992
5005
|
wasAborted = true;
|
|
4993
5006
|
}
|
|
4994
|
-
const externalToolCallCount = allToolCalls.filter((tc) => tc.toolName !==
|
|
5007
|
+
const externalToolCallCount = allToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME).length;
|
|
4995
5008
|
// Clamp the terminal calls' max_tokens so prompt + output stays inside
|
|
4996
5009
|
// the model window: pre-4.5 Claude models 400 on input + max_tokens >
|
|
4997
5010
|
// window, which would defeat the very synthesis these calls exist for
|
|
4998
5011
|
// on context-capped turns. Only bites when the prompt is near the
|
|
4999
5012
|
// window (min() is a no-op on ordinary step-cap turns).
|
|
5000
|
-
const terminalMaxTokens = Math.max(
|
|
5013
|
+
const terminalMaxTokens = Math.max(FINALIZATION_MIN_TOKENS, Math.min(generateMaxTokens, getContextWindowSize("vertex", modelName) -
|
|
5001
5014
|
contextGuard.projectedNextPromptTokens -
|
|
5002
|
-
|
|
5015
|
+
FINALIZATION_HEADROOM_TOKENS));
|
|
5003
5016
|
if (wasAborted) {
|
|
5004
5017
|
// Budget already blown — never issue another model call; prefer the
|
|
5005
5018
|
// prose the model already produced (mirrors the Gemini abort path),
|
|
@@ -5042,20 +5055,22 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
5042
5055
|
stepNumber: maxSteps + 1,
|
|
5043
5056
|
model: requestParams.model,
|
|
5044
5057
|
maxTokens: terminalMaxTokens,
|
|
5045
|
-
temperature:
|
|
5046
|
-
topP:
|
|
5058
|
+
temperature: generateSampling.temperature,
|
|
5059
|
+
topP: generateSampling.topP,
|
|
5047
5060
|
system: cachedFinal.system,
|
|
5048
5061
|
messages: cachedFinal.messages,
|
|
5049
5062
|
}, () => withTimeout(client.messages.create({
|
|
5050
5063
|
...requestParams,
|
|
5051
|
-
|
|
5064
|
+
...toWireParams("anthropic", {
|
|
5065
|
+
maxTokens: terminalMaxTokens,
|
|
5066
|
+
}),
|
|
5052
5067
|
// Resized, not inherited: terminalMaxTokens can sit below the
|
|
5053
5068
|
// budget requestParams' thinking was sized for.
|
|
5054
5069
|
thinking: this.buildClaudeThinkingParam(options, terminalMaxTokens, forceFinalResult),
|
|
5055
5070
|
...(forceFinalResult && {
|
|
5056
5071
|
tool_choice: {
|
|
5057
5072
|
type: "tool",
|
|
5058
|
-
name:
|
|
5073
|
+
name: FINAL_RESULT_TOOL_NAME,
|
|
5059
5074
|
},
|
|
5060
5075
|
}),
|
|
5061
5076
|
...(cachedFinal.system !== undefined && {
|
|
@@ -5073,7 +5088,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
5073
5088
|
totalCacheCreationTokens +=
|
|
5074
5089
|
response.usage?.cache_creation_input_tokens || 0;
|
|
5075
5090
|
lastStopReason = response.stop_reason;
|
|
5076
|
-
const forcedFinalResult = response.content.find((block) => block.type === "tool_use" &&
|
|
5091
|
+
const forcedFinalResult = response.content.find((block) => block.type === "tool_use" &&
|
|
5092
|
+
block.name === FINAL_RESULT_TOOL_NAME);
|
|
5077
5093
|
if (forcedFinalResult) {
|
|
5078
5094
|
structuredOutput = forcedFinalResult.input;
|
|
5079
5095
|
finalText = JSON.stringify(structuredOutput);
|
|
@@ -5162,13 +5178,15 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
5162
5178
|
stepNumber: maxSteps + 1,
|
|
5163
5179
|
model: requestParams.model,
|
|
5164
5180
|
maxTokens: terminalMaxTokens,
|
|
5165
|
-
temperature:
|
|
5166
|
-
topP:
|
|
5181
|
+
temperature: generateSampling.temperature,
|
|
5182
|
+
topP: generateSampling.topP,
|
|
5167
5183
|
system: cachedBackstop.system,
|
|
5168
5184
|
messages: cachedBackstop.messages,
|
|
5169
5185
|
}, () => withTimeout(client.messages.create({
|
|
5170
5186
|
...requestParams,
|
|
5171
|
-
|
|
5187
|
+
...toWireParams("anthropic", {
|
|
5188
|
+
maxTokens: terminalMaxTokens,
|
|
5189
|
+
}),
|
|
5172
5190
|
// Assigned outright, never a conditional spread: spreading
|
|
5173
5191
|
// requestParams above may already carry a `thinking` sized
|
|
5174
5192
|
// for the original max_tokens, and only adding a key when
|
|
@@ -5266,8 +5284,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
5266
5284
|
args: call.args,
|
|
5267
5285
|
}))
|
|
5268
5286
|
: allToolCalls;
|
|
5269
|
-
const externalToolCalls = engineToolCalls.filter((tc) => tc.toolName !==
|
|
5270
|
-
const externalToolExecutions = toolExecutions.filter((te) => te.name !==
|
|
5287
|
+
const externalToolCalls = engineToolCalls.filter((tc) => tc.toolName !== FINAL_RESULT_TOOL_NAME);
|
|
5288
|
+
const externalToolExecutions = toolExecutions.filter((te) => te.name !== FINAL_RESULT_TOOL_NAME);
|
|
5271
5289
|
// Absolute backstop — no code path may surface empty content on a turn
|
|
5272
5290
|
// that ran tools (the consumer-facing "empty response" incident shape).
|
|
5273
5291
|
// Cause-aware: an aborted/timed-out turn gets the honest exit message,
|
|
@@ -5289,7 +5307,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
5289
5307
|
// "tool-calls" (aligned with the Gemini paths, so consumers detect
|
|
5290
5308
|
// capped turns uniformly); everything else — including a successful
|
|
5291
5309
|
// forced finalization or backstop synthesis — is a normal "stop".
|
|
5292
|
-
const resolvedFinishReason = lastStopReason ===
|
|
5310
|
+
const resolvedFinishReason = translateFinishReason(ANTHROPIC_MESSAGES_FINISH, lastStopReason) ===
|
|
5311
|
+
"length"
|
|
5293
5312
|
? "length"
|
|
5294
5313
|
: hitStepLimit && !synthesizedFinalAnswer
|
|
5295
5314
|
? "tool-calls"
|
|
@@ -6446,42 +6465,9 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
6446
6465
|
});
|
|
6447
6466
|
}
|
|
6448
6467
|
formatProviderError(error) {
|
|
6449
|
-
|
|
6450
|
-
|
|
6451
|
-
|
|
6452
|
-
: typeof errorRecord?.statusCode === "number"
|
|
6453
|
-
? errorRecord.statusCode
|
|
6454
|
-
: undefined;
|
|
6455
|
-
const rules = [
|
|
6456
|
-
{
|
|
6457
|
-
// Duck-typed on .name rather than `instanceof TimeoutError` —
|
|
6458
|
-
// Vertex's own `withTimeout` (../../utils/async/index.js) throws a
|
|
6459
|
-
// TimeoutError class distinct from the one classifyProviderError's
|
|
6460
|
-
// built-in fast path checks (../../utils/timeout.js), so that fast
|
|
6461
|
-
// path never fires for a real Vertex timeout. This rule preserves
|
|
6462
|
-
// the pre-migration duck-typed match (both classes set
|
|
6463
|
-
// `.name = "TimeoutError"`) and the original Vertex-specific
|
|
6464
|
-
// message — see task-4-report.md for the full writeup.
|
|
6465
|
-
match: (ctx) => ctx.errorName === "TimeoutError",
|
|
6466
|
-
errorClass: NetworkError,
|
|
6467
|
-
message: "Google Vertex AI request timed out. Consider increasing timeout or using a lighter model.",
|
|
6468
|
-
},
|
|
6469
|
-
{
|
|
6470
|
-
match: (ctx) => /PERMISSION_DENIED|UNAUTHENTICATED|Invalid API key/i.test(ctx.message) ||
|
|
6471
|
-
statusCode === 401 ||
|
|
6472
|
-
statusCode === 403,
|
|
6473
|
-
errorClass: AuthenticationError,
|
|
6474
|
-
message: () => `Google Vertex AI Permission Denied. Your Google Cloud credentials don't have permission to access Vertex AI. ` +
|
|
6475
|
-
`Required Steps: 1. Ensure your service account has Vertex AI User role ` +
|
|
6476
|
-
`2. Check if Vertex AI API is enabled in your project ` +
|
|
6477
|
-
`3. Verify your project ID is correct ` +
|
|
6478
|
-
`4. Confirm your location/region has Vertex AI available`,
|
|
6479
|
-
},
|
|
6480
|
-
{
|
|
6481
|
-
match: (ctx) => /NOT_FOUND|model not found|Model not found/i.test(ctx.message) ||
|
|
6482
|
-
statusCode === 404,
|
|
6483
|
-
errorClass: InvalidModelError,
|
|
6484
|
-
message: () => {
|
|
6468
|
+
return classifyWithProviderRules(error, VERTEX_ERROR_RULES, this.providerName, this.modelName, {
|
|
6469
|
+
hooks: {
|
|
6470
|
+
"vertex.modelUnavailable": () => {
|
|
6485
6471
|
const modelSuggestions = this.getModelSuggestions(this.modelName);
|
|
6486
6472
|
return (`Model '${this.modelName}' is not available in region ${this.location}. ` +
|
|
6487
6473
|
`Suggested alternatives: ${modelSuggestions}. ` +
|
|
@@ -6490,31 +6476,11 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
6490
6476
|
`3. Ensure your project has access to the model ` +
|
|
6491
6477
|
`4. For Claude models, enable Anthropic integration in Google Cloud Console`);
|
|
6492
6478
|
},
|
|
6493
|
-
|
|
6494
|
-
|
|
6495
|
-
// Rate limit / quota / capacity errors. Anthropic-on-Vertex capacity
|
|
6496
|
-
// exhaustion surfaces as overloaded_error (HTTP 529) — same
|
|
6497
|
-
// operational meaning as a 429, so classify it here instead of the
|
|
6498
|
-
// generic 5xx branch below.
|
|
6499
|
-
match: (ctx) => /QUOTA_EXCEEDED|RATE_LIMIT_EXCEEDED|rate limit/i.test(ctx.message) ||
|
|
6500
|
-
messageNamesStatus(ctx.message, 429) ||
|
|
6501
|
-
statusCode === 429 ||
|
|
6502
|
-
statusCode === 529 ||
|
|
6503
|
-
/overloaded/i.test(ctx.message),
|
|
6504
|
-
errorClass: RateLimitError,
|
|
6505
|
-
message: (ctx) => {
|
|
6506
|
-
// Surface retry guidance when the SDK error carries it.
|
|
6507
|
-
// @google/genai ApiError nests RetryInfo inside the JSON error
|
|
6508
|
-
// body's details array, so fall back to scraping retryDelay out
|
|
6509
|
-
// of the raw message.
|
|
6479
|
+
"vertex.rateLimit": (ctx) => {
|
|
6480
|
+
const errorRecord = ctx.error;
|
|
6510
6481
|
const retryDelay = typeof errorRecord?.retryDelay === "string"
|
|
6511
6482
|
? errorRecord.retryDelay
|
|
6512
6483
|
: (/["']?retryDelay["']?\s*[:=]\s*["']?(\d+(?:\.\d+)?s)/.exec(ctx.message)?.[1] ?? undefined);
|
|
6513
|
-
// Prefer the per-request context the native catches attach to the
|
|
6514
|
-
// error (this.modelName can be stale when options.model overrides
|
|
6515
|
-
// the instance default). Gemini models are force-routed to the
|
|
6516
|
-
// "global" endpoint regardless of configured location — report
|
|
6517
|
-
// the region the request actually hit.
|
|
6518
6484
|
const requestModel = typeof errorRecord?.requestModel === "string"
|
|
6519
6485
|
? errorRecord.requestModel
|
|
6520
6486
|
: this.modelName;
|
|
@@ -6532,34 +6498,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
6532
6498
|
`4. Request provisioned throughput for sustained load`);
|
|
6533
6499
|
},
|
|
6534
6500
|
},
|
|
6535
|
-
|
|
6536
|
-
match: (ctx) => /ECONNRESET|ENOTFOUND|ETIMEDOUT|ECONNREFUSED|network|connection/i.test(ctx.message),
|
|
6537
|
-
errorClass: NetworkError,
|
|
6538
|
-
message: (ctx) => `Connection error: ${ctx.message}`,
|
|
6539
|
-
},
|
|
6540
|
-
{
|
|
6541
|
-
match: (ctx) => /server error|Internal Server Error|INTERNAL|UNAVAILABLE/i.test(ctx.message) ||
|
|
6542
|
-
[500, 502, 503, 504].some((code) => messageNamesStatus(ctx.message, code)) ||
|
|
6543
|
-
(statusCode !== undefined && statusCode >= 500 && statusCode < 600),
|
|
6544
|
-
errorClass: ProviderError,
|
|
6545
|
-
message: (ctx) => `Google Vertex AI server error: ${ctx.message}. Please try again later.`,
|
|
6546
|
-
},
|
|
6547
|
-
{
|
|
6548
|
-
match: (ctx) => /INVALID_ARGUMENT/i.test(ctx.message),
|
|
6549
|
-
errorClass: ProviderError,
|
|
6550
|
-
message: (ctx) => `Google Vertex AI Invalid Request: ${ctx.message}. ` +
|
|
6551
|
-
`Check: 1. Request parameters are within model limits ` +
|
|
6552
|
-
`2. Input text is properly formatted ` +
|
|
6553
|
-
`3. Temperature and other settings are valid ` +
|
|
6554
|
-
`4. Model supports your request type`,
|
|
6555
|
-
},
|
|
6556
|
-
{
|
|
6557
|
-
match: () => true,
|
|
6558
|
-
errorClass: ProviderError,
|
|
6559
|
-
message: (ctx) => `Google Vertex AI error: ${ctx.message}`,
|
|
6560
|
-
},
|
|
6561
|
-
];
|
|
6562
|
-
return classifyProviderError(error, rules, this.providerName, this.modelName);
|
|
6501
|
+
});
|
|
6563
6502
|
}
|
|
6564
6503
|
/**
|
|
6565
6504
|
* Memory-safe cache management for model configurations
|