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 57f7816
Show file tree
Hide file tree
Showing 7 changed files with 4,619 additions and 5,303 deletions.
9,884 changes: 4,595 additions & 5,289 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/components/Disclosure/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getConfig } from '@edx/frontend-platform/config';
import MessageForm from '../MessageForm';
import './Disclosure.scss';

const Disclosure = ({ courseId }) => (
const Disclosure = ({ courseId, unitId }) => (
<div className="disclosure d-flex flex-column align-items-stretch px-4 py-3">
<h2 className="text-light-100">
Xpert
Expand Down Expand Up @@ -45,12 +45,13 @@ const Disclosure = ({ courseId }) => (
</Hyperlink>
.
</p>
<MessageForm courseId={courseId} />
<MessageForm courseId={courseId} unitId={unitId} />
</div>
);

Disclosure.propTypes = {
courseId: PropTypes.string.isRequired,
unitId: PropTypes.string.isRequired,
};

export default Disclosure;
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
6 changes: 4 additions & 2 deletions 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 @@ -136,7 +137,7 @@ const Sidebar = ({
variant="primary"
invertColors={!disclosureAcknowledged}
/>
{disclosureAcknowledged ? (getSidebar()) : (<Disclosure courseId={courseId} />)}
{disclosureAcknowledged ? (getSidebar()) : (<Disclosure courseId={courseId} unitId={unitId} />)}
</div>
)
);
Expand All @@ -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';

async function fetchChatResponse(courseId, messageList) {
async function fetchChatResponse(courseId, messageList, unitId) {

Check warning on line 4 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L4

Added line #L4 was not covered by tests
const payload = messageList.map((message) => ({
role: message?.role,
content: message?.content,
}));

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

Check warning on line 11 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L10-L11

Added lines #L10 - L11 were not covered by tests

let queryString = new URLSearchParams(queryParams);
queryString = queryString.toString();

Check warning on line 14 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L13-L14

Added lines #L13 - L14 were not covered by tests

const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}?${queryString}`);

Check warning on line 16 in src/data/api.js

View check run for this annotation

Codecov / codecov/patch

src/data/api.js#L16

Added line #L16 was not covered by tests

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 57f7816

Please sign in to comment.