coyotte508 HF staff commited on
Commit
831f161
1 Parent(s): dea9425

♻️ Better invalidation mechanism (#72)

Browse files
src/lib/types/UrlDependency.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ /* eslint-disable no-shadow */
2
+ export enum UrlDependency {
3
+ ConversationList = "conversation:list",
4
+ }
src/routes/+layout.server.ts CHANGED
@@ -1,14 +1,17 @@
1
  import type { LayoutServerLoad } from "./$types";
2
  import { collections } from "$lib/server/database";
3
  import type { Conversation } from "$lib/types/Conversation";
 
4
 
5
- export const load: LayoutServerLoad = async (event) => {
6
  const { conversations } = collections;
7
 
 
 
8
  return {
9
  conversations: await conversations
10
  .find({
11
- sessionId: event.locals.sessionId,
12
  })
13
  .sort({ updatedAt: -1 })
14
  .project<Pick<Conversation, "title" | "_id" | "updatedAt" | "createdAt">>({
 
1
  import type { LayoutServerLoad } from "./$types";
2
  import { collections } from "$lib/server/database";
3
  import type { Conversation } from "$lib/types/Conversation";
4
+ import { UrlDependency } from "$lib/types/UrlDependency";
5
 
6
+ export const load: LayoutServerLoad = async ({ locals, depends }) => {
7
  const { conversations } = collections;
8
 
9
+ depends(UrlDependency.ConversationList);
10
+
11
  return {
12
  conversations: await conversations
13
  .find({
14
+ sessionId: locals.sessionId,
15
  })
16
  .sort({ updatedAt: -1 })
17
  .project<Pick<Conversation, "title" | "_id" | "updatedAt" | "createdAt">>({
src/routes/+layout.svelte CHANGED
@@ -1,5 +1,5 @@
1
  <script lang="ts">
2
- import { goto, invalidateAll } from "$app/navigation";
3
  import { page } from "$app/stores";
4
  import "../styles/main.css";
5
  import type { LayoutData } from "./$types";
@@ -7,6 +7,7 @@
7
  import CarbonTrashCan from "~icons/carbon/trash-can";
8
  import CarbonExport from "~icons/carbon/export";
9
  import { base } from "$app/paths";
 
10
 
11
  export let data: LayoutData;
12
 
@@ -67,7 +68,7 @@
67
  }
68
 
69
  if ($page.params.id !== id) {
70
- await invalidateAll();
71
  } else {
72
  await goto(base || "/", { invalidateAll: true });
73
  }
 
1
  <script lang="ts">
2
+ import { goto, invalidate } from "$app/navigation";
3
  import { page } from "$app/stores";
4
  import "../styles/main.css";
5
  import type { LayoutData } from "./$types";
 
7
  import CarbonTrashCan from "~icons/carbon/trash-can";
8
  import CarbonExport from "~icons/carbon/export";
9
  import { base } from "$app/paths";
10
+ import { UrlDependency } from "$lib/types/UrlDependency";
11
 
12
  export let data: LayoutData;
13
 
 
68
  }
69
 
70
  if ($page.params.id !== id) {
71
+ await invalidate(UrlDependency.ConversationList);
72
  } else {
73
  await goto(base || "/", { invalidateAll: true });
74
  }
src/routes/conversation/[id]/+page.svelte CHANGED
@@ -10,10 +10,18 @@
10
  import { trimSuffix } from "$lib/utils/trimSuffix";
11
  import { PUBLIC_SEP_TOKEN } from "$env/static/public";
12
  import { trimPrefix } from "$lib/utils/trimPrefix";
 
13
 
14
  export let data: PageData;
15
 
16
- $: messages = data.messages;
 
 
 
 
 
 
 
17
 
18
  const hf = new HfInference();
19
 
@@ -78,15 +86,9 @@
78
  }
79
 
80
  async function summarizeTitle(id: string) {
81
- const response = await fetch(`${base}/conversation/${id}/summarize`, {
82
  method: "POST",
83
  });
84
- if (response.ok) {
85
- /// TODO(actually invalidate)
86
- await invalidate("/");
87
- await invalidate((url) => url.pathname === "/" || url.pathname === base);
88
- location.reload();
89
- }
90
  }
91
 
92
  async function writeMessage(message: string) {
@@ -101,11 +103,12 @@
101
  await getTextGenerationStream(message);
102
 
103
  if (messages.filter((m) => m.from === "user").length === 1) {
104
- summarizeTitle($page.params.id).catch(console.error);
 
 
 
 
105
  }
106
-
107
- // Reload conversation order - doesn't seem to work
108
- // await invalidate('/');
109
  } finally {
110
  loading = false;
111
  }
 
10
  import { trimSuffix } from "$lib/utils/trimSuffix";
11
  import { PUBLIC_SEP_TOKEN } from "$env/static/public";
12
  import { trimPrefix } from "$lib/utils/trimPrefix";
13
+ import { UrlDependency } from "$lib/types/UrlDependency";
14
 
15
  export let data: PageData;
16
 
17
+ let messages = data.messages;
18
+ let lastLoadedMessages = data.messages;
19
+
20
+ // Since we modify the messages array locally, we don't want to reset it if an old version is passed
21
+ $: if (data.messages !== lastLoadedMessages) {
22
+ messages = data.messages;
23
+ lastLoadedMessages = data.messages;
24
+ }
25
 
26
  const hf = new HfInference();
27
 
 
86
  }
87
 
88
  async function summarizeTitle(id: string) {
89
+ await fetch(`${base}/conversation/${id}/summarize`, {
90
  method: "POST",
91
  });
 
 
 
 
 
 
92
  }
93
 
94
  async function writeMessage(message: string) {
 
103
  await getTextGenerationStream(message);
104
 
105
  if (messages.filter((m) => m.from === "user").length === 1) {
106
+ summarizeTitle($page.params.id)
107
+ .then(() => invalidate(UrlDependency.ConversationList))
108
+ .catch(console.error);
109
+ } else {
110
+ await invalidate(UrlDependency.ConversationList);
111
  }
 
 
 
112
  } finally {
113
  loading = false;
114
  }