Skip to content

Commit

Permalink
handle multiple cancels
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Nov 5, 2023
1 parent 74b47a8 commit 0ab399a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
7 changes: 3 additions & 4 deletions web/src/components/assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ export const AssistantEntry: React.FC<{ entry: AssistantType }> = ({
<CitationsBlock
key={i}
text={paragraph}
citations={entry.citationsMap}
citations={entry.citationsMap || new Map()}
textRenderer={(t) => <GlossarySpan content={t} />}
/>
))}
<ul className="mt-5">
{
{entry.citationsMap &&
// show citations
Array.from(entry.citationsMap.values()).map((citation) => (
<li key={citation.index}>
<ShowCitation citation={citation} />
</li>
))
}
))}
</ul>
</div>
);
26 changes: 14 additions & 12 deletions web/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ type ChatParams = {
const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => {
const [entries, setEntries] = useState<Entry[]>([]);

const [query, setQuery] = useState(randomQuestion());
const [query, setQuery] = useState(() => randomQuestion());
const [current, setCurrent] = useState<CurrentSearch>();
const [followups, setFollowups] = useState<Followup[]>([]);
const [controller, setController] = useState(new AbortController());
const [controller, setController] = useState(() => new AbortController());
const { citations, setEntryCitations } = useCitations();

const updateCurrent = (current: CurrentSearch) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -209,12 +214,9 @@ const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => {
)
)}

<Followups
followups={followups}
onClick={withController(fetchFollowup)}
/>
<Followups followups={followups} onClick={abortable(fetchFollowup)} />
<SearchBox
search={withController(search)}
search={abortable(search)}
query={query}
onQuery={(v: string) => {
setQuery(v);
Expand Down
4 changes: 2 additions & 2 deletions web/src/pages/semantic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<SemanticEntry[]>([]);

const semantic_search = async (query: string) => {
Expand Down
2 changes: 1 addition & 1 deletion web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type AssistantEntry = {
role: "assistant";
content: string;
citations?: Citation[];
citationsMap: Map<string, Citation>;
citationsMap?: Map<string, Citation>;
deleted?: boolean;
};

Expand Down

0 comments on commit 0ab399a

Please sign in to comment.