Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 15, 2023
2 parents 2cf5bf4 + 20882a7 commit 27d83a2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 13 deletions.
8 changes: 6 additions & 2 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client";

export interface OpenAIListModelResponse {
object: string;
Expand All @@ -28,13 +29,16 @@ export class ChatGPTApi implements LLMApi {

path(path: string): string {
let openaiUrl = useAccessStore.getState().openaiUrl;
const apiPath = "/api/openai";

if (openaiUrl.length === 0) {
openaiUrl = DEFAULT_API_HOST;
const isApp = !!getClientConfig()?.isApp;
openaiUrl = isApp ? DEFAULT_API_HOST : apiPath;
}
if (openaiUrl.endsWith("/")) {
openaiUrl = openaiUrl.slice(0, openaiUrl.length - 1);
}
if (!openaiUrl.startsWith("http") && !openaiUrl.startsWith("/api/openai")) {
if (!openaiUrl.startsWith("http") && !openaiUrl.startsWith(apiPath)) {
openaiUrl = "https://" + openaiUrl;
}
return [openaiUrl, path].join("/");
Expand Down
2 changes: 1 addition & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ function _Chat() {
const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;

if (isTouchTopEdge) {
if (isTouchTopEdge && !isTouchBottomEdge) {
setMsgRenderIndex(prevPageMsgIndex);
} else if (isTouchBottomEdge) {
setMsgRenderIndex(nextPageMsgIndex);
Expand Down
14 changes: 13 additions & 1 deletion app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dynamic from "next/dynamic";
import { Path, SlotID } from "../constant";
import { ErrorBoundary } from "./error";

import { getLang } from "../locales";
import { getISOLang, getLang } from "../locales";

import {
HashRouter as Router,
Expand Down Expand Up @@ -86,6 +86,17 @@ export function useSwitchTheme() {
}, [config.theme]);
}

function useHtmlLang() {
useEffect(() => {
const lang = getISOLang();
const htmlLang = document.documentElement.lang;

if (lang !== htmlLang) {
document.documentElement.lang = lang;
}
}, []);
}

const useHasHydrated = () => {
const [hasHydrated, setHasHydrated] = useState<boolean>(false);

Expand Down Expand Up @@ -168,6 +179,7 @@ export function useLoadData() {
export function Home() {
useSwitchTheme();
useLoadData();
useHtmlLang();

useEffect(() => {
console.log("[Config] got config from build time", getClientConfig());
Expand Down
8 changes: 1 addition & 7 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ export function Mermaid(props: { code: string }) {
if (!svg) return;
const text = new XMLSerializer().serializeToString(svg);
const blob = new Blob([text], { type: "image/svg+xml" });
console.log(blob);
// const url = URL.createObjectURL(blob);
// const win = window.open(url);
// if (win) {
// win.onload = () => URL.revokeObjectURL(url);
// }
showImageModal(URL.createObjectURL(blob));
}

Expand Down Expand Up @@ -152,11 +146,11 @@ export function Markdown(
className="markdown-body"
style={{
fontSize: `${props.fontSize ?? 14}px`,
direction: /[\u0600-\u06FF]/.test(props.content) ? "rtl" : "ltr",
}}
ref={mdRef}
onContextMenu={props.onContextMenu}
onDoubleClickCapture={props.onDoubleClickCapture}
dir="auto"
>
{props.loading ? (
<LoadingIcon />
Expand Down
16 changes: 16 additions & 0 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,22 @@ export function Settings() {
></InputRange>
</ListItem>

<ListItem
title={Locale.Settings.AutoGenerateTitle.Title}
subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
>
<input
type="checkbox"
checked={config.enableAutoGenerateTitle}
onChange={(e) =>
updateConfig(
(config) =>
(config.enableAutoGenerateTitle = e.currentTarget.checked),
)
}
></input>
</ListItem>

<ListItem
title={Locale.Settings.SendPreviewBubble.Title}
subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./styles/globals.scss";
import "./styles/markdown.scss";
import "./styles/highlight.scss";
import { getClientConfig } from "./config/client";
import { type Metadata } from 'next';
import { type Metadata } from "next";

export const metadata: Metadata = {
title: "ChatGPT Next Web",
Expand Down
4 changes: 4 additions & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ const cn = {
Title: "预览气泡",
SubTitle: "在预览气泡中预览 Markdown 内容",
},
AutoGenerateTitle: {
Title: "自动生成标题",
SubTitle: "根据对话内容生成合适的标题",
},
Mask: {
Splash: {
Title: "面具启动页",
Expand Down
4 changes: 4 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ const en: LocaleType = {
Title: "Send Preview Bubble",
SubTitle: "Preview markdown in bubble",
},
AutoGenerateTitle: {
Title: "Auto Generate Title",
SubTitle: "Generate a suitable title based on the conversation content",
},
Mask: {
Splash: {
Title: "Mask Splash Screen",
Expand Down
10 changes: 10 additions & 0 deletions app/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,13 @@ export function changeLang(lang: Lang) {
setItem(LANG_KEY, lang);
location.reload();
}

export function getISOLang() {
const isoLangString: Record<string, string> = {
cn: "zh-Hans",
tw: "zh-Hant",
};

const lang = getLang();
return isoLangString[lang] ?? lang;
}
2 changes: 2 additions & 0 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ export const useChatStore = create<ChatStore>()(
},

summarizeSession() {
const config = useAppConfig.getState();
const session = get().currentSession();

// remove error messages if any
Expand All @@ -487,6 +488,7 @@ export const useChatStore = create<ChatStore>()(
// should summarize topic after chating more than 50 words
const SUMMARIZE_MIN_LEN = 50;
if (
config.enableAutoGenerateTitle &&
session.topic === DEFAULT_TOPIC &&
countMessages(messages) >= SUMMARIZE_MIN_LEN
) {
Expand Down
7 changes: 6 additions & 1 deletion app/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DEFAULT_CONFIG = {
theme: Theme.Auto as Theme,
tightBorder: !!getClientConfig()?.isApp,
sendPreviewBubble: true,
enableAutoGenerateTitle: true,
sidebarWidth: 300,

disablePromptHint: false,
Expand Down Expand Up @@ -147,7 +148,7 @@ export const useAppConfig = create<ChatConfigStore>()(
}),
{
name: StoreKey.Config,
version: 3.6,
version: 3.7,
migrate(persistedState, version) {
const state = persistedState as ChatConfig;

Expand All @@ -170,6 +171,10 @@ export const useAppConfig = create<ChatConfigStore>()(
state.modelConfig.enableInjectSystemPrompts = true;
}

if (version < 3.7) {
state.enableAutoGenerateTitle = true;
}

return state as any;
},
},
Expand Down

0 comments on commit 27d83a2

Please sign in to comment.