using AIStudio.Provider;
namespace AIStudio.Chat;
public static class ListContentBlockExtensions
{
///
/// Processes a list of content blocks by transforming them into a collection of message results asynchronously.
///
/// The list of content blocks to process.
/// A function that transforms each content block into a message result asynchronously.
/// An asynchronous task that resolves to a list of transformed results.
public static async Task> BuildMessages(this List blocks, Func> transformer)
{
var messages = blocks
.Where(n => n.ContentType is ContentType.TEXT && !string.IsNullOrWhiteSpace((n.Content as ContentText)?.Text))
.Select(transformer)
.ToList();
// Await all messages:
await Task.WhenAll(messages);
// Select all results:
return messages.Select(n => n.Result).ToList();
}
}