Skip to content

Commit bc9dad8

Browse files
author
vbarda
committed
handle new chat gracefully
1 parent 1cdafc6 commit bc9dad8

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

frontend/app/components/ChatWindow.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const MODEL_TYPES = [
4646
"cohere_command",
4747
];
4848

49+
const THREAD_NAME_PLACEHOLDER = "New chat"
50+
4951
const defaultLlmValue =
5052
MODEL_TYPES[Math.floor(Math.random() * MODEL_TYPES.length)];
5153

@@ -105,16 +107,18 @@ export function ChatWindow() {
105107
tags: ["model:" + llm],
106108
};
107109

110+
const getThreadName = (messageValue: string) => (
111+
messageValue.length > 20
112+
? messageValue.slice(0, 20) + "..."
113+
: messageValue
114+
)
115+
108116
const renameThread = async (messageValue: string) => {
109117
// NOTE: we're only setting this on the first message
110118
if (currentThread == null || messages.length > 1) {
111119
return;
112120
}
113-
114-
const threadName =
115-
messageValue.length > 20
116-
? messageValue.slice(0, 20) + "..."
117-
: messageValue;
121+
const threadName = getThreadName(messageValue);
118122
await updateThread(currentThread["thread_id"], threadName);
119123
};
120124

@@ -125,11 +129,17 @@ export function ChatWindow() {
125129
if (isLoading) {
126130
return;
127131
}
128-
if (currentThread == null) {
129-
return;
130-
}
132+
131133
const messageValue = message ?? input;
132134
if (messageValue === "") return;
135+
136+
let thread = currentThread;
137+
if (thread == null) {
138+
const threadName = getThreadName(messageValue);
139+
thread = await createThread(threadName);
140+
insertUrlParam("threadId", thread["thread_id"]);
141+
}
142+
133143
setInput("");
134144
const formattedMessage: Message = {
135145
id: Math.random().toString(),
@@ -164,7 +174,7 @@ export function ChatWindow() {
164174
await renameThread(messageValue);
165175
await startStream(
166176
[formattedMessage],
167-
currentThread["thread_id"],
177+
thread["thread_id"],
168178
assistantId,
169179
config,
170180
);

0 commit comments

Comments
 (0)