P0 会话可靠性收口(根治"模型思考着会话就停止"): - P0-1 finish_reason 全链路贯通:DONE 事件与 IterationStep 新增 finishReason,OpenAI 共享 SSE / Anthropic message_delta.stop_reason / Ollama done_reason 三路采集,TRACE 层弃用硬编码 'stop' 记录真值 - P0-2 空响应守卫 + 降级重试:零产出流→可重试错误走退避;思考耗尽输出预算(reasoning-only + length)→自动关闭思考降级重试一次;仍失败→OUTPUT_LENGTH_EXCEEDED 结构化错误 + 故障转移;附带根治 abort 恰逢零工具调用轮被 COMPLETED 抢占的真实缺陷 - P0-3 思考×能力×预算三对齐:DeepSeek/MiMo/Agnes/Ollama 四家 supportsThinking=false 强制不发思考参数;小输出预算告警;设置页联动提示 - P0-4 渲染层可见性:截断/空完成/友好错误三类提示,i18n 全部出层 - P0-5 回归四件套:reasoning-only 终止判定、集成级空闲超时、504 引擎重试归类、思考中 abort→USER_INTERRUPT、P4-2 强制收尾路径 FEAT-1:LLM 设置新增「最大输出上限」——Provider 支持矩阵显隐 + 模型上限钳制提示 + 超限保存警告 + llm.maxTokens 热生效 P1 修复面收口: - 渲染层三缺陷根治:后台会话回放缓冲(2000 条/4MB 有界 + agent:getReplayState + 事件总线)+ abort 双层自愈 + sendMessage 收尾兜底 + 中断卡片清扫 - 工具 abort 信号全覆盖:web_search/web_fetch/http_request/code_search/git 系列/delegate_task 全部接入引擎中断;web_search 时间预算收敛(720s→≤240s);移除伪造 ToolExecutionContext 与死代码 - 安全:本地 Pinned CONNECT 代理根治浏览器通道 DNS rebinding(校验期 IP pinning,可注入 resolver 表测);配置 URL 域名解析深校验(DeepCheckSoftFailure 软失败);SSE 空 error 帧防御修复;Ollama generate/embed AbortSignal.any 合并 - 缺陷清单:UTF-16 BOM 读取、tmp 同毫秒碰撞(nanoid 后缀)、code_search JS 回退参数对称(case_sensitive/前后文独立)、list_directory include_node_modules、崩溃自愈退避(60s 窗 ≥3 次停 reload)、MemoryViewer/Sidebar i18n 收口 P2 能力演进: - 会话回收站:SCHEMA_VERSION 3 + 迁移 10(deleted_at,存在性守卫),软删除/恢复/彻底删除/30 天自动清理(启动+24h),searchMessages 聚合剔除,Sidebar 回收站面板 - 会话回放播放器:sessions:listRecordings/readRecording(白名单+目录边界+20MB 上限),SessionReplayPlayer 时间轴/步进/变速,Trace 面板入口 - electron-updater 自动更新:双轨(手动 feed 比对保留),生产环境启动静默检查 + update:status 广播 + app:updateInstall + LogsSettings UpdatePanel + builder publish 配置 - @ 文件提及:workspace.listFiles/readFileClip(边界/512KB/NUL 拒绝/MEMORY.md 保护),ChatInput Fuse 联想+键盘导航+附件管线注入 - MCP Resources/Prompts 发现:可选能力 try/catch 降级,mcp:listServerContents,MCPSettings 展开视图 - 文档对齐:内部 API 标准 HTML(Adapter 清单补 MiMo/已实现注记/STREAM_RESET/DONE.finishReason/ repetition_truncation 映射);README v0.8.0 亮点表 P3 测试基建: - 新增 4 个测试文件:engine-stream-contract(6)、engine-stream-reliability(4:集成空闲超时/504 重试/思考中 abort/P4-2 强制收尾)、thinking-capability-gate(7)、pinned-proxy(9,含深校验 5)、session-trash(5,DB 域)、use-agent-stream hook 级(5)、agent.test 回放缓冲(2) - 契约更新:orchestrator 被中断 SubAgent success=false(abort 优先级修复语义)、SSE 空 error 帧、UTF-16 正常读取、DeepSeek 未配置思考显式 disabled、迁移矩阵 v2→3 - 弱断言根治:registry WEBP 单向断言、hooks-contracts 自比恒真、memory 空 token 补强 全量验证:typecheck 0 错误 / lint 0 问题 / 系统 Node 2144 通过(301 DB 用例按 ABI 跳过)/ Electron ABI 2445/2445 全量通过 0 跳过
160 lines
6.4 KiB
TypeScript
160 lines
6.4 KiB
TypeScript
/**
|
||
* MiMo (Xiaomi) Provider Adapter
|
||
*
|
||
* 基于 OpenAI 兼容 API。支持 Tool Calling、Thinking 模式、流式输出。
|
||
* 模型: mimo-v2.5-pro(1M 上下文 / 131072 max_tokens)/ mimo-v2.5(1M 上下文 / 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_tokens;mimo-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 模式下未配置时兜底 32768(thinking 占用 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 P0-3: 模型能力门控 —— supportsThinking===false 的模型强制 disabled
|
||
//(思考 token 计入 max_completion_tokens,能力不符的模型上会耗尽输出预算)
|
||
const modelThinkingSupported =
|
||
MimoAdapter.MODEL_INFO[this.config.defaultModel]?.supportsThinking !== false;
|
||
const wantThinking = request.params.thinkingEnabled !== false && modelThinkingSupported;
|
||
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' };
|
||
// 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;
|
||
}
|
||
}
|