store.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { create } from "zustand";
  2. import { persist } from "zustand/middleware";
  3. import { type ChatCompletionResponseMessage } from "openai";
  4. import { requestChat, requestChatStream, requestWithPrompt } from "./requests";
  5. import { trimTopic } from "./utils";
  6. export type Message = ChatCompletionResponseMessage & {
  7. date: string;
  8. streaming?: boolean;
  9. };
  10. export enum SubmitKey {
  11. Enter = "Enter",
  12. CtrlEnter = "Ctrl + Enter",
  13. ShiftEnter = "Shift + Enter",
  14. AltEnter = "Alt + Enter",
  15. }
  16. export enum Theme {
  17. Auto = "auto",
  18. Dark = "dark",
  19. Light = "light",
  20. }
  21. interface ChatConfig {
  22. maxToken?: number;
  23. historyMessageCount: number; // -1 means all
  24. compressMessageLengthThreshold: number;
  25. sendBotMessages: boolean; // send bot's message or not
  26. submitKey: SubmitKey;
  27. avatar: string;
  28. theme: Theme;
  29. tightBorder: boolean;
  30. }
  31. const DEFAULT_CONFIG: ChatConfig = {
  32. historyMessageCount: 5,
  33. compressMessageLengthThreshold: 500,
  34. sendBotMessages: true as boolean,
  35. submitKey: SubmitKey.CtrlEnter as SubmitKey,
  36. avatar: "1f603",
  37. theme: Theme.Auto as Theme,
  38. tightBorder: false,
  39. };
  40. interface ChatStat {
  41. tokenCount: number;
  42. wordCount: number;
  43. charCount: number;
  44. }
  45. interface ChatSession {
  46. id: number;
  47. topic: string;
  48. memoryPrompt: string;
  49. messages: Message[];
  50. stat: ChatStat;
  51. lastUpdate: string;
  52. lastSummarizeIndex: number;
  53. }
  54. const DEFAULT_TOPIC = "新的聊天";
  55. function createEmptySession(): ChatSession {
  56. const createDate = new Date().toLocaleString();
  57. return {
  58. id: Date.now(),
  59. topic: DEFAULT_TOPIC,
  60. memoryPrompt: "",
  61. messages: [
  62. {
  63. role: "assistant",
  64. content: "有什么可以帮你的吗",
  65. date: createDate,
  66. },
  67. ],
  68. stat: {
  69. tokenCount: 0,
  70. wordCount: 0,
  71. charCount: 0,
  72. },
  73. lastUpdate: createDate,
  74. lastSummarizeIndex: 0,
  75. };
  76. }
  77. interface ChatStore {
  78. config: ChatConfig;
  79. sessions: ChatSession[];
  80. currentSessionIndex: number;
  81. removeSession: (index: number) => void;
  82. selectSession: (index: number) => void;
  83. newSession: () => void;
  84. currentSession: () => ChatSession;
  85. onNewMessage: (message: Message) => void;
  86. onUserInput: (content: string) => Promise<void>;
  87. summarizeSession: () => void;
  88. updateStat: (message: Message) => void;
  89. updateCurrentSession: (updater: (session: ChatSession) => void) => void;
  90. updateMessage: (
  91. sessionIndex: number,
  92. messageIndex: number,
  93. updater: (message?: Message) => void
  94. ) => void;
  95. getMessagesWithMemory: () => Message[];
  96. getConfig: () => ChatConfig;
  97. resetConfig: () => void;
  98. updateConfig: (updater: (config: ChatConfig) => void) => void;
  99. clearAllData: () => void;
  100. }
  101. const LOCAL_KEY = "chat-next-web-store";
  102. export const useChatStore = create<ChatStore>()(
  103. persist(
  104. (set, get) => ({
  105. sessions: [createEmptySession()],
  106. currentSessionIndex: 0,
  107. config: {
  108. ...DEFAULT_CONFIG,
  109. },
  110. resetConfig() {
  111. set(() => ({ config: { ...DEFAULT_CONFIG } }));
  112. },
  113. getConfig() {
  114. return get().config;
  115. },
  116. updateConfig(updater) {
  117. const config = get().config;
  118. updater(config);
  119. set(() => ({ config }));
  120. },
  121. selectSession(index: number) {
  122. set({
  123. currentSessionIndex: index,
  124. });
  125. },
  126. removeSession(index: number) {
  127. set((state) => {
  128. let nextIndex = state.currentSessionIndex;
  129. const sessions = state.sessions;
  130. if (sessions.length === 1) {
  131. return {
  132. currentSessionIndex: 0,
  133. sessions: [createEmptySession()],
  134. };
  135. }
  136. sessions.splice(index, 1);
  137. if (nextIndex === index) {
  138. nextIndex -= 1;
  139. }
  140. return {
  141. currentSessionIndex: nextIndex,
  142. sessions,
  143. };
  144. });
  145. },
  146. newSession() {
  147. set((state) => ({
  148. currentSessionIndex: 0,
  149. sessions: [createEmptySession()].concat(state.sessions),
  150. }));
  151. },
  152. currentSession() {
  153. let index = get().currentSessionIndex;
  154. const sessions = get().sessions;
  155. if (index < 0 || index >= sessions.length) {
  156. index = Math.min(sessions.length - 1, Math.max(0, index));
  157. set(() => ({ currentSessionIndex: index }));
  158. }
  159. const session = sessions[index];
  160. return session;
  161. },
  162. onNewMessage(message) {
  163. get().updateStat(message);
  164. get().summarizeSession();
  165. },
  166. async onUserInput(content) {
  167. const message: Message = {
  168. role: "user",
  169. content,
  170. date: new Date().toLocaleString(),
  171. };
  172. get().updateCurrentSession((session) => {
  173. session.messages.push(message);
  174. });
  175. // get last five messges
  176. const messages = get().currentSession().messages.concat(message);
  177. const botMessage: Message = {
  178. content: "",
  179. role: "assistant",
  180. date: new Date().toLocaleString(),
  181. streaming: true,
  182. };
  183. get().updateCurrentSession((session) => {
  184. session.messages.push(botMessage);
  185. });
  186. const recentMessages = get().getMessagesWithMemory()
  187. requestChatStream(recentMessages, {
  188. onMessage(content, done) {
  189. if (done) {
  190. botMessage.streaming = false;
  191. get().onNewMessage(botMessage)
  192. } else {
  193. botMessage.content = content;
  194. set(() => ({}));
  195. }
  196. },
  197. onError(error) {
  198. botMessage.content += "\n\n出错了,稍后重试吧";
  199. botMessage.streaming = false;
  200. set(() => ({}));
  201. },
  202. filterBot: !get().config.sendBotMessages,
  203. });
  204. },
  205. getMessagesWithMemory() {
  206. const session = get().currentSession()
  207. const config = get().config
  208. const recentMessages = session.messages.slice(-config.historyMessageCount);
  209. const memoryPrompt: Message = {
  210. role: 'system',
  211. content: '这是你和用户的历史聊天总结:' + session.memoryPrompt,
  212. date: ''
  213. }
  214. if (session.memoryPrompt) {
  215. recentMessages.unshift(memoryPrompt)
  216. }
  217. return recentMessages
  218. },
  219. updateMessage(
  220. sessionIndex: number,
  221. messageIndex: number,
  222. updater: (message?: Message) => void
  223. ) {
  224. const sessions = get().sessions;
  225. const session = sessions.at(sessionIndex);
  226. const messages = session?.messages;
  227. updater(messages?.at(messageIndex));
  228. set(() => ({ sessions }));
  229. },
  230. summarizeSession() {
  231. const session = get().currentSession();
  232. if (session.topic === DEFAULT_TOPIC) {
  233. // should summarize topic
  234. requestWithPrompt(
  235. session.messages,
  236. "直接返回这句话的简要主题,不要解释,如果没有主题,请直接返回“闲聊”"
  237. ).then((res) => {
  238. get().updateCurrentSession(
  239. (session) => (session.topic = trimTopic(res))
  240. );
  241. });
  242. }
  243. const messages = get().getMessagesWithMemory()
  244. const toBeSummarizedMsgs = messages.slice(session.lastSummarizeIndex)
  245. const historyMsgLength = session.memoryPrompt.length + toBeSummarizedMsgs.reduce((pre, cur) => pre + cur.content.length, 0)
  246. const lastSummarizeIndex = messages.length
  247. if (historyMsgLength > 500) {
  248. requestChatStream(toBeSummarizedMsgs.concat({
  249. role: 'system',
  250. content: '总结一下你和用户的对话,用作后续的上下文提示 prompt,控制在 50 字以内',
  251. date: ''
  252. }), {
  253. filterBot: false,
  254. onMessage(message, done) {
  255. session.memoryPrompt = message
  256. session.lastSummarizeIndex = lastSummarizeIndex
  257. if (done) {
  258. console.log('[Memory] ', session.memoryPrompt)
  259. }
  260. },
  261. onError(error) {
  262. console.error('[Summarize] ', error)
  263. },
  264. })
  265. }
  266. },
  267. updateStat(message) {
  268. get().updateCurrentSession((session) => {
  269. session.stat.charCount += message.content.length;
  270. // TODO: should update chat count and word count
  271. });
  272. },
  273. updateCurrentSession(updater) {
  274. const sessions = get().sessions;
  275. const index = get().currentSessionIndex;
  276. updater(sessions[index]);
  277. set(() => ({ sessions }));
  278. },
  279. clearAllData() {
  280. if (confirm('确认清除所有聊天、设置数据?')) {
  281. localStorage.clear()
  282. location.reload()
  283. }
  284. },
  285. }),
  286. {
  287. name: LOCAL_KEY,
  288. }
  289. )
  290. );