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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.smtaiserver.smtaiserver.control;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.dom4j.Document;
import org.springframework.web.servlet.ModelAndView;
 
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
import com.smtaiserver.smtaiserver.core.SMTAIServerRequest;
import com.smtaiserver.smtaiserver.core.SMTCheckChatStreamView;
import com.smtaiserver.smtaiserver.database.SMTDatabase;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords;
import com.smtaiserver.smtaiserver.javaai.qwen.agent.SMTQwenAgent;
import com.smtservlet.core.SMTRequest;
import com.smtservlet.util.SMTJsonWriter;
import com.smtservlet.util.SMTStatic;
 
public class SMTWorkflowManagerControl
{
    private static String[] _flowFuncNames = new String[] {
        "assessment",        "评估"
    };
    
    public ModelAndView publishWorkflowAgent(SMTAIServerRequest tranReq) throws Exception 
    {
        String id = tranReq.convParamToString("agent_id", true);
        String publish = tranReq.convParamToString("publish", true);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT * FROM ai_agent_workflow WHERE agent_id=?", new Object[] {
                id
            });
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现工作流id", null);
            
            String errmsg = null;
            
            // 检查工作流是否合法,如果不合法直接设置为"N"
            try
            {
                DBRecord rec = recs.getRecord(0);
                SMTQwenAgent agent = (SMTQwenAgent) Class.forName(rec.getString("clz_name")).newInstance();
                agent.initInstance(rec);
            }
            catch(Exception ex)
            {
                errmsg = "工作流配置错误";
                publish = "N";
            }
            
            db.executeSQL("UPDATE ai_agent_workflow SET agent_order=? WHERE agent_id=?", new Object[] {
                "Y".equals(publish) ? 1 : 0,
                id
            });
            
            String curPublish = (recs.getRecord(0).getInteger("agent_order") > 0) ? "Y" : "N";
            if(!publish.equals(curPublish))
                SMTAIServerApp.getApp().clearQwenAgentManager();
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, errmsg, null);
            jsonWr.addKeyValue("publish", publish);
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    } 
    
    public ModelAndView getFlowJsonFuncNames(SMTAIServerRequest tranReq) throws Exception 
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        jsonWr.beginArray("funcs");
        {
            for(int i = 0; i < _flowFuncNames.length; i += 2)
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", _flowFuncNames[i + 0]);
                    jsonWr.addKeyValue("title", _flowFuncNames[i + 1]);
                }
                jsonWr.endMap();
            }
        }
        jsonWr.endArray();
        
        return tranReq.returnJson(jsonWr);
    }
    
    public ModelAndView checkWorkflowAgentValidate(SMTAIServerRequest tranReq) throws Exception 
    {
        String agentId = tranReq.convParamToString("agent_id", true);
        String question = tranReq.convParamToString("question", true);
        
        DBRecords recs;
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            recs = db.querySQL("SELECT * FROM ai_agent_workflow WHERE agent_id=?", new Object[] {
                agentId
            });
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现记录", null);    
        }
        finally
        {
            db.close();
        }
        
        DBRecord rec = recs.getRecord(0);
        SMTQwenAgent agent = (SMTQwenAgent) Class.forName(rec.getString("clz_name")).newInstance();
        agent.initInstance(rec);
        Map<String, SMTQwenAgent> mapId2Agent = new HashMap<>();
        mapId2Agent.put(agent.getAgentId(), agent);
        
        return new ModelAndView(new SMTCheckChatStreamView(tranReq, question, mapId2Agent));
 
    }
    
    public ModelAndView getFlowJsonAgentNames(SMTRequest tranReq) throws Exception 
    {
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {    
            DBRecords recs = db.querySQL(
                  " SELECT agent_id, agent_title FROM ("
                + " SELECT agent_id, agent_title FROM ai_agent_amis"
                + " UNION ALL"
                + " SELECT knowlg_id, knowlg_title FROM ai_agent_knowlg"
                + " UNION ALL"
                + " SELECT agent_id, agent_title FROM ai_agent_metrics"
                + " UNION ALL"
                + " SELECT agent_id, agent_title FROM ai_agent_workflow"
                + ") T WHERE agent_title IS NOT NULL ORDER BY agent_title"
                , null);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            jsonWr.beginArray("agents");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("agent_id"));
                    jsonWr.addKeyValue("title", rec.getString("agent_title"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            
               return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }        
 
    }
    
    public ModelAndView updateWorkflowJsonFlow(SMTRequest tranReq) throws Exception 
    {
        String agentId = tranReq.convParamToString("agent_id", true);
        String sJson = tranReq.convParamToString("json_flow", true);
 
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT * FROM ai_agent_workflow WHERE agent_id=?", new Object[] {
                agentId
            });
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现工作流id", null);
 
            db.executeSQL("UPDATE ai_agent_workflow SET clz_arguments = ? WHERE agent_id=?", new Object[] {
                sJson,
                agentId
                });
            
            if(recs.getRecord(0).getInteger("agent_order") > 0)
                SMTAIServerApp.getApp().clearQwenAgentManager();
            
               return tranReq.returnJsonState(true, null, null);
        }
        finally
        {
            db.close();
        }        
    }
    
    public ModelAndView getWorkflowJsonFlow(SMTRequest tranReq) throws Exception 
    {
        String agentId = tranReq.convParamToString("agent_id", true);
 
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {    
            DBRecords recs = db.querySQL("SELECT clz_arguments FROM ai_agent_workflow WHERE agent_id=?", new Object[] {
                agentId
                });
            
            if(recs.getRowCount() == 0)
                throw new Exception("can't find agent");
            
            String sJson = recs.getRecord(0).getString("clz_arguments");
            if(SMTStatic.isNullOrEmpty(sJson))
                return tranReq.returnJsonState(true, null, null);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            jsonWr.addKeyRaw("json_flow", sJson);
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }        
    }
 
    
    public ModelAndView updateWorkflowAgent(SMTRequest tranReq) throws Exception 
    {
        String agentId = tranReq.convParamToString("agent_id", true);
        String title = tranReq.convParamToString("title", true);
        String prompt = tranReq.convParamToString("prompt", true);
 
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {    
            db.executeSQL("UPDATE ai_agent_workflow SET agent_title=?, agent_xml=?, update_time=? WHERE agent_id=?", new Object[] {
                title,
                "<TITLE>" + SMTStatic.convHtmlBodyToString(prompt) + "</TITLE>",
                new Date(),
                agentId
                });
           
            return tranReq.returnJsonState(true, null, null);
        }
        finally
        {
            db.close();
        }        
    }
    
    public ModelAndView deleteWorkflowAgent(SMTRequest tranReq) throws Exception 
    {
        String agentId = tranReq.convParamToString("agent_id", true);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {    
            db.executeSQL("DELETE FROM ai_agent_workflow WHERE agent_id=?", new Object[] {agentId});
           
            return tranReq.returnJsonState(true, null, null);
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView addWorkflowAgent(SMTRequest tranReq) throws Exception 
    {
        String groupId = tranReq.convParamToString("group_id", true);
        String title = tranReq.convParamToString("title", true);
        String prompt = tranReq.convParamToString("prompt", true);
        String innerCall = tranReq.convParamToString("inner_call", true);
        String agentId = "a_" + SMTStatic.newUUID();
        Date time = new Date();
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            db.executeSQL(
                  " INSERT INTO ai_agent_workflow"
                + " (agent_id,agent_group,agent_type,agent_title,agent_xml,clz_name,clz_arguments,agent_order,is_debug,create_user,create_time,update_time,agent_note,inner_call)"
                + " VALUES"
                + " (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                , new Object[] {
                    agentId,
                    groupId,
                    "query",
                    title,
                    "<TITLE>" + SMTStatic.convHtmlBodyToString(prompt) + "</TITLE>",
                    "com.smtaiserver.smtaiserver.javaai.qwen.agent.SMTQwenAgentJsonWorkflow",
                    null,
                    0,
                    null,
                    tranReq.getLoginUserId(),
                    time,
                    time,
                    null,
                    innerCall
                });    
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            jsonWr.addKeyValue("agent_id", agentId);
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
 
    }
    
    
    public ModelAndView getWorkflowAgentList(SMTRequest tranReq) throws Exception 
    {
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL(
                  " SELECT A.agent_id, A.agent_title, A.agent_xml, A.clz_arguments, A.create_time, A.update_time, A.agent_note, A.agent_order, A.agent_group, A.inner_call, B.user_name"
                + " FROM (SELECT * FROM ai_agent_workflow WHERE clz_name='com.smtaiserver.smtaiserver.javaai.qwen.agent.SMTQwenAgentJsonWorkflow') "
                + " A LEFT JOIN sys_user_info B ON A.create_user=B.user_id", null);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("agent_id"));
                    jsonWr.addKeyValue("title", rec.getString("agent_title"));
                    jsonWr.addKeyValue("create_user", rec.getString("user_name"));
                    jsonWr.addKeyValue("create_time", rec.getString("create_time"));
                    jsonWr.addKeyValue("note", rec.getString("agent_note"));
                    jsonWr.addKeyValue("agent_group", rec.getString("agent_group"));
                    jsonWr.addKeyValue("inner_call", rec.getString("inner_call"));
                    jsonWr.addKeyValue("published", (rec.getInteger("agent_order") > 0) ? "Y" : "N");
                    
                    jsonWr.beginMap("supervisor");
                    {
                        Document doc = SMTStatic.convStrToXmlDoc("<ROOT>" + rec.getString("agent_xml") + "</ROOT>");
                        String prompt = SMTStatic.trimStrLines(doc.selectSingleNode("ROOT/TITLE").getText());
                        jsonWr.addKeyValue("prompt", prompt);
                    }
                    jsonWr.endMap();
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
 
    public ModelAndView getLLMInfoList(SMTRequest tranReq) throws Exception 
    {
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recsFactory = db.querySQL("SELECT * FROM ai_llm_factory", null);
            DBRecords recsConnection = db.querySQL("SELECT * FROM ai_llm_connect", null);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            jsonWr.beginArray("factorys");
            for(DBRecord rec : recsFactory.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("factory_id", rec.getString("factory_id"));
                    jsonWr.addKeyValue("factory_title", rec.getString("factory_title"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            jsonWr.beginArray("connections");
            for(DBRecord rec : recsConnection.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("connect_id", rec.getString("connect_id"));
                    jsonWr.addKeyValue("factory_id", rec.getString("factory_id"));
                    jsonWr.addKeyValue("connect_title", rec.getString("connect_title"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
 
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
 
}