Files
Changtian Li bc5b8332ec kimi-desktop 0.1.6: per-role reasoning effort and context input budget (#813)
kimi-desktop 0.1.6: separate reasoning effort and an optional context input budget for the primary and secondary roles.

The change (f07e7f4a..1d14a185, since the round-6 approval):
- The settings page and API save `<role>ReasoningEffort` and `<role>ContextTokens` for each role. Only efforts and context limits the catalog publishes are offered; an unknown limit allows no custom budget.
- Inference drops Kimi's top-level `thinking`, `reasoning` and `reasoning_effort`, and sends the chosen effort, or none for the provider default. Kimi's native menu aliases no longer choose the model.
- A custom budget counts a copy of the request through magpie's `/v1/messages/count_tokens`. Over the budget gives 400 `context_length_exceeded`; a failed count gives fixed 503 `context_count_unavailable`. No generation is sent in either case. Without a budget, nothing extra is sent.
- Role-scoped worker profiles (`v2:[model, role]`) tell primary from secondary even on the same model. Older roleless profiles keep their binding and use defaults.
- 1d14a185 removes blank lines only.

Run at 1d14a1859f63fd4bb512d492d1de419ad3e6c79a on mote (macOS, Bun 1.4.2, HOME/TMPDIR/XDG in the run's own folder):
- `bun test` in packages/kimi-desktop: 626 pass, 0 fail, 29 files.
- `bun scripts/check.mjs kimi-desktop`: exit 0.
- 14 hand breaks, each red in the package suite:
  - drop `applyReasoningOptions`;
  - strip thinking fields only when an effort is set;
  - drop the budget check, or make it `>=`;
  - `useFresh` false, or without its model check;
  - a legacy roleless profile treated as primary;
  - a profile name without its role;
  - no `registerRoles`;
  - no `optionsMatch` in save;
  - no `refreshLiveModelOptions`;
  - count with `redirect: 'follow'`, or with no reply size limit;
  - a 131072 fallback for an unknown catalog context.
- CI at the head: test and on-npm pass. The PR merges onto main f379bf90+ with no conflicts.

Not blocking, left for a follow-up:
- Each inference now reads connection.json (adapter.mjs:547) with no fallback to the live connection. An unreadable file turns every request into 502, even without a budget.
- The comment above `applyReasoningOptions` ("Preserve the legacy connection untouched…") no longer matches the always-strip policy.
2026-11-09 10:07:07 +08:00

111 lines
6.7 KiB
JavaScript

// magpie-community/plugins#356: the user's shared instructions went to the
// model wrapped in <magpie-kimi-shared-instructions>, so every prompt named
// the app. The tag is neutral now, and a history that still carries the old
// tag from earlier turns has it taken out, never sent twice.
import {test,expect} from 'bun:test';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {applyInstructions} from './library.mjs';
import {recoveryPeer} from './test-fixtures/control-peer.mjs';
function withRules(text, run) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'kimi-desktop-rules-'));
try { const file = path.join(dir, 'instructions.md'); fs.writeFileSync(file, text); return run(file); }
finally { fs.rmSync(dir, {recursive: true, force: true}); }
}
const history = [
{role: 'system', content: 'Kimi native system prompt'},
{role: 'system', content: '<magpie-kimi-shared-instructions>\nold rule\n</magpie-kimi-shared-instructions>'},
{role: 'user', content: 'fix the build'},
{role: 'assistant', content: 'on it'},
];
test('the prompt with shared instructions names no magpie, old tag included', () => withRules('use tabs', file => {
const out = applyInstructions({model: 'k3-agent', messages: history}, file);
expect(JSON.stringify(out)).not.toMatch(/magpie/i);
const shared = out.messages.filter(m => m.role === 'system' && m.content.includes('use tabs'));
expect(shared).toEqual([{role: 'system', content: '<shared-instructions>\nuse tabs\n</shared-instructions>'}]);
expect(out.messages.map(m => m.content)).toEqual(['Kimi native system prompt',
'<shared-instructions>\nuse tabs\n</shared-instructions>', 'fix the build', 'on it']);
expect(applyInstructions(out, file)).toEqual(out);
}));
test('with no shared instructions an old tagged turn is still taken out', () => withRules('', file => {
const out = applyInstructions({model: 'k3-agent', messages: history}, file);
expect(JSON.stringify(out)).not.toMatch(/magpie/i);
expect(out.messages).toEqual([history[0], history[2], history[3]]);
}));
for (const legacy of [false, true]) {
test(`a real bridge keeps branding out of native V2 tools and Agent history (${legacy ? 'legacy aliases' : 'neutral aliases'})`, async () => {
const f = recoveryPeer({ secondaryModel: 'provider/secondary' });
try {
await f.controller.tick();
const fixture = JSON.parse(fs.readFileSync(new URL('./test-fixtures/v2-agent-schema.json', import.meta.url), 'utf8'));
const main = f.controller.catalog.find(row => row.id === 'provider/main').alias;
const secondary = f.controller.catalog.find(row => row.id === 'provider/secondary').alias;
let text = JSON.stringify(fixture.request)
.replaceAll(fixture.expectedAliases.primary, main).replaceAll(fixture.expectedAliases.secondary, secondary);
if (legacy) text = text.replaceAll('local-model-', 'magpie-local-');
const request = JSON.parse(text);
request.model = legacy ? secondary.replace('local-model-', 'magpie-local-') : secondary;
const key = JSON.parse(fs.readFileSync(f.controller.configFile, 'utf8')).credentials.kimiCode.apiKey;
const response = await fetch(f.controller.bridgeURL + '/chat/completions', { method: 'POST',
headers: { Authorization: 'Bearer ' + key, 'Content-Type': 'application/json' }, body: JSON.stringify(request) });
expect(response.status).toBe(200);
await response.text();
expect(f.requests).toHaveLength(1);
const upstream = f.requests[0].body;
expect(upstream.model).toBe('provider/main');
expect(JSON.stringify(upstream)).not.toMatch(/magpie/i);
expect(upstream.tools[0].function.parameters.properties.model.enum).toEqual(['primary', 'secondary', 'native']);
expect(upstream.tools[0].function.description).toContain('- secondary [default]:');
expect(upstream.tools[0].function.description).not.toContain(main);
expect(upstream.tools[0].function.description).not.toContain(secondary);
expect(JSON.stringify(upstream.messages)).toContain(secondary);
const original = JSON.parse(text).tools[0].function.parameters;
const actual = upstream.tools[0].function.parameters;
expect(actual.required).toEqual(original.required);
for (const [name, schema] of Object.entries(original.properties)) {
if (name !== 'model') expect(actual.properties[name]).toEqual(schema);
}
} finally { await f.cleanup(); }
});
}
test('the real bridge preserves user filenames and non-Agent data while normalizing only old Agent model identities', async () => {
const f = recoveryPeer({ secondaryModel: 'provider/secondary' });
try {
await f.controller.tick();
const row = f.controller.catalog.find(row => row.id === 'provider/secondary');
const old = row.alias.replace('local-model-', 'magpie-local-');
const filename = `/tmp/${old}.json`;
const fixture = JSON.parse(fs.readFileSync(new URL('./test-fixtures/v2-agent-schema.json', import.meta.url), 'utf8'));
const agent = structuredClone(fixture.request.tools[0]);
agent.function.parameters.properties.prompt.description = filename;
const readTool = { type: 'function', function: { name: 'read_file', parameters: {
type: 'object', properties: { [old]: { type: 'string' }, path: { type: 'string' } },
} } };
const request = { model: 'native', tools: [agent, readTool], messages: [
{ role: 'user', content: `Read exactly ${filename}` },
{ role: 'assistant', tool_calls: [{ id: 'old-agent', type: 'function', function: { name: 'Agent',
arguments: JSON.stringify({ model: old, prompt: `Read exactly ${filename}`, description: 'Read path' }) } }] },
{ role: 'tool', tool_call_id: 'old-agent', content: JSON.stringify({ [old]: filename }) },
] };
const key = JSON.parse(fs.readFileSync(f.controller.configFile, 'utf8')).credentials.kimiCode.apiKey;
const response = await fetch(f.controller.bridgeURL + '/chat/completions', { method: 'POST',
headers: { Authorization: 'Bearer ' + key }, body: JSON.stringify(request) });
expect(response.status).toBe(200); await response.text();
const sent = f.requests.at(-1).body;
expect(sent.messages[0]).toEqual(request.messages[0]);
expect(sent.messages[2]).toEqual(request.messages[2]);
expect(sent.tools[1]).toEqual(readTool);
expect(sent.tools[0].function.parameters.properties.prompt.description).toBe(filename);
expect(sent.tools[0].function.parameters.properties.model.enum).toEqual(['primary', 'secondary', 'native']);
expect(JSON.parse(sent.messages[1].tool_calls[0].function.arguments))
.toEqual({ model: row.alias, prompt: `Read exactly ${filename}`, description: 'Read path' });
} finally { await f.cleanup(); }
});