@lobehub/chat 0.82.7 → 0.82.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/next.config.mjs +6 -1
  3. package/package.json +5 -1
  4. package/src/app/chat/{features/Header → (desktop)/features/ChatHeader}/Desktop.tsx +2 -2
  5. package/src/app/chat/{features/Header → (desktop)/features/ChatHeader}/index.tsx +1 -1
  6. package/src/app/chat/(desktop)/features/ChatInput/index.tsx +22 -0
  7. package/src/app/chat/{features/SessionList/Header → (desktop)/features/SessionHeader}/Desktop.tsx +1 -1
  8. package/src/app/chat/{features/SessionList/Header → (desktop)/features/SessionHeader}/index.tsx +1 -1
  9. package/src/app/chat/{features → (desktop)/features}/SessionList/Desktop.tsx +1 -1
  10. package/src/app/chat/(desktop)/features/SessionList/index.tsx +20 -0
  11. package/src/app/chat/{features/Sidebar → (desktop)/features/SideBar}/Desktop.tsx +4 -4
  12. package/src/app/chat/(desktop)/features/SideBar/index.tsx +20 -0
  13. package/src/app/chat/(desktop)/index.tsx +24 -0
  14. package/src/app/chat/{layout.desktop.tsx → (desktop)/layout/Desktop.tsx} +4 -4
  15. package/src/app/chat/(desktop)/layout/index.tsx +25 -0
  16. package/src/app/chat/(mobile)/features/ChatHeader.tsx +1 -0
  17. package/src/app/chat/(mobile)/features/ChatInput.tsx +14 -0
  18. package/src/app/chat/(mobile)/features/SessionList.tsx +12 -0
  19. package/src/app/chat/(mobile)/features/Topics.tsx +12 -0
  20. package/src/app/chat/(mobile)/index.tsx +20 -0
  21. package/src/app/chat/(mobile)/layout.tsx +15 -0
  22. package/src/app/chat/(mobile)/mobile/page.tsx +21 -0
  23. package/src/app/chat/{features/Header → components/ChatHeader}/Mobile.tsx +2 -1
  24. package/src/app/chat/{features/Header → components/ChatHeader}/ShareButton/Preview.tsx +3 -3
  25. package/src/app/chat/{features/Conversation/Input → components/ChatInput}/ActionBar/Token.tsx +10 -7
  26. package/src/app/chat/{features/Conversation/Input → components/ChatInput}/index.tsx +30 -24
  27. package/src/app/chat/components/Inspector/index.tsx +17 -0
  28. package/src/app/chat/{features → components}/SessionList/Mobile.tsx +1 -1
  29. package/src/app/chat/{features → components}/SessionList/index.tsx +3 -10
  30. package/src/app/chat/features/Conversation/index.tsx +8 -4
  31. package/src/app/chat/features/PageTitle.tsx +19 -0
  32. package/src/app/chat/layout.tsx +14 -0
  33. package/src/app/chat/page.tsx +9 -4
  34. package/src/app/chat/settings/(desktop)/Header/index.tsx +13 -0
  35. package/src/app/chat/settings/{layout.desktop.tsx → (desktop)/layout.desktop.tsx} +5 -4
  36. package/src/app/chat/settings/(mobile)/Header.tsx +12 -0
  37. package/src/app/chat/settings/{layout.mobile.tsx → (mobile)/layout.mobile.tsx} +3 -1
  38. package/src/app/chat/settings/{features → components}/Header/index.tsx +4 -13
  39. package/src/app/chat/settings/layout.tsx +14 -0
  40. package/src/app/chat/settings/page.tsx +36 -5
  41. package/src/app/home/Redirect.tsx +29 -0
  42. package/src/app/home/index.tsx +18 -29
  43. package/src/app/market/features/SideBar/AgentInfo/TokenTag.tsx +4 -3
  44. package/src/app/metadata.ts +4 -1
  45. package/src/app/page.tsx +7 -3
  46. package/src/app/welcome/{layout.desktop.tsx → DesktopLayout/Desktop.tsx} +5 -4
  47. package/src/app/welcome/DesktopLayout/index.tsx +15 -0
  48. package/src/app/welcome/{layout.mobile.tsx → MobileLayout.tsx} +6 -3
  49. package/src/app/welcome/features/Banner/index.tsx +2 -0
  50. package/src/app/welcome/features/Footer/index.tsx +2 -0
  51. package/src/app/welcome/layout.tsx +22 -0
  52. package/src/app/welcome/page.tsx +10 -5
  53. package/src/components/PageTitle.tsx +11 -0
  54. package/src/features/AgentSetting/AgentPrompt/index.tsx +3 -6
  55. package/src/hooks/useTokenCount.ts +20 -0
  56. package/src/layout/AppMobileLayout.tsx +15 -24
  57. package/src/prompts/chat.ts +4 -2
  58. package/src/store/session/hooks/useEffectAfterHydrated.ts +11 -6
  59. package/src/store/session/slices/chat/actions/message.ts +1 -1
  60. package/src/store/session/slices/chat/actions/topic.ts +3 -3
  61. package/src/store/session/slices/chat/helpers.ts +3 -4
  62. package/src/store/session/slices/chat/selectors/chat.ts +5 -0
  63. package/src/store/session/slices/chat/selectors/index.ts +9 -5
  64. package/src/store/session/slices/session/action.ts +1 -1
  65. package/src/utils/responsive.ts +19 -0
  66. package/src/utils/tokenizer.ts +5 -0
  67. package/src/app/chat/features/Sidebar/index.tsx +0 -29
  68. package/src/app/chat/index.tsx +0 -54
  69. package/src/app/chat/layout.mobile.tsx +0 -20
  70. package/src/app/chat/mobile/index.tsx +0 -38
  71. package/src/app/chat/mobile/page.tsx +0 -7
  72. package/src/app/chat/settings/index.tsx +0 -51
  73. package/src/app/home/Loading.tsx +0 -23
  74. package/src/app/welcome/index.tsx +0 -39
  75. package/src/store/session/slices/chat/selectors/token.ts +0 -28
  76. /package/src/app/chat/{features/Conversation/Input → (desktop)/features/ChatInput}/Desktop.tsx +0 -0
  77. /package/src/app/chat/{features/SessionList/Header/Mobile.tsx → (mobile)/features/SessionHeader.tsx} +0 -0
  78. /package/src/app/chat/{features/Header → components/ChatHeader}/PluginTag/PluginStatus.tsx +0 -0
  79. /package/src/app/chat/{features/Header → components/ChatHeader}/PluginTag/index.tsx +0 -0
  80. /package/src/app/chat/{features/Header → components/ChatHeader}/ShareButton/Inner.tsx +0 -0
  81. /package/src/app/chat/{features/Header → components/ChatHeader}/ShareButton/index.tsx +0 -0
  82. /package/src/app/chat/{features/Header → components/ChatHeader}/ShareButton/style.ts +0 -0
  83. /package/src/app/chat/{features/Conversation/Input → components/ChatInput}/ActionBar/ActionLeft.tsx +0 -0
  84. /package/src/app/chat/{features/Conversation/Input → components/ChatInput}/ActionBar/ActionRight.tsx +0 -0
  85. /package/src/app/chat/{features/Conversation/Input → components/ChatInput}/Footer/SaveTopic.tsx +0 -0
  86. /package/src/app/chat/{features/Conversation/Input → components/ChatInput}/Footer/index.tsx +0 -0
  87. /package/src/app/chat/{features/Conversation/Input → components/ChatInput}/Mobile.tsx +0 -0
  88. /package/src/app/chat/{features/Sidebar → components/Inspector}/Mobile.tsx +0 -0
  89. /package/src/app/chat/{features/Sidebar → components/Inspector}/SystemRole/Header.tsx +0 -0
  90. /package/src/app/chat/{features/Sidebar → components/Inspector}/SystemRole/index.tsx +0 -0
  91. /package/src/app/chat/{features/Sidebar → components/Inspector}/SystemRole/style.ts +0 -0
  92. /package/src/app/chat/{features/Sidebar → components/Inspector}/Topic/DefaultContent.tsx +0 -0
  93. /package/src/app/chat/{features/Sidebar → components/Inspector}/Topic/SkeletonList.tsx +0 -0
  94. /package/src/app/chat/{features/Sidebar → components/Inspector}/Topic/TopicContent.tsx +0 -0
  95. /package/src/app/chat/{features/Sidebar → components/Inspector}/Topic/TopicItem.tsx +0 -0
  96. /package/src/app/chat/{features/Sidebar → components/Inspector}/Topic/index.tsx +0 -0
  97. /package/src/app/chat/{features/Sidebar → components/Inspector}/TopicSearchBar/index.tsx +0 -0
  98. /package/src/app/chat/{features → components}/SessionList/CollapseGroup/index.tsx +0 -0
  99. /package/src/app/chat/{features → components}/SessionList/Inbox/index.tsx +0 -0
  100. /package/src/app/chat/{features → components}/SessionList/List/AddButton.tsx +0 -0
  101. /package/src/app/chat/{features → components}/SessionList/List/Item/Actions.tsx +0 -0
  102. /package/src/app/chat/{features → components}/SessionList/List/Item/index.tsx +0 -0
  103. /package/src/app/chat/{features → components}/SessionList/List/SkeletonList.tsx +0 -0
  104. /package/src/app/chat/{features → components}/SessionList/List/index.tsx +0 -0
  105. /package/src/app/chat/settings/{features → (desktop)}/Header/Desktop.tsx +0 -0
  106. /package/src/app/chat/settings/{features → components}/Header/Mobile.tsx +0 -0
@@ -1,21 +1,26 @@
1
1
  import { useEffect } from 'react';
2
2
 
3
- import { useSessionStore } from '../store';
3
+ import { SessionStore, useSessionStore } from '../store';
4
4
 
5
5
  export const useEffectAfterSessionHydrated = (
6
- fn: (session: typeof useSessionStore) => void,
6
+ fn: (session: typeof useSessionStore, store: SessionStore) => void,
7
7
  deps: any[] = [],
8
8
  ) => {
9
+ // const hasTrigger = useRef(false);
9
10
  useEffect(() => {
10
11
  const hasRehydrated = useSessionStore.persist.hasHydrated();
11
12
 
12
13
  if (hasRehydrated) {
13
- // 等价 useEffect 多次触发
14
- fn(useSessionStore);
14
+ // equal useEffect triggered multi-time
15
+ fn(useSessionStore, useSessionStore.getState());
15
16
  } else {
16
- // 等价于 useEffect 第一次触发
17
+ // keep onFinishHydration just are triggered only once
18
+ // if (hasTrigger.current) return;
19
+ //
20
+ // hasTrigger.current = true;
21
+ // equal useEffect first trigger
17
22
  useSessionStore.persist.onFinishHydration(() => {
18
- fn(useSessionStore);
23
+ fn(useSessionStore, useSessionStore.getState());
19
24
  });
20
25
  }
21
26
  }, deps);
@@ -285,7 +285,7 @@ export const chatMessage: StateCreator<
285
285
 
286
286
  if (!activeTopicId && chats.length >= 2) {
287
287
  const { saveToTopic, toggleTopic } = get();
288
- const id = saveToTopic();
288
+ const id = await saveToTopic();
289
289
  if (id) toggleTopic(id);
290
290
  }
291
291
  },
@@ -26,7 +26,7 @@ export interface ChatTopicAction {
26
26
  /**
27
27
  * 将当前消息保存为主题
28
28
  */
29
- saveToTopic: () => string | undefined;
29
+ saveToTopic: () => Promise<string | undefined>;
30
30
  /**
31
31
  * 切换主题
32
32
  * @param id - 要切换的主题的 ID
@@ -72,7 +72,7 @@ export const chatTopic: StateCreator<
72
72
  // 切换到默认 topic
73
73
  toggleTopic();
74
74
  },
75
- saveToTopic: () => {
75
+ saveToTopic: async () => {
76
76
  const session = sessionSelectors.currentSession(get());
77
77
  if (!session) return;
78
78
 
@@ -116,7 +116,7 @@ export const chatTopic: StateCreator<
116
116
  output += x;
117
117
  dispatchTopic({ id: topicId, key: 'title', type: 'updateChatTopic', value: output });
118
118
  },
119
- params: promptSummaryTitle(messages),
119
+ params: await promptSummaryTitle(messages),
120
120
  });
121
121
 
122
122
  return topicId;
@@ -1,9 +1,8 @@
1
- import { encode } from 'gpt-tokenizer';
2
-
3
1
  import { OpenAIChatMessage } from '@/types/openai';
2
+ import { encodeAsync } from '@/utils/tokenizer';
4
3
 
5
- export const getMessagesTokenCount = (messages: OpenAIChatMessage[]) =>
6
- encode(messages.map((m) => m.content).join('')).length;
4
+ export const getMessagesTokenCount = async (messages: OpenAIChatMessage[]) =>
5
+ encodeAsync(messages.map((m) => m.content).join(''));
7
6
 
8
7
  export const chatHelpers = {
9
8
  getMessagesTokenCount,
@@ -79,3 +79,8 @@ export const currentChatsWithHistoryConfig = (s: SessionStore): ChatMessage[] =>
79
79
 
80
80
  return getSlicedMessagesWithConfig(chats, config);
81
81
  };
82
+
83
+ export const chatsMessageString = (s: SessionStore): string => {
84
+ const chats = currentChatsWithHistoryConfig(s);
85
+ return chats.map((m) => m.content).join('');
86
+ };
@@ -1,14 +1,18 @@
1
- import { currentChats, currentChatsWithGuideMessage, getChatsById } from './chat';
2
- import { chatsTokenCount, systemRoleTokenCount, totalTokenCount } from './token';
1
+ import {
2
+ chatsMessageString,
3
+ currentChats,
4
+ currentChatsWithGuideMessage,
5
+ currentChatsWithHistoryConfig,
6
+ getChatsById,
7
+ } from './chat';
3
8
  import { currentTopics, getTopicMessages } from './topic';
4
9
 
5
10
  export const chatSelectors = {
6
- chatsTokenCount,
11
+ chatsMessageString,
7
12
  currentChats,
8
13
  currentChatsWithGuideMessage,
14
+ currentChatsWithHistoryConfig,
9
15
  getChatsById,
10
- systemRoleTokenCount,
11
- totalTokenCount,
12
16
  };
13
17
 
14
18
  export const topicSelectors = {
@@ -95,7 +95,7 @@ export const createSessionSlice: StateCreator<
95
95
 
96
96
  dispatchSession({ session: newSession, type: 'addSession' });
97
97
 
98
- await switchSession(newSession.id);
98
+ switchSession(newSession.id);
99
99
 
100
100
  return newSession.id;
101
101
  },
@@ -0,0 +1,19 @@
1
+ import { headers } from 'next/headers';
2
+ import { UAParser } from 'ua-parser-js';
3
+
4
+ /**
5
+ * check mobile device in server
6
+ */
7
+ export const isMobileDevice = () => {
8
+ if (typeof process === 'undefined') {
9
+ throw new Error('[Server method] you are importing a server-only module outside of server');
10
+ }
11
+
12
+ const { get } = headers();
13
+ const ua = get('user-agent');
14
+
15
+ console.log(ua);
16
+ const device = new UAParser(ua || '').getDevice();
17
+
18
+ return device.type === 'mobile';
19
+ };
@@ -0,0 +1,5 @@
1
+ export const encodeAsync = async (str: string) => {
2
+ const { encode } = await import('gpt-tokenizer');
3
+
4
+ return encode(str).length;
5
+ };
@@ -1,29 +0,0 @@
1
- import { useResponsive } from 'antd-style';
2
- import { memo } from 'react';
3
- import { Flexbox } from 'react-layout-kit';
4
-
5
- import Desktop from './Desktop';
6
- import Mobile from './Mobile';
7
- import { Topic } from './Topic';
8
- import TopicSearchBar from './TopicSearchBar';
9
-
10
- const SideBar = memo(() => {
11
- const { mobile } = useResponsive();
12
-
13
- const Render = mobile ? Mobile : Desktop;
14
-
15
- return (
16
- <Render>
17
- <Flexbox height={'100%'} style={{ overflow: 'hidden' }}>
18
- <Flexbox padding={16}>
19
- <TopicSearchBar />
20
- </Flexbox>
21
- <Flexbox gap={16} paddingInline={16} style={{ overflowY: 'auto', position: 'relative' }}>
22
- <Topic />
23
- </Flexbox>
24
- </Flexbox>
25
- </Render>
26
- );
27
- });
28
-
29
- export default SideBar;
@@ -1,54 +0,0 @@
1
- 'use client';
2
-
3
- import { useResponsive } from 'antd-style';
4
- import Head from 'next/head';
5
- import { memo } from 'react';
6
- import { Flexbox } from 'react-layout-kit';
7
-
8
- import { useSessionStore } from '@/store/session';
9
- import { useEffectAfterSessionHydrated } from '@/store/session/hooks';
10
- import { agentSelectors } from '@/store/session/selectors';
11
- import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';
12
-
13
- import Conversation from './features/Conversation';
14
- import Header from './features/Header';
15
- import SideBar from './features/Sidebar';
16
- import DesktopLayout from './layout.desktop';
17
- import MobileLayout from './layout.mobile';
18
-
19
- const Chat = memo(() => {
20
- const { mobile } = useResponsive();
21
- const [avatar, title] = useSessionStore((s) => [
22
- agentSelectors.currentAgentAvatar(s),
23
- agentSelectors.currentAgentTitle(s),
24
- ]);
25
- const pageTitle = genSiteHeadTitle([avatar, title].filter(Boolean).join(' '));
26
- const RenderLayout = mobile ? MobileLayout : DesktopLayout;
27
-
28
- useEffectAfterSessionHydrated(
29
- (store) => {
30
- store.setState({ isMobile: mobile });
31
- },
32
- [mobile],
33
- );
34
-
35
- return (
36
- <>
37
- <Head>
38
- <title>{pageTitle}</title>
39
- </Head>
40
- <RenderLayout>
41
- {!mobile && (
42
- <>
43
- <Header />
44
- <Flexbox flex={1} height={'calc(100vh - 64px)'} horizontal>
45
- <Conversation />
46
- <SideBar />
47
- </Flexbox>
48
- </>
49
- )}
50
- </RenderLayout>
51
- </>
52
- );
53
- });
54
- export default Chat;
@@ -1,20 +0,0 @@
1
- import { memo } from 'react';
2
-
3
- import AppMobileLayout from '@/layout/AppMobileLayout';
4
- import { useSwitchSideBarOnInit } from '@/store/global';
5
- import { SidebarTabKey } from '@/store/global/initialState';
6
-
7
- import { Sessions } from './features/SessionList';
8
- import Header from './features/SessionList/Header';
9
-
10
- const ChatMobileLayout = memo(() => {
11
- useSwitchSideBarOnInit(SidebarTabKey.Chat);
12
-
13
- return (
14
- <AppMobileLayout navBar={<Header />} showTabBar>
15
- <Sessions />
16
- </AppMobileLayout>
17
- );
18
- });
19
-
20
- export default ChatMobileLayout;
@@ -1,38 +0,0 @@
1
- 'use client';
2
-
3
- import Head from 'next/head';
4
- import { memo } from 'react';
5
- import { Flexbox } from 'react-layout-kit';
6
-
7
- import AppMobileLayout from '@/layout/AppMobileLayout';
8
- import { useSessionStore } from '@/store/session';
9
- import { agentSelectors } from '@/store/session/selectors';
10
- import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';
11
-
12
- import Conversation from '../features/Conversation';
13
- import Header from '../features/Header';
14
- import SideBar from '../features/Sidebar';
15
-
16
- const Chat = memo(() => {
17
- const [avatar, title] = useSessionStore((s) => [
18
- agentSelectors.currentAgentAvatar(s),
19
- agentSelectors.currentAgentTitle(s),
20
- ]);
21
-
22
- const pageTitle = genSiteHeadTitle([avatar, title].filter(Boolean).join(' '));
23
-
24
- return (
25
- <>
26
- <Head>
27
- <title>{pageTitle}</title>
28
- </Head>
29
- <AppMobileLayout navBar={<Header />}>
30
- <Flexbox height={'calc(100vh - 44px)'} horizontal>
31
- <Conversation mobile />
32
- <SideBar />
33
- </Flexbox>
34
- </AppMobileLayout>
35
- </>
36
- );
37
- });
38
- export default Chat;
@@ -1,7 +0,0 @@
1
- import Page from './index';
2
-
3
- const Index = () => {
4
- return <Page />;
5
- };
6
-
7
- export default Index;
@@ -1,51 +0,0 @@
1
- 'use client';
2
-
3
- import { useResponsive } from 'antd-style';
4
- import isEqual from 'fast-deep-equal';
5
- import Head from 'next/head';
6
- import { memo } from 'react';
7
- import { useTranslation } from 'react-i18next';
8
-
9
- import AgentSetting from '@/features/AgentSetting';
10
- import { useSessionStore } from '@/store/session';
11
- import { agentSelectors } from '@/store/session/selectors';
12
- import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';
13
-
14
- import DesktopLayout from './layout.desktop';
15
- import MobileLayout from './layout.mobile';
16
-
17
- const EditPage = memo(() => {
18
- const { mobile } = useResponsive();
19
- const { t } = useTranslation('setting');
20
- const config = useSessionStore(agentSelectors.currentAgentConfig, isEqual);
21
- const meta = useSessionStore(agentSelectors.currentAgentMeta, isEqual);
22
- const [updateAgentConfig, updateAgentMeta, title] = useSessionStore((s) => [
23
- s.updateAgentConfig,
24
- s.updateAgentMeta,
25
- agentSelectors.currentAgentTitle(s),
26
- ]);
27
-
28
- const pageTitle = genSiteHeadTitle(t('header.sessionWithName', { name: title }));
29
-
30
- const settings = (
31
- <AgentSetting
32
- config={config}
33
- meta={meta}
34
- onConfigChange={updateAgentConfig}
35
- onMetaChange={updateAgentMeta}
36
- />
37
- );
38
-
39
- const RenderLayout = mobile ? MobileLayout : DesktopLayout;
40
-
41
- return (
42
- <>
43
- <Head>
44
- <title>{pageTitle}</title>
45
- </Head>
46
- <RenderLayout>{settings}</RenderLayout>
47
- </>
48
- );
49
- });
50
-
51
- export default EditPage;
@@ -1,23 +0,0 @@
1
- import { Icon, Logo } from '@lobehub/ui';
2
- import { Loader2 } from 'lucide-react';
3
- import { memo } from 'react';
4
- import { useTranslation } from 'react-i18next';
5
- import { Center, Flexbox } from 'react-layout-kit';
6
-
7
- const Loading = memo(() => {
8
- const { t } = useTranslation('common');
9
-
10
- return (
11
- <Flexbox height={'100vh'} width={'100%'}>
12
- <Center flex={1} gap={12} width={'100%'}>
13
- <Logo extra={'Chat'} size={48} type={'combine'} />
14
- <Center gap={16} horizontal>
15
- <Icon icon={Loader2} spin />
16
- {t('appInitializing')}
17
- </Center>
18
- </Center>
19
- </Flexbox>
20
- );
21
- });
22
-
23
- export default Loading;
@@ -1,39 +0,0 @@
1
- 'use client';
2
-
3
- import { useResponsive } from 'antd-style';
4
- import Head from 'next/head';
5
- import { memo } from 'react';
6
- import { useTranslation } from 'react-i18next';
7
-
8
- import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';
9
-
10
- import Banner from './features/Banner';
11
- import Footer from './features/Footer';
12
- import Layout from './layout.desktop';
13
- import Mobile from './layout.mobile';
14
-
15
- const Welcome = memo(() => {
16
- const { mobile } = useResponsive();
17
- const { t } = useTranslation('welcome');
18
- const pageTitle = genSiteHeadTitle(t('header'));
19
-
20
- return (
21
- <>
22
- <Head>
23
- <title>{pageTitle}</title>
24
- </Head>
25
- {mobile ? (
26
- <Mobile>
27
- <Banner />
28
- </Mobile>
29
- ) : (
30
- <Layout>
31
- <Banner />
32
- <Footer />
33
- </Layout>
34
- )}
35
- </>
36
- );
37
- });
38
-
39
- export default Welcome;
@@ -1,28 +0,0 @@
1
- import { encode } from 'gpt-tokenizer';
2
-
3
- import { agentSelectors } from '@/store/session/slices/agentConfig';
4
-
5
- import type { SessionStore } from '../../../store';
6
- import { currentChatsWithHistoryConfig } from './chat';
7
-
8
- // TODO: We should find a smaller package to do this. gpt-tokenizer has 2M Size
9
- const count = (text: string) => encode(text).length;
10
-
11
- export const systemRoleSel = (s: SessionStore): string => {
12
- const config = agentSelectors.currentAgentConfig(s);
13
-
14
- return config.systemRole;
15
- };
16
-
17
- export const systemRoleTokenCount = (s: SessionStore): number => {
18
- const systemRole = systemRoleSel(s);
19
-
20
- return count(systemRole || '');
21
- };
22
-
23
- export const chatsTokenCount = (s: SessionStore): number => {
24
- const chats = currentChatsWithHistoryConfig(s);
25
- return count(chats.map((m) => m.content).join(''));
26
- };
27
-
28
- export const totalTokenCount = (s: SessionStore) => chatsTokenCount(s) + systemRoleTokenCount(s);