Skip to content

Commit

Permalink
824 streamline frontend change level network calls (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic authored Mar 4, 2024
1 parent 00a5068 commit ee5bec5
Show file tree
Hide file tree
Showing 32 changed files with 385 additions and 325 deletions.
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) {
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

0 comments on commit ee5bec5

Please sign in to comment.