संपादित करें

इसके माध्यम से साझा किया गया


Session

AgentSession is the conversation state container used across agent runs.

What AgentSession contains

Field Purpose
StateBag Arbitrary state container for this session

The C# AgentSession is an abstract base class. Concrete implementations (created via CreateSessionAsync()) may add additional state e.g. an id for remote chat history storage, when service-managed history is used.

Field Purpose
session_id Local unique identifier for this session
service_session_id Remote service conversation identifier (when service-managed history is used)
state Mutable dictionary shared with context/history providers

Built-in usage pattern

AgentSession session = await agent.CreateSessionAsync();

var first = await agent.RunAsync("My name is Alice.", session);
var second = await agent.RunAsync("What is my name?", session);
session = agent.create_session()

first = await agent.run("My name is Alice.", session=session)
second = await agent.run("What is my name?", session=session)

Creating a session from an existing service conversation ID

Create a new session from an existing conversation id varies by agent type. Here are some examples.

When using ChatClientAgent

AgentSession session = await chatClientAgent.CreateSessionAsync(conversationId);

When using an A2AAgent

AgentSession session = await a2aAgent.CreateSessionAsync(contextId, taskId);

Use this when the backing service already has conversation state.

session = agent.get_session(service_session_id="<service-conversation-id>")
response = await agent.run("Continue this conversation.", session=session)

Serialization and restoration

var serialized = agent.SerializeSession(session);
AgentSession resumed = await agent.DeserializeSessionAsync(serialized);
serialized = session.to_dict()
resumed = AgentSession.from_dict(serialized)

Important

Sessions are agent/service-specific. Reusing a session with a different agent configuration or provider can lead to invalid context.

Next steps