-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix: Preserve first message during conversation condensing #7910
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
Conversation
When users invoke slash commands, the command content is appended to the first message. This fix ensures the first message is always preserved during condensing operations, preventing loss of vital context like slash command instructions. - Always preserve the first message intact during summarization - Only summarize messages from index 1 onwards - Maintains command context and initial user request throughout the conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this fix! The implementation correctly addresses the issue of preserving the first message during conversation condensing, which is crucial for maintaining slash command context. The approach elegantly mirrors the existing pattern in truncateConversation for consistency. I've left a few suggestions for consideration below.
| const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP)) | ||
|
|
||
| // Always preserve the first message (which may contain slash command content) | ||
| const firstMessage = messages[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a safety check here to handle the edge case where the messages array might be empty? While unlikely in practice, it would make the code more robust. Consider checking if messages.length === 0 before accessing messages[0].
| const response: SummarizeResponse = { messages, cost: 0, summary: "" } | ||
| const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP)) | ||
|
|
||
| // Always preserve the first message (which may contain slash command content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider enhancing this comment to explicitly mention why we preserve the first message: 'Always preserve the first message (which may contain slash command content, initial user request, and important context)'. This would help future maintainers understand the critical importance of this preservation.
| // Always preserve the first message (which may contain slash command content) | ||
| const firstMessage = messages[0] | ||
| // Get messages to summarize, excluding the first message and last N messages | ||
| const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(1, -N_MESSAGES_TO_KEEP)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here creates an implicit minimum of 4 messages (first + 3 to keep). Would it be clearer to extract this as a constant like MIN_MESSAGES_FOR_CONDENSING = N_MESSAGES_TO_KEEP + 1? Then the condition could be more explicit about the requirement.
|
|
||
| const newMessages = [...messages.slice(0, -N_MESSAGES_TO_KEEP), summaryMessage, ...keepMessages] | ||
| // Reconstruct messages: [first message, summary, last N messages] | ||
| const newMessages = [firstMessage, summaryMessage, ...keepMessages] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work maintaining consistency with the structure. The reconstruction clearly shows the preserved first message, followed by the summary, and then the recent messages.
When users invoke slash commands, the command content is appended to the first message. This fix ensures the first message is always preserved during condensing operations, preventing loss of vital context like slash command instructions.
Changes
Testing
src/core/condense/__tests__/condense.spec.tsProblem
When a user types a slash command (e.g.,
/prr), the command content is appended to the first message viaparseMentions. However, when the conversation is condensed, this first message was replaced with a summary, losing the vital command content that was appended.Solution
Modified the
summarizeConversationfunction insrc/core/condense/index.tsto always preserve the first message intact, similar to howtruncateConversationinsrc/core/sliding-window/index.tsdoes.Important
Preserve the first message during conversation summarization to retain slash command content.
summarizeConversationinindex.tsnow preserves the first message during summarization.condense.spec.tsto verify first message preservation, handling of slash commands, and complex content.summarizeConversationto preserve the first message, similar totruncateConversation.This description was created by
for 3f504e7. You can customize this summary. It will automatically update as commits are pushed.