From b986a1ff1702cfb3612f27ceebef180aa6064722 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 9 Dec 2024 01:36:38 -0800 Subject: [PATCH] symbols from previous context items --- .../markdown/StyledMarkdownPreview.tsx | 54 ++++++++++++------- gui/src/redux/thunks/updateFileSymbols.ts | 2 + 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 46a52a97a..8b10aef81 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -1,6 +1,6 @@ import { SymbolWithRange } from "core"; import { ctxItemToRifWithContents } from "core/commands/util"; -import { memo, useEffect, useRef } from "react"; +import { memo, useEffect, useMemo, useRef } from "react"; import { useRemark } from "react-remark"; import rehypeHighlight, { Options } from "rehype-highlight"; import rehypeKatex from "rehype-katex"; @@ -160,20 +160,35 @@ 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 contextItems = useAppSelector( - (state) => state.session.history[props.itemIndex - 1]?.contextItems, - ); - const contextItemsRef = useUpdatingRef(contextItems); - const symbols = useAppSelector((state) => state.session.symbols); + // Grab context items that are this one or further back + const history = useAppSelector((state) => state.session.history); + const previousFileContextItems = useMemo(() => { + const index = props.itemIndex; + if (index === undefined) { + return []; + } + const previousItems = history.flatMap((item, i) => + i <= index ? item.contextItems : [], + ); + return previousItems.filter( + (item) => item.uri?.type === "file" && item?.uri?.value, + ); + }, [props.itemIndex, history]); + const previousFileContextItemsRef = useUpdatingRef(previousFileContextItems); - const symbolsRef = useRef([]); - 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]); + // Extract global symbols for files matching previous context items + const allSymbols = useAppSelector((state) => state.session.symbols); + const previousFileContextItemSymbols = useMemo(() => { + const uniqueUris = new Set( + previousFileContextItems.map((item) => item.uri!.value!), + ); + return Object.entries(allSymbols) + .filter((e) => uniqueUris.has(e[0])) + .map((f) => f[1]) + .flat(); + }, [allSymbols, previousFileContextItems]); + const symbolsRef = useUpdatingRef(previousFileContextItemSymbols); const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ @@ -265,18 +280,17 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( code: ({ node, ...codeProps }) => { const content = getCodeChildrenContent(codeProps.children); - if (content && contextItemsRef.current) { - const ctxItem = contextItemsRef.current.find( - (ctxItem) => - ctxItem.uri?.value.includes(content) && - ctxItem.uri.type === "file", + if (content && previousFileContextItemsRef.current) { + // Insert file links for matching previous context items + const ctxItem = previousFileContextItemsRef.current.find((item) => + item.uri!.value!.includes(content), ); - if (ctxItem) { const rif = ctxItemToRifWithContents(ctxItem); return ; } + // Insert symbols for exact matches const exactSymbol = symbolsRef.current.find( (s) => s.name === content, ); @@ -284,7 +298,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( return ; } - // PARTIAL - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` + // Partial matches - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` const partialSymbol = symbolsRef.current.find((s) => content.startsWith(s.name), ); diff --git a/gui/src/redux/thunks/updateFileSymbols.ts b/gui/src/redux/thunks/updateFileSymbols.ts index a4af91454..c3fa38224 100644 --- a/gui/src/redux/thunks/updateFileSymbols.ts +++ b/gui/src/redux/thunks/updateFileSymbols.ts @@ -34,6 +34,7 @@ export const updateFileSymbolsFromNewContextItems = createAsyncThunk< if (result.status === "error") { throw new Error(result.error); } + console.log("Got symbols", result.content); dispatch(updateFileSymbols(result.content)); } } catch (e) { @@ -85,6 +86,7 @@ export const updateFileSymbolsFromHistory = createAsyncThunk< if (result.status === "error") { throw new Error(result.error); } + console.log("Got symbols", result.content); dispatch(updateFileSymbols(result.content)); } } catch (e) {