-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: use session_service and memory_service from invocation context #2890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,10 +53,10 @@ def __init__(self, agent: BaseAgent, skip_summarization: bool = False): | |
|
||
super().__init__(name=agent.name, description=agent.description) | ||
|
||
@model_validator(mode='before') | ||
@model_validator(mode="before") | ||
@classmethod | ||
def populate_name(cls, data: Any) -> Any: | ||
data['name'] = data['agent'].name | ||
data["name"] = data["agent"].name | ||
return data | ||
|
||
@override | ||
|
@@ -73,11 +73,11 @@ def _get_declaration(self) -> types.FunctionDeclaration: | |
parameters=types.Schema( | ||
type=types.Type.OBJECT, | ||
properties={ | ||
'request': types.Schema( | ||
"request": types.Schema( | ||
type=types.Type.STRING, | ||
), | ||
}, | ||
required=['request'], | ||
required=["request"], | ||
), | ||
description=self.agent.description, | ||
name=self.name, | ||
|
@@ -105,15 +105,14 @@ async def run_async( | |
) -> Any: | ||
from ..agents.llm_agent import LlmAgent | ||
from ..runners import Runner | ||
from ..sessions.in_memory_session_service import InMemorySessionService | ||
|
||
if self.skip_summarization: | ||
tool_context.actions.skip_summarization = True | ||
|
||
if isinstance(self.agent, LlmAgent) and self.agent.input_schema: | ||
input_value = self.agent.input_schema.model_validate(args) | ||
content = types.Content( | ||
role='user', | ||
role="user", | ||
parts=[ | ||
types.Part.from_text( | ||
text=input_value.model_dump_json(exclude_none=True) | ||
|
@@ -122,15 +121,15 @@ async def run_async( | |
) | ||
else: | ||
content = types.Content( | ||
role='user', | ||
parts=[types.Part.from_text(text=args['request'])], | ||
role="user", | ||
parts=[types.Part.from_text(text=args["request"])], | ||
) | ||
runner = Runner( | ||
app_name=self.agent.name, | ||
agent=self.agent, | ||
artifact_service=ForwardingArtifactService(tool_context), | ||
session_service=InMemorySessionService(), | ||
memory_service=InMemoryMemoryService(), | ||
session_service=tool_context._invocation_context.session_service, | ||
memory_service=tool_context._invocation_context.memory_service, | ||
Comment on lines
+131
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this change correctly sources the services from the invocation context, it relies on accessing the protected member For better maintainability and a cleaner design, consider exposing these services as public properties on Example of what could be added to @property
def session_service(self) -> BaseSessionService:
return self._invocation_context.session_service
@property
def memory_service(self) -> Optional[BaseMemoryService]:
return self._invocation_context.memory_service With this change, the code here would become |
||
credential_service=tool_context._invocation_context.credential_service, | ||
plugins=list(tool_context._invocation_context.plugin_manager.plugins), | ||
) | ||
|
@@ -154,8 +153,8 @@ async def run_async( | |
last_content = event.content | ||
|
||
if not last_content: | ||
return '' | ||
merged_text = '\n'.join(p.text for p in last_content.parts if p.text) | ||
return "" | ||
merged_text = "\n".join(p.text for p in last_content.parts if p.text) | ||
if isinstance(self.agent, LlmAgent) and self.agent.output_schema: | ||
tool_result = self.agent.output_schema.model_validate_json( | ||
merged_text | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might want to revert the style changes (unless they are coming from ruff and not your personal settings :)