Class InProcessRuntime
public sealed class InProcessRuntime : IAgentRuntime, ISaveState, IHostedService
- Inheritance
-
InProcessRuntime
- Implements
- Inherited Members
- Extension Methods
Constructors
InProcessRuntime()
public InProcessRuntime()
Properties
DeliverToSelf
public bool DeliverToSelf { get; set; }
Property Value
Methods
AddSubscriptionAsync(ISubscriptionDefinition)
Adds a new subscription for the runtime to handle when processing published messages.
public ValueTask AddSubscriptionAsync(ISubscriptionDefinition subscription)
Parameters
subscriptionISubscriptionDefinitionThe subscription to add.
Returns
- ValueTask
A task representing the asynchronous operation.
GetAgentAsync(AgentId, bool)
Retrieves an agent by its unique identifier.
public ValueTask<AgentId> GetAgentAsync(AgentId agentId, bool lazy = true)
Parameters
Returns
GetAgentAsync(AgentType, string, bool)
Retrieves an agent by its type.
public ValueTask<AgentId> GetAgentAsync(AgentType agentType, string key = "default", bool lazy = true)
Parameters
agentTypeAgentTypeThe type of the agent.
keystringAn optional key to specify variations of the agent. Defaults to "default".
lazyboolIf
true, the agent is fetched lazily.
Returns
GetAgentAsync(string, string, bool)
Retrieves an agent by its string representation.
public ValueTask<AgentId> GetAgentAsync(string agent, string key = "default", bool lazy = true)
Parameters
agentstringThe string representation of the agent.
keystringAn optional key to specify variations of the agent. Defaults to "default".
lazyboolIf
true, the agent is fetched lazily.
Returns
GetAgentMetadataAsync(AgentId)
Retrieves metadata for an agent.
public ValueTask<AgentMetadata> GetAgentMetadataAsync(AgentId agentId)
Parameters
agentIdAgentIdThe ID of the agent.
Returns
- ValueTask<AgentMetadata>
A task representing the asynchronous operation, returning the agent's metadata.
LoadAgentStateAsync(AgentId, JsonElement)
Loads the saved state into an agent.
public ValueTask LoadAgentStateAsync(AgentId agentId, JsonElement state)
Parameters
agentIdAgentIdThe ID of the agent whose state is being restored.
stateJsonElementThe state dictionary to restore.
Returns
- ValueTask
A task representing the asynchronous operation.
LoadStateAsync(JsonElement)
Loads a previously saved state into the object.
public ValueTask LoadStateAsync(JsonElement state)
Parameters
stateJsonElementA dictionary representing the saved state. The structure of the state is implementation-defined but must be JSON serializable.
Returns
- ValueTask
A task representing the asynchronous operation.
ProcessNextMessageAsync(CancellationToken)
public ValueTask ProcessNextMessageAsync(CancellationToken cancellation = default)
Parameters
cancellationCancellationToken
Returns
PublishMessageAsync(object, TopicId, AgentId?, string?, CancellationToken)
Publishes a message to all agents subscribed to the given topic. No responses are expected from publishing.
public ValueTask PublishMessageAsync(object message, TopicId topic, AgentId? sender = null, string? messageId = null, CancellationToken cancellation = default)
Parameters
messageobjectThe message to publish.
topicTopicIdThe topic to publish the message to.
senderAgentId?The agent sending the message. Defaults to
null.messageIdstringA unique message ID. If
null, a new one will be generated.cancellationCancellationToken
Returns
- ValueTask
A task representing the asynchronous operation.
Exceptions
- UndeliverableException
Thrown if the message cannot be delivered.
RegisterAgentFactoryAsync(AgentType, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>>)
Registers an agent factory with the runtime, associating it with a specific agent type. The type must be unique.
public ValueTask<AgentType> RegisterAgentFactoryAsync(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)
Parameters
typeAgentTypeThe agent type to associate with the factory.
factoryFuncFunc<AgentId, IAgentRuntime, ValueTask<IHostableAgent>>A function that asynchronously creates the agent instance.
Returns
- ValueTask<AgentType>
A task representing the asynchronous operation, returning the registered AgentType.
RegisterAgentFactoryAsync<TAgent>(AgentType, Func<AgentId, IAgentRuntime, ValueTask<TAgent>>)
public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent
Parameters
typeAgentTypefactoryFuncFunc<AgentId, IAgentRuntime, ValueTask<TAgent>>
Returns
Type Parameters
TAgent
RemoveSubscriptionAsync(string)
Removes a subscription from the runtime.
public ValueTask RemoveSubscriptionAsync(string subscriptionId)
Parameters
subscriptionIdstringThe unique identifier of the subscription to remove.
Returns
- ValueTask
A task representing the asynchronous operation.
Exceptions
- KeyNotFoundException
Thrown if the subscription does not exist.
RunUntilIdleAsync()
public Task RunUntilIdleAsync()
Returns
SaveAgentStateAsync(AgentId)
Saves the state of an agent. The result must be JSON serializable.
public ValueTask<JsonElement> SaveAgentStateAsync(AgentId agentId)
Parameters
agentIdAgentIdThe ID of the agent whose state is being saved.
Returns
- ValueTask<JsonElement>
A task representing the asynchronous operation, returning a dictionary of the saved state.
SaveStateAsync()
Saves the current state of the object.
public ValueTask<JsonElement> SaveStateAsync()
Returns
- ValueTask<JsonElement>
A task representing the asynchronous operation, returning a dictionary containing the saved state. The structure of the state is implementation-defined but must be JSON serializable.
SendMessageAsync(object, AgentId, AgentId?, string?, CancellationToken)
Sends a message to an agent and gets a response. This method should be used to communicate directly with an agent.
public ValueTask<object?> SendMessageAsync(object message, AgentId recepient, AgentId? sender = null, string? messageId = null, CancellationToken cancellationToken = default)
Parameters
messageobjectThe message to send.
recepientAgentIdThe agent to send the message to.
senderAgentId?The agent sending the message. Should be
nullif sent from an external source.messageIdstringA unique identifier for the message. If
null, a new ID will be generated.cancellationTokenCancellationTokenA token to cancel the operation if needed.
Returns
- ValueTask<object>
A task representing the asynchronous operation, returning the response from the agent.
Exceptions
- CantHandleException
Thrown if the recipient cannot handle the message.
- UndeliverableException
Thrown if the message cannot be delivered.
StartAsync(CancellationToken)
public ValueTask StartAsync(CancellationToken token = default)
Parameters
tokenCancellationToken
Returns
StopAsync(CancellationToken)
public ValueTask StopAsync(CancellationToken token = default)
Parameters
tokenCancellationToken
Returns
TryGetAgentProxyAsync(AgentId)
Attempts to retrieve an AgentProxy for the specified agent.
public ValueTask<AgentProxy> TryGetAgentProxyAsync(AgentId agentId)
Parameters
agentIdAgentIdThe ID of the agent.
Returns
- ValueTask<AgentProxy>
A task representing the asynchronous operation, returning an AgentProxy if successful.