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,23 @@
|
||||
* OpenAI Chat Completions API(/v1/chat/completions),支持 Tool Calling、
|
||||
* 流式输出、多模态图片、o 系列推理模型的 reasoning_effort 参数。
|
||||
*
|
||||
* 与 DeepSeek 适配器的关键差异:
|
||||
* - o 系列 / gpt-5 系列模型使用 max_completion_tokens(非 max_tokens)
|
||||
* - Thinking 模式通过顶层 reasoning_effort 参数(o 系列模型)
|
||||
* - 模型列表从 /v1/models 动态获取
|
||||
* v0.6.4 P3-1: 继承 OpenAICompatibleAdapter —— 传输/组装/回退链收敛到共享基类,
|
||||
* 本文件只保留 OpenAI 差异点:o 系列/gpt-5 的字段名路由与 reasoning_effort、
|
||||
* 推理模型拒图的前置拦截(升级为 ModelCapabilityError)、动态 /models 列表、
|
||||
* 非推理模型 temperature 控制。
|
||||
*
|
||||
* @see apis 官方文档 https://platform.openai.com/docs/api-reference/chat
|
||||
* @see https://platform.openai.com/docs/api-reference/chat
|
||||
*/
|
||||
|
||||
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 {
|
||||
ModelCapabilityError,
|
||||
OpenAICompatibleAdapter,
|
||||
} from './shared/openai-compatible-base';
|
||||
|
||||
export class OpenAIAdapter extends BaseAdapter {
|
||||
export class OpenAIAdapter extends OpenAICompatibleAdapter {
|
||||
override readonly providerId: string = 'openai';
|
||||
readonly supportedModels = ['gpt-4o', 'gpt-4o-mini', 'gpt-4.1', 'o3-mini'];
|
||||
readonly supportsToolCalling = true;
|
||||
@@ -64,78 +65,27 @@ export class OpenAIAdapter extends BaseAdapter {
|
||||
},
|
||||
};
|
||||
|
||||
// ===== POST /v1/chat/completions(非流式) =====
|
||||
// ===== 共享基类差异声明 =====
|
||||
|
||||
async send(request: MetonaRequest): Promise<MetonaResponse> {
|
||||
const body = this.toNativeRequest(request, false);
|
||||
|
||||
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, 'OpenAI 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 /v1/chat/completions(流式) =====
|
||||
protected override sendTimeoutMs(): number {
|
||||
return 120_000;
|
||||
}
|
||||
|
||||
async *sendStream(request: MetonaRequest): AsyncIterable<MetonaStreamEvent> {
|
||||
const body = this.toNativeRequest(request, true);
|
||||
protected override modelInfoTable(): Record<string, MetonaModelInfo> {
|
||||
return OpenAIAdapter.MODEL_INFO;
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
protected override providerLabel(): string {
|
||||
return 'OpenAI';
|
||||
}
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
await this.throwHttpError(response, 'OpenAI stream error');
|
||||
}
|
||||
|
||||
yield* parseSSEStream(
|
||||
// 非空断言:上方 if 已确保 response.body 不为 null
|
||||
response.body!,
|
||||
request.meta.requestId,
|
||||
request.meta.sessionId,
|
||||
request.meta.iteration,
|
||||
);
|
||||
// v0.6.4: OpenAI 家族兜底窗口为 128K(其余 OpenAI 兼容 Provider 为 1M)
|
||||
protected override defaultContextWindowFallback(): number {
|
||||
return 128_000;
|
||||
}
|
||||
|
||||
// ===== GET /v1/models =====
|
||||
@@ -158,34 +108,20 @@ export class OpenAIAdapter extends BaseAdapter {
|
||||
return this.supportedModels.map((id) => OpenAIAdapter.MODEL_INFO[id] ?? { id });
|
||||
}
|
||||
|
||||
override getContextWindow(): number {
|
||||
if (typeof this.config.contextWindow === 'number' && this.config.contextWindow > 0) {
|
||||
return this.config.contextWindow;
|
||||
}
|
||||
const modelInfo = OpenAIAdapter.MODEL_INFO[this.config.defaultModel];
|
||||
return modelInfo?.contextWindow ?? 128_000;
|
||||
}
|
||||
// ========== 协议参数映射(OpenAI 差异点) ==========
|
||||
|
||||
// ========== 私有方法 ==========
|
||||
|
||||
/**
|
||||
* 构建 OpenAI 原生请求体
|
||||
*
|
||||
* OpenAI 特有处理:
|
||||
* - 多模态图片:user 消息 images[] → content 数组
|
||||
* - o 系列(o1/o3/o4)与 gpt-5 系列使用 max_completion_tokens + reasoning_effort
|
||||
* - 思考模式下 temperature 被部分推理模型拒绝,不传
|
||||
*/
|
||||
private toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
protected override toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
// 推理模型检测(o 系列使用新参数名)
|
||||
const model = this.config.defaultModel;
|
||||
const isReasoningModel = /^(o\d|gpt-5)/.test(model);
|
||||
|
||||
// 推理模型不支持图片输入 — 前置校验(转换在共享层,此处仅拦截)
|
||||
// 推理模型不支持图片输入 — 前置校验
|
||||
// v0.6.4 升级: 原实现抛裸 Error 落入 UNKNOWN 错误码;现在抛 ModelCapabilityError
|
||||
// (携带 status=400),引擎按"不可重试请求级错误"处理,UI 可区分能力限制与一般故障。
|
||||
if (isReasoningModel) {
|
||||
const hasImages = request.messages.some((m) => m.images?.length);
|
||||
if (hasImages) {
|
||||
throw new Error(`Model "${model}" does not support image inputs`);
|
||||
throw new ModelCapabilityError(model, 'image inputs');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,6 +174,8 @@ export class OpenAIAdapter extends BaseAdapter {
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
// 已知边界(协议限制,待上游放开后移除此注释):o 系列不支持 stop 参数,
|
||||
// 当前仍透传 —— 若推理模型 + stop 组合触发 400 属上游约束而非本层缺陷。
|
||||
if (request.params.stopSequences?.length) {
|
||||
body.stop = request.params.stopSequences;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user