context filled text, pruning warning
This commit is contained in:
parent
9431872c7c
commit
7e6c974e4d
12 changed files with 160 additions and 194 deletions
10
core/index.d.ts
vendored
10
core/index.d.ts
vendored
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -529,6 +529,7 @@ function compileChatMessages({
|
|||
reassembled.push(systemMsg);
|
||||
}
|
||||
reassembled.push(...historyWithTokens.map(({ tokens, ...rest }) => rest));
|
||||
reassembled.push(...toolSequence);
|
||||
|
||||
const contextPercentage = currentTotal / contextLength;
|
||||
return {
|
||||
|
|
|
|||
54
gui/src/components/mainInput/ContextStatus.tsx
Normal file
54
gui/src/components/mainInput/ContextStatus.tsx
Normal file
|
|
@ -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 (
|
||||
<div className="text-description-muted my-0.5 flex flex-row items-center gap-2 text-xs">
|
||||
<span
|
||||
className="cursor-pointer underline"
|
||||
onClick={() => {
|
||||
dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: true,
|
||||
generateTitle: false,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
>
|
||||
New Session
|
||||
</span>
|
||||
{contextPercentage !== undefined && (
|
||||
<>
|
||||
<span data-tooltip-id="percent-tooltip">{percent}%</span>
|
||||
<ToolTip id="percent-tooltip">
|
||||
{`${percent}% of context is filled`}
|
||||
</ToolTip>
|
||||
</>
|
||||
)}
|
||||
|
||||
{isPruned && (
|
||||
<>
|
||||
<div
|
||||
data-tooltip-id="prune-tooltip"
|
||||
className="bg-warning h-2 w-2 rounded-full"
|
||||
></div>
|
||||
<ToolTip id="prune-tooltip">
|
||||
Chat history exceeds context limit, old messages are being removed
|
||||
</ToolTip>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ContextStatus;
|
||||
|
|
@ -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) {
|
|||
/>
|
||||
</div>
|
||||
)}
|
||||
{props.isLastUserInput &&
|
||||
warningMessage &&
|
||||
(warningMessage.category === "exceeded-context-length" ? (
|
||||
<WarningMessageBox
|
||||
warningMessage={warningMessage}
|
||||
actions={[NewSessionAction]}
|
||||
/>
|
||||
) : (
|
||||
<WarningMessageBox
|
||||
warningMessage={warningMessage}
|
||||
actions={[EditConfigAction]}
|
||||
/>
|
||||
))}
|
||||
{props.isLastUserInput && <InlineErrorMessage />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
48
gui/src/components/mainInput/InlineErrorMessage.tsx
Normal file
48
gui/src/components/mainInput/InlineErrorMessage.tsx
Normal file
|
|
@ -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 (
|
||||
<div
|
||||
className={`border-border relative m-2 flex flex-col rounded-md border border-solid bg-transparent p-4`}
|
||||
>
|
||||
<p className={`thread-message text-error text-center`}>
|
||||
{`Message exceeds context limit.`}
|
||||
</p>
|
||||
<div className="text-description flex flex-row items-center justify-center gap-1.5 px-3">
|
||||
<div
|
||||
className="cursor-pointer text-xs hover:underline"
|
||||
onClick={() => {
|
||||
ideMessenger.post("config/openProfile", {
|
||||
profileId: undefined,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<span className="xs:flex hidden">Open config</span>
|
||||
<span className="xs:hidden">Config</span>
|
||||
</div>
|
||||
|
|
||||
<span
|
||||
className="cursor-pointer text-xs hover:underline"
|
||||
onClick={() => {
|
||||
ideMessenger.post("config/openProfile", {
|
||||
profileId: undefined,
|
||||
});
|
||||
}}
|
||||
>
|
||||
Hide
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
import { useContext } from "react";
|
||||
import { IdeMessengerContext } from "../../../../context/IdeMessenger";
|
||||
export default function EditConfigAction() {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
return (
|
||||
<span
|
||||
onClick={() =>
|
||||
ideMessenger.post("config/openProfile", { profileId: undefined })
|
||||
}
|
||||
className="hover:underline"
|
||||
>
|
||||
Open configuration file
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
import { useAppDispatch } from "../../../../redux/hooks";
|
||||
import { newSession } from "../../../../redux/slices/sessionSlice";
|
||||
export default function NewSessionAction() {
|
||||
const dispatch = useAppDispatch();
|
||||
return (
|
||||
<span onClick={() => dispatch(newSession())} className="hover:underline">
|
||||
Start a new session
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
|
@ -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<HTMLDivElement>(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 (
|
||||
<div className="flex flex-col">
|
||||
<div
|
||||
onClick={toggleExpendable}
|
||||
className={`h-[9px] w-[9px] self-end ${showTrigger ? "" : "hidden"} bg-warning my-2 mr-4 rounded-lg`}
|
||||
></div>
|
||||
|
||||
<div
|
||||
onClick={toggleExpendable}
|
||||
ref={expendableDivRef}
|
||||
style={{
|
||||
transition: "max-height 0.3s ease-in-out",
|
||||
overflowY: "hidden",
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -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 (
|
||||
<div
|
||||
className={`relative m-2 flex flex-col rounded-md border border-solid ${warningMessageBorderColor} bg-transparent p-4`}
|
||||
>
|
||||
<p className={`thread-message ${warningMessageTextColor}`}>
|
||||
<b>Warning: </b>
|
||||
{`${warningMessage.message}`}
|
||||
</p>
|
||||
{actions && (
|
||||
<div>
|
||||
<p>{`Recommended Action${actions.length > 1 ? "s" : ""}:`}</p>
|
||||
<ul>
|
||||
{actions.map((action, ind) => {
|
||||
return <li key={`warning-action-${ind}`}>{action()}</li>;
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function WarningMessageBox({
|
||||
warningMessage,
|
||||
actions,
|
||||
}: {
|
||||
warningMessage: WarningMessage;
|
||||
actions?: (() => ReactElement)[];
|
||||
}) {
|
||||
if (warningMessage.category === "exceeded-context-length") {
|
||||
return (
|
||||
<ExpendableBox
|
||||
children={
|
||||
<MessageBox warningMessage={warningMessage} actions={actions} />
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return <MessageBox warningMessage={warningMessage} actions={actions} />;
|
||||
}
|
||||
|
|
@ -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() {
|
|||
<div className="flex flex-row items-center justify-between pb-1 pl-0.5 pr-2">
|
||||
<div className="xs:inline hidden">
|
||||
{history.length === 0 && lastSessionId && !isInEdit && (
|
||||
<div className="xs:inline hidden">
|
||||
<NewSessionButton
|
||||
onClick={async () => {
|
||||
await dispatch(
|
||||
loadLastSession({
|
||||
saveCurrentSession: true,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<ArrowLeftIcon className="h-3 w-3" />
|
||||
<span className="text-xs">Last Session</span>
|
||||
</NewSessionButton>
|
||||
</div>
|
||||
<NewSessionButton
|
||||
onClick={async () => {
|
||||
await dispatch(
|
||||
loadLastSession({
|
||||
saveCurrentSession: true,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<ArrowLeftIcon className="h-3 w-3" />
|
||||
<span className="text-xs">Last Session</span>
|
||||
</NewSessionButton>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<ContextStatus />
|
||||
</div>
|
||||
</div>
|
||||
{!hasDismissedExploreDialog && <ExploreDialogWatcher />}
|
||||
{history.length === 0 && (
|
||||
|
|
|
|||
|
|
@ -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<string, string>;
|
||||
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<number>) => {
|
||||
// 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<WarningMessage | undefined>,
|
||||
action: PayloadAction<SessionState["inlineErrorMessage"]>,
|
||||
) => {
|
||||
state.warningMessage = action.payload;
|
||||
state.inlineErrorMessage = action.payload;
|
||||
},
|
||||
setIsPruned: (state, action: PayloadAction<boolean>) => {
|
||||
state.isPruned = action.payload;
|
||||
},
|
||||
setContextPercentage: (state, action: PayloadAction<number>) => {
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue