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 _listDownFlowNodes = new ArrayList<>(); private List _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 _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 _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; } }