MCPcopy Create free account
hub / github.com/coder/aibridge / newMessagesService

Method newMessagesService

intercept/messages/base.go:207–257  ·  view source on GitHub ↗
(ctx context.Context, opts ...option.RequestOption)

Source from the content-addressed store, hash-verified

205}
206
207func (i *interceptionBase) newMessagesService(ctx context.Context, opts ...option.RequestOption) (anthropic.MessageService, error) {
208 // BYOK with access token uses Authorization: Bearer.
209 // Otherwise use X-Api-Key (centralized or BYOK with personal API key).
210 if i.cfg.BYOKBearerToken != "" {
211 i.logger.Debug(ctx, "using byok access token auth",
212 slog.F("bearer_hint", utils.MaskSecret(i.cfg.BYOKBearerToken)),
213 )
214 opts = append(opts, option.WithAuthToken(i.cfg.BYOKBearerToken))
215 } else {
216 i.logger.Debug(ctx, "using api key auth",
217 slog.F("api_key_hint", utils.MaskSecret(i.cfg.Key)),
218 )
219 opts = append(opts, option.WithAPIKey(i.cfg.Key))
220 }
221 opts = append(opts, option.WithBaseURL(i.cfg.BaseURL))
222
223 // Add extra headers if configured.
224 // Some providers require additional headers that are not added by the SDK.
225 // TODO(ssncferreira): remove as part of https://github.com/coder/aibridge/issues/192
226 for key, value := range i.cfg.ExtraHeaders {
227 opts = append(opts, option.WithHeader(key, value))
228 }
229
230 // Forward client headers to upstream. This middleware runs after the SDK
231 // has built the request, and replaces the outgoing headers with the sanitized
232 // client headers plus provider auth.
233 if i.clientHeaders != nil {
234 opts = append(opts, option.WithMiddleware(func(req *http.Request, next option.MiddlewareNext) (*http.Response, error) {
235 req.Header = intercept.BuildUpstreamHeaders(req.Header, i.clientHeaders, i.authHeaderName)
236 return next(req)
237 }))
238 }
239
240 // Add API dump middleware if configured
241 if mw := apidump.NewBridgeMiddleware(i.cfg.APIDumpDir, i.providerName, i.Model(), i.id, i.logger, quartz.NewReal()); mw != nil {
242 opts = append(opts, option.WithMiddleware(mw))
243 }
244
245 if i.bedrockCfg != nil {
246 ctx, cancel := context.WithTimeout(ctx, time.Second*30)
247 defer cancel()
248 bedrockOpts, err := i.withAWSBedrockOptions(ctx, i.bedrockCfg)
249 if err != nil {
250 return anthropic.MessageService{}, err
251 }
252 opts = append(opts, bedrockOpts...)
253 i.augmentRequestForBedrock()
254 }
255
256 return anthropic.NewMessageService(opts...), nil
257}
258
259// withBody returns a per-request option that sends the current raw request
260// payload as the request body. This is called for each API request so that the

Callers 2

ProcessRequestMethod · 0.80
ProcessRequestMethod · 0.80

Calls 6

ModelMethod · 0.95
withAWSBedrockOptionsMethod · 0.95
MaskSecretFunction · 0.92
BuildUpstreamHeadersFunction · 0.92
NewBridgeMiddlewareFunction · 0.92

Tested by

no test coverage detected