Skip to content

Commit

Permalink
update memory
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 29, 2023
1 parent cb70452 commit df6e13d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 33 deletions.
5 changes: 3 additions & 2 deletions app/src/components/ReloadService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next";
import { useToast } from "./ui/use-toast.ts";
import { useEffect } from "react";
import { ToastAction } from "./ui/toast.tsx";
import { getMemory, setMemory } from "@/utils/memory.ts";

function ReloadPrompt() {
const { t } = useTranslation();
Expand All @@ -24,7 +25,7 @@ function ReloadPrompt() {
},
});

const before = localStorage.getItem("version") || "";
const before = getMemory("version");
if (before.length > 0 && before !== version) {
toast({
title: t("service.update-success"),
Expand All @@ -34,7 +35,7 @@ function ReloadPrompt() {
`[service] service worker updated (from ${before} to ${version})`,
);
}
localStorage.setItem("version", version);
setMemory("version", version);

useEffect(() => {
if (offlineReady) {
Expand Down
9 changes: 5 additions & 4 deletions app/src/components/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createContext, useContext, useEffect, useState } from "react";
import { Moon, Sun } from "lucide-react";

import { Button } from "./ui/button";
import { getMemory, setMemory } from "@/utils/memory.ts";

type Theme = "dark" | "light" | "system";

Expand All @@ -25,7 +26,7 @@ export function activeTheme(theme: Theme) {
: "light";

root.classList.add(theme);
localStorage.setItem("theme", theme);
setMemory("theme", theme);
}

const initialState: ThemeProviderState = {
Expand All @@ -34,7 +35,7 @@ const initialState: ThemeProviderState = {
activeTheme(theme);
},
toggleTheme: () => {
const theme = localStorage.getItem("theme") as Theme;
const theme = getMemory("theme") as Theme;
activeTheme(theme === "dark" ? "light" : "dark");
},
};
Expand All @@ -46,7 +47,7 @@ export function ThemeProvider({
...props
}: ThemeProviderProps) {
const [theme, setTheme] = useState<Theme>(
() => (localStorage.getItem("theme") as Theme) || defaultTheme,
() => (getMemory("theme") as Theme) || defaultTheme,
);

useEffect(() => {
Expand All @@ -70,7 +71,7 @@ export function ThemeProvider({
const value = {
theme,
setTheme: (theme: Theme) => {
localStorage.setItem("theme", theme);
setMemory("theme", theme);
setTheme(theme);
},
};
Expand Down
3 changes: 2 additions & 1 deletion app/src/components/app/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ModeToggle from "@/components/ThemeProvider.tsx";
import I18nProvider from "@/components/I18nProvider.tsx";
import router from "@/router.tsx";
import MenuBar from "./MenuBar.tsx";
import { getMemory } from "@/utils/memory.ts";

function NavMenu() {
const username = useSelector(selectUsername);
Expand All @@ -35,7 +36,7 @@ function NavBar() {
const { t } = useTranslation();
const dispatch = useDispatch();
useEffect(() => {
validateToken(dispatch, localStorage.getItem(tokenField) ?? "");
validateToken(dispatch, getMemory(tokenField));
}, []);
const auth = useSelector(selectAuthenticated);

Expand Down
21 changes: 12 additions & 9 deletions app/src/components/home/ChatWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ import {
DialogTitle,
} from "@/components/ui/dialog.tsx";
import { version } from "@/conf.ts";
import {clearHistoryState, getQueryParam} from "@/utils/path.ts";
import {forgetMemory, popMemory, recordMemory} from "@/utils/memory.ts";
import {useToast} from "@/components/ui/use-toast.ts";
import {ToastAction} from "@/components/ui/toast.tsx";
import { clearHistoryState, getQueryParam } from "@/utils/path.ts";
import { forgetMemory, popMemory, setMemory } from "@/utils/memory.ts";
import { useToast } from "@/components/ui/use-toast.ts";
import { ToastAction } from "@/components/ui/toast.tsx";

function ChatSpace() {
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -165,12 +165,15 @@ function ChatWrapper() {
title: t("chat.recall"),
description: t("chat.recall-desc"),
action: (
<ToastAction altText={t("chat.recall-cancel")} onClick={() => {
setInput("");
}}>
<ToastAction
altText={t("chat.recall-cancel")}
onClick={() => {
setInput("");
}}
>
{t("chat.recall-cancel")}
</ToastAction>
)
),
});
}
}, []);
Expand Down Expand Up @@ -217,7 +220,7 @@ function ChatWrapper() {
value={input}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setInput(e.target.value);
recordMemory("history", e.target.value);
setMemory("history", e.target.value);
}}
placeholder={t("chat.placeholder")}
onKeyDown={async (e: React.KeyboardEvent<HTMLInputElement>) => {
Expand Down
32 changes: 27 additions & 5 deletions app/src/conf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import axios from "axios";
import { Model } from "./conversation/types.ts";
import {getDev, getRestApi, getTokenField, getWebsocketApi} from "@/utils/env.ts";
import {
getDev,
getRestApi,
getTokenField,
getWebsocketApi,
} from "@/utils/env.ts";
import { getMemory } from "@/utils/memory.ts";

export const version = "3.5.21";
export const version = "3.6.0";
export const dev: boolean = getDev();
export const deploy: boolean = true;
export let rest_api: string = getRestApi(deploy);
Expand Down Expand Up @@ -30,9 +36,24 @@ export const supportModels: Model[] = [
{ id: "bing-creative", name: "New Bing", free: true, auth: true },

// zhipu models
{ id: "zhipu-chatglm-pro", name: "智谱 ChatGLM Pro", free: false, auth: true },
{ id: "zhipu-chatglm-std", name: "智谱 ChatGLM Std", free: false, auth: true },
{ id: "zhipu-chatglm-lite", name: "智谱 ChatGLM Lite", free: true, auth: true },
{
id: "zhipu-chatglm-pro",
name: "智谱 ChatGLM Pro",
free: false,
auth: true,
},
{
id: "zhipu-chatglm-std",
name: "智谱 ChatGLM Std",
free: false,
auth: true,
},
{
id: "zhipu-chatglm-lite",
name: "智谱 ChatGLM Lite",
free: true,
auth: true,
},
];

export function login() {
Expand All @@ -41,3 +62,4 @@ export function login() {

axios.defaults.baseURL = rest_api;
axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.headers.common["Authorization"] = getMemory(tokenField);
3 changes: 2 additions & 1 deletion app/src/conversation/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { tokenField, ws_api } from "@/conf.ts";
import { getMemory } from "@/utils/memory.ts";

export const endpoint = `${ws_api}/chat`;

Expand Down Expand Up @@ -37,7 +38,7 @@ export class Connection {
this.connection.onopen = () => {
this.state = true;
this.send({
token: localStorage.getItem(tokenField) || "anonymous",
token: getMemory(tokenField) || "anonymous",
id: this.id,
});
};
Expand Down
5 changes: 3 additions & 2 deletions app/src/conversation/generation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ws_api } from "@/conf.ts";
import { tokenField, ws_api } from "@/conf.ts";
import { getMemory } from "@/utils/memory.ts";

export const endpoint = `${ws_api}/generation/create`;

Expand Down Expand Up @@ -93,7 +94,7 @@ export class GenerationManager {

public generate(prompt: string, model: string) {
this.setProcessing(true);
const token = localStorage.getItem("token") || "anonymous";
const token = getMemory(tokenField) || "anonymous";
if (token) {
this.connection = new WebSocket(endpoint);
this.connection.onopen = () => {
Expand Down
5 changes: 3 additions & 2 deletions app/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import { getMemory, setMemory } from "@/utils/memory.ts";

// the translations
// (tip move them in a JSON file and import them,
Expand Down Expand Up @@ -654,7 +655,7 @@ i18n
export default i18n;

export function getStorage(): string {
const storage = localStorage.getItem("language");
const storage = getMemory("language");
if (storage && supportedLanguages.includes(storage)) {
return storage;
}
Expand All @@ -673,7 +674,7 @@ export function setLanguage(i18n: any, lang: string): void {
.then(() =>
console.debug(`[i18n] language changed (language: ${i18n.language})`),
);
localStorage.setItem("language", lang);
setMemory("language", lang);
return;
}
console.warn(`[i18n] language ${lang} is not supported`);
Expand Down
5 changes: 3 additions & 2 deletions app/src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";
import { tokenField } from "@/conf.ts";
import { AppDispatch } from "./index.ts";
import { forgetMemory, setMemory } from "@/utils/memory.ts";

export const authSlice = createSlice({
name: "auth",
Expand All @@ -16,7 +17,7 @@ export const authSlice = createSlice({
const token = (action.payload as string).trim();
state.token = token;
axios.defaults.headers.common["Authorization"] = token;
if (token.length > 0) localStorage.setItem(tokenField, token);
if (token.length > 0) setMemory(tokenField, token);
},
setAuthenticated: (state, action) => {
state.authenticated = action.payload as boolean;
Expand All @@ -32,7 +33,7 @@ export const authSlice = createSlice({
state.authenticated = false;
state.username = "";
axios.defaults.headers.common["Authorization"] = "";
localStorage.removeItem(tokenField);
forgetMemory(tokenField);

location.reload();
},
Expand Down
5 changes: 3 additions & 2 deletions app/src/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Message } from "@/conversation/types.ts";
import { insertStart } from "@/utils/base.ts";
import { RootState } from "./index.ts";
import { supportModels } from "@/conf.ts";
import { getMemory, setMemory } from "@/utils/memory.ts";

type initialStateType = {
history: ConversationInstance[];
Expand All @@ -25,7 +26,7 @@ const chatSlice = createSlice({
initialState: {
history: [],
messages: [],
model: GetModel(localStorage.getItem("model")),
model: GetModel(getMemory("model")),
web: false,
current: -1,
} as initialStateType,
Expand All @@ -52,7 +53,7 @@ const chatSlice = createSlice({
state.messages = action.payload as Message[];
},
setModel: (state, action) => {
localStorage.setItem("model", action.payload as string);
setMemory("model", action.payload as string);
state.model = action.payload as string;
},
setWeb: (state, action) => {
Expand Down
6 changes: 3 additions & 3 deletions app/src/utils/memory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export function recordMemory(key: string, value: string) {
export function setMemory(key: string, value: string) {
const data = value.trim();
localStorage.setItem(key, data);
}

export function recallMemory(key: string): string {
export function getMemory(key: string): string {
return (localStorage.getItem(key) || "").trim();
}

Expand All @@ -16,7 +16,7 @@ export function clearMemory() {
}

export function popMemory(key: string): string {
const value = recallMemory(key);
const value = getMemory(key);
forgetMemory(key);
return value;
}

0 comments on commit df6e13d

Please sign in to comment.