108 lines
3.7 KiB
JavaScript
108 lines
3.7 KiB
JavaScript
import { createRequire } from 'node:module';
|
|
import path from 'node:path';
|
|
|
|
const harnessPath = process.env.JARVIS_HARNESS_PATH || path.resolve(new URL('../vendor/agent-harness', import.meta.url).pathname);
|
|
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 function assertSdkVersion() {
|
|
const sdkPackage = require(path.join(harnessPath, 'node_modules/@qvac/sdk/package.json'));
|
|
if (!/^0\.19\./.test(sdkPackage.version)) {
|
|
throw new Error(`Jarvis requires vendored @qvac/sdk in the 0.19.x series; found ${sdkPackage.version}`);
|
|
}
|
|
return sdkPackage.version;
|
|
}
|
|
|
|
export async function acquireQvac() {
|
|
ownerCount += 1;
|
|
if (!loadPromise) {
|
|
assertSdkVersion();
|
|
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 async function cancelQvac() {
|
|
await Agent.engine.cancel();
|
|
}
|
|
|
|
export async function cancelQvacRequest({ requestId, modelId, kind } = {}) {
|
|
if (!requestId && !modelId) throw new Error('requestId or modelId is required');
|
|
const sdk = await Agent.engine.ensureInit();
|
|
if (typeof sdk.cancel !== 'function') throw new Error('QVAC runtime does not expose cancel()');
|
|
await sdk.cancel(requestId ? { requestId } : { modelId, kind });
|
|
}
|
|
|
|
export async function suspendQvac() {
|
|
const sdk = await Agent.engine.ensureInit();
|
|
if (typeof sdk.suspend !== 'function') throw new Error('QVAC runtime does not expose suspend()');
|
|
await sdk.suspend();
|
|
}
|
|
|
|
export async function resumeQvac() {
|
|
const sdk = await Agent.engine.ensureInit();
|
|
if (typeof sdk.resume !== 'function') throw new Error('QVAC runtime does not expose resume()');
|
|
await sdk.resume();
|
|
}
|
|
|
|
export async function qvacRuntimeState() {
|
|
const sdk = await Agent.engine.ensureInit();
|
|
if (typeof sdk.state !== 'function') return 'unknown';
|
|
return sdk.state();
|
|
}
|
|
|
|
const MASTER_CALLS = new Set(['assessModelFit', 'getSystemResources', 'state', 'heartbeat']);
|
|
export async function callQvac(method, input) {
|
|
if (!MASTER_CALLS.has(method)) throw new Error(`QVAC method is not exposed through the master: ${method}`);
|
|
const sdk = await Agent.engine.ensureInit();
|
|
if (typeof sdk[method] !== 'function') throw new Error(`QVAC SDK does not expose ${method}()`);
|
|
return input === undefined ? sdk[method]() : sdk[method](input);
|
|
}
|
|
|
|
export function qvacStatus() {
|
|
return { ...QVAC_MASTER, owners: ownerCount, loaded: Agent.engine.getLoaded() };
|
|
}
|
|
|
|
export { Agent };
|