forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_card_builder.py
More file actions
580 lines (476 loc) · 17.4 KB
/
agent_card_builder.py
File metadata and controls
580 lines (476 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import re
from typing import Dict
from typing import List
from typing import Optional
from a2a.types import AgentCapabilities
from a2a.types import AgentCard
from a2a.types import AgentProvider
from a2a.types import AgentSkill
from a2a.types import SecurityScheme
from ...agents.base_agent import BaseAgent
from ...agents.llm_agent import LlmAgent
from ...agents.loop_agent import LoopAgent
from ...agents.parallel_agent import ParallelAgent
from ...agents.sequential_agent import SequentialAgent
from ...tools.example_tool import ExampleTool
from ..experimental import a2a_experimental
@a2a_experimental
class AgentCardBuilder:
"""Builder class for creating agent cards from ADK agents.
This class provides functionality to convert ADK agents into A2A agent cards,
including extracting skills, capabilities, and metadata from various agent
types.
"""
def __init__(
self,
*,
agent: BaseAgent,
rpc_url: Optional[str] = None,
capabilities: Optional[AgentCapabilities] = None,
doc_url: Optional[str] = None,
provider: Optional[AgentProvider] = None,
agent_version: Optional[str] = None,
security_schemes: Optional[Dict[str, SecurityScheme]] = None,
):
if not agent:
raise ValueError('Agent cannot be None or empty.')
self._agent = agent
self._rpc_url = rpc_url or 'http://localhost:80/a2a'
self._capabilities = capabilities or AgentCapabilities()
self._doc_url = doc_url
self._provider = provider
self._security_schemes = security_schemes
self._agent_version = agent_version or '0.0.1'
async def build(self) -> AgentCard:
"""Build and return the complete agent card."""
try:
primary_skills = await _build_primary_skills(self._agent)
sub_agent_skills = await _build_sub_agent_skills(self._agent)
all_skills = primary_skills + sub_agent_skills
return AgentCard(
name=self._agent.name,
description=self._agent.description or 'An ADK Agent',
doc_url=self._doc_url,
url=f"{self._rpc_url.rstrip('/')}",
version=self._agent_version,
capabilities=self._capabilities,
skills=all_skills,
default_input_modes=['text/plain'],
default_output_modes=['text/plain'],
supports_authenticated_extended_card=False,
provider=self._provider,
security_schemes=self._security_schemes,
)
except Exception as e:
raise RuntimeError(
f'Failed to build agent card for {self._agent.name}: {e}'
) from e
# Module-level helper functions
async def _build_primary_skills(agent: BaseAgent) -> List[AgentSkill]:
"""Build skills for any agent type."""
if isinstance(agent, LlmAgent):
return await _build_llm_agent_skills(agent)
else:
return await _build_non_llm_agent_skills(agent)
async def _build_llm_agent_skills(agent: LlmAgent) -> List[AgentSkill]:
"""Build skills for LLM agent."""
skills = []
# 1. Agent skill (main model skill)
agent_description = _build_llm_agent_description_with_instructions(agent)
agent_examples = await _extract_examples_from_agent(agent)
skills.append(
AgentSkill(
id=agent.name,
name='model',
description=agent_description,
examples=_extract_inputs_from_examples(agent_examples),
input_modes=_get_input_modes(agent),
output_modes=_get_output_modes(agent),
tags=['llm'],
)
)
# 2. Tool skills
if agent.tools:
tool_skills = await _build_tool_skills(agent)
skills.extend(tool_skills)
# 3. Planner skill
if agent.planner:
skills.append(_build_planner_skill(agent))
# 4. Code executor skill
if agent.code_executor:
skills.append(_build_code_executor_skill(agent))
return skills
async def _build_sub_agent_skills(agent: BaseAgent) -> List[AgentSkill]:
"""Build skills for all sub-agents."""
sub_agent_skills = []
for sub_agent in agent.sub_agents:
try:
sub_skills = await _build_primary_skills(sub_agent)
for skill in sub_skills:
# Create a new skill instance to avoid modifying original if shared
aggregated_skill = AgentSkill(
id=f'{sub_agent.name}_{skill.id}',
name=f'{sub_agent.name}: {skill.name}',
description=skill.description,
examples=skill.examples,
input_modes=skill.input_modes,
output_modes=skill.output_modes,
tags=[f'sub_agent:{sub_agent.name}'] + (skill.tags or []),
)
sub_agent_skills.append(aggregated_skill)
except Exception as e:
# Log warning but continue with other sub-agents
print(
f'Warning: Failed to build skills for sub-agent {sub_agent.name}: {e}'
)
continue
return sub_agent_skills
async def _build_tool_skills(agent: LlmAgent) -> List[AgentSkill]:
"""Build skills for agent tools."""
tool_skills = []
canonical_tools = await agent.canonical_tools()
for tool in canonical_tools:
# Skip example tools as they're handled separately
if isinstance(tool, ExampleTool):
continue
tool_name = (
tool.name
if hasattr(tool, 'name') and tool.name
else tool.__class__.__name__
)
tool_skills.append(
AgentSkill(
id=f'{agent.name}-{tool_name}',
name=tool_name,
description=getattr(tool, 'description', f'Tool: {tool_name}'),
examples=None,
input_modes=None,
output_modes=None,
tags=['llm', 'tools'],
)
)
return tool_skills
def _build_planner_skill(agent: LlmAgent) -> AgentSkill:
"""Build planner skill for LLM agent."""
return AgentSkill(
id=f'{agent.name}-planner',
name='planning',
description='Can think about the tasks to do and make plans',
examples=None,
input_modes=None,
output_modes=None,
tags=['llm', 'planning'],
)
def _build_code_executor_skill(agent: LlmAgent) -> AgentSkill:
"""Build code executor skill for LLM agent."""
return AgentSkill(
id=f'{agent.name}-code-executor',
name='code-execution',
description='Can execute code',
examples=None,
input_modes=None,
output_modes=None,
tags=['llm', 'code_execution'],
)
async def _build_non_llm_agent_skills(agent: BaseAgent) -> List[AgentSkill]:
"""Build skills for non-LLM agents."""
skills = []
# 1. Agent skill (main agent skill)
agent_description = _build_agent_description(agent)
agent_examples = await _extract_examples_from_agent(agent)
# Determine agent type and name
agent_type = _get_agent_type(agent)
agent_name = _get_agent_skill_name(agent)
skills.append(
AgentSkill(
id=agent.name,
name=agent_name,
description=agent_description,
examples=_extract_inputs_from_examples(agent_examples),
input_modes=_get_input_modes(agent),
output_modes=_get_output_modes(agent),
tags=[agent_type],
)
)
# 2. Sub-agent orchestration skill (for agents with sub-agents)
if agent.sub_agents:
orchestration_skill = _build_orchestration_skill(agent, agent_type)
if orchestration_skill:
skills.append(orchestration_skill)
return skills
def _build_orchestration_skill(
agent: BaseAgent, agent_type: str
) -> Optional[AgentSkill]:
"""Build orchestration skill for agents with sub-agents."""
sub_agent_descriptions = []
for sub_agent in agent.sub_agents:
description = sub_agent.description or 'No description'
sub_agent_descriptions.append(f'{sub_agent.name}: {description}')
if not sub_agent_descriptions:
return None
return AgentSkill(
id=f'{agent.name}-sub-agents',
name='sub-agents',
description='Orchestrates: ' + '; '.join(sub_agent_descriptions),
examples=None,
input_modes=None,
output_modes=None,
tags=[agent_type, 'orchestration'],
)
def _get_agent_type(agent: BaseAgent) -> str:
"""Get the agent type for tagging."""
if isinstance(agent, LlmAgent):
return 'llm'
elif isinstance(agent, SequentialAgent):
return 'sequential_workflow'
elif isinstance(agent, ParallelAgent):
return 'parallel_workflow'
elif isinstance(agent, LoopAgent):
return 'loop_workflow'
else:
return 'custom_agent'
def _get_agent_skill_name(agent: BaseAgent) -> str:
"""Get the skill name based on agent type."""
if isinstance(agent, LlmAgent):
return 'model'
elif isinstance(agent, (SequentialAgent, ParallelAgent, LoopAgent)):
return 'workflow'
else:
return 'custom'
def _build_agent_description(agent: BaseAgent) -> str:
"""Build agent description from agent.description and workflow-specific descriptions."""
description_parts = []
# Add agent description
if agent.description:
description_parts.append(agent.description)
# Add workflow-specific descriptions for non-LLM agents
if not isinstance(agent, LlmAgent):
workflow_description = _get_workflow_description(agent)
if workflow_description:
description_parts.append(workflow_description)
return (
' '.join(description_parts)
if description_parts
else _get_default_description(agent)
)
def _build_llm_agent_description_with_instructions(agent: LlmAgent) -> str:
"""Build agent description including instructions for LlmAgents."""
description_parts = []
# Add agent description
if agent.description:
description_parts.append(agent.description)
# Add instruction (with pronoun replacement) - only for LlmAgent
if agent.instruction:
instruction = _replace_pronouns(agent.instruction)
description_parts.append(instruction)
# Add global instruction (with pronoun replacement) - only for LlmAgent
if agent.global_instruction:
global_instruction = _replace_pronouns(agent.global_instruction)
description_parts.append(global_instruction)
return (
' '.join(description_parts)
if description_parts
else _get_default_description(agent)
)
def _replace_pronouns(text: str) -> str:
"""Replace pronouns and conjugate common verbs for agent description.
(e.g., "You are" -> "I am", "your" -> "my").
"""
pronoun_map = {
# Longer phrases with verb conjugations
'you are': 'I am',
'you were': 'I was',
"you're": 'I am',
"you've": 'I have',
# Standalone pronouns
'yours': 'mine',
'your': 'my',
'you': 'I',
}
# Sort keys by length (descending) to ensure longer phrases are matched first.
# This prevents "you" in "you are" from being replaced on its own.
sorted_keys = sorted(pronoun_map.keys(), key=len, reverse=True)
pattern = r'\b(' + '|'.join(re.escape(key) for key in sorted_keys) + r')\b'
return re.sub(
pattern,
lambda match: pronoun_map[match.group(1).lower()],
text,
flags=re.IGNORECASE,
)
def _get_workflow_description(agent: BaseAgent) -> Optional[str]:
"""Get workflow-specific description for non-LLM agents."""
if not agent.sub_agents:
return None
if isinstance(agent, SequentialAgent):
return _build_sequential_description(agent)
elif isinstance(agent, ParallelAgent):
return _build_parallel_description(agent)
elif isinstance(agent, LoopAgent):
return _build_loop_description(agent)
return None
def _build_sequential_description(agent: SequentialAgent) -> str:
"""Build description for sequential workflow agent."""
descriptions = []
for i, sub_agent in enumerate(agent.sub_agents, 1):
sub_description = (
sub_agent.description or f'execute the {sub_agent.name} agent'
)
if i == 1:
descriptions.append(f'First, this agent will {sub_description}')
elif i == len(agent.sub_agents):
descriptions.append(f'Finally, this agent will {sub_description}')
else:
descriptions.append(f'Then, this agent will {sub_description}')
return ' '.join(descriptions) + '.'
def _build_parallel_description(agent: ParallelAgent) -> str:
"""Build description for parallel workflow agent."""
descriptions = []
for i, sub_agent in enumerate(agent.sub_agents):
sub_description = (
sub_agent.description or f'execute the {sub_agent.name} agent'
)
if i == 0:
descriptions.append(f'This agent will {sub_description}')
elif i == len(agent.sub_agents) - 1:
descriptions.append(f'and {sub_description}')
else:
descriptions.append(f', {sub_description}')
return ' '.join(descriptions) + ' simultaneously.'
def _build_loop_description(agent: LoopAgent) -> str:
"""Build description for loop workflow agent."""
max_iterations = agent.max_iterations or 'unlimited'
descriptions = []
for i, sub_agent in enumerate(agent.sub_agents):
sub_description = (
sub_agent.description or f'execute the {sub_agent.name} agent'
)
if i == 0:
descriptions.append(f'This agent will {sub_description}')
elif i == len(agent.sub_agents) - 1:
descriptions.append(f'and {sub_description}')
else:
descriptions.append(f', {sub_description}')
return (
f"{' '.join(descriptions)} in a loop (max {max_iterations} iterations)."
)
def _get_default_description(agent: BaseAgent) -> str:
"""Get default description based on agent type."""
agent_type_descriptions = {
LlmAgent: 'An LLM-based agent',
SequentialAgent: 'A sequential workflow agent',
ParallelAgent: 'A parallel workflow agent',
LoopAgent: 'A loop workflow agent',
}
for agent_type, description in agent_type_descriptions.items():
if isinstance(agent, agent_type):
return description
return 'A custom agent'
def _extract_inputs_from_examples(examples: Optional[list[dict]]) -> list[str]:
"""Extracts only the input strings so they can be added to an AgentSkill."""
if examples is None:
return []
extracted_inputs = []
for example in examples:
example_input = example.get('input')
if not example_input:
continue
parts = example_input.get('parts')
if parts is not None:
part_texts = []
for part in parts:
text = part.get('text')
if text is not None:
part_texts.append(text)
extracted_inputs.append('\n'.join(part_texts))
else:
text = example_input.get('text')
if text is not None:
extracted_inputs.append(text)
return extracted_inputs
async def _extract_examples_from_agent(
agent: BaseAgent,
) -> Optional[List[Dict]]:
"""Extract examples from example_tool if configured; otherwise, from agent instruction."""
if not isinstance(agent, LlmAgent):
return None
# First, try to find example_tool in tools
try:
canonical_tools = await agent.canonical_tools()
for tool in canonical_tools:
if isinstance(tool, ExampleTool):
return _convert_example_tool_examples(tool)
except Exception as e:
print(f'Warning: Failed to extract examples from tools: {e}')
# If no example_tool found, try to extract examples from instruction
if agent.instruction:
return _extract_examples_from_instruction(agent.instruction)
return None
def _convert_example_tool_examples(tool: ExampleTool) -> List[Dict]:
"""Convert ExampleTool examples to the expected format."""
examples = []
for example in tool.examples:
examples.append({
'input': (
example.input.model_dump()
if hasattr(example.input, 'model_dump')
else example.input
),
'output': [
output.model_dump() if hasattr(output, 'model_dump') else output
for output in example.output
],
})
return examples
def _extract_examples_from_instruction(
instruction: str,
) -> Optional[List[Dict]]:
"""Extract examples from agent instruction text using regex patterns."""
examples = []
# Look for common example patterns in instructions
example_patterns = [
r'Example Query:\s*["\']([^"\']+)["\']',
r'Example Response:\s*["\']([^"\']+)["\']',
r'Example:\s*["\']([^"\']+)["\']',
]
for pattern in example_patterns:
matches = re.findall(pattern, instruction, re.IGNORECASE)
if matches:
for i in range(0, len(matches), 2):
if i + 1 < len(matches):
examples.append({
'input': {'text': matches[i]},
'output': [{'text': matches[i + 1]}],
})
return examples if examples else None
def _get_input_modes(agent: BaseAgent) -> Optional[List[str]]:
"""Get input modes based on agent model."""
if not isinstance(agent, LlmAgent):
return None
# This could be enhanced to check model capabilities
# For now, return None to use default_input_modes
return None
def _get_output_modes(agent: BaseAgent) -> Optional[List[str]]:
"""Get output modes from Agent.generate_content_config.response_modalities."""
if not isinstance(agent, LlmAgent):
return None
if (
hasattr(agent, 'generate_content_config')
and agent.generate_content_config
and hasattr(agent.generate_content_config, 'response_modalities')
):
return agent.generate_content_config.response_modalities
return None