insertUserMessageAndSetPending inserts a user message, transitions the chat to pending when needed, and returns the refreshed chat row.
( ctx context.Context, store database.Store, lockedChat database.Chat, modelConfigID uuid.UUID, content pqtype.NullRawMessage, createdBy uuid.UUID, apiKeyID string, )
| 4096 | // insertUserMessageAndSetPending inserts a user message, transitions the |
| 4097 | // chat to pending when needed, and returns the refreshed chat row. |
| 4098 | func insertUserMessageAndSetPending( |
| 4099 | ctx context.Context, |
| 4100 | store database.Store, |
| 4101 | lockedChat database.Chat, |
| 4102 | modelConfigID uuid.UUID, |
| 4103 | content pqtype.NullRawMessage, |
| 4104 | createdBy uuid.UUID, |
| 4105 | apiKeyID string, |
| 4106 | ) (database.ChatMessage, database.Chat, error) { |
| 4107 | msgParams := database.InsertChatMessagesParams{ //nolint:exhaustruct // Fields populated by appendUserChatMessage. |
| 4108 | ChatID: lockedChat.ID, |
| 4109 | } |
| 4110 | insertUserMsg := newUserChatMessage( |
| 4111 | apiKeyID, |
| 4112 | content, |
| 4113 | database.ChatMessageVisibilityBoth, |
| 4114 | modelConfigID, |
| 4115 | chatprompt.CurrentContentVersion, |
| 4116 | ) |
| 4117 | insertUserMsg = insertUserMsg.withCreatedBy(createdBy) |
| 4118 | appendUserChatMessage(&msgParams, insertUserMsg) |
| 4119 | messages, err := insertChatMessageWithStore(ctx, store, msgParams) |
| 4120 | if err != nil { |
| 4121 | return database.ChatMessage{}, database.Chat{}, err |
| 4122 | } |
| 4123 | message := messages[0] |
| 4124 | |
| 4125 | if lockedChat.Status == database.ChatStatusPending { |
| 4126 | if modelConfigID == uuid.Nil || lockedChat.LastModelConfigID == modelConfigID { |
| 4127 | return message, lockedChat, nil |
| 4128 | } |
| 4129 | // The InsertChatMessages CTE updates chats.last_model_config_id when |
| 4130 | // the message's model config differs. Reload to surface that change. |
| 4131 | updatedChat, err := store.GetChatByID(ctx, lockedChat.ID) |
| 4132 | if err != nil { |
| 4133 | return database.ChatMessage{}, database.Chat{}, xerrors.Errorf("get chat after model config update: %w", err) |
| 4134 | } |
| 4135 | return message, updatedChat, nil |
| 4136 | } |
| 4137 | |
| 4138 | updatedChat, err := store.UpdateChatStatus(ctx, database.UpdateChatStatusParams{ |
| 4139 | ID: lockedChat.ID, |
| 4140 | Status: database.ChatStatusPending, |
| 4141 | WorkerID: uuid.NullUUID{}, |
| 4142 | StartedAt: sql.NullTime{}, |
| 4143 | HeartbeatAt: sql.NullTime{}, |
| 4144 | LastError: pqtype.NullRawMessage{}, |
| 4145 | }) |
| 4146 | if err != nil { |
| 4147 | return database.ChatMessage{}, database.Chat{}, xerrors.Errorf("set chat pending: %w", err) |
| 4148 | } |
| 4149 | return message, updatedChat, nil |
| 4150 | } |
| 4151 | |
| 4152 | // shouldQueueUserMessage reports whether a user message should be |
| 4153 | // queued while a chat is active. |
no test coverage detected