@@ -9,28 +9,44 @@ import "oj-c/drawer-popup";
9
9
import MutableArrayDataProvider = require( "ojs/ojmutablearraydataprovider" ) ;
10
10
import { MessageToastItem } from "oj-c/message-toast" ;
11
11
import { InputSearchElement } from "ojs/ojinputsearch" ;
12
- import { useState , useEffect , useRef } from "preact/hooks" ;
12
+ import { useState , useEffect , useRef , useContext } 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" ;
18
+ import { ConvoCtx } from "../app" ;
15
19
16
20
type ServiceTypes = "text" | "summary" | "sim" ;
21
+ type BackendTypes = "java" | "python" ;
17
22
type Chat = {
18
23
id ?: number ;
19
24
question ?: string ;
20
25
answer ?: string ;
21
26
loading ?: string ;
22
27
} ;
28
+
29
+ const defaultServiceType : string = localStorage . getItem ( "service" ) || "text" ;
30
+ const defaultBackendType : string = localStorage . getItem ( "backend" ) || "java" ;
31
+
23
32
const Content = ( ) => {
33
+ const conversationId = useContext ( ConvoCtx ) ;
24
34
const [ update , setUpdate ] = useState < Array < object > > ( [ ] ) ;
25
35
const [ busy , setBusy ] = useState < boolean > ( false ) ;
26
- const [ summaryResults , setSummaryResults ] = useState < string | null > ( "" ) ;
36
+ const [ summaryResults , setSummaryResults ] = useState < string > ( "" ) ;
37
+ const [ modelId , setModelId ] = useState < string | null > ( null ) ;
27
38
const [ summaryPrompt , setSummaryPrompt ] = useState < string > ( ) ;
28
- const [ serviceType , setServiceType ] = useState < ServiceTypes > ( "summary" ) ;
39
+ const [ serviceType , setServiceType ] = useState < ServiceTypes > (
40
+ defaultServiceType as ServiceTypes
41
+ ) ;
42
+ const [ backendType , setBackendType ] = useState < BackendTypes > (
43
+ defaultBackendType as BackendTypes
44
+ ) ;
29
45
const [ settingsOpened , setSettingsOpened ] = useState < boolean > ( false ) ;
30
46
const question = useRef < string > ( ) ;
31
47
const chatData = useRef < Array < object > > ( [ ] ) ;
32
48
const socket = useRef < WebSocket > ( ) ;
33
- const [ connState , setConnState ] = useState < string > ( "Disconnected" ) ;
49
+ const [ client , setClient ] = useState < Client | null > ( null ) ;
34
50
35
51
const messagesDP = useRef (
36
52
new MutableArrayDataProvider < MessageToastItem [ "summary" ] , MessageToastItem > (
@@ -39,77 +55,6 @@ const Content = () => {
39
55
)
40
56
) ;
41
57
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
58
// Simulation code
114
59
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
115
60
const runSimulation = async ( ) => {
@@ -121,7 +66,6 @@ const Content = () => {
121
66
if ( Q ) {
122
67
if ( x > 0 ) tempArray . pop ( ) ;
123
68
tempArray . push ( { question : JSON . parse ( Questions ) [ x ] } ) ;
124
- // tempArray.push({ loading: "loading" });
125
69
Q = false ;
126
70
x ++ ;
127
71
} else {
@@ -135,24 +79,52 @@ const Content = () => {
135
79
await sleep ( 2000 ) ;
136
80
}
137
81
} ;
82
+
138
83
useEffect ( ( ) => {
139
84
switch ( serviceType ) {
140
85
case "text" :
141
- initWebSocket ( ) ;
142
- console . log ( "Running Gen AI" ) ;
86
+ if ( backendType === "python" ) {
87
+ initWebSocket (
88
+ setSummaryResults ,
89
+ setBusy ,
90
+ setUpdate ,
91
+ messagesDP ,
92
+ socket ,
93
+ chatData
94
+ ) ;
95
+ } else {
96
+ setClient (
97
+ InitStomp ( setBusy , setUpdate , messagesDP , chatData , serviceType )
98
+ ) ;
99
+ }
100
+ console . log ( "Running Generative service" ) ;
143
101
return ;
144
102
case "sim" :
145
103
runSimulation ( ) ;
146
- console . log ( "running simulation" ) ;
104
+ console . log ( "Running simulation" ) ;
147
105
return ;
148
106
case "summary" :
149
- initWebSocket ( ) ;
150
- console . log ( "summary loading" ) ;
107
+ if ( backendType === "python" ) {
108
+ initWebSocket (
109
+ setSummaryResults ,
110
+ setBusy ,
111
+ setUpdate ,
112
+ messagesDP ,
113
+ socket ,
114
+ chatData
115
+ ) ;
116
+ } else {
117
+ setClient (
118
+ InitStomp ( setBusy , setUpdate , messagesDP , chatData , serviceType )
119
+ ) ;
120
+ }
121
+ console . log ( "Running Summarization service" ) ;
151
122
return ;
152
123
}
153
124
return ( ) => {
154
125
socket . current ? ( socket . current . onclose = ( ) => { } ) : null ;
155
126
socket . current ?. close ( ) ;
127
+ client ?. deactivate ( ) ;
156
128
} ;
157
129
} , [ serviceType ] ) ;
158
130
@@ -168,7 +140,6 @@ const Content = () => {
168
140
autoTimeout : "on" ,
169
141
} ,
170
142
] ;
171
- //alert("Still waiting for an answer! Hang in there a little longer.");
172
143
return ;
173
144
}
174
145
if ( event . detail . value ) {
@@ -191,12 +162,13 @@ const Content = () => {
191
162
setUpdate ( chatData . current ) ;
192
163
setBusy ( true ) ;
193
164
194
- // simulating the delay for now just to show what the animation looks like.
195
- setTimeout ( ( ) => {
165
+ if ( backendType === "python" ) {
196
166
socket . current ?. send (
197
167
JSON . stringify ( { msgType : "question" , data : question . current } )
198
168
) ;
199
- } , 300 ) ;
169
+ } else {
170
+ sendPrompt ( client , question . current ! , modelId ! , conversationId ! ) ;
171
+ }
200
172
}
201
173
} ;
202
174
@@ -215,19 +187,32 @@ const Content = () => {
215
187
} ;
216
188
217
189
const serviceTypeChangeHandler = ( service : ServiceTypes ) => {
190
+ localStorage . setItem ( "service" , service ) ;
218
191
setUpdate ( [ ] ) ;
219
192
chatData . current = [ ] ;
220
193
setServiceType ( service ) ;
221
- toggleDrawer ( ) ;
222
194
} ;
223
-
195
+ const backendTypeChangeHandler = ( backend : BackendTypes ) => {
196
+ setUpdate ( [ ] ) ;
197
+ chatData . current = [ ] ;
198
+ setBackendType ( backend ) ;
199
+ localStorage . setItem ( "backend" , backend ) ;
200
+ location . reload ( ) ;
201
+ } ;
202
+ const modelIdChangeHandler = ( event : CustomEvent ) => {
203
+ console . log ( "model Id: " , event . detail . value ) ;
204
+ if ( event . detail . value != null ) setModelId ( event . detail . value ) ;
205
+ } ;
224
206
const clearSummary = ( ) => {
225
207
setSummaryResults ( "" ) ;
226
208
} ;
227
209
228
210
const updateSummaryPrompt = ( val : string ) => {
229
211
setSummaryPrompt ( val ) ;
230
212
} ;
213
+ const updateSummaryResults = ( summary : string ) => {
214
+ setSummaryResults ( summary ) ;
215
+ } ;
231
216
232
217
return (
233
218
< div class = "oj-web-applayout-max-width oj-web-applayout-content oj-flex oj-sm-flex-direction-column demo-bg-main" >
@@ -238,8 +223,11 @@ const Content = () => {
238
223
aria-label = "Settings Drawer"
239
224
>
240
225
< Settings
241
- serviceType = { serviceType }
242
- serviceChange = { serviceTypeChangeHandler }
226
+ aiServiceType = { serviceType }
227
+ backendType = { backendType }
228
+ aiServiceChange = { serviceTypeChangeHandler }
229
+ backendChange = { backendTypeChangeHandler }
230
+ modelIdChange = { modelIdChangeHandler }
243
231
/>
244
232
</ oj-c-drawer-popup >
245
233
< div class = "oj-flex-bar oj-flex-item demo-header oj-sm-12" >
@@ -248,9 +236,7 @@ const Content = () => {
248
236
position = "top"
249
237
onojClose = { handleToastClose }
250
238
> </ oj-c-message-toast >
251
- { /* <h1 class="oj-typography-heading-lg oj-flex-bar-start"> </h1> */ }
252
239
< div class = "oj-flex-bar-end oj-color-invert demo-header-end" >
253
- { /* <h6 class="oj-sm-margin-2x-end">{connState}</h6> */ }
254
240
< oj-button onojAction = { toggleDrawer } label = "Toggle" display = "icons" >
255
241
< span slot = "startIcon" class = "oj-ux-ico-menu" > </ span >
256
242
</ oj-button >
@@ -273,9 +259,11 @@ const Content = () => {
273
259
{ serviceType === "summary" && (
274
260
< Summary
275
261
fileChanged = { handleFileUpload }
262
+ summaryChanged = { updateSummaryResults }
276
263
summary = { summaryResults }
277
264
clear = { clearSummary }
278
265
prompt = { updateSummaryPrompt }
266
+ backendType = { backendType }
279
267
/>
280
268
) }
281
269
</ div >
0 commit comments