(rw http.ResponseWriter, r *http.Request)
| 4612 | } |
| 4613 | |
| 4614 | func (api *API) putChatSystemPrompt(rw http.ResponseWriter, r *http.Request) { |
| 4615 | ctx := r.Context() |
| 4616 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 4617 | httpapi.Forbidden(rw) |
| 4618 | return |
| 4619 | } |
| 4620 | // Cap the raw request body to prevent excessive memory use from |
| 4621 | // payloads padded with invisible characters that sanitize away. |
| 4622 | r.Body = http.MaxBytesReader(rw, r.Body, int64(2*maxSystemPromptLenBytes)) |
| 4623 | var req codersdk.UpdateChatSystemPromptRequest |
| 4624 | if !httpapi.Read(ctx, rw, r, &req) { |
| 4625 | return |
| 4626 | } |
| 4627 | sanitizedPrompt := chatd.SanitizePromptText(req.SystemPrompt) |
| 4628 | // 128 KiB is generous for a system prompt while still |
| 4629 | // preventing abuse or accidental pastes of large content. |
| 4630 | if len(sanitizedPrompt) > maxSystemPromptLenBytes { |
| 4631 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 4632 | Message: "System prompt exceeds maximum length.", |
| 4633 | Detail: fmt.Sprintf("Maximum length is %d bytes, got %d.", maxSystemPromptLenBytes, len(sanitizedPrompt)), |
| 4634 | }) |
| 4635 | return |
| 4636 | } |
| 4637 | err := api.Database.InTx(func(tx database.Store) error { |
| 4638 | if err := tx.UpsertChatSystemPrompt(ctx, sanitizedPrompt); err != nil { |
| 4639 | return err |
| 4640 | } |
| 4641 | // Only update the include-default flag when the caller explicitly |
| 4642 | // provides it. Omitting the field preserves whatever is currently |
| 4643 | // stored (or the schema-level default for new deployments), |
| 4644 | // avoiding a backward-compatibility regression for older clients |
| 4645 | // that only send system_prompt. |
| 4646 | if req.IncludeDefaultSystemPrompt != nil { |
| 4647 | return tx.UpsertChatIncludeDefaultSystemPrompt(ctx, *req.IncludeDefaultSystemPrompt) |
| 4648 | } |
| 4649 | return nil |
| 4650 | }, nil) |
| 4651 | if err != nil { |
| 4652 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 4653 | Message: "Internal error updating chat system prompt configuration.", |
| 4654 | Detail: err.Error(), |
| 4655 | }) |
| 4656 | return |
| 4657 | } |
| 4658 | rw.WriteHeader(http.StatusNoContent) |
| 4659 | } |
| 4660 | |
| 4661 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 4662 | // |
nothing calls this directly
no test coverage detected