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