@@ -194,61 +194,49 @@ async function send(form) {
194194 // conversation.push({ role: 'user', content })
195195 }
196196
197- if ( conversation . length )
198- sendMessage ( conversation )
199- }
197+ if ( conversation . length ) {
198+ try {
200199
201- async function sendMessage ( messages ) {
202- try {
203- const requestOptions = {
204- method : 'POST' ,
205- headers : {
206- 'Authorization' : `Bearer ${ apiKey } ` ,
207- 'Content-Type' : 'application/json' ,
208- } ,
209- body : JSON . stringify ( {
210- messages,
211- max_tokens,
212- temperature,
213- n,
214- stop,
215- model
216- } ) ,
217- } ;
200+ let data = await crud . socket . send ( {
201+ method : 'openai.chat' ,
202+ chat : {
203+ messages : conversation ,
204+ max_tokens,
205+ temperature,
206+ n,
207+ stop,
208+ model
209+ }
210+ } )
211+
212+ if ( data ) {
213+ const content = data . chat . choices [ 0 ] . message . content ;
214+ console . log ( content )
215+ let responseElement = document . querySelector ( '[openai="response"]' )
216+ if ( responseElement )
217+ responseElement . setValue ( content )
218+ }
218219
219- const response = await fetch ( apiUrl , requestOptions ) ;
220- const data = await response . json ( ) ;
220+ // const object = extractObjectFromCode(content);
221+ // if (object) {
222+ // const { component, action, data } = object;
223+ // if (CoCreate[component] && CoCreate[component][action]) {
224+ // CoCreate[component][action](data);
225+ // } else {
226+ // console.error('Invalid CoCreateJS API function:', component, action);
227+ // }
228+ // }
229+ document . dispatchEvent ( new CustomEvent ( 'openAi' , {
230+ detail : { }
231+ } ) ) ;
221232
222- // const content = JSON.parse(data.choices[0].message.content);
223- const content = data . choices [ 0 ] . message . content ;
224- if ( content ) {
225- console . log ( content )
226- let responseElement = document . querySelector ( '[openai="response"]' )
227- if ( responseElement )
228- responseElement . setValue ( content )
233+ } catch ( error ) {
234+ console . error ( 'Error:' , error ) ;
229235 }
230- // const object = extractObjectFromCode(content);
231- // if (object) {
232- // const { component, action, data } = object;
233- // if (CoCreate[component] && CoCreate[component][action]) {
234- // CoCreate[component][action](data);
235- // } else {
236- // console.error('Invalid CoCreateJS API function:', component, action);
237- // }
238- // }
239- document . dispatchEvent ( new CustomEvent ( 'openAi' , {
240- detail : { }
241- } ) ) ;
242236
243- } catch ( error ) {
244- console . error ( 'Error:' , error ) ;
245237 }
246238}
247239
248- // Call the async function
249- // init();
250-
251-
252240// Function to extract the object from the generated code
253241function extractObjectFromCode ( code ) {
254242 try {
0 commit comments