Documentation
¶
Index ¶
- Constants
- func ActorHeadersAsAnthropicOpts(actor *context.Actor) []ant_option.RequestOption
- func ActorHeadersAsOpenAIOpts(actor *context.Actor) []oai_option.RequestOption
- func ActorIDHeader() string
- func ActorMetadataHeader(name string) string
- func BuildUpstreamHeaders(sdkHeader http.Header, clientHeaders http.Header, authHeaderName string) http.Header
- func IsActorHeader(name string) bool
- func PrepareClientHeaders(clientHeaders http.Header) http.Header
- type CredentialInfo
- type CredentialKind
- type Interceptor
- type ResponseError
Constants ¶
const ( OpenAIErrTypeError = "error" OpenAIErrTypeAPI = "api_error" OpenAIErrTypeRateLimit = "rate_limit_error" OpenAIErrCodeServer = "server_error" OpenAIErrCodeRateLimit = "rate_limit_exceeded" )
OpenAI error type and code constants used by the chatcompletions and responses interceptors. The OpenAI Go SDK does not expose these as typed constants, so we define our own. See https://platform.openai.com/docs/guides/error-codes.
Variables ¶
This section is empty.
Functions ¶
func ActorHeadersAsAnthropicOpts ¶
func ActorHeadersAsAnthropicOpts(actor *context.Actor) []ant_option.RequestOption
ActorHeadersAsAnthropicOpts produces a slice of headers using Anthropic's RequestOption type.
func ActorHeadersAsOpenAIOpts ¶
func ActorHeadersAsOpenAIOpts(actor *context.Actor) []oai_option.RequestOption
ActorHeadersAsOpenAIOpts produces a slice of headers using OpenAI's RequestOption type.
func ActorIDHeader ¶
func ActorIDHeader() string
func ActorMetadataHeader ¶
func BuildUpstreamHeaders ¶
func BuildUpstreamHeaders(sdkHeader http.Header, clientHeaders http.Header, authHeaderName string) http.Header
BuildUpstreamHeaders produces the header set for an upstream SDK request. It starts from the prepared client headers, then preserves specific headers from the SDK-built request that must not be overwritten.
func IsActorHeader ¶
Types ¶
type CredentialInfo ¶
type CredentialInfo struct {
Kind CredentialKind
Hint string
Length int
}
CredentialInfo holds credential metadata for an interception.
func NewCredentialInfo ¶
func NewCredentialInfo(kind CredentialKind, credential string) CredentialInfo
NewCredentialInfo creates a CredentialInfo from a raw credential. The credential is automatically masked before storage so that the original secret is never retained.
type CredentialKind ¶
type CredentialKind string
CredentialKind identifies how a request was authenticated. Keep in sync with the credential_kind enum in coderd's database.
const ( CredentialKindCentralized CredentialKind = "centralized" CredentialKindBYOK CredentialKind = "byok" )
Credential kind constants for interception recording.
type Interceptor ¶
type Interceptor interface {
// ID returns the unique identifier for this interception.
ID() uuid.UUID
// Setup injects some required dependencies. This MUST be called before using the interceptor
// to process requests.
Setup(logger slog.Logger, rec recorder.Recorder, mcpProxy mcp.ServerProxier)
// Model returns the model in use for this [Interceptor].
Model() string
// ProcessRequest handles the HTTP request.
ProcessRequest(w http.ResponseWriter, r *http.Request) error
// Specifies whether an interceptor handles streaming or not.
Streaming() bool
// TraceAttributes returns tracing attributes for this [Interceptor]
TraceAttributes(*http.Request) []attribute.KeyValue
// Credential returns the credential metadata for this interception.
Credential() CredentialInfo
// CorrelatingToolCallID returns the ID of a tool call result submitted
// in the request, if present. This is used to correlate the current
// interception back to the previous interception that issued those tool
// calls. If multiple tool use results are present, we use the last one
// (most recent). Both Anthropic's /v1/messages and OpenAI's /v1/responses
// require that ALL tool results are submitted for tool choices returned
// by the model, so any single tool call ID is sufficient to identify the
// parent interception.
CorrelatingToolCallID() *string
}
Interceptor describes a (potentially) stateful interaction with an AI provider.
type ResponseError ¶ added in v2.34.0
type ResponseError struct {
ErrorObject *shared.ErrorObject `json:"error"`
StatusCode int `json:"-"`
RetryAfter time.Duration `json:"-"`
}
ResponseError is the OpenAI-shaped error envelope returned to clients. StatusCode and RetryAfter map to HTTP headers, not JSON fields. The chatcompletions and responses interceptors both use the same response error format.
func NewResponseError ¶ added in v2.34.0
func NewResponseError(msg, errType, code string, status int, retryAfter time.Duration) *ResponseError
NewResponseError builds a ResponseError with the OpenAI-shaped envelope. errType and code should be one of the OpenAIErrType* and OpenAIErrCode* constants defined above.
func ResponseErrorFromAPIError ¶ added in v2.34.0
func ResponseErrorFromAPIError(err error) *ResponseError
ResponseErrorFromAPIError converts an OpenAI SDK error into a ResponseError. Returns nil if err is not an *openai.Error.
func ResponseErrorFromKeyPool ¶ added in v2.34.0
func ResponseErrorFromKeyPool(keyPoolErr *keypool.Error) *ResponseError
ResponseErrorFromKeyPool translates a *keypool.Error into a developer-facing ResponseError shaped for the OpenAI API.
func (*ResponseError) Error ¶ added in v2.34.0
func (e *ResponseError) Error() string
func (*ResponseError) ToResponse ¶ added in v2.34.0
func (e *ResponseError) ToResponse() *http.Response
ToResponse marshals e into an *http.Response shaped for the OpenAI API.