Repository navigation
Expand file tree
/
Copy pathintegration.py
More file actions
291 lines (241 loc) · 9.48 KB
/
Copy pathintegration.py
File metadata and controls
291 lines (241 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Plugin Integration Helper
This module shows how to integrate the User-in-Loop plugin system
into existing workflows with minimal code changes.
The key idea is to add ONE LINE at each hook point:
context = await plugins.run_hook(InteractionPoint.XXX, context, task_id)
Example integration in execute_chat_based_planning_pipeline:
# Before (original code):
planning_result = await run_chat_planning_agent(user_input, logger)
# After (with plugin):
context = {"user_input": user_input, "task_id": task_id}
context = await plugins.run_hook(InteractionPoint.BEFORE_PLANNING, context, task_id)
user_input = context.get("requirements", user_input) # May be enhanced
planning_result = await run_chat_planning_agent(user_input, logger)
context["planning_result"] = planning_result
context = await plugins.run_hook(InteractionPoint.AFTER_PLANNING, context, task_id)
if context.get("workflow_cancelled"):
return {"status": "cancelled", "reason": context.get("cancel_reason")}
"""
import asyncio
from typing import Any, Callable, Dict, List, Optional
from datetime import datetime
from .base import (
PluginRegistry,
InteractionPoint,
InteractionRequest,
InteractionResponse,
get_default_registry,
)
class WorkflowPluginIntegration:
"""
Helper class for integrating plugins with workflow execution.
This class bridges the plugin system with the workflow service,
handling the communication between backend and frontend.
Usage in workflow_service.py:
from workflows.plugins.integration import WorkflowPluginIntegration
class WorkflowService:
def __init__(self):
self._plugin_integration = WorkflowPluginIntegration(self)
async def execute_chat_planning(self, task_id, requirements, ...):
# Get context with plugin support
context = self._plugin_integration.create_context(
task_id=task_id,
user_input=requirements,
)
# Run before-planning plugins
context = await self._plugin_integration.run_hook(
InteractionPoint.BEFORE_PLANNING,
context
)
# Continue with (possibly enhanced) requirements
requirements = context.get("requirements", requirements)
...
"""
def __init__(
self, workflow_service: Any, registry: Optional[PluginRegistry] = None
):
"""
Initialize plugin integration.
Args:
workflow_service: The WorkflowService instance
registry: Optional custom plugin registry (uses default if not provided)
"""
self._workflow_service = workflow_service
self._registry = registry or get_default_registry()
# Set up interaction callback
self._registry.set_interaction_callback(self._handle_interaction)
# Pending interactions (task_id -> response_future)
self._pending_interactions: Dict[str, asyncio.Future] = {}
def create_context(self, task_id: str, **kwargs) -> Dict[str, Any]:
"""Create a workflow context with plugin support."""
return {
"task_id": task_id,
"timestamp": datetime.utcnow().isoformat(),
**kwargs,
}
async def run_hook(
self,
hook_point: InteractionPoint,
context: Dict[str, Any],
) -> Dict[str, Any]:
"""
Run plugins at a hook point.
This is the main entry point for plugin execution.
"""
task_id = context.get("task_id")
return await self._registry.run_hook(hook_point, context, task_id)
async def request_interaction(
self,
task_id: str,
request: InteractionRequest,
) -> InteractionResponse:
"""Request one user interaction without running plugin mutation logic."""
return await self._handle_interaction(task_id, request)
async def _handle_interaction(
self,
task_id: str,
request: InteractionRequest,
) -> InteractionResponse:
"""
Handle interaction request from a plugin.
This method:
1. Publishes the interaction request through the workflow event sink
2. Waits for user response (via submit_response)
3. Returns the response to the plugin
"""
# Update task status
task = self._workflow_service.get_task(task_id)
if task:
task.status = "waiting_for_input"
task.pending_interaction = {
"type": request.interaction_type,
"title": request.title,
"description": request.description,
"data": request.data,
"options": request.options,
"required": request.required,
}
# Create future for response (use get_running_loop for Python 3.10+ compatibility)
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
response_future: asyncio.Future = loop.create_future()
self._pending_interactions[task_id] = response_future
# Publish through the service-owned event sink.
await self._workflow_service._broadcast(
task_id,
{
"type": "interaction_required",
"task_id": task_id,
"interaction_type": request.interaction_type,
"title": request.title,
"description": request.description,
"data": request.data,
"options": request.options,
"required": request.required,
"timestamp": datetime.utcnow().isoformat(),
},
)
try:
# Wait for response
response = await asyncio.wait_for(
response_future, timeout=request.timeout_seconds
)
return response
except asyncio.TimeoutError:
# Return timeout response
return InteractionResponse(
action="timeout",
data={},
skipped=True,
)
finally:
# Clean up
self._pending_interactions.pop(task_id, None)
if task:
task.status = "running"
task.pending_interaction = None
def submit_response(
self,
task_id: str,
action: str,
data: Optional[Dict[str, Any]] = None,
skipped: bool = False,
) -> bool:
"""
Submit user's response to a pending interaction.
Called by the API endpoint when user responds.
Args:
task_id: The task ID
action: User's action (e.g., "confirm", "modify", "skip")
data: Response data
skipped: Whether user chose to skip
Returns:
True if response was submitted, False if no pending interaction
"""
future = self._pending_interactions.get(task_id)
if future and not future.done():
response = InteractionResponse(
action=action,
data=data or {},
skipped=skipped,
)
future.set_result(response)
return True
return False
def has_pending_interaction(self, task_id: str) -> bool:
"""Check if a task has a pending interaction."""
return task_id in self._pending_interactions
def cancel_interaction(self, task_id: str) -> bool:
"""Cancel a pending interaction (e.g., when task is cancelled)."""
future = self._pending_interactions.get(task_id)
if future and not future.done():
future.cancel()
self._pending_interactions.pop(task_id, None)
return True
return False
def create_plugin_enabled_wrapper(
original_function: Callable,
before_hooks: List[InteractionPoint],
after_hooks: List[InteractionPoint],
integration: WorkflowPluginIntegration,
) -> Callable:
"""
Create a wrapper that adds plugin hooks around an existing function.
This is useful for wrapping existing workflow functions without
modifying their code.
Example:
# Original function
async def execute_planning(requirements, logger):
...
# Wrap with plugins
execute_planning_with_plugins = create_plugin_enabled_wrapper(
execute_planning,
before_hooks=[InteractionPoint.BEFORE_PLANNING],
after_hooks=[InteractionPoint.AFTER_PLANNING],
integration=plugin_integration,
)
"""
async def wrapper(*args, task_id: str = None, **kwargs):
context = integration.create_context(
task_id=task_id or "unknown",
args=args,
kwargs=kwargs,
)
# Run before hooks
for hook in before_hooks:
context = await integration.run_hook(hook, context)
if context.get("workflow_cancelled"):
return {"status": "cancelled", "reason": context.get("cancel_reason")}
# Execute original function
result = await original_function(*args, **kwargs)
# Run after hooks
context["result"] = result
for hook in after_hooks:
context = await integration.run_hook(hook, context)
if context.get("workflow_cancelled"):
return {"status": "cancelled", "reason": context.get("cancel_reason")}
return result
return wrapper