Skip to content

Commit

Permalink
feat(chat): regenerate on empty assistant message (#2783) (#2788)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaBondar authored Dec 11, 2024
1 parent ef330b7 commit 8e535a7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
66 changes: 16 additions & 50 deletions apps/chat-e2e/src/tests/workWithModels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
MockedChatApiResponseBodies,
Theme,
} from '@/src/testData';
import { Cursors, Overflow, Styles } from '@/src/ui/domData';
import { Overflow } from '@/src/ui/domData';
import { keys } from '@/src/ui/keyboard';
import { GeneratorUtil, ModelsUtil } from '@/src/utils';
import { expect } from '@playwright/test';
Expand Down Expand Up @@ -121,7 +121,7 @@ dialTest(

await expect
.soft(
chatMessages.regenerate.getElementLocator(),
sendMessage.regenerate.getElementLocator(),
ExpectedMessages.regenerateIsAvailable,
)
.toBeVisible();
Expand Down Expand Up @@ -384,8 +384,8 @@ dialTest(
chat,
setTestIds,
chatMessages,
baseAssertion,
sendMessage,
page,
tooltip,
localStorageManager,
iconApiHelper,
Expand Down Expand Up @@ -431,59 +431,25 @@ dialTest(
undefined,
expectedModelIcon,
);
await expect
.soft(
chatMessages.regenerate.getElementLocator(),
ExpectedMessages.regenerateIsAvailable,
)
.toBeVisible();
await baseAssertion.assertElementState(
chatMessages.regenerate,
'visible',
);
await baseAssertion.assertElementState(
sendMessage.regenerate,
'visible',
);
},
);

await dialTest.step(
'Hover over Send button and verify it is disabled and tooltip is shown',
async () => {
for (let i = 1; i <= 2; i++) {
if (i === 2) {
const messagesCountBefore =
await chatMessages.chatMessages.getElementsCount();
await sendMessage.messageInput.fillInInput(' ');
await page.keyboard.press(keys.enter);
const messagesCountAfter =
await chatMessages.chatMessages.getElementsCount();
expect
.soft(
messagesCountBefore === messagesCountAfter,
ExpectedMessages.messageCountIsCorrect,
)
.toBeTruthy();
}
const isSendMessageBtnEnabled =
await sendMessage.sendMessageButton.isElementEnabled();
expect
.soft(
isSendMessageBtnEnabled,
ExpectedMessages.sendMessageButtonDisabled,
)
.toBeFalsy();

await sendMessage.sendMessageButton.hoverOver();
const sendBtnCursor =
await sendMessage.sendMessageButton.getComputedStyleProperty(
Styles.cursor,
);
expect
.soft(
sendBtnCursor[0],
ExpectedMessages.sendButtonCursorIsNotAllowed,
)
.toBe(Cursors.notAllowed);

const tooltipContent = await tooltip.getContent();
expect
.soft(tooltipContent, ExpectedMessages.tooltipContentIsValid)
.toBe(ExpectedConstants.regenerateResponseTooltip);
}
await sendMessage.regenerate.hoverOver();
const tooltipContent = await tooltip.getContent();
expect
.soft(tooltipContent, ExpectedMessages.tooltipContentIsValid)
.toBe(ExpectedConstants.regenerateResponseTooltip);
},
);

Expand Down
2 changes: 1 addition & 1 deletion apps/chat-e2e/src/ui/webElements/chatMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ChatMessages extends BaseElement {
ChatSelectors.chatMessage,
);

public regenerate = new BaseElement(this.page, ChatSelectors.regenerate);
public regenerate = this.getChildElementBySelector(ChatSelectors.regenerate);
private inputAttachments!: InputAttachments;

getInputAttachments(): InputAttachments {
Expand Down
1 change: 1 addition & 0 deletions apps/chat-e2e/src/ui/webElements/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class SendMessage extends BaseElement {
public stopGenerating = this.getChildElementBySelector(
SendMessageSelectors.stopGenerating,
);
public regenerate = this.getChildElementBySelector(ChatSelectors.regenerate);

public proceedGenerating = this.getChildElementBySelector(
SendMessageSelectors.proceedGenerating,
Expand Down
16 changes: 12 additions & 4 deletions apps/chat/src/components/Chat/ChatInput/ChatInputMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export const ChatInputMessage = ({
const isModelsLoaded = useAppSelector(ModelsSelectors.selectIsModelsLoaded);
const isChatFullWidth = useAppSelector(UISelectors.selectIsChatFullWidth);

const isError = isLastAssistantMessageEmpty || isMessageError;
const shouldRegenerate =
isLastMessageError || (isLastAssistantMessageEmpty && !messageIsStreaming);

const selectedModels = useAppSelector(
ConversationsSelectors.selectSelectedConversationsModels,
Expand Down Expand Up @@ -166,7 +167,7 @@ export const ChatInputMessage = ({
]);
const isSendDisabled =
isReplay ||
isError ||
isMessageError ||
isInputEmpty ||
!isModelsLoaded ||
isUploadingFilePresent ||
Expand Down Expand Up @@ -205,6 +206,11 @@ export const ChatInputMessage = ({
return;
}

if (shouldRegenerate) {
onRegenerate();
return;
}

if (isSendDisabled) {
return;
}
Expand Down Expand Up @@ -234,6 +240,7 @@ export const ChatInputMessage = ({
}
}, [
messageIsStreaming,
shouldRegenerate,
isSendDisabled,
dispatch,
onSend,
Expand All @@ -244,6 +251,7 @@ export const ChatInputMessage = ({
setContent,
textareaRef,
onStopConversation,
onRegenerate,
]);

const handleKeyDown = useCallback(
Expand Down Expand Up @@ -380,7 +388,7 @@ export const ChatInputMessage = ({
if (isReplay) {
return t('Please continue replay to continue working with conversation');
}
if (isError) {
if (shouldRegenerate) {
return t('Regenerate response');
}
if (isUploadingFilePresent) {
Expand Down Expand Up @@ -437,7 +445,7 @@ export const ChatInputMessage = ({
/>
<ChatControls
showReplayControls={showReplayControls}
onSend={isLastMessageError ? onRegenerate : handleSend}
onSend={shouldRegenerate ? onRegenerate : handleSend}
tooltip={tooltipContent()}
isLastMessageError={isLastMessageError}
isLoading={isLoading}
Expand Down
12 changes: 10 additions & 2 deletions apps/chat/src/components/Chat/ChatInput/SendMessageButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ export const SendMessageButton = ({
ConversationsSelectors.selectIsConversationsStreaming,
);

if (isLastMessageError) {
const isLastAssistantMessageEmpty = useAppSelector(
ConversationsSelectors.selectIsLastAssistantMessageEmpty,
);

if (
isLastMessageError ||
(isLastAssistantMessageEmpty && !messageIsStreaming)
) {
return (
<button
className={classNames(
'absolute top-[calc(50%_-_12px)] rounded text-error hover:text-accent-primary',
'absolute top-[calc(50%_-_12px)] rounded hover:text-accent-primary',
isLastMessageError && 'text-error',
isOverlay ? 'right-3' : 'right-4',
)}
onClick={onSend}
Expand Down

0 comments on commit 8e535a7

Please sign in to comment.