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
package com.smtaiserver.smtaiserver.javaai.qwen;
 
import java.util.HashMap;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.WebApplicationContext;
 
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
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.extcall.SMTQwenExtCall;
import com.smtservlet.core.SMTApp.SMTStartupInstance;
 
public class SMTQwenApp implements SMTStartupInstance 
{
 
    @Value("${spring.datasource.druid.validation-query}")
    protected String                        _dbAlidationQuery;
    @Value("${spring.datasource.druid.max-active}")
    protected int                            _dbMaxActive;
    @Value("${spring.datasource.druid.initial-size}")
    protected int                            _dbInitialSize;
    @Value("${spring.datasource.druid.min-idle}")
    protected int                            _dbMinIdle;
    @Value("${spring.datasource.druid.max-wait}")
    protected int                            _dbMaxWait;
    @Value("${spring.datasource.druid.time-between-eviction-runs-millis}")
    protected int                            _dbTimeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.druid.min-evictable-idle-time-millis}")
    protected int                            _dbMinEvictableIdleTimeMillis;
    @Value("${spring.datasource.druid.test-while-idle}")
    protected boolean                        _dbTestWhileIdle;
    @Value("${spring.datasource.druid.test-on-borrow}")
    protected boolean                        _dbTestOnBorrow;
    @Value("${spring.datasource.druid.test-on-return}")
    protected boolean                        _dbTestOnReturn;
    @Value("${spring.datasource.druid.pool-prepared-statements}")
    protected boolean                        _dbPoolPreparedStatements;
    @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")
    protected int                            _dbMaxPoolPreparedStatementPerConnectionSize;
    @Value("${hswater.logger.llm}")
    protected boolean                        _traceLLM;
 
    
    static private SMTQwenApp                 _This;
    
    protected String                        _agentToolPromptTemplate;
    protected Map<String, SMTQwenExtCall>    _mapId2ExtCall = new HashMap<>();
    
    public static SMTQwenApp getApp()
    {
        return _This;
    }
    
    public SMTQwenApp()
    {
        _This = this;
        
 
    }
    
    public boolean isTraceLLM()
    {
        return _traceLLM;
    }
    
    public static void initInstance(SMTDatabase db) throws Exception
    {
    }
    
    public String getAgentToolPromptTemplate()
    {
        return _agentToolPromptTemplate;
    }
 
    @Override
    public void initInstance(Object thisApp, WebApplicationContext webApplicationContext) throws Exception
    {
        _agentToolPromptTemplate = (String)SMTAIServerApp.getApp().getGlobalConfig("prompt.agent_tools");
        
        // 从数据库中读取
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            loadExtCallMap(db);
        }
        finally
        {
            db.close();
        }
        
        // 初始化时候加载AgentManager缓存。
        SMTAIServerApp.getApp().getQwenAgentManager();
    }
    
    public SMTQwenExtCall getExtCall(String callId) throws Exception
    {
        SMTQwenExtCall extCall = _mapId2ExtCall.get(callId);
        if(extCall == null)
            throw new Exception("can't find ext call id : " + callId);
 
        return extCall;
    }
 
    protected void loadExtCallMap(SMTDatabase db) throws Exception
    {
        String filterSQL;
        if(SMTAIServerApp.getApp().isAppDebugMode())
        {
            filterSQL = "call_enable='Y' OR call_enable='DEBUG'";
        }
        else
        {
            filterSQL = "call_enable='Y'";
        }
        
        DBRecords recs = db.querySQL("SELECT * FROM ai_chat_ext_call WHERE " + filterSQL, null);
        for(DBRecord rec : recs.getRecords())
        {
            SMTQwenExtCall extCall = (SMTQwenExtCall) Class.forName(rec.getString("call_class")).newInstance();
            extCall.initInstance(rec);
            _mapId2ExtCall.put(extCall.getCallId(), extCall);
        }
    }
 
    @Override
    public void exitInstance() throws Exception
    {
        
    }
 
}