hermes-agent/apps/desktop/src/lib/runtime-readiness.ts
Brooklyn Nicholson 62fe9fd101 style(desktop,tui): fix all lint/type/formatting issues
Bring apps/desktop and ui-tui to a clean state for typecheck, eslint,
and prettier:

- Run prettier across both trees (printWidth/wrap drift; prettier is not
  CI-enforced for these JS projects, so main had accumulated drift).
- Apply eslint --fix for padding-line-between-statements and perfectionist
  import/export sorting.
- Manual fixes for non-auto-fixable rules:
  - remove unused node:net import in electron/main.cjs (uses Electron net)
  - replace inline `typeof import(...)` annotations with top-level
    `import type * as EnvModule` in two ui-tui test files
  - scoped eslint-disable no-control-regex on intentional sentinel/ANSI
    regexes (mathUnicode.ts, text.ts)
  - resolve react-hooks/exhaustive-deps per-case: correct swapped/missing
    deps, collapse redundant session.* members, and justified disables on
    settings mount-only data-load effects to preserve run-once behavior

No behavior changes; test pass/fail counts are unchanged from the main
baseline.
2026-06-26 01:04:33 -05:00

152 lines
4.2 KiB
TypeScript

export interface SetupStatusSnapshot {
provider_configured?: boolean
}
export interface RuntimeCheckSnapshot {
error?: string
ok?: boolean
}
export interface RuntimeReadinessSignals {
setup: null | SetupStatusSnapshot
setupError: null | string
runtime: null | RuntimeCheckSnapshot
runtimeError: null | string
}
export interface RuntimeReadinessOptions {
defaultReason?: string
requestedProvider?: string
unknownReady?: boolean
}
export interface RuntimeReadinessResult {
checksDisagree: boolean
ready: boolean
reason: null | string
source: 'fallback' | 'runtime_check' | 'setup_status'
}
export type RuntimeReadinessRequester = <T = unknown>(method: string, params?: Record<string, unknown>) => Promise<T>
const DEFAULT_NOT_READY_REASON = 'Add a provider credential before sending your first message.'
function toErrorMessage(error: unknown): null | string {
if (error instanceof Error) {
return error.message
}
if (typeof error === 'string') {
return error
}
if (error === null || error === undefined) {
return null
}
return String(error)
}
function normalizeMessage(value: null | string | undefined): null | string {
const next = value?.trim()
return next ? next : null
}
async function requestWithFallback<T>(
requestGateway: RuntimeReadinessRequester,
method: string,
params?: Record<string, unknown>
): Promise<{ error: null | string; value: null | T }> {
try {
return { error: null, value: await requestGateway<T>(method, params) }
} catch (error) {
return { error: toErrorMessage(error), value: null }
}
}
export async function fetchRuntimeReadinessSignals(
requestGateway: RuntimeReadinessRequester,
requestedProvider?: string
): Promise<RuntimeReadinessSignals> {
const runtimeParams = requestedProvider?.trim() ? { provider: requestedProvider.trim() } : undefined
const [setup, runtime] = await Promise.all([
requestWithFallback<SetupStatusSnapshot>(requestGateway, 'setup.status'),
requestWithFallback<RuntimeCheckSnapshot>(requestGateway, 'setup.runtime_check', runtimeParams)
])
return {
setup: setup.value,
setupError: setup.error,
runtime: runtime.value,
runtimeError: runtime.error
}
}
export function interpretRuntimeReadiness(
signals: RuntimeReadinessSignals,
options: RuntimeReadinessOptions = {}
): RuntimeReadinessResult {
const defaultReason = options.defaultReason ?? DEFAULT_NOT_READY_REASON
const unknownReady = options.unknownReady ?? false
const setupConfigured =
typeof signals.setup?.provider_configured === 'boolean' ? Boolean(signals.setup.provider_configured) : undefined
const runtimeOk = typeof signals.runtime?.ok === 'boolean' ? Boolean(signals.runtime.ok) : undefined
const runtimeFailure = normalizeMessage(signals.runtime?.error) ?? normalizeMessage(signals.runtimeError)
const setupFailure = normalizeMessage(signals.setupError)
const checksDisagree =
typeof setupConfigured === 'boolean' && typeof runtimeOk === 'boolean' && setupConfigured !== runtimeOk
if (typeof runtimeOk === 'boolean') {
if (runtimeOk) {
return {
checksDisagree,
ready: true,
reason: null,
source: 'runtime_check'
}
}
let reason = runtimeFailure ?? defaultReason
if (checksDisagree && setupConfigured) {
reason = `${reason} setup.status reports configured credentials, but runtime resolution still failed.`
}
return {
checksDisagree,
ready: false,
reason,
source: 'runtime_check'
}
}
if (typeof setupConfigured === 'boolean') {
return {
checksDisagree: false,
ready: setupConfigured,
reason: setupConfigured ? null : (runtimeFailure ?? setupFailure ?? defaultReason),
source: 'setup_status'
}
}
return {
checksDisagree: false,
ready: unknownReady,
reason: unknownReady ? null : (runtimeFailure ?? setupFailure ?? defaultReason),
source: 'fallback'
}
}
export async function evaluateRuntimeReadiness(
requestGateway: RuntimeReadinessRequester,
options: RuntimeReadinessOptions = {}
): Promise<RuntimeReadinessResult> {
const signals = await fetchRuntimeReadinessSignals(requestGateway, options.requestedProvider)
return interpretRuntimeReadiness(signals, options)
}