agentsml-impl 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentsml_impl/__init__.py +3 -0
- agentsml_impl/cli.py +472 -0
- agentsml_impl/compiler/__init__.py +1 -0
- agentsml_impl/compiler/capabilities.py +330 -0
- agentsml_impl/compiler/conditions.py +177 -0
- agentsml_impl/compiler/diagnostics.py +112 -0
- agentsml_impl/compiler/durations.py +31 -0
- agentsml_impl/compiler/expressions.py +123 -0
- agentsml_impl/compiler/identifiers.py +60 -0
- agentsml_impl/compiler/ir.py +391 -0
- agentsml_impl/compiler/limits.py +28 -0
- agentsml_impl/compiler/loader.py +83 -0
- agentsml_impl/compiler/naming.py +87 -0
- agentsml_impl/compiler/planner.py +79 -0
- agentsml_impl/compiler/render.py +90 -0
- agentsml_impl/compiler/schema_validation.py +121 -0
- agentsml_impl/compiler/semantic_validation.py +424 -0
- agentsml_impl/protocols/__init__.py +1 -0
- agentsml_impl/runtime/__init__.py +1 -0
- agentsml_impl/runtime/events.py +59 -0
- agentsml_impl/runtime/json_schema.py +138 -0
- agentsml_impl/runtime/tools.py +145 -0
- agentsml_impl/schemas/agentsml.schema.json +406 -0
- agentsml_impl/schemas/v0.1/agentsml.schema.json +628 -0
- agentsml_impl/schemas/v0.2/agentsml.schema.json +363 -0
- agentsml_impl/schemas/v0.3/agentsml.schema.json +406 -0
- agentsml_impl/targets/__init__.py +1 -0
- agentsml_impl/targets/a2a/__init__.py +0 -0
- agentsml_impl/targets/a2a/adapter.py +94 -0
- agentsml_impl/targets/a2a/capabilities.yaml +25 -0
- agentsml_impl/targets/ag2/__init__.py +0 -0
- agentsml_impl/targets/ag2/adapter.py +655 -0
- agentsml_impl/targets/ag2/capabilities.yaml +37 -0
- agentsml_impl/targets/ag_ui/__init__.py +0 -0
- agentsml_impl/targets/ag_ui/adapter.py +123 -0
- agentsml_impl/targets/ag_ui/capabilities.yaml +25 -0
- agentsml_impl/targets/agno/__init__.py +0 -0
- agentsml_impl/targets/agno/adapter.py +191 -0
- agentsml_impl/targets/agno/capabilities.yaml +26 -0
- agentsml_impl/targets/ap2/__init__.py +0 -0
- agentsml_impl/targets/ap2/adapter.py +84 -0
- agentsml_impl/targets/ap2/capabilities.yaml +25 -0
- agentsml_impl/targets/base.py +157 -0
- agentsml_impl/targets/bedrock_agentcore/__init__.py +0 -0
- agentsml_impl/targets/bedrock_agentcore/adapter.py +592 -0
- agentsml_impl/targets/bedrock_agentcore/capabilities.yaml +26 -0
- agentsml_impl/targets/claude_agent_sdk/__init__.py +0 -0
- agentsml_impl/targets/claude_agent_sdk/adapter.py +178 -0
- agentsml_impl/targets/claude_agent_sdk/capabilities.yaml +26 -0
- agentsml_impl/targets/crewai/__init__.py +0 -0
- agentsml_impl/targets/crewai/adapter.py +441 -0
- agentsml_impl/targets/crewai/capabilities.yaml +30 -0
- agentsml_impl/targets/databricks/__init__.py +0 -0
- agentsml_impl/targets/databricks/adapter.py +427 -0
- agentsml_impl/targets/databricks/capabilities.yaml +26 -0
- agentsml_impl/targets/flowise/__init__.py +0 -0
- agentsml_impl/targets/flowise/adapter.py +71 -0
- agentsml_impl/targets/flowise/capabilities.yaml +26 -0
- agentsml_impl/targets/google_adk/__init__.py +0 -0
- agentsml_impl/targets/google_adk/adapter.py +589 -0
- agentsml_impl/targets/google_adk/capabilities.yaml +36 -0
- agentsml_impl/targets/langchain/__init__.py +1 -0
- agentsml_impl/targets/langchain/adapter.py +351 -0
- agentsml_impl/targets/langchain/capabilities.yaml +30 -0
- agentsml_impl/targets/langflow/__init__.py +0 -0
- agentsml_impl/targets/langflow/adapter.py +147 -0
- agentsml_impl/targets/langflow/capabilities.yaml +26 -0
- agentsml_impl/targets/langgraph/__init__.py +0 -0
- agentsml_impl/targets/langgraph/adapter.py +911 -0
- agentsml_impl/targets/langgraph/capabilities.yaml +33 -0
- agentsml_impl/targets/llamaindex/__init__.py +1 -0
- agentsml_impl/targets/llamaindex/adapter.py +406 -0
- agentsml_impl/targets/llamaindex/capabilities.yaml +31 -0
- agentsml_impl/targets/lyzr/__init__.py +0 -0
- agentsml_impl/targets/lyzr/adapter.py +146 -0
- agentsml_impl/targets/lyzr/capabilities.yaml +26 -0
- agentsml_impl/targets/mastra/__init__.py +0 -0
- agentsml_impl/targets/mastra/adapter.py +635 -0
- agentsml_impl/targets/mastra/capabilities.yaml +32 -0
- agentsml_impl/targets/mcp_server/__init__.py +0 -0
- agentsml_impl/targets/mcp_server/adapter.py +141 -0
- agentsml_impl/targets/mcp_server/capabilities.yaml +25 -0
- agentsml_impl/targets/microsoft_agent_framework/__init__.py +0 -0
- agentsml_impl/targets/microsoft_agent_framework/adapter.py +563 -0
- agentsml_impl/targets/microsoft_agent_framework/capabilities.yaml +26 -0
- agentsml_impl/targets/n8n/__init__.py +0 -0
- agentsml_impl/targets/n8n/adapter.py +139 -0
- agentsml_impl/targets/n8n/capabilities.yaml +26 -0
- agentsml_impl/targets/openai_agents/__init__.py +0 -0
- agentsml_impl/targets/openai_agents/adapter.py +175 -0
- agentsml_impl/targets/openai_agents/capabilities.yaml +27 -0
- agentsml_impl/targets/pydantic_ai/__init__.py +0 -0
- agentsml_impl/targets/pydantic_ai/adapter.py +922 -0
- agentsml_impl/targets/pydantic_ai/capabilities.yaml +37 -0
- agentsml_impl/targets/registry.py +290 -0
- agentsml_impl/targets/relevance_ai/__init__.py +0 -0
- agentsml_impl/targets/relevance_ai/adapter.py +66 -0
- agentsml_impl/targets/relevance_ai/capabilities.yaml +26 -0
- agentsml_impl/targets/smolagents/__init__.py +0 -0
- agentsml_impl/targets/smolagents/adapter.py +210 -0
- agentsml_impl/targets/smolagents/capabilities.yaml +27 -0
- agentsml_impl/targets/snowflake_cortex/__init__.py +0 -0
- agentsml_impl/targets/snowflake_cortex/adapter.py +167 -0
- agentsml_impl/targets/snowflake_cortex/capabilities.yaml +26 -0
- agentsml_impl/targets/spring_ai/__init__.py +1 -0
- agentsml_impl/targets/spring_ai/adapter.py +626 -0
- agentsml_impl/targets/spring_ai/capabilities.yaml +31 -0
- agentsml_impl/targets/strands_agents/__init__.py +1 -0
- agentsml_impl/targets/strands_agents/adapter.py +309 -0
- agentsml_impl/targets/strands_agents/capabilities.yaml +29 -0
- agentsml_impl/targets/support_matrix.py +91 -0
- agentsml_impl/targets/ucp/__init__.py +0 -0
- agentsml_impl/targets/ucp/adapter.py +86 -0
- agentsml_impl/targets/ucp/capabilities.yaml +25 -0
- agentsml_impl/targets/vercel_ai/__init__.py +1 -0
- agentsml_impl/targets/vercel_ai/adapter.py +349 -0
- agentsml_impl/targets/vercel_ai/capabilities.yaml +30 -0
- agentsml_impl/targets/vertex_agent_engine/__init__.py +0 -0
- agentsml_impl/targets/vertex_agent_engine/adapter.py +407 -0
- agentsml_impl/targets/vertex_agent_engine/capabilities.yaml +26 -0
- agentsml_impl/targets/zapier_agents/__init__.py +0 -0
- agentsml_impl/targets/zapier_agents/adapter.py +106 -0
- agentsml_impl/targets/zapier_agents/capabilities.yaml +26 -0
- agentsml_impl-0.2.0.dist-info/METADATA +109 -0
- agentsml_impl-0.2.0.dist-info/RECORD +129 -0
- agentsml_impl-0.2.0.dist-info/WHEEL +5 -0
- agentsml_impl-0.2.0.dist-info/entry_points.txt +3 -0
- agentsml_impl-0.2.0.dist-info/licenses/LICENSE +28 -0
- agentsml_impl-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
"""Google ADK target adapter — Level 1 code generation.
|
|
2
|
+
|
|
3
|
+
Fixes (2026-06-09):
|
|
4
|
+
- Non-Gemini models now use LiteLlm(model=...) wrapper (was plain string — broken).
|
|
5
|
+
- SessionService vs MemoryService distinction mapped from agent.memory.type.
|
|
6
|
+
- ArtifactService wiring when agent has output_schema.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Tuple
|
|
12
|
+
|
|
13
|
+
from agentsml_impl.compiler.identifiers import python_identifier_ir
|
|
14
|
+
from agentsml_impl.compiler.ir import AgentSystemIR, ModelIR, StepIR
|
|
15
|
+
from agentsml_impl.compiler.diagnostics import DiagCode, DiagnosticSet
|
|
16
|
+
from agentsml_impl.compiler.planner import GenerationPlan
|
|
17
|
+
from agentsml_impl.compiler.render import toml_string, write_file
|
|
18
|
+
from agentsml_impl.compiler.naming import pascal_identifier, snake_identifier
|
|
19
|
+
from agentsml_impl.runtime.tools import generate_tools_module
|
|
20
|
+
from agentsml_impl.runtime.json_schema import generate_models_source
|
|
21
|
+
from agentsml_impl.targets.base import TargetAdapter, GenerationResult
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Adapter(TargetAdapter):
|
|
25
|
+
@property
|
|
26
|
+
def name(self) -> str:
|
|
27
|
+
return "google-adk"
|
|
28
|
+
|
|
29
|
+
def analyze(self, ir: AgentSystemIR, diagnostics: DiagnosticSet) -> None:
|
|
30
|
+
from agentsml_impl.compiler.capabilities import analyze_capabilities
|
|
31
|
+
analyze_capabilities(ir, "google_adk", diagnostics)
|
|
32
|
+
for mk, model in ir.models.items():
|
|
33
|
+
if model.provider.lower() not in ("google", "gemini", "google-cloud", "google_cloud"):
|
|
34
|
+
diagnostics.add_info(
|
|
35
|
+
DiagCode.REQUIRES_ADAPTER,
|
|
36
|
+
f"Model '{mk}' uses provider '{model.provider}'. "
|
|
37
|
+
"Generated code will use LiteLlm wrapper for non-Gemini providers. "
|
|
38
|
+
"Ensure 'litellm' is installed and the provider API key is set.",
|
|
39
|
+
path=f"models.{mk}.provider",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def generate(self, ir: AgentSystemIR, plan: GenerationPlan,
|
|
43
|
+
out_dir: Path, diagnostics: DiagnosticSet) -> GenerationResult:
|
|
44
|
+
ir = python_identifier_ir(ir)
|
|
45
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
46
|
+
files: list[str] = []
|
|
47
|
+
files += _write_agent(ir, plan, out_dir, diagnostics)
|
|
48
|
+
files += _write_tools(ir, out_dir, diagnostics)
|
|
49
|
+
files += _write_schemas(ir, out_dir, diagnostics)
|
|
50
|
+
files += _write_runner(ir, plan, out_dir)
|
|
51
|
+
files += _write_init(out_dir)
|
|
52
|
+
files += _write_pyproject(ir, plan, out_dir)
|
|
53
|
+
files += _write_readme(ir, plan, out_dir)
|
|
54
|
+
return GenerationResult(target=self.name, out_dir=out_dir,
|
|
55
|
+
files_written=files, success=True)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ---------------------------------------------------------------------------
|
|
59
|
+
# Model string helpers
|
|
60
|
+
# ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
def _model_expr(model: ModelIR) -> Tuple[str, bool]:
|
|
63
|
+
"""Return (model_expr_str, needs_litellm_import).
|
|
64
|
+
|
|
65
|
+
For Gemini providers, returns a plain string literal usable as the `model=`
|
|
66
|
+
kwarg. For all other providers, returns a LiteLlm(model="…") constructor
|
|
67
|
+
call — the correct ADK pattern for non-Gemini LLMs.
|
|
68
|
+
|
|
69
|
+
LiteLLM provider prefixes:
|
|
70
|
+
openai/ anthropic/ azure/ cohere/ huggingface/ ollama/
|
|
71
|
+
"""
|
|
72
|
+
p = model.provider.lower()
|
|
73
|
+
n = model.name
|
|
74
|
+
if p in ("google", "gemini", "google-cloud", "google_cloud"):
|
|
75
|
+
return f'"{n}"', False
|
|
76
|
+
|
|
77
|
+
# Map AgentsML provider names → LiteLLM prefixes
|
|
78
|
+
litellm_prefix_map = {
|
|
79
|
+
"openai": "openai",
|
|
80
|
+
"anthropic": "anthropic",
|
|
81
|
+
"azure": "azure",
|
|
82
|
+
"azure-openai": "azure",
|
|
83
|
+
"cohere": "cohere",
|
|
84
|
+
"huggingface": "huggingface",
|
|
85
|
+
"mistral": "mistral",
|
|
86
|
+
"ollama": "ollama",
|
|
87
|
+
"groq": "groq",
|
|
88
|
+
"bedrock": "bedrock",
|
|
89
|
+
"vertex": "vertex_ai",
|
|
90
|
+
}
|
|
91
|
+
prefix = litellm_prefix_map.get(p, p)
|
|
92
|
+
litellm_id = f"{prefix}/{n}"
|
|
93
|
+
return f'LiteLlm(model="{litellm_id}")', True
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _write_agent(ir: AgentSystemIR, plan: GenerationPlan,
|
|
97
|
+
out_dir: Path, ds: DiagnosticSet) -> list[str]:
|
|
98
|
+
wf = ir.workflows.get(plan.main_workflow_key)
|
|
99
|
+
steps = wf.steps if wf else []
|
|
100
|
+
|
|
101
|
+
# Collect whether any agent needs LiteLlm
|
|
102
|
+
needs_litellm = False
|
|
103
|
+
is_multi_step = len(steps) > 1
|
|
104
|
+
|
|
105
|
+
agent_blocks: list[str] = []
|
|
106
|
+
|
|
107
|
+
def add_agent_block(var_name: str, runtime_name: str, ak: str) -> None:
|
|
108
|
+
agent = ir.agents[ak]
|
|
109
|
+
model = ir.models.get(agent.model)
|
|
110
|
+
if model:
|
|
111
|
+
mstr, uses_litellm = _model_expr(model)
|
|
112
|
+
if uses_litellm:
|
|
113
|
+
needs_litellm = True
|
|
114
|
+
else:
|
|
115
|
+
mstr = '"gemini-2.0-flash"'
|
|
116
|
+
|
|
117
|
+
tools_list = (", ".join(f'FunctionTool({snake_identifier(t, prefix="tool")})' for t in agent.tools)
|
|
118
|
+
if agent.tools else "")
|
|
119
|
+
inst = agent.instructions.replace('"', '\\"')
|
|
120
|
+
output_schema_line = ""
|
|
121
|
+
if agent.output_schema:
|
|
122
|
+
output_schema_line = f"\n output_schema={pascal_identifier(ak, prefix='Agent')}Output,"
|
|
123
|
+
agent_blocks.append(f"""\
|
|
124
|
+
{var_name} = LlmAgent(
|
|
125
|
+
name="{runtime_name}",
|
|
126
|
+
model={mstr},
|
|
127
|
+
instruction=\"\"\"{inst}\"\"\",
|
|
128
|
+
tools=[{tools_list}],{output_schema_line}
|
|
129
|
+
)""")
|
|
130
|
+
|
|
131
|
+
agent_steps = [step for step in steps if step.agent and step.agent in ir.agents]
|
|
132
|
+
a2a_steps = [step for step in steps if step.a2a_agent]
|
|
133
|
+
used_agents = {step.agent for step in agent_steps}
|
|
134
|
+
for step in agent_steps:
|
|
135
|
+
add_agent_block(_step_agent_var(step), step.id, step.agent)
|
|
136
|
+
for ak in ir.agents.keys() - used_agents:
|
|
137
|
+
add_agent_block(f"{snake_identifier(ak, prefix='agent')}_agent", ak, ak)
|
|
138
|
+
|
|
139
|
+
sub_agents = [_step_agent_var(step) for step in agent_steps]
|
|
140
|
+
sub_agents += [_step_a2a_var(step) for step in a2a_steps]
|
|
141
|
+
staged_sub_agents, parallel_blocks = _dependency_stages(steps, ir, plan.package_name)
|
|
142
|
+
if staged_sub_agents and parallel_blocks:
|
|
143
|
+
root_agent_block = (
|
|
144
|
+
f'root_agent = SequentialAgent(\n'
|
|
145
|
+
f' name="{plan.package_name}",\n'
|
|
146
|
+
f' sub_agents=[{", ".join(staged_sub_agents)}],\n'
|
|
147
|
+
f')'
|
|
148
|
+
)
|
|
149
|
+
elif is_multi_step and sub_agents:
|
|
150
|
+
root_agent_block = (
|
|
151
|
+
f'root_agent = SequentialAgent(\n'
|
|
152
|
+
f' name="{plan.package_name}",\n'
|
|
153
|
+
f' sub_agents=[{", ".join(sub_agents)}],\n'
|
|
154
|
+
f')'
|
|
155
|
+
)
|
|
156
|
+
elif sub_agents:
|
|
157
|
+
root_agent_block = f'root_agent = {sub_agents[0]}'
|
|
158
|
+
else:
|
|
159
|
+
root_agent_block = (
|
|
160
|
+
f'root_agent = LlmAgent(\n'
|
|
161
|
+
f' name="{plan.package_name}",\n'
|
|
162
|
+
f' model="gemini-2.0-flash",\n'
|
|
163
|
+
f' instruction="Agent generated from AgentsML {plan.project_name}.",\n'
|
|
164
|
+
f' tools=[],\n'
|
|
165
|
+
f')'
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
if wf and wf.edges:
|
|
169
|
+
ds.add_warning(DiagCode.LOSSY_MAPPING,
|
|
170
|
+
"Workflow edges are mapped to SequentialAgent — conditional routing "
|
|
171
|
+
"requires a custom routing callback (before_agent_callback).",
|
|
172
|
+
path=f"workflows.{plan.main_workflow_key}.edges",
|
|
173
|
+
recommendation="Implement a before_agent_callback for conditional logic.")
|
|
174
|
+
|
|
175
|
+
has_approvals = any(s.approval for s in steps)
|
|
176
|
+
if has_approvals:
|
|
177
|
+
ds.add_warning(DiagCode.REQUIRES_ADAPTER,
|
|
178
|
+
"Human approval steps require a custom before_tool_callback in Google ADK. "
|
|
179
|
+
"Vertex AI human-in-the-loop integration recommended.",
|
|
180
|
+
path=f"workflows.{plan.main_workflow_key}",
|
|
181
|
+
recommendation="Implement a before_tool_callback for approval gates.")
|
|
182
|
+
|
|
183
|
+
for step in steps:
|
|
184
|
+
if step.a2a_agent:
|
|
185
|
+
ds.add_info(
|
|
186
|
+
DiagCode.REQUIRES_ADAPTER,
|
|
187
|
+
f"A2A step '{step.id}' is generated as a RemoteA2aAgent that calls the remote agent card.",
|
|
188
|
+
path=f"workflows.{plan.main_workflow_key}.steps.{step.id}.a2a_agent",
|
|
189
|
+
recommendation="Ensure the remote agent card is reachable at runtime.",
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
a2a_blocks, a2a_imports = _a2a_step_blocks(steps, ir, ds, plan.main_workflow_key)
|
|
193
|
+
|
|
194
|
+
has_mcp = any(t.is_mcp() for t in ir.tools.values())
|
|
195
|
+
mcp_import = "from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters" if has_mcp else ""
|
|
196
|
+
litellm_import = "from google.adk.models.lite_llm import LiteLlm" if needs_litellm else ""
|
|
197
|
+
a2a_import_block = "\n".join(a2a_imports)
|
|
198
|
+
secret_helper = _SECRET_HELPER if "_require_secret(" in a2a_import_block + "".join(a2a_blocks) else ""
|
|
199
|
+
os_import = "import os" if secret_helper else ""
|
|
200
|
+
|
|
201
|
+
tools_import = (
|
|
202
|
+
f"from .tools import {', '.join(snake_identifier(tool, prefix='tool') for tool in ir.tools)}"
|
|
203
|
+
if ir.tools else ""
|
|
204
|
+
)
|
|
205
|
+
schemas_import = ("from .schemas import " +
|
|
206
|
+
", ".join(
|
|
207
|
+
f"{pascal_identifier(k, prefix='Agent')}Output"
|
|
208
|
+
for k, v in ir.agents.items() if v.output_schema
|
|
209
|
+
)
|
|
210
|
+
if any(v.output_schema for v in ir.agents.values()) else "")
|
|
211
|
+
|
|
212
|
+
agent_block_str = "\n\n".join(agent_blocks)
|
|
213
|
+
a2a_block_str = "\n\n".join(a2a_blocks)
|
|
214
|
+
|
|
215
|
+
c = f'''\
|
|
216
|
+
"""Google ADK agent definitions for {plan.project_name}.
|
|
217
|
+
|
|
218
|
+
Auto-generated by agentsml-impl. Re-run to regenerate.
|
|
219
|
+
"""
|
|
220
|
+
from __future__ import annotations
|
|
221
|
+
{os_import}
|
|
222
|
+
from google.adk.agents import LlmAgent, SequentialAgent, ParallelAgent
|
|
223
|
+
from google.adk.tools import FunctionTool
|
|
224
|
+
{litellm_import}
|
|
225
|
+
{mcp_import}
|
|
226
|
+
{a2a_import_block}
|
|
227
|
+
{tools_import}
|
|
228
|
+
{schemas_import}
|
|
229
|
+
{secret_helper}
|
|
230
|
+
# ── Agent definitions ─────────────────────────────────────────────────────────
|
|
231
|
+
{agent_block_str}
|
|
232
|
+
|
|
233
|
+
# ── Remote A2A agents ─────────────────────────────────────────────────────────
|
|
234
|
+
{a2a_block_str or "# No remote A2A agents"}
|
|
235
|
+
|
|
236
|
+
# ── Dependency stages ─────────────────────────────────────────────────────────
|
|
237
|
+
{chr(10).join(parallel_blocks) or "# No parallel dependency stages"}
|
|
238
|
+
|
|
239
|
+
# ── Root agent (entry point) ──────────────────────────────────────────────────
|
|
240
|
+
{root_agent_block}
|
|
241
|
+
'''
|
|
242
|
+
write_file(out_dir, "agent.py", c)
|
|
243
|
+
return ["agent.py"]
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _dependency_stages(
|
|
247
|
+
steps: list[StepIR],
|
|
248
|
+
ir: AgentSystemIR,
|
|
249
|
+
package_name: str,
|
|
250
|
+
) -> tuple[list[str], list[str]]:
|
|
251
|
+
"""Map explicit depends_on layers to sequential and parallel ADK stages.
|
|
252
|
+
|
|
253
|
+
Step order remains sequential when no dependencies are declared. Only
|
|
254
|
+
sibling agent steps at the same dependency depth become a ParallelAgent.
|
|
255
|
+
"""
|
|
256
|
+
if not any(step.depends_on for step in steps):
|
|
257
|
+
return [], []
|
|
258
|
+
|
|
259
|
+
by_id = {step.id: step for step in steps}
|
|
260
|
+
depths: dict[str, int] = {}
|
|
261
|
+
visiting: set[str] = set()
|
|
262
|
+
|
|
263
|
+
def depth(step_id: str) -> int:
|
|
264
|
+
if step_id in depths:
|
|
265
|
+
return depths[step_id]
|
|
266
|
+
if step_id in visiting:
|
|
267
|
+
return 0
|
|
268
|
+
visiting.add(step_id)
|
|
269
|
+
step = by_id[step_id]
|
|
270
|
+
dependencies = [dep for dep in step.depends_on if dep in by_id]
|
|
271
|
+
value = 0 if not dependencies else max(depth(dep) for dep in dependencies) + 1
|
|
272
|
+
visiting.remove(step_id)
|
|
273
|
+
depths[step_id] = value
|
|
274
|
+
return value
|
|
275
|
+
|
|
276
|
+
agent_stages: dict[int, list[str]] = {}
|
|
277
|
+
for step in steps:
|
|
278
|
+
if step.agent and step.agent in ir.agents:
|
|
279
|
+
agent_stages.setdefault(depth(step.id), []).append(_step_agent_var(step))
|
|
280
|
+
elif step.a2a_agent:
|
|
281
|
+
agent_stages.setdefault(depth(step.id), []).append(_step_a2a_var(step))
|
|
282
|
+
|
|
283
|
+
staged_agents: list[str] = []
|
|
284
|
+
parallel_blocks: list[str] = []
|
|
285
|
+
for stage_number, agents in sorted(agent_stages.items()):
|
|
286
|
+
if len(agents) == 1:
|
|
287
|
+
staged_agents.append(agents[0])
|
|
288
|
+
continue
|
|
289
|
+
stage_name = f"{package_name}_stage_{stage_number}"
|
|
290
|
+
parallel_blocks.append(
|
|
291
|
+
f'{stage_name} = ParallelAgent(\n'
|
|
292
|
+
f' name="{stage_name}",\n'
|
|
293
|
+
f' sub_agents=[{", ".join(agents)}],\n'
|
|
294
|
+
f')'
|
|
295
|
+
)
|
|
296
|
+
staged_agents.append(stage_name)
|
|
297
|
+
|
|
298
|
+
return staged_agents, parallel_blocks
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _step_agent_var(step: StepIR) -> str:
|
|
302
|
+
return f"{snake_identifier(step.id, prefix='step')}_agent"
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _step_a2a_var(step: StepIR) -> str:
|
|
306
|
+
return f"{snake_identifier(step.id, prefix='step')}_a2a"
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _a2a_step_blocks(
|
|
310
|
+
steps: list[StepIR],
|
|
311
|
+
ir: AgentSystemIR,
|
|
312
|
+
ds: DiagnosticSet,
|
|
313
|
+
workflow_key: str,
|
|
314
|
+
) -> tuple[list[str], list[str]]:
|
|
315
|
+
"""Generate a RemoteA2aAgent instance for every step that calls a remote agent.
|
|
316
|
+
|
|
317
|
+
Returns the agent blocks and the import lines they need.
|
|
318
|
+
"""
|
|
319
|
+
blocks: list[str] = []
|
|
320
|
+
imports: list[str] = []
|
|
321
|
+
if not any(step.a2a_agent for step in steps):
|
|
322
|
+
return blocks, imports
|
|
323
|
+
|
|
324
|
+
imports.append("from google.adk.agents.remote_a2a_agent import RemoteA2aAgent")
|
|
325
|
+
needs_secret_helper = False
|
|
326
|
+
|
|
327
|
+
for step in steps:
|
|
328
|
+
if not step.a2a_agent:
|
|
329
|
+
continue
|
|
330
|
+
spec = ir.a2a_agents.get(step.a2a_agent)
|
|
331
|
+
if spec is None:
|
|
332
|
+
ds.add_error(
|
|
333
|
+
DiagCode.AMBIGUOUS_REFERENCE,
|
|
334
|
+
f"A2A step '{step.id}' references undeclared A2A agent '{step.a2a_agent}'.",
|
|
335
|
+
path=f"workflows.{workflow_key}.steps.{step.id}.a2a_agent",
|
|
336
|
+
recommendation=f"Declare '{step.a2a_agent}' in the a2a_agents block.",
|
|
337
|
+
)
|
|
338
|
+
continue
|
|
339
|
+
card = spec.get("agent_card_url") or spec.get("agent_card")
|
|
340
|
+
if not card:
|
|
341
|
+
ds.add_warning(
|
|
342
|
+
DiagCode.MISSING_SCHEMA,
|
|
343
|
+
f"A2A agent '{step.a2a_agent}' declares no agent_card_url, so the remote agent cannot be generated.",
|
|
344
|
+
path=f"a2a_agents.{step.a2a_agent}",
|
|
345
|
+
recommendation="Add agent_card_url pointing at the remote agent card.",
|
|
346
|
+
)
|
|
347
|
+
continue
|
|
348
|
+
|
|
349
|
+
description = spec.get("description") or ""
|
|
350
|
+
auth = spec.get("auth") or {}
|
|
351
|
+
auth_lines = ""
|
|
352
|
+
if auth.get("type") == "bearer" and auth.get("secret_ref"):
|
|
353
|
+
needs_secret_helper = True
|
|
354
|
+
auth_lines = (
|
|
355
|
+
" auth_scheme=HTTPBearer(),\n"
|
|
356
|
+
" auth_credential=AuthCredential(\n"
|
|
357
|
+
" auth_type=AuthCredentialTypes.HTTP,\n"
|
|
358
|
+
" http=HttpAuth(\n"
|
|
359
|
+
" scheme=\"bearer\",\n"
|
|
360
|
+
f" credentials=HttpCredentials(token=_require_secret({str(auth['secret_ref'])!r})),\n"
|
|
361
|
+
" ),\n"
|
|
362
|
+
" ),\n"
|
|
363
|
+
)
|
|
364
|
+
elif auth:
|
|
365
|
+
ds.add_warning(
|
|
366
|
+
DiagCode.REQUIRES_ADAPTER,
|
|
367
|
+
f"A2A agent '{step.a2a_agent}' declares auth type '{auth.get('type')}', "
|
|
368
|
+
"which the ADK adapter does not generate.",
|
|
369
|
+
path=f"a2a_agents.{step.a2a_agent}.auth",
|
|
370
|
+
recommendation="Wire the credential for this remote agent manually.",
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
blocks.append(f"""\
|
|
374
|
+
{_step_a2a_var(step)} = RemoteA2aAgent(
|
|
375
|
+
name="{step.id}",
|
|
376
|
+
description={description!r},
|
|
377
|
+
agent_card={card!r},
|
|
378
|
+
{auth_lines})""")
|
|
379
|
+
|
|
380
|
+
if needs_secret_helper:
|
|
381
|
+
imports.append("from google.adk.auth import AuthCredential, AuthCredentialTypes")
|
|
382
|
+
imports.append("from google.adk.auth.auth_credential import HttpAuth, HttpCredentials")
|
|
383
|
+
imports.append("from fastapi.openapi.models import HTTPBearer")
|
|
384
|
+
return blocks, imports
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
_SECRET_HELPER = '''
|
|
388
|
+
|
|
389
|
+
def _require_secret(name: str) -> str:
|
|
390
|
+
"""Read a required secret from the environment.
|
|
391
|
+
|
|
392
|
+
The credential is resolved when the module is imported, so a missing secret
|
|
393
|
+
fails immediately with a clear message instead of an opaque auth failure.
|
|
394
|
+
"""
|
|
395
|
+
value = os.environ.get(name)
|
|
396
|
+
if not value:
|
|
397
|
+
raise RuntimeError(f"Missing required secret environment variable {name}")
|
|
398
|
+
return value
|
|
399
|
+
'''
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _write_tools(ir: AgentSystemIR, out_dir: Path, ds: DiagnosticSet) -> list[str]:
|
|
403
|
+
if not ir.tools:
|
|
404
|
+
return []
|
|
405
|
+
c = generate_tools_module(ir.tools, ds, framework="google-adk")
|
|
406
|
+
write_file(out_dir, "tools.py", c)
|
|
407
|
+
return ["tools.py"]
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _write_init(out_dir: Path) -> list[str]:
|
|
411
|
+
write_file(out_dir, "__init__.py", "from . import agent # noqa: F401\n")
|
|
412
|
+
return ["__init__.py"]
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def _write_schemas(ir: AgentSystemIR, out_dir: Path, ds: DiagnosticSet) -> list[str]:
|
|
416
|
+
schemas = {f"{k}_output": v.output_schema for k, v in ir.agents.items() if v.output_schema}
|
|
417
|
+
if not schemas:
|
|
418
|
+
return []
|
|
419
|
+
c = generate_models_source(schemas, ds)
|
|
420
|
+
write_file(out_dir, "schemas.py", c)
|
|
421
|
+
return ["schemas.py"]
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _write_runner(ir: AgentSystemIR, plan: GenerationPlan, out_dir: Path) -> list[str]:
|
|
425
|
+
# Determine memory/session service imports based on agent memory config
|
|
426
|
+
has_long_term = any(
|
|
427
|
+
a.memory and a.memory.get("type") == "long_term"
|
|
428
|
+
for a in ir.agents.values()
|
|
429
|
+
)
|
|
430
|
+
has_output_schema = any(a.output_schema for a in ir.agents.values())
|
|
431
|
+
|
|
432
|
+
memory_import = ""
|
|
433
|
+
memory_service_init = ""
|
|
434
|
+
if has_long_term:
|
|
435
|
+
memory_import = "from google.adk.memory import InMemoryMemoryService"
|
|
436
|
+
memory_service_init = "\n memory_service = InMemoryMemoryService()"
|
|
437
|
+
artifact_import = ""
|
|
438
|
+
artifact_service_init = ""
|
|
439
|
+
if has_output_schema:
|
|
440
|
+
artifact_import = "from google.adk.artifacts import InMemoryArtifactService"
|
|
441
|
+
artifact_service_init = "\n artifact_service = InMemoryArtifactService()"
|
|
442
|
+
artifact_kwarg = "\n artifact_service=artifact_service," if has_output_schema else ""
|
|
443
|
+
memory_kwarg = "\n memory_service=memory_service," if has_long_term else ""
|
|
444
|
+
|
|
445
|
+
a2a_server_block = ""
|
|
446
|
+
a2a_server_import = ""
|
|
447
|
+
if ir.a2a_server:
|
|
448
|
+
server = ir.a2a_server or {}
|
|
449
|
+
port = server.get("port") or 8001
|
|
450
|
+
host = server.get("host") or "0.0.0.0"
|
|
451
|
+
a2a_server_import = "from google.adk.a2a.utils.agent_to_a2a import to_a2a"
|
|
452
|
+
a2a_server_block = f'''
|
|
453
|
+
|
|
454
|
+
# ── A2A service ──────────────────────────────────────────────────────────────
|
|
455
|
+
# Exposes the root agent as an A2A service. Serve it with:
|
|
456
|
+
# uvicorn {plan.package_name}.runner:a2a_app --host {host} --port {port}
|
|
457
|
+
A2A_PORT = {port}
|
|
458
|
+
a2a_app = to_a2a(root_agent, host="{host}", port={port})
|
|
459
|
+
'''
|
|
460
|
+
|
|
461
|
+
c = f'''\
|
|
462
|
+
"""Runner for {plan.project_name} (Google ADK). Auto-generated."""
|
|
463
|
+
from __future__ import annotations
|
|
464
|
+
import asyncio
|
|
465
|
+
from google.adk.runners import Runner
|
|
466
|
+
from google.adk.sessions import InMemorySessionService
|
|
467
|
+
from google.genai import types
|
|
468
|
+
{memory_import}
|
|
469
|
+
{artifact_import}
|
|
470
|
+
{a2a_server_import}
|
|
471
|
+
from .agent import root_agent
|
|
472
|
+
|
|
473
|
+
APP_NAME = "{plan.package_name}"
|
|
474
|
+
USER_ID = "local_user"
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
async def run_async(question: str) -> str:
|
|
478
|
+
session_service = InMemorySessionService(){memory_service_init}{artifact_service_init}
|
|
479
|
+
session = await session_service.create_session(
|
|
480
|
+
app_name=APP_NAME, user_id=USER_ID
|
|
481
|
+
)
|
|
482
|
+
runner = Runner(agent=root_agent, app_name=APP_NAME,
|
|
483
|
+
session_service=session_service,{artifact_kwarg}{memory_kwarg})
|
|
484
|
+
content = types.Content(role="user", parts=[types.Part(text=question)])
|
|
485
|
+
final = ""
|
|
486
|
+
async for event in runner.run_async(
|
|
487
|
+
user_id=USER_ID, session_id=session.id, new_message=content
|
|
488
|
+
):
|
|
489
|
+
if event.is_final_response() and event.content:
|
|
490
|
+
for part in event.content.parts:
|
|
491
|
+
if part.text:
|
|
492
|
+
final += part.text
|
|
493
|
+
return final
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def main() -> None:
|
|
497
|
+
import sys
|
|
498
|
+
q = " ".join(sys.argv[1:]) or "Hello!"
|
|
499
|
+
print(asyncio.run(run_async(q)))
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
if __name__ == "__main__":
|
|
503
|
+
main()
|
|
504
|
+
{a2a_server_block}'''
|
|
505
|
+
write_file(out_dir, "runner.py", c)
|
|
506
|
+
return ["runner.py"]
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def _write_pyproject(ir: AgentSystemIR, plan: GenerationPlan, out_dir: Path) -> list[str]:
|
|
510
|
+
needs_litellm = any(
|
|
511
|
+
m.provider.lower() not in ("google", "gemini", "google-cloud", "google_cloud")
|
|
512
|
+
for m in ir.models.values()
|
|
513
|
+
)
|
|
514
|
+
litellm_dep = '\n "litellm",' if needs_litellm else ""
|
|
515
|
+
needs_a2a = bool(ir.a2a_agents or ir.a2a_server)
|
|
516
|
+
adk_dep = '"google-adk[a2a]>=2.14.0",' if needs_a2a else '"google-adk>=2.14.0",'
|
|
517
|
+
c = f'''\
|
|
518
|
+
[build-system]
|
|
519
|
+
requires = ["setuptools>=68"]
|
|
520
|
+
build-backend = "setuptools.build_meta"
|
|
521
|
+
|
|
522
|
+
[project]
|
|
523
|
+
name = "{plan.package_name}"
|
|
524
|
+
version = "{ir.metadata.version or '0.1.0'}"
|
|
525
|
+
description = {toml_string(ir.metadata.description or plan.project_name)}
|
|
526
|
+
requires-python = ">=3.11"
|
|
527
|
+
dependencies = [
|
|
528
|
+
{adk_dep}
|
|
529
|
+
"google-genai",
|
|
530
|
+
"pydantic>=2.7",
|
|
531
|
+
"python-dotenv",{litellm_dep}
|
|
532
|
+
]
|
|
533
|
+
|
|
534
|
+
[tool.setuptools.packages.find]
|
|
535
|
+
where = ["."]
|
|
536
|
+
'''
|
|
537
|
+
write_file(out_dir, "pyproject.toml", c)
|
|
538
|
+
return ["pyproject.toml"]
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def _write_readme(ir: AgentSystemIR, plan: GenerationPlan, out_dir: Path) -> list[str]:
|
|
542
|
+
agents_md = "\n".join(f"- `{k}`: {v.description or ''}" for k, v in ir.agents.items())
|
|
543
|
+
tools_md = "\n".join(f"- `{k}`: {v.description[:70]}" for k, v in ir.tools.items())
|
|
544
|
+
|
|
545
|
+
# Check if LiteLlm is used
|
|
546
|
+
non_gemini = [
|
|
547
|
+
f"{k} ({m.provider}/{m.name})"
|
|
548
|
+
for k, m in ir.models.items()
|
|
549
|
+
if m.provider.lower() not in ("google", "gemini", "google-cloud", "google_cloud")
|
|
550
|
+
]
|
|
551
|
+
litellm_note = ""
|
|
552
|
+
if non_gemini:
|
|
553
|
+
litellm_note = f"""
|
|
554
|
+
## Non-Gemini Models (LiteLlm)
|
|
555
|
+
|
|
556
|
+
The following models use the `LiteLlm` wrapper — set the corresponding API keys:
|
|
557
|
+
{chr(10).join(f'- {m}' for m in non_gemini)}
|
|
558
|
+
"""
|
|
559
|
+
|
|
560
|
+
c = f"""\
|
|
561
|
+
# {plan.project_name} — Google ADK
|
|
562
|
+
|
|
563
|
+
> Auto-generated by `agentsml-impl`.
|
|
564
|
+
|
|
565
|
+
## Agents
|
|
566
|
+
{agents_md or "None"}
|
|
567
|
+
|
|
568
|
+
## Tools
|
|
569
|
+
{tools_md or "None"}
|
|
570
|
+
{litellm_note}
|
|
571
|
+
## Quickstart
|
|
572
|
+
```bash
|
|
573
|
+
pip install -e .
|
|
574
|
+
# For Gemini models: set GOOGLE_API_KEY or use Application Default Credentials
|
|
575
|
+
# For non-Gemini models: set the appropriate provider API key (OPENAI_API_KEY, etc.)
|
|
576
|
+
python runner.py "Your question here"
|
|
577
|
+
# Or use the ADK dev server:
|
|
578
|
+
adk web
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
## Smoke test
|
|
582
|
+
```bash
|
|
583
|
+
python -c "from agent import root_agent; print(root_agent.name)"
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
> See `diagnostics.yaml` for portability warnings.
|
|
587
|
+
"""
|
|
588
|
+
write_file(out_dir, "README.md", c)
|
|
589
|
+
return ["README.md"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
target: google_adk
|
|
2
|
+
kind: code
|
|
3
|
+
language: python
|
|
4
|
+
status: implemented
|
|
5
|
+
level: 1
|
|
6
|
+
version_tested: "2.14.0"
|
|
7
|
+
supports:
|
|
8
|
+
agents: native
|
|
9
|
+
tools: native
|
|
10
|
+
workflows: native
|
|
11
|
+
graph_edges: lossy
|
|
12
|
+
graph_conditions: manual
|
|
13
|
+
cycles: lossy
|
|
14
|
+
parallel: native
|
|
15
|
+
human_approval: manual
|
|
16
|
+
mcp: manual
|
|
17
|
+
a2a: native
|
|
18
|
+
ag_ui: adapter
|
|
19
|
+
structured_output: adapter
|
|
20
|
+
observability: native
|
|
21
|
+
artifacts:
|
|
22
|
+
- python_project
|
|
23
|
+
- diagnostics
|
|
24
|
+
- readme
|
|
25
|
+
notes: >
|
|
26
|
+
Google ADK maps workflow steps to SequentialAgent/ParallelAgent/LlmAgent.
|
|
27
|
+
Explicit depends_on layers generate sequential stages with sibling agent
|
|
28
|
+
steps grouped into ParallelAgent stages; workflows without dependencies
|
|
29
|
+
remain sequential by declared step order.
|
|
30
|
+
Steps that call a remote agent are generated as RemoteA2aAgent instances
|
|
31
|
+
that resolve the declared agent card at runtime, and an a2a_server block
|
|
32
|
+
generates a to_a2a() service entrypoint.
|
|
33
|
+
MCP and A2A require manual wrapper wiring in generated projects. Complex graph
|
|
34
|
+
DAG edges emit lossy_mapping; use LoopAgent for iteration patterns.
|
|
35
|
+
Human approval requires custom callback or external gate. Vertex AI
|
|
36
|
+
deployment supported via Agent Engine.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Target: langchain."""
|