diff --git a/.gitignore b/.gitignore index d7e107a..31c3260 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ dist-ssr *.njsproj *.sln *.sw? +*.env diff --git a/package-lock.json b/package-lock.json index 2124ce7..5298436 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@sveltejs/vite-plugin-svelte": "^2.0.2", "@tsconfig/svelte": "^3.0.0", "@types/marked": "^4.0.8", + "@types/node": "^18.15.3", "bulma": "^0.9.4", "bulma-prefers-dark": "^0.1.0-beta.1", "copy-to-clipboard": "^3.3.3", @@ -652,6 +653,12 @@ "integrity": "sha512-HVNzMT5QlWCOdeuBsgXP8EZzKUf0+AXzN+sLmjvaB3ZlLqO+e4u0uXrdw9ub69wBKFs+c6/pA4r9sy6cCDvImw==", "dev": true }, + "node_modules/@types/node": { + "version": "18.15.3", + "resolved": "https://mirrors.cloud.tencent.com/npm/@types/node/-/node-18.15.3.tgz", + "integrity": "sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==", + "dev": true + }, "node_modules/@types/pug": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.6.tgz", diff --git a/package.json b/package.json index 5c44355..1dd9e7f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@sveltejs/vite-plugin-svelte": "^2.0.2", "@tsconfig/svelte": "^3.0.0", "@types/marked": "^4.0.8", + "@types/node": "^18.15.3", "bulma": "^0.9.4", "bulma-prefers-dark": "^0.1.0-beta.1", "copy-to-clipboard": "^3.3.3", diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index fc0313c..1633c4d 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -144,7 +144,7 @@ // Send API request /* // Not working yet: a way to get the response as a stream - await fetchEventSource("https://api.openai.com/v1/chat/completions", { + await fetchEventSource(import.meta.env.VITE_API_BASE + "/v1/chat/completions", { method: "POST", headers: { Authorization: @@ -185,7 +185,7 @@ }, {}) } response = await ( - await fetch('https://api.openai.com/v1/chat/completions', { + await fetch(import.meta.env.VITE_API_BASE + '/v1/chat/completions', { method: 'POST', headers: { Authorization: `Bearer ${$apiKeyStorage}`, @@ -275,7 +275,7 @@ // Load available models from OpenAI const allModels = (await ( - await fetch('https://api.openai.com/v1/models', { + await fetch(import.meta.env.VITE_API_BASE + '/v1/models', { method: 'GET', headers: { Authorization: `Bearer ${$apiKeyStorage}`, diff --git a/vite.config.ts b/vite.config.ts index 3290cdd..50d70f9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,10 +1,14 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte' import purgecss from '@fullhuman/postcss-purgecss' // https://vitejs.dev/config/ export default defineConfig(({ command, mode, ssrBuild }) => { + // Load env file based on `mode` in the current working directory. + const env = loadEnv(mode, process.cwd()) + // Set default api base url + env.VITE_API_BASE = 'https://api.openai.com' // Only run PurgeCSS in production builds if (command === 'build') { return {