{
models.value.splice(index, 1)
}
const save = async () => {
- console.log(currentModel.value)
if (!currentModel.value) {
showWarning('Please select at least one model.')
return
diff --git a/composables/states.js b/composables/states.js
index 64c7050..d40c2db 100644
--- a/composables/states.js
+++ b/composables/states.js
@@ -1,3 +1,5 @@
-
export const useModels = () => useState('models', () => getStoredModels())
-export const useCurrentModel = () => useState('currentModel', () => getCurrentModel())
\ No newline at end of file
+
+export const useCurrentModel = () => useState('currentModel', () => getCurrentModel())
+
+export const useApiKey = () => useState('apiKey', () => getStoredApiKey())
\ No newline at end of file
diff --git a/server/api/conversation.post.js b/server/api/conversation.post.js
index 9ffb549..425a3da 100644
--- a/server/api/conversation.post.js
+++ b/server/api/conversation.post.js
@@ -1,6 +1,5 @@
import ChatGPTClient from '@waylaidwanderer/chatgpt-api'
import { PassThrough } from 'node:stream'
-import {getSetting, setSetting} from "~/utils/keyv";
const serializeSSEEvent = (chunk) => {
let payload = "";
@@ -24,7 +23,6 @@ const serializeSSEEvent = (chunk) => {
}
export default defineEventHandler(async (event) => {
- const runtimeConfig = useRuntimeConfig()
const body = await readBody(event)
const conversationId = body.conversationId ? body.conversationId.toString() : undefined
const parentMessageId = body.parentMessageId ? body.parentMessageId.toString() : undefined
@@ -38,9 +36,7 @@ export default defineEventHandler(async (event) => {
'Connection': 'keep-alive'
})
- const apiKey = await getSetting('apiKey')
-
- if (!apiKey) {
+ if (!body.openaiApiKey) {
writeToTunnel({
event: 'error',
data: JSON.stringify({
@@ -76,7 +72,7 @@ export default defineEventHandler(async (event) => {
uri: 'sqlite://database.sqlite'
};
- const chatGptClient = new ChatGPTClient(apiKey, clientOptions, cacheOptions);
+ const chatGptClient = new ChatGPTClient(body.openaiApiKey, clientOptions, cacheOptions);
try {
const response = await chatGptClient.sendMessage(body.message, {
diff --git a/utils/enums.js b/utils/enums.js
index ad89340..0d94f56 100644
--- a/utils/enums.js
+++ b/utils/enums.js
@@ -2,4 +2,5 @@
export const STORAGE_KEY = {
OPENAI_MODELS: 'openai_models',
CURRENT_OPENAI_MODEL: 'current_openai_model',
+ OPENAI_API_KEY: 'openai_api_key',
}
\ No newline at end of file
diff --git a/utils/localStorage.js b/utils/localStorage.js
index 3556082..d6760d1 100644
--- a/utils/localStorage.js
+++ b/utils/localStorage.js
@@ -1,11 +1,13 @@
-import {useCurrentModel, useModels} from "~/composables/states";
const get = (key) => {
- let val = localStorage.getItem(key)
- if (val) {
- val = JSON.parse(val)
+ if (typeof window !== 'undefined') {
+ let val = localStorage.getItem(key)
+ if (val) {
+ val = JSON.parse(val)
+ }
+ return val
}
- return val
+ return null
}
const set = (key, val) => {
@@ -40,4 +42,14 @@ export const getCurrentModel = () => {
model = DEFAULT_OPENAI_MODEL
}
return model
+}
+
+export const setApiKey = (val) => {
+ const apiKey = useApiKey()
+ set(STORAGE_KEY.OPENAI_API_KEY, val)
+ apiKey.value = val
+}
+
+export const getStoredApiKey = () => {
+ return get(STORAGE_KEY.OPENAI_API_KEY)
}
\ No newline at end of file