From 332f901be562b7d5d16576424c94fe0ea6fc07b9 Mon Sep 17 00:00:00 2001 From: Axel Suarez Martinez Date: Thu, 28 Aug 2025 15:32:20 -0700 Subject: [PATCH 1/2] Fixing temp state save implementation (ignore but dont throw) --- .../microsoft_agents/hosting/core/app/state/temp_state.py | 4 ++++ .../microsoft_agents/hosting/core/state/agent_state.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py index 546d53b4..52901d71 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py @@ -96,6 +96,10 @@ async def load(self, turn_context: TurnContext, force: bool = False, **_) -> Non """Loads the state asynchronously""" pass + async def save(self, turn_context, force=False): + """Saves the state asynchronously""" + pass + async def save_changes( self, turn_context: TurnContext, force: bool = False, **_ ) -> None: diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py index a30312b6..fb55cf6d 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py @@ -215,7 +215,7 @@ def get_value( else None ) - if not value and default_value_factory: + if not value and default_value_factory is not None: # If the value is None and a factory is provided, call the factory to get a default value return default_value_factory() From 2d59a93f9d98c6933ab52b15368cca19847c6281 Mon Sep 17 00:00:00 2001 From: Axel Suarez Date: Fri, 29 Aug 2025 14:59:32 -0700 Subject: [PATCH 2/2] TurnState fixes --- .../hosting/core/app/agent_application.py | 6 +++--- .../hosting/core/app/state/conversation_state.py | 3 +++ .../hosting/core/app/state/temp_state.py | 2 +- .../hosting/core/app/state/turn_state.py | 13 +++++++++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py index a379c35c..01e5bb7b 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py @@ -730,7 +730,7 @@ async def _on_turn(self, context: TurnContext): await self._on_activity(context, turn_state) logger.debug("Running after turn middleware") - if not await self._run_after_turn_middleware(context, turn_state): + if await self._run_after_turn_middleware(context, turn_state): await turn_state.save(context) return except ApplicationError as err: @@ -798,7 +798,7 @@ async def _run_before_turn_middleware(self, context: TurnContext, state: StateT) for before_turn in self._internal_before_turn: is_ok = await before_turn(context, state) if not is_ok: - await state.save(context, self._options.storage) + await state.save(context) return False return True @@ -824,7 +824,7 @@ async def _run_after_turn_middleware(self, context: TurnContext, state: StateT): for after_turn in self._internal_after_turn: is_ok = await after_turn(context, state) if not is_ok: - await state.save(context, self._options.storage) + await state.save(context) return False return True diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/conversation_state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/conversation_state.py index d1baac17..d7436ab3 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/conversation_state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/conversation_state.py @@ -46,3 +46,6 @@ def get_storage_key( raise ValueError("Invalid activity: missing conversation_id.") return f"{channel_id}/conversations/{conversation_id}" + + def clear(self, turn_context): + return super().clear(turn_context) diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py index 52901d71..cb954c72 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py @@ -48,7 +48,7 @@ def input_files(self) -> List[InputFile]: def input_files(self, value: List[InputFile]) -> None: self.set_value(self.INPUT_FILES_KEY, value) - def clear(self) -> None: + def clear(self, turn_context: TurnContext) -> None: """Clears all state values""" self._state.clear() diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/turn_state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/turn_state.py index bf6dce74..be1f8339 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/turn_state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/turn_state.py @@ -246,16 +246,21 @@ async def load(self, turn_context: TurnContext, force: bool = False) -> None: ] await asyncio.gather(*tasks) - def clear(self, scope: str) -> None: + def clear(self, turn_context: TurnContext, scope: str = None) -> None: """ Clears a state scope. Args: scope: The name of the scope to clear. """ - scope_obj = self.get_scope_by_name(scope) - if hasattr(scope_obj, "clear"): - scope_obj.clear() + if scope: + scope_obj = self.get_scope_by_name(scope) + if hasattr(scope_obj, "clear"): + scope_obj.clear(turn_context) + else: + for scope in self._scopes.values(): + if hasattr(scope, "clear"): + scope.clear(turn_context) async def save(self, turn_context: TurnContext, force: bool = False) -> None: """