diff --git a/bin/dispatcher b/bin/dispatcher index 983eb77..9716cf6 100755 --- a/bin/dispatcher +++ b/bin/dispatcher @@ -1,6 +1,6 @@ #!/usr/bin/env sh ROOT="$(pwd)" -VERSION="1.4.1" +VERSION="1.4.5" cd "$(dirname "$0")" || exit cd "../" # Navigate to the root directory diff --git a/bin/dispatcher.cmd b/bin/dispatcher.cmd index fd2f81b..5f33de0 100755 --- a/bin/dispatcher.cmd +++ b/bin/dispatcher.cmd @@ -33,7 +33,7 @@ set "JAVA_CMD=%JAVA_HOME%\bin\java.exe" @REM Consolidate classpath entries, initialize ROOT and VERSION set "ROOT=%~dp0..\" -set "VERSION=1.4.2" +set "VERSION=1.4.5" set "classpath=%ROOT%target\classes;%ROOT%lib\tinystruct-%VERSION%-jar-with-dependencies.jar;%ROOT%lib\tinystruct-%VERSION%.jar;%ROOT%lib\*;%ROOT%WEB-INF\lib\*;%ROOT%WEB-INF\classes;%USERPROFILE%\.m2\repository\org\tinystruct\tinystruct\%VERSION%\tinystruct-%VERSION%-jar-with-dependencies.jar;%USERPROFILE%\.m2\repository\org\tinystruct\tinystruct\%VERSION%\tinystruct-%VERSION%.jar" @REM Run Java application diff --git a/src/main/java/custom/ai/OpenAI.java b/src/main/java/custom/ai/OpenAI.java index 1b254b4..46d8796 100644 --- a/src/main/java/custom/ai/OpenAI.java +++ b/src/main/java/custom/ai/OpenAI.java @@ -20,27 +20,27 @@ public class OpenAI extends AbstractApplication implements Provider { @Action("openai") public Builder call() throws ApplicationException { - if (this.context.getAttribute("api") == null) { + if (getContext().getAttribute("api") == null) { throw new ApplicationException("API is required"); } Builder payload = null; String contentType = "application/json"; Object image = null, mask = null; - if (this.context.getAttribute("content-type") != null && this.context.getAttribute("content-type").toString().equalsIgnoreCase("multipart/form-data")) { + if (getContext().getAttribute("content-type") != null && getContext().getAttribute("content-type").toString().equalsIgnoreCase("multipart/form-data")) { contentType = "multipart/form-data"; - if ((image = this.context.getAttribute("image")) != null && image.toString().startsWith("data:image/png;base64,")) { + if ((image = getContext().getAttribute("image")) != null && image.toString().startsWith("data:image/png;base64,")) { image = image.toString().substring("data:image/png;base64,".length()); } - if ((mask = this.context.getAttribute("mask")) != null && mask.toString().startsWith("data:image/png;base64,")) { + if ((mask = getContext().getAttribute("mask")) != null && mask.toString().startsWith("data:image/png;base64,")) { mask = mask.toString().substring("data:image/png;base64,".length()); } - } else if (this.context.getAttribute("payload") == null) { + } else if (getContext().getAttribute("payload") == null) { throw new ApplicationException("Payload is required"); } - String api = this.context.getAttribute("api").toString(); + String api = getContext().getAttribute("api").toString(); // Replace YOUR_API_KEY with your actual API key String API_KEY = this.config.get("openai.api_key"); @@ -52,8 +52,8 @@ public Builder call() throws ApplicationException { HttpRequestBuilder builder = new HttpRequestBuilder(); builder.setHeaders(headers).setMethod(Method.POST); - if (this.context.getAttribute("payload") != null) { - payload = (Builder) this.context.getAttribute("payload"); + if (getContext().getAttribute("payload") != null) { + payload = (Builder) getContext().getAttribute("payload"); if (contentType.equalsIgnoreCase("multipart/form-data")) { builder.setParameter("prompt", payload.get("prompt").toString()); builder.setParameter("user", payload.get("user").toString()); diff --git a/src/main/java/custom/ai/SearchAI.java b/src/main/java/custom/ai/SearchAI.java index 94de969..c2e23e0 100644 --- a/src/main/java/custom/ai/SearchAI.java +++ b/src/main/java/custom/ai/SearchAI.java @@ -30,10 +30,10 @@ public class SearchAI extends AbstractApplication implements Provider { @Action("search") @Override public Builder call() throws ApplicationException { - if (this.context.getAttribute("--query") == null) { + if (getContext().getAttribute("--query") == null) { throw new ApplicationException("query is required"); } - String query = this.context.getAttribute("--query").toString().trim(); + String query = getContext().getAttribute("--query").toString().trim(); HttpRequestBuilder builder = new HttpRequestBuilder(); Headers headers = new Headers(); diff --git a/src/main/java/custom/ai/StabilityAI.java b/src/main/java/custom/ai/StabilityAI.java index d0e9e77..9928206 100644 --- a/src/main/java/custom/ai/StabilityAI.java +++ b/src/main/java/custom/ai/StabilityAI.java @@ -17,23 +17,23 @@ public class StabilityAI extends AbstractApplication implements Provider { @Action("stability") public Builder call() throws ApplicationException { - if (this.context.getAttribute("api") == null) { + if (getContext().getAttribute("api") == null) { throw new ApplicationException("API is required"); } Builder payload = null; String contentType = "application/json"; Object image = null; - if (this.context.getAttribute("content-type") != null && this.context.getAttribute("content-type").toString().equalsIgnoreCase("multipart/form-data")) { + if (getContext().getAttribute("content-type") != null && getContext().getAttribute("content-type").toString().equalsIgnoreCase("multipart/form-data")) { contentType = "multipart/form-data"; - if ((image = this.context.getAttribute("image")) != null && image.toString().startsWith("data:image/png;base64,")) { + if ((image = getContext().getAttribute("image")) != null && image.toString().startsWith("data:image/png;base64,")) { image = image.toString().substring("data:image/png;base64,".length()); } - } else if (this.context.getAttribute("payload") == null) { + } else if (getContext().getAttribute("payload") == null) { throw new ApplicationException("Payload is required"); } - String api = this.context.getAttribute("api").toString(); + String api = getContext().getAttribute("api").toString(); // Replace YOUR_API_KEY with your actual API key String API_KEY = this.config.get("stability.api_key"); @@ -47,8 +47,8 @@ public Builder call() throws ApplicationException { builder.setVersion(Version.HTTP1_1); builder.setHeaders(headers).setMethod(Method.POST); - if (this.context.getAttribute("payload") != null) { - payload = (Builder) this.context.getAttribute("payload"); + if (getContext().getAttribute("payload") != null) { + payload = (Builder) getContext().getAttribute("payload"); if (contentType.equalsIgnoreCase("multipart/form-data")) { builder.setParameter("text_prompts[0][text]", payload.get("text_prompts[0][text]").toString()); builder.setParameter("cfg_scale", Float.parseFloat(payload.get("cfg_scale").toString())); diff --git a/src/main/java/custom/application/v1/smalltalk.java b/src/main/java/custom/application/v1/smalltalk.java index 72b05c4..9ebaf5a 100644 --- a/src/main/java/custom/application/v1/smalltalk.java +++ b/src/main/java/custom/application/v1/smalltalk.java @@ -4,6 +4,7 @@ import custom.ai.OpenAI; import custom.ai.SearchAI; import custom.ai.StabilityAI; +import org.apache.logging.log4j.EventLogger; import org.tinystruct.ApplicationContext; import org.tinystruct.ApplicationException; import org.tinystruct.ApplicationRuntimeException; @@ -15,6 +16,8 @@ import org.tinystruct.handler.Reforward; import org.tinystruct.http.*; import org.tinystruct.system.ApplicationManager; +import org.tinystruct.system.Event; +import org.tinystruct.system.EventDispatcher; import org.tinystruct.system.annotation.Action; import org.tinystruct.system.template.variable.Variable; import org.tinystruct.system.util.Matrix; @@ -40,6 +43,7 @@ public class smalltalk extends DistributedMessageQueue implements SessionListene private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s"); private boolean cliMode; private boolean chatGPT; + private static final EventDispatcher dispatcher = EventDispatcher.getInstance(); public void init() { super.init(); @@ -51,6 +55,8 @@ public void init() { SessionManager.getInstance().addListener(this); + dispatcher.registerHandler(SessionCreated.class, event -> System.out.println(event.getPayload())); + ApplicationManager.install(new OpenAI()); ApplicationManager.install(new StabilityAI()); ApplicationManager.install(new SearchAI()); @@ -64,8 +70,8 @@ public void init() { @Action("talk") public smalltalk index() { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); Object meetingCode = request.getSession().getAttribute("meeting_code"); @@ -73,7 +79,7 @@ public smalltalk index() { meetingCode = java.util.UUID.randomUUID().toString(); request.getSession().setAttribute("meeting_code", meetingCode); - System.out.println("New meeting generated:" + meetingCode); + dispatcher.dispatch(new SessionCreated(String.valueOf(meetingCode))); } List session_ids; @@ -116,8 +122,8 @@ public String update(String sessionId) throws ApplicationException { @Action("talk/matrix") public String matrix(String meetingCode) throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); request.headers().add(Header.CACHE_CONTROL.set("no-cache, no-store, max-age=0, must-revalidate")); response.headers().add(Header.CACHE_CONTROL.set("no-cache, no-store, max-age=0, must-revalidate")); @@ -132,15 +138,15 @@ public String matrix(String meetingCode) throws ApplicationException { @Action("talk/join") public Object join(String meetingCode) throws ApplicationException { if (groups.containsKey(meetingCode)) { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); request.getSession().setAttribute("meeting_code", meetingCode); Reforward reforward = new Reforward(request, response); reforward.setDefault("/?q=talk"); return reforward.forward(); } else { - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); response.setStatus(ResponseStatus.NOT_FOUND); return "Invalid meeting code."; } @@ -148,8 +154,8 @@ public Object join(String meetingCode) throws ApplicationException { @Action("talk/start") public Object start(String name) throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); request.getSession().setAttribute("user", name); Object meetingCode = request.getSession().getAttribute("meeting_code"); @@ -166,10 +172,10 @@ public Object start(String name) throws ApplicationException { @Action("talk/command") public String command() { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); final Object meetingCode = request.getSession().getAttribute("meeting_code"); final String sessionId = request.getSession().getId(); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); if (meetingCode != null && sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) { if (request.getSession().getAttribute("user") == null) { @@ -189,7 +195,7 @@ public String command() { @Action("talk/save") public String save() { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); final Object meetingCode = request.getSession().getAttribute("meeting_code"); if (this.groups.containsKey(meetingCode)) { final String sessionId = request.getSession().getId(); @@ -252,7 +258,7 @@ public void run() { } } - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); response.setStatus(ResponseStatus.REQUEST_TIMEOUT); return "{ \"error\": \"expired\" }"; } @@ -639,10 +645,10 @@ private String imageProcessor(ImageProcessorType imageProcessorType, String imag _message.put("prompt", prompt[1]); _message.put("user", prompt[0]); - context.setAttribute("payload", _message); - context.setAttribute("api", IMAGES_GENERATIONS); + getContext().setAttribute("payload", _message); + getContext().setAttribute("api", IMAGES_GENERATIONS); - apiResponse = (Builder) ApplicationManager.call("openai", context); + apiResponse = (Builder) ApplicationManager.call("openai", getContext()); break; case EDITS: // TODO @@ -659,12 +665,12 @@ private String imageProcessor(ImageProcessorType imageProcessorType, String imag _message.put("prompt", prompt[1]); _message.put("user", prompt[0]); - context.setAttribute("content-type", "multipart/form-data"); - context.setAttribute("image", image); - context.setAttribute("payload", _message); - context.setAttribute("api", IMAGES_EDITS); + getContext().setAttribute("content-type", "multipart/form-data"); + getContext().setAttribute("image", image); + getContext().setAttribute("payload", _message); + getContext().setAttribute("api", IMAGES_EDITS); - apiResponse = (Builder) ApplicationManager.call("openai", context); + apiResponse = (Builder) ApplicationManager.call("openai", getContext()); break; case VARIATIONS: // TODO @@ -681,11 +687,11 @@ private String imageProcessor(ImageProcessorType imageProcessorType, String imag _message.put("prompt", prompt[1]); _message.put("user", prompt[0]); - context.setAttribute("image", image); - context.setAttribute("payload", _message); - context.setAttribute("api", IMAGES_VARIATIONS); + getContext().setAttribute("image", image); + getContext().setAttribute("payload", _message); + getContext().setAttribute("api", IMAGES_VARIATIONS); - apiResponse = (Builder) ApplicationManager.call("openai", context); + apiResponse = (Builder) ApplicationManager.call("openai", getContext()); default: break; } @@ -707,21 +713,21 @@ private String imageProcessor(ImageProcessorType imageProcessorType, String imag } public String update() throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); final Object meetingCode = request.getSession().getAttribute("meeting_code"); final String sessionId = request.getSession().getId(); if (meetingCode != null) { return this.update(meetingCode.toString(), sessionId); } - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); response.setStatus(ResponseStatus.REQUEST_TIMEOUT); return "{ \"error\": \"expired\" }"; } public String update(String meetingCode, String sessionId) throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); if (request.getSession().getId().equalsIgnoreCase(sessionId)) { String error = "{ \"error\": \"expired\" }"; if (this.groups.containsKey(meetingCode)) { @@ -733,7 +739,7 @@ public String update(String meetingCode, String sessionId) throws ApplicationExc error = "{ \"error\": \"session-timeout\" }"; } - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); response.setStatus(ResponseStatus.REQUEST_TIMEOUT); return error; } @@ -743,7 +749,7 @@ public String update(String meetingCode, String sessionId) throws ApplicationExc @Action("talk/upload") public String upload() throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); final Object meetingCode = request.getSession().getAttribute("meeting_code"); if (meetingCode == null) throw new ApplicationException("Not allowed to upload any files."); @@ -755,7 +761,7 @@ public String upload() throws ApplicationException { for (FileEntity file : list) { final Builder builder = new Builder(); builder.put("type", file.getContentType()); - builder.put("file", new StringBuilder().append(this.context.getAttribute(HTTP_HOST)).append("files/").append(file.getFilename())); + builder.put("file", new StringBuilder().append(getContext().getAttribute(HTTP_HOST)).append("files/").append(file.getFilename())); final File f = new File(path + File.separator + file.getFilename()); if (!f.exists()) { if (!f.getParentFile().exists()) { @@ -793,8 +799,8 @@ public String upload() throws ApplicationException { } public byte[] download(String fileName, boolean encoded) throws ApplicationException { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); - final Response response = (Response) this.context.getAttribute(HTTP_RESPONSE); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); + final Response response = (Response) getContext().getAttribute(HTTP_RESPONSE); final Object meetingCode = request.getSession().getAttribute("meeting_code"); if (encoded && meetingCode == null) throw new ApplicationException("Not allowed to download any files."); @@ -842,7 +848,7 @@ public byte[] download(String fileName) throws ApplicationException { @Action("talk/topic") public boolean topic() { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); final Object meeting_code = request.getSession().getAttribute("meeting_code"); if (meeting_code != null && request.getParameter("topic") != null) { @@ -854,7 +860,7 @@ public boolean topic() { } protected smalltalk exit() { - final Request request = (Request) this.context.getAttribute(HTTP_REQUEST); + final Request request = (Request) getContext().getAttribute(HTTP_REQUEST); request.getSession().removeAttribute("meeting_code"); return this; } @@ -876,7 +882,7 @@ public void onSessionEvent(SessionEvent arg0) { meetingCode = java.util.UUID.randomUUID().toString(); arg0.getSession().setAttribute("meeting_code", meetingCode); - System.out.println("New meeting generated by HttpSessionListener:" + meetingCode); + dispatcher.dispatch(new SessionCreated(String.valueOf(meetingCode))); } final String sessionId = arg0.getSession().getId();