fix: memory工具结果丢失 — formatToolResultForModel丢弃read_all的entries; 历史消息剔除用户消息
This commit is contained in:
@@ -988,7 +988,8 @@ function buildHistoryMessages(msgs: ChatMessage[], maxCount = 20): OllamaMessage
|
||||
const result: OllamaMessage[] = [];
|
||||
|
||||
for (const msg of msgs) {
|
||||
if (msg.role !== 'user' && msg.role !== 'assistant' && msg.role !== 'system') continue;
|
||||
// 只注入 assistant 和 system 消息(用户消息仅当前发送的一条,由 runAgentLoop userContent 单独传入)
|
||||
if (msg.role !== 'assistant' && msg.role !== 'system') continue;
|
||||
if (result.length >= maxCount) break;
|
||||
|
||||
let content = msg.content || '';
|
||||
@@ -999,19 +1000,7 @@ function buildHistoryMessages(msgs: ChatMessage[], maxCount = 20): OllamaMessage
|
||||
continue;
|
||||
}
|
||||
|
||||
if (msg.role === 'user') {
|
||||
// 用户消息:注入文件内容
|
||||
if (msg._fileContents?.length) {
|
||||
const fileParts = buildFileContentParts(msg._fileContents);
|
||||
content = content ? content + '\n\n---\n' + fileParts.join('\n\n---\n') : fileParts.join('\n\n---\n');
|
||||
}
|
||||
result.push({
|
||||
role: 'user',
|
||||
content,
|
||||
...(msg.images?.length && { images: msg.images }),
|
||||
...((msg as any)._videoFrames?.length && { images: [...(msg.images || []), ...(msg as any)._videoFrames.map((f: any) => f.base64)] }),
|
||||
});
|
||||
} else if (msg.role === 'assistant') {
|
||||
if (msg.role === 'assistant') {
|
||||
// 构建 assistant 消息
|
||||
const assistantMsg: OllamaMessage = {
|
||||
role: 'assistant',
|
||||
|
||||
@@ -332,10 +332,12 @@ function formatToolResultForModel(toolName: string, result: ToolResult): string
|
||||
}
|
||||
|
||||
case 'memory': {
|
||||
// 仅处理去重文字信号(触发 handleObserving 的⛔终止),
|
||||
// 正常结果交给 default 分支保留全部字段(entries/total/results等)
|
||||
if ((result as any).duplicate) {
|
||||
return `⚠️ 重复添加: ${(result as any).message || '相同内容已存在'}\n✅ 记忆操作已全部完成,不要再调用 memory 工具,直接输出最终回答。`;
|
||||
}
|
||||
return JSON.stringify({ success: true, action: result.action, type: (result as any).type, content: (result as any).content });
|
||||
// fall through to default
|
||||
}
|
||||
|
||||
default: {
|
||||
|
||||
Reference in New Issue
Block a user