[docs]classAgentInstantiationContext:def__init__(self)->None:raiseRuntimeError("AgentInstantiationContext cannot be instantiated. It is a static class that provides context management for agent instantiation.")_AGENT_INSTANTIATION_CONTEXT_VAR:ClassVar[ContextVar[tuple[AgentRuntime,AgentId]]]=ContextVar("_AGENT_INSTANTIATION_CONTEXT_VAR")@classmethod@contextmanagerdefpopulate_context(cls,ctx:tuple[AgentRuntime,AgentId])->Generator[None,Any,None]:""":meta private:"""token=AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.set(ctx)try:yieldfinally:AgentInstantiationContext._AGENT_INSTANTIATION_CONTEXT_VAR.reset(token)
[docs]@classmethoddefcurrent_runtime(cls)->AgentRuntime:try:returncls._AGENT_INSTANTIATION_CONTEXT_VAR.get()[0]exceptLookupErrorase:raiseRuntimeError("AgentInstantiationContext.runtime() must be called within an instantiation context such as when the AgentRuntime is instantiating an agent. Mostly likely this was caused by directly instantiating an agent instead of using the AgentRuntime to do so.")frome
[docs]@classmethoddefcurrent_agent_id(cls)->AgentId:try:returncls._AGENT_INSTANTIATION_CONTEXT_VAR.get()[1]exceptLookupErrorase:raiseRuntimeError("AgentInstantiationContext.agent_id() must be called within an instantiation context such as when the AgentRuntime is instantiating an agent. Mostly likely this was caused by directly instantiating an agent instead of using the AgentRuntime to do so.")frome