From 3ab54c71d2b947a0c172a409cca1da0dcc960eb9 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 9 Dec 2024 01:01:49 -0800 Subject: [PATCH] move symbol updates to thunks --- gui/src/App.tsx | 4 +- gui/src/editorInset/EditorInset.tsx | 3 +- gui/src/hooks/useSetup.ts | 26 ++----- gui/src/redux/thunks/gatherContext.ts | 8 +- gui/src/redux/thunks/updateFileSymbols.ts | 94 +++++++++++++++++++++++ gui/src/util/symbols.ts | 35 --------- 6 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 gui/src/redux/thunks/updateFileSymbols.ts delete mode 100644 gui/src/util/symbols.ts diff --git a/gui/src/App.tsx b/gui/src/App.tsx index 244d811b5..0c3235e03 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -70,9 +70,7 @@ const router = createMemoryRouter([ TODO - look into a more redux-esque way to do this */ function SetupListeners() { - const dispatch = useDispatch(); - - useSetup(dispatch); + useSetup(); return <>; } diff --git a/gui/src/editorInset/EditorInset.tsx b/gui/src/editorInset/EditorInset.tsx index c06661971..9f7393e20 100644 --- a/gui/src/editorInset/EditorInset.tsx +++ b/gui/src/editorInset/EditorInset.tsx @@ -17,7 +17,6 @@ const EditorInsetDiv = styled.div` `; function EditorInset() { - const dispatch = useDispatch(); const availableSlashCommands = useAppSelector( selectSlashCommandComboBoxInputs, ); @@ -25,7 +24,7 @@ function EditorInset() { (store) => store.config.config.contextProviders, ); - useSetup(dispatch); + useSetup(); const elementRef = useRef(null); diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index b828824a8..db89e0428 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -6,14 +6,9 @@ import { AppDispatch } from "../redux/store"; import { streamResponseThunk } from "../redux/thunks/streamResponse"; import { isJetBrains } from "../util"; import { setLocalStorage } from "../util/localStorage"; -import { updateFileSymbolsFromContextItems } from "../util/symbols"; import { useWebviewListener } from "./useWebviewListener"; -import { useAppSelector } from "../redux/hooks"; -import { - selectDefaultModel, - setConfig, - setConfigError, -} from "../redux/slices/configSlice"; +import { useAppDispatch, useAppSelector } from "../redux/hooks"; +import { setConfig, setConfigError } from "../redux/slices/configSlice"; import { updateIndexingStatus } from "../redux/slices/indexingSlice"; import { updateDocsSuggestions } from "../redux/slices/miscSlice"; import { @@ -23,8 +18,10 @@ import { } from "../redux/slices/sessionSlice"; import { setTTSActive } from "../redux/slices/uiSlice"; import useUpdatingRef from "./useUpdatingRef"; +import { updateFileSymbolsFromHistory } from "../redux/thunks/updateFileSymbols"; -function useSetup(dispatch: AppDispatch) { +function useSetup() { + const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); const history = useAppSelector((store) => store.session.history); const defaultModelTitle = useAppSelector( @@ -87,18 +84,11 @@ function useSetup(dispatch: AppDispatch) { // Load symbols for chat on any session change const sessionId = useAppSelector((state) => state.session.id); - const sessionIdRef = useRef(""); - useEffect(() => { - if (sessionIdRef.current !== sessionId) { - updateFileSymbolsFromContextItems( - history.flatMap((item) => item.contextItems), - ideMessenger, - dispatch, - ); + if (sessionId) { + dispatch(updateFileSymbolsFromHistory()); } - sessionIdRef.current = sessionId; - }, [sessionId, history, ideMessenger, dispatch]); + }, [sessionId]); // ON LOAD useEffect(() => { diff --git a/gui/src/redux/thunks/gatherContext.ts b/gui/src/redux/thunks/gatherContext.ts index 596752c8a..3e9202551 100644 --- a/gui/src/redux/thunks/gatherContext.ts +++ b/gui/src/redux/thunks/gatherContext.ts @@ -10,10 +10,10 @@ import { getBasename, getRelativePath } from "core/util"; import resolveEditorContent, { hasSlashCommandOrContextProvider, } from "../../components/mainInput/resolveInput"; -import { updateFileSymbolsFromContextItems } from "../../util/symbols"; import { ThunkApiType } from "../store"; import { selectDefaultModel } from "../slices/configSlice"; import { setIsGatheringContext } from "../slices/sessionSlice"; +import { updateFileSymbolsFromNewContextItems } from "./updateFileSymbols"; export const gatherContext = createAsyncThunk< { @@ -100,11 +100,7 @@ export const gatherContext = createAsyncThunk< } } - await updateFileSymbolsFromContextItems( - selectedContextItems, - extra.ideMessenger, - dispatch, - ); + dispatch(updateFileSymbolsFromNewContextItems(selectedContextItems)); if (promptPreamble) { if (typeof content === "string") { diff --git a/gui/src/redux/thunks/updateFileSymbols.ts b/gui/src/redux/thunks/updateFileSymbols.ts new file mode 100644 index 000000000..a4af91454 --- /dev/null +++ b/gui/src/redux/thunks/updateFileSymbols.ts @@ -0,0 +1,94 @@ +import { createAsyncThunk } from "@reduxjs/toolkit"; +import { ThunkApiType } from "../store"; +import { updateFileSymbols } from "../slices/sessionSlice"; +import { ContextItemWithId } from "core"; + +/* + Get file symbols for given context items + Overwrite symbols for existing file matches +*/ +export const updateFileSymbolsFromNewContextItems = createAsyncThunk< + void, + ContextItemWithId[], + ThunkApiType +>( + "symbols/updateFromContextItems", + async (contextItems, { dispatch, extra, getState }) => { + try { + // Get unique file uris from context items + const uniqueUris = Array.from( + new Set( + contextItems + .filter((item) => item.uri?.type === "file" && item?.uri?.value) + .map((item) => item.uri!.value), + ), + ); + console.log("Updating symbols for files", uniqueUris); + + // Update symbols for those files + if (uniqueUris.length > 0) { + const result = await extra.ideMessenger.request( + "context/getSymbolsForFiles", + { uris: uniqueUris }, + ); + if (result.status === "error") { + throw new Error(result.error); + } + dispatch(updateFileSymbols(result.content)); + } + } catch (e) { + console.error( + "Error updating file symbols from context items", + e, + contextItems, + ); + } + }, +); + +/* + - Update file symbols for all context items in history + - That don't already have symbols +*/ +export const updateFileSymbolsFromHistory = createAsyncThunk< + void, + undefined, + ThunkApiType +>("symbols/updateFromHistory", async (_, { dispatch, extra, getState }) => { + try { + const state = getState(); + + // Get unique context item file uris from all history + const contextItems = state.session.history.flatMap( + (item) => item.contextItems, + ); + const uniqueUris = new Set( + contextItems + .filter((item) => item.uri?.type === "file" && item?.uri?.value) + .map((item) => item.uri!.value), + ); + + // Remove if already in symbols + Object.keys(state.session.symbols).forEach((key) => { + uniqueUris.delete(key); + }); + + const uriArray = Array.from(uniqueUris); + console.log("Updating symbols for files", uriArray); + + // And then update symbols for those files + if (uriArray.length > 0) { + const result = await extra.ideMessenger.request( + "context/getSymbolsForFiles", + { uris: uriArray }, + ); + if (result.status === "error") { + throw new Error(result.error); + } + dispatch(updateFileSymbols(result.content)); + } + } catch (e) { + // Catch all - don't want file symbols to break the chat experience for now + console.error("Error updating file symbols from context items", e); + } +}); diff --git a/gui/src/util/symbols.ts b/gui/src/util/symbols.ts deleted file mode 100644 index a79e3eb2f..000000000 --- a/gui/src/util/symbols.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { ContextItemWithId } from "core"; -import { IIdeMessenger } from "../context/IdeMessenger"; -import { updateFileSymbols } from "../redux/slices/sessionSlice"; -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) { - // Catch all - don't want file symbols to break the chat experience for now - console.error("Error updating file symbols from context items", e); - } -}