Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import * as Effect from "effect/Effect";// Explicit local test network/storage seams; all ingress uses production routing.import { decryptProviderKey } from "../../worker/model-settings";import runtime from "../../worker/index";import { PersonalAgent as NativePersonalAgent } from "../../worker/personal-agent";import { Sandbox } from "../../worker/sandbox";import { getAgentByName } from "agents";import { handleCustomerBridge } from "../../worker/bridge";import { loadCustomerConfig, loadCustomerSecrets,} from "../../configuration/customer";import type { Env } from "../../worker/personal-agent";export { Sandbox };export { Conversation } from "../../worker/conversation";let exchangeMode = "normal";export class PersonalAgent extends NativePersonalAgent { async fixtureKeySurvived() { const row = this.sql<{ encrypted_key: string; }>`SELECT encrypted_key FROM flarebot_provider_credentials WHERE provider = 'anthropic'`[0]; if (!row?.encrypted_key) return false; const key = await Effect.runPromise( decryptProviderKey( row.encrypted_key, loadCustomerSecrets(this.env).sessionSecret, loadCustomerConfig(this.env).installation.installationId, ), ); return ( key.reveal() === "sk-ant-fixture-preserved-key-sentinel" && !row.encrypted_key.includes("sk-ant-fixture") ); } fixtureInspect() { return { models: this.getModelSettings(), challenges: this.sql`SELECT * FROM flarebot_login_challenges`, replays: this.sql`SELECT * FROM flarebot_health_replays`, probes: this.sql`SELECT * FROM flarebot_health_probes`, conversations: this.sql`SELECT * FROM flarebot_conversations`, }; } fixtureExpire() { this.sql`UPDATE flarebot_login_challenges SET expires_at = 0`; this.sql`UPDATE flarebot_health_replays SET expires_at = 0`; }}export default { async fetch(request, env, ctx) { const url = new URL(request.url); const installation = loadCustomerConfig(env).effectiveInstallation; request = new Request( new URL( url.pathname + url.search, request.headers.get("x-fixture-origin") ?? installation.runtimeOrigin, ), request, ); if (url.pathname === "/__bridge__/exchange-mode") { exchangeMode = ((await request.json()) as { mode: string }).mode; return Response.json({ ok: true }); } if (url.pathname.startsWith("/__bridge__/")) { const personal = (await getAgentByName( env.PersonalAgent, "personal", )) as unknown as DurableObjectStub<PersonalAgent>; if (url.pathname.endsWith("/key-survived")) return Response.json(await personal.fixtureKeySurvived()); if (url.pathname.endsWith("/expire")) { await personal.fixtureExpire(); return Response.json({ ok: true }); } if (url.pathname.endsWith("/create")) { try { await personal.createLoginChallenge(await request.json()); return Response.json({ ok: true }); } catch { return new Response("limit", { status: 429 }); } } if (url.pathname.endsWith("/claim")) { const b = (await request.json()) as { state: string; bindingHash: string; }; return Response.json( await personal.claimLoginChallenge(b.state, b.bindingHash), ); } return Response.json(await personal.fixtureInspect()); } const personal = await getAgentByName(env.PersonalAgent, "personal"); const requestOrigin = new URL(request.url).origin; const effectiveInstallation = requestOrigin !== installation.runtimeOrigin && (await personal.configuredDomainOrigin()) === requestOrigin ? { ...installation, runtimeOrigin: requestOrigin } : installation; const bridge = await Effect.runPromise( handleCustomerBridge( request, env, effectiveInstallation, loadCustomerSecrets(env).sessionSecret, async (input) => { const outgoing = new Request(input); const dest = new URL(outgoing.url); const headers = new Headers(outgoing.headers); headers.set("x-fixture-origin", dest.origin); const exchanged = await fetch( new Request( `http://127.0.0.1:${(env as Env & { TEST_CP_PORT: string }).TEST_CP_PORT}${dest.pathname}`, ({ method: outgoing.method, headers, body: outgoing.body, redirect: 'manual' as const, signal: outgoing.signal}), ), ); if (exchangeMode === "lost") { exchangeMode = "normal"; return new Response(null, { status: 503 }); } return exchanged; }, ), ); return bridge ?? runtime.fetch(request, env, ctx); },} satisfies ExportedHandler<Env>;