All files / api/src/middlewares agent-and-skill.ts

75% Statements 30/40
80% Branches 8/10
100% Functions 0/0
75% Lines 30/40

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  1x 1x   1x   1x 1x 12x     12x   11x 7x 7x 7x 7x 7x 7x 7x           6x 6x 6x 6x 6x 6x 6x 7x           5x 5x 5x 11x 10x 12x 1x  
import type { AppContext } from '@api/types/hono';
import { getAgent } from '@api/utils/super-agents/agents';
import { getSkill } from '@api/utils/super-agents/skills';
import type { Next } from 'hono';
import { createMiddleware } from 'hono/factory';
 
export const agentAndSkillMiddleware = 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 saConfig = c.get('sa_config_pre_processed');
        const agent = await getAgent(
          c,
          c.get('user_data_storage_connector'),
          saConfig.agent_name,
        );
        if (!agent) {
          return c.json(
            { error: `Agent with name ${saConfig.agent_name} not found` },
            404,
          );
        }
        const skill = await getSkill(
          c,
          c.get('user_data_storage_connector'),
          agent.id,
          agent.name,
          saConfig.skill_name,
        );
        if (!skill) {
          return c.json(
            { error: `Skill with name ${saConfig.skill_name} not found` },
            404,
          );
        }
        c.set('agent', agent);
        c.set('skill', skill);
      }
    }
    await next();
  },
);