diff --git a/app/components/chat.module.scss b/app/components/chat.module.scss index 3a1be3910..0e2741e70 100644 --- a/app/components/chat.module.scss +++ b/app/components/chat.module.scss @@ -107,3 +107,70 @@ user-select: text; } } + +.clear-context { + margin: 20px 0 0 0; + padding: 4px 0; + + border-top: var(--border-in-light); + border-bottom: var(--border-in-light); + box-shadow: var(--card-shadow) inset; + + display: flex; + justify-content: center; + align-items: center; + + color: var(--black); + transition: all ease 0.3s; + cursor: pointer; + overflow: hidden; + position: relative; + font-size: 12px; + + animation: slide-in ease 0.3s; + + $linear: linear-gradient( + to right, + rgba(0, 0, 0, 0), + rgba(0, 0, 0, 1), + rgba(0, 0, 0, 0) + ); + mask-image: $linear; + + @mixin show { + transform: translateY(0); + position: relative; + transition: all ease 0.3s; + opacity: 1; + } + + @mixin hide { + transform: translateY(-50%); + position: absolute; + transition: all ease 0.1s; + opacity: 0; + } + + &-tips { + @include show; + opacity: 0.5; + } + + &-revert-btn { + color: var(--primary); + @include hide; + } + + &:hover { + opacity: 1; + border-color: var(--primary); + + .clear-context-tips { + @include hide; + } + + .clear-context-revert-btn { + @include show; + } + } +} diff --git a/app/components/chat.tsx b/app/components/chat.tsx index e1cb78de6..d736d18d5 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -14,6 +14,8 @@ import MaskIcon from "../icons/mask.svg"; import MaxIcon from "../icons/max.svg"; import MinIcon from "../icons/min.svg"; import ResetIcon from "../icons/reload.svg"; +import BreakIcon from "../icons/break.svg"; +import SettingsIcon from "../icons/chat-settings.svg"; import LightIcon from "../icons/light.svg"; import DarkIcon from "../icons/dark.svg"; @@ -51,7 +53,7 @@ import { IconButton } from "./button"; import styles from "./home.module.scss"; import chatStyle from "./chat.module.scss"; -import { ListItem, Modal, showModal } from "./ui-lib"; +import { ListItem, Modal, showModal, showToast } from "./ui-lib"; import { useLocation, useNavigate } from "react-router-dom"; import { LAST_INPUT_KEY, Path, REQUEST_TIMEOUT_MS } from "../constant"; import { Avatar } from "./emoji"; @@ -289,6 +291,28 @@ export function PromptHints(props: { ); } +function ClearContextDivider() { + const chatStore = useChatStore(); + + return ( + <div + className={chatStyle["clear-context"]} + onClick={() => + chatStore.updateCurrentSession( + (session) => (session.clearContextIndex = -1), + ) + } + > + <div className={chatStyle["clear-context-tips"]}> + {Locale.Context.Clear} + </div> + <div className={chatStyle["clear-context-revert-btn"]}> + {Locale.Context.Revert} + </div> + </div> + ); +} + function useScrollToBottom() { // for auto-scroll const scrollRef = useRef<HTMLDivElement>(null); @@ -321,6 +345,7 @@ export function ChatActions(props: { }) { const config = useAppConfig(); const navigate = useNavigate(); + const chatStore = useChatStore(); // switch themes const theme = config.theme; @@ -359,7 +384,7 @@ export function ChatActions(props: { className={`${chatStyle["chat-input-action"]} clickable`} onClick={props.showPromptModal} > - <BrainIcon /> + <SettingsIcon /> </div> )} @@ -391,6 +416,22 @@ export function ChatActions(props: { > <MaskIcon /> </div> + + <div + className={`${chatStyle["chat-input-action"]} clickable`} + onClick={() => { + chatStore.updateCurrentSession((session) => { + if (session.clearContextIndex === session.messages.length) { + session.clearContextIndex = -1; + } else { + session.clearContextIndex = session.messages.length; + session.memoryPrompt = ""; // will clear memory + } + }); + }} + > + <BreakIcon /> + </div> </div> ); } @@ -602,6 +643,12 @@ export function Chat() { context.push(copiedHello); } + // clear context index = context length + index in messages + const clearContextIndex = + (session.clearContextIndex ?? -1) >= 0 + ? session.clearContextIndex! + context.length + : -1; + // preview messages const messages = context .concat(session.messages as RenderMessage[]) @@ -736,86 +783,91 @@ export function Chat() { !(message.preview || message.content.length === 0); const showTyping = message.preview || message.streaming; + const shouldShowClearContextDivider = i === clearContextIndex - 1; + return ( - <div - key={i} - className={ - isUser ? styles["chat-message-user"] : styles["chat-message"] - } - > - <div className={styles["chat-message-container"]}> - <div className={styles["chat-message-avatar"]}> - {message.role === "user" ? ( - <Avatar avatar={config.avatar} /> - ) : ( - <MaskAvatar mask={session.mask} /> - )} - </div> - {showTyping && ( - <div className={styles["chat-message-status"]}> - {Locale.Chat.Typing} + <> + <div + key={i} + className={ + isUser ? styles["chat-message-user"] : styles["chat-message"] + } + > + <div className={styles["chat-message-container"]}> + <div className={styles["chat-message-avatar"]}> + {message.role === "user" ? ( + <Avatar avatar={config.avatar} /> + ) : ( + <MaskAvatar mask={session.mask} /> + )} </div> - )} - <div className={styles["chat-message-item"]}> - {showActions && ( - <div className={styles["chat-message-top-actions"]}> - {message.streaming ? ( + {showTyping && ( + <div className={styles["chat-message-status"]}> + {Locale.Chat.Typing} + </div> + )} + <div className={styles["chat-message-item"]}> + {showActions && ( + <div className={styles["chat-message-top-actions"]}> + {message.streaming ? ( + <div + className={styles["chat-message-top-action"]} + onClick={() => onUserStop(message.id ?? i)} + > + {Locale.Chat.Actions.Stop} + </div> + ) : ( + <> + <div + className={styles["chat-message-top-action"]} + onClick={() => onDelete(message.id ?? i)} + > + {Locale.Chat.Actions.Delete} + </div> + <div + className={styles["chat-message-top-action"]} + onClick={() => onResend(message.id ?? i)} + > + {Locale.Chat.Actions.Retry} + </div> + </> + )} + <div className={styles["chat-message-top-action"]} - onClick={() => onUserStop(message.id ?? i)} + onClick={() => copyToClipboard(message.content)} > - {Locale.Chat.Actions.Stop} + {Locale.Chat.Actions.Copy} </div> - ) : ( - <> - <div - className={styles["chat-message-top-action"]} - onClick={() => onDelete(message.id ?? i)} - > - {Locale.Chat.Actions.Delete} - </div> - <div - className={styles["chat-message-top-action"]} - onClick={() => onResend(message.id ?? i)} - > - {Locale.Chat.Actions.Retry} - </div> - </> - )} - - <div - className={styles["chat-message-top-action"]} - onClick={() => copyToClipboard(message.content)} - > - {Locale.Chat.Actions.Copy} + </div> + )} + <Markdown + content={message.content} + loading={ + (message.preview || message.content.length === 0) && + !isUser + } + onContextMenu={(e) => onRightClick(e, message)} + onDoubleClickCapture={() => { + if (!isMobileScreen) return; + setUserInput(message.content); + }} + fontSize={fontSize} + parentRef={scrollRef} + defaultShow={i >= messages.length - 10} + /> + </div> + {!isUser && !message.preview && ( + <div className={styles["chat-message-actions"]}> + <div className={styles["chat-message-action-date"]}> + {message.date.toLocaleString()} </div> </div> )} - <Markdown - content={message.content} - loading={ - (message.preview || message.content.length === 0) && - !isUser - } - onContextMenu={(e) => onRightClick(e, message)} - onDoubleClickCapture={() => { - if (!isMobileScreen) return; - setUserInput(message.content); - }} - fontSize={fontSize} - parentRef={scrollRef} - defaultShow={i >= messages.length - 10} - /> </div> - {!isUser && !message.preview && ( - <div className={styles["chat-message-actions"]}> - <div className={styles["chat-message-action-date"]}> - {message.date.toLocaleString()} - </div> - </div> - )} </div> - </div> + {shouldShowClearContextDivider && <ClearContextDivider />} + </> ); })} </div> diff --git a/app/icons/break.svg b/app/icons/break.svg new file mode 100644 index 000000000..fbfe04f46 --- /dev/null +++ b/app/icons/break.svg @@ -0,0 +1 @@ +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16" fill="none"><g opacity="1" transform="translate(0 0) rotate(0)"><g opacity="1" transform="translate(1.0001220703125 2) rotate(0)"><path id="路径 1" style="fill:#333333; opacity:1;" d="M13.275,-0.27515c0.261,0.26101 0.3915,0.57606 0.3915,0.94515v10.66c0,0.36907 -0.1305,0.68413 -0.3915,0.9452c-0.261,0.261 -0.57603,0.3915 -0.9451,0.3915h-10.66002c-0.36909,0 -0.68415,-0.1305 -0.94516,-0.3915c-0.26101,-0.26107 -0.39151,-0.57613 -0.39151,-0.9452v-10.66c0,-0.3691 0.1305,-0.68415 0.39151,-0.94515c0.26101,-0.26101 0.57606,-0.39151 0.94516,-0.39151h10.66002c0.36907,0 0.6841,0.1305 0.9451,0.39151zM1.66655,11.33c0,0.0022 0.00111,0.0033 0.00333,0.0033h10.66002c0.0022,0 0.0033,-0.0011 0.0033,-0.0033v-10.66c0,-0.00222 -0.0011,-0.00333 -0.0033,-0.00333l-10.66002,0c-0.00222,0 -0.00333,0.00111 -0.00333,0.00333z"></path><path id="路径 2" style="fill:#333333; opacity:1;" d="M9.76327,7.50715c-0.02999,0.02563 -0.06201,0.04842 -0.09604,0.06837c-0.03403,0.01995 -0.06956,0.03674 -0.10658,0.05039c-0.03702,0.01364 -0.07495,0.02391 -0.11379,0.03082c-0.03885,0.00691 -0.07799,0.01035 -0.11744,0.0103c-0.03945,-0.00004 -0.07859,-0.00356 -0.11742,-0.01055c-0.03883,-0.00699 -0.07674,-0.01734 -0.11373,-0.03106c-0.03699,-0.01372 -0.07248,-0.03059 -0.10647,-0.05061c-0.03399,-0.02002 -0.06596,-0.04288 -0.0959,-0.06858l-1.89578,-1.62728l-1.89578,1.62728c-0.02993,0.0257 -0.0619,0.04856 -0.09589,0.06858c-0.03399,0.02002 -0.06949,0.03689 -0.10648,0.05061c-0.03699,0.01372 -0.07489,0.02407 -0.11372,0.03106c-0.03883,0.00699 -0.07797,0.01051 -0.11742,0.01055c-0.03945,0.00005 -0.0786,-0.00339 -0.11744,-0.0103c-0.03885,-0.00691 -0.07678,-0.01718 -0.11379,-0.03082c-0.03702,-0.01365 -0.07255,-0.03044 -0.10658,-0.05039c-0.03404,-0.01995 -0.06605,-0.04274 -0.09604,-0.06837l-1.90593,-1.629l-1.89671,1.62808c-0.06708,0.05758 -0.14263,0.10013 -0.22664,0.12766c-0.08401,0.02753 -0.17009,0.03793 -0.25824,0.03121c-0.08815,-0.00671 -0.17166,-0.03004 -0.25053,-0.06998c-0.07887,-0.03994 -0.14709,-0.09345 -0.20467,-0.16054c-0.02851,-0.03321 -0.05351,-0.06889 -0.07499,-0.10703c-0.02148,-0.03814 -0.03904,-0.07801 -0.05267,-0.11961c-0.01363,-0.04159 -0.02307,-0.08412 -0.02832,-0.12758c-0.00525,-0.04346 -0.00622,-0.08701 -0.00289,-0.13066c0.00333,-0.04365 0.01088,-0.08655 0.02266,-0.12871c0.01178,-0.04216 0.02755,-0.08277 0.04733,-0.12182c0.01978,-0.03905 0.04317,-0.07579 0.07019,-0.11024c0.02701,-0.03444 0.05713,-0.06592 0.09035,-0.09443l2.32999,-2c0.02994,-0.02569 0.06191,-0.04855 0.0959,-0.06857c0.03399,-0.02003 0.06948,-0.0369 0.10647,-0.05062c0.03699,-0.01372 0.0749,-0.02407 0.11373,-0.03106c0.03883,-0.00699 0.07797,-0.01051 0.11742,-0.01055c0.03945,-0.00004 0.0786,0.00339 0.11744,0.0103c0.03884,0.00691 0.07677,0.01718 0.11379,0.03082c0.03702,0.01365 0.07255,0.03044 0.10658,0.05039c0.03404,0.01995 0.06605,0.04274 0.09604,0.06837l1.90592,1.629l1.89671,-1.62808c0.02998,-0.02573 0.062,-0.04862 0.09605,-0.06866c0.03405,-0.02005 0.0696,-0.03693 0.10665,-0.05065c0.03705,-0.01372 0.07503,-0.02407 0.11392,-0.03104c0.03889,-0.00697 0.07809,-0.01045 0.1176,-0.01045c0.03951,0 0.07872,0.00348 0.11761,0.01045c0.03889,0.00697 0.07686,0.01732 0.11391,0.03104c0.03705,0.01372 0.0726,0.0306 0.10665,0.05065c0.03405,0.02004 0.06607,0.04293 0.09605,0.06866l1.89671,1.62808l1.90595,-1.629c0.03,-0.02563 0.062,-0.04842 0.096,-0.06837c0.03407,-0.01995 0.0696,-0.03674 0.1066,-0.05038c0.037,-0.01365 0.07493,-0.02392 0.1138,-0.03083c0.03887,-0.00691 0.078,-0.01034 0.1174,-0.0103c0.03947,0.00004 0.0786,0.00356 0.1174,0.01055c0.03887,0.00699 0.0768,0.01734 0.1138,0.03106c0.037,0.01372 0.07247,0.03059 0.1064,0.05062c0.034,0.02002 0.06597,0.04288 0.0959,0.06857l2.33,2c0.06713,0.05758 0.12067,0.12581 0.1606,0.20468c0.03993,0.07887 0.06327,0.16237 0.07,0.25052c0.00667,0.08815 -0.00377,0.17424 -0.0313,0.25825c-0.02747,0.08401 -0.07,0.15955 -0.1276,0.22663c-0.02853,0.03322 -0.06,0.06334 -0.0944,0.09035c-0.03447,0.02701 -0.07123,0.05041 -0.1103,0.07019c-0.03907,0.01977 -0.07967,0.03555 -0.1218,0.04733c-0.04213,0.01177 -0.08503,0.01932 -0.1287,0.02265c-0.04367,0.00333 -0.08723,0.00236 -0.1307,-0.00289c-0.04347,-0.00525 -0.086,-0.01469 -0.1276,-0.02832c-0.0416,-0.01363 -0.08147,-0.03118 -0.1196,-0.05267c-0.03813,-0.02148 -0.0738,-0.04648 -0.107,-0.07499l-1.8967,-1.62808z"></path></g><g opacity="1" transform="translate(0 0) rotate(0)"><mask id="bg-mask-0" fill="white"><use xlink:href="#path_0"></use></mask><g mask="url(#bg-mask-0)" ></g></g></g><defs><rect id="path_0" x="0" y="0" width="16" height="16" /></defs></svg> diff --git a/app/icons/chat-settings.svg b/app/icons/chat-settings.svg new file mode 100644 index 000000000..0a37b294c --- /dev/null +++ b/app/icons/chat-settings.svg @@ -0,0 +1 @@ +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16" fill="none"><g opacity="1" transform="translate(0 0) rotate(0)"><mask id="bg-mask-0" fill="white"><use xlink:href="#path_0"></use></mask><g mask="url(#bg-mask-0)" ><path id="路径 1" style="fill:#333333; opacity:1;" d="M6.72814,14.5981c-0.01324,0.04127 -0.03034,0.08087 -0.0513,0.1188c-0.02097,0.03787 -0.04539,0.0734 -0.07328,0.1066c-0.02789,0.03313 -0.05873,0.06327 -0.0925,0.0904c-0.03377,0.0272 -0.06984,0.05083 -0.10822,0.0709c-0.03837,0.02013 -0.07833,0.03637 -0.11987,0.0487c-0.04154,0.01233 -0.08388,0.02053 -0.12702,0.0246c-0.04314,0.00407 -0.08626,0.00393 -0.12937,-0.0004c-0.04311,-0.0044 -0.0854,-0.01287 -0.12686,-0.0254c-1.23645,-0.3754 -2.2986,-1.02387 -3.18645,-1.9454c-0.02851,-0.0296 -0.05407,-0.06157 -0.07668,-0.0959c-0.02261,-0.03433 -0.04187,-0.07043 -0.0578,-0.1083c-0.01592,-0.03793 -0.02824,-0.07697 -0.03695,-0.1171c-0.00871,-0.0402 -0.01366,-0.08083 -0.01485,-0.1219c-0.00119,-0.04107 0.00139,-0.0819 0.00775,-0.1225c0.00636,-0.0406 0.01639,-0.08027 0.03008,-0.119c0.0137,-0.0388 0.03083,-0.07597 0.0514,-0.1115c0.02057,-0.0356 0.04423,-0.069 0.07098,-0.1002c0.20634,-0.24073 0.30951,-0.52613 0.30951,-0.8562c0,-0.37087 -0.1295,-0.6858 -0.38849,-0.9448c-0.25899,-0.259 -0.57394,-0.3885 -0.94485,-0.3885c-0.04989,0 -0.07669,0.0003 -0.0804,0.0009c-0.04217,0.00707 -0.08462,0.01003 -0.12735,0.0089c-0.04273,-0.00113 -0.08496,-0.00634 -0.12669,-0.01563c-0.04173,-0.00926 -0.0822,-0.02241 -0.1214,-0.03946c-0.0392,-0.01705 -0.07642,-0.03767 -0.11165,-0.06188c-0.03523,-0.02421 -0.06784,-0.05155 -0.09782,-0.08202c-0.02997,-0.03048 -0.05677,-0.06354 -0.08038,-0.09917c-0.02362,-0.03563 -0.04362,-0.07318 -0.06001,-0.11266c-0.01639,-0.03948 -0.02887,-0.08016 -0.03744,-0.12204c-0.10235,-0.50037 -0.15352,-0.99157 -0.15352,-1.4736c0,-0.75119 0.11375,-1.48509 0.34125,-2.20172c0.02141,-0.06746 0.05278,-0.12993 0.09409,-0.1874c0.04131,-0.05747 0.09053,-0.1071 0.14765,-0.1489c0.05712,-0.04179 0.11932,-0.07368 0.18659,-0.09567c0.06728,-0.02199 0.13631,-0.03298 0.20708,-0.03298h0.02c0.36931,0 0.68399,-0.13047 0.94404,-0.39142c0.25953,-0.26043 0.3893,-0.5744 0.3893,-0.94191c0,-0.21499 -0.04467,-0.41525 -0.134,-0.60079c-0.01539,-0.03197 -0.02815,-0.06495 -0.03828,-0.09896c-0.01013,-0.03401 -0.01749,-0.0686 -0.02209,-0.10378c-0.0046,-0.03519 -0.00638,-0.07051 -0.00535,-0.10598c0.00104,-0.03547 0.00489,-0.07063 0.01154,-0.10548c0.00665,-0.03485 0.01603,-0.06895 0.02812,-0.10231c0.0121,-0.03335 0.02677,-0.06554 0.044,-0.09655c0.01724,-0.03101 0.03683,-0.06046 0.05877,-0.08835c0.02193,-0.02789 0.04594,-0.05386 0.07203,-0.07791c0.88008,-0.81151 1.8877,-1.37553 3.02286,-1.69206c0.03683,-0.01027 0.07426,-0.01732 0.1123,-0.02115c0.03804,-0.00383 0.07613,-0.00438 0.11426,-0.00166c0.03813,0.00272 0.07575,0.00868 0.11286,0.01788c0.03711,0.0092 0.07317,0.02149 0.10816,0.03689c0.03499,0.0154 0.06841,0.03367 0.10026,0.05482c0.03185,0.02115 0.06166,0.04486 0.08943,0.07114c0.02777,0.02627 0.05309,0.05473 0.07597,0.08536c0.02287,0.03063 0.04296,0.063 0.06027,0.09709c0.11094,0.21851 0.27189,0.39311 0.48286,0.5238c0.21669,0.13421 0.45425,0.20132 0.7127,0.20132c0.25349,0 0.48779,-0.0669 0.7029,-0.20069c0.21084,-0.13113 0.37172,-0.30594 0.48265,-0.52443c0.01731,-0.03409 0.0374,-0.06646 0.06027,-0.09709c0.02287,-0.03063 0.0482,-0.05909 0.07597,-0.08536c0.02777,-0.02628 0.05758,-0.04999 0.08943,-0.07114c0.03185,-0.02115 0.06527,-0.03942 0.10026,-0.05482c0.03499,-0.0154 0.07105,-0.0277 0.10816,-0.03689c0.03711,-0.0092 0.07473,-0.01516 0.11286,-0.01788c0.03813,-0.00272 0.07622,-0.00217 0.11426,0.00166c0.03804,0.00383 0.07547,0.01088 0.1123,0.02115c1.12831,0.31462 2.13819,0.87768 3.02966,1.68916c0.027,0.02457 0.0518,0.05117 0.0744,0.0798c0.02267,0.02863 0.0428,0.05892 0.0604,0.09087c0.01767,0.03194 0.0326,0.0651 0.0448,0.09949c0.0122,0.03439 0.0215,0.06955 0.0279,0.10547c0.00647,0.03592 0.00993,0.07212 0.0104,0.10861c0.00047,0.03649 -0.00203,0.07277 -0.0075,0.10884c-0.00547,0.03608 -0.01387,0.07147 -0.0252,0.10617c-0.01127,0.0347 -0.0253,0.06825 -0.0421,0.10064c-0.09013,0.17379 -0.1352,0.36816 -0.1352,0.58312c0,0.36591 0.13077,0.67961 0.3923,0.9411c0.26147,0.26149 0.57517,0.39223 0.9411,0.39223h0.03c0.07073,0.00001 0.13977,0.011 0.2071,0.03298c0.06727,0.02199 0.12947,0.05388 0.1866,0.09567c0.05707,0.0418 0.10627,0.09143 0.1476,0.1489c0.04133,0.05747 0.0727,0.11994 0.0941,0.1874c0.2208,0.69563 0.3312,1.42953 0.3312,2.20172c0,0.51461 -0.04783,1.00581 -0.1435,1.4736c-0.0084,0.04124 -0.02063,0.08132 -0.0367,0.12025c-0.016,0.03892 -0.03553,0.07599 -0.0586,0.11122c-0.02307,0.03522 -0.04923,0.06797 -0.0785,0.09824c-0.0292,0.03027 -0.06103,0.05752 -0.0955,0.08176c-0.0344,0.02423 -0.07077,0.04502 -0.1091,0.06237c-0.0384,0.01735 -0.07807,0.03094 -0.119,0.04077c-0.04093,0.00983 -0.0824,0.01575 -0.1244,0.01775c-0.04207,0.00193 -0.08393,-0.00007 -0.1256,-0.006c-0.0154,-0.0022 -0.05063,-0.0033 -0.1057,-0.0033c-0.36753,0 -0.6815,0.12977 -0.9419,0.3893c-0.261,0.26007 -0.3915,0.57473 -0.3915,0.944c0,0.32167 0.10557,0.6099 0.3167,0.8647c0.02593,0.03127 0.0488,0.06463 0.0686,0.1001c0.01987,0.03547 0.03633,0.07243 0.0494,0.1109c0.01307,0.03853 0.02253,0.0779 0.0284,0.1181c0.00587,0.0402 0.00803,0.0806 0.0065,0.1212c-0.00153,0.0406 -0.00673,0.08073 -0.0156,0.1204c-0.00887,0.03967 -0.02123,0.07817 -0.0371,0.1155c-0.01593,0.0374 -0.0351,0.07307 -0.0575,0.107c-0.0224,0.03387 -0.0477,0.06543 -0.0759,0.0947c-0.88787,0.92153 -1.95003,1.57 -3.1865,1.9454c-0.04147,0.01253 -0.08374,0.021 -0.12683,0.0254c-0.04311,0.00433 -0.08624,0.00447 -0.12937,0.0004c-0.04314,-0.00407 -0.08548,-0.01227 -0.12702,-0.0246c-0.04154,-0.01233 -0.0815,-0.02857 -0.11987,-0.0487c-0.03838,-0.02007 -0.07446,-0.0437 -0.10823,-0.0709c-0.03377,-0.02713 -0.0646,-0.05727 -0.09249,-0.0904c-0.02789,-0.0332 -0.05232,-0.06873 -0.07328,-0.1066c-0.02096,-0.03793 -0.03806,-0.07753 -0.0513,-0.1188c-0.088,-0.27413 -0.24691,-0.49663 -0.47672,-0.6675c-0.23281,-0.17307 -0.49565,-0.2596 -0.78852,-0.2596c-0.29781,0 -0.56389,0.08677 -0.79823,0.2603c-0.22999,0.17033 -0.38899,0.3926 -0.477,0.6668zM10.2483,13.5665c0.62587,-0.253 1.19093,-0.6004 1.6952,-1.0422c-0.18453,-0.36547 -0.2768,-0.76213 -0.2768,-1.19c0,-0.36267 0.07023,-0.70967 0.2107,-1.041c0.1354,-0.31926 0.32637,-0.60174 0.5729,-0.84743c0.24593,-0.24511 0.52843,-0.43492 0.8475,-0.56943c0.2146,-0.09045 0.4355,-0.1515 0.6627,-0.18317c0.02413,-0.22265 0.0362,-0.4523 0.0362,-0.68893c0,-0.47946 -0.05117,-0.9389 -0.1535,-1.37832c-0.1866,-0.03495 -0.36897,-0.09025 -0.5471,-0.1659c-0.31847,-0.13531 -0.60067,-0.32594 -0.8466,-0.57187c-0.24593,-0.24594 -0.43657,-0.52816 -0.5719,-0.84667c-0.1406,-0.33093 -0.2109,-0.67668 -0.2109,-1.03724c0,-0.23901 0.0291,-0.46799 0.0873,-0.68693c-0.52047,-0.41351 -1.0849,-0.73156 -1.6933,-0.95415c-0.18127,0.22043 -0.39802,0.40907 -0.65023,0.56594c-0.4307,0.26787 -0.89973,0.40181 -1.40709,0.40181c-0.51145,0 -0.98305,-0.13372 -1.41482,-0.40117c-0.25318,-0.15683 -0.47066,-0.34573 -0.65245,-0.56668c-0.61189,0.22369 -1.17553,0.54268 -1.6909,0.95699c0.05655,0.22017 0.08483,0.44823 0.08483,0.68419c0,0.36022 -0.06959,0.7054 -0.20876,1.03554c-0.13451,0.31907 -0.32433,0.60159 -0.56944,0.84755c-0.24569,0.24654 -0.52817,0.4375 -0.84744,0.57287c-0.17627,0.07474 -0.35698,0.1296 -0.54214,0.16457c-0.10815,0.45165 -0.16222,0.91147 -0.16222,1.37947c0,0.22646 0.01407,0.45628 0.0422,0.68947c0.22681,0.0318 0.44696,0.09262 0.66046,0.18245c0.31983,0.13457 0.60261,0.32471 0.84833,0.57043c0.24572,0.24571 0.43586,0.52848 0.57043,0.84831c0.13905,0.33053 0.20858,0.67697 0.20858,1.0393c0,0.43413 -0.09124,0.8317 -0.27372,1.1927c0.50351,0.44053 1.06757,0.78703 1.69216,1.0395c0.17198,-0.27107 0.39302,-0.50663 0.66312,-0.7067c0.47005,-0.34807 1.00064,-0.5221 1.59177,-0.5221c0.58707,0 1.11507,0.1743 1.58399,0.5229c0.26914,0.20007 0.48945,0.43537 0.66094,0.7059z"></path><path id="路径 2" style="fill:#333333; opacity:1;" d="M10.1218,10.1218c-0.27729,0.27727 -0.59569,0.49207 -0.9552,0.6444c-0.37293,0.15807 -0.76292,0.2371 -1.16997,0.2371c-0.40727,0 -0.79698,-0.07913 -1.16915,-0.2374c-0.35877,-0.15253 -0.67624,-0.3677 -0.9524,-0.6455c-0.27567,-0.27731 -0.48912,-0.59583 -0.64036,-0.95555c-0.15651,-0.37225 -0.23476,-0.76166 -0.23476,-1.16822c0,-0.40677 0.07836,-0.79591 0.23507,-1.16741c0.15144,-0.35897 0.36525,-0.67656 0.64144,-0.95275c0.27619,-0.27619 0.59378,-0.49 0.95275,-0.64144c0.3715,-0.15671 0.76064,-0.23507 1.16741,-0.23507c0.40656,0 0.79597,0.07825 1.16822,0.23476c0.35972,0.15124 0.67824,0.36469 0.95555,0.64036c0.2778,0.27616 0.49297,0.59363 0.6455,0.9524c0.15827,0.37217 0.2374,0.76189 0.2374,1.16915c0,0.40705 -0.07903,0.79704 -0.2371,1.16997c-0.15233,0.35951 -0.36713,0.67791 -0.6444,0.9552zM7.99663,6.33329c-0.46091,0 -0.85336,0.162 -1.17735,0.48599c-0.32399,0.32399 -0.48599,0.71644 -0.48599,1.17735c0,0.46231 0.16246,0.85689 0.48738,1.18374c0.32447,0.32639 0.71646,0.48959 1.17596,0.48959c0.4609,0 0.85501,-0.16366 1.18234,-0.49099c0.32733,-0.32733 0.49099,-0.72144 0.49099,-1.18234c0,-0.4595 -0.1632,-0.85149 -0.48959,-1.17596c-0.32685,-0.32492 -0.72143,-0.48738 -1.18374,-0.48738z"></path></g></g><defs><rect id="path_0" x="0" y="0" width="16" height="16" /></defs></svg> diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 4250178c6..ce0108f2f 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -160,12 +160,11 @@ const cn = { BotHello: "有什么可以帮你的吗", Error: "出错了,稍后重试吧", Prompt: { - History: (content: string) => - "这是 ai 和用户的历史聊天总结作为前情提要:" + content, + History: (content: string) => "这是历史聊天总结作为前情提要:" + content, Topic: "使用四到五个字直接返回这句话的简要主题,不要解释、不要标点、不要语气词、不要多余文本,如果没有主题,请直接返回“闲聊”", Summarize: - "简要总结一下你和用户的对话,用作后续的上下文提示 prompt,控制在 200 字以内", + "简要总结一下对话内容,用作后续的上下文提示 prompt,控制在 200 字以内", }, }, Copy: { @@ -173,9 +172,11 @@ const cn = { Failed: "复制失败,请赋予剪切板权限", }, Context: { - Toast: (x: any) => `已设置 ${x} 条前置上下文`, + Toast: (x: any) => `包含 ${x} 条预设提示词`, Edit: "当前对话设置", Add: "新增预设对话", + Clear: "上下文已清除", + Revert: "恢复上下文", }, Plugin: { Name: "插件", diff --git a/app/locales/en.ts b/app/locales/en.ts index 1f136f003..12ad101a3 100644 --- a/app/locales/en.ts +++ b/app/locales/en.ts @@ -163,12 +163,11 @@ const en: RequiredLocaleType = { Error: "Something went wrong, please try again later.", Prompt: { History: (content: string) => - "This is a summary of the chat history between the AI and the user as a recap: " + - content, + "This is a summary of the chat history as a recap: " + content, Topic: "Please generate a four to five word title summarizing our conversation without any lead-in, punctuation, quotation marks, periods, symbols, or additional text. Remove enclosing quotation marks.", Summarize: - "Summarize our discussion briefly in 200 words or less to use as a prompt for future context.", + "Summarize the discussion briefly in 200 words or less to use as a prompt for future context.", }, }, Copy: { @@ -179,6 +178,8 @@ const en: RequiredLocaleType = { Toast: (x: any) => `With ${x} contextual prompts`, Edit: "Contextual and Memory Prompts", Add: "Add a Prompt", + Clear: "Context Cleared", + Revert: "Revert", }, Plugin: { Name: "Plugin", diff --git a/app/store/chat.ts b/app/store/chat.ts index 888ac3a0c..4abba0cf5 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -45,6 +45,7 @@ export interface ChatSession { stat: ChatStat; lastUpdate: number; lastSummarizeIndex: number; + clearContextIndex?: number; mask: Mask; } @@ -341,7 +342,12 @@ export const useChatStore = create<ChatStore>()( getMessagesWithMemory() { const session = get().currentSession(); const modelConfig = session.mask.modelConfig; - const messages = session.messages.filter((msg) => !msg.isError); + + // wont send cleared context messages + const clearedContextMessages = session.messages.slice( + (session.clearContextIndex ?? -1) + 1, + ); + const messages = clearedContextMessages.filter((msg) => !msg.isError); const n = messages.length; const context = session.mask.context.slice(); @@ -362,17 +368,17 @@ export const useChatStore = create<ChatStore>()( n - modelConfig.historyMessageCount, ); const longTermMemoryMessageIndex = session.lastSummarizeIndex; - const oldestIndex = Math.max( + const mostRecentIndex = Math.max( shortTermMemoryMessageIndex, longTermMemoryMessageIndex, ); - const threshold = modelConfig.compressMessageLengthThreshold; + const threshold = modelConfig.compressMessageLengthThreshold * 2; // get recent messages as many as possible const reversedRecentMessages = []; for ( let i = n - 1, count = 0; - i >= oldestIndex && count < threshold; + i >= mostRecentIndex && count < threshold; i -= 1 ) { const msg = messages[i]; @@ -410,15 +416,15 @@ export const useChatStore = create<ChatStore>()( const session = get().currentSession(); // remove error messages if any - const cleanMessages = session.messages.filter((msg) => !msg.isError); + const messages = session.messages; // should summarize topic after chating more than 50 words const SUMMARIZE_MIN_LEN = 50; if ( session.topic === DEFAULT_TOPIC && - countMessages(cleanMessages) >= SUMMARIZE_MIN_LEN + countMessages(messages) >= SUMMARIZE_MIN_LEN ) { - const topicMessages = cleanMessages.concat( + const topicMessages = messages.concat( createMessage({ role: "user", content: Locale.Store.Prompt.Topic, @@ -440,9 +446,13 @@ export const useChatStore = create<ChatStore>()( } const modelConfig = session.mask.modelConfig; - let toBeSummarizedMsgs = cleanMessages.slice( + const summarizeIndex = Math.max( session.lastSummarizeIndex, + session.clearContextIndex ?? 0, ); + let toBeSummarizedMsgs = messages + .filter((msg) => !msg.isError) + .slice(summarizeIndex); const historyMsgLength = countMessages(toBeSummarizedMsgs);