66 @date:2026/1/14 19:14
77 @desc:
88"""
9- import uuid
109
11- from application .models import ChatUserType , Chat
10+ import uuid_utils .compat as uuid
11+ from django .db .models import QuerySet
12+
13+ from application .models import ChatUserType , Chat , ChatRecord , ChatSourceChoices
1214from chat .serializers .chat import ChatSerializers
15+ from knowledge .models import State
1316from trigger .handler .base_task import BaseTriggerTask
17+ from trigger .models import TaskRecord
1418
1519
1620def get_reference (fields , obj ):
@@ -43,15 +47,34 @@ def get_application_execute_parameters(parameter_setting, kwargs):
4347 parameters [field ] = get_field_value (field_setting , kwargs )
4448 api_input_field_list = parameter_setting .get ('api_input_field_list' )
4549 if api_input_field_list :
46- for key , value in api_input_field_list .item ():
50+ for key , value in api_input_field_list .items ():
4751 parameters ['form_data' ][key ] = get_field_value (value , kwargs )
4852 user_input_field_list = parameter_setting .get ('user_input_field_list' )
4953 if user_input_field_list :
50- for key , value in user_input_field_list .item ():
54+ for key , value in user_input_field_list .items ():
5155 parameters ['form_data' ][key ] = get_field_value (value , kwargs )
5256 return parameters
5357
5458
59+ def get_loop_workflow_node (node_list ):
60+ result = []
61+ for item in node_list :
62+ if item .get ('type' ) == 'loop-node' :
63+ for loop_item in item .get ('loop_node_data' ) or []:
64+ for inner_item in loop_item .values ():
65+ result .append (inner_item )
66+ return result
67+
68+
69+ def get_workflow_state (details ):
70+ node_list = details .values ()
71+ all_node = [* node_list , * get_loop_workflow_node (node_list )]
72+ err = any ([True for value in all_node if value .get ('status' ) == 500 and not value .get ('enableException' )])
73+ if err :
74+ return State .FAILURE
75+ return State .SUCCESS
76+
77+
5578class ApplicationTask (BaseTriggerTask ):
5679 def support (self , trigger_task , ** kwargs ):
5780 return trigger_task .get ('source_type' ) == 'APPLICATION'
@@ -61,22 +84,39 @@ def execute(self, trigger_task, **kwargs):
6184 parameters = get_application_execute_parameters (parameter_setting , kwargs )
6285 parameters ['re_chat' ] = False
6386 parameters ['stream' ] = True
64- chat_id = uuid .uuid1 ()
65- chat_user_id = str (uuid .uuid1 ())
87+ chat_id = uuid .uuid7 ()
88+ chat_user_id = str (uuid .uuid7 ())
89+ chat_record_id = str (uuid .uuid7 ())
90+ parameters ['chat_record_id' ] = chat_record_id
6691 application_id = trigger_task .get ('source_id' )
6792 message = parameters .get ('message' )
6893 Chat .objects .get_or_create (id = chat_id , defaults = {
6994 'application_id' : application_id ,
7095 'abstract' : message ,
7196 'chat_user_id' : chat_user_id ,
7297 'chat_user_type' : ChatUserType .ANONYMOUS_USER .value ,
73- 'asker' : {'username' : "游客" }
98+ 'asker' : {'username' : "游客" },
99+ 'source' : {
100+ 'type' : ChatSourceChoices .TRIGGER .value
101+ },
74102 })
75-
76- list (ChatSerializers (data = {
77- "chat_id" : chat_id ,
78- "chat_user_id" : chat_user_id ,
79- 'chat_user_type' : ChatUserType .ANONYMOUS_USER .value ,
80- 'application_id' : application_id ,
81- 'debug' : False
82- }).chat (instance = parameters ))
103+ task_record_id = uuid .uuid7 ()
104+ TaskRecord (id = task_record_id , source_type = "APPLICATION" , source_id = application_id ,
105+ task_record_id = chat_record_id ,
106+ meta = {'chat_id' : chat_id },
107+ state = State .STARTED ).save ()
108+ try :
109+ list (ChatSerializers (data = {
110+ "chat_id" : chat_id ,
111+ "chat_user_id" : chat_user_id ,
112+ 'chat_user_type' : ChatUserType .ANONYMOUS_USER .value ,
113+ 'application_id' : application_id ,
114+ 'source' : {
115+ 'type' : ChatSourceChoices .TRIGGER .value
116+ },
117+ 'debug' : False
118+ }).chat (instance = parameters ))
119+ finally :
120+ chat_record = QuerySet (ChatRecord ).filter (id = chat_record_id ).first ()
121+ state = get_workflow_state (chat_record .details )
122+ QuerySet (TaskRecord ).filter (id = task_record_id ).update (state = state , run_time = chat_record .run_time )
0 commit comments