Files
Agent-Workbench/tests/model-protocols.test.mjs
T

109 lines
9.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { fixture, compatibleFixture, completion, until, assertToolPairs } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
import { completionBody, requestMetrics } from '../src/shared/contracts/model-request.mjs';
import { ConfigImporter } from '../src/core/models/config-import.mjs';
test('owned CC Switch SQLite imports the selected TOML Responses wire_api and leaves unknown clients untouched', t => {
const f = fixture(t), file = path.join(f.root, 'owned-switch.db'), db = new DatabaseSync(file);
db.exec('CREATE TABLE providers (id TEXT,app_type TEXT,name TEXT,settings_config TEXT)');
const insert = db.prepare('INSERT INTO providers VALUES(?,?,?,?)');
insert.run('responses', 'codex', 'Owned Responses', JSON.stringify({ config: 'model = "owned"\nmodel_provider = "own"\n[model_providers.own]\nbase_url = "http://127.0.0.1:12345/v1"\nwire_api = "responses"', auth: { OPENAI_API_KEY: 'owned-import-key' } }));
insert.run('unknown', 'workbuddy', 'Unknown format', '{}'); db.close();
const original = fs.readFileSync(file), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub);
const preview = importer.preview('ccswitch', file);
assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses');
assert.equal(JSON.stringify(preview).includes('owned-import-key'), false); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
assert.deepEqual(fs.readFileSync(file), original);
});
const response = (output, extra = {}) => ({ body: { id: 'resp_owned', status: 'completed', output, usage: { input_tokens: 137, output_tokens: 29, total_tokens: 166 }, ...extra } });
const message = text => ({ type: 'message', id: 'msg_owned', status: 'completed', role: 'assistant', content: [{ type: 'output_text', text }] });
const fn = (id, name = 'list_files', args = '{"path":"","depth":1}') => ({ type: 'function_call', id: 'fc_' + id, call_id: id, status: 'completed', name, arguments: args });
const connect = (hub, server, protocol = 'responses', extra = {}) => hub.upsert({ name: 'Owned ' + protocol, model: 'owned-model', protocol,
baseUrl: server.baseUrl, requiresKey: false, ...extra });
const runUntilIdle = async (runtime, session) => { runtime.start(session.id, 'Inspect files and finish.'); await until(() => !runtime.isActive(session.id), 15000); };
test('Responses executes one original tool through Runtime, carries encrypted Items, reports raw usage and seals exact wire bytes', async t => {
const f = fixture(t), reasoning = { type: 'reasoning', id: 'rs_owned', summary: [], encrypted_content: 'owned-encrypted-reasoning' };
const server = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output')
? response([message('工程读取完成')]) : response([reasoning, fn('owned_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), connection = connect(hub, server);
hub.select({ type: 'connection', id: connection.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(server.requests.length, 2); assertToolPairs(f.session.providerMessages);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
assert.deepEqual(server.requests[1].input.input.find(item => item.type === 'reasoning'), reasoning);
assert.equal(server.requests[1].input.input.find(item => item.type === 'function_call_output').call_id, 'owned_read');
const usage = f.store.core.run.get(f.session.runId).usage;
assert.equal(usage.inputTokens, 274); assert.equal(usage.outputTokens, 58);
for (const attempt of hub.view().attempts) {
const sent = server.requests.find(item => createHash('sha256').update(JSON.stringify(item.input)).digest('hex') === attempt.requestSha256);
assert.ok(sent); assert.equal(sent.url, '/v1/responses'); assert.equal(sent.input.store, false);
assert.equal(sent.input.tools[0].type, 'function'); assert.equal(sent.input.tools[0].strict, false);
assert.equal(sent.input.tools[0].function, undefined); assert.equal(attempt.protocol, 'responses');
assert.equal(attempt.usage.raw.input_tokens, 137); assert.equal(attempt.runId, f.session.runId);
const artifact = f.store.owned('artifact', attempt.requestArtifactId, f.session.id);
assert.equal(artifact.sha256, attempt.requestSha256); assert.equal(attempt.requestBytes, Buffer.byteLength(JSON.stringify(sent.input)));
}
for (const plan of [...f.store.core.context_plan.values()]) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('mixed-protocol pool keeps both actual variants and changes route on 429 without re-executing completed tools', async t => {
const f = fixture(t), limited = await compatibleFixture(t, () => ({ status: 429, body: { error: { message: 'owned rate limit' } } }));
const good = await compatibleFixture(t, (_url, body) => body.input.some(item => item.type === 'function_call_output') ? response([message('Done')]) : response([fn('mixed_read')]));
const provider = new Provider(), hub = provider.modelHub(f.store), first = connect(hub, limited, 'chat-completions'), second = connect(hub, good);
const pool = hub.pool({ name: 'Mixed protocol pool', members: [{ connectionId: first.id }, { connectionId: second.id }] });
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
await runUntilIdle(runtime, f.session);
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.events.slice(-3))); assert.equal(limited.requests.length, 1); assert.equal(good.requests.length, 2);
assert.ok(limited.requests[0].input.messages); assert.ok(good.requests[0].input.input);
assert.equal(f.session.events.filter(item => item.tool === 'list_files' && item.phase === 'completed').length, 1);
const plan = [...f.store.core.context_plan.values()][0], body = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(plan.requestArtifactId)));
assert.equal(body.kind, 'model-route-plan'); assert.equal(body.variants.length, 2);
assert.equal(plan.estimatedInputTokens, Math.max(...body.variants.map(item => requestMetrics(item.body).estimatedInputTokens)));
assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
});
test('Responses incomplete, failed, missing call identity and unsupported hosted outputs never dispatch a tool', async t => {
for (const value of [response([fn('partial')], { status: 'incomplete' }), response([fn('failed')], { status: 'failed' }),
response([{ ...fn('bad'), call_id: '' }]), response([{ type: 'computer_call', id: 'not-supported' }])]) {
await t.test('Reject ' + JSON.stringify(value).slice(0, 100), async sub => {
const f = fixture(sub), server = await compatibleFixture(sub, () => value), provider = new Provider(), hub = provider.modelHub(f.store);
const connection = connect(hub, server); hub.select({ type: 'connection', id: connection.id });
const runtime = new Runtime(f.store, provider); sub.after(() => runtime.stopAll()); await runUntilIdle(runtime, f.session);
assert.notEqual(f.session.status, 'completed'); assert.equal(server.requests.length, 1);
assert.equal(f.session.events.filter(item => item.kind === 'tool').length, 0);
assert.equal(hub.view().attempts[0].status, 'failed');
});
}
});
test('wire adapter preserves tool IDs but never passes Responses-private Items to Chat or another endpoint', () => {
const messages = [{ role: 'user', content: 'Inspect' }, { role: 'assistant', content: '', tool_calls: [{ id: 'call_own', type: 'function', function: { name: 'read_file', arguments: '{"path":"README.md"}' } }],
providerState: { protocol: 'responses', endpoint: 'https://owned.invalid/v1', model: 'm', items: [{ type: 'reasoning', encrypted_content: 'owned-private' }] } },
{ role: 'tool', tool_call_id: 'call_own', content: '{"result":"owned"}' }];
const config = { model: 'm', baseUrl: 'https://other.invalid/v1', maxOutputTokens: 512 };
const chat = completionBody(config, messages, []); assert.equal(chat.messages[1].providerState, undefined);
const native = completionBody({ ...config, protocol: 'responses' }, messages, []);
assert.equal(JSON.stringify(native).includes('owned-private'), false);
assert.equal(native.input.find(item => item.type === 'function_call').call_id, 'call_own');
assert.equal(native.input.find(item => item.type === 'function_call_output').call_id, 'call_own');
});
test('connection JSON import accepts explicit Responses protocol and continues to reject unknown protocols without reading client sessions', t => {
const f = fixture(t), hub = new Provider().modelHub(f.store), importer = new ConfigImporter(hub), file = path.join(f.root, 'owned-connections.json');
fs.writeFileSync(file, JSON.stringify({ schema: 1, connections: [{ ...connectInput(), protocol: 'responses' }, { ...connectInput(), protocol: 'unknown' }] }));
const preview = importer.preview('workbench-json', file); assert.equal(preview.entries.length, 1); assert.equal(preview.entries[0].protocol, 'responses'); assert.equal(preview.notices.length, 1);
const result = importer.apply(preview.id); assert.equal(hub.connection(result.imported[0]).protocol, 'responses');
});
function connectInput() { return { name: 'Owned import', model: 'owned-model', baseUrl: 'http://127.0.0.1:12345/v1', requiresKey: false }; }