using System.Runtime.CompilerServices; using AIStudio.Provider; using AIStudio.Tools.ToolCallingSystem.Harness; namespace AIStudio.Tests.Provider.ToolCalling; /// /// Plays back the stream of a provider for the adapter tests, the way the shared stream reader hands it out. /// internal static class ScriptedStream { /// /// Ends a stream the way a stop, a timeout, or a broken connection does: the lines before it go out, but the stream never /// reaches its end. /// public const string CUT_OFF = ""; /// /// Hands out the given payloads as data lines, and marks the end of the stream once all of them went out. /// /// The payloads, one per line; cuts the stream off right there. /// Marked when the stream reaches its end. /// The cancellation token. public static async IAsyncEnumerable Lines(string[] data, ServerSentEventStreamEnd streamEnd, [EnumeratorCancellation] CancellationToken token = default) { foreach (var line in data) { token.ThrowIfCancellationRequested(); if (line is CUT_OFF) yield break; yield return new ServerSentEvent($"data: {line}", line); } streamEnd.MarkReached(); await Task.CompletedTask; } /// /// Runs the next round of the given adapter and tells whether it ended with an outcome. /// public static async Task RoundCompletesAsync(IToolCallingProviderAdapter adapter) { var completes = false; await foreach (var streamEvent in adapter.ExecuteRoundAsync(null, true)) completes |= streamEvent.Kind is ToolCallingStreamEventKind.ROUND_COMPLETED; return completes; } }