feat: v0.8.0 流语义补全 · 会话可靠 · 恢复力 — finish_reason 全链路贯通根治"思考中停止" · 2445 用例全量回归
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 跳过
This commit is contained in:
@@ -238,8 +238,10 @@ describe('DeepSeek vision 模型多模态请求格式(v0.5.4)', () => {
|
||||
} as MetonaRequest);
|
||||
|
||||
const body = requestBody();
|
||||
// vision 模型 supportsThinking=false,但参数仍透传 thinking enabled(引擎决定)
|
||||
expect(body.thinking).toEqual({ type: 'enabled' });
|
||||
// v0.8.0 P0-3 能力门控: vision 模型 supportsThinking=false → 显式 disabled,
|
||||
// 且不发送 reasoning_effort(思考会耗尽该模型 8192 输出预算 —— 生产事故根因)
|
||||
expect(body.thinking).toEqual({ type: 'disabled' });
|
||||
expect(body.reasoning_effort).toBeUndefined();
|
||||
});
|
||||
|
||||
it('vision 模型 messages 数组首位始终为 system 消息', async () => {
|
||||
|
||||
@@ -757,11 +757,14 @@ describe('DeepSeekAdapter — thinking 映射矩阵', () => {
|
||||
expect(bodies[0].reasoning_effort).toBeUndefined();
|
||||
});
|
||||
|
||||
it('thinkingEnabled 未配置 → 不发送 thinking 字段', async () => {
|
||||
it('thinkingEnabled 未配置 → 显式 disabled(v0.8.0 P0-3 确定性契约)', async () => {
|
||||
// v0.8.0 P0-3 契约变更: DeepSeek 服务端默认 thinking.enabled,未配置即发请求
|
||||
// 会得到隐式思考 —— 现在未配置一律显式 disabled,行为不依赖服务端隐式默认
|
||||
//(与 Agnes/MiMo 的显式口径对齐;引擎路径恒传布尔值,此处为兜底确定性)。
|
||||
const adapter = makeAdapter();
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false } }));
|
||||
expect(bodies[0].thinking).toBeUndefined();
|
||||
expect(bodies[0].thinking).toEqual({ type: 'disabled' });
|
||||
});
|
||||
|
||||
it('effort 未配置缺省 high → reasoning_effort=high', async () => {
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
readStreamChunkWithIdleTimeout,
|
||||
SseUpstreamError,
|
||||
} from '../shared/sse-stream';
|
||||
import { MetonaStreamEventType } from '../../types';
|
||||
import { MetonaStreamEventType, type MetonaStreamEvent } from '../../types';
|
||||
|
||||
/** 构造 SSE 测试流 */
|
||||
function makeStream(chunks: string[]): ReadableStream<Uint8Array> {
|
||||
@@ -586,13 +586,33 @@ describe('parseSSEStream — 上游错误帧 status 推断矩阵', () => {
|
||||
expect((err as SseUpstreamError).status).toBe(503);
|
||||
});
|
||||
|
||||
it('error 为空对象 → 回退 message 为 {}(JSON.stringify 兜底使其仍被识别为错误帧)', async () => {
|
||||
// 源码注明的防御意图是无害空对象不报错,但 message 兜底取 JSON.stringify({})='{}',
|
||||
// 恒真值使该防御分支失效 —— 此处锁定实际行为并留档(见报告)。
|
||||
const err = await collectError({ error: {} });
|
||||
it('error 为空对象 → 不构成错误帧(v0.8.0 P1-3.1 防御分支根治后契约)', async () => {
|
||||
// v0.8.0 P1-3.1 根治: 旧实现 message 兜底取 JSON.stringify({})='{}' 恒真值,
|
||||
// `{"error":{}}` 被误判为错误帧抛 SseUpstreamError 并触发不必要的重试。
|
||||
// 新契约:无 message 且无 status/code 标识的空对象为无害帧,静默跳过。
|
||||
const events: MetonaStreamEvent[] = [];
|
||||
let error: Error | null = null;
|
||||
try {
|
||||
for await (const ev of parseSSEStream(
|
||||
makeStream(['data: {"error":{}}\n\n', 'data: [DONE]\n\n']),
|
||||
'r',
|
||||
's',
|
||||
1,
|
||||
)) {
|
||||
events.push(ev);
|
||||
}
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeNull();
|
||||
expect(events.some((ev) => ev.type === MetonaStreamEventType.DONE)).toBe(true);
|
||||
});
|
||||
|
||||
it('error 有 code 无 message → 仍构成错误帧(标识存在,占位文案兜底)', async () => {
|
||||
const err = await collectError({ error: { code: 'rate_limit_exceeded' } });
|
||||
expect(err).toBeInstanceOf(SseUpstreamError);
|
||||
expect((err as Error).message).toContain('upstream_error');
|
||||
expect((err as SseUpstreamError).status).toBeUndefined();
|
||||
expect((err as SseUpstreamError).status).toBe(429);
|
||||
});
|
||||
|
||||
it('error 为空白字符串 → 不算错误帧', async () => {
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* v0.8.0 P0-3: 思考参数 × 模型能力 门控矩阵。
|
||||
*
|
||||
* 根因回顾:MODEL_INFO 标注 supportsThinking:false 的模型(如
|
||||
* deepseek-v4-flash-vision-exp)此前仍被发送 thinking 参数 —— 思考耗尽输出
|
||||
* 预算(8192 上限)导致 finish_reason=length 空回复、会话静默停止。
|
||||
* 本文件钉住四家 Provider 的能力门控行为与输出预算告警前置条件。
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { DeepSeekAdapter } from '../deepseek.adapter';
|
||||
import { AgnesAdapter } from '../agnes-ai.adapter';
|
||||
import { MimoAdapter } from '../mimo.adapter';
|
||||
import { OllamaAdapter } from '../ollama.adapter';
|
||||
import type { MetonaRequest } from '../../types';
|
||||
|
||||
const SYSTEM_PROMPT = {
|
||||
roleDefinition: 'test',
|
||||
outputConstraints: '',
|
||||
safetyGuidelines: '',
|
||||
};
|
||||
|
||||
function makeRequest(overrides?: Partial<MetonaRequest['params']>): MetonaRequest {
|
||||
return {
|
||||
meta: {
|
||||
sessionId: 's',
|
||||
iteration: 1,
|
||||
requestId: 'r',
|
||||
timestamp: Date.now(),
|
||||
agentVersion: '1.0.0',
|
||||
},
|
||||
systemPrompt: SYSTEM_PROMPT,
|
||||
messages: [{ role: 'user', content: 'hi', timestamp: Date.now() }],
|
||||
params: {
|
||||
maxTokens: 63488,
|
||||
temperature: 0,
|
||||
stream: true,
|
||||
thinkingEnabled: true,
|
||||
thinkingEffort: 'max',
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function asNative(
|
||||
adapter: unknown,
|
||||
): (req: MetonaRequest, stream: boolean) => Record<string, unknown> {
|
||||
return (
|
||||
adapter as { toNativeRequest: (r: MetonaRequest, s: boolean) => Record<string, unknown> }
|
||||
).toNativeRequest.bind(adapter);
|
||||
}
|
||||
|
||||
describe('P0-3 thinking capability gate', () => {
|
||||
it('DeepSeek: vision-exp (supportsThinking:false) → thinking disabled, no reasoning_effort, max_tokens clamped to 8192', async () => {
|
||||
const adapter = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
baseURL: 'https://api.deepseek.com',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'deepseek-v4-flash-vision-exp',
|
||||
});
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.thinking).toEqual({ type: 'disabled' });
|
||||
expect(body.reasoning_effort).toBeUndefined();
|
||||
expect(body.max_tokens).toBe(8192);
|
||||
});
|
||||
|
||||
it('DeepSeek: pro (supportsThinking:true) → thinking enabled + reasoning_effort mapped (max→max)', async () => {
|
||||
const adapter = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
baseURL: 'https://api.deepseek.com',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'deepseek-v4-pro',
|
||||
});
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.thinking).toEqual({ type: 'enabled' });
|
||||
expect(body.reasoning_effort).toBe('max');
|
||||
});
|
||||
|
||||
it('Agnes: supportsThinking:false model → enable_thinking:false', async () => {
|
||||
const adapter = new AgnesAdapter({
|
||||
provider: 'agnes',
|
||||
baseURL: 'https://apihub.agnes-ai.com/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'agnes-2.0-flash',
|
||||
});
|
||||
// 临时改写元信息模拟"不支持思考"的模型,结束后恢复
|
||||
const table = AgnesAdapter['MODEL_INFO'] as Record<string, { supportsThinking: boolean }>;
|
||||
const original = table['agnes-2.0-flash'].supportsThinking;
|
||||
table['agnes-2.0-flash'].supportsThinking = false;
|
||||
try {
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.chat_template_kwargs).toEqual({ enable_thinking: false });
|
||||
} finally {
|
||||
table['agnes-2.0-flash'].supportsThinking = original;
|
||||
}
|
||||
});
|
||||
|
||||
it('Agnes: supportsThinking:true model → enable_thinking:true', async () => {
|
||||
const adapter = new AgnesAdapter({
|
||||
provider: 'agnes',
|
||||
baseURL: 'https://apihub.agnes-ai.com/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'agnes-2.0-flash',
|
||||
});
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.chat_template_kwargs).toEqual({ enable_thinking: true });
|
||||
});
|
||||
|
||||
it('MiMo: supportsThinking:false model → thinking disabled + temperature passthrough', async () => {
|
||||
const adapter = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
baseURL: 'https://api.xiaomimimo.com/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'mimo-v2.5-pro',
|
||||
});
|
||||
const table = MimoAdapter['MODEL_INFO'] as Record<string, { supportsThinking: boolean }>;
|
||||
const original = table['mimo-v2.5-pro'].supportsThinking;
|
||||
table['mimo-v2.5-pro'].supportsThinking = false;
|
||||
try {
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.thinking).toEqual({ type: 'disabled' });
|
||||
expect(body.temperature).toBe(0);
|
||||
} finally {
|
||||
table['mimo-v2.5-pro'].supportsThinking = original;
|
||||
}
|
||||
});
|
||||
|
||||
it('Ollama: probed no-thinking (cachedThinkingSupport=false) → no think parameter', async () => {
|
||||
const adapter = new OllamaAdapter({
|
||||
provider: 'ollama',
|
||||
baseURL: 'http://localhost:11434',
|
||||
apiKey: '',
|
||||
defaultModel: 'qwen3:latest',
|
||||
});
|
||||
(adapter as unknown as { cachedThinkingSupport: boolean | null }).cachedThinkingSupport = false;
|
||||
const body = await (
|
||||
adapter as unknown as {
|
||||
toNativeRequest: (r: MetonaRequest) => Promise<Record<string, unknown>>;
|
||||
}
|
||||
).toNativeRequest(makeRequest());
|
||||
expect(body.think).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Ollama: probe unknown (null) fails open → think parameter present', async () => {
|
||||
const adapter = new OllamaAdapter({
|
||||
provider: 'ollama',
|
||||
baseURL: 'http://localhost:11434',
|
||||
apiKey: '',
|
||||
defaultModel: 'qwen3:latest',
|
||||
});
|
||||
(adapter as unknown as { cachedThinkingSupport: boolean | null }).cachedThinkingSupport = null;
|
||||
const body = await (
|
||||
adapter as unknown as {
|
||||
toNativeRequest: (r: MetonaRequest) => Promise<Record<string, unknown>>;
|
||||
}
|
||||
).toNativeRequest(makeRequest());
|
||||
expect(body.think).toBe(true); // effort=max → true
|
||||
});
|
||||
});
|
||||
@@ -56,7 +56,10 @@ export class AgnesAdapter extends OpenAICompatibleAdapter {
|
||||
|
||||
// ========== 协议参数映射(Agnes 差异点) ==========
|
||||
|
||||
protected override toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
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);
|
||||
@@ -96,12 +99,21 @@ export class AgnesAdapter extends OpenAICompatibleAdapter {
|
||||
// 显式发送 enable_thinking:false —— 原实现只在 thinkingEnabled===true 时写该字段,
|
||||
// 若服务端默认开启思考,客户端没有任何路径把它关掉(DeepSeek/MiMo 均显式发送
|
||||
// disabled 保持对称,唯独此处漏了)。
|
||||
// v0.8.0 P0-3: 模型能力门控 —— supportsThinking===false 的模型强制关闭思考
|
||||
{
|
||||
const effort = request.params.thinkingEffort ?? 'high';
|
||||
const modelThinkingSupported =
|
||||
AgnesAdapter.MODEL_INFO[this.config.defaultModel]?.supportsThinking !== false;
|
||||
// 未配置 thinkingEnabled 一律显式关闭 —— 与 DeepSeek/MiMo 的"服务端默认开启,
|
||||
// 必须显式发送 disabled"口径对齐,让行为确定性不依赖服务端隐式默认。
|
||||
const wantThinking = request.params.thinkingEnabled === true && effort !== 'low';
|
||||
const wantThinking =
|
||||
request.params.thinkingEnabled === true && effort !== 'low' && modelThinkingSupported;
|
||||
body.chat_template_kwargs = { enable_thinking: wantThinking };
|
||||
if (request.params.thinkingEnabled === true && !modelThinkingSupported) {
|
||||
log.warn(
|
||||
`[Agnes] model "${this.config.defaultModel}" does not support thinking — sending enable_thinking:false (P0-3 capability gate)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
|
||||
@@ -24,6 +24,28 @@ import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types'
|
||||
import { MetonaFinishReason, MetonaStreamEventType } from '../types';
|
||||
import type { MetonaModelInfo } from '../types/metona-adapter';
|
||||
|
||||
/**
|
||||
* v0.8.0 P0-1: Anthropic stop_reason → 归一化 OpenAI 语义(与 MetonaFinishReason
|
||||
* 的非流式映射语义一致)。pause_turn(长回复暂停续传标记)视为自然停止。
|
||||
*/
|
||||
function mapAnthropicStopReason(reason: string): string {
|
||||
switch (reason) {
|
||||
case 'max_tokens':
|
||||
return 'length';
|
||||
case 'tool_use':
|
||||
return 'tool_calls';
|
||||
case 'refusal':
|
||||
case 'content_filter':
|
||||
return 'content_filter';
|
||||
case 'end_turn':
|
||||
case 'pause_turn':
|
||||
case 'stop_sequence':
|
||||
return 'stop';
|
||||
default:
|
||||
return 'stop';
|
||||
}
|
||||
}
|
||||
|
||||
export class AnthropicAdapter extends BaseAdapter {
|
||||
override readonly providerId: string = 'anthropic';
|
||||
readonly supportedModels = ['claude-sonnet-4-5', 'claude-opus-4-1', 'claude-haiku-4-5'];
|
||||
@@ -116,6 +138,10 @@ export class AnthropicAdapter extends BaseAdapter {
|
||||
let buffer = '';
|
||||
let eventName = '';
|
||||
let streamEndedNormally = false;
|
||||
// v0.8.0 P0-1: 采集 message_delta.delta.stop_reason —— Anthropic 的停止原因
|
||||
// 在 message_delta(而非 message_stop)事件携带;旧实现只读 usage,
|
||||
// max_tokens 截断在流式路径完全不可见(与 OpenAI 共享层 finish_reason 缺口同源)
|
||||
let streamStopReason: string | undefined;
|
||||
|
||||
// 工具调用缓冲:content block index → { id, name, argsBuffer }
|
||||
const toolBlocks = new Map<number, { id: string; name: string; argsBuffer: string }>();
|
||||
@@ -256,11 +282,22 @@ export class AnthropicAdapter extends BaseAdapter {
|
||||
},
|
||||
});
|
||||
}
|
||||
// v0.8.0 P0-1: 采集停止原因(message_delta.delta.stop_reason,可能在
|
||||
// 多个 message_delta 中重复出现,取任一即可;归一化为 OpenAI 语义)
|
||||
const delta = data.delta as Record<string, unknown> | undefined;
|
||||
if (delta && typeof delta.stop_reason === 'string') {
|
||||
streamStopReason = mapAnthropicStopReason(delta.stop_reason);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'message_stop': {
|
||||
streamEndedNormally = true;
|
||||
events.push({ type: MetonaStreamEventType.DONE, ...base() });
|
||||
events.push({
|
||||
type: MetonaStreamEventType.DONE,
|
||||
...base(),
|
||||
// v0.8.0 P0-1: 携带归一化停止原因
|
||||
...(streamStopReason ? { finishReason: streamStopReason } : {}),
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'error': {
|
||||
@@ -362,7 +399,13 @@ export class AnthropicAdapter extends BaseAdapter {
|
||||
log.warn('[Anthropic] Stream ended without message_stop (connection likely dropped)');
|
||||
}
|
||||
toolBlocks.clear();
|
||||
yield { type: MetonaStreamEventType.DONE, ...base() };
|
||||
yield {
|
||||
type: MetonaStreamEventType.DONE,
|
||||
...base(),
|
||||
// v0.8.0 P0-1: 断流合成路径同样携带已观察到的停止原因
|
||||
//(断流时多为 undefined —— 引擎据此走空响应守卫/重试而非误判自然结束)
|
||||
...(streamStopReason ? { finishReason: streamStopReason } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -164,7 +164,10 @@ export class DeepSeekAdapter extends OpenAICompatibleAdapter {
|
||||
|
||||
// ========== 协议参数映射(DeepSeek 差异点) ==========
|
||||
|
||||
protected override 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());
|
||||
@@ -200,10 +203,17 @@ export class DeepSeekAdapter extends OpenAICompatibleAdapter {
|
||||
}
|
||||
|
||||
// Thinking 模式
|
||||
// v0.8.0 P0-3 根治: 模型能力门控 —— MODEL_INFO.supportsThinking === false 的
|
||||
// 模型(如 deepseek-v4-flash-vision-exp)一律不发思考参数并显式 disabled,
|
||||
// 防止思考消耗输出预算(生产事故:vision-exp 8192 输出预算被 max 档思考
|
||||
// 全部烧光 → finish_reason=length 空回复 → 会话静默停止)。
|
||||
const modelThinkingSupported =
|
||||
DeepSeekAdapter.MODEL_INFO[this.config.defaultModel]?.supportsThinking !== false;
|
||||
const wantThinking = request.params.thinkingEnabled === true && modelThinkingSupported;
|
||||
// API 默认 thinking.type = "enabled",必须显式发送 disabled 才能关闭
|
||||
if (request.params.thinkingEnabled === false) {
|
||||
if (!wantThinking) {
|
||||
body.thinking = { type: 'disabled' };
|
||||
} else if (request.params.thinkingEnabled) {
|
||||
} else {
|
||||
body.thinking = { type: 'enabled' };
|
||||
const effortMap: Record<string, string> = {
|
||||
low: 'high',
|
||||
@@ -213,6 +223,13 @@ export class DeepSeekAdapter extends OpenAICompatibleAdapter {
|
||||
};
|
||||
// DeepSeek API 仅支持 high / max 两档,low/medium 映射为 high
|
||||
body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high';
|
||||
// v0.8.0 P0-3: 思考会占用输出预算 —— 钳制后预算过小时显式告警
|
||||
//(思考 token 计入 max_tokens,预算过小会出现"思考耗尽正文为零"截断)
|
||||
if (maxTokens < 8192) {
|
||||
log.warn(
|
||||
`[DeepSeek] thinking enabled with small output budget (${maxTokens} tokens after model clamp) — reasoning may consume the entire budget and truncate the answer`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* @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';
|
||||
@@ -72,7 +73,10 @@ export class MimoAdapter extends OpenAICompatibleAdapter {
|
||||
|
||||
// ========== 协议参数映射(MiMo 差异点) ==========
|
||||
|
||||
protected override toNativeRequest(request: MetonaRequest, stream: boolean): Record<string, unknown> {
|
||||
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);
|
||||
@@ -122,7 +126,12 @@ export class MimoAdapter extends OpenAICompatibleAdapter {
|
||||
|
||||
// Thinking 模式(与 DeepSeek 参数结构一致)
|
||||
// MiMo API 默认 thinking.type = "enabled",必须显式发送 disabled 才能关闭
|
||||
if (request.params.thinkingEnabled === false) {
|
||||
// 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;
|
||||
@@ -131,6 +140,13 @@ export class MimoAdapter extends OpenAICompatibleAdapter {
|
||||
// 启用思考(包括 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`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
|
||||
@@ -192,6 +192,14 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
// 流结束
|
||||
if (chunk.done) {
|
||||
streamEndedNormally = true;
|
||||
// v0.8.0 P0-1: 采集 done_reason —— Ollama 的停止原因在最终 done chunk
|
||||
// 携带;旧实现流式路径完全忽略(length 截断不可见),现归一化后随
|
||||
// DONE 事件上交引擎
|
||||
const doneReason = mapOllamaDoneReason(
|
||||
chunk.done_reason as string | undefined,
|
||||
Array.isArray(chunk.message?.tool_calls) && chunk.message.tool_calls.length > 0,
|
||||
);
|
||||
const finishReason = doneReason === MetonaFinishReason.LENGTH ? 'length' : undefined;
|
||||
// 发送 usage 信息
|
||||
yield {
|
||||
type: MetonaStreamEventType.USAGE,
|
||||
@@ -214,6 +222,9 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
// v0.8.0 P0-1: 仅 length 需要显式上交(stop/tool_calls 语义由
|
||||
// 事件流本身表达;load/unload 属本地引擎状态非响应语义)
|
||||
...(finishReason ? { finishReason } : {}),
|
||||
};
|
||||
return;
|
||||
}
|
||||
@@ -252,6 +263,8 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
format?: string | object;
|
||||
images?: string[];
|
||||
options?: Record<string, unknown>;
|
||||
/** v0.8.0 P1-3.2: 外部取消信号(与固定 300s 超时合并,任一触发即中止) */
|
||||
signal?: AbortSignal;
|
||||
}): Promise<{
|
||||
response: string;
|
||||
thinking?: string;
|
||||
@@ -262,8 +275,13 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
const response = await fetch(`${this.baseURL}/api/generate`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ...params, stream: false }),
|
||||
signal: AbortSignal.timeout(300_000),
|
||||
body: JSON.stringify({ ...params, stream: false, signal: undefined }),
|
||||
// v0.8.0 P1-3.2 根治: 旧实现固定 AbortSignal.timeout(300_000) —— 外部中断
|
||||
// 无法取消该请求(最长 5 分钟资源悬挂)。现用 AbortSignal.any 合并外部
|
||||
// signal 与超时信号,任一触发即中止(Node ≥20.3 / Electron 35 满足)。
|
||||
signal: params.signal
|
||||
? AbortSignal.any([params.signal, AbortSignal.timeout(300_000)])
|
||||
: AbortSignal.timeout(300_000),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`Ollama generate error: ${response.status}`);
|
||||
@@ -290,12 +308,17 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
model: string;
|
||||
input: string | string[];
|
||||
dimensions?: number;
|
||||
/** v0.8.0 P1-3.2: 外部取消信号(与固定 60s 超时合并) */
|
||||
signal?: AbortSignal;
|
||||
}): Promise<{ embeddings: number[][]; totalDuration: number }> {
|
||||
const response = await fetch(`${this.baseURL}/api/embed`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(params),
|
||||
signal: AbortSignal.timeout(60_000),
|
||||
// v0.8.0 P1-3.2: 与 generate 同口径 —— 外部 signal 与超时合并
|
||||
signal: params.signal
|
||||
? AbortSignal.any([params.signal, AbortSignal.timeout(60_000)])
|
||||
: AbortSignal.timeout(60_000),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`Ollama embed error: ${response.status}`);
|
||||
@@ -381,9 +404,18 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
*/
|
||||
private cachedContextWindow: number | null = null;
|
||||
private refreshingContextWindow = false;
|
||||
/** v0.8.0 P0-3: /api/show 探测只发一次(构造函数发起),失败不重试(fail-open) */
|
||||
private probeAttempted = false;
|
||||
/**
|
||||
* v0.8.0 P0-3: /api/show capabilities 探测缓存 —— 模型是否支持思考。
|
||||
* null = 未探测/探测失败(fail-open 放行,与 listModels 能力回退策略一致);
|
||||
* false = 服务端明确不支持 → toNativeRequest 不发 think 参数。
|
||||
*/
|
||||
private cachedThinkingSupport: boolean | null = null;
|
||||
|
||||
private refreshContextWindow(): void {
|
||||
if (this.refreshingContextWindow) return;
|
||||
if (this.refreshingContextWindow || this.probeAttempted) return;
|
||||
this.probeAttempted = true;
|
||||
this.refreshingContextWindow = true;
|
||||
void this.showModel(this.config.defaultModel)
|
||||
.then((info) => {
|
||||
@@ -396,6 +428,10 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
log.info(`[Ollama] Context window (num_ctx) detected: ${value}`);
|
||||
}
|
||||
}
|
||||
// v0.8.0 P0-3: 同一次探测顺带缓存思考能力(供 toNativeRequest 同步门控)
|
||||
if (Array.isArray(info.capabilities)) {
|
||||
this.cachedThinkingSupport = info.capabilities.map(String).includes('thinking');
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
/* 模型探测失败不阻塞对话 */
|
||||
@@ -573,6 +609,9 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
}
|
||||
|
||||
private async toNativeRequest(request: MetonaRequest): Promise<Record<string, unknown>> {
|
||||
// 说明:能力/num_ctx 探测由构造函数 fire-and-forget 发起(probeAttempts 上限
|
||||
// 1 次)—— 此处不再重复探测,避免每次请求都打 /api/show(也保持测试环境的
|
||||
// fetch 捕获不受污染)。探测失败时 cachedThinkingSupport=null → 门控 fail-open。
|
||||
const messages: Record<string, unknown>[] = [
|
||||
{
|
||||
role: 'system',
|
||||
@@ -662,14 +701,32 @@ export class OllamaAdapter extends BaseAdapter {
|
||||
}
|
||||
|
||||
// Thinking 模式
|
||||
// v0.8.0 P0-3: 模型能力门控 —— /api/show capabilities 探测为不支持思考
|
||||
//(cachedThinkingSupport === false)时不发 think 参数(Ollama 服务端默认关闭);
|
||||
// 未探测/探测失败(null)fail-open 放行,与 listModels 能力回退策略一致。
|
||||
// 探测在适配器实例创建时 fire-and-forget 发起(refreshContextWindow)。
|
||||
if (request.params.thinkingEnabled) {
|
||||
const effortMap: Record<string, string | boolean> = {
|
||||
low: 'low',
|
||||
medium: 'medium',
|
||||
high: 'high',
|
||||
max: true,
|
||||
};
|
||||
body.think = effortMap[request.params.thinkingEffort ?? 'high'] ?? true;
|
||||
const modelThinkingSupported = this.cachedThinkingSupport !== false;
|
||||
if (modelThinkingSupported) {
|
||||
const effortMap: Record<string, string | boolean> = {
|
||||
low: 'low',
|
||||
medium: 'medium',
|
||||
high: 'high',
|
||||
max: true,
|
||||
};
|
||||
body.think = effortMap[request.params.thinkingEffort ?? 'high'] ?? true;
|
||||
// v0.8.0 P0-3: 思考占用 num_predict 输出预算 —— 预算过小时显式告警
|
||||
const numPredict = request.params.maxTokens;
|
||||
if (typeof numPredict === 'number' && numPredict < 8192) {
|
||||
log.warn(
|
||||
`[Ollama] thinking enabled with small num_predict budget (${numPredict}) — reasoning may consume the entire budget and truncate the answer`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
log.warn(
|
||||
`[Ollama] model "${this.config.defaultModel}" does not support thinking (per /api/show capabilities) — omitting think parameter (P0-3 capability gate)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return body;
|
||||
|
||||
@@ -12,13 +12,11 @@
|
||||
* @see https://platform.openai.com/docs/api-reference/chat
|
||||
*/
|
||||
|
||||
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 {
|
||||
ModelCapabilityError,
|
||||
OpenAICompatibleAdapter,
|
||||
} from './shared/openai-compatible-base';
|
||||
import { ModelCapabilityError, OpenAICompatibleAdapter } from './shared/openai-compatible-base';
|
||||
|
||||
export class OpenAIAdapter extends OpenAICompatibleAdapter {
|
||||
override readonly providerId: string = 'openai';
|
||||
@@ -110,7 +108,10 @@ export class OpenAIAdapter extends OpenAICompatibleAdapter {
|
||||
|
||||
// ========== 协议参数映射(OpenAI 差异点) ==========
|
||||
|
||||
protected override 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);
|
||||
@@ -168,6 +169,12 @@ export class OpenAIAdapter extends OpenAICompatibleAdapter {
|
||||
max: 'high',
|
||||
};
|
||||
body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high';
|
||||
// v0.8.0 P0-3: 推理 token 计入 max_completion_tokens —— 钳制后预算过小时告警
|
||||
if (oaMaxTokens < 8192) {
|
||||
log.warn(
|
||||
`[OpenAI] reasoning enabled with small output budget (${oaMaxTokens} tokens after model clamp) — reasoning may consume the entire budget and truncate the answer`,
|
||||
);
|
||||
}
|
||||
} else if (!isReasoningModel) {
|
||||
// 非推理模型使用温度控制
|
||||
body.temperature = request.params.temperature;
|
||||
|
||||
@@ -139,17 +139,17 @@ function extractUpstreamErrorFrame(
|
||||
? e.status_code
|
||||
: undefined;
|
||||
const rawCode = typeof e.code === 'string' ? e.code : typeof e.type === 'string' ? e.type : '';
|
||||
const message =
|
||||
typeof e.message === 'string'
|
||||
? e.message
|
||||
: typeof e.msg === 'string'
|
||||
? e.msg
|
||||
: JSON.stringify(errObj);
|
||||
|
||||
// 无消息且无状态码的无害空对象不算错误(防御性)
|
||||
if (!message && rawStatus === undefined && !rawCode) return null;
|
||||
// v0.8.0 P1-3.1 根治: 旧实现 message 兜底取 JSON.stringify(errObj) —— '{}' 恒为
|
||||
// 真值,使本防御分支永不可达,`{"error":{}}` 被误判为错误帧抛 SseUpstreamError
|
||||
// (原行为被 sse-stream.test.ts 锁定留档,本次随修复同步改约)。
|
||||
// 现契约:仅当存在显式 message/msg 或 status/code 标识之一才构成错误帧;
|
||||
// 描述文本只在有标识但无显式文本时由序列化/占位生成。
|
||||
const explicitMessage =
|
||||
typeof e.message === 'string' ? e.message : typeof e.msg === 'string' ? e.msg : '';
|
||||
if (!explicitMessage && rawStatus === undefined && !rawCode) return null;
|
||||
return {
|
||||
message: message || `upstream error (${rawCode || rawStatus})`,
|
||||
message: explicitMessage || `upstream error (${rawCode || rawStatus})`,
|
||||
status: rawStatus,
|
||||
code: rawCode || undefined,
|
||||
};
|
||||
@@ -326,6 +326,10 @@ export async function* parseSSEStream(
|
||||
let buffer = '';
|
||||
// v0.6.3: 是否收到过 [DONE](流断开兜底用)
|
||||
let sawDone = false;
|
||||
// v0.8.0 P0-1: 跟踪 Provider 原生 finish_reason,随 DONE 事件携带给引擎
|
||||
// ('stop' | 'length' | 'tool_calls' | 'content_filter' 等;引擎据此区分
|
||||
// 自然完成与输出上限截断,此前 length 仅落日志、引擎不可见)
|
||||
let lastFinishReason: string | undefined;
|
||||
|
||||
// v0.7.4 P1-2: 流空闲超时 — 服务器保活但不再推送数据(连接挂死)时,
|
||||
// reader.read() 会无限挂起,totalTimeoutMs 只在迭代之间检查,无法兜底。
|
||||
@@ -369,6 +373,8 @@ export async function* parseSSEStream(
|
||||
iteration,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
// v0.8.0 P0-1: 携带 Provider 原生 finish_reason(未见任何帧时 undefined)
|
||||
...(lastFinishReason ? { finishReason: lastFinishReason } : {}),
|
||||
};
|
||||
return;
|
||||
}
|
||||
@@ -480,6 +486,11 @@ export async function* parseSSEStream(
|
||||
|
||||
// 非 [DONE] 但 finish_reason 为 tool_calls 时提前 flush 缓冲区
|
||||
const finishReason = chunk.choices?.[0]?.finish_reason as string | undefined;
|
||||
// v0.8.0 P0-1: 记录最近一次 finish_reason(最终帧通常携带于最后一个
|
||||
// 含 choices 的 chunk),随 DONE 事件上交通知引擎与前端
|
||||
if (finishReason) {
|
||||
lastFinishReason = finishReason;
|
||||
}
|
||||
if (finishReason === 'tool_calls') {
|
||||
// L-4 修复: 使用 flushToolCallBuffer 替代重复的遍历代码
|
||||
yield* flushToolCallBuffer(toolCallsBuffer, requestId, sessionId, iteration, seqRef);
|
||||
@@ -528,6 +539,10 @@ export async function* parseSSEStream(
|
||||
iteration,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
// v0.8.0 P0-1: 断流合成路径同样携带已观察到的 finish_reason
|
||||
//(注意:断流时 finishReason 多为 undefined —— 引擎据此走空响应守卫/重试,
|
||||
// 而不是误判为模型自然说完了)
|
||||
...(lastFinishReason ? { finishReason: lastFinishReason } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user