diff --git a/app/requests.ts b/app/requests.ts index 497c8737..6a155394 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -65,10 +65,12 @@ function getHeaders() { } export function requestOpenaiClient(path: string) { + const openaiUrl = useAccessStore.getState().openaiUrl; return (body: any, method = "POST") => - fetch("/api/openai/" + path, { + fetch(openaiUrl + path, { method, body: body && JSON.stringify(body), + headers: getHeaders(), }); } @@ -163,7 +165,8 @@ export async function requestChatStream( const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS); try { - const res = await fetch("/api/openai/v1/chat/completions", { + const openaiUrl = useAccessStore.getState().openaiUrl; + const res = await fetch(openaiUrl + "v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", diff --git a/app/store/access.ts b/app/store/access.ts index ba0faf93..17515166 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -8,6 +8,7 @@ export interface AccessControlStore { token: string; needCode: boolean; + openaiUrl: string; updateToken: (_: string) => void; updateCode: (_: string) => void; @@ -24,16 +25,18 @@ export const useAccessStore = create()( token: "", accessCode: "", needCode: true, + openaiUrl: "/api/openai/", + enabledAccessControl() { get().fetch(); return get().needCode; }, updateCode(code: string) { - set((state) => ({ accessCode: code })); + set(() => ({ accessCode: code })); }, updateToken(token: string) { - set((state) => ({ token })); + set(() => ({ token })); }, isAuthorized() { // has token or has code or disabled access control