Skip to content

.NET: Add Cosmos chat history retrieval API - #7412

Open
ilia-sokolov wants to merge 4 commits into
microsoft:mainfrom
ilia-sokolov:contrib/3809-cosmos-get-messages
Open

.NET: Add Cosmos chat history retrieval API#7412
ilia-sokolov wants to merge 4 commits into
microsoft:mainfrom
ilia-sokolov:contrib/3809-cosmos-get-messages

Conversation

@ilia-sokolov

@ilia-sokolov ilia-sokolov commented Jul 29, 2026

Copy link
Copy Markdown

Motivation & Context

CosmosChatHistoryProvider exposes utilities for counting and clearing stored messages, but applications cannot retrieve those messages without running the agent invocation pipeline. This makes migration, inspection, and other out-of-band conversation-history scenarios unnecessarily difficult.

Description & Review Guide

  • What are the major changes?
    • Add GetMessagesAsync(AgentSession?, CancellationToken) to retrieve the messages stored for a session.
    • Reuse that method from ProvideChatHistoryAsync so public and invocation retrieval share the same Cosmos query, pagination, MaxItemCount, and MaxMessagesToRetrieve behavior.
    • Document that direct retrieval returns raw stored messages without the invocation pipeline's output filter or chat-history source attribution.
    • Dispose the Cosmos feed iterator and add coverage for pagination, empty histories, filtering/source attribution, disposal, and cancellation.
  • What is the impact of these changes?
    • Consumers can inspect Cosmos-backed chat history without initiating an agent run.
    • Existing invocation behavior remains unchanged.
  • What do you want reviewers to focus on?
    • The session-aware API shape and the documented distinction between raw retrieval and invocation-processed messages.

Related Issue

Fixes #3809

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
    • The Cosmos emulator-dependent tests are added and compile successfully, but were skipped locally because the emulator is unavailable. They should run in the emulator-enabled CI environment.
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI review requested due to automatic review settings July 29, 2026 20:33
@agent-framework-automation agent-framework-automation Bot added the .NET Usage: [Issues, PRs], Target: .Net label Jul 29, 2026
@ilia-sokolov
ilia-sokolov marked this pull request as ready for review July 29, 2026 20:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a public retrieval API to the .NET Cosmos-backed chat history provider so callers can read stored conversation messages out-of-band (without running the agent invocation pipeline), while reusing the same Cosmos query/pagination logic that invocation retrieval uses.

Changes:

  • Added GetMessagesAsync(AgentSession?, CancellationToken) to CosmosChatHistoryProvider for direct message retrieval.
  • Refactored invocation history retrieval (ProvideChatHistoryAsync) to reuse GetMessagesAsync so both paths share the same query/paging/limits behavior.
  • Added Cosmos emulator-dependent unit tests covering pagination, empty histories, filter/source-attribution differences, disposal, and cancellation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs Introduces the new retrieval API and routes invocation history retrieval through it; disposes the Cosmos query iterator.
dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs Adds emulator-based tests validating the new public retrieval behavior and its differences from invocation-processed history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Feature]: Add GetMessagesAsync method to CosmosChatHistoryProvider

2 participants