diff --git a/app/src/core/backend.ts b/app/src/core/backend.ts index 14932a22..b59a7fc9 100644 --- a/app/src/core/backend.ts +++ b/app/src/core/backend.ts @@ -24,6 +24,7 @@ export interface User { export class Backend extends EventEmitter { public user: User | null = null; + public services: string[] = []; private checkedSession = false; private sessionInterval = new AsyncLoop(() => this.getSession(), 1000 * 30); @@ -70,8 +71,10 @@ export class Backend extends EventEmitter { avatar: session.picture, services: session.services, }; + this.services = session.services || []; } else { this.user = null; + this.services = session?.services || []; } this.checkedSession = true; diff --git a/app/src/core/chat/openai.ts b/app/src/core/chat/openai.ts index 8b63def7..f107c537 100644 --- a/app/src/core/chat/openai.ts +++ b/app/src/core/chat/openai.ts @@ -7,7 +7,7 @@ import { backend } from "../backend"; export const defaultModel = 'gpt-3.5-turbo'; export function isProxySupported() { - return !!backend.current?.user?.services?.includes('openai'); + return !!backend.current?.services?.includes('openai'); } function shouldUseProxy(apiKey: string | undefined | null) { @@ -141,5 +141,5 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p export const maxTokensByModel = { "chatgpt-3.5-turbo": 2048, - "gpt-4": 8096, + "gpt-4": 8192, } \ No newline at end of file diff --git a/app/src/tts-plugins/elevenlabs.tsx b/app/src/tts-plugins/elevenlabs.tsx index b9c8daf7..e9648551 100644 --- a/app/src/tts-plugins/elevenlabs.tsx +++ b/app/src/tts-plugins/elevenlabs.tsx @@ -6,7 +6,7 @@ import { defaultElevenLabsVoiceID, defaultVoiceList } from "./elevenlabs-default import { backend } from "../core/backend"; function isProxySupported() { - return !!backend.current?.user?.services?.includes('elevenlabs'); + return !!backend.current?.services?.includes('elevenlabs'); } function shouldUseProxy(apiKey: string | undefined | null) { diff --git a/server/src/config.ts b/server/src/config.ts index f1b69d27..79abf2fc 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -18,6 +18,7 @@ export interface Config { // When provided, signed in users will be able to access OpenAI through the server // without needing their own API key. apiKey?: string; + loginRequired?: boolean; }; elevenlabs?: { @@ -25,9 +26,10 @@ export interface Config { // When provided, signed in users will be able to access ElevenLabs through the server // without needing their own API key. apiKey?: string; + loginRequired?: boolean; }; }; - + /* Optional configuration for enabling Transport Layer Security (TLS) in the server. Requires specifying the file paths for the key and cert files. Includes: @@ -102,9 +104,18 @@ if (!fs.existsSync('./data')) { fs.mkdirSync('./data'); } -const filename = process.env.CHATWITHGPT_CONFIG_FILENAME - ? path.resolve(process.env.CHATWITHGPT_CONFIG_FILENAME) - : path.resolve(__dirname, '../data/config.yaml'); +let filename = process.env.CHATWITHGPT_CONFIG_FILENAME as string; + +// assume config.yaml if no filename is provided: +if (!filename) { + filename = path.resolve(__dirname, '../data/config.yaml') + + // try config.yml if config.yaml doesn't exist: + const fallbackFilename = path.resolve(__dirname, '../data/config.yml'); + if (!fs.existsSync(filename) && fs.existsSync(fallbackFilename)) { + filename = fallbackFilename; + } +} if (fs.existsSync(filename)) { config = { @@ -132,6 +143,7 @@ if (process.argv.includes('--self-signed')) { } if (config.publicSiteURL) { + // remove trailing slash: config.publicSiteURL = config.publicSiteURL.replace(/\/$/, ''); } diff --git a/server/src/endpoints/service-proxies/elevenlabs/text-to-speech.ts b/server/src/endpoints/service-proxies/elevenlabs/text-to-speech.ts index 8f35aef4..92d18961 100644 --- a/server/src/endpoints/service-proxies/elevenlabs/text-to-speech.ts +++ b/server/src/endpoints/service-proxies/elevenlabs/text-to-speech.ts @@ -23,6 +23,6 @@ export default class ElevenLabsTTSProxyRequestHandler extends RequestHandler { } public isProtected() { - return true; + return config.services?.elevenlabs?.loginRequired ?? true; } } \ No newline at end of file diff --git a/server/src/endpoints/service-proxies/elevenlabs/voices.ts b/server/src/endpoints/service-proxies/elevenlabs/voices.ts index d9f2a2d3..eebf9077 100644 --- a/server/src/endpoints/service-proxies/elevenlabs/voices.ts +++ b/server/src/endpoints/service-proxies/elevenlabs/voices.ts @@ -2,6 +2,7 @@ import express from 'express'; import RequestHandler from "../../base"; import axios from 'axios'; import { endpoint, apiKey } from './text-to-speech'; +import { config } from '../../../config'; export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler { async handler(req: express.Request, res: express.Response) { @@ -16,6 +17,6 @@ export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler } public isProtected() { - return true; + return config.services?.elevenlabs?.loginRequired ?? true; } } \ No newline at end of file diff --git a/server/src/endpoints/service-proxies/openai/index.ts b/server/src/endpoints/service-proxies/openai/index.ts index 5c1eca1e..a263f08e 100644 --- a/server/src/endpoints/service-proxies/openai/index.ts +++ b/server/src/endpoints/service-proxies/openai/index.ts @@ -17,6 +17,6 @@ export default class OpenAIProxyRequestHandler extends RequestHandler { } public isProtected() { - return true; + return config.services?.openai?.loginRequired ?? true; } } \ No newline at end of file diff --git a/server/src/endpoints/session.ts b/server/src/endpoints/session.ts index 23ea0c18..97acb0e9 100644 --- a/server/src/endpoints/session.ts +++ b/server/src/endpoints/session.ts @@ -7,7 +7,12 @@ export default class SessionRequestHandler extends RequestHandler { const request = req as any; const availableServiceNames = Object.keys(config.services || {}) - .filter(key => (config.services as any)?.[key]?.apiKey); + .filter(key => { + const serviceConfig = (config.services as any)?.[key]; + const apiKey = serviceConfig?.apiKey; + const loginRequired = serviceConfig?.loginRequired ?? true; + return apiKey && (!loginRequired || request.isAuthenticated()); + }); if (request.oidc) { const user = request.oidc.user; @@ -40,6 +45,7 @@ export default class SessionRequestHandler extends RequestHandler { res.json({ authProvider: this.context.authProvider, authenticated: false, + services: availableServiceNames, }); } } \ No newline at end of file