68
68
from models .workflow import (
69
69
Workflow ,
70
70
WorkflowNodeExecution ,
71
- WorkflowRun ,
72
71
WorkflowRunStatus ,
73
72
)
74
73
@@ -104,10 +103,12 @@ def __init__(
104
103
)
105
104
106
105
if isinstance (user , EndUser ):
107
- self ._user_id = user .session_id
106
+ self ._user_id = user .id
107
+ user_session_id = user .session_id
108
108
self ._created_by_role = CreatedByRole .END_USER
109
109
elif isinstance (user , Account ):
110
110
self ._user_id = user .id
111
+ user_session_id = user .id
111
112
self ._created_by_role = CreatedByRole .ACCOUNT
112
113
else :
113
114
raise NotImplementedError (f"User type not supported: { type (user )} " )
@@ -125,7 +126,7 @@ def __init__(
125
126
SystemVariableKey .QUERY : message .query ,
126
127
SystemVariableKey .FILES : application_generate_entity .files ,
127
128
SystemVariableKey .CONVERSATION_ID : conversation .id ,
128
- SystemVariableKey .USER_ID : self . _user_id ,
129
+ SystemVariableKey .USER_ID : user_session_id ,
129
130
SystemVariableKey .DIALOGUE_COUNT : dialogue_count ,
130
131
SystemVariableKey .APP_ID : application_generate_entity .app_config .app_id ,
131
132
SystemVariableKey .WORKFLOW_ID : workflow .id ,
@@ -137,6 +138,7 @@ def __init__(
137
138
138
139
self ._conversation_name_generate_thread = None
139
140
self ._recorded_files : list [Mapping [str , Any ]] = []
141
+ self ._workflow_run_id = ""
140
142
141
143
def process (self ) -> Union [ChatbotAppBlockingResponse , Generator [ChatbotAppStreamResponse , None , None ]]:
142
144
"""
@@ -266,7 +268,6 @@ def _process_stream_response(
266
268
"""
267
269
# init fake graph runtime state
268
270
graph_runtime_state : Optional [GraphRuntimeState ] = None
269
- workflow_run : Optional [WorkflowRun ] = None
270
271
271
272
for queue_message in self ._queue_manager .listen ():
272
273
event = queue_message .event
@@ -291,111 +292,163 @@ def _process_stream_response(
291
292
user_id = self ._user_id ,
292
293
created_by_role = self ._created_by_role ,
293
294
)
295
+ self ._workflow_run_id = workflow_run .id
294
296
message = self ._get_message (session = session )
295
297
if not message :
296
298
raise ValueError (f"Message not found: { self ._message_id } " )
297
299
message .workflow_run_id = workflow_run .id
298
- session .commit ()
299
-
300
300
workflow_start_resp = self ._workflow_start_to_stream_response (
301
301
session = session , task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run
302
302
)
303
+ session .commit ()
304
+
303
305
yield workflow_start_resp
304
306
elif isinstance (
305
307
event ,
306
308
QueueNodeRetryEvent ,
307
309
):
308
- if not workflow_run :
310
+ if not self . _workflow_run_id :
309
311
raise ValueError ("workflow run not initialized." )
310
- workflow_node_execution = self ._handle_workflow_node_execution_retried (
311
- workflow_run = workflow_run , event = event
312
- )
313
312
314
- node_retry_resp = self ._workflow_node_retry_to_stream_response (
315
- event = event ,
316
- task_id = self ._application_generate_entity .task_id ,
317
- workflow_node_execution = workflow_node_execution ,
318
- )
313
+ with Session (db .engine ) as session :
314
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
315
+ workflow_node_execution = self ._handle_workflow_node_execution_retried (
316
+ session = session , workflow_run = workflow_run , event = event
317
+ )
318
+ node_retry_resp = self ._workflow_node_retry_to_stream_response (
319
+ session = session ,
320
+ event = event ,
321
+ task_id = self ._application_generate_entity .task_id ,
322
+ workflow_node_execution = workflow_node_execution ,
323
+ )
324
+ session .commit ()
319
325
320
326
if node_retry_resp :
321
327
yield node_retry_resp
322
328
elif isinstance (event , QueueNodeStartedEvent ):
323
- if not workflow_run :
329
+ if not self . _workflow_run_id :
324
330
raise ValueError ("workflow run not initialized." )
325
331
326
- workflow_node_execution = self ._handle_node_execution_start (workflow_run = workflow_run , event = event )
332
+ with Session (db .engine ) as session :
333
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
334
+ workflow_node_execution = self ._handle_node_execution_start (
335
+ session = session , workflow_run = workflow_run , event = event
336
+ )
327
337
328
- node_start_resp = self ._workflow_node_start_to_stream_response (
329
- event = event ,
330
- task_id = self ._application_generate_entity .task_id ,
331
- workflow_node_execution = workflow_node_execution ,
332
- )
338
+ node_start_resp = self ._workflow_node_start_to_stream_response (
339
+ session = session ,
340
+ event = event ,
341
+ task_id = self ._application_generate_entity .task_id ,
342
+ workflow_node_execution = workflow_node_execution ,
343
+ )
344
+ session .commit ()
333
345
334
346
if node_start_resp :
335
347
yield node_start_resp
336
348
elif isinstance (event , QueueNodeSucceededEvent ):
337
- workflow_node_execution = self ._handle_workflow_node_execution_success (event )
338
-
339
349
# Record files if it's an answer node or end node
340
350
if event .node_type in [NodeType .ANSWER , NodeType .END ]:
341
351
self ._recorded_files .extend (self ._fetch_files_from_node_outputs (event .outputs or {}))
342
352
343
- node_finish_resp = self ._workflow_node_finish_to_stream_response (
344
- event = event ,
345
- task_id = self ._application_generate_entity .task_id ,
346
- workflow_node_execution = workflow_node_execution ,
347
- )
353
+ with Session (db .engine ) as session :
354
+ workflow_node_execution = self ._handle_workflow_node_execution_success (session = session , event = event )
355
+
356
+ node_finish_resp = self ._workflow_node_finish_to_stream_response (
357
+ session = session ,
358
+ event = event ,
359
+ task_id = self ._application_generate_entity .task_id ,
360
+ workflow_node_execution = workflow_node_execution ,
361
+ )
362
+ session .commit ()
348
363
349
364
if node_finish_resp :
350
365
yield node_finish_resp
351
366
elif isinstance (event , QueueNodeFailedEvent | QueueNodeInIterationFailedEvent | QueueNodeExceptionEvent ):
352
- workflow_node_execution = self ._handle_workflow_node_execution_failed (event )
367
+ with Session (db .engine ) as session :
368
+ workflow_node_execution = self ._handle_workflow_node_execution_failed (session = session , event = event )
369
+
370
+ node_finish_resp = self ._workflow_node_finish_to_stream_response (
371
+ session = session ,
372
+ event = event ,
373
+ task_id = self ._application_generate_entity .task_id ,
374
+ workflow_node_execution = workflow_node_execution ,
375
+ )
376
+ session .commit ()
353
377
354
- node_finish_resp = self ._workflow_node_finish_to_stream_response (
355
- event = event ,
356
- task_id = self ._application_generate_entity .task_id ,
357
- workflow_node_execution = workflow_node_execution ,
358
- )
359
378
if node_finish_resp :
360
379
yield node_finish_resp
361
-
362
380
elif isinstance (event , QueueParallelBranchRunStartedEvent ):
363
- if not workflow_run :
381
+ if not self . _workflow_run_id :
364
382
raise ValueError ("workflow run not initialized." )
365
383
366
- yield self ._workflow_parallel_branch_start_to_stream_response (
367
- task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run , event = event
368
- )
384
+ with Session (db .engine ) as session :
385
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
386
+ parallel_start_resp = self ._workflow_parallel_branch_start_to_stream_response (
387
+ session = session ,
388
+ task_id = self ._application_generate_entity .task_id ,
389
+ workflow_run = workflow_run ,
390
+ event = event ,
391
+ )
392
+
393
+ yield parallel_start_resp
369
394
elif isinstance (event , QueueParallelBranchRunSucceededEvent | QueueParallelBranchRunFailedEvent ):
370
- if not workflow_run :
395
+ if not self . _workflow_run_id :
371
396
raise ValueError ("workflow run not initialized." )
372
397
373
- yield self ._workflow_parallel_branch_finished_to_stream_response (
374
- task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run , event = event
375
- )
398
+ with Session (db .engine ) as session :
399
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
400
+ parallel_finish_resp = self ._workflow_parallel_branch_finished_to_stream_response (
401
+ session = session ,
402
+ task_id = self ._application_generate_entity .task_id ,
403
+ workflow_run = workflow_run ,
404
+ event = event ,
405
+ )
406
+
407
+ yield parallel_finish_resp
376
408
elif isinstance (event , QueueIterationStartEvent ):
377
- if not workflow_run :
409
+ if not self . _workflow_run_id :
378
410
raise ValueError ("workflow run not initialized." )
379
411
380
- yield self ._workflow_iteration_start_to_stream_response (
381
- task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run , event = event
382
- )
412
+ with Session (db .engine ) as session :
413
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
414
+ iter_start_resp = self ._workflow_iteration_start_to_stream_response (
415
+ session = session ,
416
+ task_id = self ._application_generate_entity .task_id ,
417
+ workflow_run = workflow_run ,
418
+ event = event ,
419
+ )
420
+
421
+ yield iter_start_resp
383
422
elif isinstance (event , QueueIterationNextEvent ):
384
- if not workflow_run :
423
+ if not self . _workflow_run_id :
385
424
raise ValueError ("workflow run not initialized." )
386
425
387
- yield self ._workflow_iteration_next_to_stream_response (
388
- task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run , event = event
389
- )
426
+ with Session (db .engine ) as session :
427
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
428
+ iter_next_resp = self ._workflow_iteration_next_to_stream_response (
429
+ session = session ,
430
+ task_id = self ._application_generate_entity .task_id ,
431
+ workflow_run = workflow_run ,
432
+ event = event ,
433
+ )
434
+
435
+ yield iter_next_resp
390
436
elif isinstance (event , QueueIterationCompletedEvent ):
391
- if not workflow_run :
437
+ if not self . _workflow_run_id :
392
438
raise ValueError ("workflow run not initialized." )
393
439
394
- yield self ._workflow_iteration_completed_to_stream_response (
395
- task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run , event = event
396
- )
440
+ with Session (db .engine ) as session :
441
+ workflow_run = self ._get_workflow_run (session = session , workflow_run_id = self ._workflow_run_id )
442
+ iter_finish_resp = self ._workflow_iteration_completed_to_stream_response (
443
+ session = session ,
444
+ task_id = self ._application_generate_entity .task_id ,
445
+ workflow_run = workflow_run ,
446
+ event = event ,
447
+ )
448
+
449
+ yield iter_finish_resp
397
450
elif isinstance (event , QueueWorkflowSucceededEvent ):
398
- if not workflow_run :
451
+ if not self . _workflow_run_id :
399
452
raise ValueError ("workflow run not initialized." )
400
453
401
454
if not graph_runtime_state :
@@ -404,7 +457,7 @@ def _process_stream_response(
404
457
with Session (db .engine ) as session :
405
458
workflow_run = self ._handle_workflow_run_success (
406
459
session = session ,
407
- workflow_run = workflow_run ,
460
+ workflow_run_id = self . _workflow_run_id ,
408
461
start_at = graph_runtime_state .start_at ,
409
462
total_tokens = graph_runtime_state .total_tokens ,
410
463
total_steps = graph_runtime_state .node_run_steps ,
@@ -421,16 +474,15 @@ def _process_stream_response(
421
474
yield workflow_finish_resp
422
475
self ._queue_manager .publish (QueueAdvancedChatMessageEndEvent (), PublishFrom .TASK_PIPELINE )
423
476
elif isinstance (event , QueueWorkflowPartialSuccessEvent ):
424
- if not workflow_run :
477
+ if not self . _workflow_run_id :
425
478
raise ValueError ("workflow run not initialized." )
426
-
427
479
if not graph_runtime_state :
428
480
raise ValueError ("graph runtime state not initialized." )
429
481
430
482
with Session (db .engine ) as session :
431
483
workflow_run = self ._handle_workflow_run_partial_success (
432
484
session = session ,
433
- workflow_run = workflow_run ,
485
+ workflow_run_id = self . _workflow_run_id ,
434
486
start_at = graph_runtime_state .start_at ,
435
487
total_tokens = graph_runtime_state .total_tokens ,
436
488
total_steps = graph_runtime_state .node_run_steps ,
@@ -439,7 +491,6 @@ def _process_stream_response(
439
491
conversation_id = None ,
440
492
trace_manager = trace_manager ,
441
493
)
442
-
443
494
workflow_finish_resp = self ._workflow_finish_to_stream_response (
444
495
session = session , task_id = self ._application_generate_entity .task_id , workflow_run = workflow_run
445
496
)
@@ -448,16 +499,15 @@ def _process_stream_response(
448
499
yield workflow_finish_resp
449
500
self ._queue_manager .publish (QueueAdvancedChatMessageEndEvent (), PublishFrom .TASK_PIPELINE )
450
501
elif isinstance (event , QueueWorkflowFailedEvent ):
451
- if not workflow_run :
502
+ if not self . _workflow_run_id :
452
503
raise ValueError ("workflow run not initialized." )
453
-
454
504
if not graph_runtime_state :
455
505
raise ValueError ("graph runtime state not initialized." )
456
506
457
507
with Session (db .engine ) as session :
458
508
workflow_run = self ._handle_workflow_run_failed (
459
509
session = session ,
460
- workflow_run = workflow_run ,
510
+ workflow_run_id = self . _workflow_run_id ,
461
511
start_at = graph_runtime_state .start_at ,
462
512
total_tokens = graph_runtime_state .total_tokens ,
463
513
total_steps = graph_runtime_state .node_run_steps ,
@@ -473,15 +523,16 @@ def _process_stream_response(
473
523
err_event = QueueErrorEvent (error = ValueError (f"Run failed: { workflow_run .error } " ))
474
524
err = self ._handle_error (event = err_event , session = session , message_id = self ._message_id )
475
525
session .commit ()
526
+
476
527
yield workflow_finish_resp
477
528
yield self ._error_to_stream_response (err )
478
529
break
479
530
elif isinstance (event , QueueStopEvent ):
480
- if workflow_run and graph_runtime_state :
531
+ if self . _workflow_run_id and graph_runtime_state :
481
532
with Session (db .engine ) as session :
482
533
workflow_run = self ._handle_workflow_run_failed (
483
534
session = session ,
484
- workflow_run = workflow_run ,
535
+ workflow_run_id = self . _workflow_run_id ,
485
536
start_at = graph_runtime_state .start_at ,
486
537
total_tokens = graph_runtime_state .total_tokens ,
487
538
total_steps = graph_runtime_state .node_run_steps ,
@@ -490,7 +541,6 @@ def _process_stream_response(
490
541
conversation_id = self ._conversation_id ,
491
542
trace_manager = trace_manager ,
492
543
)
493
-
494
544
workflow_finish_resp = self ._workflow_finish_to_stream_response (
495
545
session = session ,
496
546
task_id = self ._application_generate_entity .task_id ,
@@ -499,6 +549,7 @@ def _process_stream_response(
499
549
# Save message
500
550
self ._save_message (session = session , graph_runtime_state = graph_runtime_state )
501
551
session .commit ()
552
+
502
553
yield workflow_finish_resp
503
554
504
555
yield self ._message_end_to_stream_response ()
0 commit comments