Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Double Responses from Continue Action #1606

Merged
merged 5 commits into from
Dec 31, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 61 additions & 32 deletions packages/plugin-bootstrap/src/actions/continue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,61 @@ export const continueAction: Action = {
options: any,
callback: HandlerCallback
) => {
if (
message.content.text.endsWith("?") ||
message.content.text.endsWith("!")
) {
return;
}

if (!state) {
state = (await runtime.composeState(message)) as State;
}

state = await runtime.updateRecentMessageState(state);

// Get the agent's recent messages
const agentMessages = state.recentMessagesData
.filter((m: { userId: any }) => m.userId === runtime.agentId)
.sort((a: Memory, b: Memory) => {
// Sort by timestamp if available, assuming newer messages have higher timestamps
const aTime = a.createdAt || 0;
const bTime = b.createdAt || 0;
return bTime - aTime;
});

// Check for immediate double response (responding twice in a row to the same message)
const lastAgentMessage = agentMessages[0];

if (lastAgentMessage?.content?.inReplyTo === message.id) {
// If our last message was already a response to this message, only allow continue if:
// 1. The last message had a CONTINUE action
// 2. We haven't hit the maxContinuesInARow limit
const continueCount = agentMessages
.filter((m: Memory) => m.content?.inReplyTo === message.id)
.filter((m: Memory) => m.content?.action === 'CONTINUE')
.length;

if (continueCount >= maxContinuesInARow) {
elizaLogger.log(`[CONTINUE] Max continues (${maxContinuesInARow}) reached for this message chain`);
return;
}

if (lastAgentMessage.content?.action !== 'CONTINUE') {
elizaLogger.log(`[CONTINUE] Last message wasn't a CONTINUE, preventing double response`);
return;
}
}

// Check if our last message or message ended with a question/exclamation and warrants a stop
if ((lastAgentMessage && lastAgentMessage.content.text &&
(lastAgentMessage.content.text.endsWith("?") ||
lastAgentMessage.content.text.endsWith("!"))) || (message.content.text.endsWith("?") || message.content.text.endsWith("!"))) {
elizaLogger.log(`[CONTINUE] My last message had question/exclamation. Not proceeding.`);
return;
}

// Prevent exact duplicate messages
const messageExists = agentMessages
.slice(0, maxContinuesInARow + 1)
.some((m: { content: any }) => m.content.text === message.content.text);

if (messageExists) {
return;
}

async function _shouldContinue(state: State): Promise<boolean> {
// If none of the above conditions are met, use the generateText to decide
const shouldRespondContext = composeContext({
Expand All @@ -120,12 +162,14 @@ export const continueAction: Action = {
return response;
}

// Use AI to determine if we should continue
const shouldContinue = await _shouldContinue(state);
if (!shouldContinue) {
elizaLogger.log("Not elaborating, returning");
elizaLogger.log("[CONTINUE] Not elaborating, returning");
return;
}

// Generate and send response
const context = composeContext({
state,
template:
Expand All @@ -150,32 +194,17 @@ export const continueAction: Action = {
type: "continue",
});

// prevent repetition
const messageExists = state.recentMessagesData
.filter((m: { userId: any }) => m.userId === runtime.agentId)
.slice(0, maxContinuesInARow + 1)
.some((m: { content: any }) => m.content === message.content);

if (messageExists) {
return;
}

await callback(response);

// if the action is CONTINUE, check if we are over maxContinuesInARow
// Check if we need to clear the CONTINUE action
if (response.action === "CONTINUE") {
const agentMessages = state.recentMessagesData
.filter((m: { userId: any }) => m.userId === runtime.agentId)
.map((m: { content: any }) => (m.content as Content).action);
const continueCount = agentMessages
.slice(0, maxContinuesInARow)
.filter((m: Memory) => m.content?.action === 'CONTINUE')
.length;

const lastMessages = agentMessages.slice(0, maxContinuesInARow);
if (lastMessages.length >= maxContinuesInARow) {
const allContinues = lastMessages.every(
(m: string | undefined) => m === "CONTINUE"
);
if (allContinues) {
response.action = null;
}
if (continueCount >= maxContinuesInARow - 1) { // -1 because we're about to add another
response.action = null;
}
}

Expand Down Expand Up @@ -598,4 +627,4 @@ export const continueAction: Action = {
},
],
] as ActionExample[][],
} as Action;
} as Action;
Loading