From 20503cd256ca8a19a1139e3de988c5cb62ff6181 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 1 Jul 2025 12:34:44 -0700 Subject: [PATCH 1/2] tool truncate history icon --- gui/src/components/mainInput/icons.ts | 2 + gui/src/pages/gui/Chat.tsx | 3 +- .../gui/ToolCallDiv/SimpleToolCallUI.tsx | 48 +++++++++++-------- gui/src/pages/gui/ToolCallDiv/ToolCall.tsx | 22 ++++++--- .../ToolCallDiv/ToolTruncateHistoryIcon.tsx | 40 ++++++++++++++++ gui/src/pages/gui/ToolCallDiv/index.tsx | 11 ++--- .../ToolCallDiv/toolCallStateToContextItem.ts | 27 +++++++++++ gui/src/redux/slices/sessionSlice.ts | 23 +++++++++ 8 files changed, 138 insertions(+), 38 deletions(-) create mode 100644 gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx create mode 100644 gui/src/pages/gui/ToolCallDiv/toolCallStateToContextItem.ts diff --git a/gui/src/components/mainInput/icons.ts b/gui/src/components/mainInput/icons.ts index d8d04cc83..62d69e65b 100644 --- a/gui/src/components/mainInput/icons.ts +++ b/gui/src/components/mainInput/icons.ts @@ -18,6 +18,7 @@ import { PlusIcon, SparklesIcon, TrashIcon, + WrenchScrewdriverIcon, } from "@heroicons/react/24/outline"; import { DiscordIcon } from "../svg/DiscordIcon"; import { GithubIcon } from "../svg/GithubIcon"; @@ -55,4 +56,5 @@ export const NAMED_ICONS: { [key: string]: any } = { "gitlab-mr": GitlabIcon, http: GlobeAltIcon, trash: TrashIcon, + toolCall: WrenchScrewdriverIcon, }; diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index fb75d200c..a965f9559 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -319,7 +319,7 @@ export function Chat() { inputId={item.message.id} /> - ) : item.message.role === "tool" ? null : item.message.role === + ) : item.message.role === "tool" ? null : item.message.role === // /> // toolCallId={item.message.toolCallId} // contextItems={item.contextItems} // ); diff --git a/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx b/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx index 4a44a8ef3..644638309 100644 --- a/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx +++ b/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx @@ -1,5 +1,5 @@ import { ChevronRightIcon } from "@heroicons/react/24/outline"; -import { ContextItemWithId, Tool, ToolCallState } from "core"; +import { Tool, ToolCallState } from "core"; import { ComponentType, useContext, useMemo, useState } from "react"; import { ContextItemsPeekItem, @@ -7,26 +7,28 @@ import { } from "../../../components/mainInput/belowMainInput/ContextItemsPeek"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; +import { toolCallStateToContextItems } from "./toolCallStateToContextItem"; import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; +import { ToolTruncateHistoryIcon } from "./ToolTruncateHistoryIcon"; interface SimpleToolCallUIProps { toolCallState: ToolCallState; tool: Tool | undefined; - contextItems: ContextItemWithId[]; icon?: ComponentType>; + historyIndex: number; } export function SimpleToolCallUI({ - contextItems, icon: Icon, toolCallState, tool, + historyIndex, }: SimpleToolCallUIProps) { const ideMessenger = useContext(IdeMessengerContext); - - const ctxItems = useMemo(() => { - return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? []; - }, [contextItems]); + const shownContextItems = useMemo(() => { + const contextItems = toolCallStateToContextItems(toolCallState); + return contextItems.filter((item) => !item.hidden); + }, [toolCallState]); const [open, setOpen] = useState(false); const [isHovered, setIsHovered] = useState(false); @@ -36,8 +38,8 @@ export function SimpleToolCallUI({ return Object.entries(toolCallState.parsedArgs); }, [toolCallState.parsedArgs]); - const isToggleable = ctxItems.length > 1; - const isSingleItem = ctxItems.length === 1; + const isToggleable = shownContextItems.length > 1; + const isSingleItem = shownContextItems.length === 1; const shouldShowContent = isToggleable ? open : false; const isClickable = isToggleable || isSingleItem; @@ -45,7 +47,7 @@ export function SimpleToolCallUI({ if (isToggleable) { setOpen((prev) => !prev); } else if (isSingleItem) { - openContextItem(ctxItems[0], ideMessenger); + openContextItem(shownContextItems[0], ideMessenger); } } @@ -71,7 +73,7 @@ export function SimpleToolCallUI({ } return ( -
+
- {args.length > 0 && ( - - )} +
+ {!!toolCallState.output?.length && ( + + )} + {args.length > 0 ? ( + + ) : null} +
- {isToggleable && ( @@ -104,8 +110,8 @@ export function SimpleToolCallUI({ shouldShowContent ? "max-h-[50vh] opacity-100" : "max-h-0 opacity-0" }`} > - {ctxItems.length > 0 ? ( - ctxItems.map((contextItem, idx) => ( + {shownContextItems.length > 0 ? ( + shownContextItems.map((contextItem, idx) => ( )) ) : ( diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx index dcfd186c7..956ef791b 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx @@ -2,12 +2,14 @@ import { Tool, ToolCallState } from "core"; import { useMemo, useState } from "react"; import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; +import { ToolTruncateHistoryIcon } from "./ToolTruncateHistoryIcon"; interface ToolCallDisplayProps { children: React.ReactNode; icon: React.ReactNode; tool: Tool | undefined; toolCallState: ToolCallState; + historyIndex: number; } export function ToolCallDisplay({ @@ -15,6 +17,7 @@ export function ToolCallDisplay({ toolCallState, children, icon, + historyIndex, }: ToolCallDisplayProps) { const [argsExpanded, setArgsExpanded] = useState(false); @@ -39,13 +42,18 @@ export function ToolCallDisplay({ toolCallState={toolCallState} />
- {!!args.length ? ( - - ) : null} +
+ {!!toolCallState.output && ( + + )} + {!!args.length ? ( + + ) : null} +
{argsExpanded && !!args.length && ( diff --git a/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx new file mode 100644 index 000000000..e4f0a1cc8 --- /dev/null +++ b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx @@ -0,0 +1,40 @@ +import { BarsArrowUpIcon } from "@heroicons/react/24/outline"; +import { chatMessageIsEmpty } from "core/llm/messages"; +import { findLastIndex } from "core/util/findLast"; +import { useMemo } from "react"; +import { useMainEditor } from "../../../components/mainInput/TipTapEditor"; +import { ToolbarButtonWithTooltip } from "../../../components/StyledMarkdownPreview/StepContainerPreToolbar/ToolbarButtonWithTooltip"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; +import { truncateHistoryToMessage } from "../../../redux/slices/sessionSlice"; + +export function ToolTruncateHistoryIcon({ + historyIndex, +}: { + historyIndex: number; +}) { + const isStreaming = useAppSelector((state) => state.session.isStreaming); + const history = useAppSelector((state) => state.session.history); + const lastMessageIndex = useMemo(() => { + return findLastIndex(history, (item) => !chatMessageIsEmpty(item.message)); + }, [history]); + const dispatch = useAppDispatch(); + const { mainEditor } = useMainEditor(); + if (isStreaming || historyIndex === lastMessageIndex) { + return null; + } + return ( + { + dispatch( + truncateHistoryToMessage({ + index: historyIndex, + }), + ); + mainEditor?.commands.focus(); + }} + > + + + ); +} diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index 9f867356e..4141631bb 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -12,12 +12,7 @@ import { PencilIcon, XMarkIcon, } from "@heroicons/react/24/outline"; -import { - ContextItemWithId, - ToolCallDelta, - ToolCallState, - ToolStatus, -} from "core"; +import { ToolCallDelta, ToolCallState, ToolStatus } from "core"; import { BuiltInToolNames } from "core/tools/builtIn"; import { ComponentType, useMemo } from "react"; import { vscButtonBackground } from "../../../components"; @@ -30,7 +25,6 @@ import { ToolCallDisplay } from "./ToolCall"; interface ToolCallDivProps { toolCall: ToolCallDelta; toolCallState: ToolCallState; - output?: ContextItemWithId[]; historyIndex: number; } @@ -86,7 +80,7 @@ export function ToolCallDiv(props: ToolCallDivProps) { icon={ props.toolCallState.status === "generated" ? ArrowRightIcon : icon } - contextItems={props.output ?? []} + historyIndex={props.historyIndex} /> ); } @@ -96,6 +90,7 @@ export function ToolCallDiv(props: ToolCallDivProps) { icon={getStatusIcon(props.toolCallState.status)} tool={tool} toolCallState={props.toolCallState} + historyIndex={props.historyIndex} > + toolCallCtxItemToCtxItemWithId(ctxItem, toolCallState.toolCallId), + ) ?? [] + ); +} + +export function toolCallCtxItemToCtxItemWithId( + ctxItem: ContextItem, + toolCallId: string, +): ContextItemWithId { + return { + ...ctxItem, + id: { + providerTitle: "toolCall", + itemId: toolCallId, + }, + }; +} diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 9c91cce73..92db57099 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -203,6 +203,28 @@ export const sessionSlice = createSlice({ state.isStreaming = true; }, + truncateHistoryToMessage: ( + state, + { + payload, + }: PayloadAction<{ + index: number; + }>, + ) => { + const { index } = payload; + + if (state.history.length && index < state.history.length) { + state.codeBlockApplyStates.curIndex = 0; + state.history = state.history.slice(0, index + 1).concat({ + message: { + id: uuidv4(), + role: "assistant", + content: "", // IMPORTANT - this is subsequently updated by response streaming + }, + contextItems: [], + }); + } + }, deleteMessage: (state, action: PayloadAction) => { // Deletes the current assistant message and the previous user message state.history.splice(action.payload - 1, 2); @@ -676,6 +698,7 @@ export const { addPromptCompletionPair, setActive, submitEditorAndInitAtIndex, + truncateHistoryToMessage, updateHistoryItemAtIndex, clearLastEmptyResponse, setMainEditorContentTrigger, From 995e8d80be8500260db1c39bc31677131dc958bf Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 2 Jul 2025 16:22:10 -0700 Subject: [PATCH 2/2] make tool icons a bit larger, update copy --- gui/src/pages/gui/Chat.tsx | 2 +- gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx | 2 +- gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index a965f9559..126c4e894 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -319,7 +319,7 @@ export function Chat() { inputId={item.message.id} /> - ) : item.message.role === "tool" ? null : item.message.role === // /> // toolCallId={item.message.toolCallId} // contextItems={item.contextItems} // - + {isShowing ? "Hide args" : "Show args"} diff --git a/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx index e4f0a1cc8..a898b400e 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx @@ -24,7 +24,7 @@ export function ToolTruncateHistoryIcon({ } return ( { dispatch( truncateHistoryToMessage({ @@ -34,7 +34,7 @@ export function ToolTruncateHistoryIcon({ mainEditor?.commands.focus(); }} > - + ); }