@juspay/neurolink 13.16.2 → 13.17.1
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 +2 -2
- package/dist/adapters/providerImageAdapter.js +29 -495
- package/dist/browser/neurolink.min.js +465 -471
- 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/core/nativeGenerateLoop.js +5 -0
- 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 +25 -42
- package/dist/neurolink.js +1 -0
- package/dist/providers/amazonBedrock/client.js +9 -37
- package/dist/providers/amazonBedrock/loopAdapter.js +3 -11
- package/dist/providers/amazonSagemaker.js +6 -0
- package/dist/providers/anthropic/client.js +57 -100
- 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.d.ts +7 -0
- package/dist/providers/googleVertex/client.js +150 -192
- 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 +53 -62
- 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 +108 -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 +115 -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 +21 -0
- package/dist/providers/rules/requestFields.js +42 -0
- package/dist/providers/rules/sagemaker.json +88 -0
- package/dist/providers/rules/schema.d.ts +242 -0
- package/dist/providers/rules/schema.js +359 -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/generate.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/providerRules.d.ts +414 -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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./provider-rules.schema.json",
|
|
3
|
+
"id": "bedrock",
|
|
4
|
+
"errors": {
|
|
5
|
+
"useDefaults": false,
|
|
6
|
+
"rules": [
|
|
7
|
+
{
|
|
8
|
+
"pattern": "AccessDeniedException",
|
|
9
|
+
"class": "authentication",
|
|
10
|
+
"message": "AWS Bedrock access denied. Check your credentials and permissions."
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"errorName": "ThrottlingException",
|
|
14
|
+
"errorCode": "ThrottlingException",
|
|
15
|
+
"class": "rate-limit",
|
|
16
|
+
"message": "Bedrock rate limit (throttled): {message}"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"pattern": "ValidationException",
|
|
20
|
+
"class": "provider",
|
|
21
|
+
"message": "Validation error: {message}"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"always": true,
|
|
25
|
+
"class": "provider",
|
|
26
|
+
"message": "AWS Bedrock error: {message}"
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"finishReasons": {
|
|
31
|
+
"converse": {
|
|
32
|
+
"map": {
|
|
33
|
+
"end_turn": "stop",
|
|
34
|
+
"stop_sequence": "stop",
|
|
35
|
+
"max_tokens": "length",
|
|
36
|
+
"tool_use": "tool-calls"
|
|
37
|
+
},
|
|
38
|
+
"default": "stop",
|
|
39
|
+
"defaultAtToolCap": "tool-calls"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"requestDefaults": {
|
|
43
|
+
"temperature": 0.7
|
|
44
|
+
},
|
|
45
|
+
"requestDefaultNotes": {
|
|
46
|
+
"temperature": "Sent on Converse and ConverseStream when the caller gives no temperature."
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./provider-rules.schema.json",
|
|
3
|
+
"id": "cohere",
|
|
4
|
+
"errors": {
|
|
5
|
+
"rules": [
|
|
6
|
+
{
|
|
7
|
+
"pattern": "invalid api token|Authentication|401|invalid_api_token",
|
|
8
|
+
"caseSensitive": true,
|
|
9
|
+
"class": "authentication",
|
|
10
|
+
"message": "Invalid Cohere API key. Check COHERE_API_KEY. Get one at https://dashboard.cohere.com/api-keys"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"pattern": "rate limit|429",
|
|
14
|
+
"caseSensitive": true,
|
|
15
|
+
"class": "rate-limit",
|
|
16
|
+
"message": "Cohere rate limit exceeded. Back off and retry."
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"pattern": "model_not_found|404",
|
|
20
|
+
"caseSensitive": true,
|
|
21
|
+
"class": "invalid-model",
|
|
22
|
+
"message": "Cohere model '{model}' not found. Use command-r, command-r-plus, or command-r7b-12-2024."
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"pattern": "trial limit|trial_limit",
|
|
26
|
+
"caseSensitive": true,
|
|
27
|
+
"class": "provider",
|
|
28
|
+
"message": "Cohere trial usage limit exceeded. Upgrade at https://dashboard.cohere.com/billing."
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./provider-rules.schema.json",
|
|
3
|
+
"id": "credentials",
|
|
4
|
+
"apiKeyFormats": {
|
|
5
|
+
"openai": "^sk-[A-Za-z0-9]{48,}$",
|
|
6
|
+
"anthropic": "^sk-ant-[A-Za-z0-9\\-_]{95,}$",
|
|
7
|
+
"google-ai": "^AIza[A-Za-z0-9\\-_]{35}$",
|
|
8
|
+
"huggingface": "^hf_[A-Za-z0-9]{37}$",
|
|
9
|
+
"mistral": "^[A-Za-z0-9]{32}$",
|
|
10
|
+
"azure": "^[A-Za-z0-9]{32}$",
|
|
11
|
+
"aws": "^[A-Z0-9]{20}$",
|
|
12
|
+
"bedrock": "^[A-Z0-9]{20}$"
|
|
13
|
+
},
|
|
14
|
+
"oauthTokenFormats": {
|
|
15
|
+
"anthropic-access": "^[A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+\\.[A-Za-z0-9\\-_]+$|^[A-Za-z0-9\\-_]{32,}$",
|
|
16
|
+
"anthropic-refresh": "^[A-Za-z0-9\\-_]{32,}$"
|
|
17
|
+
},
|
|
18
|
+
"credentialFormatNotes": {
|
|
19
|
+
"aws": "AWS access key ID format.",
|
|
20
|
+
"bedrock": "AWS access key ID: 20 uppercase alphanumerics.",
|
|
21
|
+
"anthropic-access": "Claude OAuth access tokens: JWT-shaped or opaque.",
|
|
22
|
+
"anthropic-refresh": "Refresh tokens are longer opaque strings.",
|
|
23
|
+
"apiKeyLengths": "Lengths quoted in the configuration hints (providerUtils.ts). Each matches its apiKeyFormats pattern; GOOGLE_AI_EXACT counts the AIza prefix."
|
|
24
|
+
},
|
|
25
|
+
"apiKeyLengths": {
|
|
26
|
+
"OPENAI_MIN": 48,
|
|
27
|
+
"ANTHROPIC_MIN": 95,
|
|
28
|
+
"HUGGINGFACE_EXACT": 37,
|
|
29
|
+
"AZURE_MIN": 32,
|
|
30
|
+
"MISTRAL_EXACT": 32,
|
|
31
|
+
"AWS_ACCESS_KEY": 20,
|
|
32
|
+
"GOOGLE_AI_EXACT": 39
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CompileErrorRulesOptions, ProviderErrorContext, ProviderErrorRule, ProviderRuleErrorJson, ProviderRuleErrors, ProviderRuleTemplateVars } from "../../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Replaces `{name}` placeholders; unknown names are left as written. A value
|
|
4
|
+
* the provider supplies wins over the built-in one, so a provider can keep
|
|
5
|
+
* its exact wording (for example `{model}` printed as `undefined`).
|
|
6
|
+
*/
|
|
7
|
+
export declare function fillErrorTemplate(template: string, ctx: ProviderErrorContext, vars: ProviderRuleTemplateVars): string;
|
|
8
|
+
/**
|
|
9
|
+
* Compiles JSON rules, in order. The caller appends the shared defaults
|
|
10
|
+
* (`DEFAULT_ERROR_RULES`) when the provider used them before.
|
|
11
|
+
*/
|
|
12
|
+
export declare function compileErrorRules(rules: readonly ProviderRuleErrorJson[], options?: CompileErrorRulesOptions): ProviderErrorRule[];
|
|
13
|
+
/**
|
|
14
|
+
* Classifies an error with a provider's `errors` block: its timeout override,
|
|
15
|
+
* its rules in order, then the shared defaults unless it opts out.
|
|
16
|
+
*/
|
|
17
|
+
export declare function classifyWithProviderRules(error: unknown, errors: ProviderRuleErrors, provider: string, modelName: string | undefined, options?: CompileErrorRulesOptions): Error;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles JSON error rules (`ProviderRuleErrorJson`) into the
|
|
3
|
+
* `ProviderErrorRule` matchers that `classifyProviderError` runs. Catalog
|
|
4
|
+
* entries and provider rules files both compile here, so every provider's
|
|
5
|
+
* error classification is data read by one engine.
|
|
6
|
+
*/
|
|
7
|
+
import { AuthenticationError, InvalidModelError, NetworkError, ProviderError, RateLimitError, } from "../../types/index.js";
|
|
8
|
+
import { classifyProviderError, DEFAULT_ERROR_RULES, messageNamesStatus, } from "../../utils/errorClassifier.js";
|
|
9
|
+
import { readOpenAIBodyErrorType } from "../../utils/providerRetry.js";
|
|
10
|
+
import { TimeoutError } from "../../utils/timeout.js";
|
|
11
|
+
const ERROR_CLASS_MAP = {
|
|
12
|
+
authentication: AuthenticationError,
|
|
13
|
+
"rate-limit": RateLimitError,
|
|
14
|
+
"invalid-model": InvalidModelError,
|
|
15
|
+
network: NetworkError,
|
|
16
|
+
provider: ProviderError,
|
|
17
|
+
};
|
|
18
|
+
const asList = (value) => value === undefined ? [] : Array.isArray(value) ? value : [value];
|
|
19
|
+
/**
|
|
20
|
+
* Replaces `{name}` placeholders; unknown names are left as written. A value
|
|
21
|
+
* the provider supplies wins over the built-in one, so a provider can keep
|
|
22
|
+
* its exact wording (for example `{model}` printed as `undefined`).
|
|
23
|
+
*/
|
|
24
|
+
export function fillErrorTemplate(template, ctx, vars) {
|
|
25
|
+
return template.replace(/\{([A-Za-z]+)\}/g, (whole, name) => {
|
|
26
|
+
if (Object.hasOwn(vars, name)) {
|
|
27
|
+
return vars[name] ?? whole;
|
|
28
|
+
}
|
|
29
|
+
if (name === "model") {
|
|
30
|
+
return ctx.modelName ?? "";
|
|
31
|
+
}
|
|
32
|
+
if (name === "message") {
|
|
33
|
+
return ctx.message;
|
|
34
|
+
}
|
|
35
|
+
if (name === "provider") {
|
|
36
|
+
return ctx.provider;
|
|
37
|
+
}
|
|
38
|
+
return whole;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** OpenAI-style error type: `error.type`, or `error.type` inside a JSON `responseBody`. */
|
|
42
|
+
function bodyErrorTypes(error) {
|
|
43
|
+
if (typeof error !== "object" || error === null) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
const record = error;
|
|
47
|
+
const types = [];
|
|
48
|
+
if (typeof record.type === "string") {
|
|
49
|
+
types.push(record.type);
|
|
50
|
+
}
|
|
51
|
+
const fromBody = readOpenAIBodyErrorType(record.responseBody);
|
|
52
|
+
if (fromBody !== undefined) {
|
|
53
|
+
types.push(fromBody);
|
|
54
|
+
}
|
|
55
|
+
return types;
|
|
56
|
+
}
|
|
57
|
+
function responseBodyOf(error) {
|
|
58
|
+
if (typeof error !== "object" || error === null) {
|
|
59
|
+
return "";
|
|
60
|
+
}
|
|
61
|
+
const body = error.responseBody;
|
|
62
|
+
return typeof body === "string" ? body : "";
|
|
63
|
+
}
|
|
64
|
+
function compileMatcher(rule) {
|
|
65
|
+
const flags = rule.caseSensitive ? "" : "i";
|
|
66
|
+
const statuses = asList(rule.status);
|
|
67
|
+
const messageStatuses = asList(rule.messageStatus);
|
|
68
|
+
const names = asList(rule.errorName);
|
|
69
|
+
const codes = asList(rule.errorCode);
|
|
70
|
+
const bodyTypes = asList(rule.bodyErrorType);
|
|
71
|
+
const single = rule.pattern !== undefined ? new RegExp(rule.pattern, flags) : undefined;
|
|
72
|
+
const group = (rule.allPatterns ?? []).map((source) => new RegExp(source, flags));
|
|
73
|
+
const veto = rule.notPattern !== undefined
|
|
74
|
+
? new RegExp(rule.notPattern, flags)
|
|
75
|
+
: undefined;
|
|
76
|
+
const hasPatterns = single !== undefined || group.length > 0;
|
|
77
|
+
return (ctx) => {
|
|
78
|
+
if (rule.always) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
const status = ctx.statusCode;
|
|
82
|
+
if (status !== undefined && statuses.includes(status)) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (status !== undefined &&
|
|
86
|
+
rule.statusRange !== undefined &&
|
|
87
|
+
status >= rule.statusRange[0] &&
|
|
88
|
+
status <= rule.statusRange[1]) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (messageStatuses.some((code) => messageNamesStatus(ctx.message, code))) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (ctx.errorName !== undefined && names.includes(ctx.errorName)) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (ctx.errorCode !== undefined && codes.includes(ctx.errorCode)) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
if (bodyTypes.length > 0 &&
|
|
101
|
+
bodyErrorTypes(ctx.error).some((type) => bodyTypes.includes(type))) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (!hasPatterns) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (rule.patternWhenNoStatus && status !== undefined) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (rule.patternStatuses !== undefined &&
|
|
111
|
+
(status === undefined || !rule.patternStatuses.includes(status))) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
const text = rule.searchResponseBody
|
|
115
|
+
? `${ctx.message} ${responseBodyOf(ctx.error)}`
|
|
116
|
+
: ctx.message;
|
|
117
|
+
if (veto !== undefined && veto.test(text)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return ((single !== undefined && single.test(text)) ||
|
|
121
|
+
(group.length > 0 && group.every((regex) => regex.test(text))));
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Compiles JSON rules, in order. The caller appends the shared defaults
|
|
126
|
+
* (`DEFAULT_ERROR_RULES`) when the provider used them before.
|
|
127
|
+
*/
|
|
128
|
+
export function compileErrorRules(rules, options = {}) {
|
|
129
|
+
return rules.map((rule) => {
|
|
130
|
+
const hook = rule.messageHook !== undefined
|
|
131
|
+
? options.hooks?.[rule.messageHook]
|
|
132
|
+
: undefined;
|
|
133
|
+
if (rule.messageHook !== undefined && hook === undefined) {
|
|
134
|
+
throw new Error(`Error rule names messageHook "${rule.messageHook}", but no such hook was supplied`);
|
|
135
|
+
}
|
|
136
|
+
const alternatives = (rule.messageWhen ?? []).map((choice) => ({
|
|
137
|
+
regex: new RegExp(choice.pattern, rule.caseSensitive ? "" : "i"),
|
|
138
|
+
message: choice.message,
|
|
139
|
+
}));
|
|
140
|
+
return {
|
|
141
|
+
match: compileMatcher(rule),
|
|
142
|
+
errorClass: ERROR_CLASS_MAP[rule.class],
|
|
143
|
+
message: (ctx) => {
|
|
144
|
+
if (hook) {
|
|
145
|
+
return hook(ctx);
|
|
146
|
+
}
|
|
147
|
+
const chosen = alternatives.find((choice) => choice.regex.test(ctx.message));
|
|
148
|
+
return fillErrorTemplate(chosen?.message ?? rule.message ?? "", ctx, options.vars?.() ?? {});
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/** Top-level HTTP status, as `error.status` then `error.statusCode`. */
|
|
154
|
+
function topLevelStatus(error) {
|
|
155
|
+
if (typeof error !== "object" || error === null) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
const record = error;
|
|
159
|
+
if (typeof record.status === "number") {
|
|
160
|
+
return record.status;
|
|
161
|
+
}
|
|
162
|
+
return typeof record.statusCode === "number" ? record.statusCode : undefined;
|
|
163
|
+
}
|
|
164
|
+
function topLevelMessage(error) {
|
|
165
|
+
if (typeof error === "object" && error !== null) {
|
|
166
|
+
const message = error.message;
|
|
167
|
+
if (typeof message === "string") {
|
|
168
|
+
return message;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return "Unknown error";
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Classifies an error with a provider's `errors` block: its timeout override,
|
|
175
|
+
* its rules in order, then the shared defaults unless it opts out.
|
|
176
|
+
*/
|
|
177
|
+
export function classifyWithProviderRules(error, errors, provider, modelName, options = {}) {
|
|
178
|
+
if (errors.timeout !== undefined && error instanceof TimeoutError) {
|
|
179
|
+
const ErrorClass = ERROR_CLASS_MAP[errors.timeout.class];
|
|
180
|
+
const message = errors.timeout.message.replace(/\{message\}/g, error.message);
|
|
181
|
+
return new ErrorClass(message, provider);
|
|
182
|
+
}
|
|
183
|
+
const rules = compileErrorRules(errors.rules, options);
|
|
184
|
+
const all = errors.useDefaults === false ? rules : [...rules, ...DEFAULT_ERROR_RULES];
|
|
185
|
+
const adjust = errors.statusFrom === "top-level" || errors.messageFrom === "top-level"
|
|
186
|
+
? (ctx) => ({
|
|
187
|
+
...ctx,
|
|
188
|
+
...(errors.statusFrom === "top-level"
|
|
189
|
+
? { statusCode: topLevelStatus(error) }
|
|
190
|
+
: {}),
|
|
191
|
+
...(errors.messageFrom === "top-level"
|
|
192
|
+
? { message: topLevelMessage(error) }
|
|
193
|
+
: {}),
|
|
194
|
+
})
|
|
195
|
+
: undefined;
|
|
196
|
+
return classifyProviderError(error, all, provider, modelName, adjust);
|
|
197
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named error-text signals from `structured-output.json`: case-insensitive
|
|
3
|
+
* patterns that recognise a provider rejection from its message, whatever
|
|
4
|
+
* vendor sent it.
|
|
5
|
+
*/
|
|
6
|
+
import { getProviderRules } from "./registry.js";
|
|
7
|
+
const compiled = new Map();
|
|
8
|
+
function signalPatterns(name) {
|
|
9
|
+
let patterns = compiled.get(name);
|
|
10
|
+
if (!patterns) {
|
|
11
|
+
const sources = getProviderRules("structured-output").errorSignals?.[name];
|
|
12
|
+
if (!sources) {
|
|
13
|
+
throw new Error(`Provider rules file "structured-output.json" has no errorSignals.${name}`);
|
|
14
|
+
}
|
|
15
|
+
patterns = sources.map((source) => new RegExp(source, "i"));
|
|
16
|
+
compiled.set(name, patterns);
|
|
17
|
+
}
|
|
18
|
+
return patterns;
|
|
19
|
+
}
|
|
20
|
+
/** Whether an error (an Error's message, or a string) matches a named signal. */
|
|
21
|
+
export function matchesErrorSignal(name, error) {
|
|
22
|
+
const message = error instanceof Error
|
|
23
|
+
? error.message
|
|
24
|
+
: typeof error === "string"
|
|
25
|
+
? error
|
|
26
|
+
: "";
|
|
27
|
+
return signalPatterns(name).some((pattern) => pattern.test(message));
|
|
28
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The provider's fallback model; throws when its rules file has none. */
|
|
2
|
+
export declare function getFallbackModel(id: string): string;
|
|
3
|
+
/** The provider's default model; throws when its rules file has none. */
|
|
4
|
+
export declare function getDefaultModel(id: string): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider's default and fallback models, from the `defaultModel` and
|
|
3
|
+
* `fallbackModel` fields in `src/lib/providers/rules/<id>.json`.
|
|
4
|
+
*/
|
|
5
|
+
import { getProviderRules } from "./registry.js";
|
|
6
|
+
/** The provider's fallback model; throws when its rules file has none. */
|
|
7
|
+
export function getFallbackModel(id) {
|
|
8
|
+
const model = getProviderRules(id).fallbackModel;
|
|
9
|
+
if (!model) {
|
|
10
|
+
throw new Error(`Provider rules file "${id}.json" has no fallbackModel`);
|
|
11
|
+
}
|
|
12
|
+
return model;
|
|
13
|
+
}
|
|
14
|
+
/** The provider's default model; throws when its rules file has none. */
|
|
15
|
+
export function getDefaultModel(id) {
|
|
16
|
+
const model = getProviderRules(id).defaultModel;
|
|
17
|
+
if (!model) {
|
|
18
|
+
throw new Error(`Provider rules file "${id}.json" has no defaultModel`);
|
|
19
|
+
}
|
|
20
|
+
return model;
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The internal structured-output tool (`final_result`) and the system-prompt
|
|
3
|
+
* texts that steer a model to it, from `structured-output.json`.
|
|
4
|
+
*/
|
|
5
|
+
import type { ProviderFinalResultTool } from "../../types/index.js";
|
|
6
|
+
/** Internal tool name, filtered out of every returned tool call and execution. */
|
|
7
|
+
export declare const FINAL_RESULT_TOOL_NAME: string;
|
|
8
|
+
/** The tool's description, as sent to the model. */
|
|
9
|
+
export declare const FINAL_RESULT_TOOL_DESCRIPTION: string;
|
|
10
|
+
/** A system-prompt instruction naming the tool (the final-result tool by default). */
|
|
11
|
+
export declare function finalResultToolInstruction(kind: keyof ProviderFinalResultTool["instructions"], toolName?: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getProviderRules } from "./registry.js";
|
|
2
|
+
function loadFinalResultTool() {
|
|
3
|
+
const tool = getProviderRules("structured-output").finalResultTool;
|
|
4
|
+
if (!tool) {
|
|
5
|
+
throw new Error('Provider rules file "structured-output.json" has no finalResultTool');
|
|
6
|
+
}
|
|
7
|
+
return tool;
|
|
8
|
+
}
|
|
9
|
+
const TOOL = loadFinalResultTool();
|
|
10
|
+
/** Internal tool name, filtered out of every returned tool call and execution. */
|
|
11
|
+
export const FINAL_RESULT_TOOL_NAME = TOOL.name;
|
|
12
|
+
/** The tool's description, as sent to the model. */
|
|
13
|
+
export const FINAL_RESULT_TOOL_DESCRIPTION = TOOL.description;
|
|
14
|
+
/** A system-prompt instruction naming the tool (the final-result tool by default). */
|
|
15
|
+
export function finalResultToolInstruction(kind, toolName = FINAL_RESULT_TOOL_NAME) {
|
|
16
|
+
return TOOL.instructions[kind].replaceAll("{tool}", toolName);
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finish-reason translation driven by the `finishReasons` tables in
|
|
3
|
+
* `src/lib/providers/rules/<id>.json`. Every provider wire and proxy format
|
|
4
|
+
* reads its table here instead of hard-coding a switch.
|
|
5
|
+
*/
|
|
6
|
+
import type { ProviderFinishReasonMap } from "../../types/index.js";
|
|
7
|
+
/** The named finish-reason table from a rules file; throws when it is missing. */
|
|
8
|
+
export declare function getFinishReasonMap(id: string, name: string): ProviderFinishReasonMap;
|
|
9
|
+
/** Translate one finish reason through a table (see ProviderFinishReasonMap). */
|
|
10
|
+
export declare function translateFinishReason(table: ProviderFinishReasonMap, raw: string | null | undefined, hadToolCallsAtCap?: boolean): string | null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getProviderRules } from "./registry.js";
|
|
2
|
+
/** The named finish-reason table from a rules file; throws when it is missing. */
|
|
3
|
+
export function getFinishReasonMap(id, name) {
|
|
4
|
+
const table = getProviderRules(id).finishReasons?.[name];
|
|
5
|
+
if (!table) {
|
|
6
|
+
throw new Error(`Provider rules file "${id}.json" has no finishReasons.${name} table`);
|
|
7
|
+
}
|
|
8
|
+
return table;
|
|
9
|
+
}
|
|
10
|
+
/** Translate one finish reason through a table (see ProviderFinishReasonMap). */
|
|
11
|
+
export function translateFinishReason(table, raw, hadToolCallsAtCap = false) {
|
|
12
|
+
const key = table.lowercase ? raw?.toLowerCase() : raw;
|
|
13
|
+
if (typeof key === "string" && Object.hasOwn(table.map, key)) {
|
|
14
|
+
return table.map[key];
|
|
15
|
+
}
|
|
16
|
+
if (key === null || key === undefined) {
|
|
17
|
+
if (table.whenAbsent !== undefined) {
|
|
18
|
+
return table.whenAbsent;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (hadToolCallsAtCap && table.defaultAtToolCap !== undefined) {
|
|
22
|
+
return table.defaultAtToolCap;
|
|
23
|
+
}
|
|
24
|
+
if (table.passthroughUnknown && typeof raw === "string") {
|
|
25
|
+
return raw;
|
|
26
|
+
}
|
|
27
|
+
return table.default;
|
|
28
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./provider-rules.schema.json",
|
|
3
|
+
"id": "gemini",
|
|
4
|
+
"finishReasons": {
|
|
5
|
+
"generateContent": {
|
|
6
|
+
"map": {
|
|
7
|
+
"MAX_TOKENS": "length",
|
|
8
|
+
"CONTINUATION": "length",
|
|
9
|
+
"TOO_MANY_TOOL_CALLS": "tool-calls",
|
|
10
|
+
"MALFORMED_FUNCTION_CALL": "error",
|
|
11
|
+
"UNEXPECTED_TOOL_CALL": "error",
|
|
12
|
+
"SAFETY": "content-filter",
|
|
13
|
+
"RECITATION": "content-filter",
|
|
14
|
+
"BLOCKLIST": "content-filter",
|
|
15
|
+
"PROHIBITED_CONTENT": "content-filter",
|
|
16
|
+
"SPII": "content-filter",
|
|
17
|
+
"LANGUAGE": "content-filter",
|
|
18
|
+
"IMAGE_SAFETY": "content-filter",
|
|
19
|
+
"IMAGE_PROHIBITED_CONTENT": "content-filter",
|
|
20
|
+
"IMAGE_RECITATION": "content-filter"
|
|
21
|
+
},
|
|
22
|
+
"default": "stop"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"usage": {
|
|
26
|
+
"input": "promptTokenCount",
|
|
27
|
+
"output": "candidatesTokenCount",
|
|
28
|
+
"cacheRead": "cachedContentTokenCount",
|
|
29
|
+
"reasoning": "thoughtsTokenCount",
|
|
30
|
+
"reasoningInOutput": false
|
|
31
|
+
},
|
|
32
|
+
"toolNames": {
|
|
33
|
+
"firstChars": "A-Za-z_",
|
|
34
|
+
"allowedChars": "A-Za-z0-9_.:-",
|
|
35
|
+
"maxLength": 128
|
|
36
|
+
},
|
|
37
|
+
"reasoning": {
|
|
38
|
+
"defaultLevel": "high",
|
|
39
|
+
"defaultBudgetTokens": 10000,
|
|
40
|
+
"levelFractions": {
|
|
41
|
+
"minimal": 0,
|
|
42
|
+
"low": 0.25,
|
|
43
|
+
"medium": 0.55,
|
|
44
|
+
"high": 1
|
|
45
|
+
},
|
|
46
|
+
"maxBudgetTokens": {
|
|
47
|
+
"rules": [
|
|
48
|
+
{
|
|
49
|
+
"match": "^gemini-3(\\.\\d+)?-pro",
|
|
50
|
+
"value": 100000
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"match": "^gemini-3(\\.\\d+)?-flash",
|
|
54
|
+
"value": 50000
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"match": "^gemini-2\\.5",
|
|
58
|
+
"value": 32000
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
"default": 10000
|
|
62
|
+
},
|
|
63
|
+
"budgetRanges": {
|
|
64
|
+
"rules": [
|
|
65
|
+
{
|
|
66
|
+
"match": "^gemini-2\\.5-flash-lite",
|
|
67
|
+
"min": 512,
|
|
68
|
+
"max": 24576
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"match": "^gemini-2\\.5-flash",
|
|
72
|
+
"min": 128,
|
|
73
|
+
"max": 24576
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"match": "^gemini-2\\.5-pro",
|
|
77
|
+
"min": 128,
|
|
78
|
+
"max": 32768
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
"fallback": {
|
|
82
|
+
"min": 512,
|
|
83
|
+
"max": 24576
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"restrictedOutputTokenLimit": 32768
|
|
87
|
+
},
|
|
88
|
+
"reasoningNotes": {
|
|
89
|
+
"levelFractions": "A thinking level maps to min + fraction * (max - min) of the model's budget range, rounded.",
|
|
90
|
+
"budgetRanges": "Gemini 2.5 thinkingBudget bounds confirmed against live Vertex responses; Vertex rejects others with INVALID_ARGUMENT \"thinking_budget is out of range\". Flash's real floor is 1, but budget 1 measured 0 thoughtsTokenCount, so the floor is 128 (Pro's) so \"minimal\" still thinks. flash-lite must come before flash. The fallback is the intersection of the three verified ranges."
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./provider-rules.schema.json",
|
|
3
|
+
"id": "google-ai",
|
|
4
|
+
"errors": {
|
|
5
|
+
"statusFrom": "top-level",
|
|
6
|
+
"messageFrom": "top-level",
|
|
7
|
+
"useDefaults": false,
|
|
8
|
+
"timeout": {
|
|
9
|
+
"class": "network",
|
|
10
|
+
"message": "{message}"
|
|
11
|
+
},
|
|
12
|
+
"rules": [
|
|
13
|
+
{
|
|
14
|
+
"status": 401,
|
|
15
|
+
"pattern": "API_KEY_INVALID|Invalid API key",
|
|
16
|
+
"caseSensitive": true,
|
|
17
|
+
"class": "authentication",
|
|
18
|
+
"message": "Invalid Google AI API key. Please check your GOOGLE_AI_API_KEY environment variable."
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"status": 429,
|
|
22
|
+
"pattern": "RATE_LIMIT_EXCEEDED|rate limit|429",
|
|
23
|
+
"caseSensitive": true,
|
|
24
|
+
"class": "rate-limit",
|
|
25
|
+
"message": "Google AI rate limit exceeded. Please try again later."
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"status": 404,
|
|
29
|
+
"pattern": "model not found|Model not found",
|
|
30
|
+
"caseSensitive": true,
|
|
31
|
+
"patternWhenNoStatus": true,
|
|
32
|
+
"class": "invalid-model",
|
|
33
|
+
"message": "Model '{model}' not found. Please check the model name and ensure it is available."
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"pattern": "ECONNRESET|ENOTFOUND|ETIMEDOUT|ECONNREFUSED|network|connection",
|
|
37
|
+
"caseSensitive": true,
|
|
38
|
+
"class": "network",
|
|
39
|
+
"message": "Connection error: {message}"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"statusRange": [500, 599],
|
|
43
|
+
"pattern": "500|502|503|504|server error|Internal Server Error",
|
|
44
|
+
"caseSensitive": true,
|
|
45
|
+
"class": "provider",
|
|
46
|
+
"message": "Google AI server error: {message}. Please try again later."
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"always": true,
|
|
50
|
+
"class": "provider",
|
|
51
|
+
"message": "Google AI error: {message}"
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
}
|