Files
coder/aibridge/context/context.go
T
Paweł Banaszewski ffc7dadfe2 refactor: use typed AI Gateway actor identity (#30258)
Adds typed AI Gateway actor identity, including the API key ID used by
recording.

Recorders now read the authenticated API key ID from each call context
instead of capturing request identity at construction time. This allows
one recorder to be reused safely across sequential and concurrent
requests. Includes context helper coverage and recorder regression
tests.

Generated by Coder Agents on behalf of @pawbana.
2026-10-05 14:00:15 +00:00

60 lines
1.6 KiB
Go

package context
import (
"context"
"github.com/google/uuid"
"golang.org/x/xerrors"
)
type (
actorContextKey struct{}
)
// ErrMissingAPIKeyID indicates that the context has no actor with an API-key ID.
var ErrMissingAPIKeyID = xerrors.New("missing authenticated API-key ID")
// Actor is the authenticated identity attached to an AI Gateway request.
type Actor struct {
ID uuid.UUID
APIKeyID string
Username string
// Email is only used for the opt-in email actor header forwarding. It is kept
// out of interception recordings.
Email string
}
// AsActor returns a context containing a copy of the authenticated actor.
func AsActor(ctx context.Context, actor Actor) context.Context {
return context.WithValue(ctx, actorContextKey{}, &actor)
}
// ActorFromContext returns the authenticated actor, or nil when absent.
func ActorFromContext(ctx context.Context) *Actor {
a, ok := ctx.Value(actorContextKey{}).(*Actor)
if !ok {
return nil
}
return a
}
// ActorIDFromContext safely extracts the actor ID from the context.
// Returns an empty string if no actor is found.
func ActorIDFromContext(ctx context.Context) string {
if actor := ActorFromContext(ctx); actor != nil && actor.ID != uuid.Nil {
return actor.ID.String()
}
return ""
}
// APIKeyIDFromContext returns the actor's API-key ID, or ErrMissingAPIKeyID if
// the actor is absent or its API-key ID is empty.
func APIKeyIDFromContext(ctx context.Context) (string, error) {
actor := ActorFromContext(ctx)
if actor == nil || actor.APIKeyID == "" {
return "", ErrMissingAPIKeyID
}
return actor.APIKeyID, nil
}