xpander-sdk 1.60.8__py3-none-any.whl → 2.0.155__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.
- xpander_sdk/__init__.py +76 -7793
- xpander_sdk/consts/__init__.py +0 -0
- xpander_sdk/consts/api_routes.py +63 -0
- xpander_sdk/core/__init__.py +0 -0
- xpander_sdk/core/module_base.py +164 -0
- xpander_sdk/core/state.py +10 -0
- xpander_sdk/core/xpander_api_client.py +119 -0
- xpander_sdk/exceptions/__init__.py +0 -0
- xpander_sdk/exceptions/module_exception.py +45 -0
- xpander_sdk/models/__init__.py +0 -0
- xpander_sdk/models/activity.py +65 -0
- xpander_sdk/models/configuration.py +92 -0
- xpander_sdk/models/events.py +70 -0
- xpander_sdk/models/frameworks.py +64 -0
- xpander_sdk/models/shared.py +102 -0
- xpander_sdk/models/user.py +21 -0
- xpander_sdk/modules/__init__.py +0 -0
- xpander_sdk/modules/agents/__init__.py +0 -0
- xpander_sdk/modules/agents/agents_module.py +164 -0
- xpander_sdk/modules/agents/models/__init__.py +0 -0
- xpander_sdk/modules/agents/models/agent.py +477 -0
- xpander_sdk/modules/agents/models/agent_list.py +107 -0
- xpander_sdk/modules/agents/models/knowledge_bases.py +33 -0
- xpander_sdk/modules/agents/sub_modules/__init__.py +0 -0
- xpander_sdk/modules/agents/sub_modules/agent.py +953 -0
- xpander_sdk/modules/agents/utils/__init__.py +0 -0
- xpander_sdk/modules/agents/utils/generic.py +2 -0
- xpander_sdk/modules/backend/__init__.py +0 -0
- xpander_sdk/modules/backend/backend_module.py +425 -0
- xpander_sdk/modules/backend/frameworks/__init__.py +0 -0
- xpander_sdk/modules/backend/frameworks/agno.py +627 -0
- xpander_sdk/modules/backend/frameworks/dispatch.py +36 -0
- xpander_sdk/modules/backend/utils/__init__.py +0 -0
- xpander_sdk/modules/backend/utils/mcp_oauth.py +95 -0
- xpander_sdk/modules/events/__init__.py +0 -0
- xpander_sdk/modules/events/decorators/__init__.py +0 -0
- xpander_sdk/modules/events/decorators/on_boot.py +94 -0
- xpander_sdk/modules/events/decorators/on_shutdown.py +94 -0
- xpander_sdk/modules/events/decorators/on_task.py +203 -0
- xpander_sdk/modules/events/events_module.py +629 -0
- xpander_sdk/modules/events/models/__init__.py +0 -0
- xpander_sdk/modules/events/models/deployments.py +25 -0
- xpander_sdk/modules/events/models/events.py +57 -0
- xpander_sdk/modules/events/utils/__init__.py +0 -0
- xpander_sdk/modules/events/utils/generic.py +56 -0
- xpander_sdk/modules/events/utils/git_init.py +32 -0
- xpander_sdk/modules/knowledge_bases/__init__.py +0 -0
- xpander_sdk/modules/knowledge_bases/knowledge_bases_module.py +217 -0
- xpander_sdk/modules/knowledge_bases/models/__init__.py +0 -0
- xpander_sdk/modules/knowledge_bases/models/knowledge_bases.py +11 -0
- xpander_sdk/modules/knowledge_bases/sub_modules/__init__.py +0 -0
- xpander_sdk/modules/knowledge_bases/sub_modules/knowledge_base.py +107 -0
- xpander_sdk/modules/knowledge_bases/sub_modules/knowledge_base_document_item.py +40 -0
- xpander_sdk/modules/knowledge_bases/utils/__init__.py +0 -0
- xpander_sdk/modules/tasks/__init__.py +0 -0
- xpander_sdk/modules/tasks/models/__init__.py +0 -0
- xpander_sdk/modules/tasks/models/task.py +153 -0
- xpander_sdk/modules/tasks/models/tasks_list.py +107 -0
- xpander_sdk/modules/tasks/sub_modules/__init__.py +0 -0
- xpander_sdk/modules/tasks/sub_modules/task.py +887 -0
- xpander_sdk/modules/tasks/tasks_module.py +492 -0
- xpander_sdk/modules/tasks/utils/__init__.py +0 -0
- xpander_sdk/modules/tasks/utils/files.py +114 -0
- xpander_sdk/modules/tools_repository/__init__.py +0 -0
- xpander_sdk/modules/tools_repository/decorators/__init__.py +0 -0
- xpander_sdk/modules/tools_repository/decorators/register_tool.py +108 -0
- xpander_sdk/modules/tools_repository/models/__init__.py +0 -0
- xpander_sdk/modules/tools_repository/models/mcp.py +68 -0
- xpander_sdk/modules/tools_repository/models/tool_invocation_result.py +14 -0
- xpander_sdk/modules/tools_repository/sub_modules/__init__.py +0 -0
- xpander_sdk/modules/tools_repository/sub_modules/tool.py +578 -0
- xpander_sdk/modules/tools_repository/tools_repository_module.py +259 -0
- xpander_sdk/modules/tools_repository/utils/__init__.py +0 -0
- xpander_sdk/modules/tools_repository/utils/generic.py +57 -0
- xpander_sdk/modules/tools_repository/utils/local_tools.py +52 -0
- xpander_sdk/modules/tools_repository/utils/schemas.py +308 -0
- xpander_sdk/utils/__init__.py +0 -0
- xpander_sdk/utils/env.py +44 -0
- xpander_sdk/utils/event_loop.py +67 -0
- xpander_sdk/utils/tools.py +32 -0
- xpander_sdk-2.0.155.dist-info/METADATA +538 -0
- xpander_sdk-2.0.155.dist-info/RECORD +85 -0
- {xpander_sdk-1.60.8.dist-info → xpander_sdk-2.0.155.dist-info}/WHEEL +1 -1
- {xpander_sdk-1.60.8.dist-info → xpander_sdk-2.0.155.dist-info/licenses}/LICENSE +0 -1
- xpander_sdk/_jsii/__init__.py +0 -39
- xpander_sdk/_jsii/xpander-sdk@1.60.8.jsii.tgz +0 -0
- xpander_sdk/py.typed +0 -1
- xpander_sdk-1.60.8.dist-info/METADATA +0 -368
- xpander_sdk-1.60.8.dist-info/RECORD +0 -9
- {xpander_sdk-1.60.8.dist-info → xpander_sdk-2.0.155.dist-info}/top_level.txt +0 -0
xpander_sdk/utils/env.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Environment utilities for the xpander.ai SDK.
|
|
3
|
+
|
|
4
|
+
This module provides utilities for reading environment variables and
|
|
5
|
+
configuring SDK behavior based on environment settings.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from os import getenv
|
|
9
|
+
|
|
10
|
+
def get_base_url() -> str:
|
|
11
|
+
"""
|
|
12
|
+
Get the base URL for xpander.ai API endpoints.
|
|
13
|
+
|
|
14
|
+
This function determines the appropriate base URL for API requests based on
|
|
15
|
+
environment variables. It supports both production and staging environments,
|
|
16
|
+
with the ability to override via environment variables.
|
|
17
|
+
|
|
18
|
+
Environment Variables:
|
|
19
|
+
XPANDER_BASE_URL: Override URL for API endpoints (optional).
|
|
20
|
+
IS_STG: Set to "true" for staging environment (optional, defaults to "false").
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
str: The base URL for xpander.ai API endpoints, always prefixed with "https://".
|
|
24
|
+
|
|
25
|
+
Example:
|
|
26
|
+
>>> url = get_base_url()
|
|
27
|
+
>>> print(url) # "https://inbound.xpander.ai"
|
|
28
|
+
|
|
29
|
+
>>> import os
|
|
30
|
+
>>> os.environ["XPANDER_BASE_URL"] = "custom.api.endpoint"
|
|
31
|
+
>>> url = get_base_url()
|
|
32
|
+
>>> print(url) # "https://custom.api.endpoint"
|
|
33
|
+
"""
|
|
34
|
+
# Support override by environment variable
|
|
35
|
+
base_url = getenv("XPANDER_BASE_URL")
|
|
36
|
+
|
|
37
|
+
if not base_url:
|
|
38
|
+
is_stg = getenv("IS_STG", "false") == "true"
|
|
39
|
+
base_url = "inbound.stg.xpander.ai" if is_stg else "inbound.xpander.ai"
|
|
40
|
+
|
|
41
|
+
if not base_url.startswith("http://") and not base_url.startswith("https://"):
|
|
42
|
+
base_url = f"https://{base_url}"
|
|
43
|
+
|
|
44
|
+
return base_url
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utilities for managing event loop execution in the xpander.ai SDK.
|
|
3
|
+
|
|
4
|
+
This module provides utilities for handling asyncio event loops, enabling
|
|
5
|
+
synchronous execution of coroutines in environments that may not natively
|
|
6
|
+
support asynchronous operations.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
from typing import Any, Awaitable
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def run_sync(coro: Awaitable[Any]) -> Any:
|
|
14
|
+
"""
|
|
15
|
+
Synchronously run an asynchronous coroutine, ensuring compatibility
|
|
16
|
+
with nested event loops in environments like Jupyter Notebooks or
|
|
17
|
+
web frameworks such as FastAPI.
|
|
18
|
+
|
|
19
|
+
This function detects and manages running event loops to prevent
|
|
20
|
+
runtime errors. For uvloop compatibility, it uses asyncio.create_task
|
|
21
|
+
and waits for completion instead of nest_asyncio when uvloop is detected.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
coro (Awaitable[Any]): The coroutine to be executed.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Any: The result of the coroutine execution.
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
>>> async def fetch_data():
|
|
31
|
+
... # simulate async operation
|
|
32
|
+
... await asyncio.sleep(1)
|
|
33
|
+
... return "data"
|
|
34
|
+
|
|
35
|
+
>>> result = run_sync(fetch_data())
|
|
36
|
+
>>> print(result) # Outputs: "data"
|
|
37
|
+
"""
|
|
38
|
+
try:
|
|
39
|
+
loop = asyncio.get_running_loop()
|
|
40
|
+
if loop.is_running():
|
|
41
|
+
# Check if we're running with uvloop
|
|
42
|
+
loop_module = type(loop).__module__
|
|
43
|
+
if 'uvloop' in loop_module.lower():
|
|
44
|
+
# uvloop doesn't support nest_asyncio, so we need a different approach
|
|
45
|
+
# Create a task and run it in the current loop context
|
|
46
|
+
import concurrent.futures
|
|
47
|
+
|
|
48
|
+
# Use a separate thread with a new event loop
|
|
49
|
+
def _run_in_thread():
|
|
50
|
+
new_loop = asyncio.new_event_loop()
|
|
51
|
+
asyncio.set_event_loop(new_loop)
|
|
52
|
+
try:
|
|
53
|
+
return new_loop.run_until_complete(coro)
|
|
54
|
+
finally:
|
|
55
|
+
new_loop.close()
|
|
56
|
+
|
|
57
|
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
58
|
+
future = executor.submit(_run_in_thread)
|
|
59
|
+
return future.result()
|
|
60
|
+
else:
|
|
61
|
+
# Use `nest_asyncio` for standard asyncio loops
|
|
62
|
+
import nest_asyncio
|
|
63
|
+
nest_asyncio.apply()
|
|
64
|
+
return loop.run_until_complete(coro)
|
|
65
|
+
except RuntimeError:
|
|
66
|
+
# No event loop in this context, safe to run
|
|
67
|
+
return asyncio.run(coro)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Any, List
|
|
2
|
+
from agents.tool import ToolContext as OpenAIAgentSDKToolContext, FunctionTool as OpenAIAgentSDKTool
|
|
3
|
+
import json
|
|
4
|
+
from xpander_sdk.modules.tools_repository.sub_modules.tool import Tool
|
|
5
|
+
|
|
6
|
+
def get_openai_agents_sdk_tools(agent: Any) -> List[OpenAIAgentSDKTool]:
|
|
7
|
+
tools: List[OpenAIAgentSDKTool] = []
|
|
8
|
+
agent_tools: List[Tool] = agent.tools.list
|
|
9
|
+
for _tool in agent_tools:
|
|
10
|
+
def make_tool(_tool_def: Tool):
|
|
11
|
+
async def invoke(context: OpenAIAgentSDKToolContext, args: str):
|
|
12
|
+
return await _tool_def.ainvoke(
|
|
13
|
+
task_id=agent.configuration.state.task.id
|
|
14
|
+
if agent.configuration.state.task
|
|
15
|
+
else None,
|
|
16
|
+
agent_id=agent.id,
|
|
17
|
+
agent_version=agent.version,
|
|
18
|
+
payload=json.loads(args),
|
|
19
|
+
configuration=_tool.configuration
|
|
20
|
+
)
|
|
21
|
+
return invoke
|
|
22
|
+
|
|
23
|
+
tools.append(
|
|
24
|
+
OpenAIAgentSDKTool(
|
|
25
|
+
on_invoke_tool=make_tool(_tool),
|
|
26
|
+
name=_tool.id,
|
|
27
|
+
description=_tool.description,
|
|
28
|
+
params_json_schema=_tool.parameters
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
return tools
|
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xpander-sdk
|
|
3
|
+
Version: 2.0.155
|
|
4
|
+
Summary: xpander.ai Backend-as-a-service for AI Agents - SDK
|
|
5
|
+
Home-page: https://www.xpander.ai
|
|
6
|
+
Author: xpanderAI
|
|
7
|
+
Author-email: dev@xpander.ai
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: python-dotenv
|
|
15
|
+
Requires-Dist: packaging
|
|
16
|
+
Requires-Dist: pydantic
|
|
17
|
+
Requires-Dist: loguru
|
|
18
|
+
Requires-Dist: httpx
|
|
19
|
+
Requires-Dist: httpx_sse
|
|
20
|
+
Requires-Dist: nest-asyncio
|
|
21
|
+
Requires-Dist: strands-agents
|
|
22
|
+
Requires-Dist: openai-agents
|
|
23
|
+
Provides-Extra: agno
|
|
24
|
+
Requires-Dist: agno; extra == "agno"
|
|
25
|
+
Requires-Dist: sqlalchemy; extra == "agno"
|
|
26
|
+
Requires-Dist: psycopg[binary,pool]; extra == "agno"
|
|
27
|
+
Requires-Dist: greenlet; extra == "agno"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: black; extra == "dev"
|
|
30
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest; extra == "dev"
|
|
32
|
+
Requires-Dist: anthropic; extra == "dev"
|
|
33
|
+
Requires-Dist: mcp; extra == "dev"
|
|
34
|
+
Requires-Dist: openai; extra == "dev"
|
|
35
|
+
Requires-Dist: fireworks-ai; extra == "dev"
|
|
36
|
+
Requires-Dist: aioboto3; extra == "dev"
|
|
37
|
+
Requires-Dist: google-genai; extra == "dev"
|
|
38
|
+
Dynamic: author
|
|
39
|
+
Dynamic: author-email
|
|
40
|
+
Dynamic: classifier
|
|
41
|
+
Dynamic: description
|
|
42
|
+
Dynamic: description-content-type
|
|
43
|
+
Dynamic: home-page
|
|
44
|
+
Dynamic: license-file
|
|
45
|
+
Dynamic: provides-extra
|
|
46
|
+
Dynamic: requires-dist
|
|
47
|
+
Dynamic: requires-python
|
|
48
|
+
Dynamic: summary
|
|
49
|
+
|
|
50
|
+
# xpander.ai SDK
|
|
51
|
+
|
|
52
|
+
[](https://www.python.org/downloads/) [](https://opensource.org/licenses/MIT) [](https://docs.xpander.ai) [](https://pypi.org/project/xpander-sdk/) [](https://pepy.tech/project/xpander-sdk)
|
|
53
|
+
|
|
54
|
+
The official Python SDK for xpander.ai - a powerful Backend-as-a-Service (BaaS) platform for building, deploying, and managing AI agents at scale.
|
|
55
|
+
|
|
56
|
+
## 🚀 Overview
|
|
57
|
+
|
|
58
|
+
xpander.ai SDK provides comprehensive tools for:
|
|
59
|
+
|
|
60
|
+
- **Agent Management**: Create, configure, and manage AI agents
|
|
61
|
+
- **Task Execution**: Handle complex task workflows and execution
|
|
62
|
+
- **Tools Repository**: Integrate external tools and services
|
|
63
|
+
- **Knowledge Bases**: Manage and search knowledge repositories
|
|
64
|
+
- **Event Handling**: Event-driven programming with decorators
|
|
65
|
+
- **Real-time Monitoring**: Track agent performance and execution
|
|
66
|
+
|
|
67
|
+
## 📦 Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install xpander-sdk
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### With Optional Dependencies
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# For Agno framework support (2.0+)
|
|
77
|
+
pip install xpander-sdk[agno]
|
|
78
|
+
|
|
79
|
+
# For development
|
|
80
|
+
pip install xpander-sdk[dev]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## 🔧 Quick Start
|
|
84
|
+
|
|
85
|
+
### 1. Configuration
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from xpander_sdk import Configuration
|
|
89
|
+
|
|
90
|
+
# Using environment variables (recommended)
|
|
91
|
+
config = Configuration()
|
|
92
|
+
|
|
93
|
+
# Or explicit configuration
|
|
94
|
+
config = Configuration(
|
|
95
|
+
api_key="your-api-key",
|
|
96
|
+
organization_id="your-org-id",
|
|
97
|
+
base_url="https://inbound.xpander.ai"
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 2. Basic Agent Operations
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from xpander_sdk import Agents, Agent, Tasks, AgentDeploymentType
|
|
105
|
+
|
|
106
|
+
# Initialize agents module
|
|
107
|
+
agents = Agents(configuration=config)
|
|
108
|
+
|
|
109
|
+
# List all agents
|
|
110
|
+
agent_list = await agents.alist()
|
|
111
|
+
|
|
112
|
+
# Load existing agent
|
|
113
|
+
agent = await agents.aget("agent-id")
|
|
114
|
+
|
|
115
|
+
# Create and execute a task
|
|
116
|
+
task = await agent.acreate_task(
|
|
117
|
+
prompt="Help me analyze this data",
|
|
118
|
+
file_urls=["https://example.com/data.csv"]
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 3. Task Management
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from xpander_sdk import Tasks, Task
|
|
126
|
+
|
|
127
|
+
# Initialize tasks module
|
|
128
|
+
tasks = Tasks(configuration=config)
|
|
129
|
+
|
|
130
|
+
# Load and manage tasks
|
|
131
|
+
task = await tasks.aget("task-id")
|
|
132
|
+
await task.aset_status(AgentExecutionStatus.Running)
|
|
133
|
+
await task.asave()
|
|
134
|
+
|
|
135
|
+
# Retrieve task activity log
|
|
136
|
+
activity_log = await task.aget_activity_log()
|
|
137
|
+
for message in activity_log.messages:
|
|
138
|
+
print(f"{message.role}: {message.content.text}")
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 4. Tools Integration
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from xpander_sdk import register_tool, ToolsRepository
|
|
145
|
+
|
|
146
|
+
# Register a local tool
|
|
147
|
+
@register_tool
|
|
148
|
+
def check_weather(location: str) -> str:
|
|
149
|
+
"""Check weather for a given location."""
|
|
150
|
+
return f"Weather in {location}: Sunny, 25°C"
|
|
151
|
+
|
|
152
|
+
# Register a tool with graph synchronization
|
|
153
|
+
@register_tool(add_to_graph=True)
|
|
154
|
+
async def analyze_data(data: list, analysis_type: str) -> dict:
|
|
155
|
+
"""Analyze data from multiple sources."""
|
|
156
|
+
return {
|
|
157
|
+
"analysis_type": analysis_type,
|
|
158
|
+
"data_points": len(data),
|
|
159
|
+
"status": "completed"
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# Use tools repository
|
|
163
|
+
tools = ToolsRepository(configuration=config)
|
|
164
|
+
weather_tool = tools.get_tool_by_id("check_weather")
|
|
165
|
+
result = await weather_tool.ainvoke(
|
|
166
|
+
agent_id="agent-id",
|
|
167
|
+
payload={"location": "New York"}
|
|
168
|
+
)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 5. Knowledge Base Operations
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from xpander_sdk import KnowledgeBases, KnowledgeBase
|
|
175
|
+
|
|
176
|
+
# Initialize knowledge bases
|
|
177
|
+
kb_module = KnowledgeBases(configuration=config)
|
|
178
|
+
|
|
179
|
+
# Create knowledge base
|
|
180
|
+
kb = await kb_module.acreate(
|
|
181
|
+
name="Company Docs",
|
|
182
|
+
description="Internal documentation"
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
# Add documents
|
|
186
|
+
documents = await kb.aadd_documents([
|
|
187
|
+
"https://example.com/doc1.pdf",
|
|
188
|
+
"https://example.com/doc2.txt"
|
|
189
|
+
])
|
|
190
|
+
|
|
191
|
+
# Search knowledge base
|
|
192
|
+
results = await kb.asearch(
|
|
193
|
+
search_query="product pricing",
|
|
194
|
+
top_k=5
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 6. Event-Driven Programming
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from xpander_sdk import on_task, Events
|
|
202
|
+
|
|
203
|
+
# Basic task handler
|
|
204
|
+
@on_task
|
|
205
|
+
async def handle_task(task):
|
|
206
|
+
print(f"Processing task: {task.id}")
|
|
207
|
+
# Task processing logic here
|
|
208
|
+
task.result = "Task processed successfully"
|
|
209
|
+
return task
|
|
210
|
+
|
|
211
|
+
# Task handler with configuration
|
|
212
|
+
@on_task(configuration=config)
|
|
213
|
+
def sync_task_handler(task):
|
|
214
|
+
print(f"Handling task synchronously: {task.id}")
|
|
215
|
+
task.result = "Sync processing complete"
|
|
216
|
+
return task
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 📚 Core Modules
|
|
221
|
+
|
|
222
|
+
| Module | Description | Documentation |
|
|
223
|
+
| ------------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------- |
|
|
224
|
+
| **Agents** | Agent creation, management, and execution | [Agents Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/AGENTS.md) |
|
|
225
|
+
| **Tasks** | Task lifecycle and execution management | [Tasks Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/TASKS.md) |
|
|
226
|
+
| **ToolsRepository** | External tools and integrations | [Tools Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/TOOLS.md) |
|
|
227
|
+
| **KnowledgeBases** | Knowledge management and search | [Knowledge Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/KNOWLEDGE.md) |
|
|
228
|
+
| **Events** | Event-driven programming | [Events Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/EVENTS.md) |
|
|
229
|
+
| **Backend** | Agent runtime arguments for frameworks | [Backend Guide](https://github.com/xpander-ai/xpander-sdk/blob/main/docs/BACKEND.md) |
|
|
230
|
+
|
|
231
|
+
## 🔄 Async/Sync Support
|
|
232
|
+
|
|
233
|
+
The SDK provides both asynchronous and synchronous interfaces:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
# Asynchronous (recommended for production)
|
|
237
|
+
agent = await Agent.aload("agent-id")
|
|
238
|
+
task = await agent.acreate_task(prompt="input data")
|
|
239
|
+
|
|
240
|
+
# Synchronous (convenient for scripts)
|
|
241
|
+
agent = Agent.load("agent-id")
|
|
242
|
+
task = agent.create_task(prompt="input data")
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## 📖 Advanced Examples
|
|
246
|
+
|
|
247
|
+
### Multi-Agent Orchestration
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
# Load multiple specialized agents
|
|
251
|
+
agents_list = await agents.alist()
|
|
252
|
+
data_agent = await agents.aget("data-agent-id")
|
|
253
|
+
writer_agent = await agents.aget("writer-agent-id")
|
|
254
|
+
|
|
255
|
+
# Chain agent executions
|
|
256
|
+
analysis_task = await data_agent.acreate_task(prompt="Analyze sales data")
|
|
257
|
+
report_task = await writer_agent.acreate_task(
|
|
258
|
+
prompt=f"Write a report based on: {analysis_task.result}"
|
|
259
|
+
)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Tool Integration with MCP Servers
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
from xpander_sdk import MCPServerDetails, MCPServerType
|
|
266
|
+
|
|
267
|
+
# Configure MCP server
|
|
268
|
+
mcp_server = MCPServerDetails(
|
|
269
|
+
name="data-server",
|
|
270
|
+
type=MCPServerType.STDIO,
|
|
271
|
+
command="python",
|
|
272
|
+
args=["-m", "mcp_server"],
|
|
273
|
+
env={"API_KEY": "your-key"}
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
# MCP servers are configured at the platform level
|
|
277
|
+
# and tools become available through ToolsRepository
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Streaming Task Execution
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
# Create a task with event streaming enabled
|
|
284
|
+
task = await agent.acreate_task(
|
|
285
|
+
prompt="complex analysis task",
|
|
286
|
+
events_streaming=True
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
# Stream events from the task
|
|
290
|
+
async for event in task.aevents():
|
|
291
|
+
print(f"Event Type: {event.type}")
|
|
292
|
+
print(f"Event Data: {event.data}")
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Task Activity Monitoring
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
from xpander_sdk import Task
|
|
299
|
+
from xpander_sdk.models.activity import (
|
|
300
|
+
AgentActivityThreadMessage,
|
|
301
|
+
AgentActivityThreadToolCall,
|
|
302
|
+
AgentActivityThreadReasoning
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
# Load a completed task
|
|
306
|
+
task = await Task.aload("task-id")
|
|
307
|
+
|
|
308
|
+
# Get detailed activity log
|
|
309
|
+
activity_log = await task.aget_activity_log()
|
|
310
|
+
|
|
311
|
+
# Analyze messages between user and agent
|
|
312
|
+
for message in activity_log.messages:
|
|
313
|
+
if isinstance(message, AgentActivityThreadMessage):
|
|
314
|
+
print(f"{message.role}: {message.content.text}")
|
|
315
|
+
elif isinstance(message, AgentActivityThreadToolCall):
|
|
316
|
+
# Tool call
|
|
317
|
+
print(f"Tool: {message.tool_name}")
|
|
318
|
+
print(f"Payload: {message.payload}")
|
|
319
|
+
print(f"Result: {message.result}")
|
|
320
|
+
elif isinstance(message, AgentActivityThreadReasoning):
|
|
321
|
+
# Reasoning step
|
|
322
|
+
print(f"Reasoning ({message.type}): {message.thought}")
|
|
323
|
+
|
|
324
|
+
# Synchronous version
|
|
325
|
+
task = Task.load("task-id")
|
|
326
|
+
activity_log = task.get_activity_log()
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Local Task Testing
|
|
330
|
+
|
|
331
|
+
```python
|
|
332
|
+
from xpander_sdk.modules.tasks.models.task import LocalTaskTest, AgentExecutionInput
|
|
333
|
+
from xpander_sdk.models.shared import OutputFormat
|
|
334
|
+
from xpander_sdk import on_task
|
|
335
|
+
|
|
336
|
+
# Define a local test task
|
|
337
|
+
local_task = LocalTaskTest(
|
|
338
|
+
input=AgentExecutionInput(text="What can you do?"),
|
|
339
|
+
output_format=OutputFormat.Json,
|
|
340
|
+
output_schema={"capabilities": "list of capabilities"}
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
# Test with local task
|
|
344
|
+
@on_task(test_task=local_task)
|
|
345
|
+
async def handle_test_task(task):
|
|
346
|
+
task.result = {
|
|
347
|
+
"capabilities": [
|
|
348
|
+
"Data analysis",
|
|
349
|
+
"Text processing",
|
|
350
|
+
"API integration"
|
|
351
|
+
]
|
|
352
|
+
}
|
|
353
|
+
return task
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## 🧪 Testing
|
|
357
|
+
|
|
358
|
+
```bash
|
|
359
|
+
# Run tests
|
|
360
|
+
pytest tests/
|
|
361
|
+
|
|
362
|
+
# Run with coverage
|
|
363
|
+
pytest tests/ --cov=xpander_sdk
|
|
364
|
+
|
|
365
|
+
# Run specific test
|
|
366
|
+
pytest tests/test_agents.py::test_agent_creation
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## 🏗️ Architecture
|
|
370
|
+
|
|
371
|
+
```plaintext
|
|
372
|
+
xpander_sdk/
|
|
373
|
+
├── core/ # Core API client and base classes
|
|
374
|
+
├── models/ # Pydantic models and configurations
|
|
375
|
+
├── modules/
|
|
376
|
+
│ ├── agents/ # Agent management
|
|
377
|
+
│ ├── tasks/ # Task execution
|
|
378
|
+
│ ├── tools_repository/ # Tools and integrations
|
|
379
|
+
│ ├── knowledge_bases/ # Knowledge management
|
|
380
|
+
│ ├── events/ # Event handling
|
|
381
|
+
│ └── backend/ # Agent runtime arguments for frameworks
|
|
382
|
+
└── utils/ # Utility functions
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## 🔒 Authentication
|
|
386
|
+
|
|
387
|
+
The SDK supports multiple authentication methods:
|
|
388
|
+
|
|
389
|
+
### Environment Variables (Recommended)
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
export XPANDER_API_KEY="your-api-key"
|
|
393
|
+
export XPANDER_ORGANIZATION_ID="your-org-id"
|
|
394
|
+
export XPANDER_BASE_URL="https://inbound.xpander.ai" # Optional
|
|
395
|
+
export XPANDER_AGENT_ID="your-agent-id" # Optional for Backend module
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### Configuration Object
|
|
399
|
+
|
|
400
|
+
```python
|
|
401
|
+
config = Configuration(
|
|
402
|
+
api_key="your-api-key",
|
|
403
|
+
organization_id="your-org-id"
|
|
404
|
+
)
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### From File
|
|
408
|
+
|
|
409
|
+
```python
|
|
410
|
+
# .env file
|
|
411
|
+
XPANDER_API_KEY=your-api-key
|
|
412
|
+
XPANDER_ORGANIZATION_ID=your-org-id
|
|
413
|
+
|
|
414
|
+
# Python code
|
|
415
|
+
from dotenv import load_dotenv
|
|
416
|
+
load_dotenv()
|
|
417
|
+
config = Configuration()
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
## 🏢 Self-Hosted Deployment
|
|
421
|
+
|
|
422
|
+
If you're using a self-hosted xpander.ai deployment, configure the SDK to point to your Agent Controller endpoint.
|
|
423
|
+
|
|
424
|
+
**Important**: Use the **Agent Controller API key** generated during Helm installation, not your xpander.ai cloud API key.
|
|
425
|
+
|
|
426
|
+
### Configuration
|
|
427
|
+
|
|
428
|
+
```bash
|
|
429
|
+
# Set environment variables
|
|
430
|
+
export XPANDER_API_KEY="your-agent-controller-api-key" # From Helm installation
|
|
431
|
+
export XPANDER_ORGANIZATION_ID="your-org-id"
|
|
432
|
+
export XPANDER_BASE_URL="https://agent-controller.my-company.com"
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
Or configure explicitly:
|
|
436
|
+
|
|
437
|
+
```python
|
|
438
|
+
from xpander_sdk import Configuration
|
|
439
|
+
|
|
440
|
+
config = Configuration(
|
|
441
|
+
api_key="your-agent-controller-api-key", # From Helm installation
|
|
442
|
+
organization_id="your-org-id",
|
|
443
|
+
base_url="https://agent-controller.my-company.com"
|
|
444
|
+
)
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Using with Agno Framework
|
|
448
|
+
|
|
449
|
+
```python
|
|
450
|
+
from xpander_sdk import Backend, Configuration
|
|
451
|
+
from agno.agent import Agent
|
|
452
|
+
|
|
453
|
+
# Configure for self-hosted
|
|
454
|
+
config = Configuration(
|
|
455
|
+
api_key="your-agent-controller-api-key", # From Helm installation
|
|
456
|
+
organization_id="your-org-id",
|
|
457
|
+
base_url="https://agent-controller.my-company.com"
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
# Initialize Backend with self-hosted config
|
|
461
|
+
backend = Backend(configuration=config)
|
|
462
|
+
|
|
463
|
+
# Create agent - it will use your self-hosted infrastructure
|
|
464
|
+
agno_agent = Agent(**backend.get_args(agent_id="agent-123"))
|
|
465
|
+
|
|
466
|
+
# Run agent
|
|
467
|
+
result = await agno_agent.arun(
|
|
468
|
+
input="What can you help me with?",
|
|
469
|
+
stream=True
|
|
470
|
+
)
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### Complete Self-Hosted Example
|
|
474
|
+
|
|
475
|
+
```python
|
|
476
|
+
import asyncio
|
|
477
|
+
from xpander_sdk import Configuration, Agent
|
|
478
|
+
|
|
479
|
+
async def main():
|
|
480
|
+
# Configure for self-hosted deployment
|
|
481
|
+
config = Configuration(
|
|
482
|
+
api_key="your-agent-controller-api-key", # From Helm installation
|
|
483
|
+
organization_id="your-org-id",
|
|
484
|
+
base_url="https://agent-controller.my-company.com"
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
# Load agent from self-hosted deployment
|
|
488
|
+
agent = await Agent.aload("agent-123", configuration=config)
|
|
489
|
+
print(f"Agent: {agent.name}")
|
|
490
|
+
|
|
491
|
+
# Create and execute task
|
|
492
|
+
task = await agent.acreate_task(
|
|
493
|
+
prompt="Analyze Q4 sales data",
|
|
494
|
+
file_urls=["https://example.com/sales-q4.csv"]
|
|
495
|
+
)
|
|
496
|
+
print(f"Task created: {task.id}")
|
|
497
|
+
print(f"Status: {task.status}")
|
|
498
|
+
|
|
499
|
+
if __name__ == "__main__":
|
|
500
|
+
asyncio.run(main())
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
**Important**: Make sure your `base_url` points to the Agent Controller endpoint (e.g., `https://agent-controller.{your-domain}`), not the root domain.
|
|
504
|
+
|
|
505
|
+
📖 **Full Guide**: [Self-Hosted Configuration Documentation](https://docs.xpander.ai/api-reference/configuration/self-hosted)
|
|
506
|
+
|
|
507
|
+
## 🔄 Error Handling
|
|
508
|
+
|
|
509
|
+
```python
|
|
510
|
+
from xpander_sdk.exceptions import ModuleException
|
|
511
|
+
|
|
512
|
+
try:
|
|
513
|
+
agent = await Agent.aload("invalid-agent-id")
|
|
514
|
+
except ModuleException as e:
|
|
515
|
+
print(f"Error {e.status_code}: {e.description}")
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
## 🤝 Contributing
|
|
519
|
+
|
|
520
|
+
1. Fork the repository
|
|
521
|
+
2. Create a feature branch (`git checkout -b feature/{base_branch}/amazing-feature`)
|
|
522
|
+
3. Commit your changes (`git commit -m 'feat/chore/fix: Add amazing feature'`)
|
|
523
|
+
4. Push to the branch (`git push origin feature/{base_branch}/amazing-feature`)
|
|
524
|
+
5. Open a Pull Request
|
|
525
|
+
|
|
526
|
+
## 📄 License
|
|
527
|
+
|
|
528
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
529
|
+
|
|
530
|
+
## 🆘 Support
|
|
531
|
+
|
|
532
|
+
- **Documentation**: [https://docs.xpander.ai](https://docs.xpander.ai)
|
|
533
|
+
- **Issues**: [GitHub Issues](https://github.com/xpander-ai/xpander-sdk/issues)
|
|
534
|
+
- **Email**: support@xpander.ai
|
|
535
|
+
|
|
536
|
+
---
|
|
537
|
+
|
|
538
|
+
Built with ❤️ by the xpander.ai team
|