@juspay/neurolink 13.16.2 → 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
|
@@ -10,6 +10,13 @@ import { SageMakerError } from "./errors.js";
|
|
|
10
10
|
import { logger } from "../../utils/logger.js";
|
|
11
11
|
import { randomUUID } from "crypto";
|
|
12
12
|
import { estimateTokens } from "../../utils/tokenEstimation.js";
|
|
13
|
+
import { getFinishReasonMap, translateFinishReason, } from "../rules/finishReasons.js";
|
|
14
|
+
import { getUsageDialect, readUsageCounts } from "../rules/wireUsage.js";
|
|
15
|
+
const HF_USAGE = getUsageDialect("sagemaker", "huggingface");
|
|
16
|
+
const OPENAI_USAGE = getUsageDialect("sagemaker", "openai");
|
|
17
|
+
const CUSTOM_USAGE = getUsageDialect("sagemaker", "custom");
|
|
18
|
+
const SAGEMAKER_HF_STREAM_FINISH = getFinishReasonMap("sagemaker", "huggingfaceStream");
|
|
19
|
+
const SAGEMAKER_OPENAI_STREAM_FINISH = getFinishReasonMap("sagemaker", "openaiStream");
|
|
13
20
|
/**
|
|
14
21
|
* Constants for JSON parsing and validation
|
|
15
22
|
*/
|
|
@@ -275,27 +282,10 @@ export class HuggingFaceStreamParser extends BaseStreamingParser {
|
|
|
275
282
|
if (!details.tokens) {
|
|
276
283
|
return undefined;
|
|
277
284
|
}
|
|
278
|
-
|
|
279
|
-
const promptTokens = Number(tokens.input) || 0;
|
|
280
|
-
const completionTokens = Number(tokens.generated) || 0;
|
|
281
|
-
return {
|
|
282
|
-
promptTokens,
|
|
283
|
-
completionTokens,
|
|
284
|
-
// Endpoints that omit the total must not report 0 next to real counts.
|
|
285
|
-
total: Number(tokens.total) || promptTokens + completionTokens,
|
|
286
|
-
};
|
|
285
|
+
return readUsageCounts(HF_USAGE, details.tokens);
|
|
287
286
|
}
|
|
288
287
|
mapFinishReason(reason) {
|
|
289
|
-
|
|
290
|
-
case "stop":
|
|
291
|
-
return "stop";
|
|
292
|
-
case "length":
|
|
293
|
-
return "length";
|
|
294
|
-
case "eos_token":
|
|
295
|
-
return "stop";
|
|
296
|
-
default:
|
|
297
|
-
return "unknown";
|
|
298
|
-
}
|
|
288
|
+
return translateFinishReason(SAGEMAKER_HF_STREAM_FINISH, reason);
|
|
299
289
|
}
|
|
300
290
|
}
|
|
301
291
|
/**
|
|
@@ -450,28 +440,11 @@ export class LlamaStreamParser extends BaseStreamingParser {
|
|
|
450
440
|
return toolCall;
|
|
451
441
|
}
|
|
452
442
|
parseLlamaUsage(usage) {
|
|
453
|
-
|
|
454
|
-
const completionTokens = Number(usage.completion_tokens) || 0;
|
|
455
|
-
return {
|
|
456
|
-
promptTokens,
|
|
457
|
-
completionTokens,
|
|
458
|
-
// Endpoints that omit total_tokens must not report 0 next to real counts.
|
|
459
|
-
total: Number(usage.total_tokens) || promptTokens + completionTokens,
|
|
460
|
-
};
|
|
443
|
+
return readUsageCounts(OPENAI_USAGE, usage);
|
|
461
444
|
}
|
|
462
445
|
mapFinishReason(reason) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
return "stop";
|
|
466
|
-
case "length":
|
|
467
|
-
return "length";
|
|
468
|
-
case "function_call":
|
|
469
|
-
return "tool-calls";
|
|
470
|
-
case "content_filter":
|
|
471
|
-
return "content-filter";
|
|
472
|
-
default:
|
|
473
|
-
return reason ? "unknown" : undefined;
|
|
474
|
-
}
|
|
446
|
+
return (translateFinishReason(SAGEMAKER_OPENAI_STREAM_FINISH, reason) ??
|
|
447
|
+
undefined);
|
|
475
448
|
}
|
|
476
449
|
}
|
|
477
450
|
/**
|
|
@@ -574,15 +547,7 @@ export class CustomStreamParser extends BaseStreamingParser {
|
|
|
574
547
|
};
|
|
575
548
|
}
|
|
576
549
|
parseCustomUsage(data) {
|
|
577
|
-
|
|
578
|
-
const promptTokens = Number(usage.prompt_tokens || usage.input_tokens) || 0;
|
|
579
|
-
const completionTokens = Number(usage.completion_tokens || usage.output_tokens) || 0;
|
|
580
|
-
return {
|
|
581
|
-
promptTokens,
|
|
582
|
-
completionTokens,
|
|
583
|
-
// Endpoints that omit total_tokens must not report 0 next to real counts.
|
|
584
|
-
total: Number(usage.total_tokens) || promptTokens + completionTokens,
|
|
585
|
-
};
|
|
550
|
+
return readUsageCounts(CUSTOM_USAGE, data.usage || data.tokens || {});
|
|
586
551
|
}
|
|
587
552
|
}
|
|
588
553
|
/**
|
|
@@ -10,6 +10,9 @@ import { logger } from "../../utils/logger.js";
|
|
|
10
10
|
import { estimateTokens } from "../../utils/tokenEstimation.js";
|
|
11
11
|
import { createSageMakerDetector } from "./detection.js";
|
|
12
12
|
import { StreamingParserFactory } from "./parsers.js";
|
|
13
|
+
import { getUsageDialect, readUsageCounts } from "../rules/wireUsage.js";
|
|
14
|
+
const BODY_USAGE = getUsageDialect("sagemaker", "responseBody");
|
|
15
|
+
const HF_BODY_USAGE = getUsageDialect("sagemaker", "huggingface");
|
|
13
16
|
/**
|
|
14
17
|
* Synthetic streaming delay in milliseconds for simulating real-time response
|
|
15
18
|
*/
|
|
@@ -346,27 +349,17 @@ export function parseUsageFromResponseBody(body) {
|
|
|
346
349
|
}
|
|
347
350
|
const usageRecord = (responseBody.usage ?? responseBody.tokens);
|
|
348
351
|
if (usageRecord && typeof usageRecord === "object") {
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
return {
|
|
353
|
-
promptTokens,
|
|
354
|
-
completionTokens,
|
|
355
|
-
total: Number(usageRecord.total_tokens) || promptTokens + completionTokens,
|
|
356
|
-
};
|
|
352
|
+
const counts = readUsageCounts(BODY_USAGE, usageRecord);
|
|
353
|
+
if (counts.promptTokens > 0 || counts.completionTokens > 0) {
|
|
354
|
+
return counts;
|
|
357
355
|
}
|
|
358
356
|
}
|
|
359
357
|
const details = responseBody.details;
|
|
360
358
|
const tokens = details?.tokens;
|
|
361
359
|
if (tokens && typeof tokens === "object") {
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return {
|
|
366
|
-
promptTokens,
|
|
367
|
-
completionTokens,
|
|
368
|
-
total: Number(tokens.total) || promptTokens + completionTokens,
|
|
369
|
-
};
|
|
360
|
+
const counts = readUsageCounts(HF_BODY_USAGE, tokens);
|
|
361
|
+
if (counts.promptTokens > 0 || counts.completionTokens > 0) {
|
|
362
|
+
return counts;
|
|
370
363
|
}
|
|
371
364
|
}
|
|
372
365
|
return undefined;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from "crypto";
|
|
2
2
|
import { normalizeJsonSchemaObject } from "../utils/schemaConversion.js";
|
|
3
3
|
import { jsonSchema, tool } from "../utils/tool.js";
|
|
4
|
+
import { getFinishReasonMap, translateFinishReason, } from "../providers/rules/finishReasons.js";
|
|
5
|
+
const TO_CLAUDE_FINISH = getFinishReasonMap("proxy", "toClaude");
|
|
4
6
|
// ---------------------------------------------------------------------------
|
|
5
7
|
// Helpers
|
|
6
8
|
// ---------------------------------------------------------------------------
|
|
@@ -233,22 +235,7 @@ export function parseClaudeRequest(body) {
|
|
|
233
235
|
* Map NeuroLink finish-reason strings to Claude stop_reason values.
|
|
234
236
|
*/
|
|
235
237
|
function mapStopReason(finishReason) {
|
|
236
|
-
|
|
237
|
-
case "stop":
|
|
238
|
-
case "end_turn":
|
|
239
|
-
return "end_turn";
|
|
240
|
-
case "length":
|
|
241
|
-
case "max_tokens":
|
|
242
|
-
return "max_tokens";
|
|
243
|
-
case "tool-calls":
|
|
244
|
-
case "tool_use":
|
|
245
|
-
return "tool_use";
|
|
246
|
-
case "content_filter":
|
|
247
|
-
case "safety":
|
|
248
|
-
return "stop_sequence"; // closest match
|
|
249
|
-
default:
|
|
250
|
-
return finishReason ?? "end_turn";
|
|
251
|
-
}
|
|
238
|
+
return translateFinishReason(TO_CLAUDE_FINISH, finishReason);
|
|
252
239
|
}
|
|
253
240
|
/**
|
|
254
241
|
* Serialize a NeuroLink GenerateResult into a Claude Messages API response.
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
* merely wrong in a log.
|
|
20
20
|
*/
|
|
21
21
|
import { toStandardBase64 } from "./proxyImageInput.js";
|
|
22
|
+
import { getFinishReasonMap, translateFinishReason, } from "../providers/rules/finishReasons.js";
|
|
23
|
+
const TO_GEMINI_FINISH = getFinishReasonMap("proxy", "toGemini");
|
|
22
24
|
/** Google's role for assistant turns. */
|
|
23
25
|
const MODEL_ROLE = "model";
|
|
24
26
|
/**
|
|
@@ -191,15 +193,7 @@ export function parseGeminiRequest(model, body, stream) {
|
|
|
191
193
|
}
|
|
192
194
|
/** Google's finishReason vocabulary. */
|
|
193
195
|
function toGeminiFinishReason(reason) {
|
|
194
|
-
|
|
195
|
-
case "length":
|
|
196
|
-
case "max_tokens":
|
|
197
|
-
return "MAX_TOKENS";
|
|
198
|
-
case "content_filter":
|
|
199
|
-
return "SAFETY";
|
|
200
|
-
default:
|
|
201
|
-
return "STOP";
|
|
202
|
-
}
|
|
196
|
+
return translateFinishReason(TO_GEMINI_FINISH, reason) ?? "STOP";
|
|
203
197
|
}
|
|
204
198
|
function usageMetadata(usage) {
|
|
205
199
|
const promptTokens = usage.input +
|
|
@@ -12,6 +12,8 @@ import { jsonSchema, tool } from "../utils/tool.js";
|
|
|
12
12
|
import { randomBytes } from "crypto";
|
|
13
13
|
import { normalizeJsonSchemaObject } from "../utils/schemaConversion.js";
|
|
14
14
|
import { applyClaudeRequestCacheBreakpoints } from "../utils/anthropicCacheBreakpoints.js";
|
|
15
|
+
import { getFinishReasonMap, translateFinishReason, } from "../providers/rules/finishReasons.js";
|
|
16
|
+
const TO_OPENAI_FINISH = getFinishReasonMap("proxy", "toOpenAI");
|
|
15
17
|
// ---------------------------------------------------------------------------
|
|
16
18
|
// Helpers
|
|
17
19
|
// ---------------------------------------------------------------------------
|
|
@@ -188,22 +190,7 @@ export function parseOpenAIRequest(body) {
|
|
|
188
190
|
* Map NeuroLink finish-reason strings to OpenAI finish_reason values.
|
|
189
191
|
*/
|
|
190
192
|
function mapFinishReason(finishReason) {
|
|
191
|
-
|
|
192
|
-
case "stop":
|
|
193
|
-
case "end_turn":
|
|
194
|
-
return "stop";
|
|
195
|
-
case "length":
|
|
196
|
-
case "max_tokens":
|
|
197
|
-
return "length";
|
|
198
|
-
case "tool-calls":
|
|
199
|
-
case "tool_use":
|
|
200
|
-
return "tool_calls";
|
|
201
|
-
case "content_filter":
|
|
202
|
-
case "safety":
|
|
203
|
-
return "content_filter";
|
|
204
|
-
default:
|
|
205
|
-
return finishReason ? "stop" : null;
|
|
206
|
-
}
|
|
193
|
+
return translateFinishReason(TO_OPENAI_FINISH, finishReason);
|
|
207
194
|
}
|
|
208
195
|
/** Add cache to the prompt total; reasoning is already part of completion. */
|
|
209
196
|
function toOpenAIUsage(usage) {
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -121,3 +121,5 @@ export * from "./proxyBudget.js";
|
|
|
121
121
|
// `decide` inference-type contracts (TypeSafe Jev is the first provider)
|
|
122
122
|
export * from "./decision.js";
|
|
123
123
|
export * from "./ollamaDiagnostics.js";
|
|
124
|
+
// Provider rules as JSON (src/lib/providers/rules/<id>.json)
|
|
125
|
+
export * from "./providerRules.js";
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider-rules JSON format: provider- and model-specific behaviour
|
|
3
|
+
* written as data in `src/lib/providers/rules/<id>.json`, read by the shared
|
|
4
|
+
* rule engines in `src/lib/providers/rules/`, instead of being hard-coded in
|
|
5
|
+
* each provider class.
|
|
6
|
+
*
|
|
7
|
+
* The error-rule shape is a superset of the catalog's `CatalogErrorRuleJson`,
|
|
8
|
+
* so catalog entries and rules files compile through the same engine.
|
|
9
|
+
*/
|
|
10
|
+
import type { ProviderErrorContext } from "./errors.js";
|
|
11
|
+
import type { CatalogErrorRuleClass } from "./providerCatalog.js";
|
|
12
|
+
/**
|
|
13
|
+
* One error-classification rule. It matches when ANY of its triggers fires:
|
|
14
|
+
* a status (`status`, `statusRange`), an error name or code (`errorName`,
|
|
15
|
+
* `errorCode`), or its patterns (`pattern` / `allPatterns`, optionally
|
|
16
|
+
* narrowed by `patternStatuses` or `patternWhenNoStatus`). Rules are tried in
|
|
17
|
+
* order; the first match wins.
|
|
18
|
+
*/
|
|
19
|
+
export type ProviderRuleErrorJson = {
|
|
20
|
+
/** HTTP status, or list of statuses, that triggers the rule. */
|
|
21
|
+
status?: number | number[];
|
|
22
|
+
/** Inclusive HTTP status range, e.g. [500, 599]. */
|
|
23
|
+
statusRange?: [number, number];
|
|
24
|
+
/** Status written in the message text as an HTTP status ("HTTP 429", "error 401"). */
|
|
25
|
+
messageStatus?: number | number[];
|
|
26
|
+
/** `error.name` values (e.g. "TimeoutError", "ThrottlingException"). */
|
|
27
|
+
errorName?: string | string[];
|
|
28
|
+
/** `error.code` values, searched through the cause chain (e.g. "ECONNREFUSED"). */
|
|
29
|
+
errorCode?: string | string[];
|
|
30
|
+
/** OpenAI-style error type, read from `error.type` or the JSON body's `error.type`. */
|
|
31
|
+
bodyErrorType?: string | string[];
|
|
32
|
+
/** Matches every error; for a catch-all last rule. */
|
|
33
|
+
always?: boolean;
|
|
34
|
+
/** Regular expression tested against the message. */
|
|
35
|
+
pattern?: string;
|
|
36
|
+
/** Regular expressions that must ALL match; an alternative to `pattern`. */
|
|
37
|
+
allPatterns?: string[];
|
|
38
|
+
/** When this matches, the pattern trigger does not fire. */
|
|
39
|
+
notPattern?: string;
|
|
40
|
+
/** Patterns are case-insensitive unless this is true. */
|
|
41
|
+
caseSensitive?: boolean;
|
|
42
|
+
/** Patterns also search the error's `responseBody`. */
|
|
43
|
+
searchResponseBody?: boolean;
|
|
44
|
+
/** Restrict the pattern trigger to these statuses; status matching stays independent. */
|
|
45
|
+
patternStatuses?: number[];
|
|
46
|
+
/** The pattern trigger fires only when the error carries no HTTP status. */
|
|
47
|
+
patternWhenNoStatus?: boolean;
|
|
48
|
+
class: CatalogErrorRuleClass;
|
|
49
|
+
/**
|
|
50
|
+
* Message for the classified error. Placeholders: `{model}`, `{message}`
|
|
51
|
+
* (the raw message), `{provider}`, and any variable the provider supplies
|
|
52
|
+
* (for example `{baseURL}`, already redacted). Required unless the rule
|
|
53
|
+
* names a `messageHook`.
|
|
54
|
+
*/
|
|
55
|
+
message?: string;
|
|
56
|
+
/** Alternative messages, chosen by the first pattern that matches the error message. */
|
|
57
|
+
messageWhen?: Array<{
|
|
58
|
+
pattern: string;
|
|
59
|
+
message: string;
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Named message builder for messages that need logic data cannot express
|
|
63
|
+
* (for example a list of model suggestions). Used instead of `message`.
|
|
64
|
+
*/
|
|
65
|
+
messageHook?: string;
|
|
66
|
+
};
|
|
67
|
+
/** How a rules file classifies errors. */
|
|
68
|
+
export type ProviderRuleErrors = {
|
|
69
|
+
/** Where the HTTP status is read: anywhere in the cause chain (default) or the top-level error only. */
|
|
70
|
+
statusFrom?: "cause-chain" | "top-level";
|
|
71
|
+
/** Message tested by patterns: top-level plus deepest cause (default), or the top-level message only. */
|
|
72
|
+
messageFrom?: "composed" | "top-level";
|
|
73
|
+
/** Replaces the shared handling of `TimeoutError`; `{message}` is the timeout's own message. */
|
|
74
|
+
timeout?: {
|
|
75
|
+
class: CatalogErrorRuleClass;
|
|
76
|
+
message: string;
|
|
77
|
+
};
|
|
78
|
+
/** Append the shared default rules after these (default true). */
|
|
79
|
+
useDefaults?: boolean;
|
|
80
|
+
rules: ProviderRuleErrorJson[];
|
|
81
|
+
};
|
|
82
|
+
/** Values a provider passes into error-message placeholders. */
|
|
83
|
+
export type ProviderRuleTemplateVars = Record<string, string | undefined>;
|
|
84
|
+
/** A provider's rules file. Each block is optional; a provider uses the ones it needs. */
|
|
85
|
+
export type ProviderRulesFile = {
|
|
86
|
+
$schema?: string;
|
|
87
|
+
/** Provider id, matching the AIProviderName value. */
|
|
88
|
+
id: string;
|
|
89
|
+
/** Error classification. */
|
|
90
|
+
errors?: ProviderRuleErrors;
|
|
91
|
+
/** Finish-reason translation tables, by name (one wire can have several). */
|
|
92
|
+
finishReasons?: Record<string, ProviderFinishReasonMap>;
|
|
93
|
+
/** Where this wire reports each token count. */
|
|
94
|
+
usage?: ProviderWireUsage;
|
|
95
|
+
/** Context window per provider and model; `_default` is the provider fallback. Key order matters (first prefix match wins). */
|
|
96
|
+
contextWindows?: Record<string, Record<string, number>>;
|
|
97
|
+
/** Why a context-window entry has its value, by provider and model. */
|
|
98
|
+
contextWindowNotes?: Record<string, Record<string, string>>;
|
|
99
|
+
/** Normalized provider spelling to the canonical `contextWindows` key. */
|
|
100
|
+
providerAliases?: Record<string, string>;
|
|
101
|
+
/** Named prompt/completion/total layouts, for wires with several (SageMaker). */
|
|
102
|
+
usageDialects?: Record<string, ProviderUsageCountDialect>;
|
|
103
|
+
/** Where each token count lives in a raw usage object, in priority order. */
|
|
104
|
+
usageFields?: ProviderUsageFields;
|
|
105
|
+
/** When a cache-inclusive input count is rebased onto the uncached part. */
|
|
106
|
+
usageRebase?: ProviderUsageRebase;
|
|
107
|
+
/** How this wire spells a tool choice, by name (one wire can have several). */
|
|
108
|
+
toolChoices?: Record<string, ProviderToolChoiceMap>;
|
|
109
|
+
/** Which tool names this wire accepts, and how others are rewritten. */
|
|
110
|
+
toolNames?: ProviderToolNameRules;
|
|
111
|
+
/** Named model-id families: case-insensitive regex sources, any match counts. */
|
|
112
|
+
modelFamilies?: Record<string, string[]>;
|
|
113
|
+
/** Why each model family holds the models it does. */
|
|
114
|
+
modelFamilyNotes?: Record<string, string>;
|
|
115
|
+
/** Images per request, by provider; an object gives a limit per model family. */
|
|
116
|
+
imageLimits?: Record<string, number | Record<string, number>>;
|
|
117
|
+
/** Images per request for a provider missing from `imageLimits`. */
|
|
118
|
+
imageLimitDefault?: number;
|
|
119
|
+
/** Why an image limit has its value, by provider. */
|
|
120
|
+
imageLimitNotes?: Record<string, string>;
|
|
121
|
+
/** Providers that route to arbitrary models, so an unknown model passes. */
|
|
122
|
+
proxyProviders?: string[];
|
|
123
|
+
/** Vision-capable model ids by provider (substring match); an empty list means no vision. */
|
|
124
|
+
visionModels?: Record<string, string[]>;
|
|
125
|
+
/** Notes on `visionModels`, by provider then model id ("*" for the provider). */
|
|
126
|
+
visionModelNotes?: Record<string, Record<string, string>>;
|
|
127
|
+
/** A `modelFamilies` name whose models count as vision-capable, by provider. */
|
|
128
|
+
visionFamilies?: Record<string, string>;
|
|
129
|
+
/** A label per model family: the first matching rule wins. */
|
|
130
|
+
modelFamilyLabels?: {
|
|
131
|
+
rules: ProviderModelRule<{
|
|
132
|
+
label: string;
|
|
133
|
+
}>[];
|
|
134
|
+
default: string;
|
|
135
|
+
};
|
|
136
|
+
/** Named lists of exact model ids. */
|
|
137
|
+
modelLists?: Record<string, string[]>;
|
|
138
|
+
/** Why each model list holds the models it does. */
|
|
139
|
+
modelListNotes?: Record<string, string>;
|
|
140
|
+
/** Thinking (reasoning) defaults and per-model budgets. */
|
|
141
|
+
reasoning?: ProviderReasoningRules;
|
|
142
|
+
/** Why the reasoning values are what they are, by key. */
|
|
143
|
+
reasoningNotes?: Record<string, string>;
|
|
144
|
+
/** API key format (regex source) by provider. */
|
|
145
|
+
apiKeyFormats?: Record<string, string>;
|
|
146
|
+
/** API key lengths quoted in configuration hints, by name. */
|
|
147
|
+
apiKeyLengths?: Record<string, number>;
|
|
148
|
+
/** OAuth token format (regex source) by token kind. */
|
|
149
|
+
oauthTokenFormats?: Record<string, string>;
|
|
150
|
+
/** Notes on the key and token formats, by key. */
|
|
151
|
+
credentialFormatNotes?: Record<string, string>;
|
|
152
|
+
/** Extended-thinking budget limits (Anthropic Messages). */
|
|
153
|
+
thinkingLimits?: ProviderThinkingLimits;
|
|
154
|
+
/** Why each thinking limit is what it is, by key. */
|
|
155
|
+
thinkingLimitNotes?: Record<string, string>;
|
|
156
|
+
/** The model a provider uses when neither the call nor the environment names one. */
|
|
157
|
+
defaultModel?: string;
|
|
158
|
+
/** Why the default model is the one it is. */
|
|
159
|
+
defaultModelNote?: string;
|
|
160
|
+
/** The model a provider falls back to when none is configured. */
|
|
161
|
+
fallbackModel?: string;
|
|
162
|
+
/** Why the fallback model is the one it is. */
|
|
163
|
+
fallbackModelNote?: string;
|
|
164
|
+
/** Named error-text signals: case-insensitive regex sources, any match counts. */
|
|
165
|
+
errorSignals?: Record<string, string[]>;
|
|
166
|
+
/** Why each error signal holds the patterns it does. */
|
|
167
|
+
errorSignalNotes?: Record<string, string>;
|
|
168
|
+
/** Request defaults the provider fills in when the caller leaves them out. */
|
|
169
|
+
requestDefaults?: ProviderRequestDefaults;
|
|
170
|
+
/** Why each request default is what it is, by key. */
|
|
171
|
+
requestDefaultNotes?: Record<string, string>;
|
|
172
|
+
/** Wire names of request parameters, by neutral name. */
|
|
173
|
+
requestFields?: ProviderRequestFields;
|
|
174
|
+
/** Why the request field names read as they do. */
|
|
175
|
+
requestFieldNotes?: Record<string, string>;
|
|
176
|
+
/** How a failed tool call is reported back to the model. */
|
|
177
|
+
toolResults?: ProviderToolResultRules;
|
|
178
|
+
/** Fixed texts inserted into the conversation. */
|
|
179
|
+
contextTexts?: ProviderContextTexts;
|
|
180
|
+
/** The internal tool a model calls to return structured output. */
|
|
181
|
+
finalResultTool?: ProviderFinalResultTool;
|
|
182
|
+
/** Why the final-result tool and its instructions read as they do. */
|
|
183
|
+
finalResultToolNotes?: Record<string, string>;
|
|
184
|
+
};
|
|
185
|
+
/** Wire names of request parameters, keyed by NeuroLink's neutral name. */
|
|
186
|
+
export type ProviderRequestFields = {
|
|
187
|
+
maxTokens: string;
|
|
188
|
+
temperature: string;
|
|
189
|
+
topP: string;
|
|
190
|
+
stopSequences: string;
|
|
191
|
+
};
|
|
192
|
+
/** Neutral request parameter values for `toWireParams`. */
|
|
193
|
+
export type ProviderWireParamValues = {
|
|
194
|
+
maxTokens?: number;
|
|
195
|
+
temperature?: number;
|
|
196
|
+
topP?: number;
|
|
197
|
+
stopSequences?: string[];
|
|
198
|
+
};
|
|
199
|
+
/** How a failed tool call is reported back to the model. */
|
|
200
|
+
export type ProviderToolResultRules = {
|
|
201
|
+
/** `{name}` and `{error}` are replaced. */
|
|
202
|
+
errorTemplate: string;
|
|
203
|
+
/** The boolean field set on an errored tool result. */
|
|
204
|
+
errorFlag: string;
|
|
205
|
+
};
|
|
206
|
+
/** Fixed texts inserted into the conversation. */
|
|
207
|
+
export type ProviderContextTexts = {
|
|
208
|
+
/** Replaces tool exchanges removed to fit the context window. */
|
|
209
|
+
elisionNote: string;
|
|
210
|
+
};
|
|
211
|
+
/** The internal structured-output tool and the prompt texts that steer a model to it. */
|
|
212
|
+
export type ProviderFinalResultTool = {
|
|
213
|
+
name: string;
|
|
214
|
+
description: string;
|
|
215
|
+
/** System-prompt texts; `{tool}` is replaced with the tool name. */
|
|
216
|
+
instructions: {
|
|
217
|
+
mustCall: string;
|
|
218
|
+
callWhenReady: string;
|
|
219
|
+
noLongerAvailable: string;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
/** Request values a provider fills in when the caller leaves them out. */
|
|
223
|
+
export type ProviderRequestDefaults = {
|
|
224
|
+
/** Sampling temperature sent when the caller gives none. */
|
|
225
|
+
temperature?: number;
|
|
226
|
+
/** Above this many output tokens, a generate call gets a longer timeout. */
|
|
227
|
+
largeOutputThresholdTokens?: number;
|
|
228
|
+
/** The shortest timeout, in ms, for a generate call above that threshold. */
|
|
229
|
+
largeOutputTimeoutFloorMs?: number;
|
|
230
|
+
/** Explicit SDK client timeout, in ms. */
|
|
231
|
+
clientTimeoutMs?: number;
|
|
232
|
+
/** Output-token floor of a forced finalization call. */
|
|
233
|
+
finalizationMinTokens?: number;
|
|
234
|
+
/** Context-window headroom kept when sizing that call. */
|
|
235
|
+
finalizationHeadroomTokens?: number;
|
|
236
|
+
};
|
|
237
|
+
/** Extended-thinking budget limits for a Messages-style wire. */
|
|
238
|
+
export type ProviderThinkingLimits = {
|
|
239
|
+
/** The smallest budget_tokens the API accepts. */
|
|
240
|
+
minBudgetTokens: number;
|
|
241
|
+
/** Tokens kept free inside max_tokens for the answer after thinking. */
|
|
242
|
+
answerReserveTokens: number;
|
|
243
|
+
};
|
|
244
|
+
/** A rule matched against a model id (`match` is a case-insensitive regex source). */
|
|
245
|
+
export type ProviderModelRule<T> = {
|
|
246
|
+
match: string;
|
|
247
|
+
} & T;
|
|
248
|
+
/** Thinking defaults and per-model budgets (Gemini). */
|
|
249
|
+
export type ProviderReasoningRules = {
|
|
250
|
+
defaultLevel: string;
|
|
251
|
+
defaultBudgetTokens: number;
|
|
252
|
+
/** Fraction of the budget range per thinking level. */
|
|
253
|
+
levelFractions: Record<string, number>;
|
|
254
|
+
maxBudgetTokens: {
|
|
255
|
+
rules: ProviderModelRule<{
|
|
256
|
+
value: number;
|
|
257
|
+
}>[];
|
|
258
|
+
default: number;
|
|
259
|
+
};
|
|
260
|
+
/** thinkingBudget bounds a model accepts; the first matching rule wins. */
|
|
261
|
+
budgetRanges: {
|
|
262
|
+
rules: ProviderModelRule<{
|
|
263
|
+
min: number;
|
|
264
|
+
max: number;
|
|
265
|
+
}>[];
|
|
266
|
+
fallback: {
|
|
267
|
+
min: number;
|
|
268
|
+
max: number;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
restrictedOutputTokenLimit: number;
|
|
272
|
+
};
|
|
273
|
+
/** A JSON value as stored in a rules file. */
|
|
274
|
+
export type ProviderRuleJsonValue = string | number | boolean | null | ProviderRuleJsonValue[] | {
|
|
275
|
+
[key: string]: ProviderRuleJsonValue;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* How one wire spells a NeuroLink tool choice. The input is a string
|
|
279
|
+
* ("auto" / "none" / "required"), a `{ type: mode }` object or
|
|
280
|
+
* `{ type: "tool", toolName }`. A `null` output sends nothing, so the
|
|
281
|
+
* provider default applies.
|
|
282
|
+
*/
|
|
283
|
+
export type ProviderToolChoiceMap = {
|
|
284
|
+
/** Output per mode. A mode left out counts as unknown. */
|
|
285
|
+
modes: Partial<Record<"auto" | "none" | "required", ProviderRuleJsonValue>>;
|
|
286
|
+
/** Whether the bare strings "auto" / "none" / "required" are read as modes. */
|
|
287
|
+
acceptStrings: boolean;
|
|
288
|
+
/** Whether `{ type: "auto" | "none" | "required" }` objects are read as modes. */
|
|
289
|
+
acceptModeObjects: boolean;
|
|
290
|
+
/** Output for `{ type: "tool", toolName }`; the string "{toolName}" becomes the wire tool name. */
|
|
291
|
+
tool: ProviderRuleJsonValue;
|
|
292
|
+
/** A tool choice with an empty or missing toolName counts as unknown. */
|
|
293
|
+
toolRequiresName: boolean;
|
|
294
|
+
/** Strings that are not read as modes are sent unchanged. */
|
|
295
|
+
passthroughStrings?: boolean;
|
|
296
|
+
/** Output for anything else. */
|
|
297
|
+
fallback: ProviderRuleJsonValue;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* The tool names a wire accepts: `firstChars` then `allowedChars`, at most
|
|
301
|
+
* `maxLength` characters. Character sets are regex class bodies. Another
|
|
302
|
+
* name has each disallowed character replaced by "_", gains a leading "_"
|
|
303
|
+
* when its first character is not allowed first, and is cut to `maxLength`.
|
|
304
|
+
*/
|
|
305
|
+
export type ProviderToolNameRules = {
|
|
306
|
+
firstChars: string;
|
|
307
|
+
allowedChars: string;
|
|
308
|
+
maxLength: number;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Dotted paths to one wire's token counts. `cacheRead` is reported INSIDE
|
|
312
|
+
* `input`; whether `reasoning` is inside `output` differs by wire.
|
|
313
|
+
*/
|
|
314
|
+
export type ProviderWireUsage = {
|
|
315
|
+
input: string;
|
|
316
|
+
output: string;
|
|
317
|
+
total?: string;
|
|
318
|
+
cacheRead?: string;
|
|
319
|
+
reasoning?: string;
|
|
320
|
+
/** True when `output` already counts the reasoning tokens (OpenAI); false when they come on top (Gemini). */
|
|
321
|
+
reasoningInOutput: boolean;
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Where an endpoint puts prompt, completion and total counts. Each list is
|
|
325
|
+
* tried in order: `pick: "truthy"` takes the first truthy value (`a || b`),
|
|
326
|
+
* `pick: "defined"` the first non-null one (`a ?? b`); with neither found,
|
|
327
|
+
* the last value is used. A missing or zero total becomes prompt + completion.
|
|
328
|
+
*/
|
|
329
|
+
export type ProviderUsageCountDialect = {
|
|
330
|
+
input: string[];
|
|
331
|
+
output: string[];
|
|
332
|
+
total: string[];
|
|
333
|
+
pick: "truthy" | "defined";
|
|
334
|
+
};
|
|
335
|
+
/** The counts a wire reported, as reported (undefined when absent). */
|
|
336
|
+
export type ProviderWireUsageCounts = {
|
|
337
|
+
input: number | undefined;
|
|
338
|
+
output: number | undefined;
|
|
339
|
+
total: number | undefined;
|
|
340
|
+
cacheRead: number | undefined;
|
|
341
|
+
reasoning: number | undefined;
|
|
342
|
+
};
|
|
343
|
+
/** Dotted paths tried in order; the first that holds a number wins. */
|
|
344
|
+
export type ProviderUsageField = {
|
|
345
|
+
paths: string[];
|
|
346
|
+
/** Skip a zero (treat it as "not reported"). */
|
|
347
|
+
positiveOnly?: boolean;
|
|
348
|
+
};
|
|
349
|
+
/** Every token count the shared usage normalizer reads. */
|
|
350
|
+
export type ProviderUsageFields = {
|
|
351
|
+
input: ProviderUsageField;
|
|
352
|
+
output: ProviderUsageField;
|
|
353
|
+
total: ProviderUsageField;
|
|
354
|
+
reasoning: ProviderUsageField;
|
|
355
|
+
cacheCreation: ProviderUsageField;
|
|
356
|
+
cacheRead: ProviderUsageField;
|
|
357
|
+
/** Cache reads already counted inside the input (OpenAI-style). */
|
|
358
|
+
cacheReadIncludedInInput: ProviderUsageField;
|
|
359
|
+
};
|
|
360
|
+
/**
|
|
361
|
+
* The input count is cache-inclusive (and must be rebased) when any
|
|
362
|
+
* `whenAnyPresent` path is set and no `unlessAnyPresent` path is.
|
|
363
|
+
*/
|
|
364
|
+
export type ProviderUsageRebase = {
|
|
365
|
+
whenAnyPresent: string[];
|
|
366
|
+
unlessAnyPresent: string[];
|
|
367
|
+
/** Path of an explicit uncached input count, used when present. */
|
|
368
|
+
uncachedInput: string;
|
|
369
|
+
/** Path of the cache-write count subtracted otherwise. */
|
|
370
|
+
cacheWrite: string;
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* Translates one wire's finish (stop) reason into another vocabulary.
|
|
374
|
+
* Lookup order: `map`, then `whenAbsent` for a null or undefined reason,
|
|
375
|
+
* then `defaultAtToolCap` when the loop stopped with tool calls pending,
|
|
376
|
+
* then the reason itself under `passthroughUnknown`, then `default`.
|
|
377
|
+
*/
|
|
378
|
+
export type ProviderFinishReasonMap = {
|
|
379
|
+
/** Exact wire value to translated value; `null` means "no reason". */
|
|
380
|
+
map: Record<string, string | null>;
|
|
381
|
+
/** Value for any reason the map does not list. */
|
|
382
|
+
default: string | null;
|
|
383
|
+
/** Value for a null or undefined reason; omitted means it falls through. */
|
|
384
|
+
whenAbsent?: string | null;
|
|
385
|
+
/** Value for an unlisted reason when the step cap cut off pending tool calls. */
|
|
386
|
+
defaultAtToolCap?: string;
|
|
387
|
+
/** Return an unlisted reason unchanged instead of `default`. */
|
|
388
|
+
passthroughUnknown?: boolean;
|
|
389
|
+
/** Lower-case the reason before the lookup. */
|
|
390
|
+
lowercase?: boolean;
|
|
391
|
+
};
|
|
392
|
+
/** Builds a message for a rule whose wording needs logic, named by `messageHook`. */
|
|
393
|
+
export type ProviderErrorMessageHook = (ctx: ProviderErrorContext) => string;
|
|
394
|
+
/** What a provider passes when its error rules are compiled. */
|
|
395
|
+
export type CompileErrorRulesOptions = {
|
|
396
|
+
/** Placeholder values such as `{baseURL}`; looked up when a rule fires. */
|
|
397
|
+
vars?: () => ProviderRuleTemplateVars;
|
|
398
|
+
/** Message builders referenced by `messageHook`. */
|
|
399
|
+
hooks?: Record<string, ProviderErrorMessageHook>;
|
|
400
|
+
};
|
|
401
|
+
/** Compiled tool-name rules: a validity check, a sanitizer and a de-duplicator. */
|
|
402
|
+
export type ProviderCompiledToolNameRules = {
|
|
403
|
+
isValid: (name: string) => boolean;
|
|
404
|
+
sanitize: (name: string) => string;
|
|
405
|
+
/** `base`, or `base` cut short with `_2`, `_3`, … until `isTaken` says no. */
|
|
406
|
+
unique: (base: string, isTaken: (name: string) => boolean) => string;
|
|
407
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider-rules JSON format: provider- and model-specific behaviour
|
|
3
|
+
* written as data in `src/lib/providers/rules/<id>.json`, read by the shared
|
|
4
|
+
* rule engines in `src/lib/providers/rules/`, instead of being hard-coded in
|
|
5
|
+
* each provider class.
|
|
6
|
+
*
|
|
7
|
+
* The error-rule shape is a superset of the catalog's `CatalogErrorRuleJson`,
|
|
8
|
+
* so catalog entries and rules files compile through the same engine.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|