Add state to core#547
Open
MehakBindra wants to merge 18 commits into
Open
Conversation
rido-min
reviewed
Jun 2, 2026
rido-min
reviewed
Jun 2, 2026
rido-min
reviewed
Jun 2, 2026
rido-min
reviewed
Jun 2, 2026
rido-min
reviewed
Jun 2, 2026
rido-min
reviewed
Jun 2, 2026
singhk97
reviewed
Jun 3, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces an opt-in turn state feature in Microsoft.Teams.Apps that loads per-conversation and per-user state at the start of each turn and saves it on successful completion, backed by IDistributedCache. It wires state through TeamsBotApplication and Context<TActivity>, adds a sample bot demonstrating the feature, and adds unit tests + a design document.
Changes:
- Add new state primitives (
TurnState,StateScope,TurnStateStore,StateSerializer) underMicrosoft.Teams.Apps.State. - Wire state enablement via
TeamsBotApplicationOptions.UseState()/AppBuilder.UseState()and load/save around routing inTeamsBotApplication.OnActivity. - Add
StateBotsample, documentation, and unit tests covering routing, serialization, and persistence semantics.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| core/test/Microsoft.Teams.Apps.UnitTests/StateWiringTests.cs | Verifies UseState() toggles options and DI wiring registers TurnStateStore. |
| core/test/Microsoft.Teams.Apps.UnitTests/State/TurnStateTests.cs | Tests scoped-path API routing and validation behavior. |
| core/test/Microsoft.Teams.Apps.UnitTests/State/TurnStateStoreTests.cs | Tests load/save semantics, change detection, delete-on-clear, and after-turn sealing. |
| core/test/Microsoft.Teams.Apps.UnitTests/State/StateSerializerTests.cs | Tests serializer round-trips and camelCase behavior. |
| core/test/Microsoft.Teams.Apps.UnitTests/State/StateScopeTests.cs | Tests scope CRUD behavior, change tracking, snapshotting, and completion guard. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplicationOptions.cs | Adds UseState() and internal configuration flags for state. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplication.HostingExtensions.cs | Registers default distributed cache + TurnStateStore when state is enabled. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplication.cs | Loads state before dispatch, saves on success, seals in finally. |
| core/src/Microsoft.Teams.Apps/State/TurnStateStore.cs | Implements cache-backed load/save, change detection, and delete-on-empty behavior. |
| core/src/Microsoft.Teams.Apps/State/TurnState.cs | Defines per-turn state container, key derivation, and scoped-path helpers. |
| core/src/Microsoft.Teams.Apps/State/StateSerializer.cs | Defines JSON serialization settings and helpers for scope documents. |
| core/src/Microsoft.Teams.Apps/State/StateScope.cs | Implements per-scope bag API with change tracking and after-turn guard. |
| core/src/Microsoft.Teams.Apps/Routing/Route.cs | Forwards ctx.State into typed contexts created during routing. |
| core/src/Microsoft.Teams.Apps/Microsoft.Teams.Apps.csproj | Adds caching abstractions package references for IDistributedCache. |
| core/src/Microsoft.Teams.Apps/Context.cs | Adds TurnState? to context and exposes Context.State. |
| core/src/Microsoft.Teams.Apps/AppBuilder.cs | Adds UseState() builder method delegating to options. |
| core/samples/StateBot/StateBot.csproj | Adds a sample project demonstrating state usage (optional Redis). |
| core/samples/StateBot/README.md | Documents sample commands and how state persists with/without Redis. |
| core/samples/StateBot/Program.cs | Implements sample bot handlers demonstrating conversation/user state and sealing behavior. |
| core/samples/StateBot/appsettings.json | Sample configuration (logging + connection strings). |
| core/docs/State-Design.md | Design document describing state semantics, architecture, and constraints. |
| core/core.slnx | Adds the new StateBot sample to the solution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new state management feature for Teams bots, enabling persistent per-conversation and per-user state using .NET's
IDistributedCacheabstraction. The design is opt-in, does not affect existing bots unless enabled, and is fully documented in a new design document. A new sample bot is also added to demonstrate usage.State Management Feature:
Added
State-Design.mddetailing the architecture, API, usage patterns, and edge cases for the newTurnStatefeature, which provides persistent state across turns and conversations using anyIDistributedCachebackend. The document covers key design choices, API surface, serialization, atomic semantics, and what the feature is (and is not) intended for.Sample Update:
Added the
StateBotsample project to the solution, demonstrating how to use the new state management APIs in practice. (core/core.slnxupdated to includesamples/StateBot/StateBot.csproj)