aibridged

package
v2.28.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2025 License: AGPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InternalMCPServerID = "coder"
)

Variables

View Source
var (
	ErrNoAuthKey             = xerrors.New("no authentication key provided")
	ErrConnect               = xerrors.New("could not connect to coderd")
	ErrUnauthorized          = xerrors.New("unauthorized")
	ErrAcquireRequestHandler = xerrors.New("failed to acquire request handler")
)
View Source
var (
	ErrEmptyConfig  = xerrors.New("empty config given")
	ErrCompileRegex = xerrors.New("compile tool regex")
)
View Source
var DefaultPoolOptions = PoolOptions{MaxItems: 100, TTL: time.Minute * 15}

Functions

func ExtractAuthToken

func ExtractAuthToken(header http.Header) string

ExtractAuthToken extracts authorization token from HTTP request using multiple sources. These sources represent the different ways clients authenticate against AI providers. It checks the Authorization header (Bearer token) and X-Api-Key header. If neither are present, an empty string is returned.

Types

type CachedBridgePool

type CachedBridgePool struct {
	// contains filtered or unexported fields
}

func NewCachedBridgePool

func NewCachedBridgePool(options PoolOptions, providers []aibridge.Provider, logger slog.Logger) (*CachedBridgePool, error)

func (*CachedBridgePool) Acquire

func (p *CachedBridgePool) Acquire(ctx context.Context, req Request, clientFn ClientFunc, mcpProxyFactory MCPProxyBuilder) (http.Handler, error)

Acquire retrieves or creates a *aibridge.RequestBridge instance per given key.

Each returned *aibridge.RequestBridge is safe for concurrent use. Each *aibridge.RequestBridge is stateful because it has MCP clients which maintain sessions to the configured MCP server.

func (*CachedBridgePool) Metrics

func (p *CachedBridgePool) Metrics() PoolMetrics

func (*CachedBridgePool) Shutdown

func (p *CachedBridgePool) Shutdown(_ context.Context) error

Shutdown will close the cache which will trigger eviction of all the Bridge entries.

type Client

func (*Client) DRPCConn

func (c *Client) DRPCConn() drpc.Conn

type ClientFunc

type ClientFunc func() (DRPCClient, error)

type DRPCClient

DRPCClient is the union of various service interfaces the client must support.

type Dialer

type Dialer func(ctx context.Context) (DRPCClient, error)

type MCPProxyBuilder

type MCPProxyBuilder interface {
	// Build creates a [mcp.ServerProxier] for the given request initiator.
	// At minimum, the Coder MCP server will be proxied.
	// The SessionKey from [Request] is used to authenticate against the Coder MCP server.
	//
	// NOTE: the [mcp.ServerProxier] instance may be proxying one or more MCP servers.
	Build(ctx context.Context, req Request) (mcp.ServerProxier, error)
}

type MCPProxyFactory

type MCPProxyFactory struct {
	// contains filtered or unexported fields
}

func NewMCPProxyFactory

func NewMCPProxyFactory(logger slog.Logger, clientFn ClientFunc) *MCPProxyFactory

func (*MCPProxyFactory) Build

type PoolMetrics

type PoolMetrics interface {
	Hits() uint64
	Misses() uint64
	KeysAdded() uint64
	KeysEvicted() uint64
}

type PoolOptions

type PoolOptions struct {
	MaxItems int64
	TTL      time.Duration
}

type Pooler

type Pooler interface {
	Acquire(ctx context.Context, req Request, clientFn ClientFunc, mcpBootstrapper MCPProxyBuilder) (http.Handler, error)
	Shutdown(ctx context.Context) error
}

Pooler describes a pool of *aibridge.RequestBridge instances from which instances can be retrieved. One *aibridge.RequestBridge instance is created per given key.

type Request

type Request struct {
	SessionKey  string
	InitiatorID uuid.UUID
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server provides the AI Bridge functionality. It is responsible for:

  • receiving requests on /api/v2/aibridged/*
  • manipulating the requests
  • relaying requests to upstream AI services and relaying responses to caller

It requires a Dialer to provide a DRPCClient implementation to communicate with a DRPCServer implementation, to persist state and perform other functions.

func New

func New(ctx context.Context, pool Pooler, rpcDialer Dialer, logger slog.Logger) (*Server, error)

func (*Server) Client

func (s *Server) Client() (DRPCClient, error)

func (*Server) Close

func (s *Server) Close() error

Close shuts down the server with a timeout of 5s.

func (*Server) GetRequestHandler

func (s *Server) GetRequestHandler(ctx context.Context, req Request) (http.Handler, error)

GetRequestHandler retrieves a (possibly reused) *aibridge.RequestBridge from the pool, for the given user.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP is the entrypoint for requests which will be intercepted by AI Bridge. This function will validate that the given API key may be used to perform the request.

An aibridge.RequestBridge instance is acquired from a pool based on the API key's owner (referred to as the "initiator"); this instance is responsible for the AI Bridge-specific handling of the request.

A DRPCClient is provided to the aibridge.RequestBridge instance so that data can be passed up to a DRPCServer for persistence.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown waits for all exiting in-flight requests to complete, or the context to expire, whichever comes first.

Directories

Path Synopsis
Package aibridgedmock is a generated GoMock package.
Package aibridgedmock is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL