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

Function readChatContextBody

coderd/workspaceagents.go:2456–2481  ·  view source on GitHub ↗

readChatContextBody reads and validates the request body for chat context endpoints. It handles MaxBytesReader wrapping, error responses, and body rewind. If the body is empty or whitespace-only and allowEmpty is true, it returns false without writing an error. nolint:revive // Add and clear endpoi

(ctx context.Context, rw http.ResponseWriter, r *http.Request, dst any, allowEmpty bool)

Source from the content-addressed store, hash-verified

2454//
2455//nolint:revive // Add and clear endpoints only differ by empty-body handling.
2456func readChatContextBody(ctx context.Context, rw http.ResponseWriter, r *http.Request, dst any, allowEmpty bool) bool {
2457 r.Body = http.MaxBytesReader(rw, r.Body, maxChatContextRequestBodyBytes)
2458 body, err := io.ReadAll(r.Body)
2459 if err != nil {
2460 var maxBytesErr *http.MaxBytesError
2461 if errors.As(err, &maxBytesErr) {
2462 httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{
2463 Message: "Request body too large.",
2464 Detail: fmt.Sprintf("Maximum request body size is %d bytes.", maxChatContextRequestBodyBytes),
2465 })
2466 return false
2467 }
2468 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
2469 Message: "Failed to read request body.",
2470 Detail: err.Error(),
2471 })
2472 return false
2473 }
2474 if allowEmpty && len(bytes.TrimSpace(body)) == 0 {
2475 r.Body = http.NoBody
2476 return false
2477 }
2478
2479 r.Body = io.NopCloser(bytes.NewReader(body))
2480 return httpapi.Read(ctx, rw, r, dst)
2481}
2482
2483// @x-apidocgen {"skip": true}
2484func (api *API) workspaceAgentAddChatContext(rw http.ResponseWriter, r *http.Request) {

Calls 5

WriteFunction · 0.92
ReadFunction · 0.92
AsMethod · 0.80
ReadAllMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected