@@ -68,7 +68,65 @@ const componentsReference = {
6868
6969} ;
7070
71- async function send ( form ) {
71+ async function send ( conversation ) {
72+ try {
73+
74+ let data = await crud . socket . send ( {
75+ method : 'openai.chat' ,
76+ chat : {
77+ messages : conversation ,
78+ max_tokens,
79+ temperature,
80+ n,
81+ stop,
82+ model
83+ }
84+ } )
85+
86+ if ( data ) {
87+ const content = data . chat . choices [ 0 ] . message . content ;
88+ console . log ( content )
89+ let responseElement = document . querySelector ( '[openai="response"]' )
90+ if ( responseElement )
91+ responseElement . setValue ( content )
92+ }
93+
94+ // const object = extractObjectFromCode(content);
95+ // if (object) {
96+ // const { component, action, data } = object;
97+ // if (CoCreate[component] && CoCreate[component][action]) {
98+ // CoCreate[component][action](data);
99+ // } else {
100+ // console.error('Invalid CoCreateJS API function:', component, action);
101+ // }
102+ // }
103+ document . dispatchEvent ( new CustomEvent ( 'openAi' , {
104+ detail : { }
105+ } ) ) ;
106+
107+ } catch ( error ) {
108+ console . error ( 'Error:' , error ) ;
109+ }
110+
111+ }
112+
113+ // Function to extract the object from the generated code
114+ function extractObjectFromCode ( code ) {
115+ try {
116+ const regex = / ( { [ \s \S ] * ?} ) / ;
117+ const matches = code . match ( regex ) ;
118+ if ( matches && matches . length === 1 ) {
119+ const object = JSON . parse ( matches [ 0 ] ) ;
120+ return object ;
121+ }
122+ return null ;
123+ } catch ( error ) {
124+ console . error ( 'Error extracting object from code:' , error ) ;
125+ return null ;
126+ }
127+ }
128+
129+ async function openaiAction ( form ) {
72130 let elements = form . querySelectorAll ( '[openai]' )
73131
74132 let conversation = forms . get ( form )
@@ -193,72 +251,17 @@ async function send(form) {
193251 // } else if (role === 'message' && content)
194252 // conversation.push({ role: 'user', content })
195253 }
196-
197- if ( conversation . length ) {
198- try {
199-
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- }
219-
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- } ) ) ;
232-
233- } catch ( error ) {
234- console . error ( 'Error:' , error ) ;
235- }
236-
237- }
238- }
239-
240- // Function to extract the object from the generated code
241- function extractObjectFromCode ( code ) {
242- try {
243- const regex = / ( { [ \s \S ] * ?} ) / ;
244- const matches = code . match ( regex ) ;
245- if ( matches && matches . length === 1 ) {
246- const object = JSON . parse ( matches [ 0 ] ) ;
247- return object ;
248- }
249- return null ;
250- } catch ( error ) {
251- console . error ( 'Error extracting object from code:' , error ) ;
252- return null ;
253- }
254+ if ( conversation . length )
255+ send ( conversation ) ;
254256}
255257
258+ export default { send }
256259
257260Actions . init ( {
258261 name : "openAi" ,
259262 endEvent : "openAi" ,
260263 callback : ( action ) => {
261- const form = action . element . closest ( " form" ) ;
262- send ( form ) ;
264+ if ( action . form )
265+ openaiAction ( action . form ) ;
263266 }
264267} )
0 commit comments