Merge pull request #2941 from continuedev/dallin/rerenders
Remove state.contextItems
This commit is contained in:
commit
be9e37894e
11 changed files with 63 additions and 196 deletions
|
|
@ -105,6 +105,7 @@ function StepContainer(props: StepContainerProps) {
|
|||
<StyledMarkdownPreview
|
||||
isRenderingInStepContainer
|
||||
source={stripImages(props.item.message.content)}
|
||||
contextItems={props.item.contextItems}
|
||||
/>
|
||||
)}
|
||||
</ContentDiv>
|
||||
|
|
|
|||
|
|
@ -1,28 +1,25 @@
|
|||
import { NodeViewWrapper } from "@tiptap/react";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { vscBadgeBackground } from "..";
|
||||
import { RootState } from "../../redux/store";
|
||||
import CodeSnippetPreview from "../markdown/CodeSnippetPreview";
|
||||
import { useSelector } from "react-redux";
|
||||
import { RootState } from "../../redux/store";
|
||||
|
||||
export const CodeBlockComponent = ({
|
||||
node,
|
||||
deleteNode,
|
||||
selected,
|
||||
editor,
|
||||
updateAttributes,
|
||||
}: any) => {
|
||||
const dispatch = useDispatch();
|
||||
export const CodeBlockComponent = (props: any) => {
|
||||
const { node, deleteNode, selected, editor, updateAttributes } = props;
|
||||
const item: ContextItemWithId = node.attrs.item;
|
||||
// const contextItems = useSelector(
|
||||
// (store: RootState) =>
|
||||
// store.state.history[store.state.history.length - 1].contextItems,
|
||||
// );
|
||||
// const isFirstContextItem = item.id === contextItems[0]?.id;
|
||||
const isFirstContextItem = false;
|
||||
|
||||
const contextItems = useSelector(
|
||||
(state: RootState) => state.state.contextItems,
|
||||
);
|
||||
return (
|
||||
<NodeViewWrapper className="code-block-with-content" as="p">
|
||||
<CodeSnippetPreview
|
||||
borderColor={
|
||||
item.id === contextItems[0]?.id
|
||||
isFirstContextItem
|
||||
? "#d0d"
|
||||
: selected
|
||||
? vscBadgeBackground
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import FileIcon from "../FileIcon";
|
|||
import SafeImg from "../SafeImg";
|
||||
import { INSTRUCTIONS_BASE_ITEM } from "core/context/providers/utils";
|
||||
import { getIconFromDropdownItem } from "./MentionList";
|
||||
import styled from "styled-components";
|
||||
import { getBasename } from "core/util";
|
||||
|
||||
interface ContextItemsPeekProps {
|
||||
|
|
|
|||
|
|
@ -225,10 +225,6 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
inDropdownRef.current = true;
|
||||
};
|
||||
|
||||
const contextItems = useSelector(
|
||||
(store: RootState) => store.state.contextItems,
|
||||
);
|
||||
|
||||
const defaultModel = useSelector(defaultModelSelector);
|
||||
const defaultModelRef = useUpdatingRef(defaultModel);
|
||||
|
||||
|
|
@ -498,19 +494,7 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
content: props.editorState || mainEditorContent || "",
|
||||
onFocus: () => setIsEditorFocused(true),
|
||||
onBlur: () => setIsEditorFocused(false),
|
||||
onUpdate: ({ editor, transaction }) => {
|
||||
// If /edit is typed and no context items are selected, select the first
|
||||
|
||||
if (contextItems.length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const json = editor.getJSON();
|
||||
let codeBlock = json.content?.find((el) => el.type === "codeBlock");
|
||||
if (!codeBlock) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
// onUpdate
|
||||
editable: !active || props.isMainInput,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
|||
source={`${fence}${getMarkdownLanguageTagForFile(
|
||||
props.item.description.split(" ")[0],
|
||||
)} ${props.item.description}\n${content}\n${fence}`}
|
||||
contextItems={[props.item]}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -14,13 +14,12 @@ import {
|
|||
import { getFontSize, isJetBrains } from "../../util";
|
||||
import "./katex.css";
|
||||
import "./markdown.css";
|
||||
import { useSelector } from "react-redux";
|
||||
import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice";
|
||||
import { ctxItemToRifWithContents } from "core/commands/util";
|
||||
import FilenameLink from "./FilenameLink";
|
||||
import StepContainerPreToolbar from "./StepContainerPreToolbar";
|
||||
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
|
||||
import StepContainerPreActionButtons from "./StepContainerPreActionButtons";
|
||||
import { ContextItemWithId } from "core";
|
||||
|
||||
const StyledMarkdown = styled.div<{
|
||||
fontSize?: number;
|
||||
|
|
@ -93,6 +92,7 @@ interface StyledMarkdownPreviewProps {
|
|||
className?: string;
|
||||
isRenderingInStepContainer?: boolean; // Currently only used to control the rendering of codeblocks
|
||||
scrollLocked?: boolean;
|
||||
contextItems?: ContextItemWithId[];
|
||||
}
|
||||
|
||||
const HLJS_LANGUAGE_CLASSNAME_PREFIX = "language-";
|
||||
|
|
@ -152,8 +152,6 @@ function processCodeBlocks(tree: any) {
|
|||
const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
||||
props: StyledMarkdownPreviewProps,
|
||||
) {
|
||||
const contextItems = useSelector(memoizedContextItemsSelector);
|
||||
|
||||
const [reactContent, setMarkdownSource] = useRemark({
|
||||
remarkPlugins: [remarkMath, () => processCodeBlocks],
|
||||
rehypePlugins: [
|
||||
|
|
@ -236,13 +234,14 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
code: ({ node, ...codeProps }) => {
|
||||
const content = getCodeChildrenContent(codeProps.children);
|
||||
|
||||
const ctxItem = contextItems.find((ctxItem) =>
|
||||
ctxItem.uri?.value.includes(content),
|
||||
);
|
||||
|
||||
if (ctxItem) {
|
||||
const rif = ctxItemToRifWithContents(ctxItem);
|
||||
return <FilenameLink rif={rif} />;
|
||||
if (props.contextItems) {
|
||||
const ctxItem = props.contextItems.find((ctxItem) =>
|
||||
ctxItem.uri?.value.includes(content),
|
||||
);
|
||||
if (ctxItem) {
|
||||
const rif = ctxItemToRifWithContents(ctxItem);
|
||||
return <FilenameLink rif={rif} />;
|
||||
}
|
||||
}
|
||||
|
||||
return <code {...codeProps}>{codeProps.children}</code>;
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import resolveEditorContent, {
|
|||
import { IIdeMessenger } from "../context/IdeMessenger";
|
||||
import { defaultModelSelector } from "../redux/selectors/modelSelectors";
|
||||
import {
|
||||
addContextItems,
|
||||
addPromptCompletionPair,
|
||||
clearLastResponse,
|
||||
initNewActiveMessage,
|
||||
|
|
@ -50,10 +49,6 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
|
|||
(store: RootState) => store.state.config.slashCommands || [],
|
||||
);
|
||||
|
||||
const contextItems = useSelector(
|
||||
(state: RootState) => state.state.contextItems,
|
||||
);
|
||||
|
||||
const history = useSelector((store: RootState) => store.state.history);
|
||||
const active = useSelector((store: RootState) => store.state.active);
|
||||
const activeRef = useRef(active);
|
||||
|
|
@ -226,26 +221,33 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
|
|||
.slice(0, 1000)
|
||||
.join("\n");
|
||||
}
|
||||
selectedContextItems.unshift({
|
||||
content: `The following file is currently open. Don't reference it if it's not relevant to the user's message.\n\n\`\`\`${getRelativePath(
|
||||
currentFile.path,
|
||||
await ideMessenger.ide.getWorkspaceDirs(),
|
||||
)}\n${currentFileContents}\n\`\`\``,
|
||||
name: `Active file: ${getBasename(currentFile.path)}`,
|
||||
description: currentFile.path,
|
||||
id: {
|
||||
itemId: currentFile.path,
|
||||
providerTitle: "file",
|
||||
},
|
||||
uri: {
|
||||
type: "file",
|
||||
value: currentFile.path,
|
||||
},
|
||||
});
|
||||
if (
|
||||
!selectedContextItems.find(
|
||||
(item) => item.uri?.value === currentFile.path,
|
||||
)
|
||||
) {
|
||||
// don't add the file if it's already in the context items
|
||||
selectedContextItems.unshift({
|
||||
content: `The following file is currently open. Don't reference it if it's not relevant to the user's message.\n\n\`\`\`${getRelativePath(
|
||||
currentFile.path,
|
||||
await ideMessenger.ide.getWorkspaceDirs(),
|
||||
)}\n${currentFileContents}\n\`\`\``,
|
||||
name: `Active file: ${getBasename(currentFile.path)}`,
|
||||
description: currentFile.path,
|
||||
id: {
|
||||
itemId: currentFile.path,
|
||||
providerTitle: "file",
|
||||
},
|
||||
uri: {
|
||||
type: "file",
|
||||
value: currentFile.path,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dispatch(addContextItems(contextItems));
|
||||
// dispatch(addContextItems(contextItems));
|
||||
|
||||
const message: ChatMessage = {
|
||||
role: "user",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { Dispatch } from "@reduxjs/toolkit";
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { useCallback, useContext, useEffect, useRef, useState } from "react";
|
||||
import { useSelector } from "react-redux";
|
||||
import { VSC_THEME_COLOR_VARS } from "../components";
|
||||
import { IdeMessengerContext } from "../context/IdeMessenger";
|
||||
|
|
@ -21,11 +21,10 @@ import useChatHandler from "./useChatHandler";
|
|||
import { useWebviewListener } from "./useWebviewListener";
|
||||
|
||||
function useSetup(dispatch: Dispatch<any>) {
|
||||
const [configLoaded, setConfigLoaded] = useState<boolean>(false);
|
||||
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
const loadConfig = async () => {
|
||||
const initialConfigLoad = useRef(false);
|
||||
const loadConfig = useCallback(async () => {
|
||||
const result = await ideMessenger.request(
|
||||
"config/getSerializedProfileInfo",
|
||||
undefined,
|
||||
|
|
@ -36,7 +35,7 @@ function useSetup(dispatch: Dispatch<any>) {
|
|||
const { config, profileId } = result.content;
|
||||
dispatch(setConfig(config));
|
||||
dispatch(setSelectedProfileId(profileId));
|
||||
setConfigLoaded(true);
|
||||
initialConfigLoad.current = true;
|
||||
setLocalStorage("disableIndexing", config.disableIndexing || false);
|
||||
|
||||
// Perform any actions needed with the config
|
||||
|
|
@ -44,13 +43,13 @@ function useSetup(dispatch: Dispatch<any>) {
|
|||
setLocalStorage("fontSize", config.ui.fontSize);
|
||||
document.body.style.fontSize = `${config.ui.fontSize}px`;
|
||||
}
|
||||
};
|
||||
}, [dispatch, ideMessenger, initialConfigLoad]);
|
||||
|
||||
// Load config from the IDE
|
||||
useEffect(() => {
|
||||
loadConfig();
|
||||
const interval = setInterval(() => {
|
||||
if (configLoaded) {
|
||||
if (initialConfigLoad.current) {
|
||||
clearInterval(interval);
|
||||
return;
|
||||
}
|
||||
|
|
@ -58,7 +57,7 @@ function useSetup(dispatch: Dispatch<any>) {
|
|||
}, 2_000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [configLoaded]);
|
||||
}, [initialConfigLoad, loadConfig]);
|
||||
|
||||
useEffect(() => {
|
||||
// Override persisted state
|
||||
|
|
|
|||
|
|
@ -290,7 +290,7 @@ export function Chat() {
|
|||
setLocalStorage("mainTextEntryCounter", 1);
|
||||
}
|
||||
},
|
||||
[state.history, state.contextItems, defaultModel, state, streamResponse],
|
||||
[state.history, defaultModel, state, streamResponse],
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
|
|
|
|||
|
|
@ -15,16 +15,6 @@ import { createSelector } from "reselect";
|
|||
import { v4 as uuidv4, v4 } from "uuid";
|
||||
import { RootState } from "../store";
|
||||
|
||||
export const memoizedContextItemsSelector = createSelector(
|
||||
[(state: RootState) => state.state.history],
|
||||
(history) => {
|
||||
return history.reduce<ContextItemWithId[]>((acc, item) => {
|
||||
acc.push(...item.contextItems);
|
||||
return acc;
|
||||
}, []);
|
||||
},
|
||||
);
|
||||
|
||||
// We need this to handle reorderings (e.g. a mid-array deletion) of the messages array.
|
||||
// The proper fix is adding a UUID to all chat messages, but this is the temp workaround.
|
||||
type ChatHistoryItemWithMessageId = ChatHistoryItem & {
|
||||
|
|
@ -33,7 +23,6 @@ type ChatHistoryItemWithMessageId = ChatHistoryItem & {
|
|||
|
||||
type State = {
|
||||
history: ChatHistoryItemWithMessageId[];
|
||||
contextItems: ContextItemWithId[];
|
||||
ttsActive: boolean;
|
||||
active: boolean;
|
||||
isGatheringContext: boolean;
|
||||
|
|
@ -49,7 +38,6 @@ type State = {
|
|||
|
||||
const initialState: State = {
|
||||
history: [],
|
||||
contextItems: [],
|
||||
ttsActive: false,
|
||||
active: false,
|
||||
isGatheringContext: false,
|
||||
|
|
@ -143,9 +131,6 @@ export const stateSlice = createSlice({
|
|||
state.history[index].contextItems = contextItems;
|
||||
}
|
||||
},
|
||||
addContextItems: (state, action: PayloadAction<ContextItemWithId[]>) => {
|
||||
state.contextItems = state.contextItems.concat(action.payload);
|
||||
},
|
||||
resubmitAtIndex: (
|
||||
state,
|
||||
{
|
||||
|
|
@ -173,7 +158,6 @@ export const stateSlice = createSlice({
|
|||
});
|
||||
|
||||
// https://github.com/continuedev/continue/pull/1021
|
||||
// state.contextItems = [];
|
||||
state.active = true;
|
||||
},
|
||||
deleteMessage: (state, action: PayloadAction<number>) => {
|
||||
|
|
@ -190,7 +174,7 @@ export const stateSlice = createSlice({
|
|||
) => {
|
||||
state.history.push({
|
||||
message: { role: "user", content: "", id: uuidv4() },
|
||||
contextItems: state.contextItems,
|
||||
contextItems: [],
|
||||
editorState: payload.editorState,
|
||||
});
|
||||
state.history.push({
|
||||
|
|
@ -201,8 +185,7 @@ export const stateSlice = createSlice({
|
|||
},
|
||||
contextItems: [],
|
||||
});
|
||||
// https://github.com/continuedev/continue/pull/1021
|
||||
// state.contextItems = [];
|
||||
|
||||
state.active = true;
|
||||
},
|
||||
setMessageAtIndex: (
|
||||
|
|
@ -272,78 +255,27 @@ export const stateSlice = createSlice({
|
|||
state.sessionId = payload.sessionId;
|
||||
} else {
|
||||
state.history = [];
|
||||
state.contextItems = [];
|
||||
state.active = false;
|
||||
state.title = "New Session";
|
||||
state.sessionId = v4();
|
||||
state.isInMultifileEdit = false;
|
||||
}
|
||||
},
|
||||
deleteContextWithIds: (
|
||||
state,
|
||||
{
|
||||
payload,
|
||||
}: PayloadAction<{ ids: ContextItemId[]; index: number | undefined }>,
|
||||
) => {
|
||||
const getKey = (id: ContextItemId) => `${id.providerTitle}-${id.itemId}`;
|
||||
const ids = new Set(payload.ids.map(getKey));
|
||||
|
||||
if (payload.index === undefined) {
|
||||
state.contextItems = state.contextItems.filter(
|
||||
(item) => !ids.has(getKey(item.id)),
|
||||
);
|
||||
} else {
|
||||
state.history[payload.index].contextItems = state.history[
|
||||
payload.index
|
||||
].contextItems.filter((item) => !ids.has(getKey(item.id)));
|
||||
}
|
||||
},
|
||||
addHighlightedCode: (
|
||||
state,
|
||||
{
|
||||
payload,
|
||||
}: PayloadAction<{ rangeInFileWithContents: any; edit: boolean }>,
|
||||
) => {
|
||||
let contextItems = [...state.contextItems].map((item) => {
|
||||
let contextItems =
|
||||
state.history[state.history.length - 1].contextItems ?? [];
|
||||
contextItems = contextItems.map((item) => {
|
||||
return { ...item, editing: false };
|
||||
});
|
||||
const base = payload.rangeInFileWithContents.filepath
|
||||
.split(/[\\/]/)
|
||||
.pop();
|
||||
|
||||
// Merge if there is overlap
|
||||
for (let i = 0; i < contextItems.length; i++) {
|
||||
const item = contextItems[i];
|
||||
if (item.description === payload.rangeInFileWithContents.filepath) {
|
||||
let newStart = payload.rangeInFileWithContents.range.start.line;
|
||||
let newEnd = payload.rangeInFileWithContents.range.end.line;
|
||||
let [oldStart, oldEnd] = item.name
|
||||
.split("(")[1]
|
||||
.split(")")[0]
|
||||
.split("-")
|
||||
.map((x: string) => parseInt(x) - 1);
|
||||
if (newStart > oldEnd || newEnd < oldStart) {
|
||||
continue;
|
||||
}
|
||||
const startLine = Math.min(newStart, oldStart);
|
||||
const endLine = Math.max(newEnd, oldEnd);
|
||||
|
||||
// const oldContents = item.content.split("\n");
|
||||
// const newContents =
|
||||
// payload.rangeInFileWithContents.contents.split("\n");
|
||||
// const finalContents = [];
|
||||
|
||||
contextItems[i] = {
|
||||
...item,
|
||||
name: `${base} (${startLine + 1}-${endLine + 1})`,
|
||||
content: payload.rangeInFileWithContents.contents,
|
||||
editing: true,
|
||||
editable: true,
|
||||
};
|
||||
return { ...state, contextItems };
|
||||
}
|
||||
}
|
||||
|
||||
const lineNums = `(${
|
||||
payload.rangeInFileWithContents.range.start.line + 1
|
||||
}-${payload.rangeInFileWithContents.range.end.line + 1})`;
|
||||
|
|
@ -358,46 +290,7 @@ export const stateSlice = createSlice({
|
|||
editing: true,
|
||||
editable: true,
|
||||
});
|
||||
|
||||
return { ...state, contextItems };
|
||||
},
|
||||
setEditingAtIds: (
|
||||
state,
|
||||
{
|
||||
payload,
|
||||
}: PayloadAction<{ ids: ContextItemId[]; index: number | undefined }>,
|
||||
) => {
|
||||
const ids = payload.ids.map((id) => id.itemId);
|
||||
|
||||
if (typeof payload.index === "undefined") {
|
||||
return {
|
||||
...state,
|
||||
contextItems: state.contextItems.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
editing: ids.includes(item.id.itemId),
|
||||
};
|
||||
}),
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
...state,
|
||||
history: state.history.map((step, i) => {
|
||||
if (i === payload.index) {
|
||||
return {
|
||||
...step,
|
||||
contextItems: step.contextItems.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
editing: ids.includes(item.id.itemId),
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
return step;
|
||||
}),
|
||||
};
|
||||
}
|
||||
state.history[state.history.length - 1].contextItems = contextItems;
|
||||
},
|
||||
setDefaultModel: (
|
||||
state,
|
||||
|
|
@ -426,15 +319,12 @@ export const stateSlice = createSlice({
|
|||
|
||||
export const {
|
||||
setContextItemsAtIndex,
|
||||
addContextItems,
|
||||
addContextItemsAtIndex,
|
||||
setInactive,
|
||||
streamUpdate,
|
||||
newSession,
|
||||
deleteContextWithIds,
|
||||
resubmitAtIndex,
|
||||
addHighlightedCode,
|
||||
setEditingAtIds,
|
||||
setDefaultModel,
|
||||
setConfig,
|
||||
setConfigError,
|
||||
|
|
|
|||
|
|
@ -40,12 +40,7 @@ const windowIDTransform = (windowID: string) =>
|
|||
);
|
||||
|
||||
const saveSubsetFilters = [
|
||||
createFilter("state", [
|
||||
"history",
|
||||
"contextItems",
|
||||
"sessionId",
|
||||
"defaultModelTitle",
|
||||
]),
|
||||
createFilter("state", ["history", "sessionId", "defaultModelTitle"]),
|
||||
];
|
||||
|
||||
const persistConfig = {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue