MCPcopy Index your code
hub / github.com/coder/coder / newAIBridgeProxyDaemon

Function newAIBridgeProxyDaemon

enterprise/cli/aibridgeproxyd.go:42–89  ·  view source on GitHub ↗

newAIBridgeProxyDaemon starts the enterprise aibridge proxy daemon, subscribes to ai_providers changes so the proxy's routing snapshot tracks the database, and registers the HTTP handler on the API. The returned io.Closer tears down both the subscription and server.

(coderAPI *coderd.API)

Source from the content-addressed store, hash-verified

40// tracks the database, and registers the HTTP handler on the API.
41// The returned io.Closer tears down both the subscription and server.
42func newAIBridgeProxyDaemon(coderAPI *coderd.API) (io.Closer, error) {
43 ctx := context.Background()
44 coderAPI.Logger.Debug(ctx, "starting in-memory aibridgeproxy daemon")
45
46 logger := coderAPI.Logger.Named("aibridgeproxyd")
47
48 reg := prometheus.WrapRegistererWithPrefix("coder_aibridgeproxyd_", coderAPI.PrometheusRegistry)
49 metrics := aibridgeproxyd.NewMetrics(reg)
50
51 var newDumper func(provider, requestID string) aibridgeproxyd.RoundTripDumper
52 if dumpDir := coderAPI.DeploymentValues.AI.BridgeProxyConfig.APIDumpDir.String(); dumpDir != "" {
53 newDumper = func(provider, requestID string) aibridgeproxyd.RoundTripDumper {
54 return apidump.NewDumper(filepath.Join(dumpDir, provider, requestID), logger)
55 }
56 }
57
58 srv, err := aibridgeproxyd.New(ctx, logger, aibridgeproxyd.Options{
59 ListenAddr: coderAPI.DeploymentValues.AI.BridgeProxyConfig.ListenAddr.String(),
60 TLSCertFile: coderAPI.DeploymentValues.AI.BridgeProxyConfig.TLSCertFile.String(),
61 TLSKeyFile: coderAPI.DeploymentValues.AI.BridgeProxyConfig.TLSKeyFile.String(),
62 CoderAccessURL: coderAPI.AccessURL.String(),
63 MITMCertFile: coderAPI.DeploymentValues.AI.BridgeProxyConfig.MITMCertFile.String(),
64 MITMKeyFile: coderAPI.DeploymentValues.AI.BridgeProxyConfig.MITMKeyFile.String(),
65 UpstreamProxy: coderAPI.DeploymentValues.AI.BridgeProxyConfig.UpstreamProxy.String(),
66 UpstreamProxyCA: coderAPI.DeploymentValues.AI.BridgeProxyConfig.UpstreamProxyCA.String(),
67 AllowedPrivateCIDRs: coderAPI.DeploymentValues.AI.BridgeProxyConfig.AllowedPrivateCIDRs.Value(),
68 NewDumper: newDumper,
69 Metrics: metrics,
70 RefreshProviders: refreshProxyProviders(coderAPI.Database),
71 })
72 if err != nil {
73 return nil, xerrors.Errorf("failed to start in-memory aibridgeproxy daemon: %w", err)
74 }
75
76 unsubscribe, err := aibridged.SubscribeProviderReload(ctx, coderAPI.Pubsub, srv, logger.Named("provider-reload"))
77 if err != nil {
78 logger.Warn(ctx, "subscribe aibridgeproxyd to ai providers change channel", slog.Error(err))
79 unsubscribe = func() {}
80 }
81
82 // Register the handler so coderd can serve the proxy endpoints.
83 coderAPI.RegisterInMemoryAIBridgeProxydHTTPHandler(srv.Handler())
84
85 return &aiBridgeProxyDaemon{
86 server: srv,
87 unsubscribe: unsubscribe,
88 }, nil
89}
90
91// refreshProxyProviders classifies every ai_providers row as enabled,
92// disabled, or error so the proxy router and any observers see the full

Callers 1

ServerMethod · 0.85

Calls 12

NewMetricsFunction · 0.92
NewDumperFunction · 0.92
NewFunction · 0.92
SubscribeProviderReloadFunction · 0.92
refreshProxyProvidersFunction · 0.85
NamedMethod · 0.80
StringMethod · 0.45
ValueMethod · 0.45
ErrorfMethod · 0.45
ErrorMethod · 0.45
HandlerMethod · 0.45

Tested by

no test coverage detected