@juspay/neurolink 13.16.1 → 13.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/dist/adapters/providerImageAdapter.js +29 -495
  3. package/dist/browser/neurolink.min.js +464 -470
  4. package/dist/constants/contextWindows.js +13 -328
  5. package/dist/constants/enums.d.ts +1 -0
  6. package/dist/constants/enums.js +2 -1
  7. package/dist/context/anthropicLoopGuard.d.ts +1 -1
  8. package/dist/context/anthropicLoopGuard.js +2 -1
  9. package/dist/core/constants.js +6 -53
  10. package/dist/core/geminiLoopAdapter.js +8 -6
  11. package/dist/core/modules/structuredOutputPolicy.d.ts +0 -21
  12. package/dist/core/modules/structuredOutputPolicy.js +4 -54
  13. package/dist/factories/providerDescriptors.js +3 -2
  14. package/dist/factories/providerRegistry.js +3 -2
  15. package/dist/models/manifests/anthropic.d.ts +1 -1
  16. package/dist/models/manifests/anthropic.js +1 -1
  17. package/dist/models/manifests/azure.js +9 -9
  18. package/dist/models/manifests/ollama.js +13 -0
  19. package/dist/models/manifests/openai.d.ts +1 -1
  20. package/dist/models/manifests/openai.js +1 -1
  21. package/dist/models/modelRegistry.js +17 -42
  22. package/dist/providers/amazonBedrock/client.js +9 -37
  23. package/dist/providers/amazonBedrock/loopAdapter.js +3 -11
  24. package/dist/providers/anthropic/client.js +42 -97
  25. package/dist/providers/anthropic/loopAdapter.js +24 -22
  26. package/dist/providers/anthropic/structuredOutput.d.ts +0 -2
  27. package/dist/providers/anthropic/structuredOutput.js +3 -5
  28. package/dist/providers/azureOpenai.js +4 -22
  29. package/dist/providers/catalog/loader.js +12 -25
  30. package/dist/providers/cohere.js +5 -30
  31. package/dist/providers/googleAiStudio/client.js +12 -61
  32. package/dist/providers/googleNativeGemini3/utils.js +43 -102
  33. package/dist/providers/googleVertex/client.js +123 -184
  34. package/dist/providers/litellm/client.js +10 -50
  35. package/dist/providers/llamaCpp.js +4 -15
  36. package/dist/providers/lmStudio.js +9 -14
  37. package/dist/providers/nvidiaNim/client.js +4 -62
  38. package/dist/providers/ollama/client.js +14 -54
  39. package/dist/providers/ollama/constants.d.ts +1 -3
  40. package/dist/providers/ollama/constants.js +2 -3
  41. package/dist/providers/openAI/client.js +5 -57
  42. package/dist/providers/openRouter/client.js +4 -65
  43. package/dist/providers/openaiChatCompletionsBase.js +34 -58
  44. package/dist/providers/openaiChatCompletionsClient.js +17 -57
  45. package/dist/providers/openaiCompatible/client.js +9 -36
  46. package/dist/providers/rules/README.md +100 -0
  47. package/dist/providers/rules/anthropic.json +104 -0
  48. package/dist/providers/rules/azure.json +13 -0
  49. package/dist/providers/rules/bedrock.json +48 -0
  50. package/dist/providers/rules/cohere.json +32 -0
  51. package/dist/providers/rules/credentials.json +34 -0
  52. package/dist/providers/rules/errorRules.d.ts +17 -0
  53. package/dist/providers/rules/errorRules.js +197 -0
  54. package/dist/providers/rules/errorSignals.d.ts +2 -0
  55. package/dist/providers/rules/errorSignals.js +28 -0
  56. package/dist/providers/rules/fallbackModel.d.ts +4 -0
  57. package/dist/providers/rules/fallbackModel.js +21 -0
  58. package/dist/providers/rules/finalResultTool.d.ts +11 -0
  59. package/dist/providers/rules/finalResultTool.js +17 -0
  60. package/dist/providers/rules/finishReasons.d.ts +10 -0
  61. package/dist/providers/rules/finishReasons.js +28 -0
  62. package/dist/providers/rules/gemini.json +92 -0
  63. package/dist/providers/rules/google-ai.json +55 -0
  64. package/dist/providers/rules/limits.json +338 -0
  65. package/dist/providers/rules/litellm.json +36 -0
  66. package/dist/providers/rules/llamacpp.json +23 -0
  67. package/dist/providers/rules/lm-studio.json +21 -0
  68. package/dist/providers/rules/model-families.json +109 -0
  69. package/dist/providers/rules/modelFamilies.d.ts +10 -0
  70. package/dist/providers/rules/modelFamilies.js +45 -0
  71. package/dist/providers/rules/nvidia-nim.json +43 -0
  72. package/dist/providers/rules/ollama.json +37 -0
  73. package/dist/providers/rules/openai-chat.json +66 -0
  74. package/dist/providers/rules/openai-compatible.json +31 -0
  75. package/dist/providers/rules/openai.json +38 -0
  76. package/dist/providers/rules/openrouter.json +57 -0
  77. package/dist/providers/rules/proxy.json +44 -0
  78. package/dist/providers/rules/registry.d.ts +7 -0
  79. package/dist/providers/rules/registry.js +76 -0
  80. package/dist/providers/rules/requestDefaults.d.ts +7 -0
  81. package/dist/providers/rules/requestDefaults.js +9 -0
  82. package/dist/providers/rules/requestFields.d.ts +18 -0
  83. package/dist/providers/rules/requestFields.js +38 -0
  84. package/dist/providers/rules/sagemaker.json +88 -0
  85. package/dist/providers/rules/schema.d.ts +234 -0
  86. package/dist/providers/rules/schema.js +352 -0
  87. package/dist/providers/rules/structured-output.json +36 -0
  88. package/dist/providers/rules/thinkingLimits.d.ts +7 -0
  89. package/dist/providers/rules/thinkingLimits.js +9 -0
  90. package/dist/providers/rules/toolRules.d.ts +16 -0
  91. package/dist/providers/rules/toolRules.js +95 -0
  92. package/dist/providers/rules/usage.json +51 -0
  93. package/dist/providers/rules/vertex.json +65 -0
  94. package/dist/providers/rules/vision.json +505 -0
  95. package/dist/providers/rules/wireUsage.d.ts +38 -0
  96. package/dist/providers/rules/wireUsage.js +82 -0
  97. package/dist/providers/sagemaker/language-model.js +6 -33
  98. package/dist/providers/sagemaker/parsers.js +13 -48
  99. package/dist/providers/sagemaker/streaming.js +9 -16
  100. package/dist/proxy/claudeFormat.js +3 -16
  101. package/dist/proxy/geminiFormat.js +3 -9
  102. package/dist/proxy/openaiFormat.js +3 -16
  103. package/dist/types/index.d.ts +1 -0
  104. package/dist/types/index.js +2 -0
  105. package/dist/types/providerRules.d.ts +407 -0
  106. package/dist/types/providerRules.js +10 -0
  107. package/dist/utils/errorClassifier.d.ts +2 -2
  108. package/dist/utils/errorClassifier.js +3 -2
  109. package/dist/utils/modelDetection.d.ts +1 -1
  110. package/dist/utils/modelDetection.js +30 -71
  111. package/dist/utils/providerConfig.d.ts +10 -9
  112. package/dist/utils/providerConfig.js +26 -25
  113. package/dist/utils/providerHealth.js +1 -0
  114. package/dist/utils/thinkingConfig.d.ts +1 -1
  115. package/dist/utils/thinkingConfig.js +8 -8
  116. package/dist/utils/tokenUtils.d.ts +12 -38
  117. package/dist/utils/tokenUtils.js +44 -127
  118. package/docs-site/static/search-index.json +5 -5
  119. package/package.json +2 -1
  120. package/dist/providers/localRuntimeOpenAICompat.d.ts +0 -16
  121. package/dist/providers/localRuntimeOpenAICompat.js +0 -26
@@ -0,0 +1,234 @@
1
+ /**
2
+ * zod validator for provider rules files (`src/lib/providers/rules/*.json`).
3
+ * Mirrors `ProviderRulesFile` in src/lib/types/providerRules.ts with strict
4
+ * objects (an unknown key is an authoring mistake) plus the cross-field
5
+ * checks the plain types cannot express.
6
+ */
7
+ import { z } from "zod";
8
+ export declare const providerRulesFileSchema: z.ZodObject<{
9
+ $schema: z.ZodOptional<z.ZodString>;
10
+ id: z.ZodString;
11
+ errors: z.ZodOptional<z.ZodObject<{
12
+ statusFrom: z.ZodOptional<z.ZodEnum<{
13
+ "cause-chain": "cause-chain";
14
+ "top-level": "top-level";
15
+ }>>;
16
+ messageFrom: z.ZodOptional<z.ZodEnum<{
17
+ "top-level": "top-level";
18
+ composed: "composed";
19
+ }>>;
20
+ timeout: z.ZodOptional<z.ZodObject<{
21
+ class: z.ZodEnum<{
22
+ provider: "provider";
23
+ authentication: "authentication";
24
+ "rate-limit": "rate-limit";
25
+ "invalid-model": "invalid-model";
26
+ network: "network";
27
+ }>;
28
+ message: z.ZodString;
29
+ }, z.core.$strict>>;
30
+ useDefaults: z.ZodOptional<z.ZodBoolean>;
31
+ rules: z.ZodArray<z.ZodObject<{
32
+ status: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodArray<z.ZodNumber>]>>;
33
+ statusRange: z.ZodOptional<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
34
+ messageStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodArray<z.ZodNumber>]>>;
35
+ errorName: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
36
+ errorCode: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
37
+ bodyErrorType: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
38
+ always: z.ZodOptional<z.ZodLiteral<true>>;
39
+ pattern: z.ZodOptional<z.ZodString>;
40
+ allPatterns: z.ZodOptional<z.ZodArray<z.ZodString>>;
41
+ notPattern: z.ZodOptional<z.ZodString>;
42
+ caseSensitive: z.ZodOptional<z.ZodBoolean>;
43
+ searchResponseBody: z.ZodOptional<z.ZodBoolean>;
44
+ patternStatuses: z.ZodOptional<z.ZodArray<z.ZodNumber>>;
45
+ patternWhenNoStatus: z.ZodOptional<z.ZodBoolean>;
46
+ class: z.ZodEnum<{
47
+ provider: "provider";
48
+ authentication: "authentication";
49
+ "rate-limit": "rate-limit";
50
+ "invalid-model": "invalid-model";
51
+ network: "network";
52
+ }>;
53
+ message: z.ZodOptional<z.ZodString>;
54
+ messageWhen: z.ZodOptional<z.ZodArray<z.ZodObject<{
55
+ pattern: z.ZodString;
56
+ message: z.ZodString;
57
+ }, z.core.$strict>>>;
58
+ messageHook: z.ZodOptional<z.ZodString>;
59
+ }, z.core.$strict>>;
60
+ }, z.core.$strict>>;
61
+ finishReasons: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
62
+ map: z.ZodRecord<z.ZodString, z.ZodNullable<z.ZodString>>;
63
+ default: z.ZodNullable<z.ZodString>;
64
+ whenAbsent: z.ZodOptional<z.ZodNullable<z.ZodString>>;
65
+ defaultAtToolCap: z.ZodOptional<z.ZodString>;
66
+ passthroughUnknown: z.ZodOptional<z.ZodBoolean>;
67
+ lowercase: z.ZodOptional<z.ZodBoolean>;
68
+ }, z.core.$strict>>>;
69
+ usage: z.ZodOptional<z.ZodObject<{
70
+ input: z.ZodString;
71
+ output: z.ZodString;
72
+ total: z.ZodOptional<z.ZodString>;
73
+ cacheRead: z.ZodOptional<z.ZodString>;
74
+ reasoning: z.ZodOptional<z.ZodString>;
75
+ reasoningInOutput: z.ZodBoolean;
76
+ }, z.core.$strict>>;
77
+ usageDialects: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
78
+ input: z.ZodArray<z.ZodString>;
79
+ output: z.ZodArray<z.ZodString>;
80
+ total: z.ZodArray<z.ZodString>;
81
+ pick: z.ZodEnum<{
82
+ truthy: "truthy";
83
+ defined: "defined";
84
+ }>;
85
+ }, z.core.$strict>>>;
86
+ usageFields: z.ZodOptional<z.ZodObject<{
87
+ input: z.ZodObject<{
88
+ paths: z.ZodArray<z.ZodString>;
89
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
90
+ }, z.core.$strict>;
91
+ output: z.ZodObject<{
92
+ paths: z.ZodArray<z.ZodString>;
93
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
94
+ }, z.core.$strict>;
95
+ total: z.ZodObject<{
96
+ paths: z.ZodArray<z.ZodString>;
97
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
98
+ }, z.core.$strict>;
99
+ reasoning: z.ZodObject<{
100
+ paths: z.ZodArray<z.ZodString>;
101
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
102
+ }, z.core.$strict>;
103
+ cacheCreation: z.ZodObject<{
104
+ paths: z.ZodArray<z.ZodString>;
105
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
106
+ }, z.core.$strict>;
107
+ cacheRead: z.ZodObject<{
108
+ paths: z.ZodArray<z.ZodString>;
109
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
110
+ }, z.core.$strict>;
111
+ cacheReadIncludedInInput: z.ZodObject<{
112
+ paths: z.ZodArray<z.ZodString>;
113
+ positiveOnly: z.ZodOptional<z.ZodBoolean>;
114
+ }, z.core.$strict>;
115
+ }, z.core.$strict>>;
116
+ contextWindows: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodNumber>>>;
117
+ contextWindowNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodString>>>;
118
+ providerAliases: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
119
+ usageRebase: z.ZodOptional<z.ZodObject<{
120
+ whenAnyPresent: z.ZodArray<z.ZodString>;
121
+ unlessAnyPresent: z.ZodArray<z.ZodString>;
122
+ uncachedInput: z.ZodString;
123
+ cacheWrite: z.ZodString;
124
+ }, z.core.$strict>>;
125
+ toolChoices: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
126
+ modes: z.ZodObject<{
127
+ auto: z.ZodOptional<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
128
+ none: z.ZodOptional<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
129
+ required: z.ZodOptional<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
130
+ }, z.core.$strict>;
131
+ acceptStrings: z.ZodBoolean;
132
+ acceptModeObjects: z.ZodBoolean;
133
+ tool: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
134
+ toolRequiresName: z.ZodBoolean;
135
+ passthroughStrings: z.ZodOptional<z.ZodBoolean>;
136
+ fallback: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
137
+ }, z.core.$strict>>>;
138
+ toolNames: z.ZodOptional<z.ZodObject<{
139
+ firstChars: z.ZodString;
140
+ allowedChars: z.ZodString;
141
+ maxLength: z.ZodNumber;
142
+ }, z.core.$strict>>;
143
+ modelFamilies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
144
+ modelFamilyNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
145
+ imageLimits: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodNumber, z.ZodRecord<z.ZodString, z.ZodNumber>]>>>;
146
+ imageLimitDefault: z.ZodOptional<z.ZodNumber>;
147
+ imageLimitNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
148
+ proxyProviders: z.ZodOptional<z.ZodArray<z.ZodString>>;
149
+ visionModels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
150
+ visionModelNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodString>>>;
151
+ visionFamilies: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
152
+ modelFamilyLabels: z.ZodOptional<z.ZodObject<{
153
+ rules: z.ZodArray<z.ZodObject<{
154
+ match: z.ZodString;
155
+ label: z.ZodString;
156
+ }, z.core.$strict>>;
157
+ default: z.ZodString;
158
+ }, z.core.$strict>>;
159
+ modelLists: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
160
+ modelListNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
161
+ reasoning: z.ZodOptional<z.ZodObject<{
162
+ defaultLevel: z.ZodString;
163
+ defaultBudgetTokens: z.ZodNumber;
164
+ levelFractions: z.ZodRecord<z.ZodString, z.ZodNumber>;
165
+ maxBudgetTokens: z.ZodObject<{
166
+ rules: z.ZodArray<z.ZodObject<{
167
+ match: z.ZodString;
168
+ value: z.ZodNumber;
169
+ }, z.core.$strict>>;
170
+ default: z.ZodNumber;
171
+ }, z.core.$strict>;
172
+ budgetRanges: z.ZodObject<{
173
+ rules: z.ZodArray<z.ZodObject<{
174
+ match: z.ZodString;
175
+ min: z.ZodNumber;
176
+ max: z.ZodNumber;
177
+ }, z.core.$strict>>;
178
+ fallback: z.ZodObject<{
179
+ min: z.ZodNumber;
180
+ max: z.ZodNumber;
181
+ }, z.core.$strict>;
182
+ }, z.core.$strict>;
183
+ restrictedOutputTokenLimit: z.ZodNumber;
184
+ }, z.core.$strict>>;
185
+ reasoningNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
186
+ apiKeyFormats: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
187
+ apiKeyLengths: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
188
+ oauthTokenFormats: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
189
+ credentialFormatNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
190
+ thinkingLimits: z.ZodOptional<z.ZodObject<{
191
+ minBudgetTokens: z.ZodNumber;
192
+ answerReserveTokens: z.ZodNumber;
193
+ }, z.core.$strict>>;
194
+ thinkingLimitNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
195
+ defaultModel: z.ZodOptional<z.ZodString>;
196
+ defaultModelNote: z.ZodOptional<z.ZodString>;
197
+ fallbackModel: z.ZodOptional<z.ZodString>;
198
+ fallbackModelNote: z.ZodOptional<z.ZodString>;
199
+ errorSignals: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
200
+ errorSignalNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
201
+ requestDefaults: z.ZodOptional<z.ZodObject<{
202
+ temperature: z.ZodOptional<z.ZodNumber>;
203
+ largeOutputThresholdTokens: z.ZodOptional<z.ZodNumber>;
204
+ largeOutputTimeoutFloorMs: z.ZodOptional<z.ZodNumber>;
205
+ clientTimeoutMs: z.ZodOptional<z.ZodNumber>;
206
+ finalizationMinTokens: z.ZodOptional<z.ZodNumber>;
207
+ finalizationHeadroomTokens: z.ZodOptional<z.ZodNumber>;
208
+ }, z.core.$strict>>;
209
+ requestDefaultNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
210
+ requestFields: z.ZodOptional<z.ZodObject<{
211
+ maxTokens: z.ZodString;
212
+ temperature: z.ZodString;
213
+ topP: z.ZodString;
214
+ stopSequences: z.ZodString;
215
+ }, z.core.$strict>>;
216
+ requestFieldNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
217
+ toolResults: z.ZodOptional<z.ZodObject<{
218
+ errorTemplate: z.ZodString;
219
+ errorFlag: z.ZodString;
220
+ }, z.core.$strict>>;
221
+ contextTexts: z.ZodOptional<z.ZodObject<{
222
+ elisionNote: z.ZodString;
223
+ }, z.core.$strict>>;
224
+ finalResultTool: z.ZodOptional<z.ZodObject<{
225
+ name: z.ZodString;
226
+ description: z.ZodString;
227
+ instructions: z.ZodObject<{
228
+ mustCall: z.ZodString;
229
+ callWhenReady: z.ZodString;
230
+ noLongerAvailable: z.ZodString;
231
+ }, z.core.$strict>;
232
+ }, z.core.$strict>>;
233
+ finalResultToolNotes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
234
+ }, z.core.$strict>;
@@ -0,0 +1,352 @@
1
+ /**
2
+ * zod validator for provider rules files (`src/lib/providers/rules/*.json`).
3
+ * Mirrors `ProviderRulesFile` in src/lib/types/providerRules.ts with strict
4
+ * objects (an unknown key is an authoring mistake) plus the cross-field
5
+ * checks the plain types cannot express.
6
+ */
7
+ import { z } from "zod";
8
+ const errorClass = z.enum([
9
+ "authentication",
10
+ "rate-limit",
11
+ "invalid-model",
12
+ "network",
13
+ "provider",
14
+ ]);
15
+ const oneOrMany = (item) => z.union([item, z.array(item).min(1)]);
16
+ const httpStatus = z.number().int().min(100).max(599);
17
+ function compiles(source) {
18
+ try {
19
+ new RegExp(source);
20
+ return true;
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ }
26
+ const regex = z
27
+ .string()
28
+ .min(1)
29
+ .refine(compiles, "must compile as a regular expression");
30
+ const errorRuleSchema = z
31
+ .strictObject({
32
+ status: oneOrMany(httpStatus).optional(),
33
+ statusRange: z.tuple([httpStatus, httpStatus]).optional(),
34
+ messageStatus: oneOrMany(httpStatus).optional(),
35
+ errorName: oneOrMany(z.string().min(1)).optional(),
36
+ errorCode: oneOrMany(z.string().min(1)).optional(),
37
+ bodyErrorType: oneOrMany(z.string().min(1)).optional(),
38
+ always: z.literal(true).optional(),
39
+ pattern: regex.optional(),
40
+ allPatterns: z.array(regex).min(1).optional(),
41
+ notPattern: regex.optional(),
42
+ caseSensitive: z.boolean().optional(),
43
+ searchResponseBody: z.boolean().optional(),
44
+ patternStatuses: z.array(httpStatus).min(1).optional(),
45
+ patternWhenNoStatus: z.boolean().optional(),
46
+ class: errorClass,
47
+ message: z.string().min(1).optional(),
48
+ messageWhen: z
49
+ .array(z.strictObject({ pattern: regex, message: z.string().min(1) }))
50
+ .min(1)
51
+ .optional(),
52
+ messageHook: z.string().min(1).optional(),
53
+ })
54
+ .superRefine((rule, ctx) => {
55
+ const triggers = [
56
+ rule.status,
57
+ rule.statusRange,
58
+ rule.messageStatus,
59
+ rule.errorName,
60
+ rule.errorCode,
61
+ rule.bodyErrorType,
62
+ rule.always,
63
+ rule.pattern,
64
+ rule.allPatterns,
65
+ ].filter((value) => value !== undefined);
66
+ if (triggers.length === 0) {
67
+ ctx.addIssue({ code: "custom", message: "rule has no trigger" });
68
+ }
69
+ if (rule.always && triggers.length > 1) {
70
+ ctx.addIssue({
71
+ code: "custom",
72
+ message: "an `always` rule takes no other trigger",
73
+ });
74
+ }
75
+ if (rule.message === undefined && rule.messageHook === undefined) {
76
+ ctx.addIssue({
77
+ code: "custom",
78
+ message: "rule needs `message` or `messageHook`",
79
+ });
80
+ }
81
+ const hasPattern = rule.pattern !== undefined || rule.allPatterns !== undefined;
82
+ for (const key of [
83
+ "notPattern",
84
+ "patternStatuses",
85
+ "patternWhenNoStatus",
86
+ "searchResponseBody",
87
+ "caseSensitive",
88
+ ]) {
89
+ if (rule[key] !== undefined && !hasPattern) {
90
+ ctx.addIssue({
91
+ code: "custom",
92
+ path: [key],
93
+ message: `\`${key}\` needs \`pattern\` or \`allPatterns\``,
94
+ });
95
+ }
96
+ }
97
+ if (rule.statusRange !== undefined &&
98
+ rule.statusRange[0] > rule.statusRange[1]) {
99
+ ctx.addIssue({
100
+ code: "custom",
101
+ path: ["statusRange"],
102
+ message: "statusRange must be [low, high]",
103
+ });
104
+ }
105
+ });
106
+ const errorsSchema = z.strictObject({
107
+ statusFrom: z.enum(["cause-chain", "top-level"]).optional(),
108
+ messageFrom: z.enum(["composed", "top-level"]).optional(),
109
+ timeout: z
110
+ .strictObject({ class: errorClass, message: z.string().min(1) })
111
+ .optional(),
112
+ useDefaults: z.boolean().optional(),
113
+ rules: z.array(errorRuleSchema).min(1),
114
+ });
115
+ const finishValue = z.string().min(1).nullable();
116
+ const finishReasonMapSchema = z
117
+ .strictObject({
118
+ map: z.record(z.string(), finishValue),
119
+ default: finishValue,
120
+ whenAbsent: finishValue.optional(),
121
+ defaultAtToolCap: z.string().min(1).optional(),
122
+ passthroughUnknown: z.boolean().optional(),
123
+ lowercase: z.boolean().optional(),
124
+ })
125
+ .superRefine((table, ctx) => {
126
+ if (table.lowercase) {
127
+ for (const key of Object.keys(table.map)) {
128
+ if (key !== key.toLowerCase()) {
129
+ ctx.addIssue({
130
+ code: "custom",
131
+ path: ["map", key],
132
+ message: "a `lowercase` table can only list lower-case reasons",
133
+ });
134
+ }
135
+ }
136
+ }
137
+ });
138
+ const usagePath = z
139
+ .string()
140
+ .regex(/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/, "must be a dotted field path");
141
+ const wireUsageSchema = z.strictObject({
142
+ input: usagePath,
143
+ output: usagePath,
144
+ total: usagePath.optional(),
145
+ cacheRead: usagePath.optional(),
146
+ reasoning: usagePath.optional(),
147
+ reasoningInOutput: z.boolean(),
148
+ });
149
+ const usageCountDialectSchema = z.strictObject({
150
+ input: z.array(usagePath).min(1),
151
+ output: z.array(usagePath).min(1),
152
+ total: z.array(usagePath).min(1),
153
+ pick: z.enum(["truthy", "defined"]),
154
+ });
155
+ const usageFieldSchema = z.strictObject({
156
+ paths: z.array(usagePath).min(1),
157
+ positiveOnly: z.boolean().optional(),
158
+ });
159
+ const usageFieldsSchema = z.strictObject({
160
+ input: usageFieldSchema,
161
+ output: usageFieldSchema,
162
+ total: usageFieldSchema,
163
+ reasoning: usageFieldSchema,
164
+ cacheCreation: usageFieldSchema,
165
+ cacheRead: usageFieldSchema,
166
+ cacheReadIncludedInInput: usageFieldSchema,
167
+ });
168
+ const usageRebaseSchema = z.strictObject({
169
+ whenAnyPresent: z.array(usagePath).min(1),
170
+ unlessAnyPresent: z.array(usagePath).min(1),
171
+ uncachedInput: usagePath,
172
+ cacheWrite: usagePath,
173
+ });
174
+ const jsonValue = z.lazy(() => z.union([
175
+ z.string(),
176
+ z.number(),
177
+ z.boolean(),
178
+ z.null(),
179
+ z.array(jsonValue),
180
+ z.record(z.string(), jsonValue),
181
+ ]));
182
+ const toolChoiceMapSchema = z.strictObject({
183
+ modes: z.strictObject({
184
+ auto: jsonValue.optional(),
185
+ none: jsonValue.optional(),
186
+ required: jsonValue.optional(),
187
+ }),
188
+ acceptStrings: z.boolean(),
189
+ acceptModeObjects: z.boolean(),
190
+ tool: jsonValue,
191
+ toolRequiresName: z.boolean(),
192
+ passthroughStrings: z.boolean().optional(),
193
+ fallback: jsonValue,
194
+ });
195
+ const charClass = z
196
+ .string()
197
+ .min(1)
198
+ .refine((body) => {
199
+ try {
200
+ new RegExp(`[${body}]`);
201
+ return !body.includes("]");
202
+ }
203
+ catch {
204
+ return false;
205
+ }
206
+ }, "must be a regex character-class body");
207
+ const toolNameRulesSchema = z.strictObject({
208
+ firstChars: charClass,
209
+ allowedChars: charClass,
210
+ maxLength: z.number().int().min(2),
211
+ });
212
+ const positiveInt = z.number().int().positive();
213
+ const budgetRange = z
214
+ .strictObject({ min: z.number().int().nonnegative(), max: positiveInt })
215
+ .refine((range) => range.min <= range.max, "min must not exceed max");
216
+ const reasoningSchema = z.strictObject({
217
+ defaultLevel: z.string().min(1),
218
+ defaultBudgetTokens: positiveInt,
219
+ levelFractions: z.record(z.string().min(1), z.number().min(0).max(1)),
220
+ maxBudgetTokens: z.strictObject({
221
+ rules: z.array(z.strictObject({ match: regex, value: positiveInt })),
222
+ default: positiveInt,
223
+ }),
224
+ budgetRanges: z.strictObject({
225
+ rules: z.array(z.strictObject({
226
+ match: regex,
227
+ min: z.number().int().nonnegative(),
228
+ max: positiveInt,
229
+ })),
230
+ fallback: budgetRange,
231
+ }),
232
+ restrictedOutputTokenLimit: positiveInt,
233
+ });
234
+ export const providerRulesFileSchema = z.strictObject({
235
+ $schema: z.string().optional(),
236
+ id: z
237
+ .string()
238
+ .regex(/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/, "id must be kebab-case"),
239
+ errors: errorsSchema.optional(),
240
+ finishReasons: z.record(z.string().min(1), finishReasonMapSchema).optional(),
241
+ usage: wireUsageSchema.optional(),
242
+ usageDialects: z
243
+ .record(z.string().min(1), usageCountDialectSchema)
244
+ .optional(),
245
+ usageFields: usageFieldsSchema.optional(),
246
+ contextWindows: z
247
+ .record(z.string().min(1), z.record(z.string().min(1), z.number().int().positive()))
248
+ .optional(),
249
+ contextWindowNotes: z
250
+ .record(z.string().min(1), z.record(z.string().min(1), z.string().min(1)))
251
+ .optional(),
252
+ providerAliases: z
253
+ .record(z.string().regex(/^[a-z]+$/), z.string().min(1))
254
+ .optional(),
255
+ usageRebase: usageRebaseSchema.optional(),
256
+ toolChoices: z.record(z.string().min(1), toolChoiceMapSchema).optional(),
257
+ toolNames: toolNameRulesSchema.optional(),
258
+ modelFamilies: z.record(z.string().min(1), z.array(regex).min(1)).optional(),
259
+ modelFamilyNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
260
+ imageLimits: z
261
+ .record(z.string().min(1), z.union([
262
+ z.number().int().positive(),
263
+ z.record(z.string().min(1), z.number().int().positive()),
264
+ ]))
265
+ .optional(),
266
+ imageLimitDefault: z.number().int().positive().optional(),
267
+ imageLimitNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
268
+ proxyProviders: z.array(z.string().min(1)).optional(),
269
+ visionModels: z
270
+ .record(z.string().min(1), z.array(z.string().min(1)))
271
+ .optional(),
272
+ visionModelNotes: z
273
+ .record(z.string().min(1), z.record(z.string().min(1), z.string().min(1)))
274
+ .optional(),
275
+ visionFamilies: z.record(z.string().min(1), z.string().min(1)).optional(),
276
+ modelFamilyLabels: z
277
+ .strictObject({
278
+ rules: z.array(z.strictObject({ match: regex, label: z.string().min(1) })),
279
+ default: z.string().min(1),
280
+ })
281
+ .optional(),
282
+ modelLists: z
283
+ .record(z.string().min(1), z.array(z.string().min(1)).min(1))
284
+ .optional(),
285
+ modelListNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
286
+ reasoning: reasoningSchema.optional(),
287
+ reasoningNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
288
+ apiKeyFormats: z.record(z.string().min(1), regex).optional(),
289
+ apiKeyLengths: z
290
+ .record(z.string().min(1), z.number().int().positive())
291
+ .optional(),
292
+ oauthTokenFormats: z.record(z.string().min(1), regex).optional(),
293
+ credentialFormatNotes: z
294
+ .record(z.string().min(1), z.string().min(1))
295
+ .optional(),
296
+ thinkingLimits: z
297
+ .strictObject({
298
+ minBudgetTokens: positiveInt,
299
+ answerReserveTokens: z.number().int().nonnegative(),
300
+ })
301
+ .optional(),
302
+ thinkingLimitNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
303
+ defaultModel: z.string().min(1).optional(),
304
+ defaultModelNote: z.string().min(1).optional(),
305
+ fallbackModel: z.string().min(1).optional(),
306
+ fallbackModelNote: z.string().min(1).optional(),
307
+ errorSignals: z.record(z.string().min(1), z.array(regex).min(1)).optional(),
308
+ errorSignalNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
309
+ requestDefaults: z
310
+ .strictObject({
311
+ temperature: z.number().min(0).max(2).optional(),
312
+ largeOutputThresholdTokens: positiveInt.optional(),
313
+ largeOutputTimeoutFloorMs: positiveInt.optional(),
314
+ clientTimeoutMs: positiveInt.optional(),
315
+ finalizationMinTokens: positiveInt.optional(),
316
+ finalizationHeadroomTokens: positiveInt.optional(),
317
+ })
318
+ .optional(),
319
+ requestDefaultNotes: z
320
+ .record(z.string().min(1), z.string().min(1))
321
+ .optional(),
322
+ requestFields: z
323
+ .strictObject({
324
+ maxTokens: z.string().min(1),
325
+ temperature: z.string().min(1),
326
+ topP: z.string().min(1),
327
+ stopSequences: z.string().min(1),
328
+ })
329
+ .optional(),
330
+ requestFieldNotes: z.record(z.string().min(1), z.string().min(1)).optional(),
331
+ toolResults: z
332
+ .strictObject({
333
+ errorTemplate: z.string().includes("{error}"),
334
+ errorFlag: z.string().min(1),
335
+ })
336
+ .optional(),
337
+ contextTexts: z.strictObject({ elisionNote: z.string().min(1) }).optional(),
338
+ finalResultTool: z
339
+ .strictObject({
340
+ name: z.string().regex(/^[A-Za-z_][A-Za-z0-9_-]*$/),
341
+ description: z.string().min(1),
342
+ instructions: z.strictObject({
343
+ mustCall: z.string().includes("{tool}"),
344
+ callWhenReady: z.string().includes("{tool}"),
345
+ noLongerAvailable: z.string().includes("{tool}"),
346
+ }),
347
+ })
348
+ .optional(),
349
+ finalResultToolNotes: z
350
+ .record(z.string().min(1), z.string().min(1))
351
+ .optional(),
352
+ });
@@ -0,0 +1,36 @@
1
+ {
2
+ "$schema": "./provider-rules.schema.json",
3
+ "id": "structured-output",
4
+ "errorSignals": {
5
+ "toolsSchemaConflict": [
6
+ "cannot be combined with (a )?(tool|function)",
7
+ "json[\\s_-]?(mode|schema|object|output)[^.]{0,60}(tool|function)",
8
+ "(tool|function)[\\s-]?call[^.]{0,60}json[\\s_-]?(mode|schema)",
9
+ "response_format[^.]{0,60}(tool|function)"
10
+ ],
11
+ "schemaComplexity": ["too many states"],
12
+ "responseFormatUnsupported": [
13
+ "does not support[^.]{0,80}response[\\s_-]?format",
14
+ "response[\\s_-]?format[^.]{0,60}(?:is )?not support",
15
+ "unsupported response[\\s_-]?format"
16
+ ]
17
+ },
18
+ "errorSignalNotes": {
19
+ "toolsSchemaConflict": "The provider cannot combine JSON output with tool calling (Groq: \"json mode cannot be combined with tool/function calling\"); the call is retried without structured output.",
20
+ "schemaComplexity": "Vertex Gemini rejects a response schema whose constrained decoding \"has too many states for serving\"; native schema enforcement is dropped and the text is coerced instead.",
21
+ "responseFormatUnsupported": "The model (not the provider) rejects response_format (Novita meta-llama/llama-3.3-70b-instruct rejects both json_schema and json_object); the schema is spelled into the prompt instead."
22
+ },
23
+ "finalResultTool": {
24
+ "name": "final_result",
25
+ "description": "Return the final structured result. You MUST call this tool when you have gathered all information and are ready to provide the final answer. The arguments should contain the structured data matching the expected schema.",
26
+ "instructions": {
27
+ "mustCall": "\n\nIMPORTANT: You MUST call the '{tool}' tool to return your response in the required structured format. Do not respond with plain text - always use the {tool} tool.",
28
+ "callWhenReady": "\n\nIMPORTANT: When you have gathered all necessary information and are ready to provide your final answer, you MUST call the '{tool}' tool with the structured data. Do not return the final answer as plain text - always use the {tool} tool.",
29
+ "noLongerAvailable": "\n\nThe {tool} tool is no longer available. Provide your final answer directly as plain text now, using the information gathered so far."
30
+ }
31
+ },
32
+ "finalResultToolNotes": {
33
+ "name": "The internal tool a model calls to return schema-shaped output while it can still call real tools. It is filtered out of every returned tool call and execution.",
34
+ "instructions": "Appended to the system prompt; {tool} is replaced with the tool name. mustCall: structured output with no other tools in play (Anthropic, Vertex Claude). callWhenReady: Vertex Gemini, with tools. noLongerAvailable: the tools-disabled synthesis call after the step cap."
35
+ }
36
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Extended-thinking budget limits, from the `thinkingLimits` block in
3
+ * `src/lib/providers/rules/<id>.json`.
4
+ */
5
+ import type { ProviderThinkingLimits } from "../../types/index.js";
6
+ /** A provider's thinking limits; throws when its rules file has none. */
7
+ export declare function getThinkingLimits(id: string): ProviderThinkingLimits;
@@ -0,0 +1,9 @@
1
+ import { getProviderRules } from "./registry.js";
2
+ /** A provider's thinking limits; throws when its rules file has none. */
3
+ export function getThinkingLimits(id) {
4
+ const limits = getProviderRules(id).thinkingLimits;
5
+ if (!limits) {
6
+ throw new Error(`Provider rules file "${id}.json" has no thinkingLimits block`);
7
+ }
8
+ return limits;
9
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Tool-choice spelling and tool-name rules, driven by the `toolChoices` and
3
+ * `toolNames` blocks in `src/lib/providers/rules/<id>.json`.
4
+ */
5
+ import type { ProviderCompiledToolNameRules, ProviderToolChoiceMap, ProviderToolNameRules } from "../../types/index.js";
6
+ /** The named tool-choice table from a rules file; throws when it is missing. */
7
+ export declare function getToolChoiceMap(id: string, name: string): ProviderToolChoiceMap;
8
+ /**
9
+ * Spell a tool choice for one wire (see ProviderToolChoiceMap). `toWireName`
10
+ * maps a registered tool name onto the name the wire knows it by. Returns
11
+ * undefined when nothing should be sent.
12
+ */
13
+ export declare function translateToolChoice(table: ProviderToolChoiceMap, choice: unknown, toWireName?: (toolName: string) => string): unknown;
14
+ /** A provider's tool-name rules; throws when its rules file has none. */
15
+ export declare function getToolNameRules(id: string): ProviderToolNameRules;
16
+ export declare function compileToolNameRules(rules: ProviderToolNameRules): ProviderCompiledToolNameRules;