diff --git a/core/index.d.ts b/core/index.d.ts index e1516a970..589b96ac0 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1698,13 +1698,3 @@ export interface CompiledMessagesResult { export interface MessageOption { precompiled: boolean; } - -export type WarningMessageLevel = "warning" | "fatal"; - -export type WarningCategory = "exceeded-context-length" | "deleted-last-input"; - -export interface WarningMessage { - message: string; - level: WarningMessageLevel; - category: WarningCategory; -} diff --git a/core/llm/countTokens.ts b/core/llm/countTokens.ts index e83a6acd9..65a7cca1f 100644 --- a/core/llm/countTokens.ts +++ b/core/llm/countTokens.ts @@ -529,6 +529,7 @@ function compileChatMessages({ reassembled.push(systemMsg); } reassembled.push(...historyWithTokens.map(({ tokens, ...rest }) => rest)); + reassembled.push(...toolSequence); const contextPercentage = currentTotal / contextLength; return { diff --git a/gui/src/components/mainInput/ContextStatus.tsx b/gui/src/components/mainInput/ContextStatus.tsx new file mode 100644 index 000000000..8366acc99 --- /dev/null +++ b/gui/src/components/mainInput/ContextStatus.tsx @@ -0,0 +1,54 @@ +import { useAppDispatch, useAppSelector } from "../../redux/hooks"; +import { saveCurrentSession } from "../../redux/thunks/session"; +import { ToolTip } from "../gui/Tooltip"; + +const ContextStatus = () => { + const dispatch = useAppDispatch(); + const contextPercentage = useAppSelector( + (state) => state.session.contextPercentage, + ); + const percent = Math.round((contextPercentage ?? 0) * 100); + const isPruned = useAppSelector((state) => state.session.isPruned); + if (!isPruned && percent < 0.6) { + return null; + } + return ( +
+ { + dispatch( + saveCurrentSession({ + openNewSession: true, + generateTitle: false, + }), + ); + }} + > + New Session + + {contextPercentage !== undefined && ( + <> + {percent}% + + {`${percent}% of context is filled`} + + + )} + + {isPruned && ( + <> +
+ + Chat history exceeds context limit, old messages are being removed + + + )} +
+ ); +}; + +export default ContextStatus; diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index d95a0a118..7234fa03c 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -7,12 +7,10 @@ import { selectSlashCommandComboBoxInputs } from "../../redux/selectors"; import { ContextItemsPeek } from "./belowMainInput/ContextItemsPeek"; import { RulesPeek } from "./belowMainInput/RulesPeek"; import { GradientBorder } from "./GradientBorder"; +import InlineErrorMessage from "./InlineErrorMessage"; import { ToolbarOptions } from "./InputToolbar"; import { Lump } from "./Lump"; import { TipTapEditor } from "./TipTapEditor"; -import WarningMessageBox from "./WarningMessageBox"; -import EditConfigAction from "./WarningMessageBox/Actions/EditConfigAction"; -import NewSessionAction from "./WarningMessageBox/Actions/NewSessionAction"; interface ContinueInputBoxProps { isLastUserInput: boolean; @@ -43,9 +41,6 @@ const EDIT_DISALLOWED_CONTEXT_PROVIDERS = [ function ContinueInputBox(props: ContinueInputBoxProps) { const isStreaming = useAppSelector((state) => state.session.isStreaming); - const warningMessage = useAppSelector( - (state) => state.session.warningMessage, - ); const availableSlashCommands = useAppSelector( selectSlashCommandComboBoxInputs, ); @@ -125,19 +120,7 @@ function ContinueInputBox(props: ContinueInputBoxProps) { /> )} - {props.isLastUserInput && - warningMessage && - (warningMessage.category === "exceeded-context-length" ? ( - - ) : ( - - ))} + {props.isLastUserInput && } ); } diff --git a/gui/src/components/mainInput/InlineErrorMessage.tsx b/gui/src/components/mainInput/InlineErrorMessage.tsx new file mode 100644 index 000000000..631b939c1 --- /dev/null +++ b/gui/src/components/mainInput/InlineErrorMessage.tsx @@ -0,0 +1,48 @@ +import { useContext } from "react"; +import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { useAppSelector } from "../../redux/hooks"; + +export type InlineErrorMessageType = "out-of-context"; + +export default function InlineErrorMessage() { + const ideMessenger = useContext(IdeMessengerContext); + const inlineErrorMessage = useAppSelector( + (state) => state.session.inlineErrorMessage, + ); + if (inlineErrorMessage === "out-of-context") { + return ( +
+

+ {`Message exceeds context limit.`} +

+
+
{ + ideMessenger.post("config/openProfile", { + profileId: undefined, + }); + }} + > + Open config + Config +
+ | + { + ideMessenger.post("config/openProfile", { + profileId: undefined, + }); + }} + > + Hide + +
+
+ ); + } + return null; +} diff --git a/gui/src/components/mainInput/WarningMessageBox/Actions/EditConfigAction.tsx b/gui/src/components/mainInput/WarningMessageBox/Actions/EditConfigAction.tsx deleted file mode 100644 index 6ff98bf61..000000000 --- a/gui/src/components/mainInput/WarningMessageBox/Actions/EditConfigAction.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import { useContext } from "react"; -import { IdeMessengerContext } from "../../../../context/IdeMessenger"; -export default function EditConfigAction() { - const ideMessenger = useContext(IdeMessengerContext); - return ( - - ideMessenger.post("config/openProfile", { profileId: undefined }) - } - className="hover:underline" - > - Open configuration file - - ); -} diff --git a/gui/src/components/mainInput/WarningMessageBox/Actions/NewSessionAction.tsx b/gui/src/components/mainInput/WarningMessageBox/Actions/NewSessionAction.tsx deleted file mode 100644 index 2f397e7a2..000000000 --- a/gui/src/components/mainInput/WarningMessageBox/Actions/NewSessionAction.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import { useAppDispatch } from "../../../../redux/hooks"; -import { newSession } from "../../../../redux/slices/sessionSlice"; -export default function NewSessionAction() { - const dispatch = useAppDispatch(); - return ( - dispatch(newSession())} className="hover:underline"> - Start a new session - - ); -} diff --git a/gui/src/components/mainInput/WarningMessageBox/ExpendableBox.tsx b/gui/src/components/mainInput/WarningMessageBox/ExpendableBox.tsx deleted file mode 100644 index dbcbf21a9..000000000 --- a/gui/src/components/mainInput/WarningMessageBox/ExpendableBox.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { ReactNode, useEffect, useRef, useState } from "react"; -export default function ExpendableBox({ children }: { children: ReactNode }) { - const [isExpended, setIsExpended] = useState(false); - const [showTrigger, setShowTrigger] = useState(true); - const expendableDivRef = useRef(null); - const toggleExpendable = () => { - setIsExpended((pre) => !pre); - }; - - useEffect(() => { - if (expendableDivRef.current) { - expendableDivRef.current.style.maxHeight = isExpended ? "600px" : "0px"; - } - - if (isExpended) { - setShowTrigger(false); - } else { - setTimeout(() => setShowTrigger(true), 200); - } - }, [isExpended]); - - return ( -
-
- -
- {children} -
-
- ); -} diff --git a/gui/src/components/mainInput/WarningMessageBox/index.tsx b/gui/src/components/mainInput/WarningMessageBox/index.tsx deleted file mode 100644 index 2b14a3457..000000000 --- a/gui/src/components/mainInput/WarningMessageBox/index.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import { WarningMessage } from "core"; -import { ReactElement } from "react"; -import ExpendableBox from "./ExpendableBox"; - -function MessageBox({ - warningMessage, - actions, -}: { - warningMessage: WarningMessage; - actions?: (() => ReactElement)[]; -}) { - const warningMessageTextColor = - warningMessage.level === "fatal" ? "text-error" : "text-warning"; - const warningMessageBorderColor = - warningMessage.level === "fatal" ? "border-error" : "border-warning"; - return ( -
-

- Warning: - {`${warningMessage.message}`} -

- {actions && ( -
-

{`Recommended Action${actions.length > 1 ? "s" : ""}:`}

-
    - {actions.map((action, ind) => { - return
  • {action()}
  • ; - })} -
-
- )} -
- ); -} - -export default function WarningMessageBox({ - warningMessage, - actions, -}: { - warningMessage: WarningMessage; - actions?: (() => ReactElement)[]; -}) { - if (warningMessage.category === "exceeded-context-length") { - return ( - - } - /> - ); - } - return ; -} diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 383f18241..46377e094 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -43,6 +43,7 @@ import { streamResponseThunk } from "../../redux/thunks/streamResponse"; import { isJetBrains, isMetaEquivalentKeyPressed } from "../../util"; import { ToolCallDiv } from "./ToolCallDiv"; +import ContextStatus from "../../components/mainInput/ContextStatus"; import { cancelStream } from "../../redux/thunks/cancelStream"; import { EmptyChatBody } from "./EmptyChatBody"; import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; @@ -409,23 +410,24 @@ export function Chat() {
{history.length === 0 && lastSessionId && !isInEdit && ( -
- { - await dispatch( - loadLastSession({ - saveCurrentSession: true, - }), - ); - }} - className="flex items-center gap-2" - > - - Last Session - -
+ { + await dispatch( + loadLastSession({ + saveCurrentSession: true, + }), + ); + }} + className="flex items-center gap-2" + > + + Last Session + )}
+
+ +
{!hasDismissedExploreDialog && } {history.length === 0 && ( diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index ddecb6c19..0c40ed2eb 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -23,7 +23,6 @@ import { Tool, ToolCallDelta, ToolCallState, - WarningMessage, } from "core"; import { BuiltInToolNames } from "core/tools/builtIn"; import { NEW_SESSION_TITLE } from "core/util/constants"; @@ -34,6 +33,7 @@ import { import { findUriInDirs, getUriPathBasename } from "core/util/uri"; import { findLastIndex } from "lodash"; import { v4 as uuidv4 } from "uuid"; +import { type InlineErrorMessageType } from "../../components/mainInput/InlineErrorMessage"; import { toolCallCtxItemToCtxItemWithId } from "../../pages/gui/ToolCallDiv/utils"; import { addToolCallDeltaToState } from "../../util/toolCallState"; import { RootState } from "../store"; @@ -224,7 +224,9 @@ type SessionState = { }; newestToolbarPreviewForInput: Record; hasReasoningEnabled?: boolean; - warningMessage?: WarningMessage; + isPruned?: boolean; + contextPercentage?: number; + inlineErrorMessage?: InlineErrorMessageType; }; const initialState: SessionState = { @@ -244,6 +246,7 @@ const initialState: SessionState = { }, lastSessionId: undefined, newestToolbarPreviewForInput: {}, + isPruned: false, }; export const sessionSlice = createSlice({ @@ -421,12 +424,17 @@ export const sessionSlice = createSlice({ }, contextItems: [], }); + state.inlineErrorMessage = undefined; + state.isPruned = false; + state.contextPercentage = undefined; } }, deleteMessage: (state, action: PayloadAction) => { // Deletes the current assistant message and the previous user message state.history.splice(action.payload - 1, 2); - state.warningMessage = undefined; + state.inlineErrorMessage = undefined; + state.isPruned = false; + state.contextPercentage = undefined; }, updateHistoryItemAtIndex: ( state, @@ -637,6 +645,10 @@ export const sessionSlice = createSlice({ state.isStreaming = false; state.symbols = {}; + state.inlineErrorMessage = undefined; + state.isPruned = false; + state.contextPercentage = undefined; + if (payload) { state.history = payload.history as any; state.title = payload.title; @@ -892,11 +904,17 @@ export const sessionSlice = createSlice({ state.newestToolbarPreviewForInput[payload.inputId] = payload.contextItemId; }, - setWarningMessage: ( + setInlineErrorMessage: ( state, - action: PayloadAction, + action: PayloadAction, ) => { - state.warningMessage = action.payload; + state.inlineErrorMessage = action.payload; + }, + setIsPruned: (state, action: PayloadAction) => { + state.isPruned = action.payload; + }, + setContextPercentage: (state, action: PayloadAction) => { + state.contextPercentage = action.payload; }, }, selectors: { @@ -981,7 +999,9 @@ export const { setNewestToolbarPreviewForInput, setIsInEdit, setHasReasoningEnabled, - setWarningMessage, + setInlineErrorMessage, + setIsPruned, + setContextPercentage, } = sessionSlice.actions; export const { selectIsGatheringContext } = sessionSlice.selectors; diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index 0e1b99a34..6c722817b 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -11,9 +11,11 @@ import { addPromptCompletionPair, setActive, setAppliedRulesAtIndex, + setContextPercentage, setInactive, + setInlineErrorMessage, + setIsPruned, setToolGenerated, - setWarningMessage, streamUpdate, } from "../slices/sessionSlice"; import { AppThunkDispatch, RootState, ThunkApiType } from "../store"; @@ -184,23 +186,16 @@ export const streamNormalInput = createAsyncThunk< ); dispatch(setActive()); - // Remove the warning message before each compileChat call - dispatch(setWarningMessage(undefined)); + dispatch(setInlineErrorMessage(undefined)); + const precompiledRes = await extra.ideMessenger.request("llm/compileChat", { messages, options: completionOptions, }); if (precompiledRes.status === "error") { - if (precompiledRes.error.includes("Not enough context ")) { - dispatch( - setWarningMessage({ - message: - "The provided context items are too large. Please trim the context item to fit the model's context length or increase the model's context length by editing the configuration.", - level: "fatal", - category: "deleted-last-input", - }), - ); + if (precompiledRes.error.includes("Not enough context")) { + dispatch(setInlineErrorMessage("out-of-context")); dispatch(setInactive()); return; } else { @@ -208,17 +203,11 @@ export const streamNormalInput = createAsyncThunk< } } - const { compiledChatMessages, didPrune } = precompiledRes.content; + const { compiledChatMessages, didPrune, contextPercentage } = + precompiledRes.content; - if (didPrune) { - dispatch( - setWarningMessage({ - message: `Chat history exceeds model's context length (${state.config.config.selectedModelByRole.chat?.contextLength} tokens). Old messages will not be included.`, - level: "warning", - category: "exceeded-context-length", - }), - ); - } + dispatch(setIsPruned(didPrune)); + dispatch(setContextPercentage(contextPercentage)); // Send request and stream response const streamAborter = state.session.streamAborter;