feat: v0.4.1 质量加固版 — 工程化基线 + 安全加固 + 测试补齐 + 体验升级
工程化(从零到一): - 新增 Gitea Actions CI(debian-latest):类型检查 + Lint + 单元测试 + 产物编译验证 - 新增 husky + lint-staged 预提交钩子(lint-staged + typecheck 门禁) - 移除坏脚本 test:e2e(无 Playwright 配置必失败);prebuild 改用内置 fs.rmSync - 依赖清理:移除死依赖 sql.js(2MB)/@playwright/test,@types/shell-quote 移至 devDependencies 安全加固: - PolicyEngine 频率限制按会话隔离(多会话并发不再互抢配额) - ConfirmationHook 拒绝记忆加 10 分钟 TTL + 恢复询问入口(新增 2 个 IPC 通道) - Windows run_command 白名单工具(git/node/npm/npx/pnpm/yarn/tsc)改走 cmd.exe /c + 参数数组执行,收窄 shell 注入面 - web_search 四引擎 HTML 解析迁移 node-html-parser(结构化主层 + 正则降级) 缺陷修复(测试驱动发现): - mapError 大小写缺陷:网络错误码永远落入 UNKNOWN 无法触发重试 - 搜狗解析器自我过滤:相对链接补全后又被 sogou.com 过滤导致结果全丢 - 百度复合类名重复收录:class="result c-container" 被双重匹配 测试补齐(113 → 194 用例): - 新增 5 个测试文件:sse-stream / base-adapter / confirmation-hook / ipc-agent 编排链路 / web-search 解析器 - 覆盖 sendMessage 全分支、SSE 流解析、错误映射、确认钩子竞态/超时/批量审批 体验升级: - OutputValidator 验证结果可见化(VALIDATION 流事件 → 聊天流提示卡) - SettingsModal 巨型组件拆分(1503 行 → 10 个文件,可独立维护) - MessageList 接入 react-virtuoso 真虚拟滚动(千条消息恒定开销) - MCP 新增 streamable HTTP 传输支持(SDK 内置传输 + DB 迁移 6 + UI 双模式)
This commit is contained in:
+462
-345
@@ -22,7 +22,10 @@ import log from 'electron-log';
|
||||
/** 单会话的 text_delta 节流状态 */
|
||||
interface ThrottleState {
|
||||
buffer: string;
|
||||
lastEventMeta: Pick<MetonaStreamEvent, 'requestId' | 'sessionId' | 'iteration' | 'seq' | 'timestamp' | 'runId'> | null;
|
||||
lastEventMeta: Pick<
|
||||
MetonaStreamEvent,
|
||||
'requestId' | 'sessionId' | 'iteration' | 'seq' | 'timestamp' | 'runId'
|
||||
> | null;
|
||||
flushTimer: ReturnType<typeof setTimeout> | null;
|
||||
}
|
||||
|
||||
@@ -37,10 +40,20 @@ interface IterationTrace {
|
||||
|
||||
export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
const {
|
||||
agentEngineManager, sessionRecorder, configService, sessionService,
|
||||
workspaceService, contextBuilder, auditService, memoryManager,
|
||||
promptInjectionDefender, outputValidator, memoryConsolidator,
|
||||
sessionSummaryService, orchestrator, confirmationHook,
|
||||
agentEngineManager,
|
||||
sessionRecorder,
|
||||
configService,
|
||||
sessionService,
|
||||
workspaceService,
|
||||
contextBuilder,
|
||||
auditService,
|
||||
memoryManager,
|
||||
promptInjectionDefender,
|
||||
outputValidator,
|
||||
memoryConsolidator,
|
||||
sessionSummaryService,
|
||||
orchestrator,
|
||||
confirmationHook,
|
||||
} = ctx;
|
||||
|
||||
// ===== 常驻事件管道:text_delta 按会话节流(F8) =====
|
||||
@@ -88,7 +101,9 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
// F8: text_delta 聚合,其他事件立即转发(先 flush 保证顺序)
|
||||
if (event.type === MetonaStreamEventType.TEXT_DELTA && event.delta) {
|
||||
const st = throttleStates.get(sessionId) ?? {
|
||||
buffer: '', lastEventMeta: null, flushTimer: null,
|
||||
buffer: '',
|
||||
lastEventMeta: null,
|
||||
flushTimer: null,
|
||||
};
|
||||
throttleStates.set(sessionId, st);
|
||||
if (st.buffer === '') {
|
||||
@@ -101,7 +116,12 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
runId: event.runId,
|
||||
};
|
||||
} else if (st.lastEventMeta) {
|
||||
st.lastEventMeta = { ...st.lastEventMeta, seq: event.seq, timestamp: event.timestamp, runId: event.runId };
|
||||
st.lastEventMeta = {
|
||||
...st.lastEventMeta,
|
||||
seq: event.seq,
|
||||
timestamp: event.timestamp,
|
||||
runId: event.runId,
|
||||
};
|
||||
}
|
||||
st.buffer += event.delta;
|
||||
if (st.flushTimer === null) {
|
||||
@@ -133,9 +153,10 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
break;
|
||||
case MetonaStreamEventType.TOOL_RESULT:
|
||||
if (event.toolResult) {
|
||||
const resultPreview = typeof event.toolResult.result === 'string'
|
||||
? event.toolResult.result
|
||||
: JSON.stringify(event.toolResult.result);
|
||||
const resultPreview =
|
||||
typeof event.toolResult.result === 'string'
|
||||
? event.toolResult.result
|
||||
: JSON.stringify(event.toolResult.result);
|
||||
sessionRecorder.recordToolResult({
|
||||
sessionId,
|
||||
iteration: event.iteration,
|
||||
@@ -167,21 +188,67 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
});
|
||||
|
||||
// ===== 常驻监听:状态变化(广播 + TRACE 迭代录制) =====
|
||||
agentEngineManager.on('stateChange', (data: {
|
||||
previous?: string; current?: string; state?: string;
|
||||
sessionId?: string; iteration?: number; runId?: string;
|
||||
}) => {
|
||||
if (data.previous) log.info(`[AGENT] State: ${data.previous} → ${data.current}`);
|
||||
broadcast('agent:stateChange', data);
|
||||
agentEngineManager.on(
|
||||
'stateChange',
|
||||
(data: {
|
||||
previous?: string;
|
||||
current?: string;
|
||||
state?: string;
|
||||
sessionId?: string;
|
||||
iteration?: number;
|
||||
runId?: string;
|
||||
}) => {
|
||||
if (data.previous) log.info(`[AGENT] State: ${data.previous} → ${data.current}`);
|
||||
broadcast('agent:stateChange', data);
|
||||
|
||||
const sessionId = data.sessionId;
|
||||
if (!sessionId || data.iteration == null) return;
|
||||
const stateValue = data.state ?? data.current ?? '';
|
||||
const trace = iterationTraces.get(sessionId);
|
||||
const sessionId = data.sessionId;
|
||||
if (!sessionId || data.iteration == null) return;
|
||||
const stateValue = data.state ?? data.current ?? '';
|
||||
const trace = iterationTraces.get(sessionId);
|
||||
|
||||
// THINKING 且迭代号变化 → 新迭代开始(关闭上一迭代)
|
||||
if (stateValue === 'THINKING' && (!trace || trace.iteration !== data.iteration)) {
|
||||
if (trace && !trace.responded) {
|
||||
// THINKING 且迭代号变化 → 新迭代开始(关闭上一迭代)
|
||||
if (stateValue === 'THINKING' && (!trace || trace.iteration !== data.iteration)) {
|
||||
if (trace && !trace.responded) {
|
||||
sessionRecorder.recordLLMResponse({
|
||||
sessionId,
|
||||
iteration: trace.iteration,
|
||||
content: trace.text,
|
||||
finishReason: 'stop',
|
||||
tokenUsage: trace.usage ?? { input: 0, output: 0, total: 0 },
|
||||
});
|
||||
}
|
||||
if (trace) {
|
||||
sessionRecorder.recordIterationEnd(sessionId, {
|
||||
iteration: trace.iteration,
|
||||
durationMs: Date.now() - trace.startedAt,
|
||||
});
|
||||
}
|
||||
iterationTraces.set(sessionId, {
|
||||
iteration: data.iteration,
|
||||
startedAt: Date.now(),
|
||||
text: '',
|
||||
responded: false,
|
||||
});
|
||||
sessionRecorder.recordIterationStart(sessionId, data.iteration);
|
||||
const provider = configService.get<string>('llm.provider') ?? '';
|
||||
const model = configService.get<string>('llm.model') ?? '';
|
||||
sessionRecorder.recordLLMRequest({
|
||||
sessionId,
|
||||
iteration: data.iteration,
|
||||
provider,
|
||||
model,
|
||||
messageCount: data.iteration + 1,
|
||||
});
|
||||
}
|
||||
|
||||
// PARSING → 本轮流式结束,记录 llm_response
|
||||
if (
|
||||
stateValue === 'PARSING' &&
|
||||
trace &&
|
||||
trace.iteration === data.iteration &&
|
||||
!trace.responded
|
||||
) {
|
||||
trace.responded = true;
|
||||
sessionRecorder.recordLLMResponse({
|
||||
sessionId,
|
||||
iteration: trace.iteration,
|
||||
@@ -190,76 +257,48 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
tokenUsage: trace.usage ?? { input: 0, output: 0, total: 0 },
|
||||
});
|
||||
}
|
||||
if (trace) {
|
||||
sessionRecorder.recordIterationEnd(sessionId, {
|
||||
iteration: trace.iteration,
|
||||
durationMs: Date.now() - trace.startedAt,
|
||||
});
|
||||
}
|
||||
iterationTraces.set(sessionId, {
|
||||
iteration: data.iteration,
|
||||
startedAt: Date.now(),
|
||||
text: '',
|
||||
responded: false,
|
||||
});
|
||||
sessionRecorder.recordIterationStart(sessionId, data.iteration);
|
||||
const provider = configService.get<string>('llm.provider') ?? '';
|
||||
const model = configService.get<string>('llm.model') ?? '';
|
||||
sessionRecorder.recordLLMRequest({
|
||||
sessionId,
|
||||
iteration: data.iteration,
|
||||
provider,
|
||||
model,
|
||||
messageCount: data.iteration + 1,
|
||||
});
|
||||
}
|
||||
|
||||
// PARSING → 本轮流式结束,记录 llm_response
|
||||
if (stateValue === 'PARSING' && trace && trace.iteration === data.iteration && !trace.responded) {
|
||||
trace.responded = true;
|
||||
sessionRecorder.recordLLMResponse({
|
||||
sessionId,
|
||||
iteration: trace.iteration,
|
||||
content: trace.text,
|
||||
finishReason: 'stop',
|
||||
tokenUsage: trace.usage ?? { input: 0, output: 0, total: 0 },
|
||||
});
|
||||
}
|
||||
|
||||
// TERMINATED → 补记最终迭代的 iteration_end(正常流程只在下一轮 THINKING 补记,
|
||||
// 最终轮无后续迭代,需在此补齐 TRACE 完整性)+ 兜底清理会话管道状态
|
||||
if (stateValue === 'TERMINATED') {
|
||||
if (trace) {
|
||||
sessionRecorder.recordIterationEnd(sessionId, {
|
||||
iteration: trace.iteration,
|
||||
durationMs: Date.now() - trace.startedAt,
|
||||
});
|
||||
// TERMINATED → 补记最终迭代的 iteration_end(正常流程只在下一轮 THINKING 补记,
|
||||
// 最终轮无后续迭代,需在此补齐 TRACE 完整性)+ 兜底清理会话管道状态
|
||||
if (stateValue === 'TERMINATED') {
|
||||
if (trace) {
|
||||
sessionRecorder.recordIterationEnd(sessionId, {
|
||||
iteration: trace.iteration,
|
||||
durationMs: Date.now() - trace.startedAt,
|
||||
});
|
||||
}
|
||||
cleanupSessionState(sessionId);
|
||||
}
|
||||
cleanupSessionState(sessionId);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// ===== 常驻监听:上下文压缩(toast + streamEvent 通知) =====
|
||||
agentEngineManager.on('compressed', (data: {
|
||||
sessionId?: string; iteration?: number; originalTokens?: number; compressedTokens?: number;
|
||||
}) => {
|
||||
const savedTokens = Math.max(0, (data.originalTokens ?? 0) - (data.compressedTokens ?? 0));
|
||||
// toast 通知用户压缩已发生
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message: `上下文压缩: ${data.originalTokens ?? '?'} → ${data.compressedTokens ?? '?'} tokens(节省 ${savedTokens})`,
|
||||
});
|
||||
// 通过 streamEvent 转发,前端 useAgentStream 监听 'compressed' 类型后更新 store
|
||||
broadcast('agent:streamEvent', {
|
||||
type: 'compressed',
|
||||
sessionId: data.sessionId ?? '',
|
||||
iteration: data.iteration ?? 0,
|
||||
originalTokens: data.originalTokens,
|
||||
compressedTokens: data.compressedTokens,
|
||||
savedTokens,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
});
|
||||
agentEngineManager.on(
|
||||
'compressed',
|
||||
(data: {
|
||||
sessionId?: string;
|
||||
iteration?: number;
|
||||
originalTokens?: number;
|
||||
compressedTokens?: number;
|
||||
}) => {
|
||||
const savedTokens = Math.max(0, (data.originalTokens ?? 0) - (data.compressedTokens ?? 0));
|
||||
// toast 通知用户压缩已发生
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message: `上下文压缩: ${data.originalTokens ?? '?'} → ${data.compressedTokens ?? '?'} tokens(节省 ${savedTokens})`,
|
||||
});
|
||||
// 通过 streamEvent 转发,前端 useAgentStream 监听 'compressed' 类型后更新 store
|
||||
broadcast('agent:streamEvent', {
|
||||
type: 'compressed',
|
||||
sessionId: data.sessionId ?? '',
|
||||
iteration: data.iteration ?? 0,
|
||||
originalTokens: data.originalTokens,
|
||||
compressedTokens: data.compressedTokens,
|
||||
savedTokens,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// ===== 常驻监听:死循环检测(toast 警告) =====
|
||||
agentEngineManager.on('deadLoop', (data: { iteration?: number; sessionId?: string }) => {
|
||||
@@ -271,300 +310,378 @@ export function registerAgentHandlers(ctx: IPCContext): void {
|
||||
});
|
||||
|
||||
// ===== 常驻监听:Provider 故障转移(P1,通知前端 + toast) =====
|
||||
agentEngineManager.on('providerSwitched', (data: {
|
||||
from?: string; to?: string; reason?: string; sessionId?: string;
|
||||
}) => {
|
||||
broadcast('agent:providerSwitched', {
|
||||
from: data.from,
|
||||
to: data.to,
|
||||
reason: data.reason ?? 'failover',
|
||||
sessionId: data.sessionId ?? '',
|
||||
});
|
||||
broadcast('toast:show', {
|
||||
type: 'warning',
|
||||
message: `Provider 故障转移: ${data.from ?? '?'} → ${data.to ?? '?'}(主 Provider 请求失败)`,
|
||||
});
|
||||
});
|
||||
agentEngineManager.on(
|
||||
'providerSwitched',
|
||||
(data: { from?: string; to?: string; reason?: string; sessionId?: string }) => {
|
||||
broadcast('agent:providerSwitched', {
|
||||
from: data.from,
|
||||
to: data.to,
|
||||
reason: data.reason ?? 'failover',
|
||||
sessionId: data.sessionId ?? '',
|
||||
});
|
||||
broadcast('toast:show', {
|
||||
type: 'warning',
|
||||
message: `Provider 故障转移: ${data.from ?? '?'} → ${data.to ?? '?'}(主 Provider 请求失败)`,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// ===== Agent 消息发送 =====
|
||||
|
||||
ipcMain.handle('agent:sendMessage', async (_event, userMessage: MetonaMessage, sessionId: string) => {
|
||||
// M-33 修复: 参数校验,防止 undefined/非字符串导致下游异常
|
||||
// P1-5 修复: 校验失败时也发 ERROR+DONE 流事件,防止 isStreaming 永久卡死
|
||||
const sendErrorEvent = (message: string, sid: string): void => {
|
||||
const errorEvent: MetonaStreamEvent = {
|
||||
type: MetonaStreamEventType.ERROR,
|
||||
requestId: '', sessionId: sid, iteration: 0, seq: 0, timestamp: Date.now(),
|
||||
error: { code: MetonaErrorCode.UNKNOWN, message, retryable: false },
|
||||
ipcMain.handle(
|
||||
'agent:sendMessage',
|
||||
async (_event, userMessage: MetonaMessage, sessionId: string) => {
|
||||
// M-33 修复: 参数校验,防止 undefined/非字符串导致下游异常
|
||||
// P1-5 修复: 校验失败时也发 ERROR+DONE 流事件,防止 isStreaming 永久卡死
|
||||
const sendErrorEvent = (message: string, sid: string): void => {
|
||||
const errorEvent: MetonaStreamEvent = {
|
||||
type: MetonaStreamEventType.ERROR,
|
||||
requestId: '',
|
||||
sessionId: sid,
|
||||
iteration: 0,
|
||||
seq: 0,
|
||||
timestamp: Date.now(),
|
||||
error: { code: MetonaErrorCode.UNKNOWN, message, retryable: false },
|
||||
};
|
||||
broadcast('agent:streamEvent', errorEvent);
|
||||
broadcast('agent:streamEvent', { ...errorEvent, type: MetonaStreamEventType.DONE });
|
||||
};
|
||||
broadcast('agent:streamEvent', errorEvent);
|
||||
broadcast('agent:streamEvent', { ...errorEvent, type: MetonaStreamEventType.DONE });
|
||||
};
|
||||
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
log.warn('[AGENT] sendMessage rejected: invalid sessionId');
|
||||
sendErrorEvent('无效的会话 ID', sessionId ?? '');
|
||||
return { success: false, error: 'Invalid sessionId' };
|
||||
}
|
||||
if (!userMessage || typeof userMessage !== 'object' || typeof userMessage.content !== 'string') {
|
||||
log.warn('[AGENT] sendMessage rejected: invalid userMessage');
|
||||
sendErrorEvent('无效的消息格式', sessionId);
|
||||
return { success: false, error: 'Invalid message format' };
|
||||
}
|
||||
log.info('[AGENT] sendMessage:', sessionId, (userMessage.content ?? '').slice(0, 80));
|
||||
|
||||
// 发送消息前确保 Adapter 使用最新配置(失败则中止,防止用旧 Provider 的 adapter 发送)
|
||||
if (!ctx.reloadAdapter()) {
|
||||
const errorMsg = 'Adapter 加载失败,请检查 LLM 配置(Provider、API Key、Base URL、Model 是否完整)';
|
||||
log.error('[AGENT]', errorMsg);
|
||||
sendErrorEvent(errorMsg, sessionId);
|
||||
sessionRecorder.stopRecording(sessionId, { totalIterations: 0, totalTokens: 0, durationMs: 0, terminationReason: 'error' });
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
|
||||
// TRACE 层:开始录制 / TOOL 层:记录会话开始
|
||||
sessionRecorder.startRecording(sessionId);
|
||||
auditService.logSessionStart(sessionId);
|
||||
|
||||
// 保存用户消息到数据库
|
||||
sessionService.saveMessage({
|
||||
sessionId,
|
||||
role: 'user',
|
||||
content: userMessage.content,
|
||||
attachments: (userMessage as MetonaMessage & { attachments?: unknown[] }).attachments,
|
||||
});
|
||||
|
||||
// P2-11: 分层加载历史——存在滚动摘要时只加载 [摘要 + 近期原文]
|
||||
const history = sessionSummaryService.buildHistoryMessages(sessionId).slice(0, -1);
|
||||
|
||||
// 从工作空间文件构建 System Prompt
|
||||
const workspaceFiles = workspaceService.getFiles();
|
||||
const systemPrompt = contextBuilder.buildSystemPrompt(workspaceFiles, workspaceService.getPath());
|
||||
|
||||
// v0.3.18 修复: SOUL.md 为空或不存在时降级到默认身份,向前端发 toast 提示用户
|
||||
if (contextBuilder.isUsingFallbackRole()) {
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message: '未找到 SOUL.md 或内容为空,已使用默认 Metona 身份。可在工作空间根目录创建 SOUL.md 自定义 Agent 人格',
|
||||
});
|
||||
}
|
||||
|
||||
// 检索与用户消息相关的记忆,注入到 System Prompt 动态区
|
||||
try {
|
||||
const memories = memoryManager.search(userMessage.content, { topK: 5, minImportance: 0.3 });
|
||||
if (memories.length > 0) {
|
||||
const memorySection = memories.map((m, i) =>
|
||||
`[${i + 1}] (${m.type}, 重要度: ${m.importance.toFixed(1)}) ${m.content.slice(0, 200)}`,
|
||||
).join('\n');
|
||||
const memoryBlock = `## Relevant Memories (Retrieved)\n${memorySection}`;
|
||||
systemPrompt.dynamicReminders = systemPrompt.dynamicReminders
|
||||
? `${systemPrompt.dynamicReminders}\n\n---\n\n${memoryBlock}`
|
||||
: memoryBlock;
|
||||
log.debug(`[AGENT] Injected ${memories.length} memories into system prompt`);
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
log.warn('[AGENT] sendMessage rejected: invalid sessionId');
|
||||
sendErrorEvent('无效的会话 ID', sessionId ?? '');
|
||||
return { success: false, error: 'Invalid sessionId' };
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('[AGENT] Memory retrieval failed, proceeding without memories:', err);
|
||||
}
|
||||
if (
|
||||
!userMessage ||
|
||||
typeof userMessage !== 'object' ||
|
||||
typeof userMessage.content !== 'string'
|
||||
) {
|
||||
log.warn('[AGENT] sendMessage rejected: invalid userMessage');
|
||||
sendErrorEvent('无效的消息格式', sessionId);
|
||||
return { success: false, error: 'Invalid message format' };
|
||||
}
|
||||
log.info('[AGENT] sendMessage:', sessionId, (userMessage.content ?? '').slice(0, 80));
|
||||
|
||||
// 附件提示注入:用户直接上传的文件/图片,避免 LLM 误以为需要在工作空间查找
|
||||
const attachments = (userMessage as MetonaMessage & { attachments?: Array<{ name: string; type: string }> }).attachments;
|
||||
if (Array.isArray(attachments) && attachments.length > 0) {
|
||||
const attachmentList = attachments.map((att, i) => {
|
||||
const typeLabel = att.type === 'image' ? 'image' : att.type === 'text' ? 'text file' : 'file';
|
||||
const note = att.type === 'image'
|
||||
? 'already provided to you via vision capability — you can SEE it directly, do NOT call view_image or any tool to read it again'
|
||||
: att.type === 'text'
|
||||
? 'content already inlined in the user message, do NOT search in workspace or read it again'
|
||||
: 'uploaded directly by user, do NOT search in workspace';
|
||||
return `${i + 1}. [${typeLabel}] ${att.name} — ${note}`;
|
||||
}).join('\n');
|
||||
|
||||
const attachmentBlock = `## User Attachments (Direct Upload)\nThe following files were uploaded directly by the user to this conversation. They are inline attachments, NOT workspace files:\n${attachmentList}\n\n**IMPORTANT**: Images listed above are already visible to you in this conversation. Do NOT call \`view_image\`, \`read_file\`, or any file tool to read them — doing so wastes a tool call and may fail (they are not workspace files).`;
|
||||
|
||||
systemPrompt.dynamicReminders = systemPrompt.dynamicReminders
|
||||
? `${systemPrompt.dynamicReminders}\n\n---\n\n${attachmentBlock}`
|
||||
: attachmentBlock;
|
||||
|
||||
log.debug(`[AGENT] Injected ${attachments.length} attachment hints into system prompt`);
|
||||
}
|
||||
|
||||
try {
|
||||
// 提示注入检测(安全模块)
|
||||
const injectionResult = promptInjectionDefender.detect(userMessage.content);
|
||||
if (injectionResult.riskScore >= 7) {
|
||||
log.warn('[PromptInjectionDefender] Blocked message:', injectionResult.findings);
|
||||
sendErrorEvent(`Message blocked by prompt injection defense: ${injectionResult.recommendation}`, sessionId);
|
||||
// 发送消息前确保 Adapter 使用最新配置(失败则中止,防止用旧 Provider 的 adapter 发送)
|
||||
if (!ctx.reloadAdapter()) {
|
||||
const errorMsg =
|
||||
'Adapter 加载失败,请检查 LLM 配置(Provider、API Key、Base URL、Model 是否完整)';
|
||||
log.error('[AGENT]', errorMsg);
|
||||
sendErrorEvent(errorMsg, sessionId);
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: 0, totalTokens: 0, durationMs: 0, terminationReason: 'error',
|
||||
totalIterations: 0,
|
||||
totalTokens: 0,
|
||||
durationMs: 0,
|
||||
terminationReason: 'error',
|
||||
});
|
||||
return { success: false, error: 'Message blocked by prompt injection defense' };
|
||||
}
|
||||
if (injectionResult.riskScore >= 4) {
|
||||
log.warn('[PromptInjectionDefender] Suspicious patterns detected:', injectionResult.findings);
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
|
||||
// TRACE 层:记录上下文构建
|
||||
sessionRecorder.recordContextBuilt(sessionId, {
|
||||
tokenCount: estimateMessagesTokens(history),
|
||||
usageRatio: 0,
|
||||
// TRACE 层:开始录制 / TOOL 层:记录会话开始
|
||||
sessionRecorder.startRecording(sessionId);
|
||||
auditService.logSessionStart(sessionId);
|
||||
|
||||
// 保存用户消息到数据库
|
||||
sessionService.saveMessage({
|
||||
sessionId,
|
||||
role: 'user',
|
||||
content: userMessage.content,
|
||||
attachments: (userMessage as MetonaMessage & { attachments?: unknown[] }).attachments,
|
||||
});
|
||||
|
||||
// 启动 Agent Loop(P2-10: 每会话独立引擎)
|
||||
const engine = agentEngineManager.getEngine(sessionId);
|
||||
const output = await engine.runStream(userMessage, sessionId, history, systemPrompt);
|
||||
// P2-11: 分层加载历史——存在滚动摘要时只加载 [摘要 + 近期原文]
|
||||
const history = sessionSummaryService.buildHistoryMessages(sessionId).slice(0, -1);
|
||||
|
||||
// 输出验证(不阻塞响应,仅记录警告)
|
||||
// v0.3.0 修复: 传入 toolResults 和 context,启用事实一致性检查和幻觉检测
|
||||
try {
|
||||
const toolResults = output.iterations
|
||||
.flatMap((step) => step.toolResults ?? [])
|
||||
.map((r) => (typeof r.result === 'string' ? r.result : JSON.stringify(r.result)));
|
||||
const context = [...history, { role: 'user', content: userMessage.content }]
|
||||
.map((m) => `${m.role}: ${m.content}`).join('\n');
|
||||
const validation = await outputValidator.validate(output.finalAnswer, {
|
||||
toolResults: toolResults.length > 0 ? toolResults : undefined,
|
||||
context,
|
||||
// 从工作空间文件构建 System Prompt
|
||||
const workspaceFiles = workspaceService.getFiles();
|
||||
const systemPrompt = contextBuilder.buildSystemPrompt(
|
||||
workspaceFiles,
|
||||
workspaceService.getPath(),
|
||||
);
|
||||
|
||||
// v0.3.18 修复: SOUL.md 为空或不存在时降级到默认身份,向前端发 toast 提示用户
|
||||
if (contextBuilder.isUsingFallbackRole()) {
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message:
|
||||
'未找到 SOUL.md 或内容为空,已使用默认 Metona 身份。可在工作空间根目录创建 SOUL.md 自定义 Agent 人格',
|
||||
});
|
||||
if (!validation.valid || validation.issues.length > 0) {
|
||||
log.warn('[OutputValidator] Validation issues:', validation.issues);
|
||||
}
|
||||
log.debug(`[OutputValidator] Score: ${validation.score}, Valid: ${validation.valid}`);
|
||||
} catch (err) {
|
||||
log.error('[OutputValidator] Validation failed:', err);
|
||||
}
|
||||
|
||||
// 保存每轮迭代的 assistant 消息到数据库(含思考内容和工具调用)
|
||||
for (const step of output.iterations) {
|
||||
if (!step.thought) continue;
|
||||
// 检索与用户消息相关的记忆,注入到 System Prompt 动态区
|
||||
try {
|
||||
const memories = memoryManager.search(userMessage.content, { topK: 5, minImportance: 0.3 });
|
||||
if (memories.length > 0) {
|
||||
const memorySection = memories
|
||||
.map(
|
||||
(m, i) =>
|
||||
`[${i + 1}] (${m.type}, 重要度: ${m.importance.toFixed(1)}) ${m.content.slice(0, 200)}`,
|
||||
)
|
||||
.join('\n');
|
||||
const memoryBlock = `## Relevant Memories (Retrieved)\n${memorySection}`;
|
||||
systemPrompt.dynamicReminders = systemPrompt.dynamicReminders
|
||||
? `${systemPrompt.dynamicReminders}\n\n---\n\n${memoryBlock}`
|
||||
: memoryBlock;
|
||||
log.debug(`[AGENT] Injected ${memories.length} memories into system prompt`);
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('[AGENT] Memory retrieval failed, proceeding without memories:', err);
|
||||
}
|
||||
|
||||
const toolCallsWithResults = step.toolCalls?.map((tc) => {
|
||||
const result = step.toolResults?.find((r) => r.toolCallId === tc.id);
|
||||
return {
|
||||
id: tc.id,
|
||||
name: tc.name,
|
||||
args: tc.args,
|
||||
status: result?.success ? 'success' as const : 'error' as const,
|
||||
result: result?.result,
|
||||
durationMs: result?.durationMs,
|
||||
error: result?.error,
|
||||
};
|
||||
});
|
||||
// 附件提示注入:用户直接上传的文件/图片,避免 LLM 误以为需要在工作空间查找
|
||||
const attachments = (
|
||||
userMessage as MetonaMessage & { attachments?: Array<{ name: string; type: string }> }
|
||||
).attachments;
|
||||
if (Array.isArray(attachments) && attachments.length > 0) {
|
||||
const attachmentList = attachments
|
||||
.map((att, i) => {
|
||||
const typeLabel =
|
||||
att.type === 'image' ? 'image' : att.type === 'text' ? 'text file' : 'file';
|
||||
const note =
|
||||
att.type === 'image'
|
||||
? 'already provided to you via vision capability — you can SEE it directly, do NOT call view_image or any tool to read it again'
|
||||
: att.type === 'text'
|
||||
? 'content already inlined in the user message, do NOT search in workspace or read it again'
|
||||
: 'uploaded directly by user, do NOT search in workspace';
|
||||
return `${i + 1}. [${typeLabel}] ${att.name} — ${note}`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
// 只有当有内容、思考内容或工具调用时才保存
|
||||
if (step.thought.content || step.thought.reasoningContent || toolCallsWithResults?.length) {
|
||||
// C-6 修复: assistant 消息仅有 tool_calls 时 content 必须为 null(而非空字符串)
|
||||
const assistantContent = (toolCallsWithResults?.length && !step.thought.content)
|
||||
? null
|
||||
: step.thought.content;
|
||||
sessionService.saveMessage({
|
||||
const attachmentBlock = `## User Attachments (Direct Upload)\nThe following files were uploaded directly by the user to this conversation. They are inline attachments, NOT workspace files:\n${attachmentList}\n\n**IMPORTANT**: Images listed above are already visible to you in this conversation. Do NOT call \`view_image\`, \`read_file\`, or any file tool to read them — doing so wastes a tool call and may fail (they are not workspace files).`;
|
||||
|
||||
systemPrompt.dynamicReminders = systemPrompt.dynamicReminders
|
||||
? `${systemPrompt.dynamicReminders}\n\n---\n\n${attachmentBlock}`
|
||||
: attachmentBlock;
|
||||
|
||||
log.debug(`[AGENT] Injected ${attachments.length} attachment hints into system prompt`);
|
||||
}
|
||||
|
||||
try {
|
||||
// 提示注入检测(安全模块)
|
||||
const injectionResult = promptInjectionDefender.detect(userMessage.content);
|
||||
if (injectionResult.riskScore >= 7) {
|
||||
log.warn('[PromptInjectionDefender] Blocked message:', injectionResult.findings);
|
||||
sendErrorEvent(
|
||||
`Message blocked by prompt injection defense: ${injectionResult.recommendation}`,
|
||||
sessionId,
|
||||
role: 'assistant',
|
||||
content: assistantContent,
|
||||
reasoningContent: step.thought.reasoningContent || undefined,
|
||||
toolCalls: toolCallsWithResults,
|
||||
iteration: step.iteration,
|
||||
);
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: 0,
|
||||
totalTokens: 0,
|
||||
durationMs: 0,
|
||||
terminationReason: 'error',
|
||||
});
|
||||
return { success: false, error: 'Message blocked by prompt injection defense' };
|
||||
}
|
||||
if (injectionResult.riskScore >= 4) {
|
||||
log.warn(
|
||||
'[PromptInjectionDefender] Suspicious patterns detected:',
|
||||
injectionResult.findings,
|
||||
);
|
||||
}
|
||||
|
||||
// v0.3.0 修复: 保存 tool 结果消息到数据库
|
||||
// OpenAI 兼容 API 要求 assistant 消息有 tool_calls 时,后续必须有对应的 tool 结果消息
|
||||
if (step.toolResults) {
|
||||
for (const result of step.toolResults) {
|
||||
const resultContent = typeof result.result === 'string'
|
||||
? result.result
|
||||
: JSON.stringify(result.result);
|
||||
// TRACE 层:记录上下文构建
|
||||
sessionRecorder.recordContextBuilt(sessionId, {
|
||||
tokenCount: estimateMessagesTokens(history),
|
||||
usageRatio: 0,
|
||||
});
|
||||
|
||||
// 启动 Agent Loop(P2-10: 每会话独立引擎)
|
||||
const engine = agentEngineManager.getEngine(sessionId);
|
||||
const output = await engine.runStream(userMessage, sessionId, history, systemPrompt);
|
||||
|
||||
// 输出验证(不阻塞响应,仅记录警告)
|
||||
// v0.3.0 修复: 传入 toolResults 和 context,启用事实一致性检查和幻觉检测
|
||||
// v0.4.1: warning 及以上级别的 issue 通过 VALIDATION 流事件推送前端展示(此前仅写日志,用户不可感知)
|
||||
try {
|
||||
const toolResults = output.iterations
|
||||
.flatMap((step) => step.toolResults ?? [])
|
||||
.map((r) => (typeof r.result === 'string' ? r.result : JSON.stringify(r.result)));
|
||||
const context = [...history, { role: 'user', content: userMessage.content }]
|
||||
.map((m) => `${m.role}: ${m.content}`)
|
||||
.join('\n');
|
||||
const validation = await outputValidator.validate(output.finalAnswer, {
|
||||
toolResults: toolResults.length > 0 ? toolResults : undefined,
|
||||
context,
|
||||
});
|
||||
if (!validation.valid || validation.issues.length > 0) {
|
||||
log.warn('[OutputValidator] Validation issues:', validation.issues);
|
||||
}
|
||||
log.debug(`[OutputValidator] Score: ${validation.score}, Valid: ${validation.valid}`);
|
||||
|
||||
// v0.4.1: 推送验证结果到前端 — 只推送 warning/error 级(info 级为噪声)
|
||||
const visibleIssues = validation.issues
|
||||
.filter((i) => i.severity === 'warning' || i.severity === 'error')
|
||||
.slice(0, 5);
|
||||
if (visibleIssues.length > 0) {
|
||||
broadcast('agent:streamEvent', {
|
||||
type: MetonaStreamEventType.VALIDATION,
|
||||
requestId: '',
|
||||
sessionId,
|
||||
iteration: output.iterations.length,
|
||||
seq: 0,
|
||||
timestamp: Date.now(),
|
||||
validation: {
|
||||
score: validation.score,
|
||||
issues: visibleIssues.map((i) => ({
|
||||
severity: i.severity,
|
||||
type: i.type,
|
||||
message: i.message,
|
||||
})),
|
||||
},
|
||||
} satisfies MetonaStreamEvent);
|
||||
}
|
||||
} catch (err) {
|
||||
log.error('[OutputValidator] Validation failed:', err);
|
||||
}
|
||||
|
||||
// 保存每轮迭代的 assistant 消息到数据库(含思考内容和工具调用)
|
||||
for (const step of output.iterations) {
|
||||
if (!step.thought) continue;
|
||||
|
||||
const toolCallsWithResults = step.toolCalls?.map((tc) => {
|
||||
const result = step.toolResults?.find((r) => r.toolCallId === tc.id);
|
||||
return {
|
||||
id: tc.id,
|
||||
name: tc.name,
|
||||
args: tc.args,
|
||||
status: result?.success ? ('success' as const) : ('error' as const),
|
||||
result: result?.result,
|
||||
durationMs: result?.durationMs,
|
||||
error: result?.error,
|
||||
};
|
||||
});
|
||||
|
||||
// 只有当有内容、思考内容或工具调用时才保存
|
||||
if (
|
||||
step.thought.content ||
|
||||
step.thought.reasoningContent ||
|
||||
toolCallsWithResults?.length
|
||||
) {
|
||||
// C-6 修复: assistant 消息仅有 tool_calls 时 content 必须为 null(而非空字符串)
|
||||
const assistantContent =
|
||||
toolCallsWithResults?.length && !step.thought.content ? null : step.thought.content;
|
||||
sessionService.saveMessage({
|
||||
sessionId,
|
||||
role: 'tool',
|
||||
content: result.error ?? resultContent,
|
||||
toolResult: result,
|
||||
role: 'assistant',
|
||||
content: assistantContent,
|
||||
reasoningContent: step.thought.reasoningContent || undefined,
|
||||
toolCalls: toolCallsWithResults,
|
||||
iteration: step.iteration,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新 Token 统计
|
||||
if (output.totalTokenUsage.totalTokens > 0) {
|
||||
sessionService.updateTokenUsage(sessionId, output.totalTokenUsage.totalTokens);
|
||||
}
|
||||
|
||||
// 更新 MEMORY.md 时间戳
|
||||
workspaceService.updateMemoryTimestamp();
|
||||
|
||||
// 会话结束:AI 判断本次对话有哪些重要内容需要持久化到 MEMORY.md
|
||||
// 异步执行,不阻塞主流程返回;失败仅记录日志
|
||||
memoryConsolidator
|
||||
.consolidate(userMessage.content, output.finalAnswer, output.iterations)
|
||||
.then((result) => {
|
||||
if (result.appended > 0) {
|
||||
log.info(`[AGENT] Memory consolidated: ${result.appended} entries appended to MEMORY.md`);
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message: `AI 已将 ${result.appended} 条重要记忆写入 MEMORY.md`,
|
||||
});
|
||||
// v0.3.0 修复: 保存 tool 结果消息到数据库
|
||||
// OpenAI 兼容 API 要求 assistant 消息有 tool_calls 时,后续必须有对应的 tool 结果消息
|
||||
if (step.toolResults) {
|
||||
for (const result of step.toolResults) {
|
||||
const resultContent =
|
||||
typeof result.result === 'string' ? result.result : JSON.stringify(result.result);
|
||||
sessionService.saveMessage({
|
||||
sessionId,
|
||||
role: 'tool',
|
||||
content: result.error ?? resultContent,
|
||||
toolResult: result,
|
||||
iteration: step.iteration,
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
log.warn('[AGENT] Memory consolidation failed:', err);
|
||||
}
|
||||
|
||||
// 更新 Token 统计
|
||||
if (output.totalTokenUsage.totalTokens > 0) {
|
||||
sessionService.updateTokenUsage(sessionId, output.totalTokenUsage.totalTokens);
|
||||
}
|
||||
|
||||
// 更新 MEMORY.md 时间戳
|
||||
workspaceService.updateMemoryTimestamp();
|
||||
|
||||
// 会话结束:AI 判断本次对话有哪些重要内容需要持久化到 MEMORY.md
|
||||
// 异步执行,不阻塞主流程返回;失败仅记录日志
|
||||
memoryConsolidator
|
||||
.consolidate(userMessage.content, output.finalAnswer, output.iterations)
|
||||
.then((result) => {
|
||||
if (result.appended > 0) {
|
||||
log.info(
|
||||
`[AGENT] Memory consolidated: ${result.appended} entries appended to MEMORY.md`,
|
||||
);
|
||||
broadcast('toast:show', {
|
||||
type: 'info',
|
||||
message: `AI 已将 ${result.appended} 条重要记忆写入 MEMORY.md`,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
log.warn('[AGENT] Memory consolidation failed:', err);
|
||||
});
|
||||
|
||||
// TOOL 层:记录会话结束 / TRACE 层:停止录制
|
||||
auditService.logSessionEnd({
|
||||
sessionId,
|
||||
totalIterations: output.iterations.length,
|
||||
totalTokens: output.totalTokenUsage.totalTokens,
|
||||
durationMs: output.durationMs,
|
||||
terminationReason: output.terminationReason,
|
||||
});
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: output.iterations.length,
|
||||
totalTokens: output.totalTokenUsage.totalTokens,
|
||||
durationMs: output.durationMs,
|
||||
terminationReason: output.terminationReason,
|
||||
});
|
||||
|
||||
// TOOL 层:记录会话结束 / TRACE 层:停止录制
|
||||
auditService.logSessionEnd({
|
||||
sessionId,
|
||||
totalIterations: output.iterations.length,
|
||||
totalTokens: output.totalTokenUsage.totalTokens,
|
||||
durationMs: output.durationMs,
|
||||
terminationReason: output.terminationReason,
|
||||
});
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: output.iterations.length,
|
||||
totalTokens: output.totalTokenUsage.totalTokens,
|
||||
durationMs: output.durationMs,
|
||||
terminationReason: output.terminationReason,
|
||||
});
|
||||
// P2-11: 会话结束后评估滚动摘要(fire-and-forget,失败仅记录)
|
||||
sessionSummaryService.maybeSummarize(sessionId).catch((err) => {
|
||||
log.warn('[AGENT] Session summary generation failed:', err);
|
||||
});
|
||||
|
||||
// P2-11: 会话结束后评估滚动摘要(fire-and-forget,失败仅记录)
|
||||
sessionSummaryService.maybeSummarize(sessionId).catch((err) => {
|
||||
log.warn('[AGENT] Session summary generation failed:', err);
|
||||
});
|
||||
log.info(
|
||||
`[AGENT] Completed: ${output.terminationReason}, ${output.iterations.length} iterations, ${output.durationMs}ms`,
|
||||
);
|
||||
|
||||
log.info(`[AGENT] Completed: ${output.terminationReason}, ${output.iterations.length} iterations, ${output.durationMs}ms`);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
log.error('[AGENT] Error:', error);
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
log.error('[AGENT] Error:', error);
|
||||
// TOOL 层:记录错误
|
||||
auditService.log({
|
||||
sessionId,
|
||||
eventType: 'error',
|
||||
actor: 'agent',
|
||||
target: 'agent_loop',
|
||||
details: { error: (error as Error).message },
|
||||
outcome: 'error',
|
||||
});
|
||||
|
||||
// TOOL 层:记录错误
|
||||
auditService.log({
|
||||
sessionId,
|
||||
eventType: 'error',
|
||||
actor: 'agent',
|
||||
target: 'agent_loop',
|
||||
details: { error: (error as Error).message },
|
||||
outcome: 'error',
|
||||
});
|
||||
// TRACE 层:停止录制
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: 0,
|
||||
totalTokens: 0,
|
||||
durationMs: 0,
|
||||
terminationReason: 'error',
|
||||
});
|
||||
|
||||
// TRACE 层:停止录制
|
||||
sessionRecorder.stopRecording(sessionId, {
|
||||
totalIterations: 0, totalTokens: 0, durationMs: 0, terminationReason: 'error',
|
||||
});
|
||||
// 发送错误事件到 UI
|
||||
const metonaError: MetonaError = {
|
||||
code: MetonaErrorCode.UNKNOWN,
|
||||
message: (error as Error).message,
|
||||
retryable: false,
|
||||
};
|
||||
broadcast('agent:streamEvent', {
|
||||
type: MetonaStreamEventType.ERROR,
|
||||
requestId: '',
|
||||
sessionId,
|
||||
iteration: 0,
|
||||
seq: 0,
|
||||
timestamp: Date.now(),
|
||||
error: metonaError,
|
||||
} satisfies MetonaStreamEvent);
|
||||
|
||||
// 发送错误事件到 UI
|
||||
const metonaError: MetonaError = {
|
||||
code: MetonaErrorCode.UNKNOWN,
|
||||
message: (error as Error).message,
|
||||
retryable: false,
|
||||
};
|
||||
broadcast('agent:streamEvent', {
|
||||
type: MetonaStreamEventType.ERROR,
|
||||
requestId: '', sessionId, iteration: 0, seq: 0, timestamp: Date.now(),
|
||||
error: metonaError,
|
||||
} satisfies MetonaStreamEvent);
|
||||
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
});
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// ===== 中断会话 =====
|
||||
|
||||
|
||||
Reference in New Issue
Block a user