Files
gnome-jarvis/daemon/qvac-master.js
T
snxraven d47e9e260f
Rolling release / release (push) Failing after 1m49s
Updates
2026-09-12 19:36:06 -04:00

207 lines
9.0 KiB
JavaScript

import { voiceSettings } from './voice-settings.js';
import { profile } from './model-profiles.js';
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);
// Bare's module loader accepts the maintained Holepunch compatibility map. The
// second argument is essential for the copied CommonJS harness: it maps its
// Node builtin requests to bare-* modules without creating a second runtime.
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'), { with: { imports: 'bare-node-runtime/imports' } });
let loadPromise = null;
let ownerCount = 0;
let operationTail = Promise.resolve();
const auxiliaryModels = new Map();
const auxiliaryLoads = new Map();
const auxiliaryOwners = new Map();
export const QVAC_MASTER = Object.freeze({
configPath: process.env.QVAC_CONFIG_PATH,
model: process.env.JARVIS_QVAC_MODEL || voiceSettings().chatModel || profile(voiceSettings().modelProfile).model,
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(), { with: { imports: 'bare-node-runtime/imports' } });
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({ auxiliaryOnly = false } = {}) {
if (auxiliaryOnly) { await qvacSdk(); ownerCount += 1; return; }
if (!loadPromise) {
// Publish the promise before the first await so concurrent callers share
// resource discovery and loading. Count only successful acquisitions.
loadPromise = Promise.resolve().then(async () => {
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) throw new Error('Jarvis requires a QVAC-visible GPU backend; run npm run gpu-doctor');
const loaded = await Agent.engine.load({
model: QVAC_MASTER.model, tools: true, device: 'gpu',
gpu_layers: QVAC_MASTER.gpuLayers, mmprojUseGpu: true,
});
if (loaded.device !== 'gpu') throw new Error(`Jarvis requires GPU QVAC inference; loaded device was ${loaded.device}`);
return loaded;
}).catch((error) => { loadPromise = null; throw error; });
}
const loaded = await loadPromise;
ownerCount += 1;
return loaded;
}
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', 's3genModelSrc']) {
if (typeof copy[key] === 'string') copy[key] = resolveSdkAsset(sdk, copy[key]);
}
return copy;
}
function retainAuxiliaryModel(modelId) {
auxiliaryOwners.set(modelId, (auxiliaryOwners.get(modelId) || 0) + 1);
return modelId;
}
/** 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 = {}, modelType = undefined) {
if (!name) throw new Error('auxiliary QVAC model name is required');
const existing = auxiliaryModels.get(String(name));
if (existing) return retainAuxiliaryModel(existing);
const key = String(name);
if (auxiliaryLoads.has(key)) return retainAuxiliaryModel(await auxiliaryLoads.get(key));
const pending = Promise.resolve().then(async () => {
const sdk = await qvacSdk();
if (typeof sdk.loadModel !== 'function') throw new Error('QVAC SDK does not expose loadModel()');
const auxiliaryConfig = resolveModelConfigAssets(sdk, modelConfig);
// GPU placement belongs to the LLM model configuration. ASR and TTS have
// their own validated schemas and reject llama.cpp-only keys.
if (!modelType || modelType === 'llm') {
auxiliaryConfig.device = 'gpu';
auxiliaryConfig.gpu_layers = QVAC_MASTER.gpuLayers;
auxiliaryConfig['mmproj-use-gpu'] = true;
}
const loadOptions = {
modelSrc: resolveSdkAsset(sdk, name),
modelConfig: auxiliaryConfig,
};
if (modelType) loadOptions.modelType = modelType;
const modelId = await withQvacMaster(() => sdk.loadModel(loadOptions));
auxiliaryModels.set(String(name), modelId);
return modelId;
});
auxiliaryLoads.set(key, pending);
try { return retainAuxiliaryModel(await pending); }
finally { if (auxiliaryLoads.get(key) === pending) auxiliaryLoads.delete(key); }
}
export async function unloadAuxiliaryModel(modelId, { force = false } = {}) {
if (!modelId) return;
const owners = auxiliaryOwners.get(modelId) || 0;
if (!force && owners > 1) { auxiliaryOwners.set(modelId, owners - 1); return; }
const sdk = await qvacSdk();
if (typeof sdk.unloadModel === 'function') await withQvacMaster(() => sdk.unloadModel({ modelId }));
auxiliaryOwners.delete(modelId);
for (const [name, id] of auxiliaryModels) if (id === modelId) auxiliaryModels.delete(name);
}
export function releaseQvac() { ownerCount = Math.max(0, ownerCount - 1); }
export function qvacBusy() {
try {
const loaded = Agent.engine.getLoaded?.();
return Boolean(loaded && loaded.requestId);
} catch {
return false;
}
}
export async function closeQvac() {
if (ownerCount > 0) return;
for (const modelId of auxiliaryModels.values()) await unloadAuxiliaryModel(modelId, { force: true }).catch(() => {});
auxiliaryModels.clear();
auxiliaryOwners.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 };