Skip to content

Commit ece1d43

Browse files
committed
fix(ai-chat): avoid resuming stale conversations after 10 minutes
1 parent cae4a94 commit ece1d43

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/components/ai/AiChatPopover.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import {
2424
PopoverTrigger
2525
} from "@flanksource-ui/components/ui/popover";
2626

27+
const ACTIVE_CONVERSATION_MAX_AGE_MS = 10 * 60 * 1000;
28+
29+
function isConversationRecent(
30+
conversation?: AIConversationRecord
31+
): conversation is AIConversationRecord {
32+
if (!conversation) {
33+
return false;
34+
}
35+
36+
return Date.now() - conversation.updatedAt <= ACTIVE_CONVERSATION_MAX_AGE_MS;
37+
}
38+
2739
type AiChatPopoverContextValue = {
2840
open: boolean;
2941
setOpen: (open: boolean) => void;
@@ -125,14 +137,14 @@ export function AiChatPopoverProvider({
125137
)
126138
: undefined) ?? storedConversations[0];
127139

128-
const nextConversationId =
129-
activeConversationId ?? storedActiveConversation?.id;
140+
if (isConversationRecent(storedActiveConversation)) {
141+
const nextConversationId =
142+
activeConversationId ?? storedActiveConversation.id;
130143

131-
if (nextConversationId) {
132144
replaceChat(
133145
new Chat<UIMessage>({
134146
id: nextConversationId,
135-
messages: storedActiveConversation?.messages ?? []
147+
messages: storedActiveConversation.messages
136148
}),
137149
{ userAction: false }
138150
);

0 commit comments

Comments
 (0)