@@ -46,6 +46,8 @@ const MODEL_TYPES = [
46
46
"cohere_command" ,
47
47
] ;
48
48
49
+ const THREAD_NAME_PLACEHOLDER = "New chat"
50
+
49
51
const defaultLlmValue =
50
52
MODEL_TYPES [ Math . floor ( Math . random ( ) * MODEL_TYPES . length ) ] ;
51
53
@@ -105,16 +107,18 @@ export function ChatWindow() {
105
107
tags : [ "model:" + llm ] ,
106
108
} ;
107
109
110
+ const getThreadName = ( messageValue : string ) => (
111
+ messageValue . length > 20
112
+ ? messageValue . slice ( 0 , 20 ) + "..."
113
+ : messageValue
114
+ )
115
+
108
116
const renameThread = async ( messageValue : string ) => {
109
117
// NOTE: we're only setting this on the first message
110
118
if ( currentThread == null || messages . length > 1 ) {
111
119
return ;
112
120
}
113
-
114
- const threadName =
115
- messageValue . length > 20
116
- ? messageValue . slice ( 0 , 20 ) + "..."
117
- : messageValue ;
121
+ const threadName = getThreadName ( messageValue ) ;
118
122
await updateThread ( currentThread [ "thread_id" ] , threadName ) ;
119
123
} ;
120
124
@@ -125,11 +129,17 @@ export function ChatWindow() {
125
129
if ( isLoading ) {
126
130
return ;
127
131
}
128
- if ( currentThread == null ) {
129
- return ;
130
- }
132
+
131
133
const messageValue = message ?? input ;
132
134
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
+
133
143
setInput ( "" ) ;
134
144
const formattedMessage : Message = {
135
145
id : Math . random ( ) . toString ( ) ,
@@ -164,7 +174,7 @@ export function ChatWindow() {
164
174
await renameThread ( messageValue ) ;
165
175
await startStream (
166
176
[ formattedMessage ] ,
167
- currentThread [ "thread_id" ] ,
177
+ thread [ "thread_id" ] ,
168
178
assistantId ,
169
179
config ,
170
180
) ;
0 commit comments