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

95 lines
4.3 KiB
JavaScript

// Only the pool's alias descriptions, default alias and table presence belong
// in a snapshot. Model definitions, providers and credentials stay in the TOML.
// Bun normalizes changed documents; an effective no-op preserves the text.
const SECONDARY_TABLE = 'secondary_model';
const POOL_KEY = 'models';
const DEFAULT_KEY = 'default_model';
const SNAPSHOT_KEYS = ['models', 'defaultModel', 'hasSecondaryTable', 'hasModelsTable'];
const unsupported = () => Error('secondary_config_unsupported');
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
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 stringMap(value) {
if (!isTable(value)) throw unsupported();
const keys = Reflect.ownKeys(value);
if (keys.some(key => typeof key !== 'string' || typeof value[key] !== 'string')) throw unsupported();
return Object.fromEntries(keys.map(key => [key, value[key]]));
}
function parseDocument(text) {
if (typeof text !== 'string') throw unsupported();
return Bun.TOML.parse(text);
}
function readState(document) {
const hasSecondaryTable = hasOwn(document, SECONDARY_TABLE);
const table = hasSecondaryTable ? document[SECONDARY_TABLE] : {};
if (!isTable(table)) throw unsupported();
const hasModelsTable = hasOwn(table, POOL_KEY);
const models = hasModelsTable ? stringMap(table[POOL_KEY]) : null;
const defaultModel = hasOwn(table, DEFAULT_KEY) ? table[DEFAULT_KEY] : null;
if (defaultModel !== null && typeof defaultModel !== 'string') throw unsupported();
return { models, defaultModel, hasSecondaryTable, hasModelsTable };
}
function normalizeSnapshot(snapshot) {
if (!isTable(snapshot) || Reflect.ownKeys(snapshot).length !== SNAPSHOT_KEYS.length
|| SNAPSHOT_KEYS.some(key => !hasOwn(snapshot, key))) throw unsupported();
const { models, defaultModel, hasSecondaryTable, hasModelsTable } = snapshot;
if (models !== null && !isTable(models)) throw unsupported();
if (defaultModel !== null && typeof defaultModel !== 'string') throw unsupported();
if (typeof hasSecondaryTable !== 'boolean' || typeof hasModelsTable !== 'boolean') throw unsupported();
// An existing empty pool is {}, while null always means the pool is absent.
if (models === null && hasModelsTable) throw unsupported();
return { models: models === null ? null : stringMap(models), defaultModel,
hasSecondaryTable, hasModelsTable };
}
function sameState(left, right) {
if (left.defaultModel !== right.defaultModel || left.hasSecondaryTable !== right.hasSecondaryTable
|| left.hasModelsTable !== right.hasModelsTable) return false;
if (left.models === null || right.models === null) return left.models === right.models;
const keys = Object.keys(left.models);
return keys.length === Object.keys(right.models).length
&& keys.every(key => hasOwn(right.models, key) && left.models[key] === right.models[key]);
}
function applyState(document, target) {
let table = hasOwn(document, SECONDARY_TABLE) ? document[SECONDARY_TABLE] : null;
if (table === null) {
if (!target.hasSecondaryTable && target.models === null && target.defaultModel === null) return;
table = document[SECONDARY_TABLE] = {};
}
if (target.models === null) delete table[POOL_KEY];
else table[POOL_KEY] = stringMap(target.models);
if (target.defaultModel === null) delete table[DEFAULT_KEY];
else table[DEFAULT_KEY] = target.defaultModel;
if (!target.hasSecondaryTable && Object.keys(table).length === 0) delete document[SECONDARY_TABLE];
}
export function inspectModelPool(text) {
try {
return readState(parseDocument(text));
} catch { throw unsupported(); }
}
export function patchModelPool(text, snapshot) {
try {
const document = parseDocument(text);
const target = normalizeSnapshot(snapshot);
const current = readState(document);
if (sameState(current, target)) return text;
applyState(document, target);
// Unowned siblings can prevent removal of their parent table. Do not
// normalize formatting when preserving them made the operation a no-op.
if (sameState(current, readState(document))) return text;
return Bun.TOML.stringify(document);
} catch { throw unsupported(); }
}