tool truncate history icon

This commit is contained in:
Dallin Romney 2025-07-01 12:34:44 -07:00
parent c86e88ff0c
commit 20503cd256
8 changed files with 138 additions and 38 deletions

View file

@ -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,
};

View file

@ -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} // <ToolOutput
"assistant" &&
item.message.toolCalls &&
item.toolCallState ? (
@ -330,7 +330,6 @@ export function Chat() {
key={i}
toolCallState={item.toolCallState!}
toolCall={toolCall}
output={history[index + 1]?.contextItems}
historyIndex={index}
/>
);

View file

@ -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<React.SVGProps<SVGSVGElement>>;
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 (
<div className="flex flex-col pl-5 pr-2 pt-4">
<div className="flex flex-col py-4 pl-5 pr-2">
<div className="flex min-w-0 flex-row items-center justify-between gap-2">
<div
className={`text-description flex min-w-0 flex-row items-center justify-between gap-1.5 text-xs transition-colors duration-200 ease-in-out ${
@ -87,15 +89,19 @@ export function SimpleToolCallUI({
</div>
<ToolCallStatusMessage tool={tool} toolCallState={toolCallState} />
</div>
{args.length > 0 && (
<ArgsToggleIcon
isShowing={showingArgs}
setIsShowing={setShowingArgs}
toolCallId={toolCallState.toolCallId}
/>
)}
<div className="flex flex-row items-center gap-1.5">
{!!toolCallState.output?.length && (
<ToolTruncateHistoryIcon historyIndex={historyIndex} />
)}
{args.length > 0 ? (
<ArgsToggleIcon
isShowing={showingArgs}
setIsShowing={setShowingArgs}
toolCallId={toolCallState.toolCallId}
/>
) : null}
</div>
</div>
<ArgsItems args={args} isShowing={showingArgs} />
{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) => (
<ContextItemsPeekItem key={idx} contextItem={contextItem} />
))
) : (

View file

@ -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}
/>
</div>
{!!args.length ? (
<ArgsToggleIcon
isShowing={argsExpanded}
setIsShowing={setArgsExpanded}
toolCallId={toolCallState.toolCallId}
/>
) : null}
<div className="flex flex-row items-center gap-1.5">
{!!toolCallState.output && (
<ToolTruncateHistoryIcon historyIndex={historyIndex} />
)}
{!!args.length ? (
<ArgsToggleIcon
isShowing={argsExpanded}
setIsShowing={setArgsExpanded}
toolCallId={toolCallState.toolCallId}
/>
) : null}
</div>
</div>
{argsExpanded && !!args.length && (
<ArgsItems args={args} isShowing={argsExpanded} />

View file

@ -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 (
<ToolbarButtonWithTooltip
tooltipContent="Truncate chat to this message"
onClick={() => {
dispatch(
truncateHistoryToMessage({
index: historyIndex,
}),
);
mainEditor?.commands.focus();
}}
>
<BarsArrowUpIcon className="h-2.5 w-2.5 flex-shrink-0" />
</ToolbarButtonWithTooltip>
);
}

View file

@ -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}
>
<FunctionSpecificToolCallDiv
toolCall={props.toolCall}

View file

@ -0,0 +1,27 @@
import { ContextItem, ContextItemWithId, ToolCallState } from "core";
export function toolCallStateToContextItems(
toolCallState: ToolCallState | undefined,
): ContextItemWithId[] {
if (!toolCallState) {
return [];
}
return (
toolCallState.output?.map((ctxItem) =>
toolCallCtxItemToCtxItemWithId(ctxItem, toolCallState.toolCallId),
) ?? []
);
}
export function toolCallCtxItemToCtxItemWithId(
ctxItem: ContextItem,
toolCallId: string,
): ContextItemWithId {
return {
...ctxItem,
id: {
providerTitle: "toolCall",
itemId: toolCallId,
},
};
}

View file

@ -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<number>) => {
// 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,