Refactor IMessageContext to use single-layer dependency injection architecture#564
Draft
Copilot wants to merge 4 commits into
Draft
Refactor IMessageContext to use single-layer dependency injection architecture#564Copilot wants to merge 4 commits into
Copilot wants to merge 4 commits into
Conversation
…support Co-authored-by: UncleDave <3244206+UncleDave@users.noreply.github.com>
…jection Co-authored-by: UncleDave <3244206+UncleDave@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor
Refactor IMessageContext to be provided as scoped dependency
Aug 19, 2025
IMessageContext and DiscordMessageContext to be provided as a scoped dependency. The scope should be based on a discord interaction, such as a message. ParallelNonBlockingPublisher may need to be refactored and take on additional respons...
Owner
|
@copilot include the uses of |
…data storage, eliminating dual-layer scoping Co-authored-by: UncleDave <3244206+UncleDave@users.noreply.github.com>
Contributor
Author
I've addressed both points in commit ea03fc3: GenAi IMessageContext Usage: Updated all GenAi components to use dependency injection instead of the kernel data storage pattern:
Eliminated Dual-Layer Scoping: The architecture now uses a single DI scope layer instead of DI + Kernel.Data:
The GenAi project now has a cleaner architecture with consistent dependency injection patterns throughout. |
Copilot
AI
changed the title
Refactor IMessageContext to be provided as scoped dependency
Refactor IMessageContext to use single-layer dependency injection architecture
Aug 19, 2025
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.
Problem
Previously,
IMessageContextwas created manually using theToMessageContext()extension method within handlers, making it difficult to use dependency injection patterns. Additionally, theParallelNonBlockingPublisherran handlers usingTask.Runwithout proper scope management, preventing handlers from accessing scoped services and potentially causing memory leaks.The GenAi project also used a dual-layer scoping approach where
IMessageContextwas both provided through dependency injection and stored inKernel.Data, creating unnecessary complexity.Solution
This PR refactors the architecture to provide
IMessageContextas a scoped dependency with a single, clean dependency injection layer.Key Changes
1. MessageContextProvider
2. Enhanced ParallelNonBlockingPublisher
MessageReceived,ReactionAdded)3. Single-Layer Dependency Injection
MessageContextProviderregistered as scopedIMessageContextregistered as scoped, resolving from the providerKernel.Datastorage pattern from GenAi project4. Simplified GenAi Architecture
KernelExtensions.csandSetMessageContext/GetMessageContextmethodsIPersonalityinterface to removeIMessageContextparametersPersonalityBaseto injectIMessageContextin constructorImageGenerationPluginto injectIMessageContextdirectlyUpdated Handler Patterns
Architecture Benefits
IMessageContextTesting
The PR includes temporary test handlers that validate the dependency injection:
!test-contextto test MessageReceived notifications!test-reactionto test ReactionAdded notificationsThese test handlers confirm that
IMessageContextis properly injected with the correct user context and can successfully reply to messages.💡 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.