From 0a5f9958554c9d9dc90548d2c6d7bcfca0d70ed6 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Sat, 30 Sep 2023 23:46:47 +0200 Subject: [PATCH] Reuse citation numbers --- web/src/pages/index.tsx | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index c04545b..a5bc9a2 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -52,6 +52,7 @@ const Home: NextPage = () => { const [entries, setEntries] = useState([]); const [current, setCurrent] = useState(); const [sessionId, setSessionId] = useState() + const [citations, setCitations] = useState([]) // [state, ready to save to localstorage] const [mode, setMode] = useState<[Mode, boolean]>(["default", false]); @@ -76,15 +77,32 @@ const Home: NextPage = () => { } }; - const updateCitations = (current: CurrentSearch) => { - const citations = Array.from(current.citationsMap.values()); - if (citations.some(c => !c.index)) { - let index = 1; - citations.forEach((c) => {c.index = index++}); - setCurrent(current) - } + + const updateCitations = (allCitations: Citation[], current: CurrentSearch) => { + const entryCitations = Array.from(current.citationsMap.values()); + if (!entryCitations.some(c => !c.index)) { + // All of the entries citations have indexes, so there weren't any changes since the last check + return } + // Get a mapping of all known citations, so as to reuse them if they appear again + const citationsMapping = Object.fromEntries(allCitations.map(c => ([c.title + c.url, c.index]))); + + entryCitations.forEach( + (c) => { + const hash = c.title + c.url; + if (!citationsMapping[hash]) { + c.index = allCitations.length + 1; + allCitations.push(c); + } else { + c.index = citationsMapping[hash]; + } + } + ) + setCitations(allCitations) + setCurrent(current) + } + const search = async ( query: string, query_source: "search" | "followups", @@ -127,7 +145,7 @@ const Home: NextPage = () => { last_entry =

Loading: Waiting for LLM...

; break; case "streaming": - updateCitations(current) + updateCitations(citations, current) last_entry = ; break; case "followups":