Fix several bugs in prompt history (#734)

Co-authored-by: Marat Boshernitsan <maratb@google.com>
This commit is contained in:
Marat Boshernitsan
2025-06-03 23:01:26 -07:00
committed by GitHub
parent c313762ba0
commit 7de790fbf2
4 changed files with 136 additions and 22 deletions

View File

@@ -43,8 +43,22 @@ export function useHistory(): UseHistoryManagerReturn {
(itemData: Omit<HistoryItem, 'id'>, baseTimestamp: number): number => {
const id = getNextMessageId(baseTimestamp);
const newItem: HistoryItem = { ...itemData, id } as HistoryItem;
setHistory((prevHistory) => [...prevHistory, newItem]);
return id; // Return the generated ID
setHistory((prevHistory) => {
if (prevHistory.length > 0) {
const lastItem = prevHistory[prevHistory.length - 1];
// Prevent adding duplicate consecutive user messages
if (
lastItem.type === 'user' &&
newItem.type === 'user' &&
lastItem.text === newItem.text
) {
return prevHistory; // Don't add the duplicate
}
}
return [...prevHistory, newItem];
});
return id; // Return the generated ID (even if not added, to keep signature)
},
[getNextMessageId],
);