Files
metona-ai-desktop/electron/harness/adapters/deepseek.adapter.ts
T
thzxx 3940716dc2
CI / 类型检查 + Lint + 单元测试 (push) Failing after 5m45s
CI / 全量测试 (Electron ABI) (push) Failing after 5m22s
CI / 产物编译验证 (push) Successful in 10m3s
feat: v0.7.0 四阶段全量迭代 — 修复面收口 · 安全纵深 · 架构还债 · 能力演进
P1 修复面收口: v0.6.3 截断自愈推全量(Anthropic/Ollama/非流式/引擎兜底); SSE 上游错误帧检测进重试通道;
clearMessages 摘要游标根治; truncateResult 内联图片白名单统一; 前端四 bug(确认弹窗锁死/MemoryViewer/
Virtuoso Footer/abort 尾部过滤) + reasoning 缓冲跨迭代污染; 托盘通知过滤与新建会话死链接线

P2 安全纵深: MCP 审批闭环(ConfirmationHook×PolicyEngine 联动+重名拒注册); SSRF 收敛 ssrf-guard 共享模块
(web_fetch 双通道校验+重定向终态复检); Electron 加固(preload CJS 化→sandbox:true/CSP/权限白名单/will-navigate);
run_command cmd.exe 白名单通道元字符守门; diff_viewer 10MB 预检; Anthropic thinking 预算下限; Agnes 思考显式关闭

P3 架构还债: OpenAICompatibleAdapter 中间基类收敛四家样板; 错误分类单轨化(删 mapError/getFetchSignal,
超时显式 ETIMEDOUT); PRAGMA user_version 迁移版本化; 死代码清理专项(cn.ts/SHORTCUTS/ContextMenu 分支/
getWindowState/modifiedArgs/sandbox 空壳); i18next 引入; a11y 第一轮; SearXNG 页批量草稿模型统一

P4 能力演进: Ollama pull 可取消/capabilities 探测/num_ctx 实测缓存; UpdateService feed 比对式自动更新
(app:updateCheck IPC + StatusBar 入口); MiMo providerOptions(web_search 服务端工具/strict JSON);
web_fetch extract_mode=markdown(turndown); network.proxyUrl 全局代理(Chromium sessions+undici dispatcher)

测试: 264 → 507 用例(Electron ABI 全绿零跳过), 覆盖引擎压缩管线/重试竞速/MEMORY.md 闸门/file_editor 五操作/
filesystem 七工具实体夹具/git 真实仓库/SSE 错误帧/全线截断自愈/Provider 请求形态矩阵/SSRF 表测/钩子分级矩阵/
OutputValidator 全量/SLO 指标/MCP 安全纯函数/task_manager 链路/渲染层纯域/i18n 桥契约
2026-08-27 17:06:58 +08:00

226 lines
7.9 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.
/**
* DeepSeek Provider Adapter
*
* 基于 OpenAI 兼容 API。支持 Tool Calling、Thinking 模式、流式输出。
* 模型: deepseek-v4-flash / deepseek-v4-pro1M 上下文,384K 最大输出)
*
* v0.6.4 P3-1: 继承 OpenAICompatibleAdapter —— send/sendStream/响应组装/
* 认证头/上下文窗口回退链全部收敛到共享基类,本文件只保留 DeepSeek 差异点:
* vision 模型判定、/models 合并、/user/balance、thinking+reasoning_effort 映射。
*
* @see apis/deepseek-api-docs-20260518.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 DeepSeekAdapter extends OpenAICompatibleAdapter {
// H-2 修复: providerId(规范要求)
override readonly providerId: string = 'deepseek';
readonly supportedModels = [
'deepseek-v4-pro',
'deepseek-v4-flash',
'deepseek-v4-flash-vision-exp',
];
readonly supportsToolCalling = true;
readonly supportsThinking = true;
// H-2 修复: DeepSeek 模型元信息(1M 上下文,384K 最大输出)
private static readonly MODEL_INFO: Record<string, MetonaModelInfo> = {
'deepseek-v4-pro': {
id: 'deepseek-v4-pro',
name: 'DeepSeek V4 Pro',
contextWindow: 1_000_000,
maxOutputTokens: 384_000,
supportsToolCalling: true,
supportsThinking: true,
description: 'DeepSeek 旗舰模型,1M 上下文,支持深度推理与工具调用',
},
'deepseek-v4-flash': {
id: 'deepseek-v4-flash',
name: 'DeepSeek V4 Flash',
contextWindow: 1_000_000,
maxOutputTokens: 384_000,
supportsToolCalling: true,
supportsThinking: true,
description: 'DeepSeek 快速版,1M 上下文,低延迟推理',
},
// v0.5.4: DeepSeek 多模态实验模型(OpenAI image_url content parts 格式)
'deepseek-v4-flash-vision-exp': {
id: 'deepseek-v4-flash-vision-exp',
name: 'DeepSeek V4 Flash Vision (Exp)',
contextWindow: 128_000,
maxOutputTokens: 8_192,
supportsToolCalling: true,
supportsThinking: false,
description: 'DeepSeek 多模态实验模型,支持图片输入(image_url content parts',
},
};
// ===== 共享基类差异声明 =====
protected override chatCompletionsUrl(): string {
return `${this.config.baseURL}/chat/completions`;
}
protected override sendTimeoutMs(): number {
return 120_000;
}
protected override modelInfoTable(): Record<string, MetonaModelInfo> {
return DeepSeekAdapter.MODEL_INFO;
}
protected override providerLabel(): string {
return 'DeepSeek';
}
/**
* v0.5.4: 当前模型是否支持多模态图片输入
*
* DeepSeek 仅 vision 系列模型支持图片(命名含 'vision');
* 非 vision 模型收到 images 时静默丢弃(避免 API 400)。
* 前端上传入口由 llm.multimodalEnabled 配置总开关控制,此处是 adapter 侧的模型级防线。
*/
private isVisionModel(): boolean {
return this.config.defaultModel.includes('vision');
}
// ===== GET /models =====
/**
* 优先尝试从 API 获取实时模型列表,并合并本地 MODEL_INFO 元数据。
* API 不可用时回退到 supportedModels。
*/
override async listModels(): Promise<MetonaModelInfo[]> {
try {
const response = await fetch(`${this.config.baseURL}/models`, {
headers: { Authorization: `Bearer ${this.config.apiKey}` },
signal: AbortSignal.timeout(10_000),
});
if (response.ok) {
const data = (await response.json()) as { data?: Array<{ id: string }> };
if (data.data?.length) {
// 合并 API 返回的模型 ID 与本地元数据
return data.data.map((m) => DeepSeekAdapter.MODEL_INFO[m.id] ?? { id: m.id });
}
}
} catch {
// API 不可用时降级
}
// 回退到 supportedModels(带本地元数据)
return this.supportedModels.map((id) => DeepSeekAdapter.MODEL_INFO[id] ?? { id });
}
// ===== GET /user/balance =====
/**
* 查询账户余额
*
* v0.5.2 修复: 官方 API 返回 balance_infos 数组格式(此前按扁平字段解析恒为 0)。
* URL 规范化: 余额端点为 {root}/user/balance(无 /v1 前缀),需剥离配置中的尾斜杠与 /v1。
*/
async getBalance(): Promise<{
currency: string;
totalBalance: string;
grantedBalance: string;
toppedUpBalance: string;
} | null> {
try {
const root = this.config.baseURL.replace(/\/+$/, '').replace(/\/v1$/, '');
const response = await fetch(`${root}/user/balance`, {
headers: { Authorization: `Bearer ${this.config.apiKey}` },
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) return null;
const data = (await response.json()) as {
is_available?: boolean;
balance_infos?: Array<{
currency?: string;
total_balance?: string;
granted_balance?: string;
topped_up_balance?: string;
}>;
// 扁平格式字段(网关/代理兼容)
currency?: string;
total_balance?: string;
granted_balance?: string;
topped_up_balance?: string;
};
const info = data.balance_infos?.[0] ?? data;
return {
currency: info.currency ?? 'CNY',
totalBalance: info.total_balance ?? '0',
grantedBalance: info.granted_balance ?? '0',
toppedUpBalance: info.topped_up_balance ?? '0',
};
} catch {
return null;
}
}
// ========== 协议参数映射(DeepSeek 差异点) ==========
protected override toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
// v0.6.2: images 处理收敛至共享层(includeImages = vision 模型才转换,
// 非 vision 静默丢弃——正确行为,见 openai-format.ts #27 记录)
const messages = buildOpenAICompatibleMessages(request, this.isVisionModel());
const tools = buildOpenAICompatibleTools(request.tools);
// v0.5.3: max_tokens 按模型上限钳制 — 引擎默认 63488 超过部分模型上限时 API 直接 400
const modelInfo = DeepSeekAdapter.MODEL_INFO[this.config.defaultModel];
const maxOutput = modelInfo?.maxOutputTokens ?? 384_000;
const maxTokens = Math.min(request.params.maxTokens ?? maxOutput, maxOutput);
const body: Record<string, unknown> = {
model: this.config.defaultModel,
messages,
temperature: request.params.temperature,
max_tokens: maxTokens,
stream,
};
// v0.5.4: vision 模型图片数审计(转换在共享层完成)
if (this.isVisionModel()) {
const imageCount = request.messages.reduce((n, m) => n + (m.images?.length ?? 0), 0);
if (imageCount > 0) {
log.info(`[DeepSeek] Vision model processing ${imageCount} image(s)`);
}
}
if (stream) {
body.stream_options = { include_usage: true };
}
if (tools) {
body.tools = tools;
}
// Thinking 模式
// API 默认 thinking.type = "enabled",必须显式发送 disabled 才能关闭
if (request.params.thinkingEnabled === false) {
body.thinking = { type: 'disabled' };
} else if (request.params.thinkingEnabled) {
body.thinking = { type: 'enabled' };
const effortMap: Record<string, string> = {
low: 'high',
medium: 'high',
high: 'high',
max: 'max',
};
// DeepSeek API 仅支持 high / max 两档,low/medium 映射为 high
body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high';
}
// 停止序列
if (request.params.stopSequences?.length) {
body.stop = request.params.stopSequences;
}
return body;
}
}