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:
@@ -1,12 +1,18 @@
|
||||
/**
|
||||
* BaseAdapter 单元测试(v0.4.1 测试补齐)
|
||||
* 覆盖:错误映射(mapError)、HTTP 错误识别(throwHttpError)、
|
||||
* ContentFilterError、上下文窗口读取、fetchWithTimeout 超时与清理
|
||||
* BaseAdapter 单元测试(v0.6.4 P3-2 错误分类单轨化后重写)
|
||||
*
|
||||
* 契约变更说明:
|
||||
* - mapError 已删除(生产路径死代码,与 engine.isRetryableError 双轨漂移)。
|
||||
* 错误分类的唯一事实来源是 engine.isRetryableError —— 本文件改为验证
|
||||
* "BaseAdapter 抛出的错误携带可判定字段"的形状契约:
|
||||
* throwHttpError → error.status;fetchWithTimeout 超时 → code='ETIMEDOUT'
|
||||
* + 'timed out' message(命中引擎网络超时分支)。
|
||||
* - 新增:错误体长度截断、外部 abort 与自身超时的区分。
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
import { BaseAdapter, ContentFilterError } from '../base-adapter';
|
||||
import { MetonaErrorCode, MetonaStreamEventType } from '../../types';
|
||||
import { MetonaStreamEventType } from '../../types';
|
||||
import type {
|
||||
IMetonaProviderAdapter,
|
||||
AdapterConfig,
|
||||
@@ -30,11 +36,6 @@ class TestAdapter extends BaseAdapter {
|
||||
// 空实现
|
||||
}
|
||||
|
||||
/** 测试辅助: 暴露 protected mapError */
|
||||
mapErrorPublic(error: unknown) {
|
||||
return this.mapError(error);
|
||||
}
|
||||
|
||||
/** 测试辅助: 暴露 protected throwHttpError */
|
||||
async throwHttpErrorPublic(response: Response, context: string) {
|
||||
return this.throwHttpError(response, context);
|
||||
@@ -56,62 +57,42 @@ function makeAdapter(config: Partial<AdapterConfig> = {}): TestAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
describe('BaseAdapter — mapError 错误映射', () => {
|
||||
it('timeout 消息映射为 NETWORK_TIMEOUT 且可重试', () => {
|
||||
const adapter = makeAdapter();
|
||||
const err = adapter.mapErrorPublic(new Error('Request timeout after 30s'));
|
||||
expect(err.code).toBe(MetonaErrorCode.NETWORK_TIMEOUT);
|
||||
expect(err.retryable).toBe(true);
|
||||
expect(err.provider).toBe('test');
|
||||
});
|
||||
// ===== 错误形状契约(engine.isRetryableError 的输入保证) =====
|
||||
|
||||
it('ECONNREFUSED 映射为 NETWORK_ERROR 且可重试', () => {
|
||||
const adapter = makeAdapter();
|
||||
const err = adapter.mapErrorPublic(new Error('fetch failed: ECONNREFUSED 127.0.0.1:11434'));
|
||||
expect(err.code).toBe(MetonaErrorCode.NETWORK_ERROR);
|
||||
expect(err.retryable).toBe(true);
|
||||
});
|
||||
describe('BaseAdapter — 抛出错误的可判定形状(单轨化契约)', () => {
|
||||
/** 与 engine.isRetryableError 相同的判定逻辑(镜像断言用) */
|
||||
const isRetryableShape = (err: unknown): boolean => {
|
||||
const e = err as { status?: number; code?: string; message?: string };
|
||||
if (e.status === 429) return true;
|
||||
if (e.status && e.status >= 500 && e.status < 600) return true;
|
||||
if (e.code === 'ECONNRESET' || e.code === 'ETIMEDOUT' || e.code === 'ENOTFOUND') return true;
|
||||
const msg = e.message?.toLowerCase() ?? '';
|
||||
if (msg.includes('aborted') || msg.includes('socket hang up')) return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
it('HTTP 401 优先按 status code 映射为 AUTH_INVALID 且不可重试', () => {
|
||||
const adapter = makeAdapter();
|
||||
const e = new Error('API error: 401 Unauthorized');
|
||||
(e as Error & { status: number }).status = 401;
|
||||
const err = adapter.mapErrorPublic(e);
|
||||
expect(err.code).toBe(MetonaErrorCode.AUTH_INVALID);
|
||||
expect(err.retryable).toBe(false);
|
||||
});
|
||||
|
||||
it('HTTP 429 映射为 RATE_LIMITED 且可重试', () => {
|
||||
const adapter = makeAdapter();
|
||||
const e = new Error('429 Too Many Requests');
|
||||
(e as Error & { status: number }).status = 429;
|
||||
const err = adapter.mapErrorPublic(e);
|
||||
expect(err.code).toBe(MetonaErrorCode.RATE_LIMITED);
|
||||
expect(err.retryable).toBe(true);
|
||||
expect(err.retryAfterMs).toBe(5000);
|
||||
});
|
||||
|
||||
it('ContentFilterError 优先映射为 CONTENT_FILTERED', () => {
|
||||
const adapter = makeAdapter();
|
||||
const cf = new ContentFilterError('high risk content', 'MiMo');
|
||||
const err = adapter.mapErrorPublic(cf);
|
||||
expect(err.code).toBe(MetonaErrorCode.CONTENT_FILTERED);
|
||||
expect(err.retryable).toBe(false);
|
||||
});
|
||||
|
||||
it('普通 Error 映射为 UNKNOWN 且不可重试', () => {
|
||||
const adapter = makeAdapter();
|
||||
const err = adapter.mapErrorPublic(new Error('whatever'));
|
||||
expect(err.code).toBe(MetonaErrorCode.UNKNOWN);
|
||||
expect(err.retryable).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BaseAdapter — throwHttpError', () => {
|
||||
function makeResponse(status: number, body: string): Response {
|
||||
return new Response(body, { status, statusText: 'Status' });
|
||||
}
|
||||
|
||||
it('throwHttpError 携带 status;429/5xx 形状可重试,4xx 不可', async () => {
|
||||
const adapter = makeAdapter();
|
||||
try {
|
||||
await adapter.throwHttpErrorPublic(makeResponse(429, 'rate limited'), 'T');
|
||||
expect.fail('should throw');
|
||||
} catch (e) {
|
||||
expect((e as Error & { status?: number }).status).toBe(429);
|
||||
expect(isRetryableShape(e)).toBe(true);
|
||||
}
|
||||
try {
|
||||
await adapter.throwHttpErrorPublic(makeResponse(401, ''), 'T');
|
||||
expect.fail('should throw');
|
||||
} catch (e) {
|
||||
expect((e as Error & { status?: number }).status).toBe(401);
|
||||
expect(isRetryableShape(e)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('content_filter 错误体抛出 ContentFilterError(含 status)', async () => {
|
||||
const adapter = makeAdapter();
|
||||
const body = JSON.stringify({ error: { code: 'content_filter', message: 'high risk' } });
|
||||
@@ -126,16 +107,20 @@ describe('BaseAdapter — throwHttpError', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('普通错误体抛出带 status 属性的 Error(供 isRetryableError 判断)', async () => {
|
||||
it('巨大 HTML 错误体在消息中被截断(v0.6.4)', async () => {
|
||||
const adapter = makeAdapter();
|
||||
await expect(
|
||||
adapter.throwHttpErrorPublic(makeResponse(503, 'Service Unavailable'), 'DeepSeek'),
|
||||
).rejects.toThrow('DeepSeek: 503');
|
||||
const bigHtml = `<html>${'x'.repeat(100_000)}</html>`;
|
||||
let caught: unknown;
|
||||
try {
|
||||
await adapter.throwHttpErrorPublic(makeResponse(503, ''), 'DeepSeek');
|
||||
await adapter.throwHttpErrorPublic(makeResponse(502, bigHtml), 'GW');
|
||||
expect.fail('should throw');
|
||||
} catch (e) {
|
||||
expect((e as Error & { status: number }).status).toBe(503);
|
||||
caught = e;
|
||||
}
|
||||
expect((caught as Error).message.length).toBeLessThan(1_000);
|
||||
expect((caught as Error).message).toContain('[truncated');
|
||||
// 截断不影响 status 判定
|
||||
expect((caught as Error & { status?: number }).status).toBe(502);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -145,7 +130,7 @@ describe('BaseAdapter — getContextWindow', () => {
|
||||
expect(adapter.getContextWindow()).toBe(128_000);
|
||||
});
|
||||
|
||||
it('未配置时返回保守默认值 1M', () => {
|
||||
it('未配置时返回兜底默认值 1M(子类应覆盖真实窗口)', () => {
|
||||
const adapter = makeAdapter();
|
||||
expect(adapter.getContextWindow()).toBe(1_000_000);
|
||||
});
|
||||
@@ -164,10 +149,11 @@ describe('BaseAdapter — listModels / healthCheck', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('BaseAdapter — fetchWithTimeout', () => {
|
||||
describe('BaseAdapter — fetchWithTimeout(P3-2 超时分类单轨化)', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('正常请求返回 Response 并清理 timer', async () => {
|
||||
@@ -181,27 +167,32 @@ describe('BaseAdapter — fetchWithTimeout', () => {
|
||||
expect(fetchMock).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('超时后 abort 请求(AbortError)', async () => {
|
||||
it('自身超时 → 显式 ETIMEDOUT + timed out 消息(命中引擎网络超时可重试分支)', async () => {
|
||||
const adapter = makeAdapter();
|
||||
vi.useFakeTimers();
|
||||
const fetchMock = vi.fn(
|
||||
(_url: string, init: RequestInit) =>
|
||||
new Promise<Response>((_resolve, reject) => {
|
||||
init.signal?.addEventListener('abort', () =>
|
||||
reject(new DOMException('Aborted', 'AbortError')),
|
||||
reject(new DOMException('This operation was aborted', 'AbortError')),
|
||||
);
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const promise = adapter.fetchWithTimeoutPublic('https://api.test.com/v1/chat', {}, 100);
|
||||
const expectation = expect(promise).rejects.toThrow('Aborted');
|
||||
const expectation = expect(promise).rejects.toSatisfy((e: Error & { code?: string }) => {
|
||||
expect(e.code).toBe('ETIMEDOUT');
|
||||
expect(e.message).toContain('timed out after 100ms');
|
||||
// 关键:消息不再是裸 "Aborted" —— 引擎按网络超时(而非碰巧可重试)分类
|
||||
return true;
|
||||
});
|
||||
vi.advanceTimersByTime(150);
|
||||
await expectation;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('外部 abort 信号触发请求中断', async () => {
|
||||
it('外部 abort(用户中断)→ 原样 AbortError,不被改写为超时', async () => {
|
||||
const adapter = makeAdapter();
|
||||
const controller = new AbortController();
|
||||
adapter.setAbortSignal(controller.signal);
|
||||
@@ -210,16 +201,20 @@ describe('BaseAdapter — fetchWithTimeout', () => {
|
||||
(_url: string, init: RequestInit) =>
|
||||
new Promise<Response>((_resolve, reject) => {
|
||||
init.signal?.addEventListener('abort', () =>
|
||||
reject(new DOMException('Aborted', 'AbortError')),
|
||||
reject(new DOMException('This operation was aborted', 'AbortError')),
|
||||
);
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const promise = adapter.fetchWithTimeoutPublic('https://api.test.com/v1/chat', {}, 30_000);
|
||||
const expectation = expect(promise).rejects.toThrow('Aborted');
|
||||
controller.abort();
|
||||
await expectation;
|
||||
await promise.catch((e: Error & { code?: string }) => {
|
||||
expect(e.name).toBe('AbortError');
|
||||
// 未被转译为字符串型 ETIMEDOUT(注意 Node DOMException 自带数字 code=20)
|
||||
expect(e.code).not.toBe('ETIMEDOUT');
|
||||
expect(e.message).not.toContain('timed out after');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,338 @@
|
||||
/**
|
||||
* Provider 请求形态测试矩阵(v0.6.4 P2-6)
|
||||
*
|
||||
* 此前 ollama(599 行)/ anthropic(532 行)两个最复杂的适配器零测试 —— 恰好也是
|
||||
* 本轮审计中缺陷密度最高的文件。本文件通过 mock fetch 记录真实请求体,
|
||||
* 锁定以下契约:
|
||||
*
|
||||
* Anthropic:
|
||||
* A1 消息转换(system 顶层 / user-assistant-tool 三角色映射 / 孤立 tool_result 过滤)
|
||||
* A2 max_tokens 按模型钳制(引擎默认 63488 → sonnet 64000 / opus 32000)
|
||||
* A3 thinking 预算下限保护(小 maxTokens 场景 budget≥1024 且 < max_tokens,此前 API 400)
|
||||
* A4 thinking 开启时不传 temperature;关闭时显式传递
|
||||
*
|
||||
* Ollama:
|
||||
* O1 options 映射(num_predict=numTokens、num_ctx=contextLength、stop、top_p)
|
||||
* O2 think 参数 effort 映射(low→"low"、max→true)与未配置时缺省
|
||||
* O3 图片归一化(data URI 剥前缀;无 URL 触发下载分支时零网络请求)
|
||||
*
|
||||
* Agnes:
|
||||
* G1 思考模式对称性 —— thinkingEnabled=false 必须显式发送 enable_thinking:false
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
vi.mock('electron-log', () => ({
|
||||
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||||
}));
|
||||
|
||||
import { AnthropicAdapter } from '../anthropic.adapter';
|
||||
import { OllamaAdapter } from '../ollama.adapter';
|
||||
import { MimoAdapter } from '../mimo.adapter';
|
||||
import { AgnesAdapter } from '../agnes-ai.adapter';
|
||||
import type { MetonaRequest } from '../../types';
|
||||
|
||||
/** 安装全局 fetch 捕获器:记录每次请求体并返回一个三家协议都能解析的合成响应 */
|
||||
function captureFetch(): { bodies: Array<Record<string, unknown>> } {
|
||||
const bodies: Array<Record<string, unknown>> = [];
|
||||
// 兼容三家的非流式解析所需的最小字段集:
|
||||
// OpenAI 兼容(agnes): choices[].message/finish_reason;Anthropic: content[]/usage/stop_reason;
|
||||
// Ollama: message/done/prompt_eval_count/eval_count
|
||||
const genericBody = {
|
||||
id: 'cmpl-test',
|
||||
object: 'chat.completion',
|
||||
created: Date.now(),
|
||||
model: 'test-model',
|
||||
choices: [{ index: 0, message: { role: 'assistant', content: 'ok' }, finish_reason: 'stop' }],
|
||||
content: [],
|
||||
usage: {
|
||||
prompt_tokens: 3,
|
||||
completion_tokens: 2,
|
||||
total_tokens: 5,
|
||||
input_tokens: 3,
|
||||
output_tokens: 2,
|
||||
prompt_eval_count: 3,
|
||||
eval_count: 2,
|
||||
},
|
||||
stop_reason: 'end_turn',
|
||||
message: { role: 'assistant', content: 'ok' },
|
||||
done: true,
|
||||
};
|
||||
const fetchMock = vi.fn(async (_url: string | URL, init?: RequestInit) => {
|
||||
bodies.push(JSON.parse(String(init?.body ?? '{}')) as Record<string, unknown>);
|
||||
return new Response(JSON.stringify(genericBody), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
return { bodies };
|
||||
}
|
||||
|
||||
function makeRequest(overrides?: Partial<MetonaRequest>): MetonaRequest {
|
||||
return {
|
||||
meta: {
|
||||
sessionId: 's1',
|
||||
iteration: 1,
|
||||
requestId: 'r1',
|
||||
timestamp: Date.now(),
|
||||
agentVersion: 'test',
|
||||
},
|
||||
systemPrompt: {
|
||||
roleDefinition: 'You are Metona.',
|
||||
outputConstraints: 'Be concise.',
|
||||
safetyGuidelines: 'Stay safe.',
|
||||
dynamicReminders: '',
|
||||
},
|
||||
messages: [{ role: 'user', content: 'hi', timestamp: Date.now() }],
|
||||
params: { maxTokens: 63_488, temperature: 0, stream: false },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ===== Anthropic =====
|
||||
|
||||
describe('AnthropicAdapter — 请求体契约', () => {
|
||||
it('A1: system 拼为顶层字段;tool 结果映射为 user 角色 tool_result 块', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({
|
||||
messages: [
|
||||
{ role: 'user', content: 'read it', timestamp: Date.now() },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: null,
|
||||
toolCalls: [
|
||||
{ id: 'tc_1', name: 'read_file', args: { path: 'a.txt' }, iteration: 1, timestamp: Date.now() },
|
||||
],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
{ role: 'tool', content: null, toolResult: { toolCallId: 'tc_1', toolName: 'read_file', result: 'data', success: true, durationMs: 1, timestamp: Date.now() }, timestamp: Date.now() },
|
||||
// 孤立 tool_result(前面没有对应 tool_use)应被过滤
|
||||
{ role: 'tool', content: null, toolResult: { toolCallId: 'tc_orphan', toolName: 'x', result: '', success: true, durationMs: 1, timestamp: Date.now() }, timestamp: Date.now() },
|
||||
{ role: 'user', content: 'next?', timestamp: Date.now() },
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
const body = bodies[0];
|
||||
expect(body.system).toContain('You are Metona.');
|
||||
expect(Array.isArray(body.messages)).toBe(true);
|
||||
const msgs = body.messages as Array<{ role: string; content: Array<Record<string, unknown>> }>;
|
||||
// tool_use 的 assistant 消息存在且携带 id/name
|
||||
const assistantToolMsg = msgs.find((m) => m.role === 'assistant');
|
||||
expect(assistantToolMsg?.content[0]).toMatchObject({ type: 'tool_use', id: 'tc_1', name: 'read_file' });
|
||||
// tool 结果以 user 角色 tool_result 形态出现且配对 id 正确;孤立者被丢弃
|
||||
const toolResultBlocks = msgs.flatMap((m) =>
|
||||
m.content.filter((c) => c.type === 'tool_result'),
|
||||
);
|
||||
expect(toolResultBlocks).toHaveLength(1);
|
||||
expect(toolResultBlocks[0].tool_use_id).toBe('tc_1');
|
||||
});
|
||||
|
||||
it('A2: max_tokens 按模型上限钳制(63488 → sonnet 64000 / opus 32000)', async () => {
|
||||
const sonnet = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
const opus = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-opus-4-1',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await sonnet.send(makeRequest());
|
||||
await opus.send(makeRequest());
|
||||
// 引擎默认 63488 低于 sonnet 上限 64000 → 原样保留;opus 上限 32000 → 钳制生效
|
||||
expect(bodies[0].max_tokens).toBe(63_488);
|
||||
expect(bodies[1].max_tokens).toBe(32_000);
|
||||
});
|
||||
|
||||
it('A3: 小 maxTokens 时 thinking budget 不跌破协议下限 1024(v0.6.4 边界加固)', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-haiku-4-5',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({ params: { maxTokens: 1500, temperature: 0, stream: false, thinkingEnabled: true, thinkingEffort: 'low' } }),
|
||||
);
|
||||
const body = bodies[0];
|
||||
const thinking = body.thinking as { type: string; budget_tokens: number };
|
||||
// max_tokens 被抬升到安全下限,budget 落在 [1024, max_tokens/2] 区间内
|
||||
expect(body.max_tokens as number).toBeGreaterThanOrEqual(2048);
|
||||
expect(thinking.budget_tokens).toBeGreaterThanOrEqual(1024);
|
||||
expect(thinking.budget_tokens).toBeLessThanOrEqual((body.max_tokens as number) / 2);
|
||||
});
|
||||
|
||||
it('A4: thinking 开启不传 temperature;关闭时显式传递', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://a.test',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({ params: { maxTokens: 4096, temperature: 0.7, stream: false, thinkingEnabled: true } }),
|
||||
);
|
||||
expect(bodies[0].temperature).toBeUndefined();
|
||||
expect(bodies[0].thinking).toBeDefined();
|
||||
|
||||
await adapter.send(
|
||||
makeRequest({ params: { maxTokens: 4096, temperature: 0.7, stream: false, thinkingEnabled: false } }),
|
||||
);
|
||||
expect(bodies[1].temperature).toBe(0.7);
|
||||
expect(bodies[1].thinking).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ===== Ollama =====
|
||||
|
||||
describe('OllamaAdapter — 请求体契约', () => {
|
||||
function makeOllama(): OllamaAdapter {
|
||||
return new OllamaAdapter({
|
||||
provider: 'ollama',
|
||||
baseURL: 'http://localhost:11434',
|
||||
defaultModel: 'qwen3',
|
||||
});
|
||||
}
|
||||
|
||||
it('O1: options 映射 num_predict/num_ctx/stop/top_p/temperature', async () => {
|
||||
const adapter = makeOllama();
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({
|
||||
params: {
|
||||
maxTokens: 8192,
|
||||
temperature: 0.3,
|
||||
topP: 0.9,
|
||||
stream: false,
|
||||
contextLength: 16384,
|
||||
stopSequences: ['STOP'],
|
||||
},
|
||||
}),
|
||||
);
|
||||
const options = bodies[0].options as Record<string, unknown>;
|
||||
expect(options.num_predict).toBe(8192);
|
||||
expect(options.num_ctx).toBe(16384);
|
||||
expect(options.temperature).toBe(0.3);
|
||||
expect(options.top_p).toBe(0.9);
|
||||
expect(options.stop).toEqual(['STOP']);
|
||||
});
|
||||
|
||||
it('O2: think 参数 effort 映射(low→"low"、max→true);未开启思考时缺省', async () => {
|
||||
const adapter = makeOllama();
|
||||
const { bodies } = captureFetch();
|
||||
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: true, thinkingEffort: 'low' } }));
|
||||
expect(bodies[0].think).toBe('low');
|
||||
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: true, thinkingEffort: 'max' } }));
|
||||
expect(bodies[1].think).toBe(true);
|
||||
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: false } }));
|
||||
expect(bodies[2].think).toBeUndefined();
|
||||
});
|
||||
|
||||
it('O3: data URI 图片剥前缀转纯 base64 数组(无网络下载路径触发)', async () => {
|
||||
const adapter = makeOllama();
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(
|
||||
makeRequest({
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: '看图',
|
||||
images: [{ url: 'data:image/png;base64,iVBORw0KGgoAAAANSU', detail: 'auto' }],
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
const messages = bodies[0].messages as Array<Record<string, unknown>>;
|
||||
const userMsg = messages[messages.length - 1];
|
||||
expect(userMsg.images).toEqual(['iVBORw0KGgoAAAANSU']);
|
||||
});
|
||||
});
|
||||
|
||||
// ===== MiMo providerOptions(v0.6.4 P4-3) =====
|
||||
|
||||
describe('MimoAdapter — 服务端能力扩展(providerOptions)', () => {
|
||||
it('enableWebSearch 开启时附加 {type:web_search} 服务端工具', async () => {
|
||||
const adapter = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
baseURL: 'http://m.test/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'mimo-v2.5',
|
||||
providerOptions: { enableWebSearch: true },
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await adapter.send(makeRequest());
|
||||
const tools = bodies[0].tools as Array<Record<string, unknown>>;
|
||||
expect(tools.some((tc) => (tc as { type?: string }).type === 'web_search')).toBe(true);
|
||||
expect(bodies[0].tool_choice).toBe('auto');
|
||||
});
|
||||
|
||||
it('responseFormatJson 开启时写入 response_format json_object;默认不写', async () => {
|
||||
const on = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
baseURL: 'http://m.test/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'mimo-v2.5',
|
||||
providerOptions: { responseFormatJson: true },
|
||||
});
|
||||
const off = new MimoAdapter({
|
||||
provider: 'mimo',
|
||||
baseURL: 'http://m.test/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'mimo-v2.5',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
await on.send(makeRequest());
|
||||
await off.send(makeRequest());
|
||||
expect(bodies[0].response_format).toEqual({ type: 'json_object' });
|
||||
expect(bodies[1].response_format).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ===== Agnes =====
|
||||
|
||||
describe('AgnesAdapter — 思考模式对称性(v0.6.4)', () => {
|
||||
it('G1: thinkingEnabled=false 显式发送 enable_thinking:false(此前无法关闭服务端默认思考)', async () => {
|
||||
const adapter = new AgnesAdapter({
|
||||
provider: 'agnes',
|
||||
baseURL: 'http://g.test/v1',
|
||||
apiKey: 'k',
|
||||
defaultModel: 'agnes-2.0-flash',
|
||||
});
|
||||
const { bodies } = captureFetch();
|
||||
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: true, thinkingEffort: 'high' } }));
|
||||
expect(
|
||||
((bodies[0].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||||
).toBe(true);
|
||||
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: false } }));
|
||||
expect(
|
||||
((bodies[1].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||||
).toBe(false);
|
||||
|
||||
// 未配置 thinkingEnabled 同样视为关闭(显式 disabled 保持与服务端默认的确定性)
|
||||
await adapter.send(makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false } }));
|
||||
expect(
|
||||
((bodies[2].chat_template_kwargs as Record<string, unknown>) ?? {}).enable_thinking,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -216,7 +216,7 @@ describe('parseOpenAICompatibleResponse — 非流式响应', () => {
|
||||
expect(result.toolCalls![0].args).toEqual({ cmd: 'ls' });
|
||||
});
|
||||
|
||||
it('损坏的 tool_calls arguments 降级为空对象', () => {
|
||||
it('损坏的 tool_calls arguments 转为 _truncatedArguments 自愈载荷(v0.6.4: 不再静默降级 {})', () => {
|
||||
const result = parseOpenAICompatibleResponse({
|
||||
choices: [
|
||||
{
|
||||
@@ -229,7 +229,9 @@ describe('parseOpenAICompatibleResponse — 非流式响应', () => {
|
||||
],
|
||||
usage: {},
|
||||
});
|
||||
expect(result.toolCalls![0].args).toEqual({});
|
||||
const args = result.toolCalls![0].args as Record<string, unknown>;
|
||||
expect(args._truncatedArguments).toBe(true);
|
||||
expect(String(args._truncatedReason)).toContain('truncated');
|
||||
});
|
||||
|
||||
it('mapOpenAIFinishReason 覆盖 MiMo repetition_truncation', () => {
|
||||
|
||||
@@ -0,0 +1,540 @@
|
||||
/**
|
||||
* 流式上游错误帧 + 全线截断自愈测试(v0.6.4)
|
||||
*
|
||||
* 背景(v0.6.3 审计遗留):
|
||||
* 1. 错误帧黑洞 —— OpenAI 兼容网关中途发送的 `{"error":{...}}` 数据帧被解析器
|
||||
* 整帧吞掉(零日志),任何上游错误都伪装成"干净的空回复 + 正常 DONE",
|
||||
* 且以普通事件而非异常出现,绕过引擎的重试/故障转移通道。
|
||||
* 2. 截断自愈只修了 OpenAI 共享层 —— v0.6.3 的 _truncatedArguments 修复未覆盖:
|
||||
* - Anthropic:content_block_stop 解析失败静默 args={};断流时未完成块整体蒸发
|
||||
* - Ollama:NDJSON 坏参抛错落入外层 catch,工具调用丢弃且同 chunk USAGE/DONE 被跳过
|
||||
* - 非流式 parseOpenAICompatibleResponse:坏参仍静默 {}
|
||||
* - 引擎兜底缓冲 finalizeToolCallsFromBuffer:坏参静默 {}
|
||||
*
|
||||
* 本文件锁定以下契约:
|
||||
* A. 上游错误帧 → 抛出携带归一化 status 的 SseUpstreamError(可驱动重试判定)
|
||||
* B. finish_reason=content_filter → ContentFilterError(终态、不重试)
|
||||
* C. data:{无空格} 变体正常解析
|
||||
* D. 非流式/Ollama/Anthropic 截断参数统一转 _truncatedArguments 自愈载荷
|
||||
* E. Ollama 坏参不再吞掉同 chunk 的 done/USAGE 处理
|
||||
* F. Anthropic 断流时未完成 tool_use 块 flush 为自愈调用 + DONE
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
vi.mock('electron-log', () => ({
|
||||
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||||
}));
|
||||
|
||||
import { parseSSEStream, parseOpenAICompatibleResponse, SseUpstreamError } from '../shared/sse-stream';
|
||||
import { ContentFilterError } from '../base-adapter';
|
||||
import { OllamaAdapter } from '../ollama.adapter';
|
||||
import { AnthropicAdapter } from '../anthropic.adapter';
|
||||
import { MetonaErrorCode, MetonaStreamEventType } from '../../types';
|
||||
import type { MetonaRequest } from '../../types';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
function makeStream(lines: string[]): ReadableStream<Uint8Array> {
|
||||
const payload = lines.join('\n') + '\n';
|
||||
return new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(payload));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function collectExpectingThrow(stream: ReadableStream<Uint8Array>): Promise<unknown> {
|
||||
try {
|
||||
for await (const _ev of parseSSEStream(stream, 'r_test', 's_test', 1)) {
|
||||
void _ev;
|
||||
}
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
throw new Error('expected parseSSEStream to throw but it completed normally');
|
||||
}
|
||||
|
||||
function sseData(json: unknown): string {
|
||||
return `data: ${JSON.stringify(json)}`;
|
||||
}
|
||||
|
||||
// ===== A. 上游错误帧 → 抛出结构化异常 =====
|
||||
|
||||
describe('parseSSEStream — 上游错误帧(v0.6.4 错误帧黑洞根治)', () => {
|
||||
it('顶层 error 帧(含数值 status)→ 抛出携带该 status 的 SseUpstreamError', async () => {
|
||||
const err = await collectExpectingThrow(
|
||||
makeStream([sseData({ error: { message: 'Gateway timeout', status: 504 } })]),
|
||||
);
|
||||
expect(err).toBeInstanceOf(SseUpstreamError);
|
||||
expect((err as SseUpstreamError).status).toBe(504);
|
||||
expect((err as Error).message).toContain('Gateway timeout');
|
||||
});
|
||||
|
||||
it('choices[0].error 变体包装也能检出', async () => {
|
||||
const err = await collectExpectingThrow(
|
||||
makeStream([
|
||||
sseData({
|
||||
choices: [{ error: { message: 'bad gateway', code: 'upstream_failure', status: 502 } }],
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(err).toBeInstanceOf(SseUpstreamError);
|
||||
expect((err as SseUpstreamError).status).toBe(502);
|
||||
});
|
||||
|
||||
it('字符串型顶层 error 也能检出', async () => {
|
||||
const err = await collectExpectingThrow(makeStream(['data: {"error":"service unavailable"}']));
|
||||
expect(err).toBeInstanceOf(SseUpstreamError);
|
||||
expect((err as Error).message).toContain('service unavailable');
|
||||
});
|
||||
|
||||
it('providerCode 归一化:rate_limit_exceeded 无数值 status → 映射 429(可重试)', async () => {
|
||||
const err = await collectExpectingThrow(
|
||||
makeStream([
|
||||
sseData({ error: { code: 'rate_limit_exceeded', message: 'too many requests' } }),
|
||||
]),
|
||||
);
|
||||
expect(err).toBeInstanceOf(SseUpstreamError);
|
||||
expect((err as SseUpstreamError).status).toBe(429);
|
||||
expect((err as SseUpstreamError).providerCode).toBe('rate_limit_exceeded');
|
||||
});
|
||||
|
||||
it('insufficient_quota → 402;invalid_api_key → 401(不可重试区间)', async () => {
|
||||
const e1 = await collectExpectingThrow(
|
||||
makeStream([sseData({ error: { code: 'insufficient_quota', message: 'quota exceeded' } })]),
|
||||
);
|
||||
expect((e1 as SseUpstreamError).status).toBe(402);
|
||||
|
||||
const e2 = await collectExpectingThrow(
|
||||
makeStream([
|
||||
sseData({ error: { code: 'invalid_api_key', message: 'Incorrect API key provided' } }),
|
||||
]),
|
||||
);
|
||||
expect((e2 as SseUpstreamError).status).toBe(401);
|
||||
});
|
||||
|
||||
it('含 content_filter 码的错误帧 → ContentFilterError(复用专用类型)', async () => {
|
||||
const err = await collectExpectingThrow(
|
||||
makeStream([
|
||||
sseData({ error: { code: 'content_filter', message: 'rejected by safety policy' } }),
|
||||
]),
|
||||
);
|
||||
expect(err).toBeInstanceOf(ContentFilterError);
|
||||
});
|
||||
|
||||
it('isRetryable 契约对齐:错误带 status 时,engine.isRetryableError 的 429/5xx 判定可直接命中', async () => {
|
||||
// 用与引擎 isRetryableError 相同的判定逻辑验证字段形态
|
||||
const isRetryableShape = (err: unknown): boolean => {
|
||||
const e = err as { status?: number; message?: string };
|
||||
if (e.status === 429) return true;
|
||||
if (e.status && e.status >= 500 && e.status < 600) return true;
|
||||
return false;
|
||||
};
|
||||
const rateLimited = await collectExpectingThrow(
|
||||
makeStream([sseData({ error: { code: 'rate_limit_exceeded', message: 'rl' } })]),
|
||||
);
|
||||
expect(isRetryableShape(rateLimited)).toBe(true);
|
||||
const authFail = await collectExpectingThrow(
|
||||
makeStream([sseData({ error: { code: 'invalid_api_key', message: 'auth' } })]),
|
||||
);
|
||||
expect(isRetryableShape(authFail)).toBe(false);
|
||||
});
|
||||
|
||||
it('正常数据帧不含 error 字段时不受影响(回归)', async () => {
|
||||
// choices[0] 中存在 delta 但无 error → 正常产出文本增量并 DONE 收尾
|
||||
const events: string[] = [];
|
||||
const stream = makeStream([
|
||||
sseData({ choices: [{ delta: { content: 'hello' } }] }),
|
||||
'data: [DONE]',
|
||||
]);
|
||||
for await (const ev of parseSSEStream(stream, 'r', 's', 1)) {
|
||||
events.push(ev.type);
|
||||
}
|
||||
expect(events).toContain(MetonaStreamEventType.TEXT_DELTA);
|
||||
expect(events[events.length - 1]).toBe(MetonaStreamEventType.DONE);
|
||||
});
|
||||
});
|
||||
|
||||
// ===== B/C. content_filter 终止映射 + 无空格 data 变体 =====
|
||||
|
||||
describe('parseSSEStream — content_filter 与行格式兼容', () => {
|
||||
it('finish_reason=content_filter → 抛出 ContentFilterError(不再当普通结束)', async () => {
|
||||
const err = await collectExpectingThrow(
|
||||
makeStream([sseData({ choices: [{ delta: {}, finish_reason: 'content_filter' }] })]),
|
||||
);
|
||||
expect(err).toBeInstanceOf(ContentFilterError);
|
||||
});
|
||||
|
||||
it('data:{}(无空格)变体被正常解析(此前整帧跳过)', async () => {
|
||||
const events: Array<{ type: string }> = [];
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode('data:{"choices":[{"delta":{"content":"hi"}}]}\n'));
|
||||
controller.enqueue(encoder.encode('data:[DONE]\n'));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
for await (const ev of parseSSEStream(stream, 'r', 's', 1)) {
|
||||
events.push({ type: ev.type });
|
||||
}
|
||||
expect(events.some((e) => e.type === MetonaStreamEventType.TEXT_DELTA)).toBe(true);
|
||||
expect(events[events.length - 1].type).toBe(MetonaStreamEventType.DONE);
|
||||
});
|
||||
});
|
||||
|
||||
// ===== D. 非流式截断自愈同步 =====
|
||||
|
||||
describe('parseOpenAICompatibleResponse — 非流式截断自愈(v0.6.4 同步)', () => {
|
||||
it('坏 JSON arguments 不再静默 {},转为 _truncatedArguments 载荷', () => {
|
||||
const result = parseOpenAICompatibleResponse({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{
|
||||
id: 'call_1',
|
||||
function: { name: 'write_file', arguments: '{"file_path": "a.html", "con' },
|
||||
},
|
||||
],
|
||||
},
|
||||
finish_reason: 'tool_calls',
|
||||
},
|
||||
],
|
||||
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
|
||||
});
|
||||
|
||||
expect(result.toolCalls).toHaveLength(1);
|
||||
const args = result.toolCalls![0].args as Record<string, unknown>;
|
||||
expect(args._truncatedArguments).toBe(true);
|
||||
expect(String(args._truncatedReason)).toContain('truncated');
|
||||
});
|
||||
|
||||
it('合法对象型 arguments 保持原样(回归)', () => {
|
||||
const result = parseOpenAICompatibleResponse({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
role: 'assistant',
|
||||
tool_calls: [{ id: 'c1', function: { name: 'think', arguments: '{"a":1}' } }],
|
||||
},
|
||||
finish_reason: 'tool_calls',
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.toolCalls![0].args).toEqual({ a: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
// ===== E/F. Ollama NDJSON 与 Anthropic 事件机 =====
|
||||
|
||||
/** 构造全局 fetch mock:返回给定行的 NDJSON/SSE 流 */
|
||||
function mockFetchWithLines(lines: string[]): ReturnType<typeof vi.fn> {
|
||||
const payload = encoder.encode(lines.join('\n') + '\n');
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(payload);
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
const fetchMock = vi.fn().mockResolvedValue(
|
||||
new Response(body, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/x-ndjson' },
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
return fetchMock;
|
||||
}
|
||||
|
||||
const baseRequest: MetonaRequest = {
|
||||
meta: { sessionId: 's1', iteration: 1, requestId: 'r1', timestamp: Date.now(), agentVersion: 'test' },
|
||||
systemPrompt: { roleDefinition: 'rd', outputConstraints: '', safetyGuidelines: '' },
|
||||
messages: [{ role: 'user', content: 'hi', timestamp: Date.now() }],
|
||||
params: { maxTokens: 4096, temperature: 0, stream: true },
|
||||
};
|
||||
|
||||
describe('OllamaAdapter.sendStream — NDJSON 截断自愈(v0.6.4)', () => {
|
||||
it('坏 JSON arguments → _truncatedArguments 工具调用,且后续 done chunk 的 USAGE/DONE 不再被吞掉', async () => {
|
||||
const adapter = new OllamaAdapter({
|
||||
provider: 'ollama',
|
||||
baseURL: 'http://localhost:11434',
|
||||
defaultModel: 'qwen3',
|
||||
});
|
||||
mockFetchWithLines([
|
||||
JSON.stringify({
|
||||
model: 'qwen3',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
tool_calls: [{ function: { name: 'write_file', arguments: '{"path": "a.txt", "cont' } }],
|
||||
},
|
||||
}),
|
||||
// 关键:同一响应流中随后仍有收尾 chunk(原实现外层 catch 会跳过这些处理)
|
||||
JSON.stringify({
|
||||
model: 'qwen3',
|
||||
message: { role: 'assistant', content: '' },
|
||||
done: true,
|
||||
done_reason: 'stop',
|
||||
prompt_eval_count: 11,
|
||||
eval_count: 7,
|
||||
}),
|
||||
]);
|
||||
|
||||
const events: string[] = [];
|
||||
let usageInputTokens = -1;
|
||||
for await (const ev of adapter.sendStream(baseRequest)) {
|
||||
events.push(ev.type);
|
||||
if (ev.type === MetonaStreamEventType.USAGE) usageInputTokens = ev.usage!.inputTokens ?? 0;
|
||||
}
|
||||
|
||||
// 流不再被坏参打断:usage 与 done 都到达
|
||||
expect(usageInputTokens).toBe(11);
|
||||
expect(events[events.length - 1]).toBe(MetonaStreamEventType.DONE);
|
||||
});
|
||||
|
||||
it('自愈载荷内容正确(_truncatedArguments=true + reason 含 truncated)', async () => {
|
||||
const adapter = new OllamaAdapter({
|
||||
provider: 'ollama',
|
||||
baseURL: 'http://localhost:11434',
|
||||
defaultModel: 'qwen3',
|
||||
});
|
||||
mockFetchWithLines([
|
||||
JSON.stringify({
|
||||
model: 'm',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
tool_calls: [{ function: { name: 'read_file', arguments: '{"file_path": "b.t' } }],
|
||||
},
|
||||
done: false,
|
||||
}),
|
||||
JSON.stringify({ model: 'm', message: { role: 'assistant', content: '' }, done: true }),
|
||||
]);
|
||||
|
||||
let completeArgs: Record<string, unknown> | undefined;
|
||||
for await (const ev of adapter.sendStream(baseRequest)) {
|
||||
if (ev.type === MetonaStreamEventType.TOOL_CALL_COMPLETE) {
|
||||
completeArgs = ev.toolCall!.args as Record<string, unknown>;
|
||||
}
|
||||
}
|
||||
expect(completeArgs).toBeDefined();
|
||||
expect(completeArgs!._truncatedArguments).toBe(true);
|
||||
expect(String(completeArgs!._truncatedReason)).toContain('truncated');
|
||||
});
|
||||
});
|
||||
|
||||
describe('AnthropicAdapter.sendStream — 事件机截断自愈 + 断流 flush(v0.6.4)', () => {
|
||||
it('缺口 A:content_block_stop 时坏 JSON → _truncatedArguments(不再静默 {})', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://anthropic.test',
|
||||
apiKey: 'sk-test',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
mockFetchWithLines([
|
||||
'event: content_block_start',
|
||||
sseData({
|
||||
type: 'content_block_start',
|
||||
index: 0,
|
||||
content_block: { type: 'tool_use', id: 'toolu_1', name: 'write_file' },
|
||||
}),
|
||||
'event: content_block_delta',
|
||||
sseData({
|
||||
type: 'content_block_delta',
|
||||
index: 0,
|
||||
delta: { type: 'input_json_delta', partial_json: '{"file_path": "a.html", "con' },
|
||||
}),
|
||||
'event: content_block_stop',
|
||||
sseData({ type: 'content_block_stop', index: 0 }),
|
||||
'event: message_stop',
|
||||
sseData({ type: 'message_stop' }),
|
||||
]);
|
||||
|
||||
let completeArgs: Record<string, unknown> | undefined;
|
||||
let completeId: string | undefined;
|
||||
for await (const ev of adapter.sendStream(baseRequest)) {
|
||||
if (ev.type === MetonaStreamEventType.TOOL_CALL_COMPLETE) {
|
||||
completeArgs = ev.toolCall!.args as Record<string, unknown>;
|
||||
completeId = ev.toolCall!.id;
|
||||
}
|
||||
}
|
||||
expect(completeArgs).toBeDefined();
|
||||
expect(completeArgs!._truncatedArguments).toBe(true);
|
||||
// 保留上游原始 block id(非 nanoid 重造)
|
||||
expect(completeId).toBe('toolu_1');
|
||||
});
|
||||
|
||||
it('缺口 B:断流未完成 tool_use 块 → flush 为自愈调用 + 补发 DONE(不再整体蒸发)', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://anthropic.test',
|
||||
apiKey: 'sk-test',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
// 有 content_block_start,但流在 content_block_stop/message_stop 之前断开
|
||||
const payload =
|
||||
'event: message_start\ndata: {"type":"message_start","message":{"role":"assistant","usage":{"input_tokens":42}}}\n\n' +
|
||||
'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_X","name":"edit_file"}}\n\n' +
|
||||
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\\"pa"}}\n\n';
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(payload));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn().mockResolvedValue(new Response(body, { status: 200 })),
|
||||
);
|
||||
|
||||
const events: Array<{ type: string; toolCallId?: string; toolCallName?: string }> = [];
|
||||
for await (const ev of adapter.sendStream(baseRequest)) {
|
||||
events.push({
|
||||
type: ev.type,
|
||||
toolCallId: ev.toolCall?.id,
|
||||
toolCallName: ev.toolCall?.name,
|
||||
});
|
||||
}
|
||||
|
||||
const complete = events.find((e) => e.type === MetonaStreamEventType.TOOL_CALL_COMPLETE);
|
||||
// 核心契约:断流前缓冲中的 block 必须以 TOOL_CALL_COMPLETE 产出(引擎才不会误判空回复完成)
|
||||
expect(complete).toBeDefined();
|
||||
expect(complete!.toolCallId).toBe('toolu_X');
|
||||
expect(complete!.toolCallName).toBe('edit_file');
|
||||
expect(events[events.length - 1].type).toBe(MetonaStreamEventType.DONE);
|
||||
});
|
||||
|
||||
it('error 事件 → 抛出携带归一化 status 的异常(overloaded → 529 可重试语义)', async () => {
|
||||
const adapter = new AnthropicAdapter({
|
||||
provider: 'anthropic',
|
||||
baseURL: 'http://anthropic.test',
|
||||
apiKey: 'sk-test',
|
||||
defaultModel: 'claude-sonnet-4-5',
|
||||
});
|
||||
mockFetchWithLines([
|
||||
'event: error',
|
||||
sseData({ type: 'error', error: { type: 'overloaded_error', message: 'Overloaded' } }),
|
||||
]);
|
||||
|
||||
let caught: unknown;
|
||||
try {
|
||||
for await (const _ev of adapter.sendStream(baseRequest)) {
|
||||
void _ev;
|
||||
}
|
||||
} catch (err) {
|
||||
caught = err;
|
||||
}
|
||||
expect(caught).toBeDefined();
|
||||
expect((caught as Error & { status?: number }).status).toBe(529);
|
||||
expect((caught as Error).message).toContain('Overloaded');
|
||||
});
|
||||
});
|
||||
|
||||
// ===== G. 引擎侧 ERROR 事件保留结构化码 =====
|
||||
|
||||
describe('MetonaErrorCode — CONTENT_FILTERED 枚举契约(finish 映射依赖)', () => {
|
||||
it('code 值稳定为 content_filtered', () => {
|
||||
expect(MetonaErrorCode.CONTENT_FILTERED).toBe('content_filtered');
|
||||
});
|
||||
});
|
||||
|
||||
// ===== H. 引擎集成:错误帧异常进入重试/故障转移通道;ERROR 码映射 CONTENT_FILTERED =====
|
||||
|
||||
import { AgentLoopEngine } from '../../agent-loop/engine';
|
||||
import { TerminationReason } from '../../agent-loop/types';
|
||||
import type { IMetonaProviderAdapter, MetonaResponse, MetonaStreamEvent } from '../../types';
|
||||
|
||||
function textDone(text: string): MetonaStreamEvent[] {
|
||||
return [
|
||||
{ type: MetonaStreamEventType.TEXT_DELTA, requestId: 'r1', sessionId: 's1', iteration: 1, seq: 0, timestamp: Date.now(), delta: text },
|
||||
{ type: MetonaStreamEventType.DONE, requestId: 'r1', sessionId: 's1', iteration: 1, seq: 1, timestamp: Date.now() },
|
||||
];
|
||||
}
|
||||
|
||||
describe('AgentLoopEngine 集成 — v0.6.4 错误通道单轨化', () => {
|
||||
const userMessage = { role: 'user' as const, content: 'hi', timestamp: Date.now() };
|
||||
const systemPrompt = { roleDefinition: '', outputConstraints: '', safetyGuidelines: '' };
|
||||
|
||||
function scriptedAdapter(
|
||||
behaviors: Array<{ throws?: Error; events?: MetonaStreamEvent[] }>,
|
||||
): { adapter: IMetonaProviderAdapter; calls: () => number } {
|
||||
let call = 0;
|
||||
const base: IMetonaProviderAdapter = {
|
||||
providerId: 'mock',
|
||||
supportedModels: ['m'],
|
||||
supportsToolCalling: true,
|
||||
supportsThinking: false,
|
||||
getContextWindow: () => 1_000_000,
|
||||
send: async (): Promise<MetonaResponse> => ({
|
||||
meta: { requestId: 'r', provider: 'mock', model: 'm', latencyMs: 0, timestamp: Date.now() },
|
||||
content: '',
|
||||
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
|
||||
finishReason: 'stop' as never,
|
||||
}),
|
||||
sendStream: async function* (): AsyncIterable<MetonaStreamEvent> {
|
||||
const b = behaviors[Math.min(call, behaviors.length - 1)];
|
||||
call++;
|
||||
if (b.throws) throw b.throws;
|
||||
for (const ev of b.events ?? []) yield ev;
|
||||
},
|
||||
setAbortSignal: vi.fn(),
|
||||
healthCheck: async () => true,
|
||||
};
|
||||
return { adapter: base, calls: () => call };
|
||||
}
|
||||
|
||||
it('SseUpstreamError(429) 首次失败 → 引擎指数退避重试后成功(不再落入 UNKNOWN 终态)', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { adapter } = scriptedAdapter([
|
||||
{ throws: new SseUpstreamError('rate limited', { status: 429 }) },
|
||||
{ events: textDone('recovered answer') },
|
||||
]);
|
||||
const engine = new AgentLoopEngine({ retryCount: 3 }, adapter);
|
||||
// 推进退避定时器(1s/2s/4s + jitter 上限)
|
||||
const runPromise = engine.runStream(userMessage, 's1', [], systemPrompt);
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
const output = await runPromise;
|
||||
expect(output.terminationReason).toBe(TerminationReason.COMPLETED);
|
||||
expect(output.finalAnswer).toBe('recovered answer');
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('引擎收到的流内 ERROR 带 code=content_filtered → finish 发出 CONTENT_FILTERED 错误事件', async () => {
|
||||
const { adapter } = scriptedAdapter([
|
||||
{
|
||||
events: [
|
||||
{
|
||||
type: MetonaStreamEventType.ERROR,
|
||||
requestId: 'r1',
|
||||
sessionId: 's1',
|
||||
iteration: 1,
|
||||
seq: 0,
|
||||
timestamp: Date.now(),
|
||||
error: {
|
||||
code: MetonaErrorCode.CONTENT_FILTERED,
|
||||
message: '内容被安全审核拦截',
|
||||
retryable: false,
|
||||
},
|
||||
},
|
||||
{ type: MetonaStreamEventType.DONE, requestId: 'r1', sessionId: 's1', iteration: 1, seq: 1, timestamp: Date.now() },
|
||||
],
|
||||
},
|
||||
]);
|
||||
const engine = new AgentLoopEngine({ retryCount: 0 }, adapter);
|
||||
const errorEvents: Array<{ code?: string; message?: string }> = [];
|
||||
engine.on('streamEvent', (ev: MetonaStreamEvent) => {
|
||||
if (ev.type === MetonaStreamEventType.ERROR) {
|
||||
errorEvents.push({ code: ev.error?.code, message: ev.error?.message });
|
||||
}
|
||||
});
|
||||
const output = await engine.runStream(userMessage, 's1', [], systemPrompt);
|
||||
expect(output.terminationReason).toBe(TerminationReason.ERROR);
|
||||
expect(errorEvents[errorEvents.length - 1]?.code).toBe(MetonaErrorCode.CONTENT_FILTERED);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user