@@ -12,8 +12,12 @@ import { InputSearchElement } from "ojs/ojinputsearch";
12
12
import { useState , useEffect , useRef } from "preact/hooks" ;
13
13
import * as Questions from "text!./data/questions.json" ;
14
14
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" ;
15
18
16
19
type ServiceTypes = "text" | "summary" | "sim" ;
20
+ type BackendTypes = "java" | "python" ;
17
21
type Chat = {
18
22
id ?: number ;
19
23
question ?: string ;
@@ -25,12 +29,13 @@ const Content = () => {
25
29
const [ busy , setBusy ] = useState < boolean > ( false ) ;
26
30
const [ summaryResults , setSummaryResults ] = useState < string | null > ( "" ) ;
27
31
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" ) ;
29
34
const [ settingsOpened , setSettingsOpened ] = useState < boolean > ( false ) ;
30
35
const question = useRef < string > ( ) ;
31
36
const chatData = useRef < Array < object > > ( [ ] ) ;
32
37
const socket = useRef < WebSocket > ( ) ;
33
- const [ connState , setConnState ] = useState < string > ( "Disconnected" ) ;
38
+ const [ client , setClient ] = useState < Client | null > ( null ) ;
34
39
35
40
const messagesDP = useRef (
36
41
new MutableArrayDataProvider < MessageToastItem [ "summary" ] , MessageToastItem > (
@@ -39,77 +44,6 @@ const Content = () => {
39
44
)
40
45
) ;
41
46
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
-
113
47
// Simulation code
114
48
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
115
49
const runSimulation = async ( ) => {
@@ -138,21 +72,33 @@ const Content = () => {
138
72
useEffect ( ( ) => {
139
73
switch ( serviceType ) {
140
74
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
+ }
142
87
console . log ( "Running Gen AI" ) ;
143
88
return ;
144
89
case "sim" :
145
90
runSimulation ( ) ;
146
91
console . log ( "running simulation" ) ;
147
92
return ;
148
93
case "summary" :
149
- initWebSocket ( ) ;
94
+ // initWebSocket();
150
95
console . log ( "summary loading" ) ;
151
96
return ;
152
97
}
153
98
return ( ) => {
154
99
socket . current ? ( socket . current . onclose = ( ) => { } ) : null ;
155
100
socket . current ?. close ( ) ;
101
+ client ?. deactivate ( ) ;
156
102
} ;
157
103
} , [ serviceType ] ) ;
158
104
@@ -193,9 +139,13 @@ const Content = () => {
193
139
194
140
// simulating the delay for now just to show what the animation looks like.
195
141
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
+ }
199
149
} , 300 ) ;
200
150
}
201
151
} ;
@@ -218,7 +168,28 @@ const Content = () => {
218
168
setUpdate ( [ ] ) ;
219
169
chatData . current = [ ] ;
220
170
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();
222
193
} ;
223
194
224
195
const clearSummary = ( ) => {
@@ -238,8 +209,10 @@ const Content = () => {
238
209
aria-label = "Settings Drawer"
239
210
>
240
211
< Settings
241
- serviceType = { serviceType }
242
- serviceChange = { serviceTypeChangeHandler }
212
+ aiServiceType = { serviceType }
213
+ backendType = { backendType }
214
+ aiServiceChange = { serviceTypeChangeHandler }
215
+ backendChange = { backendTypeChangeHandler }
243
216
/>
244
217
</ oj-c-drawer-popup >
245
218
< div class = "oj-flex-bar oj-flex-item demo-header oj-sm-12" >
@@ -257,11 +230,14 @@ const Content = () => {
257
230
</ div >
258
231
</ div >
259
232
{ 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
+ </ >
265
241
) }
266
242
{ serviceType === "sim" && (
267
243
< Simulation
0 commit comments