mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
♻️ refactor: 重构 selector 文件组织
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { encode } from 'gpt-tokenizer';
|
||||
|
||||
import { agentSelectors } from '@/store/session';
|
||||
import { ChatMessage } from '@/types/chatMessage';
|
||||
|
||||
import type { SessionStore } from '../../store';
|
||||
import { sessionSelectors } from '../session';
|
||||
import type { SessionStore } from '../../../store';
|
||||
import { sessionSelectors } from '../../session';
|
||||
|
||||
// 展示在聊天框中的消息
|
||||
const currentChats = (s: SessionStore): ChatMessage[] => {
|
||||
export const currentChats = (s: SessionStore): ChatMessage[] => {
|
||||
const session = sessionSelectors.currentSession(s);
|
||||
if (!session) return [];
|
||||
|
||||
@@ -53,30 +51,8 @@ const currentChats = (s: SessionStore): ChatMessage[] => {
|
||||
return finalList;
|
||||
};
|
||||
|
||||
const systemRoleSel = (s: SessionStore): string => {
|
||||
export const systemRoleSel = (s: SessionStore): string => {
|
||||
const config = agentSelectors.currentAgentConfigSafe(s);
|
||||
|
||||
return config.systemRole;
|
||||
};
|
||||
|
||||
const systemRoleTokens = (s: SessionStore): number[] => {
|
||||
const systemRole = systemRoleSel(s);
|
||||
|
||||
return encode(systemRole || '');
|
||||
};
|
||||
|
||||
const chatsTokens = (s: SessionStore): number[] => {
|
||||
const chats = currentChats(s);
|
||||
return encode(chats.map((m) => m.content).join(''));
|
||||
};
|
||||
|
||||
const systemRoleTokenCount = (s: SessionStore) => systemRoleTokens(s).length;
|
||||
|
||||
const totalTokenCount = (s: SessionStore) => chatsTokens(s).length + systemRoleTokenCount(s);
|
||||
|
||||
export const chatSelectors = {
|
||||
currentChats,
|
||||
systemRoleTokenCount,
|
||||
systemRoleTokens,
|
||||
totalTokenCount,
|
||||
};
|
||||
9
src/store/session/slices/chat/selectors/index.ts
Normal file
9
src/store/session/slices/chat/selectors/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { currentChats } from './chat';
|
||||
import { chatsTokenCount, systemRoleTokenCount, totalTokenCount } from './token';
|
||||
|
||||
export const chatSelectors = {
|
||||
chatsTokenCount,
|
||||
currentChats,
|
||||
systemRoleTokenCount,
|
||||
totalTokenCount,
|
||||
};
|
||||
21
src/store/session/slices/chat/selectors/token.ts
Normal file
21
src/store/session/slices/chat/selectors/token.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { encode } from 'gpt-tokenizer';
|
||||
|
||||
import type { SessionStore } from '../../../store';
|
||||
import { currentChats, systemRoleSel } from './chat';
|
||||
|
||||
const systemRoleTokens = (s: SessionStore): number[] => {
|
||||
const systemRole = systemRoleSel(s);
|
||||
|
||||
return encode(systemRole || '');
|
||||
};
|
||||
|
||||
const chatsTokens = (s: SessionStore): number[] => {
|
||||
const chats = currentChats(s);
|
||||
return encode(chats.map((m) => m.content).join(''));
|
||||
};
|
||||
|
||||
export const chatsTokenCount = (s: SessionStore) => chatsTokens(s).length;
|
||||
|
||||
export const systemRoleTokenCount = (s: SessionStore) => systemRoleTokens(s).length;
|
||||
|
||||
export const totalTokenCount = (s: SessionStore) => chatsTokens(s).length + systemRoleTokenCount(s);
|
||||
Reference in New Issue
Block a user