import { VSCodeButton, VSCodeLink } from "@vscode/webview-ui-toolkit/react" import debounce from "debounce" import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { useDeepCompareEffect, useEvent, useMount } from "react-use" import { Virtuoso, type VirtuosoHandle } from "react-virtuoso" import styled from "styled-components" import { ClineApiReqInfo, ClineAsk, ClineMessage, ClineSayBrowserAction, ClineSayTool, ExtensionMessage, } from "@shared/ExtensionMessage" import { findLast } from "@shared/array" import { combineApiRequests } from "@shared/combineApiRequests" import { combineCommandSequences } from "@shared/combineCommandSequences" import { getApiMetrics } from "@shared/getApiMetrics" import { useExtensionState } from "@/context/ExtensionStateContext" import { vscode } from "@/utils/vscode" import { TaskServiceClient, SlashServiceClient, FileServiceClient, UiServiceClient } from "@/services/grpc-client" import HistoryPreview from "@/components/history/HistoryPreview" import { normalizeApiConfiguration } from "@/components/settings/ApiOptions" import Announcement from "@/components/chat/Announcement" import BrowserSessionRow from "@/components/chat/BrowserSessionRow" import ChatRow from "@/components/chat/ChatRow" import ChatTextArea from "@/components/chat/ChatTextArea" import QuotedMessagePreview from "@/components/chat/QuotedMessagePreview" import TaskHeader from "@/components/chat/TaskHeader" import TelemetryBanner from "@/components/common/TelemetryBanner" import { unified } from "unified" import remarkStringify from "remark-stringify" import rehypeRemark from "rehype-remark" import rehypeParse from "rehype-parse" import HomeHeader from "../welcome/HomeHeader" import AutoApproveBar from "./auto-approve-menu/AutoApproveBar" import { SuggestedTasks } from "../welcome/SuggestedTasks" interface ChatViewProps { isHidden: boolean showAnnouncement: boolean hideAnnouncement: () => void showHistoryView: () => void } // Function to clean up markdown escape characters function cleanupMarkdownEscapes(markdown: string): string { return ( markdown // Handle underscores and asterisks (single or multiple) .replace(/\\([_*]+)/g, "$1") // Handle angle brackets (for generics and XML) .replace(/\\([<>])/g, "$1") // Handle backticks (for code) .replace(/\\(`)/g, "$1") // Handle other common markdown special characters .replace(/\\([[\]()#.!])/g, "$1") // Fix multiple consecutive backslashes .replace(/\\{2,}([_*`<>[\]()#.!])/g, "$1") ) } async function convertHtmlToMarkdown(html: string) { // Process the HTML to Markdown const result = await unified() .use(rehypeParse as any, { fragment: true }) // Parse HTML fragments .use(rehypeRemark as any) // Convert HTML to Markdown AST .use(remarkStringify as any, { // Convert Markdown AST to text bullet: "-", // Use - for unordered lists emphasis: "*", // Use * for emphasis strong: "_", // Use _ for strong listItemIndent: "one", // Use one space for list indentation rule: "-", // Use - for horizontal rules ruleSpaces: false, // No spaces in horizontal rules fences: true, escape: false, entities: false, }) .process(html) const md = String(result) // Apply comprehensive cleanup of escape characters return cleanupMarkdownEscapes(md) } // Anthropic limits to 20 images, which we use to constrain both images & files for simplicity export const MAX_IMAGES_AND_FILES_PER_MESSAGE = 20 const QUICK_WINS_HISTORY_THRESHOLD = 300 const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryView }: ChatViewProps) => { const { version, clineMessages: messages, taskHistory, apiConfiguration, telemetrySetting } = useExtensionState() const shouldShowQuickWins = false // !taskHistory || taskHistory.length < QUICK_WINS_HISTORY_THRESHOLD //const task = messages.length > 0 ? (messages[0].say === "task" ? messages[0] : undefined) : undefined) : undefined const task = useMemo(() => messages.at(0), [messages]) // leaving this less safe version here since if the first message is not a task, then the extension is in a bad state and needs to be debugged (see Cline.abort) const modifiedMessages = useMemo(() => combineApiRequests(combineCommandSequences(messages.slice(1))), [messages]) // has to be after api_req_finished are all reduced into api_req_started messages const apiMetrics = useMemo(() => getApiMetrics(modifiedMessages), [modifiedMessages]) const lastApiReqTotalTokens = useMemo(() => { const getTotalTokensFromApiReqMessage = (msg: ClineMessage) => { if (!msg.text) return 0 const { tokensIn, tokensOut, cacheWrites, cacheReads }: ClineApiReqInfo = JSON.parse(msg.text) return (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0) } const lastApiReqMessage = findLast(modifiedMessages, (msg) => { if (msg.say !== "api_req_started") return false return getTotalTokensFromApiReqMessage(msg) > 0 }) if (!lastApiReqMessage) return undefined return getTotalTokensFromApiReqMessage(lastApiReqMessage) }, [modifiedMessages]) const [inputValue, setInputValue] = useState("") const [activeQuote, setActiveQuote] = useState(null) const [isTextAreaFocused, setIsTextAreaFocused] = useState(false) const textAreaRef = useRef(null) const [sendingDisabled, setSendingDisabled] = useState(false) const [selectedImages, setSelectedImages] = useState([]) const [selectedFiles, setSelectedFiles] = useState([]) // we need to hold on to the ask because useEffect > lastMessage will always let us know when an ask comes in and handle it, but by the time handleMessage is called, the last message might not be the ask anymore (it could be a say that followed) const [clineAsk, setClineAsk] = useState(undefined) const [enableButtons, setEnableButtons] = useState(false) const [primaryButtonText, setPrimaryButtonText] = useState("Approve") const [secondaryButtonText, setSecondaryButtonText] = useState("Reject") const [didClickCancel, setDidClickCancel] = useState(false) const virtuosoRef = useRef(null) const [expandedRows, setExpandedRows] = useState>({}) const scrollContainerRef = useRef(null) const disableAutoScrollRef = useRef(false) const [showScrollToBottom, setShowScrollToBottom] = useState(false) const [isAtBottom, setIsAtBottom] = useState(false) const [pendingScrollToMessage, setPendingScrollToMessage] = useState(null) useEffect(() => { const handleCopy = async (e: ClipboardEvent) => { const targetElement = e.target as HTMLElement | null // If the copy event originated from an input or textarea, // let the default browser behavior handle it. if ( targetElement && (targetElement.tagName === "INPUT" || targetElement.tagName === "TEXTAREA" || targetElement.isContentEditable) ) { return } if (window.getSelection) { const selection = window.getSelection() if (selection && selection.rangeCount > 0) { const range = selection.getRangeAt(0) const commonAncestor = range.commonAncestorContainer let textToCopy: string | null = null // Check if the selection is inside an element where plain text copy is preferred let currentElement = commonAncestor.nodeType === Node.ELEMENT_NODE ? (commonAncestor as HTMLElement) : commonAncestor.parentElement let preferPlainTextCopy = false while (currentElement) { if (currentElement.tagName === "PRE" && currentElement.querySelector("code")) { preferPlainTextCopy = true break } // Check computed white-space style const computedStyle = window.getComputedStyle(currentElement) if ( computedStyle.whiteSpace === "pre" || computedStyle.whiteSpace === "pre-wrap" || computedStyle.whiteSpace === "pre-line" ) { // If the element itself or an ancestor has pre-like white-space, // and the selection is likely contained within it, prefer plain text. // This helps with elements like the TaskHeader's text display. preferPlainTextCopy = true break } // Stop searching if we reach a known chat message boundary or body if ( currentElement.classList.contains("chat-row-assistant-message-container") || currentElement.classList.contains("chat-row-user-message-container") || currentElement.tagName === "BODY" ) { break } currentElement = currentElement.parentElement } if (preferPlainTextCopy) { // For code blocks or elements with pre-formatted white-space, get plain text. textToCopy = selection.toString() } else { // For other content, use the existing HTML-to-Markdown conversion const clonedSelection = range.cloneContents() const div = document.createElement("div") div.appendChild(clonedSelection) const selectedHtml = div.innerHTML textToCopy = await convertHtmlToMarkdown(selectedHtml) } if (textToCopy !== null) { try { FileServiceClient.copyToClipboard({ value: textToCopy }).catch((err) => { console.error("Error copying to clipboard:", err) }) e.preventDefault() } catch (error) { console.error("Error copying to clipboard:", error) } } } } } document.addEventListener("copy", handleCopy) return () => { document.removeEventListener("copy", handleCopy) } }, []) // UI layout depends on the last 2 messages // (since it relies on the content of these messages, we are deep comparing. i.e. the button state after hitting button sets enableButtons to false, and this effect otherwise would have to true again even if messages didn't change const lastMessage = useMemo(() => messages.at(-1), [messages]) const secondLastMessage = useMemo(() => messages.at(-2), [messages]) useDeepCompareEffect(() => { // if last message is an ask, show user ask UI // if user finished a task, then start a new task with a new conversation history since in this moment that the extension is waiting for user response, the user could close the extension and the conversation history would be lost. // basically as long as a task is active, the conversation history will be persisted if (lastMessage) { switch (lastMessage.type) { case "ask": const isPartial = lastMessage.partial === true switch (lastMessage.ask) { case "api_req_failed": setSendingDisabled(true) setClineAsk("api_req_failed") setEnableButtons(true) setPrimaryButtonText("Retry") setSecondaryButtonText("Start New Task") break case "mistake_limit_reached": setSendingDisabled(false) setClineAsk("mistake_limit_reached") setEnableButtons(true) setPrimaryButtonText("Proceed Anyways") setSecondaryButtonText("Start New Task") break case "auto_approval_max_req_reached": setSendingDisabled(true) setClineAsk("auto_approval_max_req_reached") setEnableButtons(true) setPrimaryButtonText("Proceed") setSecondaryButtonText("Start New Task") break case "followup": setSendingDisabled(isPartial) setClineAsk("followup") setEnableButtons(false) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) break case "plan_mode_respond": setSendingDisabled(isPartial) setClineAsk("plan_mode_respond") setEnableButtons(false) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) break case "tool": setSendingDisabled(isPartial) setClineAsk("tool") setEnableButtons(!isPartial) const tool = JSON.parse(lastMessage.text || "{}") as ClineSayTool switch (tool.tool) { case "editedExistingFile": case "newFileCreated": setPrimaryButtonText("Save") setSecondaryButtonText("Reject") break default: setPrimaryButtonText("Approve") setSecondaryButtonText("Reject") break } break case "browser_action_launch": setSendingDisabled(isPartial) setClineAsk("browser_action_launch") setEnableButtons(!isPartial) setPrimaryButtonText("Approve") setSecondaryButtonText("Reject") break case "command": setSendingDisabled(isPartial) setClineAsk("command") setEnableButtons(!isPartial) setPrimaryButtonText("Run Command") setSecondaryButtonText("Reject") break case "command_output": setSendingDisabled(false) setClineAsk("command_output") setEnableButtons(true) setPrimaryButtonText("Proceed While Running") setSecondaryButtonText(undefined) break case "use_mcp_server": setSendingDisabled(isPartial) setClineAsk("use_mcp_server") setEnableButtons(!isPartial) setPrimaryButtonText("Approve") setSecondaryButtonText("Reject") break case "completion_result": // extension waiting for feedback. but we can just present a new task button setSendingDisabled(isPartial) setClineAsk("completion_result") setEnableButtons(!isPartial) setPrimaryButtonText("Start New Task") setSecondaryButtonText(undefined) break case "resume_task": setSendingDisabled(false) setClineAsk("resume_task") setEnableButtons(true) setPrimaryButtonText("Resume Task") setSecondaryButtonText(undefined) setDidClickCancel(false) // special case where we reset the cancel button state break case "resume_completed_task": setSendingDisabled(false) setClineAsk("resume_completed_task") setEnableButtons(true) setPrimaryButtonText("Start New Task") setSecondaryButtonText(undefined) setDidClickCancel(false) break case "new_task": setSendingDisabled(isPartial) setClineAsk("new_task") setEnableButtons(!isPartial) setPrimaryButtonText("Start New Task with Context") setSecondaryButtonText(undefined) break case "condense": setSendingDisabled(isPartial) setClineAsk("condense") setEnableButtons(!isPartial) setPrimaryButtonText("Condense Conversation") setSecondaryButtonText(undefined) break case "report_bug": setSendingDisabled(isPartial) setClineAsk("report_bug") setEnableButtons(!isPartial) setPrimaryButtonText("Report GitHub issue") setSecondaryButtonText(undefined) break } break case "say": // don't want to reset since there could be a "say" after an "ask" while ask is waiting for response switch (lastMessage.say) { case "api_req_started": if (secondLastMessage?.ask === "command_output") { // if the last ask is a command_output, and we receive an api_req_started, then that means the command has finished and we don't need input from the user anymore (in every other case, the user has to interact with input field or buttons to continue, which does the following automatically) setInputValue("") setSendingDisabled(true) setSelectedImages([]) setSelectedFiles([]) setClineAsk(undefined) setEnableButtons(false) } break case "task": case "error": case "api_req_finished": case "text": case "browser_action": case "browser_action_result": case "browser_action_launch": case "command": case "use_mcp_server": case "command_output": case "mcp_server_request_started": case "mcp_server_response": case "completion_result": case "tool": case "load_mcp_documentation": break } break } } else { // this would get called after sending the first message, so we have to watch messages.length instead // No messages, so user has to submit a task // setTextAreaDisabled(false) // setClineAsk(undefined) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) } }, [lastMessage, secondLastMessage]) useEffect(() => { if (messages.length === 0) { setSendingDisabled(false) setClineAsk(undefined) setEnableButtons(false) setPrimaryButtonText("Approve") setSecondaryButtonText("Reject") } }, [messages.length]) useEffect(() => { setExpandedRows({}) }, [task?.ts]) const isStreaming = useMemo(() => { const isLastAsk = !!modifiedMessages.at(-1)?.ask // checking clineAsk isn't enough since messages effect may be called again for a tool for example, set clineAsk to its value, and if the next message is not an ask then it doesn't reset. This is likely due to how much more often we're updating messages as compared to before, and should be resolved with optimizations as it's likely a rendering bug. but as a final guard for now, the cancel button will show if the last message is not an ask const isToolCurrentlyAsking = isLastAsk && clineAsk !== undefined && enableButtons && primaryButtonText !== undefined if (isToolCurrentlyAsking) { return false } const isLastMessagePartial = modifiedMessages.at(-1)?.partial === true if (isLastMessagePartial) { return true } else { const lastApiReqStarted = findLast(modifiedMessages, (message) => message.say === "api_req_started") if (lastApiReqStarted && lastApiReqStarted.text != null && lastApiReqStarted.say === "api_req_started") { const cost = JSON.parse(lastApiReqStarted.text).cost if (cost === undefined) { // api request has not finished yet return true } } } return false }, [modifiedMessages, clineAsk, enableButtons, primaryButtonText]) const handleSendMessage = useCallback( async (text: string, images: string[], files: string[]) => { let messageToSend = text.trim() const hasContent = messageToSend || images.length > 0 || files.length > 0 // Prepend the active quote if it exists if (activeQuote && hasContent) { const prefix = "[context] \n> " const formattedQuote = activeQuote const suffix = "\n[/context] \n\n" messageToSend = `${prefix} ${formattedQuote} ${suffix} ${messageToSend}` } if (hasContent) { console.log("[ChatView] handleSendMessage - Sending message:", messageToSend) if (messages.length === 0) { await TaskServiceClient.newTask({ text: messageToSend, images, files }) } else if (clineAsk) { switch (clineAsk) { case "followup": case "plan_mode_respond": case "tool": case "browser_action_launch": case "command": // user can provide feedback to a tool or command use case "command_output": // user can send input to command stdin case "use_mcp_server": case "completion_result": // if this happens then the user has feedback for the completion result case "resume_task": case "resume_completed_task": case "mistake_limit_reached": case "new_task": // user can provide feedback or reject the new task suggestion case "condense": case "report_bug": await TaskServiceClient.askResponse({ responseType: "messageResponse", text: messageToSend, images, files, }) break // there is no other case that a textfield should be enabled } } setInputValue("") setActiveQuote(null) // Clear quote when sending message setSendingDisabled(true) setSelectedImages([]) setSelectedFiles([]) setClineAsk(undefined) setEnableButtons(false) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) disableAutoScrollRef.current = false } }, [messages.length, clineAsk, activeQuote], ) const startNewTask = useCallback(async () => { setActiveQuote(null) // Clear the active quote state await TaskServiceClient.clearTask({}) }, []) /* This logic depends on the useEffect[messages] above to set clineAsk, after which buttons are shown and we then send an askResponse to the extension. */ const handlePrimaryButtonClick = useCallback( async (text?: string, images?: string[], files?: string[]) => { const trimmedInput = text?.trim() switch (clineAsk) { case "api_req_failed": case "command": case "command_output": case "tool": case "browser_action_launch": case "use_mcp_server": case "resume_task": case "mistake_limit_reached": case "auto_approval_max_req_reached": if (trimmedInput || (images && images.length > 0) || (files && files.length > 0)) { await TaskServiceClient.askResponse({ responseType: "yesButtonClicked", text: trimmedInput, images: images, files: files, }) } else { await TaskServiceClient.askResponse({ responseType: "yesButtonClicked", }) } // Clear input state after sending setInputValue("") setActiveQuote(null) // Clear quote when using primary button setSelectedImages([]) setSelectedFiles([]) break case "completion_result": case "resume_completed_task": // extension waiting for feedback. but we can just present a new task button startNewTask() break case "new_task": console.info("new task button clicked!", { lastMessage, messages, clineAsk, text }) await TaskServiceClient.newTask({ text: lastMessage?.text, images: [], files: [], }) break case "condense": await SlashServiceClient.condense({ value: lastMessage?.text }).catch((err) => console.error(err)) break case "report_bug": await SlashServiceClient.reportBug({ value: lastMessage?.text }).catch((err) => console.error(err)) break } setSendingDisabled(true) setClineAsk(undefined) setEnableButtons(false) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) disableAutoScrollRef.current = false }, [clineAsk, startNewTask, lastMessage], ) const handleSecondaryButtonClick = useCallback( async (text?: string, images?: string[], files?: string[]) => { const trimmedInput = text?.trim() if (isStreaming) { await TaskServiceClient.cancelTask({}) setDidClickCancel(true) return } switch (clineAsk) { case "api_req_failed": case "mistake_limit_reached": case "auto_approval_max_req_reached": startNewTask() break case "command": case "tool": case "browser_action_launch": case "use_mcp_server": if (trimmedInput || (images && images.length > 0) || (files && files.length > 0)) { await TaskServiceClient.askResponse({ responseType: "noButtonClicked", text: trimmedInput, images: images, files: files, }) } else { // responds to the API with a "This operation failed" and lets it try again await TaskServiceClient.askResponse({ responseType: "noButtonClicked", }) } // Clear input state after sending setInputValue("") setActiveQuote(null) // Clear quote when using secondary button setSelectedImages([]) setSelectedFiles([]) break } setSendingDisabled(true) setClineAsk(undefined) setEnableButtons(false) // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) disableAutoScrollRef.current = false }, [clineAsk, startNewTask, isStreaming], ) const handleTaskCloseButtonClick = useCallback(() => { startNewTask() }, [startNewTask]) const handleFocusChange = useCallback((isFocused: boolean) => { setIsTextAreaFocused(isFocused) }, []) const { selectedModelInfo } = useMemo(() => { return normalizeApiConfiguration(apiConfiguration) }, [apiConfiguration]) const selectFilesAndImages = useCallback(async () => { try { const response = await FileServiceClient.selectFiles({ value: selectedModelInfo.supportsImages, }) if ( response && response.values1 && response.values2 && (response.values1.length > 0 || response.values2.length > 0) ) { const currentTotal = selectedImages.length + selectedFiles.length const availableSlots = MAX_IMAGES_AND_FILES_PER_MESSAGE - currentTotal if (availableSlots > 0) { // Prioritize images first const imagesToAdd = Math.min(response.values1.length, availableSlots) if (imagesToAdd > 0) { setSelectedImages((prevImages) => [...prevImages, ...response.values1.slice(0, imagesToAdd)]) } // Use remaining slots for files const remainingSlots = availableSlots - imagesToAdd if (remainingSlots > 0) { setSelectedFiles((prevFiles) => [...prevFiles, ...response.values2.slice(0, remainingSlots)]) } } } } catch (error) { console.error("Error selecting images & files:", error) } }, [selectedModelInfo.supportsImages]) const shouldDisableFilesAndImages = selectedImages.length + selectedFiles.length >= MAX_IMAGES_AND_FILES_PER_MESSAGE const handleMessage = useCallback( (e: MessageEvent) => { const message: ExtensionMessage = e.data switch (message.type) { case "action": switch (message.action!) { case "didBecomeVisible": if (!isHidden && !sendingDisabled && !enableButtons) { textAreaRef.current?.focus() } break case "focusChatInput": textAreaRef.current?.focus() if (isHidden) { window.dispatchEvent(new CustomEvent("chatButtonClicked")) } break } break } // textAreaRef.current is not explicitly required here since react guarantees that ref will be stable across re-renders, and we're not using its value but its reference. }, [isHidden, sendingDisabled, enableButtons, handleSendMessage, handlePrimaryButtonClick, handleSecondaryButtonClick], ) useEvent("message", handleMessage) // Set up addToInput subscription useEffect(() => { const cleanup = UiServiceClient.subscribeToAddToInput( {}, { onResponse: (event) => { if (event.value) { setInputValue((prevValue) => { const newText = event.value const newTextWithNewline = newText + "\n" return prevValue ? `${prevValue}\n${newTextWithNewline}` : newTextWithNewline }) // Add scroll to bottom after state update // Auto focus the input and start the cursor on a new line for easy typing setTimeout(() => { if (textAreaRef.current) { textAreaRef.current.scrollTop = textAreaRef.current.scrollHeight textAreaRef.current.focus() } }, 0) } }, onError: (error) => { console.error("Error in addToInput subscription:", error) }, onComplete: () => { console.log("addToInput subscription completed") }, }, ) return cleanup }, []) useMount(() => { // NOTE: the vscode window needs to be focused for this to work textAreaRef.current?.focus() }) useEffect(() => { const timer = setTimeout(() => { if (!isHidden && !sendingDisabled && !enableButtons) { textAreaRef.current?.focus() } }, 50) return () => { clearTimeout(timer) } }, [isHidden, sendingDisabled, enableButtons]) const visibleMessages = useMemo(() => { return modifiedMessages.filter((message) => { switch (message.ask) { case "completion_result": // don't show a chat row for a completion_result ask without text. This specific type of message only occurs if cline wants to execute a command as part of its completion result, in which case we interject the completion_result tool with the execute_command tool. if (message.text === "") { return false } break case "api_req_failed": // this message is used to update the latest api_req_started that the request failed case "resume_task": case "resume_completed_task": return false } switch (message.say) { case "api_req_finished": // combineApiRequests removes this from modifiedMessages anyways case "api_req_retried": // this message is used to update the latest api_req_started that the request was retried case "deleted_api_reqs": // aggregated api_req metrics from deleted messages return false case "text": // Sometimes cline returns an empty text message, we don't want to render these. (We also use a say text for user messages, so in case they just sent images we still render that) if ((message.text ?? "") === "" && (message.images?.length ?? 0) === 0) { return false } break case "mcp_server_request_started": return false } return true }) }, [modifiedMessages]) const isBrowserSessionMessage = (message: ClineMessage): boolean => { // which of visible messages are browser session messages, see above // NOTE: any messages we want to make as part of a browser session should be included here // There was an issue where we added checkpoints after browser actions, and it resulted in browser sessions being disrupted. if (message.type === "ask") { return ["browser_action_launch"].includes(message.ask!) } if (message.type === "say") { return [ "browser_action_launch", "api_req_started", "text", "browser_action", "browser_action_result", "checkpoint_created", "reasoning", ].includes(message.say!) } return false } const groupedMessages = useMemo(() => { const result: (ClineMessage | ClineMessage[])[] = [] let currentGroup: ClineMessage[] = [] let isInBrowserSession = false const endBrowserSession = () => { if (currentGroup.length > 0) { result.push([...currentGroup]) currentGroup = [] isInBrowserSession = false } } visibleMessages.forEach((message) => { if (message.ask === "browser_action_launch" || message.say === "browser_action_launch") { // complete existing browser session if any endBrowserSession() // start new isInBrowserSession = true currentGroup.push(message) } else if (isInBrowserSession) { // end session if api_req_started is cancelled if (message.say === "api_req_started") { // get last api_req_started in currentGroup to check if it's cancelled. If it is then this api req is not part of the current browser session const lastApiReqStarted = [...currentGroup].reverse().find((m) => m.say === "api_req_started") if (lastApiReqStarted?.text != null) { const info = JSON.parse(lastApiReqStarted.text) const isCancelled = info.cancelReason != null if (isCancelled) { endBrowserSession() result.push(message) return } } } if (isBrowserSessionMessage(message)) { currentGroup.push(message) // Check if this is a close action if (message.say === "browser_action") { const browserAction = JSON.parse(message.text || "{}") as ClineSayBrowserAction if (browserAction.action === "close") { endBrowserSession() } } } else { // complete existing browser session if any endBrowserSession() result.push(message) } } else { result.push(message) } }) // Handle case where browser session is the last group if (currentGroup.length > 0) { result.push([...currentGroup]) } return result }, [visibleMessages]) // scrolling const scrollToBottomSmooth = useMemo( () => debounce( () => { virtuosoRef.current?.scrollTo({ top: Number.MAX_SAFE_INTEGER, behavior: "smooth", }) }, 10, { immediate: true }, ), [], ) const scrollToBottomAuto = useCallback(() => { virtuosoRef.current?.scrollTo({ top: Number.MAX_SAFE_INTEGER, behavior: "auto", // instant causes crash }) }, []) const scrollToMessage = useCallback( (messageIndex: number) => { setPendingScrollToMessage(messageIndex) const targetMessage = messages[messageIndex] if (!targetMessage) { setPendingScrollToMessage(null) return } const visibleIndex = visibleMessages.findIndex((msg) => msg.ts === targetMessage.ts) if (visibleIndex === -1) { setPendingScrollToMessage(null) return } let groupIndex = -1 let currentVisibleIndex = 0 for (let i = 0; i < groupedMessages.length; i++) { const group = groupedMessages[i] if (Array.isArray(group)) { const groupSize = group.length const messageInGroup = group.some((msg) => msg.ts === targetMessage.ts) if (messageInGroup) { groupIndex = i break } currentVisibleIndex += groupSize } else { if (group.ts === targetMessage.ts) { groupIndex = i break } currentVisibleIndex++ } } if (groupIndex !== -1) { setPendingScrollToMessage(null) disableAutoScrollRef.current = true requestAnimationFrame(() => { requestAnimationFrame(() => { virtuosoRef.current?.scrollToIndex({ index: groupIndex, align: "start", behavior: "smooth", }) }) }) } }, [messages, visibleMessages, groupedMessages], ) // scroll when user toggles certain rows const toggleRowExpansion = useCallback( (ts: number) => { const isCollapsing = expandedRows[ts] ?? false const lastGroup = groupedMessages.at(-1) const isLast = Array.isArray(lastGroup) ? lastGroup[0].ts === ts : lastGroup?.ts === ts const secondToLastGroup = groupedMessages.at(-2) const isSecondToLast = Array.isArray(secondToLastGroup) ? secondToLastGroup[0].ts === ts : secondToLastGroup?.ts === ts const isLastCollapsedApiReq = isLast && !Array.isArray(lastGroup) && // Make sure it's not a browser session group lastGroup?.say === "api_req_started" && !expandedRows[lastGroup.ts] setExpandedRows((prev) => ({ ...prev, [ts]: !prev[ts], })) // disable auto scroll when user expands row if (!isCollapsing) { disableAutoScrollRef.current = true } if (isCollapsing && isAtBottom) { const timer = setTimeout(() => { scrollToBottomAuto() }, 0) return () => clearTimeout(timer) } else if (isLast || isSecondToLast) { if (isCollapsing) { if (isSecondToLast && !isLastCollapsedApiReq) { return } const timer = setTimeout(() => { scrollToBottomAuto() }, 0) return () => clearTimeout(timer) } else { const timer = setTimeout(() => { virtuosoRef.current?.scrollToIndex({ index: groupedMessages.length - (isLast ? 1 : 2), align: "start", }) }, 0) return () => clearTimeout(timer) } } }, [groupedMessages, expandedRows, scrollToBottomAuto, isAtBottom], ) const handleRowHeightChange = useCallback( (isTaller: boolean) => { if (!disableAutoScrollRef.current) { if (isTaller) { scrollToBottomSmooth() } else { setTimeout(() => { scrollToBottomAuto() }, 0) } } }, [scrollToBottomSmooth, scrollToBottomAuto], ) useEffect(() => { if (!disableAutoScrollRef.current) { setTimeout(() => { scrollToBottomSmooth() }, 50) // return () => clearTimeout(timer) // dont cleanup since if visibleMessages.length changes it cancels. } }, [groupedMessages.length, scrollToBottomSmooth]) useEffect(() => { if (pendingScrollToMessage !== null) { scrollToMessage(pendingScrollToMessage) } }, [pendingScrollToMessage, groupedMessages, scrollToMessage]) const handleWheel = useCallback((event: Event) => { const wheelEvent = event as WheelEvent if (wheelEvent.deltaY && wheelEvent.deltaY < 0) { if (scrollContainerRef.current?.contains(wheelEvent.target as Node)) { // user scrolled up disableAutoScrollRef.current = true } } }, []) useEvent("wheel", handleWheel, window, { passive: true }) // passive improves scrolling performance const placeholderText = useMemo(() => { const text = task ? "Type a message..." : "Type your task here..." return text }, [task]) const itemContent = useCallback( (index: number, messageOrGroup: ClineMessage | ClineMessage[]) => { // browser session group if (Array.isArray(messageOrGroup)) { return ( expandedRows[messageTs] ?? false} onToggleExpand={(messageTs: number) => { setExpandedRows((prev) => ({ ...prev, [messageTs]: !prev[messageTs], })) }} onSetQuote={setActiveQuote} /> ) } // We display certain statuses for the last message only // If the last message is a checkpoint, we want to show the status of the previous message const nextMessage = index < groupedMessages.length - 1 && groupedMessages[index + 1] const isNextCheckpoint = !Array.isArray(nextMessage) && nextMessage && nextMessage?.say === "checkpoint_created" const isLastMessageGroup = isNextCheckpoint && index === groupedMessages.length - 2 const isLast = index === groupedMessages.length - 1 || isLastMessageGroup // regular message return ( toggleRowExpansion(messageOrGroup.ts)} lastModifiedMessage={modifiedMessages.at(-1)} isLast={isLast} onHeightChange={handleRowHeightChange} inputValue={inputValue} sendMessageFromChatRow={handleSendMessage} onSetQuote={setActiveQuote} /> ) }, [ expandedRows, modifiedMessages, groupedMessages.length, toggleRowExpansion, handleRowHeightChange, inputValue, setActiveQuote, ], ) return (
{task ? ( ) : (
{telemetrySetting === "unset" && } {showAnnouncement && } {!shouldShowQuickWins && taskHistory.length > 0 && }
)} {!task && ( <> )} {task && ( <>
, // Add empty padding at the bottom }} // increasing top by 3_000 to prevent jumping around when user collapses a row increaseViewportBy={{ top: 3_000, bottom: Number.MAX_SAFE_INTEGER, }} // hack to make sure the last message is always rendered to get truly perfect scroll to bottom animation when new messages are added (Number.MAX_SAFE_INTEGER is safe for arithmetic operations, which is all virtuoso uses this value for in src/sizeRangeSystem.ts) data={groupedMessages} // messages is the raw format returned by extension, modifiedMessages is the manipulated structure that combines certain messages of related type, and visibleMessages is the filtered structure that removes messages that should not be rendered itemContent={itemContent} atBottomStateChange={(isAtBottom) => { setIsAtBottom(isAtBottom) if (isAtBottom) { disableAutoScrollRef.current = false } setShowScrollToBottom(disableAutoScrollRef.current && !isAtBottom) }} atBottomThreshold={10} // anything lower causes issues with followOutput initialTopMostItemIndex={groupedMessages.length - 1} />
{showScrollToBottom ? (
{ scrollToBottomSmooth() disableAutoScrollRef.current = false }}>
) : (
{primaryButtonText && !isStreaming && ( handlePrimaryButtonClick(inputValue, selectedImages, selectedFiles)}> {primaryButtonText} )} {(secondaryButtonText || isStreaming) && ( handleSecondaryButtonClick(inputValue, selectedImages, selectedFiles)}> {isStreaming ? "Cancel" : secondaryButtonText} )}
)} )} {(() => { return activeQuote ? (
setActiveQuote(null)} isFocused={isTextAreaFocused} />
) : null })()} handleSendMessage(inputValue, selectedImages, selectedFiles)} onSelectFilesAndImages={selectFilesAndImages} shouldDisableFilesAndImages={shouldDisableFilesAndImages} onHeightChange={() => { if (isAtBottom) { scrollToBottomAuto() } }} />
) } const ScrollToBottomButton = styled.div` background-color: color-mix(in srgb, var(--vscode-toolbar-hoverBackground) 55%, transparent); border-radius: 3px; overflow: hidden; cursor: pointer; display: flex; justify-content: center; align-items: center; flex: 1; height: 25px; &:hover { background-color: color-mix(in srgb, var(--vscode-toolbar-hoverBackground) 90%, transparent); } &:active { background-color: color-mix(in srgb, var(--vscode-toolbar-hoverBackground) 70%, transparent); } ` export default ChatView