|
@@ -44,14 +44,21 @@ const makeRequestParam = (
|
|
|
|
|
|
function getHeaders() {
|
|
function getHeaders() {
|
|
const accessStore = useAccessStore.getState();
|
|
const accessStore = useAccessStore.getState();
|
|
- let headers: Record<string, string> = {};
|
|
|
|
|
|
+ const headers = {
|
|
|
|
+ Authorization: "",
|
|
|
|
+ };
|
|
|
|
|
|
- if (accessStore.enabledAccessControl()) {
|
|
|
|
- headers["access-code"] = accessStore.accessCode;
|
|
|
|
- }
|
|
|
|
|
|
+ const makeBearer = (token: string) => `Bearer ${token.trim()}`;
|
|
|
|
+ const validString = (x: string) => x && x.length > 0;
|
|
|
|
|
|
- if (accessStore.token && accessStore.token.length > 0) {
|
|
|
|
- headers["token"] = accessStore.token;
|
|
|
|
|
|
+ // use user's api key first
|
|
|
|
+ if (validString(accessStore.token)) {
|
|
|
|
+ headers.Authorization = makeBearer(accessStore.token);
|
|
|
|
+ } else if (
|
|
|
|
+ accessStore.enabledAccessControl() &&
|
|
|
|
+ validString(accessStore.accessCode)
|
|
|
|
+ ) {
|
|
|
|
+ headers.Authorization = makeBearer(accessStore.accessCode);
|
|
}
|
|
}
|
|
|
|
|
|
return headers;
|
|
return headers;
|
|
@@ -59,13 +66,8 @@ function getHeaders() {
|
|
|
|
|
|
export function requestOpenaiClient(path: string) {
|
|
export function requestOpenaiClient(path: string) {
|
|
return (body: any, method = "POST") =>
|
|
return (body: any, method = "POST") =>
|
|
- fetch("/api/openai", {
|
|
|
|
|
|
+ fetch("/api/openai/" + path, {
|
|
method,
|
|
method,
|
|
- headers: {
|
|
|
|
- "Content-Type": "application/json",
|
|
|
|
- path,
|
|
|
|
- ...getHeaders(),
|
|
|
|
- },
|
|
|
|
body: body && JSON.stringify(body),
|
|
body: body && JSON.stringify(body),
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -161,16 +163,16 @@ export async function requestChatStream(
|
|
const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
|
|
const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
|
|
|
|
|
|
try {
|
|
try {
|
|
- const res = await fetch("/api/openai", {
|
|
|
|
|
|
+ const res = await fetch("/api/openai/v1/chat/completions", {
|
|
method: "POST",
|
|
method: "POST",
|
|
headers: {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Content-Type": "application/json",
|
|
- path: "v1/chat/completions",
|
|
|
|
...getHeaders(),
|
|
...getHeaders(),
|
|
},
|
|
},
|
|
body: JSON.stringify(req),
|
|
body: JSON.stringify(req),
|
|
signal: controller.signal,
|
|
signal: controller.signal,
|
|
});
|
|
});
|
|
|
|
+
|
|
clearTimeout(reqTimeoutId);
|
|
clearTimeout(reqTimeoutId);
|
|
|
|
|
|
let responseText = "";
|
|
let responseText = "";
|