feat: 添加排除提到的人和消息ID的功能以优化聊天记录获取逻辑

This commit is contained in:
zhaoyingbo 2024-12-04 09:54:10 +00:00
parent 0a3d46e0b4
commit 553f212cc8
4 changed files with 34 additions and 11 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -9,7 +9,15 @@ const agent = async (ctx: Context.Data) => {
larkCard, larkCard,
larkService, larkService,
appInfo, appInfo,
larkBody: { messageId, msgText, chatId, mentions, rawMsgText, openId }, larkBody: {
messageId,
msgText,
chatId,
mentions,
rawMsgText,
openId,
mentionedRobot,
},
} = ctx } = ctx
const cardGender = larkCard.child("groupAgent") const cardGender = larkCard.child("groupAgent")
// 回复一个loading的卡片 // 回复一个loading的卡片
@ -36,6 +44,7 @@ const agent = async (ctx: Context.Data) => {
mentions, mentions,
senderOpenId: openId, senderOpenId: openId,
excludedMessageIds: [message_id, messageId], excludedMessageIds: [message_id, messageId],
excludeMentions: [mentionedRobot?.id?.open_id || ""],
}) })
// 如果没有聊天记录,返回错误信息 // 如果没有聊天记录,返回错误信息
if (chatHistory.length === 0) { if (chatHistory.length === 0) {

View File

@ -50,14 +50,16 @@ const getChatHistory = async (
endTime, endTime,
senderOpenId, senderOpenId,
mentions: targetUsers, mentions: targetUsers,
excludedMessageIds = [], // 新增参数 excludedMessageIds = [],
excludeMentions = [],
}: { }: {
chatId: string chatId: string
startTime: string startTime: string
endTime: string endTime: string
senderOpenId?: string senderOpenId?: string
mentions?: LarkEvent.Mention[] mentions?: LarkEvent.Mention[]
excludedMessageIds?: string[] // 新增参数类型 excludedMessageIds?: string[] // 不需要的消息 ID
excludeMentions?: string[] // 不需要的提到的人
} }
): Promise<{ ): Promise<{
messages: Message[] messages: Message[]
@ -108,15 +110,27 @@ const getChatHistory = async (
// 遍历历史消息 // 遍历历史消息
for (const chat of chatHistory) { for (const chat of chatHistory) {
// 过滤掉不需要的消息 ID let hasExcludeMention = false
if (excludedMessageIds.includes(chat.message_id)) {
continue
}
if (chat.mentions) { if (chat.mentions) {
for (const mention of chat.mentions) { for (const mention of chat.mentions) {
mentions.set(mention.id, mention.name) mentions.set(mention.id, mention.name)
// 过滤掉不需要的提到的人
if (excludeMentions.includes(mention.id)) {
hasExcludeMention = true
}
} }
} }
// 如果提到了不要包含的人,则跳过该消息
if (hasExcludeMention) {
continue
}
// 过滤掉不需要的消息 ID
if (excludedMessageIds.includes(chat.message_id)) {
continue
}
// 过滤掉不是文本和post消息的消息 // 过滤掉不是文本和post消息的消息
if (!allowedMsgTypes.includes(chat.msg_type)) { if (!allowedMsgTypes.includes(chat.msg_type)) {
continue continue

View File

@ -29,20 +29,20 @@
"husky": "^9.1.7", "husky": "^9.1.7",
"lint-staged": "^15.2.10", "lint-staged": "^15.2.10",
"oxlint": "^0.13.2", "oxlint": "^0.13.2",
"prettier": "^3.4.1", "prettier": "^3.4.2",
"prisma": "5.22.0", "prisma": "5.22.0",
"typescript-eslint": "^8.16.0" "typescript-eslint": "^8.17.0"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.5.4" "typescript": "^5.5.4"
}, },
"dependencies": { "dependencies": {
"@egg/hooks": "^1.2.0", "@egg/hooks": "^1.2.0",
"@egg/lark-msg-tool": "^1.18.0", "@egg/lark-msg-tool": "^1.19.0",
"@egg/logger": "^1.6.0", "@egg/logger": "^1.6.0",
"@egg/net-tool": "^1.19.0", "@egg/net-tool": "^1.19.0",
"@egg/path-tool": "^1.4.1", "@egg/path-tool": "^1.4.1",
"@langchain/core": "^0.3.19", "@langchain/core": "^0.3.20",
"@langchain/openai": "^0.3.14", "@langchain/openai": "^0.3.14",
"@prisma/client": "5.22.0", "@prisma/client": "5.22.0",
"joi": "^17.13.3", "joi": "^17.13.3",