import { type AgentRuntime, MACHINE_STALE_TIMEOUT_MS } from "@vtit-agent-coding/shared"; import { type D1, parseDbTimestamp } from "./db"; import { listMachines, type MachineRecord, type MachineWithAgentsRecord } from "./machineRepo"; export async function legacyRuntimeAvailable(db: D1, ownerId: string, runtime: AgentRuntime): Promise { const machines = await listMachines(db, ownerId); return legacyRuntimeAvailableOnMachines(machines, runtime); } export function legacyRuntimeAvailableOnMachines(machines: Array, runtime: AgentRuntime): boolean { return machines.some( (machine) => machine.hosting === "local" && legacyMachineHeartbeatFresh(machine) && machine.runtimes.some((entry) => entry.name === runtime && entry.status === "ready"), ); } export function legacyMachineHeartbeatFresh(machine: MachineRecord | MachineWithAgentsRecord, now = Date.now()): boolean { if (machine.status !== "online" || !machine.last_heartbeat_at) return false; const heartbeatAt = parseDbTimestamp(machine.last_heartbeat_at); return Number.isFinite(heartbeatAt) && heartbeatAt >= now - MACHINE_STALE_TIMEOUT_MS; }