Merge pull request #1247 from Yidadaa/proxy-api
fix: #1237 #1233 mask bug and proxy bug
This commit is contained in:
commit
92d2373d77
@ -1,6 +1,7 @@
|
|||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
import { getServerSideConfig } from "../config/server";
|
import { getServerSideConfig } from "../config/server";
|
||||||
import md5 from "spark-md5";
|
import md5 from "spark-md5";
|
||||||
|
import { ACCESS_CODE_PREFIX } from "../constant";
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
@ -17,10 +18,10 @@ function getIP(req: NextRequest) {
|
|||||||
|
|
||||||
function parseApiKey(bearToken: string) {
|
function parseApiKey(bearToken: string) {
|
||||||
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
|
||||||
const isOpenAiKey = token.startsWith("sk-");
|
const isOpenAiKey = !token.startsWith(ACCESS_CODE_PREFIX);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessCode: isOpenAiKey ? "" : token,
|
accessCode: isOpenAiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length),
|
||||||
apiKey: isOpenAiKey ? token : "",
|
apiKey: isOpenAiKey ? token : "",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,11 @@ declare global {
|
|||||||
type DangerConfig = typeof DANGER_CONFIG;
|
type DangerConfig = typeof DANGER_CONFIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST() {
|
async function handle() {
|
||||||
return NextResponse.json(DANGER_CONFIG);
|
return NextResponse.json(DANGER_CONFIG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const GET = handle;
|
||||||
|
export const POST = handle;
|
||||||
|
|
||||||
export const runtime = "edge";
|
export const runtime = "edge";
|
||||||
|
@ -21,7 +21,7 @@ import { useNavigate } from "react-router-dom";
|
|||||||
|
|
||||||
import chatStyle from "./chat.module.scss";
|
import chatStyle from "./chat.module.scss";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { downloadAs } from "../utils";
|
import { downloadAs, readFromFile } from "../utils";
|
||||||
import { Updater } from "../api/openai/typing";
|
import { Updater } from "../api/openai/typing";
|
||||||
import { ModelConfigList } from "./model-config";
|
import { ModelConfigList } from "./model-config";
|
||||||
import { FileName, Path } from "../constant";
|
import { FileName, Path } from "../constant";
|
||||||
@ -222,6 +222,21 @@ export function MaskPage() {
|
|||||||
downloadAs(JSON.stringify(masks), FileName.Masks);
|
downloadAs(JSON.stringify(masks), FileName.Masks);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const importFromFile = () => {
|
||||||
|
readFromFile().then((content) => {
|
||||||
|
try {
|
||||||
|
const importMasks = JSON.parse(content);
|
||||||
|
if (Array.isArray(importMasks)) {
|
||||||
|
for (const mask of importMasks) {
|
||||||
|
if (mask.name) {
|
||||||
|
maskStore.create(mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ErrorBoundary>
|
<ErrorBoundary>
|
||||||
<div className={styles["mask-page"]}>
|
<div className={styles["mask-page"]}>
|
||||||
@ -247,7 +262,7 @@ export function MaskPage() {
|
|||||||
<IconButton
|
<IconButton
|
||||||
icon={<UploadIcon />}
|
icon={<UploadIcon />}
|
||||||
bordered
|
bordered
|
||||||
onClick={() => showToast(Locale.WIP)}
|
onClick={() => importFromFile()}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="window-action-button">
|
<div className="window-action-button">
|
||||||
@ -371,7 +386,10 @@ export function MaskPage() {
|
|||||||
key="export"
|
key="export"
|
||||||
bordered
|
bordered
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
downloadAs(JSON.stringify(editingMask), "mask.json")
|
downloadAs(
|
||||||
|
JSON.stringify(editingMask),
|
||||||
|
`${editingMask.name}.json`,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
/>,
|
/>,
|
||||||
<IconButton
|
<IconButton
|
||||||
|
@ -36,3 +36,5 @@ export enum StoreKey {
|
|||||||
export const MAX_SIDEBAR_WIDTH = 500;
|
export const MAX_SIDEBAR_WIDTH = 500;
|
||||||
export const MIN_SIDEBAR_WIDTH = 230;
|
export const MIN_SIDEBAR_WIDTH = 230;
|
||||||
export const NARROW_SIDEBAR_WIDTH = 100;
|
export const NARROW_SIDEBAR_WIDTH = 100;
|
||||||
|
|
||||||
|
export const ACCESS_CODE_PREFIX = "ak-";
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
useChatStore,
|
useChatStore,
|
||||||
} from "./store";
|
} from "./store";
|
||||||
import { showToast } from "./components/ui-lib";
|
import { showToast } from "./components/ui-lib";
|
||||||
|
import { ACCESS_CODE_PREFIX } from "./constant";
|
||||||
|
|
||||||
const TIME_OUT_MS = 60000;
|
const TIME_OUT_MS = 60000;
|
||||||
|
|
||||||
@ -58,7 +59,9 @@ function getHeaders() {
|
|||||||
accessStore.enabledAccessControl() &&
|
accessStore.enabledAccessControl() &&
|
||||||
validString(accessStore.accessCode)
|
validString(accessStore.accessCode)
|
||||||
) {
|
) {
|
||||||
headers.Authorization = makeBearer(accessStore.accessCode);
|
headers.Authorization = makeBearer(
|
||||||
|
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
|
@ -57,6 +57,7 @@ export const useMaskStore = create<MaskStore>()(
|
|||||||
...createEmptyMask(),
|
...createEmptyMask(),
|
||||||
...mask,
|
...mask,
|
||||||
id,
|
id,
|
||||||
|
builtin: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
set(() => ({ masks }));
|
set(() => ({ masks }));
|
||||||
|
20
app/utils.ts
20
app/utils.ts
@ -42,6 +42,26 @@ export function downloadAs(text: string, filename: string) {
|
|||||||
document.body.removeChild(element);
|
document.body.removeChild(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function readFromFile() {
|
||||||
|
return new Promise<string>((res, rej) => {
|
||||||
|
const fileInput = document.createElement("input");
|
||||||
|
fileInput.type = "file";
|
||||||
|
fileInput.accept = "application/json";
|
||||||
|
|
||||||
|
fileInput.onchange = (event: any) => {
|
||||||
|
const file = event.target.files[0];
|
||||||
|
const fileReader = new FileReader();
|
||||||
|
fileReader.onload = (e: any) => {
|
||||||
|
res(e.target.result);
|
||||||
|
};
|
||||||
|
fileReader.onerror = (e) => rej(e);
|
||||||
|
fileReader.readAsText(file);
|
||||||
|
};
|
||||||
|
|
||||||
|
fileInput.click();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function isIOS() {
|
export function isIOS() {
|
||||||
const userAgent = navigator.userAgent.toLowerCase();
|
const userAgent = navigator.userAgent.toLowerCase();
|
||||||
return /iphone|ipad|ipod/.test(userAgent);
|
return /iphone|ipad|ipod/.test(userAgent);
|
||||||
|
@ -16,7 +16,9 @@ const nextConfig = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return {
|
||||||
|
afterFiles: ret,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
webpack(config) {
|
webpack(config) {
|
||||||
config.module.rules.push({
|
config.module.rules.push({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user