nsarrazin HF staff commited on
Commit
f249cfc
1 Parent(s): a3b5894

Title update occur when available (#512)

Browse files

* wip: rename happens realtime

* Make title change happen when available

* remove debug log

* rm commented out code

* lint

* replace queue by single value

* remove .js

* reset initial pending message after writing

src/lib/stores/titleUpdate.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import { writable } from "svelte/store";
2
+
3
+ export interface TitleUpdate {
4
+ convId: string;
5
+ title: string;
6
+ }
7
+
8
+ export default writable<TitleUpdate | null>(null);
src/lib/types/MessageUpdate.ts CHANGED
@@ -27,7 +27,7 @@ export type WebSearchUpdate = {
27
 
28
  export type StatusUpdate = {
29
  type: "status";
30
- status: "started" | "pending" | "finished" | "error";
31
  message?: string;
32
  };
33
 
 
27
 
28
  export type StatusUpdate = {
29
  type: "status";
30
+ status: "started" | "pending" | "finished" | "error" | "title";
31
  message?: string;
32
  };
33
 
src/routes/+layout.svelte CHANGED
@@ -16,6 +16,7 @@
16
  import SettingsModal from "$lib/components/SettingsModal.svelte";
17
  import LoginModal from "$lib/components/LoginModal.svelte";
18
  import { PUBLIC_APP_ASSETS, PUBLIC_APP_NAME } from "$env/static/public";
 
19
 
20
  export let data;
21
 
@@ -99,6 +100,18 @@
99
  (data.requiresLogin
100
  ? !data.user
101
  : !data.settings.ethicsModalAcceptedAt && !!PUBLIC_APP_DISCLAIMER);
 
 
 
 
 
 
 
 
 
 
 
 
102
  </script>
103
 
104
  <svelte:head>
 
16
  import SettingsModal from "$lib/components/SettingsModal.svelte";
17
  import LoginModal from "$lib/components/LoginModal.svelte";
18
  import { PUBLIC_APP_ASSETS, PUBLIC_APP_NAME } from "$env/static/public";
19
+ import titleUpdate from "$lib/stores/titleUpdate";
20
 
21
  export let data;
22
 
 
100
  (data.requiresLogin
101
  ? !data.user
102
  : !data.settings.ethicsModalAcceptedAt && !!PUBLIC_APP_DISCLAIMER);
103
+
104
+ $: if ($titleUpdate) {
105
+ const convIdx = data.conversations.findIndex(({ id }) => id === $titleUpdate?.convId);
106
+
107
+ if (convIdx != -1) {
108
+ data.conversations[convIdx].title = $titleUpdate?.title ?? data.conversations[convIdx].title;
109
+ }
110
+ // update data.conversations
111
+ data.conversations = [...data.conversations];
112
+
113
+ $titleUpdate = null;
114
+ }
115
  </script>
116
 
117
  <svelte:head>
src/routes/conversation/+server.ts CHANGED
@@ -6,7 +6,6 @@ import { base } from "$app/paths";
6
  import { z } from "zod";
7
  import type { Message } from "$lib/types/Message";
8
  import { models, validateModel } from "$lib/server/models";
9
- import { authCondition } from "$lib/server/auth";
10
 
11
  export const POST: RequestHandler = async ({ locals, request }) => {
12
  const body = await request.text();
@@ -43,9 +42,7 @@ export const POST: RequestHandler = async ({ locals, request }) => {
43
 
44
  const res = await collections.conversations.insertOne({
45
  _id: new ObjectId(),
46
- title:
47
- title ||
48
- "Untitled " + ((await collections.conversations.countDocuments(authCondition(locals))) + 1),
49
  messages,
50
  model: values.model,
51
  preprompt: preprompt === model?.preprompt ? undefined : preprompt,
 
6
  import { z } from "zod";
7
  import type { Message } from "$lib/types/Message";
8
  import { models, validateModel } from "$lib/server/models";
 
9
 
10
  export const POST: RequestHandler = async ({ locals, request }) => {
11
  const body = await request.text();
 
42
 
43
  const res = await collections.conversations.insertOne({
44
  _id: new ObjectId(),
45
+ title: title || "New Chat",
 
 
46
  messages,
47
  model: values.model,
48
  preprompt: preprompt === model?.preprompt ? undefined : preprompt,
src/routes/conversation/[id]/+page.svelte CHANGED
@@ -14,6 +14,7 @@
14
  import type { Message } from "$lib/types/Message";
15
  import { PUBLIC_APP_DISCLAIMER } from "$env/static/public";
16
  import type { MessageUpdate, WebSearchUpdate } from "$lib/types/MessageUpdate";
 
17
 
18
  export let data;
19
 
@@ -138,9 +139,9 @@
138
 
139
  // if it's not done we parse the value, which contains all messages
140
  const inputs = value.split("\n");
141
- inputs.forEach((el: string) => {
142
  try {
143
- let update = JSON.parse(el) as MessageUpdate;
144
  if (update.type === "finalAnswer") {
145
  finalAnswer = update.text;
146
  reader.cancel();
@@ -161,6 +162,18 @@
161
  }
162
  } else if (update.type === "webSearch") {
163
  webSearchMessages = [...webSearchMessages, update];
 
 
 
 
 
 
 
 
 
 
 
 
164
  }
165
  } catch (parseError) {
166
  // in case of parsing error we wait for the next message
@@ -220,7 +233,8 @@
220
  onMount(async () => {
221
  // only used in case of creating new conversations (from the parent POST endpoint)
222
  if ($pendingMessage) {
223
- writeMessage($pendingMessage);
 
224
  }
225
  });
226
 
 
14
  import type { Message } from "$lib/types/Message";
15
  import { PUBLIC_APP_DISCLAIMER } from "$env/static/public";
16
  import type { MessageUpdate, WebSearchUpdate } from "$lib/types/MessageUpdate";
17
+ import titleUpdate from "$lib/stores/titleUpdate";
18
 
19
  export let data;
20
 
 
139
 
140
  // if it's not done we parse the value, which contains all messages
141
  const inputs = value.split("\n");
142
+ inputs.forEach(async (el: string) => {
143
  try {
144
+ const update = JSON.parse(el) as MessageUpdate;
145
  if (update.type === "finalAnswer") {
146
  finalAnswer = update.text;
147
  reader.cancel();
 
162
  }
163
  } else if (update.type === "webSearch") {
164
  webSearchMessages = [...webSearchMessages, update];
165
+ } else if (update.type === "status") {
166
+ if (update.status === "title" && update.message) {
167
+ const conv = data.conversations.find(({ id }) => id === $page.params.id);
168
+ if (conv) {
169
+ conv.title = update.message;
170
+
171
+ $titleUpdate = {
172
+ title: update.message,
173
+ convId: $page.params.id,
174
+ };
175
+ }
176
+ }
177
  }
178
  } catch (parseError) {
179
  // in case of parsing error we wait for the next message
 
233
  onMount(async () => {
234
  // only used in case of creating new conversations (from the parent POST endpoint)
235
  if ($pendingMessage) {
236
+ await writeMessage($pendingMessage);
237
+ $pendingMessage = "";
238
  }
239
  });
240
 
src/routes/conversation/[id]/+server.ts CHANGED
@@ -121,14 +121,6 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
121
  ];
122
  })() satisfies Message[];
123
 
124
- if (conv.title.startsWith("Untitled")) {
125
- try {
126
- conv.title = (await summarize(newPrompt)) ?? conv.title;
127
- } catch (e) {
128
- console.error(e);
129
- }
130
- }
131
-
132
  await collections.conversations.updateOne(
133
  {
134
  _id: convId,
@@ -156,6 +148,28 @@ export async function POST({ request, fetch, locals, params, getClientAddress })
156
 
157
  update({ type: "status", status: "started" });
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  let webSearchResults: WebSearch | undefined;
160
 
161
  if (webSearch) {
 
121
  ];
122
  })() satisfies Message[];
123
 
 
 
 
 
 
 
 
 
124
  await collections.conversations.updateOne(
125
  {
126
  _id: convId,
 
148
 
149
  update({ type: "status", status: "started" });
150
 
151
+ if (conv.title === "New Chat" && messages.length === 1) {
152
+ try {
153
+ conv.title = (await summarize(newPrompt)) ?? conv.title;
154
+ update({ type: "status", status: "title", message: conv.title });
155
+ } catch (e) {
156
+ console.error(e);
157
+ }
158
+ }
159
+
160
+ await collections.conversations.updateOne(
161
+ {
162
+ _id: convId,
163
+ },
164
+ {
165
+ $set: {
166
+ messages,
167
+ title: conv.title,
168
+ updatedAt: new Date(),
169
+ },
170
+ }
171
+ );
172
+
173
  let webSearchResults: WebSearch | undefined;
174
 
175
  if (webSearch) {
src/routes/conversation/[id]/share/+server.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { authCondition } from "$lib/server/auth";
2
  import { collections } from "$lib/server/database";
3
  import type { SharedConversation } from "$lib/types/SharedConversation";
4
- import { getShareUrl } from "$lib/utils/getShareUrl.js";
5
  import { hashConv } from "$lib/utils/hashConv";
6
  import { error } from "@sveltejs/kit";
7
  import { ObjectId } from "mongodb";
 
1
  import { authCondition } from "$lib/server/auth";
2
  import { collections } from "$lib/server/database";
3
  import type { SharedConversation } from "$lib/types/SharedConversation";
4
+ import { getShareUrl } from "$lib/utils/getShareUrl";
5
  import { hashConv } from "$lib/utils/hashConv";
6
  import { error } from "@sveltejs/kit";
7
  import { ObjectId } from "mongodb";