330 lines
12 KiB
JavaScript
330 lines
12 KiB
JavaScript
'use strict';
|
|
const test = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs/promises'),
|
|
os = require('node:os'),
|
|
path = require('node:path'),
|
|
http = require('node:http');
|
|
const adapter = () => require('../../src/modules/assistant/dsh-runtime.cjs');
|
|
const config = (patch = {}) => ({
|
|
providerId: 'fixture',
|
|
preset: 'custom',
|
|
protocol: 'openai-completions',
|
|
providerName: 'Fixture',
|
|
apiBase: 'https://example.test/v1',
|
|
apiKey: 'TEST_KEY',
|
|
model: 'fixture-model',
|
|
contextWindow: 32768,
|
|
maxTokens: 1024,
|
|
timeoutSeconds: 30,
|
|
allowActions: false,
|
|
...patch,
|
|
});
|
|
test('resolved unspecified model capacity retains repair budget and clamps small contexts', async (t) => {
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-provider-capacity-')),
|
|
{ AssistantStore } = require('../../src/modules/assistant/store.cjs'),
|
|
{ ProviderRegistry } = require('../../src/modules/assistant/providers.cjs'),
|
|
store = new AssistantStore(dir);
|
|
t.after(async () => {
|
|
store.close();
|
|
await fs.rm(dir, { recursive: true, force: true });
|
|
});
|
|
store.secretSettings = () => ({
|
|
apiBase: 'https://api.deepseek.com/anthropic',
|
|
model: 'migrated-legacy',
|
|
apiKey: 'LOCAL_LEGACY_KEY',
|
|
});
|
|
store.db.exec('DELETE FROM assistant_providers;DELETE FROM assistant_provider_defaults');
|
|
const registry = new ProviderRegistry(store),
|
|
legacy = registry.resolve(),
|
|
{ profilePatch } = adapter(),
|
|
official = (repairMode) =>
|
|
JSON.parse(profilePatch({ ...legacy, timeoutSeconds: 30, repairMode }))[0].insert.find(
|
|
(p) => p.id === 'llm-deepseek',
|
|
).config;
|
|
assert.equal(legacy.maxTokens, 32768);
|
|
assert.equal(official(false).maxTokens, 8192);
|
|
assert.equal(official(true).maxTokens, 32768);
|
|
const row = registry
|
|
.save({
|
|
version: 0,
|
|
name: 'Native',
|
|
preset: 'custom',
|
|
protocol: 'openai-completions',
|
|
apiBase: 'https://example.test/v1',
|
|
apiKey: 'LOCAL_NATIVE_KEY',
|
|
enabled: true,
|
|
models: [
|
|
{ id: 'unbounded' },
|
|
{ id: 'small', contextWindow: 4096 },
|
|
{ id: 'explicit', contextWindow: 65536, maxTokens: 1024 },
|
|
],
|
|
})
|
|
.providers.find((p) => p.id !== 'deepseek');
|
|
for (const [modelId, capacity] of [
|
|
['unbounded', 32768],
|
|
['small', 4096],
|
|
['explicit', 1024],
|
|
]) {
|
|
const selected = registry.resolve({ providerId: row.id, modelId });
|
|
assert.equal(selected.maxTokens, capacity);
|
|
const native = JSON.parse(
|
|
profilePatch({ ...selected, timeoutSeconds: 30, repairMode: true }),
|
|
)[0].insert.find((p) => p.id === 'llm-selected').config.providers['order-assistant-selected'];
|
|
assert.equal(native.models[0].maxTokens, capacity);
|
|
}
|
|
});
|
|
test('selected DeepSeek official adapter honors configured caps while old config keeps legacy defaults', () => {
|
|
const { profilePatch } = adapter(),
|
|
selected = JSON.parse(
|
|
profilePatch(config({ preset: 'deepseek', protocol: 'anthropic-messages' })),
|
|
)[0].insert.find((p) => p.id === 'llm-deepseek').config;
|
|
assert.equal(selected.maxTokens, 1024);
|
|
assert.equal(selected.defaultContextWindow, 32768);
|
|
const old = JSON.parse(
|
|
profilePatch({
|
|
apiBase: 'https://api.deepseek.com/anthropic',
|
|
model: 'legacy',
|
|
timeoutSeconds: 30,
|
|
}),
|
|
)[0].insert.find((p) => p.id === 'llm-deepseek').config;
|
|
assert.equal(old.maxTokens, 8192);
|
|
assert.equal(old.defaultContextWindow, 131072);
|
|
});
|
|
test('selected profiles mount only chosen native protocol with private explicit credential', () => {
|
|
const { profilePatch, environment } = adapter();
|
|
for (const protocol of ['openai-completions', 'openai-responses', 'anthropic-messages']) {
|
|
const selected = config({ protocol }),
|
|
patch = profilePatch(selected);
|
|
assert.match(patch, /dsh-llm-pi-ai/);
|
|
assert.doesNotMatch(patch, /TEST_KEY|dsh-llm-deepseek-api-key|oauth|authorization/i);
|
|
const rows = JSON.parse(patch)[0].insert,
|
|
provider = rows.find((p) => p.name === '@deepseek-ai/dsh-llm-pi-ai').config.providers;
|
|
assert.deepEqual(Object.keys(provider), ['order-assistant-selected']);
|
|
assert.equal(provider['order-assistant-selected'].api, protocol);
|
|
assert.equal(provider['order-assistant-selected'].models.length, 1);
|
|
assert.equal(provider['order-assistant-selected'].models[0].maxTokens, 1024);
|
|
const env = environment('C:/isolated', 'TEST_KEY', selected);
|
|
assert.equal(env.ORDER_ASSISTANT_API_KEY, 'TEST_KEY');
|
|
assert.equal(env.DEEPSEEK_API_KEY, undefined);
|
|
assert.equal(env.OPENAI_API_KEY, undefined);
|
|
}
|
|
assert.match(
|
|
profilePatch(config({ preset: 'deepseek', protocol: 'anthropic-messages' })),
|
|
/dsh-llm-deepseek-api-key/,
|
|
);
|
|
});
|
|
const output = '{"reply":"local provider works","actions":[]}';
|
|
function messageSse() {
|
|
return [
|
|
{
|
|
type: 'message_start',
|
|
message: {
|
|
id: 'local',
|
|
type: 'message',
|
|
role: 'assistant',
|
|
model: 'fixture-model',
|
|
content: [],
|
|
usage: { input_tokens: 3, output_tokens: 0 },
|
|
},
|
|
},
|
|
{ type: 'content_block_start', index: 0, content_block: { type: 'text', text: output } },
|
|
{ type: 'content_block_stop', index: 0 },
|
|
{ type: 'message_delta', delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 8 } },
|
|
{ type: 'message_stop' },
|
|
]
|
|
.map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`)
|
|
.join('');
|
|
}
|
|
function chatSse() {
|
|
return (
|
|
[
|
|
{
|
|
id: 'local',
|
|
object: 'chat.completion.chunk',
|
|
created: 1,
|
|
model: 'fixture-model',
|
|
choices: [{ index: 0, delta: { role: 'assistant', content: output }, finish_reason: null }],
|
|
},
|
|
{
|
|
id: 'local',
|
|
object: 'chat.completion.chunk',
|
|
created: 1,
|
|
model: 'fixture-model',
|
|
choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
|
|
usage: { prompt_tokens: 3, completion_tokens: 8, total_tokens: 11 },
|
|
},
|
|
]
|
|
.map((e) => 'data: ' + JSON.stringify(e) + '\n\n')
|
|
.join('') + 'data: [DONE]\n\n'
|
|
);
|
|
}
|
|
function responseSse() {
|
|
const item = {
|
|
id: 'msg_local',
|
|
type: 'message',
|
|
role: 'assistant',
|
|
status: 'completed',
|
|
content: [{ type: 'output_text', text: output, annotations: [] }],
|
|
},
|
|
response = {
|
|
id: 'resp_local',
|
|
object: 'response',
|
|
created_at: 1,
|
|
model: 'fixture-model',
|
|
status: 'completed',
|
|
output: [item],
|
|
usage: { input_tokens: 3, output_tokens: 8, total_tokens: 11 },
|
|
};
|
|
return [
|
|
{ type: 'response.created', response: { ...response, status: 'in_progress', output: [] } },
|
|
{
|
|
type: 'response.output_item.added',
|
|
output_index: 0,
|
|
item: { ...item, status: 'in_progress', content: [] },
|
|
},
|
|
{
|
|
type: 'response.content_part.added',
|
|
item_id: item.id,
|
|
output_index: 0,
|
|
content_index: 0,
|
|
part: { type: 'output_text', text: '', annotations: [] },
|
|
},
|
|
{
|
|
type: 'response.output_text.delta',
|
|
item_id: item.id,
|
|
output_index: 0,
|
|
content_index: 0,
|
|
delta: output,
|
|
},
|
|
{
|
|
type: 'response.output_text.done',
|
|
item_id: item.id,
|
|
output_index: 0,
|
|
content_index: 0,
|
|
text: output,
|
|
},
|
|
{ type: 'response.output_item.done', output_index: 0, item },
|
|
{ type: 'response.completed', response },
|
|
]
|
|
.map((e, i) => `event: ${e.type}\ndata: ${JSON.stringify({ ...e, sequence_number: i })}\n\n`)
|
|
.join('');
|
|
}
|
|
for (const protocol of ['openai-completions', 'openai-responses', 'anthropic-messages'])
|
|
test(
|
|
'actual pinned DSH ' + protocol + ' sends only selected key/model without tools',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { DshRuntime, installedRuntime } = adapter(),
|
|
entry = installedRuntime();
|
|
if (!entry) return t.skip('Pinned DSH runtime is not installed');
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-provider-runtime-'));
|
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
const requests = [];
|
|
const server = http.createServer((req, res) => {
|
|
let body = '';
|
|
req.on('data', (c) => (body += c));
|
|
req.on('end', () => {
|
|
requests.push({ url: req.url, headers: req.headers, body: JSON.parse(body) });
|
|
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 }),
|
|
selected = config({
|
|
protocol,
|
|
runtimePath: entry,
|
|
apiBase:
|
|
`http://127.0.0.1:${server.address().port}` +
|
|
(protocol === 'anthropic-messages' ? '' : '/v1'),
|
|
});
|
|
const old = process.env.OPENAI_API_KEY;
|
|
process.env.OPENAI_API_KEY = 'AMBIENT_MUST_NOT_LEAK';
|
|
try {
|
|
const answer = await runtime.chat({
|
|
config: selected,
|
|
text: 'test',
|
|
history: [],
|
|
context: {},
|
|
signal: new AbortController().signal,
|
|
});
|
|
assert.equal(JSON.parse(answer).reply, 'local provider works');
|
|
} finally {
|
|
if (old === undefined) delete process.env.OPENAI_API_KEY;
|
|
else process.env.OPENAI_API_KEY = old;
|
|
}
|
|
assert.equal(requests.length, 1);
|
|
assert.equal(
|
|
new URL(requests[0].url, 'http://local.test').pathname,
|
|
protocol === 'anthropic-messages'
|
|
? '/v1/messages'
|
|
: protocol === 'openai-responses'
|
|
? '/v1/responses'
|
|
: '/v1/chat/completions',
|
|
);
|
|
assert.equal(requests[0].body.model, 'fixture-model');
|
|
assert.equal(
|
|
requests[0].body.max_tokens ??
|
|
requests[0].body.max_completion_tokens ??
|
|
requests[0].body.max_output_tokens,
|
|
1024,
|
|
);
|
|
assert.deepEqual(requests[0].body.tools ?? [], []);
|
|
assert.equal(
|
|
requests[0].headers[protocol === 'anthropic-messages' ? 'x-api-key' : 'authorization'],
|
|
protocol === 'anthropic-messages' ? 'TEST_KEY' : 'Bearer TEST_KEY',
|
|
);
|
|
assert.equal(JSON.stringify(requests).includes('AMBIENT_MUST_NOT_LEAK'), false);
|
|
assert.deepEqual(await fs.readdir(runtime.runs), []);
|
|
},
|
|
);
|
|
test(
|
|
'native selected stream cancellation cleans private profile',
|
|
{ timeout: 90000 },
|
|
async (t) => {
|
|
const { DshRuntime, installedRuntime } = adapter(),
|
|
entry = installedRuntime();
|
|
if (!entry) return t.skip('Pinned DSH runtime is not installed');
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-provider-cancel-'));
|
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
const controller = new AbortController();
|
|
let requested;
|
|
const arrived = new Promise((r) => (requested = r));
|
|
const server = http.createServer((req, res) => {
|
|
res.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
res.write('data: {"id":"local","object":"chat.completion.chunk","choices":[]}\n\n');
|
|
requested();
|
|
});
|
|
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
|
t.after(
|
|
() =>
|
|
new Promise((r) => {
|
|
server.closeAllConnections();
|
|
server.close(r);
|
|
}),
|
|
);
|
|
const runtime = new DshRuntime({ stateDir: root });
|
|
const pending = runtime.chat({
|
|
config: config({
|
|
runtimePath: entry,
|
|
apiBase: `http://127.0.0.1:${server.address().port}/v1`,
|
|
}),
|
|
text: 'test',
|
|
context: {},
|
|
signal: controller.signal,
|
|
});
|
|
await arrived;
|
|
controller.abort();
|
|
await assert.rejects(pending, (e) => e.status === 499);
|
|
assert.deepEqual(await fs.readdir(runtime.runs), []);
|
|
},
|
|
);
|