96 lines
7.1 KiB
JavaScript
96 lines
7.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import http from 'node:http';
|
|
import { compatibleFixture } from './helpers.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
|
|
const model = 'owned-output';
|
|
const truncated = protocol => protocol === 'anthropic-messages'
|
|
? { id: 'msg_owned', type: 'message', role: 'assistant', model, stop_reason: 'max_tokens', content: [{ type: 'text', text: 'Owned unfinished response' }], usage: { input_tokens: 10, output_tokens: 20 } }
|
|
: protocol === 'responses'
|
|
? { id: 'resp_owned', model, status: 'incomplete', incomplete_details: { reason: 'max_output_tokens' }, output: [], usage: { input_tokens: 10, output_tokens: 20, total_tokens: 30 } }
|
|
: { model, choices: [{ finish_reason: 'length', message: { role: 'assistant', content: 'Owned unfinished response' } }], usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 } };
|
|
|
|
test('passthrough truncation describes actual HTTP output fields and preserves upstream body, usage and one physical attempt', async t => {
|
|
const scenarios = [
|
|
{ protocol: 'anthropic-messages', value: 2048 },
|
|
{ protocol: 'anthropic-messages', upstreamModel: { id: model, maxOutputTokens: 128000 }, value: 128000 },
|
|
{ protocol: 'anthropic-messages', nativeParameters: { max_tokens: 65536 }, value: 65536 },
|
|
{ protocol: 'anthropic-messages', model: 'deepseek-flash' },
|
|
{ protocol: 'anthropic-messages', model: 'deepseek-flash', nativeParameters: { max_tokens: 65536 }, value: 65536 },
|
|
{ protocol: 'chat-completions' },
|
|
{ protocol: 'chat-completions', nativeParameters: { max_tokens: 96000 }, value: 96000 },
|
|
{ protocol: 'chat-completions', nativeParameters: { max_completion_tokens: 128000 }, field: 'max_completion_tokens', value: 128000 },
|
|
{ protocol: 'responses' },
|
|
{ protocol: 'responses', nativeParameters: { max_output_tokens: 48000 }, value: 48000 },
|
|
];
|
|
for (const scenario of scenarios) await t.test(scenario.protocol + ' ' + (scenario.value ?? 'no output field'), async child => {
|
|
const response = { ...truncated(scenario.protocol), model: scenario.model ?? model }, upstream = await compatibleFixture(child, () => ({ body: response }));
|
|
const provider = new Provider(), events = [];
|
|
child.after(() => provider.abortRequests());
|
|
const config = { executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
|
|
maxOutputTokens: 2048, model, baseUrl: upstream.baseUrl, ...scenario };
|
|
const field = scenario.field ?? (scenario.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens');
|
|
await assert.rejects(provider.complete(config, [{ role: 'user', content: 'ORIGINAL FULL INPUT' }], null, undefined, event => events.push(event), null, []), cause => {
|
|
const diagnostic = cause.diagnostic;
|
|
assert.equal(diagnostic.reason, 'output_truncated'); assert.equal(diagnostic.origin, 'upstream');
|
|
assert.equal(diagnostic.retryDecision, 'passthrough');
|
|
assert.equal(diagnostic.upstream.rawPreview, JSON.stringify(response));
|
|
assert.ok(diagnostic.wireAttemptId); assert.equal(diagnostic.upstream.httpStatus, 200);
|
|
assert.doesNotMatch(diagnostic.message, /工作台未附加输出额度/);
|
|
if (scenario.model === 'deepseek-flash') assert.doesNotMatch(diagnostic.message, /Messages 协议要求 max_tokens/);
|
|
if (scenario.value !== undefined) assert.ok(diagnostic.message.includes(field + '=' + scenario.value), diagnostic.message);
|
|
else assert.match(diagnostic.message, /实际请求未携带输出 token 上限/);
|
|
return true;
|
|
});
|
|
assert.equal(upstream.requests.length, 1); assert.ok(JSON.stringify(upstream.requests[0].input).includes('ORIGINAL FULL INPUT'));
|
|
if (scenario.value !== undefined) assert.equal(upstream.requests[0].input[field], scenario.value);
|
|
else for (const key of ['max_tokens', 'max_completion_tokens', 'max_output_tokens']) assert.equal(Object.hasOwn(upstream.requests[0].input, key), false);
|
|
const receipt = events.find(event => event.consumption)?.consumption;
|
|
assert.equal(receipt.usageStatus, 'reported'); assert.equal(receipt.usage.total_tokens, 30);
|
|
assert.equal(events.filter(event => event.consumption).length, 1);
|
|
});
|
|
});
|
|
|
|
test('streaming truncation also reports the actual output field without retrying', async t => {
|
|
const requests = [];
|
|
const server = http.createServer(async (request, response) => {
|
|
const chunks = []; for await (const bytes of request) chunks.push(bytes);
|
|
requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')));
|
|
response.writeHead(200, { 'Content-Type': 'text/event-stream' });
|
|
response.end('data: ' + JSON.stringify({ choices: [{ delta: { content: 'Owned partial' }, index: 0, finish_reason: 'length' }] })
|
|
+ '\n\ndata: ' + JSON.stringify({ choices: [], usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 } }) + '\n\ndata: [DONE]\n\n');
|
|
});
|
|
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
|
|
const provider = new Provider(), events = []; t.after(() => provider.abortRequests());
|
|
await assert.rejects(provider.complete({ executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
|
|
protocol: 'chat-completions', model, streaming: true, nativeParameters: { max_completion_tokens: 99999 },
|
|
baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1' }, [{ role: 'user', content: 'Owned' }], null, undefined,
|
|
event => events.push(event), null, []), cause => {
|
|
assert.equal(cause.diagnostic.reason, 'output_truncated');
|
|
assert.match(cause.diagnostic.message, /max_completion_tokens=99999/);
|
|
assert.equal(cause.diagnostic.retryDecision, 'passthrough'); return true;
|
|
});
|
|
assert.equal(requests.length, 1); assert.equal(requests[0].max_completion_tokens, 99999);
|
|
assert.equal(events.filter(event => event.consumption).length, 1);
|
|
});
|
|
|
|
test('Responses incomplete reasons other than max_output_tokens retain their upstream meaning', async t => {
|
|
for (const reason of ['content_filter', undefined]) await t.test(reason ?? 'missing reason', async child => {
|
|
const response = { ...truncated('responses'), incomplete_details: reason ? { reason } : {} };
|
|
const upstream = await compatibleFixture(child, () => ({ body: response }));
|
|
const provider = new Provider(), events = []; child.after(() => provider.abortRequests());
|
|
await assert.rejects(provider.complete({ executionMode: 'unrestricted', capacityMode: 'passthrough', mode: 'compatible',
|
|
protocol: 'responses', model, baseUrl: upstream.baseUrl }, [{ role: 'user', content: 'Owned input' }], null, undefined,
|
|
event => events.push(event), null, []), cause => {
|
|
assert.notEqual(cause.diagnostic.reason, 'output_truncated');
|
|
assert.equal(cause.diagnostic.kind, 'incomplete'); assert.equal(cause.diagnostic.origin, 'upstream');
|
|
assert.equal(cause.diagnostic.upstream.rawPreview, JSON.stringify(response));
|
|
assert.equal(cause.diagnostic.upstream.reason, reason ?? null); return true;
|
|
});
|
|
assert.equal(upstream.requests.length, 1);
|
|
assert.equal(events.filter(event => event.consumption).length, 1);
|
|
});
|
|
});
|