Merge pull request #6668 from continuedev/dallin/pruning-updates
Context limit and pruning updates
This commit is contained in:
commit
8b5cd291e6
18 changed files with 221 additions and 248 deletions
15
core/index.d.ts
vendored
15
core/index.d.ts
vendored
|
|
@ -1712,23 +1712,12 @@ export interface CompleteOnboardingPayload {
|
|||
apiKey?: string;
|
||||
}
|
||||
|
||||
export type PruningStatus = "deleted-last-input" | "pruned" | "not-pruned";
|
||||
|
||||
export interface CompiledMessagesResult {
|
||||
compiledChatMessages: ChatMessage[];
|
||||
pruningStatus: PruningStatus;
|
||||
didPrune: boolean;
|
||||
contextPercentage: number;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
CompiledMessagesResult,
|
||||
MessageContent,
|
||||
MessagePart,
|
||||
PruningStatus,
|
||||
Tool,
|
||||
} from "../index.js";
|
||||
import { autodetectTemplateType } from "./autodetect.js";
|
||||
|
|
@ -363,9 +362,6 @@ function getTokenCountingBufferSafety(contextLength: number) {
|
|||
}
|
||||
|
||||
const MIN_RESPONSE_TOKENS = 1000;
|
||||
function getMinResponseTokens(maxTokens: number) {
|
||||
return Math.min(MIN_RESPONSE_TOKENS, maxTokens);
|
||||
}
|
||||
|
||||
function pruneRawPromptFromTop(
|
||||
modelName: string,
|
||||
|
|
@ -425,6 +421,8 @@ function compileChatMessages({
|
|||
supportsImages: boolean;
|
||||
tools?: Tool[];
|
||||
}): CompiledMessagesResult {
|
||||
let didPrune = false;
|
||||
|
||||
let msgsCopy: ChatMessage[] = msgs.map((m) => ({ ...m }));
|
||||
|
||||
// If images not supported, convert MessagePart[] to string
|
||||
|
|
@ -446,22 +444,15 @@ function compileChatMessages({
|
|||
|
||||
msgsCopy = addSpaceToAnyEmptyMessages(msgsCopy);
|
||||
|
||||
while (msgsCopy.length > 1) {
|
||||
if (isUserOrToolMsg(msgsCopy.at(-1))) {
|
||||
break;
|
||||
}
|
||||
msgsCopy.pop();
|
||||
// Extract the tool sequence from the end of the message array
|
||||
const toolSequence = extractToolSequence(msgsCopy);
|
||||
|
||||
// Count tokens for all messages in the tool sequence
|
||||
let lastMessagesTokens = 0;
|
||||
for (const msg of toolSequence) {
|
||||
lastMessagesTokens += countChatMessageTokens(modelName, msg);
|
||||
}
|
||||
|
||||
// // Extract the tool sequence from the end of the message array
|
||||
// const toolSequence = extractToolSequence(msgsCopy);
|
||||
|
||||
// // Count tokens for all messages in the tool sequence
|
||||
// let lastMessagesTokens = 0;
|
||||
// for (const msg of toolSequence) {
|
||||
// lastMessagesTokens += countChatMessageTokens(modelName, msg);
|
||||
// }
|
||||
|
||||
// System message
|
||||
let systemMsgTokens = 0;
|
||||
if (systemMsg) {
|
||||
|
|
@ -475,7 +466,7 @@ function compileChatMessages({
|
|||
}
|
||||
|
||||
const countingSafetyBuffer = getTokenCountingBufferSafety(contextLength);
|
||||
const minOutputTokens = getMinResponseTokens(maxTokens);
|
||||
const minOutputTokens = Math.min(MIN_RESPONSE_TOKENS, maxTokens);
|
||||
|
||||
let inputTokensAvailable = contextLength;
|
||||
|
||||
|
|
@ -486,19 +477,19 @@ function compileChatMessages({
|
|||
// Non-negotiable messages
|
||||
inputTokensAvailable -= toolTokens;
|
||||
inputTokensAvailable -= systemMsgTokens;
|
||||
// inputTokensAvailable -= lastMessagesTokens;
|
||||
inputTokensAvailable -= lastMessagesTokens;
|
||||
|
||||
// Make sure there's enough context for the non-excludable items
|
||||
if (inputTokensAvailable < 0) {
|
||||
throw new Error(
|
||||
`Not enough context available to include the system message, last user message, and tools.
|
||||
There must be at least ${minOutputTokens} tokens remaining for output.
|
||||
Request had the following token counts:
|
||||
- contextLength: ${contextLength}
|
||||
- counting safety buffer: ${countingSafetyBuffer}
|
||||
- tools: ~${toolTokens}
|
||||
- system message: ~${systemMsgTokens}
|
||||
- max output tokens: ${maxTokens}`,
|
||||
There must be at least ${minOutputTokens} tokens remaining for output.
|
||||
Request had the following token counts:
|
||||
- contextLength: ${contextLength}
|
||||
- counting safety buffer: ${countingSafetyBuffer}
|
||||
- tools: ~${toolTokens}
|
||||
- system message: ~${systemMsgTokens}
|
||||
- max output tokens: ${maxTokens}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -513,12 +504,10 @@ function compileChatMessages({
|
|||
};
|
||||
});
|
||||
|
||||
let pruningStatus: PruningStatus = "not-pruned";
|
||||
|
||||
while (historyWithTokens.length > 0 && currentTotal > inputTokensAvailable) {
|
||||
const message = historyWithTokens.shift()!;
|
||||
currentTotal -= message.tokens;
|
||||
pruningStatus = "pruned";
|
||||
didPrune = true;
|
||||
|
||||
// At this point make sure no latent tool response without corresponding call
|
||||
while (historyWithTokens[0]?.role === "tool") {
|
||||
|
|
@ -527,18 +516,24 @@ function compileChatMessages({
|
|||
}
|
||||
}
|
||||
|
||||
if (historyWithTokens.length === 0) {
|
||||
pruningStatus = "deleted-last-input";
|
||||
}
|
||||
|
||||
// Now reassemble
|
||||
const reassembled: ChatMessage[] = [];
|
||||
if (systemMsg) {
|
||||
reassembled.push(systemMsg);
|
||||
}
|
||||
reassembled.push(...historyWithTokens.map(({ tokens, ...rest }) => rest));
|
||||
reassembled.push(...toolSequence);
|
||||
|
||||
return { compiledChatMessages: reassembled, pruningStatus };
|
||||
const inputTokens =
|
||||
currentTotal + systemMsgTokens + toolTokens + lastMessagesTokens;
|
||||
const availableTokens =
|
||||
contextLength - countingSafetyBuffer - minOutputTokens;
|
||||
const contextPercentage = inputTokens / availableTokens;
|
||||
return {
|
||||
compiledChatMessages: reassembled,
|
||||
didPrune,
|
||||
contextPercentage,
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
|||
|
|
@ -940,7 +940,7 @@ export abstract class BaseLLM implements ILLM {
|
|||
let messages = _messages;
|
||||
|
||||
// If not precompiled, compile the chat messages
|
||||
if (!messageOptions || messageOptions.precompiled === false) {
|
||||
if (!messageOptions?.precompiled) {
|
||||
const { compiledChatMessages } = compileChatMessages({
|
||||
modelName: completionOptions.model,
|
||||
msgs: _messages,
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@ export default function StepContainer(props: StepContainerProps) {
|
|||
const historyItemAfterThis = useAppSelector(
|
||||
(state) => state.session.history[props.index + 1],
|
||||
);
|
||||
const warningMessage = useAppSelector(
|
||||
(state) => state.session.warningMessage,
|
||||
);
|
||||
const uiConfig = useAppSelector(selectUIConfig);
|
||||
|
||||
// Calculate dimming and indicator state based on latest summary index
|
||||
|
|
|
|||
77
gui/src/components/mainInput/ContextStatus.tsx
Normal file
77
gui/src/components/mainInput/ContextStatus.tsx
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
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 history = useAppSelector((state) => state.session.history);
|
||||
const percent = Math.round((contextPercentage ?? 0) * 100);
|
||||
const isPruned = useAppSelector((state) => state.session.isPruned);
|
||||
if (!isPruned && percent < 60) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const barColorClass = isPruned
|
||||
? "bg-error"
|
||||
: percent > 80
|
||||
? "bg-warning"
|
||||
: "bg-description";
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ToolTip
|
||||
id="context-status"
|
||||
closeEvents={{
|
||||
// blur: false,
|
||||
mouseleave: true,
|
||||
click: true,
|
||||
mouseup: false,
|
||||
}}
|
||||
clickable
|
||||
>
|
||||
<div className="flex flex-col gap-0">
|
||||
<span className="inline-block">
|
||||
{`${percent}% of context filled`}
|
||||
</span>
|
||||
{isPruned && (
|
||||
<span className="inline-block">
|
||||
{`Oldest messages are being removed`}
|
||||
</span>
|
||||
)}
|
||||
{history.length > 0 && (
|
||||
<div>
|
||||
<span className="inline-block">Start a</span>{" "}
|
||||
<span
|
||||
className="inline-block cursor-pointer underline"
|
||||
onClick={() => {
|
||||
void dispatch(
|
||||
saveCurrentSession({
|
||||
openNewSession: true,
|
||||
generateTitle: false,
|
||||
}),
|
||||
);
|
||||
}}
|
||||
>
|
||||
New Session
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</ToolTip>
|
||||
<div
|
||||
data-tooltip-id="context-status"
|
||||
className="border-description-muted relative h-[14px] w-[7px] rounded-[1px] border-[0.5px] border-solid md:h-[10px] md:w-[5px]"
|
||||
>
|
||||
<div
|
||||
className={`transition-height absolute bottom-0 left-0 w-full duration-300 ease-in-out ${barColorClass}`}
|
||||
style={{ height: `${percent}%` }}
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ContextStatus;
|
||||
|
|
@ -10,9 +10,6 @@ import { GradientBorder } from "./GradientBorder";
|
|||
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 +40,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 +119,6 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
/>
|
||||
</div>
|
||||
)}
|
||||
{props.isLastUserInput &&
|
||||
warningMessage &&
|
||||
(warningMessage.category === "exceeded-context-length" ? (
|
||||
<WarningMessageBox
|
||||
warningMessage={warningMessage}
|
||||
actions={[NewSessionAction]}
|
||||
/>
|
||||
) : (
|
||||
<WarningMessageBox
|
||||
warningMessage={warningMessage}
|
||||
actions={[EditConfigAction]}
|
||||
/>
|
||||
))}
|
||||
</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 { useAppDispatch, useAppSelector } from "../../redux/hooks";
|
||||
import { setInlineErrorMessage } from "../../redux/slices/sessionSlice";
|
||||
|
||||
export type InlineErrorMessageType = "out-of-context";
|
||||
|
||||
export default function InlineErrorMessage() {
|
||||
const dispatch = useAppDispatch();
|
||||
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={() => {
|
||||
dispatch(setInlineErrorMessage(undefined));
|
||||
}}
|
||||
>
|
||||
Hide
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ import ModelSelect from "../modelSelection/ModelSelect";
|
|||
import { ModeSelect } from "../ModeSelect";
|
||||
import { Button } from "../ui";
|
||||
import { useFontSize } from "../ui/font";
|
||||
import ContextStatus from "./ContextStatus";
|
||||
import HoverItem from "./InputToolbar/HoverItem";
|
||||
|
||||
export interface ToolbarOptions {
|
||||
|
|
@ -170,6 +171,7 @@ function InputToolbar(props: InputToolbarProps) {
|
|||
fontSize: tinyFont,
|
||||
}}
|
||||
>
|
||||
{!isInEdit && <ContextStatus />}
|
||||
{!props.toolbarOptions?.hideUseCodebase && !isInEdit && (
|
||||
<div
|
||||
className={`${toolsSupported ? "md:flex" : "int:flex"} hover:underline" hidden transition-colors duration-200`}
|
||||
|
|
@ -209,7 +211,6 @@ function InputToolbar(props: InputToolbarProps) {
|
|||
</span>
|
||||
</HoverItem>
|
||||
)}
|
||||
|
||||
<Button
|
||||
data-tooltip-id="enter-tooltip"
|
||||
variant={props.isMainInput ? "primary" : "secondary"}
|
||||
|
|
|
|||
|
|
@ -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} />;
|
||||
}
|
||||
|
|
@ -218,7 +218,7 @@ function ModelSelect() {
|
|||
{modelSelectTitle(selectedModel) || "Select model"}
|
||||
</span>
|
||||
<ChevronDownIcon
|
||||
className="h-2 w-2 flex-shrink-0 hover:brightness-110"
|
||||
className="hidden h-2 w-2 flex-shrink-0 hover:brightness-110 min-[200px]:flex"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</ListboxButton>
|
||||
|
|
|
|||
|
|
@ -86,7 +86,8 @@ const DEFAULT_MOCK_CORE_RESPONSES: MockResponses = {
|
|||
},
|
||||
"llm/compileChat": {
|
||||
compiledChatMessages: [],
|
||||
pruningStatus: "not-pruned",
|
||||
didPrune: false,
|
||||
contextPercentage: 0.5,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import { streamResponseThunk } from "../../redux/thunks/streamResponse";
|
|||
import { isJetBrains, isMetaEquivalentKeyPressed } from "../../util";
|
||||
import { ToolCallDiv } from "./ToolCallDiv";
|
||||
|
||||
import InlineErrorMessage from "../../components/mainInput/InlineErrorMessage";
|
||||
import { cancelStream } from "../../redux/thunks/cancelStream";
|
||||
import { EmptyChatBody } from "./EmptyChatBody";
|
||||
import { ExploreDialogWatcher } from "./ExploreDialogWatcher";
|
||||
|
|
@ -123,9 +124,6 @@ export function Chat() {
|
|||
const jetbrains = useMemo(() => {
|
||||
return isJetBrains();
|
||||
}, []);
|
||||
const warningMessage = useAppSelector(
|
||||
(state) => state.session.warningMessage,
|
||||
);
|
||||
|
||||
useAutoScroll(stepsDivRef, history);
|
||||
|
||||
|
|
@ -411,6 +409,7 @@ export function Chat() {
|
|||
>
|
||||
{renderChatHistoryItem(item, index)}
|
||||
</ErrorBoundary>
|
||||
{index === history.length - 1 && <InlineErrorMessage />}
|
||||
</div>
|
||||
))}
|
||||
</StepsDiv>
|
||||
|
|
@ -432,21 +431,19 @@ 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>
|
||||
|
|
|
|||
|
|
@ -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,8 +224,10 @@ type SessionState = {
|
|||
};
|
||||
newestToolbarPreviewForInput: Record<string, string>;
|
||||
hasReasoningEnabled?: boolean;
|
||||
isPruned?: boolean;
|
||||
contextPercentage?: number;
|
||||
inlineErrorMessage?: InlineErrorMessageType;
|
||||
compactionLoading: Record<number, boolean>; // Track compaction loading by message index
|
||||
warningMessage?: WarningMessage;
|
||||
};
|
||||
|
||||
const initialState: SessionState = {
|
||||
|
|
@ -431,12 +433,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,
|
||||
|
|
@ -647,6 +654,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;
|
||||
|
|
@ -913,11 +924,17 @@ export const sessionSlice = createSlice({
|
|||
delete state.compactionLoading[index];
|
||||
}
|
||||
},
|
||||
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: {
|
||||
|
|
@ -1002,8 +1019,10 @@ export const {
|
|||
setNewestToolbarPreviewForInput,
|
||||
setIsInEdit,
|
||||
setHasReasoningEnabled,
|
||||
setInlineErrorMessage,
|
||||
setIsPruned,
|
||||
setContextPercentage,
|
||||
setCompactionLoading,
|
||||
setWarningMessage,
|
||||
} = 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,42 +186,29 @@ 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") {
|
||||
throw new Error(precompiledRes.error);
|
||||
}
|
||||
|
||||
const { compiledChatMessages, pruningStatus } = precompiledRes.content;
|
||||
|
||||
switch (pruningStatus) {
|
||||
case "pruned":
|
||||
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",
|
||||
}),
|
||||
);
|
||||
break;
|
||||
case "deleted-last-input":
|
||||
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 {
|
||||
throw new Error(precompiledRes.error);
|
||||
}
|
||||
}
|
||||
|
||||
const { compiledChatMessages, didPrune, contextPercentage } =
|
||||
precompiledRes.content;
|
||||
|
||||
dispatch(setIsPruned(didPrune));
|
||||
dispatch(setContextPercentage(contextPercentage));
|
||||
|
||||
// Send request and stream response
|
||||
const streamAborter = state.session.streamAborter;
|
||||
const gen = extra.ideMessenger.llmStreamChat(
|
||||
|
|
|
|||
|
|
@ -25,4 +25,4 @@ export default function createReleaseConfig(packageName) {
|
|||
"@semantic-release/github",
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue