move symbol updates to thunks
This commit is contained in:
parent
c8a7bf90fb
commit
3ab54c71d2
6 changed files with 106 additions and 64 deletions
|
|
@ -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 <></>;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<HTMLDivElement | null>(null);
|
||||
|
||||
|
|
|
|||
|
|
@ -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(() => {
|
||||
|
|
|
|||
|
|
@ -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") {
|
||||
|
|
|
|||
94
gui/src/redux/thunks/updateFileSymbols.ts
Normal file
94
gui/src/redux/thunks/updateFileSymbols.ts
Normal file
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue