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

feat: Call LearningAssistantSummary endpoint #68

Merged
merged 9 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 () => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice renaming!

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
Loading