11import React , { useState , useEffect , useRef } from 'react' ;
2- import PropTypes from 'prop-types ' ;
2+ import { logError , logInfo } from '@edx/frontend-platform/logging ' ;
33import {
44 Button ,
55 Form ,
@@ -11,6 +11,23 @@ import { AutoAwesome, Close } from '@openedx/paragon/icons';
1111import { getConfig } from '@edx/frontend-platform' ;
1212import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
1313import { callWorkflowService , prepareContextData } from '../services' ;
14+ import { WORKFLOW_ACTIONS } from '../constants' ;
15+
16+ interface AIEducatorLibraryAssistComponentProps {
17+ courseId : string ;
18+ locationId : string ;
19+ setResponse : ( response : string ) => void ;
20+ hasAsked : boolean ;
21+ setHasAsked : ( hasAsked : boolean ) => void ;
22+ libraries ?: Array < { id : string ; title : string } > ;
23+ titleText ?: string ;
24+ buttonText ?: string ;
25+ preloadPreviousSession ?: boolean ;
26+ customMessage ?: string ;
27+ onSuccess ?: ( ) => void ;
28+ onError ?: ( error : any ) => void ;
29+ debug ?: boolean ;
30+ }
1431
1532/**
1633 * AI Educator Library Assist Component
@@ -24,14 +41,14 @@ const AIEducatorLibraryAssistComponent = ({
2441 hasAsked,
2542 setHasAsked,
2643 libraries : librariesProp ,
27- titleText,
28- buttonText,
29- preloadPreviousSession ,
30- customMessage ,
44+ titleText = 'AI Assistant' ,
45+ buttonText = 'Start' ,
46+ customMessage = 'Use an AI workflow to create multiple answer questions from this unit in a content library' ,
47+ preloadPreviousSession = false ,
3148 onSuccess,
3249 onError,
33- debug,
34- } ) => {
50+ debug = false ,
51+ } : AIEducatorLibraryAssistComponentProps ) => {
3552 const [ showForm , setShowForm ] = useState ( false ) ;
3653 const [ isLoading , setIsLoading ] = useState ( false ) ;
3754 const [ error , setError ] = useState ( '' ) ;
@@ -66,8 +83,7 @@ const AIEducatorLibraryAssistComponent = ({
6683 const endpoint = `${ baseUrl } /api/libraries/v2/?pagination=false&order=title` ;
6784
6885 if ( debug ) {
69- // eslint-disable-next-line no-console
70- console . log ( 'Fetching libraries from:' , endpoint ) ;
86+ logInfo ( 'Fetching libraries from:' , endpoint ) ;
7187 }
7288
7389 const { data } = await getAuthenticatedHttpClient ( ) . get ( endpoint ) ;
@@ -79,12 +95,10 @@ const AIEducatorLibraryAssistComponent = ({
7995 setLibrariesFetched ( true ) ;
8096
8197 if ( debug ) {
82- // eslint-disable-next-line no-console
83- console . log ( 'Fetched libraries:' , fetchedLibraries ) ;
98+ logInfo ( 'Fetched libraries:' , fetchedLibraries ) ;
8499 }
85100 } catch ( err ) {
86- // eslint-disable-next-line no-console
87- console . error ( 'Error fetching libraries:' , err ) ;
101+ logError ( 'Error fetching libraries:' , err ) ;
88102 setError ( 'Failed to load libraries. Please try again.' ) ;
89103 } finally {
90104 setIsLoadingLibraries ( false ) ;
@@ -116,8 +130,8 @@ const AIEducatorLibraryAssistComponent = ({
116130
117131 const data = await callWorkflowService ( {
118132 context : contextData ,
119- action : 'get_current_session_response' ,
120133 payload : {
134+ action : WORKFLOW_ACTIONS . GET_CURRENT_SESSION_RESPONSE ,
121135 requestId : `ai-request-${ Date . now ( ) } ` ,
122136 } ,
123137 } ) ;
@@ -128,14 +142,12 @@ const AIEducatorLibraryAssistComponent = ({
128142 setHasAsked ( true ) ;
129143 } else if ( debug ) {
130144 // No previous session or empty response - do nothing, show normal component
131- // eslint-disable-next-line no-console
132- console . log ( 'No previous session found or empty response' ) ;
145+ logInfo ( 'No previous session found or empty response' ) ;
133146 }
134147 } catch ( err ) {
135148 // Silent fail - no previous session is not an error
136149 if ( debug ) {
137- // eslint-disable-next-line no-console
138- console . log ( 'Error loading previous session:' , err ) ;
150+ logInfo ( 'Error loading previous session:' , err ) ;
139151 }
140152 } finally {
141153 setIsLoading ( false ) ;
@@ -179,13 +191,13 @@ const AIEducatorLibraryAssistComponent = ({
179191
180192 const data = await callWorkflowService ( {
181193 context : contextData ,
182- action : 'run_async' ,
183194 payload : {
195+ action : WORKFLOW_ACTIONS . RUN_ASYNC ,
184196 requestId : `ai-request-${ Date . now ( ) } ` ,
185- user_input : {
186- library_id : selectedLibrary ,
187- num_questions : numberOfQuestions ,
188- extra_instructions : additionalInstructions ,
197+ userInput : {
198+ libraryId : selectedLibrary ,
199+ numQuestions : numberOfQuestions ,
200+ extraInstructions : additionalInstructions ,
189201 } ,
190202 } ,
191203 } ) ;
@@ -225,8 +237,11 @@ const AIEducatorLibraryAssistComponent = ({
225237 } catch ( err ) {
226238 // eslint-disable-next-line no-console
227239 console . error ( 'Error generating library questions:' , err ) ;
228- const errorMessage = err . response ?. data ?. error
229- || err . message
240+
241+ // Type guard for error
242+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
243+ const errorMessage = ( err as any ) ?. response ?. data ?. error
244+ || error . message
230245 || 'Failed to generate questions. Please try again.' ;
231246 setError ( errorMessage ) ;
232247
@@ -419,36 +434,4 @@ const AIEducatorLibraryAssistComponent = ({
419434 ) ;
420435} ;
421436
422- AIEducatorLibraryAssistComponent . propTypes = {
423- courseId : PropTypes . string . isRequired ,
424- locationId : PropTypes . string . isRequired ,
425- hasAsked : PropTypes . bool . isRequired ,
426- setResponse : PropTypes . func . isRequired ,
427- setHasAsked : PropTypes . func . isRequired ,
428- libraries : PropTypes . arrayOf (
429- PropTypes . shape ( {
430- id : PropTypes . string . isRequired ,
431- title : PropTypes . string . isRequired ,
432- } ) ,
433- ) ,
434- titleText : PropTypes . string ,
435- buttonText : PropTypes . string ,
436- preloadPreviousSession : PropTypes . bool ,
437- customMessage : PropTypes . string ,
438- onSuccess : PropTypes . func ,
439- onError : PropTypes . func ,
440- debug : PropTypes . bool ,
441- } ;
442-
443- AIEducatorLibraryAssistComponent . defaultProps = {
444- libraries : null ,
445- titleText : 'AI Assistant' ,
446- buttonText : 'Start' ,
447- customMessage : 'Use an AI workflow to create multiple answer questions from this unit in a content library' ,
448- preloadPreviousSession : false ,
449- onSuccess : null ,
450- onError : null ,
451- debug : false ,
452- } ;
453-
454437export default AIEducatorLibraryAssistComponent ;
0 commit comments