HTTP 失败映射和受控请求字段
策略的具体实现如下。
ts
httpFailures: Readonly<Record<number, ModelProviderErrorCode>> = Object.freeze({
400: 'MODEL_CONFIG',
401: 'MODEL_AUTH',
402: 'MODEL_BALANCE',
403: 'MODEL_AUTH',
404: 'MODEL_CONFIG',
429: 'MODEL_BUSY',
})
reservedRequestFields = [
'model',
'messages',
'stream',
'max_tokens',
'max_completion_tokens',
'toJSON',
] as const
function assertRequestExtensions(config: CompatibleModelConfig): void {
if (
config.maxOutputTokens !== undefined &&
(!Number.isSafeInteger(config.maxOutputTokens) || config.maxOutputTokens < 1)
) {
throw new TypeError('Provider maxOutputTokens must be a positive safe integer');
}
for (const key of reservedRequestFields) {
if (config.requestBody && Object.hasOwn(config.requestBody, key)) {
throw new TypeError(`Provider requestBody cannot override ${key}`);
}
}
}