// Copyright (c) Microsoft. All rights reserved. #pragma warning disable CS0618 // Tests intentionally exercise obsolete extension methods using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AgentConformance.IntegrationTests; using AgentConformance.IntegrationTests.Support; using Azure.AI.Extensions.OpenAI; using Azure.AI.Projects; using Microsoft.Agents.AI; using Microsoft.Agents.AI.AzureAI; using Microsoft.Extensions.AI; using OpenAI.Responses; using Shared.IntegrationTests; namespace AzureAI.IntegrationTests; [Obsolete("Use FoundryVersionedAgentFixture instead. These tests exercise obsolete AIProjectClient extension methods.")] public class AIProjectClientFixture : IChatClientAgentFixture { private FoundryAgent _agent = null!; private AIProjectClient _client = null!; public IChatClient ChatClient => this._agent.GetService()!.ChatClient; public AIAgent Agent => this._agent; public async Task CreateConversationAsync() { var response = await this._client.GetProjectOpenAIClient().GetProjectConversationsClient().CreateProjectConversationAsync(); return response.Value.Id; } public async Task> GetChatHistoryAsync(AIAgent agent, AgentSession session) { var chatClientSession = (ChatClientAgentSession)session; if (chatClientSession.ConversationId?.StartsWith("conv_", StringComparison.OrdinalIgnoreCase) == true) { // Conversation sessions do not persist message history. return await this.GetChatHistoryFromConversationAsync(chatClientSession.ConversationId); } if (chatClientSession.ConversationId?.StartsWith("resp_", StringComparison.OrdinalIgnoreCase) == true) { return await this.GetChatHistoryFromResponsesChainAsync(chatClientSession.ConversationId); } var chatHistoryProvider = agent.GetService(); if (chatHistoryProvider is null) { return []; } return (await chatHistoryProvider.InvokingAsync(new(agent, session, []))).ToList(); } private async Task> GetChatHistoryFromResponsesChainAsync(string conversationId) { var openAIResponseClient = this._client.GetProjectOpenAIClient().GetProjectResponsesClient(); var inputItems = await openAIResponseClient.GetResponseInputItemsAsync(conversationId).ToListAsync(); var response = await openAIResponseClient.GetResponseAsync(conversationId); var responseItem = response.Value.OutputItems.FirstOrDefault()!; // Take the messages that were the chat history leading up to the current response // remove the instruction messages, and reverse the order so that the most recent message is last. var previousMessages = inputItems .Select(ConvertToChatMessage) .Where(x => x.Text != "You are a helpful assistant.") .Reverse(); // Convert the response item to a chat message. var responseMessage = ConvertToChatMessage(responseItem); // Concatenate the previous messages with the response message to get a full chat history // that includes the current response. return [.. previousMessages, responseMessage]; } private static ChatMessage ConvertToChatMessage(ResponseItem item) { if (item is MessageResponseItem messageResponseItem) { var role = messageResponseItem.Role == MessageRole.User ? ChatRole.User : ChatRole.Assistant; return new ChatMessage(role, messageResponseItem.Content.FirstOrDefault()?.Text); } throw new NotSupportedException("This test currently only supports text messages"); } private async Task> GetChatHistoryFromConversationAsync(string conversationId) { List messages = []; await foreach (AgentResponseItem item in this._client.GetProjectOpenAIClient().GetProjectConversationsClient().GetProjectConversationItemsAsync(conversationId, order: "asc")) { var openAIItem = item.AsResponseResultItem(); if (openAIItem is MessageResponseItem messageItem) { messages.Add(new ChatMessage { Role = new ChatRole(messageItem.Role.ToString()), Contents = messageItem.Content .Where(c => c.Kind is ResponseContentPartKind.OutputText or ResponseContentPartKind.InputText) .Select(c => new TextContent(c.Text)) .ToList() }); } } return messages; } public async Task CreateChatClientAgentAsync( string name = "HelpfulAssistant", string instructions = "You are a helpful assistant.", IList? aiTools = null) { return (await this._client.CreateAIAgentAsync(GenerateUniqueAgentName(name), model: TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName), instructions: instructions, tools: aiTools)).GetService()!; } public async Task CreateChatClientAgentAsync(ChatClientAgentOptions options) { options.Name ??= GenerateUniqueAgentName("HelpfulAssistant"); return (await this._client.CreateAIAgentAsync(model: TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName), options)).GetService()!; } public static string GenerateUniqueAgentName(string baseName) => $"{baseName}-{Guid.NewGuid().ToString("N").Substring(0, 8)}"; public Task DeleteAgentAsync(ChatClientAgent agent) => this._client.Agents.DeleteAgentAsync(agent.Name); public async Task DeleteSessionAsync(AgentSession session) { var typedSession = (ChatClientAgentSession)session; if (typedSession.ConversationId?.StartsWith("conv_", StringComparison.OrdinalIgnoreCase) == true) { await this._client.GetProjectOpenAIClient().GetProjectConversationsClient().DeleteConversationAsync(typedSession.ConversationId); } else if (typedSession.ConversationId?.StartsWith("resp_", StringComparison.OrdinalIgnoreCase) == true) { await this.DeleteResponseChainAsync(typedSession.ConversationId!); } } private async Task DeleteResponseChainAsync(string lastResponseId) { var response = await this._client.GetProjectOpenAIClient().GetProjectResponsesClient().GetResponseAsync(lastResponseId); await this._client.GetProjectOpenAIClient().GetProjectResponsesClient().DeleteResponseAsync(lastResponseId); if (response.Value.PreviousResponseId is not null) { await this.DeleteResponseChainAsync(response.Value.PreviousResponseId); } } public ValueTask DisposeAsync() { GC.SuppressFinalize(this); if (this._client is not null && this._agent is not null) { return new ValueTask(this._client.Agents.DeleteAgentAsync(this._agent.Name)); } return default; } public virtual async ValueTask InitializeAsync() { this._client = new(new Uri(TestConfiguration.GetRequiredValue(TestSettings.AzureAIProjectEndpoint)), TestAzureCliCredentials.CreateAzureCliCredential()); this._agent = await this._client.CreateAIAgentAsync(GenerateUniqueAgentName("HelpfulAssistant"), model: TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName), instructions: "You are a helpful assistant."); } public async Task InitializeAsync(ChatClientAgentOptions options) { this._client = new(new Uri(TestConfiguration.GetRequiredValue(TestSettings.AzureAIProjectEndpoint)), TestAzureCliCredentials.CreateAzureCliCredential()); options.Name ??= GenerateUniqueAgentName("HelpfulAssistant"); this._agent = await this._client.CreateAIAgentAsync(model: TestConfiguration.GetRequiredValue(TestSettings.AzureAIModelDeploymentName), options); } }