import * as Effect from "effect/Effect"; // Local immutable-artifact fixture only. Production routing, login, SQLite, // facets and scheduling remain real; only inference and bridge networking vary. import runtime from "../../worker/index"; import { PersonalAgent as NativePersonalAgent, type Env, } from "../../worker/personal-agent"; import { Conversation as FixtureConversation } from "./think-worker"; import { getAgentByName } from "agents"; import { handleCustomerBridge } from "../../worker/bridge"; import { authorizeRuntimeRequest } from "../../worker/session"; import { decryptProviderKey } from "../../worker/model-settings"; import { loadCustomerConfig, loadCustomerSecrets, } from "../../configuration/customer"; export { Sandbox } from "../../worker/sandbox"; declare const __UPGRADE_FIXTURE_RELEASE__: string; export class Conversation extends FixtureConversation { fixtureSubmit(text: string) { return this.submitMessages([ { id: crypto.randomUUID(), role: "user", parts: [{ type: "text", text }], }, ]); } async fixtureSnapshot() { return { name: this.name, messages: await this.getMessages(), submissions: await this.listSubmissions({ limit: 100 }), }; } } export class PersonalAgent extends NativePersonalAgent { async fixtureSubmit(id: string, text: string) { if (!(await this.prepareConversation(id))) throw new Error("Unknown conversation"); return (await this.subAgent(Conversation, id)).fixtureSubmit(text); } async fixtureSnapshot() { const settings = this.sql<{ anthropic_key: string | null; }>`SELECT * FROM flarebot_model_settings`; const credentials = this.sql<{ provider: string; encrypted_key: string; }>`SELECT * FROM flarebot_provider_credentials ORDER BY provider`; const encrypted = credentials.find( (row) => row.provider === "anthropic", )?.encrypted_key; const key = encrypted ? await Effect.runPromise( decryptProviderKey( encrypted, loadCustomerSecrets(this.env).sessionSecret, loadCustomerConfig(this.env).installation.installationId, ), ) : null; const facets = this.listSubAgents(Conversation).sort((a, b) => a.name.localeCompare(b.name), ); const conversations = []; for (const facet of facets) conversations.push( await (await this.subAgent(Conversation, facet.name)).fixtureSnapshot(), ); return { parentId: this.ctx.id.toString(), parentName: this.name, facets, conversations, runtime: this.sql`SELECT * FROM flarebot_runtime`, metadata: this.sql`SELECT * FROM flarebot_conversations ORDER BY id`, settings, credentials, keyDecrypts: key?.reveal() === "sk-ant-upgrade-preserved-private-sentinel", schedules: (await this.listSchedules()) .filter((s) => s.callback === "dispatchScheduledTask") .sort((a, b) => a.id.localeCompare(b.id)), tasks: this.sql`SELECT * FROM flarebot_tasks ORDER BY id`, runs: this.sql`SELECT * FROM flarebot_task_runs ORDER BY id`, }; } } export default { async fetch(request, env, ctx) { const path = new URL(request.url); const installation = loadCustomerConfig(env).effectiveInstallation; request = new Request( new URL(path.pathname + path.search, installation.runtimeOrigin), request, ); const secret = loadCustomerSecrets(env).sessionSecret; if (path.pathname.startsWith("/__upgrade__/")) { const denied = await Effect.runPromise( authorizeRuntimeRequest(request, secret, installation), ); if (denied) return denied; if (path.pathname === "/__upgrade__/identity") return Response.json({ release: __UPGRADE_FIXTURE_RELEASE__ }); const personal = await getAgentByName( env.PersonalAgent as unknown as DurableObjectNamespace, "personal", ); if (path.pathname === "/__upgrade__/snapshot") return Response.json(await personal.fixtureSnapshot()); if ( path.pathname === "/__upgrade__/submit" && request.method === "POST" ) { const body = await request.json<{ id: string; text: string }>(); return Response.json(await personal.fixtureSubmit(body.id, body.text)); } return new Response(null, { status: 404 }); } const bridge = await Effect.runPromise( handleCustomerBridge( request, env, installation, secret, async (input) => { const outgoing = new Request(input); return fetch( new Request( `http://127.0.0.1:${(env as Env & { TEST_BRIDGE_PORT: string }).TEST_BRIDGE_PORT}/exchange`, outgoing, ), ); }, ), ); return bridge ?? runtime.fetch(request, env, ctx); }, } satisfies ExportedHandler;