import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { DatabaseSync } from 'node:sqlite'; import { Agent, fetch } from 'undici'; export let root = path.resolve('.qa/live-validation-ad'), origin = 'http://127.0.0.1:4318'; // Used only by the isolated local driver verifier, never by a real batch. export function configureFixture(options) { const target = path.resolve(options.root), url = new URL(options.origin); assert.equal(path.dirname(target), path.resolve('.qa')); assert.match(path.basename(target), /^ad-node-driver-[a-f0-9-]{36}$/); assert.equal(url.hostname, '127.0.0.1'); assert.notEqual(url.port, '4318'); assert.ok(url.port); root = target; origin = url.origin; } export function connectionDefinition(value) { const { health, status, activeRequests, ...definition } = value; return definition; } export function connectionInput(value) { const fields = ['id', 'revision', 'name', 'brand', 'model', 'baseUrl', 'enabled', 'requiresKey', 'streaming', 'protocol', 'messagesAuth', 'capabilityDeclarations', 'contextLimit', 'maxOutputTokens', 'concurrency', 'nativeParameters', 'outputTokenField', 'modelIdentityPolicy', 'modelIdentityAliases', 'modelIdentityAliasSource', 'parameterProfile']; return Object.fromEntries(fields.filter(key => Object.hasOwn(value, key)).map(key => [key, value[key]])); } export function compareConfig(before, after) { assert.ok(Number.isInteger(after.configRevision) && after.configRevision >= before.configRevision); assert.deepEqual({ ...after, configRevision: before.configRevision }, before); return { original: before.configRevision, current: after.configRevision, allOtherFields: 'exact' }; } export const sha = value => createHash('sha256').update(value).digest('hex'); export const save = (relative, value) => { const file = path.join(root, relative); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value, null, 2) + '\n', { flag: 'wx' }); }; export const read = relative => JSON.parse(fs.readFileSync(path.join(root, relative), 'utf8')); export function attempts() { const db = new DatabaseSync(path.join(read('current.json').host.identity.dataRoot, 'workbench.sqlite'), { readOnly: true }); try { const row = db.prepare('SELECT value FROM metadata WHERE key=?').get('modelAttempts'); return row ? JSON.parse(row.value) : []; } finally { db.close(); } } export function publicMetadata(dataRoot) { const db = new DatabaseSync(path.join(dataRoot, 'workbench.sqlite'), { readOnly: true }); try { return Object.fromEntries(['config','modelRegistry','modelAttempts','modelNetworkDiagnostics'].map(key => { const row = db.prepare('SELECT value FROM metadata WHERE key=?').get(key); return [key, row ? JSON.parse(row.value) : null]; })); } finally { db.close(); } } export async function client() { const dispatcher = new Agent({ connect: { autoSelectFamily: true, timeout: 5000 } }); let token; const api = async (route, input, timeout = 30000) => { const response = await fetch(origin + '/api' + route, { dispatcher, method: input === undefined ? 'GET' : 'POST', redirect: 'error', signal: AbortSignal.timeout(timeout), headers: { ...(token ? { 'X-Workbench-Token': token } : {}), 'Content-Type': 'application/json', Connection: 'close' }, ...(input === undefined ? {} : { body: JSON.stringify(input) }) }); const value = await response.json(); if (!response.ok) throw Object.assign(new Error(route + ': ' + (value.error ?? response.status)), { status: response.status, response: value }); return value; }; const boot = await api('/bootstrap'); token = boot.token; return { api, boot, close: () => dispatcher.close() }; } export async function captureAttempts(api, rows, relative) { const captures = []; for (const row of rows) for (const kind of ['request', 'response']) { const id = row[kind + 'ArtifactId']; if (!id) continue; let content = '', page; do { page = await api('/artifacts/' + id + '?session=' + row.sessionId + '&offset=' + content.length + '&limit=16000'); content += page.content; } while (page.truncated); assert.equal(sha(content), row[kind + 'Sha256']); const name = relative + '/' + row.id + '-' + kind + (kind === 'request' ? '.json' : row.streaming ? '.sse' : '.txt'); save(name, content); captures.push({ attemptId: row.id, kind, path: name, sha256: sha(content), bytes: Buffer.byteLength(content) }); } return captures; } export async function captureSession(api, sessionId, prefix) { const state = await api('/sessions/' + sessionId); save(prefix + '/session.json', state); save(prefix + '/export.json', await api('/sessions/' + sessionId + '/export')); const rows = attempts().filter(value => value.sessionId === sessionId); save(prefix + '/attempts.json', rows); const captures = await captureAttempts(api, rows, prefix + '/captures'); return { state, rows, captures }; }