diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index b477fcce9..cc1191d92 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -11,8 +11,6 @@ import type { export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { openUrl: [string, void]; - // We pass the `curSelectedModel` because we currently cannot access the - // default model title in the GUI from JB applyToFile: [ { text: string; diff --git a/gui/src/components/FileIcon.tsx b/gui/src/components/FileIcon.tsx index 24003595a..839f7ed7c 100644 --- a/gui/src/components/FileIcon.tsx +++ b/gui/src/components/FileIcon.tsx @@ -4,9 +4,9 @@ import { useMemo } from "react"; import { themeIcons } from "seti-file-icons"; export interface FileIconProps { - filename: string; height: `${number}px`; width: `${number}px`; + filename: string; } export default function FileIcon({ filename, height, width }: FileIconProps) { diff --git a/gui/src/components/markdown/StepContainerPreActionButtons.tsx b/gui/src/components/markdown/StepContainerPreActionButtons.tsx deleted file mode 100644 index 7e42dfb1c..000000000 --- a/gui/src/components/markdown/StepContainerPreActionButtons.tsx +++ /dev/null @@ -1,138 +0,0 @@ -import { - ArrowLeftEndOnRectangleIcon, - CommandLineIcon, - PlayIcon, -} from "@heroicons/react/24/outline"; -import { useContext, useRef, useState } from "react"; -import { v4 as uuidv4 } from "uuid"; -import { - defaultBorderRadius, - vscCommandCenterInactiveBorder, - vscEditorBackground, -} from ".."; -import { IdeMessengerContext } from "../../context/IdeMessenger"; -import { useWebviewListener } from "../../hooks/useWebviewListener"; -import { useAppSelector } from "../../redux/hooks"; -import { selectUIConfig } from "../../redux/slices/configSlice"; -import { isJetBrains } from "../../util"; -import { CopyIconButton } from "../gui/CopyIconButton"; -import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; -import { getTerminalCommand, isTerminalCodeBlock } from "./utils"; - -interface StepContainerPreActionButtonsProps { - language: string | null; - codeBlockContent: string; - codeBlockIndex: number; - children: any; -} - -export default function StepContainerPreActionButtons({ - language, - codeBlockContent, - codeBlockIndex, - children, -}: StepContainerPreActionButtonsProps) { - const [hovering, setHovering] = useState(false); - const ideMessenger = useContext(IdeMessengerContext); - const uiConfig = useAppSelector(selectUIConfig); - const streamIdRef = useRef(null); - const nextCodeBlockIndex = useAppSelector( - (state) => state.session.codeBlockApplyStates.curIndex, - ); - const isStreaming = useAppSelector((state) => state.session.isStreaming); - const isBottomToolbarPosition = - uiConfig?.codeBlockToolbarPosition == "bottom"; - - const toolTipPlacement = isBottomToolbarPosition ? "top" : "bottom"; - - const shouldRunTerminalCmd = - !isJetBrains() && isTerminalCodeBlock(language, codeBlockContent); - const isNextCodeBlock = nextCodeBlockIndex === codeBlockIndex; - - if (streamIdRef.current === null) { - streamIdRef.current = uuidv4(); - } - - function onClickApply() { - if (!streamIdRef.current) { - return; - } - - ideMessenger.post("applyToFile", { - streamId: streamIdRef.current, - text: codeBlockContent, - }); - } - - async function onClickRunTerminal(): Promise { - if (shouldRunTerminalCmd) { - return ideMessenger.ide.runCommand(getTerminalCommand(codeBlockContent)); - } - } - - // Handle apply keyboard shortcut - useWebviewListener( - "applyCodeFromChat", - async () => onClickApply(), - [isNextCodeBlock, codeBlockContent], - !isNextCodeBlock, - ); - - return ( -
setHovering(true)} - onMouseLeave={() => setHovering(false)} - className="bg-vsc-editor-background relative my-2.5" - style={{ - border: `1px solid ${vscCommandCenterInactiveBorder}`, - borderRadius: defaultBorderRadius, - }} - > -
{children}
- {hovering && !isStreaming && ( -
- {shouldRunTerminalCmd && ( - - - - )} - - - - - ideMessenger.post("insertAtCursor", { text: codeBlockContent }) - } - tooltipPlacement={toolTipPlacement} - > - - - -
- )} -
- ); -} diff --git a/gui/src/components/markdown/StepContainerPreToolbar/ApplyActions.tsx b/gui/src/components/markdown/StepContainerPreToolbar/ApplyActions.tsx index 541abd78e..4b35b4b2c 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/ApplyActions.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/ApplyActions.tsx @@ -37,12 +37,12 @@ export default function ApplyActions(props: ApplyActionsProps) { const applyButton = (text: string) => ( @@ -53,7 +53,7 @@ export default function ApplyActions(props: ApplyActionsProps) { return (
- Applying changes + Applying
diff --git a/gui/src/components/markdown/StepContainerPreToolbar/CopyButton.tsx b/gui/src/components/markdown/StepContainerPreToolbar/CopyButton.tsx index 195615380..2167afc41 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/CopyButton.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/CopyButton.tsx @@ -1,6 +1,6 @@ import { CheckIcon, ClipboardIcon } from "@heroicons/react/24/outline"; +import { lightGray } from "../.."; import useCopy from "../../../hooks/useCopy"; -import { lightGray, vscForeground } from "../.."; interface CopyButtonProps { text: string; @@ -11,23 +11,17 @@ export default function CopyButton({ text }: CopyButtonProps) { return (
{copied ? ( - <> - - Copied - + ) : ( - <> - - Copy - + )}
diff --git a/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx b/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx index ee46ccee8..7b204190b 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx @@ -1,41 +1,25 @@ -import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; import { getLastNPathParts } from "core/util/uri"; -import { useContext } from "react"; -import { IdeMessengerContext } from "../../../context/IdeMessenger"; import FileIcon from "../../FileIcon"; export interface FileInfoProps { - relativeFilepath: string; + filepath: string; + onClick: () => void; range?: string; } -const FileInfo = ({ relativeFilepath, range }: FileInfoProps) => { - const ideMessenger = useContext(IdeMessengerContext); - - async function onClickFileName() { - const fileUri = await inferResolvedUriFromRelativePath( - relativeFilepath, - ideMessenger.ide, - ); - ideMessenger.post("showFile", { - filepath: fileUri, - }); - } - +export const FileInfo = ({ filepath, range, onClick }: FileInfoProps) => { return (
- +
- {getLastNPathParts(relativeFilepath, 1)} + {getLastNPathParts(filepath, 1)} {range && ` ${range}`}
); }; - -export default FileInfo; diff --git a/gui/src/components/markdown/StepContainerPreToolbar/InsertButton.tsx b/gui/src/components/markdown/StepContainerPreToolbar/InsertButton.tsx new file mode 100644 index 000000000..7d56e012b --- /dev/null +++ b/gui/src/components/markdown/StepContainerPreToolbar/InsertButton.tsx @@ -0,0 +1,25 @@ +import { ArrowLeftEndOnRectangleIcon } from "@heroicons/react/24/outline"; +import { lightGray } from "../.."; + +/** + * Button that inserts code at the current cursor position + */ +interface InsertButtonProps { + onInsert: () => void; +} + +export default function InsertButton({ onInsert }: InsertButtonProps) { + return ( +
+
+ +
+
+ ); +} diff --git a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx index c00290e4c..0397603f3 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -1,7 +1,6 @@ import { ChevronDownIcon } from "@heroicons/react/24/outline"; import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; -import { debounce } from "lodash"; -import { useContext, useEffect, useRef, useState } from "react"; +import { useContext, useState } from "react"; import styled from "styled-components"; import { defaultBorderRadius, @@ -11,17 +10,17 @@ import { import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; import { useAppSelector } from "../../../redux/hooks"; -import { selectSelectedChatModel } from "../../../redux/slices/configSlice"; import { selectApplyStateByStreamId, selectIsInEditMode, } from "../../../redux/slices/sessionSlice"; import { getFontSize } from "../../../util"; -import { childrenToText, isTerminalCodeBlock } from "../utils"; +import { isTerminalCodeBlock } from "../utils"; import ApplyActions from "./ApplyActions"; import CopyButton from "./CopyButton"; -import FileInfo from "./FileInfo"; +import { FileInfo } from "./FileInfo"; import GeneratingCodeLoader from "./GeneratingCodeLoader"; +import InsertButton from "./InsertButton"; import RunInTerminalButton from "./RunInTerminalButton"; const TopDiv = styled.div` @@ -50,7 +49,7 @@ const ToolbarDiv = styled.div<{ isExpanded: boolean }>` export interface StepContainerPreToolbarProps { codeBlockContent: string; language: string | null; - relativeFilepath: string; + relativeFilepath?: string; isGeneratingCodeBlock: boolean; codeBlockIndex: number; // To track which codeblock we are applying codeBlockStreamId: string; @@ -60,47 +59,91 @@ export interface StepContainerPreToolbarProps { disableManualApply?: boolean; } -export default function StepContainerPreToolbar( - props: StepContainerPreToolbarProps, -) { +export default function StepContainerPreToolbar({ + codeBlockContent, + language, + relativeFilepath, + isGeneratingCodeBlock, + codeBlockIndex, + codeBlockStreamId, + range, + children, + expanded, + disableManualApply, +}: StepContainerPreToolbarProps) { const ideMessenger = useContext(IdeMessengerContext); - const wasGeneratingRef = useRef(props.isGeneratingCodeBlock); const isInEditMode = useAppSelector(selectIsInEditMode); const [isExpanded, setIsExpanded] = useState( - props.expanded ?? (isInEditMode ? false : true), + expanded ?? (isInEditMode ? false : true), ); - const [codeBlockContent, setCodeBlockContent] = useState(""); + // We store the resolved file URI to ensure we use the same file when + // accepting/rejecting diffs, even if no `relativeFilepath` is provided + const [resolvedFileUri, setResolvedFileUri] = useState( + undefined, + ); const nextCodeBlockIndex = useAppSelector( (state) => state.session.codeBlockApplyStates.curIndex, ); const applyState = useAppSelector((state) => - selectApplyStateByStreamId(state, props.codeBlockStreamId), + selectApplyStateByStreamId(state, codeBlockStreamId), ); - const isNextCodeBlock = nextCodeBlockIndex === props.codeBlockIndex; - const hasFileExtension = /\.[0-9a-z]+$/i.test(props.relativeFilepath); + const isNextCodeBlock = nextCodeBlockIndex === codeBlockIndex; + const hasFileExtension = + relativeFilepath && /\.[0-9a-z]+$/i.test(relativeFilepath); - const selectedChatModel = useAppSelector(selectSelectedChatModel); + const displayFilepath = relativeFilepath ?? resolvedFileUri; + + async function getFileUri() { + // If we've already resolved a file URI (from clicking apply), use that + if (resolvedFileUri) { + return resolvedFileUri; + } + + // If a relative filepath was provided, try to resolve it + if (relativeFilepath) { + return await inferResolvedUriFromRelativePath( + relativeFilepath, + ideMessenger.ide, + ); + } + + // If no filepath was provided, get the current file + const currentFile = await ideMessenger.ide.getCurrentFile(); + if (currentFile) { + return currentFile.path; + } + + return undefined; + } async function onClickApply() { - if (!selectedChatModel) { + const fileUri = await getFileUri(); + if (!fileUri) { + ideMessenger.ide.showToast( + "error", + "Could not resolve filepath to apply changes", + ); + return; } - let fileUri = await inferResolvedUriFromRelativePath( - props.relativeFilepath, - ideMessenger.ide, - ); + setResolvedFileUri(fileUri); ideMessenger.post("applyToFile", { - streamId: props.codeBlockStreamId, + streamId: codeBlockStreamId, filepath: fileUri, text: codeBlockContent, }); } + function onClickInsertAtCursor() { + ideMessenger.post("insertAtCursor", { text: codeBlockContent }); + } + + // TODO: This logic should be moved to a thunk // Handle apply keyboard shortcut useWebviewListener( "applyCodeFromChat", @@ -109,64 +152,42 @@ export default function StepContainerPreToolbar( !isNextCodeBlock, ); - useEffect(() => { - if (codeBlockContent === "") { - setCodeBlockContent(childrenToText(props.children.props.children)); - } else { - const debouncedEffect = debounce(() => { - setCodeBlockContent(childrenToText(props.children.props.children)); - }, 100); - - debouncedEffect(); - - return () => { - debouncedEffect.cancel(); - }; + async function handleDiffAction(action: "accept" | "reject") { + const filepath = await getFileUri(); + if (!filepath) { + ideMessenger.ide.showToast( + "error", + `Could not resolve filepath to ${action} changes`, + ); + return; } - }, [props.children, codeBlockContent]); - // useEffect(() => { - // const hasCompletedGenerating = - // wasGeneratingRef.current && !isGeneratingCodeBlock; - - // wasGeneratingRef.current = isGeneratingCodeBlock; - // if (hasCompletedGenerating) { - // if (props.autoApply) { - // onClickApply(); - // } - // } - // }, [wasGeneratingRef, isGeneratingCodeBlock, props.autoApply]); - - async function onClickAcceptApply() { - const fileUri = await inferResolvedUriFromRelativePath( - props.relativeFilepath, - ideMessenger.ide, - ); - ideMessenger.post("acceptDiff", { - filepath: fileUri, - streamId: props.codeBlockStreamId, + ideMessenger.post(`${action}Diff`, { + filepath, + streamId: codeBlockStreamId, }); + + setResolvedFileUri(undefined); } - async function onClickRejectApply() { - const fileUri = await inferResolvedUriFromRelativePath( - props.relativeFilepath, - ideMessenger.ide, - ); - ideMessenger.post("rejectDiff", { - filepath: fileUri, - streamId: props.codeBlockStreamId, - }); - } + function onClickFilename() { + if (resolvedFileUri) { + ideMessenger.post("showFile", { + filepath: resolvedFileUri, + }); + } - function onClickExpand() { - setIsExpanded(!isExpanded); + if (relativeFilepath) { + ideMessenger.post("showFile", { + filepath: relativeFilepath, + }); + } } // We want until there is an extension in the filepath to avoid rendering - // an incomplete filepath - if (!hasFileExtension) { - return props.children; + // an incomplete filepath + if (relativeFilepath && !hasFileExtension) { + return children; } return ( @@ -174,35 +195,42 @@ export default function StepContainerPreToolbar(
setIsExpanded(!isExpanded)} className={`h-3.5 w-3.5 flex-shrink-0 cursor-pointer text-gray-400 hover:brightness-125 ${ isExpanded ? "rotate-0" : "-rotate-90" }`} /> - + {displayFilepath ? ( + + ) : ( + {language} + )}
-
- {props.isGeneratingCodeBlock ? ( +
+ {isGeneratingCodeBlock ? ( ) : ( <> - - {isTerminalCodeBlock(props.language, props.codeBlockContent) ? ( - + + + + {isTerminalCodeBlock(language, codeBlockContent) ? ( + ) : ( handleDiffAction("accept")} + onClickReject={() => handleDiffAction("reject")} /> )} @@ -211,13 +239,7 @@ export default function StepContainerPreToolbar( {isExpanded && ( -
- {props.children} -
+
{children}
)} ); diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 6af2f68d5..15951d5d1 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -22,7 +22,6 @@ import { ToolTip } from "../gui/Tooltip"; import FilenameLink from "./FilenameLink"; import "./katex.css"; import "./markdown.css"; -import StepContainerPreActionButtons from "./StepContainerPreActionButtons"; import StepContainerPreToolbar from "./StepContainerPreToolbar"; import SymbolLink from "./SymbolLink"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; @@ -301,6 +300,8 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const preChildProps = preProps?.children?.[0]?.props ?? {}; const { className, range } = preChildProps; + + // TODO: what is this when we odnt have one? const relativeFilePath = preChildProps["data-relativefilepath"]; const codeBlockContent = preChildProps["data-codeblockcontent"]; @@ -310,22 +311,10 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const language = getLanguageFromClassName(className); - if (!relativeFilePath) { - return ( - - - - ); - } - const isGeneratingCodeBlock = preChildProps["data-islastcodeblock"] && isLastItemRef.current && - !isStreamingRef.current; + isStreamingRef.current; if (codeblockState.current[codeBlockIndex] === undefined) { codeblockState.current[codeBlockIndex] = { @@ -337,7 +326,6 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( isGeneratingCodeBlock; } - // We use a custom toolbar for codeblocks in the step container return ( ) : null; } diff --git a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx index 80927e6e7..f1592a7fe 100644 --- a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx +++ b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx @@ -29,9 +29,9 @@ export function EditFile(props: EditToolCallProps) { return ( <> diff --git a/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx b/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx index 35fc0e54f..4bb104cce 100644 --- a/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx +++ b/gui/src/pages/gui/ToolCallDiv/RunTerminalCommand.tsx @@ -9,7 +9,7 @@ interface RunTerminalCommandToolCallProps { export function RunTerminalCommand(props: RunTerminalCommandToolCallProps) { return ( ); diff --git a/manual-testing-sandbox/test.js b/manual-testing-sandbox/test.js index eee0aa7c5..d2920d6af 100644 --- a/manual-testing-sandbox/test.js +++ b/manual-testing-sandbox/test.js @@ -7,7 +7,6 @@ class Calculator { this.result += number; return this; } - subtract(number) { return this; }