Skip to content

Commit 4b1e3fd

Browse files
author
jabrock
committed
adding settings and code for changing backend service
1 parent 945a851 commit 4b1e3fd

File tree

7 files changed

+255
-96
lines changed

7 files changed

+255
-96
lines changed

app/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"version": "1.0.0",
44
"description": "Sample Client app showing communication with OCI Generative AI services via Websocket",
55
"dependencies": {
6-
"@oracle/oraclejet": "~16.0.0",
7-
"@oracle/oraclejet-core-pack": "~16.0.0",
6+
"@oracle/oraclejet": "~16.1.0",
7+
"@oracle/oraclejet-core-pack": "~16.1.0",
8+
"@stomp/stompjs": "^7.0.0",
89
"marked": "^4.3.0",
910
"uuid": "^9.0.1"
1011
},
1112
"devDependencies": {
12-
"@oracle/ojet-cli": "~16.0.0",
13-
"@oracle/oraclejet-audit": "^16.0.0",
13+
"@oracle/ojet-cli": "~16.1.0",
14+
"@oracle/oraclejet-audit": "^16.1.0",
1415
"@types/uuid": "^9.0.7",
1516
"extract-zip": "^1.7.0",
1617
"fs-extra": "^8.1.0",

app/path_mapping.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@
380380
"path": "libs/chai/chai.js",
381381
"cdnPath": "chai/chai-4.3.10.min"
382382
}
383+
},
384+
"stompjs": {
385+
"cwd": "node_modules/@stomp/stompjs/bundles",
386+
"debug": {
387+
"src": "stomp.umd.js",
388+
"path": "libs/stompjs/stomp.umd.js"
389+
},
390+
"release": {
391+
"src": "stomp.umd.min.js",
392+
"path": "libs/stompjs/stomp.umd.min.js"
393+
}
383394
}
384395
}
385396
}

app/src/components/content/index.tsx

Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import { InputSearchElement } from "ojs/ojinputsearch";
1212
import { useState, useEffect, useRef } from "preact/hooks";
1313
import * as Questions from "text!./data/questions.json";
1414
import * as Answers from "text!./data/answers.json";
15+
import { initWebSocket } from "./websocket-interface";
16+
import { InitStomp, sendPrompt } from "./stomp-interface";
17+
import { Client } from "@stomp/stompjs";
1518

1619
type ServiceTypes = "text" | "summary" | "sim";
20+
type BackendTypes = "java" | "python";
1721
type Chat = {
1822
id?: number;
1923
question?: string;
@@ -25,12 +29,13 @@ const Content = () => {
2529
const [busy, setBusy] = useState<boolean>(false);
2630
const [summaryResults, setSummaryResults] = useState<string | null>("");
2731
const [summaryPrompt, setSummaryPrompt] = useState<string>();
28-
const [serviceType, setServiceType] = useState<ServiceTypes>("summary");
32+
const [serviceType, setServiceType] = useState<ServiceTypes>("text");
33+
const [backendType, setBackendType] = useState<BackendTypes>("java");
2934
const [settingsOpened, setSettingsOpened] = useState<boolean>(false);
3035
const question = useRef<string>();
3136
const chatData = useRef<Array<object>>([]);
3237
const socket = useRef<WebSocket>();
33-
const [connState, setConnState] = useState<string>("Disconnected");
38+
const [client, setClient] = useState<Client | null>(null);
3439

3540
const messagesDP = useRef(
3641
new MutableArrayDataProvider<MessageToastItem["summary"], MessageToastItem>(
@@ -39,77 +44,6 @@ const Content = () => {
3944
)
4045
);
4146

42-
const gateway = `ws://${window.location.hostname}:1986`;
43-
let sockTimer: any = null;
44-
45-
// setup the websocket connection
46-
const initWebSocket = () => {
47-
console.log("Trying to open a WebSocket connection...");
48-
socket.current = new WebSocket(gateway);
49-
socket.current.binaryType = "arraybuffer";
50-
socket.current.onopen = onOpen;
51-
socket.current.onerror = onError;
52-
socket.current.onclose = onClose;
53-
socket.current.onmessage = onMessage;
54-
};
55-
56-
// handle all messages coming from the websocket service
57-
const onMessage = (event: any) => {
58-
const msg = JSON.parse(event.data);
59-
60-
switch (msg.msgType) {
61-
// switch (Object.keys(msg)[0]) {
62-
case "message":
63-
console.log("message: ", msg.data);
64-
return msg.data;
65-
case "question":
66-
console.log("question: ", msg.data);
67-
return msg.data;
68-
case "summary":
69-
console.log("summary");
70-
setSummaryResults(msg.data);
71-
return;
72-
case "answer":
73-
console.log("answer: ", msg.data);
74-
if (msg.data !== "connected") {
75-
let tempArray = [...chatData.current];
76-
// remove the animation item before adding answer
77-
setBusy(false);
78-
tempArray.pop();
79-
messagesDP.current.data = [];
80-
tempArray.push({
81-
id: tempArray.length as number,
82-
answer: msg.data,
83-
});
84-
chatData.current = tempArray;
85-
setUpdate(chatData.current);
86-
}
87-
return msg.data;
88-
default:
89-
return "unknown";
90-
}
91-
};
92-
93-
const onOpen = () => {
94-
clearInterval(sockTimer);
95-
console.log("Connection opened");
96-
socket.current?.send(
97-
JSON.stringify({ msgType: "message", data: "connected" })
98-
);
99-
setConnState("Connected");
100-
};
101-
102-
// if the connection is lost, wait one minute and try again.
103-
const onError = () => {
104-
sockTimer = setInterval(initWebSocket, 1000 * 60);
105-
};
106-
function onClose() {
107-
console.log("Connection closed");
108-
setConnState("Disconnected");
109-
socket.current ? (socket.current.onclose = () => {}) : null;
110-
socket.current?.close();
111-
}
112-
11347
// Simulation code
11448
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
11549
const runSimulation = async () => {
@@ -138,21 +72,33 @@ const Content = () => {
13872
useEffect(() => {
13973
switch (serviceType) {
14074
case "text":
141-
initWebSocket();
75+
if (backendType === "python") {
76+
initWebSocket(
77+
setSummaryResults,
78+
setBusy,
79+
setUpdate,
80+
messagesDP,
81+
socket,
82+
chatData
83+
);
84+
} else {
85+
setClient(InitStomp(setBusy, setUpdate, messagesDP, chatData));
86+
}
14287
console.log("Running Gen AI");
14388
return;
14489
case "sim":
14590
runSimulation();
14691
console.log("running simulation");
14792
return;
14893
case "summary":
149-
initWebSocket();
94+
// initWebSocket();
15095
console.log("summary loading");
15196
return;
15297
}
15398
return () => {
15499
socket.current ? (socket.current.onclose = () => {}) : null;
155100
socket.current?.close();
101+
client?.deactivate();
156102
};
157103
}, [serviceType]);
158104

@@ -193,9 +139,13 @@ const Content = () => {
193139

194140
// simulating the delay for now just to show what the animation looks like.
195141
setTimeout(() => {
196-
socket.current?.send(
197-
JSON.stringify({ msgType: "question", data: question.current })
198-
);
142+
if (backendType === "python") {
143+
socket.current?.send(
144+
JSON.stringify({ msgType: "question", data: question.current })
145+
);
146+
} else {
147+
sendPrompt(client, question.current!);
148+
}
199149
}, 300);
200150
}
201151
};
@@ -218,7 +168,28 @@ const Content = () => {
218168
setUpdate([]);
219169
chatData.current = [];
220170
setServiceType(service);
221-
toggleDrawer();
171+
//toggleDrawer();
172+
};
173+
const backendTypeChangeHandler = (backend: BackendTypes) => {
174+
setUpdate([]);
175+
chatData.current = [];
176+
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();
222193
};
223194

224195
const clearSummary = () => {
@@ -238,8 +209,10 @@ const Content = () => {
238209
aria-label="Settings Drawer"
239210
>
240211
<Settings
241-
serviceType={serviceType}
242-
serviceChange={serviceTypeChangeHandler}
212+
aiServiceType={serviceType}
213+
backendType={backendType}
214+
aiServiceChange={serviceTypeChangeHandler}
215+
backendChange={backendTypeChangeHandler}
243216
/>
244217
</oj-c-drawer-popup>
245218
<div class="oj-flex-bar oj-flex-item demo-header oj-sm-12">
@@ -257,11 +230,14 @@ const Content = () => {
257230
</div>
258231
</div>
259232
{serviceType === "text" && (
260-
<Chat
261-
data={update}
262-
question={question}
263-
questionChanged={handleQuestionChange}
264-
/>
233+
<>
234+
{/* <oj-button onojAction={sendPrompt}>Send Prompt</oj-button> */}
235+
<Chat
236+
data={update}
237+
question={question}
238+
questionChanged={handleQuestionChange}
239+
/>
240+
</>
265241
)}
266242
{serviceType === "sim" && (
267243
<Simulation

app/src/components/content/settings.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,69 @@ import { CRadiosetElement } from "oj-c/radioset";
66
import MutableArrayDataProvider = require("ojs/ojmutablearraydataprovider");
77

88
type ServiceTypeVal = "text" | "summary" | "sim";
9+
type BackendTypeVal = "java" | "python";
910
type Services = {
1011
label: string;
1112
value: ServiceTypeVal;
1213
};
1314
type Props = {
14-
serviceType: "text" | "summary" | "sim";
15-
serviceChange: (service: ServiceTypeVal) => void;
15+
aiServiceType: ServiceTypeVal;
16+
backendType: BackendTypeVal;
17+
aiServiceChange: (service: ServiceTypeVal) => void;
18+
backendChange: (backend: BackendTypeVal) => void;
1619
};
1720

1821
const serviceTypes = [
1922
{ value: "text", label: "Generative Text" },
2023
{ value: "summary", label: "Summarize" },
2124
{ value: "sim", label: "Simulation" },
2225
];
26+
27+
const backendTypes = [
28+
{ value: "java", label: "Java" },
29+
{ value: "python", label: "Python" },
30+
];
2331
const serviceOptionsDP = new MutableArrayDataProvider<
2432
Services["value"],
2533
Services
2634
>(serviceTypes, { keyAttributes: "value" });
35+
const backendOptionsDP = new MutableArrayDataProvider<
36+
Services["value"],
37+
Services
38+
>(backendTypes, { keyAttributes: "value" });
2739

2840
export const Settings = (props: Props) => {
2941
const handleServiceTypeChange = (event: any) => {
3042
if (event.detail.updatedFrom === "internal")
31-
props.serviceChange(event.detail.value);
43+
props.aiServiceChange(event.detail.value);
44+
};
45+
const handleBackendTypeChange = (event: any) => {
46+
if (event.detail.updatedFrom === "internal")
47+
props.backendChange(event.detail.value);
3248
};
3349

3450
return (
3551
<div class="oj-sm-margin-4x">
3652
<h2 class="oj-typography-heading-sm">Service Settings</h2>
3753
<oj-c-form-layout>
3854
<oj-c-radioset
39-
id="enabledRadioset"
40-
value={props.serviceType}
55+
id="serviceTypeRadioset"
56+
value={props.aiServiceType}
4157
labelHint="AI Service Type"
4258
options={serviceOptionsDP}
4359
onvalueChanged={handleServiceTypeChange}
4460
></oj-c-radioset>
4561
</oj-c-form-layout>
62+
<h2 class="oj-typography-heading-sm">Backend Service Type</h2>
63+
<oj-c-form-layout>
64+
<oj-c-radioset
65+
id="backendTypeRadioset"
66+
value={props.backendType}
67+
labelHint="Backend Service Type"
68+
options={backendOptionsDP}
69+
onvalueChanged={handleBackendTypeChange}
70+
></oj-c-radioset>
71+
</oj-c-form-layout>
4672
</div>
4773
);
4874
};

0 commit comments

Comments
 (0)