// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Purview;
///
/// A middleware chat client that connects to Microsoft Purview.
///
internal class PurviewChatClient : IChatClient
{
private readonly IChatClient _innerChatClient;
private readonly PurviewWrapper _purviewWrapper;
///
/// Initializes a new instance of the class.
///
/// The inner chat client to wrap.
/// The purview wrapper used to interact with the Purview service.
public PurviewChatClient(IChatClient innerChatClient, PurviewWrapper purviewWrapper)
{
this._innerChatClient = innerChatClient;
this._purviewWrapper = purviewWrapper;
}
///
public void Dispose()
{
this._purviewWrapper.Dispose();
this._innerChatClient.Dispose();
}
///
public Task GetResponseAsync(IEnumerable messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
return this._purviewWrapper.ProcessChatContentAsync(messages, options, this._innerChatClient, cancellationToken);
}
///
public object? GetService(Type serviceType, object? serviceKey = null)
{
return this._innerChatClient.GetService(serviceType, serviceKey);
}
///
public async IAsyncEnumerable GetStreamingResponseAsync(IEnumerable messages,
ChatOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Task responseTask = this._purviewWrapper.ProcessChatContentAsync(messages, options, this._innerChatClient, cancellationToken);
foreach (var update in (await responseTask.ConfigureAwait(false)).ToChatResponseUpdates())
{
yield return update;
}
}
}