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

295 lines
9.1 KiB
JavaScript

// Context token counting / estimation helper (Anthropic count_tokens DTO).
// Pure conversion OpenAI-chat -> Anthropic count DTO + a single gateway POST.
// Never claims exactness: the gateway may answer with a rough estimate.
const UNAVAILABLE = 'context_count_unavailable';
const COUNT_PATH = '/messages/count_tokens';
const MAX_BODY_BYTES = 8192;
const DEFAULT_USER_AGENT = 'Desktop Kimi Work';
// Root already lowercases the incoming request headers; only these are forwarded.
const FORWARD_HEADERS = ['user-agent', 'x-msh-version', 'r-timezone', 'x-session-id', 'session_id'];
function unavailable() {
const error = new Error(UNAVAILABLE);
error.code = UNAVAILABLE;
return error;
}
function fail() {
throw unavailable();
}
function isPlainObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function textBlocks(text) {
if (text.length === 0) return [];
return [{ type: 'text', text }];
}
function imageBlockFromUrl(url) {
if (typeof url !== 'string' || url.length === 0) fail();
const dataMatch = /^data:image\/(jpeg|png|gif|webp);base64,([A-Za-z0-9+/=\s]+)$/i.exec(url);
if (dataMatch) {
const data = dataMatch[2].replace(/\s+/g, '');
if (data.length === 0) fail();
return {
type: 'image',
source: { type: 'base64', media_type: `image/${dataMatch[1].toLowerCase()}`, data },
};
}
let parsed;
try {
parsed = new URL(url);
} catch {
fail();
}
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') fail();
return { type: 'image', source: { type: 'url', url } };
}
function contentPartToBlocks(part) {
if (typeof part === 'string') return textBlocks(part);
if (!isPlainObject(part)) fail();
if (part.type === 'text') {
if (typeof part.text !== 'string') fail();
return textBlocks(part.text);
}
if (part.type === 'image_url') {
const raw = part.image_url;
const url = typeof raw === 'string' ? raw : isPlainObject(raw) ? raw.url : undefined;
return [imageBlockFromUrl(url)];
}
// audio/video/file/input_audio/... are unsupported: refuse, never skip.
return fail();
}
function normalizeContent(content) {
if (typeof content === 'string') return textBlocks(content);
if (!Array.isArray(content)) fail();
const blocks = [];
for (const part of content) blocks.push(...contentPartToBlocks(part));
return blocks;
}
function systemTextBlocks(content) {
if (typeof content === 'string') return textBlocks(content);
if (!Array.isArray(content)) fail();
const blocks = [];
for (const part of content) {
if (typeof part === 'string') {
blocks.push(...textBlocks(part));
continue;
}
// Anthropic system supports text only: images and other blocks are refused.
if (!isPlainObject(part) || part.type !== 'text' || typeof part.text !== 'string') fail();
blocks.push(...textBlocks(part.text));
}
return blocks;
}
function parseToolArguments(raw) {
// Missing/null/empty arguments are a malformed tool call; legal no-arg calls use "{}".
if (typeof raw !== 'string' || raw.length === 0) fail();
let parsed;
try {
parsed = JSON.parse(raw);
} catch {
fail();
}
if (!isPlainObject(parsed)) fail();
return parsed;
}
function toolCallToBlock(call) {
if (!isPlainObject(call)) fail();
if (call.type !== undefined && call.type !== 'function') fail();
const fn = call.function;
if (!isPlainObject(fn)) fail();
if (typeof fn.name !== 'string' || fn.name.length === 0) fail();
if (typeof call.id !== 'string' || call.id.length === 0) fail();
return {
type: 'tool_use',
id: call.id,
name: fn.name,
input: parseToolArguments(fn.arguments),
};
}
function toolDefToAnthropic(tool) {
if (!isPlainObject(tool)) fail();
if (tool.type !== 'function') fail();
const fn = tool.function;
if (!isPlainObject(fn)) fail();
if (typeof fn.name !== 'string' || fn.name.length === 0) fail();
const schema = fn.parameters;
let inputSchema;
if (schema === undefined) {
inputSchema = { type: 'object', properties: {} }; // legal no-arg tool
} else if (isPlainObject(schema)) {
inputSchema = schema; // keep original value/reference
} else {
fail(); // explicit null or non-object schema is malformed
}
const out = { name: fn.name, input_schema: inputSchema };
if (fn.description !== undefined) {
if (typeof fn.description !== 'string') fail();
out.description = fn.description;
}
return out;
}
export function buildCountRequest(chatPayload) {
if (!isPlainObject(chatPayload)) fail();
if (typeof chatPayload.model !== 'string' || chatPayload.model.length === 0) fail();
if (!Array.isArray(chatPayload.messages)) fail();
const systemBlocks = [];
const messages = [];
for (const message of chatPayload.messages) {
if (!isPlainObject(message)) fail();
const role = message.role;
// Legacy function_call and tool_calls outside assistant cannot be preserved: refuse.
if (message.function_call != null) fail();
if (role !== 'assistant' && message.tool_calls != null) fail();
if (role === 'system' || role === 'developer') {
systemBlocks.push(...systemTextBlocks(message.content));
continue;
}
if (role === 'user') {
messages.push({ role: 'user', content: normalizeContent(message.content) });
continue;
}
if (role === 'assistant') {
const blocks = [];
if (message.content !== undefined && message.content !== null) {
blocks.push(...normalizeContent(message.content));
}
if (message.reasoning_content !== undefined && message.reasoning_content !== null) {
if (typeof message.reasoning_content !== 'string') fail();
blocks.push(...textBlocks(message.reasoning_content));
}
if (message.tool_calls != null) {
if (!Array.isArray(message.tool_calls)) fail();
for (const call of message.tool_calls) blocks.push(toolCallToBlock(call));
}
if (blocks.length === 0) fail();
messages.push({ role: 'assistant', content: blocks });
continue;
}
if (role === 'tool') {
if (typeof message.tool_call_id !== 'string' || message.tool_call_id.length === 0) fail();
messages.push({
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: message.tool_call_id,
content: normalizeContent(message.content),
},
],
});
continue;
}
fail();
}
if (messages.length === 0) fail();
const dto = { model: chatPayload.model };
if (systemBlocks.length > 0) dto.system = systemBlocks;
dto.messages = messages;
if (chatPayload.tools !== undefined) {
if (!Array.isArray(chatPayload.tools)) fail();
dto.tools = chatPayload.tools.map(toolDefToAnthropic);
}
return dto;
}
async function readBoundedJson(response) {
const body = response.body;
if (!body || typeof body.getReader !== 'function') fail();
const reader = body.getReader();
const chunks = [];
let total = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) break;
total += value.byteLength;
if (total > MAX_BODY_BYTES) {
await reader.cancel();
fail();
}
chunks.push(value);
}
const bytes = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
bytes.set(chunk, offset);
offset += chunk.byteLength;
}
// Decoder and JSON errors fall through to the single outer catch.
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
return JSON.parse(text);
}
function buildHeaders(gatewayKey, requestHeaders) {
const headers = {
'content-type': 'application/json',
authorization: `Bearer ${gatewayKey}`,
'user-agent': DEFAULT_USER_AGENT,
};
const source = requestHeaders ?? {};
for (const name of FORWARD_HEADERS) {
const value = source[name];
if (typeof value === 'string') headers[name] = value;
}
return headers;
}
export async function countContextTokens(chatPayload, options = {}) {
try {
const { gatewayURL, gatewayKey, fetchImpl = fetch, signal, requestHeaders = {} } = options ?? {};
if (typeof gatewayURL !== 'string' || gatewayURL.length === 0) fail();
if (typeof gatewayKey !== 'string' || gatewayKey.length === 0) fail();
if (typeof fetchImpl !== 'function') fail();
const dto = buildCountRequest(chatPayload);
const url = `${gatewayURL.replace(/\/+$/, '')}${COUNT_PATH}`;
const headers = buildHeaders(gatewayKey, requestHeaders);
const timeout = AbortSignal.timeout(6000);
const response = await fetchImpl(url, {
method: 'POST',
redirect: 'error',
proxy: '',
headers,
body: JSON.stringify(dto),
signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
});
if (!response || response.ok !== true) {
await response?.body?.cancel();
fail();
}
const payload = await readBoundedJson(response);
if (!isPlainObject(payload)) fail();
const tokens = payload.input_tokens;
if (!Number.isSafeInteger(tokens) || tokens < 0) fail();
return tokens;
} catch {
// Single fixed failure: never surface provider body, error text, or secrets.
throw unavailable();
}
}