Files
metona-ai-desktop/electron/harness/adapters/mimo.adapter.ts
T
thzxx 839860083f
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m8s
CI / 全量测试 (Electron ABI) (push) Failing after 6m8s
CI / 产物编译验证 (push) Successful in 11m1s
fix: v0.8.0 修订 — 思考用户意图优先 · 移除元信息硬门控 · 实施清单入库
- DeepSeek/MiMo/Agnes 移除 supportsThinking 元信息硬门控:思考参数完全遵循用户配置
  (事故复盘中 vision-exp 元信息标注不支持思考、实际产生了 8189 token 推理内容,
  元信息不可靠;预算耗尽由引擎降级重试兜底,元信息不符仅告警不拦截)
- Ollama 保留 /api/show 能力探测门控(服务端硬协议约束:向不支持思考的模型发
  think 每次请求 400,属协议正确性而非意图覆盖),探测失败 fail-open
- LLM 设置提示文案修订:元信息不符仍按用户配置发送,降级重试自动兜底
- 测试契约反向钉住:vision-exp + 用户开启→照发 enabled+reasoning_effort;
  关闭/未配置→显式 disabled;Ollama 探测 false→不发 think / null→fail-open
- 补录 docs/v0.8.0-迭代实施清单.md(含逐项验证记录与本次修订记录;
  首次提交时该文件因故未入库,本次补齐)
- 验证:typecheck 0 错误 / lint 0 问题 / 系统 Node 2146 通过 / thinking 矩阵 101 用例全绿
2026-09-05 20:49:20 +08:00

165 lines
6.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* MiMo (Xiaomi) Provider Adapter
*
* 基于 OpenAI 兼容 API。支持 Tool Calling、Thinking 模式、流式输出。
* 模型: mimo-v2.5-pro1M 上下文 / 131072 max_tokens/ mimo-v2.51M 上下文 / 32768 max_tokens
*
* v0.6.4 P3-1: 继承 OpenAICompatibleAdapter —— 传输/组装/回退链收敛到共享基类,
* 本文件只保留 MiMo 差异点:max_completion_tokens 字段名、tool_choice 强制 "auto"、
* 思考模式与 temperature/top_p 互斥、无 /models 端点(本地元数据列表)。
*
* @see apis/mimo-api-docs-20260715.html
*/
import log from 'electron-log';
import type { MetonaRequest } from '../types';
import type { MetonaModelInfo } from '../types/metona-adapter';
import { buildOpenAICompatibleMessages, buildOpenAICompatibleTools } from './shared/openai-format';
import { OpenAICompatibleAdapter } from './shared/openai-compatible-base';
export class MimoAdapter extends OpenAICompatibleAdapter {
override readonly providerId: string = 'mimo';
readonly supportedModels = ['mimo-v2.5-pro', 'mimo-v2.5'];
readonly supportsToolCalling = true;
readonly supportsThinking = true;
// MiMo 模型元信息
// mimo-v2.5-pro: 1M 上下文 / 131072 max_tokensmimo-v2.5: 1M 上下文 / 32768 max_tokens
private static readonly MODEL_INFO: Record<string, MetonaModelInfo> = {
'mimo-v2.5-pro': {
id: 'mimo-v2.5-pro',
name: 'MiMo V2.5 Pro',
contextWindow: 1_000_000,
maxOutputTokens: 131_072,
supportsToolCalling: true,
supportsThinking: true,
description: '小米 MiMo 旗舰模型,支持深度思考与工具调用',
},
'mimo-v2.5': {
id: 'mimo-v2.5',
name: 'MiMo V2.5',
contextWindow: 1_000_000,
maxOutputTokens: 32_768,
supportsToolCalling: true,
supportsThinking: true,
description: '小米 MiMo 标准模型,低延迟推理',
},
};
// ===== 共享基类差异声明 =====
protected override chatCompletionsUrl(): string {
return `${this.config.baseURL}/chat/completions`;
}
protected override sendTimeoutMs(): number {
return 120_000;
}
protected override modelInfoTable(): Record<string, MetonaModelInfo> {
return MimoAdapter.MODEL_INFO;
}
protected override providerLabel(): string {
return 'MiMo';
}
/**
* MiMo 官方未提供 /models 端点,直接返回本地元数据。
*/
override async listModels(): Promise<MetonaModelInfo[]> {
return this.supportedModels.map((id) => MimoAdapter.MODEL_INFO[id] ?? { id });
}
// ========== 协议参数映射(MiMo 差异点) ==========
protected override toNativeRequest(
request: MetonaRequest,
stream: boolean,
): Record<string, unknown> {
// v0.6.2: images 处理收敛至共享层(原索引对齐循环在孤立 tool 过滤后会错位)
const messages = buildOpenAICompatibleMessages(request, true);
const tools = buildOpenAICompatibleTools(request.tools);
// MiMo 使用 max_completion_tokens(非 max_tokens
// #41 修复: thinking 模式下未配置时兜底 32768thinking 占用 token 配额,API 默认值过小会截断输出)
// v0.5.3: 按模型上限钳制(pro 131072 / standard 32768)—
// 引擎默认 63488 超过 standard 上限时 API 直接 400
const mimoMaxOutput =
MimoAdapter.MODEL_INFO[this.config.defaultModel]?.maxOutputTokens ?? 131_072;
const mimoDefault = request.params.thinkingEnabled !== false ? 32_768 : mimoMaxOutput;
const body: Record<string, unknown> = {
model: this.config.defaultModel,
messages,
max_completion_tokens: Math.min(request.params.maxTokens ?? mimoDefault, mimoMaxOutput),
stream,
};
if (stream) {
body.stream_options = { include_usage: true };
}
if (tools) {
body.tools = tools;
// MiMo 仅支持 tool_choice: "auto"
body.tool_choice = 'auto';
}
// v0.6.4 P4-3: MiMo 服务端内置工具透出 —— config.providerOptions.enableWebSearch
// 开启后附加 {type:'web_search'} 服务端搜索工具(annotations 引用随响应返回,
// 由上层归并为文本内容展示)。与客户端 tools 定义互不影响。
const providerOptions = this.config.providerOptions as Record<string, unknown> | undefined;
if (providerOptions?.['enableWebSearch'] === true) {
const serverTools = body.tools
? [...(body.tools as Array<Record<string, unknown>>), { type: 'web_search' }]
: [{ type: 'web_search' }];
body.tools = serverTools;
if (!body.tool_choice) body.tool_choice = 'auto';
}
// v0.6.4 P4-3: strict JSON 响应格式开关(response_format: json_object)——
// 供结构化抽取类任务使用;与流式模式兼容性由服务端保证(文档标注支持子集)
if (providerOptions?.['responseFormatJson'] === true) {
body.response_format = { type: 'json_object' };
}
// Thinking 模式(与 DeepSeek 参数结构一致)
// MiMo API 默认 thinking.type = "enabled",必须显式发送 disabled 才能关闭
// v0.8.0 修订(用户意图优先): 思考参数完全遵循用户配置,不再按
// supportsThinking 元信息硬门控 —— 预算耗尽风险由引擎空响应守卫的
// 降级重试链路兜底;元信息与配置不符时仅告警不拦截。
const wantThinking = request.params.thinkingEnabled !== false;
if (!wantThinking) {
// 显式禁用思考:传 disabled + temperature/top_p(非思考模式下这两个参数有效)
body.thinking = { type: 'disabled' };
body.temperature = request.params.temperature;
body.top_p = request.params.topP;
} else {
// 启用思考(包括 undefined,因为 MiMo 默认 enabled
// 思考模式下 temperature/top_p 被 API 强制覆盖为 1.0/0.95,不传
body.thinking = { type: 'enabled' };
// 元信息标注不支持思考但用户开启 —— 告知降级兜底路径(不拦截)
if (MimoAdapter.MODEL_INFO[this.config.defaultModel]?.supportsThinking === false) {
log.warn(
`[MiMo] model "${this.config.defaultModel}" metadata says thinking unsupported — sending thinking params per user config (degraded retry handles budget exhaustion)`,
);
}
// v0.8.0 P0-3: 思考占用输出预算 —— 钳制后预算过小时显式告警
const effectiveMax = body.max_completion_tokens as number;
if (typeof effectiveMax === 'number' && effectiveMax < 8192) {
log.warn(
`[MiMo] thinking enabled with small output budget (${effectiveMax} tokens after model clamp) — reasoning may consume the entire budget and truncate the answer`,
);
}
}
// 停止序列
if (request.params.stopSequences?.length) {
body.stop = request.params.stopSequences;
}
return body;
}
}