package com.smtaiserver.smtaiserver.javaai.jsonflow.script; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.mozilla.javascript.Context; import org.mozilla.javascript.ImporterTopLevel; import org.mozilla.javascript.ScriptableObject; import com.smtaiserver.smtaiserver.core.SMTAIServerApp; import com.smtservlet.util.Json; import com.smtservlet.util.SMTStatic; public class SMTJsonFlowScriptScope extends ImporterTopLevel { private static Logger _logger = LogManager.getLogger(SMTJsonFlowScriptScope.class); public void __init__(Context cx) throws Exception { // Define some global functions particular to the shell. Note // that these functions are not part of ECMA. initStandardObjects(cx, false); initialStaticVarbs(); String[] funcListArr = new String[]{ "typeOf", "log", "convLLMJson" }; defineRunFunctions(SMTJsonFlowScriptScope.class, funcListArr); //defineProperty("GlobalArgs", GlobalArgs, ScriptableObject.READONLY); //defineProperty("Global", this, ScriptableObject.READONLY); } protected void defineRunFunctions(Class clz, String[] names) { defineFunctionProperties(names, clz, ScriptableObject.DONTENUM); } protected void newStaticVarb(String id, Class clzObj, Object[] args) throws Exception { //_mapId2StaticVarb.put(id, new StaticVarb(this, clzObj, args)); } protected void initialStaticVarbs() throws Exception { // for(Field field : this.getClass().getFields()) // { // if(!ScriptRunLibInf.class.isAssignableFrom(field.getType())) // continue; // // newStaticVarb(field.getName(), field.getType(), null); // } } public String typeOf(Object obj) { if(obj == null) return "NULL"; return obj.getClass().getSimpleName(); } public void log(String type, String message) { switch(type.toUpperCase().charAt(0)) { case 'D': _logger.debug(message); break; case 'W': _logger.warn(message); break; case 'E': _logger.fatal(message); break; default: _logger.info(message); break; } } public Object convLLMJson(String llmJson) { try { Json jsonLLM = SMTStatic.convLLMAnswerToJson(llmJson, true); return SMTAIServerApp.convJsonToJS(jsonLLM); } catch(Exception ex) { return null; } } }