@@ -11,6 +11,7 @@ import React, { useEffect, useRef } from 'react';
1111import { JupyterFrontEnd } from '@jupyterlab/application' ;
1212import { INotebookTracker } from '@jupyterlab/notebook' ;
1313import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
14+ import { IDocumentManager } from '@jupyterlab/docmanager' ;
1415import { addIcon , historyIcon , deleteIcon , settingsIcon } from '@jupyterlab/ui-components' ;
1516import { ReadonlyPartialJSONObject , UUID } from '@lumino/coreutils' ;
1617
@@ -49,6 +50,7 @@ import {
4950import { getActiveCellOutput } from '../../utils/cellOutput' ;
5051import { OperatingSystem } from '../../utils/user' ;
5152import { IStreamlitPreviewManager } from '../AppPreview/StreamlitPreviewPlugin' ;
53+ import { ensureNotebookExists } from './utils' ;
5254import { waitForNotebookReady } from '../../utils/waitForNotebookReady' ;
5355import { getBase64EncodedCellOutputInNotebook } from './utils' ;
5456import { logEvent } from '../../restAPI/RestAPI' ;
@@ -122,6 +124,7 @@ interface IChatTaskpaneProps {
122124 app : JupyterFrontEnd
123125 operatingSystem : OperatingSystem
124126 websocketClient : CompletionWebsocketClient
127+ documentManager : IDocumentManager
125128}
126129
127130// Re-export types from hooks for backward compatibility
@@ -142,6 +145,7 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
142145 app,
143146 operatingSystem,
144147 websocketClient,
148+ documentManager,
145149} ) => {
146150
147151 // User signup state
@@ -374,6 +378,7 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
374378 // Step 3: No post processing step needed for explaining code.
375379 }
376380
381+
377382 const sendAgentExecutionMessage = async (
378383 input : string ,
379384 messageIndex ?: number ,
@@ -384,12 +389,6 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
384389 // Step 0: reset the state for a new message
385390 resetForNewMessage ( )
386391
387- const agentTargetNotebookPanel = agentTargetNotebookPanelRef . current
388-
389- if ( agentTargetNotebookPanel === null ) {
390- return
391- }
392-
393392 // Step 1: Add the user's message to the chat history
394393 const newChatHistoryManager = getDuplicateChatHistoryManager ( )
395394
@@ -400,15 +399,15 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
400399
401400 const agentExecutionMetadata = newChatHistoryManager . addAgentExecutionMessage (
402401 activeThreadIdRef . current ,
403- agentTargetNotebookPanel ,
402+ agentTargetNotebookPanelRef . current ,
404403 input ,
405404 additionalContext
406405 )
407406 if ( messageIndex !== undefined ) {
408407 agentExecutionMetadata . index = messageIndex
409408 }
410409
411- agentExecutionMetadata . base64EncodedActiveCellOutput = await getBase64EncodedCellOutputInNotebook ( agentTargetNotebookPanel , sendCellIDOutput )
410+ agentExecutionMetadata . base64EncodedActiveCellOutput = await getBase64EncodedCellOutputInNotebook ( agentTargetNotebookPanelRef . current , sendCellIDOutput )
412411
413412 setChatHistoryManager ( newChatHistoryManager )
414413 setLoadingStatus ( 'thinking' ) ;
@@ -430,6 +429,9 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
430429 // Step 0: reset the state for a new message
431430 resetForNewMessage ( )
432431
432+ // Ensure a notebook exists before proceeding
433+ await ensureNotebookExists ( notebookTracker , documentManager ) ;
434+
433435 // Enable follow mode when user sends a new message
434436 setAutoScrollFollowMode ( true ) ;
435437
@@ -702,6 +704,7 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
702704 app,
703705 streamlitPreviewManager,
704706 websocketClient,
707+ documentManager,
705708 chatHistoryManagerRef,
706709 activeThreadIdRef,
707710 activeRequestControllerRef,
0 commit comments