chore: refactoring and tests, splitting up files (#12495)
This commit is contained in:
29
packages/app/src/utils/server-health.ts
Normal file
29
packages/app/src/utils/server-health.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
|
||||
|
||||
export type ServerHealth = { healthy: boolean; version?: string }
|
||||
|
||||
interface CheckServerHealthOptions {
|
||||
timeoutMs?: number
|
||||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
function timeoutSignal(timeoutMs: number) {
|
||||
return (AbortSignal as unknown as { timeout?: (ms: number) => AbortSignal }).timeout?.(timeoutMs)
|
||||
}
|
||||
|
||||
export async function checkServerHealth(
|
||||
url: string,
|
||||
fetch: typeof globalThis.fetch,
|
||||
opts?: CheckServerHealthOptions,
|
||||
): Promise<ServerHealth> {
|
||||
const signal = opts?.signal ?? timeoutSignal(opts?.timeoutMs ?? 3000)
|
||||
const sdk = createOpencodeClient({
|
||||
baseUrl: url,
|
||||
fetch,
|
||||
signal,
|
||||
})
|
||||
return sdk.global
|
||||
.health()
|
||||
.then((x) => ({ healthy: x.data?.healthy === true, version: x.data?.version }))
|
||||
.catch(() => ({ healthy: false }))
|
||||
}
|
||||
Reference in New Issue
Block a user