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 桥契约
This commit is contained in:
@@ -4,22 +4,21 @@
|
||||
* 基于 OpenAI 兼容 API。支持 Tool Calling、Thinking 模式、流式输出。
|
||||
* 模型: deepseek-v4-flash / deepseek-v4-pro(1M 上下文,384K 最大输出)
|
||||
*
|
||||
* 独立继承 BaseAdapter,通过 shared/openai-format 和 shared/sse-stream 复用
|
||||
* OpenAI 兼容格式构建和 SSE 流式解析逻辑。不与其他 Provider Adapter 耦合。
|
||||
* 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 { BaseAdapter } from './base-adapter';
|
||||
import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types';
|
||||
import { MetonaFinishReason } from '../types';
|
||||
import type { MetonaRequest } from '../types';
|
||||
import type { MetonaModelInfo } from '../types/metona-adapter';
|
||||
import { buildOpenAICompatibleMessages, buildOpenAICompatibleTools } from './shared/openai-format';
|
||||
import { parseSSEStream, parseOpenAICompatibleResponse } from './shared/sse-stream';
|
||||
import { OpenAICompatibleAdapter } from './shared/openai-compatible-base';
|
||||
|
||||
export class DeepSeekAdapter extends BaseAdapter {
|
||||
// H-2 修复: provider → providerId(规范要求)
|
||||
export class DeepSeekAdapter extends OpenAICompatibleAdapter {
|
||||
// H-2 修复: providerId(规范要求)
|
||||
override readonly providerId: string = 'deepseek';
|
||||
readonly supportedModels = [
|
||||
'deepseek-v4-pro',
|
||||
@@ -61,6 +60,24 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
},
|
||||
};
|
||||
|
||||
// ===== 共享基类差异声明 =====
|
||||
|
||||
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: 当前模型是否支持多模态图片输入
|
||||
*
|
||||
@@ -72,94 +89,13 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
return this.config.defaultModel.includes('vision');
|
||||
}
|
||||
|
||||
// ===== POST /chat/completions (非流式) =====
|
||||
|
||||
// H-2 修复: chat → send(规范要求)
|
||||
async send(request: MetonaRequest): Promise<MetonaResponse> {
|
||||
const body = this.toNativeRequest(request, false);
|
||||
|
||||
// #24 修复: 使用 fetchWithTimeout 替代 getFetchSignal + fetch,确保 timer 清理
|
||||
const response = await this.fetchWithTimeout(
|
||||
`${this.config.baseURL}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
...this.config.headers,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
this.config.timeoutMs ?? 120_000,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
await this.throwHttpError(response, 'DeepSeek API error');
|
||||
}
|
||||
|
||||
const data = (await response.json()) as Record<string, unknown>;
|
||||
const parsed = parseOpenAICompatibleResponse(data);
|
||||
|
||||
return {
|
||||
meta: {
|
||||
requestId: request.meta.requestId,
|
||||
provider: this.providerId,
|
||||
model: (data.model as string) ?? this.config.defaultModel,
|
||||
latencyMs: 0,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
content: parsed.content,
|
||||
reasoningContent: parsed.reasoningContent,
|
||||
toolCalls: parsed.toolCalls,
|
||||
usage: parsed.usage,
|
||||
finishReason: parsed.finishReason as MetonaFinishReason,
|
||||
};
|
||||
}
|
||||
|
||||
// ===== POST /chat/completions (流式) =====
|
||||
|
||||
// H-2 修复: chatStream → sendStream(规范要求)
|
||||
async *sendStream(request: MetonaRequest): AsyncIterable<MetonaStreamEvent> {
|
||||
const body = this.toNativeRequest(request, true);
|
||||
|
||||
// #24 修复: 使用 fetchWithTimeout 替代 getFetchSignal + fetch,确保 timer 清理
|
||||
const response = await this.fetchWithTimeout(
|
||||
`${this.config.baseURL}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
...this.config.headers,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
this.config.timeoutMs ?? 300_000,
|
||||
);
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
await this.throwHttpError(response, 'DeepSeek stream error');
|
||||
}
|
||||
|
||||
yield* parseSSEStream(
|
||||
// 非空断言:上方 if 已确保 response.body 不为 null
|
||||
// TypeScript 无法通过 await Promise<never> 正确收窄,需显式断言
|
||||
response.body!,
|
||||
request.meta.requestId,
|
||||
request.meta.sessionId,
|
||||
request.meta.iteration,
|
||||
);
|
||||
}
|
||||
|
||||
// ===== GET /models =====
|
||||
|
||||
/**
|
||||
* H-2 修复: 返回 MetonaModelInfo[](规范要求)
|
||||
*
|
||||
* 优先尝试从 API 获取实时模型列表,并合并本地 MODEL_INFO 元数据。
|
||||
* API 不可用时回退到 supportedModels。
|
||||
*/
|
||||
async listModels(): Promise<MetonaModelInfo[]> {
|
||||
override async listModels(): Promise<MetonaModelInfo[]> {
|
||||
try {
|
||||
const response = await fetch(`${this.config.baseURL}/models`, {
|
||||
headers: { Authorization: `Bearer ${this.config.apiKey}` },
|
||||
@@ -179,36 +115,13 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
return this.supportedModels.map((id) => DeepSeekAdapter.MODEL_INFO[id] ?? { id });
|
||||
}
|
||||
|
||||
/**
|
||||
* H-2 修复: 获取上下文窗口大小(规范要求)
|
||||
*
|
||||
* v0.3.1: 优先使用配置注入的 contextWindow,回退到 MODEL_INFO 默认值。
|
||||
* DeepSeek OpenAI 兼容 API 不支持 context_window 参数,此值仅用于
|
||||
* Engine 压缩判断和前端 UI 显示。
|
||||
*/
|
||||
override getContextWindow(): number {
|
||||
// v0.3.1: 优先使用配置注入的 contextWindow
|
||||
if (typeof this.config.contextWindow === 'number' && this.config.contextWindow > 0) {
|
||||
return this.config.contextWindow;
|
||||
}
|
||||
// 回退到 MODEL_INFO
|
||||
const modelInfo = DeepSeekAdapter.MODEL_INFO[this.config.defaultModel];
|
||||
return modelInfo?.contextWindow ?? 1_000_000;
|
||||
}
|
||||
|
||||
// ===== GET /user/balance =====
|
||||
|
||||
/**
|
||||
* 查询账户余额
|
||||
*
|
||||
* v0.5.2 修复: DeepSeek 官方 API 实际返回 `balance_infos` 数组格式:
|
||||
* { "is_available": true, "balance_infos": [{ "currency": "CNY",
|
||||
* "total_balance": "110.00", "granted_balance": "10.00", "topped_up_balance": "100.00" }] }
|
||||
* 此前按扁平字段解析(data.total_balance)→ 永远取到 undefined → 恒显示 0。
|
||||
* 现优先取 balance_infos[0],回退扁平格式(兼容网关/代理的简化响应)。
|
||||
*
|
||||
* URL 规范化: 余额端点为 {root}/user/balance(无 /v1 前缀)。用户配置的
|
||||
* baseURL 可能带 /v1 或尾斜杠(chat 端点两种写法都合法),此处剥离后拼接。
|
||||
* v0.5.2 修复: 官方 API 返回 balance_infos 数组格式(此前按扁平字段解析恒为 0)。
|
||||
* URL 规范化: 余额端点为 {root}/user/balance(无 /v1 前缀),需剥离配置中的尾斜杠与 /v1。
|
||||
*/
|
||||
async getBalance(): Promise<{
|
||||
currency: string;
|
||||
@@ -217,7 +130,6 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
toppedUpBalance: string;
|
||||
} | null> {
|
||||
try {
|
||||
// 规范化 baseURL:去尾斜杠、去尾 /v1(余额端点在根路径下)
|
||||
const root = this.config.baseURL.replace(/\/+$/, '').replace(/\/v1$/, '');
|
||||
const response = await fetch(`${root}/user/balance`, {
|
||||
headers: { Authorization: `Bearer ${this.config.apiKey}` },
|
||||
@@ -238,7 +150,6 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
granted_balance?: string;
|
||||
topped_up_balance?: string;
|
||||
};
|
||||
// 优先官方 balance_infos 数组,回退扁平格式
|
||||
const info = data.balance_infos?.[0] ?? data;
|
||||
return {
|
||||
currency: info.currency ?? 'CNY',
|
||||
@@ -251,17 +162,9 @@ export class DeepSeekAdapter extends BaseAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 私有方法 ==========
|
||||
// ========== 协议参数映射(DeepSeek 差异点) ==========
|
||||
|
||||
/**
|
||||
* 构建 DeepSeek 原生请求体
|
||||
*
|
||||
* DeepSeek 特有参数:
|
||||
* - thinking: { type: "enabled" } — 启用思考模式
|
||||
* - reasoning_effort — 思考强度映射
|
||||
* - stream_options: { include_usage: true } — 流式返回 usage
|
||||
*/
|
||||
private toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user