MCPcopy Index your code
hub / github.com/coder/coder / updateChatModelConfig

Method updateChatModelConfig

coderd/exp_chats.go:6981–7211  ·  view source on GitHub ↗
(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

6979}
6980
6981func (api *API) updateChatModelConfig(rw http.ResponseWriter, r *http.Request) {
6982 ctx := r.Context()
6983 apiKey := httpmw.APIKey(r)
6984 if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) {
6985 httpapi.Forbidden(rw)
6986 return
6987 }
6988
6989 modelConfigID, ok := parseChatModelConfigID(rw, r)
6990 if !ok {
6991 return
6992 }
6993
6994 existing, err := api.Database.GetChatModelConfigByID(ctx, modelConfigID)
6995 if err != nil {
6996 if httpapi.Is404Error(err) {
6997 httpapi.ResourceNotFound(rw)
6998 return
6999 }
7000 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
7001 Message: "Failed to get chat model config.",
7002 Detail: err.Error(),
7003 })
7004 return
7005 }
7006
7007 var req codersdk.UpdateChatModelConfigRequest
7008 if !httpapi.Read(ctx, rw, r, &req) {
7009 return
7010 }
7011
7012 if strings.TrimSpace(req.Provider) != "" && req.AIProviderID == nil {
7013 requestedProvider := chatprovider.NormalizeProvider(req.Provider)
7014 if requestedProvider == "" {
7015 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{Message: "Invalid provider."})
7016 return
7017 }
7018 if requestedProvider != existing.Provider {
7019 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{Message: "AI provider ID is required when updating provider."})
7020 return
7021 }
7022 }
7023
7024 provider := existing.Provider
7025 aiProviderID := existing.AIProviderID
7026 if req.AIProviderID != nil {
7027 //nolint:gocritic // The route already authorized chat model config updates.
7028 aiProvider, err := api.Database.GetAIProviderByID(dbauthz.AsChatd(ctx), *req.AIProviderID)
7029 if err != nil {
7030 if httpapi.Is404Error(err) {
7031 httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{Message: "AI provider is not configured."})
7032 return
7033 }
7034 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
7035 Message: "Failed to get AI provider.",
7036 Detail: err.Error(),
7037 })
7038 return

Callers 1

updateChatModelConfigFunction · 0.80

Calls 15

AuthorizeMethod · 0.95
APIKeyFunction · 0.92
ForbiddenFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
WriteFunction · 0.92
ReadFunction · 0.92
NormalizeProviderFunction · 0.92
AsChatdFunction · 0.92
IsUniqueViolationFunction · 0.92
parseChatModelConfigIDFunction · 0.85

Tested by

no test coverage detected