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 _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 { } }