feat: 升级至 v0.3.1 — 全量代码审计修复 + 安全增强
本次升级基于完整代码审查,修复 Critical/High/Medium/Low 四级共 96 项问题, 并通过返工审计修复 10 项遗留问题,tsc 双端类型检查零错误。 Critical (10/10 完成): - C-4: command.ts 接入 shell-quote 进行 token-level 注入检测,替代原有正则匹配 可防御 r"m" -rf /、$'rm'、$(echo rm) 等字符串拼接绕过 High (11/11 完成): - 竞态保护、Promise.allSettled、AbortController 资源泄漏、IPC 参数校验等 Medium (55/55 完成): - 事务保护、敏感数据脱敏、枚举校验、MUI v9 Stack prop 迁移、 React 组件 cancelled 标志、类型收窄等 Low (20/20 完成): - 辅助方法提取(flushToolCallBuffer/scoreAndPushMemory/tryAddColumn 等) - nanoid 统一替代 Date.now()+Math.random() - confirm() 替换为 MUI Dialog、useMemo 缓存、魔法数字命名化等 返工审计修复 (10/10 完成): - L-11: LogsSettings 残留的原生 confirm()/alert() 全部替换为 MUI Dialog/Alert - M-53: MemoryViewer handleSearch 独立 ref,修复 searching 状态卡死 - M-42: 脱敏短值(length <= 4)泄露修复 - M-47: tasks:update 补全 title/description 类型校验 - L-9: ollama.adapter 非流式路径 nanoid 统一 - M-45: audit:query limit 策略与 memory:listAll 一致化 - SettingsModal handleConfirmRemove 补全 try/catch + loadServers cleanup - L-15: CommandPalette useMemo 补全 sessions 响应式依赖 - useAgentStream 事件类型补全 seq/timestamp 字段 新增依赖: shell-quote + @types/shell-quote 版本号: 0.3.0 -> 0.3.1
This commit is contained in:
@@ -12,6 +12,52 @@ import { nanoid } from 'nanoid';
|
||||
import type { MetonaStreamEvent, MetonaTokenUsage } from '../../types';
|
||||
import { MetonaStreamEventType } from '../../types';
|
||||
|
||||
/**
|
||||
* L-4 修复: 提取 flushToolCallBuffer 辅助函数,消除 [DONE] 分支和 finish_reason='tool_calls' 分支的重复代码
|
||||
*
|
||||
* 遍历工具调用缓冲区,对每个缓冲的工具调用:
|
||||
* 1. JSON.parse argsBuffer(失败则跳过)
|
||||
* 2. yield 一个 TOOL_CALL_COMPLETE 事件
|
||||
* 3. 清空缓冲区
|
||||
*
|
||||
* @param toolCallsBuffer - 工具调用缓冲区(index → { name, argsBuffer })
|
||||
* @param requestId - 请求 ID
|
||||
* @param sessionId - 会话 ID
|
||||
* @param iteration - 当前迭代轮次
|
||||
* @param seqRef - seq 计数器引用(递增)
|
||||
* @yields MetonaStreamEvent
|
||||
*/
|
||||
function* flushToolCallBuffer(
|
||||
toolCallsBuffer: Map<number, { name: string; argsBuffer: string }>,
|
||||
requestId: string,
|
||||
sessionId: string,
|
||||
iteration: number,
|
||||
seqRef: { seq: number },
|
||||
): Generator<MetonaStreamEvent> {
|
||||
for (const [, buf] of toolCallsBuffer) {
|
||||
try {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCall: {
|
||||
id: `tc_${nanoid(8)}`,
|
||||
name: buf.name,
|
||||
args: buf.argsBuffer ? JSON.parse(buf.argsBuffer) : {},
|
||||
iteration,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
// JSON 解析失败,跳过该工具调用
|
||||
}
|
||||
}
|
||||
toolCallsBuffer.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 OpenAI 兼容 SSE 流式响应
|
||||
*
|
||||
@@ -29,7 +75,7 @@ export async function* parseSSEStream(
|
||||
): AsyncGenerator<MetonaStreamEvent> {
|
||||
const reader = responseBody.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let seq = 0;
|
||||
const seqRef = { seq: 0 };
|
||||
let buffer = '';
|
||||
|
||||
// 工具调用缓冲区:index → { name, argsBuffer }
|
||||
@@ -50,36 +96,15 @@ export async function* parseSSEStream(
|
||||
|
||||
// 流结束
|
||||
if (data === '[DONE]') {
|
||||
// 将缓冲区中未完成拼接的工具调用发送
|
||||
for (const [index, buf] of toolCallsBuffer) {
|
||||
try {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCall: {
|
||||
id: `tc_${nanoid(8)}`,
|
||||
name: buf.name,
|
||||
args: buf.argsBuffer ? JSON.parse(buf.argsBuffer) : {},
|
||||
iteration,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
// JSON 解析失败,跳过
|
||||
}
|
||||
}
|
||||
toolCallsBuffer.clear();
|
||||
// L-4 修复: 使用 flushToolCallBuffer 替代重复的遍历代码
|
||||
yield* flushToolCallBuffer(toolCallsBuffer, requestId, sessionId, iteration, seqRef);
|
||||
|
||||
yield {
|
||||
type: MetonaStreamEventType.DONE,
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
return;
|
||||
@@ -96,7 +121,7 @@ export async function* parseSSEStream(
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: delta.content,
|
||||
};
|
||||
@@ -109,7 +134,7 @@ export async function* parseSSEStream(
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: delta.reasoning_content,
|
||||
};
|
||||
@@ -131,7 +156,7 @@ export async function* parseSSEStream(
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCallDelta: {
|
||||
index: idx,
|
||||
@@ -158,7 +183,7 @@ export async function* parseSSEStream(
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
seq: seqRef.seq++,
|
||||
timestamp: Date.now(),
|
||||
usage,
|
||||
};
|
||||
@@ -167,28 +192,8 @@ export async function* parseSSEStream(
|
||||
// 非 [DONE] 但 finish_reason 为 tool_calls 时提前 flush 缓冲区
|
||||
const finishReason = chunk.choices?.[0]?.finish_reason as string | undefined;
|
||||
if (finishReason === 'tool_calls') {
|
||||
for (const [index, buf] of toolCallsBuffer) {
|
||||
try {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||||
requestId,
|
||||
sessionId,
|
||||
iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCall: {
|
||||
id: `tc_${nanoid(8)}`,
|
||||
name: buf.name,
|
||||
args: buf.argsBuffer ? JSON.parse(buf.argsBuffer) : {},
|
||||
iteration,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
// JSON 解析失败,跳过
|
||||
}
|
||||
}
|
||||
toolCallsBuffer.clear();
|
||||
// L-4 修复: 使用 flushToolCallBuffer 替代重复的遍历代码
|
||||
yield* flushToolCallBuffer(toolCallsBuffer, requestId, sessionId, iteration, seqRef);
|
||||
}
|
||||
} catch {
|
||||
// 跳过解析失败的行
|
||||
|
||||
Reference in New Issue
Block a user