mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Scope provider state by source_id and standardize source IDs (#3995)
* Initial plan * Add FoundryMemoryProvider and tests Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Add sample and documentation for FoundryMemoryProvider Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Address code review feedback for FoundryMemoryProvider Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Address PR review comments: Add DEFAULT_SOURCE_ID, use logging.getLogger, move state to session.state Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Fix Foundry memory ItemParam usage and exports Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Refactor provider hook state and standardize source IDs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Support endpoint-based Foundry memory init Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix core README workflows link Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated implementation and sample * Split out Foundry memory provider changes Remove FoundryMemoryProvider implementation/tests/sample plus export and docs mentions from this branch so only non-Foundry changes remain. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger CI rerun for PR #3995 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
a5f948c215
commit
cc98d5b6f7
@@ -23,12 +23,14 @@ class UserInfo(BaseModel):
|
||||
class UserInfoMemory(BaseContextProvider):
|
||||
"""Context provider that extracts and remembers user info (name, age).
|
||||
|
||||
State is stored in ``session.state["user-info-memory"]`` so it survives
|
||||
State is stored in ``session.state["user_info_memory"]`` so it survives
|
||||
serialization via ``session.to_dict()`` / ``AgentSession.from_dict()``.
|
||||
"""
|
||||
|
||||
DEFAULT_SOURCE_ID = "user_info_memory"
|
||||
|
||||
def __init__(self, client: SupportsChatGetResponse):
|
||||
super().__init__("user-info-memory")
|
||||
super().__init__(self.DEFAULT_SOURCE_ID)
|
||||
self._chat_client = client
|
||||
|
||||
async def before_run(
|
||||
@@ -40,8 +42,7 @@ class UserInfoMemory(BaseContextProvider):
|
||||
state: dict[str, Any],
|
||||
) -> None:
|
||||
"""Provide user information context before each agent call."""
|
||||
my_state = state.setdefault(self.source_id, {})
|
||||
user_info = my_state.setdefault("user_info", UserInfo())
|
||||
user_info = state.setdefault("user_info", UserInfo())
|
||||
|
||||
instructions: list[str] = []
|
||||
|
||||
@@ -70,8 +71,7 @@ class UserInfoMemory(BaseContextProvider):
|
||||
state: dict[str, Any],
|
||||
) -> None:
|
||||
"""Extract user information from messages after each agent call."""
|
||||
my_state = state.setdefault(self.source_id, {})
|
||||
user_info = my_state.setdefault("user_info", UserInfo())
|
||||
user_info = state.setdefault("user_info", UserInfo())
|
||||
if user_info.name is not None and user_info.age is not None:
|
||||
return # Already have everything
|
||||
|
||||
@@ -92,7 +92,7 @@ class UserInfoMemory(BaseContextProvider):
|
||||
user_info.name = extracted.name
|
||||
if extracted and user_info.age is None and extracted.age:
|
||||
user_info.age = extracted.age
|
||||
state.setdefault(self.source_id, {})["user_info"] = user_info
|
||||
state["user_info"] = user_info
|
||||
except Exception:
|
||||
pass # Failed to extract, continue without updating
|
||||
|
||||
@@ -113,7 +113,7 @@ async def main():
|
||||
print(await agent.run("I am 20 years old", session=session))
|
||||
|
||||
# Inspect extracted user info from session state
|
||||
user_info = session.state.get("user-info-memory", {}).get("user_info", UserInfo())
|
||||
user_info = session.state.get(UserInfoMemory.DEFAULT_SOURCE_ID, {}).get("user_info", UserInfo())
|
||||
print()
|
||||
print(f"MEMORY - User Name: {user_info.name}")
|
||||
print(f"MEMORY - User Age: {user_info.age}")
|
||||
|
||||
Reference in New Issue
Block a user