mirror of
https://github.com/coder/coder.git
synced 2026-09-23 05:43:53 +08:00
## Description Separates the aibridge provider configuration from the per-request configuration an interceptor actually needs, and introduces a single `Credential` type that each provider resolves per request. Previously a provider handed its full config to the interceptor (including fields the interceptor didn't use) while other request data was passed as loose arguments, and authentication was spread across config fields and arguments. ## Changes - Add `intercept.Config`: the per-request, provider-agnostic configuration an interceptor needs (`ProviderName`, `BaseURL`, `APIDumpDir`, `SendActorHeaders`). - Introduce a single `Credential` interface (`BYOK` and `Centralized`) that each provider resolves per request in `resolveCredential`, and have interceptors route on the credential kind. - Fail fast with `ErrNoCredential` when a request is neither BYOK nor backed by a centralized key pool. - Remove unused provider config fields (`Key`, `BYOKBearerToken`, `ExtraHeaders`). Closes: coder/aibridge#266 Closes: https://linear.app/codercom/issue/AIGOV-221/refactor-separate-provider-and-interceptor-configs > [!NOTE] > Initially generated by Claude Opus 4.7, modified and reviewed by @ssncferreira
43 lines
1.8 KiB
Go
43 lines
1.8 KiB
Go
package intercept
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/aibridge/mcp"
|
|
"github.com/coder/coder/v2/aibridge/recorder"
|
|
)
|
|
|
|
// Interceptor describes a (potentially) stateful interaction with an AI provider.
|
|
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 resolved for this interception. Its
|
|
// Hint/Length reflect the key in use (the last failover key for a pool
|
|
// credential, otherwise the static credential), for logs and records.
|
|
Credential() Credential
|
|
// 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
|
|
}
|