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