Skip to content
中文

HTTP failure mapping and controlled request fields ​

The policy implementation follows.

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}`);
    }
  }
}

API reference, usage examples and developer guides.