(options: MakeSelfHostAppOptions = {})
| 49 | } |
| 50 | |
| 51 | export const makeSelfHostApp = async (options: MakeSelfHostAppOptions = {}) => { |
| 52 | const config = loadConfig(); |
| 53 | |
| 54 | // ---- eager async boot: the shared libSQL handle ----------------------- |
| 55 | const dbHandle = await createSelfHostDb({ |
| 56 | path: options.dbPath ?? config.dbPath, |
| 57 | namespace: SELF_HOST_NAMESPACE, |
| 58 | version: SELF_HOST_SCHEMA_VERSION, |
| 59 | }); |
| 60 | |
| 61 | // Boot-time data migrations: each registry entry runs once and is stamped |
| 62 | // in the `data_migration` ledger; stamped entries are skipped without |
| 63 | // touching the data. |
| 64 | await Effect.runPromise(runSqliteDataMigrations(dbHandle.client, selfHostDataMigrations)); |
| 65 | |
| 66 | // ---- auth providers --------------------------------------------------- |
| 67 | // Better Auth: cookie/bearer/api-key identity + /api/auth handler + account |
| 68 | // API + MCP OAuth seam, all over the shared libSQL handle. |
| 69 | const { identityLayer, authHandler, betterAuth } = await resolveAuthProviders(dbHandle); |
| 70 | |
| 71 | // ---- the in-process MCP serving seams (+ shutdown hook) ---------------- |
| 72 | // Pass the pinned public origin so browser-approval URLs are reachable behind |
| 73 | // a reverse proxy (not the internal 127.0.0.1 bind from the request URL). |
| 74 | const mcp = makeSelfHostMcpSeams(dbHandle, betterAuth, config.webBaseUrl); |
| 75 | |
| 76 | // CLI device-login discovery (`executor login`). Points the CLI at Better |
| 77 | // Auth's device endpoints; `requestFormat: "json"` because those endpoints |
| 78 | // only accept JSON (unlike WorkOS's form-encoded ones). The issued token is a |
| 79 | // Better Auth session that `bearer()` accepts on the /api/* plane. |
| 80 | const cliLoginHandler = HttpEffect.fromWebHandler( |
| 81 | async () => |
| 82 | new Response( |
| 83 | JSON.stringify({ |
| 84 | provider: "better-auth", |
| 85 | deviceAuthorizationEndpoint: `${config.webBaseUrl}/api/auth/device/code`, |
| 86 | tokenEndpoint: `${config.webBaseUrl}/api/auth/device/token`, |
| 87 | clientId: "executor-cli", |
| 88 | requestFormat: "json", |
| 89 | }), |
| 90 | { headers: { "content-type": "application/json" } }, |
| 91 | ), |
| 92 | ); |
| 93 | |
| 94 | const { appLayer, toWebHandler } = ExecutorApp.make({ |
| 95 | plugins: selfHostPlugins, |
| 96 | providers: { |
| 97 | identity: identityLayer, |
| 98 | account: selfHostAccountMiddleware(betterAuth), |
| 99 | db: SelfHostDbProvider, |
| 100 | engine: { codeExecutor: SelfHostCodeExecutorProvider }, // decorator defaults to no-op (no metering) |
| 101 | mcp: { auth: mcp.auth, sessions: mcp.sessions, reporter: mcp.reporter }, |
| 102 | plugins: { provider: SelfHostPluginsProvider, config: SelfHostHostConfig }, |
| 103 | errorCapture: ErrorCaptureLive, |
| 104 | }, |
| 105 | extensions: { |
| 106 | routes: [ |
| 107 | // CLI device-login discovery, must precede the /api/auth/* wildcard |
| 108 | // below (Better Auth would otherwise 404 it). The verification page it |
no test coverage detected