Skip to content

Commit 1c5ee11

Browse files
committed
fix: include user message in auto-capture when prePromptMessageCount excludes it
Closes #1248 When OpenClaw's prePromptMessageCount points to an index that excludes the current turn's user message, the VLM extractor receives only assistant-role text (a recap/acknowledgment), yielding 0 extractable memories. This adds an ensureLastUserMessage parameter to extractNewTurnTexts. When true, if no user message appears in the extracted window, the last user message in the transcript is prepended to results. Adapted from the original PR (#1256) which targeted the now-renamed openclaw-memory-plugin directory.
1 parent 7f05828 commit 1c5ee11

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

examples/openclaw-plugin/context-engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ export function createMemoryOpenVikingContextEngine(params: {
806806
? afterTurnParams.prePromptMessageCount
807807
: 0;
808808

809-
const { texts: newTexts, newCount } = extractNewTurnTexts(messages, start);
809+
const { texts: newTexts, newCount } = extractNewTurnTexts(messages, start, true);
810810

811811
if (newTexts.length === 0) {
812812
diag("afterTurn_skip", OVSessionId, {

examples/openclaw-plugin/text-utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ function formatToolResultContent(content: unknown): string {
419419
export function extractNewTurnTexts(
420420
messages: unknown[],
421421
startIndex: number,
422+
ensureLastUserMessage?: boolean,
422423
): { texts: string[]; newCount: number } {
423424
const texts: string[] = [];
424425
let count = 0;
@@ -452,6 +453,36 @@ export function extractNewTurnTexts(
452453
}
453454
}
454455
}
456+
// Defensive fix: ensure the last user message is included even if
457+
// startIndex skipped it (OpenClaw prePromptMessageCount off-by-one)
458+
// See: https://github.com/volcengine/OpenViking/issues/1248
459+
if (ensureLastUserMessage && messages.length > 0) {
460+
let lastUserText: string | null = null;
461+
for (let i = messages.length - 1; i >= 0; i--) {
462+
const msg = messages[i] as Record<string, unknown>;
463+
if (!msg || typeof msg !== "object") continue;
464+
if (msg.role === "user") {
465+
const content = msg.content;
466+
if (typeof content === "string" && content.trim()) {
467+
lastUserText = `[user]: ${content.trim()}`;
468+
} else if (Array.isArray(content)) {
469+
for (const block of content) {
470+
const b = block as Record<string, unknown>;
471+
if (b?.type === "text" && typeof b.text === "string" && b.text.trim()) {
472+
lastUserText = `[user]: ${b.text.trim()}`;
473+
break;
474+
}
475+
}
476+
}
477+
break;
478+
}
479+
}
480+
if (lastUserText && !texts.some((t) => t.startsWith("[user]:"))) {
481+
texts.unshift(lastUserText);
482+
count++;
483+
}
484+
}
485+
455486
return { texts, newCount: count };
456487
}
457488

0 commit comments

Comments
 (0)