using System.Text.Json.Serialization;
using AIStudio.Provider;
using AIStudio.Tools.Services;
namespace AIStudio.Settings.DataModel;
///
/// An e-mail mailbox on an IMAP server, which AI Studio embeds and indexes itself.
///
///
/// Mailboxes are kept in Data.Mailboxes rather than in DataSources, which is why this is no
/// IDataSource: classic RAG, Semantic Search and the agents never see a mailbox. Only the mail
/// tools read from one.
///
public readonly record struct DataSourceMailbox : IIndexedDataSource, ISecretId
{
public DataSourceMailbox()
{
}
///
public uint Num { get; init; }
///
public string Id { get; init; } = Guid.Empty.ToString();
///
public string Name { get; init; } = string.Empty;
///
public DataSourceType Type { get; init; } = DataSourceType.MAILBOX;
///
public bool IsEnterpriseConfiguration { get; init; }
///
public Guid EnterpriseConfigurationPluginId { get; init; } = Guid.Empty;
///
public string EmbeddingId { get; init; } = Guid.Empty.ToString();
///
public int MaxChunkTokenLength { get; init; }
///
public int ChunkOverlapTokenLength { get; init; } = DataSourceEmbeddingService.DEFAULT_CHUNK_OVERLAP_TOKEN_LENGTH;
///
///
/// There is no default, the user has to choose one of the levels which IsAllowedMailboxConfidence
/// accepts. Until then, the level is NONE, and no provider may read the mailbox.
///
public ConfidenceLevel ConfidenceLevel { get; init; } = ConfidenceLevel.NONE;
///
/// The host name of the IMAP server, e.g., imap.example.org.
///
public string Host { get; init; } = string.Empty;
///
/// The port of the IMAP server. The default is the one for IMAP with TLS from the first byte on.
///
public int Port { get; init; } = MailboxTransportSecurityExtensions.SSL_ON_CONNECT_PORT;
///
/// How the connection to the IMAP server is encrypted.
///
public MailboxTransportSecurity TransportSecurity { get; init; } = MailboxTransportSecurity.SSL_ON_CONNECT;
///
/// How AI Studio signs in to the IMAP server.
///
public MailboxAuthMethod AuthMethod { get; init; } = MailboxAuthMethod.PASSWORD;
///
/// The username to sign in with, often the e-mail address.
///
public string Username { get; init; } = string.Empty;
///
/// The folder to which the synchronization and all mail tools are restricted, together with its subfolders.
///
///
/// The full path as the server names it, including the server's own hierarchy delimiter. Empty
/// means the whole mailbox, apart from the trash and the junk folder.
///
public string RootFolder { get; init; } = string.Empty;
///
/// How far back the index reaches. Flagged mails are indexed regardless of their age.
///
public MailboxMaxAge MaxAge { get; init; } = MailboxMaxAge.LAST_12_MONTHS;
///
/// Whether the text of attached documents is indexed as well.
///
public bool IndexAttachments { get; init; } = true;
///
/// The size in megabytes up to which the text of an attachment is indexed. Of a larger one, only the name is.
///
public int MaxAttachmentSizeMegabytes { get; init; } = 10;
///
/// Where a chat may still send data, once it has read from this mailbox.
///
///
/// An organization may demand a stricter one, see DataMailboxes.MinimumOutboundDataRestriction.
///
public OutboundDataRestriction OutboundDataRestriction { get; init; } = OutboundDataRestriction.ONLY_CONFIGURED_SERVICES;
///
/// The maximum number of mails one search returns. Searched page by page, it is the size of a page.
///
public ushort MaxMatches { get; init; } = 10;
#region Implementation of ISecretId
///
/// The OS keyring stores the password under this ID together with the name of the mailbox, so
/// that the user recognizes the entry there. Renaming a mailbox therefore stores the password
/// anew, and deletes the old entry.
///
[JsonIgnore]
string ISecretId.SecretId => this.IsEnterpriseConfiguration ? $"{ISecretId.ENTERPRISE_KEY_PREFIX}::{this.Id}" : this.Id;
[JsonIgnore]
string ISecretId.SecretName => this.Name;
#endregion
}