2026-10-21 13:52:26 +02:00
using AIStudio.Provider ;
2026-10-22 10:57:04 +02:00
using AIStudio.Provider.SelfHosted ;
2025-12-17 11:33:08 +01:00
using AIStudio.Tools.PluginSystem ;
2025-02-18 11:24:43 +01:00
namespace AIStudio.Chat ;
public static class IImageSourceExtensions
{
2025-12-17 11:33:08 +01:00
private static string TB ( string fallbackEN ) => I18N . I . T ( fallbackEN , typeof ( IImageSourceExtensions ). Namespace , nameof ( IImageSourceExtensions ));
2026-10-21 13:52:26 +02:00
/// <summary>
2026-10-22 10:57:04 +02:00
/// The largest image, in bytes, we send anywhere: no image prepared for a model is larger, and
/// TryAsBase64 skips every larger file.
2026-10-21 13:52:26 +02:00
/// </summary>
public const long MAX_IMAGE_SIZE_BYTES = 10_000_000 ;
/// <summary>
/// The largest image file, in bytes, which we send through the given provider.
/// </summary>
/// <remarks>
2026-10-22 10:57:04 +02:00
/// The smallest of the limits we know: the one of a self-hosted engine, the one of the provider,
/// and MAX_IMAGE_SIZE_BYTES, our own. A provider or engine which states no limit gets ours.
2026-10-21 13:52:26 +02:00
/// </remarks>
2026-10-22 10:57:04 +02:00
/// <param name="provider">The configured provider the image is sent through.</param>
2026-10-21 13:52:26 +02:00
/// <returns>The size limit of one image file.</returns>
2026-10-22 10:57:04 +02:00
public static long MaxImageSizeBytesFor ( AIStudio . Settings . Provider provider )
2025-12-30 18:30:32 +01:00
{
2026-10-22 10:57:04 +02:00
var engineLimit = provider . UsedLLMProvider is LLMProviders . SELF_HOSTED ? provider . Host . GetMaxImageSizeBytes () : null ;
var providerLimit = provider . UsedLLMProvider . GetMaxImageSizeBytes ();
return Math . Min ( Math . Min ( engineLimit ?? MAX_IMAGE_SIZE_BYTES , providerLimit ?? MAX_IMAGE_SIZE_BYTES ), MAX_IMAGE_SIZE_BYTES );
2026-01-07 12:56:11 +01:00
}
2025-02-18 11:24:43 +01:00
/// <summary>
/// Read the image content as a base64 string.
/// </summary>
/// <remarks>
2026-10-22 10:57:04 +02:00
/// This is for the images inside the content of a chat, which go to external data sources and
/// agents as they are. Attached images, which go to a model, take ModelImageService instead: it
/// scales them down and encodes them anew within the limit of the provider.<br/>
/// <br/>
2025-02-18 11:24:43 +01:00
/// The images are directly converted to base64 strings. The maximum
/// size of the image is around 10 MB. If the image is larger, the method
2025-12-30 18:30:32 +01:00
/// returns an empty string.<br/>
/// <br/>
/// Note as well that this method returns just the base64 string without
/// any data URI prefix (like "data:image/png;base64,"). The caller has
/// to take care of that if needed.
2025-02-18 11:24:43 +01:00
/// </remarks>
/// <param name="image">The image source.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>The image content as a base64 string; might be empty.</returns>
2025-12-30 18:30:32 +01:00
public static async Task <( bool success , string base64Content )> TryAsBase64 ( this IImageSource image , CancellationToken token = default )
2025-02-18 11:24:43 +01:00
{
switch ( image . SourceType )
{
case ContentImageSource . BASE64 :
2025-12-30 18:30:32 +01:00
return ( success : true , image . Source );
2025-02-18 11:24:43 +01:00
case ContentImageSource . URL :
{
2026-05-31 18:46:54 +02:00
using var httpClient = ExternalHttpClientTimeout . CreateHttpClient ( ExternalHttpTrustPolicy . ALLOW_CUSTOM_ROOTS_WHEN_HOST_WHITELISTED );
2026-05-21 16:48:34 +02:00
using var timeoutTokenSource = ExternalHttpClientTimeout . CreateTimeoutTokenSource ( token );
var timeoutToken = timeoutTokenSource . Token ;
using var response = await httpClient . GetAsync ( image . Source , HttpCompletionOption . ResponseHeadersRead , timeoutToken );
2025-02-18 11:24:43 +01:00
if ( response . IsSuccessStatusCode )
{
// Read the length of the content:
var lengthBytes = response . Content . Headers . ContentLength ;
2026-10-21 13:52:26 +02:00
if ( lengthBytes > MAX_IMAGE_SIZE_BYTES )
2025-12-17 11:33:08 +01:00
{
await MessageBus . INSTANCE . SendError ( new ( Icons . Material . Filled . ImageNotSupported , TB ( "The image at the URL is too large (>10 MB). Skipping the image." )));
2025-12-30 18:30:32 +01:00
return ( success : false , string . Empty );
2025-12-17 11:33:08 +01:00
}
2026-05-21 16:48:34 +02:00
var bytes = await response . Content . ReadAsByteArrayAsync ( timeoutToken );
2025-12-30 18:30:32 +01:00
return ( success : true , Convert . ToBase64String ( bytes ));
2025-02-18 11:24:43 +01:00
}
2025-12-30 18:30:32 +01:00
await MessageBus . INSTANCE . SendError ( new ( Icons . Material . Filled . ImageNotSupported , TB ( "Failed to download the image from the URL. Skipping the image." )));
return ( success : false , string . Empty );
2025-02-18 11:24:43 +01:00
}
case ContentImageSource . LOCAL_PATH :
if ( File . Exists ( image . Source ))
{
// Read the content length:
var length = new FileInfo ( image . Source ). Length ;
2026-10-21 13:52:26 +02:00
if ( length > MAX_IMAGE_SIZE_BYTES )
2025-12-17 11:33:08 +01:00
{
await MessageBus . INSTANCE . SendError ( new ( Icons . Material . Filled . ImageNotSupported , TB ( "The local image file is too large (>10 MB). Skipping the image." )));
2025-12-30 18:30:32 +01:00
return ( success : false , string . Empty );
2025-12-17 11:33:08 +01:00
}
2025-02-18 11:24:43 +01:00
var bytes = await File . ReadAllBytesAsync ( image . Source , token );
2025-12-30 18:30:32 +01:00
return ( success : true , Convert . ToBase64String ( bytes ));
2025-02-18 11:24:43 +01:00
}
2025-12-30 18:30:32 +01:00
await MessageBus . INSTANCE . SendError ( new ( Icons . Material . Filled . ImageNotSupported , TB ( "The local image file does not exist. Skipping the image." )));
return ( success : false , string . Empty );
2025-02-18 11:24:43 +01:00
default :
2025-12-30 18:30:32 +01:00
return ( success : false , string . Empty );
2025-02-18 11:24:43 +01:00
}
}
}