a2a-adapter 0.1.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.
a2a_adapter/loader.py ADDED
@@ -0,0 +1,131 @@
1
+ """
2
+ Adapter factory for loading framework-specific adapters.
3
+
4
+ This module provides the load_a2a_agent function which acts as a factory
5
+ for creating appropriate adapter instances based on configuration.
6
+ """
7
+
8
+ from typing import Any, Dict
9
+
10
+ from .adapter import BaseAgentAdapter
11
+
12
+
13
+ async def load_a2a_agent(config: Dict[str, Any]) -> BaseAgentAdapter:
14
+ """
15
+ Factory function to load an agent adapter based on configuration.
16
+
17
+ This function inspects the 'adapter' key in the config dictionary and
18
+ instantiates the appropriate adapter class with the provided configuration.
19
+
20
+ Args:
21
+ config: Configuration dictionary with at least an 'adapter' key.
22
+ Additional keys depend on the adapter type:
23
+
24
+ - n8n: requires 'webhook_url', optional 'timeout', 'headers',
25
+ 'payload_template', 'message_field'
26
+ - crewai: requires 'crew' (CrewAI Crew instance)
27
+ - langchain: requires 'runnable', optional 'input_key', 'output_key'
28
+ - callable: requires 'callable' (async function)
29
+
30
+ Returns:
31
+ Configured BaseAgentAdapter instance
32
+
33
+ Raises:
34
+ ValueError: If adapter type is unknown or required config is missing
35
+ ImportError: If required framework package is not installed
36
+
37
+ Examples:
38
+ >>> # Load n8n adapter (basic)
39
+ >>> adapter = await load_a2a_agent({
40
+ ... "adapter": "n8n",
41
+ ... "webhook_url": "https://n8n.example.com/webhook/agent",
42
+ ... "timeout": 30
43
+ ... })
44
+
45
+ >>> # Load n8n adapter with custom payload mapping
46
+ >>> adapter = await load_a2a_agent({
47
+ ... "adapter": "n8n",
48
+ ... "webhook_url": "http://localhost:5678/webhook/my-workflow",
49
+ ... "payload_template": {"name": "A2A Agent"}, # Static fields
50
+ ... "message_field": "event" # Use "event" instead of "message"
51
+ ... })
52
+
53
+ >>> # Load CrewAI adapter
54
+ >>> from crewai import Crew, Agent, Task
55
+ >>> crew = Crew(agents=[...], tasks=[...])
56
+ >>> adapter = await load_a2a_agent({
57
+ ... "adapter": "crewai",
58
+ ... "crew": crew
59
+ ... })
60
+
61
+ >>> # Load LangChain adapter
62
+ >>> from langchain_core.runnables import RunnablePassthrough
63
+ >>> adapter = await load_a2a_agent({
64
+ ... "adapter": "langchain",
65
+ ... "runnable": chain,
66
+ ... "input_key": "input"
67
+ ... })
68
+ """
69
+ adapter_type = config.get("adapter")
70
+
71
+ if not adapter_type:
72
+ raise ValueError("Config must include 'adapter' key specifying adapter type")
73
+
74
+ if adapter_type == "n8n":
75
+ from .integrations.n8n import N8nAgentAdapter
76
+
77
+ webhook_url = config.get("webhook_url")
78
+ if not webhook_url:
79
+ raise ValueError("n8n adapter requires 'webhook_url' in config")
80
+
81
+ return N8nAgentAdapter(
82
+ webhook_url=webhook_url,
83
+ timeout=config.get("timeout", 30),
84
+ headers=config.get("headers"),
85
+ payload_template=config.get("payload_template"),
86
+ message_field=config.get("message_field", "message"),
87
+ )
88
+
89
+ elif adapter_type == "crewai":
90
+ from .integrations.crewai import CrewAIAgentAdapter
91
+
92
+ crew = config.get("crew")
93
+ if crew is None:
94
+ raise ValueError("crewai adapter requires 'crew' instance in config")
95
+
96
+ return CrewAIAgentAdapter(
97
+ crew=crew,
98
+ inputs_key=config.get("inputs_key", "inputs"),
99
+ )
100
+
101
+ elif adapter_type == "langchain":
102
+ from .integrations.langchain import LangChainAgentAdapter
103
+
104
+ runnable = config.get("runnable")
105
+ if runnable is None:
106
+ raise ValueError("langchain adapter requires 'runnable' in config")
107
+
108
+ return LangChainAgentAdapter(
109
+ runnable=runnable,
110
+ input_key=config.get("input_key", "input"),
111
+ output_key=config.get("output_key"),
112
+ )
113
+
114
+ elif adapter_type == "callable":
115
+ from .integrations.callable import CallableAgentAdapter
116
+
117
+ func = config.get("callable")
118
+ if func is None:
119
+ raise ValueError("callable adapter requires 'callable' function in config")
120
+
121
+ return CallableAgentAdapter(
122
+ func=func,
123
+ supports_streaming=config.get("supports_streaming", False),
124
+ )
125
+
126
+ else:
127
+ raise ValueError(
128
+ f"Unknown adapter type: {adapter_type}. "
129
+ f"Supported types: n8n, crewai, langchain, callable"
130
+ )
131
+