nsarrazin commited on
Commit
c4447f2
·
1 Parent(s): 373b19a

fix: stop button working properly

Browse files
src/lib/server/abortedGenerations.ts CHANGED
@@ -7,11 +7,13 @@ import { onExit } from "./exitHandler";
7
  export class AbortedGenerations {
8
  private static instance: AbortedGenerations;
9
 
10
- private abortedGenerations: Map<string, Date> = new Map();
11
 
12
  private constructor() {
13
- const interval = setInterval(this.updateList, 1000);
14
  onExit(() => clearInterval(interval));
 
 
15
  }
16
 
17
  public static getInstance(): AbortedGenerations {
@@ -22,16 +24,16 @@ export class AbortedGenerations {
22
  return AbortedGenerations.instance;
23
  }
24
 
25
- public getList(): Map<string, Date> {
26
- return this.abortedGenerations;
27
  }
28
 
29
  private async updateList() {
30
  try {
31
  const aborts = await collections.abortedGenerations.find({}).sort({ createdAt: 1 }).toArray();
32
 
33
- this.abortedGenerations = new Map(
34
- aborts.map(({ conversationId, createdAt }) => [conversationId.toString(), createdAt])
35
  );
36
  } catch (err) {
37
  logger.error(err);
 
7
  export class AbortedGenerations {
8
  private static instance: AbortedGenerations;
9
 
10
+ private abortedGenerations: Record<string, Date> = {};
11
 
12
  private constructor() {
13
+ const interval = setInterval(() => this.updateList(), 1000);
14
  onExit(() => clearInterval(interval));
15
+
16
+ this.updateList();
17
  }
18
 
19
  public static getInstance(): AbortedGenerations {
 
24
  return AbortedGenerations.instance;
25
  }
26
 
27
+ public getAbortTime(conversationId: string): Date | undefined {
28
+ return this.abortedGenerations[conversationId];
29
  }
30
 
31
  private async updateList() {
32
  try {
33
  const aborts = await collections.abortedGenerations.find({}).sort({ createdAt: 1 }).toArray();
34
 
35
+ this.abortedGenerations = Object.fromEntries(
36
+ aborts.map((abort) => [abort.conversationId.toString(), abort.createdAt])
37
  );
38
  } catch (err) {
39
  logger.error(err);
src/lib/server/textGeneration/generate.ts CHANGED
@@ -193,8 +193,12 @@ Do not use prefixes such as Response: or Answer: when answering to the user.`,
193
  }
194
 
195
  // abort check
196
- const date = AbortedGenerations.getInstance().getList().get(conv._id.toString());
197
- if (date && date > promptedAt) break;
 
 
 
 
198
 
199
  // no output check
200
  if (!output) break;
 
193
  }
194
 
195
  // abort check
196
+ const date = AbortedGenerations.getInstance().getAbortTime(conv._id.toString());
197
+
198
+ if (date && date > promptedAt) {
199
+ logger.info(`Aborting generation for conversation ${conv._id}`);
200
+ break;
201
+ }
202
 
203
  // no output check
204
  if (!output) break;
src/routes/conversation/[id]/+page.svelte CHANGED
@@ -520,7 +520,21 @@
520
  on:showAlternateMsg={onShowAlternateMsg}
521
  on:vote={(event) => voteMessage(event.detail.score, event.detail.id)}
522
  on:share={() => shareConversation(page.params.id, data.title)}
523
- on:stop={() => (($isAborted = true), (loading = false))}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
  models={data.models}
525
  currentModel={findCurrentModel([...data.models, ...data.oldModels], data.model)}
526
  assistant={data.assistant}
 
520
  on:showAlternateMsg={onShowAlternateMsg}
521
  on:vote={(event) => voteMessage(event.detail.score, event.detail.id)}
522
  on:share={() => shareConversation(page.params.id, data.title)}
523
+ on:stop={async () => {
524
+ await fetch(`${base}/conversation/${page.params.id}/stop-generating`, {
525
+ method: "POST",
526
+ }).then((r) => {
527
+ if (r.ok) {
528
+ setTimeout(() => {
529
+ $isAborted = true;
530
+ loading = false;
531
+ }, 3000);
532
+ } else {
533
+ $isAborted = true;
534
+ loading = false;
535
+ }
536
+ });
537
+ }}
538
  models={data.models}
539
  currentModel={findCurrentModel([...data.models, ...data.oldModels], data.model)}
540
  assistant={data.assistant}