agentex-sdk 0.1.0a6__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.
- agentex/__init__.py +103 -0
- agentex/_base_client.py +1992 -0
- agentex/_client.py +506 -0
- agentex/_compat.py +219 -0
- agentex/_constants.py +14 -0
- agentex/_exceptions.py +108 -0
- agentex/_files.py +123 -0
- agentex/_models.py +829 -0
- agentex/_qs.py +150 -0
- agentex/_resource.py +43 -0
- agentex/_response.py +830 -0
- agentex/_streaming.py +333 -0
- agentex/_types.py +219 -0
- agentex/_utils/__init__.py +57 -0
- agentex/_utils/_logs.py +25 -0
- agentex/_utils/_proxy.py +65 -0
- agentex/_utils/_reflection.py +42 -0
- agentex/_utils/_resources_proxy.py +24 -0
- agentex/_utils/_streams.py +12 -0
- agentex/_utils/_sync.py +86 -0
- agentex/_utils/_transform.py +447 -0
- agentex/_utils/_typing.py +151 -0
- agentex/_utils/_utils.py +422 -0
- agentex/_version.py +4 -0
- agentex/lib/.keep +4 -0
- agentex/lib/__init__.py +0 -0
- agentex/lib/adk/__init__.py +41 -0
- agentex/lib/adk/_modules/__init__.py +0 -0
- agentex/lib/adk/_modules/acp.py +247 -0
- agentex/lib/adk/_modules/agent_task_tracker.py +176 -0
- agentex/lib/adk/_modules/agents.py +77 -0
- agentex/lib/adk/_modules/events.py +141 -0
- agentex/lib/adk/_modules/messages.py +285 -0
- agentex/lib/adk/_modules/state.py +291 -0
- agentex/lib/adk/_modules/streaming.py +75 -0
- agentex/lib/adk/_modules/tasks.py +124 -0
- agentex/lib/adk/_modules/tracing.py +194 -0
- agentex/lib/adk/providers/__init__.py +9 -0
- agentex/lib/adk/providers/_modules/__init__.py +0 -0
- agentex/lib/adk/providers/_modules/litellm.py +232 -0
- agentex/lib/adk/providers/_modules/openai.py +416 -0
- agentex/lib/adk/providers/_modules/sgp.py +85 -0
- agentex/lib/adk/utils/__init__.py +5 -0
- agentex/lib/adk/utils/_modules/__init__.py +0 -0
- agentex/lib/adk/utils/_modules/templating.py +94 -0
- agentex/lib/cli/__init__.py +0 -0
- agentex/lib/cli/commands/__init__.py +0 -0
- agentex/lib/cli/commands/agents.py +328 -0
- agentex/lib/cli/commands/init.py +227 -0
- agentex/lib/cli/commands/main.py +33 -0
- agentex/lib/cli/commands/secrets.py +169 -0
- agentex/lib/cli/commands/tasks.py +118 -0
- agentex/lib/cli/commands/uv.py +133 -0
- agentex/lib/cli/handlers/__init__.py +0 -0
- agentex/lib/cli/handlers/agent_handlers.py +160 -0
- agentex/lib/cli/handlers/cleanup_handlers.py +186 -0
- agentex/lib/cli/handlers/deploy_handlers.py +351 -0
- agentex/lib/cli/handlers/run_handlers.py +452 -0
- agentex/lib/cli/handlers/secret_handlers.py +670 -0
- agentex/lib/cli/templates/default/.dockerignore.j2 +43 -0
- agentex/lib/cli/templates/default/Dockerfile-uv.j2 +42 -0
- agentex/lib/cli/templates/default/Dockerfile.j2 +42 -0
- agentex/lib/cli/templates/default/README.md.j2 +193 -0
- agentex/lib/cli/templates/default/deploy/example.yaml.j2 +55 -0
- agentex/lib/cli/templates/default/manifest.yaml.j2 +116 -0
- agentex/lib/cli/templates/default/project/acp.py.j2 +29 -0
- agentex/lib/cli/templates/default/pyproject.toml.j2 +33 -0
- agentex/lib/cli/templates/default/requirements.txt.j2 +5 -0
- agentex/lib/cli/templates/deploy/Screenshot 2025-03-19 at 10.36.57/342/200/257AM.png +0 -0
- agentex/lib/cli/templates/deploy/example.yaml.j2 +55 -0
- agentex/lib/cli/templates/sync/.dockerignore.j2 +43 -0
- agentex/lib/cli/templates/sync/Dockerfile-uv.j2 +42 -0
- agentex/lib/cli/templates/sync/Dockerfile.j2 +42 -0
- agentex/lib/cli/templates/sync/README.md.j2 +293 -0
- agentex/lib/cli/templates/sync/deploy/example.yaml.j2 +55 -0
- agentex/lib/cli/templates/sync/manifest.yaml.j2 +116 -0
- agentex/lib/cli/templates/sync/project/acp.py.j2 +26 -0
- agentex/lib/cli/templates/sync/pyproject.toml.j2 +33 -0
- agentex/lib/cli/templates/sync/requirements.txt.j2 +5 -0
- agentex/lib/cli/templates/temporal/.dockerignore.j2 +43 -0
- agentex/lib/cli/templates/temporal/Dockerfile-uv.j2 +48 -0
- agentex/lib/cli/templates/temporal/Dockerfile.j2 +48 -0
- agentex/lib/cli/templates/temporal/README.md.j2 +316 -0
- agentex/lib/cli/templates/temporal/deploy/example.yaml.j2 +55 -0
- agentex/lib/cli/templates/temporal/manifest.yaml.j2 +137 -0
- agentex/lib/cli/templates/temporal/project/acp.py.j2 +30 -0
- agentex/lib/cli/templates/temporal/project/run_worker.py.j2 +33 -0
- agentex/lib/cli/templates/temporal/project/workflow.py.j2 +66 -0
- agentex/lib/cli/templates/temporal/pyproject.toml.j2 +34 -0
- agentex/lib/cli/templates/temporal/requirements.txt.j2 +5 -0
- agentex/lib/cli/utils/cli_utils.py +14 -0
- agentex/lib/cli/utils/credential_utils.py +103 -0
- agentex/lib/cli/utils/exceptions.py +6 -0
- agentex/lib/cli/utils/kubectl_utils.py +135 -0
- agentex/lib/cli/utils/kubernetes_secrets_utils.py +185 -0
- agentex/lib/core/__init__.py +0 -0
- agentex/lib/core/adapters/__init__.py +0 -0
- agentex/lib/core/adapters/llm/__init__.py +1 -0
- agentex/lib/core/adapters/llm/adapter_litellm.py +46 -0
- agentex/lib/core/adapters/llm/adapter_sgp.py +55 -0
- agentex/lib/core/adapters/llm/port.py +24 -0
- agentex/lib/core/adapters/streams/adapter_redis.py +128 -0
- agentex/lib/core/adapters/streams/port.py +50 -0
- agentex/lib/core/clients/__init__.py +1 -0
- agentex/lib/core/clients/temporal/__init__.py +0 -0
- agentex/lib/core/clients/temporal/temporal_client.py +181 -0
- agentex/lib/core/clients/temporal/types.py +47 -0
- agentex/lib/core/clients/temporal/utils.py +56 -0
- agentex/lib/core/services/__init__.py +0 -0
- agentex/lib/core/services/adk/__init__.py +0 -0
- agentex/lib/core/services/adk/acp/__init__.py +0 -0
- agentex/lib/core/services/adk/acp/acp.py +210 -0
- agentex/lib/core/services/adk/agent_task_tracker.py +85 -0
- agentex/lib/core/services/adk/agents.py +43 -0
- agentex/lib/core/services/adk/events.py +61 -0
- agentex/lib/core/services/adk/messages.py +164 -0
- agentex/lib/core/services/adk/providers/__init__.py +0 -0
- agentex/lib/core/services/adk/providers/litellm.py +256 -0
- agentex/lib/core/services/adk/providers/openai.py +723 -0
- agentex/lib/core/services/adk/providers/sgp.py +99 -0
- agentex/lib/core/services/adk/state.py +120 -0
- agentex/lib/core/services/adk/streaming.py +262 -0
- agentex/lib/core/services/adk/tasks.py +69 -0
- agentex/lib/core/services/adk/tracing.py +36 -0
- agentex/lib/core/services/adk/utils/__init__.py +0 -0
- agentex/lib/core/services/adk/utils/templating.py +58 -0
- agentex/lib/core/temporal/__init__.py +0 -0
- agentex/lib/core/temporal/activities/__init__.py +207 -0
- agentex/lib/core/temporal/activities/activity_helpers.py +37 -0
- agentex/lib/core/temporal/activities/adk/__init__.py +0 -0
- agentex/lib/core/temporal/activities/adk/acp/__init__.py +0 -0
- agentex/lib/core/temporal/activities/adk/acp/acp_activities.py +86 -0
- agentex/lib/core/temporal/activities/adk/agent_task_tracker_activities.py +76 -0
- agentex/lib/core/temporal/activities/adk/agents_activities.py +35 -0
- agentex/lib/core/temporal/activities/adk/events_activities.py +50 -0
- agentex/lib/core/temporal/activities/adk/messages_activities.py +94 -0
- agentex/lib/core/temporal/activities/adk/providers/__init__.py +0 -0
- agentex/lib/core/temporal/activities/adk/providers/litellm_activities.py +71 -0
- agentex/lib/core/temporal/activities/adk/providers/openai_activities.py +210 -0
- agentex/lib/core/temporal/activities/adk/providers/sgp_activities.py +42 -0
- agentex/lib/core/temporal/activities/adk/state_activities.py +85 -0
- agentex/lib/core/temporal/activities/adk/streaming_activities.py +33 -0
- agentex/lib/core/temporal/activities/adk/tasks_activities.py +48 -0
- agentex/lib/core/temporal/activities/adk/tracing_activities.py +55 -0
- agentex/lib/core/temporal/activities/adk/utils/__init__.py +0 -0
- agentex/lib/core/temporal/activities/adk/utils/templating_activities.py +41 -0
- agentex/lib/core/temporal/services/__init__.py +0 -0
- agentex/lib/core/temporal/services/temporal_task_service.py +69 -0
- agentex/lib/core/temporal/types/__init__.py +0 -0
- agentex/lib/core/temporal/types/workflow.py +5 -0
- agentex/lib/core/temporal/workers/__init__.py +0 -0
- agentex/lib/core/temporal/workers/worker.py +162 -0
- agentex/lib/core/temporal/workflows/workflow.py +26 -0
- agentex/lib/core/tracing/__init__.py +5 -0
- agentex/lib/core/tracing/processors/agentex_tracing_processor.py +117 -0
- agentex/lib/core/tracing/processors/sgp_tracing_processor.py +119 -0
- agentex/lib/core/tracing/processors/tracing_processor_interface.py +40 -0
- agentex/lib/core/tracing/trace.py +311 -0
- agentex/lib/core/tracing/tracer.py +70 -0
- agentex/lib/core/tracing/tracing_processor_manager.py +62 -0
- agentex/lib/environment_variables.py +87 -0
- agentex/lib/py.typed +0 -0
- agentex/lib/sdk/__init__.py +0 -0
- agentex/lib/sdk/config/__init__.py +0 -0
- agentex/lib/sdk/config/agent_config.py +61 -0
- agentex/lib/sdk/config/agent_manifest.py +219 -0
- agentex/lib/sdk/config/build_config.py +35 -0
- agentex/lib/sdk/config/deployment_config.py +117 -0
- agentex/lib/sdk/config/local_development_config.py +56 -0
- agentex/lib/sdk/config/project_config.py +103 -0
- agentex/lib/sdk/fastacp/__init__.py +3 -0
- agentex/lib/sdk/fastacp/base/base_acp_server.py +406 -0
- agentex/lib/sdk/fastacp/fastacp.py +74 -0
- agentex/lib/sdk/fastacp/impl/agentic_base_acp.py +72 -0
- agentex/lib/sdk/fastacp/impl/sync_acp.py +109 -0
- agentex/lib/sdk/fastacp/impl/temporal_acp.py +97 -0
- agentex/lib/sdk/fastacp/tests/README.md +297 -0
- agentex/lib/sdk/fastacp/tests/conftest.py +307 -0
- agentex/lib/sdk/fastacp/tests/pytest.ini +10 -0
- agentex/lib/sdk/fastacp/tests/run_tests.py +227 -0
- agentex/lib/sdk/fastacp/tests/test_base_acp_server.py +450 -0
- agentex/lib/sdk/fastacp/tests/test_fastacp_factory.py +344 -0
- agentex/lib/sdk/fastacp/tests/test_integration.py +477 -0
- agentex/lib/sdk/state_machine/__init__.py +6 -0
- agentex/lib/sdk/state_machine/noop_workflow.py +21 -0
- agentex/lib/sdk/state_machine/state.py +10 -0
- agentex/lib/sdk/state_machine/state_machine.py +189 -0
- agentex/lib/sdk/state_machine/state_workflow.py +16 -0
- agentex/lib/sdk/utils/__init__.py +0 -0
- agentex/lib/sdk/utils/messages.py +223 -0
- agentex/lib/types/__init__.py +0 -0
- agentex/lib/types/acp.py +94 -0
- agentex/lib/types/agent_configs.py +79 -0
- agentex/lib/types/agent_results.py +29 -0
- agentex/lib/types/credentials.py +34 -0
- agentex/lib/types/fastacp.py +61 -0
- agentex/lib/types/files.py +13 -0
- agentex/lib/types/json_rpc.py +49 -0
- agentex/lib/types/llm_messages.py +354 -0
- agentex/lib/types/task_message_updates.py +171 -0
- agentex/lib/types/tracing.py +34 -0
- agentex/lib/utils/__init__.py +0 -0
- agentex/lib/utils/completions.py +131 -0
- agentex/lib/utils/console.py +14 -0
- agentex/lib/utils/io.py +29 -0
- agentex/lib/utils/iterables.py +14 -0
- agentex/lib/utils/json_schema.py +23 -0
- agentex/lib/utils/logging.py +31 -0
- agentex/lib/utils/mcp.py +17 -0
- agentex/lib/utils/model_utils.py +46 -0
- agentex/lib/utils/parsing.py +15 -0
- agentex/lib/utils/regex.py +6 -0
- agentex/lib/utils/temporal.py +13 -0
- agentex/py.typed +0 -0
- agentex/resources/__init__.py +103 -0
- agentex/resources/agents.py +707 -0
- agentex/resources/events.py +294 -0
- agentex/resources/messages/__init__.py +33 -0
- agentex/resources/messages/batch.py +271 -0
- agentex/resources/messages/messages.py +492 -0
- agentex/resources/spans.py +557 -0
- agentex/resources/states.py +544 -0
- agentex/resources/tasks.py +615 -0
- agentex/resources/tracker.py +384 -0
- agentex/types/__init__.py +56 -0
- agentex/types/acp_type.py +7 -0
- agentex/types/agent.py +29 -0
- agentex/types/agent_list_params.py +13 -0
- agentex/types/agent_list_response.py +10 -0
- agentex/types/agent_rpc_by_name_params.py +21 -0
- agentex/types/agent_rpc_params.py +51 -0
- agentex/types/agent_rpc_params1.py +21 -0
- agentex/types/agent_rpc_response.py +20 -0
- agentex/types/agent_rpc_result.py +90 -0
- agentex/types/agent_task_tracker.py +34 -0
- agentex/types/data_content.py +30 -0
- agentex/types/data_content_param.py +31 -0
- agentex/types/data_delta.py +14 -0
- agentex/types/event.py +29 -0
- agentex/types/event_list_params.py +22 -0
- agentex/types/event_list_response.py +10 -0
- agentex/types/message_author.py +7 -0
- agentex/types/message_create_params.py +18 -0
- agentex/types/message_list_params.py +14 -0
- agentex/types/message_list_response.py +10 -0
- agentex/types/message_style.py +7 -0
- agentex/types/message_update_params.py +18 -0
- agentex/types/messages/__init__.py +8 -0
- agentex/types/messages/batch_create_params.py +16 -0
- agentex/types/messages/batch_create_response.py +10 -0
- agentex/types/messages/batch_update_params.py +16 -0
- agentex/types/messages/batch_update_response.py +10 -0
- agentex/types/shared/__init__.py +3 -0
- agentex/types/shared/task_message_update.py +83 -0
- agentex/types/span.py +36 -0
- agentex/types/span_create_params.py +40 -0
- agentex/types/span_list_params.py +12 -0
- agentex/types/span_list_response.py +10 -0
- agentex/types/span_update_params.py +37 -0
- agentex/types/state.py +25 -0
- agentex/types/state_create_params.py +16 -0
- agentex/types/state_list_params.py +16 -0
- agentex/types/state_list_response.py +10 -0
- agentex/types/state_update_params.py +16 -0
- agentex/types/task.py +23 -0
- agentex/types/task_delete_by_name_response.py +8 -0
- agentex/types/task_delete_response.py +8 -0
- agentex/types/task_list_response.py +10 -0
- agentex/types/task_message.py +33 -0
- agentex/types/task_message_content.py +16 -0
- agentex/types/task_message_content_param.py +17 -0
- agentex/types/task_message_delta.py +16 -0
- agentex/types/text_content.py +53 -0
- agentex/types/text_content_param.py +54 -0
- agentex/types/text_delta.py +14 -0
- agentex/types/tool_request_content.py +36 -0
- agentex/types/tool_request_content_param.py +37 -0
- agentex/types/tool_request_delta.py +18 -0
- agentex/types/tool_response_content.py +36 -0
- agentex/types/tool_response_content_param.py +36 -0
- agentex/types/tool_response_delta.py +18 -0
- agentex/types/tracker_list_params.py +16 -0
- agentex/types/tracker_list_response.py +10 -0
- agentex/types/tracker_update_params.py +19 -0
- agentex_sdk-0.1.0a6.dist-info/METADATA +426 -0
- agentex_sdk-0.1.0a6.dist-info/RECORD +289 -0
- agentex_sdk-0.1.0a6.dist-info/WHEEL +4 -0
- agentex_sdk-0.1.0a6.dist-info/entry_points.txt +2 -0
- agentex_sdk-0.1.0a6.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,124 @@
|
|
1
|
+
from datetime import timedelta
|
2
|
+
|
3
|
+
from temporalio.common import RetryPolicy
|
4
|
+
|
5
|
+
from agentex import AsyncAgentex
|
6
|
+
from agentex.lib.core.services.adk.tasks import TasksService
|
7
|
+
from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
|
8
|
+
from agentex.lib.core.temporal.activities.adk.tasks_activities import (
|
9
|
+
DeleteTaskParams,
|
10
|
+
GetTaskParams,
|
11
|
+
TasksActivityName,
|
12
|
+
)
|
13
|
+
from agentex.lib.core.tracing.tracer import AsyncTracer
|
14
|
+
from agentex.types.task import Task
|
15
|
+
from agentex.lib.utils.logging import make_logger
|
16
|
+
from agentex.lib.utils.temporal import in_temporal_workflow
|
17
|
+
|
18
|
+
logger = make_logger(__name__)
|
19
|
+
|
20
|
+
DEFAULT_RETRY_POLICY = RetryPolicy(maximum_attempts=1)
|
21
|
+
|
22
|
+
|
23
|
+
class TasksModule:
|
24
|
+
"""
|
25
|
+
Module for managing tasks in Agentex.
|
26
|
+
Provides high-level async methods for retrieving, listing, and deleting tasks.
|
27
|
+
"""
|
28
|
+
|
29
|
+
def __init__(
|
30
|
+
self,
|
31
|
+
tasks_service: TasksService | None = None,
|
32
|
+
):
|
33
|
+
if tasks_service is None:
|
34
|
+
agentex_client = AsyncAgentex()
|
35
|
+
tracer = AsyncTracer(agentex_client)
|
36
|
+
self._tasks_service = TasksService(
|
37
|
+
agentex_client=agentex_client, tracer=tracer
|
38
|
+
)
|
39
|
+
else:
|
40
|
+
self._tasks_service = tasks_service
|
41
|
+
|
42
|
+
async def get(
|
43
|
+
self,
|
44
|
+
*,
|
45
|
+
task_id: str | None = None,
|
46
|
+
task_name: str | None = None,
|
47
|
+
trace_id: str | None = None,
|
48
|
+
parent_span_id: str | None = None,
|
49
|
+
start_to_close_timeout: timedelta = timedelta(seconds=5),
|
50
|
+
heartbeat_timeout: timedelta = timedelta(seconds=5),
|
51
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
52
|
+
) -> Task:
|
53
|
+
"""
|
54
|
+
Get a task by ID or name.
|
55
|
+
Args:
|
56
|
+
task_id: The ID of the task to retrieve.
|
57
|
+
task_name: The name of the task to retrieve.
|
58
|
+
Returns:
|
59
|
+
The task entry.
|
60
|
+
"""
|
61
|
+
params = GetTaskParams(
|
62
|
+
task_id=task_id,
|
63
|
+
task_name=task_name,
|
64
|
+
trace_id=trace_id,
|
65
|
+
parent_span_id=parent_span_id,
|
66
|
+
)
|
67
|
+
if in_temporal_workflow():
|
68
|
+
return await ActivityHelpers.execute_activity(
|
69
|
+
activity_name=TasksActivityName.GET_TASK,
|
70
|
+
request=params,
|
71
|
+
response_type=Task,
|
72
|
+
start_to_close_timeout=start_to_close_timeout,
|
73
|
+
retry_policy=retry_policy,
|
74
|
+
heartbeat_timeout=heartbeat_timeout,
|
75
|
+
)
|
76
|
+
else:
|
77
|
+
return await self._tasks_service.get_task(
|
78
|
+
task_id=task_id,
|
79
|
+
task_name=task_name,
|
80
|
+
trace_id=trace_id,
|
81
|
+
parent_span_id=parent_span_id,
|
82
|
+
)
|
83
|
+
|
84
|
+
async def delete(
|
85
|
+
self,
|
86
|
+
*,
|
87
|
+
task_id: str | None = None,
|
88
|
+
task_name: str | None = None,
|
89
|
+
trace_id: str | None = None,
|
90
|
+
parent_span_id: str | None = None,
|
91
|
+
start_to_close_timeout: timedelta = timedelta(seconds=5),
|
92
|
+
heartbeat_timeout: timedelta = timedelta(seconds=5),
|
93
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
94
|
+
) -> Task:
|
95
|
+
"""
|
96
|
+
Delete a task by ID or name.
|
97
|
+
Args:
|
98
|
+
task_id: The ID of the task to delete.
|
99
|
+
task_name: The name of the task to delete.
|
100
|
+
Returns:
|
101
|
+
The deleted task entry.
|
102
|
+
"""
|
103
|
+
params = DeleteTaskParams(
|
104
|
+
task_id=task_id,
|
105
|
+
task_name=task_name,
|
106
|
+
trace_id=trace_id,
|
107
|
+
parent_span_id=parent_span_id,
|
108
|
+
)
|
109
|
+
if in_temporal_workflow():
|
110
|
+
return await ActivityHelpers.execute_activity(
|
111
|
+
activity_name=TasksActivityName.DELETE_TASK,
|
112
|
+
request=params,
|
113
|
+
response_type=Task,
|
114
|
+
start_to_close_timeout=start_to_close_timeout,
|
115
|
+
retry_policy=retry_policy,
|
116
|
+
heartbeat_timeout=heartbeat_timeout,
|
117
|
+
)
|
118
|
+
else:
|
119
|
+
return await self._tasks_service.delete_task(
|
120
|
+
task_id=task_id,
|
121
|
+
task_name=task_name,
|
122
|
+
trace_id=trace_id,
|
123
|
+
parent_span_id=parent_span_id,
|
124
|
+
)
|
@@ -0,0 +1,194 @@
|
|
1
|
+
from collections.abc import AsyncGenerator
|
2
|
+
from contextlib import asynccontextmanager
|
3
|
+
from datetime import timedelta
|
4
|
+
from typing import Any
|
5
|
+
|
6
|
+
from temporalio.common import RetryPolicy
|
7
|
+
|
8
|
+
from agentex import AsyncAgentex
|
9
|
+
from agentex.lib.core.services.adk.tracing import TracingService
|
10
|
+
from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
|
11
|
+
from agentex.lib.core.temporal.activities.adk.tracing_activities import (
|
12
|
+
EndSpanParams,
|
13
|
+
StartSpanParams,
|
14
|
+
TracingActivityName,
|
15
|
+
)
|
16
|
+
from agentex.lib.core.tracing.tracer import AsyncTracer
|
17
|
+
from agentex.types.span import Span
|
18
|
+
from agentex.lib.utils.logging import make_logger
|
19
|
+
from agentex.lib.utils.model_utils import BaseModel
|
20
|
+
from agentex.lib.utils.temporal import in_temporal_workflow
|
21
|
+
|
22
|
+
logger = make_logger(__name__)
|
23
|
+
|
24
|
+
DEFAULT_RETRY_POLICY = RetryPolicy(maximum_attempts=1)
|
25
|
+
|
26
|
+
|
27
|
+
class TracingModule:
|
28
|
+
"""
|
29
|
+
Module for managing tracing and span operations in Agentex.
|
30
|
+
Provides high-level async methods for starting, ending, and managing spans for distributed tracing.
|
31
|
+
"""
|
32
|
+
|
33
|
+
def __init__(self, tracing_service: TracingService | None = None):
|
34
|
+
"""
|
35
|
+
Initialize the tracing interface.
|
36
|
+
|
37
|
+
Args:
|
38
|
+
tracing_activities (Optional[TracingActivities]): Optional pre-configured tracing activities. If None, will be auto-initialized.
|
39
|
+
"""
|
40
|
+
if tracing_service is None:
|
41
|
+
agentex_client = AsyncAgentex()
|
42
|
+
tracer = AsyncTracer(agentex_client)
|
43
|
+
self._tracing_service = TracingService(tracer=tracer)
|
44
|
+
else:
|
45
|
+
self._tracing_service = tracing_service
|
46
|
+
|
47
|
+
@asynccontextmanager
|
48
|
+
async def span(
|
49
|
+
self,
|
50
|
+
trace_id: str,
|
51
|
+
name: str,
|
52
|
+
input: list[Any] | dict[str, Any] | BaseModel | None = None,
|
53
|
+
data: list[Any] | dict[str, Any] | BaseModel | None = None,
|
54
|
+
parent_id: str | None = None,
|
55
|
+
start_to_close_timeout: timedelta = timedelta(seconds=5),
|
56
|
+
heartbeat_timeout: timedelta = timedelta(seconds=5),
|
57
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
58
|
+
) -> AsyncGenerator[Span | None, None]:
|
59
|
+
"""
|
60
|
+
Async context manager for creating and automatically closing a span.
|
61
|
+
Yields the started span object. The span is automatically ended when the context exits.
|
62
|
+
|
63
|
+
If trace_id is falsy, acts as a no-op context manager.
|
64
|
+
|
65
|
+
Args:
|
66
|
+
trace_id (str): The trace ID for the span.
|
67
|
+
name (str): The name of the span.
|
68
|
+
input (Union[List, Dict, BaseModel]): The input for the span.
|
69
|
+
parent_id (Optional[str]): The parent span ID for the span.
|
70
|
+
data (Optional[Union[List, Dict, BaseModel]]): The data for the span.
|
71
|
+
start_to_close_timeout (timedelta): The start to close timeout for the span.
|
72
|
+
heartbeat_timeout (timedelta): The heartbeat timeout for the span.
|
73
|
+
retry_policy (RetryPolicy): The retry policy for the span.
|
74
|
+
|
75
|
+
Returns:
|
76
|
+
AsyncGenerator[Optional[Span], None]: An async generator that yields the started span object.
|
77
|
+
"""
|
78
|
+
if not trace_id:
|
79
|
+
yield None
|
80
|
+
return
|
81
|
+
|
82
|
+
span: Span | None = await self.start_span(
|
83
|
+
trace_id=trace_id,
|
84
|
+
name=name,
|
85
|
+
input=input,
|
86
|
+
parent_id=parent_id,
|
87
|
+
data=data,
|
88
|
+
start_to_close_timeout=start_to_close_timeout,
|
89
|
+
heartbeat_timeout=heartbeat_timeout,
|
90
|
+
retry_policy=retry_policy,
|
91
|
+
)
|
92
|
+
try:
|
93
|
+
yield span
|
94
|
+
finally:
|
95
|
+
if span:
|
96
|
+
await self.end_span(
|
97
|
+
trace_id=trace_id,
|
98
|
+
span=span,
|
99
|
+
start_to_close_timeout=start_to_close_timeout,
|
100
|
+
heartbeat_timeout=heartbeat_timeout,
|
101
|
+
retry_policy=retry_policy,
|
102
|
+
)
|
103
|
+
|
104
|
+
async def start_span(
|
105
|
+
self,
|
106
|
+
trace_id: str,
|
107
|
+
name: str,
|
108
|
+
input: list[Any] | dict[str, Any] | BaseModel | None = None,
|
109
|
+
parent_id: str | None = None,
|
110
|
+
data: list[Any] | dict[str, Any] | BaseModel | None = None,
|
111
|
+
start_to_close_timeout: timedelta = timedelta(seconds=5),
|
112
|
+
heartbeat_timeout: timedelta = timedelta(seconds=1),
|
113
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
114
|
+
) -> Span | None:
|
115
|
+
"""
|
116
|
+
Start a new span in the trace.
|
117
|
+
|
118
|
+
Args:
|
119
|
+
trace_id (str): The trace ID for the span.
|
120
|
+
name (str): The name of the span.
|
121
|
+
input (Union[List, Dict, BaseModel]): The input for the span.
|
122
|
+
parent_id (Optional[str]): The parent span ID for the span.
|
123
|
+
data (Optional[Union[List, Dict, BaseModel]]): The data for the span.
|
124
|
+
start_to_close_timeout (timedelta): The start to close timeout for the span.
|
125
|
+
heartbeat_timeout (timedelta): The heartbeat timeout for the span.
|
126
|
+
retry_policy (RetryPolicy): The retry policy for the span.
|
127
|
+
|
128
|
+
Returns:
|
129
|
+
Span: The started span object.
|
130
|
+
"""
|
131
|
+
params = StartSpanParams(
|
132
|
+
trace_id=trace_id,
|
133
|
+
parent_id=parent_id,
|
134
|
+
name=name,
|
135
|
+
input=input,
|
136
|
+
data=data,
|
137
|
+
)
|
138
|
+
if in_temporal_workflow():
|
139
|
+
return await ActivityHelpers.execute_activity(
|
140
|
+
activity_name=TracingActivityName.START_SPAN,
|
141
|
+
request=params,
|
142
|
+
response_type=Span,
|
143
|
+
start_to_close_timeout=start_to_close_timeout,
|
144
|
+
retry_policy=retry_policy,
|
145
|
+
heartbeat_timeout=heartbeat_timeout,
|
146
|
+
)
|
147
|
+
else:
|
148
|
+
return await self._tracing_service.start_span(
|
149
|
+
trace_id=trace_id,
|
150
|
+
name=name,
|
151
|
+
input=input,
|
152
|
+
parent_id=parent_id,
|
153
|
+
data=data,
|
154
|
+
)
|
155
|
+
|
156
|
+
async def end_span(
|
157
|
+
self,
|
158
|
+
trace_id: str,
|
159
|
+
span: Span,
|
160
|
+
start_to_close_timeout: timedelta = timedelta(seconds=5),
|
161
|
+
heartbeat_timeout: timedelta = timedelta(seconds=1),
|
162
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
163
|
+
) -> Span:
|
164
|
+
"""
|
165
|
+
End an existing span in the trace.
|
166
|
+
|
167
|
+
Args:
|
168
|
+
trace_id (str): The trace ID for the span.
|
169
|
+
span (Span): The span to end.
|
170
|
+
start_to_close_timeout (timedelta): The start to close timeout for the span.
|
171
|
+
heartbeat_timeout (timedelta): The heartbeat timeout for the span.
|
172
|
+
retry_policy (RetryPolicy): The retry policy for the span.
|
173
|
+
|
174
|
+
Returns:
|
175
|
+
Span: The ended span object.
|
176
|
+
"""
|
177
|
+
params = EndSpanParams(
|
178
|
+
trace_id=trace_id,
|
179
|
+
span=span,
|
180
|
+
)
|
181
|
+
if in_temporal_workflow():
|
182
|
+
return await ActivityHelpers.execute_activity(
|
183
|
+
activity_name=TracingActivityName.END_SPAN,
|
184
|
+
request=params,
|
185
|
+
response_type=Span,
|
186
|
+
start_to_close_timeout=start_to_close_timeout,
|
187
|
+
retry_policy=retry_policy,
|
188
|
+
heartbeat_timeout=heartbeat_timeout,
|
189
|
+
)
|
190
|
+
else:
|
191
|
+
return await self._tracing_service.end_span(
|
192
|
+
trace_id=trace_id,
|
193
|
+
span=span,
|
194
|
+
)
|
@@ -0,0 +1,9 @@
|
|
1
|
+
from agentex.lib.adk.providers._modules.litellm import LiteLLMModule
|
2
|
+
from agentex.lib.adk.providers._modules.openai import OpenAIModule
|
3
|
+
from agentex.lib.adk.providers._modules.sgp import SGPModule
|
4
|
+
|
5
|
+
openai = OpenAIModule()
|
6
|
+
litellm = LiteLLMModule()
|
7
|
+
sgp = SGPModule()
|
8
|
+
|
9
|
+
__all__ = ["openai", "litellm", "sgp"]
|
File without changes
|
@@ -0,0 +1,232 @@
|
|
1
|
+
from collections.abc import AsyncGenerator
|
2
|
+
from datetime import timedelta
|
3
|
+
|
4
|
+
from temporalio.common import RetryPolicy
|
5
|
+
|
6
|
+
from agentex import AsyncAgentex
|
7
|
+
from agentex.lib.core.adapters.llm.adapter_litellm import LiteLLMGateway
|
8
|
+
from agentex.lib.core.adapters.streams.adapter_redis import RedisEventStreamRepository
|
9
|
+
from agentex.lib.core.services.adk.providers.litellm import LiteLLMService
|
10
|
+
from agentex.lib.core.services.adk.streaming import StreamingService
|
11
|
+
from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
|
12
|
+
from agentex.lib.core.temporal.activities.adk.providers.litellm_activities import (
|
13
|
+
ChatCompletionAutoSendParams,
|
14
|
+
ChatCompletionParams,
|
15
|
+
ChatCompletionStreamAutoSendParams,
|
16
|
+
LiteLLMActivityName,
|
17
|
+
)
|
18
|
+
from agentex.lib.core.tracing.tracer import AsyncTracer
|
19
|
+
from agentex.lib.types.llm_messages import Completion, LLMConfig
|
20
|
+
from agentex.types.task_message import TaskMessage
|
21
|
+
from agentex.lib.utils.logging import make_logger
|
22
|
+
from agentex.lib.utils.temporal import in_temporal_workflow
|
23
|
+
|
24
|
+
logger = make_logger(__name__)
|
25
|
+
|
26
|
+
# Default retry policy for all LiteLLM operations
|
27
|
+
DEFAULT_RETRY_POLICY = RetryPolicy(maximum_attempts=1)
|
28
|
+
|
29
|
+
|
30
|
+
class LiteLLMModule:
|
31
|
+
"""
|
32
|
+
Module for managing LiteLLM agent operations in Agentex.
|
33
|
+
Provides high-level methods for chat completion, streaming, agentic streaming.
|
34
|
+
"""
|
35
|
+
|
36
|
+
def __init__(
|
37
|
+
self,
|
38
|
+
litellm_service: LiteLLMService | None = None,
|
39
|
+
):
|
40
|
+
if litellm_service is None:
|
41
|
+
# Create default service
|
42
|
+
agentex_client = AsyncAgentex()
|
43
|
+
stream_repository = RedisEventStreamRepository()
|
44
|
+
streaming_service = StreamingService(
|
45
|
+
agentex_client=agentex_client,
|
46
|
+
stream_repository=stream_repository,
|
47
|
+
)
|
48
|
+
litellm_gateway = LiteLLMGateway()
|
49
|
+
tracer = AsyncTracer(agentex_client)
|
50
|
+
self._litellm_service = LiteLLMService(
|
51
|
+
agentex_client=agentex_client,
|
52
|
+
llm_gateway=litellm_gateway,
|
53
|
+
streaming_service=streaming_service,
|
54
|
+
tracer=tracer,
|
55
|
+
)
|
56
|
+
else:
|
57
|
+
self._litellm_service = litellm_service
|
58
|
+
|
59
|
+
async def chat_completion(
|
60
|
+
self,
|
61
|
+
llm_config: LLMConfig,
|
62
|
+
trace_id: str | None = None,
|
63
|
+
parent_span_id: str | None = None,
|
64
|
+
start_to_close_timeout: timedelta = timedelta(seconds=120),
|
65
|
+
heartbeat_timeout: timedelta = timedelta(seconds=120),
|
66
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
67
|
+
) -> Completion:
|
68
|
+
"""
|
69
|
+
Perform a chat completion using LiteLLM.
|
70
|
+
|
71
|
+
Args:
|
72
|
+
llm_config (LLMConfig): The configuration for the LLM.
|
73
|
+
trace_id (Optional[str]): The trace ID for tracing.
|
74
|
+
parent_span_id (Optional[str]): The parent span ID for tracing.
|
75
|
+
start_to_close_timeout (timedelta): The start to close timeout.
|
76
|
+
heartbeat_timeout (timedelta): The heartbeat timeout.
|
77
|
+
retry_policy (RetryPolicy): The retry policy.
|
78
|
+
|
79
|
+
Returns:
|
80
|
+
Completion: An OpenAI compatible Completion object
|
81
|
+
"""
|
82
|
+
if in_temporal_workflow():
|
83
|
+
params = ChatCompletionParams(
|
84
|
+
trace_id=trace_id, parent_span_id=parent_span_id, llm_config=llm_config
|
85
|
+
)
|
86
|
+
return await ActivityHelpers.execute_activity(
|
87
|
+
activity_name=LiteLLMActivityName.CHAT_COMPLETION,
|
88
|
+
request=params,
|
89
|
+
response_type=Completion,
|
90
|
+
start_to_close_timeout=start_to_close_timeout,
|
91
|
+
heartbeat_timeout=heartbeat_timeout,
|
92
|
+
retry_policy=retry_policy,
|
93
|
+
)
|
94
|
+
else:
|
95
|
+
return await self._litellm_service.chat_completion(
|
96
|
+
llm_config=llm_config,
|
97
|
+
trace_id=trace_id,
|
98
|
+
parent_span_id=parent_span_id,
|
99
|
+
)
|
100
|
+
|
101
|
+
async def chat_completion_auto_send(
|
102
|
+
self,
|
103
|
+
task_id: str,
|
104
|
+
llm_config: LLMConfig,
|
105
|
+
trace_id: str | None = None,
|
106
|
+
parent_span_id: str | None = None,
|
107
|
+
start_to_close_timeout: timedelta = timedelta(seconds=120),
|
108
|
+
heartbeat_timeout: timedelta = timedelta(seconds=120),
|
109
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
110
|
+
) -> TaskMessage | None:
|
111
|
+
"""
|
112
|
+
Chat completion with automatic TaskMessage creation.
|
113
|
+
|
114
|
+
Args:
|
115
|
+
task_id (str): The ID of the task.
|
116
|
+
llm_config (LLMConfig): The configuration for the LLM (must have stream=False).
|
117
|
+
trace_id (Optional[str]): The trace ID for tracing.
|
118
|
+
parent_span_id (Optional[str]): The parent span ID for tracing.
|
119
|
+
start_to_close_timeout (timedelta): The start to close timeout.
|
120
|
+
heartbeat_timeout (timedelta): The heartbeat timeout.
|
121
|
+
retry_policy (RetryPolicy): The retry policy.
|
122
|
+
|
123
|
+
Returns:
|
124
|
+
TaskMessage: The final TaskMessage
|
125
|
+
"""
|
126
|
+
if in_temporal_workflow():
|
127
|
+
# Use streaming activity with stream=False for non-streaming auto-send
|
128
|
+
params = ChatCompletionAutoSendParams(
|
129
|
+
trace_id=trace_id,
|
130
|
+
parent_span_id=parent_span_id,
|
131
|
+
task_id=task_id,
|
132
|
+
llm_config=llm_config,
|
133
|
+
)
|
134
|
+
return await ActivityHelpers.execute_activity(
|
135
|
+
activity_name=LiteLLMActivityName.CHAT_COMPLETION_AUTO_SEND,
|
136
|
+
request=params,
|
137
|
+
response_type=TaskMessage,
|
138
|
+
start_to_close_timeout=start_to_close_timeout,
|
139
|
+
heartbeat_timeout=heartbeat_timeout,
|
140
|
+
retry_policy=retry_policy,
|
141
|
+
)
|
142
|
+
else:
|
143
|
+
return await self._litellm_service.chat_completion_auto_send(
|
144
|
+
task_id=task_id,
|
145
|
+
llm_config=llm_config,
|
146
|
+
trace_id=trace_id,
|
147
|
+
parent_span_id=parent_span_id,
|
148
|
+
)
|
149
|
+
|
150
|
+
async def chat_completion_stream(
|
151
|
+
self,
|
152
|
+
llm_config: LLMConfig,
|
153
|
+
trace_id: str | None = None,
|
154
|
+
parent_span_id: str | None = None,
|
155
|
+
) -> AsyncGenerator[Completion, None]:
|
156
|
+
"""
|
157
|
+
Stream chat completion chunks using LiteLLM.
|
158
|
+
|
159
|
+
DEFAULT: Returns raw streaming chunks for manual handling.
|
160
|
+
|
161
|
+
NOTE: This method does NOT work in Temporal workflows!
|
162
|
+
Temporal activities cannot return generators. Use chat_completion_stream_auto_send() instead.
|
163
|
+
|
164
|
+
Args:
|
165
|
+
llm_config (LLMConfig): The configuration for the LLM (must have stream=True).
|
166
|
+
trace_id (Optional[str]): The trace ID for tracing.
|
167
|
+
parent_span_id (Optional[str]): The parent span ID for tracing.
|
168
|
+
start_to_close_timeout (timedelta): The start to close timeout.
|
169
|
+
heartbeat_timeout (timedelta): The heartbeat timeout.
|
170
|
+
retry_policy (RetryPolicy): The retry policy.
|
171
|
+
|
172
|
+
Returns:
|
173
|
+
AsyncGenerator[Completion, None]: Generator yielding completion chunks
|
174
|
+
|
175
|
+
Raises:
|
176
|
+
ValueError: If called from within a Temporal workflow
|
177
|
+
"""
|
178
|
+
# Delegate to service - it handles temporal workflow checks
|
179
|
+
async for chunk in self._litellm_service.chat_completion_stream(
|
180
|
+
llm_config=llm_config,
|
181
|
+
trace_id=trace_id,
|
182
|
+
parent_span_id=parent_span_id,
|
183
|
+
):
|
184
|
+
yield chunk
|
185
|
+
|
186
|
+
async def chat_completion_stream_auto_send(
|
187
|
+
self,
|
188
|
+
task_id: str,
|
189
|
+
llm_config: LLMConfig,
|
190
|
+
trace_id: str | None = None,
|
191
|
+
parent_span_id: str | None = None,
|
192
|
+
start_to_close_timeout: timedelta = timedelta(seconds=120),
|
193
|
+
heartbeat_timeout: timedelta = timedelta(seconds=120),
|
194
|
+
retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
195
|
+
) -> TaskMessage | None:
|
196
|
+
"""
|
197
|
+
Stream chat completion with automatic TaskMessage creation and streaming.
|
198
|
+
|
199
|
+
Args:
|
200
|
+
task_id (str): The ID of the task to run the agent for.
|
201
|
+
llm_config (LLMConfig): The configuration for the LLM (must have stream=True).
|
202
|
+
trace_id (Optional[str]): The trace ID for tracing.
|
203
|
+
parent_span_id (Optional[str]): The parent span ID for tracing.
|
204
|
+
start_to_close_timeout (timedelta): The start to close timeout.
|
205
|
+
heartbeat_timeout (timedelta): The heartbeat timeout.
|
206
|
+
retry_policy (RetryPolicy): The retry policy.
|
207
|
+
|
208
|
+
Returns:
|
209
|
+
TaskMessage: The final TaskMessage after streaming is complete
|
210
|
+
"""
|
211
|
+
if in_temporal_workflow():
|
212
|
+
params = ChatCompletionStreamAutoSendParams(
|
213
|
+
trace_id=trace_id,
|
214
|
+
parent_span_id=parent_span_id,
|
215
|
+
task_id=task_id,
|
216
|
+
llm_config=llm_config,
|
217
|
+
)
|
218
|
+
return await ActivityHelpers.execute_activity(
|
219
|
+
activity_name=LiteLLMActivityName.CHAT_COMPLETION_STREAM_AUTO_SEND,
|
220
|
+
request=params,
|
221
|
+
response_type=TaskMessage,
|
222
|
+
start_to_close_timeout=start_to_close_timeout,
|
223
|
+
heartbeat_timeout=heartbeat_timeout,
|
224
|
+
retry_policy=retry_policy,
|
225
|
+
)
|
226
|
+
else:
|
227
|
+
return await self._litellm_service.chat_completion_stream_auto_send(
|
228
|
+
task_id=task_id,
|
229
|
+
llm_config=llm_config,
|
230
|
+
trace_id=trace_id,
|
231
|
+
parent_span_id=parent_span_id,
|
232
|
+
)
|