All files / api/src/middlewares super-agents-configuration.ts

28.49% Statements 106/372
100% Branches 14/14
40% Functions 2/5
28.49% Lines 106/372

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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 5041x     1x 1x 1x         1x 1x 1x               1x               1x               2402x 2402x 2402x 2402x 2402x 2402x 2402x                     2402x 2402x 2402x 2402x     2402x 2402x 2502x 2502x   2402x 2402x 2402x                     2402x   2402x   4804x 4804x 4804x 4804x     4804x 4804x 4804x 4804x   4804x 5100x 5100x 5100x 5100x   5100x 4500x 4500x 5100x     4804x 4804x 4804x       4804x 4804x 4804x 4804x           4804x 4804x   4804x 4804x   4804x 4804x 4804x 4804x 4804x 4804x 4804x 4804x 4804x 4804x 4804x 4804x   4804x 3073x 3073x 3073x 4804x   2402x 2402x     1x 5x 5x 5x   5x 5x   5x 10x 10x 8x 8x 8x 10x   5x 5x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1x 1x                                                                                                                               1x  
import { handleGenerateArms } from '@api/optimization/skill-optimizations';
import type { UserDataStorageConnector } from '@api/types/connector';
import type { AppContext } from '@api/types/hono';
import { generateEmbeddingForRequest } from '@api/utils/embeddings';
import { resolveEmbeddingModelConfig } from '@api/utils/evaluation-model-resolver';
import {
  cosineSimilarity,
  getInitialClusterCentroids,
  sampleBeta,
} from '@api/utils/math';
import { renderTemplate } from '@api/utils/templates';
import { error } from '@shared/console-logging';
import {
  FunctionName,
  OptimizationType,
  type SuperAgentsConfig,
  SuperAgentsTarget,
  type SuperAgentsTargetPreProcessed,
  type TargetConfigurationParams,
} from '@shared/types/api/request';
import { ReasoningEffort } from '@shared/types/api/routes/shared/thinking';
import type { AIProvider } from '@shared/types/constants';
import type {
  SkillOptimizationArm,
  SkillOptimizationCluster,
} from '@shared/types/data';
import type { SkillOptimizationClusterCreateParams } from '@shared/types/data/skill-optimization-cluster';
import type { Next } from 'hono';
import { createMiddleware } from 'hono/factory';
 
/**
 * Exported for tests. This is the decision that picks which configuration
 * serves a request, and the middleware around it needs a database, an
 * embedding and a resolved skill before it is reached -- so the choice itself
 * is only reachable directly.
 */
export async function getOptimalArm(
  c: AppContext,
  arms: SkillOptimizationArm[],
  skillId: string,
  userDataStorageConnector: UserDataStorageConnector,
  explorationTemperature = 1.0,
): Promise<SkillOptimizationArm> {
  // Implement Thompson Sampling algorithm for multi-armed bandit with weighted evaluations
  // Thompson Sampling uses Bayesian approach: sample from posterior Beta distribution
  // and select the arm with highest sampled value
  //
  // The exploration_temperature parameter controls exploration/exploitation:
  // - temperature = 1.0: Standard Thompson Sampling (balanced)
  // - temperature > 1.0: More exploration (flattens distribution, takes more risks)
  // - temperature < 1.0: More exploitation (sharpens distribution, sticks to known good arms)
 
  // Fetch evaluations to get weights
  const evaluations =
    await userDataStorageConnector.getSkillOptimizationEvaluations(c, {
      skill_id: skillId,
    });
 
  // Create a map of evaluation_id -> weight
  const evaluationWeights = new Map<string, number>();
  for (const evaluation of evaluations) {
    evaluationWeights.set(evaluation.id, evaluation.weight);
  }
 
  let optimalArm = arms[0];
  let maxSample = -Infinity;
  const samples: {
    armId: string;
    n: number;
    weighted_mean: number;
    total_reward: number;
    alpha: number;
    beta: number;
    alpha_adjusted: number;
    beta_adjusted: number;
    baseSample: number;
    sample: number;
  }[] = [];
 
  for (const arm of arms) {
    // Fetch arm stats for this arm
    const armStats =
      await userDataStorageConnector.getSkillOptimizationArmStats(c, {
        arm_id: arm.id,
      });
 
    // Calculate weighted average mean and total reward
    let weightedMeanSum = 0;
    let totalWeight = 0;
    let totalRequests = 0;
    let weightedTotalReward = 0;
 
    for (const stat of armStats) {
      const weight = evaluationWeights.get(stat.evaluation_id) || 1.0;
      weightedMeanSum += stat.mean * weight;
      weightedTotalReward += stat.total_reward * weight;
      totalWeight += weight;
      // Use max n across all evaluations as the request count
      if (stat.n > totalRequests) {
        totalRequests = stat.n;
      }
    }
 
    // Calculate weighted mean (0 if no stats yet)
    const weightedMean = totalWeight > 0 ? weightedMeanSum / totalWeight : 0;
    const weightedReward =
      totalWeight > 0 ? weightedTotalReward / totalWeight : 0;
 
    // Beta distribution parameters with uniform prior (Beta(1,1))
    // alpha = successes + 1, beta = failures + 1
    const successes = weightedReward;
    const failures = totalRequests - weightedReward;
    const baseAlpha = successes + 1;
    const baseBeta = failures + 1;
 
    // Apply temperature to Beta parameters BEFORE sampling
    // Higher temperature (> 1) shrinks parameters toward 1, making distribution more uniform
    // Lower temperature (< 1) exaggerates parameters, making distribution more peaked
    // This is the correct way to apply temperature in Thompson Sampling
    const alpha = (baseAlpha - 1) / explorationTemperature + 1;
    const beta = (baseBeta - 1) / explorationTemperature + 1;
 
    const baseSample = sampleBeta(alpha, beta);
    const sample = baseSample;
 
    samples.push({
      armId: arm.id,
      n: totalRequests,
      weighted_mean: weightedMean,
      total_reward: weightedReward,
      alpha: baseAlpha,
      beta: baseBeta,
      alpha_adjusted: alpha,
      beta_adjusted: beta,
      baseSample,
      sample,
    });
 
    if (sample > maxSample) {
      maxSample = sample;
      optimalArm = arm;
    }
  }
 
  return optimalArm;
}
 
/** Exported for tests, for the same reason as `getOptimalArm`. */
export function getOptimalCluster(
  embedding: number[],
  clusters: SkillOptimizationCluster[],
): SkillOptimizationCluster {
  // Find the cluster with the highest cosine similarity to the embedding
  let optimalCluster = clusters[0];
  let maxSimilarity = -1;
 
  for (const cluster of clusters) {
    const similarity = cosineSimilarity(embedding, cluster.centroid);
    if (similarity > maxSimilarity) {
      maxSimilarity = similarity;
      optimalCluster = cluster;
    }
  }
 
  return optimalCluster;
}
 
function getRandomValueInRange(min: number, max: number): number {
  return Math.random() * (max - min) + min;
}
 
const ReasoningMap: Record<number, ReasoningEffort | null> = {
  0: null,
  1: null,
  2: ReasoningEffort.MINIMAL,
  3: ReasoningEffort.MINIMAL,
  4: ReasoningEffort.LOW,
  5: ReasoningEffort.LOW,
  6: ReasoningEffort.MEDIUM,
  7: ReasoningEffort.MEDIUM,
  8: ReasoningEffort.HIGH,
  9: ReasoningEffort.HIGH,
};
 
function getRandomReasoningEffortFromRange(
  min: number,
  max: number,
): ReasoningEffort | null {
  const randomValue = getRandomValueInRange(min, max) * 9.9;
 
  const randomIndex = Math.floor(randomValue);
 
  return ReasoningMap[randomIndex];
}
 
async function validateTargetConfiguration(
  c: AppContext,
  userDataStorageConnector: UserDataStorageConnector,
  saTargetPreProcessed: SuperAgentsTargetPreProcessed,
  embedding: number[] | null,
  systemPromptVariables?: Record<string, unknown>,
): Promise<SuperAgentsTarget | Response> {
  let saTargetConfiguration: TargetConfigurationParams;
  let resolvedApiKey: string | undefined;
  let resolvedCustomHost: string | undefined;
 
  // Apply optimization if specified
  // Optimizations can only be applied if the embedding is provided
  if (
    embedding &&
    saTargetPreProcessed.optimization === OptimizationType.AUTO
  ) {
    try {
      const skill = c.get('skill');
 
      let clusters =
        await userDataStorageConnector.getSkillOptimizationClusters(c, {
          skill_id: skill.id,
        });
 
      // Get embedding model config for cluster centroids
      const embeddingConfig = await resolveEmbeddingModelConfig(
        c,
        userDataStorageConnector,
      );
 
      if (clusters.length === 0) {
        // Only create clusters if embedding model is configured
        if (embeddingConfig) {
          try {
            // Create initial clusters with equally spaced centroids
            const initialCentroids = getInitialClusterCentroids(
              skill.configuration_count,
              embeddingConfig.dimensions,
            );
            const clusterParams: SkillOptimizationClusterCreateParams[] =
              initialCentroids.map((centroid, index) => ({
                agent_id: skill.agent_id,
                skill_id: skill.id,
                name: `${index + 1}`,
                total_steps: 0,
                observability_total_requests: 0,
                centroid,
                embedding_model_id: embeddingConfig.modelId,
              }));
 
            clusters =
              await userDataStorageConnector.createSkillOptimizationClusters(
                c,
                clusterParams,
              );
 
            await handleGenerateArms(c, userDataStorageConnector, skill.id);
          } catch (_error) {
            // If cluster creation fails (e.g., duplicate from concurrent request),
            // fetch the existing clusters instead
            clusters =
              await userDataStorageConnector.getSkillOptimizationClusters(c, {
                skill_id: skill.id,
              });
            // If we still have no clusters, throw the original error
            if (clusters.length === 0) {
              throw _error;
            }
          }
        }
      }
 
      const optimalCluster = getOptimalCluster(embedding, clusters);
 
      const arms = await userDataStorageConnector.getSkillOptimizationArms(c, {
        skill_id: skill.id,
        cluster_id: optimalCluster.id,
      });
 
      const optimalArm = await getOptimalArm(
        c,
        arms,
        skill.id,
        userDataStorageConnector,
        skill.exploration_temperature,
      );
 
      c.set('pulled_arm', optimalArm);
 
      const renderedSystemPrompt = renderTemplate(
        optimalArm.params.system_prompt!,
        systemPromptVariables || {},
        skill.allowed_template_variables,
      );
 
      // Resolve model_id to get model name and provider
      const models = await userDataStorageConnector.getModels(c, {
        id: optimalArm.params.model_id,
      });
 
      if (models.length === 0) {
        return c.json(
          {
            error: `Model with ID '${optimalArm.params.model_id}' not found`,
          },
          422,
        );
      }
 
      const model = models[0];
 
      // Get the provider from the associated API key
      const apiKeyRecord =
        await userDataStorageConnector.getAIProviderAPIKeyById(
          c,
          model.ai_provider_id,
        );
 
      if (!apiKeyRecord) {
        return c.json(
          {
            error: `API key with ID '${model.ai_provider_id}' not found for model`,
          },
          422,
        );
      }
 
      resolvedApiKey = apiKeyRecord.api_key || undefined;
      resolvedCustomHost =
        (apiKeyRecord.custom_fields?.custom_host as string) || undefined;
 
      const reasoningEffort = getRandomReasoningEffortFromRange(
        optimalArm.params.thinking_min,
        optimalArm.params.thinking_max,
      );
 
      saTargetConfiguration = {
        ai_provider: apiKeyRecord.ai_provider as AIProvider,
        model: model.model_name,
        system_prompt: renderedSystemPrompt,
        temperature: getRandomValueInRange(
          optimalArm.params.temperature_min,
          optimalArm.params.temperature_max,
        ),
        top_p: getRandomValueInRange(
          optimalArm.params.top_p_min,
          optimalArm.params.top_p_max,
        ),
        frequency_penalty: getRandomValueInRange(
          optimalArm.params.frequency_penalty_min,
          optimalArm.params.frequency_penalty_max,
        ),
        presence_penalty: getRandomValueInRange(
          optimalArm.params.presence_penalty_min,
          optimalArm.params.presence_penalty_max,
        ),
        reasoning_effort: reasoningEffort,
        seed: null,
        max_tokens: null,
        additional_params: null,
        stop: null,
      };
    } catch (e) {
      error(e);
      return c.json(
        {
          error: `Failed to load optimization parameters: ${e instanceof Error ? e.message : 'Unknown error'}`,
        },
        500,
      );
    }
  } else if (saTargetPreProcessed.provider) {
    const provider = saTargetPreProcessed.provider;
    const model = saTargetPreProcessed.model;
 
    if (!model) {
      return c.json(
        {
          error: `model must be defined`,
        },
        422,
      );
    }
 
    saTargetConfiguration = {
      ai_provider: provider,
      model: model,
      system_prompt: null,
      temperature: null,
      max_tokens: null,
      top_p: null,
      frequency_penalty: null,
      presence_penalty: null,
      stop: null,
      seed: null,
      additional_params: null,
      reasoning_effort: null,
    };
  } else {
    return c.json(
      {
        error: `No configuration_name or provider defined`,
      },
      500,
    );
  }
 
  if (saTargetPreProcessed.api_key) {
    resolvedApiKey = saTargetPreProcessed.api_key;
  }
 
  const rawData = {
    ...saTargetPreProcessed,
    configuration: saTargetConfiguration,
    api_key: resolvedApiKey,
    // Use resolved custom_host from API key if available, otherwise keep any directly provided value
    custom_host: resolvedCustomHost || saTargetPreProcessed.custom_host,
    provider: undefined,
    configuration_name: undefined,
    configuration_version: undefined,
    model: undefined,
  };
 
  const parseResult = SuperAgentsTarget.safeParse(rawData);
 
  if (!parseResult.success) {
    error(
      'Error while parsing Super Agents target configuration',
      parseResult.error,
    );
    return c.json(
      {
        error: `Error while parsing Super Agents target configuration`,
      },
      500,
    );
  }
 
  return parseResult.data;
}
 
export const saConfigurationInjectorMiddleware = createMiddleware(
  async (c: AppContext, next: Next) => {
    const url = new URL(c.req.url);
 
    // Only set variables for API requests
    if (url.pathname.startsWith('/v1/')) {
      // Don't set variables for Super Agents API requests
      if (!url.pathname.startsWith('/v1/super-agents')) {
        const saConfigPreProcessed = c.get('sa_config_pre_processed');
        const saRequestData = c.get('sa_request_data');
 
        // Generate embeddings for specific endpoints
        // The embedding will be saved with the log after the request is completed
        let embedding = null;
        if (
          saRequestData &&
          (saRequestData.functionName === FunctionName.CHAT_COMPLETE ||
            saRequestData.functionName === FunctionName.STREAM_CHAT_COMPLETE ||
            saRequestData.functionName === FunctionName.CREATE_MODEL_RESPONSE)
        ) {
          try {
            embedding = await generateEmbeddingForRequest(
              c,
              saRequestData,
              c.get('user_data_storage_connector'),
            );
          } catch {
            embedding = null;
          }
          c.set('embedding', embedding);
        }
 
        const saTargetsOrResponses = await Promise.all(
          saConfigPreProcessed.targets.map((target) =>
            validateTargetConfiguration(
              c,
              c.get('user_data_storage_connector'),
              target,
              embedding,
              saConfigPreProcessed.system_prompt_variables,
            ),
          ),
        );
 
        // In case of an error return a response
        for (const saTargetOrResponse of saTargetsOrResponses) {
          if (saTargetOrResponse instanceof Response) {
            return saTargetOrResponse;
          }
        }
 
        const saTargets = saTargetsOrResponses.filter(
          (target) => !(target instanceof Response),
        ) as SuperAgentsTarget[];
 
        const saConfig: SuperAgentsConfig = {
          ...saConfigPreProcessed,
          targets: saTargets,
        };
 
        c.set('sa_config', saConfig);
      }
    }
    await next();
  },
);