All files / shared/src/types/api/request headers.ts

99.18% Statements 242/244
100% Branches 26/26
50% Functions 1/2
99.18% Lines 242/244

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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 3571x 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 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 1x 1x 1x 1x 1x 27x 1x 1x 26x 1x 1x 27x 27x 3x 27x 1x 1x 26x 1x 1x 27x 1x 1x 26x 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 1x 1x 1x 1x       1x 1x 1x   1x 1x 22x 53x 53x 53x 53x 50x 53x 1x 1x 53x 21x 22x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x          
import { SuperAgentsRequestBody } from '@shared/types/api/request/body';
import { ReasoningEffort } from '@shared/types/api/routes/shared/thinking';
 
import { AIProvider, RETRY_STATUS_CODES } from '@shared/types/constants';
import { CacheMode, CacheSettings } from '@shared/types/middleware/cache';
import { Hook } from '@shared/types/middleware/hooks';
import { removeEndingPath, removeTrailingSlash } from '@shared/utils/url';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
 
export enum HeaderKey {
  CONFIG = 'sa-config',
  CONTENT_TYPE = 'content-type',
}
 
/**
 * Settings for retrying requests.
 */
export const RetrySettings = z.object({
  /** The maximum number of retry attempts. */
  attempts: z.number(),
  /** The HTTP status codes on which to retry. */
  on_status_codes: z.array(z.number()).default(RETRY_STATUS_CODES).optional(),
  /** Whether to use the provider's retry wait. */
  use_retry_after_header: z.boolean().optional(),
});
 
export type RetrySettings = z.infer<typeof RetrySettings>;
 
export const AzureOpenAIConfig = z.object({
  url: z
    .url({ error: '`url` is required' })
    .transform(removeTrailingSlash)
    .transform(removeEndingPath),
});
 
export const AzureAIFoundryConfig = z.object({
  url: z
    .url({ error: '`url` is required' })
    .transform(removeTrailingSlash)
    .transform(removeEndingPath),
});
 
export const AnthropicConfig = z.object({
  url: z
    .url({ error: '`url` is required' })
    .transform(removeTrailingSlash)
    .transform(removeEndingPath),
});
 
export const SuperAgentsTargetBase = z.object({
  // Target Details
  id: z.string().optional(),
  index: z.number().optional(),
 
  // Target Settings
  weight: z.number().default(1),
  on_status_codes: z.array(z.number()).optional(),
  request_timeout: z.number().optional(),
  custom_host: z.string().optional(),
  forward_headers: z.array(z.string()).optional(),
  cache: CacheSettings.default({ mode: CacheMode.DISABLED }),
  retry: RetrySettings.default({ attempts: 0 }),
 
  inner_provider: z.enum(AIProvider).optional(),
  // TODO: Remove this required field and allow defining through the UI
  api_key: z
    .string()
    .optional()
    .describe('The API key for the provider if not defined through the UI'),
 
  // Reference to stored API key - alternative to direct api_key
  api_key_id: z
    .string()
    .uuid()
    .optional()
    .describe('Reference to stored API key ID from ai_provider_api_keys table'),
 
  // Anthropic specific
  anthropic_beta: z.string().optional(),
  anthropic_version: z.string().optional(),
 
  // AWS specific
  aws_secret_access_key: z.string().optional(),
  aws_access_key_id: z.string().optional(),
  aws_session_token: z.string().optional(),
  aws_region: z.string().optional(),
  aws_auth_type: z.string().optional(),
  aws_role_arn: z.string().optional(),
  aws_external_id: z.string().optional(),
  aws_s3_bucket: z.string().optional(),
  aws_s3_object_key: z.string().optional(),
  aws_bedrock_model: z.string().optional(),
  aws_server_side_encryption: z.string().optional(),
  aws_server_side_encryption_kms_key_id: z.string().optional(),
 
  // Azure OpenAI specific
  azure_openai_config: AzureOpenAIConfig.optional(),
 
  // Azure AI Foundry specific
  azure_ai_foundry_config: AzureAIFoundryConfig.optional(),
 
  // Azure Other
  azure_extra_params: z.string().optional(),
  azure_auth_mode: z.string().optional(),
  azure_managed_client_id: z.string().optional(),
  azure_entra_client_id: z.string().optional(),
  azure_entra_client_secret: z.string().optional(),
  azure_entra_tenant_id: z.string().optional(),
  azure_ad_auth: z.string().optional(),
  azure_ad_token: z.string().optional(),
 
  // Cortex specific
  snowflake_account: z.string().optional(),
 
  // Fireworks fine-tune required fields
  fireworks_account_id: z.string().optional(),
 
  // Google specific
  vertex_service_account_json: z.string().optional(),
  vertex_region: z.string().optional(),
  vertex_project_id: z.string().optional(),
  vertex_storage_bucket_name: z.string().optional(),
  vertex_model_name: z.string().optional(),
  // Required for file uploads with google.
  filename: z.string().optional(),
 
  // Hugging Face specific
  huggingface_base_url: z.string().optional(),
 
  // Mistral specific
  // Parameter to determine if fim/completions endpoint is to be used
  mistral_fim_completion: z.string().optional(),
 
  // OpenAI specific
  openai_project: z.string().optional(),
  openai_organization: z.string().optional(),
  openai_beta: z.string().optional(),
 
  // Sagemaker specific
  amzn_sagemaker_custom_attributes: z.string().optional(),
  amzn_sagemaker_target_model: z.string().optional(),
  amzn_sagemaker_target_variant: z.string().optional(),
  amzn_sagemaker_target_container_hostname: z.string().optional(),
  amzn_sagemaker_inference_id: z.string().optional(),
  amzn_sagemaker_session_id: z.string().optional(),
  amzn_sagemaker_model_name: z.string().optional(),
 
  // Stability AI specific
  stability_client_id: z.string().optional(),
  stability_client_user_id: z.string().optional(),
  stability_client_version: z.string().optional(),
  stability_url_to_fetch: z.string().optional(),
 
  // Ollama specific
  ollama_url_to_fetch: z.string().optional(),
 
  // Mistral AI specific
});
 
export type SuperAgentsTargetBase = z.infer<typeof SuperAgentsTargetBase>;
 
export enum OptimizationType {
  AUTO = 'auto',
  NONE = 'none',
}
 
export const SuperAgentsTargetPreProcessed = SuperAgentsTargetBase.extend({
  /** Super Agents optimization type */
  optimization: z
    .enum(OptimizationType)
    .optional()
    .default(OptimizationType.NONE)
    .describe('The name of the Super Agents configuration to use'),
 
  /** Super Agents optimization version */
  optimization_version: z
    .number()
    .min(0)
    .optional()
    .describe('The version of the Super Agents configuration to use'),
 
  /** The AI provider to use if no configuration is provided */
  provider: z
    .enum(AIProvider, {
      error: (err) => {
        return `Invalid provider: ${err.input}`;
      },
    })
    .optional()
    .describe('The AI provider to use if no configuration is provided'),
 
  /** The AI model to use if no configuration is provided */
  model: z
    .string()
    .optional()
    .describe('The AI model to use if no configuration is provided'),
})
  .refine((data) => {
    if (!data.provider && data.optimization === OptimizationType.NONE) {
      return false;
    }
    return true;
  }, '`provider` is required when optimization is not set to auto')
  .refine((data) => {
    if (
      data.optimization_version &&
      data.optimization === OptimizationType.NONE
    ) {
      return false;
    }
    return true;
  }, '`optimization_version` is defined, but `optimization` is set to none. Set `optimization` to auto to use an optimization version.')
  .refine((data) => {
    if (data.provider && !data.model) {
      return false;
    }
    return true;
  }, 'A model is required when using a provider.');
 
export type SuperAgentsTargetPreProcessed = z.infer<
  typeof SuperAgentsTargetPreProcessed
>;
 
// Configuration parameters - the AI parameters for a specific version
export const TargetConfigurationParams = z.object({
  ai_provider: z.enum(AIProvider),
  model: z.string().min(1),
  system_prompt: z.string().min(1).nullable(),
  temperature: z.number().min(0).max(2).nullable(),
  max_tokens: z.number().int().positive().nullable(),
  top_p: z.number().min(0).max(1).nullable(),
  frequency_penalty: z.number().min(-2).max(2).nullable(),
  presence_penalty: z.number().min(-2).max(2).nullable(),
  stop: z.array(z.string()).nullable(),
  seed: z.number().int().nullable(),
  reasoning_effort: z.enum(ReasoningEffort).nullable(),
  // Additional provider-specific parameters can be added here
  additional_params: z.record(z.string(), z.unknown()).nullable(),
});
export type TargetConfigurationParams = z.infer<
  typeof TargetConfigurationParams
>;
 
/** Super Agents Target with configuration name and version validated and processed.
 * The configuration options have already been applied to the target.
 *
 * For example, the `provider` and `model` fields have already been set to the value of the configuration. */
export const SuperAgentsTarget = SuperAgentsTargetBase.extend({
  configuration: TargetConfigurationParams,
  api_key: z.string().describe('The API key for the provider').optional(),
});
 
export type SuperAgentsTarget = z.infer<typeof SuperAgentsTarget>;
 
export enum StrategyModes {
  LOADBALANCE = 'loadbalance',
  FALLBACK = 'fallback',
  SINGLE = 'single',
  CONDITIONAL = 'conditional',
}
 
export const Strategy = z.object({
  mode: z.enum(StrategyModes).describe('The strategy mode to use'),
  on_status_codes: z.array(z.number()).optional(),
  conditions: z
    .array(
      z.object({
        query: z.record(z.string(), z.unknown()),
        target: z.string(),
      }),
    )
    .optional(),
  default: z.string().optional(),
});
 
export type Strategy = z.infer<typeof Strategy>;
 
export const NonPrivateSuperAgentsConfig = z.object({
  agent_name: z.string({ error: 'Agent name is required' }),
  skill_name: z.string({ error: 'Skill name is required' }),
  override_params: SuperAgentsRequestBody.optional(),
  request_timeout: z.number().optional(),
  forward_headers: z.array(z.string()).optional(),
  force_refresh: z.boolean().optional(),
  force_hook_refresh: z.boolean().optional(),
  strict_open_ai_compliance: z.boolean().optional(),
  metadata: z.record(z.string(), z.unknown()).optional(),
 
  /** Variables for the system prompt template in the Super Agents configuration */
  system_prompt_variables: z
    .record(z.string(), z.unknown())
    .optional()
    .describe(
      'The variables for the system prompt template in the Super Agents configuration',
    ),
});
 
export type NonPrivateSuperAgentsConfig = z.infer<
  typeof NonPrivateSuperAgentsConfig
>;
 
export const BaseSuperAgentsConfig = NonPrivateSuperAgentsConfig.extend({
  strategy: Strategy.default({
    mode: StrategyModes.SINGLE,
  }),
  hooks: z.array(Hook).default([]),
 
  // Observability
  app_id: z.string().optional(),
  // Use a function to generate UUID lazily at parse time, not module load time
  // This is required for Cloudflare Workers which disallow random generation in global scope
  trace_id: z.string().default(() => uuidv4()),
  span_id: z.string().optional(),
  span_name: z.string().optional(),
  parent_span_id: z.string().optional(),
  user_human_name: z.string().optional(),
});
 
export type BaseSuperAgentsConfig = z.infer<typeof BaseSuperAgentsConfig>;
 
export const SuperAgentsConfig = BaseSuperAgentsConfig.extend({
  targets: z.array(SuperAgentsTarget),
})
  // Validate Google Vertex AI specific fields
  .refine(
    (value) => {
      for (const target of value.targets) {
        const isGoogleVertexAIProvider =
          target.configuration.ai_provider === AIProvider.GOOGLE_VERTEX_AI;
        const hasGoogleVertexAIFields =
          (target.vertex_project_id && target.vertex_region) ||
          (target.vertex_region && target.vertex_service_account_json);
        if (isGoogleVertexAIProvider && !hasGoogleVertexAIFields) {
          return false;
        }
      }
      return true;
    },
    {
      message: `Invalid configuration. ('vertex_project_id' and 'vertex_region') or ('vertex_service_account_json' and 'vertex_region') are required for Google Vertex AI provider. Example: { 'provider': 'vertex-ai', 'vertex_project_id': 'my-project-id', 'vertex_region': 'us-central1', api_key: 'ya29...' }`,
    },
  );
 
export type SuperAgentsConfig = z.infer<typeof SuperAgentsConfig>;
 
export const SuperAgentsConfigPreProcessed = BaseSuperAgentsConfig.extend({
  targets: z.preprocess(
    (val) => val ?? [{ optimization: OptimizationType.AUTO }],
    z.array(SuperAgentsTargetPreProcessed),
  ),
});
 
export type SuperAgentsConfigPreProcessed = z.infer<
  typeof SuperAgentsConfigPreProcessed
>;