mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-02 13:07:21 +00:00
Stopped blocking a thread while streams wait for their next line
This commit is contained in:
3 files changed
+8
-21
No files matched your search
@@ -1007,22 +1007,12 @@ public abstract class BaseProvider : IProvider, ISecretId
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// Read the stream, line by line:
|
// Read the stream, line by line, until it ends. We never ask the reader whether the
|
||||||
|
// stream ended: that would wait for the next data without any timeout, and it would
|
||||||
|
// hold a thread meanwhile. Reading the next line tells the same, see below.
|
||||||
//
|
//
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
if(streamReader.EndOfStream)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Stream, string.Format(TB("Tried to stream the LLM provider '{0}' answer. There were some problems with the stream. The message is: '{1}'"), this.InstanceName, e.Message)));
|
|
||||||
this.logger.LogWarning(e, "Failed to read the end-of-stream state from {ProviderName} '{ProviderInstanceName}': {ErrorMessage}", providerName, this.InstanceName, e.Message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the token is canceled:
|
// Check if the token is canceled:
|
||||||
if (token.IsCancellationRequested)
|
if (token.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
@@ -1058,13 +1048,14 @@ public abstract class BaseProvider : IProvider, ISecretId
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The stream ended:
|
||||||
if (line is null)
|
if (line is null)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Skip empty lines:
|
// Skip empty lines:
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
if (string.IsNullOrWhiteSpace(line))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (this.TryCreateProviderRequestExceptionFromStreamLine(providerName, line, out var providerRequestException))
|
if (this.TryCreateProviderRequestExceptionFromStreamLine(providerName, line, out var providerRequestException))
|
||||||
throw providerRequestException;
|
throw providerRequestException;
|
||||||
|
|
||||||
|
|||||||
@@ -27,12 +27,9 @@ public partial class RustService
|
|||||||
// Read events line by line:
|
// Read events line by line:
|
||||||
using var reader = new StreamReader(stream);
|
using var reader = new StreamReader(stream);
|
||||||
|
|
||||||
// Read until the end of the stream or cancellation:
|
// Read the next line of JSON until the end of the stream or cancellation:
|
||||||
while(!reader.EndOfStream && !stopToken.IsCancellationRequested)
|
while(!stopToken.IsCancellationRequested && await reader.ReadLineAsync(stopToken) is { } line)
|
||||||
{
|
{
|
||||||
// Read the next line of JSON from the stream:
|
|
||||||
var line = await reader.ReadLineAsync(stopToken);
|
|
||||||
|
|
||||||
// Skip empty lines:
|
// Skip empty lines:
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
if (string.IsNullOrWhiteSpace(line))
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -352,9 +352,8 @@ public sealed partial class RustService
|
|||||||
await using var stream = await response.Content.ReadAsStreamAsync(token);
|
await using var stream = await response.Content.ReadAsStreamAsync(token);
|
||||||
using var reader = new StreamReader(stream);
|
using var reader = new StreamReader(stream);
|
||||||
|
|
||||||
while (!reader.EndOfStream && !token.IsCancellationRequested)
|
while (!token.IsCancellationRequested && await reader.ReadLineAsync(token) is { } line)
|
||||||
{
|
{
|
||||||
var line = await reader.ReadLineAsync(token);
|
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
if (string.IsNullOrWhiteSpace(line))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user