@@ -184,6 +184,11 @@ const plugin: JupyterFrontEndPlugin<void> = {
184184 const codeModel = cell . model as ICodeCellModel ;
185185 const outputs = codeModel . outputs ;
186186 let errorSection = '' ;
187+ let jsonError : {
188+ ename : string ;
189+ evalue : string ;
190+ traceback : string [ ] ;
191+ } | null = null ;
187192
188193 for ( let i = 0 ; i < outputs . length ; i ++ ) {
189194 const output = outputs . get ( i ) ;
@@ -193,6 +198,7 @@ const plugin: JupyterFrontEndPlugin<void> = {
193198 evalue : string ;
194199 traceback : string [ ] ;
195200 } ;
201+ jsonError = json ;
196202 const traceback = json . traceback
197203 . map ( line => line . replace ( ANSI_ESCAPE , '' ) )
198204 . join ( '\n' ) ;
@@ -203,45 +209,72 @@ const plugin: JupyterFrontEndPlugin<void> = {
203209 }
204210 }
205211
206- // Collect preceding markdown cells as exercise description context.
212+ // Collect preceding cells context.
207213 const notebook = notebookTracker ?. currentWidget ?. content ;
208214 const notebookPath = notebookTracker ?. currentWidget ?. context . path ?? '' ;
209- let description : string | undefined ;
215+ let studentContext = '' ;
210216 let attachment : INotebookAttachment | undefined ;
211217
212218 if ( notebook ) {
213219 const activeCellIndex = notebook . activeCellIndex ;
214- const markdownCells : { id : string ; source : string } [ ] = [ ] ;
220+ let lastMdIdx = - 1 ;
215221
222+ // Find the index of the most recent markdown cell above the active cell
216223 for ( let i = activeCellIndex - 1 ; i >= 0 ; i -- ) {
217224 const precedingCell = notebook . widgets [ i ] ;
218- if ( precedingCell . model . type === 'code' ) {
225+ if ( precedingCell . model . type === 'markdown' ) {
226+ lastMdIdx = i ;
219227 break ;
220228 }
221- if ( precedingCell . model . type === 'markdown' ) {
222- const mdSource = precedingCell . model . sharedModel . source . trim ( ) ;
223- if ( mdSource ) {
224- markdownCells . unshift ( {
225- id : precedingCell . model . id ,
226- source : mdSource
227- } ) ;
228- }
229+ }
230+
231+ // Gather all cells from that markdown cell up to activeCellIndex - 1
232+ const startIdx = lastMdIdx !== - 1 ? lastMdIdx : 0 ;
233+ const contextCells = [ ] ;
234+ for ( let i = startIdx ; i < activeCellIndex ; i ++ ) {
235+ contextCells . push ( notebook . widgets [ i ] ) ;
236+ }
237+
238+ let contextStr = '' ;
239+ const markdownCellsForAttachment = [ ] ;
240+
241+ for ( const cCell of contextCells ) {
242+ const cSource = cCell . model . sharedModel . source . trim ( ) ;
243+ if ( ! cSource ) {
244+ continue ;
245+ }
246+
247+ if ( cCell . model . type === 'markdown' ) {
248+ contextStr += `${ cSource } \n\n` ;
249+ markdownCellsForAttachment . push ( {
250+ id : cCell . model . id ,
251+ input_type : 'markdown' as const
252+ } ) ;
253+ } else if ( cCell . model . type === 'code' ) {
254+ contextStr += `Preceding Code:\n\`\`\`${ language } \n${ cSource } \n\`\`\`\n\n` ;
229255 }
230256 }
231257
232- if ( markdownCells . length > 0 ) {
233- description = markdownCells . map ( c => c . source ) . join ( '\n\n' ) ;
258+ studentContext = contextStr . trim ( ) ;
259+
260+ if ( markdownCellsForAttachment . length > 0 ) {
234261 attachment = {
235262 type : 'notebook' ,
236263 value : notebookPath ,
237- cells : markdownCells . map ( c => ( {
238- id : c . id ,
239- input_type : 'markdown' as const
240- } ) )
264+ cells : markdownCellsForAttachment
241265 } ;
242266 }
243267 }
244268
269+ // Format student answer
270+ let studentAnswer = source ;
271+ if ( jsonError ) {
272+ const traceback = jsonError . traceback
273+ . map ( line => line . replace ( ANSI_ESCAPE , '' ) )
274+ . join ( '\n' ) ;
275+ studentAnswer += `\n\nError:\n${ jsonError . ename } : ${ jsonError . evalue } \n${ traceback } ` ;
276+ }
277+
245278 const question = errorSection
246279 ? 'Can you explain this code and the error it produced?'
247280 : 'Can you explain this code?' ;
@@ -254,7 +287,8 @@ const plugin: JupyterFrontEndPlugin<void> = {
254287
255288 await tutorModel . sendMessageToAI ( {
256289 body,
257- description,
290+ studentContext,
291+ studentAnswer,
258292 attachments : attachment ? [ attachment ] : undefined
259293 } ) ;
260294 } ,
0 commit comments