Merge branch 'Yidadaa:main' into main

This commit is contained in:
Hk-Gosuto
2023-07-10 12:26:34 +08:00
committed by GitHub
27 changed files with 498 additions and 270 deletions

View File

@@ -16,19 +16,20 @@ 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;
toolPrompt?: string;
};
export function createMessage(override: Partial<ChatMessage>): ChatMessage {
return {
id: Date.now(),
id: nanoid(),
date: new Date().toLocaleString(),
role: "user",
content: "",
@@ -44,7 +45,7 @@ export interface ChatStat {
}
export interface ChatSession {
id: number;
id: string;
topic: string;
memoryPrompt: string;
@@ -66,7 +67,7 @@ export const BOT_HELLO: ChatMessage = createMessage({
function createEmptySession(): ChatSession {
return {
id: Date.now() + Math.random(),
id: nanoid(),
topic: DEFAULT_TOPIC,
memoryPrompt: "",
messages: [],
@@ -86,7 +87,6 @@ function createEmptySession(): ChatSession {
interface ChatStore {
sessions: ChatSession[];
currentSessionIndex: number;
globalId: number;
clearSessions: () => void;
moveSession: (from: number, to: number) => void;
selectSession: (index: number) => void;
@@ -143,7 +143,6 @@ export const useChatStore = create<ChatStore>()(
(set, get) => ({
sessions: [createEmptySession()],
currentSessionIndex: 0,
globalId: 0,
clearSessions() {
set(() => ({
@@ -186,9 +185,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;
@@ -304,7 +300,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
@@ -367,10 +362,7 @@ Reply in ${getLang()} and markdown.`;
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");
@@ -387,7 +379,7 @@ Reply in ${getLang()} and markdown.`;
session.messages = session.messages.concat();
});
ChatControllerPool.remove(
sessionIndex,
session.id,
botMessage.id ?? messageIndex,
);
@@ -396,7 +388,7 @@ Reply in ${getLang()} and markdown.`;
onController(controller) {
// collect controller for stop/retry
ChatControllerPool.addController(
sessionIndex,
session.id,
botMessage.id ?? messageIndex,
controller,
);
@@ -589,11 +581,13 @@ Reply in ${getLang()} and markdown.`;
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;
@@ -630,13 +624,12 @@ Reply in ${getLang()} and markdown.`;
}),
{
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;
@@ -651,6 +644,14 @@ Reply in ${getLang()} and markdown.`;
}
}
if (version < 3) {
// migrate id to nanoid
newState.sessions.forEach((s) => {
s.id = nanoid();
s.messages.forEach((m) => (m.id = nanoid()));
});
}
return newState;
},
},