import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import http from 'node:http'; import { randomUUID } from 'node:crypto'; import { fixture, launchWorkbench } from '../../tests/helpers.mjs'; import { invokeProbe, inspectorClosed } from './inspector.mjs'; import { routes } from './common.mjs'; const output = path.resolve('.qa/aa-driver-check-' + randomUUID()); fs.mkdirSync(output); const cleanup = [], t = { after: action => cleanup.push(action) }, f = fixture(t), wire = [], checks = []; const server = http.createServer(async (request, response) => { const chunks = []; for await (const chunk of request) chunks.push(chunk); const body = JSON.parse(Buffer.concat(chunks)); wire.push(body); const content = body.messages.at(-1).content; const text = Array.isArray(content) ? content.map(block => block.text ?? '').join('') : content; response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify(request.url.includes('messages') ? { id: 'msg-owned', type: 'message', role: 'assistant', model: body.model, content: [{ type: 'text', text }], stop_reason: 'end_turn', usage: { input_tokens: 10, output_tokens: 20 } } : { id: 'owned', model: body.model, choices: [{ message: { role: 'assistant', content: text }, finish_reason: 'stop' }], usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 } })); }); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); let result; try { await inspectorClosed(); const host = await launchWorkbench(t, f.data); for (const route of routes) { const protocol = route.key === 'claude' ? 'anthropic-messages' : 'chat-completions'; const connection = await host.api('/model-center/connections', { name: 'AA owned ' + route.key, brand: route.key === 'deepseek' ? 'DeepSeek' : '自定义', model: 'owned-aa-' + route.key, baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1', protocol, requiresKey: true, apiKey: 'owned-aa-synthetic', contextLimit: 16000, maxOutputTokens: 2048 }); for (const mode of ['off', 'on']) { const session = await host.api('/sessions', { projectId: f.project.id }); const prompt = 'OK owned-aa-' + randomUUID(), before = wire.length; const value = await invokeProbe({ pid: host.child.pid, port: new URL(host.url).port * 1, runtimeRoot: host.bootstrap.runtimeRoot, sessionId: session.id, projectId: f.project.id, connectionId: connection.id, connectionRevision: connection.revision, credentialRevision: connection.credentialRevision, baseUrl: connection.baseUrl, model: connection.model, protocol, title: 'AA owned ' + mode, prompt, nativeParameters: route[mode] }); fs.writeFileSync(path.join(output, route.key + '-' + mode + '.json'), JSON.stringify(value.result, null, 2)); assert.equal(value.result.status, 'completed'); assert.equal(value.result.wireStarts, 1); assert.equal(value.result.actualTools, 0); assert.equal(value.result.message.content, prompt); assert.equal(wire.length, before + 1); const sent = wire.at(-1); assert.equal(sent.max_tokens, 2048); assert.ok(!sent.tools?.length); for (const [key, expected] of Object.entries(route[mode])) assert.deepEqual(sent[key], expected); const { attempts } = await host.api('/model-center'); assert.equal(attempts.find(row => row.sessionId === session.id).modelIdentity.status, 'matched'); checks.push(route.key + '-' + mode); } } assert.equal(checks.length, 6); await inspectorClosed(); result = { passed: true, checks, syntheticRequests: wire.length, realModelRequests: 0, realCatalogRequests: 0, originalFormalHostTouched: false }; } catch (error) { result = { passed: false, checks, error: error.stack, realModelRequests: 0 }; process.exitCode = 1; } finally { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); for (const action of cleanup.reverse()) await action(); fs.writeFileSync(path.join(output, 'result.json'), JSON.stringify(result, null, 2)); console.log(JSON.stringify({ ...result, output })); }