-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathworkflows.py
More file actions
248 lines (207 loc) · 7.07 KB
/
Copy pathworkflows.py
File metadata and controls
248 lines (207 loc) · 7.07 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
"""
Workflows API Routes
Handles paper-to-code and chat-based planning workflows
"""
from fastapi import APIRouter, BackgroundTasks, HTTPException
from services.workflow_service import workflow_service
from models.requests import (
PaperToCodeRequest,
ChatPlanningRequest,
InteractionResponseRequest,
)
from models.responses import TaskResponse
router = APIRouter()
@router.post("/paper-to-code", response_model=TaskResponse)
async def start_paper_to_code(
request: PaperToCodeRequest,
background_tasks: BackgroundTasks,
):
"""
Start a paper-to-code workflow.
Returns a task ID that can be used to track progress via WebSocket.
"""
task = workflow_service.create_task(
session_id=request.session_id,
task_kind="paper" if request.input_type != "url" else "url",
)
# Run workflow in background
background_tasks.add_task(
workflow_service.execute_paper_to_code,
task.task_id,
request.input_source,
request.input_type,
request.enable_indexing,
request.enable_user_interaction,
)
return TaskResponse(
task_id=task.task_id,
session_id=task.session_id,
task_short_id=task.task_short_id or task.task_id[:8],
status="started",
message="Paper-to-code workflow started",
)
@router.post("/chat-planning", response_model=TaskResponse)
async def start_chat_planning(
request: ChatPlanningRequest,
background_tasks: BackgroundTasks,
):
"""
Start a chat-based planning workflow.
Returns a task ID that can be used to track progress via WebSocket.
"""
task = workflow_service.create_task(
session_id=request.session_id,
task_kind="chat",
)
# Run workflow in background
background_tasks.add_task(
workflow_service.execute_chat_planning,
task.task_id,
request.requirements,
request.enable_indexing,
request.enable_user_interaction,
)
return TaskResponse(
task_id=task.task_id,
session_id=task.session_id,
task_short_id=task.task_short_id or task.task_id[:8],
status="started",
message="Chat planning workflow started",
)
@router.get("/status/{task_id}")
async def get_workflow_status(task_id: str):
"""Get the status of a workflow task"""
task = workflow_service.get_task_by_any_id(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
response = {
"task_id": task.task_id,
"session_id": task.session_id,
"task_short_id": task.task_short_id or task.task_id[:8],
"status": task.status,
"progress": task.progress,
"message": task.message,
"result": task.result,
"error": task.error,
"error_details": task.error_details,
"started_at": task.started_at.isoformat() if task.started_at else None,
"completed_at": task.completed_at.isoformat() if task.completed_at else None,
}
# Include pending interaction if waiting for input
if task.status == "waiting_for_input" and task.pending_interaction:
response["pending_interaction"] = task.pending_interaction
return response
@router.post("/cancel/{task_id}")
async def cancel_workflow(task_id: str):
"""Cancel a running workflow"""
success = workflow_service.cancel_task(task_id)
if not success:
raise HTTPException(
status_code=400,
detail="Task not found or cannot be cancelled",
)
return {"status": "cancelled", "task_id": task_id}
@router.post("/respond/{task_id}")
async def respond_to_interaction(task_id: str, request: InteractionResponseRequest):
"""
Submit user's response to a pending interaction.
This is used for User-in-Loop functionality where the workflow
pauses to ask the user for input (e.g., requirement questions,
plan confirmation).
"""
task = workflow_service.get_task_by_any_id(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
if task.status != "waiting_for_input":
raise HTTPException(
status_code=400,
detail=f"Task is not waiting for input (current status: {task.status})",
)
# Check if plugin integration is available
plugin_integration = getattr(workflow_service, "_plugin_integration", None)
if plugin_integration is None:
raise HTTPException(
status_code=501, detail="User-in-Loop plugin system not enabled"
)
success = plugin_integration.submit_response(
task_id=task_id,
action=request.action,
data=request.data,
skipped=request.skipped,
)
if not success:
raise HTTPException(
status_code=400, detail="No pending interaction for this task"
)
return {
"status": "ok",
"task_id": task_id,
"action": request.action,
}
@router.get("/interaction/{task_id}")
async def get_pending_interaction(task_id: str):
"""
Get the pending interaction for a task, if any.
Returns the interaction data that needs user response.
"""
task = workflow_service.get_task_by_any_id(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
if task.status != "waiting_for_input" or not task.pending_interaction:
return {
"has_interaction": False,
"task_id": task_id,
"status": task.status,
}
return {
"has_interaction": True,
"task_id": task_id,
"status": task.status,
"interaction": task.pending_interaction,
}
@router.get("/active")
async def get_active_tasks():
"""
Get all active (running) tasks.
Useful for recovering tasks after page refresh.
"""
active_tasks = workflow_service.get_active_tasks()
return {
"tasks": [
{
"task_id": task.task_id,
"session_id": task.session_id,
"task_short_id": task.task_short_id or task.task_id[:8],
"status": task.status,
"progress": task.progress,
"message": task.message,
"started_at": task.started_at,
}
for task in active_tasks
]
}
@router.get("/recent")
async def get_recent_tasks(limit: int = 10):
"""
Get recent tasks (completed, error, or running).
Useful for task history.
"""
recent_tasks = workflow_service.get_recent_tasks(limit)
return {
"tasks": [
{
"task_id": task.task_id,
"session_id": task.session_id,
"task_short_id": task.task_short_id or task.task_id[:8],
"status": task.status,
"progress": task.progress,
"message": task.message,
"result": task.result,
"error": task.error,
"error_details": task.error_details,
"started_at": task.started_at,
"completed_at": task.completed_at,
}
for task in recent_tasks
]
}