Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 8, 2024
1 parent 46ae363 commit 5bfb9ed
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/database/server/migrations/0011_famous_morlocks.sql

This file was deleted.

2 changes: 2 additions & 0 deletions src/database/server/migrations/0011_first_fat_cobra.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "topics" ADD COLUMN "summary" text;--> statement-breakpoint
ALTER TABLE "topics" ADD COLUMN "metadata" jsonb;
12 changes: 9 additions & 3 deletions src/database/server/migrations/meta/0011_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "316aac1e-94ed-4b6f-bde2-d5591a59d542",
"id": "1b1a0430-ca2b-467e-ab33-d7e2c122e7df",
"prevId": "3cb81077-e3b2-4871-8a14-056dd8eceaed",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -3147,12 +3147,18 @@
"primaryKey": false,
"notNull": false
},
"history_message_id": {
"name": "history_message_id",
"summary": {
"name": "summary",
"type": "text",
"primaryKey": false,
"notNull": false
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"accessed_at": {
"name": "accessed_at",
"type": "timestamp with time zone",
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
{
"idx": 11,
"version": "7",
"when": 1730984913923,
"tag": "0011_famous_morlocks",
"when": 1731065442199,
"tag": "0011_first_fat_cobra",
"breakpoints": true
}
],
Expand Down
5 changes: 3 additions & 2 deletions src/database/server/schemas/lobechat/topic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable sort-keys-fix/sort-keys-fix */
import { boolean, pgTable, text, unique } from 'drizzle-orm/pg-core';
import { boolean, jsonb, pgTable, text, unique } from 'drizzle-orm/pg-core';

import { idGenerator } from '../../utils/idGenerator';
import { timestamps } from './_helpers';
Expand All @@ -19,7 +19,8 @@ export const topics = pgTable(
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
clientId: text('client_id'),
historyMessageId: text('history_message_id'),
summary: text('summary'),
metadata: jsonb('metadata'),
...timestamps,
},
(t) => ({
Expand Down
49 changes: 38 additions & 11 deletions src/features/Conversation/Messages/History/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ModelTag } from '@lobehub/icons';
import { Icon, Markdown } from '@lobehub/ui';
import { Typography } from 'antd';
import { createStyles } from 'antd-style';
import { ScrollText } from 'lucide-react';
import { Center, Flexbox } from 'react-layout-kit';
Expand All @@ -8,26 +10,51 @@ import { ChatMessage } from '@/types/message';
import HistoryDivider from '../../components/HistoryDivider';

const useStyles = createStyles(({ css, token }) => ({
md: css`
background-color: ${token.colorBgContainer};
container: css`
//background-color: ${token.colorBgContainer};
padding: 12px;
border-radius: 12px;
`,
content: css`
color: ${token.colorTextDescription};
`,
line: css`
width: 3px;
height: 100%;
background: ${token.colorBorder};
`,
}));

const History = ({ content }: ChatMessage) => {
const { styles } = useStyles();
const History = ({ content, extra }: ChatMessage) => {
const { styles, theme } = useStyles();
return (
<Flexbox paddingInline={24} style={{ paddingBottom: 8 }}>
<HistoryDivider enable />

<Flexbox align={'flex-start'} className={styles.md} gap={8} horizontal>
<Center height={24} width={24}>
<Icon icon={ScrollText} size={{ fontSize: 20 }}></Icon>
</Center>
<Flexbox gap={4}>
<Flexbox style={{ fontSize: 16 }}>历史消息总结</Flexbox>
<Markdown variant={'chat'}>{content}</Markdown>
<Flexbox className={styles.container} gap={8}>
<Flexbox align={'flex-start'} gap={8} horizontal>
<Center height={20} width={20}>
<Icon
icon={ScrollText}
size={{ fontSize: 16 }}
style={{ color: theme.colorTextDescription }}
/>
</Center>
<Typography.Text type={'secondary'}>历史消息总结</Typography.Text>

{extra?.fromModel && (
<div>
<ModelTag model={extra?.fromModel} />
</div>
)}
</Flexbox>
<Flexbox align={'flex-start'} gap={8} horizontal>
<Flexbox align={'center'} padding={8} width={20}>
<div className={styles.line}></div>
</Flexbox>
<Markdown className={styles.content} variant={'chat'}>
{content}
</Markdown>
</Flexbox>
</Flexbox>
</Flexbox>
Expand Down
53 changes: 8 additions & 45 deletions src/store/chat/slices/aiChat/actions/generateAIChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { knowledgeBaseQAPrompts } from '@/prompts/knowledgeBaseQA';
import { chatService } from '@/services/chat';
import { messageService } from '@/services/message';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { chatHelpers } from '@/store/chat/helpers';
import { ChatStore } from '@/store/chat/store';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
Expand All @@ -24,6 +23,7 @@ import { MessageSemanticSearchChunk } from '@/types/rag';
import { setNamespace } from '@/utils/storeDebug';

import { chatSelectors, topicSelectors } from '../../../selectors';
import { getAgentChatConfig, getAgentConfig, getAgentKnowledge } from './helpers';

const n = setNamespace('ai');

Expand Down Expand Up @@ -94,14 +94,8 @@ export interface AIGenerateAction {
* Controls the streaming state of tool calling processes, updating the UI accordingly
*/
internal_toggleToolCallingStreaming: (id: string, streaming: boolean[] | undefined) => void;

internal_summaryHistory: (messages: ChatMessage[]) => Promise<void>;
}

const getAgentConfig = () => agentSelectors.currentAgentConfig(useAgentStore.getState());
const getAgentChatConfig = () => agentSelectors.currentAgentChatConfig(useAgentStore.getState());
const getAgentKnowledge = () => agentSelectors.currentEnabledKnowledge(useAgentStore.getState());

export const generateAIChat: StateCreator<
ChatStore,
[['zustand/devtools', never]],
Expand Down Expand Up @@ -270,7 +264,7 @@ export const generateAIChat: StateCreator<
// create a new array to avoid the original messages array change
const messages = [...originalMessages];

const { model, provider } = getAgentConfig();
const { model, provider, chatConfig } = getAgentConfig();

let fileChunks: MessageSemanticSearchChunk[] | undefined;
let ragQueryId;
Expand Down Expand Up @@ -331,45 +325,14 @@ export const generateAIChat: StateCreator<
await triggerToolCalls(assistantId);
}

// 5. summary history if context messages is larger than X
// TODO: 需要改成 context message 限制
if (originalMessages.length > 6) {
const historyMessages = originalMessages.slice(0, -6);
await get().internal_summaryHistory(historyMessages);
}
},
internal_summaryHistory: async (messages) => {
const { model, provider } = getAgentConfig();

const historyCompressConfig = systemAgentSelectors.historyCompress(useUserStore.getState());

let historySummary = '';
await chatService.fetchPresetTaskResult({
onFinish: async (text) => {
historySummary = text;
},

params: {
...summaryHistory(messages),
model: historyCompressConfig.model,
provider: historyCompressConfig.provider,
stream: false,
},
});
// 5. summary history if context messages is larger than historyCount
const historyCount = chatConfig.historyCount || 6;

const historyMessage: CreateMessageParams = {
role: 'history',
content: historySummary,
fromModel: model,
fromProvider: provider,
parentId: messages[0].id,
sessionId: get().activeId,
topicId: get().activeTopicId,
createdAt: messages.at(-1)?.createdAt,
};
if (chatConfig.enableHistoryCount && originalMessages.length > historyCount) {
const historyMessages = originalMessages.slice(0, -historyCount);

await messageService.createMessage(historyMessage);
await get().refreshMessages();
await get().internal_summaryHistory(historyMessages);
}
},
internal_fetchAIChatMessage: async (messages, assistantId, params) => {
const {
Expand Down
9 changes: 9 additions & 0 deletions src/store/chat/slices/aiChat/actions/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';

export const getAgentConfig = () => agentSelectors.currentAgentConfig(useAgentStore.getState());
export const getAgentChatConfig = () =>
agentSelectors.currentAgentChatConfig(useAgentStore.getState());

export const getAgentKnowledge = () =>
agentSelectors.currentEnabledKnowledge(useAgentStore.getState());
4 changes: 3 additions & 1 deletion src/store/chat/slices/aiChat/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { StateCreator } from 'zustand/vanilla';
import { ChatStore } from '@/store/chat/store';

import { AIGenerateAction, generateAIChat } from './generateAIChat';
import { ChatMemoryAction, chatMemory } from './memory';
import { ChatRAGAction, chatRag } from './rag';

export interface ChatAIChatAction extends ChatRAGAction, AIGenerateAction {
export interface ChatAIChatAction extends ChatRAGAction, ChatMemoryAction, AIGenerateAction {
/**/
}

Expand All @@ -17,4 +18,5 @@ export const chatAiChat: StateCreator<
> = (...params) => ({
...chatRag(...params),
...generateAIChat(...params),
...chatMemory(...params),
});
51 changes: 51 additions & 0 deletions src/store/chat/slices/aiChat/actions/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { StateCreator } from 'zustand/vanilla';

import { summaryHistory } from '@/chains/summaryHistory';
import { chatService } from '@/services/chat';
import { topicService } from '@/services/topic';
import { ChatStore } from '@/store/chat';
import { useUserStore } from '@/store/user';
import { systemAgentSelectors } from '@/store/user/selectors';
import { ChatMessage } from '@/types/message';

import { getAgentConfig } from './helpers';

export interface ChatMemoryAction {
internal_summaryHistory: (messages: ChatMessage[]) => Promise<void>;
}

export const chatMemory: StateCreator<
ChatStore,
[['zustand/devtools', never]],
[],
ChatMemoryAction
> = (set, get) => ({
internal_summaryHistory: async (messages) => {
const topicId = get().activeTopicId;
if (messages.length <= 1 || !topicId) return;

const { model, provider } = getAgentConfig();

const historyCompressConfig = systemAgentSelectors.historyCompress(useUserStore.getState());

let historySummary = '';
await chatService.fetchPresetTaskResult({
onFinish: async (text) => {
historySummary = text;
},

params: {
...summaryHistory(messages),
model: historyCompressConfig.model,
provider: historyCompressConfig.provider,
stream: false,
},
});

await topicService.updateTopic(topicId, {
metadata: { model, provider },
summary: historySummary,
});
await get().refreshTopic();
},
});
2 changes: 1 addition & 1 deletion src/types/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BaseDataModel } from '../meta';
import { ChatPluginPayload, ChatToolPayload } from './tools';
import { Translate } from './translate';

export type MessageRoleType = 'user' | 'system' | 'assistant' | 'tool' | 'history';
export type MessageRoleType = 'user' | 'system' | 'assistant' | 'tool';

/**
* 聊天消息错误对象
Expand Down
7 changes: 7 additions & 0 deletions src/types/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ export interface GroupedTopic {
title?: string;
}

export interface ChatTopicMetadata {
model?: string;
provider?: string;
}

export interface ChatTopic extends Omit<BaseDataModel, 'meta'> {
favorite?: boolean;
metadata?: ChatTopicMetadata | null;
sessionId?: string;
summary?: string;
title: string;
}

Expand Down

0 comments on commit 5bfb9ed

Please sign in to comment.