11# coding=utf-8
22import json
3+ import re
34import time
45import uuid
5- from typing import Dict
6+ from typing import Dict , List
67
8+ from application .flow .common import Answer
79from application .flow .i_step_node import NodeResult , INode
810from application .flow .step_node .application_node .i_application_node import IApplicationNode
911from application .models import Chat
@@ -19,7 +21,8 @@ def _is_interrupt_exec(node, node_variable: Dict, workflow_variable: Dict):
1921
2022def _write_context (node_variable : Dict , workflow_variable : Dict , node : INode , workflow , answer : str ):
2123 result = node_variable .get ('result' )
22- node .context ['child_node' ] = node_variable .get ('child_node' )
24+ node .context ['application_node_dict' ] = node_variable .get ('application_node_dict' )
25+ node .context ['node_dict' ] = node_variable .get ('node_dict' , {})
2326 node .context ['is_interrupt_exec' ] = node_variable .get ('is_interrupt_exec' )
2427 node .context ['message_tokens' ] = result .get ('usage' , {}).get ('prompt_tokens' , 0 )
2528 node .context ['answer_tokens' ] = result .get ('usage' , {}).get ('completion_tokens' , 0 )
@@ -43,6 +46,7 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
4346 answer = ''
4447 usage = {}
4548 node_child_node = {}
49+ application_node_dict = node .context .get ('application_node_dict' , {})
4650 is_interrupt_exec = False
4751 for chunk in response :
4852 # 先把流转成字符串
@@ -61,6 +65,20 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
6165 answer += content
6266 node_child_node = {'runtime_node_id' : runtime_node_id , 'chat_record_id' : chat_record_id ,
6367 'child_node' : child_node }
68+
69+ if real_node_id is not None :
70+ application_node = application_node_dict .get (real_node_id , None )
71+ if application_node is None :
72+
73+ application_node_dict [real_node_id ] = {'content' : content ,
74+ 'runtime_node_id' : runtime_node_id ,
75+ 'chat_record_id' : chat_record_id ,
76+ 'child_node' : child_node ,
77+ 'index' : len (application_node_dict ),
78+ 'view_type' : view_type }
79+ else :
80+ application_node ['content' ] += content
81+
6482 yield {'content' : content ,
6583 'node_type' : node_type ,
6684 'runtime_node_id' : runtime_node_id , 'chat_record_id' : chat_record_id ,
@@ -72,6 +90,7 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
7290 node_variable ['result' ] = {'usage' : usage }
7391 node_variable ['is_interrupt_exec' ] = is_interrupt_exec
7492 node_variable ['child_node' ] = node_child_node
93+ node_variable ['application_node_dict' ] = application_node_dict
7594 _write_context (node_variable , workflow_variable , node , workflow , answer )
7695
7796
@@ -90,12 +109,43 @@ def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, wor
90109 _write_context (node_variable , workflow_variable , node , workflow , answer )
91110
92111
112+ def reset_application_node_dict (application_node_dict , runtime_node_id , node_data ):
113+ try :
114+ if application_node_dict is None :
115+ return
116+ for key in application_node_dict :
117+ application_node = application_node_dict [key ]
118+ if application_node .get ('runtime_node_id' ) == runtime_node_id :
119+ content : str = application_node .get ('content' )
120+ match = re .search ('<form_rander>.*?</form_rander>' , content )
121+ if match :
122+ form_setting_str = match .group ().replace ('<form_rander>' , '' ).replace ('</form_rander>' , '' )
123+ form_setting = json .loads (form_setting_str )
124+ form_setting ['is_submit' ] = True
125+ form_setting ['form_data' ] = node_data
126+ value = f'<form_rander>{ json .dumps (form_setting )} </form_rander>'
127+ res = re .sub ('<form_rander>.*?</form_rander>' ,
128+ '${value}' , content )
129+ application_node ['content' ] = res .replace ('${value}' , value )
130+ except Exception as e :
131+ pass
132+
133+
93134class BaseApplicationNode (IApplicationNode ):
94- def get_answer_text (self ):
135+ def get_answer_list (self ) -> List [ Answer ] | None :
95136 if self .answer_text is None :
96137 return None
97- return {'content' : self .answer_text , 'runtime_node_id' : self .runtime_node_id ,
98- 'chat_record_id' : self .workflow_params ['chat_record_id' ], 'child_node' : self .context .get ('child_node' )}
138+ application_node_dict = self .context .get ('application_node_dict' )
139+ if application_node_dict is None :
140+ return [
141+ Answer (self .answer_text , self .view_type , self .runtime_node_id , self .workflow_params ['chat_record_id' ],
142+ self .context .get ('child_node' ))]
143+ else :
144+ return [Answer (n .get ('content' ), n .get ('view_type' ), self .runtime_node_id ,
145+ self .workflow_params ['chat_record_id' ], {'runtime_node_id' : n .get ('runtime_node_id' ),
146+ 'chat_record_id' : n .get ('chat_record_id' )
147+ , 'child_node' : n .get ('child_node' )}) for n in
148+ sorted (application_node_dict .values (), key = lambda item : item .get ('index' ))]
99149
100150 def save_context (self , details , workflow_manage ):
101151 self .context ['answer' ] = details .get ('answer' )
@@ -124,6 +174,8 @@ def execute(self, application_id, message, chat_id, chat_record_id, stream, re_c
124174 runtime_node_id = child_node .get ('runtime_node_id' )
125175 record_id = child_node .get ('chat_record_id' )
126176 child_node_value = child_node .get ('child_node' )
177+ application_node_dict = self .context .get ('application_node_dict' )
178+ reset_application_node_dict (application_node_dict , runtime_node_id , node_data )
127179
128180 response = ChatMessageSerializer (
129181 data = {'chat_id' : current_chat_id , 'message' : message ,
@@ -181,5 +233,6 @@ def get_details(self, index: int, **kwargs):
181233 'err_message' : self .err_message ,
182234 'global_fields' : global_fields ,
183235 'document_list' : self .workflow_manage .document_list ,
184- 'image_list' : self .workflow_manage .image_list
236+ 'image_list' : self .workflow_manage .image_list ,
237+ 'application_node_dict' : self .context .get ('application_node_dict' )
185238 }
0 commit comments