EXPERIMENTAL: this endpoint is experimental and is subject to change.
(rw http.ResponseWriter, r *http.Request)
| 3325 | |
| 3326 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 3327 | func (api *API) promoteChatQueuedMessage(rw http.ResponseWriter, r *http.Request) { |
| 3328 | ctx := r.Context() |
| 3329 | apiKey := httpmw.APIKey(r) |
| 3330 | chat := httpmw.ChatParam(r) |
| 3331 | chatID := chat.ID |
| 3332 | |
| 3333 | // Promoting a queued message triggers LLM inference, |
| 3334 | // requiring update permission on the org-scoped chat resource. |
| 3335 | if !api.Authorize(r, policy.ActionUpdate, chat.RBACObject()) { |
| 3336 | httpapi.ResourceNotFound(rw) |
| 3337 | return |
| 3338 | } |
| 3339 | |
| 3340 | // Only the chat owner may promote messages. See |
| 3341 | // postChatMessages for the security rationale. |
| 3342 | if apiKey.UserID != chat.OwnerID { |
| 3343 | httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ |
| 3344 | Message: "Only the chat owner may promote queued messages.", |
| 3345 | }) |
| 3346 | return |
| 3347 | } |
| 3348 | |
| 3349 | if chat.Archived { |
| 3350 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 3351 | Message: "Cannot promote queued messages in an archived chat.", |
| 3352 | }) |
| 3353 | return |
| 3354 | } |
| 3355 | |
| 3356 | queuedMessageIDStr := chi.URLParam(r, "queuedMessage") |
| 3357 | queuedMessageID, err := strconv.ParseInt(queuedMessageIDStr, 10, 64) |
| 3358 | if err != nil { |
| 3359 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 3360 | Message: "Invalid queued message ID.", |
| 3361 | Detail: err.Error(), |
| 3362 | }) |
| 3363 | return |
| 3364 | } |
| 3365 | |
| 3366 | if api.chatDaemon == nil { |
| 3367 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 3368 | Message: "Chat processor is unavailable.", |
| 3369 | Detail: "Chat processor is not configured.", |
| 3370 | }) |
| 3371 | return |
| 3372 | } |
| 3373 | |
| 3374 | _, txErr := api.chatDaemon.PromoteQueued(ctx, chatd.PromoteQueuedOptions{ |
| 3375 | ChatID: chatID, |
| 3376 | CreatedBy: apiKey.UserID, |
| 3377 | QueuedMessageID: queuedMessageID, |
| 3378 | }) |
| 3379 | |
| 3380 | if txErr != nil { |
| 3381 | if maybeWriteLimitErr(ctx, rw, txErr) { |
| 3382 | return |
| 3383 | } |
| 3384 | if xerrors.Is(txErr, chatd.ErrChatArchived) { |
no test coverage detected