import { createRequire } from 'node:module'; import path from 'node:path'; const harnessPath = process.env.JARVIS_HARNESS_PATH || '/home/raven/dev/agent-harness'; const require = createRequire(import.meta.url); process.env.QVAC_CONFIG_PATH ||= path.resolve(new URL('../qvac.config.json', import.meta.url).pathname); const Agent = require(path.join(harnessPath, 'index.js')); let loadPromise = null; let ownerCount = 0; export const QVAC_MASTER = Object.freeze({ configPath: process.env.QVAC_CONFIG_PATH, model: process.env.JARVIS_QVAC_MODEL || 'qwen3.5-4b', device: 'gpu', gpuLayers: 99, }); export async function acquireQvac() { ownerCount += 1; if (!loadPromise) { const resources = await Agent.engine.resources(); const gpuVisible = (resources.gpus?.length || 0) > 0 || Boolean(resources.drivers?.vulkan || resources.drivers?.cuda || resources.drivers?.opencl || resources.gpu); if (!gpuVisible) { ownerCount = Math.max(0, ownerCount - 1); throw new Error('Jarvis requires a QVAC-visible GPU backend; run npm run gpu-doctor'); } loadPromise = Agent.engine.load({ model: QVAC_MASTER.model, tools: true, device: 'gpu', gpu_layers: QVAC_MASTER.gpuLayers, mmprojUseGpu: true, }).then((loaded) => { if (loaded.device !== 'gpu') { throw new Error(`Jarvis requires GPU QVAC inference; loaded device was ${loaded.device}`); } return loaded; }).catch((error) => { loadPromise = null; ownerCount = Math.max(0, ownerCount - 1); throw error; }); } return loadPromise; } export function releaseQvac() { ownerCount = Math.max(0, ownerCount - 1); } export async function closeQvac() { if (ownerCount > 0) return; loadPromise = null; await Agent.engine.close(); } export function qvacStatus() { return { ...QVAC_MASTER, owners: ownerCount, loaded: Agent.engine.getLoaded() }; } export { Agent };