All files / shared/src/types/ai-providers model-capabilities.ts

100% Statements 62/62
100% Branches 0/0
100% Functions 0/0
100% Lines 62/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249              1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x           1x           1x       1x         1x 1x               1x         1x         1x             1x           1x                             1x 1x             1x           1x                                       1x 1x             1x       1x           1x         1x                           1x         1x 1x                 1x       1x         1x           1x         1x         1x         1x 1x          
/**
 * Model capability validation types for AI providers.
 *
 * This module defines schemas for declaring which parameters and features
 * are supported by specific models across different AI providers.
 */
 
import { z } from 'zod';
 
/**
 * Model parameters as a const array for use with z.enum.
 */
const MODEL_PARAMETERS = [
  'temperature',
  'top_p',
  'max_tokens',
  'max_completion_tokens',
  'frequency_penalty',
  'presence_penalty',
  'stop',
  'seed',
  'reasoning_effort',
  'thinking',
  'logprobs',
  'top_logprobs',
] as const;
 
/**
 * Zod enum schema for model parameters.
 */
export const ModelParameterSchema = z.enum(MODEL_PARAMETERS);
 
/**
 * Inferred type from the schema.
 */
export type ModelParameter = z.infer<typeof ModelParameterSchema>;
 
/**
 * Enum-like object for backward compatibility.
 */
export const ModelParameter = {
  TEMPERATURE: 'temperature',
  TOP_P: 'top_p',
  MAX_TOKENS: 'max_tokens',
  MAX_COMPLETION_TOKENS: 'max_completion_tokens',
  FREQUENCY_PENALTY: 'frequency_penalty',
  PRESENCE_PENALTY: 'presence_penalty',
  STOP: 'stop',
  SEED: 'seed',
  REASONING_EFFORT: 'reasoning_effort',
  THINKING: 'thinking',
  LOGPROBS: 'logprobs',
  TOP_LOGPROBS: 'top_logprobs',
} as const;
 
/**
 * Zod schema for model parameters array.
 */
const ModelParameterArray = z.array(ModelParameterSchema);
 
/**
 * Zod schema for parameter mapping (e.g., legacy parameter remapping).
 * Uses z.string() keys to allow partial mappings.
 */
const ParameterMapping = z.record(z.string(), ModelParameterSchema);
 
/**
 * Zod schema for parameter range configuration.
 * Defines the valid range for a parameter (min/max values).
 */
export const ParameterRange = z.object({
  /**
   * Minimum value for this parameter.
   */
  min: z.number(),
 
  /**
   * Maximum value for this parameter.
   */
  max: z.number(),
});
 
export type ParameterRange = z.infer<typeof ParameterRange>;
 
/**
 * Zod schema for parameter ranges mapping.
 * Maps parameter names to their valid ranges.
 */
const ParameterRanges = z.record(z.string(), ParameterRange);
 
/**
 * Endpoint-specific parameter configuration schema.
 */
export const EndpointParameterConfig = z.object({
  /**
   * Parameters that are NOT supported for this endpoint.
   * If undefined, all parameters are assumed to be supported.
   */
  unsupportedParameters: ModelParameterArray.optional(),
 
  /**
   * Parameters that ARE supported for this endpoint.
   * If defined, only these parameters will be allowed.
   * Takes precedence over unsupportedParameters.
   */
  supportedParameters: ModelParameterArray.optional(),
 
  /**
   * Parameter remapping for this endpoint.
   * For example, o1 models use 'max_completion_tokens' instead of 'max_tokens'.
   */
  legacyParameterMapping: ParameterMapping.optional(),
 
  /**
   * Parameter ranges for this endpoint.
   * Defines the valid min/max values for each parameter.
   * Used to transform normalized 0-1 values to the model's expected range.
   *
   * Example:
   * ```typescript
   * parameterRanges: {
   *   temperature: { min: 0, max: 2 },    // OpenAI supports 0-2
   *   frequency_penalty: { min: -2, max: 2 }  // OpenAI supports -2 to 2
   * }
   * ```
   */
  parameterRanges: ParameterRanges.optional(),
});
 
export type EndpointParameterConfig = z.infer<typeof EndpointParameterConfig>;
 
/**
 * Configuration for a specific model's capabilities.
 */
export const ModelCapability = z.object({
  /**
   * Model identifier (e.g., 'gpt-4', 'grok-3-mini-beta').
   * Can use wildcards: 'gpt-4*' matches all GPT-4 variants.
   * Can also be a RegExp for complex patterns.
   */
  modelPattern: z.union([z.string(), z.instanceof(RegExp)]),
 
  /**
   * Endpoint-specific parameter configurations.
   * Keys should be FunctionName enum values.
   *
   * If not specified for an endpoint, all parameters are assumed supported.
   *
   * Example:
   * ```typescript
   * endpointConfigs: {
   *   [FunctionName.CHAT_COMPLETE]: {
   *     unsupportedParameters: [ModelParameter.REASONING_EFFORT]
   *   },
   *   [FunctionName.COMPLETE]: {
   *     supportedParameters: [ModelParameter.TEMPERATURE, ModelParameter.MAX_TOKENS]
   *   }
   * }
   * ```
   */
  endpointConfigs: z.record(z.string(), EndpointParameterConfig),
});
 
export type ModelCapability = z.infer<typeof ModelCapability>;
 
/**
 * Provider-specific model capabilities configuration.
 */
export const ProviderModelCapabilities = z.object({
  /**
   * AI provider identifier.
   */
  provider: z.string(),
 
  /**
   * Default parameters that are supported by all models from this provider.
   * Individual model configurations can override this.
   */
  defaultSupportedParameters: ModelParameterArray.optional(),
 
  /**
   * Default parameters that are NOT supported by any models from this provider.
   */
  defaultUnsupportedParameters: ModelParameterArray.optional(),
 
  /**
   * Default parameter ranges for all models from this provider.
   * Individual model configurations can override specific ranges.
   *
   * Example:
   * ```typescript
   * defaultParameterRanges: {
   *   temperature: { min: 0, max: 2 },
   *   frequency_penalty: { min: -2, max: 2 }
   * }
   * ```
   */
  defaultParameterRanges: ParameterRanges.optional(),
 
  /**
   * Model-specific capability configurations.
   */
  models: z.array(ModelCapability),
});
 
export type ProviderModelCapabilities = z.infer<
  typeof ProviderModelCapabilities
>;
 
/**
 * Result of parameter validation for a specific model.
 */
export const ParameterValidationResult = z.object({
  /**
   * Whether the parameter is supported.
   */
  isSupported: z.boolean(),
 
  /**
   * The parameter name to use (may be remapped for legacy models).
   */
  parameterName: ModelParameterSchema.optional(),
 
  /**
   * The transformed value (if parameter range mapping was applied).
   * If undefined, use the original value.
   */
  transformedValue: z.number().optional(),
 
  /**
   * The parameter range used for transformation (if applicable).
   */
  parameterRange: ParameterRange.optional(),
 
  /**
   * Reason why the parameter is not supported (if applicable).
   */
  reason: z.string().optional(),
 
  /**
   * Warning message (e.g., for deprecated parameters).
   */
  warning: z.string().optional(),
});
 
export type ParameterValidationResult = z.infer<
  typeof ParameterValidationResult
>;