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:
@@ -3,26 +3,21 @@
|
||||
*
|
||||
* OpenAI 兼容 API。支持 Tool Calling、Thinking 模式、多模态(图片 — URL + Base64)。
|
||||
*
|
||||
* 独立继承 BaseAdapter,通过 shared/openai-format 和 shared/sse-stream 复用
|
||||
* OpenAI 兼容格式构建和 SSE 流式解析逻辑。不与其他 Provider Adapter 耦合。
|
||||
*
|
||||
* 与 DeepSeek 的差异:
|
||||
* - Thinking 模式使用 chat_template_kwargs(非 thinking 字段)
|
||||
* - 默认 max_tokens 更大(65536 vs 8192)
|
||||
* v0.6.4 P3-1: 继承 OpenAICompatibleAdapter —— 传输/组装/回退链收敛到共享基类,
|
||||
* 本文件只保留 Agnes 差异点:chat_template_kwargs 思考开关(v0.6.4 对称性修复)、
|
||||
* 无条件 includeImages、非流式默认超时 300s。
|
||||
* 注:Agnes API 未提供 /models 端点,listModels 使用基类默认实现。
|
||||
*
|
||||
* @see apis/agnes-ai-api-docs-20260625.html
|
||||
*/
|
||||
|
||||
import { BaseAdapter } from './base-adapter';
|
||||
import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types';
|
||||
import { MetonaFinishReason } from '../types';
|
||||
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 { parseSSEStream, parseOpenAICompatibleResponse } from './shared/sse-stream';
|
||||
import log from 'electron-log';
|
||||
import { OpenAICompatibleAdapter } from './shared/openai-compatible-base';
|
||||
|
||||
export class AgnesAdapter extends BaseAdapter {
|
||||
// H-2 修复: provider → providerId(规范要求)
|
||||
export class AgnesAdapter extends OpenAICompatibleAdapter {
|
||||
override readonly providerId: string = 'agnes';
|
||||
readonly supportedModels = ['agnes-2.0-flash'];
|
||||
readonly supportsToolCalling = true;
|
||||
@@ -41,114 +36,27 @@ export class AgnesAdapter extends BaseAdapter {
|
||||
},
|
||||
};
|
||||
|
||||
// ===== 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 ?? 300_000,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
await this.throwHttpError(response, 'Agnes AI 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,
|
||||
};
|
||||
protected override chatCompletionsUrl(): string {
|
||||
return `${this.config.baseURL}/chat/completions`;
|
||||
}
|
||||
|
||||
// ===== 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, 'Agnes AI stream error');
|
||||
}
|
||||
|
||||
yield* parseSSEStream(
|
||||
// 非空断言:上方 if 已确保 response.body 不为 null
|
||||
response.body!,
|
||||
request.meta.requestId,
|
||||
request.meta.sessionId,
|
||||
request.meta.iteration,
|
||||
);
|
||||
protected override sendTimeoutMs(): number {
|
||||
return 300_000;
|
||||
}
|
||||
|
||||
/**
|
||||
* H-2 修复: 获取上下文窗口大小(规范要求)
|
||||
*
|
||||
* v0.3.1: 优先使用配置注入的 contextWindow,回退到 MODEL_INFO 默认值。
|
||||
* Agnes OpenAI 兼容 API 不支持 context_window 参数,此值仅用于
|
||||
* Engine 压缩判断和前端 UI 显示。
|
||||
* 注意:Agnes API 未提供 /models 端点,listModels 使用基类默认实现。
|
||||
*/
|
||||
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 = AgnesAdapter.MODEL_INFO[this.config.defaultModel];
|
||||
return modelInfo?.contextWindow ?? 1_000_000;
|
||||
protected override modelInfoTable(): Record<string, MetonaModelInfo> {
|
||||
return AgnesAdapter.MODEL_INFO;
|
||||
}
|
||||
|
||||
// ========== 私有方法 ==========
|
||||
protected override providerLabel(): string {
|
||||
return 'Agnes AI';
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 Agnes AI 原生请求体
|
||||
*
|
||||
* Agnes AI 特有参数:
|
||||
* - 多模态图片:user 消息的 images[] → OpenAI content 数组 [{type:"text"}, {type:"image_url"}]
|
||||
* 支持 HTTPS URL 或 base64 Data URI(与 MiMo 一致)
|
||||
* - chat_template_kwargs: { enable_thinking: true } — 启用思考模式(非 thinking 字段)
|
||||
* - 默认 max_tokens: 65536(1M 上下文,65.5K 最大输出)
|
||||
*/
|
||||
private toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
// ========== 协议参数映射(Agnes 差异点) ==========
|
||||
|
||||
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);
|
||||
@@ -182,12 +90,18 @@ export class AgnesAdapter extends BaseAdapter {
|
||||
body.tools = tools;
|
||||
}
|
||||
|
||||
// C-3 修复: Thinking 模式 — Agnes 使用 chat_template_kwargs 而非 thinking
|
||||
// C-3 修复 + v0.6.4 对称性修复: Thinking 模式 — Agnes 使用 chat_template_kwargs
|
||||
// Agnes API 仅支持 enable_thinking: true/false,不支持 effort 级别
|
||||
// thinkingEffort === 'low' 时映射为 false(不启用深度思考),其他级别映射为 true
|
||||
if (request.params.thinkingEnabled) {
|
||||
// thinkingEffort === 'low' 时映射为 false;thinkingEnabled 为 false 或未配置时
|
||||
// 显式发送 enable_thinking:false —— 原实现只在 thinkingEnabled===true 时写该字段,
|
||||
// 若服务端默认开启思考,客户端没有任何路径把它关掉(DeepSeek/MiMo 均显式发送
|
||||
// disabled 保持对称,唯独此处漏了)。
|
||||
{
|
||||
const effort = request.params.thinkingEffort ?? 'high';
|
||||
body.chat_template_kwargs = { enable_thinking: effort !== 'low' };
|
||||
// 未配置 thinkingEnabled 一律显式关闭 —— 与 DeepSeek/MiMo 的"服务端默认开启,
|
||||
// 必须显式发送 disabled"口径对齐,让行为确定性不依赖服务端隐式默认。
|
||||
const wantThinking = request.params.thinkingEnabled === true && effort !== 'low';
|
||||
body.chat_template_kwargs = { enable_thinking: wantThinking };
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
|
||||
Reference in New Issue
Block a user