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

153 lines
4.2 KiB
JavaScript

// Read and patch only the two secondary-model scalars. Snapshots contain
// their values and original table presence, never the rest of the config.
// Bun reserializes changed documents; unchanged text is returned verbatim.
const SECONDARY_TABLE = 'secondary_model';
const EXPERIMENTAL_TABLE = 'experimental';
const MODEL_KEY = 'model';
const FLAG_KEY = 'secondary-model';
const UNSUPPORTED_MESSAGE = 'secondary_config_unsupported';
function unsupported() {
return new Error(UNSUPPORTED_MESSAGE);
}
function isTable(value) {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
function hasOwn(object, key) {
return Object.prototype.hasOwnProperty.call(object, key);
}
function parseDocument(text) {
if (typeof text !== 'string') {
throw unsupported();
}
return Bun.TOML.parse(text);
}
function readState(document) {
const hasSecondaryTable = hasOwn(document, SECONDARY_TABLE);
let model = null;
if (hasSecondaryTable) {
const table = document[SECONDARY_TABLE];
if (!isTable(table)) {
throw unsupported();
}
if (hasOwn(table, MODEL_KEY)) {
if (typeof table[MODEL_KEY] !== 'string') {
throw unsupported();
}
model = table[MODEL_KEY];
}
}
const hasExperimentalTable = hasOwn(document, EXPERIMENTAL_TABLE);
let enabled = null;
if (hasExperimentalTable) {
const table = document[EXPERIMENTAL_TABLE];
if (!isTable(table)) {
throw unsupported();
}
if (hasOwn(table, FLAG_KEY)) {
if (typeof table[FLAG_KEY] !== 'boolean') {
throw unsupported();
}
enabled = table[FLAG_KEY];
}
}
return { model, enabled, hasSecondaryTable, hasExperimentalTable };
}
function normalizeSnapshot(snapshot) {
if (snapshot === null || typeof snapshot !== 'object' || Array.isArray(snapshot)) {
throw unsupported();
}
const { model, enabled, hasSecondaryTable, hasExperimentalTable } = snapshot;
if (model !== null && typeof model !== 'string') {
throw unsupported();
}
if (enabled !== null && typeof enabled !== 'boolean') {
throw unsupported();
}
if (typeof hasSecondaryTable !== 'boolean' || typeof hasExperimentalTable !== 'boolean') {
throw unsupported();
}
return { model, enabled, hasSecondaryTable, hasExperimentalTable };
}
function applyState(document, target) {
if (target.model === null) {
const table = hasOwn(document, SECONDARY_TABLE) ? document[SECONDARY_TABLE] : null;
if (table !== null) {
delete table[MODEL_KEY];
if (!target.hasSecondaryTable && Object.keys(table).length === 0) {
delete document[SECONDARY_TABLE];
}
} else if (target.hasSecondaryTable) {
document[SECONDARY_TABLE] = {};
}
} else {
if (!hasOwn(document, SECONDARY_TABLE)) {
document[SECONDARY_TABLE] = {};
}
document[SECONDARY_TABLE][MODEL_KEY] = target.model;
}
if (target.enabled === null) {
const table = hasOwn(document, EXPERIMENTAL_TABLE) ? document[EXPERIMENTAL_TABLE] : null;
if (table !== null) {
delete table[FLAG_KEY];
if (!target.hasExperimentalTable && Object.keys(table).length === 0) {
delete document[EXPERIMENTAL_TABLE];
}
} else if (target.hasExperimentalTable) {
document[EXPERIMENTAL_TABLE] = {};
}
} else {
if (!hasOwn(document, EXPERIMENTAL_TABLE)) {
document[EXPERIMENTAL_TABLE] = {};
}
document[EXPERIMENTAL_TABLE][FLAG_KEY] = target.enabled;
}
}
export function inspectSecondary(text) {
try {
return readState(parseDocument(text));
} catch {
throw unsupported();
}
}
export function patchSecondary(text, snapshot) {
try {
const document = parseDocument(text);
const target = normalizeSnapshot(snapshot);
const current = readState(document);
if (
current.model === target.model &&
current.enabled === target.enabled &&
current.hasSecondaryTable === target.hasSecondaryTable &&
current.hasExperimentalTable === target.hasExperimentalTable
) {
return text;
}
applyState(document, target);
return Bun.TOML.stringify(document);
} catch {
throw unsupported();
}
}