refactor(server): inject database options

This commit is contained in:
Dax Raad 2026-07-20 13:01:01 -04:00
commit f971aa0719
14 changed files with 93 additions and 52 deletions

View file

@ -1,6 +1,7 @@
export * as ServerProcess from "./process"
import { NodeHttpServer, NodeHttpServerRequest } from "@effect/platform-node"
import { Database } from "@opencode-ai/core/database/database"
import { InstallationVersion } from "@opencode-ai/core/installation/version"
import { SessionRestart } from "@opencode-ai/core/session/execution/restart"
import { ServiceStatus } from "@opencode-ai/protocol/groups/health"
@ -20,6 +21,7 @@ export type Options<E = never, R = never> = {
readonly port: Option.Option<number>
readonly password: string
readonly instanceID: string
readonly database?: Database.Options
readonly service?: {
readonly onListen: (
address: HttpServer.Address,
@ -66,11 +68,15 @@ export const start = Effect.fn("ServerProcess.start")(function* <E, R>(options:
const boot = Effect.gen(function* () {
const context = yield* Layer.buildWithScope(
createRoutes(options.password, () => {
const address = bound.server.address()
if (address === null || typeof address === "string") return []
const host = address.family === "IPv6" ? `[${address.address}]` : address.address
return ServerInfo.connectionURLs(`http://${host}:${address.port}`, options.hostname)
createRoutes({
password: options.password,
serviceURLs: () => {
const address = bound.server.address()
if (address === null || typeof address === "string") return []
const host = address.family === "IPv6" ? `[${address.address}]` : address.address
return ServerInfo.connectionURLs(`http://${host}:${address.port}`, options.hostname)
},
database: options.database,
}).pipe(Layer.provide(NodeHttpServer.layerHttpServices)),
applicationScope,
)

View file

@ -51,25 +51,32 @@ const applicationServices = LayerNode.group([
SessionRestart.node,
])
export function createRoutes(password?: string, serviceURLs: () => ReadonlyArray<string> = () => []) {
export interface Options {
readonly password?: string
readonly serviceURLs?: () => ReadonlyArray<string>
readonly database?: Database.Options
}
export function createRoutes(options: Options = {}) {
return makeRoutes(
password
? ServerAuth.Config.configLayer({ username: "opencode", password: Option.some(password) })
options.password
? ServerAuth.Config.configLayer({ username: "opencode", password: Option.some(options.password) })
: ServerAuth.Config.layer,
serviceURLs,
options,
)
}
export function createEmbeddedRoutes() {
return makeRoutes(ServerAuth.Config.configLayer({ username: "opencode", password: Option.none() }))
export function createEmbeddedRoutes(options: Pick<Options, "database"> = {}) {
return makeRoutes(ServerAuth.Config.configLayer({ username: "opencode", password: Option.none() }), options)
}
function makeRoutes<AuthError, AuthServices>(
auth: Layer.Layer<ServerAuth.Config, AuthError, AuthServices>,
serviceURLs: () => ReadonlyArray<string> = () => [],
options: Pick<Options, "database" | "serviceURLs">,
) {
const pluginRuntimeCell = PluginRuntime.makeCell()
const replacements: LayerNode.Replacements = [
[Database.node, Database.layer(options.database)],
[PluginRuntime.node, PluginRuntime.layerWithCell(pluginRuntimeCell)],
[PluginRuntime.providerNode, PluginRuntime.providerNodeWithCell(pluginRuntimeCell)],
]
@ -88,7 +95,7 @@ function makeRoutes<AuthError, AuthServices>(
const services = Layer.succeedContext(context)
const requestServices = Layer.merge(
Layer.succeedContext(Context.pick(PermissionSaved.Service, Project.Service, WellKnown.Service)(context)),
ServerInfo.layer(serviceURLs),
ServerInfo.layer(options.serviceURLs ?? (() => [])),
)
return HttpApiBuilder.layer(Api, { openapiPath: "/openapi.json" }).pipe(
Layer.provide(handlers.pipe(Layer.provide(services))),