Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

1.4 KB
Raw Blame
import { Response } from "express";
import { config } from "../../config";
import { getAgentJobAccess } from "../../lib/operational-job-access";
import { AgentSkillResponse, RequestWithAuth } from "./types";

export async function agentSkillController(
  req: RequestWithAuth<{ jobId: string }, AgentSkillResponse, any>,
  res: Response<AgentSkillResponse>,
) {
  const access = await getAgentJobAccess(req.params.jobId);

  if (
    !access ||
    access.expiresAtMs <= Date.now() ||
    access.teamId !== req.auth.team_id
  ) {
    return res.status(404).json({
      success: false,
      error: "Agent job not found",
    });
  }

  if (!config.EXTRACT_V3_BETA_URL) {
    throw new Error("Agent beta is not enabled.");
  }

  const query = req.query.refresh === "1" ? "?refresh=1" : "";
  const upstream = await fetch(
    `${config.EXTRACT_V3_BETA_URL}/internal/extracts/${req.params.jobId}/skill${query}`,
    {
      headers: {
        Authorization: `Bearer ${config.AGENT_INTEROP_SECRET}`,
      },
    },
  );

  const body = (await upstream.json()) as AgentSkillResponse;

  // 404 (skills off, or nothing stored), 409 (run still going, or it produced
  // no data) and 410 (a zero data retention run past its retention window)
  // all mean "no skill" to the caller, so the upstream status carries through
  // rather than collapsing into a 500.
  return res.status(upstream.status).json(body);
}