Skip to content

Commit 54a20db

Browse files
committed
fix(coderd): avoid serializing null message content in postChatMessages
1 parent c35f43a commit 54a20db

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

coderd/chat.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -316,30 +316,35 @@ You are running as an agent - please keep going until the user's query is comple
316316
return
317317
}
318318

319-
raw, err := json.Marshal(acc.Messages())
320-
if err != nil {
321-
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
322-
Message: "Failed to marshal chat message",
323-
Detail: err.Error(),
324-
})
325-
return
326-
}
327-
messages = append(messages, acc.Messages()...)
319+
// acc.Messages() may sometimes return nil. Serializing this
320+
// will cause a pq error: "cannot extract elements from a scalar".
321+
newMessages := append([]aisdk.Message{}, acc.Messages()...)
322+
if len(newMessages) > 0 {
323+
raw, err := json.Marshal(newMessages)
324+
if err != nil {
325+
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
326+
Message: "Failed to marshal chat message",
327+
Detail: err.Error(),
328+
})
329+
return
330+
}
331+
messages = append(messages, newMessages...)
328332

329-
// Insert these messages into the database!
330-
_, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{
331-
ChatID: chat.ID,
332-
CreatedAt: dbtime.Now(),
333-
Model: req.Model,
334-
Provider: provider.Provider,
335-
Content: raw,
336-
})
337-
if err != nil {
338-
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
339-
Message: "Failed to insert chat messages",
340-
Detail: err.Error(),
333+
// Insert these messages into the database!
334+
_, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{
335+
ChatID: chat.ID,
336+
CreatedAt: dbtime.Now(),
337+
Model: req.Model,
338+
Provider: provider.Provider,
339+
Content: raw,
341340
})
342-
return
341+
if err != nil {
342+
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
343+
Message: "Failed to insert chat messages",
344+
Detail: err.Error(),
345+
})
346+
return
347+
}
343348
}
344349

345350
if acc.FinishReason() == aisdk.FinishReasonToolCalls {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy