import { Agents24 } from "@agents24/node"; import { createAgents24BffHandler, type Agents24BffHandler } from "@agents24/node/bff"; import { getServerEnvironment } from "./env"; import { resolveAgents24Principal, revokeAgents24Principal } from "./principal"; let handler: Agents24BffHandler | undefined; // Initialize on the first runtime request, never while the host collects routes during a build. export function handleAgents24(request: Request): Promise { if (!handler) { const env = getServerEnvironment(); handler = createAgents24BffHandler({ client: new Agents24({ baseUrl: env.AGENTS24_BASE_URL, apiKey: env.AGENTS24_API_KEY }), agents: env.AGENTS24_AGENTS, integrationId: env.AGENTS24_INTEGRATION_ID, integrationName: env.AGENTS24_INTEGRATION_NAME, runtimeTarget: (env.AGENTS24_RUNTIME_TARGET || (process.env.NODE_ENV === "production" ? "published" : "draft")) as "draft" | "published", resolvePrincipal: resolveAgents24Principal, revokePrincipal: revokeAgents24Principal, }); } return handler(request); }