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 27 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 };
26 changes: 26 additions & 0 deletions backend/src/controller/levelController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Response } from 'express';

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

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

if (level === undefined) {
res.status(400).send('Level not provided');
return;
}

if (!isValidLevel(level)) {
res.status(400).send('Invalid level');
return;
}

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

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

import { GetStartRequest } from '@src/models/api/getStartRequest';
import { LEVEL_NAMES } from '@src/models/level';
import { StartGetRequest } from '@src/models/api/StartGetRequest';
import { LEVEL_NAMES, isValidLevel } from '@src/models/level';
import { getValidOpenAIModels } from '@src/openai';
import {
systemRoleLevel1,
systemRoleLevel2,
systemRoleLevel3,
} from '@src/promptTemplates';

function handleStart(req: GetStartRequest, res: Response) {
import { sendErrorResponse } from './handleError';

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

if (level === undefined) {
sendErrorResponse(res, 400, 'Level not provided');
return;
}

if (!isValidLevel(level)) {
sendErrorResponse(res, 400, 'Invalid level');
return;
}

const systemRoles = [
{ level: LEVEL_NAMES.LEVEL_1, systemRole: systemRoleLevel1 },
{ level: LEVEL_NAMES.LEVEL_2, systemRole: systemRoleLevel2 },
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 All @@ -16,6 +16,6 @@ export type GetStartRequest = Request<
},
never,
{
level: LEVEL_NAMES;
level?: LEVEL_NAMES;
}
>;
6 changes: 5 additions & 1 deletion backend/src/models/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const LEVEL_NAMES = {

type LEVEL_NAMES = (typeof LEVEL_NAMES)[keyof typeof LEVEL_NAMES];

function isValidLevel(levelValue: unknown) {
return Object.values(LEVEL_NAMES).includes(levelValue as LEVEL_NAMES);
}

interface LevelState {
level: LEVEL_NAMES;
chatHistory: ChatMessage[];
Expand All @@ -32,5 +36,5 @@ function getInitialLevelStates() {
);
}

export { getInitialLevelStates, LEVEL_NAMES };
export { getInitialLevelStates, LEVEL_NAMES, isValidLevel };
export type { LevelState };
25 changes: 6 additions & 19 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 All @@ -28,16 +24,8 @@ import {
import { handleResetProgress } from './controller/resetController';
import { handleStart } from './controller/startController';
import { handleTest } from './controller/testController';
import { ChatModel, defaultChatModel } from './models/chat';
import { LevelState, getInitialLevelStates } from './models/level';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}
import { defaultChatModel } from './models/chat';
import { getInitialLevelStates } from './models/level';

const sessionSigningSecret = process.env.SESSION_SECRET;
if (!sessionSigningSecret) {
Expand Down Expand Up @@ -97,19 +85,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
15 changes: 0 additions & 15 deletions backend/test/integration/chatController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ import { Response } from 'express';

import { handleChatToGPT } from '@src/controller/chatController';
import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { ChatModel } from '@src/models/chat';
import { ChatMessage } from '@src/models/chatMessage';
import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES, LevelState } from '@src/models/level';
import { systemRoleLevel1 } from '@src/promptTemplates';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
interface LevelState {
level: LEVEL_NAMES;
chatHistory: ChatMessage[];
defences: Defence[];
sentEmails: EmailInfo[];
}
}

// mock the api call
const mockCreateChatCompletion =
jest.fn<
Expand Down
2 changes: 1 addition & 1 deletion backend/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*.ts", "../jest.config.ts"]
"include": ["./**/*.ts", "../typings", "../jest.config.ts"]
}
Loading
Loading