Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 3, 2025

Motivation and Context

Addresses review feedback from #2505 to add state persistence for ContextualFunctionProvider, enabling restoration of recent message context across sessions or in distributed scenarios.

Description

Implements serialization/deserialization following the TextSearchProvider pattern:

Core Changes:

  • Added constructor overload accepting JsonElement serializedState with deserialization logic
  • Implemented Serialize() method to persist recent messages queue as ChatMessage objects
  • Created ContextualFunctionProviderState class and registered with AgentJsonUtilities.JsonContext
  • Respects NumberOfRecentMessagesInContext limit during deserialization, truncating if changed

Usage:

// Serialize provider state
JsonElement state = provider.Serialize();

// Restore from serialized state
var restoredProvider = new ContextualFunctionProvider(
    vectorStore,
    vectorDimensions,
    functions,
    maxNumberOfFunctions,
    serializedState: state,
    options: options);

Testing:

  • 5 new unit tests covering empty state, serialization, deserialization, round-trip, and limit truncation
  • Updated test to demonstrate truncation behavior with limit of 2 messages (3 messages added, only 2 most recent kept)
  • All 345 existing tests pass

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 3, 2025 11:06
- Added constructor overload that accepts JsonElement serializedState
- Implemented Serialize method to persist recent messages
- Added ContextualFunctionProviderState class
- Registered state class with AgentJsonUtilities JsonContext
- Added comprehensive unit tests for serialization/deserialization

Co-authored-by: westey-m <[email protected]>
- Reordered constructor parameters to maintain consistency (options before jsonSerializerOptions)
- Made default JsonElement explicit with default(JsonElement) in constructor chaining
- All tests continue to pass

Co-authored-by: westey-m <[email protected]>
- Updated jsonSerializerOptions parameter comments to clarify actual usage
- Made it clear that AgentJsonUtilities.DefaultOptions is used
- All tests continue to pass

Co-authored-by: westey-m <[email protected]>
Copilot AI changed the title [WIP] Add serialization code and unit tests for ContextualFunctionProvider Add serialization support for ContextualFunctionProvider Dec 3, 2025
Copilot AI requested a review from westey-m December 3, 2025 11:19
Copilot finished work on behalf of westey-m December 3, 2025 11:19
var functions = new List<AIFunction> { CreateFunction("f1") };
var options = new ContextualFunctionProviderOptions
{
NumberOfRecentMessagesInContext = 3
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot , change this to 2, so that we can see messages truncation in action.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to 2. The test now demonstrates truncation by adding 3 messages (M1, M2, M3) but only keeping the 2 most recent ones (M2, M3) in the serialized state.

Changes committed in 3ee80a1.

- Updated test to use limit of 2 messages instead of 3
- Now test shows truncation in action (3 messages added, only 2 kept)
- Updated assertion to expect 2 messages in serialized state
- All tests pass

Co-authored-by: westey-m <[email protected]>
Copilot AI requested a review from westey-m December 3, 2025 12:08
Copilot finished work on behalf of westey-m December 3, 2025 12:08
@github-actions github-actions bot changed the title Add serialization support for ContextualFunctionProvider .NET: Add serialization support for ContextualFunctionProvider Dec 3, 2025
@westey-m westey-m marked this pull request as ready for review December 3, 2025 12:18
@westey-m westey-m merged commit 7965285 into contextual-function-provider-port Dec 3, 2025
2 checks passed
@crickman crickman deleted the copilot/sub-pr-2505-again branch December 4, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants