AI-Studio/app/MindWork AI Studio/Chat/ContentImage.cs

85 lines
2.4 KiB
C#
Raw Normal View History

using System.Text.Json.Serialization;
2024-05-04 09:11:09 +00:00
using AIStudio.Provider;
2025-12-17 10:33:08 +00:00
using AIStudio.Tools.Validation;
2024-05-04 09:11:09 +00:00
namespace AIStudio.Chat;
/// <summary>
/// Represents an image inside the chat.
/// </summary>
public sealed class ContentImage : IContent, IImageSource
2024-05-04 09:11:09 +00:00
{
#region Implementation of IContent
/// <inheritdoc />
[JsonIgnore]
2025-05-24 17:11:28 +00:00
public bool InitialRemoteWait { get; set; }
2024-05-04 09:11:09 +00:00
/// <inheritdoc />
[JsonIgnore]
2025-05-24 17:11:28 +00:00
public bool IsStreaming { get; set; }
2024-05-04 09:11:09 +00:00
/// <inheritdoc />
[JsonIgnore]
2024-05-04 09:11:09 +00:00
public Func<Task> StreamingDone { get; set; } = () => Task.CompletedTask;
/// <inheritdoc />
[JsonIgnore]
2024-05-04 09:11:09 +00:00
public Func<Task> StreamingEvent { get; set; } = () => Task.CompletedTask;
/// <inheritdoc />
public List<Source> Sources { get; set; } = [];
/// <inheritdoc />
2025-12-28 15:50:36 +00:00
public List<FileAttachment> FileAttachments { get; set; } = [];
2024-05-04 09:11:09 +00:00
/// <inheritdoc />
public Task<ChatThread> CreateFromProviderAsync(IProvider provider, Model chatModel, IContent? lastUserPrompt, ChatThread? chatChatThread, CancellationToken token = default)
2024-05-04 09:11:09 +00:00
{
throw new NotImplementedException();
}
2025-05-24 10:27:00 +00:00
/// <inheritdoc />
2025-05-24 17:11:28 +00:00
public IContent DeepClone() => new ContentImage
2025-05-24 10:27:00 +00:00
{
2025-05-24 17:11:28 +00:00
Source = this.Source,
InitialRemoteWait = this.InitialRemoteWait,
IsStreaming = this.IsStreaming,
SourceType = this.SourceType,
2025-12-30 17:30:32 +00:00
Sources = [..this.Sources],
FileAttachments = [..this.FileAttachments],
2025-05-24 17:11:28 +00:00
};
2024-05-04 09:11:09 +00:00
#endregion
2025-12-17 10:33:08 +00:00
/// <summary>
/// Creates a ContentImage from a local file path.
/// </summary>
/// <param name="filePath">The path to the image file.</param>
/// <returns>A new ContentImage instance if the file is valid, null otherwise.</returns>
public static async Task<ContentImage?> CreateFromFileAsync(string filePath)
{
if (!await FileExtensionValidation.IsImageExtensionValidWithNotifyAsync(filePath))
return null;
return new ContentImage
{
SourceType = ContentImageSource.LOCAL_PATH,
Source = filePath,
};
}
2024-05-04 09:11:09 +00:00
/// <summary>
/// The type of the image source.
2024-05-04 09:11:09 +00:00
/// </summary>
/// <remarks>
/// Is the image source a URL, a local file path, a base64 string, etc.?
/// </remarks>
public required ContentImageSource SourceType { get; init; }
2024-05-04 09:11:09 +00:00
/// <summary>
/// The image source.
2024-05-04 09:11:09 +00:00
/// </summary>
public required string Source { get; set; }
2024-05-04 09:11:09 +00:00
}