Files
agent-framework/dotnet/src/Microsoft.Agents.AI.Abstractions/AIContext.cs
T
3168eb4870 .NET: [BREAKING] Add session StateBag for state storage and support multiple providers on the Agent (#3806)
* .NET: [BREAKING] Add session statebag to use for state storage instead of inside providers (#3737)

* Add a StateBag to AgentSession and pass Agent and AgentSession to AIContextProvider and ChatHistoryProviders

* Convert all AIContextProviders to use the statebag

* Update InMemoryChatHistoryProvider to use StateBag

* Update Comsos and Workflow ChatHistoryProviders

* Update 3rd party chat history storage sample.

* Remove serialize method from providers

* Replacing provider factories with properties

* Remove Providers from Session and flatten state bag serialization

* Update samples to use getservice on agent

* Updated additional session types to serialize statebag

* Fix regression

* Address PR comments

* Address PR comments.

* Fix formatting

* Fix unit tests

* Remove InMemoryAgentSession since it is not required anymore.

* Address PR comments

* Convert sessions for A2AAgent, ChatClientAgent, CopilotStudioAgent and GithubCopilotAgent to use regular json serialization.

* Fix durable agent session jso usgae

* Add jso to InMemory and Workflow ChatHistoryProviders

* Update InMemoryChatHistoryProvider to use an options class for it's many optional settings.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address PR feedback

* Fix verification bug.

* Improve state bag thread safety

* Address PR comments and fix unit tests

* Address PR comments

* Fix unit test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add a public StateKey property to providers (#3810)

* .NET: [BREAKING] Update providers in such a way that they can participate in a pipeline (#3846)

* Make providers pipeline capable

* Fix unit tests

* Move source stamping to providers from base class

* Also update samples.

* Address PR comments

* Rename AsAgentRequestMessageSourcedMessage to WithAgentRequestMessageSource

* .NET: [BREAKING] Add consistent message filtering to all providers. (#3851)

* Add consistent message filtering to all providers.

* Remove old chat history filtering classes

* Fix merge issues

* Fix unit test

* Enforce non-nullable property

* Fix merging bug and make troubleshooting source info easier by adding tostring implementation

* .NET: [BREAKING] Add support for multiple AIContextProviders on a ChatClientAgent (#3863)

* Add support for multiple AIContextProviders on a ChatClientAgent

* Address PR comments and fix tests

* Address PR comments.

* .NET: [BREAKING]Delay AIContext Materialization until the end of the pipeline is reached. (#3883)

* Delay AIContext Materialization until the end of the pipeline is reached.

* Address PR comments.

* Address PR comments

* Modify InvokedContext to be immutable (#3888)

* .NET: Address Feedback on StateBag feature branch PR (#3910)

* Address Feedback on statebag feature branch PR

* Update dotnet/src/Microsoft.Agents.AI.DurableTask/CHANGELOG.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address PR comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-02-13 14:08:07 +00:00

110 lines
5.7 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI;
/// <summary>
/// Represents additional context information that can be dynamically provided to AI models during agent invocations.
/// </summary>
/// <remarks>
/// <para>
/// <see cref="AIContext"/> serves as a container for contextual information that <see cref="AIContextProvider"/> instances
/// can supply to enhance AI model interactions. This context is merged with
/// the agent's base configuration before being passed to the underlying AI model.
/// </para>
/// <para>
/// The context system enables dynamic, runtime-specific enhancements to agent capabilities including:
/// <list type="bullet">
/// <item><description>Adding relevant background information from knowledge bases</description></item>
/// <item><description>Injecting task-specific instructions or guidelines</description></item>
/// <item><description>Providing specialized tools or functions for the current interaction</description></item>
/// <item><description>Including contextual messages that inform the AI about the current situation</description></item>
/// </list>
/// </para>
/// <para>
/// Context information is transient by default and applies only to the current invocation, however messages
/// added through the <see cref="Messages"/> property will be permanently incorporated into the conversation history.
/// </para>
/// </remarks>
public sealed class AIContext
{
/// <summary>
/// Gets or sets additional instructions to provide to the AI model for the current invocation.
/// </summary>
/// <value>
/// Instructions text that will be combined with any existing agent instructions or system prompts,
/// or <see langword="null"/> if no additional instructions should be provided.
/// </value>
/// <remarks>
/// <para>
/// These instructions are transient and apply only to the current AI model invocation. They are combined
/// with any existing agent instructions, system prompts, and conversation history to provide comprehensive
/// context to the AI model.
/// </para>
/// <para>
/// Instructions can be used to:
/// <list type="bullet">
/// <item><description>Provide context-specific behavioral guidance</description></item>
/// <item><description>Add domain-specific knowledge or constraints</description></item>
/// <item><description>Modify the agent's persona or response style for the current interaction</description></item>
/// <item><description>Include situational awareness information</description></item>
/// </list>
/// </para>
/// </remarks>
public string? Instructions { get; set; }
/// <summary>
/// Gets or sets the sequence of messages to use for the current invocation.
/// </summary>
/// <value>
/// A sequence of <see cref="ChatMessage"/> instances to be used for the current invocation,
/// or <see langword="null"/> if no messages should be used.
/// </value>
/// <remarks>
/// <para>
/// Unlike <see cref="Instructions"/> and <see cref="Tools"/>, messages added through this property may become
/// permanent additions to the conversation history.
/// If chat history is managed by the underlying AI service, these messages will become part of chat history.
/// If chat history is managed using a <see cref="ChatHistoryProvider"/>, these messages will be passed to the
/// <see cref="ChatHistoryProvider.InvokedCoreAsync(ChatHistoryProvider.InvokedContext, System.Threading.CancellationToken)"/> method,
/// and the provider can choose which of these messages to permanently add to the conversation history.
/// </para>
/// <para>
/// This property is useful for:
/// <list type="bullet">
/// <item><description>Injecting relevant historical context e.g. memories</description></item>
/// <item><description>Injecting relevant background information e.g. via Retrieval Augmented Generation</description></item>
/// <item><description>Adding system messages that provide ongoing context</description></item>
/// </list>
/// </para>
/// </remarks>
public IEnumerable<ChatMessage>? Messages { get; set; }
/// <summary>
/// Gets or sets a sequence of tools or functions to make available to the AI model for the current invocation.
/// </summary>
/// <value>
/// A sequence of <see cref="AITool"/> instances that will be available to the AI model during the current invocation,
/// or <see langword="null"/> if no additional tools should be provided.
/// </value>
/// <remarks>
/// <para>
/// These tools are transient and apply only to the current AI model invocation. Any existing tools
/// are provided as input to the <see cref="AIContextProvider"/> instances, so context providers can choose to modify or replace the existing tools
/// as needed based on the current context. The resulting set of tools is then passed to the underlying AI model, which may choose to utilize them when generating responses.
/// </para>
/// <para>
/// Context-specific tools enable:
/// <list type="bullet">
/// <item><description>Providing specialized functions based on user intent or conversation context</description></item>
/// <item><description>Adding domain-specific capabilities for particular types of queries</description></item>
/// <item><description>Enabling access to external services or data sources relevant to the current task</description></item>
/// <item><description>Offering interactive capabilities tailored to the current conversation state</description></item>
/// </list>
/// </para>
/// </remarks>
public IEnumerable<AITool>? Tools { get; set; }
}