All files / api/src/connectors/evaluations/role-adherence/service evaluate.ts

91.15% Statements 103/113
16.66% Branches 2/12
100% Functions 2/2
91.15% Lines 103/113

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 1421x 1x 1x     1x 1x 1x           1x   2x 2x 2x         2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x       2x   2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x   2x   2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x   2x 2x       2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x             2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x  
import { getRoleAdherenceMainTemplate } from '@api/connectors/evaluations/role-adherence/templates/main';
import { RoleAdherenceEvaluationParameters } from '@api/connectors/evaluations/role-adherence/types';
import { createLLMJudge } from '@api/evaluations/llm-judge';
import type { UserDataStorageConnector } from '@api/types/connector';
import type { AppContext } from '@api/types/hono';
import { resolveEvaluationModelConfig } from '@api/utils/evaluation-model-resolver';
import { extractOutputFromResponseBody } from '@api/utils/super-agents/responses';
import { SuperAgentsResponseBody } from '@shared/types/api/response';
import type {
  SkillOptimizationEvaluation,
  SkillOptimizationEvaluationResult,
} from '@shared/types/data';
import type { Log } from '@shared/types/data/log';
import { EvaluationMethodName } from '@shared/types/evaluations';
 
function pickRoleData(
  log: Log,
  params: RoleAdherenceEvaluationParameters,
): {
  role_definition: string;
  assistant_output: string;
  instructions?: string;
} {
  const role_definition =
    params.role_definition || (log.metadata?.role_definition as string) || '';
 
  // Extract assistant output using standard utilities
  let assistant_output = params.assistant_output;
  if (!assistant_output) {
    try {
      const responseBody = SuperAgentsResponseBody.parse(
        log.ai_provider_request_log.response_body,
      );
      assistant_output = extractOutputFromResponseBody(responseBody);
    } catch {
      // Fallback to metadata if parsing fails
      assistant_output = (log.metadata?.assistant_output as string) || '';
    }
  }
 
  const instructions =
    params.instructions || (log.metadata?.instructions as string);
  return { role_definition, assistant_output, instructions };
}
 
export async function evaluateLog(
  c: AppContext,
  evaluation: SkillOptimizationEvaluation,
  log: Log,
  storageConnector: UserDataStorageConnector,
): Promise<SkillOptimizationEvaluationResult> {
  const params = RoleAdherenceEvaluationParameters.parse(evaluation.params);
 
  // Resolve model configuration from evaluation.model_id or system settings
  const modelConfig = await resolveEvaluationModelConfig(
    c,
    evaluation,
    storageConnector,
  );
 
  const llmJudge = createLLMJudge(
    c,
    {
      temperature: params.temperature,
      max_tokens: params.max_tokens,
    },
    modelConfig ?? undefined,
  );
 
  const start_time = Date.now();
 
  const { role_definition, assistant_output, instructions } = pickRoleData(
    log,
    params,
  );
 
  const tpl = getRoleAdherenceMainTemplate({
    role_definition,
    assistant_output,
    instructions,
    strict_mode: params.strict_mode || false,
    verbose_mode: params.verbose_mode ?? true,
    include_reason: params.include_reason ?? true,
  });
 
  const judgeResult = await llmJudge.evaluate({
    text: `${tpl.systemPrompt}\n\n${tpl.userPrompt}`,
  });
 
  let final_score = judgeResult.score;
  if (params.strict_mode) {
    final_score = final_score === 1.0 ? 1.0 : 0.0;
  }
 
  const execution_time = Date.now() - start_time;
  const judgeModelName = modelConfig?.model ?? null;
  const judgeModelProvider = modelConfig?.provider ?? null;
 
  const evaluationResult: SkillOptimizationEvaluationResult = {
    evaluation_id: evaluation.id,
    method: EvaluationMethodName.ROLE_ADHERENCE,
    score: final_score,
    extra_data: {
      reasoning: judgeResult.reasoning,
      role_definition,
      assistant_output,
      instructions,
      strict_mode: params.strict_mode,
      metadata: judgeResult.metadata,
      execution_time,
      execution_time_ms: execution_time,
      evaluated_at: new Date().toISOString(),
    },
    display_info: [
      {
        label: 'Reasoning',
        content: judgeResult.reasoning,
      },
      {
        label: 'Role Definition',
        content: role_definition,
      },
      ...(instructions
        ? [
            {
              label: 'Additional Instructions',
              content: instructions,
            },
          ]
        : []),
      {
        label: 'Assistant Output',
        content: assistant_output,
      },
    ],
    judge_model_name: judgeModelName,
    judge_model_provider: judgeModelProvider,
  };
 
  return evaluationResult;
}