generated from google/new-project
-
Notifications
You must be signed in to change notification settings - Fork 489
feat: Add support for creating dynamic agent cards in the helloworld #288
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
Open
dmandar
wants to merge
6
commits into
main
Choose a base branch
from
md-agentcardshw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03bf5bb
feat: Add support for creating dynamic agent cards in the helloworld …
dmandar feb49c5
Merge branch 'main' into md-agentcardshw
holtskinner a52cc18
Fix review comments
dmandar d5c8499
Merge branch 'md-agentcardshw' of https://github.com/a2aproject/a2a-s…
dmandar 9a9172f
Merge branch 'main' into md-agentcardshw
dmandar 90a02fa
Update samples/python/agents/helloworld/pyproject.toml
holtskinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import datetime | ||
|
|
||
| import click | ||
| import uvicorn | ||
|
|
||
| from a2a.server.agent_execution import RequestContext | ||
| from a2a.server.apps import A2AStarletteApplication | ||
| from a2a.server.request_handlers import DefaultRequestHandler | ||
| from a2a.server.tasks import InMemoryTaskStore | ||
|
|
@@ -13,26 +17,94 @@ | |
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # --8<-- [start:AgentSkill] | ||
| skill = AgentSkill( | ||
| id='hello_world', | ||
| name='Returns hello world', | ||
| description='just returns hello world', | ||
| tags=['hello world'], | ||
| examples=['hi', 'hello world'], | ||
| ) | ||
| # --8<-- [end:AgentSkill] | ||
| def create_dynamic_public_card(base_card: AgentCard) -> AgentCard: | ||
| """Dynamically modifies the public agent card. | ||
| This function is called for each request to the public card endpoint. | ||
| It can be used to add dynamic information, like a timestamp. | ||
| Args: | ||
| base_card: The base agent card to use as a template. | ||
| Returns: | ||
| A new, dynamically generated AgentCard. | ||
| """ | ||
| # Start with a copy of the base card to avoid modifying the original | ||
| public_card = base_card.model_copy(deep=True) | ||
| now = datetime.datetime.now(datetime.timezone.utc).isoformat() | ||
| # Set the description directly to make the function more robust. | ||
| # This avoids depending on the description from the base_card. | ||
| public_card.description = f'Just a hello world agent (last updated: {now})' | ||
| return public_card | ||
|
|
||
|
|
||
| def create_dynamic_extended_card( | ||
| base_card: AgentCard, context: RequestContext | ||
| ) -> AgentCard: | ||
| """Dynamically creates an extended agent card from a base card. | ||
| This function is called for each request to the extended card endpoint, | ||
| allowing the card to be customized based on the request context, such as | ||
| authentication headers. | ||
| Args: | ||
| base_card: The public agent card to use as a template. | ||
| context: The server call context, containing request information. | ||
| Returns: | ||
| A new, dynamically generated AgentCard. | ||
| """ | ||
| # Start with a copy of the base card to avoid modifying the original | ||
| extended_card = base_card.model_copy(deep=True) | ||
|
|
||
| # Add the extended skill that is always present for authenticated users | ||
| extended_skill = AgentSkill( | ||
| id='super_hello_world', | ||
| name='Returns a SUPER Hello World', | ||
| description='A more enthusiastic greeting, only for authenticated users.', | ||
| tags=['hello world', 'super', 'extended'], | ||
| examples=['super hi', 'give me a super hello'], | ||
| ) | ||
| extended_card.skills.append(extended_skill) | ||
|
|
||
| # Update basic properties for the extended card | ||
| extended_card.name = 'Hello World Agent - Extended Edition' | ||
| extended_card.description = ( | ||
| 'The full-featured hello world agent for authenticated users.' | ||
| ) | ||
| extended_card.version = '1.0.1' | ||
|
|
||
| return extended_card | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option( | ||
| '--dynamic-public-card', | ||
| is_flag=True, | ||
| default=False, | ||
| help='Enable dynamic public agent card.', | ||
| ) | ||
| @click.option( | ||
| '--enable-extended-card', | ||
| is_flag=True, | ||
| default=False, | ||
| help='Enable support for an extended agent card.', | ||
| ) | ||
| @click.option( | ||
| '--dynamic-extended-card', | ||
| is_flag=True, | ||
| default=False, | ||
| help='Make the extended agent card dynamic (requires --enable-extended-card).', | ||
| ) | ||
| def main(dynamic_public_card, enable_extended_card, dynamic_extended_card): | ||
| skill = AgentSkill( | ||
| id='hello_world', | ||
| name='Returns hello world', | ||
| description='just returns hello world', | ||
| tags=['hello world'], | ||
| examples=['hi', 'hello world'], | ||
| ) | ||
dmandar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # --8<-- [start:AgentCard] | ||
| # This will be the public-facing agent card | ||
| public_agent_card = AgentCard( | ||
| name='Hello World Agent', | ||
|
|
@@ -42,36 +114,55 @@ | |
| default_input_modes=['text'], | ||
| default_output_modes=['text'], | ||
| capabilities=AgentCapabilities(streaming=True), | ||
| skills=[skill], # Only the basic skill for the public card | ||
| supports_authenticated_extended_card=True, | ||
| ) | ||
| # --8<-- [end:AgentCard] | ||
|
Member
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. Please add these region tags back, they are used in the public tutorial https://a2a-protocol.org/latest/tutorials/python/3-agent-skills-and-card/#agent-skills |
||
|
|
||
| # This will be the authenticated extended agent card | ||
| # It includes the additional 'extended_skill' | ||
| specific_extended_agent_card = public_agent_card.model_copy( | ||
| update={ | ||
| 'name': 'Hello World Agent - Extended Edition', # Different name for clarity | ||
| 'description': 'The full-featured hello world agent for authenticated users.', | ||
| 'version': '1.0.1', # Could even be a different version | ||
| # Capabilities and other fields like url, default_input_modes, default_output_modes, | ||
| # supports_authenticated_extended_card are inherited from public_agent_card unless specified here. | ||
| 'skills': [ | ||
| skill, | ||
| extended_skill, | ||
| ], # Both skills for the extended card | ||
| } | ||
| skills=[skill], | ||
| supports_authenticated_extended_card=enable_extended_card, | ||
| ) | ||
|
|
||
| request_handler = DefaultRequestHandler( | ||
| agent_executor=HelloWorldAgentExecutor(), | ||
| task_store=InMemoryTaskStore(), | ||
| ) | ||
|
|
||
| card_modifier_func = ( | ||
| create_dynamic_public_card if dynamic_public_card else None | ||
| ) | ||
| extended_card_modifier_func = None | ||
| static_extended_card = None | ||
|
|
||
| if enable_extended_card: | ||
| if dynamic_extended_card: | ||
| extended_card_modifier_func = create_dynamic_extended_card | ||
| else: | ||
| # Create a static extended card if dynamic is not requested | ||
| extended_skill = AgentSkill( | ||
| id='super_hello_world', | ||
| name='Returns a SUPER Hello World', | ||
| description='A more enthusiastic greeting, only for authenticated users.', | ||
| tags=['hello world', 'super', 'extended'], | ||
| examples=['super hi', 'give me a super hello'], | ||
| ) | ||
dmandar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static_extended_card = public_agent_card.model_copy( | ||
| update={ | ||
| 'name': 'Hello World Agent - Extended Edition', | ||
| 'description': 'The full-featured hello world agent for authenticated users.', | ||
| 'version': '1.0.1', | ||
| 'skills': [ | ||
| skill, | ||
| extended_skill, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| server = A2AStarletteApplication( | ||
| agent_card=public_agent_card, | ||
| http_handler=request_handler, | ||
| extended_agent_card=specific_extended_agent_card, | ||
| card_modifier=card_modifier_func, | ||
| extended_agent_card=static_extended_card, | ||
| extended_card_modifier=extended_card_modifier_func, | ||
| ) | ||
|
|
||
| uvicorn.run(server.build(), host='0.0.0.0', port=9999) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.