From b3e856df1d0aa00038f0e4048c209ce0c7def444 Mon Sep 17 00:00:00 2001 From: butterfly Date: Mon, 6 May 2024 19:26:39 +0800 Subject: [PATCH 1/2] feat: fix 1)the property named 'role' of the first message must be 'user' 2)if default summarize model 'gpt-3.5-turbo' is blocked, use currentModel instead 3)if apiurl&apikey set by location, useCustomConfig would be opened --- app/api/webdav/[...path]/route.ts | 12 +++++++----- app/client/platforms/anthropic.ts | 7 +++++++ app/components/chat.tsx | 1 + app/config/server.ts | 8 ++++---- app/constant.ts | 2 +- app/store/chat.ts | 16 ++++++++++++++-- app/utils/model.ts | 5 +---- 7 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/api/webdav/[...path]/route.ts b/app/api/webdav/[...path]/route.ts index 3dd9ca3cda8c..816c2046b22f 100644 --- a/app/api/webdav/[...path]/route.ts +++ b/app/api/webdav/[...path]/route.ts @@ -1,12 +1,12 @@ import { NextRequest, NextResponse } from "next/server"; -import { STORAGE_KEY, internalWhiteWebDavEndpoints } from "../../../constant"; +import { STORAGE_KEY, internalAllowedWebDavEndpoints } from "../../../constant"; import { getServerSideConfig } from "@/app/config/server"; const config = getServerSideConfig(); -const mergedWhiteWebDavEndpoints = [ - ...internalWhiteWebDavEndpoints, - ...config.whiteWebDevEndpoints, +const mergedAllowedWebDavEndpoints = [ + ...internalAllowedWebDavEndpoints, + ...config.allowedWebDevEndpoints, ].filter((domain) => Boolean(domain.trim())); async function handle( @@ -24,7 +24,9 @@ async function handle( // Validate the endpoint to prevent potential SSRF attacks if ( - !mergedWhiteWebDavEndpoints.some((white) => endpoint?.startsWith(white)) + !mergedAllowedWebDavEndpoints.some( + (allowedEndpoint) => endpoint?.startsWith(allowedEndpoint), + ) ) { return NextResponse.json( { diff --git a/app/client/platforms/anthropic.ts b/app/client/platforms/anthropic.ts index ba07dcc761e6..54a171cdae93 100644 --- a/app/client/platforms/anthropic.ts +++ b/app/client/platforms/anthropic.ts @@ -161,6 +161,13 @@ export class ClaudeApi implements LLMApi { }; }); + if (prompt[0]?.role === "assistant") { + prompt.unshift({ + role: "user", + content: "", + }); + } + const requestBody: AnthropicChatRequest = { messages: prompt, stream: shouldStream, diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 85df5b9a82c7..c1400edc6635 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1088,6 +1088,7 @@ function _Chat() { if (payload.url) { accessStore.update((access) => (access.openaiUrl = payload.url!)); } + accessStore.useCustomConfig = true; }); } } catch { diff --git a/app/config/server.ts b/app/config/server.ts index 618112172ab6..edf58aaff3f8 100644 --- a/app/config/server.ts +++ b/app/config/server.ts @@ -82,9 +82,9 @@ export const getServerSideConfig = () => { `[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`, ); - const whiteWebDevEndpoints = (process.env.WHITE_WEBDEV_ENDPOINTS ?? "").split( - ",", - ); + const allowedWebDevEndpoints = ( + process.env.WHITE_WEBDEV_ENDPOINTS ?? "" + ).split(","); return { baseUrl: process.env.BASE_URL, @@ -120,6 +120,6 @@ export const getServerSideConfig = () => { disableFastLink: !!process.env.DISABLE_FAST_LINK, customModels, defaultModel, - whiteWebDevEndpoints, + allowedWebDevEndpoints, }; }; diff --git a/app/constant.ts b/app/constant.ts index 8b6549566d79..a3d9c206f994 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -197,7 +197,7 @@ export const CHAT_PAGE_SIZE = 15; export const MAX_RENDER_MSG_COUNT = 45; // some famous webdav endpoints -export const internalWhiteWebDavEndpoints = [ +export const internalAllowedWebDavEndpoints = [ "https://dav.jianguoyun.com/dav/", "https://dav.dropdav.com/", "https://dav.box.com/dav", diff --git a/app/store/chat.ts b/app/store/chat.ts index b305264b644d..a5412eaa914b 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -21,6 +21,8 @@ import { estimateTokenLength } from "../utils/token"; import { nanoid } from "nanoid"; import { createPersistStore } from "../utils/store"; import { identifyDefaultClaudeModel } from "../utils/checkers"; +import { collectModelsWithDefaultModel } from "../utils/model"; +import { useAccessStore } from "./access"; export type ChatMessage = RequestMessage & { date: string; @@ -87,9 +89,19 @@ function createEmptySession(): ChatSession { function getSummarizeModel(currentModel: string) { // if it is using gpt-* models, force to use 3.5 to summarize if (currentModel.startsWith("gpt")) { - return SUMMARIZE_MODEL; + const configStore = useAppConfig.getState(); + const accessStore = useAccessStore.getState(); + const allModel = collectModelsWithDefaultModel( + configStore.models, + [configStore.customModels, accessStore.customModels].join(","), + accessStore.defaultModel, + ); + const summarizeModel = allModel.find( + (m) => m.name === SUMMARIZE_MODEL && m.available, + ); + return summarizeModel?.name ?? currentModel; } - if (currentModel.startsWith("gemini-pro")) { + if (currentModel.startsWith("gemini")) { return GEMINI_SUMMARIZE_MODEL; } return currentModel; diff --git a/app/utils/model.ts b/app/utils/model.ts index 6477640aad11..056fff2e98d9 100644 --- a/app/utils/model.ts +++ b/app/utils/model.ts @@ -64,13 +64,10 @@ export function collectModelTableWithDefaultModel( ) { let modelTable = collectModelTable(models, customModels); if (defaultModel && defaultModel !== "") { - delete modelTable[defaultModel]; modelTable[defaultModel] = { + ...modelTable[defaultModel], name: defaultModel, - displayName: defaultModel, available: true, - provider: - modelTable[defaultModel]?.provider ?? customProvider(defaultModel), isDefault: true, }; } From a1493bfb4e9efe0a2e12917ab861bbf2321dbd7d Mon Sep 17 00:00:00 2001 From: Dean-YZG Date: Mon, 6 May 2024 20:46:53 +0800 Subject: [PATCH 2/2] feat: bugfix --- app/client/platforms/anthropic.ts | 2 +- app/components/chat.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/client/platforms/anthropic.ts b/app/client/platforms/anthropic.ts index 54a171cdae93..e90c8f057b2b 100644 --- a/app/client/platforms/anthropic.ts +++ b/app/client/platforms/anthropic.ts @@ -164,7 +164,7 @@ export class ClaudeApi implements LLMApi { if (prompt[0]?.role === "assistant") { prompt.unshift({ role: "user", - content: "", + content: ";", }); } diff --git a/app/components/chat.tsx b/app/components/chat.tsx index c1400edc6635..c8a79870c868 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1088,7 +1088,7 @@ function _Chat() { if (payload.url) { accessStore.update((access) => (access.openaiUrl = payload.url!)); } - accessStore.useCustomConfig = true; + accessStore.update((access) => (access.useCustomConfig = true)); }); } } catch {