mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 09:30:43 +00:00
fix(agents): harden replay normalization guards
This commit is contained in:
committed by
Ayaan Zaidi
parent
3105597f8f
commit
f58dd0f9b6
@@ -40,6 +40,7 @@ import {
|
||||
type AssistantUsageSnapshot,
|
||||
type UsageLike,
|
||||
} from "../usage.js";
|
||||
import { log } from "./logger.js";
|
||||
import { dropThinkingBlocks } from "./thinking.js";
|
||||
|
||||
const INTER_SESSION_PREFIX_BASE = "[Inter-session message]";
|
||||
@@ -235,6 +236,12 @@ export function normalizeAssistantReplayContent(messages: AgentMessage[]): Agent
|
||||
if (!message || message.role !== "assistant" || Array.isArray(message.content)) {
|
||||
continue;
|
||||
}
|
||||
if (typeof message.content !== "string") {
|
||||
log.warn(
|
||||
`normalizeAssistantReplayContent: repairing malformed assistant content ` +
|
||||
`(index=${i}, type=${typeof message.content})`,
|
||||
);
|
||||
}
|
||||
out[i] = {
|
||||
...(message as unknown as Record<string, unknown>),
|
||||
content:
|
||||
|
||||
@@ -1144,9 +1144,11 @@ export async function runEmbeddedAttempt(
|
||||
throw new Error("Embedded agent session missing");
|
||||
}
|
||||
const activeSession = session;
|
||||
const baseConvertToLlm = activeSession.agent.convertToLlm.bind(activeSession.agent);
|
||||
activeSession.agent.convertToLlm = async (messages) =>
|
||||
await baseConvertToLlm(normalizeAssistantReplayContent(messages));
|
||||
if (typeof activeSession.agent.convertToLlm === "function") {
|
||||
const baseConvertToLlm = activeSession.agent.convertToLlm.bind(activeSession.agent);
|
||||
activeSession.agent.convertToLlm = async (messages) =>
|
||||
await baseConvertToLlm(normalizeAssistantReplayContent(messages));
|
||||
}
|
||||
let prePromptMessageCount = activeSession.messages.length;
|
||||
abortSessionForYield = () => {
|
||||
yieldAbortSettled = Promise.resolve(activeSession.abort());
|
||||
@@ -2193,7 +2195,7 @@ export async function runEmbeddedAttempt(
|
||||
activeSession.agent.state.messages = normalizedReplayMessages;
|
||||
}
|
||||
finalPromptText = effectivePrompt;
|
||||
const btwSnapshotMessages = activeSession.messages.slice(-MAX_BTW_SNAPSHOT_MESSAGES);
|
||||
const btwSnapshotMessages = normalizedReplayMessages.slice(-MAX_BTW_SNAPSHOT_MESSAGES);
|
||||
updateActiveEmbeddedRunSnapshot(params.sessionId, {
|
||||
transcriptLeafId,
|
||||
messages: btwSnapshotMessages,
|
||||
|
||||
Reference in New Issue
Block a user