@juspay/neurolink 9.23.0 → 9.25.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/README.md +10 -13
- package/dist/adapters/tts/googleTTSHandler.js +26 -1
- package/dist/adapters/video/vertexVideoHandler.js +23 -17
- package/dist/cli/commands/config.d.ts +3 -3
- package/dist/cli/commands/observability.d.ts +53 -0
- package/dist/cli/commands/observability.js +453 -0
- package/dist/cli/commands/telemetry.d.ts +63 -0
- package/dist/cli/commands/telemetry.js +689 -0
- package/dist/cli/factories/commandFactory.d.ts +34 -0
- package/dist/cli/factories/commandFactory.js +321 -116
- package/dist/cli/parser.js +6 -9
- package/dist/cli/utils/formatters.d.ts +13 -0
- package/dist/cli/utils/formatters.js +23 -0
- package/dist/constants/contextWindows.js +6 -0
- package/dist/constants/enums.d.ts +6 -0
- package/dist/constants/enums.js +8 -2
- package/dist/context/budgetChecker.js +75 -48
- package/dist/context/contextCompactor.js +135 -127
- package/dist/core/baseProvider.d.ts +5 -0
- package/dist/core/baseProvider.js +158 -102
- package/dist/core/conversationMemoryInitializer.js +7 -4
- package/dist/core/conversationMemoryManager.d.ts +2 -0
- package/dist/core/conversationMemoryManager.js +6 -2
- package/dist/core/modules/GenerationHandler.d.ts +2 -2
- package/dist/core/modules/GenerationHandler.js +12 -12
- package/dist/evaluation/ragasEvaluator.js +39 -19
- package/dist/evaluation/scoring.js +46 -20
- package/dist/features/ppt/index.d.ts +1 -1
- package/dist/features/ppt/index.js +1 -1
- package/dist/features/ppt/presentationOrchestrator.js +23 -0
- package/dist/features/ppt/slideGenerator.js +13 -0
- package/dist/features/ppt/slideRenderers.d.ts +1 -1
- package/dist/features/ppt/slideRenderers.js +6 -4
- package/dist/features/ppt/slideTypeInference.d.ts +1 -1
- package/dist/features/ppt/slideTypeInference.js +75 -73
- package/dist/files/fileTools.d.ts +6 -6
- package/dist/index.d.ts +46 -12
- package/dist/index.js +79 -17
- package/dist/lib/adapters/tts/googleTTSHandler.js +26 -1
- package/dist/lib/adapters/video/vertexVideoHandler.js +23 -17
- package/dist/lib/constants/contextWindows.js +6 -0
- package/dist/lib/constants/enums.d.ts +6 -0
- package/dist/lib/constants/enums.js +8 -2
- package/dist/lib/context/budgetChecker.js +75 -48
- package/dist/lib/context/contextCompactor.js +135 -127
- package/dist/lib/core/baseProvider.d.ts +5 -0
- package/dist/lib/core/baseProvider.js +158 -102
- package/dist/lib/core/conversationMemoryInitializer.js +7 -4
- package/dist/lib/core/conversationMemoryManager.d.ts +2 -0
- package/dist/lib/core/conversationMemoryManager.js +6 -2
- package/dist/lib/core/modules/GenerationHandler.d.ts +2 -2
- package/dist/lib/core/modules/GenerationHandler.js +12 -12
- package/dist/lib/evaluation/ragasEvaluator.js +39 -19
- package/dist/lib/evaluation/scoring.js +46 -20
- package/dist/lib/features/ppt/index.d.ts +1 -1
- package/dist/lib/features/ppt/index.js +1 -1
- package/dist/lib/features/ppt/presentationOrchestrator.js +23 -0
- package/dist/lib/features/ppt/slideGenerator.js +13 -0
- package/dist/lib/features/ppt/slideRenderers.d.ts +1 -1
- package/dist/lib/features/ppt/slideRenderers.js +6 -4
- package/dist/lib/features/ppt/slideTypeInference.d.ts +1 -1
- package/dist/lib/features/ppt/slideTypeInference.js +75 -73
- package/dist/lib/files/fileTools.d.ts +6 -6
- package/dist/lib/index.d.ts +46 -12
- package/dist/lib/index.js +79 -17
- package/dist/lib/mcp/httpRateLimiter.js +39 -12
- package/dist/lib/mcp/httpRetryHandler.js +22 -1
- package/dist/lib/mcp/mcpClientFactory.js +13 -15
- package/dist/lib/memory/memoryRetrievalTools.js +22 -0
- package/dist/lib/neurolink.d.ts +64 -72
- package/dist/lib/neurolink.js +984 -566
- package/dist/lib/observability/exporterRegistry.d.ts +152 -0
- package/dist/lib/observability/exporterRegistry.js +414 -0
- package/dist/lib/observability/exporters/arizeExporter.d.ts +32 -0
- package/dist/lib/observability/exporters/arizeExporter.js +139 -0
- package/dist/lib/observability/exporters/baseExporter.d.ts +117 -0
- package/dist/lib/observability/exporters/baseExporter.js +191 -0
- package/dist/lib/observability/exporters/braintrustExporter.d.ts +30 -0
- package/dist/lib/observability/exporters/braintrustExporter.js +155 -0
- package/dist/lib/observability/exporters/datadogExporter.d.ts +37 -0
- package/dist/lib/observability/exporters/datadogExporter.js +197 -0
- package/dist/lib/observability/exporters/index.d.ts +13 -0
- package/dist/lib/observability/exporters/index.js +14 -0
- package/dist/lib/observability/exporters/laminarExporter.d.ts +48 -0
- package/dist/lib/observability/exporters/laminarExporter.js +303 -0
- package/dist/lib/observability/exporters/langfuseExporter.d.ts +47 -0
- package/dist/lib/observability/exporters/langfuseExporter.js +200 -0
- package/dist/lib/observability/exporters/langsmithExporter.d.ts +26 -0
- package/dist/lib/observability/exporters/langsmithExporter.js +124 -0
- package/dist/lib/observability/exporters/otelExporter.d.ts +39 -0
- package/dist/lib/observability/exporters/otelExporter.js +165 -0
- package/dist/lib/observability/exporters/posthogExporter.d.ts +48 -0
- package/dist/lib/observability/exporters/posthogExporter.js +288 -0
- package/dist/lib/observability/exporters/sentryExporter.d.ts +32 -0
- package/dist/lib/observability/exporters/sentryExporter.js +166 -0
- package/dist/lib/observability/index.d.ts +25 -0
- package/dist/lib/observability/index.js +32 -0
- package/dist/lib/observability/metricsAggregator.d.ts +260 -0
- package/dist/lib/observability/metricsAggregator.js +553 -0
- package/dist/lib/observability/otelBridge.d.ts +49 -0
- package/dist/lib/observability/otelBridge.js +132 -0
- package/dist/lib/observability/retryPolicy.d.ts +192 -0
- package/dist/lib/observability/retryPolicy.js +384 -0
- package/dist/lib/observability/sampling/index.d.ts +4 -0
- package/dist/lib/observability/sampling/index.js +5 -0
- package/dist/lib/observability/sampling/samplers.d.ts +116 -0
- package/dist/lib/observability/sampling/samplers.js +217 -0
- package/dist/lib/observability/spanProcessor.d.ts +129 -0
- package/dist/lib/observability/spanProcessor.js +288 -0
- package/dist/lib/observability/tokenTracker.d.ts +156 -0
- package/dist/lib/observability/tokenTracker.js +414 -0
- package/dist/lib/observability/types/exporterTypes.d.ts +250 -0
- package/dist/lib/observability/types/exporterTypes.js +6 -0
- package/dist/lib/observability/types/index.d.ts +6 -0
- package/dist/lib/observability/types/index.js +5 -0
- package/dist/lib/observability/types/spanTypes.d.ts +244 -0
- package/dist/lib/observability/types/spanTypes.js +93 -0
- package/dist/lib/observability/utils/index.d.ts +4 -0
- package/dist/lib/observability/utils/index.js +5 -0
- package/dist/lib/observability/utils/spanSerializer.d.ts +115 -0
- package/dist/lib/observability/utils/spanSerializer.js +287 -0
- package/dist/lib/providers/amazonSagemaker.d.ts +5 -4
- package/dist/lib/providers/amazonSagemaker.js +3 -4
- package/dist/lib/providers/googleVertex.d.ts +7 -0
- package/dist/lib/providers/googleVertex.js +80 -2
- package/dist/lib/rag/pipeline/RAGPipeline.d.ts +0 -5
- package/dist/lib/rag/pipeline/RAGPipeline.js +122 -87
- package/dist/lib/rag/ragIntegration.js +30 -0
- package/dist/lib/rag/retrieval/hybridSearch.js +22 -0
- package/dist/lib/server/abstract/baseServerAdapter.js +51 -19
- package/dist/lib/server/middleware/common.js +44 -12
- package/dist/lib/services/server/ai/observability/instrumentation.d.ts +2 -2
- package/dist/lib/services/server/ai/observability/instrumentation.js +10 -5
- package/dist/lib/types/cli.d.ts +18 -2
- package/dist/lib/types/conversationMemoryInterface.d.ts +2 -0
- package/dist/lib/types/generateTypes.d.ts +2 -2
- package/dist/lib/types/modelTypes.d.ts +18 -18
- package/dist/lib/types/providers.d.ts +5 -0
- package/dist/lib/utils/pricing.js +25 -1
- package/dist/lib/utils/ttsProcessor.js +74 -59
- package/dist/lib/workflow/config.d.ts +36 -36
- package/dist/lib/workflow/core/ensembleExecutor.js +10 -0
- package/dist/lib/workflow/core/judgeScorer.js +20 -2
- package/dist/lib/workflow/core/workflowRunner.js +34 -1
- package/dist/mcp/httpRateLimiter.js +39 -12
- package/dist/mcp/httpRetryHandler.js +22 -1
- package/dist/mcp/mcpClientFactory.js +13 -15
- package/dist/memory/memoryRetrievalTools.js +22 -0
- package/dist/neurolink.d.ts +64 -72
- package/dist/neurolink.js +984 -566
- package/dist/observability/FEATURE-STATUS.md +269 -0
- package/dist/observability/exporterRegistry.d.ts +152 -0
- package/dist/observability/exporterRegistry.js +413 -0
- package/dist/observability/exporters/arizeExporter.d.ts +32 -0
- package/dist/observability/exporters/arizeExporter.js +138 -0
- package/dist/observability/exporters/baseExporter.d.ts +117 -0
- package/dist/observability/exporters/baseExporter.js +190 -0
- package/dist/observability/exporters/braintrustExporter.d.ts +30 -0
- package/dist/observability/exporters/braintrustExporter.js +154 -0
- package/dist/observability/exporters/datadogExporter.d.ts +37 -0
- package/dist/observability/exporters/datadogExporter.js +196 -0
- package/dist/observability/exporters/index.d.ts +13 -0
- package/dist/observability/exporters/index.js +13 -0
- package/dist/observability/exporters/laminarExporter.d.ts +48 -0
- package/dist/observability/exporters/laminarExporter.js +302 -0
- package/dist/observability/exporters/langfuseExporter.d.ts +47 -0
- package/dist/observability/exporters/langfuseExporter.js +199 -0
- package/dist/observability/exporters/langsmithExporter.d.ts +26 -0
- package/dist/observability/exporters/langsmithExporter.js +123 -0
- package/dist/observability/exporters/otelExporter.d.ts +39 -0
- package/dist/observability/exporters/otelExporter.js +164 -0
- package/dist/observability/exporters/posthogExporter.d.ts +48 -0
- package/dist/observability/exporters/posthogExporter.js +287 -0
- package/dist/observability/exporters/sentryExporter.d.ts +32 -0
- package/dist/observability/exporters/sentryExporter.js +165 -0
- package/dist/observability/index.d.ts +25 -0
- package/dist/observability/index.js +31 -0
- package/dist/observability/metricsAggregator.d.ts +260 -0
- package/dist/observability/metricsAggregator.js +552 -0
- package/dist/observability/otelBridge.d.ts +49 -0
- package/dist/observability/otelBridge.js +131 -0
- package/dist/observability/retryPolicy.d.ts +192 -0
- package/dist/observability/retryPolicy.js +383 -0
- package/dist/observability/sampling/index.d.ts +4 -0
- package/dist/observability/sampling/index.js +4 -0
- package/dist/observability/sampling/samplers.d.ts +116 -0
- package/dist/observability/sampling/samplers.js +216 -0
- package/dist/observability/spanProcessor.d.ts +129 -0
- package/dist/observability/spanProcessor.js +287 -0
- package/dist/observability/tokenTracker.d.ts +156 -0
- package/dist/observability/tokenTracker.js +413 -0
- package/dist/observability/types/exporterTypes.d.ts +250 -0
- package/dist/observability/types/exporterTypes.js +5 -0
- package/dist/observability/types/index.d.ts +6 -0
- package/dist/observability/types/index.js +4 -0
- package/dist/observability/types/spanTypes.d.ts +244 -0
- package/dist/observability/types/spanTypes.js +92 -0
- package/dist/observability/utils/index.d.ts +4 -0
- package/dist/observability/utils/index.js +4 -0
- package/dist/observability/utils/spanSerializer.d.ts +115 -0
- package/dist/observability/utils/spanSerializer.js +286 -0
- package/dist/providers/amazonSagemaker.d.ts +5 -4
- package/dist/providers/amazonSagemaker.js +3 -4
- package/dist/providers/googleVertex.d.ts +7 -0
- package/dist/providers/googleVertex.js +80 -2
- package/dist/rag/pipeline/RAGPipeline.d.ts +0 -5
- package/dist/rag/pipeline/RAGPipeline.js +122 -87
- package/dist/rag/ragIntegration.js +30 -0
- package/dist/rag/retrieval/hybridSearch.js +22 -0
- package/dist/server/abstract/baseServerAdapter.js +51 -19
- package/dist/server/middleware/common.js +44 -12
- package/dist/services/server/ai/observability/instrumentation.d.ts +2 -2
- package/dist/services/server/ai/observability/instrumentation.js +10 -5
- package/dist/types/cli.d.ts +18 -2
- package/dist/types/conversationMemoryInterface.d.ts +2 -0
- package/dist/types/generateTypes.d.ts +2 -2
- package/dist/types/providers.d.ts +5 -0
- package/dist/utils/pricing.js +25 -1
- package/dist/utils/ttsProcessor.js +74 -59
- package/dist/workflow/config.d.ts +52 -52
- package/dist/workflow/core/ensembleExecutor.js +10 -0
- package/dist/workflow/core/judgeScorer.js +20 -2
- package/dist/workflow/core/workflowRunner.js +34 -1
- package/package.json +1 -1
|
@@ -10,6 +10,8 @@ import { TextToSpeechClient } from "@google-cloud/text-to-speech";
|
|
|
10
10
|
import { TTSError, TTS_ERROR_CODES } from "../../utils/ttsProcessor.js";
|
|
11
11
|
import { ErrorCategory, ErrorSeverity } from "../../constants/enums.js";
|
|
12
12
|
import { logger } from "../../utils/logger.js";
|
|
13
|
+
import { SpanSerializer, SpanType, SpanStatus, } from "../../observability/index.js";
|
|
14
|
+
import { getMetricsAggregator } from "../../observability/index.js";
|
|
13
15
|
export class GoogleTTSHandler {
|
|
14
16
|
client = null;
|
|
15
17
|
voicesCache = null;
|
|
@@ -68,18 +70,26 @@ export class GoogleTTSHandler {
|
|
|
68
70
|
retriable: false,
|
|
69
71
|
});
|
|
70
72
|
}
|
|
73
|
+
const span = SpanSerializer.createSpan(SpanType.TTS, "tts.google.listVoices", {
|
|
74
|
+
"tts.operation": "listVoices",
|
|
75
|
+
"tts.provider": "google",
|
|
76
|
+
});
|
|
71
77
|
try {
|
|
72
78
|
// Return cached voices if available, valid, and no language filter is specified
|
|
73
79
|
if (this.voicesCache &&
|
|
74
80
|
Date.now() - this.voicesCache.timestamp <
|
|
75
81
|
GoogleTTSHandler.CACHE_TTL_MS &&
|
|
76
82
|
!languageCode) {
|
|
83
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
84
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
77
85
|
return this.voicesCache.voices;
|
|
78
86
|
}
|
|
79
87
|
// Call Google Cloud listVoices API
|
|
80
88
|
const [response] = await this.client.listVoices(languageCode ? { languageCode } : {});
|
|
81
89
|
if (!response.voices || response.voices.length === 0) {
|
|
82
90
|
logger.warn("Google Cloud TTS returned no voices");
|
|
91
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
92
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
83
93
|
return [];
|
|
84
94
|
}
|
|
85
95
|
const voices = [];
|
|
@@ -118,9 +128,14 @@ export class GoogleTTSHandler {
|
|
|
118
128
|
if (!languageCode) {
|
|
119
129
|
this.voicesCache = { voices, timestamp: Date.now() };
|
|
120
130
|
}
|
|
131
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
132
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
121
133
|
return voices;
|
|
122
134
|
}
|
|
123
135
|
catch (err) {
|
|
136
|
+
// Record error span
|
|
137
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, err instanceof Error ? err.message : "Unknown error");
|
|
138
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
124
139
|
// Log error but return empty array for graceful degradation
|
|
125
140
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
126
141
|
logger.error(`Failed to fetch Google TTS voices: ${message}`);
|
|
@@ -144,6 +159,13 @@ export class GoogleTTSHandler {
|
|
|
144
159
|
retriable: false,
|
|
145
160
|
});
|
|
146
161
|
}
|
|
162
|
+
const voiceId = options.voice ?? "en-US-Neural2-C";
|
|
163
|
+
const span = SpanSerializer.createSpan(SpanType.TTS, "tts.google.synthesize", {
|
|
164
|
+
"tts.operation": "synthesize",
|
|
165
|
+
"tts.provider": "google",
|
|
166
|
+
"tts.voice": voiceId,
|
|
167
|
+
"tts.format": options.format ?? "mp3",
|
|
168
|
+
});
|
|
147
169
|
const startTime = Date.now();
|
|
148
170
|
try {
|
|
149
171
|
const isSSML = text.startsWith("<speak>") && text.endsWith("</speak>");
|
|
@@ -160,7 +182,6 @@ export class GoogleTTSHandler {
|
|
|
160
182
|
retriable: false,
|
|
161
183
|
});
|
|
162
184
|
}
|
|
163
|
-
const voiceId = options.voice ?? "en-US-Neural2-C";
|
|
164
185
|
const languageCode = this.extractLanguageCode(voiceId);
|
|
165
186
|
const audioEncoding = this.mapFormat(options.format ?? "mp3");
|
|
166
187
|
const request = {
|
|
@@ -204,6 +225,8 @@ export class GoogleTTSHandler {
|
|
|
204
225
|
});
|
|
205
226
|
})();
|
|
206
227
|
const latency = Date.now() - startTime;
|
|
228
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.OK);
|
|
229
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
207
230
|
return {
|
|
208
231
|
buffer,
|
|
209
232
|
format: options.format ?? "mp3",
|
|
@@ -216,6 +239,8 @@ export class GoogleTTSHandler {
|
|
|
216
239
|
};
|
|
217
240
|
}
|
|
218
241
|
catch (err) {
|
|
242
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR, err instanceof Error ? err.message : String(err));
|
|
243
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
219
244
|
if (err instanceof TTSError) {
|
|
220
245
|
throw err;
|
|
221
246
|
}
|
|
@@ -70,7 +70,14 @@ const DEFAULT_LOCATION = "us-central1";
|
|
|
70
70
|
* ```
|
|
71
71
|
*/
|
|
72
72
|
export function isVertexVideoConfigured() {
|
|
73
|
+
// Same credential detection as googleVertex.ts hasGoogleCredentials().
|
|
74
|
+
// GoogleAuth (used by getAccessToken) also supports ADC from
|
|
75
|
+
// `gcloud auth application-default login` automatically, so we only
|
|
76
|
+
// gate on the explicit env vars here — if none are set, we still
|
|
77
|
+
// allow the call through and let GoogleAuth resolve ADC at runtime.
|
|
78
|
+
// This avoids duplicating GoogleAuth's discovery logic.
|
|
73
79
|
return !!(process.env.GOOGLE_APPLICATION_CREDENTIALS ||
|
|
80
|
+
process.env.GOOGLE_APPLICATION_CREDENTIALS_NEUROLINK ||
|
|
74
81
|
process.env.GOOGLE_SERVICE_ACCOUNT_KEY ||
|
|
75
82
|
(process.env.GOOGLE_AUTH_CLIENT_EMAIL &&
|
|
76
83
|
process.env.GOOGLE_AUTH_PRIVATE_KEY));
|
|
@@ -270,21 +277,10 @@ function calculateDimensions(resolution, aspectRatio) {
|
|
|
270
277
|
* ```
|
|
271
278
|
*/
|
|
272
279
|
export async function generateVideoWithVertex(image, prompt, options = {}, region) {
|
|
273
|
-
//
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
message: "Vertex AI credentials not configured. Set GOOGLE_APPLICATION_CREDENTIALS environment variable",
|
|
278
|
-
category: ErrorCategory.CONFIGURATION,
|
|
279
|
-
severity: ErrorSeverity.HIGH,
|
|
280
|
-
retriable: false,
|
|
281
|
-
context: {
|
|
282
|
-
provider: "vertex",
|
|
283
|
-
feature: "video-generation",
|
|
284
|
-
suggestion: "Set GOOGLE_APPLICATION_CREDENTIALS to the path of your service account JSON file",
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
}
|
|
280
|
+
// Credential validation is deferred to getAccessToken() which uses
|
|
281
|
+
// GoogleAuth — it handles env vars, service accounts, AND ADC from
|
|
282
|
+
// `gcloud auth application-default login` automatically.
|
|
283
|
+
// Same pattern as googleVertex.ts — no synchronous pre-check needed.
|
|
288
284
|
const config = await getVertexConfig();
|
|
289
285
|
const project = config.project;
|
|
290
286
|
const location = region || config.location;
|
|
@@ -312,7 +308,12 @@ export async function generateVideoWithVertex(image, prompt, options = {}, regio
|
|
|
312
308
|
// Get auth token
|
|
313
309
|
const accessToken = await getAccessToken();
|
|
314
310
|
// Construct API request - predictLongRunning endpoint
|
|
315
|
-
|
|
311
|
+
// Global endpoint uses aiplatform.googleapis.com (no region prefix),
|
|
312
|
+
// same pattern as googleVertex.ts createVertexSettings
|
|
313
|
+
const apiHost = location === "global"
|
|
314
|
+
? "aiplatform.googleapis.com"
|
|
315
|
+
: `${location}-aiplatform.googleapis.com`;
|
|
316
|
+
const endpoint = `https://${apiHost}/v1/projects/${project}/locations/${location}/publishers/google/models/${VEO_MODEL}:predictLongRunning`;
|
|
316
317
|
// Request body structure (verified working from video.js reference)
|
|
317
318
|
const requestBody = {
|
|
318
319
|
instances: [
|
|
@@ -727,7 +728,12 @@ export async function generateTransitionWithVertex(firstFrame, lastFrame, prompt
|
|
|
727
728
|
*/
|
|
728
729
|
async function pollOperation(modelOrEndpoint, operationName, accessToken, project, location, timeoutMs) {
|
|
729
730
|
const startTime = Date.now();
|
|
730
|
-
|
|
731
|
+
// Global endpoint uses aiplatform.googleapis.com (no region prefix),
|
|
732
|
+
// same pattern as the predictLongRunning endpoint at line 374
|
|
733
|
+
const pollHost = location === "global"
|
|
734
|
+
? "aiplatform.googleapis.com"
|
|
735
|
+
: `${location}-aiplatform.googleapis.com`;
|
|
736
|
+
const pollEndpoint = `https://${pollHost}/v1/projects/${project}/locations/${location}/publishers/google/models/${modelOrEndpoint}:fetchPredictOperation`;
|
|
731
737
|
while (Date.now() - startTime < timeoutMs) {
|
|
732
738
|
const result = await makePollRequest(pollEndpoint, operationName, accessToken);
|
|
733
739
|
if (result.done) {
|
|
@@ -88,7 +88,10 @@ export const MODEL_CONTEXT_WINDOWS = {
|
|
|
88
88
|
},
|
|
89
89
|
"google-ai": {
|
|
90
90
|
_default: 1_048_576,
|
|
91
|
+
"gemini-3.1-pro": 1_048_576,
|
|
91
92
|
"gemini-3.1-pro-preview": 1_048_576,
|
|
93
|
+
"gemini-3.1-flash": 1_048_576,
|
|
94
|
+
"gemini-3.1-flash-lite": 1_048_576,
|
|
92
95
|
"gemini-3-pro-preview": 1_048_576,
|
|
93
96
|
"gemini-3-pro-image-preview": 65_536,
|
|
94
97
|
"gemini-3-flash-preview": 1_048_576,
|
|
@@ -114,7 +117,10 @@ export const MODEL_CONTEXT_WINDOWS = {
|
|
|
114
117
|
"claude-opus-4-20250514": 200_000,
|
|
115
118
|
"claude-opus-4": 200_000,
|
|
116
119
|
// Gemini on Vertex
|
|
120
|
+
"gemini-3.1-pro": 1_048_576,
|
|
117
121
|
"gemini-3.1-pro-preview": 1_048_576,
|
|
122
|
+
"gemini-3.1-flash": 1_048_576,
|
|
123
|
+
"gemini-3.1-flash-lite": 1_048_576,
|
|
118
124
|
"gemini-3-pro-preview": 1_048_576,
|
|
119
125
|
"gemini-3-pro-latest": 1_048_576,
|
|
120
126
|
"gemini-3-flash-preview": 1_048_576,
|
|
@@ -234,7 +234,10 @@ export declare enum VertexModels {
|
|
|
234
234
|
CLAUDE_3_SONNET = "claude-3-sonnet-20240229",
|
|
235
235
|
CLAUDE_3_OPUS = "claude-3-opus-20240229",
|
|
236
236
|
CLAUDE_3_HAIKU = "claude-3-haiku-20240307",
|
|
237
|
+
GEMINI_3_1_PRO = "gemini-3.1-pro",
|
|
237
238
|
GEMINI_3_1_PRO_PREVIEW = "gemini-3.1-pro-preview",
|
|
239
|
+
GEMINI_3_1_FLASH = "gemini-3.1-flash",
|
|
240
|
+
GEMINI_3_1_FLASH_LITE = "gemini-3.1-flash-lite",
|
|
238
241
|
GEMINI_3_PRO = "gemini-3-pro",
|
|
239
242
|
GEMINI_3_PRO_PREVIEW_11_2025 = "gemini-3-pro-preview-11-2025",
|
|
240
243
|
GEMINI_3_PRO_LATEST = "gemini-3-pro-latest",
|
|
@@ -258,7 +261,10 @@ export declare enum VertexModels {
|
|
|
258
261
|
* Supported Models for Google AI Studio
|
|
259
262
|
*/
|
|
260
263
|
export declare enum GoogleAIModels {
|
|
264
|
+
GEMINI_3_1_PRO = "gemini-3.1-pro",
|
|
261
265
|
GEMINI_3_1_PRO_PREVIEW = "gemini-3.1-pro-preview",
|
|
266
|
+
GEMINI_3_1_FLASH = "gemini-3.1-flash",
|
|
267
|
+
GEMINI_3_1_FLASH_LITE = "gemini-3.1-flash-lite",
|
|
262
268
|
GEMINI_3_PRO_PREVIEW = "gemini-3-pro-preview",
|
|
263
269
|
GEMINI_3_PRO_IMAGE_PREVIEW = "gemini-3-pro-image-preview",
|
|
264
270
|
GEMINI_3_FLASH = "gemini-3-flash",
|
|
@@ -331,8 +331,11 @@ export var VertexModels;
|
|
|
331
331
|
VertexModels["CLAUDE_3_SONNET"] = "claude-3-sonnet-20240229";
|
|
332
332
|
VertexModels["CLAUDE_3_OPUS"] = "claude-3-opus-20240229";
|
|
333
333
|
VertexModels["CLAUDE_3_HAIKU"] = "claude-3-haiku-20240307";
|
|
334
|
-
// Gemini 3.1 Series (Released
|
|
334
|
+
// Gemini 3.1 Series (Released March 2026)
|
|
335
|
+
VertexModels["GEMINI_3_1_PRO"] = "gemini-3.1-pro";
|
|
335
336
|
VertexModels["GEMINI_3_1_PRO_PREVIEW"] = "gemini-3.1-pro-preview";
|
|
337
|
+
VertexModels["GEMINI_3_1_FLASH"] = "gemini-3.1-flash";
|
|
338
|
+
VertexModels["GEMINI_3_1_FLASH_LITE"] = "gemini-3.1-flash-lite";
|
|
336
339
|
// Gemini 3 Series (Preview)
|
|
337
340
|
VertexModels["GEMINI_3_PRO"] = "gemini-3-pro";
|
|
338
341
|
VertexModels["GEMINI_3_PRO_PREVIEW_11_2025"] = "gemini-3-pro-preview-11-2025";
|
|
@@ -361,8 +364,11 @@ export var VertexModels;
|
|
|
361
364
|
*/
|
|
362
365
|
export var GoogleAIModels;
|
|
363
366
|
(function (GoogleAIModels) {
|
|
364
|
-
// Gemini 3.1 Series (Released
|
|
367
|
+
// Gemini 3.1 Series (Released March 2026)
|
|
368
|
+
GoogleAIModels["GEMINI_3_1_PRO"] = "gemini-3.1-pro";
|
|
365
369
|
GoogleAIModels["GEMINI_3_1_PRO_PREVIEW"] = "gemini-3.1-pro-preview";
|
|
370
|
+
GoogleAIModels["GEMINI_3_1_FLASH"] = "gemini-3.1-flash";
|
|
371
|
+
GoogleAIModels["GEMINI_3_1_FLASH_LITE"] = "gemini-3.1-flash-lite";
|
|
366
372
|
// Gemini 3 Series (Preview)
|
|
367
373
|
GoogleAIModels["GEMINI_3_PRO_PREVIEW"] = "gemini-3-pro-preview";
|
|
368
374
|
GoogleAIModels["GEMINI_3_PRO_IMAGE_PREVIEW"] = "gemini-3-pro-image-preview";
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { getAvailableInputTokens } from "../constants/contextWindows.js";
|
|
10
10
|
import { estimateMessagesTokens, estimateTokens, TOKENS_PER_MESSAGE, } from "../utils/tokenEstimation.js";
|
|
11
|
+
import { SpanSerializer, SpanType, SpanStatus, } from "../observability/index.js";
|
|
12
|
+
import { getMetricsAggregator } from "../observability/index.js";
|
|
11
13
|
/** Default compaction threshold (80% of available input) */
|
|
12
14
|
const DEFAULT_COMPACTION_THRESHOLD = 0.8;
|
|
13
15
|
/** Estimated tokens per tool definition */
|
|
@@ -20,53 +22,78 @@ const TOKENS_PER_TOOL_DEFINITION = 200;
|
|
|
20
22
|
* against available input space.
|
|
21
23
|
*/
|
|
22
24
|
export function checkContextBudget(params) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
25
|
+
const span = SpanSerializer.createSpan(SpanType.CONTEXT_COMPACTION, "context.budgetCheck", {
|
|
26
|
+
"context.operation": "budgetCheck",
|
|
27
|
+
});
|
|
28
|
+
const startTime = Date.now();
|
|
29
|
+
try {
|
|
30
|
+
const { provider, model, maxTokens, systemPrompt, conversationMessages, currentPrompt, toolDefinitions, fileAttachments, compactionThreshold = DEFAULT_COMPACTION_THRESHOLD, } = params;
|
|
31
|
+
const availableInputTokens = getAvailableInputTokens(provider, model, maxTokens);
|
|
32
|
+
// Estimate each category
|
|
33
|
+
const systemPromptTokens = systemPrompt
|
|
34
|
+
? estimateTokens(systemPrompt, provider) + TOKENS_PER_MESSAGE
|
|
35
|
+
: 0;
|
|
36
|
+
const conversationHistoryTokens = conversationMessages?.length
|
|
37
|
+
? estimateMessagesTokens(conversationMessages, provider)
|
|
38
|
+
: 0;
|
|
39
|
+
const currentPromptTokens = currentPrompt
|
|
40
|
+
? estimateTokens(currentPrompt, provider) + TOKENS_PER_MESSAGE
|
|
41
|
+
: 0;
|
|
42
|
+
const toolDefinitionTokens = toolDefinitions?.length
|
|
43
|
+
? toolDefinitions.reduce((sum, tool) => {
|
|
44
|
+
try {
|
|
45
|
+
const serialized = JSON.stringify(tool);
|
|
46
|
+
return sum + estimateTokens(serialized, provider);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return sum + TOKENS_PER_TOOL_DEFINITION;
|
|
50
|
+
}
|
|
51
|
+
}, 0)
|
|
52
|
+
: 0;
|
|
53
|
+
const fileAttachmentTokens = fileAttachments?.length
|
|
54
|
+
? fileAttachments.reduce((sum, file) => sum + estimateTokens(file.content, provider), 0)
|
|
55
|
+
: 0;
|
|
56
|
+
const estimatedInputTokens = systemPromptTokens +
|
|
57
|
+
conversationHistoryTokens +
|
|
58
|
+
currentPromptTokens +
|
|
59
|
+
toolDefinitionTokens +
|
|
60
|
+
fileAttachmentTokens;
|
|
61
|
+
const usageRatio = availableInputTokens > 0
|
|
62
|
+
? estimatedInputTokens / availableInputTokens
|
|
63
|
+
: 1;
|
|
64
|
+
const withinBudget = estimatedInputTokens <= availableInputTokens;
|
|
65
|
+
const shouldCompact = usageRatio >= compactionThreshold;
|
|
66
|
+
const result = {
|
|
67
|
+
withinBudget,
|
|
68
|
+
estimatedInputTokens,
|
|
69
|
+
availableInputTokens,
|
|
70
|
+
usageRatio,
|
|
71
|
+
shouldCompact,
|
|
72
|
+
breakdown: {
|
|
73
|
+
systemPrompt: systemPromptTokens,
|
|
74
|
+
conversationHistory: conversationHistoryTokens,
|
|
75
|
+
currentPrompt: currentPromptTokens,
|
|
76
|
+
toolDefinitions: toolDefinitionTokens,
|
|
77
|
+
fileAttachments: fileAttachmentTokens,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
span.durationMs = Date.now() - startTime;
|
|
81
|
+
const endedSpan = SpanSerializer.endSpan(SpanSerializer.updateAttributes(span, {
|
|
82
|
+
"context.budgetUsage": usageRatio,
|
|
83
|
+
"context.triggered": shouldCompact,
|
|
84
|
+
"context.estimatedTokens": estimatedInputTokens,
|
|
85
|
+
"context.availableTokens": availableInputTokens,
|
|
86
|
+
}), SpanStatus.OK);
|
|
87
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
span.durationMs = Date.now() - startTime;
|
|
92
|
+
const endedSpan = SpanSerializer.endSpan(span, SpanStatus.ERROR);
|
|
93
|
+
endedSpan.statusMessage =
|
|
94
|
+
error instanceof Error ? error.message : String(error);
|
|
95
|
+
getMetricsAggregator().recordSpan(endedSpan);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
71
98
|
}
|
|
72
99
|
//# sourceMappingURL=budgetChecker.js.map
|