"""Protocol definitions for the Haive engine system.This module defines protocol classes that establish common interfacesfor various components in the Haive framework. These protocols enablestatic type checking, runtime type checking, and duck typing for componentsthat implement common behaviors."""fromtypingimportProtocol,TypeVar,runtime_checkable# Type variablesI=TypeVar("I")# Input typeO=TypeVar("O")# Output type
[docs]@runtime_checkableclassInvokable(Protocol[I,O]):"""Protocol for objects that can be invoked synchronously. Defines a common interface for any object that can process input data and return output data through an invoke method. This enables consistent interaction with different components that share this capability. Type Parameters: I: The input data type. O: The output data type. Examples: >>> from typing import Dict, Any >>> class MyProcessor: ... def invoke(self, input_data: str, **kwargs) -> Dict[str, Any]: ... return {"processed": input_data.upper()} ... >>> processor = MyProcessor() >>> from haive.core.engine.base.protocols import Invokable >>> isinstance(processor, Invokable) True >>> result = processor.invoke("hello") >>> result {'processed': 'HELLO'} """
[docs]definvoke(self,input_data:I,**kwargs)->O:"""Process input data and return output data. Args: input_data (I): The input data to process. **kwargs: Additional keyword arguments for processing. Returns: O: The processed output data. """...
[docs]@runtime_checkableclassAsyncInvokable(Protocol[I,O]):"""Protocol for objects that can be invoked asynchronously. Defines a common interface for any object that can process input data and return output data through an asynchronous ainvoke method. This allows for non-blocking invocation of components that implement this interface. Type Parameters: I: The input data type. O: The output data type. Examples: >>> import asyncio >>> from typing import Dict, Any >>> class MyAsyncProcessor: ... async def ainvoke(self, input_data: str, **kwargs) -> Dict[str, Any]: ... await asyncio.sleep(0.1) # Simulate async work ... return {"processed": input_data.upper()} ... >>> processor = MyAsyncProcessor() >>> from haive.core.engine.base.protocols import AsyncInvokable >>> isinstance(processor, AsyncInvokable) True >>> # Usage in an async context >>> async def process(): ... result = await processor.ainvoke("hello") ... print(result) >>> # {'processed': 'HELLO'} """
[docs]asyncdefainvoke(self,input_data:I,**kwargs)->O:"""Process input data asynchronously and return output data. Args: input_data (I): The input data to process. **kwargs: Additional keyword arguments for processing. Returns: O: The processed output data. """...