allow services for anonymous users & gpt4 fix

main
Cogent Apps 2023-04-29 18:29:48 +00:00
parent 5bcd75f120
commit 8f9ec46f9e
8 changed files with 33 additions and 11 deletions

View File

@ -24,6 +24,7 @@ export interface User {
export class Backend extends EventEmitter { export class Backend extends EventEmitter {
public user: User | null = null; public user: User | null = null;
public services: string[] = [];
private checkedSession = false; private checkedSession = false;
private sessionInterval = new AsyncLoop(() => this.getSession(), 1000 * 30); private sessionInterval = new AsyncLoop(() => this.getSession(), 1000 * 30);
@ -70,8 +71,10 @@ export class Backend extends EventEmitter {
avatar: session.picture, avatar: session.picture,
services: session.services, services: session.services,
}; };
this.services = session.services || [];
} else { } else {
this.user = null; this.user = null;
this.services = session?.services || [];
} }
this.checkedSession = true; this.checkedSession = true;

View File

@ -7,7 +7,7 @@ import { backend } from "../backend";
export const defaultModel = 'gpt-3.5-turbo'; export const defaultModel = 'gpt-3.5-turbo';
export function isProxySupported() { export function isProxySupported() {
return !!backend.current?.user?.services?.includes('openai'); return !!backend.current?.services?.includes('openai');
} }
function shouldUseProxy(apiKey: string | undefined | null) { function shouldUseProxy(apiKey: string | undefined | null) {
@ -141,5 +141,5 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
export const maxTokensByModel = { export const maxTokensByModel = {
"chatgpt-3.5-turbo": 2048, "chatgpt-3.5-turbo": 2048,
"gpt-4": 8096, "gpt-4": 8192,
} }

View File

@ -6,7 +6,7 @@ import { defaultElevenLabsVoiceID, defaultVoiceList } from "./elevenlabs-default
import { backend } from "../core/backend"; import { backend } from "../core/backend";
function isProxySupported() { function isProxySupported() {
return !!backend.current?.user?.services?.includes('elevenlabs'); return !!backend.current?.services?.includes('elevenlabs');
} }
function shouldUseProxy(apiKey: string | undefined | null) { function shouldUseProxy(apiKey: string | undefined | null) {

View File

@ -18,6 +18,7 @@ export interface Config {
// When provided, signed in users will be able to access OpenAI through the server // When provided, signed in users will be able to access OpenAI through the server
// without needing their own API key. // without needing their own API key.
apiKey?: string; apiKey?: string;
loginRequired?: boolean;
}; };
elevenlabs?: { elevenlabs?: {
@ -25,9 +26,10 @@ export interface Config {
// When provided, signed in users will be able to access ElevenLabs through the server // When provided, signed in users will be able to access ElevenLabs through the server
// without needing their own API key. // without needing their own API key.
apiKey?: string; apiKey?: string;
loginRequired?: boolean;
}; };
}; };
/* /*
Optional configuration for enabling Transport Layer Security (TLS) in the server. Optional configuration for enabling Transport Layer Security (TLS) in the server.
Requires specifying the file paths for the key and cert files. Includes: Requires specifying the file paths for the key and cert files. Includes:
@ -102,9 +104,18 @@ if (!fs.existsSync('./data')) {
fs.mkdirSync('./data'); fs.mkdirSync('./data');
} }
const filename = process.env.CHATWITHGPT_CONFIG_FILENAME let filename = process.env.CHATWITHGPT_CONFIG_FILENAME as string;
? path.resolve(process.env.CHATWITHGPT_CONFIG_FILENAME)
: path.resolve(__dirname, '../data/config.yaml'); // 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)) { if (fs.existsSync(filename)) {
config = { config = {
@ -132,6 +143,7 @@ if (process.argv.includes('--self-signed')) {
} }
if (config.publicSiteURL) { if (config.publicSiteURL) {
// remove trailing slash:
config.publicSiteURL = config.publicSiteURL.replace(/\/$/, ''); config.publicSiteURL = config.publicSiteURL.replace(/\/$/, '');
} }

View File

@ -23,6 +23,6 @@ export default class ElevenLabsTTSProxyRequestHandler extends RequestHandler {
} }
public isProtected() { public isProtected() {
return true; return config.services?.elevenlabs?.loginRequired ?? true;
} }
} }

View File

@ -2,6 +2,7 @@ import express from 'express';
import RequestHandler from "../../base"; import RequestHandler from "../../base";
import axios from 'axios'; import axios from 'axios';
import { endpoint, apiKey } from './text-to-speech'; import { endpoint, apiKey } from './text-to-speech';
import { config } from '../../../config';
export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler { export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler {
async handler(req: express.Request, res: express.Response) { async handler(req: express.Request, res: express.Response) {
@ -16,6 +17,6 @@ export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler
} }
public isProtected() { public isProtected() {
return true; return config.services?.elevenlabs?.loginRequired ?? true;
} }
} }

View File

@ -17,6 +17,6 @@ export default class OpenAIProxyRequestHandler extends RequestHandler {
} }
public isProtected() { public isProtected() {
return true; return config.services?.openai?.loginRequired ?? true;
} }
} }

View File

@ -7,7 +7,12 @@ export default class SessionRequestHandler extends RequestHandler {
const request = req as any; const request = req as any;
const availableServiceNames = Object.keys(config.services || {}) 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) { if (request.oidc) {
const user = request.oidc.user; const user = request.oidc.user;
@ -40,6 +45,7 @@ export default class SessionRequestHandler extends RequestHandler {
res.json({ res.json({
authProvider: this.context.authProvider, authProvider: this.context.authProvider,
authenticated: false, authenticated: false,
services: availableServiceNames,
}); });
} }
} }