Skip to content

Commit 26c1e5d

Browse files
committed
mito-ai: fix agent target notebook
1 parent b44eb42 commit 26c1e5d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

mito-ai/src/Extensions/AiChat/ChatTaskpane.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,6 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
389389
// Step 0: reset the state for a new message
390390
resetForNewMessage()
391391

392-
// Ensure a notebook exists before proceeding
393-
const agentTargetNotebookPanel = await ensureNotebookExists(notebookTracker, documentManager);
394-
agentTargetNotebookPanelRef.current = agentTargetNotebookPanel;
395-
396392
// Step 1: Add the user's message to the chat history
397393
const newChatHistoryManager = getDuplicateChatHistoryManager()
398394

@@ -403,15 +399,15 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
403399

404400
const agentExecutionMetadata = newChatHistoryManager.addAgentExecutionMessage(
405401
activeThreadIdRef.current,
406-
agentTargetNotebookPanel,
402+
agentTargetNotebookPanelRef.current,
407403
input,
408404
additionalContext
409405
)
410406
if (messageIndex !== undefined) {
411407
agentExecutionMetadata.index = messageIndex
412408
}
413409

414-
agentExecutionMetadata.base64EncodedActiveCellOutput = await getBase64EncodedCellOutputInNotebook(agentTargetNotebookPanel, sendCellIDOutput)
410+
agentExecutionMetadata.base64EncodedActiveCellOutput = await getBase64EncodedCellOutputInNotebook(agentTargetNotebookPanelRef.current, sendCellIDOutput)
415411

416412
setChatHistoryManager(newChatHistoryManager)
417413
setLoadingStatus('thinking');
@@ -708,6 +704,7 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
708704
app,
709705
streamlitPreviewManager,
710706
websocketClient,
707+
documentManager,
711708
chatHistoryManagerRef,
712709
activeThreadIdRef,
713710
activeRequestControllerRef,

mito-ai/src/Extensions/AiChat/hooks/useAgentExecution.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { useRef, useState } from 'react';
77
import { JupyterFrontEnd } from '@jupyterlab/application';
88
import { INotebookTracker } from '@jupyterlab/notebook';
9+
import { IDocumentManager } from '@jupyterlab/docmanager';
910
import { UUID } from '@lumino/coreutils';
1011
import { IStreamlitPreviewManager } from '../../AppPreview/StreamlitPreviewPlugin';
1112
import { CompletionWebsocketClient } from '../../../websockets/completions/CompletionsWebsocketClient';
@@ -18,6 +19,7 @@ import { getCodeBlockFromMessage } from '../../../utils/strings';
1819
import { getAIOptimizedCellsInNotebookPanel, setActiveCellByIDInNotebookPanel } from '../../../utils/notebook';
1920
import { AgentReviewStatus } from '../ChatTaskpane';
2021
import { LoadingStatus } from './useChatState';
22+
import { ensureNotebookExists } from '../utils';
2123

2224
export type AgentExecutionStatus = 'working' | 'stopping' | 'idle';
2325

@@ -28,6 +30,7 @@ interface UseAgentExecutionProps {
2830
app: JupyterFrontEnd;
2931
streamlitPreviewManager: IStreamlitPreviewManager;
3032
websocketClient: CompletionWebsocketClient;
33+
documentManager: IDocumentManager;
3134
chatHistoryManagerRef: React.MutableRefObject<ChatHistoryManager>;
3235
activeThreadIdRef: React.MutableRefObject<string>;
3336
activeRequestControllerRef: React.MutableRefObject<AbortController | null>;
@@ -63,6 +66,7 @@ export const useAgentExecution = ({
6366
app,
6467
streamlitPreviewManager,
6568
websocketClient,
69+
documentManager,
6670
chatHistoryManagerRef,
6771
activeThreadIdRef,
6872
activeRequestControllerRef,
@@ -133,7 +137,10 @@ export const useAgentExecution = ({
133137
messageIndex?: number,
134138
additionalContext?: Array<{ type: string, value: string }>
135139
): Promise<void> => {
136-
agentTargetNotebookPanelRef.current = notebookTracker.currentWidget;
140+
141+
// Ensure a notebook exists before proceeding with agent execution
142+
const agentTargetNotebookPanel = await ensureNotebookExists(notebookTracker, documentManager);
143+
agentTargetNotebookPanelRef.current = agentTargetNotebookPanel;
137144

138145
agentReview.acceptAllAICode();
139146
agentReview.setNotebookSnapshotPreAgentExecution(getAIOptimizedCellsInNotebookPanel(agentTargetNotebookPanelRef.current));

0 commit comments

Comments
 (0)