113 lines
4.0 KiB
JavaScript
113 lines
4.0 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
http = require('node:http'),
|
|
fs = require('node:fs/promises'),
|
|
os = require('node:os'),
|
|
path = require('node:path'),
|
|
{ createCanvas } = require('@napi-rs/canvas');
|
|
const { chatSse, messageSse, responseSse } = require('../fixtures/provider-streams.cjs');
|
|
for (const variant of [
|
|
{ protocol: 'openai-completions' },
|
|
{ protocol: 'openai-responses' },
|
|
{ protocol: 'anthropic-messages' },
|
|
{ protocol: 'anthropic-messages', preset: 'deepseek' },
|
|
])
|
|
test(
|
|
'official DSH sends actual image blocks through ' +
|
|
(variant.preset || 'native') +
|
|
' ' +
|
|
variant.protocol +
|
|
' without native tools',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const protocol = variant.protocol;
|
|
require('../fixtures/isolated-vault-key.cjs')(t);
|
|
const {
|
|
DshRuntime,
|
|
installedRuntime,
|
|
} = require('../../src/modules/assistant/dsh-runtime.cjs'),
|
|
entry = installedRuntime();
|
|
if (!entry) return t.skip('Pinned runtime absent');
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'vision-runtime-'));
|
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
const bytes = createCanvas(10, 10).toBuffer('image/png'),
|
|
images = [{ name: 'picture.png', mediaType: 'image/png', data: bytes.toString('base64') }],
|
|
requests = [];
|
|
const server = http.createServer((req, res) => {
|
|
let text = '';
|
|
req.on('data', (c) => (text += c));
|
|
req.on('end', () => {
|
|
requests.push({ url: req.url, headers: req.headers, body: JSON.parse(text) });
|
|
res.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
res.end(
|
|
protocol === 'anthropic-messages'
|
|
? messageSse()
|
|
: protocol === 'openai-responses'
|
|
? responseSse()
|
|
: chatSse(),
|
|
);
|
|
});
|
|
});
|
|
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
|
t.after(() => new Promise((r) => server.close(r)));
|
|
const runtime = new DshRuntime({ stateDir: root }),
|
|
config = {
|
|
runtimePath: entry,
|
|
preset: variant.preset || 'custom',
|
|
providerName: 'Vision fixture',
|
|
protocol,
|
|
apiBase:
|
|
'http://127.0.0.1:' +
|
|
server.address().port +
|
|
(variant.preset === 'deepseek'
|
|
? '/anthropic'
|
|
: protocol === 'anthropic-messages'
|
|
? ''
|
|
: '/v1'),
|
|
apiKey: 'REAL_VISION_KEY',
|
|
model: 'fixture-model',
|
|
inputModalities: ['text', 'image'],
|
|
timeoutSeconds: 30,
|
|
allowActions: false,
|
|
};
|
|
const answer = await runtime.chat({
|
|
config,
|
|
images,
|
|
text: '分析图片',
|
|
context: { attachments: [{ name: 'picture.png', kind: 'image' }] },
|
|
history: [],
|
|
signal: new AbortController().signal,
|
|
});
|
|
assert.equal(JSON.parse(answer).reply, 'local provider works');
|
|
assert.equal(requests.length, 1);
|
|
const body = requests[0].body,
|
|
blocks = (protocol === 'openai-responses' ? body.input : body.messages)
|
|
.filter((m) => m.role === 'user')
|
|
.at(-1).content;
|
|
assert.ok(
|
|
blocks.some(
|
|
(b) =>
|
|
b.type ===
|
|
(protocol === 'anthropic-messages'
|
|
? 'image'
|
|
: protocol === 'openai-responses'
|
|
? 'input_image'
|
|
: 'image_url'),
|
|
),
|
|
);
|
|
assert.equal(
|
|
blocks
|
|
.filter((b) => b.type === 'text' || b.type === 'input_text')
|
|
.some((b) => b.text.includes(images[0].data)),
|
|
false,
|
|
);
|
|
assert.deepEqual(body.tools || [], []);
|
|
assert.equal(
|
|
requests[0].headers[protocol === 'anthropic-messages' ? 'x-api-key' : 'authorization'],
|
|
protocol === 'anthropic-messages' ? 'REAL_VISION_KEY' : 'Bearer REAL_VISION_KEY',
|
|
);
|
|
assert.deepEqual(await fs.readdir(runtime.runs), []);
|
|
},
|
|
);
|