Skip to content

Commit

Permalink
reset initialMessage when duplicate the conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoliduena committed Mar 3, 2025
1 parent 668dcaf commit 8b79614
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export enum FlyoutPositionMode {

export function ChatFlyout({
initialTitle,
initialMessages,
initialMessages: initialMessagesFromProps,
initialFlyoutPositionMode,
onFlyoutPositionModeChange,
isOpen,
Expand All @@ -69,6 +69,7 @@ export function ChatFlyout({
const knowledgeBase = useKnowledgeBase();

const [conversationId, setConversationId] = useState<string | undefined>(undefined);
const [initialMessages, setInitialMessages] = useState(initialMessagesFromProps);

const [flyoutPositionMode, setFlyoutPositionMode] = useState<FlyoutPositionMode>(
initialFlyoutPositionMode || FlyoutPositionMode.OVERLAY
Expand All @@ -90,6 +91,7 @@ export function ChatFlyout({

const onConversationDuplicate = (conversation: Conversation) => {
conversationList.conversations.refresh();
setInitialMessages([]);
setConversationId(conversation.conversation.id);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ describe('useConversation', () => {
jest.clearAllMocks();
});

describe('with initial messages and a conversation id', () => {
it('throws an error', () => {
expect(() =>
renderHook(useConversation, {
initialProps: {
chatService: mockChatService,
connectorId: 'my-connector',
initialMessages: [
{
'@timestamp': new Date().toISOString(),
message: { content: '', role: MessageRole.User },
},
],
initialConversationId: 'foo',
onConversationDuplicate: jest.fn(),
},
wrapper,
})
).toThrow(/Cannot set initialMessages if initialConversationId is set/);
});
});

describe('without initial messages and a conversation id', () => {
beforeEach(() => {
hookResult = renderHook(useConversation, {
Expand Down Expand Up @@ -388,7 +410,7 @@ describe('useConversation', () => {
result.current.saveTitle('my-new-title');
} catch (e) {
expect(e).toBeInstanceOf(Error);
expect(e.message).toBe('Cannot save title if conversation is not stored');
expect(e.message).toBe('Cannot set initialMessages if initialConversationId is set');
done();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export function useConversation({
const initialMessages = useOnce(initialMessagesFromProps);
const initialTitle = useOnce(initialTitleFromProps);

if (initialMessages.length && initialConversationId) {
throw new Error('Cannot set initialMessages if initialConversationId is set');
}

const update = (nextConversationObject: Conversation) => {
return service
.callApi(`PUT /internal/observability_ai_assistant/conversation/{conversationId}`, {
Expand Down Expand Up @@ -221,12 +225,7 @@ export function useConversation({
}
: currentUser,
state,
next: (_messages: Message[]) =>
next(_messages, (error) => {
if (error) {
conversation.refresh();
}
}),
next: (_messages: Message[]) => next(_messages, () => conversation.refresh()),
stop,
messages,
saveTitle: (title: string) => {
Expand Down

0 comments on commit 8b79614

Please sign in to comment.