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 7 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 fetchLearningAssistantSummary(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,
fetchLearningAssistantSummary,
};
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 { fetchLearningAssistantSummary } 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('fetchLearningAssistantSummary()', () => {
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 fetchLearningAssistantSummary(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`);
});
});
});
8 changes: 8 additions & 0 deletions src/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const initialState = {
disclosureAcknowledged: false,
sidebarIsOpen: false,
isEnabled: false,
auditTrial: {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the initial state be {}? The default would be no trial, which would be represented by the empty object.

startDate: null,
expirationDate: null,
},
};

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

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

export const {
Expand Down
69 changes: 39 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,
fetchLearningAssistantSummary,
} 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,45 @@ export function updateSidebarIsOpen(isOpen) {
};
}

export function getIsEnabled(courseId) {
export function getLearningAssistantSummary(courseId) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I replaced the getLearningAssistantMessageHistory, getIsEnabled , and getAuditTrial thunks with a singular getLearningAssistantSummary thunk.

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Could you rename this to getLearningAssistantChatSummary? Marcos had some concerns about confusion between that summary and the unit summary, which we may integrate into Learning Assistant.

return async (dispatch) => {
dispatch(setApiIsLoading(true));

try {
const data = await fetchLearningAssistantEnabled(courseId);
const data = await fetchLearningAssistantSummary(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) { // eslint-disable-line no-undef
Copy link
Member

Choose a reason for hiding this comment

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

What was the issue with no-undef here? I don't see what would be undefined here. Do we need this eslint-disable declaration?

Copy link
Member

Choose a reason for hiding this comment

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

If auditTrial data is empty, is there a reason we shouldn't call setAuditTrial. If it's empty, it would just set auditTrial to the empty object. Is there something wrong with that behavior? I think that's a reasonable behavior, as the empty object signifies the absence of a trial.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh that's an artifact from when I was using lodash, I'll get rid of this

dispatch(setAuditTrial(auditTrial));
}
} catch (error) {
// NOTE: When used to not show if fetching the messages failed
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify what this comment means? I'm not following.

Copy link
Member Author

Choose a reason for hiding this comment

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

I meant to use "we" instead of "when", but also I don't think this comment is necessary so I'm removing it.

// But we do know since this endpoint is combined.
dispatch(setApiError());
}
dispatch(setApiIsLoading(false));
};
}
Loading
Loading