Skip to content

Commit

Permalink
828 steamline win level info message (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic authored and chriswilty committed Apr 8, 2024
1 parent 42e51d4 commit 3425cb5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 39 deletions.
24 changes: 24 additions & 0 deletions backend/src/controller/chatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ async function handleChatToGPT(req: OpenAiChatRequest, res: Response) {
...levelResult.chatResponse,
wonLevel:
!levelResult.chatResponse.defenceReport.isBlocked &&
!levelResult.chatResponse.openAIErrorMessage &&
isLevelWon(levelResult.chatResponse.sentEmails, currentLevel),
};

Expand Down Expand Up @@ -334,6 +335,29 @@ async function handleChatToGPT(req: OpenAiChatRequest, res: Response) {
req.session.levelState[currentLevel].chatHistory = updatedChatHistory;
req.session.levelState[currentLevel].sentEmails = totalSentEmails;

// If the level has just been won, add a level complete message to chat history and return it in the response
const levelCompleteMessageInChatHistory = req.session.levelState[
currentLevel
].chatHistory.some((msg) => msg.chatMessageType === 'LEVEL_COMPLETE');
if (updatedChatResponse.wonLevel && !levelCompleteMessageInChatHistory) {
const levelCompleteMessage = {
chatMessageType: 'LEVEL_COMPLETE',
infoMessage:
currentLevel === LEVEL_NAMES.LEVEL_3
? `🎉 Congratulations, you have completed the final level of your assignment!`
: `🎉 Congratulations! You have completed this level. Please click on the next level to continue.`,
} as ChatInfoMessage;

// add level complete message to chat history
req.session.levelState[currentLevel].chatHistory = pushMessageToHistory(
req.session.levelState[currentLevel].chatHistory,
levelCompleteMessage
);

// add level complete message to chat response
updatedChatResponse.wonLevelMessage = levelCompleteMessage;
}

console.log('chatResponse: ', updatedChatResponse);
console.log('chatHistory: ', updatedChatHistory);

Expand Down
3 changes: 2 additions & 1 deletion backend/src/models/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ChatCompletionMessageParam,
} from 'openai/resources/chat/completions';

import { ChatMessage } from './chatMessage';
import { ChatInfoMessage, ChatMessage } from './chatMessage';
import { DEFENCE_ID } from './defence';
import { EmailInfo } from './email';

Expand Down Expand Up @@ -98,6 +98,7 @@ interface ChatHttpResponse {
openAIErrorMessage: string | null;
sentEmails: EmailInfo[];
transformedMessageInfo?: string;
wonLevelMessage?: ChatInfoMessage;
}

interface LevelHandlerResponse {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/chatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TransformedChatMessage } from './chat';
const chatInfoMessageTypes = [
'DEFENCE_ALERTED',
'DEFENCE_TRIGGERED',
'LEVEL_INFO',
'LEVEL_COMPLETE',
'RESET_LEVEL',
'ERROR_MSG',
'BOT_BLOCKED',
Expand Down
21 changes: 17 additions & 4 deletions backend/test/unit/controller/chatController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,17 @@ describe('handleChatToGPT unit tests', () => {

await handleChatToGPT(req, res);

const expectedWonLevelMessage = {
infoMessage:
'🎉 Congratulations! You have completed this level. Please click on the next level to continue.',
chatMessageType: 'LEVEL_COMPLETE',
} as ChatMessage;

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: true })
expect.objectContaining({
wonLevel: true,
wonLevelMessage: expectedWonLevelMessage,
})
);
});

Expand Down Expand Up @@ -827,11 +836,13 @@ describe('handleChatToGPT unit tests', () => {
await handleChatToGPT(req, res);

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: false })
expect.objectContaining({
wonLevel: false,
})
);
});

test('Given win condition met AND openAI error THEN level is won', async () => {
test('Given win condition met AND openAI error THEN level is not won', async () => {
const newUserChatMessage = {
completion: {
content: 'Here is the answer to the level',
Expand Down Expand Up @@ -868,7 +879,9 @@ describe('handleChatToGPT unit tests', () => {
await handleChatToGPT(req, res);

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: true })
expect.objectContaining({
wonLevel: false,
})
);
});
});
Expand Down
28 changes: 5 additions & 23 deletions frontend/src/components/ChatBox/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,9 @@ function ChatBox({
setChatInput(recalledMessage);
}, [recalledMessageReverseIndex]);

function getSuccessMessage() {
return currentLevel < LEVEL_NAMES.LEVEL_3
? `Congratulations! You have completed this level. Please click on the next level to continue.`
: `Congratulations, you have completed the final level of your assignment!`;
}

function isLevelComplete() {
// level is complete if the chat contains a LEVEL_INFO message
return messages.some((message) => message.type === 'LEVEL_INFO');
}

function processChatResponse(response: ChatResponse) {
const transformedMessageInfo = response.transformedMessageInfo;
const transformedMessage = response.transformedMessage;
// add transformation info message to the chat box
if (transformedMessageInfo) {
addChatMessage({
message: transformedMessageInfo,
Expand Down Expand Up @@ -156,19 +144,13 @@ function ChatBox({
// update emails
addSentEmails(response.sentEmails);

if (response.wonLevel && !isLevelComplete()) {
if (response.wonLevelMessage) {
updateNumCompletedLevels(currentLevel);
const successMessage = getSuccessMessage();
addChatMessage({
type: 'LEVEL_INFO',
message: successMessage,
});
// asynchronously add the message to the chat history
void chatService.addInfoMessageToChatHistory(
successMessage,
'LEVEL_INFO',
currentLevel
const levelCompleteMessage = chatService.makeChatMessageFromDTO(
response.wonLevelMessage
);
addChatMessage(levelCompleteMessage);

// if this is the last level, show the level complete overlay
if (currentLevel === LEVEL_NAMES.LEVEL_3) {
openLevelsCompleteOverlay();
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/components/ChatBox/ChatBoxMessage/MessageBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function MessageBubble({
const baseClassName = 'message-bubble';

const messageTypeClassName =
message.type === 'LEVEL_INFO'
message.type === 'LEVEL_COMPLETE'
? 'level-info'
: message.type === 'USER'
? 'user'
Expand All @@ -35,7 +35,7 @@ function MessageBubble({
);

const messageAuthor =
message.type === 'LEVEL_INFO'
message.type === 'LEVEL_COMPLETE'
? ''
: message.type === 'USER'
? 'You said:'
Expand All @@ -48,12 +48,6 @@ function MessageBubble({
return (
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
<section className={className} lang="en" tabIndex={0}>
{message.type === 'LEVEL_INFO' && (
<>
<p className="header">Information</p>
<span className="visually-hidden"> message: </span>
</>
)}
<span className="visually-hidden">{messageAuthor}</span>
{message.transformedMessage ? (
<span>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ExportChat/ExportChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getMessageStyle(type: CHAT_MESSAGE_TYPE) {
return styles.chatBoxInfo;
case 'BOT_BLOCKED':
case 'BOT':
case 'LEVEL_INFO':
case 'LEVEL_COMPLETE':
case 'ERROR_MSG':
return styles.chatBoxMessageBot;
case 'USER':
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/models/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type CHAT_MESSAGE_TYPE =
| 'GENERIC_INFO'
| 'USER'
| 'USER_TRANSFORMED'
| 'LEVEL_INFO'
| 'LEVEL_COMPLETE'
| 'DEFENCE_ALERTED'
| 'DEFENCE_TRIGGERED'
| 'SYSTEM'
Expand Down Expand Up @@ -71,6 +71,7 @@ interface ChatResponse {
isError: boolean;
sentEmails: EmailInfo[];
transformedMessageInfo?: string;
wonLevelMessage?: ChatMessageDTO;
}

interface ChatCompletionRequestMessage {
Expand Down

0 comments on commit 3425cb5

Please sign in to comment.