Files
coder/coderd/httpmw/requestid.go
Kyle Carberry edee917d88 feat: add experimental agents support (#22290)
feat: add AI chat system with agent tools and chat UI

Introduce the chatd subsystem and Agents UI for AI-powered chat
within Coder workspaces.

- Add chatd package with chat loop, message compaction, prompt
  management, and LLM provider integration (OpenAI, Anthropic)
- Add agent tools: create workspace, list/read templates, read/write/
  edit files, execute commands
- Add chat API endpoints with streaming, message editing, and
  durable reconnection
- Add database schema and migrations for chats, chat messages, chat
  providers, and chat model configs
- Add RBAC policies and dbauthz enforcement for chat resources
- Add Agents UI pages with conversation timeline, queued messages
  list, diff viewer, and model configuration panel
- Add comprehensive test coverage including coderd integration tests,
  chatd unit tests, and Storybook stories
- Gate feature behind experiments flag

---------

Co-authored-by: Cian Johnston <cian@coder.com>
Co-authored-by: Danielle Maywood <danielle@themaywoods.com>
Co-authored-by: Jeremy Ruppel <jeremy@coder.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-27 16:50:56 +00:00

52 lines
1.3 KiB
Go

package httpmw
import (
"context"
"net/http"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"cdr.dev/slog/v3"
)
type requestIDContextKey struct{}
// RequestID returns the ID of the request.
func RequestID(r *http.Request) uuid.UUID {
rid, ok := RequestIDOptional(r)
if !ok {
panic("developer error: request id middleware not provided")
}
return rid
}
// RequestIDOptional returns the request ID when present.
func RequestIDOptional(r *http.Request) (uuid.UUID, bool) {
rid, ok := r.Context().Value(requestIDContextKey{}).(uuid.UUID)
return rid, ok
}
// WithRequestID stores a request ID in the context.
func WithRequestID(ctx context.Context, rid uuid.UUID) context.Context {
return context.WithValue(ctx, requestIDContextKey{}, rid)
}
// AttachRequestID adds a request ID to each HTTP request.
func AttachRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rid := uuid.New()
ridString := rid.String()
ctx := context.WithValue(r.Context(), requestIDContextKey{}, rid)
ctx = slog.With(ctx, slog.F("request_id", rid))
trace.SpanFromContext(ctx).
SetAttributes(attribute.String("request_id", rid.String()))
rw.Header().Set("X-Coder-Request-Id", ridString)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}