Repository: microsoft/semantic-kernel
File: dotnet/src/Agents/OpenAI/OpenAIResponseAgent.cs (C#)

Code before:
        if (this.StoreEnabled && agentThread.Id != null)
        {
            creationOptions.PreviousResponseId = agentThread.Id;
        }

        await foreach (StreamingResponseUpdate update in this.Client.CreateResponseStreamingAsync(inputItems, creationOptions, cancellationToken).ConfigureAwait(false))
        {
            if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate)
            {
                yield return outputTextUpdate.ToStreamingChatMessageContent();
            }
            else if (update is StreamingResponseCompletedUpdate responseCompletedUpdate)
            {
                if (this.StoreEnabled)
                {
                    this.UpdateResponseId(agentThread, responseCompletedUpdate.Response.Id);
                }
                foreach (var item in responseCompletedUpdate.Response.OutputItems)
                {
                    history.Add(item.ToChatMessageContent());
                }
            }
        }
    }

    private ChatHistory GetChatHistory(AgentThread agentThread)
    {
        if (agentThread is ChatHistoryAgentThread chatHistoryAgentThread)
        {
            return chatHistoryAgentThread.ChatHistory;
        }

        throw new InvalidOperationException("The agent thread is not a ChatHistoryAgentThread.");
    }

    private void UpdateResponseId(AgentThread agentThread, string id)
    {
        if (agentThread is OpenAIResponseAgentThread openAIResponseAgentThread)
        {
            openAIResponseAgentThread.ResponseId = id;
            return;
        }

        throw new InvalidOperationException("The agent thread is not an OpenAIResponseAgentThread.");
    }
    #endregion
}


Review comment (refactor):
I'm curious how we're supporting "summary" response (that is used by reasoning models)?

- https://platform.openai.com/docs/api-reference/responses/object
- https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/TextReasoningContent.cs




Code after:
        [EnumeratorCancellation] CancellationToken cancellationToken)
    {
        var kernel = options?.Kernel ?? this.Kernel;

        var overrideHistory = history;
        if (!this.StoreEnabled)
        {
            // Use the thread chat history
            overrideHistory = [.. this.GetChatHistory(agentThread), .. history];
        }

        var inputItems = overrideHistory.Select(c => c.ToResponseItem());
        var creationOptions = new ResponseCreationOptions()
        {
            EndUserId = this.GetDisplayName(),
            Instructions = $"{this.Instructions}\n{options?.AdditionalInstructions}",
            StoredOutputEnabled = this.StoreEnabled,
        };
        if (this.StoreEnabled && agentThread.Id != null)
        {
            creationOptions.PreviousResponseId = agentThread.Id;
        }

        await foreach (StreamingResponseUpdate update in this.Client.CreateResponseStreamingAsync(inputItems, creationOptions, cancellationToken).ConfigureAwait(false))
        {
            if (update is StreamingResponseOutputTextDeltaUpdate outputTextUpdate)
            {
                yield return outputTextUpdate.ToStreamingChatMessageContent();
            }
            else if (update is StreamingResponseCompletedUpdate responseCompletedUpdate)
            {
                if (this.StoreEnabled)
                {
                    this.UpdateResponseId(agentThread, responseCompletedUpdate.Response.Id);
                }
                foreach (var item in responseCompletedUpdate.Response.OutputItems)
                {
                    history.Add(item.ToChatMessageContent());
                }
            }
        }
    }

    private ChatHistory GetChatHistory(AgentThread agentThread)
    {
        if (agentThread is ChatHistoryAgentThread chatHistoryAgentThread)
        {
            return chatHistoryAgentThread.ChatHistory;
        }

        throw new InvalidOperationException("The agent thread is not a ChatHistoryAgentThread.");