Skip to content

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

1.8 KB
Raw Blame
import { Response } from "express";
import { config } from "../../config";
import { listActiveBrowserSessionsForRequest } from "../../lib/browser-sessions";
import { getAgentJobAccess } from "../../lib/operational-job-access";
import { AgentTraceResponse, RequestWithAuth } from "./types";

const AGENT_BROWSER_VIEWPORT = { width: 1280, height: 720 } as const;

export async function agentTraceController(
  req: RequestWithAuth<{ jobId: string }, AgentTraceResponse, any>,
  res: Response<AgentTraceResponse>,
) {
  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 includeLiveView = req.query.liveView === "true";

  const [upstream, activeBrowserSessions] = await Promise.all([
    fetch(
      `${config.EXTRACT_V3_BETA_URL}/internal/extracts/${req.params.jobId}/trace`,
      {
        headers: {
          Authorization: `Bearer ${config.AGENT_INTEROP_SECRET}`,
        },
      },
    ),
    includeLiveView
      ? listActiveBrowserSessionsForRequest(req.auth.team_id, req.params.jobId)
      : Promise.resolve([]),
  ]);
  const body = (await upstream.json()) as AgentTraceResponse;

  if (!upstream.ok || body.success !== true) {
    return res.status(upstream.status).json(body);
  }

  return res.status(upstream.status).json({
    ...body,
    ...(includeLiveView
      ? {
          activeBrowserSessions: activeBrowserSessions.map(session => ({
            id: session.id,
            liveViewUrl: session.cdp_path,
            viewport: AGENT_BROWSER_VIEWPORT,
          })),
        }
      : {}),
  });
}