diff --git a/gui/src/components/History/HistoryTableRow.tsx b/gui/src/components/History/HistoryTableRow.tsx index 26107c199..695389c29 100644 --- a/gui/src/components/History/HistoryTableRow.tsx +++ b/gui/src/components/History/HistoryTableRow.tsx @@ -1,11 +1,12 @@ import { PencilSquareIcon, TrashIcon } from "@heroicons/react/24/outline"; import { SessionInfo } from "core"; import React, { useState } from "react"; -import { useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import { Input } from ".."; import useHistory from "../../hooks/useHistory"; import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; +import { RootState } from "../../redux/store"; function lastPartOfPath(path: string): string { const sep = path.includes("/") ? "/" : "\\"; @@ -29,6 +30,9 @@ export function HistoryTableRow({ const [sessionTitleEditValue, setSessionTitleEditValue] = useState( session.title, ); + const currentSessionId = useSelector( + (state: RootState) => state.state.sessionId, + ); const { saveSession, deleteSession, loadSession, getSession, updateSession } = useHistory(dispatch); @@ -59,8 +63,11 @@ export function HistoryTableRow({ className="hover:bg-vsc-editor-background relative box-border flex max-w-full cursor-pointer overflow-hidden rounded-lg p-3" onClick={async () => { // Save current session - await saveSession(); - await loadSession(session.sessionId); + if (session.sessionId !== currentSessionId) { + await saveSession(); + await loadSession(session.sessionId); + } + navigate("/"); }} > diff --git a/gui/src/components/mainInput/resolveInput.ts b/gui/src/components/mainInput/resolveInput.ts index 4c7f4c640..a933bd1a3 100644 --- a/gui/src/components/mainInput/resolveInput.ts +++ b/gui/src/components/mainInput/resolveInput.ts @@ -14,6 +14,7 @@ import { setIsGatheringContext, updateFileSymbols, } from "../../redux/slices/stateSlice"; +import { updateFileSymbolsFromContextItems } from "../../util/symbols"; interface MentionAttrs { label: string; @@ -174,23 +175,7 @@ async function resolveEditorContent( ); contextItems.push(...defaultContextItems.flat()); - const contextItemFileUris = Array.from( - new Set( - contextItems - .filter((item) => item.uri?.type === "file" && item?.uri?.value) - .map((item) => item.uri.value), - ), - ); - - const symbolsResult = await ideMessenger.request( - "context/getSymbolsForFiles", - { - uris: contextItemFileUris, - }, - ); - if (symbolsResult.status === "success") { - dispatch(updateFileSymbols(symbolsResult.content)); - } + await updateFileSymbolsFromContextItems(contextItems, ideMessenger, dispatch); if (contextItemsText !== "") { contextItemsText += "\n"; diff --git a/gui/src/components/markdown/FilenameLink.tsx b/gui/src/components/markdown/FilenameLink.tsx index 0d1166a00..d63bf8309 100644 --- a/gui/src/components/markdown/FilenameLink.tsx +++ b/gui/src/components/markdown/FilenameLink.tsx @@ -21,11 +21,11 @@ function FilenameLink({ rif }: FilenameLinkProps) { return ( - + {getBasename(rif.filepath)} diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 9ded0464f..ca1ea2afd 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -164,28 +164,18 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( // The refs are a workaround because rehype options are stored on initiation // So they won't use the most up-to-date state values // So in this case we just put them in refs - const symbolsForContextItems = useRef([]); + const symbolsRef = useRef([]); const contextItemsRef = useRef([]); + useEffect(() => { contextItemsRef.current = contextItems || []; - if (!contextItems) { - symbolsForContextItems.current = []; - } - // Get unique URIs and then find relevant symbols - const contextUris = Array.from( - new Set( - contextItems.filter((item) => item?.uri).map((item) => item.uri!.value), - ), - ); - const contextSymbols: SymbolWithRange[] = []; - contextUris.forEach((uri) => { - const fileSymbols = symbols[uri]; - if (fileSymbols) { - contextSymbols.push(...fileSymbols); - } - }); - symbolsForContextItems.current = contextSymbols; - }, [contextItems, symbols]); + }, [contextItems]); + useEffect(() => { + // Note, before I was only looking for symbols that matched + // Context item files on current history item + // but in practice global symbols for session makes way more sense + symbolsRef.current = Object.values(symbols).flat(); + }, [symbols]); const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [remarkMath, () => processCodeBlocks], @@ -278,7 +268,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( return ; } - const exactSymbol = symbolsForContextItems.current.find( + const exactSymbol = symbolsRef.current.find( (s) => s.name === content, ); if (exactSymbol) { @@ -286,7 +276,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( } // PARTIAL - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` - const partialSymbol = symbolsForContextItems.current.find((s) => + const partialSymbol = symbolsRef.current.find((s) => content.startsWith(s.name), ); if (partialSymbol) { diff --git a/gui/src/components/markdown/SymbolLink.tsx b/gui/src/components/markdown/SymbolLink.tsx index b18d302df..569a493e5 100644 --- a/gui/src/components/markdown/SymbolLink.tsx +++ b/gui/src/components/markdown/SymbolLink.tsx @@ -20,10 +20,10 @@ function SymbolLink({ symbol, content }: SymbolLinkProps) { return ( - + {content} diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index f159ffea3..b52271ab4 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -7,9 +7,10 @@ import { useSelector } from "react-redux"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useLastSessionContext } from "../context/LastSessionContext"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { newSession } from "../redux/slices/stateSlice"; +import { newSession, updateFileSymbols } from "../redux/slices/stateSlice"; import { RootState } from "../redux/store"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; +import { updateFileSymbolsFromContextItems } from "../util/symbols"; const MAX_TITLE_LENGTH = 100; @@ -22,7 +23,6 @@ function truncateText(text: string, maxLength: number) { function useHistory(dispatch: Dispatch) { const state = useSelector((state: RootState) => state.state); - const defaultModel = useSelector(defaultModelSelector); const ideMessenger = useContext(IdeMessengerContext); const { lastSessionId, setLastSessionId } = useLastSessionContext(); @@ -122,7 +122,18 @@ function useHistory(dispatch: Dispatch) { if (result.status === "error") { throw new Error(result.error); } + const persistedSessionInfo = result.content; + + // Update file symbols on session load for all context items + await updateFileSymbolsFromContextItems( + persistedSessionInfo.history + .map((item) => item.contextItems ?? []) + .flat(), + ideMessenger, + dispatch, + ); + dispatch(newSession(persistedSessionInfo)); return persistedSessionInfo; } diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index c8abe266e..09735499f 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -4,7 +4,6 @@ import { ChatHistoryItem, ChatMessage, Checkpoint, - ContextItemId, ContextItemWithId, FileSymbolMap, PersistedSessionInfo, @@ -146,7 +145,6 @@ export const stateSlice = createSlice({ state.mainEditorContent = undefined; }, updateFileSymbols: (state, action: PayloadAction) => { - console.log("UPDATE SYMBOLS", action.payload); state.symbols = Object.assign(state.symbols, action.payload); }, setContextItemsAtIndex: ( @@ -379,7 +377,7 @@ export const stateSlice = createSlice({ curApplyState.numDiffs = payload.numDiffs ?? curApplyState.numDiffs; curApplyState.filepath = payload.filepath ?? curApplyState.filepath; } - if(payload.status === "done"){ + if (payload.status === "done") { state.nextCodeBlockToApplyIndex++; } }, diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index 986075a84..725d88c01 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -39,12 +39,7 @@ const windowIDTransform = (windowID: string) => ); const saveSubsetFilters = [ - createFilter("state", [ - "history", - "sessionId", - "defaultModelTitle", - "symbols", - ]), + createFilter("state", ["history", "sessionId", "defaultModelTitle"]), ]; const persistConfig = { diff --git a/gui/src/util/symbols.ts b/gui/src/util/symbols.ts new file mode 100644 index 000000000..98848053a --- /dev/null +++ b/gui/src/util/symbols.ts @@ -0,0 +1,34 @@ +import { ContextItemWithId } from "core"; +import { IIdeMessenger } from "../context/IdeMessenger"; +import { updateFileSymbols } from "../redux/slices/stateSlice"; +import { Dispatch } from "@reduxjs/toolkit"; + +export async function updateFileSymbolsFromContextItems( + contextItems: ContextItemWithId[], + ideMessenger: IIdeMessenger, + dispatch: Dispatch, +) { + // Given a list of context items, + // Get unique file uris + try { + const contextUris = Array.from( + new Set( + contextItems + .filter((item) => item.uri?.type === "file" && item?.uri?.value) + .map((item) => item.uri.value), + ), + ); + // And then update symbols for those files + if (contextUris.length > 0) { + const symbolsResult = await ideMessenger.request( + "context/getSymbolsForFiles", + { uris: contextUris }, + ); + if (symbolsResult.status === "success") { + dispatch(updateFileSymbols(symbolsResult.content)); + } + } + } catch (e) { + console.error("Error updating file symbols from context items", e); + } +}