Skip to content

Commit

Permalink
feat: Call LearningAssistantSummary endpoint (#68)
Browse files Browse the repository at this point in the history
* feat: Call LearningAssistantAuditTrial endpoint

* fix: correct state + add gating for message form

* temp: fixed slice, attempting to implment tests

* temp: debug stdout for testing

* temp: rollback point before summary endpt refactor

* feat: refactored to fit with summary endpt

- also refactored tests accordingly

* chore: some nits

* fix: convert stored datestring back to date + nits

* chore: use > not >=
  • Loading branch information
ilee2u authored Dec 5, 2024
1 parent 455345e commit f1f690c
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 252 deletions.
14 changes: 3 additions & 11 deletions src/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ async function fetchChatResponse(courseId, messageList, unitId, customQueryParam
return data;
}

async function fetchLearningAssistantEnabled(courseId) {
const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/enabled`);

const { data } = await getAuthenticatedHttpClient().get(url.href);
return data;
}

async function fetchLearningAssistantMessageHistory(courseId) {
const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/history`);
async function fetchLearningAssistantChatSummary(courseId) {
const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/chat-summary`);

const { data } = await getAuthenticatedHttpClient().get(url.href);
return data;
}

export {
fetchChatResponse,
fetchLearningAssistantEnabled,
fetchLearningAssistantMessageHistory,
fetchLearningAssistantChatSummary,
};
47 changes: 27 additions & 20 deletions src/data/api.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-import-assign */
import * as auth from '@edx/frontend-platform/auth';

import { fetchLearningAssistantMessageHistory } from './api';
import { fetchLearningAssistantChatSummary } from './api';

jest.mock('@edx/frontend-platform/auth');

Expand All @@ -16,38 +16,45 @@ describe('API', () => {
jest.restoreAllMocks();
});

describe('fetchLearningAssistantMessageHistory()', () => {
const fakeCourseId = 'course-v1:edx+test+23';
const apiPayload = [
{
role: 'user',
content: 'Marco',
timestamp: '2024-11-04T19:05:07.403363Z',
describe('fetchLearningAssistantChatSummary()', () => {
const courseId = 'course-v1:edx+test+23';
const apiPayload = {
enabled: true,
message_history: [
{
role: 'user',
content: 'Marco',
timestamp: '2024-11-04T19:05:07.403363Z',
},
{
role: 'assistant',
content: 'Polo',
timestamp: '2024-11-04T19:05:21.357636Z',
},
],
audit_trial: {
start_date: '2024-12-02T14:59:16.148236Z',
expiration_date: '9999-12-16T14:59:16.148236Z',
},
{
role: 'assistant',
content: 'Polo',
timestamp: '2024-11-04T19:05:21.357636Z',
},
];
};

const fakeGet = jest.fn(async () => ({
const mockGet = jest.fn(async () => ({
data: apiPayload,
catch: () => {},
catch: () => { },
}));

beforeEach(() => {
auth.getAuthenticatedHttpClient = jest.fn(() => ({
get: fakeGet,
get: mockGet,
}));
});

it('should call the endpoint and process the results', async () => {
const response = await fetchLearningAssistantMessageHistory(fakeCourseId);
const response = await fetchLearningAssistantChatSummary(courseId);

expect(response).toEqual(apiPayload);
expect(fakeGet).toHaveBeenCalledTimes(1);
expect(fakeGet).toHaveBeenCalledWith(`${CHAT_RESPONSE_URL}/${fakeCourseId}/history`);
expect(mockGet).toHaveBeenCalledTimes(1);
expect(mockGet).toHaveBeenCalledWith(`${CHAT_RESPONSE_URL}/${courseId}/chat-summary`);
});
});
});
5 changes: 5 additions & 0 deletions src/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const initialState = {
disclosureAcknowledged: false,
sidebarIsOpen: false,
isEnabled: false,
auditTrial: {},
};

export const learningAssistantSlice = createSlice({
Expand Down Expand Up @@ -44,6 +45,9 @@ export const learningAssistantSlice = createSlice({
setIsEnabled: (state, { payload }) => {
state.isEnabled = payload;
},
setAuditTrial: (state, { payload }) => {
state.auditTrial = payload;
},
},
});

Expand All @@ -57,6 +61,7 @@ export const {
setDisclosureAcknowledged,
setSidebarIsOpen,
setIsEnabled,
setAuditTrial,
} = learningAssistantSlice.actions;

export const {
Expand Down
67 changes: 37 additions & 30 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';

import trackChatBotMessageOptimizely from '../utils/optimizelyExperiment';
import { fetchChatResponse, fetchLearningAssistantMessageHistory, fetchLearningAssistantEnabled } from './api';
import {
fetchChatResponse,
fetchLearningAssistantChatSummary,
} from './api';
import {
setCurrentMessage,
clearCurrentMessage,
Expand All @@ -13,6 +16,7 @@ import {
setDisclosureAcknowledged,
setSidebarIsOpen,
setIsEnabled,
setAuditTrial,
} from './slice';
import { OPTIMIZELY_PROMPT_EXPERIMENT_KEY } from './optimizely';

Expand Down Expand Up @@ -73,33 +77,6 @@ export function getChatResponse(courseId, unitId, promptExperimentVariationKey =
};
}

export function getLearningAssistantMessageHistory(courseId) {
return async (dispatch) => {
dispatch(setApiIsLoading(true));

try {
const rawMessageList = await fetchLearningAssistantMessageHistory(courseId);

if (rawMessageList.length) {
const messageList = rawMessageList
.map(({ timestamp, ...msg }) => ({
...msg,
timestamp: new Date(timestamp), // Parse ISO time to Date()
}));

dispatch(setMessageList({ messageList }));

// If it has chat history, then we assume the user already aknowledged.
dispatch(setDisclosureAcknowledged(true));
}
} catch (e) {
// If fetching the messages fail, we just won't show it.
}

dispatch(setApiIsLoading(false));
};
}

export function updateCurrentMessage(content) {
return (dispatch) => {
dispatch(setCurrentMessage({ currentMessage: content }));
Expand All @@ -124,13 +101,43 @@ export function updateSidebarIsOpen(isOpen) {
};
}

export function getIsEnabled(courseId) {
export function getLearningAssistantChatSummary(courseId) {
return async (dispatch) => {
dispatch(setApiIsLoading(true));

try {
const data = await fetchLearningAssistantEnabled(courseId);
const data = await fetchLearningAssistantChatSummary(courseId);

// Enabled
dispatch(setIsEnabled(data.enabled));

// Message History
const rawMessageList = data.message_history;

// If returned message history data is not empty
if (rawMessageList.length) {
const messageList = rawMessageList
.map(({ timestamp, ...msg }) => ({
...msg,
timestamp: new Date(timestamp).toString(), // Parse ISO time to Date()
}));

dispatch(setMessageList({ messageList }));

// If it has chat history, then we assume the user already aknowledged.
dispatch(setDisclosureAcknowledged(true));
}

// Audit Trial
const auditTrial = data.audit_trial;

// If returned audit trial data is not empty
if (Object.keys(auditTrial).length !== 0) {
dispatch(setAuditTrial(auditTrial));
}
} catch (error) {
dispatch(setApiError());
}
dispatch(setApiIsLoading(false));
};
}
Loading

0 comments on commit f1f690c

Please sign in to comment.