🗃️ refactor: migrate some agent config to chatConfig (#2646)

* 🗃️ refactor: refactor the agent config to chat config

*  test: fix test

* 🗃️ refactor: add db migration

*  test: fix test
This commit is contained in:
Arvin Xu
2024-05-25 10:09:28 +08:00
committed by GitHub
parent 25e6007c92
commit 2f311dc4b6
53 changed files with 1066 additions and 213 deletions

View File

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

@@ -75,7 +75,7 @@ const SessionItem = memo<SessionItemProps>(({ id }) => {
addon={addon}
avatar={avatar}
avatarBackground={avatarBackground}
date={updateAt}
date={updateAt?.valueOf()}
description={description}
loading={loading}
pin={pin}

View File

@@ -19,6 +19,9 @@ const Layout = ({ children, session }: LayoutProps) => {
</Flexbox>
</Flexbox>
<Migration />
{/* ↓ cloud slot ↓ */}
{/* ↑ cloud slot ↑ */}
</>
);
};

View File

@@ -16,7 +16,10 @@ const EditPage = memo(() => {
const id = useSessionStore((s) => s.activeId);
const config = useAgentStore(agentSelectors.currentAgentConfig, isEqual);
const meta = useSessionStore(sessionMetaSelectors.currentAgentMeta, isEqual);
const [updateAgentConfig] = useAgentStore((s) => [s.updateAgentConfig]);
const [updateAgentConfig, updateAgentChatConfig] = useAgentStore((s) => [
s.updateAgentConfig,
s.updateAgentChatConfig,
]);
const [updateAgentMeta, title] = useSessionStore((s) => [
s.updateSessionMeta,
@@ -30,6 +33,7 @@ const EditPage = memo(() => {
config={config}
id={id}
meta={meta}
onChatConfigChange={updateAgentChatConfig}
onConfigChange={updateAgentConfig}
onMetaChange={updateAgentMeta}
/>

View File

@@ -8,23 +8,28 @@ const COPYRIGHT = `© ${new Date().getFullYear()} LobeHub, LLC`;
const DesktopLayout = ({ children }: PropsWithChildren) => {
return (
<Flexbox
align={'center'}
height={'100%'}
justify={'space-between'}
padding={16}
style={{ overflow: 'hidden', position: 'relative' }}
width={'100%'}
>
<Logo size={36} style={{ alignSelf: 'flex-start' }} type={'text'} />
<GridShowcase innerProps={{ gap: 24 }} style={{ maxWidth: 1024 }} width={'100%'}>
{children}
</GridShowcase>
<Flexbox align={'center'} horizontal justify={'space-between'}>
<span style={{ opacity: 0.5 }}>{COPYRIGHT}</span>
<Follow />
<>
<Flexbox
align={'center'}
height={'100%'}
justify={'space-between'}
padding={16}
style={{ overflow: 'hidden', position: 'relative' }}
width={'100%'}
>
<Logo size={36} style={{ alignSelf: 'flex-start' }} type={'text'} />
<GridShowcase innerProps={{ gap: 24 }} style={{ maxWidth: 1024 }} width={'100%'}>
{children}
</GridShowcase>
<Flexbox align={'center'} horizontal justify={'space-between'}>
<span style={{ opacity: 0.5 }}>{COPYRIGHT}</span>
<Follow />
</Flexbox>
</Flexbox>
</Flexbox>
{/* ↓ cloud slot ↓ */}
{/* ↑ cloud slot ↑ */}
</>
);
};

View File

@@ -28,8 +28,10 @@ const Layout = memo<PropsWithChildren>(({ children }) => {
const id = useSessionStore((s) => s.activeId);
const config = useAgentStore(agentSelectors.currentAgentConfig, isEqual);
const meta = useSessionStore(sessionMetaSelectors.currentAgentMeta, isEqual);
const [updateAgentConfig] = useAgentStore((s) => [s.updateAgentConfig]);
const [updateAgentConfig, updateAgentChatConfig] = useAgentStore((s) => [
s.updateAgentConfig,
s.updateAgentChatConfig,
]);
const [updateAgentMeta] = useSessionStore((s) => [
s.updateSessionMeta,
sessionMetaSelectors.currentAgentTitle(s),
@@ -47,6 +49,7 @@ const Layout = memo<PropsWithChildren>(({ children }) => {
config={config}
id={id}
meta={meta}
onChatConfigChange={updateAgentChatConfig}
onConfigChange={updateAgentConfig}
onMetaChange={updateAgentMeta}
/>

View File

@@ -21,7 +21,7 @@ export const generateMetadata = async (): Promise<Metadata> => {
},
description: t('chat.description'),
icons: {
apple: '/icons/apple-touch-icon.png',
apple: '/apple-touch-icon.png',
icon: '/favicon.ico',
shortcut: '/favicon-32x32.ico',
},

View File

@@ -9,12 +9,12 @@ export const WELCOME_GUIDE_CHAT_ID = 'welcome';
export const DEFAULT_AGENT_LOBE_SESSION: LobeAgentSession = {
config: DEFAULT_AGENT_CONFIG,
createdAt: Date.now(),
createdAt: new Date(),
id: '',
meta: DEFAULT_AGENT_META,
model: DEFAULT_AGENT_CONFIG.model,
type: LobeSessionType.Agent,
updatedAt: Date.now(),
updatedAt: new Date(),
};
export const DEFAULT_INBOX_SESSION: LobeAgentSession = merge(DEFAULT_AGENT_LOBE_SESSION, {

View File

@@ -1,6 +1,6 @@
import { DEFAULT_AGENT_META } from '@/const/meta';
import { ModelProvider } from '@/libs/agent-runtime';
import { LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent';
import { GlobalDefaultAgent } from '@/types/settings';
export const DEFAUTT_AGENT_TTS_CONFIG: LobeAgentTTSConfig = {
@@ -12,11 +12,15 @@ export const DEFAUTT_AGENT_TTS_CONFIG: LobeAgentTTSConfig = {
},
};
export const DEFAULT_AGENT_CONFIG: LobeAgentConfig = {
export const DEFAULT_AGENT_CHAT_CONFIG: LobeAgentChatConfig = {
autoCreateTopicThreshold: 2,
displayMode: 'chat',
enableAutoCreateTopic: true,
historyCount: 1,
};
export const DEFAULT_AGENT_CONFIG: LobeAgentConfig = {
chatConfig: DEFAULT_AGENT_CHAT_CONFIG,
model: 'gpt-3.5-turbo',
params: {
frequency_penalty: 0,

View File

@@ -1,6 +1,7 @@
import Dexie, { Transaction } from 'dexie';
import { MigrationLLMSettings } from '@/migrations/FromV3ToV4';
import { MigrationAgentChatConfig } from '@/migrations/FromV5ToV6';
import { uuid } from '@/utils/uuid';
import { DB_File } from '../schemas/files';
@@ -72,6 +73,10 @@ export class BrowserDB extends Dexie {
.stores(dbSchemaV9)
.upgrade((trans) => this.upgradeToV9(trans));
this.version(10)
.stores(dbSchemaV9)
.upgrade((trans) => this.upgradeToV10(trans));
this.files = this.table('files');
this.sessions = this.table('sessions');
this.messages = this.table('messages');
@@ -159,6 +164,11 @@ export class BrowserDB extends Dexie {
});
};
/**
* 2024.05.11
*
* message role=function to role=tool
*/
upgradeToV9 = async (trans: Transaction) => {
const messages = trans.table('messages');
await messages.toCollection().modify(async (message: DBModel<DB_Message>) => {
@@ -185,6 +195,18 @@ export class BrowserDB extends Dexie {
}
});
};
/**
* 2024.05.25
* migrate some agent config to chatConfig
*/
upgradeToV10 = async (trans: Transaction) => {
const sessions = trans.table('sessions');
await sessions.toCollection().modify(async (session: DBModel<DB_Session>) => {
if (session.config)
session.config = MigrationAgentChatConfig.migrateChatConfig(session.config as any);
});
};
}
export const browserDB = new BrowserDB();

View File

@@ -1,11 +1,9 @@
import { DB_Settings } from '@/database/client/schemas/user';
import { V4Settings } from './type';
import { V4Settings, V5Settings } from './type';
export const migrateSettingsToUser = (
settings: V4Settings,
): { avatar: string; settings: DB_Settings } => {
const dbSettings: DB_Settings = {
): { avatar: string; settings: V5Settings } => {
const dbSettings: V5Settings = {
defaultAgent: settings.defaultAgent,
fontSize: settings.fontSize,
language: settings.language,

View File

@@ -58,7 +58,7 @@ interface V4DefaultAgent {
meta: MetaData;
}
interface V4OpenAIConfig {
interface OpenAIConfig {
OPENAI_API_KEY: string;
azureApiVersion?: string;
customModelName?: string;
@@ -68,15 +68,10 @@ interface V4OpenAIConfig {
}
interface V4LLMConfig {
openAI: V4OpenAIConfig;
openAI: OpenAIConfig;
}
interface V4Tool {
dalle: {
autoGenerate: boolean;
};
}
interface V4TTSConfig {
interface TTSConfig {
openAI: {
sttModel: 'whisper-1';
ttsModel: 'tts-1' | 'tts-1-hd';
@@ -95,6 +90,19 @@ export interface V4Settings {
password: string;
primaryColor?: string;
themeMode: ThemeMode;
tool: V4Tool;
tts: V4TTSConfig;
tts: TTSConfig;
}
export interface V5Settings {
defaultAgent: V4DefaultAgent;
fontSize: number;
language: string;
languageModel: {
openai: OpenAIConfig;
};
neutralColor?: string;
password: string;
primaryColor?: string;
themeMode: ThemeMode;
tts: TTSConfig;
}

View File

@@ -246,16 +246,20 @@ class _SessionModel extends BaseModel {
private mapToDB_Session(session: LobeAgentSession): DBModel<DB_Session> {
return {
...session,
createdAt: session.createdAt?.valueOf(),
group: session.group || SessionDefaultGroup.Default,
pinned: session.pinned ? 1 : 0,
updatedAt: session.updatedAt?.valueOf(),
};
}
private DB_SessionToAgentSession(session: DBModel<DB_Session>) {
return {
...session,
createdAt: new Date(session.createdAt),
model: session.config.model,
pinned: !!session.pinned,
updatedAt: new Date(session.updatedAt),
} as LobeAgentSession;
}

View File

@@ -1,5 +1,6 @@
import { z } from 'zod';
import { AgentChatConfigSchema } from '@/types/agent';
import { LobeMetaDataSchema } from '@/types/meta';
const fewShotsSchema = z.array(
@@ -23,16 +24,8 @@ const ttsSchema = z.object({
});
export const AgentSchema = z.object({
autoCreateTopicThreshold: z.number().default(2),
compressThreshold: z.number().optional(),
displayMode: z.enum(['chat', 'docs']).optional(),
enableAutoCreateTopic: z.boolean().default(true),
enableCompressThreshold: z.boolean().optional(),
enableHistoryCount: z.boolean().optional(),
enableMaxTokens: z.boolean().optional(),
chatConfig: AgentChatConfigSchema,
fewShots: fewShotsSchema.optional(),
historyCount: z.number().default(8).optional(),
inputTemplate: z.string().optional(),
model: z.string().default('gpt-3.5-turbo'),
params: z.object({
frequency_penalty: z.number().default(0).optional(),

View File

@@ -3,15 +3,16 @@
import { Form, ItemGroup, SelectWithImg, SliderWithInput } from '@lobehub/ui';
import { Input, Switch } from 'antd';
import { useThemeMode } from 'antd-style';
import isEqual from 'fast-deep-equal';
import { LayoutList, MessagesSquare } from 'lucide-react';
import { memo } from 'react';
import { memo, useLayoutEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { FORM_STYLE } from '@/const/layoutTokens';
import { imageUrl } from '@/const/url';
import { useStore } from '../store';
import { useAgentSyncSettings } from '../useSyncAgemtSettings';
import { selectors } from '../store/selectors';
const AgentChat = memo(() => {
const { t } = useTranslation('setting');
@@ -23,15 +24,23 @@ const AgentChat = memo(() => {
enableHistoryCount,
enableCompressThreshold,
updateConfig,
] = useStore((s) => [
s.config.displayMode,
s.config.enableAutoCreateTopic,
s.config.enableHistoryCount,
s.config.enableCompressThreshold,
s.setAgentConfig,
]);
] = useStore((s) => {
const config = selectors.chatConfig(s);
useAgentSyncSettings(form);
return [
config.displayMode,
config.enableAutoCreateTopic,
config.enableHistoryCount,
config.enableCompressThreshold,
s.setChatConfig,
];
});
const config = useStore(selectors.chatConfig, isEqual);
useLayoutEffect(() => {
form.setFieldsValue(config);
}, [config]);
const chat: ItemGroup = {
children: [

View File

@@ -9,6 +9,7 @@ import { useTranslation } from 'react-i18next';
import { FORM_STYLE } from '@/const/layoutTokens';
import { useStore } from '../store';
import { selectors } from '../store/selectors';
import { useAgentSyncSettings } from '../useSyncAgemtSettings';
import ModelSelect from './ModelSelect';
@@ -17,7 +18,7 @@ const AgentModal = memo(() => {
const [form] = Form.useForm();
const [enableMaxTokens, updateConfig] = useStore((s) => [
s.config.enableMaxTokens,
selectors.chatConfig(s).enableMaxTokens,
s.setAgentConfig,
]);

View File

@@ -6,11 +6,11 @@ import { createStoreUpdater } from 'zustand-utils';
import { State, useStoreApi } from './store';
export type StoreUpdaterProps = Partial<
Pick<State, 'onMetaChange' | 'onConfigChange' | 'meta' | 'config' | 'id'>
Pick<State, 'onMetaChange' | 'onChatConfigChange' | 'onConfigChange' | 'meta' | 'config' | 'id'>
>;
const StoreUpdater = memo<StoreUpdaterProps>(
({ onConfigChange, id, onMetaChange, meta, config }) => {
({ onConfigChange, onChatConfigChange, id, onMetaChange, meta, config }) => {
const storeApi = useStoreApi();
const useStoreUpdater = createStoreUpdater(storeApi);
@@ -18,6 +18,7 @@ const StoreUpdater = memo<StoreUpdaterProps>(
useStoreUpdater('config', config);
useStoreUpdater('onConfigChange', onConfigChange);
useStoreUpdater('onMetaChange', onMetaChange);
useStoreUpdater('onChatConfigChange', onChatConfigChange);
useStoreUpdater('id', id);
return null;

View File

@@ -6,7 +6,7 @@ import { chainSummaryDescription } from '@/chains/summaryDescription';
import { chainSummaryTags } from '@/chains/summaryTags';
import { TraceNameMap, TracePayload, TraceTopicType } from '@/const/trace';
import { chatService } from '@/services/chat';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { MetaData } from '@/types/meta';
import { MessageTextChunk } from '@/utils/fetch';
import { setNamespace } from '@/utils/storeDebug';
@@ -45,15 +45,15 @@ export interface Action {
autocompleteMeta: (key: keyof MetaData) => void;
dispatchConfig: (payload: ConfigDispatch) => void;
dispatchMeta: (payload: MetaDataDispatch) => void;
getCurrentTracePayload: (data: Partial<TracePayload>) => TracePayload;
resetAgentConfig: () => void;
resetAgentConfig: () => void;
resetAgentMeta: () => void;
setAgentConfig: (config: Partial<LobeAgentConfig>) => void;
setAgentMeta: (meta: Partial<MetaData>) => void;
setChatConfig: (config: Partial<LobeAgentChatConfig>) => void;
streamUpdateMetaArray: (key: keyof MetaData) => any;
streamUpdateMetaString: (key: keyof MetaData) => any;
toggleAgentPlugin: (pluginId: string, state?: boolean) => void;
@@ -230,10 +230,10 @@ export const store: StateCreator<Store, [['zustand/devtools', never]]> = (set, g
topicId: TraceTopicType.AgentSettings,
...data,
}),
resetAgentConfig: () => {
get().dispatchConfig({ type: 'reset' });
},
resetAgentMeta: () => {
get().dispatchMeta({ type: 'reset' });
},
@@ -244,6 +244,13 @@ export const store: StateCreator<Store, [['zustand/devtools', never]]> = (set, g
setAgentMeta: (meta) => {
get().dispatchMeta({ type: 'update', value: meta });
},
setChatConfig: (config) => {
const nextConfig = { ...get().config.chatConfig, ...config };
set({ config: { ...get().config, chatConfig: nextConfig } }, false, 'updateChatConfig');
get().onChatConfigChange?.(nextConfig);
},
streamUpdateMetaArray: (key: keyof MetaData) => {
let value = '';

View File

@@ -1,6 +1,6 @@
import { DEFAULT_AGENT_META } from '@/const/meta';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { MetaData } from '@/types/meta';
export interface State {
@@ -9,6 +9,7 @@ export interface State {
id?: string;
meta: MetaData;
onChatConfigChange?: (config: LobeAgentChatConfig) => void;
onConfigChange?: (config: LobeAgentConfig) => void;
onMetaChange?: (meta: MetaData) => void;
}

View File

@@ -0,0 +1,11 @@
import { DEFAULT_AGENT_CHAT_CONFIG } from '@/const/settings';
import { LobeAgentChatConfig } from '@/types/agent';
import { Store } from './action';
const chatConfig = (s: Store): LobeAgentChatConfig =>
s.config.chatConfig || DEFAULT_AGENT_CHAT_CONFIG;
export const selectors = {
chatConfig,
};

View File

@@ -12,8 +12,8 @@ const History = memo(() => {
const { t } = useTranslation('setting');
const [historyCount, unlimited, updateAgentConfig] = useAgentStore((s) => {
const config = agentSelectors.currentAgentConfig(s);
return [config.historyCount, !config.enableHistoryCount, s.updateAgentConfig];
const config = agentSelectors.currentAgentChatConfig(s);
return [config.historyCount, !config.enableHistoryCount, s.updateAgentChatConfig];
});
return (

View File

@@ -42,7 +42,7 @@ const Item = memo<ChatListItemProps>(({ index, id }) => {
const { t } = useTranslation('common');
const { styles, cx } = useStyles();
const [type = 'chat'] = useAgentStore((s) => {
const config = agentSelectors.currentAgentConfig(s);
const config = agentSelectors.currentAgentChatConfig(s);
return [config.displayMode];
});
@@ -103,7 +103,7 @@ const Item = memo<ChatListItemProps>(({ index, id }) => {
}, [item?.error]);
const enableHistoryDivider = useAgentStore((s) => {
const config = agentSelectors.currentAgentConfig(s);
const config = agentSelectors.currentAgentChatConfig(s);
return (
config.enableHistoryCount &&
historyLength > (config.historyCount ?? 0) &&

View File

@@ -0,0 +1,247 @@
{
"exportType": "sessions",
"state": {
"sessions": [
{
"config": {
"chatConfig": {
"displayMode": "chat",
"historyCount": 1,
"enableCompressThreshold": false,
"enableMaxTokens": false,
"inputTemplate": ""
},
"model": "gpt-3.5-turbo",
"params": {
"frequency_penalty": 0,
"presence_penalty": 0,
"temperature": 0.6,
"top_p": 1,
"max_tokens": 2200
},
"plugins": [],
"systemRole": "你是一名 Postgresql 的数据库专家。用户是一名数据库小白,你需要使用简单直白的方式告诉用户如何使用 ostgresql 和它相应的 orm 工具 primsa"
},
"id": "06cc3e20-e870-4099-a619-c07a849d742d",
"meta": {
"avatar": "🐘",
"backgroundColor": "rgba(0,0,0,0)",
"title": "Postgresql 指南",
"description": "PostgreSQL 数据库和 Prisma ORM 工具的简单使用指南",
"tags": ["数据库", "PostgreSQL", "教程", "ORM", "Prisma"]
},
"type": "agent",
"createdAt": 1698463356269,
"group": "default",
"updatedAt": 1698463356269,
"pinned": true
}
],
"messages": [
{
"content": "是的,你可以使用 Prisma 来自定义生成 `id` 的逻辑,例如使用 `nanoid` 库来生成唯一标识符。\n\n首先你需要在项目中安装 `nanoid` 库:\n\n```\nnpm install nanoid\n```\n\n然后你可以在 Prisma 的模型定义中使用 `@default` 指令,并提供一个自定义的默认值函数来生成 `id`。\n\n```prisma\nmodel Message {\n id String @id @default(dbgenerated(\"nanoid(10)\"))\n\n createdAt DateTime @default(now())\n updatedAt DateTime @updatedAt\n}\n```\n\n在上述模型定义中我们使用 `dbgenerated` 指令来定义一个自定义的默认值函数。函数 `nanoid(10)` 会生成一个长度为 10 的唯一标识符。\n\n请注意使用自定义的 `id` 生成逻辑可能会对数据库性能产生一些影响,因为每次创建新记录时都需要执行自定义函数。因此,请确保你的自定义逻辑是高效的,并在实际使用中进行测试和评估性能。",
"id": "n2Tsl3Dm",
"meta": {},
"parentId": "JSZaJOGj",
"role": "assistant",
"topicId": "CZg3IEdy",
"createdAt": 1698463981313,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698464004494,
"fromModel": "gpt-3.5-turbo"
},
{
"content": "如何指定 自定义函数的引入位置?",
"id": "xPft8Msa",
"meta": {},
"role": "user",
"topicId": "CZg3IEdy",
"createdAt": 1698464173224,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698464173226
},
{
"content": "在 Prisma 中,你可以通过在 `schema.prisma` 文件的顶部使用 `generator` 块来指定自定义函数的引入位置。\n\n例如假设你想使用 `nanoid` 库来生成自定义的 `id`,你可以按照以下方式配置:\n\n```prisma\ngenerator client {\n provider = \"prisma-client-js\"\n output = \"./node_modules/@prisma/client\"\n}\n\ngenerator dbgenerated {\n provider = \"prisma-dbgenerator\"\n output = \"./node_modules/@prisma/client\"\n binaryTargets = [\"native\"]\n previewFeatures = [\"nativeTypes\"]\n config = {\n \"generator\": {\n \"dbgenerated\": {\n \"output\": \"./src/db/generated\",\n \"provider\": \"./src/db/generated/provider.js\"\n }\n }\n }\n}\n```\n\n在上述配置中我们定义了两个生成器。`client` 生成器用于生成 Prisma 客户端代码,而 `dbgenerated` 生成器用于生成数据库相关的代码。\n\n注意到 `dbgenerated` 生成器的 `config` 部分中有一个 `provider` 字段,它指定了自定义函数的引入位置。在这个示例中,我们将自定义函数放在 `./src/db/generated/provider.js` 文件中。\n\n你可以根据你的项目结构和需要来调整路径和文件名确保指定的 `provider` 路径正确引入了自定义函数。",
"id": "jZc2KKRs",
"meta": {},
"parentId": "xPft8Msa",
"role": "assistant",
"topicId": "CZg3IEdy",
"createdAt": 1698464173228,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698464181896,
"fromModel": "gpt-3.5-turbo"
},
{
"content": "如何启动一个 postgres Docker 镜像?",
"id": "LjG9XSuq",
"meta": {},
"role": "user",
"topicId": "MNWhBEh5",
"createdAt": 1698754460196,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754463118
},
{
"content": "要启动一个 PostgreSQL Docker 镜像,你可以按照以下步骤操作:\n\n1. 首先,确保你已经安装了 Docker可以在终端中运行 `docker --version` 来检查。\n\n2. 在终端中运行以下命令以下载并启动一个 PostgreSQL 镜像:\n\n ```shell\n docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres\n ```\n\n 这将从 Docker Hub 下载 PostgreSQL 镜像",
"id": "CL7E1Mc5",
"meta": {},
"parentId": "LjG9XSuq",
"role": "assistant",
"topicId": "MNWhBEh5",
"createdAt": 1698754479751,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754507690,
"fromModel": "gpt-3.5-turbo"
},
{
"content": "如何指定本地存储的目录到 /www/database/postgres",
"id": "UIvHZr7m",
"meta": {},
"role": "user",
"topicId": "MNWhBEh5",
"createdAt": 1698754591763,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754591764
},
{
"content": "要将本地存储的目录映射到容器内的 `/var/lib/postgresql/data` 目录,你可以使用 Docker 的 `-v` 参数。\n\n以下是示例命令\n\n```shell\ndocker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -v /www/database/postgres:/var/lib/postgresql/data -d postgres\n```\n\n在这个命令中我们使用了 `-v` 参数来指定本地目录 `/www/database/postgres` 映射到容器内的 `/var/lib/postgresql/data` 目录。这样,所有的 PostgreSQL 数据将会保存在本地指定的目录中。",
"id": "XLJOK44r",
"meta": {},
"parentId": "UIvHZr7m",
"role": "assistant",
"topicId": "MNWhBEh5",
"createdAt": 1698754591765,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754609275,
"fromModel": "gpt-3.5-turbo"
},
{
"content": "docker run --name postgres -e POSTGRES_PASSWORD=DSLapHYdzNrai5j6 -p 33300:5432 -v /www/database/postgres:/var/lib/postgresql/data -d postgres",
"id": "W3J6jQjm",
"meta": {},
"role": "user",
"topicId": "MNWhBEh5",
"createdAt": 1698754700529,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754700530
},
{
"content": "如何启动 pgadmin",
"id": "L7mq0ehV",
"meta": {},
"role": "user",
"topicId": "MNWhBEh5",
"createdAt": 1698755749886,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698755749888
},
{
"content": "要启动 pgAdmin你可以按照以下步骤操作\n\n1. 首先,确保你已经安装了 Docker并且已经成功启动了 PostgreSQL Docker 容器。\n\n2. 在终端中运行以下命令以启动 pgAdmin Docker 容器:\n\n ```shell\n docker run --name my-pgadmin -p 5050:80 -e PGADMIN_DEFAULT_EMAIL=myemail@example.com -e PGADMIN_DEFAULT_PASSWORD=mypassword -d dpage/pgadmin4\n ```\n\n 在这个命令中,我们使用了 `-p` 参数来将容器的端口 80 映射到本地的端口 5050。你可以根据需要修改这个端口映射。\n\n 我们还使用了 `-e` 参数来设置 pgAdmin 的默认登录邮箱和密码。你可以根据需要修改这些值。\n\n3. 在浏览器中访问 `http://localhost:5050`,你将看到 pgAdmin 的登录界面。\n\n4. 使用在上一步中设置的邮箱和密码进行登录。\n\n5. 在 pgAdmin 中,你可以添加一个新的服务器连接,以连接到运行在 Docker 容器中的 PostgreSQL 数据库。要添加一个新的服务器连接,你需要提供以下信息:\n\n - 主机名/地址:使用 Docker 容器的 IP 地址或者 `localhost`。\n - 端口:使用 Docker 容器中 PostgreSQL 的映射端口,例如 33300。\n - 用户名和密码:使用在启动 PostgreSQL 容器时设置的用户名和密码。\n\n 完成上述信息的填写后,点击保存并连接到 PostgreSQL 服务器。\n\n现在你已经成功启动了 pgAdmin 并连接到了你的 PostgreSQL 数据库。你可以使用 pgAdmin 来管理和操作你的数据库。",
"id": "d5XTX9EQ",
"meta": {},
"parentId": "L7mq0ehV",
"role": "assistant",
"topicId": "MNWhBEh5",
"createdAt": 1698755749889,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698755786183,
"fromModel": "gpt-3.5-turbo"
},
{
"content": "",
"id": "tool_calls_KPPDiRyW",
"meta": {},
"parentId": "42k72jMi",
"role": "assistant",
"createdAt": 1690650544842,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1690650572389,
"fromModel": "gpt-3.5-turbo-16k",
"tools": [
{
"apiName": "websiteCrawler",
"arguments": "{\n \"url\": \"https://mp.weixin.qq.com/s?__biz=MjM5MTA1MjAxMQ==&mid=2651264337&idx=1&sn=d7d9126578c74c912e1f0d42cb5629da&chksm=bd48ccd58a3f45c3f2cbc7d7b732c820b9e7cd6b547c06acc8170b233710b5fb5ed62f5fd94d&mpshare=1&scene=1&srcid=07294Mpw7C6JCLgtagL1cBDR&sharer_sharetime=1690622712877&sharer_shareid=0358058a42fc25387d28337fc3d22c3c#rd\"\n}",
"id": "tool_call_KPPDiRyW",
"identifier": "websiteCrawler",
"type": "default"
}
]
},
{
"content": "abcabc",
"id": "KPPDiRyW",
"meta": {},
"parentId": "tool_calls_KPPDiRyW",
"role": "tool",
"createdAt": 1690650544852,
"plugin": {
"apiName": "websiteCrawler",
"arguments": "{\n \"url\": \"https://mp.weixin.qq.com/s?__biz=MjM5MTA1MjAxMQ==&mid=2651264337&idx=1&sn=d7d9126578c74c912e1f0d42cb5629da&chksm=bd48ccd58a3f45c3f2cbc7d7b732c820b9e7cd6b547c06acc8170b233710b5fb5ed62f5fd94d&mpshare=1&scene=1&srcid=07294Mpw7C6JCLgtagL1cBDR&sharer_sharetime=1690622712877&sharer_shareid=0358058a42fc25387d28337fc3d22c3c#rd\"\n}",
"identifier": "websiteCrawler",
"type": "default"
},
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1690650572399,
"fromModel": "gpt-3.5-turbo-16k",
"tool_call_id": "tool_call_KPPDiRyW"
},
{
"content": "",
"id": "tool_calls_9cRjevRQ",
"meta": {},
"parentId": "3nDXtEKv",
"role": "assistant",
"createdAt": 1700065743395,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1700065751851,
"fromModel": "gpt-3.5-turbo-16k",
"tools": [
{
"apiName": "getWebsiteContent",
"arguments": "{\n \"url\": \"https://nodejs.org/api/packages.html#dual-package-hazard\"\n}",
"id": "tool_call_9cRjevRQ",
"identifier": "website-crawler",
"type": "default"
}
]
},
{
"content": "bbbbb",
"id": "9cRjevRQ",
"meta": {},
"parentId": "tool_calls_9cRjevRQ",
"role": "tool",
"plugin": {
"apiName": "getWebsiteContent",
"arguments": "{\n \"url\": \"https://nodejs.org/api/packages.html#dual-package-hazard\"\n}",
"identifier": "website-crawler",
"type": "default"
},
"createdAt": 1700065743405,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1700065751861,
"fromModel": "gpt-3.5-turbo-16k",
"tool_call_id": "tool_call_9cRjevRQ"
}
],
"topics": [
{
"id": "CZg3IEdy",
"title": "Prisma中的cuid函数作用是什么",
"createdAt": 1698463911747,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698463915716
},
{
"id": "MNWhBEh5",
"title": "启动 PostgreSQL Docker 镜像方法",
"createdAt": 1698754463117,
"sessionId": "06cc3e20-e870-4099-a619-c07a849d742d",
"updatedAt": 1698754464867
}
],
"sessionGroups": []
},
"version": 6
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,61 @@
import type { Migration, MigrationData } from '@/migrations/VersionController';
import { V5ConfigState, V5Session } from './types/v5';
import { V6ConfigState, V6Session } from './types/v6';
export class MigrationV5ToV6 implements Migration {
// from this version to start migration
version = 5;
migrate(data: MigrationData<V5ConfigState>): MigrationData<V6ConfigState> {
const { sessions } = data.state;
return {
...data,
state: {
...data.state,
sessions: MigrationV5ToV6.migrateSession(sessions),
},
};
}
static migrateChatConfig(config: V5Session['config']): V6Session['config'] {
const {
autoCreateTopicThreshold,
enableAutoCreateTopic,
compressThreshold,
enableCompressThreshold,
enableHistoryCount,
enableMaxTokens,
historyCount,
inputTemplate,
displayMode,
...agentConfig
} = config;
return {
...agentConfig,
chatConfig: {
autoCreateTopicThreshold,
compressThreshold,
displayMode,
enableAutoCreateTopic,
enableCompressThreshold,
enableHistoryCount,
enableMaxTokens,
historyCount,
inputTemplate,
},
};
}
static migrateSession(sessions: V5Session[]): V6Session[] {
return sessions.map(({ config, updateAt, updatedAt, createdAt, createAt, ...res }) => ({
...res,
config: MigrationV5ToV6.migrateChatConfig(config),
createdAt: createdAt || createAt!,
updatedAt: updatedAt || updateAt!,
}));
}
}
export const MigrationAgentChatConfig = MigrationV5ToV6;

View File

@@ -0,0 +1,50 @@
import { describe } from 'vitest';
import { MigrationData, VersionController } from '@/migrations/VersionController';
import { MigrationV1ToV2 } from '../FromV1ToV2';
import inputV1Data from '../FromV1ToV2/fixtures/input-v1-session.json';
import { MigrationV2ToV3 } from '../FromV2ToV3';
import { MigrationV3ToV4 } from '../FromV3ToV4';
import { MigrationV4ToV5 } from '../FromV4ToV5';
import outputDataFromV1ToV6 from './fixtures/from-v1-to-v6-output.json';
import sessionInputV5 from './fixtures/session-input-v5.json';
import sessionOutputV6 from './fixtures/session-output-v6.json';
import { MigrationV5ToV6 } from './index';
describe('MigrationV5ToV6', () => {
let migrations;
let versionController: VersionController<any>;
beforeEach(() => {
migrations = [MigrationV5ToV6];
versionController = new VersionController(migrations, 6);
});
describe('should migrate data correctly from previous versions', () => {
it('session config', () => {
const data: MigrationData = sessionInputV5;
const migratedData = versionController.migrate(data);
expect(migratedData.version).toEqual(sessionOutputV6.version);
expect(migratedData.state.sessions).toEqual(sessionOutputV6.state.sessions);
});
});
it('should work correct from v1 to v6', () => {
const data: MigrationData = inputV1Data;
versionController = new VersionController(
[MigrationV5ToV6, MigrationV4ToV5, MigrationV3ToV4, MigrationV2ToV3, MigrationV1ToV2],
6,
);
const migratedData = versionController.migrate(data);
expect(migratedData.version).toEqual(outputDataFromV1ToV6.version);
expect(migratedData.state.messages).toEqual(outputDataFromV1ToV6.state.messages);
expect(migratedData.state.sessions).toEqual(outputDataFromV1ToV6.state.sessions);
expect(migratedData.state.topics).toEqual(outputDataFromV1ToV6.state.topics);
});
});

View File

@@ -0,0 +1,48 @@
import { LobeAgentTTSConfig } from '@/types/agent';
import { FewShots, LLMParams } from '@/types/llm';
/**
* Lobe Agent
*/
export interface V5Session {
config: V5AgentConfig;
createAt?: number;
createdAt: number;
group?: string;
id: string;
meta: {
avatar?: string;
backgroundColor?: string;
description?: string;
tags?: string[];
title?: string;
};
model: string;
pinned?: boolean;
type: 'agent';
updateAt?: number;
updatedAt: number;
}
export interface V5AgentConfig {
autoCreateTopicThreshold: number;
compressThreshold?: number;
displayMode?: 'chat' | 'docs';
enableAutoCreateTopic: boolean;
enableCompressThreshold?: boolean;
enableHistoryCount?: boolean;
enableMaxTokens?: boolean;
fewShots?: FewShots;
historyCount?: number;
inputTemplate?: string;
model: string;
params: LLMParams;
plugins?: string[];
provider?: string;
systemRole: string;
tts: LobeAgentTTSConfig;
}
export interface V5ConfigState {
sessions: V5Session[];
}

View File

@@ -0,0 +1,60 @@
import { FewShots, LLMParams } from '@/types/llm';
export type TTSServer = 'openai' | 'edge' | 'microsoft';
export interface TTSConfig {
showAllLocaleVoice?: boolean;
sttLocale: 'auto' | string;
ttsService: TTSServer;
voice: {
edge?: string;
microsoft?: string;
openai: string;
};
}
export interface ChatConfig {
autoCreateTopicThreshold: number;
compressThreshold?: number;
displayMode?: 'chat' | 'docs';
enableAutoCreateTopic?: boolean;
enableCompressThreshold?: boolean;
enableHistoryCount?: boolean;
enableMaxTokens?: boolean;
historyCount?: number;
inputTemplate?: string;
}
export interface V6AgentConfig {
chatConfig: ChatConfig;
fewShots?: FewShots;
model: string;
params: LLMParams;
plugins?: string[];
provider?: string;
systemRole: string;
tts: TTSConfig;
}
export interface V6Session {
config: V6AgentConfig;
createdAt: number;
group?: string;
id: string;
meta: {
avatar?: string;
backgroundColor?: string;
description?: string;
tags?: string[];
title?: string;
};
model: string;
pinned?: boolean;
type: 'agent';
updatedAt: number;
}
export interface V6ConfigState {
sessions: V6Session[];
}

View File

@@ -6,12 +6,19 @@ import { MigrationV0ToV1 } from './FromV0ToV1';
import { MigrationV1ToV2 } from './FromV1ToV2';
import { MigrationV3ToV4 } from './FromV3ToV4';
import { MigrationV4ToV5 } from './FromV4ToV5';
import { MigrationV5ToV6 } from './FromV5ToV6';
// Current latest version
export const CURRENT_CONFIG_VERSION = 5;
export const CURRENT_CONFIG_VERSION = 6;
// Version migrations module
const ConfigMigrations = [
/**
* 2024.05.24
*
* some config in agentConfig change to chatConfig
*/
MigrationV5ToV6,
/**
* 2024.05.11
*

View File

@@ -205,7 +205,7 @@ describe('SessionService', () => {
describe('updateSessionConfig', () => {
it('should update the config of a session', async () => {
// Setup
const newConfig = { compressThreshold: 2 } as LobeAgentConfig;
const newConfig = { model: 'abc' } as LobeAgentConfig;
(SessionModel.updateConfig as Mock).mockResolvedValue({ ...mockSession, config: newConfig });
// Execute

View File

@@ -5,7 +5,7 @@ import { SessionModel } from '@/database/client/models/session';
import { SessionGroupModel } from '@/database/client/models/sessionGroup';
import { UserModel } from '@/database/client/models/user';
import { useUserStore } from '@/store/user';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import {
ChatSessionList,
LobeAgentSession,
@@ -96,7 +96,14 @@ export class ClientService implements ISessionService {
return SessionModel.update(id, { ...data, pinned });
}
async updateSessionConfig(activeId: string, config: DeepPartial<LobeAgentConfig>) {
async updateSessionConfig(
activeId: string,
config: DeepPartial<LobeAgentConfig>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_?: AbortSignal,
) {
// TODO: 需要删除这部分处理逻辑
// 后续直接给用户创建一个 inbox 的 session
if (activeId === INBOX_SESSION_ID) {
return useUserStore.getState().updateDefaultAgent({ config });
}
@@ -104,6 +111,15 @@ export class ClientService implements ISessionService {
return SessionModel.updateConfig(activeId, config);
}
async updateSessionChatConfig(
activeId: string,
config: DeepPartial<LobeAgentChatConfig>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_?: AbortSignal,
) {
return this.updateSessionConfig(activeId, { chatConfig: config });
}
async removeSession(id: string) {
return SessionModel.delete(id);
}

View File

@@ -1,7 +1,7 @@
/* eslint-disable typescript-sort-keys/interface */
import { DeepPartial } from 'utility-types';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { BatchTaskResult } from '@/types/service';
import {
ChatSessionList,
@@ -30,7 +30,16 @@ export interface ISessionService {
): Promise<any>;
getSessionConfig(id: string): Promise<LobeAgentConfig>;
updateSessionConfig(id: string, config: DeepPartial<LobeAgentConfig>): Promise<any>;
updateSessionConfig(
id: string,
config: DeepPartial<LobeAgentConfig>,
signal?: AbortSignal,
): Promise<any>;
updateSessionChatConfig(
id: string,
config: DeepPartial<LobeAgentChatConfig>,
signal?: AbortSignal,
): Promise<any>;
removeSession(id: string): Promise<any>;
removeAllSessions(): Promise<any>;

View File

@@ -2,10 +2,12 @@
exports[`agentSelectors > defaultAgentConfig > should merge DEFAULT_AGENT_CONFIG and defaultAgent(s).config correctly 1`] = `
{
"autoCreateTopicThreshold": 2,
"displayMode": "chat",
"enableAutoCreateTopic": true,
"historyCount": 1,
"chatConfig": {
"autoCreateTopicThreshold": 2,
"displayMode": "chat",
"enableAutoCreateTopic": true,
"historyCount": 1,
},
"model": "gpt-3.5-turbo",
"params": {
"frequency_penalty": 0,

View File

@@ -99,7 +99,11 @@ describe('AgentSlice', () => {
await result.current.updateAgentConfig(config);
});
expect(updateSessionConfigMock).toHaveBeenCalledWith('inbox', config);
expect(updateSessionConfigMock).toHaveBeenCalledWith(
'inbox',
config,
expect.any(AbortSignal),
);
expect(refreshMock).toHaveBeenCalled();
updateSessionConfigMock.mockRestore();
refreshMock.mockRestore();
@@ -122,7 +126,11 @@ describe('AgentSlice', () => {
await result.current.updateAgentConfig(config);
});
expect(updateSessionConfigMock).toHaveBeenCalledWith('session-id', config);
expect(updateSessionConfigMock).toHaveBeenCalledWith(
'session-id',
config,
expect.any(AbortSignal),
);
expect(refreshMock).toHaveBeenCalled();
updateSessionConfigMock.mockRestore();
refreshMock.mockRestore();
@@ -229,7 +237,11 @@ describe('AgentSlice', () => {
await result.current.internal_updateAgentConfig('test-session-id', { foo: 'bar' } as any);
});
expect(updateSessionConfigMock).toHaveBeenCalledWith('test-session-id', { foo: 'bar' });
expect(updateSessionConfigMock).toHaveBeenCalledWith(
'test-session-id',
{ foo: 'bar' },
undefined,
);
});
it('should trigger internal_refreshAgentConfig', async () => {

View File

@@ -8,8 +8,9 @@ import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { useClientDataSWR } from '@/libs/swr';
import { globalService } from '@/services/global';
import { sessionService } from '@/services/session';
import { AgentState } from '@/store/agent/slices/chat/initialState';
import { useSessionStore } from '@/store/session';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { merge } from '@/utils/merge';
import { AgentStore } from '../../store';
@@ -21,6 +22,7 @@ import { agentSelectors } from './selectors';
export interface AgentChatAction {
removePlugin: (id: string) => void;
togglePlugin: (id: string, open?: boolean) => Promise<void>;
updateAgentChatConfig: (config: Partial<LobeAgentChatConfig>) => Promise<void>;
updateAgentConfig: (config: Partial<LobeAgentConfig>) => Promise<void>;
useFetchAgentConfig: (id: string) => SWRResponse<LobeAgentConfig>;
@@ -28,8 +30,18 @@ export interface AgentChatAction {
/* eslint-disable typescript-sort-keys/interface */
internal_updateAgentConfig: (id: string, data: DeepPartial<LobeAgentConfig>) => Promise<void>;
internal_updateAgentConfig: (
id: string,
data: DeepPartial<LobeAgentConfig>,
signal?: AbortSignal,
) => Promise<void>;
internal_updateAgentChatConfig: (
id: string,
data: DeepPartial<LobeAgentChatConfig>,
signal?: AbortSignal,
) => Promise<void>;
internal_refreshAgentConfig: (id: string) => Promise<void>;
internal_createAbortController: (key: keyof AgentState) => AbortController;
/* eslint-enable */
}
@@ -69,12 +81,23 @@ export const createChatSlice: StateCreator<
await get().updateAgentConfig(config);
},
updateAgentChatConfig: async (config) => {
const { activeId } = get();
if (!activeId) return;
const controller = get().internal_createAbortController('updateAgentChatConfigSignal');
await get().internal_updateAgentChatConfig(activeId, config, controller.signal);
},
updateAgentConfig: async (config) => {
const { activeId } = get();
if (!activeId) return;
await get().internal_updateAgentConfig(activeId, config);
const controller = get().internal_createAbortController('updateAgentConfigSignal');
await get().internal_updateAgentConfig(activeId, config, controller.signal);
},
useFetchAgentConfig: (sessionId) =>
useClientDataSWR<LobeAgentConfig>(
@@ -113,19 +136,42 @@ export const createChatSlice: StateCreator<
/* eslint-disable sort-keys-fix/sort-keys-fix */
internal_updateAgentConfig: async (id, data) => {
internal_updateAgentConfig: async (id, data, signal) => {
const prevModel = agentSelectors.currentAgentModel(get());
// optimistic update at frontend
set({ agentConfig: merge(get().agentConfig, data) }, false, 'optimistic_updateAgentConfig');
await sessionService.updateSessionConfig(id, data);
await sessionService.updateSessionConfig(id, data, signal);
await get().internal_refreshAgentConfig(id);
// refresh sessions to update the agent config if the model has changed
if (prevModel !== data.model) await useSessionStore.getState().refreshSessions();
},
internal_updateAgentChatConfig: async (id, data, signal) => {
// optimistic update at frontend
const chatConfig = merge(get().agentConfig.chatConfig, data);
set(
{ agentConfig: { ...get().agentConfig, chatConfig } },
false,
'optimistic_updateAgentConfig',
);
await sessionService.updateSessionChatConfig(id, data, signal);
await get().internal_refreshAgentConfig(id);
},
internal_refreshAgentConfig: async (id) => {
await mutate([FETCH_AGENT_CONFIG_KEY, id]);
},
internal_createAbortController: (key) => {
const abortController = get()[key] as AbortController;
if (abortController) abortController.abort('canceled');
const controller = new AbortController();
set({ [key]: controller }, false, 'internal_createAbortController');
return controller;
},
});

View File

@@ -9,6 +9,8 @@ export interface AgentState {
defaultAgentConfig: LobeAgentConfig;
isAgentConfigInit: boolean;
isDefaultAgentConfigInit: boolean;
updateAgentChatConfigSignal?: AbortController;
updateAgentConfigSignal?: AbortController;
}
export const initialAgentChatState: AgentState = {

View File

@@ -3,9 +3,6 @@ import { describe, expect, it } from 'vitest';
import { INBOX_SESSION_ID } from '@/const/session';
import { DEFAULT_AGENT_CONFIG, DEFAUTT_AGENT_TTS_CONFIG } from '@/const/settings';
import { AgentStore } from '@/store/agent';
import { UserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/slices/settings/selectors';
import { LobeAgentConfig } from '@/types/agent';
import { agentSelectors } from './selectors';

View File

@@ -1,9 +1,13 @@
import { VoiceList } from '@lobehub/tts';
import { INBOX_SESSION_ID } from '@/const/session';
import { DEFAULT_AGENT_CONFIG, DEFAUTT_AGENT_TTS_CONFIG } from '@/const/settings';
import {
DEFAULT_AGENT_CHAT_CONFIG,
DEFAULT_AGENT_CONFIG,
DEFAUTT_AGENT_TTS_CONFIG,
} from '@/const/settings';
import { AgentStore } from '@/store/agent';
import { LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig, LobeAgentTTSConfig } from '@/types/agent';
import { merge } from '@/utils/merge';
const isInboxSession = (s: AgentStore) => s.activeId === INBOX_SESSION_ID;
@@ -15,6 +19,9 @@ const defaultAgentConfig = (s: AgentStore) => merge(DEFAULT_AGENT_CONFIG, s.defa
const currentAgentConfig = (s: AgentStore): LobeAgentConfig =>
merge(s.defaultAgentConfig, s.agentConfig);
const currentAgentChatConfig = (s: AgentStore): LobeAgentChatConfig =>
currentAgentConfig(s).chatConfig || DEFAULT_AGENT_CHAT_CONFIG;
const currentAgentSystemRole = (s: AgentStore) => {
return currentAgentConfig(s).systemRole;
};
@@ -73,6 +80,7 @@ const hasSystemRole = (s: AgentStore) => {
};
export const agentSelectors = {
currentAgentChatConfig,
currentAgentConfig,
currentAgentModel,
currentAgentModelProvider,

View File

@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { ChatMessage } from '@/types/message';
import { OpenAIChatMessage } from '@/types/openai/chat';
import { encodeAsync } from '@/utils/tokenizer';
@@ -74,13 +74,13 @@ describe('chatHelpers', () => {
] as ChatMessage[];
it('returns all messages if history is disabled', () => {
const config = { enableHistoryCount: false, historyCount: 0 } as LobeAgentConfig;
const config = { enableHistoryCount: false, historyCount: 0 } as LobeAgentChatConfig;
const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config);
expect(slicedMessages).toEqual(messages);
});
it('returns last N messages based on historyCount', () => {
const config = { enableHistoryCount: true, historyCount: 2 } as LobeAgentConfig;
const config = { enableHistoryCount: true, historyCount: 2 } as LobeAgentChatConfig;
const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config);
expect(slicedMessages).toEqual([
{ id: '2', content: 'Second' },
@@ -89,19 +89,19 @@ describe('chatHelpers', () => {
});
it('returns empty array when historyCount is negative', () => {
const config = { enableHistoryCount: true, historyCount: -1 } as LobeAgentConfig;
const config = { enableHistoryCount: true, historyCount: -1 } as LobeAgentChatConfig;
const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config);
expect(slicedMessages).toEqual([]);
});
it('returns all messages if historyCount exceeds the array length', () => {
const config = { enableHistoryCount: true, historyCount: 5 } as LobeAgentConfig;
const config = { enableHistoryCount: true, historyCount: 5 } as LobeAgentChatConfig;
const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config);
expect(slicedMessages).toEqual(messages);
});
it('returns an empty array for an empty message array', () => {
const config = { enableHistoryCount: true, historyCount: 2 } as LobeAgentConfig;
const config = { enableHistoryCount: true, historyCount: 2 } as LobeAgentChatConfig;
const slicedMessages = chatHelpers.getSlicedMessagesWithConfig([], config);
expect(slicedMessages).toEqual([]);
});

View File

@@ -1,4 +1,4 @@
import { LobeAgentConfig } from '@/types/agent';
import { LobeAgentChatConfig } from '@/types/agent';
import { ChatMessage } from '@/types/message';
import { OpenAIChatMessage } from '@/types/openai/chat';
import { encodeAsync } from '@/utils/tokenizer';
@@ -11,7 +11,7 @@ export const getMessageById = (messages: ChatMessage[], id: string) =>
const getSlicedMessagesWithConfig = (
messages: ChatMessage[],
config: LobeAgentConfig,
config: LobeAgentChatConfig,
): ChatMessage[] => {
// if historyCount is not enabled or set to 0, return all messages
if (!config.enableHistoryCount || !config.historyCount) return messages;

View File

@@ -4,7 +4,7 @@ import useSWR, { mutate } from 'swr';
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { LOADING_FLAT } from '@/const/message';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { DEFAULT_AGENT_CHAT_CONFIG, DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { TraceEventType } from '@/const/trace';
import { chatService } from '@/services/chat';
import { messageService } from '@/services/message';
@@ -70,6 +70,9 @@ beforeEach(() => {
vi.clearAllMocks();
useChatStore.setState(mockState, false);
vi.spyOn(agentSelectors, 'currentAgentConfig').mockImplementation(() => DEFAULT_AGENT_CONFIG);
vi.spyOn(agentSelectors, 'currentAgentChatConfig').mockImplementation(
() => DEFAULT_AGENT_CHAT_CONFIG,
);
vi.spyOn(sessionMetaSelectors, 'currentAgentMeta').mockImplementation(() => ({ tags: [] }));
});
@@ -417,8 +420,10 @@ describe('chatMessage actions', () => {
act(() => {
useAgentStore.setState({
agentConfig: {
enableAutoCreateTopic: false,
autoCreateTopicThreshold: 1,
chatConfig: {
enableAutoCreateTopic: false,
autoCreateTopicThreshold: 1,
},
},
});
useChatStore.setState({
@@ -447,7 +452,7 @@ describe('chatMessage actions', () => {
(messageService.createMessage as Mock).mockResolvedValue('new-message-id');
// Mock agent config to simulate auto-create topic behavior
(agentSelectors.currentAgentConfig as Mock).mockImplementation(() => ({
(agentSelectors.currentAgentChatConfig as Mock).mockImplementation(() => ({
autoCreateTopicThreshold,
enableAutoCreateTopic,
}));

View File

@@ -134,6 +134,7 @@ export interface ChatMessageAction {
}
const getAgentConfig = () => agentSelectors.currentAgentConfig(useAgentStore.getState());
const getAgentChatConfig = () => agentSelectors.currentAgentChatConfig(useAgentStore.getState());
const preventLeavingFn = (e: BeforeUnloadEvent) => {
// set returnValue to trigger alert modal
@@ -242,7 +243,7 @@ export const chatMessage: StateCreator<
topicId: activeTopicId,
};
const agentConfig = getAgentConfig();
const agentConfig = getAgentChatConfig();
let tempMessageId: string | undefined = undefined;
let newTopicId: string | undefined = undefined;
@@ -463,19 +464,20 @@ export const chatMessage: StateCreator<
n('generateMessage(start)', { assistantId, messages }) as string,
);
const config = getAgentConfig();
const agentConfig = getAgentConfig();
const chatConfig = agentConfig.chatConfig;
const compiler = template(config.inputTemplate, { interpolate: /{{([\S\s]+?)}}/g });
const compiler = template(chatConfig.inputTemplate, { interpolate: /{{([\S\s]+?)}}/g });
// ================================== //
// messages uniformly preprocess //
// ================================== //
// 1. slice messages with config
let preprocessMsgs = chatHelpers.getSlicedMessagesWithConfig(messages, config);
let preprocessMsgs = chatHelpers.getSlicedMessagesWithConfig(messages, chatConfig);
// 2. replace inputMessage template
preprocessMsgs = !config.inputTemplate
preprocessMsgs = !chatConfig.inputTemplate
? preprocessMsgs
: preprocessMsgs.map((m) => {
if (m.role === 'user') {
@@ -492,21 +494,23 @@ export const chatMessage: StateCreator<
});
// 3. add systemRole
if (config.systemRole) {
preprocessMsgs.unshift({ content: config.systemRole, role: 'system' } as ChatMessage);
if (agentConfig.systemRole) {
preprocessMsgs.unshift({ content: agentConfig.systemRole, role: 'system' } as ChatMessage);
}
// 4. handle max_tokens
config.params.max_tokens = config.enableMaxTokens ? config.params.max_tokens : undefined;
agentConfig.params.max_tokens = chatConfig.enableMaxTokens
? agentConfig.params.max_tokens
: undefined;
// 5. handle config for the vision model
// Due to the gpt-4-vision-preview model's default max_tokens is very small
// we need to set the max_tokens a larger one.
if (config.model === 'gpt-4-vision-preview') {
if (agentConfig.model === 'gpt-4-vision-preview') {
/* eslint-disable unicorn/no-lonely-if */
if (!config.params.max_tokens)
if (!agentConfig.params.max_tokens)
// refs: https://github.com/lobehub/lobe-chat/issues/837
config.params.max_tokens = 2048;
agentConfig.params.max_tokens = 2048;
}
let isFunctionCall = false;
@@ -517,10 +521,10 @@ export const chatMessage: StateCreator<
abortController,
params: {
messages: preprocessMsgs,
model: config.model,
provider: config.provider,
...config.params,
plugins: config.plugins,
model: agentConfig.model,
provider: agentConfig.provider,
...agentConfig.params,
plugins: agentConfig.plugins,
},
trace: {
traceId: params?.traceId,

View File

@@ -148,8 +148,10 @@ describe('chatSelectors', () => {
useAgentStore.setState({
activeId: 'inbox',
agentConfig: {
historyCount: 2,
enableHistoryCount: true,
chatConfig: {
historyCount: 2,
enableHistoryCount: true,
},
model: 'abc',
} as LobeAgentConfig,
});

View File

@@ -102,7 +102,7 @@ const currentChatIDsWithGuideMessage = (s: ChatStore) => {
const currentChatsWithHistoryConfig = (s: ChatStore): ChatMessage[] => {
const chats = currentChats(s);
const config = agentSelectors.currentAgentConfig(useAgentStore.getState());
const config = agentSelectors.currentAgentChatConfig(useAgentStore.getState());
return chatHelpers.getSlicedMessagesWithConfig(chats, config);
};

View File

@@ -34,9 +34,6 @@ vi.mock('@/components/AntdStaticMethods', () => ({
},
}));
// Mock router
const mockRouterPush = vi.fn();
const mockRefresh = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
@@ -72,12 +69,14 @@ describe('SessionAction', () => {
let createdSessionId;
await act(async () => {
createdSessionId = await result.current.createSession({ config: { displayMode: 'docs' } });
createdSessionId = await result.current.createSession({
config: { chatConfig: { displayMode: 'docs' } },
});
});
const call = vi.mocked(sessionService.createSession).mock.calls[0];
expect(call[0]).toEqual(LobeSessionType.Agent);
expect(call[1]).toMatchObject({ config: { displayMode: 'docs' } });
expect(call[1]).toMatchObject({ config: { chatConfig: { displayMode: 'docs' } } });
expect(createdSessionId).toBe(newSessionId);
});
@@ -91,14 +90,14 @@ describe('SessionAction', () => {
await act(async () => {
createdSessionId = await result.current.createSession(
{ config: { displayMode: 'docs' } },
{ config: { chatConfig: { displayMode: 'docs' } } },
false,
);
});
const call = vi.mocked(sessionService.createSession).mock.calls[0];
expect(call[0]).toEqual(LobeSessionType.Agent);
expect(call[1]).toMatchObject({ config: { displayMode: 'docs' } });
expect(call[1]).toMatchObject({ config: { chatConfig: { displayMode: 'docs' } } });
expect(createdSessionId).toBe(newSessionId);
});

View File

@@ -69,10 +69,12 @@ exports[`settingsSelectors > dalleConfig > should return the dalle configuration
exports[`settingsSelectors > defaultAgent > should merge DEFAULT_AGENT and s.settings.defaultAgent correctly 1`] = `
{
"config": {
"autoCreateTopicThreshold": 2,
"displayMode": "chat",
"enableAutoCreateTopic": true,
"historyCount": 1,
"chatConfig": {
"autoCreateTopicThreshold": 2,
"displayMode": "chat",
"enableAutoCreateTopic": true,
"historyCount": 1,
},
"model": "gpt-3.5-turbo",
"params": {
"frequency_penalty": 0,

View File

@@ -1,3 +1,5 @@
import { z } from 'zod';
import { FewShots, LLMParams } from '@/types/llm';
export type TTSServer = 'openai' | 'edge' | 'microsoft';
@@ -12,27 +14,10 @@ export interface LobeAgentTTSConfig {
openai: string;
};
}
export interface LobeAgentConfig {
autoCreateTopicThreshold: number;
compressThreshold?: number;
displayMode?: 'chat' | 'docs';
enableAutoCreateTopic: boolean;
/**
* 历史消息长度压缩阈值
*/
enableCompressThreshold?: boolean;
/**
* 开启历史记录条数
*/
enableHistoryCount?: boolean;
enableMaxTokens?: boolean;
export interface LobeAgentConfig {
chatConfig: LobeAgentChatConfig;
fewShots?: FewShots;
/**
* 历史消息条数
*/
historyCount?: number;
inputTemplate?: string;
/**
* 角色所使用的语言模型
* @default gpt-3.5-turbo
@@ -60,6 +45,39 @@ export interface LobeAgentConfig {
tts: LobeAgentTTSConfig;
}
export interface LobeAgentChatConfig {
autoCreateTopicThreshold: number;
compressThreshold?: number;
displayMode?: 'chat' | 'docs';
enableAutoCreateTopic?: boolean;
/**
* 历史消息长度压缩阈值
*/
enableCompressThreshold?: boolean;
/**
* 开启历史记录条数
*/
enableHistoryCount?: boolean;
enableMaxTokens?: boolean;
/**
* 历史消息条数
*/
historyCount?: number;
inputTemplate?: string;
}
export const AgentChatConfigSchema = z.object({
autoCreateTopicThreshold: z.number().default(2),
compressThreshold: z.number().optional(),
displayMode: z.enum(['chat', 'docs']).optional(),
enableAutoCreateTopic: z.boolean().optional(),
enableCompressThreshold: z.boolean().optional(),
enableHistoryCount: z.boolean().optional(),
enableMaxTokens: z.boolean().optional(),
historyCount: z.number().optional(),
});
export type LobeAgentConfigKeys =
| keyof LobeAgentConfig
| ['params', keyof LobeAgentConfig['params']];

View File

@@ -1,57 +0,0 @@
import { LobeAgentConfig } from '@/types/agent';
import { BaseDataModel, MetaData } from './meta';
export enum LobeSessionType {
Agent = 'agent',
Group = 'group',
}
export type SessionGroupId = SessionDefaultGroup | string;
export enum SessionDefaultGroup {
Default = 'default',
Pinned = 'pinned',
}
export interface SessionGroupItem {
createdAt: number;
id: string;
name: string;
sort?: number;
updatedAt: number;
}
export type SessionGroups = SessionGroupItem[];
/**
* Lobe Agent
*/
export interface LobeAgentSession extends BaseDataModel {
config: LobeAgentConfig;
group?: SessionGroupId;
model: string;
pinned?: boolean;
type: LobeSessionType.Agent;
}
export interface LobeAgentSettings {
/**
* 语言模型角色设定
*/
config: LobeAgentConfig;
meta: MetaData;
}
export type LobeSessions = LobeAgentSession[];
export interface CustomSessionGroup extends SessionGroupItem {
children: LobeSessions;
}
export type LobeSessionGroups = SessionGroupItem[];
export interface ChatSessionList {
sessionGroups: LobeSessionGroups;
sessions: LobeSessions;
}

View File

@@ -0,0 +1,35 @@
import { LobeAgentConfig } from '@/types/agent';
import { MetaData } from '../meta';
import { SessionGroupId } from './sessionGroup';
export enum LobeSessionType {
Agent = 'agent',
Group = 'group',
}
/**
* Lobe Agent
*/
export interface LobeAgentSession {
config: LobeAgentConfig;
createdAt: Date;
group?: SessionGroupId;
id: string;
meta: MetaData;
model: string;
pinned?: boolean;
tags?: string[];
type: LobeSessionType.Agent;
updatedAt: Date;
}
export interface LobeAgentSettings {
/**
* 语言模型角色设定
*/
config: LobeAgentConfig;
meta: MetaData;
}
export type LobeSessions = LobeAgentSession[];

View File

@@ -0,0 +1,10 @@
import { LobeSessions } from '@/types/session/agentSession';
import { LobeSessionGroups } from '@/types/session/sessionGroup';
export * from './agentSession';
export * from './sessionGroup';
export interface ChatSessionList {
sessionGroups: LobeSessionGroups;
sessions: LobeSessions;
}

View File

@@ -0,0 +1,24 @@
import { LobeSessions } from './agentSession';
export enum SessionDefaultGroup {
Default = 'default',
Pinned = 'pinned',
}
export type SessionGroupId = SessionDefaultGroup | string;
export interface SessionGroupItem {
createdAt: number;
id: string;
name: string;
sort?: number;
updatedAt: number;
}
export type SessionGroups = SessionGroupItem[];
export interface CustomSessionGroup extends SessionGroupItem {
children: LobeSessions;
}
export type LobeSessionGroups = SessionGroupItem[];

View File

@@ -13,7 +13,13 @@ export default defineConfig({
},
coverage: {
all: false,
exclude: ['__mocks__/**'],
exclude: [
'__mocks__/**',
// just ignore the migration code
// we will use pglite in the future
// so the coverage of this file is not important
'src/database/client/core/db.ts',
],
provider: 'v8',
reporter: ['text', 'json', 'lcov', 'text-summary'],
},