(rw http.ResponseWriter, r *http.Request)
| 6789 | } |
| 6790 | |
| 6791 | func (api *API) createChatModelConfig(rw http.ResponseWriter, r *http.Request) { |
| 6792 | ctx := r.Context() |
| 6793 | apiKey := httpmw.APIKey(r) |
| 6794 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 6795 | httpapi.Forbidden(rw) |
| 6796 | return |
| 6797 | } |
| 6798 | |
| 6799 | var req codersdk.CreateChatModelConfigRequest |
| 6800 | if !httpapi.Read(ctx, rw, r, &req) { |
| 6801 | return |
| 6802 | } |
| 6803 | |
| 6804 | if req.AIProviderID == nil { |
| 6805 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{Message: "AI provider ID is required."}) |
| 6806 | return |
| 6807 | } |
| 6808 | //nolint:gocritic // The route already authorized chat model config updates. |
| 6809 | aiProvider, err := api.Database.GetAIProviderByID(dbauthz.AsChatd(ctx), *req.AIProviderID) |
| 6810 | if err != nil { |
| 6811 | if httpapi.Is404Error(err) { |
| 6812 | httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{Message: "AI provider is not configured."}) |
| 6813 | return |
| 6814 | } |
| 6815 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 6816 | Message: "Failed to get AI provider.", |
| 6817 | Detail: err.Error(), |
| 6818 | }) |
| 6819 | return |
| 6820 | } |
| 6821 | if !aiProvider.Enabled { |
| 6822 | httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{Message: "AI provider is disabled."}) |
| 6823 | return |
| 6824 | } |
| 6825 | provider := string(aiProvider.Type) |
| 6826 | aiProviderID := uuid.NullUUID{UUID: aiProvider.ID, Valid: true} |
| 6827 | |
| 6828 | model := strings.TrimSpace(req.Model) |
| 6829 | if model == "" { |
| 6830 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 6831 | Message: "Model is required.", |
| 6832 | }) |
| 6833 | return |
| 6834 | } |
| 6835 | |
| 6836 | if validationErr := validateChatModelConfigProviderModel(aiProvider, model); validationErr != nil { |
| 6837 | httpapi.Write(ctx, rw, http.StatusBadRequest, validationErr.Response) |
| 6838 | return |
| 6839 | } |
| 6840 | |
| 6841 | enabled := true |
| 6842 | if req.Enabled != nil { |
| 6843 | enabled = *req.Enabled |
| 6844 | } |
| 6845 | isDefault := false |
| 6846 | if req.IsDefault != nil { |
| 6847 | isDefault = *req.IsDefault |
| 6848 | } |
no test coverage detected