@@ -14,6 +14,7 @@ import (
14
14
"github.com/coder/coder/v2/coderd/database/dbtime"
15
15
"github.com/coder/coder/v2/coderd/httpapi"
16
16
"github.com/coder/coder/v2/coderd/httpmw"
17
+ "github.com/coder/coder/v2/coderd/util/strings"
17
18
"github.com/coder/coder/v2/codersdk"
18
19
"github.com/coder/coder/v2/codersdk/toolsdk"
19
20
)
@@ -224,10 +225,19 @@ func (api *API) postChatMessages(w http.ResponseWriter, r *http.Request) {
224
225
})
225
226
return
226
227
}
228
+ var newTitle string
229
+ accMessages := acc .Messages ()
230
+ // If for some reason the stream didn't return any messages, use the
231
+ // original message as the title.
232
+ if len (accMessages )== 0 {
233
+ newTitle = strings .Truncate (messages [0 ].Content ,40 )
234
+ }else {
235
+ newTitle = strings .Truncate (accMessages [0 ].Content ,40 )
236
+ }
227
237
err = api .Database .UpdateChatByID (ctx , database.UpdateChatByIDParams {
228
238
ID :chat .ID ,
229
- Title :acc . Messages ()[ 0 ]. Content ,
230
- UpdatedAt :time .Now (),
239
+ Title :newTitle ,
240
+ UpdatedAt :dbtime .Now (),
231
241
})
232
242
if err != nil {
233
243
httpapi .Write (ctx ,w ,http .StatusInternalServerError , codersdk.Response {