1
1
# coding=utf-8
2
2
import json
3
+ import re
3
4
import time
4
5
import uuid
5
- from typing import Dict
6
+ from typing import Dict , List
6
7
8
+ from application .flow .common import Answer
7
9
from application .flow .i_step_node import NodeResult , INode
8
10
from application .flow .step_node .application_node .i_application_node import IApplicationNode
9
11
from application .models import Chat
@@ -19,7 +21,8 @@ def _is_interrupt_exec(node, node_variable: Dict, workflow_variable: Dict):
19
21
20
22
def _write_context (node_variable : Dict , workflow_variable : Dict , node : INode , workflow , answer : str ):
21
23
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' , {})
23
26
node .context ['is_interrupt_exec' ] = node_variable .get ('is_interrupt_exec' )
24
27
node .context ['message_tokens' ] = result .get ('usage' , {}).get ('prompt_tokens' , 0 )
25
28
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
43
46
answer = ''
44
47
usage = {}
45
48
node_child_node = {}
49
+ application_node_dict = node .context .get ('application_node_dict' , {})
46
50
is_interrupt_exec = False
47
51
for chunk in response :
48
52
# 先把流转成字符串
@@ -61,6 +65,20 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
61
65
answer += content
62
66
node_child_node = {'runtime_node_id' : runtime_node_id , 'chat_record_id' : chat_record_id ,
63
67
'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
+
64
82
yield {'content' : content ,
65
83
'node_type' : node_type ,
66
84
'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
72
90
node_variable ['result' ] = {'usage' : usage }
73
91
node_variable ['is_interrupt_exec' ] = is_interrupt_exec
74
92
node_variable ['child_node' ] = node_child_node
93
+ node_variable ['application_node_dict' ] = application_node_dict
75
94
_write_context (node_variable , workflow_variable , node , workflow , answer )
76
95
77
96
@@ -90,12 +109,43 @@ def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, wor
90
109
_write_context (node_variable , workflow_variable , node , workflow , answer )
91
110
92
111
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
+
93
134
class BaseApplicationNode (IApplicationNode ):
94
- def get_answer_text (self ):
135
+ def get_answer_list (self ) -> List [ Answer ] | None :
95
136
if self .answer_text is None :
96
137
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' ))]
99
149
100
150
def save_context (self , details , workflow_manage ):
101
151
self .context ['answer' ] = details .get ('answer' )
@@ -124,6 +174,8 @@ def execute(self, application_id, message, chat_id, chat_record_id, stream, re_c
124
174
runtime_node_id = child_node .get ('runtime_node_id' )
125
175
record_id = child_node .get ('chat_record_id' )
126
176
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 )
127
179
128
180
response = ChatMessageSerializer (
129
181
data = {'chat_id' : current_chat_id , 'message' : message ,
@@ -181,5 +233,6 @@ def get_details(self, index: int, **kwargs):
181
233
'err_message' : self .err_message ,
182
234
'global_fields' : global_fields ,
183
235
'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' )
185
238
}
0 commit comments