Skip to content

/clear only clears the UI and keeps previous messages in model context #1549

Description

@QiuLsG

Description

The /clear slash command makes the current chat look empty, but it only clears the client-side message state. The persisted messages remain in the database and are loaded into the model context again when the user sends the next message in the same chat.

This makes the visible conversation state disagree with the context sent to the model.

Steps to reproduce

  1. Start a chat and send a memorable fact, for example: My favorite color is teal.
  2. Run the /clear slash command.
  3. Confirm that the messages disappear from the UI.
  4. In the same chat, ask: What is my favorite color?
  5. Refresh the page and observe that the old messages also return.

Expected behavior

Running /clear should clear the context represented by the UI. A subsequent request in that chat should not include messages that were cleared, and refreshing should not restore them.

Actual behavior

The model still receives the old messages and can answer from the supposedly cleared context. Refreshing restores the old conversation in the UI.

Cause

The command handler only calls setMessages(() => []):

case "clear":
setMessages(() => []);
break;

The next request for the same chat reloads all persisted messages and prepends them to the new message:

const chat = await getChatById({ id });
let messagesFromDb: DBMessage[] = [];
let titlePromise: Promise<string> | null = null;
if (chat) {
if (chat.userId !== session.user.id) {
return new ChatbotError("forbidden:chat").toResponse();
}
messagesFromDb = await getMessagesByChatId({ id });

} else {
uiMessages = [
...convertToUIMessages(messagesFromDb),
message as ChatMessage,
];

Possible fix

Either clear the persisted message history for the current chat (including related records where appropriate), or make /clear start a new chat. In either case, the visible state, persisted state, and model context should remain consistent.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions