|
| 1 | +export * as ServerDiscovery from "./server-discovery" |
| 2 | + |
| 3 | +import { makeRuntime } from "@/effect/run-service" |
| 4 | +import { ServerAuth } from "@/server/auth" |
| 5 | +import { AppFileSystem } from "@opencode-ai/core/filesystem" |
| 6 | +import { Global } from "@opencode-ai/core/global" |
| 7 | +import { Context, Effect, Layer, Option, Schema } from "effect" |
| 8 | +import { readFileSync, unlinkSync } from "fs" |
| 9 | +import path from "path" |
| 10 | + |
| 11 | +export const file = path.join(Global.Path.state, "server.json") |
| 12 | + |
| 13 | +const Entry = Schema.Struct({ |
| 14 | + url: Schema.String, |
| 15 | + pid: Schema.Number, |
| 16 | +}) |
| 17 | +type Entry = typeof Entry.Type |
| 18 | +const decodeEntry = Schema.decodeUnknownOption(Entry) |
| 19 | + |
| 20 | +export interface Interface { |
| 21 | + readonly write: (url: URL) => Effect.Effect<void> |
| 22 | + readonly remove: () => Effect.Effect<void> |
| 23 | + readonly find: () => Effect.Effect<string | undefined> |
| 24 | +} |
| 25 | + |
| 26 | +export class Service extends Context.Service<Service, Interface>()("@opencode/CliServerDiscovery") {} |
| 27 | + |
| 28 | +export const layer = Layer.effect( |
| 29 | + Service, |
| 30 | + Effect.gen(function* () { |
| 31 | + const fs = yield* AppFileSystem.Service |
| 32 | + |
| 33 | + const read = Effect.fn("CliServerDiscovery.read")(function* () { |
| 34 | + const entry = yield* fs.readJson(file).pipe(Effect.catch(() => Effect.succeed(undefined))) |
| 35 | + return Option.getOrUndefined(decodeEntry(entry)) |
| 36 | + }) |
| 37 | + |
| 38 | + const remove = Effect.fn("CliServerDiscovery.remove")(function* () { |
| 39 | + const entry = yield* read() |
| 40 | + if (entry?.pid !== process.pid) return |
| 41 | + yield* fs.remove(file).pipe(Effect.ignore) |
| 42 | + }) |
| 43 | + |
| 44 | + const removeStale = Effect.fn("CliServerDiscovery.removeStale")(function* (entry: Entry) { |
| 45 | + const current = yield* read() |
| 46 | + if (current?.pid !== entry.pid || current.url !== entry.url) return |
| 47 | + yield* fs.remove(file).pipe(Effect.ignore) |
| 48 | + }) |
| 49 | + |
| 50 | + return Service.of({ |
| 51 | + write: Effect.fn("CliServerDiscovery.write")(function* (url) { |
| 52 | + yield* fs.writeJson(file, { url: localURL(url).toString(), pid: process.pid }, 0o600).pipe(Effect.orDie) |
| 53 | + }), |
| 54 | + remove, |
| 55 | + find: Effect.fn("CliServerDiscovery.find")(function* () { |
| 56 | + const entry = yield* read() |
| 57 | + if (!entry) return undefined |
| 58 | + const url = yield* healthy(entry.url) |
| 59 | + if (url) return url |
| 60 | + yield* removeStale(entry) |
| 61 | + }), |
| 62 | + }) |
| 63 | + }), |
| 64 | +) |
| 65 | + |
| 66 | +export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer)) |
| 67 | + |
| 68 | +const { runPromise } = makeRuntime(Service, defaultLayer) |
| 69 | + |
| 70 | +export const find = () => runPromise((discovery) => discovery.find()) |
| 71 | + |
| 72 | +export function removeSync() { |
| 73 | + const entry = readSync() |
| 74 | + if (entry?.pid !== process.pid) return |
| 75 | + try { |
| 76 | + unlinkSync(file) |
| 77 | + } catch {} |
| 78 | +} |
| 79 | + |
| 80 | +function readSync() { |
| 81 | + try { |
| 82 | + return Option.getOrUndefined(decodeEntry(JSON.parse(readFileSync(file, "utf8")))) |
| 83 | + } catch { |
| 84 | + return undefined |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function healthy(input: string) { |
| 89 | + return Effect.tryPromise({ |
| 90 | + try: async () => { |
| 91 | + const url = new URL(input) |
| 92 | + if (url.protocol !== "http:" && url.protocol !== "https:") return undefined |
| 93 | + const response = await fetch(new URL("/global/health", url), { |
| 94 | + headers: ServerAuth.headers(), |
| 95 | + signal: AbortSignal.timeout(1000), |
| 96 | + }) |
| 97 | + if (!response.ok) return undefined |
| 98 | + const body = (await response.json()) as unknown |
| 99 | + if (typeof body === "object" && body !== null && "healthy" in body && body.healthy === true) { |
| 100 | + return url.toString() |
| 101 | + } |
| 102 | + }, |
| 103 | + catch: () => undefined, |
| 104 | + }).pipe(Effect.catch(() => Effect.succeed(undefined))) |
| 105 | +} |
| 106 | + |
| 107 | +function localURL(url: URL) { |
| 108 | + const result = new URL(url) |
| 109 | + if (result.hostname === "0.0.0.0") result.hostname = "127.0.0.1" |
| 110 | + if (result.hostname === "::") result.hostname = "::1" |
| 111 | + return result |
| 112 | +} |
0 commit comments