Skip to content

Commit bc68cfb

Browse files
committed
fix: get apikey from localstorage
1 parent cb04644 commit bc68cfb

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Actions from '@cocreate/actions'
77
// object: { _id: '' },
88
// })
99

10-
console.log('test', apifromCrud)
11-
const apiKey = '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';
10+
// console.log('test', apifromCrud)
11+
const apiKey = localStorage.getItem('openAiKey');
1212
const apiUrl = 'https://api.openai.com/v1/chat/completions';
1313
const model = 'gpt-3.5-turbo'
1414
const max_tokens = 1024;
@@ -151,12 +151,20 @@ async function send(form) {
151151
// 3 types avialable system, user, assistant
152152
for (let element of elements) {
153153
let role = element.getAttribute('openai')
154-
let content = await element.getValue()
154+
let content
155+
if (element.hasAttribute('crud')) {
156+
// let json = JSON.parse(element.getAttribute('crud'))
157+
// content = await crud.send()
158+
} else {
159+
content = await element.getValue()
160+
}
155161

156162
// if (role === 'system' && !systemMessages.has(content)) {
157163
// systemMessages.set(content, true)
158164

159165
if (role === 'system') {
166+
conversation.push({ role, content })
167+
160168
// if (content === 'componentsReference')
161169
// conversation.push({ role, content: JSON.stringify(componentsReference) })
162170
// else
@@ -198,18 +206,21 @@ async function sendMessage(messages) {
198206

199207
// const content = JSON.parse(data.choices[0].message.content);
200208
const content = data.choices[0].message.content;
201-
if (content)
209+
if (content) {
202210
console.log(content)
203-
204-
const object = extractObjectFromCode(content);
205-
if (object) {
206-
const { component, action, data } = object;
207-
if (CoCreate[component] && CoCreate[component][action]) {
208-
CoCreate[component][action](data);
209-
} else {
210-
console.error('Invalid CoCreateJS API function:', component, action);
211-
}
211+
let responseElement = document.querySelector('[openai="response"]')
212+
if (responseElement)
213+
responseElement.setValue(content)
212214
}
215+
// const object = extractObjectFromCode(content);
216+
// if (object) {
217+
// const { component, action, data } = object;
218+
// if (CoCreate[component] && CoCreate[component][action]) {
219+
// CoCreate[component][action](data);
220+
// } else {
221+
// console.error('Invalid CoCreateJS API function:', component, action);
222+
// }
223+
// }
213224
} catch (error) {
214225
console.error('Error:', error);
215226
}

0 commit comments

Comments
 (0)