diff --git a/core/index.d.ts b/core/index.d.ts index b5cac75ae..c5ac2d62f 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1216,6 +1216,7 @@ export type EditStatus = | "done"; export type ApplyStateStatus = + | "not-started" // Apply state created but not necessarily streaming | "streaming" // Changes are being applied to the file | "done" // All changes have been applied, awaiting user to accept/reject | "closed"; // All changes have been applied. Note that for new files, we immediately set the status to "closed" diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx index 0c1c8c7d0..17bbc5802 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx @@ -48,6 +48,14 @@ export function ApplyActions(props: ApplyActionsProps) { ); switch (props.applyState ? props.applyState.status : null) { + case "not-started": + return ( +
+ + Pending + +
+ ); case "streaming": return (
@@ -84,8 +92,8 @@ export function ApplyActions(props: ApplyActionsProps) {
); case "closed": - if (!hasRejected && props.applyState?.numDiffs === 0) { - if (showApplied) { + if (isSuccessful) { + if (showApplied || props.disableManualApply) { return ( Applied @@ -93,9 +101,6 @@ export function ApplyActions(props: ApplyActionsProps) { ); } - if (props.disableManualApply) { - return null; - } return applyButton("Reapply"); } default: diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx index b730b30e5..28e13e5b3 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/GeneratingCodeLoader.tsx @@ -3,22 +3,24 @@ import Spinner from "../../gui/Spinner"; export interface GeneratingCodeLoaderProps { showLineCount: boolean; codeBlockContent: string; + isPending: boolean; } export function GeneratingCodeLoader({ showLineCount, codeBlockContent, + isPending, }: GeneratingCodeLoaderProps) { const numLinesCodeBlock = codeBlockContent.split("\n").length; const linesGeneratedText = numLinesCodeBlock === 1 ? `1 line generated` - : `${numLinesCodeBlock} lines generated`; + : `${numLinesCodeBlock} lines ${isPending ? "pending" : "generated"}`; return ( {showLineCount ? linesGeneratedText : "Generating"} - + {!isPending && } ); } diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx index 202d87972..c56ba099c 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx @@ -52,7 +52,7 @@ export interface StepContainerPreToolbarProps { codeBlockContent: string; language: string | null; relativeFilepath?: string; - isGeneratingCodeBlock: boolean; + isFinalCodeblock: boolean; codeBlockIndex: number; // To track which codeblock we are applying codeBlockStreamId: string; range?: string; @@ -65,7 +65,7 @@ export function StepContainerPreToolbar({ codeBlockContent, language, relativeFilepath, - isGeneratingCodeBlock, + isFinalCodeblock, codeBlockIndex, codeBlockStreamId, range, @@ -115,6 +115,9 @@ export function StepContainerPreToolbar({ const hasFileExtension = relativeFilepath && /\.[0-9a-z]+$/i.test(relativeFilepath); + const isStreaming = useAppSelector((store) => store.session.isStreaming); + const isGeneratingCodeBlock = isFinalCodeblock && isStreaming; + // If we are creating a file, we already render that in the button // so we don't want to dispaly it twice here const displayFilepath = relativeFilepath ?? appliedFileUri; @@ -272,20 +275,23 @@ export function StepContainerPreToolbar({
- {isGeneratingCodeBlock ? ( + {!isGeneratingCodeBlock && ( +
+ + +
+ )} + + {isGeneratingCodeBlock || applyState?.status === "not-started" ? ( ) : ( - <> -
- - -
- - {renderActionButtons()} - + renderActionButtons() )}
diff --git a/gui/src/components/StyledMarkdownPreview/index.tsx b/gui/src/components/StyledMarkdownPreview/index.tsx index 1cab75874..36700cf46 100644 --- a/gui/src/components/StyledMarkdownPreview/index.tsx +++ b/gui/src/components/StyledMarkdownPreview/index.tsx @@ -126,7 +126,7 @@ interface StyledMarkdownPreviewProps { itemIndex?: number; useParentBackgroundColor?: boolean; disableManualApply?: boolean; - singleCodeblockStreamId?: string; + forceStreamId?: string; expandCodeblocks?: boolean; } @@ -195,27 +195,16 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const pastFileInfoRef = useUpdatingRef(pastFileInfo); const isLastItem = useMemo(() => { - return props.itemIndex && props.itemIndex === history.length - 1; + return props.itemIndex === history.length - 1; }, [history.length, props.itemIndex]); const isLastItemRef = useUpdatingRef(isLastItem); - const isStreaming = useAppSelector((store) => store.session.isStreaming); - const isStreamingRef = useUpdatingRef(isStreaming); - - const codeblockState = useRef<{ streamId: string; isGenerating: boolean }[]>( - [], - ); - + const codeblockStreamIds = useRef([]); useEffect(() => { - if (props.singleCodeblockStreamId) { - codeblockState.current = [ - { - streamId: props.singleCodeblockStreamId, - isGenerating: false, - }, - ]; + if (props.forceStreamId) { + codeblockStreamIds.current = [props.forceStreamId]; } - }, [props.singleCodeblockStreamId]); + }, [props.forceStreamId, codeblockStreamIds]); const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ @@ -310,19 +299,12 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const language = getLanguageFromClassName(className); - const isGeneratingCodeBlock = - preChildProps["data-islastcodeblock"] && - isLastItemRef.current && - isStreamingRef.current; + const isFinalCodeblock = + preChildProps["data-islastcodeblock"] && isLastItemRef.current; - if (codeblockState.current[codeBlockIndex] === undefined) { - codeblockState.current[codeBlockIndex] = { - streamId: uuidv4(), - isGenerating: isGeneratingCodeBlock, - }; - } else { - codeblockState.current[codeBlockIndex].isGenerating = - isGeneratingCodeBlock; + if (codeblockStreamIds.current[codeBlockIndex] === undefined) { + codeblockStreamIds.current[codeBlockIndex] = + props.forceStreamId ?? uuidv4(); } return ( @@ -331,11 +313,9 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( codeBlockIndex={codeBlockIndex} language={language} relativeFilepath={relativeFilePath} - isGeneratingCodeBlock={isGeneratingCodeBlock} + isFinalCodeblock={isFinalCodeblock} range={range} - codeBlockStreamId={ - codeblockState.current[codeBlockIndex].streamId - } + codeBlockStreamId={codeblockStreamIds.current[codeBlockIndex]} expanded={props.expandCodeblocks} disableManualApply={props.disableManualApply} > @@ -381,7 +361,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const codeWrapState = uiConfig?.codeWrap ? "pre-wrap" : "pre"; return ( store.session.activeToolStreamId, + const currentToolCallApplyState = useAppSelector( + selectCurrentToolCallApplyState, ); const isEnterDisabled = props.disabled || isEditModeAndNoCodeToEdit || toolCallState?.status === "generated" || - !!activeToolCallStreamId; + (currentToolCallApplyState && + currentToolCallApplyState.status !== "closed"); const toolsSupported = defaultModel && modelSupportsTools(defaultModel); diff --git a/gui/src/components/mainInput/Lump/LumpToolbar.tsx b/gui/src/components/mainInput/Lump/LumpToolbar.tsx index 45064a814..f8953cbf7 100644 --- a/gui/src/components/mainInput/Lump/LumpToolbar.tsx +++ b/gui/src/components/mainInput/Lump/LumpToolbar.tsx @@ -109,7 +109,9 @@ export function LumpToolbar() { if (toolCallState?.status === "generated") { return ( - +
+ Pending tool call +
store.session.activeToolStreamId, + const currentToolCallApplyState = useAppSelector( + selectCurrentToolCallApplyState, ); useWebviewListener( "updateApplyState", async (state) => { dispatch(updateApplyState(state)); - const lastHistoryMsg = history.at(-1); - const [streamId, toolCallId] = activeToolStreamId ?? []; + + // Handle apply status updates that are associated with current tool call if ( - toolCallId && state.status === "closed" && - lastHistoryMsg?.toolCallState?.toolCallId === toolCallId + currentToolCallApplyState && + currentToolCallApplyState.streamId === state.streamId ) { - if (state.streamId === streamId) { - // const output: ContextItem = { - // name: "Edit tool output", - // content: "Completed edit", - // description: "", - // }; - dispatch( - acceptToolCall({ - toolCallId, - }), - ); - // dispatch(setToolCallOutput([])); - dispatch( - streamResponseAfterToolCall({ - toolCallId, - }), - ); - } + // const output: ContextItem = { + // name: "Edit tool output", + // content: "Completed edit", + // description: "", + // }; + dispatch( + acceptToolCall({ + toolCallId: currentToolCallApplyState.toolCallId!, + }), + ); + // dispatch(setToolCallOutput([])); + dispatch( + streamResponseAfterToolCall({ + toolCallId: currentToolCallApplyState.toolCallId!, + }), + ); } }, - [activeToolStreamId, history], + [currentToolCallApplyState, history], ); } diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index f8f3eee18..162cc0752 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -30,7 +30,10 @@ import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { selectUseHub } from "../../redux/selectors"; -import { selectCurrentToolCall } from "../../redux/selectors/selectCurrentToolCall"; +import { + selectCurrentToolCall, + selectCurrentToolCallApplyState, +} from "../../redux/selectors/selectCurrentToolCall"; import { selectSelectedChatModel } from "../../redux/slices/configSlice"; import { submitEdit } from "../../redux/slices/editModeState"; import { @@ -152,6 +155,10 @@ export function Chat() { const { widget, highlights } = useFindWidget(stepsDivRef); + const currentToolCallApplyState = useAppSelector( + selectCurrentToolCallApplyState, + ); + const sendInput = useCallback( ( editorState: JSONContent, @@ -164,6 +171,14 @@ export function Chat() { "Cannot submit message while awaiting tool confirmation", ); } + if ( + currentToolCallApplyState && + currentToolCallApplyState.status !== "closed" + ) { + return console.error( + "Cannot submit message while awaiting tool call apply", + ); + } if (selectedChatModel?.provider === "free-trial") { const newCount = incrementFreeTrialCount(); @@ -352,6 +367,7 @@ export function Chat() { toolCallState={item.toolCallState!} toolCall={toolCall} output={history[index + 1]?.contextItems} + historyIndex={index} />
); diff --git a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx index 7e50b7305..a3e81355b 100644 --- a/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx +++ b/gui/src/pages/gui/ToolCallDiv/CreateFile.tsx @@ -4,6 +4,7 @@ import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview"; interface CreateFileToolCallProps { relativeFilepath: string; fileContents: string; + historyIndex: number; } export function CreateFile(props: CreateFileToolCallProps) { @@ -14,6 +15,7 @@ export function CreateFile(props: CreateFileToolCallProps) { isRenderingInStepContainer disableManualApply source={src} + itemIndex={props.historyIndex} /> ) : null; } diff --git a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx index a298325b5..35b9d1a5f 100644 --- a/gui/src/pages/gui/ToolCallDiv/EditFile.tsx +++ b/gui/src/pages/gui/ToolCallDiv/EditFile.tsx @@ -1,26 +1,40 @@ import { getMarkdownLanguageTagForFile } from "core/util"; -import { useMemo } from "react"; +import { useEffect } from "react"; +import { v4 as uuidv4 } from "uuid"; import AcceptRejectAllButtons from "../../../components/AcceptRejectAllButtons"; import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview"; -import { useAppSelector } from "../../../redux/hooks"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; +import { + selectApplyStateByToolCallId, + updateApplyState, +} from "../../../redux/slices/sessionSlice"; type EditToolCallProps = { relativeFilePath: string; changes: string; toolCallId?: string; + historyIndex: number; }; export function EditFile(props: EditToolCallProps) { const src = `\`\`\`${getMarkdownLanguageTagForFile(props.relativeFilePath ?? "test.txt")} ${props.relativeFilePath}\n${props.changes ?? ""}\n\`\`\``; + const dispatch = useAppDispatch(); const isStreaming = useAppSelector((state) => state.session.isStreaming); - const states = useAppSelector((store) => store.session.codeBlockApplyStates); - - const applyState = useMemo(() => { - return states.states.find( - (state) => state.toolCallId && state.toolCallId === props.toolCallId, - ); - }, [states, props.toolCallId]); + const applyState = useAppSelector((state) => + selectApplyStateByToolCallId(state, props.toolCallId), + ); + useEffect(() => { + if (!applyState) { + dispatch( + updateApplyState({ + streamId: uuidv4(), + toolCallId: props.toolCallId, + status: "not-started", + }), + ); + } + }, [applyState, props.toolCallId]); if (!props.relativeFilePath) { return null; @@ -32,8 +46,9 @@ export function EditFile(props: EditToolCallProps) { isRenderingInStepContainer disableManualApply source={src} - singleCodeblockStreamId={applyState?.streamId} + forceStreamId={applyState?.streamId} expandCodeblocks={false} + itemIndex={props.historyIndex} /> {/* TODO better indicator of generation at bottom */} {/* {isStreaming && applyState?.status === "streaming" && ( diff --git a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx index 02759dd18..516282d74 100644 --- a/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx @@ -7,9 +7,11 @@ import { RunTerminalCommand } from "./RunTerminalCommand"; function FunctionSpecificToolCallDiv({ toolCall, toolCallState, + historyIndex, }: { toolCall: ToolCallDelta; toolCallState: ToolCallState; + historyIndex: number; }) { const args = toolCallState.parsedArgs; @@ -19,6 +21,7 @@ function FunctionSpecificToolCallDiv({ ); case BuiltInToolNames.EditExistingFile: @@ -27,6 +30,7 @@ function FunctionSpecificToolCallDiv({ relativeFilePath={args.filepath} changes={args.changes} toolCallId={toolCall.id} + historyIndex={historyIndex} /> ); case BuiltInToolNames.RunTerminalCommand: diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index 19bb13c02..5fd016d45 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -31,6 +31,7 @@ interface ToolCallDivProps { toolCall: ToolCallDelta; toolCallState: ToolCallState; output?: ContextItemWithId[]; + historyIndex: number; } const toolCallIcons: Record = { @@ -100,6 +101,7 @@ export function ToolCallDiv(props: ToolCallDivProps) { ); diff --git a/gui/src/redux/selectors/selectCurrentToolCall.ts b/gui/src/redux/selectors/selectCurrentToolCall.ts index eb0bf669b..56c3a6cc1 100644 --- a/gui/src/redux/selectors/selectCurrentToolCall.ts +++ b/gui/src/redux/selectors/selectCurrentToolCall.ts @@ -8,3 +8,20 @@ export const selectCurrentToolCall = createSelector( return findCurrentToolCall(history); }, ); + +export const selectCurrentToolCallApplyState = createSelector( + [ + (store: RootState) => store.session.history, + (store: RootState) => store.session.codeBlockApplyStates, + ], + (history, applyStates) => { + const currentToolCall = findCurrentToolCall(history); + if (!currentToolCall) { + return undefined; + } + return applyStates.states.find( + (state) => + state.toolCallId && state.toolCallId === currentToolCall.toolCallId, + ); + }, +); diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 6adbabcf7..5f74e0c50 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -21,7 +21,6 @@ import { ToolCallDelta, ToolCallState, } from "core"; -import { BuiltInToolNames } from "core/tools/builtIn"; import { NEW_SESSION_TITLE } from "core/util/constants"; import { incrementalParseJson } from "core/util/incrementalParseJson"; import { renderChatMessage } from "core/util/messageContent"; @@ -54,7 +53,6 @@ type SessionState = { states: ApplyState[]; curIndex: number; }; - activeToolStreamId?: [string, string]; newestToolbarPreviewForInput: Record; }; @@ -357,7 +355,7 @@ export const sessionSlice = createSlice({ if ( !( toolCallDelta.id && - toolCallDelta.function?.arguments && + toolCallDelta.function?.arguments !== undefined && toolCallDelta.function?.name && toolCallDelta.type ) @@ -368,19 +366,6 @@ export const sessionSlice = createSlice({ ); } - if ( - toolCallDelta.id && - toolCallDelta.function?.name === - BuiltInToolNames.EditExistingFile - ) { - const streamId = uuidv4(); - state.codeBlockApplyStates.states.push({ - streamId, - toolCallId: toolCallDelta.id, - status: "streaming", - }); - state.activeToolStreamId = [streamId, toolCallDelta.id]; - } historyItem.toolCallState = toolCallDeltaToState(toolCallDelta); } state.history.push(historyItem); @@ -660,9 +645,6 @@ export const sessionSlice = createSlice({ toolCallId: string; }>, ) => { - if (action.payload.toolCallId === state.activeToolStreamId?.[1]) { - state.activeToolStreamId = undefined; - } const toolCallState = findToolCall( state.history, action.payload.toolCallId, @@ -677,9 +659,6 @@ export const sessionSlice = createSlice({ toolCallId: string; }>, ) => { - if (action.payload.toolCallId === state.activeToolStreamId?.[1]) { - state.activeToolStreamId = undefined; - } const toolCallState = findToolCall( state.history, action.payload.toolCallId, @@ -694,9 +673,6 @@ export const sessionSlice = createSlice({ toolCallId: string; }>, ) => { - if (action.payload.toolCallId === state.activeToolStreamId?.[1]) { - state.activeToolStreamId = undefined; - } const toolCallState = findToolCall( state.history, action.payload.toolCallId, @@ -803,6 +779,16 @@ export const selectApplyStateByStreamId = createSelector( }, ); +export const selectApplyStateByToolCallId = createSelector( + [ + (state: RootState) => state.session.codeBlockApplyStates.states, + (state: RootState, toolCallId?: string) => toolCallId, + ], + (states, toolCallId) => { + return states.find((state) => state.toolCallId === toolCallId); + }, +); + export const { updateFileSymbols, setContextItemsAtIndex, diff --git a/gui/src/redux/thunks/callCurrentTool.ts b/gui/src/redux/thunks/callCurrentTool.ts index 61795de96..f6f408238 100644 --- a/gui/src/redux/thunks/callCurrentTool.ts +++ b/gui/src/redux/thunks/callCurrentTool.ts @@ -63,7 +63,11 @@ export const callCurrentTool = createAsyncThunk( } = await callClientTool(toolCallState.toolCall, { dispatch, ideMessenger: extra.ideMessenger, - activeToolStreamId: state.session.activeToolStreamId?.[0], + streamId: state.session.codeBlockApplyStates.states.find( + (state) => + state.toolCallId && state.toolCallId === toolCallState.toolCallId, + )?.streamId, + getState, }); output = clientToolOuput; errorMessage = clientToolError; diff --git a/gui/src/util/clientTools/callClientTool.ts b/gui/src/util/clientTools/callClientTool.ts index 6d451fe7e..65f11fbf6 100644 --- a/gui/src/util/clientTools/callClientTool.ts +++ b/gui/src/util/clientTools/callClientTool.ts @@ -1,13 +1,14 @@ import { ContextItem, ToolCall } from "core"; import { BuiltInToolNames } from "core/tools/builtIn"; import { IIdeMessenger } from "../../context/IdeMessenger"; -import { AppThunkDispatch } from "../../redux/store"; +import { AppThunkDispatch, RootState } from "../../redux/store"; import { editToolImpl } from "./editImpl"; export interface ClientToolExtras { + getState: () => RootState; dispatch: AppThunkDispatch; ideMessenger: IIdeMessenger; - activeToolStreamId?: string; + streamId?: string; } export interface ClientToolOutput { diff --git a/gui/src/util/clientTools/editImpl.ts b/gui/src/util/clientTools/editImpl.ts index 3e5271228..d1f80f25a 100644 --- a/gui/src/util/clientTools/editImpl.ts +++ b/gui/src/util/clientTools/editImpl.ts @@ -1,5 +1,4 @@ import { resolveRelativePathInDir } from "core/util/ideUtils"; -import { updateApplyState } from "../../redux/slices/sessionSlice"; import { ClientToolImpl } from "./callClientTool"; export const editToolImpl: ClientToolImpl = async ( @@ -7,7 +6,7 @@ export const editToolImpl: ClientToolImpl = async ( toolCallId, extras, ) => { - if (!extras.activeToolStreamId) { + if (!extras.streamId) { throw new Error("Invalid apply state"); } const firstUriMatch = await resolveRelativePathInDir( @@ -18,7 +17,7 @@ export const editToolImpl: ClientToolImpl = async ( throw new Error(`${args.filepath} does not exist`); } const apply = await extras.ideMessenger.request("applyToFile", { - streamId: extras.activeToolStreamId, + streamId: extras.streamId, text: args.changes, toolCallId, filepath: firstUriMatch, @@ -26,17 +25,7 @@ export const editToolImpl: ClientToolImpl = async ( if (apply.status === "error") { throw new Error(apply.error); } - if (extras.activeToolStreamId) { - extras.dispatch( - updateApplyState({ - streamId: extras.activeToolStreamId, - status: "closed", - toolCallId, - numDiffs: 0, - filepath: args.filepath, - }), - ); - } + return { respondImmediately: false, output: undefined, // No immediate output.