mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-25 04:33:48 +00:00
Added a check for characters which an API key cannot contain (#1227)
This commit is contained in:
1 parent
c72a1948b3
commit
e5a7f5423f
40 files changed
+1179
-71
No files matched your search
@@ -139,7 +139,7 @@ public sealed class ProviderAnthropic() : BaseProvider(LLMProviders.ANTHROPIC, n
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "messages");
|
||||
|
||||
// Set the authorization header:
|
||||
request.Headers.Add("x-api-key", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Add("x-api-key", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
// Set the Anthropic version:
|
||||
request.Headers.Add("anthropic-version", "2023-06-01");
|
||||
@@ -166,7 +166,7 @@ public sealed class ProviderAnthropic() : BaseProvider(LLMProviders.ANTHROPIC, n
|
||||
async Task<HttpRequestMessage> RequestBuilder()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "messages");
|
||||
request.Headers.Add("x-api-key", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Add("x-api-key", await this.DecryptAPIKey(requestedSecret));
|
||||
request.Headers.Add("anthropic-version", "2023-06-01");
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(requestDto, JSON_SERIALIZER_OPTIONS), Encoding.UTF8, "application/json");
|
||||
return request;
|
||||
|
||||
@@ -15,6 +15,7 @@ using AIStudio.Tools.ToolCallingSystem.Harness;
|
||||
using AIStudio.Tools.MIME;
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
using AIStudio.Tools.Rust;
|
||||
using AIStudio.Tools.Validation;
|
||||
|
||||
using Host = AIStudio.Provider.SelfHosted.Host;
|
||||
|
||||
@@ -148,6 +149,11 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
|
||||
protected static ModelLoadResult FailedModelLoadResult(ModelLoadFailureReason failureReason, string? technicalDetails = null) => ModelLoadResult.Failure(failureReason, technicalDetails);
|
||||
|
||||
/// <summary>
|
||||
/// The result of loading the models with a key which cannot be sent, so that nothing was requested.
|
||||
/// </summary>
|
||||
protected static ModelLoadResult UnusableAPIKeyModelLoadResult() => FailedModelLoadResult(ModelLoadFailureReason.INVALID_API_KEY_CHARACTERS, "The API key contains a character which an HTTP header cannot carry.");
|
||||
|
||||
protected bool IsTimeoutException(Exception exception, CancellationToken token = default)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
@@ -165,15 +171,72 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
ExternalHttpClientTimeout.GetTimeoutDescription(),
|
||||
action)));
|
||||
|
||||
protected async Task<string?> GetModelLoadingSecretKey(SecretStoreType storeType, string? apiKeyProvisional = null, bool isTryingSecret = false) => apiKeyProvisional switch
|
||||
/// <summary>
|
||||
/// Gets the API key for loading the models: the one the user is entering, or else the stored one.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Either is checked before it goes into a header. A key which the header cannot carry would
|
||||
/// otherwise fail in the HTTP stack, with a message nobody understands.
|
||||
/// </remarks>
|
||||
/// <param name="storeType">Where the stored key is kept.</param>
|
||||
/// <param name="apiKeyProvisional">The key the user is entering and did not store yet, or null for the stored one.</param>
|
||||
/// <param name="isTryingSecret">Whether a missing key is fine, so that the keyring logs no error for it.</param>
|
||||
/// <returns>The key without the whitespace around it, or whether it cannot be sent.</returns>
|
||||
protected async Task<ModelLoadingSecretKey> GetModelLoadingSecretKey(SecretStoreType storeType, string? apiKeyProvisional = null, bool isTryingSecret = false)
|
||||
{
|
||||
not null => apiKeyProvisional,
|
||||
_ => await Program.RUST_SERVICE.GetAPIKey(this, storeType, isTrying: isTryingSecret) switch
|
||||
var key = apiKeyProvisional;
|
||||
if (key is null)
|
||||
{
|
||||
{ Success: true } result => await result.Secret.Decrypt(Program.ENCRYPTION),
|
||||
_ => null,
|
||||
var requestedSecret = await Program.RUST_SERVICE.GetAPIKey(this, storeType, isTrying: isTryingSecret);
|
||||
if (!requestedSecret.Success)
|
||||
return ModelLoadingSecretKey.NONE;
|
||||
|
||||
key = await requestedSecret.Secret.Decrypt(Program.ENCRYPTION);
|
||||
}
|
||||
};
|
||||
|
||||
var issue = APIKeyValidation.Check(key);
|
||||
if (!issue.IsProblem)
|
||||
return new(APIKeyValidation.Normalize(key), false);
|
||||
|
||||
this.logger.LogWarning("The API key for loading the models of '{ProviderInstanceName}' (provider={ProviderType}) contains a character which an API key cannot contain ({IssueKind}). The models were not requested.", this.InstanceName, this.Provider, issue.Kind);
|
||||
return ModelLoadingSecretKey.UNUSABLE;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a stored API key for the header of a request.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Every stored key which goes into a header goes through here, so that a key with a character
|
||||
/// the header cannot carry never reaches the HTTP stack: its messages about such keys mean
|
||||
/// nothing to the user, and some of them even quote the key. Such a key was stored before AI
|
||||
/// Studio checked keys, or it reached the keyring some other way. The request then fails with a
|
||||
/// ProviderRequestException, which tells the user to enter the key again. APIKeyHeaderTests
|
||||
/// checks that no provider decrypts a key on its own.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// A key which is empty without the whitespace around it fails as a missing key: a header with
|
||||
/// an empty key would only earn a rejection from the provider.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="requestedSecret">The stored key as the keyring returned it.</param>
|
||||
/// <returns>The key without the whitespace around it.</returns>
|
||||
protected async Task<string> DecryptAPIKey(RequestedSecret requestedSecret)
|
||||
{
|
||||
var key = await requestedSecret.Secret.Decrypt(Program.ENCRYPTION);
|
||||
var issue = APIKeyValidation.Check(key);
|
||||
if (issue.IsProblem)
|
||||
{
|
||||
this.logger.LogWarning("The stored API key of '{ProviderInstanceName}' (provider={ProviderType}) contains a character which an API key cannot contain ({IssueKind}). The request was not sent.", this.InstanceName, this.Provider, issue.Kind);
|
||||
throw new ProviderRequestException(ProviderRequestFailureReason.INVALID_API_KEY_CHARACTERS, this.GetProviderRequestFailureUserMessage(ProviderRequestFailureReason.INVALID_API_KEY_CHARACTERS));
|
||||
}
|
||||
|
||||
var headerValue = APIKeyValidation.Normalize(key);
|
||||
if (headerValue.Length is 0)
|
||||
throw new ProviderRequestException(ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY, this.GetProviderRequestFailureUserMessage(ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY));
|
||||
|
||||
return headerValue;
|
||||
}
|
||||
|
||||
protected static ModelLoadFailureReason GetDefaultModelLoadFailureReason(HttpResponseMessage response) => response.StatusCode switch
|
||||
{
|
||||
@@ -203,14 +266,17 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var secretKey = await this.GetModelLoadingSecretKey(storeType, apiKeyProvisional, isTryingSecret);
|
||||
if (string.IsNullOrWhiteSpace(secretKey) && !isTryingSecret)
|
||||
if (secretKey.IsUnusable)
|
||||
return UnusableAPIKeyModelLoadResult();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(secretKey.Key) && !isTryingSecret)
|
||||
return FailedModelLoadResult(ModelLoadFailureReason.INVALID_OR_MISSING_API_KEY, "No API key available for model loading.");
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestPath);
|
||||
if (requestConfigurator is not null)
|
||||
requestConfigurator(request, secretKey ?? string.Empty);
|
||||
else if (!string.IsNullOrWhiteSpace(secretKey))
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey);
|
||||
requestConfigurator(request, secretKey.Key ?? string.Empty);
|
||||
else if (!string.IsNullOrWhiteSpace(secretKey.Key))
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey.Key);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -269,6 +335,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
{
|
||||
ProviderRequestFailureReason.TOO_MANY_REQUESTS => TA("The provider rejected the request because too many requests were sent. Please wait a moment and try again."),
|
||||
ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY => string.Format(TA("The API key for the provider '{0}' is missing or was rejected. Please check the key in the settings."), this.NameForUser),
|
||||
ProviderRequestFailureReason.INVALID_API_KEY_CHARACTERS => APIKeyValidation.GetUnusableKeyMessage(this.NameForUser),
|
||||
ProviderRequestFailureReason.AUTHENTICATION_OR_PERMISSION_ERROR => string.Format(TA("The provider '{0}' refused the request. Your account might not be allowed to use the selected model, or the provider might not serve your region."), this.NameForUser),
|
||||
ProviderRequestFailureReason.PROVIDER_UNAVAILABLE => string.Format(TA("The provider '{0}' could not be reached. Please check whether it is running and reachable, then try again."), this.NameForUser),
|
||||
ProviderRequestFailureReason.MODEL_NOT_FOUND => string.Format(TA("The provider '{0}' does not know the selected model. Please select another model."), this.NameForUser),
|
||||
@@ -1460,7 +1527,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
|
||||
// Set the authorization header:
|
||||
if (requestedSecret.Success)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
// Set provider-specific headers:
|
||||
requestHeadersAction?.Invoke(request.Headers);
|
||||
@@ -1514,7 +1581,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, requestPath);
|
||||
if (requestedSecret.Success)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
headersAction?.Invoke(request.Headers);
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(requestDto, JSON_SERIALIZER_OPTIONS), Encoding.UTF8, "application/json");
|
||||
@@ -1623,7 +1690,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
{
|
||||
case LLMProviders.SELF_HOSTED:
|
||||
if(requestedSecret.Success)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
break;
|
||||
|
||||
@@ -1634,7 +1701,13 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
return TranscriptionResult.Failure();
|
||||
}
|
||||
|
||||
request.Headers.Add("Authorization", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
//
|
||||
// Fireworks gets the bare key, without the scheme 'Bearer'. Headers.Add would parse the key as
|
||||
// an Authorization header, refuse one with a character such as '/' or '=', and quote the whole
|
||||
// key in its exception. DecryptAPIKey already made sure that the key holds only printable ASCII,
|
||||
// so it goes out as it is:
|
||||
//
|
||||
request.Headers.TryAddWithoutValidation("Authorization", await this.DecryptAPIKey(requestedSecret));
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -1644,7 +1717,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
return TranscriptionResult.Failure();
|
||||
}
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1677,6 +1750,11 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (ProviderRequestException e)
|
||||
{
|
||||
// We know what went wrong, e.g., a stored key which cannot be sent, and the user gets to read it:
|
||||
return TranscriptionResult.Failure(e.UserMessage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (this.IsTimeoutException(e, token))
|
||||
@@ -1713,7 +1791,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
{
|
||||
case LLMProviders.SELF_HOSTED:
|
||||
if(requestedSecret.Success)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
break;
|
||||
|
||||
@@ -1724,7 +1802,7 @@ public abstract class BaseProvider : IProvider, ISecretId
|
||||
throw new ProviderRequestException(ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY, this.GetProviderRequestFailureUserMessage(ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY));
|
||||
}
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ public class ProviderGoogle() : BaseProvider(LLMProviders.GOOGLE, new Uri("https
|
||||
var embeddingRequest = JsonSerializer.Serialize(payload, JSON_SERIALIZER_OPTIONS);
|
||||
var embedUrl = $"https://generativelanguage.googleapis.com/v1beta/models/{modelName}:embedContent";
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, embedUrl);
|
||||
request.Headers.Add("x-goog-api-key", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Add("x-goog-api-key", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
// Set the content:
|
||||
request.Content = new StringContent(embeddingRequest, Encoding.UTF8, "application/json");
|
||||
|
||||
@@ -126,13 +126,16 @@ public sealed class ProviderHelmholtz() : BaseProvider(LLMProviders.HELMHOLTZ, n
|
||||
private async Task<ModelLoadResult> LoadModels(SecretStoreType storeType, string? apiKeyProvisional, CancellationToken token)
|
||||
{
|
||||
var secretKey = await this.GetModelLoadingSecretKey(storeType, apiKeyProvisional);
|
||||
if (string.IsNullOrWhiteSpace(secretKey))
|
||||
if (secretKey.IsUnusable)
|
||||
return UnusableAPIKeyModelLoadResult();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(secretKey.Key))
|
||||
return FailedModelLoadResult(ModelLoadFailureReason.INVALID_OR_MISSING_API_KEY, "No API key available for model loading.");
|
||||
|
||||
try
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "models");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey.Key);
|
||||
|
||||
using var response = await this.HttpClient.SendAsync(request, token);
|
||||
var body = await response.Content.ReadAsStringAsync(token);
|
||||
|
||||
@@ -4,6 +4,12 @@ public enum ModelLoadFailureReason
|
||||
{
|
||||
NONE,
|
||||
INVALID_OR_MISSING_API_KEY,
|
||||
|
||||
/// <summary>
|
||||
/// The API key contains a character which an HTTP header cannot carry, so the models were never requested.
|
||||
/// </summary>
|
||||
INVALID_API_KEY_CHARACTERS,
|
||||
|
||||
AUTHENTICATION_OR_PERMISSION_ERROR,
|
||||
INSUFFICIENT_QUOTA,
|
||||
TOO_MANY_REQUESTS,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
using AIStudio.Tools.Validation;
|
||||
|
||||
namespace AIStudio.Provider;
|
||||
|
||||
@@ -13,6 +14,7 @@ public static class ModelLoadFailureReasonExtensions
|
||||
public string ToUserMessage(string providerName) => failureReason switch
|
||||
{
|
||||
ModelLoadFailureReason.INVALID_OR_MISSING_API_KEY => string.Format(TB("We could not load models from '{0}'. The API key is probably missing, invalid, or expired."), providerName),
|
||||
ModelLoadFailureReason.INVALID_API_KEY_CHARACTERS => APIKeyValidation.GetUnusableKeyMessage(providerName),
|
||||
ModelLoadFailureReason.AUTHENTICATION_OR_PERMISSION_ERROR => string.Format(TB("We could not load models from '{0}'. The account or API key does not have the required permissions."), providerName),
|
||||
ModelLoadFailureReason.INSUFFICIENT_QUOTA => string.Format(TB("We could not load models from '{0}' because the account appears to have no API credits left."), providerName),
|
||||
ModelLoadFailureReason.TOO_MANY_REQUESTS => string.Format(TA("We could not load models from '{0}' because too many requests were sent. Please wait a moment and try again."), providerName),
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace AIStudio.Provider;
|
||||
|
||||
/// <summary>
|
||||
/// The API key for loading the models of a provider, ready for a header.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A plain struct instead of a record on purpose: the generated ToString of a record would print the
|
||||
/// key wherever somebody logs this value by mistake.
|
||||
/// </remarks>
|
||||
/// <param name="key">The key without the whitespace around it, or null when there is none or it cannot be sent.</param>
|
||||
/// <param name="isUnusable">Whether the key contains a character which an HTTP header cannot carry.</param>
|
||||
public readonly struct ModelLoadingSecretKey(string? key, bool isUnusable)
|
||||
{
|
||||
/// <summary>
|
||||
/// No key at all, e.g., because none is stored.
|
||||
/// </summary>
|
||||
public static readonly ModelLoadingSecretKey NONE = new(null, false);
|
||||
|
||||
/// <summary>
|
||||
/// A key which cannot be sent, and which therefore is not handed out.
|
||||
/// </summary>
|
||||
public static readonly ModelLoadingSecretKey UNUSABLE = new(null, true);
|
||||
|
||||
/// <summary>
|
||||
/// The key without the whitespace around it, or null when there is none or it cannot be sent.
|
||||
/// </summary>
|
||||
public string? Key { get; } = key;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the key contains a character which an HTTP header cannot carry.
|
||||
/// </summary>
|
||||
public bool IsUnusable { get; } = isUnusable;
|
||||
}
|
||||
@@ -272,7 +272,7 @@ public sealed class ProviderOpenAI() : BaseProvider(LLMProviders.OPEN_AI, new Ur
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, requestPath);
|
||||
|
||||
// Set the authorization header:
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
|
||||
// Set the content:
|
||||
request.Content = new StringContent(openAIChatRequest, Encoding.UTF8, "application/json");
|
||||
@@ -303,7 +303,7 @@ public sealed class ProviderOpenAI() : BaseProvider(LLMProviders.OPEN_AI, new Ur
|
||||
async Task<HttpRequestMessage> RequestBuilder()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "responses");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(Program.ENCRYPTION));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await this.DecryptAPIKey(requestedSecret));
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(requestDto, JSON_SERIALIZER_OPTIONS), Encoding.UTF8, "application/json");
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,16 @@ public enum ProviderRequestFailureReason
|
||||
/// </remarks>
|
||||
INVALID_OR_MISSING_API_KEY,
|
||||
|
||||
/// <summary>
|
||||
/// The stored API key contains a character which an HTTP header cannot carry, so the request was never sent.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Such a key was stored before AI Studio checked keys, or reached the keyring some other way.
|
||||
/// The provider never saw it, so this is no rejection of its own: the user has to enter the key
|
||||
/// again, or ask whoever provides it.
|
||||
/// </remarks>
|
||||
INVALID_API_KEY_CHARACTERS,
|
||||
|
||||
/// <summary>
|
||||
/// The key was accepted, but the account is not allowed to do what we asked for.
|
||||
/// </summary>
|
||||
|
||||
@@ -21,6 +21,7 @@ public static class ProviderRequestFailureReasonExtensions
|
||||
ProviderRequestFailureReason.TOO_MANY_REQUESTS => TB("Too many requests"),
|
||||
ProviderRequestFailureReason.MODEL_NOT_SUPPORTED_BY_PROVIDER => TB("Model not offered"),
|
||||
ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY => TB("API key problem"),
|
||||
ProviderRequestFailureReason.INVALID_API_KEY_CHARACTERS => TB("API key problem"),
|
||||
ProviderRequestFailureReason.AUTHENTICATION_OR_PERMISSION_ERROR => TB("Not permitted"),
|
||||
ProviderRequestFailureReason.PROVIDER_UNAVAILABLE => TB("Provider unreachable"),
|
||||
ProviderRequestFailureReason.MODEL_NOT_FOUND => TB("Model unknown"),
|
||||
@@ -41,6 +42,7 @@ public static class ProviderRequestFailureReasonExtensions
|
||||
/// </remarks>
|
||||
public bool IsFixedInProviderSettings => failureReason is
|
||||
ProviderRequestFailureReason.INVALID_OR_MISSING_API_KEY or
|
||||
ProviderRequestFailureReason.INVALID_API_KEY_CHARACTERS or
|
||||
ProviderRequestFailureReason.AUTHENTICATION_OR_PERMISSION_ERROR or
|
||||
ProviderRequestFailureReason.MODEL_NOT_FOUND or
|
||||
ProviderRequestFailureReason.MODEL_NOT_SUPPORTED_BY_PROVIDER or
|
||||
|
||||
@@ -195,6 +195,8 @@ public sealed class ProviderSelfHosted(Host host, string hostname) : BaseProvide
|
||||
private async Task<ModelLoadResult> LoadModels(SecretStoreType storeType, string? apiKeyProvisional, CancellationToken token)
|
||||
{
|
||||
var secretKey = await this.GetModelLoadingSecretKey(storeType, apiKeyProvisional, isTryingSecret: true);
|
||||
if (secretKey.IsUnusable)
|
||||
return UnusableAPIKeyModelLoadResult();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -204,8 +206,8 @@ public sealed class ProviderSelfHosted(Host host, string hostname) : BaseProvide
|
||||
// rejects an empty bearer with 401, where it would have let a request without any
|
||||
// authorization header through. The dialogs hand us their key field as it stands, so
|
||||
// an empty string arrives here whenever the user stored no key:
|
||||
if(!string.IsNullOrWhiteSpace(secretKey))
|
||||
lmStudioRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey);
|
||||
if(!string.IsNullOrWhiteSpace(secretKey.Key))
|
||||
lmStudioRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey.Key);
|
||||
|
||||
using var lmStudioResponse = await this.HttpClient.SendAsync(lmStudioRequest, token);
|
||||
if(!lmStudioResponse.IsSuccessStatusCode)
|
||||
@@ -288,12 +290,14 @@ public sealed class ProviderSelfHosted(Host host, string hostname) : BaseProvide
|
||||
private async Task<ModelLoadResult> LoadLlamaCppTextModels(string? apiKeyProvisional, CancellationToken token)
|
||||
{
|
||||
var secretKey = await this.GetModelLoadingSecretKey(SecretStoreType.LLM_PROVIDER, apiKeyProvisional, true);
|
||||
if (secretKey.IsUnusable)
|
||||
return UnusableAPIKeyModelLoadResult();
|
||||
|
||||
try
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "models");
|
||||
if (!string.IsNullOrWhiteSpace(secretKey))
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey);
|
||||
if (!string.IsNullOrWhiteSpace(secretKey.Key))
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey.Key);
|
||||
|
||||
using var response = await this.HttpClient.SendAsync(request, token);
|
||||
var responseBody = await response.Content.ReadAsStringAsync(token);
|
||||
|
||||
Reference in new issue
Block a user