8000 Test updates · microsoft/Agents-for-python@af5882f · GitHub
[go: up one dir, main page]

Skip to content

Commit af5882f

Browse files
committed
Test updates
1 parent 1b0f2aa commit af5882f

File tree

11 files changed

+818
-1162
lines changed

11 files changed

+818
-1162
lines changed

libraries/Builder/microsoft-agents-builder/microsoft/agents/builder/app/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def adapter(self) -> ChannelServiceAdapter:
113113
return self._adapter
114114

115115
@property
116-
def auth(self) -> AuthManager[StateT]:
116+
def auth(self):
117117
"""
118118
The application's authentication manager
119119
"""

libraries/Builder/microsoft-agents-builder/microsoft/agents/builder/app/state/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
from .conversation_state import ConversationState
2-
from .memory import Memory, MemoryBase
32
from .state import State, StatePropertyAccessor, state
43
from .temp_state import TempState
54
from .turn_state import TurnState
65

76
__all__ = [
87
"StatePropertyAccessor",
98
"ConversationState",
10-
"Memory",
11-
"MemoryBase",
129
"state",
1310
"State",
1411
"StatePropertyAccessor",

libraries/Builder/microsoft-agents-builder/microsoft/agents/builder/app/state/conversation_state.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,39 @@
55

66
from __future__ import annotations
77

8-
from typing import Any, Dict, Optional
8+
from typing import Type
99

1010
from microsoft.agents.storage import Storage, StoreItem
1111

1212
from microsoft.agents.builder.turn_context import TurnContext
13-
from microsoft.agents.builder.app.state.state import State, state
13+
from microsoft.agents.builder.state import AgentState
1414

1515

16-
@state
17-
class ConversationState(State):
16+
class ConversationState(AgentState):
1817
"""
1918
Default Conversation State
2019
"""
2120

22-
@classmethod
23-
async def load(
24-
cls, context: TurnContext, storage: Optional[Storage] = None
25-
) -> "ConversationState":
26-
activity = context.activity
21+
CONTEXT_SERVICE_KEY = "conversation_state"
2722

28-
if not activity.channel_id:
29-
raise ValueError("missing activity.channel_id")
30-
if not activity.conversation:
31-
raise ValueError("missing activity.conversation")
32-
if not activity.recipient:
33-
raise ValueError("missing activity.recipient")
23+
def __init__(self, storage: Storage) -> None:
24+
"""
25+
Initialize ConversationState with a key and optional properties.
3426
35-
channel_id = activity.channel_id
36-
conversation_id = activity.conversation.id
37-
bot_id = activity.recipient.id
38-
key = f"{channel_id}/{bot_id}/conversations/{conversation_id}"
27+
param storage: Storage instance to use for state management.
28+
type storage: Storage
29+
"""
30+
super().__init__(storage=storage, context_service_key=self.CONTEXT_SERVICE_KEY)
3931

40-
if not storage:
41-
return cls(__key__=key)
32+
def get_storage_key(
33+
self, turn_context: TurnContext, *, target_cls: Type[StoreItem] = None
34+
):
35+
channel_id = turn_context.activity.channel_id
36+
if not channel_id:
37+
raise ValueError("Invalid activity: missing channel_id.")
4238

43-
data: Dict[str, Any] = await storage.read([key])
39+
conversation_id = turn_context.activity.conversation.id
40+
if not conversation_id:
41+
raise ValueError("Invalid activity: missing conversation_id.")
4442

45-
if key in data:
46-
if isinstance(data[key], StoreItem):
47-
return cls(__key__=key, **vars(data[key]))
48-
return cls(__key__=key, **data[key])
49-
50-
return cls(__key__=key, **data)
43+
return f"{channel_id}/conversations/{conversation_id}"

libraries/Builder/microsoft-agents-builder/microsoft/agents/builder/app/state/memory.py

Lines changed: 0 additions & 127 deletions
This file was deleted.

libraries/Builder/microsoft-agents-builder/microsoft/agents/builder/app/state/temp_state.py

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,113 @@
55

66
from __future__ import annotations
77

8-
from typing import Dict, List, Optional
8+
from typing import Dict, List, Optional, TypeVar, Callable, Any, Generic
99

1010
from microsoft.agents.storage import Storage
1111

1212
from microsoft.agents.builder.turn_context import TurnContext
1313
from microsoft.agents.builder.app.input_file import InputFile
14-
from microsoft.agents.builder.app.state.state import State, state
14+
from microsoft.agents.builder.state import AgentState
1515

16+
T = TypeVar("T")
1617

17-
@state
18-
class TempState(State):
18+
19+
class TempState(AgentState):
1920
"""
2021
Default Temp State
2122
"""
2223

23-
input: str = ""
24-
"Input passed from the user to the AI Library"
24+
INPUT_FILES_KEY = "inputFiles"
25+
"""Name of the input files key"""
26+
27+
AUTH_TOKEN_KEY = "authTokens"
28+
"""Name of the auth tokens property"""
29+
30+
SCOPE_NAME = "temp"
31+
"""State scope name"""
32+
33+
def __init__(self):
34+
self._state: Dict[str, Any] = {}
35+
36+
@property
37+
def name(self) -> str:
38+
"""Gets the name of this state scope"""
39+
return self.SCOPE_NAME
40+
41+
@property
42+
def input_files(self) -> List[InputFile]:
43+
"""Downloaded files included in the Activity"""
44+
return self.get_value(self.INPUT_FILES_KEY, lambda: [])
45+
46+
@input_files.setter
47+
def input_files(self, value: List[InputFile]) -> None:
48+
self.set_value(self.INPUT_FILES_KEY, value)
49+
50+
def clear_state(self) -> None:
51+
"""Clears all state values"""
52+
self._state.clear()
2553

26-
input_files: List[InputFile] = []
27-
"Downloaded files passed by the user to the AI Library"
54+
async def delete_state(self, turn_context: TurnContext, **_) -> None:
55+
"""Deletes all state values asynchronously"""
56+
self._state.clear()
2857

29-
last_output: str = ""
30-
"Output returned from the last executed action"
58+
def has_value(self, name: str) -> bool:
59+
"""Checks if a value exists in the state with the given name"""
60+
return name in self._state
3161

32-
action_outputs: Dict[str, str] = {}
33-
"All outputs returned from the action sequence that was executed"
62+
def delete_value(self, name: str) -> None:
63+
"""Removes a value from the state"""
64+
if name in self._state:
65+
del self._state[name]
3466

35-
auth_tokens: Dict[str, str] = {}
36-
"User authentication tokens"
67+
def get_value(
68+
self, name: str, default_value_factory: Optional[Callable[[], T]] = None
69+
) -> T:
70+
"""Gets a value from the state with the given name, using a factory for default values if not found"""
71+
if name not in self._state and default_value_factory is not None:
72+
value = default_value_factory()
73+
self.set_value(name, value)
74+
return value
75+
return self._state.get(name)
3776

38-
duplicate_token_exchange: Optional[bool] = None
39-
"Flag indicating whether a token exchange event has already been processed"
77+
def set_value(self, name: str, value: Any) -> None:
78+
"""Sets a value in the state with the given name"""
79+
self._state[name] = value
80+
81+
def get_typed_value(self, type_cls: type) -> Any:
82+
"""Gets a value from the state using the type's full name as the key"""
83+
return self.get_value(type_cls.__module__ + "." + type_cls.__qualname__)
84+
85+
def set_typed_value(self, value: Any) -> None:
86+
"""Sets a value in the state using the type's full name as the key"""
87+
type_cls = type(value)
88+
self.set_value(type_cls.__module__ + "." + type_cls.__qualname__, value)
89+
90+
def is_loaded(self) -> bool:
91+
"""Checks if the state is loaded"""
92+
return True
93+
94+
async def load_async(
95+
self, turn_context: TurnContext, force: bool = False, **_
96+
) -> None:
97+
"""Loads the state asynchronously"""
98+
pass
99+
100+
async def save_changes_async(
101+
self, turn_context: TurnContext, force: bool = False, **_
102+
) -> None:
103+
"""Saves the state changes asynchronously"""
104+
pass
40105

41106
async def save(
42107
self, _context: TurnContext, storage: Optional[Storage] = None
43108
) -> None:
109+
"""Legacy method to save the state"""
44110
return
45111

46112
@classmethod
47113
async def load(
48114
cls, context: TurnContext, storage: Optional[Storage] = None
49115
) -> "TempState":
50-
# pylint: disable=unused-argument
116+
"""Legacy method to load the state"""
51117
return cls()

0 commit comments

Comments
 (0)
0