kubiya-control-plane-api 0.1.0__py3-none-any.whl → 0.3.4__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.

Potentially problematic release.


This version of kubiya-control-plane-api might be problematic. Click here for more details.

Files changed (185) hide show
  1. control_plane_api/README.md +266 -0
  2. control_plane_api/__init__.py +0 -0
  3. control_plane_api/__version__.py +1 -0
  4. control_plane_api/alembic/README +1 -0
  5. control_plane_api/alembic/env.py +98 -0
  6. control_plane_api/alembic/script.py.mako +28 -0
  7. control_plane_api/alembic/versions/1382bec74309_initial_migration_with_all_models.py +251 -0
  8. control_plane_api/alembic/versions/1f54bc2a37e3_add_analytics_tables.py +162 -0
  9. control_plane_api/alembic/versions/2e4cb136dc10_rename_toolset_ids_to_skill_ids_in_teams.py +30 -0
  10. control_plane_api/alembic/versions/31cd69a644ce_add_skill_templates_table.py +28 -0
  11. control_plane_api/alembic/versions/89e127caa47d_add_jobs_and_job_executions_tables.py +161 -0
  12. control_plane_api/alembic/versions/add_llm_models_table.py +51 -0
  13. control_plane_api/alembic/versions/b0e10697f212_add_runtime_column_to_teams_simple.py +42 -0
  14. control_plane_api/alembic/versions/ce43b24b63bf_add_execution_trigger_source_and_fix_.py +155 -0
  15. control_plane_api/alembic/versions/d4eaf16e3f8d_rename_toolsets_to_skills.py +84 -0
  16. control_plane_api/alembic/versions/efa2dc427da1_rename_metadata_to_custom_metadata.py +32 -0
  17. control_plane_api/alembic/versions/f973b431d1ce_add_workflow_executor_to_skill_types.py +44 -0
  18. control_plane_api/alembic.ini +148 -0
  19. control_plane_api/api/index.py +12 -0
  20. control_plane_api/app/__init__.py +11 -0
  21. control_plane_api/app/activities/__init__.py +20 -0
  22. control_plane_api/app/activities/agent_activities.py +379 -0
  23. control_plane_api/app/activities/team_activities.py +410 -0
  24. control_plane_api/app/activities/temporal_cloud_activities.py +577 -0
  25. control_plane_api/app/config/__init__.py +35 -0
  26. control_plane_api/app/config/api_config.py +354 -0
  27. control_plane_api/app/config/model_pricing.py +318 -0
  28. control_plane_api/app/config.py +95 -0
  29. control_plane_api/app/database.py +135 -0
  30. control_plane_api/app/exceptions.py +408 -0
  31. control_plane_api/app/lib/__init__.py +11 -0
  32. control_plane_api/app/lib/job_executor.py +312 -0
  33. control_plane_api/app/lib/kubiya_client.py +235 -0
  34. control_plane_api/app/lib/litellm_pricing.py +166 -0
  35. control_plane_api/app/lib/planning_tools/__init__.py +22 -0
  36. control_plane_api/app/lib/planning_tools/agents.py +155 -0
  37. control_plane_api/app/lib/planning_tools/base.py +189 -0
  38. control_plane_api/app/lib/planning_tools/environments.py +214 -0
  39. control_plane_api/app/lib/planning_tools/resources.py +240 -0
  40. control_plane_api/app/lib/planning_tools/teams.py +198 -0
  41. control_plane_api/app/lib/policy_enforcer_client.py +939 -0
  42. control_plane_api/app/lib/redis_client.py +436 -0
  43. control_plane_api/app/lib/supabase.py +71 -0
  44. control_plane_api/app/lib/temporal_client.py +138 -0
  45. control_plane_api/app/lib/validation/__init__.py +20 -0
  46. control_plane_api/app/lib/validation/runtime_validation.py +287 -0
  47. control_plane_api/app/main.py +128 -0
  48. control_plane_api/app/middleware/__init__.py +8 -0
  49. control_plane_api/app/middleware/auth.py +513 -0
  50. control_plane_api/app/middleware/exception_handler.py +267 -0
  51. control_plane_api/app/middleware/rate_limiting.py +384 -0
  52. control_plane_api/app/middleware/request_id.py +202 -0
  53. control_plane_api/app/models/__init__.py +27 -0
  54. control_plane_api/app/models/agent.py +79 -0
  55. control_plane_api/app/models/analytics.py +206 -0
  56. control_plane_api/app/models/associations.py +81 -0
  57. control_plane_api/app/models/environment.py +63 -0
  58. control_plane_api/app/models/execution.py +93 -0
  59. control_plane_api/app/models/job.py +179 -0
  60. control_plane_api/app/models/llm_model.py +75 -0
  61. control_plane_api/app/models/presence.py +49 -0
  62. control_plane_api/app/models/project.py +47 -0
  63. control_plane_api/app/models/session.py +38 -0
  64. control_plane_api/app/models/team.py +66 -0
  65. control_plane_api/app/models/workflow.py +55 -0
  66. control_plane_api/app/policies/README.md +121 -0
  67. control_plane_api/app/policies/approved_users.rego +62 -0
  68. control_plane_api/app/policies/business_hours.rego +51 -0
  69. control_plane_api/app/policies/rate_limiting.rego +100 -0
  70. control_plane_api/app/policies/tool_restrictions.rego +86 -0
  71. control_plane_api/app/routers/__init__.py +4 -0
  72. control_plane_api/app/routers/agents.py +364 -0
  73. control_plane_api/app/routers/agents_v2.py +1260 -0
  74. control_plane_api/app/routers/analytics.py +1014 -0
  75. control_plane_api/app/routers/context_manager.py +562 -0
  76. control_plane_api/app/routers/environment_context.py +270 -0
  77. control_plane_api/app/routers/environments.py +715 -0
  78. control_plane_api/app/routers/execution_environment.py +517 -0
  79. control_plane_api/app/routers/executions.py +1911 -0
  80. control_plane_api/app/routers/health.py +92 -0
  81. control_plane_api/app/routers/health_v2.py +326 -0
  82. control_plane_api/app/routers/integrations.py +274 -0
  83. control_plane_api/app/routers/jobs.py +1344 -0
  84. control_plane_api/app/routers/models.py +82 -0
  85. control_plane_api/app/routers/models_v2.py +361 -0
  86. control_plane_api/app/routers/policies.py +639 -0
  87. control_plane_api/app/routers/presence.py +234 -0
  88. control_plane_api/app/routers/projects.py +902 -0
  89. control_plane_api/app/routers/runners.py +379 -0
  90. control_plane_api/app/routers/runtimes.py +172 -0
  91. control_plane_api/app/routers/secrets.py +155 -0
  92. control_plane_api/app/routers/skills.py +1001 -0
  93. control_plane_api/app/routers/skills_definitions.py +140 -0
  94. control_plane_api/app/routers/task_planning.py +1256 -0
  95. control_plane_api/app/routers/task_queues.py +654 -0
  96. control_plane_api/app/routers/team_context.py +270 -0
  97. control_plane_api/app/routers/teams.py +1400 -0
  98. control_plane_api/app/routers/worker_queues.py +1545 -0
  99. control_plane_api/app/routers/workers.py +935 -0
  100. control_plane_api/app/routers/workflows.py +204 -0
  101. control_plane_api/app/runtimes/__init__.py +6 -0
  102. control_plane_api/app/runtimes/validation.py +344 -0
  103. control_plane_api/app/schemas/job_schemas.py +295 -0
  104. control_plane_api/app/services/__init__.py +1 -0
  105. control_plane_api/app/services/agno_service.py +619 -0
  106. control_plane_api/app/services/litellm_service.py +190 -0
  107. control_plane_api/app/services/policy_service.py +525 -0
  108. control_plane_api/app/services/temporal_cloud_provisioning.py +150 -0
  109. control_plane_api/app/skills/__init__.py +44 -0
  110. control_plane_api/app/skills/base.py +229 -0
  111. control_plane_api/app/skills/business_intelligence.py +189 -0
  112. control_plane_api/app/skills/data_visualization.py +154 -0
  113. control_plane_api/app/skills/docker.py +104 -0
  114. control_plane_api/app/skills/file_generation.py +94 -0
  115. control_plane_api/app/skills/file_system.py +110 -0
  116. control_plane_api/app/skills/python.py +92 -0
  117. control_plane_api/app/skills/registry.py +65 -0
  118. control_plane_api/app/skills/shell.py +102 -0
  119. control_plane_api/app/skills/workflow_executor.py +469 -0
  120. control_plane_api/app/utils/workflow_executor.py +354 -0
  121. control_plane_api/app/workflows/__init__.py +11 -0
  122. control_plane_api/app/workflows/agent_execution.py +507 -0
  123. control_plane_api/app/workflows/agent_execution_with_skills.py +222 -0
  124. control_plane_api/app/workflows/namespace_provisioning.py +326 -0
  125. control_plane_api/app/workflows/team_execution.py +399 -0
  126. control_plane_api/scripts/seed_models.py +239 -0
  127. control_plane_api/worker/__init__.py +0 -0
  128. control_plane_api/worker/activities/__init__.py +0 -0
  129. control_plane_api/worker/activities/agent_activities.py +1241 -0
  130. control_plane_api/worker/activities/approval_activities.py +234 -0
  131. control_plane_api/worker/activities/runtime_activities.py +388 -0
  132. control_plane_api/worker/activities/skill_activities.py +267 -0
  133. control_plane_api/worker/activities/team_activities.py +1217 -0
  134. control_plane_api/worker/config/__init__.py +31 -0
  135. control_plane_api/worker/config/worker_config.py +275 -0
  136. control_plane_api/worker/control_plane_client.py +529 -0
  137. control_plane_api/worker/examples/analytics_integration_example.py +362 -0
  138. control_plane_api/worker/models/__init__.py +1 -0
  139. control_plane_api/worker/models/inputs.py +89 -0
  140. control_plane_api/worker/runtimes/__init__.py +31 -0
  141. control_plane_api/worker/runtimes/base.py +789 -0
  142. control_plane_api/worker/runtimes/claude_code_runtime.py +1443 -0
  143. control_plane_api/worker/runtimes/default_runtime.py +617 -0
  144. control_plane_api/worker/runtimes/factory.py +173 -0
  145. control_plane_api/worker/runtimes/validation.py +93 -0
  146. control_plane_api/worker/services/__init__.py +1 -0
  147. control_plane_api/worker/services/agent_executor.py +422 -0
  148. control_plane_api/worker/services/agent_executor_v2.py +383 -0
  149. control_plane_api/worker/services/analytics_collector.py +457 -0
  150. control_plane_api/worker/services/analytics_service.py +464 -0
  151. control_plane_api/worker/services/approval_tools.py +310 -0
  152. control_plane_api/worker/services/approval_tools_agno.py +207 -0
  153. control_plane_api/worker/services/cancellation_manager.py +177 -0
  154. control_plane_api/worker/services/data_visualization.py +827 -0
  155. control_plane_api/worker/services/jira_tools.py +257 -0
  156. control_plane_api/worker/services/runtime_analytics.py +328 -0
  157. control_plane_api/worker/services/session_service.py +194 -0
  158. control_plane_api/worker/services/skill_factory.py +175 -0
  159. control_plane_api/worker/services/team_executor.py +574 -0
  160. control_plane_api/worker/services/team_executor_v2.py +465 -0
  161. control_plane_api/worker/services/workflow_executor_tools.py +1418 -0
  162. control_plane_api/worker/tests/__init__.py +1 -0
  163. control_plane_api/worker/tests/e2e/__init__.py +0 -0
  164. control_plane_api/worker/tests/e2e/test_execution_flow.py +571 -0
  165. control_plane_api/worker/tests/integration/__init__.py +0 -0
  166. control_plane_api/worker/tests/integration/test_control_plane_integration.py +308 -0
  167. control_plane_api/worker/tests/unit/__init__.py +0 -0
  168. control_plane_api/worker/tests/unit/test_control_plane_client.py +401 -0
  169. control_plane_api/worker/utils/__init__.py +1 -0
  170. control_plane_api/worker/utils/chunk_batcher.py +305 -0
  171. control_plane_api/worker/utils/retry_utils.py +60 -0
  172. control_plane_api/worker/utils/streaming_utils.py +373 -0
  173. control_plane_api/worker/worker.py +753 -0
  174. control_plane_api/worker/workflows/__init__.py +0 -0
  175. control_plane_api/worker/workflows/agent_execution.py +589 -0
  176. control_plane_api/worker/workflows/team_execution.py +429 -0
  177. kubiya_control_plane_api-0.3.4.dist-info/METADATA +229 -0
  178. kubiya_control_plane_api-0.3.4.dist-info/RECORD +182 -0
  179. kubiya_control_plane_api-0.3.4.dist-info/entry_points.txt +2 -0
  180. kubiya_control_plane_api-0.3.4.dist-info/top_level.txt +1 -0
  181. kubiya_control_plane_api-0.1.0.dist-info/METADATA +0 -66
  182. kubiya_control_plane_api-0.1.0.dist-info/RECORD +0 -5
  183. kubiya_control_plane_api-0.1.0.dist-info/top_level.txt +0 -1
  184. {kubiya_control_plane_api-0.1.0.dist-info/licenses → control_plane_api}/LICENSE +0 -0
  185. {kubiya_control_plane_api-0.1.0.dist-info → kubiya_control_plane_api-0.3.4.dist-info}/WHEEL +0 -0
@@ -0,0 +1,507 @@
1
+ """Agent execution workflow for Temporal"""
2
+
3
+ from dataclasses import dataclass, field
4
+ from datetime import timedelta
5
+ from typing import Optional, List, Dict, Any
6
+ from temporalio import workflow
7
+ import asyncio
8
+
9
+ with workflow.unsafe.imports_passed_through():
10
+ from control_plane_api.app.activities.agent_activities import (
11
+ execute_agent_llm,
12
+ update_execution_status,
13
+ update_agent_status,
14
+ ActivityExecuteAgentInput,
15
+ ActivityUpdateExecutionInput,
16
+ ActivityUpdateAgentInput,
17
+ )
18
+
19
+
20
+ @dataclass
21
+ class AgentExecutionInput:
22
+ """Input for agent execution workflow"""
23
+ execution_id: str
24
+ agent_id: str
25
+ organization_id: str
26
+ prompt: str
27
+ system_prompt: Optional[str] = None
28
+ model_id: Optional[str] = None
29
+ model_config: dict = None
30
+ agent_config: dict = None
31
+ mcp_servers: dict = None # MCP servers configuration
32
+ user_metadata: dict = None
33
+ runtime_type: str = "default" # "default" (Agno) or "claude_code"
34
+ control_plane_url: Optional[str] = None # Control Plane URL for fetching credentials/secrets
35
+ api_key: Optional[str] = None # API key for authentication
36
+
37
+ def __post_init__(self):
38
+ if self.model_config is None:
39
+ self.model_config = {}
40
+ if self.agent_config is None:
41
+ self.agent_config = {}
42
+ if self.mcp_servers is None:
43
+ self.mcp_servers = {}
44
+ if self.user_metadata is None:
45
+ self.user_metadata = {}
46
+
47
+
48
+ @dataclass
49
+ class TeamExecutionInput:
50
+ """Input for team execution workflow (uses same workflow as agent)"""
51
+ execution_id: str
52
+ team_id: str
53
+ organization_id: str
54
+ prompt: str
55
+ system_prompt: Optional[str] = None
56
+ model_id: Optional[str] = None
57
+ model_config: dict = None
58
+ team_config: dict = None
59
+ mcp_servers: dict = None # MCP servers configuration
60
+ user_metadata: dict = None
61
+ runtime_type: str = "default" # "default" (Agno) or "claude_code"
62
+ control_plane_url: Optional[str] = None # Control Plane URL for fetching credentials/secrets
63
+ api_key: Optional[str] = None # API key for authentication
64
+
65
+ def __post_init__(self):
66
+ if self.model_config is None:
67
+ self.model_config = {}
68
+ if self.team_config is None:
69
+ self.team_config = {}
70
+ if self.mcp_servers is None:
71
+ self.mcp_servers = {}
72
+ if self.user_metadata is None:
73
+ self.user_metadata = {}
74
+
75
+ def to_agent_input(self) -> AgentExecutionInput:
76
+ """Convert TeamExecutionInput to AgentExecutionInput for workflow reuse"""
77
+ return AgentExecutionInput(
78
+ execution_id=self.execution_id,
79
+ agent_id=self.team_id, # Use team_id as agent_id
80
+ organization_id=self.organization_id,
81
+ prompt=self.prompt,
82
+ system_prompt=self.system_prompt,
83
+ model_id=self.model_id,
84
+ model_config=self.model_config,
85
+ agent_config=self.team_config,
86
+ mcp_servers=self.mcp_servers,
87
+ user_metadata=self.user_metadata,
88
+ runtime_type=self.runtime_type,
89
+ control_plane_url=self.control_plane_url,
90
+ api_key=self.api_key,
91
+ )
92
+
93
+
94
+ @dataclass
95
+ class ChatMessage:
96
+ """Represents a message in the conversation"""
97
+ role: str # "user", "assistant", "system", "tool"
98
+ content: str
99
+ timestamp: str
100
+ tool_name: Optional[str] = None
101
+ tool_input: Optional[Dict[str, Any]] = None
102
+ tool_output: Optional[Dict[str, Any]] = None
103
+ # User attribution for messages
104
+ user_id: Optional[str] = None
105
+ user_name: Optional[str] = None
106
+ user_email: Optional[str] = None
107
+ user_avatar: Optional[str] = None
108
+
109
+
110
+ @dataclass
111
+ class ExecutionState:
112
+ """Current state of the execution for queries"""
113
+ status: str # "pending", "running", "waiting_for_input", "paused", "completed", "failed"
114
+ messages: List[ChatMessage] = field(default_factory=list)
115
+ current_response: str = ""
116
+ error_message: Optional[str] = None
117
+ usage: Dict[str, Any] = field(default_factory=dict)
118
+ metadata: Dict[str, Any] = field(default_factory=dict)
119
+ is_waiting_for_input: bool = False
120
+ should_complete: bool = False
121
+ is_paused: bool = False
122
+
123
+
124
+ @workflow.defn
125
+ class AgentExecutionWorkflow:
126
+ """
127
+ Workflow for executing an agent with LLM with Temporal message passing support.
128
+
129
+ This workflow:
130
+ 1. Updates execution status to running
131
+ 2. Executes the agent's LLM call
132
+ 3. Updates execution with results
133
+ 4. Updates agent status
134
+ 5. Supports queries for real-time state access
135
+ 6. Supports signals for adding followup messages
136
+ """
137
+
138
+ def __init__(self) -> None:
139
+ """Initialize workflow state"""
140
+ self._state = ExecutionState(status="pending")
141
+ self._lock = asyncio.Lock()
142
+ self._new_message_count = 0
143
+ self._processed_message_count = 0
144
+
145
+ @workflow.query
146
+ def get_state(self) -> ExecutionState:
147
+ """Query handler: Get current execution state including messages and status"""
148
+ return self._state
149
+
150
+ @workflow.signal
151
+ async def add_message(self, message: ChatMessage) -> None:
152
+ """
153
+ Signal handler: Add a message to the conversation.
154
+ This allows clients to send followup messages while the workflow is running.
155
+ The workflow will wake up and process this message.
156
+ """
157
+ async with self._lock:
158
+ self._state.messages.append(message)
159
+ self._new_message_count += 1
160
+ self._state.is_waiting_for_input = False
161
+ workflow.logger.info(
162
+ f"Message added to conversation",
163
+ extra={
164
+ "role": message.role,
165
+ "content_preview": message.content[:100] if message.content else "",
166
+ "total_messages": len(self._state.messages)
167
+ }
168
+ )
169
+
170
+ @workflow.signal
171
+ async def mark_as_done(self) -> None:
172
+ """
173
+ Signal handler: Mark the workflow as complete.
174
+ This signals that the user is done with the conversation and the workflow should complete.
175
+ """
176
+ async with self._lock:
177
+ self._state.should_complete = True
178
+ self._state.is_waiting_for_input = False
179
+ workflow.logger.info("Workflow marked as done by user")
180
+
181
+ @workflow.signal
182
+ async def pause_execution(self) -> None:
183
+ """
184
+ Signal handler: Pause the workflow execution.
185
+ This pauses the workflow - it will stop processing but remain active.
186
+ Resume can be called to continue execution.
187
+ """
188
+ async with self._lock:
189
+ if not self._state.is_paused:
190
+ self._state.is_paused = True
191
+ self._state.status = "paused"
192
+ workflow.logger.info("Workflow paused by user")
193
+
194
+ @workflow.signal
195
+ async def resume_execution(self) -> None:
196
+ """
197
+ Signal handler: Resume a paused workflow execution.
198
+ This resumes the workflow from where it was paused.
199
+ """
200
+ async with self._lock:
201
+ if self._state.is_paused:
202
+ self._state.is_paused = False
203
+ # Restore previous status (either running or waiting_for_input)
204
+ self._state.status = "waiting_for_input" if self._state.is_waiting_for_input else "running"
205
+ workflow.logger.info("Workflow resumed by user")
206
+
207
+ @workflow.run
208
+ async def run(self, input: AgentExecutionInput) -> dict:
209
+ """
210
+ Run the agent execution workflow with Human-in-the-Loop (HITL) pattern.
211
+
212
+ This workflow implements a continuous conversation loop:
213
+ 1. Process the initial user message
214
+ 2. Execute LLM and return response
215
+ 3. Wait for user input (signals)
216
+ 4. Process followup messages in a loop
217
+ 5. Only complete when user explicitly marks as done
218
+
219
+ Args:
220
+ input: Workflow input with execution details
221
+
222
+ Returns:
223
+ Execution result dict with response, usage, etc.
224
+ """
225
+ workflow.logger.info(
226
+ f"Starting agent execution workflow with HITL pattern",
227
+ extra={
228
+ "execution_id": input.execution_id,
229
+ "agent_id": input.agent_id,
230
+ "organization_id": input.organization_id,
231
+ }
232
+ )
233
+
234
+ # Initialize state with user's initial message
235
+ self._state.messages.append(ChatMessage(
236
+ role="user",
237
+ content=input.prompt,
238
+ timestamp=workflow.now().isoformat(),
239
+ ))
240
+ self._state.status = "running"
241
+ self._new_message_count = 1 # Initial message counts as a new message
242
+ self._processed_message_count = 0 # No messages processed yet (no response)
243
+
244
+ try:
245
+ # Step 1: Update execution status to running
246
+ await workflow.execute_activity(
247
+ update_execution_status,
248
+ ActivityUpdateExecutionInput(
249
+ execution_id=input.execution_id,
250
+ status="running",
251
+ started_at=workflow.now().isoformat(),
252
+ execution_metadata={
253
+ "workflow_started": True,
254
+ "has_mcp_servers": bool(input.mcp_servers),
255
+ "mcp_server_count": len(input.mcp_servers) if input.mcp_servers else 0,
256
+ "hitl_enabled": True,
257
+ },
258
+ ),
259
+ start_to_close_timeout=timedelta(seconds=30),
260
+ )
261
+
262
+ # Step 2: Update agent status to running
263
+ await workflow.execute_activity(
264
+ update_agent_status,
265
+ ActivityUpdateAgentInput(
266
+ agent_id=input.agent_id,
267
+ organization_id=input.organization_id,
268
+ status="running",
269
+ last_active_at=workflow.now().isoformat(),
270
+ ),
271
+ start_to_close_timeout=timedelta(seconds=30),
272
+ )
273
+
274
+ # HITL Conversation Loop - Continue until user marks as done
275
+ conversation_turn = 0
276
+ while not self._state.should_complete:
277
+ # Check if workflow is paused - wait until resumed
278
+ if self._state.is_paused:
279
+ workflow.logger.info("Workflow is paused, waiting for resume signal")
280
+ await workflow.wait_condition(
281
+ lambda: not self._state.is_paused or self._state.should_complete,
282
+ timeout=timedelta(hours=24)
283
+ )
284
+ if self._state.should_complete:
285
+ break
286
+ workflow.logger.info("Workflow resumed, continuing execution")
287
+
288
+ conversation_turn += 1
289
+ workflow.logger.info(
290
+ f"Starting conversation turn {conversation_turn}",
291
+ extra={"turn": conversation_turn, "message_count": len(self._state.messages)}
292
+ )
293
+
294
+ # Get the latest user message (last message added)
295
+ latest_message = self._state.messages[-1] if self._state.messages else None
296
+ latest_prompt = latest_message.content if latest_message and latest_message.role == "user" else input.prompt
297
+
298
+ # Execute agent LLM call with session_id - Agno handles conversation history automatically
299
+ llm_result = await workflow.execute_activity(
300
+ execute_agent_llm,
301
+ ActivityExecuteAgentInput(
302
+ execution_id=input.execution_id,
303
+ agent_id=input.agent_id,
304
+ organization_id=input.organization_id,
305
+ prompt=latest_prompt, # Current turn's prompt
306
+ system_prompt=input.system_prompt,
307
+ model_id=input.model_id,
308
+ model_config=input.model_config,
309
+ mcp_servers=input.mcp_servers,
310
+ session_id=input.execution_id, # Use execution_id as session_id for 1:1 mapping
311
+ user_id=input.user_metadata.get("user_id") if input.user_metadata else None,
312
+ control_plane_url=input.control_plane_url, # Pass Control Plane URL from workflow input
313
+ api_key=input.api_key, # Pass API key from workflow input
314
+ ),
315
+ start_to_close_timeout=timedelta(minutes=10),
316
+ )
317
+
318
+ # Update state with assistant response
319
+ if llm_result.get("response"):
320
+ async with self._lock:
321
+ self._state.messages.append(ChatMessage(
322
+ role="assistant",
323
+ content=llm_result["response"],
324
+ timestamp=workflow.now().isoformat(),
325
+ ))
326
+ self._state.current_response = llm_result["response"]
327
+ self._processed_message_count += 1
328
+
329
+ # Update usage and metadata (accumulate across turns)
330
+ if llm_result.get("usage"):
331
+ # Accumulate token usage across conversation turns
332
+ current_usage = self._state.usage
333
+ new_usage = llm_result.get("usage", {})
334
+ self._state.usage = {
335
+ "prompt_tokens": current_usage.get("prompt_tokens", 0) + new_usage.get("prompt_tokens", 0),
336
+ "completion_tokens": current_usage.get("completion_tokens", 0) + new_usage.get("completion_tokens", 0),
337
+ "total_tokens": current_usage.get("total_tokens", 0) + new_usage.get("total_tokens", 0),
338
+ }
339
+
340
+ # Update metadata with latest turn info
341
+ self._state.metadata.update({
342
+ "model": llm_result.get("model"),
343
+ "latest_finish_reason": llm_result.get("finish_reason"),
344
+ "mcp_tools_used": self._state.metadata.get("mcp_tools_used", 0) + llm_result.get("mcp_tools_used", 0),
345
+ "latest_run_id": llm_result.get("run_id"),
346
+ "conversation_turns": conversation_turn,
347
+ })
348
+
349
+ # Check if LLM call failed
350
+ if not llm_result.get("success"):
351
+ self._state.status = "failed"
352
+ self._state.error_message = llm_result.get("error")
353
+ break
354
+
355
+ # Update execution status to waiting_for_input
356
+ self._state.status = "waiting_for_input"
357
+ self._state.is_waiting_for_input = True
358
+
359
+ # Update database to reflect waiting state
360
+ await workflow.execute_activity(
361
+ update_execution_status,
362
+ ActivityUpdateExecutionInput(
363
+ execution_id=input.execution_id,
364
+ status="waiting_for_input",
365
+ response=self._state.current_response,
366
+ usage=self._state.usage,
367
+ execution_metadata={
368
+ **self._state.metadata,
369
+ "conversation_turns": conversation_turn,
370
+ "waiting_for_user": True,
371
+ },
372
+ ),
373
+ start_to_close_timeout=timedelta(seconds=30),
374
+ )
375
+
376
+ workflow.logger.info(
377
+ f"Waiting for user input after turn {conversation_turn}",
378
+ extra={"turn": conversation_turn}
379
+ )
380
+
381
+ # Wait for either:
382
+ # 1. New message from user (add_message signal)
383
+ # 2. User marks as done (mark_as_done signal)
384
+ # 3. User pauses execution (pause_execution signal)
385
+ # 4. Timeout (24 hours for long-running conversations)
386
+ await workflow.wait_condition(
387
+ lambda: self._new_message_count > self._processed_message_count or self._state.should_complete or self._state.is_paused,
388
+ timeout=timedelta(hours=24)
389
+ )
390
+
391
+ # Don't update processed count here - it will be updated after we add the assistant's response
392
+
393
+ if self._state.should_complete:
394
+ workflow.logger.info("User marked workflow as done")
395
+ break
396
+
397
+ # If paused while waiting, loop back to check pause condition at top of while loop
398
+ if self._state.is_paused:
399
+ workflow.logger.info("Workflow paused while waiting for input")
400
+ continue
401
+
402
+ # Continue loop to process new message
403
+ self._state.status = "running"
404
+
405
+ # Conversation complete - finalize workflow
406
+ final_status = "failed" if self._state.status == "failed" else "completed"
407
+ self._state.status = final_status
408
+
409
+ await workflow.execute_activity(
410
+ update_execution_status,
411
+ ActivityUpdateExecutionInput(
412
+ execution_id=input.execution_id,
413
+ status=final_status,
414
+ completed_at=workflow.now().isoformat(),
415
+ response=self._state.current_response,
416
+ error_message=self._state.error_message,
417
+ usage=self._state.usage,
418
+ execution_metadata={
419
+ **self._state.metadata,
420
+ "workflow_completed": True,
421
+ "total_conversation_turns": conversation_turn,
422
+ },
423
+ ),
424
+ start_to_close_timeout=timedelta(seconds=30),
425
+ )
426
+
427
+ # Update agent final status
428
+ agent_final_status = "completed" if final_status == "completed" else "failed"
429
+ await workflow.execute_activity(
430
+ update_agent_status,
431
+ ActivityUpdateAgentInput(
432
+ agent_id=input.agent_id,
433
+ organization_id=input.organization_id,
434
+ status=agent_final_status,
435
+ last_active_at=workflow.now().isoformat(),
436
+ error_message=self._state.error_message if final_status == "failed" else None,
437
+ ),
438
+ start_to_close_timeout=timedelta(seconds=30),
439
+ )
440
+
441
+ workflow.logger.info(
442
+ f"Agent execution workflow completed with HITL",
443
+ extra={
444
+ "execution_id": input.execution_id,
445
+ "status": final_status,
446
+ "conversation_turns": conversation_turn,
447
+ }
448
+ )
449
+
450
+ return {
451
+ "success": final_status == "completed",
452
+ "execution_id": input.execution_id,
453
+ "status": final_status,
454
+ "response": self._state.current_response,
455
+ "usage": self._state.usage,
456
+ "conversation_turns": conversation_turn,
457
+ }
458
+
459
+ except Exception as e:
460
+ # Update state with error
461
+ self._state.status = "failed"
462
+ self._state.error_message = str(e)
463
+ self._state.metadata["error_type"] = type(e).__name__
464
+
465
+ workflow.logger.error(
466
+ f"Agent execution workflow failed",
467
+ extra={
468
+ "execution_id": input.execution_id,
469
+ "error": str(e),
470
+ }
471
+ )
472
+
473
+ # Update execution as failed
474
+ try:
475
+ await workflow.execute_activity(
476
+ update_execution_status,
477
+ ActivityUpdateExecutionInput(
478
+ execution_id=input.execution_id,
479
+ status="failed",
480
+ completed_at=workflow.now().isoformat(),
481
+ error_message=f"Workflow error: {str(e)}",
482
+ execution_metadata={
483
+ "workflow_error": True,
484
+ "error_type": type(e).__name__,
485
+ },
486
+ ),
487
+ start_to_close_timeout=timedelta(seconds=30),
488
+ )
489
+
490
+ await workflow.execute_activity(
491
+ update_agent_status,
492
+ ActivityUpdateAgentInput(
493
+ agent_id=input.agent_id,
494
+ organization_id=input.organization_id,
495
+ status="failed",
496
+ last_active_at=workflow.now().isoformat(),
497
+ error_message=str(e),
498
+ ),
499
+ start_to_close_timeout=timedelta(seconds=30),
500
+ )
501
+ except Exception as update_error:
502
+ workflow.logger.error(
503
+ f"Failed to update status after error",
504
+ extra={"error": str(update_error)}
505
+ )
506
+
507
+ raise