Files
plugins/packages/kimi-desktop/page/boot.js
T
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

76 lines
3.5 KiB
JavaScript

// The host page cannot fetch. Navigate its existing sandboxed iframe directly
// to the plugin service; magpie.service supplies a proxy URL for Magpie Web.
(async () => {
const DEFAULT_PORT = 3491;
let theme = '';
let lang = 'en';
let port = DEFAULT_PORT;
let service = null;
try {
const ready = await window.magpie?.ready;
if (typeof ready?.dark === 'boolean') theme = ready.dark ? 'dark' : 'light';
if (typeof ready?.lang === 'string' && ready.lang) lang = ready.lang;
} catch { /* The service can use the system theme without the bridge. */ }
const chinese = /^zh(?:-|$)/i.test(lang);
if (document.documentElement) document.documentElement.lang = chinese ? 'zh' : 'en';
document.title = chinese ? 'Kimi Work 模型' : 'Kimi Work models';
const heading = document.getElementById('heading');
if (heading) heading.textContent = document.title;
const message = document.getElementById('message');
if (message) message.textContent = chinese ? '正在连接设置服务…' : 'Connecting to the settings service…';
try {
const options = await window.magpie?.options?.get?.();
const value = Number(options?.settingsPort);
if (Number.isInteger(value) && value > 0 && value < 65536) port = value;
} catch { /* The server and launcher share the same default. */ }
try {
const value = window.magpie?.service;
if (typeof value === 'string') {
const url = new URL(value, location.href);
if ((url.protocol === 'http:' || url.protocol === 'https:') && !url.username && !url.password) service = url;
}
} catch { /* An old host may not provide a service proxy. */ }
if (!service) {
const host = String(location.hostname || '').toLowerCase().replace(/^\[|\]$/g, '');
const local = host === '' || host === 'localhost' || host.endsWith('.localhost') || host === '127.0.0.1' || host === '::1';
if (!local) {
if (message) message.textContent = chinese
? '未获得设置服务地址。请在运行 Magpie 的桌面端打开,或使用支持插件服务代理的 Magpie Web。'
: 'No settings service address is available. Open this page on the Magpie desktop, or use Magpie Web with plugin service proxy support.';
return;
}
service = new URL(`http://127.0.0.1:${port}/`);
}
if (!service.pathname.endsWith('/')) service.pathname += '/';
if (theme) service.searchParams.set('theme', theme);
service.searchParams.set('lang', lang);
// An opaque frame cannot use connect-src 'self'; the service validates this
// concrete origin before including it in its response's connect-src policy.
service.searchParams.set('uiOrigin', service.origin);
const retry = document.getElementById('retry');
retry.textContent = chinese ? '重试' : 'Retry';
let attempts = 0;
const connect = () => {
attempts += 1;
// WebKit can leave this document alive when a navigation is refused.
// Successful navigation destroys the document and its pending timer.
setTimeout(() => {
if (attempts < 3) connect();
else {
if (message) message.textContent = chinese
? '未能打开设置服务。请稍候,然后点击“重试”。'
: 'The settings service could not be opened. Wait a moment, then select Retry.';
retry.hidden = false;
}
}, 3000);
location.replace(service.href);
};
retry.addEventListener('click', () => {
attempts = 0;
retry.hidden = true;
if (message) message.textContent = chinese ? '正在连接设置服务…' : 'Connecting to the settings service…';
connect();
});
connect();
})();