mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-09 05:34:01 +08:00
feat: #920 migrate id to nanoid
This commit is contained in:
@@ -16,18 +16,19 @@ import { api, RequestMessage } from "../client/api";
|
||||
import { ChatControllerPool } from "../client/controller";
|
||||
import { prettyObject } from "../utils/format";
|
||||
import { estimateTokenLength } from "../utils/token";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
export type ChatMessage = RequestMessage & {
|
||||
date: string;
|
||||
streaming?: boolean;
|
||||
isError?: boolean;
|
||||
id?: number;
|
||||
id: string;
|
||||
model?: ModelType;
|
||||
};
|
||||
|
||||
export function createMessage(override: Partial<ChatMessage>): ChatMessage {
|
||||
return {
|
||||
id: Date.now(),
|
||||
id: nanoid(),
|
||||
date: new Date().toLocaleString(),
|
||||
role: "user",
|
||||
content: "",
|
||||
@@ -42,7 +43,7 @@ export interface ChatStat {
|
||||
}
|
||||
|
||||
export interface ChatSession {
|
||||
id: number;
|
||||
id: string;
|
||||
topic: string;
|
||||
|
||||
memoryPrompt: string;
|
||||
@@ -63,7 +64,7 @@ export const BOT_HELLO: ChatMessage = createMessage({
|
||||
|
||||
function createEmptySession(): ChatSession {
|
||||
return {
|
||||
id: Date.now() + Math.random(),
|
||||
id: nanoid(),
|
||||
topic: DEFAULT_TOPIC,
|
||||
memoryPrompt: "",
|
||||
messages: [],
|
||||
@@ -82,7 +83,6 @@ function createEmptySession(): ChatSession {
|
||||
interface ChatStore {
|
||||
sessions: ChatSession[];
|
||||
currentSessionIndex: number;
|
||||
globalId: number;
|
||||
clearSessions: () => void;
|
||||
moveSession: (from: number, to: number) => void;
|
||||
selectSession: (index: number) => void;
|
||||
@@ -139,7 +139,6 @@ export const useChatStore = create<ChatStore>()(
|
||||
(set, get) => ({
|
||||
sessions: [createEmptySession()],
|
||||
currentSessionIndex: 0,
|
||||
globalId: 0,
|
||||
|
||||
clearSessions() {
|
||||
set(() => ({
|
||||
@@ -182,9 +181,6 @@ export const useChatStore = create<ChatStore>()(
|
||||
newSession(mask) {
|
||||
const session = createEmptySession();
|
||||
|
||||
set(() => ({ globalId: get().globalId + 1 }));
|
||||
session.id = get().globalId;
|
||||
|
||||
if (mask) {
|
||||
const config = useAppConfig.getState();
|
||||
const globalModelConfig = config.modelConfig;
|
||||
@@ -300,7 +296,6 @@ export const useChatStore = create<ChatStore>()(
|
||||
// get recent messages
|
||||
const recentMessages = get().getMessagesWithMemory();
|
||||
const sendMessages = recentMessages.concat(userMessage);
|
||||
const sessionIndex = get().currentSessionIndex;
|
||||
const messageIndex = get().currentSession().messages.length + 1;
|
||||
|
||||
// save user's and bot's message
|
||||
@@ -334,10 +329,7 @@ export const useChatStore = create<ChatStore>()(
|
||||
botMessage.content = message;
|
||||
get().onNewMessage(botMessage);
|
||||
}
|
||||
ChatControllerPool.remove(
|
||||
sessionIndex,
|
||||
botMessage.id ?? messageIndex,
|
||||
);
|
||||
ChatControllerPool.remove(session.id, botMessage.id);
|
||||
},
|
||||
onError(error) {
|
||||
const isAborted = error.message.includes("aborted");
|
||||
@@ -354,7 +346,7 @@ export const useChatStore = create<ChatStore>()(
|
||||
session.messages = session.messages.concat();
|
||||
});
|
||||
ChatControllerPool.remove(
|
||||
sessionIndex,
|
||||
session.id,
|
||||
botMessage.id ?? messageIndex,
|
||||
);
|
||||
|
||||
@@ -363,7 +355,7 @@ export const useChatStore = create<ChatStore>()(
|
||||
onController(controller) {
|
||||
// collect controller for stop/retry
|
||||
ChatControllerPool.addController(
|
||||
sessionIndex,
|
||||
session.id,
|
||||
botMessage.id ?? messageIndex,
|
||||
controller,
|
||||
);
|
||||
@@ -556,11 +548,13 @@ export const useChatStore = create<ChatStore>()(
|
||||
modelConfig.sendMemory
|
||||
) {
|
||||
api.llm.chat({
|
||||
messages: toBeSummarizedMsgs.concat({
|
||||
role: "system",
|
||||
content: Locale.Store.Prompt.Summarize,
|
||||
date: "",
|
||||
}),
|
||||
messages: toBeSummarizedMsgs.concat(
|
||||
createMessage({
|
||||
role: "system",
|
||||
content: Locale.Store.Prompt.Summarize,
|
||||
date: "",
|
||||
}),
|
||||
),
|
||||
config: { ...modelConfig, stream: true },
|
||||
onUpdate(message) {
|
||||
session.memoryPrompt = message;
|
||||
@@ -597,13 +591,12 @@ export const useChatStore = create<ChatStore>()(
|
||||
}),
|
||||
{
|
||||
name: StoreKey.Chat,
|
||||
version: 2,
|
||||
version: 3,
|
||||
migrate(persistedState, version) {
|
||||
const state = persistedState as any;
|
||||
const newState = JSON.parse(JSON.stringify(state)) as ChatStore;
|
||||
|
||||
if (version < 2) {
|
||||
newState.globalId = 0;
|
||||
newState.sessions = [];
|
||||
|
||||
const oldSessions = state.sessions;
|
||||
@@ -618,6 +611,14 @@ export const useChatStore = create<ChatStore>()(
|
||||
}
|
||||
}
|
||||
|
||||
if (version < 3) {
|
||||
// migrate id to nanoid
|
||||
newState.sessions.forEach((s) => {
|
||||
s.id = nanoid();
|
||||
s.messages.forEach((m) => (m.id = nanoid()));
|
||||
});
|
||||
}
|
||||
|
||||
return newState;
|
||||
},
|
||||
},
|
||||
|
Reference in New Issue
Block a user