Files
gnome-jarvis/daemon/qvac-master.js
T
snxraven d1f339170e
Rolling release / release (push) Failing after 3m54s
CI / verify (push) Successful in 4m34s
R2
2026-09-11 15:00:17 -04:00

172 lines
6.9 KiB
JavaScript

import { createRequire } from 'node:module';
import fs from 'node:fs';
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;
let operationTail = Promise.resolve();
const auxiliaryModels = new Map();
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 sdkPackagePath() {
// Package exports intentionally block @qvac/sdk/package.json in current
// releases, so resolve the version file from the two supported install
// layouts instead of requiring an exported subpath.
const candidates = [
path.join(harnessPath, 'node_modules/@qvac/sdk/package.json'),
path.resolve(harnessPath, '../../node_modules/@qvac/sdk/package.json'),
path.resolve(new URL('../node_modules/@qvac/sdk/package.json', import.meta.url).pathname),
];
for (const candidate of candidates) if (fs.existsSync(candidate)) return candidate;
throw new Error(`Jarvis could not resolve @qvac/sdk from the harness or repository; checked ${candidates.join(', ')}`);
}
export function assertSdkVersion() {
const sdkPackage = require(sdkPackagePath());
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 async function qvacSdk() {
return Agent.engine.ensureInit();
}
export function withQvacMaster(task) {
const operation = operationTail.then(task, task);
operationTail = operation.catch(() => {});
return operation;
}
function resolveSdkAsset(sdk, name) {
if (name && typeof name !== 'string') return name;
return sdk[name] || sdk.models?.[name] || name;
}
function resolveModelConfigAssets(sdk, config) {
const copy = { ...config };
for (const key of ['vadModelSrc', 'projectionModelSrc', 'vocabModelSrc']) {
if (typeof copy[key] === 'string') copy[key] = resolveSdkAsset(sdk, copy[key]);
}
return copy;
}
/** Load ASR/TTS models in the same SDK worker and under the same master lock.
* These are auxiliary model IDs; they do not create another QVAC runtime. */
export async function loadAuxiliaryModel(name, modelConfig = {}) {
if (!name) throw new Error('auxiliary QVAC model name is required');
const existing = auxiliaryModels.get(String(name));
if (existing) return existing;
const sdk = await qvacSdk();
if (typeof sdk.loadModel !== 'function') throw new Error('QVAC SDK does not expose loadModel()');
const modelId = await withQvacMaster(() => sdk.loadModel({
modelSrc: resolveSdkAsset(sdk, name),
modelConfig: { ...resolveModelConfigAssets(sdk, modelConfig), device: 'gpu', gpu_layers: QVAC_MASTER.gpuLayers, 'mmproj-use-gpu': true },
}));
auxiliaryModels.set(String(name), modelId);
return modelId;
}
export async function unloadAuxiliaryModel(modelId) {
if (!modelId) return;
const sdk = await qvacSdk();
if (typeof sdk.unloadModel === 'function') await withQvacMaster(() => sdk.unloadModel({ modelId }));
for (const [name, id] of auxiliaryModels) if (id === modelId) auxiliaryModels.delete(name);
}
export function releaseQvac() { ownerCount = Math.max(0, ownerCount - 1); }
export async function closeQvac() {
if (ownerCount > 0) return;
for (const modelId of auxiliaryModels.values()) await unloadAuxiliaryModel(modelId).catch(() => {});
auxiliaryModels.clear();
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 withQvacMaster(() => 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', 'downloadAsset', 'cancel', 'embed', 'batchCompletion', 'ragIngest', 'ragChunk', 'ragSaveEmbeddings', 'ragSearch', 'ragReindex', 'ragDeleteEmbeddings', 'ragListWorkspaces', 'ragCloseWorkspace', 'ragDeleteWorkspace', 'translate', 'ocr', 'classify', 'diffusion', 'video', 'audioGen', 'transcribe', 'textToSpeech', 'finetune', 'bciTranscribe', 'vla', 'worldCreateScene', 'worldStep', 'modelRegistryList', 'modelRegistrySearch', 'modelRegistryGetModel']);
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 withQvacMaster(() => input === undefined ? sdk[method]() : Array.isArray(input) ? sdk[method](...input) : sdk[method](input));
}
export function qvacStatus() {
return { ...QVAC_MASTER, owners: ownerCount, loaded: Agent.engine.getLoaded(), auxiliaryModels: auxiliaryModels.size };
}
export { Agent };