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

Method putUserChatCustomPrompt

coderd/exp_chats.go:5804–5845  ·  view source on GitHub ↗

EXPERIMENTAL: this endpoint is experimental and is subject to change.

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

5802
5803// EXPERIMENTAL: this endpoint is experimental and is subject to change.
5804func (api *API) putUserChatCustomPrompt(rw http.ResponseWriter, r *http.Request) {
5805 var (
5806 ctx = r.Context()
5807 apiKey = httpmw.APIKey(r)
5808 )
5809 // Cap the raw request body to prevent excessive memory use from
5810 // payloads padded with invisible characters that sanitize away.
5811 r.Body = http.MaxBytesReader(rw, r.Body, int64(2*maxSystemPromptLenBytes))
5812
5813 var params codersdk.UserChatCustomPrompt
5814 if !httpapi.Read(ctx, rw, r, &params) {
5815 return
5816 }
5817
5818 sanitizedPrompt := chatd.SanitizePromptText(params.CustomPrompt)
5819 // Apply the same 128 KiB limit as the deployment system prompt.
5820 if len(sanitizedPrompt) > maxSystemPromptLenBytes {
5821 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
5822 Message: "Custom prompt exceeds maximum length.",
5823 Detail: fmt.Sprintf("Maximum length is %d bytes, got %d.", maxSystemPromptLenBytes, len(sanitizedPrompt)),
5824 })
5825 return
5826 }
5827
5828 updatedConfig, err := api.Database.UpdateUserChatCustomPrompt(ctx, database.UpdateUserChatCustomPromptParams{
5829 UserID: apiKey.UserID,
5830 ChatCustomPrompt: sanitizedPrompt,
5831 })
5832 if err != nil {
5833 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
5834 Message: "Error updating user chat custom prompt.",
5835 Detail: err.Error(),
5836 })
5837 return
5838 }
5839
5840 publishChatConfigEvent(api.Logger, api.Pubsub, pubsub.ChatConfigEventUserPrompt, apiKey.UserID)
5841
5842 httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserChatCustomPrompt{
5843 CustomPrompt: updatedConfig.Value,
5844 })
5845}
5846
5847// @Summary Get user chat compaction thresholds
5848// @x-apidocgen {"skip": true}

Callers

nothing calls this directly

Calls 8

APIKeyFunction · 0.92
ReadFunction · 0.92
SanitizePromptTextFunction · 0.92
WriteFunction · 0.92
publishChatConfigEventFunction · 0.85
ContextMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected