8000 fix(coderd): chat: avoid panic if no messages are accumulated · coder/coder@05fa48d · GitHub
[go: up one dir, main page]

Skip to content

Commit 05fa48d

Browse files
committed
fix(coderd): chat: avoid panic if no messages are accumulated
1 parent ccefabe commit 05fa48d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

coderd/chat.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/coder/coder/v2/coderd/database/dbtime"
1515
"github.com/coder/coder/v2/coderd/httpapi"
1616
"github.com/coder/coder/v2/coderd/httpmw"
17+
"github.com/coder/coder/v2/coderd/util/strings"
1718
"github.com/coder/coder/v2/codersdk"
1819
"github.com/coder/coder/v2/codersdk/toolsdk"
1920
)
@@ -224,10 +225,19 @@ func (api *API) postChatMessages(w http.ResponseWriter, r *http.Request) {
224225
})
225226
return
226227
}
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+
}
227237
err = api.Database.UpdateChatByID(ctx, database.UpdateChatByIDParams{
228238
ID: chat.ID,
229-
Title: acc.Messages()[0].Content,
230-
UpdatedAt: time.Now(),
239+
Title: newTitle,
240+
UpdatedAt: dbtime.Now(),
231241
})
232242
if err != nil {
233243
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{

0 commit comments

Comments
 (0)
0