68 lines
4.3 KiB
JavaScript
68 lines
4.3 KiB
JavaScript
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';
|
|
// Each named route is a separate frozen sample. The original formal Provider
|
|
// uses its saved credential internally; this driver never accesses key values.
|
|
const experiments = {
|
|
claude: { name: 'apikey-Claude', connectionId: '9ae348a5-d0a6-42ed-8664-0da5f9effe34',
|
|
model: 'claude-opus-5.5', protocol: 'anthropic-messages', baseUrl: 'https://api.apikey.fan', revision: 1, credentialRevision: 1 },
|
|
deepseek: { name: 'DeepSeek', connectionId: '1a7a55a9-29f2-4b9f-8ea2-5a55afd7eacd',
|
|
model: 'deepseek-flash', protocol: 'chat-completions', baseUrl: 'https://api.deepseek.com/v1', revision: 1, credentialRevision: 1 },
|
|
'claude-corrected': { name: 'apikey-Claude(用户更正后)', connectionId: '9ae348a5-d0a6-42ed-8664-0da5f9effe34',
|
|
model: 'claude-opus-5-5', protocol: 'anthropic-messages', baseUrl: 'https://api.apikey.fan', revision: 2, credentialRevision: 2 },
|
|
'claude-retry': { name: 'apikey-Claude(用户要求再试)', connectionId: '9ae348a5-d0a6-42ed-8664-0da5f9effe34',
|
|
model: 'claude-opus-5-5', protocol: 'anthropic-messages', baseUrl: 'https://api.apikey.fan', revision: 3, credentialRevision: 3 },
|
|
};
|
|
export const target = process.argv[2];
|
|
export const experiment = experiments[target];
|
|
assert.ok(experiment, 'Select one pre-registered Z route.');
|
|
export const root = path.resolve('.qa/live-provider-z', target);
|
|
export const origin = 'http://127.0.0.1:4318';
|
|
export const connectionId = experiment.connectionId;
|
|
export const sha = value => createHash('sha256').update(value).digest('hex');
|
|
export const readRoot = () => JSON.parse(fs.readFileSync(path.join(root, 'current.json')));
|
|
export const dataRoot = () => readRoot().host.identity.dataRoot;
|
|
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 function meta(name) {
|
|
assert.ok(['config', 'modelRegistry', 'modelAttempts', 'modelNetworkDiagnostics'].includes(name));
|
|
const db = new DatabaseSync(path.join(dataRoot(), 'workbench.sqlite'), { readOnly: true });
|
|
try { const row = db.prepare('SELECT value FROM metadata WHERE key=?').get(name); return 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 });
|
|
return value;
|
|
};
|
|
const boot = await api('/bootstrap'); token = boot.token;
|
|
return { api, boot, close: () => dispatcher.close() };
|
|
}
|
|
export async function captureAttempts(api, attempts, relative) {
|
|
const captures = [];
|
|
for (const attempt of attempts) for (const kind of ['request', 'response']) {
|
|
const id = attempt[kind + 'ArtifactId']; if (!id) continue;
|
|
let content = '', offset = 0, page;
|
|
do {
|
|
page = await api('/artifacts/' + id + '?session=' + attempt.sessionId + '&offset=' + offset + '&limit=16000');
|
|
assert.ok(page.content.length || !page.truncated); content += page.content; offset += page.content.length;
|
|
} while (page.truncated);
|
|
assert.equal(sha(content), page.sha256); assert.equal(page.sha256, attempt[kind + 'Sha256']);
|
|
const name = `${relative}/${attempt.id}-${kind}.${kind === 'request' ? 'json' : attempt.streaming ? 'sse' : 'txt'}`;
|
|
save(name, content); captures.push({ attemptId: attempt.id, kind, artifactId: id, path: name, sha256: sha(content), bytes: Buffer.byteLength(content) });
|
|
}
|
|
return captures;
|
|
}
|