EXPERIMENTAL: this endpoint is experimental and is subject to change.
(rw http.ResponseWriter, r *http.Request)
| 5323 | |
| 5324 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 5325 | func (api *API) putChatAdvisorConfig(rw http.ResponseWriter, r *http.Request) { |
| 5326 | ctx := r.Context() |
| 5327 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 5328 | httpapi.Forbidden(rw) |
| 5329 | return |
| 5330 | } |
| 5331 | |
| 5332 | var req codersdk.UpdateAdvisorConfigRequest |
| 5333 | if !httpapi.Read(ctx, rw, r, &req) { |
| 5334 | return |
| 5335 | } |
| 5336 | if req.MaxUsesPerRun < 0 { |
| 5337 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 5338 | Message: fmt.Sprintf("max_uses_per_run %d must be non-negative.", req.MaxUsesPerRun), |
| 5339 | }) |
| 5340 | return |
| 5341 | } |
| 5342 | if req.MaxOutputTokens < 0 { |
| 5343 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 5344 | Message: fmt.Sprintf("max_output_tokens %d must be non-negative.", req.MaxOutputTokens), |
| 5345 | }) |
| 5346 | return |
| 5347 | } |
| 5348 | if req.ModelConfigID != uuid.Nil { |
| 5349 | // Use system context because GetChatModelConfigByID requires |
| 5350 | // deployment-config read access, which can be broader than the |
| 5351 | // handler's explicit update check. The lookup only validates that |
| 5352 | // the referenced model exists before persisting deployment config. |
| 5353 | //nolint:gocritic // This admin-authorized validation lookup intentionally bypasses read authz. |
| 5354 | if _, err := api.Database.GetChatModelConfigByID(dbauthz.AsSystemRestricted(ctx), req.ModelConfigID); err != nil { |
| 5355 | if errors.Is(err, sql.ErrNoRows) || httpapi.Is404Error(err) { |
| 5356 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 5357 | Message: fmt.Sprintf("model_config_id %q does not match any existing model config.", req.ModelConfigID), |
| 5358 | }) |
| 5359 | return |
| 5360 | } |
| 5361 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 5362 | Message: "Internal error validating advisor model config.", |
| 5363 | Detail: err.Error(), |
| 5364 | }) |
| 5365 | return |
| 5366 | } |
| 5367 | } |
| 5368 | |
| 5369 | raw, err := json.Marshal(req) |
| 5370 | if err != nil { |
| 5371 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 5372 | Message: "Internal error encoding advisor configuration.", |
| 5373 | Detail: err.Error(), |
| 5374 | }) |
| 5375 | return |
| 5376 | } |
| 5377 | if err := api.Database.UpsertChatAdvisorConfig(ctx, string(raw)); err != nil { |
| 5378 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 5379 | Message: "Internal error updating advisor configuration.", |
| 5380 | Detail: err.Error(), |
| 5381 | }) |
| 5382 | return |
nothing calls this directly
no test coverage detected