2323
2424from .aswarm import Agent as SwarmAgent , Swarm
2525from .chat_agent import ChatHistory , ChatMessage , Delegate
26+ from .memory import Memory , NoMemory
2627from .model import default_model , Model
2728
2829
@@ -47,6 +48,7 @@ def __init__(
4748 name : str = "" ,
4849 system : str = "" ,
4950 model : Model = default_model ,
51+ memory : Memory | None = None ,
5052 static_agents : list [str ] | None = None ,
5153 dynamic_agents : list [DiscoveryQuery ] | None = None ,
5254 timeout : float = 300 ,
@@ -64,6 +66,7 @@ def __init__(
6466 self ._sub_agents : dict [str , Schema ] = {}
6567 self ._swarm_agent : SwarmAgent | None = None
6668
69+ self ._memory : Memory = memory or NoMemory ()
6770 self ._history : ChatHistory = ChatHistory (messages = [])
6871
6972 @property
@@ -83,6 +86,10 @@ def system(self) -> str:
8386 def model (self ) -> Model :
8487 return self ._model
8588
89+ @property
90+ def memory (self ) -> Memory :
91+ return self ._memory
92+
8693 @property
8794 def static_agents (self ) -> list [str ] | None :
8895 return self ._static_agents
@@ -131,6 +138,7 @@ async def _update_swarm_agent(self) -> None:
131138
132139 def _transfer_to_agent (self , agent_type : str ):
133140 async def run () -> AsyncIterator [ChatMessage ]:
141+ # TODO: Handle memory?
134142 async for chunk in Delegate (self , agent_type ).handle (self ._history ):
135143 yield chunk
136144
@@ -203,6 +211,7 @@ async def deregister_sub_agents(
203211 async def handle_history (
204212 self , msg : ChatHistory , ctx : Context
205213 ) -> AsyncIterator [ChatMessage ]:
214+ # TODO: Handle memory?
206215 response = self ._handle_history (msg , ctx )
207216 async for resp in response :
208217 yield resp
@@ -211,10 +220,19 @@ async def handle_history(
211220 async def handle_message (
212221 self , msg : ChatMessage , ctx : Context
213222 ) -> AsyncIterator [ChatMessage ]:
214- history = ChatHistory (messages = [msg ])
223+ existing = await self .memory .get_items ()
224+ history = ChatHistory (messages = existing + [msg ])
225+
215226 response = self ._handle_history (history , ctx )
227+ full_content = ""
216228 async for resp in response :
217229 yield resp
230+ full_content += resp .content
231+
232+ await self .memory .add_items (
233+ msg , # input item
234+ ChatMessage (role = "assistant" , content = full_content ), # output item
235+ )
218236
219237 async def _handle_history (
220238 self , msg : ChatHistory , ctx : Context
0 commit comments