Skip to content

Commit

Permalink
parse data uri from ImageMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleLittleCloud committed Nov 19, 2024
1 parent 191c916 commit 65b61f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
64 changes: 45 additions & 19 deletions dotnet/src/AutoGen.Core/Message/ImageMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,65 @@
// ImageMessage.cs

using System;
using System.Text.RegularExpressions;

namespace AutoGen.Core;

public class ImageMessage : IMessage
{
public ImageMessage(Role role, string url, string? from = null, string? mimeType = null)
: this(role, new Uri(url), from, mimeType)
{
}
private static readonly Regex s_DataUriRegex = new Regex(@"^data:(?<mediatype>[^;]+);base64,(?<data>.*)$", RegexOptions.Compiled);

public ImageMessage(Role role, Uri uri, string? from = null, string? mimeType = null)
/// <summary>
/// Create an ImageMessage from a url.
/// The url can be a regular url or a data uri.
/// If the url is a data uri, the scheme must be "data" and the format must be data:[<mediatype>][;base64],<data>
/// </summary>
public ImageMessage(Role role, string url, string? from = null, string? mimeType = null)
{
this.Role = role;
this.From = from;
this.Url = uri.ToString();

// try infer mimeType from uri extension if not provided
if (mimeType is null)
// url might be a data uri or a regular url
if (url.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
// the url must be in the format of data:[<mediatype>][;base64],<data>
var match = s_DataUriRegex.Match(url);

if (!match.Success)
{
throw new ArgumentException("Invalid DataUri format, expected data:[<mediatype>][;base64],<data>", nameof(url));
}

this.Data = new BinaryData(Convert.FromBase64String(match.Groups["data"].Value), match.Groups["mediatype"].Value);

this.MimeType = match.Groups["mediatype"].Value;
}
else
{
mimeType = uri switch
this.Url = url;
// try infer mimeType from uri extension if not provided
if (mimeType is null)
{
_ when uri.AbsoluteUri.EndsWith(".png", StringComparison.OrdinalIgnoreCase) => "image/png",
_ when uri.AbsoluteUri.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when uri.AbsoluteUri.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when uri.AbsoluteUri.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) => "image/gif",
_ when uri.AbsoluteUri.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) => "image/bmp",
_ when uri.AbsoluteUri.EndsWith(".webp", StringComparison.OrdinalIgnoreCase) => "image/webp",
_ when uri.AbsoluteUri.EndsWith(".svg", StringComparison.OrdinalIgnoreCase) => "image/svg+xml",
_ => throw new ArgumentException("MimeType is required for ImageMessage", nameof(mimeType))
};
mimeType = url switch
{
_ when url.EndsWith(".png", StringComparison.OrdinalIgnoreCase) => "image/png",
_ when url.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when url.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",
_ when url.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) => "image/gif",
_ when url.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) => "image/bmp",
_ when url.EndsWith(".webp", StringComparison.OrdinalIgnoreCase) => "image/webp",
_ when url.EndsWith(".svg", StringComparison.OrdinalIgnoreCase) => "image/svg+xml",
_ => throw new ArgumentException("MimeType is required for ImageMessage", nameof(mimeType))
};
}

this.MimeType = mimeType;
}
}

this.MimeType = mimeType;
public ImageMessage(Role role, Uri uri, string? from = null, string? mimeType = null)
: this(role, uri.ToString(), from, mimeType)
{
}

public ImageMessage(Role role, BinaryData data, string? from = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private IEnumerable<ChatMessageContent> ProcessMessageForOthers(ImageMessage mes
}
else if (message.BuildDataUri() is string dataUri)
{
collectionItems.Add(new ImageContent(new Uri(dataUri)));
collectionItems.Add(new ImageContent(dataUri));
}
else
{
Expand Down

0 comments on commit 65b61f2

Please sign in to comment.