mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-05 16:49:40 +00:00
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
287 lines
10 KiB
C#
287 lines
10 KiB
C#
using AIStudio.Provider;
|
|
using AIStudio.Settings;
|
|
using AIStudio.Settings.DataModel;
|
|
using AIStudio.Tools.ERIClient.DataModel;
|
|
using AIStudio.Tools.PluginSystem;
|
|
|
|
namespace AIStudio.Tools.Validation;
|
|
|
|
public sealed class DataSourceValidation
|
|
{
|
|
public const int MAX_NAME_LENGTH = 40;
|
|
|
|
public const int MIN_ATTACHMENT_SIZE_MEGABYTES = 1;
|
|
|
|
public const int MAX_ATTACHMENT_SIZE_MEGABYTES = 2048;
|
|
|
|
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(DataSourceValidation).Namespace, nameof(DataSourceValidation));
|
|
|
|
public static bool IsNameValid(string name) => !string.IsNullOrWhiteSpace(name) && name.Length <= MAX_NAME_LENGTH && !name.Any(char.IsControl);
|
|
|
|
public Func<string> GetSecretStorageIssue { get; init; } = () => string.Empty;
|
|
|
|
public Func<string> GetPreviousDataSourceName { get; init; } = () => string.Empty;
|
|
|
|
public Func<IEnumerable<string>> GetUsedDataSourceNames { get; init; } = () => [];
|
|
|
|
public Func<AuthMethod> GetAuthMethod { get; init; } = () => AuthMethod.NONE;
|
|
|
|
public Func<SecurityRequirements?> GetSecurityRequirements { get; init; } = () => null;
|
|
|
|
public Func<bool> GetSelectedCloudEmbedding { get; init; } = () => false;
|
|
|
|
public Func<EmbeddingProvider?> GetSelectedEmbeddingProvider { get; init; } = () => null;
|
|
|
|
public Func<ConfidenceLevel> GetConfidenceLevel { get; init; } = () => ConfidenceLevel.NONE;
|
|
|
|
public Func<SettingsManager?> GetSettingsManager { get; init; } = () => null;
|
|
|
|
public Func<bool> GetTestedConnection { get; init; } = () => false;
|
|
|
|
public Func<bool> GetTestedConnectionResult { get; init; } = () => false;
|
|
|
|
public Func<IReadOnlyList<AuthMethod>> GetAvailableAuthMethods { get; init; } = () => [];
|
|
|
|
public string? ValidatingHostname(string hostname)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(hostname))
|
|
return TB("Please enter a hostname, e.g., http://localhost");
|
|
|
|
if(!hostname.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) && !hostname.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
|
|
return TB("The hostname must start with either http:// or https://");
|
|
|
|
if(!Uri.TryCreate(hostname, UriKind.Absolute, out _))
|
|
return TB("The hostname is not a valid HTTP(S) URL.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidatePort(int port)
|
|
{
|
|
if(port is < 1 or > 65535)
|
|
return TB("The port must be between 1 and 65535.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateSecurityPolicy(DataSourceSecurity securityPolicy)
|
|
{
|
|
if(securityPolicy is DataSourceSecurity.NOT_SPECIFIED)
|
|
return TB("Please select your security policy.");
|
|
|
|
var dataSourceSecurity = this.GetSecurityRequirements();
|
|
if (dataSourceSecurity is null)
|
|
return null;
|
|
|
|
if(dataSourceSecurity.Value.AllowedProviderType is ProviderType.SELF_HOSTED && securityPolicy is not DataSourceSecurity.SELF_HOSTED)
|
|
return TB("This data source can only be used with a self-hosted LLM provider. Please change the security policy.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateUsername(string username)
|
|
{
|
|
if(this.GetAuthMethod() is not AuthMethod.USERNAME_PASSWORD)
|
|
return null;
|
|
|
|
if(string.IsNullOrWhiteSpace(username))
|
|
return TB("The username must not be empty.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidatingSecret(string secret)
|
|
{
|
|
var authMethod = this.GetAuthMethod();
|
|
if(authMethod is AuthMethod.NONE or AuthMethod.KERBEROS)
|
|
return null;
|
|
|
|
var secretStorageIssue = this.GetSecretStorageIssue();
|
|
if(!string.IsNullOrWhiteSpace(secretStorageIssue))
|
|
return secretStorageIssue;
|
|
|
|
if (string.IsNullOrWhiteSpace(secret))
|
|
return authMethod switch
|
|
{
|
|
AuthMethod.TOKEN => TB("Please enter your secure access token."),
|
|
AuthMethod.USERNAME_PASSWORD => TB("Please enter your password."),
|
|
|
|
_ => TB("Please enter the secret necessary for authentication.")
|
|
};
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateRetrievalProcess(RetrievalInfo retrievalInfo)
|
|
{
|
|
if(retrievalInfo == default)
|
|
return TB("Please select one retrieval process.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidatingName(string dataSourceName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(dataSourceName))
|
|
return TB("The name must not be empty.");
|
|
|
|
if (dataSourceName.Length > MAX_NAME_LENGTH)
|
|
return TB("The name must not exceed 40 characters.");
|
|
|
|
if (dataSourceName.Any(char.IsControl))
|
|
return TB("The name must not contain control characters.");
|
|
|
|
var lowerName = dataSourceName.ToLowerInvariant();
|
|
if(lowerName != this.GetPreviousDataSourceName() && this.GetUsedDataSourceNames().Contains(lowerName))
|
|
return TB("The name is already used by another data source. Please choose a different name.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidatePath(string path)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(path))
|
|
return TB("The path must not be empty. Please select a directory.");
|
|
|
|
if(!Directory.Exists(path))
|
|
return TB("The path does not exist. Please select a valid directory.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateFilePath(string filePath)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(filePath))
|
|
return TB("The file path must not be empty. Please select a file.");
|
|
|
|
if(!File.Exists(filePath))
|
|
return TB("The file does not exist. Please select a valid file.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateEmbeddingId(string embeddingId)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(embeddingId))
|
|
return TB("Please select an embedding provider.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateEmbeddingProviderAccess(string embeddingId)
|
|
{
|
|
var embeddingIssue = this.ValidateEmbeddingId(embeddingId);
|
|
return embeddingIssue ?? this.ValidateSelectedEmbeddingProviderAccess();
|
|
}
|
|
|
|
public string? ValidateDataSourceConfidenceLevel(ConfidenceLevel confidenceLevel)
|
|
{
|
|
if(confidenceLevel is ConfidenceLevel.NONE)
|
|
return TB("Please select a required provider confidence level.");
|
|
|
|
return this.ValidateSelectedEmbeddingProviderAccess();
|
|
}
|
|
|
|
public string? ValidateMailboxConfidenceLevel(ConfidenceLevel confidenceLevel)
|
|
{
|
|
if(confidenceLevel is ConfidenceLevel.NONE)
|
|
return TB("Please select a required provider confidence level.");
|
|
|
|
if(!confidenceLevel.IsAllowedMailboxConfidence())
|
|
return string.Format(TB("A mailbox requires a provider confidence level from '{0}' to '{1}'."), ConfidenceLevel.VERY_LOW.GetName(), ConfidenceLevel.HIGH.GetName());
|
|
|
|
return this.ValidateSelectedEmbeddingProviderAccess();
|
|
}
|
|
|
|
public static string? ValidateMailboxHost(string host)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(host))
|
|
return TB("Please enter the host of the IMAP server, e.g., imap.example.org.");
|
|
|
|
if(Uri.CheckHostName(host.Trim()) is not (UriHostNameType.Dns or UriHostNameType.IPv4 or UriHostNameType.IPv6))
|
|
return TB("Please enter the host alone, without a protocol, a port, or a path, e.g., imap.example.org.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string? ValidateMailboxTransportSecurity(MailboxTransportSecurity transportSecurity)
|
|
{
|
|
if(transportSecurity is MailboxTransportSecurity.UNKNOWN)
|
|
return TB("Please select how the connection to the server is encrypted.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string? ValidateMailboxUsername(string username)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(username))
|
|
return TB("The username must not be empty.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateMailboxPassword(string password)
|
|
{
|
|
var secretStorageIssue = this.GetSecretStorageIssue();
|
|
if(!string.IsNullOrWhiteSpace(secretStorageIssue))
|
|
return secretStorageIssue;
|
|
|
|
if(string.IsNullOrEmpty(password))
|
|
return TB("Please enter your password.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string? ValidateMailboxMaxAttachmentSize(int megabytes)
|
|
{
|
|
if(megabytes is < MIN_ATTACHMENT_SIZE_MEGABYTES or > MAX_ATTACHMENT_SIZE_MEGABYTES)
|
|
return string.Format(TB("The size must be between {0} and {1} MB."), MIN_ATTACHMENT_SIZE_MEGABYTES, MAX_ATTACHMENT_SIZE_MEGABYTES);
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateUserAcknowledgedCloudEmbedding(bool value)
|
|
{
|
|
if(this.GetSelectedCloudEmbedding() && !value)
|
|
return TB("Please acknowledge that you are aware of the cloud embedding implications.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateTestedConnection()
|
|
{
|
|
if(!this.GetTestedConnection())
|
|
return TB("Please test the connection before saving.");
|
|
|
|
if(!this.GetTestedConnectionResult())
|
|
return TB("The connection test failed. Please check the connection settings.");
|
|
|
|
return null;
|
|
}
|
|
|
|
public string? ValidateAuthMethod(AuthMethod authMethod)
|
|
{
|
|
if(!this.GetAvailableAuthMethods().Contains(authMethod))
|
|
return TB("Please select one valid authentication method.");
|
|
|
|
return null;
|
|
}
|
|
|
|
private string? ValidateSelectedEmbeddingProviderAccess()
|
|
{
|
|
var selectedEmbedding = this.GetSelectedEmbeddingProvider();
|
|
var settingsManager = this.GetSettingsManager();
|
|
if(selectedEmbedding is null || settingsManager is null)
|
|
return null;
|
|
|
|
var confidenceLevel = this.GetConfidenceLevel();
|
|
if(selectedEmbedding.GetConfidenceLevel(settingsManager).AllowsDataSourceConfidenceLevel(confidenceLevel))
|
|
return null;
|
|
|
|
return string.Format(
|
|
TB("The selected embedding provider has confidence '{0}', but this data source requires provider confidence '{1}'. Select an embedding provider with equal or higher confidence or lower the required confidence level."),
|
|
selectedEmbedding.GetConfidenceLevel(settingsManager).GetName(),
|
|
confidenceLevel.GetName());
|
|
}
|
|
}
|