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.
Files changed (289) hide show
  1. agentex/__init__.py +103 -0
  2. agentex/_base_client.py +1992 -0
  3. agentex/_client.py +506 -0
  4. agentex/_compat.py +219 -0
  5. agentex/_constants.py +14 -0
  6. agentex/_exceptions.py +108 -0
  7. agentex/_files.py +123 -0
  8. agentex/_models.py +829 -0
  9. agentex/_qs.py +150 -0
  10. agentex/_resource.py +43 -0
  11. agentex/_response.py +830 -0
  12. agentex/_streaming.py +333 -0
  13. agentex/_types.py +219 -0
  14. agentex/_utils/__init__.py +57 -0
  15. agentex/_utils/_logs.py +25 -0
  16. agentex/_utils/_proxy.py +65 -0
  17. agentex/_utils/_reflection.py +42 -0
  18. agentex/_utils/_resources_proxy.py +24 -0
  19. agentex/_utils/_streams.py +12 -0
  20. agentex/_utils/_sync.py +86 -0
  21. agentex/_utils/_transform.py +447 -0
  22. agentex/_utils/_typing.py +151 -0
  23. agentex/_utils/_utils.py +422 -0
  24. agentex/_version.py +4 -0
  25. agentex/lib/.keep +4 -0
  26. agentex/lib/__init__.py +0 -0
  27. agentex/lib/adk/__init__.py +41 -0
  28. agentex/lib/adk/_modules/__init__.py +0 -0
  29. agentex/lib/adk/_modules/acp.py +247 -0
  30. agentex/lib/adk/_modules/agent_task_tracker.py +176 -0
  31. agentex/lib/adk/_modules/agents.py +77 -0
  32. agentex/lib/adk/_modules/events.py +141 -0
  33. agentex/lib/adk/_modules/messages.py +285 -0
  34. agentex/lib/adk/_modules/state.py +291 -0
  35. agentex/lib/adk/_modules/streaming.py +75 -0
  36. agentex/lib/adk/_modules/tasks.py +124 -0
  37. agentex/lib/adk/_modules/tracing.py +194 -0
  38. agentex/lib/adk/providers/__init__.py +9 -0
  39. agentex/lib/adk/providers/_modules/__init__.py +0 -0
  40. agentex/lib/adk/providers/_modules/litellm.py +232 -0
  41. agentex/lib/adk/providers/_modules/openai.py +416 -0
  42. agentex/lib/adk/providers/_modules/sgp.py +85 -0
  43. agentex/lib/adk/utils/__init__.py +5 -0
  44. agentex/lib/adk/utils/_modules/__init__.py +0 -0
  45. agentex/lib/adk/utils/_modules/templating.py +94 -0
  46. agentex/lib/cli/__init__.py +0 -0
  47. agentex/lib/cli/commands/__init__.py +0 -0
  48. agentex/lib/cli/commands/agents.py +328 -0
  49. agentex/lib/cli/commands/init.py +227 -0
  50. agentex/lib/cli/commands/main.py +33 -0
  51. agentex/lib/cli/commands/secrets.py +169 -0
  52. agentex/lib/cli/commands/tasks.py +118 -0
  53. agentex/lib/cli/commands/uv.py +133 -0
  54. agentex/lib/cli/handlers/__init__.py +0 -0
  55. agentex/lib/cli/handlers/agent_handlers.py +160 -0
  56. agentex/lib/cli/handlers/cleanup_handlers.py +186 -0
  57. agentex/lib/cli/handlers/deploy_handlers.py +351 -0
  58. agentex/lib/cli/handlers/run_handlers.py +452 -0
  59. agentex/lib/cli/handlers/secret_handlers.py +670 -0
  60. agentex/lib/cli/templates/default/.dockerignore.j2 +43 -0
  61. agentex/lib/cli/templates/default/Dockerfile-uv.j2 +42 -0
  62. agentex/lib/cli/templates/default/Dockerfile.j2 +42 -0
  63. agentex/lib/cli/templates/default/README.md.j2 +193 -0
  64. agentex/lib/cli/templates/default/deploy/example.yaml.j2 +55 -0
  65. agentex/lib/cli/templates/default/manifest.yaml.j2 +116 -0
  66. agentex/lib/cli/templates/default/project/acp.py.j2 +29 -0
  67. agentex/lib/cli/templates/default/pyproject.toml.j2 +33 -0
  68. agentex/lib/cli/templates/default/requirements.txt.j2 +5 -0
  69. agentex/lib/cli/templates/deploy/Screenshot 2025-03-19 at 10.36.57/342/200/257AM.png +0 -0
  70. agentex/lib/cli/templates/deploy/example.yaml.j2 +55 -0
  71. agentex/lib/cli/templates/sync/.dockerignore.j2 +43 -0
  72. agentex/lib/cli/templates/sync/Dockerfile-uv.j2 +42 -0
  73. agentex/lib/cli/templates/sync/Dockerfile.j2 +42 -0
  74. agentex/lib/cli/templates/sync/README.md.j2 +293 -0
  75. agentex/lib/cli/templates/sync/deploy/example.yaml.j2 +55 -0
  76. agentex/lib/cli/templates/sync/manifest.yaml.j2 +116 -0
  77. agentex/lib/cli/templates/sync/project/acp.py.j2 +26 -0
  78. agentex/lib/cli/templates/sync/pyproject.toml.j2 +33 -0
  79. agentex/lib/cli/templates/sync/requirements.txt.j2 +5 -0
  80. agentex/lib/cli/templates/temporal/.dockerignore.j2 +43 -0
  81. agentex/lib/cli/templates/temporal/Dockerfile-uv.j2 +48 -0
  82. agentex/lib/cli/templates/temporal/Dockerfile.j2 +48 -0
  83. agentex/lib/cli/templates/temporal/README.md.j2 +316 -0
  84. agentex/lib/cli/templates/temporal/deploy/example.yaml.j2 +55 -0
  85. agentex/lib/cli/templates/temporal/manifest.yaml.j2 +137 -0
  86. agentex/lib/cli/templates/temporal/project/acp.py.j2 +30 -0
  87. agentex/lib/cli/templates/temporal/project/run_worker.py.j2 +33 -0
  88. agentex/lib/cli/templates/temporal/project/workflow.py.j2 +66 -0
  89. agentex/lib/cli/templates/temporal/pyproject.toml.j2 +34 -0
  90. agentex/lib/cli/templates/temporal/requirements.txt.j2 +5 -0
  91. agentex/lib/cli/utils/cli_utils.py +14 -0
  92. agentex/lib/cli/utils/credential_utils.py +103 -0
  93. agentex/lib/cli/utils/exceptions.py +6 -0
  94. agentex/lib/cli/utils/kubectl_utils.py +135 -0
  95. agentex/lib/cli/utils/kubernetes_secrets_utils.py +185 -0
  96. agentex/lib/core/__init__.py +0 -0
  97. agentex/lib/core/adapters/__init__.py +0 -0
  98. agentex/lib/core/adapters/llm/__init__.py +1 -0
  99. agentex/lib/core/adapters/llm/adapter_litellm.py +46 -0
  100. agentex/lib/core/adapters/llm/adapter_sgp.py +55 -0
  101. agentex/lib/core/adapters/llm/port.py +24 -0
  102. agentex/lib/core/adapters/streams/adapter_redis.py +128 -0
  103. agentex/lib/core/adapters/streams/port.py +50 -0
  104. agentex/lib/core/clients/__init__.py +1 -0
  105. agentex/lib/core/clients/temporal/__init__.py +0 -0
  106. agentex/lib/core/clients/temporal/temporal_client.py +181 -0
  107. agentex/lib/core/clients/temporal/types.py +47 -0
  108. agentex/lib/core/clients/temporal/utils.py +56 -0
  109. agentex/lib/core/services/__init__.py +0 -0
  110. agentex/lib/core/services/adk/__init__.py +0 -0
  111. agentex/lib/core/services/adk/acp/__init__.py +0 -0
  112. agentex/lib/core/services/adk/acp/acp.py +210 -0
  113. agentex/lib/core/services/adk/agent_task_tracker.py +85 -0
  114. agentex/lib/core/services/adk/agents.py +43 -0
  115. agentex/lib/core/services/adk/events.py +61 -0
  116. agentex/lib/core/services/adk/messages.py +164 -0
  117. agentex/lib/core/services/adk/providers/__init__.py +0 -0
  118. agentex/lib/core/services/adk/providers/litellm.py +256 -0
  119. agentex/lib/core/services/adk/providers/openai.py +723 -0
  120. agentex/lib/core/services/adk/providers/sgp.py +99 -0
  121. agentex/lib/core/services/adk/state.py +120 -0
  122. agentex/lib/core/services/adk/streaming.py +262 -0
  123. agentex/lib/core/services/adk/tasks.py +69 -0
  124. agentex/lib/core/services/adk/tracing.py +36 -0
  125. agentex/lib/core/services/adk/utils/__init__.py +0 -0
  126. agentex/lib/core/services/adk/utils/templating.py +58 -0
  127. agentex/lib/core/temporal/__init__.py +0 -0
  128. agentex/lib/core/temporal/activities/__init__.py +207 -0
  129. agentex/lib/core/temporal/activities/activity_helpers.py +37 -0
  130. agentex/lib/core/temporal/activities/adk/__init__.py +0 -0
  131. agentex/lib/core/temporal/activities/adk/acp/__init__.py +0 -0
  132. agentex/lib/core/temporal/activities/adk/acp/acp_activities.py +86 -0
  133. agentex/lib/core/temporal/activities/adk/agent_task_tracker_activities.py +76 -0
  134. agentex/lib/core/temporal/activities/adk/agents_activities.py +35 -0
  135. agentex/lib/core/temporal/activities/adk/events_activities.py +50 -0
  136. agentex/lib/core/temporal/activities/adk/messages_activities.py +94 -0
  137. agentex/lib/core/temporal/activities/adk/providers/__init__.py +0 -0
  138. agentex/lib/core/temporal/activities/adk/providers/litellm_activities.py +71 -0
  139. agentex/lib/core/temporal/activities/adk/providers/openai_activities.py +210 -0
  140. agentex/lib/core/temporal/activities/adk/providers/sgp_activities.py +42 -0
  141. agentex/lib/core/temporal/activities/adk/state_activities.py +85 -0
  142. agentex/lib/core/temporal/activities/adk/streaming_activities.py +33 -0
  143. agentex/lib/core/temporal/activities/adk/tasks_activities.py +48 -0
  144. agentex/lib/core/temporal/activities/adk/tracing_activities.py +55 -0
  145. agentex/lib/core/temporal/activities/adk/utils/__init__.py +0 -0
  146. agentex/lib/core/temporal/activities/adk/utils/templating_activities.py +41 -0
  147. agentex/lib/core/temporal/services/__init__.py +0 -0
  148. agentex/lib/core/temporal/services/temporal_task_service.py +69 -0
  149. agentex/lib/core/temporal/types/__init__.py +0 -0
  150. agentex/lib/core/temporal/types/workflow.py +5 -0
  151. agentex/lib/core/temporal/workers/__init__.py +0 -0
  152. agentex/lib/core/temporal/workers/worker.py +162 -0
  153. agentex/lib/core/temporal/workflows/workflow.py +26 -0
  154. agentex/lib/core/tracing/__init__.py +5 -0
  155. agentex/lib/core/tracing/processors/agentex_tracing_processor.py +117 -0
  156. agentex/lib/core/tracing/processors/sgp_tracing_processor.py +119 -0
  157. agentex/lib/core/tracing/processors/tracing_processor_interface.py +40 -0
  158. agentex/lib/core/tracing/trace.py +311 -0
  159. agentex/lib/core/tracing/tracer.py +70 -0
  160. agentex/lib/core/tracing/tracing_processor_manager.py +62 -0
  161. agentex/lib/environment_variables.py +87 -0
  162. agentex/lib/py.typed +0 -0
  163. agentex/lib/sdk/__init__.py +0 -0
  164. agentex/lib/sdk/config/__init__.py +0 -0
  165. agentex/lib/sdk/config/agent_config.py +61 -0
  166. agentex/lib/sdk/config/agent_manifest.py +219 -0
  167. agentex/lib/sdk/config/build_config.py +35 -0
  168. agentex/lib/sdk/config/deployment_config.py +117 -0
  169. agentex/lib/sdk/config/local_development_config.py +56 -0
  170. agentex/lib/sdk/config/project_config.py +103 -0
  171. agentex/lib/sdk/fastacp/__init__.py +3 -0
  172. agentex/lib/sdk/fastacp/base/base_acp_server.py +406 -0
  173. agentex/lib/sdk/fastacp/fastacp.py +74 -0
  174. agentex/lib/sdk/fastacp/impl/agentic_base_acp.py +72 -0
  175. agentex/lib/sdk/fastacp/impl/sync_acp.py +109 -0
  176. agentex/lib/sdk/fastacp/impl/temporal_acp.py +97 -0
  177. agentex/lib/sdk/fastacp/tests/README.md +297 -0
  178. agentex/lib/sdk/fastacp/tests/conftest.py +307 -0
  179. agentex/lib/sdk/fastacp/tests/pytest.ini +10 -0
  180. agentex/lib/sdk/fastacp/tests/run_tests.py +227 -0
  181. agentex/lib/sdk/fastacp/tests/test_base_acp_server.py +450 -0
  182. agentex/lib/sdk/fastacp/tests/test_fastacp_factory.py +344 -0
  183. agentex/lib/sdk/fastacp/tests/test_integration.py +477 -0
  184. agentex/lib/sdk/state_machine/__init__.py +6 -0
  185. agentex/lib/sdk/state_machine/noop_workflow.py +21 -0
  186. agentex/lib/sdk/state_machine/state.py +10 -0
  187. agentex/lib/sdk/state_machine/state_machine.py +189 -0
  188. agentex/lib/sdk/state_machine/state_workflow.py +16 -0
  189. agentex/lib/sdk/utils/__init__.py +0 -0
  190. agentex/lib/sdk/utils/messages.py +223 -0
  191. agentex/lib/types/__init__.py +0 -0
  192. agentex/lib/types/acp.py +94 -0
  193. agentex/lib/types/agent_configs.py +79 -0
  194. agentex/lib/types/agent_results.py +29 -0
  195. agentex/lib/types/credentials.py +34 -0
  196. agentex/lib/types/fastacp.py +61 -0
  197. agentex/lib/types/files.py +13 -0
  198. agentex/lib/types/json_rpc.py +49 -0
  199. agentex/lib/types/llm_messages.py +354 -0
  200. agentex/lib/types/task_message_updates.py +171 -0
  201. agentex/lib/types/tracing.py +34 -0
  202. agentex/lib/utils/__init__.py +0 -0
  203. agentex/lib/utils/completions.py +131 -0
  204. agentex/lib/utils/console.py +14 -0
  205. agentex/lib/utils/io.py +29 -0
  206. agentex/lib/utils/iterables.py +14 -0
  207. agentex/lib/utils/json_schema.py +23 -0
  208. agentex/lib/utils/logging.py +31 -0
  209. agentex/lib/utils/mcp.py +17 -0
  210. agentex/lib/utils/model_utils.py +46 -0
  211. agentex/lib/utils/parsing.py +15 -0
  212. agentex/lib/utils/regex.py +6 -0
  213. agentex/lib/utils/temporal.py +13 -0
  214. agentex/py.typed +0 -0
  215. agentex/resources/__init__.py +103 -0
  216. agentex/resources/agents.py +707 -0
  217. agentex/resources/events.py +294 -0
  218. agentex/resources/messages/__init__.py +33 -0
  219. agentex/resources/messages/batch.py +271 -0
  220. agentex/resources/messages/messages.py +492 -0
  221. agentex/resources/spans.py +557 -0
  222. agentex/resources/states.py +544 -0
  223. agentex/resources/tasks.py +615 -0
  224. agentex/resources/tracker.py +384 -0
  225. agentex/types/__init__.py +56 -0
  226. agentex/types/acp_type.py +7 -0
  227. agentex/types/agent.py +29 -0
  228. agentex/types/agent_list_params.py +13 -0
  229. agentex/types/agent_list_response.py +10 -0
  230. agentex/types/agent_rpc_by_name_params.py +21 -0
  231. agentex/types/agent_rpc_params.py +51 -0
  232. agentex/types/agent_rpc_params1.py +21 -0
  233. agentex/types/agent_rpc_response.py +20 -0
  234. agentex/types/agent_rpc_result.py +90 -0
  235. agentex/types/agent_task_tracker.py +34 -0
  236. agentex/types/data_content.py +30 -0
  237. agentex/types/data_content_param.py +31 -0
  238. agentex/types/data_delta.py +14 -0
  239. agentex/types/event.py +29 -0
  240. agentex/types/event_list_params.py +22 -0
  241. agentex/types/event_list_response.py +10 -0
  242. agentex/types/message_author.py +7 -0
  243. agentex/types/message_create_params.py +18 -0
  244. agentex/types/message_list_params.py +14 -0
  245. agentex/types/message_list_response.py +10 -0
  246. agentex/types/message_style.py +7 -0
  247. agentex/types/message_update_params.py +18 -0
  248. agentex/types/messages/__init__.py +8 -0
  249. agentex/types/messages/batch_create_params.py +16 -0
  250. agentex/types/messages/batch_create_response.py +10 -0
  251. agentex/types/messages/batch_update_params.py +16 -0
  252. agentex/types/messages/batch_update_response.py +10 -0
  253. agentex/types/shared/__init__.py +3 -0
  254. agentex/types/shared/task_message_update.py +83 -0
  255. agentex/types/span.py +36 -0
  256. agentex/types/span_create_params.py +40 -0
  257. agentex/types/span_list_params.py +12 -0
  258. agentex/types/span_list_response.py +10 -0
  259. agentex/types/span_update_params.py +37 -0
  260. agentex/types/state.py +25 -0
  261. agentex/types/state_create_params.py +16 -0
  262. agentex/types/state_list_params.py +16 -0
  263. agentex/types/state_list_response.py +10 -0
  264. agentex/types/state_update_params.py +16 -0
  265. agentex/types/task.py +23 -0
  266. agentex/types/task_delete_by_name_response.py +8 -0
  267. agentex/types/task_delete_response.py +8 -0
  268. agentex/types/task_list_response.py +10 -0
  269. agentex/types/task_message.py +33 -0
  270. agentex/types/task_message_content.py +16 -0
  271. agentex/types/task_message_content_param.py +17 -0
  272. agentex/types/task_message_delta.py +16 -0
  273. agentex/types/text_content.py +53 -0
  274. agentex/types/text_content_param.py +54 -0
  275. agentex/types/text_delta.py +14 -0
  276. agentex/types/tool_request_content.py +36 -0
  277. agentex/types/tool_request_content_param.py +37 -0
  278. agentex/types/tool_request_delta.py +18 -0
  279. agentex/types/tool_response_content.py +36 -0
  280. agentex/types/tool_response_content_param.py +36 -0
  281. agentex/types/tool_response_delta.py +18 -0
  282. agentex/types/tracker_list_params.py +16 -0
  283. agentex/types/tracker_list_response.py +10 -0
  284. agentex/types/tracker_update_params.py +19 -0
  285. agentex_sdk-0.1.0a6.dist-info/METADATA +426 -0
  286. agentex_sdk-0.1.0a6.dist-info/RECORD +289 -0
  287. agentex_sdk-0.1.0a6.dist-info/WHEEL +4 -0
  288. agentex_sdk-0.1.0a6.dist-info/entry_points.txt +2 -0
  289. agentex_sdk-0.1.0a6.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,416 @@
1
+ from datetime import timedelta
2
+ from typing import Any, Literal
3
+
4
+ from agents import Agent, RunResult, RunResultStreaming
5
+ from agents.agent import StopAtTools, ToolsToFinalOutputFunction
6
+ from agents.agent_output import AgentOutputSchemaBase
7
+ from agents.model_settings import ModelSettings
8
+ from agents.tool import Tool
9
+ from mcp import StdioServerParameters
10
+ from temporalio.common import RetryPolicy
11
+
12
+ from agentex import AsyncAgentex
13
+ from agentex.lib.core.adapters.streams.adapter_redis import RedisEventStreamRepository
14
+ from agentex.lib.core.services.adk.providers.openai import OpenAIService
15
+ from agentex.lib.core.services.adk.streaming import StreamingService
16
+ from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
17
+ from agentex.lib.core.temporal.activities.adk.providers.openai_activities import (
18
+ OpenAIActivityName,
19
+ RunAgentAutoSendParams,
20
+ RunAgentParams,
21
+ RunAgentStreamedAutoSendParams,
22
+ )
23
+ from agentex.lib.core.tracing.tracer import AsyncTracer
24
+ from agentex.lib.types.agent_results import (
25
+ SerializableRunResult,
26
+ SerializableRunResultStreaming,
27
+ )
28
+ from agentex.lib.utils.logging import make_logger
29
+ from agentex.lib.utils.temporal import in_temporal_workflow
30
+
31
+ logger = make_logger(__name__)
32
+
33
+ # Default retry policy for all OpenAI operations
34
+ DEFAULT_RETRY_POLICY = RetryPolicy(maximum_attempts=1)
35
+
36
+
37
+ class OpenAIModule:
38
+ """
39
+ Module for managing OpenAI agent operations in Agentex.
40
+ Provides high-level methods for running agents with and without streaming.
41
+ """
42
+
43
+ def __init__(
44
+ self,
45
+ openai_service: OpenAIService | None = None,
46
+ ):
47
+ if openai_service is None:
48
+ # Create default service
49
+ agentex_client = AsyncAgentex()
50
+ stream_repository = RedisEventStreamRepository()
51
+ streaming_service = StreamingService(
52
+ agentex_client=agentex_client,
53
+ stream_repository=stream_repository,
54
+ )
55
+ tracer = AsyncTracer(agentex_client)
56
+ self._openai_service = OpenAIService(
57
+ agentex_client=agentex_client,
58
+ streaming_service=streaming_service,
59
+ tracer=tracer,
60
+ )
61
+ else:
62
+ self._openai_service = openai_service
63
+
64
+ async def run_agent(
65
+ self,
66
+ input_list: list[dict[str, Any]],
67
+ mcp_server_params: list[StdioServerParameters],
68
+ agent_name: str,
69
+ agent_instructions: str,
70
+ trace_id: str | None = None,
71
+ parent_span_id: str | None = None,
72
+ start_to_close_timeout: timedelta = timedelta(seconds=600),
73
+ heartbeat_timeout: timedelta = timedelta(seconds=600),
74
+ retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
75
+ handoff_description: str | None = None,
76
+ handoffs: list[Agent] | None = None,
77
+ model: str | None = None,
78
+ model_settings: ModelSettings | None = None,
79
+ tools: list[Tool] | None = None,
80
+ output_type: type[Any] | AgentOutputSchemaBase | None = None,
81
+ tool_use_behavior: (
82
+ Literal["run_llm_again", "stop_on_first_tool"]
83
+ | StopAtTools
84
+ | ToolsToFinalOutputFunction
85
+ ) = "run_llm_again",
86
+ ) -> SerializableRunResult | RunResult:
87
+ """
88
+ Run an agent without streaming or TaskMessage creation.
89
+
90
+ DEFAULT: No TaskMessage creation, returns only the result.
91
+
92
+ Args:
93
+ input_list: List of input data for the agent.
94
+ mcp_server_params: MCP server parameters for the agent.
95
+ agent_name: The name of the agent to run.
96
+ agent_instructions: Instructions for the agent.
97
+ trace_id: Optional trace ID for tracing.
98
+ parent_span_id: Optional parent span for tracing.
99
+ start_to_close_timeout: Maximum time allowed for the operation.
100
+ heartbeat_timeout: Maximum time between heartbeats.
101
+ retry_policy: Policy for retrying failed operations.
102
+ handoff_description: Optional description of the handoff.
103
+ handoffs: Optional list of handoffs.
104
+ model: Optional model to use.
105
+ model_settings: Optional model settings.
106
+ tools: Optional list of tools.
107
+ output_type: Optional output type.
108
+ tool_use_behavior: Optional tool use behavior.
109
+
110
+ Returns:
111
+ Union[SerializableRunResult, RunResult]: SerializableRunResult when in Temporal, RunResult otherwise.
112
+ """
113
+ if in_temporal_workflow():
114
+ params = RunAgentParams(
115
+ trace_id=trace_id,
116
+ parent_span_id=parent_span_id,
117
+ input_list=input_list,
118
+ mcp_server_params=mcp_server_params,
119
+ agent_name=agent_name,
120
+ agent_instructions=agent_instructions,
121
+ handoff_description=handoff_description,
122
+ handoffs=handoffs,
123
+ model=model,
124
+ model_settings=model_settings,
125
+ tools=tools,
126
+ output_type=output_type,
127
+ tool_use_behavior=tool_use_behavior,
128
+ )
129
+ return await ActivityHelpers.execute_activity(
130
+ activity_name=OpenAIActivityName.RUN_AGENT,
131
+ request=params,
132
+ response_type=SerializableRunResult,
133
+ start_to_close_timeout=start_to_close_timeout,
134
+ heartbeat_timeout=heartbeat_timeout,
135
+ retry_policy=retry_policy,
136
+ )
137
+ else:
138
+ return await self._openai_service.run_agent(
139
+ input_list=input_list,
140
+ mcp_server_params=mcp_server_params,
141
+ agent_name=agent_name,
142
+ agent_instructions=agent_instructions,
143
+ trace_id=trace_id,
144
+ parent_span_id=parent_span_id,
145
+ handoff_description=handoff_description,
146
+ handoffs=handoffs,
147
+ model=model,
148
+ model_settings=model_settings,
149
+ tools=tools,
150
+ output_type=output_type,
151
+ tool_use_behavior=tool_use_behavior,
152
+ )
153
+
154
+ async def run_agent_auto_send(
155
+ self,
156
+ task_id: str,
157
+ input_list: list[dict[str, Any]],
158
+ mcp_server_params: list[StdioServerParameters],
159
+ agent_name: str,
160
+ agent_instructions: str,
161
+ trace_id: str | None = None,
162
+ parent_span_id: str | None = None,
163
+ start_to_close_timeout: timedelta = timedelta(seconds=600),
164
+ heartbeat_timeout: timedelta = timedelta(seconds=600),
165
+ retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
166
+ handoff_description: str | None = None,
167
+ handoffs: list[Agent] | None = None,
168
+ model: str | None = None,
169
+ model_settings: ModelSettings | None = None,
170
+ tools: list[Tool] | None = None,
171
+ output_type: type[Any] | AgentOutputSchemaBase | None = None,
172
+ tool_use_behavior: (
173
+ Literal["run_llm_again", "stop_on_first_tool"]
174
+ | StopAtTools
175
+ | ToolsToFinalOutputFunction
176
+ ) = "run_llm_again",
177
+ ) -> SerializableRunResult | RunResult:
178
+ """
179
+ Run an agent with automatic TaskMessage creation.
180
+
181
+ Args:
182
+ task_id: The ID of the task to run the agent for.
183
+ input_list: List of input data for the agent.
184
+ mcp_server_params: MCP server parameters for the agent.
185
+ agent_name: The name of the agent to run.
186
+ agent_instructions: Instructions for the agent.
187
+ trace_id: Optional trace ID for tracing.
188
+ parent_span_id: Optional parent span for tracing.
189
+ start_to_close_timeout: Maximum time allowed for the operation.
190
+ heartbeat_timeout: Maximum time between heartbeats.
191
+ retry_policy: Policy for retrying failed operations.
192
+ handoff_description: Optional description of the handoff.
193
+ handoffs: Optional list of handoffs.
194
+ model: Optional model to use.
195
+ model_settings: Optional model settings.
196
+ tools: Optional list of tools.
197
+ output_type: Optional output type.
198
+ tool_use_behavior: Optional tool use behavior.
199
+
200
+ Returns:
201
+ Union[SerializableRunResult, RunResult]: SerializableRunResult when in Temporal, RunResult otherwise.
202
+ """
203
+ if in_temporal_workflow():
204
+ params = RunAgentAutoSendParams(
205
+ trace_id=trace_id,
206
+ parent_span_id=parent_span_id,
207
+ task_id=task_id,
208
+ input_list=input_list,
209
+ mcp_server_params=mcp_server_params,
210
+ agent_name=agent_name,
211
+ agent_instructions=agent_instructions,
212
+ handoff_description=handoff_description,
213
+ handoffs=handoffs,
214
+ model=model,
215
+ model_settings=model_settings,
216
+ tools=tools,
217
+ output_type=output_type,
218
+ tool_use_behavior=tool_use_behavior,
219
+ )
220
+ return await ActivityHelpers.execute_activity(
221
+ activity_name=OpenAIActivityName.RUN_AGENT_AUTO_SEND,
222
+ request=params,
223
+ response_type=SerializableRunResult,
224
+ start_to_close_timeout=start_to_close_timeout,
225
+ heartbeat_timeout=heartbeat_timeout,
226
+ retry_policy=retry_policy,
227
+ )
228
+ else:
229
+ return await self._openai_service.run_agent_auto_send(
230
+ task_id=task_id,
231
+ input_list=input_list,
232
+ mcp_server_params=mcp_server_params,
233
+ agent_name=agent_name,
234
+ agent_instructions=agent_instructions,
235
+ trace_id=trace_id,
236
+ parent_span_id=parent_span_id,
237
+ handoff_description=handoff_description,
238
+ handoffs=handoffs,
239
+ model=model,
240
+ model_settings=model_settings,
241
+ tools=tools,
242
+ output_type=output_type,
243
+ tool_use_behavior=tool_use_behavior,
244
+ )
245
+
246
+ async def run_agent_streamed(
247
+ self,
248
+ input_list: list[dict[str, Any]],
249
+ mcp_server_params: list[StdioServerParameters],
250
+ agent_name: str,
251
+ agent_instructions: str,
252
+ trace_id: str | None = None,
253
+ parent_span_id: str | None = None,
254
+ handoff_description: str | None = None,
255
+ handoffs: list[Agent] | None = None,
256
+ model: str | None = None,
257
+ model_settings: ModelSettings | None = None,
258
+ tools: list[Tool] | None = None,
259
+ output_type: type[Any] | AgentOutputSchemaBase | None = None,
260
+ tool_use_behavior: (
261
+ Literal["run_llm_again", "stop_on_first_tool"]
262
+ | StopAtTools
263
+ | ToolsToFinalOutputFunction
264
+ ) = "run_llm_again",
265
+ ) -> RunResultStreaming:
266
+ """
267
+ Run an agent with streaming enabled but no TaskMessage creation.
268
+
269
+ DEFAULT: No TaskMessage creation, returns only the result.
270
+
271
+ NOTE: This method does NOT work in Temporal workflows!
272
+ Use run_agent_streamed_auto_send() instead for Temporal workflows.
273
+
274
+ Args:
275
+ input_list: List of input data for the agent.
276
+ mcp_server_params: MCP server parameters for the agent.
277
+ agent_name: The name of the agent to run.
278
+ agent_instructions: Instructions for the agent.
279
+ trace_id: Optional trace ID for tracing.
280
+ parent_span_id: Optional parent span for tracing.
281
+ start_to_close_timeout: Maximum time allowed for the operation.
282
+ heartbeat_timeout: Maximum time between heartbeats.
283
+ retry_policy: Policy for retrying failed operations.
284
+ handoff_description: Optional description of the handoff.
285
+ handoffs: Optional list of handoffs.
286
+ model: Optional model to use.
287
+ model_settings: Optional model settings.
288
+ tools: Optional list of tools.
289
+ output_type: Optional output type.
290
+ tool_use_behavior: Optional tool use behavior.
291
+
292
+ Returns:
293
+ RunResultStreaming: The result of the agent run with streaming.
294
+
295
+ Raises:
296
+ ValueError: If called from within a Temporal workflow
297
+ """
298
+ # Temporal workflows should use the auto_send variant
299
+ if in_temporal_workflow():
300
+ raise ValueError(
301
+ "run_agent_streamed() cannot be used in Temporal workflows. "
302
+ "Use run_agent_streamed_auto_send() instead, which properly handles "
303
+ "TaskMessage creation and streaming through the streaming service."
304
+ )
305
+
306
+ return await self._openai_service.run_agent_streamed(
307
+ input_list=input_list,
308
+ mcp_server_params=mcp_server_params,
309
+ agent_name=agent_name,
310
+ agent_instructions=agent_instructions,
311
+ trace_id=trace_id,
312
+ parent_span_id=parent_span_id,
313
+ handoff_description=handoff_description,
314
+ handoffs=handoffs,
315
+ model=model,
316
+ model_settings=model_settings,
317
+ tools=tools,
318
+ output_type=output_type,
319
+ tool_use_behavior=tool_use_behavior,
320
+ )
321
+
322
+ async def run_agent_streamed_auto_send(
323
+ self,
324
+ task_id: str,
325
+ input_list: list[dict[str, Any]],
326
+ mcp_server_params: list[StdioServerParameters],
327
+ agent_name: str,
328
+ agent_instructions: str,
329
+ trace_id: str | None = None,
330
+ parent_span_id: str | None = None,
331
+ start_to_close_timeout: timedelta = timedelta(seconds=600),
332
+ heartbeat_timeout: timedelta = timedelta(seconds=600),
333
+ retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
334
+ handoff_description: str | None = None,
335
+ handoffs: list[Agent] | None = None,
336
+ model: str | None = None,
337
+ model_settings: ModelSettings | None = None,
338
+ tools: list[Tool] | None = None,
339
+ output_type: type[Any] | AgentOutputSchemaBase | None = None,
340
+ tool_use_behavior: (
341
+ Literal["run_llm_again", "stop_on_first_tool"]
342
+ | StopAtTools
343
+ | ToolsToFinalOutputFunction
344
+ ) = "run_llm_again",
345
+ mcp_timeout_seconds: int | None = None,
346
+ ) -> SerializableRunResultStreaming | RunResultStreaming:
347
+ """
348
+ Run an agent with streaming enabled and automatic TaskMessage creation.
349
+
350
+ Args:
351
+ task_id: The ID of the task to run the agent for.
352
+ input_list: List of input data for the agent.
353
+ mcp_server_params: MCP server parameters for the agent.
354
+ agent_name: The name of the agent to run.
355
+ agent_instructions: Instructions for the agent.
356
+ trace_id: Optional trace ID for tracing.
357
+ parent_span_id: Optional parent span for tracing.
358
+ start_to_close_timeout: Maximum time allowed for the operation.
359
+ heartbeat_timeout: Maximum time between heartbeats.
360
+ retry_policy: Policy for retrying failed operations.
361
+ handoff_description: Optional description of the handoff.
362
+ handoffs: Optional list of handoffs.
363
+ model: Optional model to use.
364
+ model_settings: Optional model settings.
365
+ tools: Optional list of tools.
366
+ output_type: Optional output type.
367
+ tool_use_behavior: Optional tool use behavior.
368
+ mcp_timeout_seconds: Optional param to set the timeout threshold for the MCP servers. Defaults to 5 seconds.
369
+
370
+ Returns:
371
+ Union[SerializableRunResultStreaming, RunResultStreaming]: SerializableRunResultStreaming when in Temporal, RunResultStreaming otherwise.
372
+ """
373
+ if in_temporal_workflow():
374
+ params = RunAgentStreamedAutoSendParams(
375
+ trace_id=trace_id,
376
+ parent_span_id=parent_span_id,
377
+ task_id=task_id,
378
+ input_list=input_list,
379
+ mcp_server_params=mcp_server_params,
380
+ agent_name=agent_name,
381
+ agent_instructions=agent_instructions,
382
+ handoff_description=handoff_description,
383
+ handoffs=handoffs,
384
+ model=model,
385
+ model_settings=model_settings,
386
+ tools=tools,
387
+ output_type=output_type,
388
+ tool_use_behavior=tool_use_behavior,
389
+ mcp_timeout_seconds=mcp_timeout_seconds,
390
+ )
391
+ return await ActivityHelpers.execute_activity(
392
+ activity_name=OpenAIActivityName.RUN_AGENT_STREAMED_AUTO_SEND,
393
+ request=params,
394
+ response_type=SerializableRunResultStreaming,
395
+ start_to_close_timeout=start_to_close_timeout,
396
+ heartbeat_timeout=heartbeat_timeout,
397
+ retry_policy=retry_policy,
398
+ )
399
+ else:
400
+ return await self._openai_service.run_agent_streamed_auto_send(
401
+ task_id=task_id,
402
+ input_list=input_list,
403
+ mcp_server_params=mcp_server_params,
404
+ agent_name=agent_name,
405
+ agent_instructions=agent_instructions,
406
+ trace_id=trace_id,
407
+ parent_span_id=parent_span_id,
408
+ handoff_description=handoff_description,
409
+ handoffs=handoffs,
410
+ model=model,
411
+ model_settings=model_settings,
412
+ tools=tools,
413
+ output_type=output_type,
414
+ tool_use_behavior=tool_use_behavior,
415
+ mcp_timeout_seconds=mcp_timeout_seconds,
416
+ )
@@ -0,0 +1,85 @@
1
+ from datetime import timedelta
2
+
3
+ from scale_gp import SGPClient, SGPClientError
4
+ from temporalio.common import RetryPolicy
5
+
6
+ from agentex import AsyncAgentex
7
+ from agentex.lib.core.services.adk.providers.sgp import SGPService
8
+ from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
9
+ from agentex.lib.core.temporal.activities.adk.providers.sgp_activities import (
10
+ DownloadFileParams,
11
+ FileContentResponse,
12
+ SGPActivityName,
13
+ )
14
+ from agentex.lib.core.tracing.tracer import AsyncTracer
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 SGPModule:
24
+ """
25
+ Module for managing SGP agent operations in Agentex.
26
+ Provides high-level methods for chat completion, streaming, agentic streaming, and message classification.
27
+ """
28
+
29
+ def __init__(
30
+ self,
31
+ sgp_service: SGPService | None = None,
32
+ ):
33
+ if sgp_service is None:
34
+ try:
35
+ sgp_client = SGPClient()
36
+ agentex_client = AsyncAgentex()
37
+ tracer = AsyncTracer(agentex_client)
38
+ self._sgp_service = SGPService(sgp_client=sgp_client, tracer=tracer)
39
+ except SGPClientError:
40
+ self._sgp_service = None
41
+ else:
42
+ self._sgp_service = sgp_service
43
+
44
+ async def download_file_content(
45
+ self,
46
+ params: DownloadFileParams,
47
+ start_to_close_timeout: timedelta = timedelta(seconds=30),
48
+ heartbeat_timeout: timedelta = timedelta(seconds=30),
49
+ retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
50
+ ) -> FileContentResponse:
51
+ """
52
+ Download the content of a file from SGP.
53
+
54
+ Args:
55
+ params (DownloadFileParams): The parameters for the download file content activity.
56
+ start_to_close_timeout (timedelta): The start to close timeout.
57
+ heartbeat_timeout (timedelta): The heartbeat timeout.
58
+ retry_policy (RetryPolicy): The retry policy.
59
+
60
+ Returns:
61
+ FileContentResponse: The content of the file
62
+ """
63
+ if self._sgp_service is None:
64
+ raise ValueError(
65
+ "SGP activities are disabled because the SGP client could not be initialized. Please check that the SGP_API_KEY environment variable is set."
66
+ )
67
+
68
+ params = DownloadFileParams(
69
+ file_id=params.file_id,
70
+ filename=params.filename,
71
+ )
72
+ if in_temporal_workflow():
73
+ return await ActivityHelpers.execute_activity(
74
+ activity_name=SGPActivityName.DOWNLOAD_FILE_CONTENT,
75
+ request=params,
76
+ response_type=FileContentResponse,
77
+ start_to_close_timeout=start_to_close_timeout,
78
+ heartbeat_timeout=heartbeat_timeout,
79
+ retry_policy=retry_policy,
80
+ )
81
+ else:
82
+ return await self._sgp_service.download_file_content(
83
+ file_id=params.file_id,
84
+ filename=params.filename,
85
+ )
@@ -0,0 +1,5 @@
1
+ from agentex.lib.adk.utils._modules.templating import TemplatingModule
2
+
3
+ __all__ = ["templating"]
4
+
5
+ templating = TemplatingModule()
File without changes
@@ -0,0 +1,94 @@
1
+ from datetime import timedelta
2
+ from typing import Any
3
+
4
+ from temporalio.common import RetryPolicy
5
+
6
+ from agentex import AsyncAgentex
7
+ from agentex.lib.core.services.adk.utils.templating import TemplatingService
8
+ from agentex.lib.core.temporal.activities.activity_helpers import ActivityHelpers
9
+ from agentex.lib.core.temporal.activities.adk.utils.templating_activities import (
10
+ JinjaActivityName,
11
+ RenderJinjaParams,
12
+ )
13
+ from agentex.lib.core.tracing.tracer import AsyncTracer
14
+ from agentex.lib.utils.logging import make_logger
15
+ from agentex.lib.utils.temporal import in_temporal_workflow
16
+
17
+ logger = make_logger(__name__)
18
+
19
+ DEFAULT_RETRY_POLICY = RetryPolicy(maximum_attempts=1)
20
+
21
+
22
+ class TemplatingModule:
23
+ """
24
+ Module for managing templating operations in Agentex.
25
+
26
+ This interface provides high-level methods for rendering Jinja templates, abstracting away
27
+ the underlying activity and workflow execution. It supports both synchronous and asynchronous
28
+ (Temporal workflow) contexts.
29
+ """
30
+
31
+ def __init__(
32
+ self,
33
+ templating_service: TemplatingService | None = None,
34
+ ):
35
+ """
36
+ Initialize the templating interface.
37
+
38
+ Args:
39
+ templating_service (Optional[TemplatingService]): Optional pre-configured templating service. If None, will be auto-initialized.
40
+ """
41
+ if templating_service is None:
42
+ agentex_client = AsyncAgentex()
43
+ tracer = AsyncTracer(agentex_client)
44
+ self._templating_service = TemplatingService(tracer=tracer)
45
+ else:
46
+ self._templating_service = templating_service
47
+
48
+ async def render_jinja(
49
+ self,
50
+ trace_id: str,
51
+ template: str,
52
+ variables: dict[str, Any],
53
+ parent_span_id: str | None = None,
54
+ start_to_close_timeout: timedelta = timedelta(seconds=10),
55
+ heartbeat_timeout: timedelta = timedelta(seconds=10),
56
+ retry_policy: RetryPolicy = DEFAULT_RETRY_POLICY,
57
+ ) -> str:
58
+ """
59
+ Render a Jinja template.
60
+
61
+ Args:
62
+ trace_id (str): Unique identifier for tracing and correlation.
63
+ template (str): The Jinja template string to render.
64
+ variables (Dict[str, Any]): Variables to use in the template.
65
+ parent_span_id (Optional[str]): Optional parent span for tracing.
66
+ start_to_close_timeout (timedelta): Maximum time allowed for the operation.
67
+ heartbeat_timeout (timedelta): Maximum time between heartbeats.
68
+ retry_policy (RetryPolicy): Policy for retrying failed operations.
69
+
70
+ Returns:
71
+ str: The rendered template as a string.
72
+ """
73
+ render_jinja_params = RenderJinjaParams(
74
+ trace_id=trace_id,
75
+ parent_span_id=parent_span_id,
76
+ template=template,
77
+ variables=variables,
78
+ )
79
+ if in_temporal_workflow():
80
+ return await ActivityHelpers.execute_activity(
81
+ activity_name=JinjaActivityName.RENDER_JINJA,
82
+ request=render_jinja_params,
83
+ response_type=str,
84
+ start_to_close_timeout=start_to_close_timeout,
85
+ heartbeat_timeout=heartbeat_timeout,
86
+ retry_policy=retry_policy,
87
+ )
88
+ else:
89
+ return await self._templating_service.render_jinja(
90
+ template=template,
91
+ variables=variables,
92
+ trace_id=trace_id,
93
+ parent_span_id=parent_span_id,
94
+ )
File without changes
File without changes