chat.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. import { trimTopic } from "../utils";
  2. import Locale, { getLang } from "../locales";
  3. import { showToast } from "../components/ui-lib";
  4. import { MaskConfig, useAppConfig } from "./config";
  5. import { createEmptyMask, Mask } from "./mask";
  6. import { DEFAULT_INPUT_TEMPLATE, StoreKey } from "../constant";
  7. import { ChatControllerPool } from "../client/common/controller";
  8. import { prettyObject } from "../utils/format";
  9. import { estimateTokenLength } from "../utils/token";
  10. import { nanoid } from "nanoid";
  11. import { createPersistStore } from "../utils/store";
  12. import { RequestMessage, api } from "../client";
  13. export type ChatMessage = RequestMessage & {
  14. date: string;
  15. streaming?: boolean;
  16. isError?: boolean;
  17. id: string;
  18. model?: string;
  19. };
  20. export function createMessage(override: Partial<ChatMessage>): ChatMessage {
  21. return {
  22. id: nanoid(),
  23. date: new Date().toLocaleString(),
  24. role: "user",
  25. content: "",
  26. ...override,
  27. };
  28. }
  29. export interface ChatStat {
  30. tokenCount: number;
  31. wordCount: number;
  32. charCount: number;
  33. }
  34. export interface ChatSession {
  35. id: string;
  36. topic: string;
  37. memoryPrompt: string;
  38. messages: ChatMessage[];
  39. stat: ChatStat;
  40. lastUpdate: number;
  41. lastSummarizeIndex: number;
  42. clearContextIndex?: number;
  43. mask: Mask;
  44. }
  45. export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
  46. export const BOT_HELLO: ChatMessage = createMessage({
  47. role: "assistant",
  48. content: Locale.Store.BotHello,
  49. });
  50. function createEmptySession(): ChatSession {
  51. return {
  52. id: nanoid(),
  53. topic: DEFAULT_TOPIC,
  54. memoryPrompt: "",
  55. messages: [],
  56. stat: {
  57. tokenCount: 0,
  58. wordCount: 0,
  59. charCount: 0,
  60. },
  61. lastUpdate: Date.now(),
  62. lastSummarizeIndex: 0,
  63. mask: createEmptyMask(),
  64. };
  65. }
  66. function countMessages(msgs: ChatMessage[]) {
  67. return msgs.reduce((pre, cur) => pre + estimateTokenLength(cur.content), 0);
  68. }
  69. function fillTemplateWith(
  70. input: string,
  71. context: {
  72. model: string;
  73. template?: string;
  74. },
  75. ) {
  76. const vars = {
  77. model: context.model,
  78. time: new Date().toLocaleString(),
  79. lang: getLang(),
  80. input: input,
  81. };
  82. let output = context.template ?? DEFAULT_INPUT_TEMPLATE;
  83. // must contains {{input}}
  84. const inputVar = "{{input}}";
  85. if (!output.includes(inputVar)) {
  86. output += "\n" + inputVar;
  87. }
  88. Object.entries(vars).forEach(([name, value]) => {
  89. output = output.replaceAll(`{{${name}}}`, value);
  90. });
  91. return output;
  92. }
  93. const DEFAULT_CHAT_STATE = {
  94. sessions: [createEmptySession()],
  95. currentSessionIndex: 0,
  96. };
  97. export const useChatStore = createPersistStore(
  98. DEFAULT_CHAT_STATE,
  99. (set, _get) => {
  100. function get() {
  101. return {
  102. ..._get(),
  103. ...methods,
  104. };
  105. }
  106. const methods = {
  107. clearSessions() {
  108. set(() => ({
  109. sessions: [createEmptySession()],
  110. currentSessionIndex: 0,
  111. }));
  112. },
  113. selectSession(index: number) {
  114. set({
  115. currentSessionIndex: index,
  116. });
  117. },
  118. moveSession(from: number, to: number) {
  119. set((state) => {
  120. const { sessions, currentSessionIndex: oldIndex } = state;
  121. // move the session
  122. const newSessions = [...sessions];
  123. const session = newSessions[from];
  124. newSessions.splice(from, 1);
  125. newSessions.splice(to, 0, session);
  126. // modify current session id
  127. let newIndex = oldIndex === from ? to : oldIndex;
  128. if (oldIndex > from && oldIndex <= to) {
  129. newIndex -= 1;
  130. } else if (oldIndex < from && oldIndex >= to) {
  131. newIndex += 1;
  132. }
  133. return {
  134. currentSessionIndex: newIndex,
  135. sessions: newSessions,
  136. };
  137. });
  138. },
  139. newSession(mask?: Mask) {
  140. const session = createEmptySession();
  141. if (mask) {
  142. const config = useAppConfig.getState();
  143. const globalModelConfig = config.globalMaskConfig;
  144. session.mask = {
  145. ...mask,
  146. config: {
  147. ...globalModelConfig,
  148. ...mask.config,
  149. },
  150. };
  151. session.topic = mask.name;
  152. }
  153. set((state) => ({
  154. currentSessionIndex: 0,
  155. sessions: [session].concat(state.sessions),
  156. }));
  157. },
  158. nextSession(delta: number) {
  159. const n = get().sessions.length;
  160. const limit = (x: number) => (x + n) % n;
  161. const i = get().currentSessionIndex;
  162. get().selectSession(limit(i + delta));
  163. },
  164. deleteSession(index: number) {
  165. const deletingLastSession = get().sessions.length === 1;
  166. const deletedSession = get().sessions.at(index);
  167. if (!deletedSession) return;
  168. const sessions = get().sessions.slice();
  169. sessions.splice(index, 1);
  170. const currentIndex = get().currentSessionIndex;
  171. let nextIndex = Math.min(
  172. currentIndex - Number(index < currentIndex),
  173. sessions.length - 1,
  174. );
  175. if (deletingLastSession) {
  176. nextIndex = 0;
  177. sessions.push(createEmptySession());
  178. }
  179. // for undo delete action
  180. const restoreState = {
  181. currentSessionIndex: get().currentSessionIndex,
  182. sessions: get().sessions.slice(),
  183. };
  184. set(() => ({
  185. currentSessionIndex: nextIndex,
  186. sessions,
  187. }));
  188. showToast(
  189. Locale.Home.DeleteToast,
  190. {
  191. text: Locale.Home.Revert,
  192. onClick() {
  193. set(() => restoreState);
  194. },
  195. },
  196. 5000,
  197. );
  198. },
  199. currentSession() {
  200. let index = get().currentSessionIndex;
  201. const sessions = get().sessions;
  202. if (index < 0 || index >= sessions.length) {
  203. index = Math.min(sessions.length - 1, Math.max(0, index));
  204. set(() => ({ currentSessionIndex: index }));
  205. }
  206. const session = sessions[index];
  207. return session;
  208. },
  209. onNewMessage(message: ChatMessage) {
  210. get().updateCurrentSession((session) => {
  211. session.messages = session.messages.concat();
  212. session.lastUpdate = Date.now();
  213. });
  214. get().updateStat(message);
  215. get().summarizeSession();
  216. },
  217. getCurrentMaskConfig() {
  218. return get().currentSession().mask.config;
  219. },
  220. extractModelConfig(maskConfig: MaskConfig) {
  221. const provider = maskConfig.provider;
  222. if (!maskConfig.modelConfig[provider]) {
  223. throw Error("[Chat] failed to initialize provider: " + provider);
  224. }
  225. return maskConfig.modelConfig[provider];
  226. },
  227. getCurrentModelConfig() {
  228. const maskConfig = this.getCurrentMaskConfig();
  229. return this.extractModelConfig(maskConfig);
  230. },
  231. getMaxTokens() {
  232. const maskConfig = this.getCurrentMaskConfig();
  233. if (maskConfig.provider === "openai") {
  234. return maskConfig.modelConfig.openai.max_tokens;
  235. } else if (maskConfig.provider === "anthropic") {
  236. return maskConfig.modelConfig.anthropic.max_tokens_to_sample;
  237. }
  238. return 8192;
  239. },
  240. getClient() {
  241. const appConfig = useAppConfig.getState();
  242. const currentMaskConfig = get().getCurrentMaskConfig();
  243. return api.createLLMClient(appConfig.providerConfig, currentMaskConfig);
  244. },
  245. async onUserInput(content: string) {
  246. const session = get().currentSession();
  247. const maskConfig = this.getCurrentMaskConfig();
  248. const modelConfig = this.getCurrentModelConfig();
  249. const userContent = fillTemplateWith(content, {
  250. model: modelConfig.model,
  251. template: maskConfig.chatConfig.template,
  252. });
  253. console.log("[User Input] after template: ", userContent);
  254. const userMessage: ChatMessage = createMessage({
  255. role: "user",
  256. content: userContent,
  257. });
  258. const botMessage: ChatMessage = createMessage({
  259. role: "assistant",
  260. streaming: true,
  261. model: modelConfig.model,
  262. });
  263. // get recent messages
  264. const recentMessages = get().getMessagesWithMemory();
  265. const sendMessages = recentMessages.concat(userMessage);
  266. const messageIndex = get().currentSession().messages.length + 1;
  267. // save user's and bot's message
  268. get().updateCurrentSession((session) => {
  269. const savedUserMessage = {
  270. ...userMessage,
  271. content,
  272. };
  273. session.messages = session.messages.concat([
  274. savedUserMessage,
  275. botMessage,
  276. ]);
  277. });
  278. const client = this.getClient();
  279. // make request
  280. client.chatStream({
  281. messages: sendMessages,
  282. onUpdate(message) {
  283. botMessage.streaming = true;
  284. if (message) {
  285. botMessage.content = message;
  286. }
  287. get().updateCurrentSession((session) => {
  288. session.messages = session.messages.concat();
  289. });
  290. },
  291. onFinish(message) {
  292. botMessage.streaming = false;
  293. if (message) {
  294. botMessage.content = message;
  295. get().onNewMessage(botMessage);
  296. }
  297. ChatControllerPool.remove(session.id, botMessage.id);
  298. },
  299. onError(error) {
  300. const isAborted = error.message.includes("aborted");
  301. botMessage.content +=
  302. "\n\n" +
  303. prettyObject({
  304. error: true,
  305. message: error.message,
  306. });
  307. botMessage.streaming = false;
  308. userMessage.isError = !isAborted;
  309. botMessage.isError = !isAborted;
  310. get().updateCurrentSession((session) => {
  311. session.messages = session.messages.concat();
  312. });
  313. ChatControllerPool.remove(
  314. session.id,
  315. botMessage.id ?? messageIndex,
  316. );
  317. console.error("[Chat] failed ", error);
  318. },
  319. onController(controller) {
  320. // collect controller for stop/retry
  321. ChatControllerPool.addController(
  322. session.id,
  323. botMessage.id ?? messageIndex,
  324. controller,
  325. );
  326. },
  327. });
  328. },
  329. getMemoryPrompt() {
  330. const session = get().currentSession();
  331. return {
  332. role: "system",
  333. content:
  334. session.memoryPrompt.length > 0
  335. ? Locale.Store.Prompt.History(session.memoryPrompt)
  336. : "",
  337. date: "",
  338. } as ChatMessage;
  339. },
  340. getMessagesWithMemory() {
  341. const session = get().currentSession();
  342. const maskConfig = this.getCurrentMaskConfig();
  343. const chatConfig = maskConfig.chatConfig;
  344. const modelConfig = this.getCurrentModelConfig();
  345. const clearContextIndex = session.clearContextIndex ?? 0;
  346. const messages = session.messages.slice();
  347. const totalMessageCount = session.messages.length;
  348. // in-context prompts
  349. const contextPrompts = session.mask.context.slice();
  350. // system prompts, to get close to OpenAI Web ChatGPT
  351. const shouldInjectSystemPrompts = chatConfig.enableInjectSystemPrompts;
  352. const systemPrompts = shouldInjectSystemPrompts
  353. ? [
  354. createMessage({
  355. role: "system",
  356. content: fillTemplateWith("", {
  357. model: modelConfig.model,
  358. template: chatConfig.template,
  359. }),
  360. }),
  361. ]
  362. : [];
  363. if (shouldInjectSystemPrompts) {
  364. console.log(
  365. "[Global System Prompt] ",
  366. systemPrompts.at(0)?.content ?? "empty",
  367. );
  368. }
  369. // long term memory
  370. const shouldSendLongTermMemory =
  371. chatConfig.sendMemory &&
  372. session.memoryPrompt &&
  373. session.memoryPrompt.length > 0 &&
  374. session.lastSummarizeIndex > clearContextIndex;
  375. const longTermMemoryPrompts = shouldSendLongTermMemory
  376. ? [get().getMemoryPrompt()]
  377. : [];
  378. const longTermMemoryStartIndex = session.lastSummarizeIndex;
  379. // short term memory
  380. const shortTermMemoryStartIndex = Math.max(
  381. 0,
  382. totalMessageCount - chatConfig.historyMessageCount,
  383. );
  384. // lets concat send messages, including 4 parts:
  385. // 0. system prompt: to get close to OpenAI Web ChatGPT
  386. // 1. long term memory: summarized memory messages
  387. // 2. pre-defined in-context prompts
  388. // 3. short term memory: latest n messages
  389. // 4. newest input message
  390. const memoryStartIndex = shouldSendLongTermMemory
  391. ? Math.min(longTermMemoryStartIndex, shortTermMemoryStartIndex)
  392. : shortTermMemoryStartIndex;
  393. // and if user has cleared history messages, we should exclude the memory too.
  394. const contextStartIndex = Math.max(clearContextIndex, memoryStartIndex);
  395. const maxTokenThreshold = this.getMaxTokens();
  396. // get recent messages as much as possible
  397. const reversedRecentMessages = [];
  398. for (
  399. let i = totalMessageCount - 1, tokenCount = 0;
  400. i >= contextStartIndex && tokenCount < maxTokenThreshold;
  401. i -= 1
  402. ) {
  403. const msg = messages[i];
  404. if (!msg || msg.isError) continue;
  405. tokenCount += estimateTokenLength(msg.content);
  406. reversedRecentMessages.push(msg);
  407. }
  408. // concat all messages
  409. const recentMessages = [
  410. ...systemPrompts,
  411. ...longTermMemoryPrompts,
  412. ...contextPrompts,
  413. ...reversedRecentMessages.reverse(),
  414. ];
  415. return recentMessages;
  416. },
  417. updateMessage(
  418. sessionIndex: number,
  419. messageIndex: number,
  420. updater: (message?: ChatMessage) => void,
  421. ) {
  422. const sessions = get().sessions;
  423. const session = sessions.at(sessionIndex);
  424. const messages = session?.messages;
  425. updater(messages?.at(messageIndex));
  426. set(() => ({ sessions }));
  427. },
  428. resetSession() {
  429. get().updateCurrentSession((session) => {
  430. session.messages = [];
  431. session.memoryPrompt = "";
  432. });
  433. },
  434. summarizeSession() {
  435. const config = useAppConfig.getState();
  436. const maskConfig = this.getCurrentMaskConfig();
  437. const chatConfig = maskConfig.chatConfig;
  438. const session = get().currentSession();
  439. // remove error messages if any
  440. const messages = session.messages;
  441. // should summarize topic after chating more than 50 words
  442. const SUMMARIZE_MIN_LEN = 50;
  443. if (
  444. chatConfig.enableAutoGenerateTitle &&
  445. session.topic === DEFAULT_TOPIC &&
  446. countMessages(messages) >= SUMMARIZE_MIN_LEN
  447. ) {
  448. const topicMessages = messages.concat(
  449. createMessage({
  450. role: "user",
  451. content: Locale.Store.Prompt.Topic,
  452. }),
  453. );
  454. const client = this.getClient();
  455. client.chat({
  456. messages: topicMessages,
  457. shouldSummarize: true,
  458. onFinish(message) {
  459. get().updateCurrentSession(
  460. (session) =>
  461. (session.topic =
  462. message.length > 0 ? trimTopic(message) : DEFAULT_TOPIC),
  463. );
  464. },
  465. });
  466. }
  467. const summarizeIndex = Math.max(
  468. session.lastSummarizeIndex,
  469. session.clearContextIndex ?? 0,
  470. );
  471. let toBeSummarizedMsgs = messages
  472. .filter((msg) => !msg.isError)
  473. .slice(summarizeIndex);
  474. const historyMsgLength = countMessages(toBeSummarizedMsgs);
  475. if (historyMsgLength > this.getMaxTokens()) {
  476. const n = toBeSummarizedMsgs.length;
  477. toBeSummarizedMsgs = toBeSummarizedMsgs.slice(
  478. Math.max(0, n - chatConfig.historyMessageCount),
  479. );
  480. }
  481. // add memory prompt
  482. toBeSummarizedMsgs.unshift(get().getMemoryPrompt());
  483. const lastSummarizeIndex = session.messages.length;
  484. console.log(
  485. "[Chat History] ",
  486. toBeSummarizedMsgs,
  487. historyMsgLength,
  488. chatConfig.compressMessageLengthThreshold,
  489. );
  490. if (
  491. historyMsgLength > chatConfig.compressMessageLengthThreshold &&
  492. chatConfig.sendMemory
  493. ) {
  494. this.getClient().chatStream({
  495. messages: toBeSummarizedMsgs.concat(
  496. createMessage({
  497. role: "system",
  498. content: Locale.Store.Prompt.Summarize,
  499. date: "",
  500. }),
  501. ),
  502. shouldSummarize: true,
  503. onUpdate(message) {
  504. session.memoryPrompt = message;
  505. },
  506. onFinish(message) {
  507. console.log("[Memory] ", message);
  508. session.lastSummarizeIndex = lastSummarizeIndex;
  509. },
  510. onError(err) {
  511. console.error("[Summarize] ", err);
  512. },
  513. });
  514. }
  515. },
  516. updateStat(message: ChatMessage) {
  517. get().updateCurrentSession((session) => {
  518. session.stat.charCount += message.content.length;
  519. // TODO: should update chat count and word count
  520. });
  521. },
  522. updateCurrentSession(updater: (session: ChatSession) => void) {
  523. const sessions = get().sessions;
  524. const index = get().currentSessionIndex;
  525. updater(sessions[index]);
  526. set(() => ({ sessions }));
  527. },
  528. clearAllData() {
  529. localStorage.clear();
  530. location.reload();
  531. },
  532. };
  533. return methods;
  534. },
  535. {
  536. name: StoreKey.Chat,
  537. version: 3.1,
  538. migrate(persistedState, version) {
  539. // TODO(yifei): migrate from old versions
  540. return persistedState as any;
  541. },
  542. },
  543. );