Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

824 streamline frontend change level network calls #839

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ffc1f60
deleted console log
pmarsh-scottlogic Feb 22, 2024
dcb9546
adds backend endpoint for get level and renames the request type
pmarsh-scottlogic Feb 22, 2024
e15ff88
renames levelService to resetService
pmarsh-scottlogic Feb 22, 2024
a1dec07
adds load level service to frontend
pmarsh-scottlogic Feb 22, 2024
2774b9c
adds levelSErvice to servvice index
pmarsh-scottlogic Feb 22, 2024
46f8f17
replaces ssitch level calls with new service method
pmarsh-scottlogic Feb 22, 2024
2ba78d6
removes unused getCHatHistory endpoint
pmarsh-scottlogic Feb 22, 2024
7f9ffd7
removes unused get defence endpoint
pmarsh-scottlogic Feb 22, 2024
e9410e9
removes endpoint for getting emails
pmarsh-scottlogic Feb 22, 2024
9edadbc
lil' tidy
pmarsh-scottlogic Feb 22, 2024
a2f73aa
deletes obsolete tests
pmarsh-scottlogic Feb 22, 2024
372aae5
start skeleton for startController test
pmarsh-scottlogic Feb 23, 2024
3d5c363
adds getStart test
pmarsh-scottlogic Feb 23, 2024
167d337
adds handling for when there is no level provided
pmarsh-scottlogic Feb 23, 2024
c9192cd
writes startController tests
pmarsh-scottlogic Feb 23, 2024
ba9f9c6
adds unit test for levelController
pmarsh-scottlogic Feb 23, 2024
14119c2
move res.status and res.send to one line
pmarsh-scottlogic Feb 27, 2024
5e3301b
removes redundant LevelState declaration from session module declaration
pmarsh-scottlogic Feb 27, 2024
e7e36f5
Extract espress-session typings
chriswilty Mar 1, 2024
c59a542
remove trailing slash
pmarsh-scottlogic Mar 1, 2024
fb4eee8
merge dev
pmarsh-scottlogic Mar 1, 2024
4c27c3f
adds test for invalid level
pmarsh-scottlogic Mar 1, 2024
76833b9
corrects expect statement
pmarsh-scottlogic Mar 1, 2024
c755895
adds isValidLevel method
pmarsh-scottlogic Mar 1, 2024
63b14b7
tests all levels for levelController
pmarsh-scottlogic Mar 1, 2024
7ab2845
adds validation and test for invalid level for startController
pmarsh-scottlogic Mar 1, 2024
b80d2b5
tests startController across all levels
pmarsh-scottlogic Mar 1, 2024
96531e5
corrects test names
pmarsh-scottlogic Mar 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions backend/src/controller/chatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { OpenAiAddInfoToChatHistoryRequest } from '@src/models/api/OpenAiAddInfoToChatHistoryRequest';
import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest';
import { OpenAiGetHistoryRequest } from '@src/models/api/OpenAiGetHistoryRequest';
import {
DefenceReport,
ChatHttpResponse,
Expand Down Expand Up @@ -359,16 +358,6 @@ function addErrorToChatHistory(
});
}

function handleGetChatHistory(req: OpenAiGetHistoryRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].chatHistory);
} else {
res.status(400);
res.send('Missing level');
}
}

function handleAddInfoToChatHistory(
req: OpenAiAddInfoToChatHistoryRequest,
res: Response
Expand Down Expand Up @@ -407,9 +396,4 @@ function handleClearChatHistory(req: OpenAiClearRequest, res: Response) {
}
}

export {
handleChatToGPT,
handleGetChatHistory,
handleAddInfoToChatHistory as handleAddInfoToChatHistory,
handleClearChatHistory,
};
export { handleChatToGPT, handleAddInfoToChatHistory, handleClearChatHistory };
12 changes: 0 additions & 12 deletions backend/src/controller/defenceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { DefenceActivateRequest } from '@src/models/api/DefenceActivateRequest';
import { DefenceConfigResetRequest } from '@src/models/api/DefenceConfigResetRequest';
import { DefenceConfigureRequest } from '@src/models/api/DefenceConfigureRequest';
import { DefenceStatusRequest } from '@src/models/api/DefenceStatusRequest';
import { DefenceConfigItem } from '@src/models/defence';
import { LEVEL_NAMES } from '@src/models/level';

Expand Down Expand Up @@ -112,20 +111,9 @@ function handleResetSingleDefence(
}
}

function handleGetDefenceStatus(req: DefenceStatusRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].defences);
} else {
res.status(400);
res.send('Missing level');
}
}

export {
handleDefenceActivation,
handleDefenceDeactivation,
handleConfigureDefence,
handleResetSingleDefence,
handleGetDefenceStatus,
};
13 changes: 1 addition & 12 deletions backend/src/controller/emailController.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { Response } from 'express';

import { EmailClearRequest } from '@src/models/api/EmailClearRequest';
import { EmailGetRequest } from '@src/models/api/EmailGetRequest';
import { LEVEL_NAMES } from '@src/models/level';

function handleGetEmails(req: EmailGetRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].sentEmails);
} else {
res.status(400);
res.send('Missing level');
}
}

function handleClearEmails(req: EmailClearRequest, res: Response) {
const level = req.body.level;
if (level !== undefined && level >= LEVEL_NAMES.LEVEL_1) {
Expand All @@ -26,4 +15,4 @@ function handleClearEmails(req: EmailClearRequest, res: Response) {
}
}

export { handleGetEmails, handleClearEmails };
export { handleClearEmails };
15 changes: 15 additions & 0 deletions backend/src/controller/levelController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Response } from 'express';

import { LevelGetRequest } from '@src/models/api/LevelGetRequest';

function handleLoadLevel(req: LevelGetRequest, res: Response) {
const { level } = req.query;

res.send({
emails: req.session.levelState[level].sentEmails,
chatHistory: req.session.levelState[level].chatHistory,
defences: req.session.levelState[level].defences,
});
}

export { handleLoadLevel };
4 changes: 2 additions & 2 deletions backend/src/controller/startController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Response } from 'express';

import { GetStartRequest } from '@src/models/api/getStartRequest';
import { StartGetRequest } from '@src/models/api/StartGetRequest';
import { LEVEL_NAMES } from '@src/models/level';
import { getValidOpenAIModels } from '@src/openai';
import {
Expand All @@ -9,7 +9,7 @@ import {
systemRoleLevel3,
} from '@src/promptTemplates';

function handleStart(req: GetStartRequest, res: Response) {
function handleStart(req: StartGetRequest, res: Response) {
pmarsh-scottlogic marked this conversation as resolved.
Show resolved Hide resolved
const { level } = req.query;

const systemRoles = [
Expand Down
12 changes: 0 additions & 12 deletions backend/src/models/api/DefenceStatusRequest.ts

This file was deleted.

13 changes: 0 additions & 13 deletions backend/src/models/api/EmailGetRequest.ts

This file was deleted.

19 changes: 19 additions & 0 deletions backend/src/models/api/LevelGetRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request } from 'express';

import { ChatMessage } from '@src/models/chatMessage';
import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type LevelGetRequest = Request<
never,
{
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences: Defence[];
},
never,
{
level: LEVEL_NAMES;
}
>;
12 changes: 0 additions & 12 deletions backend/src/models/api/OpenAiGetHistoryRequest.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type GetStartRequest = Request<
export type StartGetRequest = Request<
never,
{
emails: EmailInfo[];
Expand Down
13 changes: 4 additions & 9 deletions backend/src/sessionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import memoryStoreFactory from 'memorystore';

import {
handleChatToGPT,
handleGetChatHistory,
handleAddInfoToChatHistory,
handleClearChatHistory,
} from './controller/chatController';
import {
handleConfigureDefence,
handleDefenceActivation,
handleDefenceDeactivation,
handleGetDefenceStatus,
handleResetSingleDefence,
} from './controller/defenceController';
import {
handleClearEmails,
handleGetEmails,
} from './controller/emailController';
import { handleClearEmails } from './controller/emailController';
import { handleLoadLevel } from './controller/levelController';
import {
handleConfigureModel,
handleGetModel,
Expand Down Expand Up @@ -97,19 +93,18 @@ router.use((req, _res, next) => {
// handshake
router.get('/start', handleStart);

router.get('/level', handleLoadLevel);

// defences
router.get('/defence/status', handleGetDefenceStatus);
router.post('/defence/activate', handleDefenceActivation);
router.post('/defence/deactivate', handleDefenceDeactivation);
router.post('/defence/configure', handleConfigureDefence);
router.post('/defence/resetConfig', handleResetSingleDefence);

// emails
router.get('/email/get', handleGetEmails);
router.post('/email/clear', handleClearEmails);

// chat
router.get('/openai/history', handleGetChatHistory);
router.post('/openai/chat', handleChatToGPT);
router.post('/openai/addInfoToHistory', handleAddInfoToChatHistory);
router.post('/openai/clear', handleClearChatHistory);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MissionInformation from './components/Overlay/MissionInformation';
import OverlayWelcome from './components/Overlay/OverlayWelcome';
import ResetProgressOverlay from './components/Overlay/ResetProgress';
import { LEVEL_NAMES } from './models/level';
import { levelService } from './service';
import { resetService } from './service';
chriswilty marked this conversation as resolved.
Show resolved Hide resolved

import './App.css';
import './Theme.css';
Expand Down Expand Up @@ -205,7 +205,7 @@ function App() {
console.log('resetting progress for all levels');

// reset on the backend
await levelService.resetAllLevelProgress();
await resetService.resetAllLevelProgress();

localStorage.setItem('numCompletedLevels', '0');
setNumCompletedLevels(0);
Expand Down
15 changes: 6 additions & 9 deletions frontend/src/components/MainComponent/MainComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
chatService,
defenceService,
emailService,
levelService,
startService,
} from '@src/service';

Expand Down Expand Up @@ -66,10 +67,6 @@ function MainComponent({

// facilitate level change
useEffect(() => {
console.log(
'useEffect currentLevel. isFirstRender.current:',
isFirstRender.current
);
if (!isFirstRender.current) {
console.log('Loading backend data for level', currentLevel);
void setNewLevel(currentLevel);
Expand All @@ -79,11 +76,11 @@ function MainComponent({

async function loadBackendData() {
try {
const { availableModels, defences, emails, history, systemRoles } =
const { availableModels, defences, emails, chatHistory, systemRoles } =
await startService.start(currentLevel);
setChatModels(availableModels);
setSystemRoles(systemRoles);
processBackendLevelData(currentLevel, emails, history, defences);
processBackendLevelData(currentLevel, emails, chatHistory, defences);
} catch (err) {
console.warn(err);
setMessages([
Expand Down Expand Up @@ -150,9 +147,9 @@ function MainComponent({

// for going switching level without clearing progress
async function setNewLevel(newLevel: LEVEL_NAMES) {
const emails = await emailService.getSentEmails(newLevel);
const chatHistory = await chatService.getChatHistory(newLevel);
const defences = await defenceService.getDefences(newLevel);
const { emails, chatHistory, defences } = await levelService.loadLevel(
newLevel
);
processBackendLevelData(newLevel, emails, chatHistory, defences);
}

Expand Down
8 changes: 7 additions & 1 deletion frontend/src/models/combined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ type StartReponse = {
systemRoles: LevelSystemRole[];
};

export type { StartReponse };
type LoadLevelResponse = {
emails: EmailInfo[];
chatHistory: ChatMessageDTO[];
defences: DefenceDTO[];
};

export type { StartReponse, LoadLevelResponse };
10 changes: 0 additions & 10 deletions frontend/src/service/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ function chatMessageDTOIsConvertible(chatMessageDTO: ChatMessageDTO) {
);
}

async function getChatHistory(level: number): Promise<ChatMessage[]> {
const response = await sendRequest(`${PATH}history?level=${level}`, {
method: 'GET',
});
const chatMessageDTOs = (await response.json()) as ChatMessageDTO[];

return getChatMessagesFromDTOResponse(chatMessageDTOs);
}

function getChatMessagesFromDTOResponse(chatMessageDTOs: ChatMessageDTO[]) {
return chatMessageDTOs
.filter(chatMessageDTOIsConvertible)
Expand Down Expand Up @@ -124,7 +115,6 @@ export {
configureGptModel,
getGptModel,
setGptModel,
getChatHistory,
addInfoMessageToChatHistory,
getChatMessagesFromDTOResponse,
};
9 changes: 0 additions & 9 deletions frontend/src/service/defenceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ import { sendRequest } from './backendService';

const PATH = 'defence/';

async function getDefences(level: number) {
const response = await sendRequest(`${PATH}status?level=${level}`, {
method: 'GET',
});
const defenceDTOs = (await response.json()) as DefenceDTO[];
return getDefencesFromDTOs(defenceDTOs);
}

function getDefencesFromDTOs(defenceDTOs: DefenceDTO[]) {
return DEFAULT_DEFENCES.map((defence) => {
const defenceDTO = defenceDTOs.find(
Expand Down Expand Up @@ -122,7 +114,6 @@ function validateDefence(
}

export {
getDefences,
toggleDefence,
configureDefence,
resetDefenceConfig,
Expand Down
11 changes: 1 addition & 10 deletions frontend/src/service/emailService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { EmailInfo } from '@src/models/email';

import { sendRequest } from './backendService';

const PATH = 'email/';
Expand All @@ -15,11 +13,4 @@ async function clearEmails(level: number): Promise<boolean> {
return response.status === 200;
}

async function getSentEmails(level: number) {
const response = await sendRequest(`${PATH}get?level=${level}`, {
method: 'GET',
});
return (await response.json()) as EmailInfo[];
}

export { clearEmails, getSentEmails };
export { clearEmails };
4 changes: 3 additions & 1 deletion frontend/src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import * as defenceService from './defenceService';
import * as documentService from './documentService';
import * as emailService from './emailService';
import * as levelService from './levelService';
import * as resetService from './resetService';
import * as startService from './startService';

export {
chatService,
defenceService,
documentService,
emailService,
levelService,
resetService,
startService,
levelService,
};
Loading
Loading