feat: v0.8.1 记忆深化 · 观测闭环 · 体验收口 — 窗口/输出上限全局单一配置 · 2478 用例全量回归 + E2E 冒烟
硬性契约:删除代码中一切写死的上下文窗口与最大输出上限(含六家模型元信息
钳制与全部兜底值)——唯一合法来源是设置面板「上下文长度」(llm.contextWindow)
与「最大输出上限」(llm.maxTokens),跨 Provider/模型原样透传。
P0 正确性收口:
- 迁移 11/12(SCHEMA_VERSION 5):记忆表 embedding 列 + 分 Provider 窗口键清理
- 记忆生命周期接线:会话终态清理 working memory / episodic 90 天 TTL / access_count 回写
- 回放缓冲模块化 + 会话终态清理(杜绝 4MB/会话内存滞留)
- i18n 收口:主进程 main-locale(zh/en,ui.locale 热切换)+ 渲染层 17 处出层
P1 能力演进:
- 本地向量混合检索:0.6×向量余弦 + 0.4×TF-IDF,Ollama embeddings 首次投产,
存量记忆惰性回填,嵌入不可用自动回退 TF-IDF
- MEMORY.md 维护闭环:固化去重消除截断盲区;两阶段维护(AI 建议 → 用户确认 →
原子改写 + 语义记忆双轨同步 + 审计);>50KB 告警
- 可观测闭环:cacheTokens 引擎→前端透传(Token 面板命中率/成本行)+ 输入框
上下文占用指示条
- MCP Prompts/Resources 对话可用:/mcp:{server}:{prompt} 与 @mcp:{server}:{uri}
P2 体验补全:
- 工具自定义策略(正则白/黑名单 + 频率 + 强制确认,热生效)
- 连续 ≥3 同类工具确认聚合为单弹框
- 会话消息游标分页(首屏 200 条向上翻页)
- 开机自启;Playwright + Electron E2E 冒烟(本地 mock LLM 零外联)
Review 回归修复:MCP 大小写失配 / 分页状态复位 / 清空=未配置语义(Number(null)=0
隐患)/ MEMORY.md 告警位置 / working_memories FK(迁移 13)/ 全局配置层废键清理;
附带根治权限加固启动时序、代理回环放行、safeStorage 降级、悬空 symlink 逃逸。
验证:typecheck/lint 0 问题;test:electron 2478/2478(0 跳过);E2E 2/2;
docs/v0.8.1-迭代实施清单.md 全项留档。
This commit is contained in:
@@ -950,11 +950,12 @@ describe('AnthropicAdapter — getContextWindow / listModels', () => {
|
||||
expect(adapter.getContextWindow()).toBe(50_000);
|
||||
});
|
||||
|
||||
it('未知模型 → 兜底 200K', () => {
|
||||
expect(makeAdapter('claude-unknown').getContextWindow()).toBe(200_000);
|
||||
// v0.8.1: 窗口唯一来源是设置面板 llm.contextWindow,未配置返回 0(无写死兜底)
|
||||
it('未知模型且未配置 → 返回 0(无写死兜底窗口)', () => {
|
||||
expect(makeAdapter('claude-unknown').getContextWindow()).toBe(0);
|
||||
});
|
||||
|
||||
it('listModels 返回本地模型元信息(无网络请求)', async () => {
|
||||
it('listModels 返回本地模型元信息(无网络请求,不含窗口/上限数值)', async () => {
|
||||
const adapter = makeAdapter();
|
||||
const models = await adapter.listModels();
|
||||
expect(models.map((m) => m.id)).toEqual([
|
||||
@@ -962,7 +963,10 @@ describe('AnthropicAdapter — getContextWindow / listModels', () => {
|
||||
'claude-opus-4-1',
|
||||
'claude-haiku-4-5',
|
||||
]);
|
||||
expect(models[0]).toMatchObject({ contextWindow: 200_000, maxOutputTokens: 64_000 });
|
||||
// v0.8.1 硬性契约: 元信息不再承载 contextWindow / maxOutputTokens
|
||||
expect(models[0]).toMatchObject({ supportsThinking: true });
|
||||
expect(models[0].contextWindow).toBeUndefined();
|
||||
expect(models[0].maxOutputTokens).toBeUndefined();
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -130,10 +130,11 @@ describe('BaseAdapter — getContextWindow', () => {
|
||||
expect(adapter.getContextWindow()).toBe(128_000);
|
||||
});
|
||||
|
||||
// v0.7.4 P4-5: 兜底从 1M 降至 128K(未知模型按最保守主流窗口预算,防 413)
|
||||
it('未配置时返回兜底默认值 128K(子类应覆盖真实窗口)', () => {
|
||||
// v0.8.1 硬性契约: 上下文窗口唯一来源是设置面板 llm.contextWindow,
|
||||
// 未配置返回 0(引擎据此跳过压缩预算)—— 任何写死兜底值均已删除
|
||||
it('未配置时返回 0(无任何写死兜底窗口,引擎跳过压缩判定)', () => {
|
||||
const adapter = makeAdapter();
|
||||
expect(adapter.getContextWindow()).toBe(128_000);
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -443,15 +444,15 @@ describe('BaseAdapter — throwHttpError 错误体解析', () => {
|
||||
|
||||
// ===== 追加:getContextWindow / listModels / healthCheck =====
|
||||
|
||||
describe('BaseAdapter — getContextWindow 回退链', () => {
|
||||
it('contextWindow=0 视为未配置(需 >0)', () => {
|
||||
describe('BaseAdapter — getContextWindow 非法值契约', () => {
|
||||
it('contextWindow=0 视为未配置(返回 0,引擎跳过压缩判定)', () => {
|
||||
const adapter = makeAdapter({ contextWindow: 0 });
|
||||
expect(adapter.getContextWindow()).toBe(128_000);
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
|
||||
it('contextWindow 为负数视为未配置', () => {
|
||||
it('contextWindow 为负数视为未配置(返回 0)', () => {
|
||||
const adapter = makeAdapter({ contextWindow: -1 });
|
||||
expect(adapter.getContextWindow()).toBe(128_000);
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ describe('DeepSeek vision 模型多模态请求格式(v0.5.4)', () => {
|
||||
expect(userMsg.content).toBe('这张图片里有什么?');
|
||||
});
|
||||
|
||||
it('vision 模型 max_tokens 钳制到 8192(MODEL_INFO 上限)', async () => {
|
||||
it('vision 模型 max_tokens 原样透传(v0.8.1:8192 钳制已废除)', async () => {
|
||||
mockFetch.mockResolvedValue(okResponse());
|
||||
const adapter = makeAdapter('deepseek-v4-flash-vision-exp');
|
||||
|
||||
@@ -114,7 +114,7 @@ describe('DeepSeek vision 模型多模态请求格式(v0.5.4)', () => {
|
||||
} as MetonaRequest);
|
||||
|
||||
const body = JSON.parse((mockFetch.mock.calls[0] as [string, RequestInit])[1].body as string);
|
||||
expect(body.max_tokens).toBe(8_192);
|
||||
expect(body.max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('vision 模型无图片时不转换(content 保持纯文本)', async () => {
|
||||
@@ -215,7 +215,7 @@ describe('DeepSeek vision 模型多模态请求格式(v0.5.4)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('vision 模型 max_tokens 未配置 → 默认 8192(MODEL_INFO 上限)', async () => {
|
||||
it('vision 模型 max_tokens 未配置 → 不下发该字段(v0.8.1:无写死兜底)', async () => {
|
||||
mockFetch.mockResolvedValue(okResponse());
|
||||
const adapter = makeAdapter('deepseek-v4-flash-vision-exp');
|
||||
|
||||
@@ -225,7 +225,7 @@ describe('DeepSeek vision 模型多模态请求格式(v0.5.4)', () => {
|
||||
} as MetonaRequest);
|
||||
|
||||
const body = requestBody();
|
||||
expect(body.max_tokens).toBe(8_192);
|
||||
expect(body.max_tokens).toBeUndefined();
|
||||
});
|
||||
|
||||
it('vision 模型 thinking 参数显式映射(thinkingEnabled 兼容)', async () => {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* Provider maxTokens 上限钳制契约测试(v0.5.3)
|
||||
* maxTokens 透传契约测试(v0.8.1 硬性契约重写)
|
||||
*
|
||||
* 背景:引擎默认 maxTokens=63488(engine.ts DEFAULT_CONFIG),超过部分模型
|
||||
* 上限时 API 直接 400 —— OpenAI gpt-4o(16384)/gpt-4.1(32768)、Anthropic
|
||||
* opus/haiku(32000)、MiMo standard(32768) 曾不可用。v0.5.3 各 adapter 按
|
||||
* MODEL_INFO.maxOutputTokens 钳制。
|
||||
* 背景:v0.5.3 曾引入"按 MODEL_INFO.maxOutputTokens 钳制"逻辑。v0.8.1 按硬性
|
||||
* 契约废除 —— 设置面板「最大输出上限」(llm.maxTokens)是唯一合法的输出上限
|
||||
* 配置,adapter 对一切 Provider/模型**原样透传** `params.maxTokens`,代码中
|
||||
* 不存在任何写死的输出上限或按模型元信息的钳制行为。
|
||||
*
|
||||
* 测试策略(v0.5.2 教训):mock fetch 记录真实请求体并断言契约 ——
|
||||
* 不 mock adapter 内部方法,验证"发出的 HTTP 请求体"这个最终事实。
|
||||
@@ -33,7 +33,7 @@ afterEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
/** 引擎默认形态的请求(maxTokens=63488,与 engine DEFAULT_CONFIG 一致) */
|
||||
/** 设置面板「最大输出上限」配置生效时的请求形态(llm.maxTokens → params.maxTokens) */
|
||||
function makeRequest(overrides: Partial<MetonaRequest> = {}): MetonaRequest {
|
||||
return {
|
||||
meta: {
|
||||
@@ -79,8 +79,8 @@ function requestBody(): Record<string, unknown> {
|
||||
return JSON.parse(init.body as string) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
it('DeepSeek v4 pro(上限 384K):63488 未超限,原样传递', async () => {
|
||||
describe('maxTokens 原样透传(v0.8.1:设置面板是唯一合法上限配置)', () => {
|
||||
it('DeepSeek:max_tokens 原样透传(不按模型钳制)', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
@@ -92,7 +92,19 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('Agnes flash(上限 65536):63488 未超限,原样传递', async () => {
|
||||
it('DeepSeek:超出旧模型元信息上限的值同样原样透传(钳制已废除)', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
baseURL: 'https://api.deepseek.com',
|
||||
apiKey: 'sk',
|
||||
defaultModel: 'deepseek-v4-flash-vision-exp',
|
||||
});
|
||||
await adapter.send(makeRequest());
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('Agnes:max_tokens 原样透传', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new AgnesAdapter({
|
||||
provider: 'agnes',
|
||||
@@ -104,7 +116,7 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('MiMo standard(上限 32768):钳制到 32768(原为 400 错误场景)', async () => {
|
||||
it('MiMo standard:max_completion_tokens 原样透传(旧 32768 钳制已废除)', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
@@ -113,10 +125,10 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
defaultModel: 'mimo-v2.5',
|
||||
});
|
||||
await adapter.send(makeRequest());
|
||||
expect(requestBody().max_completion_tokens).toBe(32_768);
|
||||
expect(requestBody().max_completion_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('MiMo pro(上限 131072):63488 未超限,原样传递', async () => {
|
||||
it('MiMo pro:max_completion_tokens 原样透传', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
@@ -128,7 +140,7 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
expect(requestBody().max_completion_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('OpenAI gpt-4o(上限 16384):钳制到 16384(原为 400 错误场景)', async () => {
|
||||
it('OpenAI gpt-4o(非推理模型):max_tokens 原样透传(旧 16384 钳制已废除)', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new OpenAIAdapter({
|
||||
provider: 'openai',
|
||||
@@ -137,10 +149,10 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
defaultModel: 'gpt-4o',
|
||||
});
|
||||
await adapter.send(makeRequest());
|
||||
expect(requestBody().max_tokens).toBe(16_384);
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('OpenAI o3-mini(上限 100K):63488 未超限,推理模型字段名正确', async () => {
|
||||
it('OpenAI o3-mini(推理模型):max_completion_tokens 原样透传,字段名路由正确', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const adapter = new OpenAIAdapter({
|
||||
provider: 'openai',
|
||||
@@ -153,7 +165,7 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
expect(requestBody().max_tokens).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Anthropic opus(上限 32000):钳制到 32000(原为 400 错误场景)', async () => {
|
||||
it('Anthropic opus:max_tokens 原样透传(旧 32000 钳制已废除)', async () => {
|
||||
mockFetch.mockResolvedValue(anthropicResponse());
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
@@ -162,10 +174,10 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
defaultModel: 'claude-opus-4-1',
|
||||
});
|
||||
await adapter.send(makeRequest());
|
||||
expect(requestBody().max_tokens).toBe(32_000);
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('Anthropic sonnet(上限 64000):63488 未超限', async () => {
|
||||
it('Anthropic sonnet:max_tokens 原样透传', async () => {
|
||||
mockFetch.mockResolvedValue(anthropicResponse());
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
@@ -177,13 +189,12 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
expect(requestBody().max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('未配置 maxTokens 时各 adapter 使用安全默认值(不超过模型上限)', async () => {
|
||||
it('未配置 maxTokens:不下发任何输出上限字段(由 Provider 服务端默认值决定)', async () => {
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
const request = makeRequest({
|
||||
params: { temperature: 0, stream: false } as MetonaRequest['params'],
|
||||
});
|
||||
|
||||
// MiMo standard + thinking 默认 → 兜底 32768(= 上限,安全)
|
||||
const mimo = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
baseURL: 'https://api.mimo.com/v1',
|
||||
@@ -191,6 +202,33 @@ describe('maxTokens 模型上限钳制(引擎默认 63488 场景)', () => {
|
||||
defaultModel: 'mimo-v2.5',
|
||||
});
|
||||
await mimo.send(request);
|
||||
expect(requestBody().max_completion_tokens).toBe(32_768);
|
||||
expect(requestBody().max_completion_tokens).toBeUndefined();
|
||||
|
||||
const deepseek = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
baseURL: 'https://api.deepseek.com',
|
||||
apiKey: 'sk',
|
||||
defaultModel: 'deepseek-v4-pro',
|
||||
});
|
||||
mockFetch.mockReset();
|
||||
mockFetch.mockResolvedValue(openAIResponse());
|
||||
await deepseek.send(request);
|
||||
expect(requestBody().max_tokens).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Anthropic 未配置 maxTokens + thinking 开启:仅满足协议下限 2048(协议不变量,非上限)', async () => {
|
||||
mockFetch.mockResolvedValue(anthropicResponse());
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'https://api.anthropic.com',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-opus-4-1',
|
||||
});
|
||||
await adapter.send(
|
||||
makeRequest({
|
||||
params: { temperature: 0, stream: false, thinkingEnabled: true } as MetonaRequest['params'],
|
||||
}),
|
||||
);
|
||||
expect(requestBody().max_tokens).toBe(2048);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -452,13 +452,25 @@ describe('OllamaAdapter — 能力探测', () => {
|
||||
expect(caps).toEqual({ supportsTools: true, supportsVision: true, supportsThinking: true });
|
||||
});
|
||||
|
||||
it('capabilities 为空数组(showModel 缺省 [])→ 三能力全 false(非 null)', async () => {
|
||||
// v0.8.1: showModel 保留 undefined 语义 —— 响应缺 capabilities 字段 = 未知 → null
|
||||
//(fail-open),不再把"字段缺失"与"权威空"混同(探测竞态下曾误判不支持思考)
|
||||
it('capabilities 字段缺失 → 返回 null(未知,fail-open)', async () => {
|
||||
const adapter = makeAdapter();
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ parameters: '', template: '' }),
|
||||
} as unknown as Response);
|
||||
expect(await adapter.probeCapabilities('qwen3')).toBeNull();
|
||||
});
|
||||
|
||||
it('capabilities 为显式空数组(服务端权威无能力)→ 三能力全 false', async () => {
|
||||
const adapter = makeAdapter();
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ parameters: '', template: '', capabilities: [] }),
|
||||
} as unknown as Response);
|
||||
expect(await adapter.probeCapabilities('qwen3')).toEqual({
|
||||
supportsTools: false,
|
||||
supportsVision: false,
|
||||
@@ -688,28 +700,33 @@ describe('OllamaAdapter — 图片归一化', () => {
|
||||
|
||||
// ===== getContextWindow =====
|
||||
|
||||
describe('OllamaAdapter — getContextWindow', () => {
|
||||
it('未探测到时返回默认 4096', () => {
|
||||
describe('OllamaAdapter — getContextWindow(v0.8.1:唯一来源是设置面板配置)', () => {
|
||||
it('未配置 contextWindow → 返回 0(无 4096 写死兜底,引擎跳过压缩判定)', () => {
|
||||
const adapter = makeAdapter();
|
||||
expect(adapter.getContextWindow()).toBe(4096);
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
|
||||
it('探测到 num_ctx 后返回实测值(机会主义缓存收敛)', async () => {
|
||||
// 需在构造前就位:构造时的 fire-and-forget 探测消费首个 fetch
|
||||
it('config.contextWindow(llm.contextWindow 注入)→ 返回配置值', () => {
|
||||
const adapter = makeAdapter('qwen3', { contextWindow: 32_768 });
|
||||
expect(adapter.getContextWindow()).toBe(32_768);
|
||||
});
|
||||
|
||||
it('/api/show 探测不再缓存窗口数值(num_ctx 由引擎 contextLength 下发)', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ parameters: 'num_ctx 32768', template: '', capabilities: [] }),
|
||||
} as unknown as Response);
|
||||
const adapter = makeAdapter();
|
||||
// 等待构造时的探测完成并写入缓存
|
||||
await vi.waitFor(() => expect(adapter.getContextWindow()).toBe(32768));
|
||||
// 探测完成(含失败路径)后窗口仍为 0 —— 探测只服务于能力门控
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
|
||||
it('探测失败(网络错误)→ 保持默认 4096', async () => {
|
||||
it('探测失败(网络错误)→ 仍返回配置值/0,不抛错不阻塞', async () => {
|
||||
mockFetch.mockRejectedValue(new Error('down'));
|
||||
const adapter = makeAdapter();
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
expect(adapter.getContextWindow()).toBe(4096);
|
||||
expect(adapter.getContextWindow()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -205,12 +205,12 @@ describe('OpenAIAdapter — 推理模型拒图(ModelCapabilityError)', () =>
|
||||
|
||||
// ===== max_completion_tokens / max_tokens 路由 =====
|
||||
|
||||
describe('OpenAIAdapter — token 参数路由', () => {
|
||||
describe('OpenAIAdapter — token 参数路由(v0.8.1:原样透传,无模型钳制)', () => {
|
||||
it.each([
|
||||
['o3-mini', 63_488, 63_488, 'max_completion_tokens'],
|
||||
['o3-mini', 200_000, 100_000, 'max_completion_tokens'], // 上限 100000
|
||||
['gpt-4o', 63_488, 16_384, 'max_tokens'],
|
||||
['gpt-4.1', 63_488, 32_768, 'max_tokens'],
|
||||
['o3-mini', 200_000, 200_000, 'max_completion_tokens'], // 超过任何旧元信息上限 → 原样
|
||||
['gpt-4o', 63_488, 63_488, 'max_tokens'],
|
||||
['gpt-4.1', 63_488, 63_488, 'max_tokens'],
|
||||
] as const)('%s maxTokens=%d → %s=%d', async (model, requested, expected, field) => {
|
||||
const adapter = makeAdapter(model);
|
||||
mockFetch.mockResolvedValue(okResponse());
|
||||
@@ -224,18 +224,18 @@ describe('OpenAIAdapter — token 参数路由', () => {
|
||||
expect(body[other]).toBeUndefined();
|
||||
});
|
||||
|
||||
it('o3-mini 未配置 maxTokens → 默认 32768(thinking 场景安全值)', async () => {
|
||||
it('o3-mini 未配置 maxTokens → 不下发 max_completion_tokens(无写死兜底)', async () => {
|
||||
const adapter = makeAdapter('o3-mini');
|
||||
mockFetch.mockResolvedValue(okResponse());
|
||||
await adapter.send(makeRequest({ params: { temperature: 0, stream: false } }));
|
||||
expect(lastBody().max_completion_tokens).toBe(32_768);
|
||||
expect(lastBody().max_completion_tokens).toBeUndefined();
|
||||
});
|
||||
|
||||
it('非推理模型未配置 maxTokens → 默认模型上限', async () => {
|
||||
it('非推理模型未配置 maxTokens → 不下发 max_tokens(无写死兜底)', async () => {
|
||||
const adapter = makeAdapter('gpt-4o');
|
||||
mockFetch.mockResolvedValue(okResponse());
|
||||
await adapter.send(makeRequest({ params: { temperature: 0, stream: false } }));
|
||||
expect(lastBody().max_tokens).toBe(16_384);
|
||||
expect(lastBody().max_tokens).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -277,23 +277,17 @@ describe('OpenAIAdapter — temperature 路由', () => {
|
||||
|
||||
// ===== getContextWindow 回退链 =====
|
||||
|
||||
describe('OpenAIAdapter — getContextWindow 回退链', () => {
|
||||
it('gpt-4.1 返回 1M 上下文', () => {
|
||||
expect(makeAdapter('gpt-4.1').getContextWindow()).toBe(1_000_000);
|
||||
});
|
||||
|
||||
it('o3-mini 返回 200K', () => {
|
||||
expect(makeAdapter('o3-mini').getContextWindow()).toBe(200_000);
|
||||
});
|
||||
|
||||
it('未知模型 → 兜底 128K(v0.7.4 P4-5 从 1M 降级)', () => {
|
||||
expect(makeAdapter('unknown-model-x').getContextWindow()).toBe(128_000);
|
||||
});
|
||||
|
||||
it('config.contextWindow 显式配置优先', () => {
|
||||
describe('OpenAIAdapter — getContextWindow(v0.8.1:唯一来源是设置面板配置)', () => {
|
||||
it('config.contextWindow 显式配置(llm.contextWindow 注入)返回配置值', () => {
|
||||
const adapter = makeAdapter('gpt-4o', { contextWindow: 64_000 });
|
||||
expect(adapter.getContextWindow()).toBe(64_000);
|
||||
});
|
||||
|
||||
it('未配置(任意模型,含已知/未知)→ 返回 0(引擎跳过压缩判定,无写死兜底)', () => {
|
||||
expect(makeAdapter('gpt-4.1').getContextWindow()).toBe(0);
|
||||
expect(makeAdapter('o3-mini').getContextWindow()).toBe(0);
|
||||
expect(makeAdapter('unknown-model-x').getContextWindow()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
// ===== listModels =====
|
||||
@@ -303,7 +297,9 @@ describe('OpenAIAdapter — listModels 动态发现与降级', () => {
|
||||
mockFetch.mockResolvedValue(okResponse({ data: [{ id: 'gpt-4o' }, { id: 'custom-model' }] }));
|
||||
const models = await makeAdapter('gpt-4o').listModels();
|
||||
expect(models).toHaveLength(2);
|
||||
expect(models[0]).toMatchObject({ id: 'gpt-4o', contextWindow: 128_000 });
|
||||
// v0.8.1: 元信息不再承载窗口/上限数值
|
||||
expect(models[0]).toMatchObject({ id: 'gpt-4o', name: 'GPT-4o' });
|
||||
expect(models[0].contextWindow).toBeUndefined();
|
||||
expect(models[1]).toEqual({ id: 'custom-model' });
|
||||
// /models 请求头携带 Bearer
|
||||
const [, init] = mockFetch.mock.calls[0] as [string, RequestInit];
|
||||
|
||||
@@ -178,7 +178,7 @@ describe('AnthropicAdapter — 请求体契约', () => {
|
||||
expect(toolResultBlocks[0].tool_use_id).toBe('tc_1');
|
||||
});
|
||||
|
||||
it('A2: max_tokens 按模型上限钳制(63488 → sonnet 64000 / opus 32000)', async () => {
|
||||
it('A2: max_tokens 原样透传(v0.8.1:模型钳制已废除,设置面板是唯一上限来源)', async () => {
|
||||
const sonnet = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
@@ -194,9 +194,9 @@ describe('AnthropicAdapter — 请求体契约', () => {
|
||||
const { bodies } = captureFetch();
|
||||
await sonnet.send(makeRequest());
|
||||
await opus.send(makeRequest());
|
||||
// 引擎默认 63488 低于 sonnet 上限 64000 → 原样保留;opus 上限 32000 → 钳制生效
|
||||
// v0.8.1: 设置面板「最大输出上限」对一切模型原样透传,无任何按模型钳制
|
||||
expect(bodies[0].max_tokens).toBe(63_488);
|
||||
expect(bodies[1].max_tokens).toBe(32_000);
|
||||
expect(bodies[1].max_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('A3: 小 maxTokens 时 thinking budget 不跌破协议下限 1024(v0.6.4 边界加固)', async () => {
|
||||
@@ -516,8 +516,8 @@ describe('AnthropicAdapter — thinking budget 按 effort 映射矩阵', () => {
|
||||
['low', 1024],
|
||||
['medium', 4096],
|
||||
['high', 16384],
|
||||
// max=32768 但 sonnet 的 max_tokens 先钳到 64000 → budget 二次钳到 floor(64000/2)=32000
|
||||
['max', 32000],
|
||||
// v0.8.1: max_tokens 不再按模型钳制(100_000 原样透传)→ budget = min(32768, floor(100000/2)) = 32768
|
||||
['max', 32768],
|
||||
] as const)('effort=%s → budget 为该档值且 < max_tokens', async (effort, expectBudget) => {
|
||||
const adapter = makeAdapter();
|
||||
const { bodies } = captureFetch();
|
||||
@@ -570,13 +570,13 @@ describe('AnthropicAdapter — thinking budget 按 effort 映射矩阵', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('AnthropicAdapter — max_tokens 钳制矩阵', () => {
|
||||
describe('AnthropicAdapter — max_tokens 透传矩阵(v0.8.1 无钳制)', () => {
|
||||
it.each([
|
||||
['claude-sonnet-4-5', 63_488, 63_488], // 引擎默认低于上限 → 原样
|
||||
['claude-sonnet-4-5', 70_000, 64_000], // 超上限 → 钳到 sonnet 64000
|
||||
['claude-opus-4-1', 63_488, 32_000], // opus 上限 32000
|
||||
['claude-haiku-4-5', 63_488, 32_000], // haiku 上限 32000
|
||||
['claude-sonnet-4-5', 500, 500], // 低于上限 → 原样
|
||||
['claude-sonnet-4-5', 63_488, 63_488],
|
||||
['claude-sonnet-4-5', 70_000, 70_000], // 超过任何旧元信息上限 → 原样透传
|
||||
['claude-opus-4-1', 63_488, 63_488],
|
||||
['claude-haiku-4-5', 63_488, 63_488],
|
||||
['claude-sonnet-4-5', 500, 500],
|
||||
])('%s maxTokens=%d → max_tokens=%d', async (model, requested, expected) => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
@@ -795,7 +795,7 @@ describe('DeepSeekAdapter — thinking 映射矩阵', () => {
|
||||
expect(bodies[0].stop).toEqual(['<END>']);
|
||||
});
|
||||
|
||||
it('max_tokens 按模型钳制(pro 384000 / vision 8192)', async () => {
|
||||
it('max_tokens 原样透传(v0.8.1:pro/vision 均不钳制)', async () => {
|
||||
const pro = makeAdapter('deepseek-v4-pro');
|
||||
const vision = makeAdapter('deepseek-v4-flash-vision-exp');
|
||||
const { bodies } = captureFetch();
|
||||
@@ -803,8 +803,8 @@ describe('DeepSeekAdapter — thinking 映射矩阵', () => {
|
||||
await vision.send(
|
||||
makeRequest({ params: { maxTokens: 63_488, temperature: 0, stream: false } }),
|
||||
);
|
||||
expect(bodies[0].max_tokens).toBe(384_000);
|
||||
expect(bodies[1].max_tokens).toBe(8_192);
|
||||
expect(bodies[0].max_tokens).toBe(500_000);
|
||||
expect(bodies[1].max_tokens).toBe(63_488);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -864,8 +864,8 @@ describe('AgnesAdapter — enable_thinking 对称性矩阵', () => {
|
||||
makeRequest({ params: { maxTokens: 70_000, temperature: 0.9, stream: false } }),
|
||||
);
|
||||
expect(bodies[0].temperature).toBe(0.9);
|
||||
// 65536 上限钳制
|
||||
expect(bodies[0].max_tokens).toBe(65_536);
|
||||
// v0.8.1: 原样透传,无 65536 钳制
|
||||
expect(bodies[0].max_tokens).toBe(70_000);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -912,21 +912,21 @@ describe('MimoAdapter — thinking 显式开关', () => {
|
||||
expect(bodies[0].top_p).toBeUndefined();
|
||||
});
|
||||
|
||||
it('max_completion_tokens 钳制矩阵(pro 131072 / standard 32768)', async () => {
|
||||
it('max_completion_tokens 原样透传(v0.8.1:pro/standard 均不钳制)', async () => {
|
||||
const pro = makeAdapter({ defaultModel: 'mimo-v2.5-pro' });
|
||||
const std = makeAdapter({ defaultModel: 'mimo-v2.5' });
|
||||
const { bodies } = captureFetch();
|
||||
await pro.send(makeRequest({ params: { maxTokens: 200_000, temperature: 0, stream: false } }));
|
||||
await std.send(makeRequest({ params: { maxTokens: 63_488, temperature: 0, stream: false } }));
|
||||
expect(bodies[0].max_completion_tokens).toBe(131_072);
|
||||
expect(bodies[1].max_completion_tokens).toBe(32_768);
|
||||
expect(bodies[0].max_completion_tokens).toBe(200_000);
|
||||
expect(bodies[1].max_completion_tokens).toBe(63_488);
|
||||
});
|
||||
|
||||
it('thinking 未关闭时未配置 maxTokens → 兜底 32768(思考占配额,防截断)', async () => {
|
||||
it('thinking 未关闭时未配置 maxTokens → 不下发该字段(v0.8.1:无写死兜底值)', async () => {
|
||||
const pro = makeAdapter({ defaultModel: 'mimo-v2.5-pro' });
|
||||
const { bodies } = captureFetch();
|
||||
await pro.send(makeRequest({ params: { temperature: 0, stream: false } }));
|
||||
expect(bodies[0].max_completion_tokens).toBe(32_768);
|
||||
expect(bodies[0].max_completion_tokens).toBeUndefined();
|
||||
});
|
||||
|
||||
it('enableWebSearch 且存在客户端 tools → web_search 服务端工具追加(不覆盖客户端工具)', async () => {
|
||||
@@ -1029,27 +1029,36 @@ describe('OpenAIAdapter — 推理模型字段路由(v0.6.4 P3-1)', () => {
|
||||
expect(bodies[0].reasoning_effort).toBeUndefined();
|
||||
});
|
||||
|
||||
it('gpt-4.1 长上下文 1M → getContextWindow 返回 1M(模型元信息表)', () => {
|
||||
const adapter = makeAdapter('gpt-4.1');
|
||||
it('gpt-4.1 → getContextWindow 返回设置面板配置值(v0.8.1:元信息不再承载窗口)', () => {
|
||||
const adapter = new OpenAIAdapter({
|
||||
provider: 'openai',
|
||||
baseURL: 'http://o.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'gpt-4.1',
|
||||
contextWindow: 1_000_000,
|
||||
});
|
||||
expect(adapter.getContextWindow()).toBe(1_000_000);
|
||||
// 未配置 → 0(引擎跳过压缩判定),无任何写死兜底
|
||||
const noCfg = makeAdapter('gpt-4.1');
|
||||
expect(noCfg.getContextWindow()).toBe(0);
|
||||
});
|
||||
|
||||
it('o3-mini max_completion_tokens 钳制到 100000', async () => {
|
||||
it('o3-mini max_completion_tokens 原样透传(v0.8.1:无 100000 钳制)', async () => {
|
||||
const adapter = makeAdapter('o3-mini');
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({ params: { maxTokens: 200_000, temperature: 0, stream: false } }),
|
||||
);
|
||||
expect(bodies[0].max_completion_tokens).toBe(100_000);
|
||||
expect(bodies[0].max_completion_tokens).toBe(200_000);
|
||||
});
|
||||
|
||||
it('gpt-4o max_tokens 钳制到 16384', async () => {
|
||||
it('gpt-4o max_tokens 原样透传(v0.8.1:无 16384 钳制)', async () => {
|
||||
const adapter = makeAdapter('gpt-4o');
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({ params: { maxTokens: 63_488, temperature: 0, stream: false } }),
|
||||
);
|
||||
expect(bodies[0].max_tokens).toBe(16_384);
|
||||
expect(bodies[0].max_tokens).toBe(63_488);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1255,16 +1264,16 @@ describe('OllamaAdapter — options 缺省与工具定义', () => {
|
||||
|
||||
// ===== 跨 Provider maxTokens 钳制矩阵 =====
|
||||
|
||||
describe('跨 Provider — maxTokens 钳制矩阵汇总', () => {
|
||||
describe('跨 Provider — maxTokens 透传矩阵汇总(v0.8.1 无钳制)', () => {
|
||||
it.each([
|
||||
['anthropic', 'claude-opus-4-1', 100_000, 32_000],
|
||||
['anthropic', 'claude-sonnet-4-5', 100_000, 64_000],
|
||||
['deepseek', 'deepseek-v4-flash-vision-exp', 100_000, 8_192],
|
||||
['agnes', 'agnes-2.0-flash', 100_000, 65_536],
|
||||
['mimo', 'mimo-v2.5', 100_000, 32_768],
|
||||
['openai', 'gpt-4o', 100_000, 16_384],
|
||||
['anthropic', 'claude-opus-4-1', 100_000, 100_000],
|
||||
['anthropic', 'claude-sonnet-4-5', 100_000, 100_000],
|
||||
['deepseek', 'deepseek-v4-flash-vision-exp', 100_000, 100_000],
|
||||
['agnes', 'agnes-2.0-flash', 100_000, 100_000],
|
||||
['mimo', 'mimo-v2.5', 100_000, 100_000],
|
||||
['openai', 'gpt-4o', 100_000, 100_000],
|
||||
] as const)(
|
||||
'%s %s maxTokens=100000 → 钳制为 %d',
|
||||
'%s %s maxTokens=100000 → 原样透传 %d',
|
||||
async (provider, model, requested, expected) => {
|
||||
const adapterMap: Record<string, unknown> = {
|
||||
anthropic: new AnthropicAdapter({
|
||||
|
||||
@@ -56,7 +56,7 @@ function asNative(
|
||||
}
|
||||
|
||||
describe('P0-3 修订: 用户思考意图优先于模型元信息', () => {
|
||||
it('DeepSeek: vision-exp(元信息 false)+ 用户开启思考 → 照发 enabled + reasoning_effort,max_tokens 仍按模型钳制 8192', async () => {
|
||||
it('DeepSeek: vision-exp(元信息 false)+ 用户开启思考 → 照发 enabled + reasoning_effort,max_tokens 原样透传(v0.8.1 无钳制)', async () => {
|
||||
const adapter = new DeepSeekAdapter({
|
||||
provider: 'deepseek',
|
||||
baseURL: 'https://api.deepseek.com',
|
||||
@@ -66,7 +66,7 @@ describe('P0-3 修订: 用户思考意图优先于模型元信息', () => {
|
||||
const body = asNative(adapter)(makeRequest(), false);
|
||||
expect(body.thinking).toEqual({ type: 'enabled' });
|
||||
expect(body.reasoning_effort).toBe('max');
|
||||
expect(body.max_tokens).toBe(8192);
|
||||
expect(body.max_tokens).toBe(63488);
|
||||
});
|
||||
|
||||
it('DeepSeek: 用户关闭思考 → 显式 disabled', async () => {
|
||||
|
||||
Reference in New Issue
Block a user