public async Task ReplyAsync()

in workshop/dotnet/App/backend/Controllers/ChatController.cs [114:158]


    public async Task<ChatResponse> ReplyAsync([FromBody]ChatRequest request)
    {
        var chatHistory = new ChatHistory();
        if (request.MessageHistory.Count == 0) { 
            chatHistory.AddSystemMessage("You are a friendly financial advisor who only emits financial advice in a creative and funny tone.");
        }
        else {
            chatHistory = request.ToChatHistory();
        }

        // Initialize fullMessage variable and add user input to chat history
        string fullMessage = "";
        if (request.InputMessage != null)
        {
            chatHistory.AddUserMessage(request.InputMessage);

            // Create a thread for the agent conversation.
            AgentThread thread = await _agentsClient.CreateThreadAsync();

            ChatMessageContent message = new(AuthorRole.User, request.InputMessage);
            await _stockSentimentAgent.AddChatMessageAsync(thread.Id, message);

            await foreach (ChatMessageContent response in _stockSentimentAgent.InvokeAsync(thread.Id))
            {
                // Include TextContent (via ChatMessageContent.Content), if present.
                string contentExpression = string.IsNullOrWhiteSpace(response.Content) ? string.Empty : response.Content;
                chatHistory.AddAssistantMessage(contentExpression);
                fullMessage += contentExpression;

                // Provide visibility for inner content (that isn't TextContent).
                foreach (KernelContent item in response.Items)
                {
                    if (item is AnnotationContent annotation)
                    {
                        var annotationExpression = ($"{annotation.Quote}: File #{annotation.FileId}");
                        chatHistory.AddAssistantMessage(annotationExpression);
                        fullMessage += annotationExpression;
                    }
                }
            }
        }
            
        var chatResponse = new ChatResponse(fullMessage, chatHistory.FromChatHistory());    
        return chatResponse;
    }