nolint:revive // HTTP handler writes to ResponseWriter.
(rw http.ResponseWriter, r *http.Request)
| 3694 | |
| 3695 | //nolint:revive // HTTP handler writes to ResponseWriter. |
| 3696 | func (api *API) proposeChatTitle(rw http.ResponseWriter, r *http.Request) { |
| 3697 | ctx := r.Context() |
| 3698 | apiKey := httpmw.APIKey(r) |
| 3699 | chat := httpmw.ChatParam(r) |
| 3700 | |
| 3701 | if !api.Authorize(r, policy.ActionUpdate, chat.RBACObject()) { |
| 3702 | httpapi.ResourceNotFound(rw) |
| 3703 | return |
| 3704 | } |
| 3705 | |
| 3706 | // Only the chat owner may propose titles. See |
| 3707 | // postChatMessages for the security rationale. |
| 3708 | if apiKey.UserID != chat.OwnerID { |
| 3709 | httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ |
| 3710 | Message: "Only the chat owner may propose a title.", |
| 3711 | }) |
| 3712 | return |
| 3713 | } |
| 3714 | |
| 3715 | if api.chatDaemon == nil { |
| 3716 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 3717 | Message: "Chat processor is unavailable.", |
| 3718 | Detail: "Chat processor is not configured.", |
| 3719 | }) |
| 3720 | return |
| 3721 | } |
| 3722 | |
| 3723 | ctx = aibridge.WithDelegatedAPIKeyID(ctx, apiKey.ID) |
| 3724 | title, err := api.chatDaemon.ProposeChatTitle(ctx, chat) |
| 3725 | if err != nil { |
| 3726 | if errors.Is(err, chatd.ErrManualTitleRegenerationInProgress) { |
| 3727 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 3728 | Message: "Title regeneration already in progress for this chat.", |
| 3729 | }) |
| 3730 | return |
| 3731 | } |
| 3732 | if maybeWriteLimitErr(ctx, rw, err) { |
| 3733 | return |
| 3734 | } |
| 3735 | if httpapi.Is404Error(err) { |
| 3736 | httpapi.ResourceNotFound(rw) |
| 3737 | return |
| 3738 | } |
| 3739 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 3740 | Message: "Failed to generate chat title.", |
| 3741 | Detail: err.Error(), |
| 3742 | }) |
| 3743 | return |
| 3744 | } |
| 3745 | |
| 3746 | httpapi.Write(ctx, rw, http.StatusOK, codersdk.ProposeChatTitleResponse{Title: title}) |
| 3747 | } |
| 3748 | |
| 3749 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 3750 | // |
no test coverage detected