|
| 1 | +import { Effect } from "effect"; |
| 2 | + |
| 3 | +export interface LocalBackendProbeResult { |
| 4 | + readonly reachable: boolean; |
| 5 | + readonly modelCount?: number; |
| 6 | + readonly error?: string; |
| 7 | +} |
| 8 | + |
| 9 | +export interface LocalBackendProbes { |
| 10 | + readonly ollama: LocalBackendProbeResult; |
| 11 | + readonly lmstudio: LocalBackendProbeResult; |
| 12 | +} |
| 13 | + |
| 14 | +const DEFAULT_PROBE_TIMEOUT_MS = 1_500; |
| 15 | + |
| 16 | +const OLLAMA_TAGS_URL = "http://localhost:11434/api/tags"; |
| 17 | +const LM_STUDIO_MODELS_URL = "http://localhost:1234/v1/models"; |
| 18 | + |
| 19 | +function isSuppressedByEnv(): boolean { |
| 20 | + const env = process.env; |
| 21 | + return env.OKCODE_DISABLE_LOCAL_BACKEND_PROBES === "1" || env.VITEST === "true"; |
| 22 | +} |
| 23 | + |
| 24 | +function toErrorMessage(cause: unknown, fallback: string): string { |
| 25 | + if (cause instanceof Error && cause.message.trim().length > 0) { |
| 26 | + if (cause.name === "AbortError") { |
| 27 | + return "timeout"; |
| 28 | + } |
| 29 | + return cause.message; |
| 30 | + } |
| 31 | + if (typeof cause === "string" && cause.trim().length > 0) { |
| 32 | + return cause; |
| 33 | + } |
| 34 | + return fallback; |
| 35 | +} |
| 36 | + |
| 37 | +function readModelCount(data: unknown, key: "models" | "data"): number | undefined { |
| 38 | + if (!data || typeof data !== "object") { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + const value = (data as Record<string, unknown>)[key]; |
| 42 | + if (Array.isArray(value)) { |
| 43 | + return value.length; |
| 44 | + } |
| 45 | + return undefined; |
| 46 | +} |
| 47 | + |
| 48 | +async function probeHttp(input: { |
| 49 | + readonly url: string; |
| 50 | + readonly modelsKey: "models" | "data"; |
| 51 | + readonly timeoutMs: number; |
| 52 | +}): Promise<LocalBackendProbeResult> { |
| 53 | + const controller = new AbortController(); |
| 54 | + const timeout = setTimeout(() => controller.abort(), input.timeoutMs); |
| 55 | + try { |
| 56 | + const response = await fetch(input.url, { |
| 57 | + method: "GET", |
| 58 | + signal: controller.signal, |
| 59 | + headers: { accept: "application/json" }, |
| 60 | + }); |
| 61 | + if (!response.ok) { |
| 62 | + return { |
| 63 | + reachable: false, |
| 64 | + error: `HTTP ${response.status}`, |
| 65 | + }; |
| 66 | + } |
| 67 | + try { |
| 68 | + const body: unknown = await response.json(); |
| 69 | + const modelCount = readModelCount(body, input.modelsKey); |
| 70 | + return modelCount !== undefined ? { reachable: true, modelCount } : { reachable: true }; |
| 71 | + } catch (cause) { |
| 72 | + // Server responded 2xx but body wasn't JSON — still counts as reachable. |
| 73 | + return { |
| 74 | + reachable: true, |
| 75 | + error: toErrorMessage(cause, "Non-JSON response"), |
| 76 | + }; |
| 77 | + } |
| 78 | + } catch (cause) { |
| 79 | + return { |
| 80 | + reachable: false, |
| 81 | + error: toErrorMessage(cause, "Network error"), |
| 82 | + }; |
| 83 | + } finally { |
| 84 | + clearTimeout(timeout); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export interface ProbeLocalBackendOptions { |
| 89 | + readonly timeoutMs?: number | undefined; |
| 90 | +} |
| 91 | + |
| 92 | +const UNREACHABLE_STUB: LocalBackendProbeResult = { reachable: false }; |
| 93 | + |
| 94 | +export const probeOllama = ( |
| 95 | + options: ProbeLocalBackendOptions = {}, |
| 96 | +): Effect.Effect<LocalBackendProbeResult> => { |
| 97 | + if (isSuppressedByEnv()) { |
| 98 | + return Effect.succeed(UNREACHABLE_STUB); |
| 99 | + } |
| 100 | + return Effect.promise(() => |
| 101 | + probeHttp({ |
| 102 | + url: OLLAMA_TAGS_URL, |
| 103 | + modelsKey: "models", |
| 104 | + timeoutMs: options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS, |
| 105 | + }), |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +export const probeLmStudio = ( |
| 110 | + options: ProbeLocalBackendOptions = {}, |
| 111 | +): Effect.Effect<LocalBackendProbeResult> => { |
| 112 | + if (isSuppressedByEnv()) { |
| 113 | + return Effect.succeed(UNREACHABLE_STUB); |
| 114 | + } |
| 115 | + return Effect.promise(() => |
| 116 | + probeHttp({ |
| 117 | + url: LM_STUDIO_MODELS_URL, |
| 118 | + modelsKey: "data", |
| 119 | + timeoutMs: options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS, |
| 120 | + }), |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export const probeCodexLocalBackends = ( |
| 125 | + options: ProbeLocalBackendOptions = {}, |
| 126 | +): Effect.Effect<LocalBackendProbes> => |
| 127 | + Effect.all( |
| 128 | + { |
| 129 | + ollama: probeOllama(options), |
| 130 | + lmstudio: probeLmStudio(options), |
| 131 | + }, |
| 132 | + { concurrency: "unbounded" }, |
| 133 | + ); |
0 commit comments