From 0ab399a0a03c2ab6577940f48edad163aa7cfc3a Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Sun, 5 Nov 2023 19:24:52 +0100 Subject: [PATCH] handle multiple cancels --- web/src/components/assistant.tsx | 7 +++---- web/src/components/chat.tsx | 26 ++++++++++++++------------ web/src/pages/semantic.tsx | 4 ++-- web/src/types.ts | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/web/src/components/assistant.tsx b/web/src/components/assistant.tsx index 5a5776a..5405656 100644 --- a/web/src/components/assistant.tsx +++ b/web/src/components/assistant.tsx @@ -11,19 +11,18 @@ export const AssistantEntry: React.FC<{ entry: AssistantType }> = ({ } /> ))}
    - { + {entry.citationsMap && // show citations Array.from(entry.citationsMap.values()).map((citation) => (
  • - )) - } + ))}
); diff --git a/web/src/components/chat.tsx b/web/src/components/chat.tsx index 383bad5..8038c32 100644 --- a/web/src/components/chat.tsx +++ b/web/src/components/chat.tsx @@ -109,10 +109,10 @@ type ChatParams = { const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => { const [entries, setEntries] = useState([]); - const [query, setQuery] = useState(randomQuestion()); + const [query, setQuery] = useState(() => randomQuestion()); const [current, setCurrent] = useState(); const [followups, setFollowups] = useState([]); - const [controller, setController] = useState(new AbortController()); + const [controller, setController] = useState(() => new AbortController()); const { citations, setEntryCitations } = useCitations(); const updateCurrent = (current: CurrentSearch) => { @@ -138,12 +138,13 @@ const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => { scroll30(); }; - const withController = + const abortable = (f: any) => (...args: any) => { - const controller = new AbortController(); - setController(controller); - return f(controller, ...args); + controller.abort(); + const newController = new AbortController(); + setController(newController); + return f(newController, ...args); }; const search = async (controller: AbortController, query: string) => { @@ -170,8 +171,12 @@ const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => { controller: AbortController, followup: Followup ) => { + setCurrent({ role: "assistant", content: "", phase: "started" }); const result = await getStampyContent(followup.pageid, controller); - addResult(followup.text, result); + if (!controller.signal.aborted) { + addResult(followup.text, result); + } + setCurrent(undefined); }; const deleteEntry = (i: number) => { @@ -209,12 +214,9 @@ const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => { ) )} - + { setQuery(v); diff --git a/web/src/pages/semantic.tsx b/web/src/pages/semantic.tsx index af98fc0..e792227 100644 --- a/web/src/pages/semantic.tsx +++ b/web/src/pages/semantic.tsx @@ -15,8 +15,8 @@ const ignoreAbort = (error: Error) => { }; const Semantic: NextPage = () => { - const [query, setQuery] = useState(randomQuestion()); - const [controller, setController] = useState(new AbortController()); + const [query, setQuery] = useState(() => randomQuestion()); + const [controller, setController] = useState(() => new AbortController()); const [results, setResults] = useState([]); const semantic_search = async (query: string) => { diff --git a/web/src/types.ts b/web/src/types.ts index 537d40c..6e24438 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -24,7 +24,7 @@ export type AssistantEntry = { role: "assistant"; content: string; citations?: Citation[]; - citationsMap: Map; + citationsMap?: Map; deleted?: boolean; };