Skip to content

Commit 9b813cc

Browse files
authored
Turn State fixes and auto saving in application layer (#107)
* Fixing temp state save implementation (ignore but dont throw) * TurnState fixes
1 parent cec8be4 commit 9b813cc

5 files changed

Lines changed: 21 additions & 9 deletions

File tree

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ async def _on_turn(self, context: TurnContext):
730730
await self._on_activity(context, turn_state)
731731

732732
logger.debug("Running after turn middleware")
733-
if not await self._run_after_turn_middleware(context, turn_state):
733+
if await self._run_after_turn_middleware(context, turn_state):
734734
await turn_state.save(context)
735735
return
736736
except ApplicationError as err:
@@ -798,7 +798,7 @@ async def _run_before_turn_middleware(self, context: TurnContext, state: StateT)
798798
for before_turn in self._internal_before_turn:
799799
is_ok = await before_turn(context, state)
800800
if not is_ok:
801-
await state.save(context, self._options.storage)
801+
await state.save(context)
802802
return False
803803
return True
804804

@@ -824,7 +824,7 @@ async def _run_after_turn_middleware(self, context: TurnContext, state: StateT):
824824
for after_turn in self._internal_after_turn:
825825
is_ok = await after_turn(context, state)
826826
if not is_ok:
827-
await state.save(context, self._options.storage)
827+
await state.save(context)
828828
return False
829829
return True
830830

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/conversation_state.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ def get_storage_key(
4646
raise ValueError("Invalid activity: missing conversation_id.")
4747

4848
return f"{channel_id}/conversations/{conversation_id}"
49+
50+
def clear(self, turn_context):
51+
return super().clear(turn_context)

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/temp_state.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def input_files(self) -> List[InputFile]:
4848
def input_files(self, value: List[InputFile]) -> None:
4949
self.set_value(self.INPUT_FILES_KEY, value)
5050

51-
def clear(self) -> None:
51+
def clear(self, turn_context: TurnContext) -> None:
5252
"""Clears all state values"""
5353
self._state.clear()
5454

@@ -96,6 +96,10 @@ async def load(self, turn_context: TurnContext, force: bool = False, **_) -> Non
9696
"""Loads the state asynchronously"""
9797
pass
9898

99+
async def save(self, turn_context, force=False):
100+
"""Saves the state asynchronously"""
101+
pass
102+
99103
async def save_changes(
100104
self, turn_context: TurnContext, force: bool = False, **_
101105
) -> None:

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/turn_state.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,21 @@ async def load(self, turn_context: TurnContext, force: bool = False) -> None:
246246
]
247247
await asyncio.gather(*tasks)
248248

249-
def clear(self, scope: str) -> None:
249+
def clear(self, turn_context: TurnContext, scope: str = None) -> None:
250250
"""
251251
Clears a state scope.
252252
253253
Args:
254254
scope: The name of the scope to clear.
255255
"""
256-
scope_obj = self.get_scope_by_name(scope)
257-
if hasattr(scope_obj, "clear"):
258-
scope_obj.clear()
256+
if scope:
257+
scope_obj = self.get_scope_by_name(scope)
258+
if hasattr(scope_obj, "clear"):
259+
scope_obj.clear(turn_context)
260+
else:
261+
for scope in self._scopes.values():
262+
if hasattr(scope, "clear"):
263+
scope.clear(turn_context)
259264

260265
async def save(self, turn_context: TurnContext, force: bool = False) -> None:
261266
"""

libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/state/agent_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def get_value(
215215
else None
216216
)
217217

218-
if not value and default_value_factory:
218+
if not value and default_value_factory is not None:
219219
# If the value is None and a factory is provided, call the factory to get a default value
220220
return default_value_factory()
221221

0 commit comments

Comments
 (0)