Skip to content

Commit

Permalink
feat: Fethes chat history when loading Xpert
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Nov 8, 2024
1 parent f3b9931 commit 885a305
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@ async function fetchLearningAssistantEnabled(courseId) {
return data;
}

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

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

export default fetchChatResponse;
export { fetchLearningAssistantEnabled };
export {
fetchLearningAssistantEnabled,
fetchLearningAssistantChatHistory,
};
32 changes: 31 additions & 1 deletion src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';

import trackChatBotMessageOptimizely from '../utils/optimizelyExperiment';
import fetchChatResponse, { fetchLearningAssistantEnabled } from './api';
import fetchChatResponse, { fetchLearningAssistantChatHistory, fetchLearningAssistantEnabled } from './api';
import {
setCurrentMessage,
clearCurrentMessage,
Expand Down Expand Up @@ -81,6 +81,36 @@ export function clearMessages() {
};
}

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

try {
const rawMessageList = await fetchLearningAssistantChatHistory(courseId);

if (rawMessageList.length) {
const messageList = rawMessageList
.filter(({ timestamp }) => timestamp) // Filter any message without timestamp
.toSorted((a, b) => (a.timestamp > b.timestamp ? 1 : -1)) // Sort by timestamp
.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) {
dispatch(setApiError());
}

dispatch(setApiIsLoading(false));
};
}

export function updateCurrentMessage(content) {
return (dispatch) => {
dispatch(setCurrentMessage({ currentMessage: content }));
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable import/prefer-default-export */
export { useMessageHistory } from './message-history';
15 changes: 15 additions & 0 deletions src/hooks/message-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable import/prefer-default-export */
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { fetchChatHistory } from '../data/thunks';

export const useMessageHistory = (courseId) => {
const dispatch = useDispatch();
const { isEnabled } = useSelector(state => state.learningAssistant);

useEffect(() => {
if (!courseId || !isEnabled) { return; }

dispatch(fetchChatHistory(courseId));
}, [dispatch, isEnabled, courseId]);
};
2 changes: 2 additions & 0 deletions src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { updateSidebarIsOpen, getIsEnabled } from '../data/thunks';
import ToggleXpert from '../components/ToggleXpertButton';
import Sidebar from '../components/Sidebar';
import { ExperimentsProvider } from '../experiments';
import { useMessageHistory } from '../hooks';

const Xpert = ({ courseId, contentToolsEnabled, unitId }) => {
const dispatch = useDispatch();
useMessageHistory(courseId);

const {
isEnabled,
Expand Down

0 comments on commit 885a305

Please sign in to comment.