Skip to content

[dotnet-port] Add GetInMemoryHistory/SetInMemoryHistory session helpers #212

Description

@github-actions

Summary

Adds two new helpers to the agent package:

  • GetInMemoryHistory(session *Session, sourceID string) []*message.Message — reads the conversation messages stored in a session by NewInMemoryHistoryProvider. Returns a defensive copy; returns nil when nothing has been stored yet or session is nil.
  • SetInMemoryHistory(session *Session, sourceID string, messages []*message.Message) — replaces the stored history slice, enabling preloading, truncation, or clearing.

Both functions accept an empty sourceID and default to "in-memory" (matching NewInMemoryHistoryProvider's default key). Custom source IDs are fully supported.

This gives callers a typed, readable way to inspect or manipulate in-memory history between turns without knowing the internal session-state key. It also enables testing scenarios where you want to preset the conversation history before running the agent.

Ported .NET PRs

The Go port covers the session-level accessor helpers (TryGetInMemoryChatHistory / SetInMemoryChatHistory). The .NET PR also adds IChatReducer integration into InMemoryChatHistoryProviderOptions; that piece is not included here because Go uses messagefilter functions for the equivalent reduction and a follow-up that wires a compaction provider option into NewInMemoryHistoryProvider would be a separate, larger change.

Breaking Changes

No. These are new exported functions with no impact on existing APIs.

Tests and Examples

10 new tests in agent/history_test.go:

  • TestGetInMemoryHistory_NilSession_ReturnsNil
  • TestGetInMemoryHistory_NoHistoryStored_ReturnsNil
  • TestGetInMemoryHistory_DefaultSourceID_ReadsProviderHistory
  • TestGetInMemoryHistory_CustomSourceID_ReadsCorrectKey
  • TestGetInMemoryHistory_ReturnsCopy
  • TestSetInMemoryHistory_NilSession_IsNoOp
  • TestSetInMemoryHistory_DefaultSourceID_RoundTrips
  • TestSetInMemoryHistory_ReplacesExistingHistory
  • TestSetInMemoryHistory_NilMessages_ClearsHistory
  • TestSetInMemoryHistory_IsReadByProvider

All 10 pass. Full ./agent/... test suite passes.

Notes

The feature comparison doc (docs/dotnet-go-sdk-feature-comparison.md) is updated to reflect that the Go SDK now has session history access helpers, keeping the "Chat history" row at Partial since reducer-trigger integration and additional built-in storage providers are still missing.

Generated by .NET to Go Porting Agent · ● 47.2M ·


Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch dotnet-port-inmemory-history-helpers-3c07f119cef6f9f8.

Click here to create the pull request

To fix the permissions issue, go to SettingsActionsGeneral and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (243 of 243 lines)
From 42bf9fe47e00353f1b9942a6c8f5a2bd6166b2ee Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Sun, 24 May 2026 09:42:32 +0000
Subject: [PATCH] agent: add GetInMemoryHistory/SetInMemoryHistory session
 helpers

Adds GetInMemoryHistory and SetInMemoryHistory to the agent package,
mirroring the .NET AgentSessionExtensions.TryGetInMemoryChatHistory
and SetInMemoryChatHistory helpers added in microsoft/agent-framework#5171
(commit 822a3eb5).

These helpers let callers inspect and replace the conversation messages
stored in a session by NewInMemoryHistoryProvider, without needing to
reach into session state directly or know the internal key name.

GetInMemoryHistory returns a copy of the stored messages (nil when no
history exists or session is nil). SetInMemoryHistory replaces the
stored slice, enabling preloading, truncation, or clearing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 agent/history.go                         |  41 +++++++
 agent/history_test.go                    | 143 +++++++++++++++++++++++
 docs/dotnet-go-sdk-feature-comparison.md |   2 +-
 3 files changed, 185 insertions(+), 1 deletion(-)

diff --git a/agent/history.go b/agent/history.go
index 69aa8f29..c93a7ef4 100644
--- a/agent/history.go
+++ b/agent/history.go
@@ -209,3 +209,44 @@ func NewInMemoryHistoryProvider(sourceID string) *HistoryProvider {
 type inmemoryState struct {
 	Messages []*message.Message `json:"messages,omitempty"`
 }
+
+// GetInMemoryHistory returns the conversation messages stored in the session by the
+// in-memory history provider identified by sourceID. If sourceID is empty it defaults
+// to "in-memory" (matching [NewInMemoryHistoryProvider]'s default). When no history
+// has been stored yet, or when session is nil, an empty slice is returned.
+//
+// This is the Go equivalent of the .NET AgentSessionExtensions.TryGetInMemoryChatHistory helper.
+func GetInMemoryHistory(session *Session,
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions