Skip to content

Commit 0b92d57

Browse files
committed
Upgrade smalltalk to sticky with the latest framework.
1 parent fd22fa4 commit 0b92d57

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/main/java/custom/ai/OpenAI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Builder call() throws ApplicationException {
4343
String api = getContext().getAttribute("api").toString();
4444

4545
// Replace YOUR_API_KEY with your actual API key
46-
String API_KEY = this.config.get("openai.api_key");
46+
String API_KEY = getConfiguration().get("openai.api_key");
4747

4848
Headers headers = new Headers();
4949
headers.add(Header.AUTHORIZATION.set("Bearer " + API_KEY));

src/main/java/custom/ai/StabilityAI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Builder call() throws ApplicationException {
3636
String api = getContext().getAttribute("api").toString();
3737

3838
// Replace YOUR_API_KEY with your actual API key
39-
String API_KEY = this.config.get("stability.api_key");
39+
String API_KEY = getConfiguration().get("stability.api_key");
4040

4141
Headers headers = new Headers();
4242
headers.add(Header.AUTHORIZATION.set("Bearer " + API_KEY));
@@ -68,7 +68,7 @@ public Builder call() throws ApplicationException {
6868
}
6969

7070
try {
71-
URLRequest request = new URLRequest(new URL(this.config.get("stability.host") + "/" + api));
71+
URLRequest request = new URLRequest(new URL(getConfiguration().get("stability.host") + "/" + api));
7272
byte[] bytes = request.send(builder);
7373
String response = new String(bytes);
7474
Builder apiResponse = new Builder();

src/main/java/custom/application/v1/smalltalk.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.tinystruct.system.ApplicationManager;
1818
import org.tinystruct.system.EventDispatcher;
1919
import org.tinystruct.system.annotation.Action;
20+
import org.tinystruct.system.template.variable.StringVariable;
2021
import org.tinystruct.system.template.variable.Variable;
2122
import org.tinystruct.system.util.Matrix;
2223
import org.tinystruct.transfer.DistributedMessageQueue;
@@ -59,8 +60,8 @@ public void init() {
5960
ApplicationManager.install(new StabilityAI());
6061
ApplicationManager.install(new SearchAI());
6162

62-
if (this.config.get("default.chat.engine") != null) {
63-
this.chatGPT = !this.config.get("default.chat.engine").equals("gpt-3");
63+
if (getConfiguration().get("default.chat.engine") != null) {
64+
this.chatGPT = !getConfiguration().get("default.chat.engine").equals("gpt-3");
6465
} else {
6566
this.chatGPT = false;
6667
}
@@ -95,10 +96,15 @@ public smalltalk index(Request request, Response response) {
9596
}
9697

9798
this.setVariable("meeting_code", meetingCode.toString());
99+
this.setVariable("meeting_url", this.getLink("talk/join", null) + "/" + meetingCode + "&lang=" + this.getLocale().toLanguageTag());
98100
this.setVariable("session_id", request.getSession().getId());
101+
this.setVariable("start_url", this.getLink("talk/start", null));
102+
this.setVariable("meeting_update_url", this.getLink("talk/update", null) + "/" + meetingCode + "/" + request.getSession().getId());
103+
this.setVariable("meeting_qr_code_url", this.getLink("talk/matrix", null) + "/" + meetingCode);
99104

100105
Variable<?> topic;
101-
if ((topic = SharedVariables.getInstance().getVariable(meetingCode.toString())) != null) {
106+
SharedVariables sharedVariables = SharedVariables.getInstance(meetingCode.toString());
107+
if ((topic = sharedVariables.getVariable(meetingCode.toString())) != null) {
102108
this.setVariable("topic", topic.getValue().toString().replaceAll("[\r\n]", "<br />"), true);
103109
} else {
104110
this.setVariable("topic", "");
@@ -151,6 +157,7 @@ public Object start(String name, Request request, Response response) throws Appl
151157
return reforward.forward();
152158
} else {
153159
this.setVariable("meeting_code", meetingCode.toString());
160+
this.setVariable("meeting_url", this.getLink("talk/join", null) + "/" + meetingCode + "&lang=" + this.getLocale().toLanguageTag());
154161
}
155162

156163
return name;
@@ -248,7 +255,7 @@ public void run() {
248255
@Action("chat")
249256
public void chat() {
250257
this.cliMode = true;
251-
if (this.config.get("openai.api_key") == null || this.config.get("openai.api_key").isEmpty()) {
258+
if (getConfiguration().get("openai.api_key") == null || getConfiguration().get("openai.api_key").isEmpty()) {
252259
String url = "https://platform.openai.com/account/api-keys";
253260

254261
Context ctx = new ApplicationContext();
@@ -265,7 +272,7 @@ public void chat() {
265272
if (console != null) {
266273
char[] chars;
267274
while ((chars = console.readPassword(prompt)) == null || chars.length == 0) ;
268-
this.config.set("openai.api_key", new String(chars));
275+
getConfiguration().set("openai.api_key", new String(chars));
269276
} else {
270277
throw new ApplicationRuntimeException("openai.api_key is required.");
271278
}
@@ -327,7 +334,7 @@ private String chat(String sessionId, String message) throws ApplicationExceptio
327334
*/
328335
private String chatGPT(String sessionId, String message, String image) throws ApplicationException {
329336
// Replace YOUR_API_KEY with your actual API key
330-
String API_URL = this.config.get("openai.api_endpoint") + "/v1/chat/completions";
337+
String API_URL = getConfiguration().get("openai.api_endpoint") + "/v1/chat/completions";
331338

332339
if (!cliMode) message = message.replaceAll("<br>|<br />", "");
333340

@@ -415,7 +422,7 @@ private Builder preprocess(String message) throws ApplicationException {
415422
*/
416423
private String chat(String sessionId, String message, String image) throws ApplicationException {
417424
// Replace YOUR_API_KEY with your actual API key
418-
String API_URL = this.config.get("openai.api_endpoint") + "/v1/completions";
425+
String API_URL = getConfiguration().get("openai.api_endpoint") + "/v1/completions";
419426

420427
if (!cliMode) message = message.replaceAll("<br>|<br />", "");
421428

@@ -680,7 +687,7 @@ public String upload(Request request) throws ApplicationException {
680687
if (meetingCode == null) throw new ApplicationException("Not allowed to upload any files.");
681688

682689
// Create path components to save the file
683-
final String path = this.config.get("system.directory") != null ? this.config.get("system.directory").toString() + "/files" : "files";
690+
final String path = getConfiguration().get("system.directory") != null ? getConfiguration().get("system.directory").toString() + "/files" : "files";
684691

685692
final Builders builders = new Builders();
686693
List<FileEntity> list = request.getAttachments();
@@ -726,7 +733,7 @@ public byte[] download(String fileName, boolean encoded, Request request, Respon
726733
if (encoded && meetingCode == null) throw new ApplicationException("Not allowed to download any files.");
727734

728735
// Create path to download the file
729-
final String fileDir = this.config.get("system.directory") != null ? this.config.get("system.directory") + "/files" : "files";
736+
final String fileDir = getConfiguration().get("system.directory") != null ? getConfiguration().get("system.directory") + "/files" : "files";
730737

731738
// Creating an object of Path class and
732739
// assigning local directory path of file to it
@@ -739,8 +746,7 @@ public byte[] download(String fileName, boolean encoded, Request request, Respon
739746
String mimeType = Files.probeContentType(path);
740747
if (mimeType != null) {
741748
response.addHeader(Header.CONTENT_TYPE.name(), mimeType);
742-
}
743-
else {
749+
} else {
744750
response.addHeader(Header.CONTENT_DISPOSITION.name(), "application/octet-stream;filename=\"" + fileName + "\"");
745751
}
746752

@@ -771,9 +777,10 @@ public byte[] download(String fileName, Request request, Response response) thro
771777
@Action("talk/topic")
772778
public boolean topic(Request request) {
773779
final Object meeting_code = request.getSession().getAttribute("meeting_code");
774-
775780
if (meeting_code != null && request.getParameter("topic") != null) {
776-
this.setSharedVariable(meeting_code.toString(), filter(request.getParameter("topic")));
781+
SharedVariables sharedVariables = SharedVariables.getInstance(meeting_code.toString());
782+
StringVariable variable = new StringVariable(meeting_code.toString(), filter(request.getParameter("topic")));
783+
sharedVariables.setVariable(variable, true);
777784
return true;
778785
}
779786

0 commit comments

Comments
 (0)