Skip to content

Commit

Permalink
feat: receive unitId as prop and forward to learning-assistant backend
Browse files Browse the repository at this point in the history
This commit modifies the Xpert components to accept a new unitId prop, which presents the unit usage key in which the Xpert is being rendered. This unit ID is forwarded to the chat completion backend API.
  • Loading branch information
MichaelRoytman committed Dec 19, 2023
1 parent c22b3e4 commit e3c4fa1
Show file tree
Hide file tree
Showing 7 changed files with 4,658 additions and 5,286 deletions.
9,912 changes: 4,637 additions & 5,275 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@optimizely/react-sdk": "^2.9.2",
"core-js": "3.31.1",
"prop-types": "15.8.1",
"query-string": "^8.1.0",
"react-markdown": "^8.0.5",
"uuid": "9.0.0"
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/MessageForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
updateCurrentMessage,
} from '../../data/thunks';

const MessageForm = ({ courseId, shouldAutofocus }) => {
const MessageForm = ({ courseId, shouldAutofocus, unitId }) => {
const { apiIsLoading, currentMessage, apiError } = useSelector(state => state.learningAssistant);
const dispatch = useDispatch();
const inputRef = useRef();
Expand All @@ -28,7 +28,7 @@ const MessageForm = ({ courseId, shouldAutofocus }) => {
if (currentMessage) {
dispatch(acknowledgeDisclosure(true));
dispatch(addChatMessage('user', currentMessage, courseId));
dispatch(getChatResponse(courseId));
dispatch(getChatResponse(courseId, unitId));
}
};

Expand Down Expand Up @@ -67,6 +67,7 @@ const MessageForm = ({ courseId, shouldAutofocus }) => {

MessageForm.propTypes = {
courseId: PropTypes.string.isRequired,
unitId: PropTypes.string.isRequired,
shouldAutofocus: PropTypes.bool,
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Sidebar = ({
courseId,
isOpen,
setIsOpen,
unitId,
}) => {
const {
apiError,
Expand Down Expand Up @@ -106,7 +107,7 @@ const Sidebar = ({
</div>
)
}
<MessageForm courseId={courseId} shouldAutofocus />
<MessageForm courseId={courseId} shouldAutofocus unitId={unitId} />
<div className="d-flex justify-content-start">
<Button
className="clear mx-2 mb-2 border-0"
Expand Down Expand Up @@ -146,6 +147,7 @@ Sidebar.propTypes = {
courseId: PropTypes.string.isRequired,
isOpen: PropTypes.bool.isRequired,
setIsOpen: PropTypes.func.isRequired,
unitId: PropTypes.string.isRequired,
};

export default Sidebar;
14 changes: 9 additions & 5 deletions src/data/api.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { getConfig } from '@edx/frontend-platform';
import { getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import queryString from 'query-string';

async function fetchChatResponse(courseId, messageList) {
async function fetchChatResponse(courseId, messageList, unitId) {
const payload = messageList.map((message) => ({
role: message?.role,
content: message?.content,
}));

const url = new URL(
`${getConfig().CHAT_RESPONSE_URL}/${courseId}`,
);
const baseUrl = `${getConfig().CHAT_RESPONSE_URL}/${courseId}`;
let queryParams = { unitId };
queryParams = snakeCaseObject(queryParams);

let url = queryString.stringifyUrl({ url: baseUrl, query: queryParams });
url = new URL(url);

const { data } = await getAuthenticatedHttpClient().post(url.href, payload);
return data;
Expand Down
4 changes: 2 additions & 2 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function addChatMessage(role, content, courseId) {
};
}

export function getChatResponse(courseId) {
export function getChatResponse(courseId, unitId) {
return async (dispatch, getState) => {
const { messageList } = getState().learningAssistant;

dispatch(setApiIsLoading(true));
try {
const message = await fetchChatResponse(courseId, messageList);
const message = await fetchChatResponse(courseId, messageList, unitId);
dispatch(setApiIsLoading(false));
dispatch(addChatMessage(message.role, message.content, courseId));
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { updateSidebarIsOpen } from '../data/thunks';
import ToggleXpert from '../components/ToggleXpertButton';
import Sidebar from '../components/Sidebar';

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

const {
Expand All @@ -27,6 +27,7 @@ const Xpert = ({ courseId, contentToolsEnabled }) => {
courseId={courseId}
isOpen={sidebarIsOpen}
setIsOpen={setSidebarIsOpen}
unitId={unitId}
/>
</div>
);
Expand All @@ -35,6 +36,7 @@ const Xpert = ({ courseId, contentToolsEnabled }) => {
Xpert.propTypes = {
courseId: PropTypes.string.isRequired,
contentToolsEnabled: PropTypes.bool.isRequired,
unitId: PropTypes.string.isRequired,
};

export default Xpert;

0 comments on commit e3c4fa1

Please sign in to comment.