mirror of
				https://github.com/MindWorkAI/AI-Studio.git
				synced 2025-10-31 13:20:20 +00:00 
			
		
		
		
	Allow using a provisional API key
This commit is contained in:
		
							parent
							
								
									84e1f35f72
								
							
						
					
					
						commit
						9d70103b72
					
				| @ -48,16 +48,18 @@ public interface IProvider | ||||
|     /// </summary> | ||||
|     /// <param name="jsRuntime">The JS runtime to access the Rust code.</param> | ||||
|     /// <param name="settings">The settings manager to access the API key.</param> | ||||
|     /// <param name="apiKeyProvisional">The provisional API key to use. Useful when the user is adding a new provider. When null, the stored API key is used.</param> | ||||
|     /// <param name="token">The cancellation token.</param> | ||||
|     /// <returns>The list of text models.</returns> | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default); | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// Load all possible image models that can be used with this provider. | ||||
|     /// </summary> | ||||
|     /// <param name="jsRuntime">The JS runtime to access the Rust code.</param> | ||||
|     /// <param name="settings">The settings manager to access the API key.</param> | ||||
|     /// <param name="apiKeyProvisional">The provisional API key to use. Useful when the user is adding a new provider. When null, the stored API key is used.</param> | ||||
|     /// <param name="token">The cancellation token.</param> | ||||
|     /// <returns>The list of image models.</returns> | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default); | ||||
| } | ||||
| @ -13,9 +13,9 @@ public class NoProvider : IProvider | ||||
| 
 | ||||
|     public string InstanceName { get; set; } = "None"; | ||||
| 
 | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult<IEnumerable<Model>>([]); | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) => Task.FromResult<IEnumerable<Model>>([]); | ||||
| 
 | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult<IEnumerable<Model>>([]); | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) => Task.FromResult<IEnumerable<Model>>([]); | ||||
| 
 | ||||
|     public async IAsyncEnumerable<string> StreamChatCompletion(IJSRuntime jsRuntime, SettingsManager settings, Model chatModel, ChatThread chatChatThread, [EnumeratorCancellation] CancellationToken token = default) | ||||
|     { | ||||
|  | ||||
| @ -151,27 +151,36 @@ public sealed class ProviderOpenAI() : BaseProvider("https://api.openai.com/v1/" | ||||
|     #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously | ||||
| 
 | ||||
|     /// <inheritdoc /> | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) | ||||
|     public Task<IEnumerable<Model>> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) | ||||
|     { | ||||
|         return this.LoadModels(jsRuntime, settings, "gpt-", token); | ||||
|         return this.LoadModels(jsRuntime, settings, "gpt-", token, apiKeyProvisional); | ||||
|     } | ||||
| 
 | ||||
|     /// <inheritdoc /> | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) | ||||
|     public Task<IEnumerable<Model>> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) | ||||
|     { | ||||
|         return this.LoadModels(jsRuntime, settings, "dall-e-", token); | ||||
|         return this.LoadModels(jsRuntime, settings, "dall-e-", token, apiKeyProvisional); | ||||
|     } | ||||
| 
 | ||||
|     #endregion | ||||
| 
 | ||||
|     private async Task<IEnumerable<Model>> LoadModels(IJSRuntime jsRuntime, SettingsManager settings, string prefix, CancellationToken token) | ||||
|     private async Task<IEnumerable<Model>> LoadModels(IJSRuntime jsRuntime, SettingsManager settings, string prefix, CancellationToken token, string? apiKeyProvisional = null) | ||||
|     { | ||||
|         var requestedSecret = await settings.GetAPIKey(jsRuntime, this); | ||||
|         if (!requestedSecret.Success) | ||||
|         var secretKey = apiKeyProvisional switch | ||||
|         { | ||||
|             not null => apiKeyProvisional, | ||||
|             _ => await settings.GetAPIKey(jsRuntime, this) switch | ||||
|             { | ||||
|                 { Success: true } result => result.Secret, | ||||
|                 _ => null, | ||||
|             } | ||||
|         }; | ||||
| 
 | ||||
|         if (secretKey is null) | ||||
|             return []; | ||||
|          | ||||
|         var request = new HttpRequestMessage(HttpMethod.Get, "models"); | ||||
|         request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", requestedSecret.Secret); | ||||
|         request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey); | ||||
| 
 | ||||
|         var response = await this.httpClient.SendAsync(request, token); | ||||
|         if(!response.IsSuccessStatusCode) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user