Skip to content

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

Merged
westey (westey-m) merged 8 commits into
microsoft:mainfrom
ilia-sokolov:contrib/3809-cosmos-get-messages
Aug 17, 2026
Merged

.NET: Add Cosmos chat history retrieval API#7412
westey (westey-m) merged 8 commits into
microsoft:mainfrom
ilia-sokolov:contrib/3809-cosmos-get-messages

Conversation

@ilia-sokolov

@ilia-sokolov Ilia Sokolov (ilia-sokolov) commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

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 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.

@ilia-sokolov

Copy link
Copy Markdown
Contributor Author

westey (@westey-m) thanks for the review!
There are 2 checks failed. Could you please "Re-run failed jobs"? Re-runs need write access on the base repo, so I can't trigger it from the fork.

@westey-m
westey (westey-m) added this pull request to the merge queue Aug 17, 2026
Merged via the queue into microsoft:main with commit c6584ff Aug 17, 2026
25 of 27 checks passed
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

4 participants