When API requests fail and auto-retry is enabled, multiple error_retry messages were shown (e.g., "Attempt 1 of 3", "Attempt 2 of 3", etc.). This change consolidates them to only show the latest retry message, reducing visual clutter during retry sequences.
406 lines
13 KiB
TypeScript
406 lines
13 KiB
TypeScript
import { findLast } from "@shared/array"
|
|
import { combineApiRequests } from "@shared/combineApiRequests"
|
|
import { combineCommandSequences } from "@shared/combineCommandSequences"
|
|
import { combineErrorRetryMessages } from "@shared/combineErrorRetryMessages"
|
|
import { combineHookSequences } from "@shared/combineHookSequences"
|
|
import type { ClineApiReqInfo, ClineMessage } from "@shared/ExtensionMessage"
|
|
import { getApiMetrics } from "@shared/getApiMetrics"
|
|
import { BooleanRequest, StringRequest } from "@shared/proto/cline/common"
|
|
import { useCallback, useEffect, useMemo } from "react"
|
|
import { useMount } from "react-use"
|
|
import { normalizeApiConfiguration } from "@/components/settings/utils/providerUtils"
|
|
import { useExtensionState } from "@/context/ExtensionStateContext"
|
|
import { useShowNavbar } from "@/context/PlatformContext"
|
|
import { FileServiceClient, UiServiceClient } from "@/services/grpc-client"
|
|
import { Navbar } from "../menu/Navbar"
|
|
import AutoApproveBar from "./auto-approve-menu/AutoApproveBar"
|
|
// Import utilities and hooks from the new structure
|
|
import {
|
|
ActionButtons,
|
|
CHAT_CONSTANTS,
|
|
ChatLayout,
|
|
convertHtmlToMarkdown,
|
|
filterVisibleMessages,
|
|
groupMessages,
|
|
InputSection,
|
|
MessagesArea,
|
|
TaskSection,
|
|
useChatState,
|
|
useMessageHandlers,
|
|
useScrollBehavior,
|
|
WelcomeSection,
|
|
} from "./chat-view"
|
|
|
|
interface ChatViewProps {
|
|
isHidden: boolean
|
|
showAnnouncement: boolean
|
|
hideAnnouncement: () => void
|
|
showHistoryView: () => void
|
|
}
|
|
|
|
// Use constants from the imported module
|
|
const MAX_IMAGES_AND_FILES_PER_MESSAGE = CHAT_CONSTANTS.MAX_IMAGES_AND_FILES_PER_MESSAGE
|
|
const QUICK_WINS_HISTORY_THRESHOLD = 3
|
|
|
|
const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryView }: ChatViewProps) => {
|
|
const showNavbar = useShowNavbar()
|
|
const {
|
|
version,
|
|
clineMessages: messages,
|
|
taskHistory,
|
|
apiConfiguration,
|
|
telemetrySetting,
|
|
mode,
|
|
userInfo,
|
|
currentFocusChainChecklist,
|
|
hooksEnabled,
|
|
} = useExtensionState()
|
|
const isProdHostedApp = userInfo?.apiBaseUrl === "https://app.cline.bot"
|
|
const shouldShowQuickWins = isProdHostedApp && (!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(() => {
|
|
const slicedMessages = messages.slice(1)
|
|
// Only combine hook sequences if hooks are enabled (both user setting and feature flag)
|
|
const areHooksEnabled = hooksEnabled?.user
|
|
const withHooks = areHooksEnabled ? combineHookSequences(slicedMessages) : slicedMessages
|
|
return combineErrorRetryMessages(combineApiRequests(combineCommandSequences(withHooks)))
|
|
}, [messages, hooksEnabled])
|
|
// 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])
|
|
|
|
// Use custom hooks for state management
|
|
const chatState = useChatState(messages)
|
|
const {
|
|
setInputValue,
|
|
selectedImages,
|
|
setSelectedImages,
|
|
selectedFiles,
|
|
setSelectedFiles,
|
|
sendingDisabled,
|
|
enableButtons,
|
|
expandedRows,
|
|
setExpandedRows,
|
|
textAreaRef,
|
|
} = chatState
|
|
|
|
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(StringRequest.create({ 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)
|
|
}
|
|
}, [])
|
|
// Button state is now managed by useButtonState hook
|
|
|
|
useEffect(() => {
|
|
setExpandedRows({})
|
|
}, [task?.ts])
|
|
|
|
// handleFocusChange is already provided by chatState
|
|
|
|
// Use message handlers hook
|
|
const messageHandlers = useMessageHandlers(messages, chatState)
|
|
|
|
const { selectedModelInfo } = useMemo(() => {
|
|
return normalizeApiConfiguration(apiConfiguration, mode)
|
|
}, [apiConfiguration, mode])
|
|
|
|
const selectFilesAndImages = useCallback(async () => {
|
|
try {
|
|
const response = await FileServiceClient.selectFiles(
|
|
BooleanRequest.create({
|
|
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
|
|
|
|
// Listen for local focusChatInput event
|
|
useEffect(() => {
|
|
const handleFocusChatInput = () => {
|
|
// Only focus chat input box if user is currently viewing the chat (not hidden).
|
|
if (!isHidden) {
|
|
textAreaRef.current?.focus()
|
|
}
|
|
}
|
|
|
|
window.addEventListener("focusChatInput", handleFocusChatInput)
|
|
|
|
return () => {
|
|
window.removeEventListener("focusChatInput", handleFocusChatInput)
|
|
}
|
|
}, [isHidden])
|
|
|
|
// 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 filterVisibleMessages(modifiedMessages)
|
|
}, [modifiedMessages])
|
|
|
|
const lastProgressMessageText = useMemo(() => {
|
|
// First check if we have a current focus chain list from the extension state
|
|
if (currentFocusChainChecklist) {
|
|
return currentFocusChainChecklist
|
|
}
|
|
|
|
// Fall back to the last task_progress message if no state focus chain list
|
|
const lastProgressMessage = [...modifiedMessages].reverse().find((message) => message.say === "task_progress")
|
|
return lastProgressMessage?.text
|
|
}, [modifiedMessages, currentFocusChainChecklist])
|
|
|
|
const groupedMessages = useMemo(() => {
|
|
return groupMessages(visibleMessages)
|
|
}, [visibleMessages])
|
|
|
|
// Use scroll behavior hook
|
|
const scrollBehavior = useScrollBehavior(messages, visibleMessages, groupedMessages, expandedRows, setExpandedRows)
|
|
|
|
const placeholderText = useMemo(() => {
|
|
const text = task ? "Type a message..." : "Type your task here..."
|
|
return text
|
|
}, [task])
|
|
|
|
return (
|
|
<ChatLayout isHidden={isHidden}>
|
|
<div className="flex flex-col flex-1 overflow-hidden">
|
|
{showNavbar && <Navbar />}
|
|
{task ? (
|
|
<TaskSection
|
|
apiMetrics={apiMetrics}
|
|
lastApiReqTotalTokens={lastApiReqTotalTokens}
|
|
lastProgressMessageText={lastProgressMessageText}
|
|
messageHandlers={messageHandlers}
|
|
selectedModelInfo={{
|
|
supportsPromptCache: selectedModelInfo.supportsPromptCache,
|
|
supportsImages: selectedModelInfo.supportsImages || false,
|
|
}}
|
|
task={task}
|
|
/>
|
|
) : (
|
|
<WelcomeSection
|
|
hideAnnouncement={hideAnnouncement}
|
|
shouldShowQuickWins={shouldShowQuickWins}
|
|
showAnnouncement={showAnnouncement}
|
|
showHistoryView={showHistoryView}
|
|
taskHistory={taskHistory}
|
|
telemetrySetting={telemetrySetting}
|
|
version={version}
|
|
/>
|
|
)}
|
|
{task && (
|
|
<MessagesArea
|
|
chatState={chatState}
|
|
groupedMessages={groupedMessages}
|
|
messageHandlers={messageHandlers}
|
|
modifiedMessages={modifiedMessages}
|
|
scrollBehavior={scrollBehavior}
|
|
task={task}
|
|
/>
|
|
)}
|
|
</div>
|
|
<footer className="bg-(--vscode-sidebar-background)" style={{ gridRow: "2" }}>
|
|
<AutoApproveBar />
|
|
<ActionButtons
|
|
chatState={chatState}
|
|
messageHandlers={messageHandlers}
|
|
messages={messages}
|
|
mode={mode}
|
|
scrollBehavior={{
|
|
scrollToBottomSmooth: scrollBehavior.scrollToBottomSmooth,
|
|
disableAutoScrollRef: scrollBehavior.disableAutoScrollRef,
|
|
showScrollToBottom: scrollBehavior.showScrollToBottom,
|
|
virtuosoRef: scrollBehavior.virtuosoRef,
|
|
}}
|
|
task={task}
|
|
/>
|
|
<InputSection
|
|
chatState={chatState}
|
|
messageHandlers={messageHandlers}
|
|
placeholderText={placeholderText}
|
|
scrollBehavior={scrollBehavior}
|
|
selectFilesAndImages={selectFilesAndImages}
|
|
shouldDisableFilesAndImages={shouldDisableFilesAndImages}
|
|
/>
|
|
</footer>
|
|
</ChatLayout>
|
|
)
|
|
}
|
|
|
|
export default ChatView
|