diff --git a/src/ol_openedx_chat/block.py b/src/ol_openedx_chat/block.py index f083bb84..49ea7c3a 100644 --- a/src/ol_openedx_chat/block.py +++ b/src/ol_openedx_chat/block.py @@ -1,3 +1,5 @@ +import logging + import pkg_resources from django.conf import settings from django.template import Context, Template @@ -12,6 +14,8 @@ from .compat import get_ol_openedx_chat_enabled_flag +log = logging.getLogger(__name__) + def get_resource_bytes(path): """ @@ -54,6 +58,7 @@ def student_view_aside(self, block, context=None): """ Renders the aside contents for the student view """ # noqa: D401 + from xmodule.video_block.transcripts_utils import Transcript # This is a workaround for those blocks which do not have has_author_view=True # because when a block does not define has_author_view=True in it, the only view @@ -78,10 +83,29 @@ def student_view_aside(self, block, context=None): ) fragment.add_css(get_resource_bytes("static/css/ai_chat.css")) fragment.add_javascript(get_resource_bytes("static/js/ai_chat.js")) + + transcript_asset_id = None + if getattr(block, "category", None) == "video": + try: + transcripts_info = block.get_transcripts_info() + if transcripts_info.get("transcripts") and transcripts_info[ + "transcripts" + ].get("en"): + transcript_asset_id = Transcript.asset_location( + block.location, transcripts_info["transcripts"]["en"] + ) + + except Exception: + log.exception( + "Error while fetching transcripts for block %s", + block.location, + ) + extra_context = { "ask_tim_drawer_title": f"about {block.display_name}", "block_id": self.scope_ids.usage_id.usage_key.block_id, "block_usage_key": self.scope_ids.usage_id.usage_key, + "transcript_asset_id": transcript_asset_id, "user_id": self.runtime.user_id, "learn_ai_api_url": settings.LEARN_AI_API_URL, "learning_mfe_base_url": settings.LEARNING_MICROFRONTEND_URL, diff --git a/src/ol_openedx_chat/static/js/ai_chat.js b/src/ol_openedx_chat/static/js/ai_chat.js index 3cc33671..392af749 100644 --- a/src/ol_openedx_chat/static/js/ai_chat.js +++ b/src/ol_openedx_chat/static/js/ai_chat.js @@ -12,7 +12,8 @@ $(`#chat-button-${init_args.block_id}`).on("click", { askTimTitle: init_args.ask_tim_drawer_title, blockUsageKey: init_args.block_usage_key, - blockID: init_args.block_id + blockID: init_args.block_id, + transcriptAssetID: init_args.transcript_asset_id }, function (event) { window.parent.postMessage( @@ -25,6 +26,7 @@ initialMessages: INITIAL_MESSAGES, blockID: event.data.blockID, blockUsageKey: event.data.blockUsageKey, + transcriptAssetID: event.data.transcriptAssetID, }, }, init_args.learning_mfe_base_url, // Ensure correct parent origin