Files
metona-ai-desktop/electron/harness/adapters/mimo.adapter.ts
T
thzxx 4cd6e997b5
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m45s
CI / 全量测试 (Electron ABI) (push) Failing after 6m28s
CI / 产物编译验证 (push) Successful in 11m18s
feat: v0.8.2 安全纵深补全 · 协议保真 · 断链修复 — 图片SSRF/根MEMORY.md保护根治 · Anthropic thinking回传+pause_turn续传 · 2523 用例全量回归 + E2E 扩充
2026-09-08 14:30:27 +08:00

164 lines
7.1 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 模型元信息
// v0.8.1: 仅承载展示与能力声明 —— 窗口/输出上限数值已按硬性契约删除,
// 唯一合法来源是设置面板 llm.contextWindow / llm.maxTokens
private static readonly MODEL_INFO: Record<string, MetonaModelInfo> = {
'mimo-v2.5-pro': {
id: 'mimo-v2.5-pro',
name: 'MiMo V2.5 Pro',
supportsToolCalling: true,
supportsThinking: true,
description: '小米 MiMo 旗舰模型,支持深度思考与工具调用',
},
'mimo-v2.5': {
id: 'mimo-v2.5',
name: 'MiMo V2.5',
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
// v0.8.1 硬性契约: 原样透传设置面板「最大输出上限」(llm.maxTokens),删除了
// 旧的"#41 thinking 兜底 32768"与"按模型上限钳制"逻辑 —— 代码中不存在任何
// 写死的输出上限;未配置时该字段不下发,由服务端默认值决定。
const body: Record<string, unknown> = {
model: this.config.defaultModel,
messages,
max_completion_tokens: request.params.maxTokens,
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'} 服务端搜索工具。与客户端 tools 定义互不影响。
// v0.8.2 P2-6: 引用注释(annotations)的采集与回填实现在共享层
// sse-stream.ts(流式 [DONE]/断流兜底 + 非流式解析统一回填 Markdown 引用列表),
// 此前注释宣称"由上层归并展示"但全链路无读取方,引用信息丢失。
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 参数结构一致)
// v0.8.0 修订(用户意图优先): 思考参数完全遵循用户配置,不再按
// supportsThinking 元信息硬门控 —— 预算耗尽风险由引擎空响应守卫的
// 降级重试链路兜底;元信息与配置不符时仅告警不拦截。
//
// v0.8.2 P2-6 根治: 未配置(undefined)时默认 **disabled** —— 与 DeepSeek
// (显式 disabled/ Agnes(显式 false)一致。旧实现 `!== false` 使 MiMo 在
// UI「未开启思考」状态下隐式进入思考模式,且思考模式下服务端强制
// temperature=1.0/top_p=0.95,用户的温度配置被静默吞掉。
const wantThinking = request.params.thinkingEnabled === true;
if (!wantThinking) {
// 显式禁用思考:传 disabled + temperature/top_p(非思考模式下这两个参数有效)
body.thinking = { type: 'disabled' };
body.temperature = request.params.temperature;
body.top_p = request.params.topP;
} else {
// 启用思考(仅显式 true —— 未配置已在上分支显式 disabled,见 P2-6 注)
// 思考模式下 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 | undefined;
if (typeof effectiveMax === 'number' && effectiveMax < 8192) {
log.warn(
`[MiMo] thinking enabled with small output budget (${effectiveMax} tokens per user config) — reasoning may consume the entire budget and truncate the answer`,
);
}
}
// 停止序列
if (request.params.stopSequences?.length) {
body.stop = request.params.stopSequences;
}
return body;
}
}