Skip to content

Commit bb8237c

Browse files
author
jabrock
committed
updating services for Java backend
1 parent 4b1e3fd commit bb8237c

File tree

7 files changed

+204
-76
lines changed

7 files changed

+204
-76
lines changed

app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"@oracle/ojet-cli": "~16.1.0",
1414
"@oracle/oraclejet-audit": "^16.1.0",
1515
"@types/uuid": "^9.0.7",
16+
"express-http-proxy": "^2.0.0",
1617
"extract-zip": "^1.7.0",
1718
"fs-extra": "^8.1.0",
1819
"glob": "7.2.0",
1920
"typescript": "5.3.2",
2021
"underscore": "^1.10.2",
22+
"url": "^0.11.3",
2123
"yargs-parser": "13.1.2"
2224
},
2325
"engines": {

app/scripts/hooks/before_serve.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@
55
66
*/
77

8-
'use strict';
8+
"use strict";
99

1010
module.exports = function (configObj) {
1111
return new Promise((resolve, reject) => {
12-
console.log('Running before_serve hook.');
12+
console.log("Running before_serve hook.");
1313
// ojet custom connect and serve options
14-
// { connectOpts, serveOpts } = configObj;
15-
// const express = require('express');
16-
// const http = require('http');
14+
const { connectOpts, serveOpts } = configObj;
15+
const express = require("express");
16+
const http = require("http");
17+
const proxy = require("express-http-proxy");
18+
const url = require("url");
19+
20+
// New hostname+path as specified by question:
21+
const apiProxy = proxy("http://localhost:8080", {
22+
proxyReqPathResolver: (req) => url.parse("/api" + req.url).path,
23+
});
24+
const app = express();
25+
app.use("/api", apiProxy);
1726
// pass back custom http
18-
// configObj['http'] = http;
27+
configObj["http"] = http;
1928
// pass back custom express app
20-
// configObj['express'] = express();
29+
configObj["express"] = app;
2130
// pass back custom options for http.createServer
2231
// const serverOptions = {...};
2332
// configObj['serverOptions'] = serverOptions;

app/src/components/content/index.tsx

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ type Chat = {
2424
answer?: string;
2525
loading?: string;
2626
};
27+
28+
const defaultServiceType: string = localStorage.getItem("service") || "text";
29+
const defaultBackendType: String = localStorage.getItem("backend") || "java";
30+
2731
const Content = () => {
2832
const [update, setUpdate] = useState<Array<object>>([]);
2933
const [busy, setBusy] = useState<boolean>(false);
30-
const [summaryResults, setSummaryResults] = useState<string | null>("");
34+
const [summaryResults, setSummaryResults] = useState<string>("");
3135
const [summaryPrompt, setSummaryPrompt] = useState<string>();
32-
const [serviceType, setServiceType] = useState<ServiceTypes>("text");
33-
const [backendType, setBackendType] = useState<BackendTypes>("java");
36+
const [serviceType, setServiceType] = useState<ServiceTypes>(
37+
defaultServiceType as ServiceTypes
38+
);
39+
const [backendType, setBackendType] = useState<BackendTypes>(
40+
defaultBackendType as BackendTypes
41+
);
3442
const [settingsOpened, setSettingsOpened] = useState<boolean>(false);
3543
const question = useRef<string>();
3644
const chatData = useRef<Array<object>>([]);
@@ -55,7 +63,6 @@ const Content = () => {
5563
if (Q) {
5664
if (x > 0) tempArray.pop();
5765
tempArray.push({ question: JSON.parse(Questions)[x] });
58-
// tempArray.push({ loading: "loading" });
5966
Q = false;
6067
x++;
6168
} else {
@@ -82,17 +89,32 @@ const Content = () => {
8289
chatData
8390
);
8491
} else {
85-
setClient(InitStomp(setBusy, setUpdate, messagesDP, chatData));
92+
setClient(
93+
InitStomp(setBusy, setUpdate, messagesDP, chatData, serviceType)
94+
);
8695
}
87-
console.log("Running Gen AI");
96+
console.log("Running Generative service");
8897
return;
8998
case "sim":
9099
runSimulation();
91-
console.log("running simulation");
100+
console.log("Running simulation");
92101
return;
93102
case "summary":
94-
// initWebSocket();
95-
console.log("summary loading");
103+
if (backendType === "python") {
104+
initWebSocket(
105+
setSummaryResults,
106+
setBusy,
107+
setUpdate,
108+
messagesDP,
109+
socket,
110+
chatData
111+
);
112+
} else {
113+
setClient(
114+
InitStomp(setBusy, setUpdate, messagesDP, chatData, serviceType)
115+
);
116+
}
117+
console.log("Running Summarization service");
96118
return;
97119
}
98120
return () => {
@@ -114,7 +136,6 @@ const Content = () => {
114136
autoTimeout: "on",
115137
},
116138
];
117-
//alert("Still waiting for an answer! Hang in there a little longer.");
118139
return;
119140
}
120141
if (event.detail.value) {
@@ -165,31 +186,17 @@ const Content = () => {
165186
};
166187

167188
const serviceTypeChangeHandler = (service: ServiceTypes) => {
189+
localStorage.setItem("service", service);
168190
setUpdate([]);
169191
chatData.current = [];
170192
setServiceType(service);
171-
//toggleDrawer();
172193
};
173194
const backendTypeChangeHandler = (backend: BackendTypes) => {
174195
setUpdate([]);
175196
chatData.current = [];
176197
setBackendType(backend);
177-
switch (backend) {
178-
case "java":
179-
setClient(InitStomp(setBusy, setUpdate, messagesDP, chatData));
180-
return;
181-
case "python":
182-
initWebSocket(
183-
setSummaryPrompt,
184-
setBusy,
185-
setUpdate,
186-
messagesDP,
187-
socket,
188-
chatData
189-
);
190-
return;
191-
}
192-
//toggleDrawer();
198+
localStorage.setItem("backend", backend);
199+
location.reload();
193200
};
194201

195202
const clearSummary = () => {
@@ -199,6 +206,9 @@ const Content = () => {
199206
const updateSummaryPrompt = (val: string) => {
200207
setSummaryPrompt(val);
201208
};
209+
const updateSummaryResults = (summary: string) => {
210+
setSummaryResults(summary);
211+
};
202212

203213
return (
204214
<div class="oj-web-applayout-max-width oj-web-applayout-content oj-flex oj-sm-flex-direction-column demo-bg-main">
@@ -221,23 +231,18 @@ const Content = () => {
221231
position="top"
222232
onojClose={handleToastClose}
223233
></oj-c-message-toast>
224-
{/* <h1 class="oj-typography-heading-lg oj-flex-bar-start"> </h1> */}
225234
<div class="oj-flex-bar-end oj-color-invert demo-header-end">
226-
{/* <h6 class="oj-sm-margin-2x-end">{connState}</h6> */}
227235
<oj-button onojAction={toggleDrawer} label="Toggle" display="icons">
228236
<span slot="startIcon" class="oj-ux-ico-menu"></span>
229237
</oj-button>
230238
</div>
231239
</div>
232240
{serviceType === "text" && (
233-
<>
234-
{/* <oj-button onojAction={sendPrompt}>Send Prompt</oj-button> */}
235-
<Chat
236-
data={update}
237-
question={question}
238-
questionChanged={handleQuestionChange}
239-
/>
240-
</>
241+
<Chat
242+
data={update}
243+
question={question}
244+
questionChanged={handleQuestionChange}
245+
/>
241246
)}
242247
{serviceType === "sim" && (
243248
<Simulation
@@ -249,9 +254,11 @@ const Content = () => {
249254
{serviceType === "summary" && (
250255
<Summary
251256
fileChanged={handleFileUpload}
257+
summaryChanged={updateSummaryResults}
252258
summary={summaryResults}
253259
clear={clearSummary}
254260
prompt={updateSummaryPrompt}
261+
backendType={backendType}
255262
/>
256263
)}
257264
</div>

app/src/components/content/settings.tsx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import "preact";
2-
import { useState } from "preact/hooks";
2+
import { useEffect, useRef } from "preact/hooks";
33
import "oj-c/radioset";
44
import "oj-c/form-layout";
5+
import "oj-c/select-single";
56
import { CRadiosetElement } from "oj-c/radioset";
67
import MutableArrayDataProvider = require("ojs/ojmutablearraydataprovider");
78

@@ -47,28 +48,63 @@ export const Settings = (props: Props) => {
4748
props.backendChange(event.detail.value);
4849
};
4950

51+
const modelDP = useRef(
52+
new MutableArrayDataProvider<string, {}>([], { keyAttributes: "id" })
53+
);
54+
55+
const fetchModels = async () => {
56+
try {
57+
const response = await fetch("/api/genai/models");
58+
if (!response.ok) {
59+
throw new Error(`Response status: ${response.status}`);
60+
}
61+
const json = await response.json();
62+
modelDP.current.data = json;
63+
} catch (error: any) {
64+
console.log(
65+
"Java service not available for fetching list of Models: ",
66+
error.message
67+
);
68+
}
69+
};
70+
71+
useEffect(() => {
72+
fetchModels();
73+
}, []);
5074
return (
5175
<div class="oj-sm-margin-4x">
52-
<h2 class="oj-typography-heading-sm">Service Settings</h2>
76+
<h2 class="oj-typography-heading-sm">AI service types</h2>
5377
<oj-c-form-layout>
5478
<oj-c-radioset
5579
id="serviceTypeRadioset"
5680
value={props.aiServiceType}
57-
labelHint="AI Service Type"
81+
labelHint="AI service options"
5882
options={serviceOptionsDP}
5983
onvalueChanged={handleServiceTypeChange}
6084
></oj-c-radioset>
6185
</oj-c-form-layout>
62-
<h2 class="oj-typography-heading-sm">Backend Service Type</h2>
86+
<h2 class="oj-typography-heading-sm">Backend service types</h2>
6387
<oj-c-form-layout>
6488
<oj-c-radioset
6589
id="backendTypeRadioset"
6690
value={props.backendType}
67-
labelHint="Backend Service Type"
91+
labelHint="Backend options"
6892
options={backendOptionsDP}
6993
onvalueChanged={handleBackendTypeChange}
7094
></oj-c-radioset>
7195
</oj-c-form-layout>
96+
{props.aiServiceType == "summary" && props.backendType == "java" && (
97+
<>
98+
<h2 class="oj-typography-heading-sm">Model options</h2>
99+
<oj-c-form-layout>
100+
<oj-c-select-single
101+
data={modelDP.current}
102+
labelHint={"Model"}
103+
itemText={"name"}
104+
></oj-c-select-single>
105+
</oj-c-form-layout>
106+
</>
107+
)}
72108
</div>
73109
);
74110
};

app/src/components/content/stomp-interface.tsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,48 @@ export const InitStomp = (
66
setBusy: any,
77
setUpdate: any,
88
messagesDP: any,
9-
chatData: any
9+
chatData: any,
10+
serviceType: any
1011
) => {
1112
//const [test, setTest] = useState();
13+
const protocol = window.location.protocol === "http:" ? "ws://" : "wss://";
14+
const hostname =
15+
window.location.hostname === "localhost"
16+
? "localhost:8080"
17+
: window.location.hostname;
18+
const serviceURL = `${protocol}${hostname}/websocket`;
19+
console.log("in the stomp init module");
1220
const client = new Client({
13-
brokerURL: "ws://localhost:8080/websocket",
21+
brokerURL: serviceURL,
1422
onConnect: () => {
15-
client.subscribe("/user/queue/answer", (message: any) => {
16-
console.log("Answer message: ", JSON.parse(message.body).content);
17-
onMessage(message);
18-
});
23+
if (serviceType === "text") {
24+
client.subscribe("/user/queue/answer", (message: any) => {
25+
console.log("Answer message: ", JSON.parse(message.body).content);
26+
onMessage(message);
27+
});
28+
} else if (serviceType === "summary") {
29+
client.subscribe("/user/queue/summary", (message: any) => {
30+
console.log("Summary message: ", JSON.parse(message.body).content);
31+
onMessage(message);
32+
});
33+
}
1934
},
2035
onStompError: (e) => {
2136
console.log("Stomp Error: ", e);
2237
},
2338
onWebSocketError: () => {
2439
console.log("Error connecting to Websocket service");
40+
serviceType === "text"
41+
? client.unsubscribe("/user/queue/answer")
42+
: client.unsubscribe("/user/queue/summary");
2543
client.deactivate();
2644
},
2745
});
2846
client.activate();
2947

3048
const onMessage = (msg: any) => {
3149
let aiAnswer = JSON.parse(msg.body).content;
32-
// console.log("answer: ", aiAnswer);
50+
//console.log("answer: ", aiAnswer);
3351
if (msg.data !== "connected") {
3452
let tempArray = [...chatData.current];
3553
// remove the animation item before adding answer

0 commit comments

Comments
 (0)