77 @desc:
88"""
99import json
10- from functools import reduce
10+ import time
11+ import traceback
1112
1213import uuid_utils .compat as uuid
1314from django .db .models import QuerySet
1415
1516from application .models import ChatUserType , Chat , ChatRecord , ChatSourceChoices , Application
1617from chat .serializers .chat import ChatSerializers
18+ from common .utils .logger import maxkb_logger
1719from knowledge .models .knowledge_action import State
1820from trigger .handler .base_task import BaseTriggerTask
1921from trigger .models import TaskRecord , TriggerTask
@@ -30,9 +32,7 @@ def get_reference(fields, obj):
3032
3133
3234def conversion_custom_value (value , _type ):
33- if ['array' , 'dict' , 'float' , 'int' , 'boolean' ].__contains__ (_type ):
34- return json .loads (value )
35- if _type == 'any' :
35+ if ['array' , 'dict' , 'float' , 'int' , 'boolean' , 'any' ].__contains__ (_type ):
3636 try :
3737 return json .loads (value )
3838 except Exception as e :
@@ -86,7 +86,7 @@ def get_application_execute_parameters(parameter_setting, application_parameters
8686 _setting = setting .get (ck )
8787 if _setting :
8888 _value = get_field_value (_setting , kwargs , cv .get ('type' ), cv .get ('required' ),
89- cv .get ('default_value' ), cv . get ( 'field' ) )
89+ cv .get ('default_value' ), ck )
9090 parameters ['form_data' ][ck ] = _value
9191 else :
9292 if cv .get ('default_value' ):
@@ -96,7 +96,7 @@ def get_application_execute_parameters(parameter_setting, application_parameters
9696 raise Exception (f'{ ck } is required' )
9797 else :
9898 value = get_field_value (setting , kwargs , value .get ('type' ), value .get ('required' ),
99- value .get ('default_value' ), value . get ( 'field' ) )
99+ value .get ('default_value' ), key )
100100 parameters ['message' if key == 'question' else key ] = value
101101 else :
102102 if value .get ('default_value' ):
@@ -145,7 +145,7 @@ def get_application_parameters_setting(application):
145145 else :
146146 base_node_list = [n for n in application .work_flow .get ('nodes' ) if n .get ('type' ) == "base-node" ]
147147 if len (base_node_list ) == 0 :
148- raise Exception ('错误的应用工作流信息 ' )
148+ raise Exception ('Incorrect application workflow information ' )
149149 base_node = base_node_list [0 ]
150150 api_input_field_list = base_node .get ('properties' ).get ('api_input_field_list' ) or []
151151 api_input_field_list = {user_field .get ('variable' ): {
@@ -178,39 +178,43 @@ def support(self, trigger_task, **kwargs):
178178
179179 def execute (self , trigger_task , ** kwargs ):
180180 parameter_setting = trigger_task .get ('parameter' )
181- application = QuerySet (Application ).filter (id = trigger_task .get ('source_id' )).only ('type' , 'work_flow' ).first ()
182- if application is None :
183- QuerySet (TriggerTask ).filter (id = trigger_task .get ('id' )).delete ()
184- return
185- application_parameters_setting = get_application_parameters_setting (application )
186- parameters = get_application_execute_parameters (parameter_setting , application_parameters_setting , kwargs )
187- parameters ['re_chat' ] = False
188- parameters ['stream' ] = True
189- chat_id = uuid .uuid7 ()
190- chat_user_id = str (uuid .uuid7 ())
191- chat_record_id = str (uuid .uuid7 ())
192- parameters ['chat_record_id' ] = chat_record_id
193- application_id = trigger_task .get ('source_id' )
194- message = parameters .get ('message' )
195- Chat .objects .get_or_create (id = chat_id , defaults = {
196- 'application_id' : application_id ,
197- 'abstract' : message ,
198- 'chat_user_id' : chat_user_id ,
199- 'chat_user_type' : ChatUserType .ANONYMOUS_USER .value ,
200- 'asker' : {'username' : "游客" },
201- 'ip_address' : kwargs .get ('body' )['ip_address' ],
202- 'source' : {
203- 'type' : ChatSourceChoices .TRIGGER .value
204- },
205- })
206181 task_record_id = uuid .uuid7 ()
207- TaskRecord (id = task_record_id , trigger_id = trigger_task .get ('trigger' ), trigger_task_id = trigger_task .get ('id' ),
208- source_type = "APPLICATION" ,
209- source_id = application_id ,
210- task_record_id = chat_record_id ,
211- meta = {'chat_id' : chat_id },
212- state = State .STARTED ).save ()
182+ start_time = time .time ()
213183 try :
184+ application = QuerySet (Application ).filter (id = trigger_task .get ('source_id' )).only ('type' ,
185+ 'work_flow' ).first ()
186+ if application is None :
187+ QuerySet (TriggerTask ).filter (id = trigger_task .get ('id' )).delete ()
188+ return
189+ application_id = trigger_task .get ('source_id' )
190+ chat_id = uuid .uuid7 ()
191+ chat_user_id = str (uuid .uuid7 ())
192+ chat_record_id = str (uuid .uuid7 ())
193+ TaskRecord (id = task_record_id , trigger_id = trigger_task .get ('trigger' ),
194+ trigger_task_id = trigger_task .get ('id' ),
195+ source_type = "APPLICATION" ,
196+ source_id = application_id ,
197+ task_record_id = chat_record_id ,
198+ meta = {'chat_id' : chat_id },
199+ state = State .STARTED ).save ()
200+ application_parameters_setting = get_application_parameters_setting (application )
201+ parameters = get_application_execute_parameters (parameter_setting , application_parameters_setting , kwargs )
202+ parameters ['re_chat' ] = False
203+ parameters ['stream' ] = True
204+ parameters ['chat_record_id' ] = chat_record_id
205+ message = parameters .get ('message' )
206+ Chat .objects .get_or_create (id = chat_id , defaults = {
207+ 'application_id' : application_id ,
208+ 'abstract' : message ,
209+ 'chat_user_id' : chat_user_id ,
210+ 'chat_user_type' : ChatUserType .ANONYMOUS_USER .value ,
211+ 'asker' : {'username' : "游客" },
212+ 'ip_address' : kwargs .get ('body' )['ip_address' ],
213+ 'source' : {
214+ 'type' : ChatSourceChoices .TRIGGER .value
215+ },
216+ })
217+
214218 list (ChatSerializers (data = {
215219 "chat_id" : chat_id ,
216220 "chat_user_id" : chat_user_id ,
@@ -223,8 +227,22 @@ def execute(self, trigger_task, **kwargs):
223227 'debug' : False
224228 }).chat (instance = parameters ))
225229 chat_record = QuerySet (ChatRecord ).filter (id = chat_record_id ).first ()
226- state = get_workflow_state (chat_record .details )
227- QuerySet (TaskRecord ).filter (id = task_record_id ).update (state = state , run_time = chat_record .run_time )
230+ if chat_record :
231+ state = get_workflow_state (chat_record .details )
232+ QuerySet (TaskRecord ).filter (id = task_record_id ).update (state = state , run_time = chat_record .run_time ,
233+ meta = {'parameter_setting' : parameter_setting ,
234+ 'input' : parameters , 'output' : None })
235+ else :
236+ QuerySet (TaskRecord ).filter (id = task_record_id ).update (state = State .FAILURE ,
237+ run_time = time .time () - start_time ,
238+ meta = {'parameter_setting' : parameter_setting ,
239+ 'input' : parameters , 'output' : None ,
240+ 'err_message' : 'Error: An unknown error occurred during the execution of the conversation' })
228241 except Exception as e :
229- state = State .FAILURE
230- QuerySet (TaskRecord ).filter (id = task_record_id ).update (state = state , run_time = 0 )
242+ maxkb_logger .error (f"Application execution error: { traceback .format_exc ()} " )
243+ QuerySet (TaskRecord ).filter (id = task_record_id ).update (
244+ state = State .FAILURE ,
245+ run_time = time .time () - start_time ,
246+ meta = {'input' : {'parameter_setting' : parameter_setting , ** kwargs }, 'output' : None ,
247+ 'err_message' : 'Error: ' + str (e )}
248+ )
0 commit comments