EXPERIMENTAL: this endpoint is experimental and is subject to change. @Summary Regenerate chat title @ID regenerate-chat-title @Security CoderSessionToken @Tags Chats @Produce json @Param chat path string true "Chat ID" format(uuid) @Success 200 {object} codersdk.Chat @Router /api/experimental/chat
(rw http.ResponseWriter, r *http.Request)
| 3640 | // |
| 3641 | //nolint:revive // HTTP handler writes to ResponseWriter. |
| 3642 | func (api *API) regenerateChatTitle(rw http.ResponseWriter, r *http.Request) { |
| 3643 | ctx := r.Context() |
| 3644 | apiKey := httpmw.APIKey(r) |
| 3645 | chat := httpmw.ChatParam(r) |
| 3646 | |
| 3647 | if !api.Authorize(r, policy.ActionUpdate, chat.RBACObject()) { |
| 3648 | httpapi.ResourceNotFound(rw) |
| 3649 | return |
| 3650 | } |
| 3651 | |
| 3652 | // Only the chat owner may regenerate titles. See |
| 3653 | // postChatMessages for the security rationale. |
| 3654 | if apiKey.UserID != chat.OwnerID { |
| 3655 | httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ |
| 3656 | Message: "Only the chat owner may regenerate the title.", |
| 3657 | }) |
| 3658 | return |
| 3659 | } |
| 3660 | |
| 3661 | if api.chatDaemon == nil { |
| 3662 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 3663 | Message: "Chat processor is unavailable.", |
| 3664 | Detail: "Chat processor is not configured.", |
| 3665 | }) |
| 3666 | return |
| 3667 | } |
| 3668 | |
| 3669 | ctx = aibridge.WithDelegatedAPIKeyID(ctx, apiKey.ID) |
| 3670 | updatedChat, err := api.chatDaemon.RegenerateChatTitle(ctx, chat) |
| 3671 | if err != nil { |
| 3672 | if errors.Is(err, chatd.ErrManualTitleRegenerationInProgress) { |
| 3673 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 3674 | Message: "Title regeneration already in progress for this chat.", |
| 3675 | }) |
| 3676 | return |
| 3677 | } |
| 3678 | if maybeWriteLimitErr(ctx, rw, err) { |
| 3679 | return |
| 3680 | } |
| 3681 | if httpapi.Is404Error(err) { |
| 3682 | httpapi.ResourceNotFound(rw) |
| 3683 | return |
| 3684 | } |
| 3685 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 3686 | Message: "Failed to regenerate chat title.", |
| 3687 | Detail: err.Error(), |
| 3688 | }) |
| 3689 | return |
| 3690 | } |
| 3691 | |
| 3692 | httpapi.Write(ctx, rw, http.StatusOK, db2sdk.Chat(updatedChat, nil, nil)) |
| 3693 | } |
| 3694 | |
| 3695 | //nolint:revive // HTTP handler writes to ResponseWriter. |
| 3696 | func (api *API) proposeChatTitle(rw http.ResponseWriter, r *http.Request) { |
no test coverage detected