| 1 | "use client"; |
| 2 | |
| 3 | import { NotebookAgentActivityInline } from "@/components/notebook/shared/NotebookAgentActivity"; |
| 4 | import { withNotebookAgentDocumentContext } from "@/lib/notebook/agent-activity"; |
| 5 | import { getNotebookAttachmentRagId } from "@/lib/notebook/attachments"; |
| 6 | import { usePresentationState } from "@/states/presentation-state"; |
| 7 | |
| 8 | interface ToolCallDisplayProps { |
| 9 | isGeneratingOutlineOverride?: boolean; |
| 10 | onOpenFileProcessor?: (context: { |
| 11 | fileAssetId?: string; |
| 12 | fileUrl?: string; |
| 13 | ragId?: string; |
| 14 | }) => void; |
| 15 | } |
| 16 | |
| 17 | export function ToolCallDisplay({ |
| 18 | isGeneratingOutlineOverride, |
| 19 | onOpenFileProcessor, |
| 20 | }: ToolCallDisplayProps) { |
| 21 | const { |
| 22 | attachedFiles, |
| 23 | extractorRagIds, |
| 24 | isGeneratingOutline: isGeneratingOutlineFromState, |
| 25 | outlineToolCalls, |
| 26 | selectedChunks, |
| 27 | webSearchEnabled, |
| 28 | } = usePresentationState((state) => ({ |
| 29 | attachedFiles: state.attachedFiles, |
| 30 | extractorRagIds: state.extractorRagIds, |
| 31 | isGeneratingOutline: state.isGeneratingOutline, |
| 32 | outlineToolCalls: state.outlineToolCalls, |
| 33 | selectedChunks: state.selectedChunks, |
| 34 | webSearchEnabled: state.webSearchEnabled, |
| 35 | })); |
| 36 | const isGeneratingOutline = |
| 37 | isGeneratingOutlineOverride ?? isGeneratingOutlineFromState; |
| 38 | const displayToolCalls = outlineToolCalls.filter( |
| 39 | (call) => call.toolName !== "loadDocumentPage", |
| 40 | ); |
| 41 | const hasSearchOrFileContext = |
| 42 | webSearchEnabled || |
| 43 | attachedFiles.length > 0 || |
| 44 | displayToolCalls.some((call) => call.toolName === "searchDocuments"); |
| 45 | const hasDynamicToolActivity = displayToolCalls.length > 0; |
| 46 | |
| 47 | if (!hasSearchOrFileContext && !hasDynamicToolActivity) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | const shouldAutoExpandActivity = |
| 52 | isGeneratingOutline && |
| 53 | (hasSearchOrFileContext || |
| 54 | hasDynamicToolActivity || |
| 55 | outlineToolCalls.length > 0); |
| 56 | const activityPanelMode = isGeneratingOutline |
| 57 | ? shouldAutoExpandActivity |
| 58 | ? "running-active" |
| 59 | : "running-idle" |
| 60 | : "idle"; |
| 61 | const toolCallsWithDocumentContext = withNotebookAgentDocumentContext( |
| 62 | displayToolCalls, |
| 63 | attachedFiles.map((file, index) => { |
| 64 | const ragId = getNotebookAttachmentRagId({ |
| 65 | attachment: file, |
| 66 | attachments: attachedFiles, |
| 67 | extractorRagIds, |
| 68 | index, |
| 69 | }); |
| 70 | |
| 71 | return { |
| 72 | fileAssetId: file.fileAssetId, |
| 73 | fileName: file.name, |
| 74 | fileUrl: file.url, |
| 75 | processingStatus: |
| 76 | file.processingStatus ?? (ragId ? "COMPLETE" : "PROCESSING"), |
| 77 | ragId, |
| 78 | }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | return ( |
| 83 | <NotebookAgentActivityInline |
| 84 | key={activityPanelMode} |
| 85 | toolCalls={toolCallsWithDocumentContext} |
| 86 | isRunning={isGeneratingOutline} |
| 87 | selectedChunks={selectedChunks} |
| 88 | defaultExpanded={activityPanelMode === "running-active"} |
| 89 | onOpenDocumentContext={onOpenFileProcessor} |
| 90 | /> |
| 91 | ); |
| 92 | } |
| 93 |