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.
212 lines
8.7 KiB
JavaScript
212 lines
8.7 KiB
JavaScript
import { describe, expect, test } from 'bun:test';
|
|
import { normalizeModels } from './catalog.mjs';
|
|
import { applyReasoningOptions, validateModelOptions } from './model-options.mjs';
|
|
|
|
// Synthetic data only. No provider call, no paid finance/database path.
|
|
const capabilities = {
|
|
reasoningEfforts: ['none', 'low', 'medium', 'high'],
|
|
contextWindow: 200000,
|
|
};
|
|
|
|
const expectFixedError = (run, code) => {
|
|
let caught;
|
|
try {
|
|
run();
|
|
} catch (error) {
|
|
caught = error;
|
|
}
|
|
expect(caught).toBeInstanceOf(Error);
|
|
expect(caught.code).toBe(code);
|
|
expect(caught.message).toBe(code);
|
|
};
|
|
|
|
const makePayload = () => {
|
|
const messages = [{ role: 'user', content: 'synthetic request' }];
|
|
const financeTool = { type: 'function', function: { name: 'synthetic_finance_query' } };
|
|
const databaseTool = { type: 'function', function: { name: 'synthetic_database_query' } };
|
|
const authorization = { scope: 'synthetic-authorization' };
|
|
const billing = { plan: 'synthetic-billing' };
|
|
return {
|
|
payload: {
|
|
model: 'synthetic/model',
|
|
thinking: 'high',
|
|
reasoning: 'high',
|
|
reasoning_effort: 'high',
|
|
max_tokens: 4096,
|
|
messages,
|
|
tools: [financeTool, databaseTool],
|
|
authorization,
|
|
billing,
|
|
},
|
|
messages,
|
|
financeTool,
|
|
databaseTool,
|
|
authorization,
|
|
billing,
|
|
};
|
|
};
|
|
|
|
describe('validateModelOptions', () => {
|
|
test('missing fields normalize to provider default and catalog cap', () => {
|
|
expect(() => validateModelOptions({}, capabilities)).not.toThrow();
|
|
expect(validateModelOptions({}, capabilities)).toEqual({ reasoningEffort: '', contextTokens: null });
|
|
});
|
|
|
|
test('accepts explicit null contextTokens as the catalog cap', () => {
|
|
expect(() => validateModelOptions({ reasoningEffort: '', contextTokens: null }, capabilities)).not.toThrow();
|
|
expect(validateModelOptions({ reasoningEffort: '', contextTokens: null }, capabilities))
|
|
.toEqual({ reasoningEffort: '', contextTokens: null });
|
|
});
|
|
|
|
test('accepts explicit effort and context at the exact catalog maximum', () => {
|
|
expect(validateModelOptions({ reasoningEffort: 'high', contextTokens: 200000 }, capabilities))
|
|
.toEqual({ reasoningEffort: 'high', contextTokens: 200000 });
|
|
});
|
|
|
|
test('rejects effort outside the explicitly declared list', () => {
|
|
expectFixedError(
|
|
() => validateModelOptions({ reasoningEffort: 'xhigh' }, capabilities),
|
|
'unsupported_reasoning_effort',
|
|
);
|
|
expectFixedError(
|
|
() => validateModelOptions({ reasoningEffort: 'high' }, { reasoningEfforts: [], contextWindow: null }),
|
|
'unsupported_reasoning_effort',
|
|
);
|
|
});
|
|
|
|
test('rejects context when the catalog cap is unknown', () => {
|
|
expectFixedError(
|
|
() => validateModelOptions({ contextTokens: 1000 }, { reasoningEfforts: [], contextWindow: null }),
|
|
'unsupported_context_length',
|
|
);
|
|
});
|
|
|
|
test('rejects context above the known catalog cap', () => {
|
|
expectFixedError(
|
|
() => validateModelOptions({ contextTokens: 200001 }, capabilities),
|
|
'unsupported_context_length',
|
|
);
|
|
});
|
|
|
|
test('rejects malformed option shapes with the fixed code', () => {
|
|
expectFixedError(() => validateModelOptions(null, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions(42, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions([], capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ reasoningEffort: 42 }, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ contextTokens: 0 }, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ contextTokens: -1 }, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ contextTokens: 1.5 }, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ contextTokens: Number.MAX_SAFE_INTEGER + 1 }, capabilities), 'invalid_model_options');
|
|
expectFixedError(() => validateModelOptions({ contextTokens: '1000' }, capabilities), 'invalid_model_options');
|
|
});
|
|
|
|
test('returns only the two normalized keys even with extra input keys', () => {
|
|
expect(validateModelOptions({ reasoningEffort: 'low', contextTokens: 1000, defaultEffort: 'high' }, capabilities))
|
|
.toEqual({ reasoningEffort: 'low', contextTokens: 1000 });
|
|
});
|
|
});
|
|
|
|
describe('catalog options integration', () => {
|
|
test('reasoning=false exposes no efforts and validation rejects any explicit effort', () => {
|
|
const model = normalizeModels({
|
|
data: [{
|
|
id: 'synthetic/no-reasoning',
|
|
reasoning: false,
|
|
context_window: 64000,
|
|
supported_reasoning_levels: [{ effort: 'high' }],
|
|
}],
|
|
})[0];
|
|
expect(model.options).toEqual({ reasoningEfforts: [], contextWindow: 64000 });
|
|
expectFixedError(
|
|
() => validateModelOptions({ reasoningEffort: 'high' }, model.options),
|
|
'unsupported_reasoning_effort',
|
|
);
|
|
});
|
|
|
|
test('same model primary and secondary settings stay independent', () => {
|
|
const model = normalizeModels({
|
|
data: [{
|
|
id: 'synthetic/shared-model',
|
|
context_window: 200000,
|
|
supported_reasoning_levels: [{ effort: 'low' }, { effort: 'high' }],
|
|
}],
|
|
})[0];
|
|
const primary = validateModelOptions({ reasoningEffort: 'low', contextTokens: 64000 }, model.options);
|
|
const secondary = validateModelOptions({ reasoningEffort: 'high', contextTokens: 128000 }, model.options);
|
|
|
|
expect(primary).toEqual({ reasoningEffort: 'low', contextTokens: 64000 });
|
|
expect(secondary).toEqual({ reasoningEffort: 'high', contextTokens: 128000 });
|
|
expect(primary).not.toBe(secondary);
|
|
|
|
const { payload, messages } = makePayload();
|
|
const primaryPayload = applyReasoningOptions(payload, primary);
|
|
const secondaryPayload = applyReasoningOptions(payload, secondary);
|
|
expect(primaryPayload.reasoning_effort).toBe('low');
|
|
expect(secondaryPayload.reasoning_effort).toBe('high');
|
|
expect(primaryPayload.messages).toBe(messages);
|
|
expect(secondaryPayload.messages).toBe(messages);
|
|
});
|
|
});
|
|
|
|
describe('applyReasoningOptions', () => {
|
|
test('null and undefined options are exact legacy identity, including sentinel references', () => {
|
|
const { payload, messages, financeTool, databaseTool, authorization, billing } = makePayload();
|
|
|
|
expect(applyReasoningOptions(payload, null)).toBe(payload);
|
|
expect(applyReasoningOptions(payload, undefined)).toBe(payload);
|
|
|
|
expect(payload.reasoning_effort).toBe('high');
|
|
expect(payload.thinking).toBe('high');
|
|
expect(payload.reasoning).toBe('high');
|
|
expect(payload.messages).toBe(messages);
|
|
expect(payload.tools[0]).toBe(financeTool);
|
|
expect(payload.tools[1]).toBe(databaseTool);
|
|
expect(payload.authorization).toBe(authorization);
|
|
expect(payload.billing).toBe(billing);
|
|
});
|
|
|
|
test('present config overrides Kimi menu thinking values with the plugin effort', () => {
|
|
const { payload, messages, financeTool, databaseTool, authorization, billing } = makePayload();
|
|
const normalized = validateModelOptions({ reasoningEffort: 'low', contextTokens: 64000 }, capabilities);
|
|
const applied = applyReasoningOptions(payload, normalized);
|
|
|
|
expect(applied).not.toBe(payload);
|
|
expect(applied.reasoning_effort).toBe('low');
|
|
expect('thinking' in applied).toBe(false);
|
|
expect('reasoning' in applied).toBe(false);
|
|
expect(applied.messages).toBe(messages);
|
|
expect(applied.tools).toBe(payload.tools);
|
|
expect(applied.tools[0]).toBe(financeTool);
|
|
expect(applied.tools[1]).toBe(databaseTool);
|
|
expect(applied.authorization).toBe(authorization);
|
|
expect(applied.billing).toBe(billing);
|
|
expect(applied.max_tokens).toBe(4096);
|
|
|
|
expect(payload.reasoning_effort).toBe('high');
|
|
expect(payload.thinking).toBe('high');
|
|
});
|
|
|
|
test('empty effort strips Kimi thinking controls and adds no effort', () => {
|
|
const { payload } = makePayload();
|
|
const applied = applyReasoningOptions(payload, validateModelOptions({}, capabilities));
|
|
|
|
expect(applied).not.toBe(payload);
|
|
expect('reasoning_effort' in applied).toBe(false);
|
|
expect('thinking' in applied).toBe(false);
|
|
expect('reasoning' in applied).toBe(false);
|
|
expect(applied.max_tokens).toBe(4096);
|
|
});
|
|
|
|
test('does not add max_tokens or context fields when applying explicit context', () => {
|
|
const { payload } = makePayload();
|
|
const normalized = validateModelOptions({ reasoningEffort: 'high', contextTokens: 100000 }, capabilities);
|
|
const applied = applyReasoningOptions(payload, normalized);
|
|
|
|
expect(applied.reasoning_effort).toBe('high');
|
|
expect('context' in applied).toBe(false);
|
|
expect('contextWindow' in applied).toBe(false);
|
|
expect('contextTokens' in applied).toBe(false);
|
|
expect(applied.max_tokens).toBe(4096);
|
|
});
|
|
});
|