// Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AgentConversation.IntegrationTests; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; namespace Microsoft.Agents.AI.Abstractions.IntegrationTests; /// /// An example that validates the harness can restore a /// pre-built conversation context and solicit a response from an agent. /// /// /// This test case uses a fixed, in-memory conversation representing a menu-ordering interaction. /// The messages are defined inline (no JSON fixture file is required), which makes this a /// self-contained example that runs without live AI credentials. /// public sealed class MenuConversationTestCase : IConversationTestCase { private const string AgentKey = "MenuAgent"; /// public string Name => "MenuConversation"; /// public IReadOnlyDictionary AgentDefinitions { get; } = new Dictionary { [AgentKey] = new ConversationAgentDefinition { Name = AgentKey, Instructions = "You are a helpful restaurant assistant. Answer questions about the menu.", Tools = [ AIFunctionFactory.Create(MenuTools.GetSpecials), AIFunctionFactory.Create(MenuTools.GetItemPrice), ] } }; /// public IReadOnlyList Steps { get; } = [ new ConversationStep { AgentName = AgentKey, Input = new ChatMessage(ChatRole.User, "What are the specials today?"), Validate = (response, metrics) => { Assert.NotNull(response); Assert.NotEmpty(response.Text); Assert.True(metrics.After.MessageCount > metrics.Before.MessageCount, "Message count should grow after the step."); } } ]; /// public IList GetInitialMessages() => // A short, representative conversation context that is already in memory. [ new ChatMessage(ChatRole.User, "Hello, I'd like to see the menu."), new ChatMessage(ChatRole.Assistant, "Welcome! I'm happy to help you with our menu. Feel free to ask about today's specials or the price of any item."), ]; /// public async Task> CreateInitialContextAsync( IReadOnlyDictionary agents, CancellationToken cancellationToken = default) { // Build the initial context by running a short greeting exchange. var agent = agents[AgentKey]; var session = await agent.CreateSessionAsync(cancellationToken).ConfigureAwait(false); await agent.RunAsync( new ChatMessage(ChatRole.User, "Hello, I'd like to see the menu."), session, cancellationToken: cancellationToken).ConfigureAwait(false); var historyProvider = agent.GetService() as InMemoryChatHistoryProvider; if (historyProvider is not null) { return historyProvider.GetMessages(session); } return GetInitialMessages(); } }