diff --git a/gui/src/components/mainInput/CodeBlockComponent.tsx b/gui/src/components/mainInput/CodeBlockComponent.tsx index 217fcc293..5d62c6d91 100644 --- a/gui/src/components/mainInput/CodeBlockComponent.tsx +++ b/gui/src/components/mainInput/CodeBlockComponent.tsx @@ -12,12 +12,11 @@ export const CodeBlockComponent = ({ editor, updateAttributes, }: any) => { - const dispatch = useDispatch(); const item: ContextItemWithId = node.attrs.item; - const contextItems = useSelector( - (state: RootState) => state.state.contextItems, - ); + // const contextItems = useSelector( + // (state: RootState) => state.state.history[0]?.contextItems[0]?.id, + // ); return ( diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index c2646415e..3353d4462 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -159,6 +159,7 @@ interface TipTapEditorProps { placeholder?: string; header?: React.ReactNode; historyKey: string; + contextItems?: ContextItemWithId[]; } function TipTapEditor(props: TipTapEditorProps) { @@ -222,10 +223,6 @@ function TipTapEditor(props: TipTapEditorProps) { inDropdownRef.current = true; }; - const contextItems = useSelector( - (store: RootState) => store.state.contextItems, - ); - const defaultModel = useSelector(defaultModelSelector); const defaultModelRef = useUpdatingRef(defaultModel); @@ -495,19 +492,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, }); diff --git a/gui/src/components/mainInput/resolveInput.ts b/gui/src/components/mainInput/resolveInput.ts index 64acba85a..27df6e4aa 100644 --- a/gui/src/components/mainInput/resolveInput.ts +++ b/gui/src/components/mainInput/resolveInput.ts @@ -30,6 +30,7 @@ async function resolveEditorContent( ideMessenger: IIdeMessenger, defaultContextProviders: DefaultContextProvider[], ): Promise<[ContextItemWithId[], RangeInFile[], MessageContent]> { + console.log("resolve editor content start"); let parts: MessagePart[] = []; let contextItemAttrs: MentionAttrs[] = []; const selectedCode: RangeInFile[] = []; @@ -171,6 +172,7 @@ async function resolveEditorContent( parts = [{ type: "text", text: lastPart }]; } } + console.log("resolve editor content end"); return [contextItems, selectedCode, parts]; } diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index a86a295c9..64bcecd04 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -15,12 +15,13 @@ 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 { RootState } from "../../redux/store"; +import { ContextItemWithId } from "core"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -93,6 +94,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 +154,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,7 +236,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( code: ({ node, ...codeProps }) => { const content = getCodeChildrenContent(codeProps.children); - const ctxItem = contextItems.find((ctxItem) => + const ctxItem = props.contextItems?.find((ctxItem) => ctxItem.uri?.value.includes(content), ); diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 2e35acc53..229c03ebc 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -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", diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index 2fed805da..c7f2a2598 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -21,7 +21,7 @@ import useChatHandler from "./useChatHandler"; import { useWebviewListener } from "./useWebviewListener"; function useSetup(dispatch: Dispatch) { - const [configLoaded, setConfigLoaded] = useState(false); + // const [configLoaded, setConfigLoaded] = useState(false); const ideMessenger = useContext(IdeMessengerContext); @@ -36,7 +36,7 @@ function useSetup(dispatch: Dispatch) { const { config, profileId } = result.content; dispatch(setConfig(config)); dispatch(setSelectedProfileId(profileId)); - setConfigLoaded(true); + // setConfigLoaded(true); setLocalStorage("disableIndexing", config.disableIndexing || false); // Perform any actions needed with the config @@ -49,16 +49,16 @@ function useSetup(dispatch: Dispatch) { // Load config from the IDE useEffect(() => { loadConfig(); - const interval = setInterval(() => { - if (configLoaded) { - clearInterval(interval); - return; - } - loadConfig(); - }, 2_000); + // const interval = setInterval(() => { + // if (configLoaded) { + // clearInterval(interval); + // return; + // } + // loadConfig(); + // }, 2_000); - return () => clearInterval(interval); - }, [configLoaded]); + // return () => clearInterval(interval); + }, []); useEffect(() => { // Override persisted state @@ -116,6 +116,7 @@ function useSetup(dispatch: Dispatch) { }, 1000); useWebviewListener("configUpdate", async () => { + console.log("config update"); await loadConfig(); if (!isJetBrains && !getLocalStorage("disableIndexing")) { diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 78bbcd262..6aaf38092 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -290,7 +290,7 @@ export function Chat() { setLocalStorage("mainTextEntryCounter", 1); } }, - [state.history, state.contextItems, defaultModel, state, streamResponse], + [state.history, defaultModel, state, streamResponse], ); useWebviewListener( diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index f2cbe8dc9..e1b22f8f7 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -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((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, @@ -148,11 +136,6 @@ export const stateSlice = createSlice({ state.history[index].contextItems = contextItems; } }, - addContextItems: (state, action: PayloadAction) => { - console.log("addContextItems"); - - state.contextItems = state.contextItems.concat(action.payload); - }, resubmitAtIndex: ( state, { @@ -182,7 +165,6 @@ export const stateSlice = createSlice({ }); // https://github.com/continuedev/continue/pull/1021 - // state.contextItems = []; state.active = true; }, deleteMessage: (state, action: PayloadAction) => { @@ -201,7 +183,7 @@ export const stateSlice = createSlice({ state.history.push({ message: { role: "user", content: "", id: uuidv4() }, - contextItems: state.contextItems, + contextItems: [], editorState: payload.editorState, }); state.history.push({ @@ -212,8 +194,7 @@ export const stateSlice = createSlice({ }, contextItems: [], }); - // https://github.com/continuedev/continue/pull/1021 - // state.contextItems = []; + state.active = true; }, setMessageAtIndex: ( @@ -290,34 +271,12 @@ 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 }>, - ) => { - console.log("deleteContextWithIds"); - - 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, { @@ -325,47 +284,15 @@ export const stateSlice = createSlice({ }: PayloadAction<{ rangeInFileWithContents: any; edit: boolean }>, ) => { console.log("addHighlightedCode"); - - 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})`; @@ -380,48 +307,7 @@ export const stateSlice = createSlice({ editing: true, editable: true, }); - - return { ...state, contextItems }; - }, - setEditingAtIds: ( - state, - { - payload, - }: PayloadAction<{ ids: ContextItemId[]; index: number | undefined }>, - ) => { - console.log("setEditingAtIds"); - - 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, @@ -454,15 +340,12 @@ export const stateSlice = createSlice({ export const { setContextItemsAtIndex, - addContextItems, addContextItemsAtIndex, setInactive, streamUpdate, newSession, - deleteContextWithIds, resubmitAtIndex, addHighlightedCode, - setEditingAtIds, setDefaultModel, setConfig, setConfigError, diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index a5d0313f4..221d275c9 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -40,12 +40,7 @@ const windowIDTransform = (windowID: string) => ); const saveSubsetFilters = [ - createFilter("state", [ - "history", - "contextItems", - "sessionId", - "defaultModelTitle", - ]), + createFilter("state", ["history", "sessionId", "defaultModelTitle"]), ]; const persistConfig = {