Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import { handleCustomerBridge } from "./bridge";import app from "../dist/server/worker.js";import { getAgentByName, routeAgentRequest } from "agents";import { loadCustomerConfig, loadCustomerSecrets,} from "../configuration/customer.ts";import { ConfigurationError } from "../configuration/validation.ts";import type { Env } from "./personal-agent";import { authorizeRuntimeRequest, privateResponse } from "./session";import { PERSONAL_PATH, conversationIdFromPath } from "./runtime-path";import { updateOnOwnerVisit } from "./update-on-visit";
export { Conversation } from "./conversation";export { Sandbox } from "./sandbox";export { PersonalAgent } from "./personal-agent";
// Octane owns SSR; this source entry owns customer runtime exports and routing.export default { async fetch(request, env, ctx) { let config; let secrets; try { config = loadCustomerConfig(env); secrets = loadCustomerSecrets(env); } catch (error) { if (!(error instanceof ConfigurationError)) throw error; console.error(error.message); return new Response( "Flarebot configuration is invalid. Check Worker configuration logs.", { status: 503, headers: { "Cache-Control": "no-store" }, }, ); } const managementInstallation = config.effectiveInstallation; const requestOrigin = new URL(request.url).origin; let effectiveInstallation = managementInstallation; let approvedDomain: string | null = null; if (requestOrigin !== managementInstallation.runtimeOrigin) { try { const personal = await getAgentByName(env.PersonalAgent, "personal"); approvedDomain = await personal.configuredDomainOrigin(); } catch { return privateResponse("Forbidden", 403); } if (approvedDomain === requestOrigin) effectiveInstallation = { ...managementInstallation, runtimeOrigin: requestOrigin, }; } const bridge = await handleCustomerBridge( request, env, effectiveInstallation, secrets.sessionSecret, ); if (bridge) return bridge; const update = await updateOnOwnerVisit( request, effectiveInstallation, secrets.sessionSecret, config.environment === "production", ); if (update) return update; const path = new URL(request.url).pathname; if (path === "/api/domain" && request.method === "GET") { const denied = await authorizeRuntimeRequest( request, secrets.sessionSecret, effectiveInstallation, ); if (denied) return denied; const personal = await getAgentByName(env.PersonalAgent, "personal"); const origin = approvedDomain ?? (await personal.configuredDomainOrigin()); const setup = new URL( "/connect", effectiveInstallation.controlPlaneOrigin, ); setup.searchParams.set( "configureDomain", effectiveInstallation.installationId, ); return Response.json( { setupUrl: setup.href, origin }, { headers: { "Cache-Control": "no-store" }, }, ); } if ( path.startsWith("/agents/") || path === "/api" || path.startsWith("/api/") ) { const denied = await authorizeRuntimeRequest( request, secrets.sessionSecret, effectiveInstallation, ); if (denied) return denied; const conversationId = conversationIdFromPath(path); if ( path !== PERSONAL_PATH && path !== `${PERSONAL_PATH}/status` && !conversationId ) return privateResponse("Not found", 404); if (conversationId && request.method !== "GET") return privateResponse("Method not allowed", 405); try { // Native readiness RPC surfaces startup failures before a WebSocket // fetch can turn them into an accepted SDK error-reporting socket. const personal = await getAgentByName(env.PersonalAgent, "personal"); if ( conversationId && !(await personal.prepareConversation(conversationId)) ) return privateResponse("Not found", 404); const response = await routeAgentRequest(request, { PersonalAgent: env.PersonalAgent, }); if (response && response.status < 500) { if (response.status === 101) return response; const privateReply = new Response(response.body, response); privateReply.headers.set("Cache-Control", "no-store"); return privateReply; } return privateResponse("Personal runtime unavailable", 503); } catch { // Initialization failures (including persisted identity mismatch) must // not expose SDK exception stacks through an HTTP response. return privateResponse("Personal runtime unavailable", 503); } } // SSR needs assets only. Never forward raw secret bindings to frontend props. return app.fetch!(request, { ASSETS: env.ASSETS }, ctx); },} satisfies ExportedHandler<Env>;