1+ using ConduitLLM . Core . Interfaces . Configuration ;
2+ using Microsoft . Extensions . DependencyInjection ;
3+
4+ namespace ConduitLLM . Admin . Adapters
5+ {
6+ /// <summary>
7+ /// Provides adapters that map Configuration services to Core interfaces for the Admin API.
8+ /// </summary>
9+ internal static class ConfigurationAdapters
10+ {
11+ /// <summary>
12+ /// Registers Core configuration service interfaces with their Configuration implementations.
13+ /// </summary>
14+ /// <param name="services">The service collection.</param>
15+ /// <returns>The service collection for chaining.</returns>
16+ public static IServiceCollection AddConfigurationAdapters ( this IServiceCollection services )
17+ {
18+ // Register adapters that map Configuration services to Core interfaces
19+ services . AddScoped < IModelProviderMappingService > ( provider =>
20+ new ModelProviderMappingServiceAdapter ( provider . GetRequiredService < ConduitLLM . Configuration . IModelProviderMappingService > ( ) ) ) ;
21+
22+ services . AddScoped < IProviderCredentialService > ( provider =>
23+ new ProviderCredentialServiceAdapter ( provider . GetRequiredService < ConduitLLM . Configuration . IProviderCredentialService > ( ) ) ) ;
24+
25+ services . AddScoped < IModelCostService > ( provider =>
26+ new ModelCostServiceAdapter ( provider . GetRequiredService < ConduitLLM . Configuration . Services . IModelCostService > ( ) ) ) ;
27+
28+ services . AddSingleton < ICacheService > ( provider =>
29+ new CacheServiceAdapter ( provider . GetRequiredService < ConduitLLM . Configuration . Services . ICacheService > ( ) ) ) ;
30+
31+ return services ;
32+ }
33+
34+ /// <summary>
35+ /// Adapter that wraps Configuration's IModelProviderMappingService to implement Core's interface.
36+ /// </summary>
37+ private class ModelProviderMappingServiceAdapter : IModelProviderMappingService
38+ {
39+ private readonly ConduitLLM . Configuration . IModelProviderMappingService _innerService ;
40+
41+ public ModelProviderMappingServiceAdapter ( ConduitLLM . Configuration . IModelProviderMappingService innerService )
42+ {
43+ _innerService = innerService ;
44+ }
45+
46+ public async Task < List < ModelProviderMapping > > GetAllMappingsAsync ( )
47+ {
48+ var mappings = await _innerService . GetAllMappingsAsync ( ) ;
49+ return mappings . Select ( m => new ModelProviderMapping
50+ {
51+ ModelAlias = m . ModelAlias ,
52+ ProviderName = m . ProviderName ,
53+ ProviderModelId = m . ProviderModelId ,
54+ DeploymentName = m . DeploymentName ,
55+ IsEnabled = true , // Default
56+ MaxContextTokens = null // Default
57+ } ) . ToList ( ) ;
58+ }
59+
60+ public async Task < ModelProviderMapping ? > GetMappingByModelAliasAsync ( string modelAlias )
61+ {
62+ var mapping = await _innerService . GetMappingByModelAliasAsync ( modelAlias ) ;
63+ if ( mapping == null ) return null ;
64+
65+ return new ModelProviderMapping
66+ {
67+ ModelAlias = mapping . ModelAlias ,
68+ ProviderName = mapping . ProviderName ,
69+ ProviderModelId = mapping . ProviderModelId ,
70+ DeploymentName = mapping . DeploymentName ,
71+ IsEnabled = true , // Default
72+ MaxContextTokens = null // Default
73+ } ;
74+ }
75+ }
76+
77+ /// <summary>
78+ /// Adapter that wraps Configuration's IProviderCredentialService to implement Core's interface.
79+ /// </summary>
80+ private class ProviderCredentialServiceAdapter : IProviderCredentialService
81+ {
82+ private readonly ConduitLLM . Configuration . IProviderCredentialService _innerService ;
83+
84+ public ProviderCredentialServiceAdapter ( ConduitLLM . Configuration . IProviderCredentialService innerService )
85+ {
86+ _innerService = innerService ;
87+ }
88+
89+ public async Task < ProviderCredentials ? > GetCredentialByProviderNameAsync ( string providerName )
90+ {
91+ var credential = await _innerService . GetCredentialByProviderNameAsync ( providerName ) ;
92+ if ( credential == null ) return null ;
93+
94+ return new ProviderCredentials
95+ {
96+ ProviderName = credential . ProviderName ,
97+ ApiKey = credential . ApiKey ,
98+ BaseUrl = credential . BaseUrl ,
99+ ApiVersion = credential . ApiVersion ,
100+ IsEnabled = credential . IsEnabled
101+ } ;
102+ }
103+ }
104+
105+ /// <summary>
106+ /// Adapter that wraps Configuration's IModelCostService to implement Core's interface.
107+ /// </summary>
108+ private class ModelCostServiceAdapter : IModelCostService
109+ {
110+ private readonly ConduitLLM . Configuration . Services . IModelCostService _innerService ;
111+
112+ public ModelCostServiceAdapter ( ConduitLLM . Configuration . Services . IModelCostService innerService )
113+ {
114+ _innerService = innerService ;
115+ }
116+
117+ public async Task < ModelCostInfo ? > GetCostForModelAsync ( string modelId , CancellationToken cancellationToken = default )
118+ {
119+ var modelCost = await _innerService . GetCostForModelAsync ( modelId , cancellationToken ) ;
120+ if ( modelCost == null ) return null ;
121+
122+ return new ModelCostInfo
123+ {
124+ ModelIdPattern = modelCost . ModelIdPattern ,
125+ InputTokenCost = modelCost . InputTokenCost ,
126+ OutputTokenCost = modelCost . OutputTokenCost ,
127+ EmbeddingTokenCost = modelCost . EmbeddingTokenCost ,
128+ ImageCostPerImage = modelCost . ImageCostPerImage
129+ } ;
130+ }
131+ }
132+
133+ /// <summary>
134+ /// Adapter that wraps Configuration's ICacheService to implement Core's interface.
135+ /// </summary>
136+ private class CacheServiceAdapter : ICacheService
137+ {
138+ private readonly ConduitLLM . Configuration . Services . ICacheService _innerService ;
139+
140+ public CacheServiceAdapter ( ConduitLLM . Configuration . Services . ICacheService innerService )
141+ {
142+ _innerService = innerService ;
143+ }
144+
145+ public T ? Get < T > ( string key ) => _innerService . Get < T > ( key ) ;
146+
147+ public void Set < T > ( string key , T value , TimeSpan ? absoluteExpiration = null , TimeSpan ? slidingExpiration = null )
148+ => _innerService . Set ( key , value , absoluteExpiration , slidingExpiration ) ;
149+
150+ public void Remove ( string key ) => _innerService . Remove ( key ) ;
151+
152+ public Task < T ? > GetOrCreateAsync < T > ( string key , Func < Task < T > > factory , TimeSpan ? absoluteExpiration = null , TimeSpan ? slidingExpiration = null )
153+ => _innerService . GetOrCreateAsync ( key , factory , absoluteExpiration , slidingExpiration ) ;
154+
155+ public void RemoveByPrefix ( string prefix ) => _innerService . RemoveByPrefix ( prefix ) ;
156+ }
157+ }
158+ }
0 commit comments