All files / web/src/hooks use-agent-validation.ts

100% Statements 28/28
80% Branches 4/5
100% Functions 2/2
100% Lines 28/28

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  1x 1x 1x                               1x 45x 45x 45x 45x 45x 33x 33x 33x 45x 45x 45x   45x 45x 45x   45x 41x 41x   45x 45x 45x 45x 45x 45x 45x  
import type { Agent } from '@shared/types/data';
import { isAgentReady } from '@shared/utils/agent-validation';
import { useQuery } from '@tanstack/react-query';
import { getSkills } from '@web/api/v1/super-agents/skills';
 
export interface UseAgentValidationResult {
  isReady: boolean;
  skillsCount: number;
  isLoading: boolean;
  missingRequirements: string[];
}
 
/**
 * Hook to check if an agent is ready (has at least one skill).
 * Fetches the skills count for the agent and returns validation status.
 *
 * @param agent - The agent to validate
 * @returns Validation result with readiness status, skills count, and loading state
 */
export function useAgentValidation(
  agent: Agent | null | undefined,
): UseAgentValidationResult {
  const { data: skills = [], isLoading } = useQuery({
    queryKey: ['agent-validation', agent?.id],
    queryFn: async () => {
      if (!agent) return [];
      return await getSkills({ agent_id: agent.id });
    },
    enabled: !!agent,
    staleTime: 30 * 1000, // Cache for 30 seconds
  });
 
  const skillsCount = skills.length;
  const ready = isAgentReady(skillsCount);
  const missingRequirements: string[] = [];
 
  if (!ready) {
    missingRequirements.push('At least one skill must be configured');
  }
 
  return {
    isReady: ready,
    skillsCount,
    isLoading,
    missingRequirements,
  };
}