TangCheng
2025-02-28 d787e447e95c7b897c2cc9c0e832f8d2e5084934
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.smtaiserver.smtaiserver.javaai.jsonflow.node;
 
import java.util.ArrayList;
import java.util.List;
import com.smtaiserver.smtaiserver.javaai.SMTJavaAIError;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowExecArg;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowManager;
import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowNode;
import com.smtservlet.util.Json;
import com.smtservlet.util.SMTJsonWriter;
import com.smtservlet.util.SMTStatic;
 
public class SMTJsonFlowNodeUserAsk extends SMTJsonFlowNode
{
    private static abstract class SMTJsonFlowNodeUserAskItem
    {
        abstract public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge);
        abstract public void afterInstance() throws Exception;
        abstract public SMTJavaAIError executeFlowNode(String title, SMTJsonFlowExecArg execArg) throws Exception;
    }
    
    private static class SMTJsonFlowNodeUserAskItemInput extends SMTJsonFlowNodeUserAskItem
    {
        private String                         _key;
        private List<SMTJsonFlowNode>        _listDownFlowNodes = new ArrayList<>();
        private List<String>                _listOptions = new ArrayList<>();
        
        public SMTJsonFlowNodeUserAskItemInput(Json jsonCond)
        {
            Json jsonValue = Json.read(jsonCond.getJsonPath("value|value", false).asString());
            _key = jsonValue.getJson("key").asString();
            Json jsonOptions = jsonValue.safeGetJson("options");
            for(Json jsonOption : jsonOptions.asJsonList())
            {
                _listOptions.add(jsonOption.asString());
            }
        }
        
        @Override
        public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge)
        {
            _listDownFlowNodes.add(tagNode);
        }
 
        @Override
        public void afterInstance() throws Exception
        {
            if(_listDownFlowNodes.size() == 0)
                throw new Exception("can't find any options downNode in input mode");
        }
 
        @Override
        public SMTJavaAIError executeFlowNode(String title, SMTJsonFlowExecArg execArg) throws Exception
        {
            // 上前端询问
            String replyId = SMTStatic.newUUID();
            SMTJsonWriter jsonWr = new SMTJsonWriter(false);
            jsonWr.addKeyValue("title", "请输入对地图的操作");
            jsonWr.addKeyValue("type", "input-select");
            jsonWr.addKeyValue("reply_id", replyId);
            jsonWr.beginArray("options");
            {
                for(String option : _listOptions)
                {
                    jsonWr.addKeyValue(null, option);
                }
            }
            jsonWr.endArray();
            
            // 等待应答
            Json jsonReply = execArg._tranReq.sendReplyChunkedBlock(replyId, jsonWr.getRootJson());
            if(jsonReply == null)
                return new SMTJavaAIError("流程被用户中断");
            
            // 将用户输入对象设置为当前对象
            String value = jsonReply.getJson("select").asString();
            
            execArg._jsonArgs.set(_key, value);
            
            
            // 将所有子节点添加作为下一轮节点
            for(SMTJsonFlowNode flowNode : _listDownFlowNodes)
            {
                execArg._stackNodeExec.addLast(flowNode.createFlowNodeExec());
            }
            
            return null;
        }
        
    }
    
    private static class SMTJsonFlowNodeUserAskItemSelect extends SMTJsonFlowNodeUserAskItem
    {
        private static class SMTJsonFlowNodeUserAskItemOption
        {
            public String                    _title;
            public String                    _sourceHandle;
            public List<SMTJsonFlowNode>    _listDownNodes = new ArrayList<>();
            
            public SMTJsonFlowNodeUserAskItemOption(Json jsonOption)
            {
                _title = jsonOption.getJson("label").asString();
                _sourceHandle = jsonOption.getJson("id").asString();
            }
            
            public void initEdge(SMTJsonFlowNode tagNode, String sourceHandle)
            {
                if(_sourceHandle.equals(sourceHandle))
                    _listDownNodes.add(tagNode);
            }
            
            public void afterInstance() throws Exception
            {
                if(_listDownNodes.size() == 0)
                    throw new Exception("can't find any options downNode in select mode");
            }
        }
        
        //////////////////////////////////////////////////////////////////////////////////////
        
        private List<SMTJsonFlowNodeUserAskItemOption>    _listSelectItem = new ArrayList<>();
        
        public SMTJsonFlowNodeUserAskItemSelect(Json jsonCond)
        {
            for(Json jsonOption : jsonCond.getJson("options").asJsonList())
            {
                SMTJsonFlowNodeUserAskItemOption option = new SMTJsonFlowNodeUserAskItemOption(jsonOption);
                _listSelectItem.add(option);
            }
        }
        
        @Override
        public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge)
        {
            String sourceHandle = jsonEdge.safeGetStr("sourceHandle", "");
            
            for(SMTJsonFlowNodeUserAskItemOption option : _listSelectItem)
            {
                option.initEdge(tagNode, sourceHandle);
            }
        }
        
        @Override
        public void afterInstance() throws Exception
        {
            if(_listSelectItem.size() == 0)
                throw new Exception("can't find any options in select mode");
            
            for(SMTJsonFlowNodeUserAskItemOption option : _listSelectItem)
            {
                option.afterInstance();
            }            
        }
        
        @Override
        public SMTJavaAIError executeFlowNode(String title, SMTJsonFlowExecArg execArg) throws Exception
        {
            // 初始化返回前端的参数
            String replyId = SMTStatic.newUUID();
            SMTJsonWriter jsonWr = new SMTJsonWriter(false);
            jsonWr.addKeyValue("title", title);
            jsonWr.addKeyValue("type", "select");
            jsonWr.addKeyValue("reply_id", replyId);
            jsonWr.beginArray("options");
            for(SMTJsonFlowNodeUserAskItemOption option : _listSelectItem)
            {
                jsonWr.addKeyValue(null, option._title);
            }
            jsonWr.endArray();
            
            // 等待前端响应
            Json jsonReply = execArg._tranReq.sendReplyChunkedBlock(replyId, jsonWr.getRootJson());
            if(jsonReply == null)
                return new SMTJavaAIError("流程被用户中断");
            
            // 根据前端响应做出分流选择
            String selectOpt = jsonReply.safeGetStr("select", "");
            for(SMTJsonFlowNodeUserAskItemOption option : _listSelectItem)
            {
                if(option._title.equals(selectOpt))
                {
                    for(SMTJsonFlowNode flowNode :    option._listDownNodes)
                    {
                        execArg._stackNodeExec.addLast(flowNode.createFlowNodeExec());
                    }
                    
                    return null;
                }
            }
            
            
            return new SMTJavaAIError("流程无法判定");
        }
    }
    
    /////////////////////////////////////////////////////////////////////////////////////
    
    private String                        _title;
    private SMTJsonFlowNodeUserAskItem    _askItem;
    
    @Override
    public void initInstane(SMTJsonFlowManager manager, Json jsonNode) throws Exception
    {
        super.initInstane(manager, jsonNode);
        
        Json jsonConds = jsonNode.getJsonPath("data|group_params|0|params|", false);
        
        for(Json jsonCond : jsonConds.asJsonList())
        {
            String key = jsonCond.getJson("key").asString();
            
            // 获取全局提示信息
            if("output_msg".equals(key))
            {
                Json jsonTitle = jsonCond.getJsonPath("value|msg", false);
                _title = jsonTitle.asString();
            }
            // 获取列表类型配置
            else if("output_result".equals(key))
            {
                String resultType = jsonCond.getJsonPath("value|type", false).asString();
                if("select".equals(resultType))
                {
                    _askItem = new SMTJsonFlowNodeUserAskItemSelect(jsonCond);
                }
                else if("input".equals(resultType))
                {
                    _askItem = new SMTJsonFlowNodeUserAskItemInput(jsonCond);
                }
                else
                {
                    throw new Exception("unkonwn output_result type : " + resultType);
                }
            }
            else
            {
                throw new Exception("unkonwn output_msg key : " + key);
            }
        }
 
    }
    
    @Override
    public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge)
    {
        super.initEdge(tagNode, jsonEdge);
        
        _askItem.initEdge(tagNode, jsonEdge);
    }
    
    public void afterInstance() throws Exception
    {
        super.afterInstance();
        
        _askItem.afterInstance();
 
    }
 
    @Override
    public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg) throws Exception
    {
        SMTJavaAIError error = _askItem.executeFlowNode(_title, execArg);
        return error;
    }
 
}