feat: 更新聊天记录获取逻辑,支持发送者ID并返回提及信息

This commit is contained in:
zhaoyingbo 2024-12-02 08:08:04 +00:00
parent c84901a680
commit de0067ec17
5 changed files with 33 additions and 12 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -9,7 +9,7 @@ const agent = async (ctx: Context.Data) => {
larkCard,
larkService,
appInfo,
larkBody: { messageId, msgText, chatId, mentions, rawMsgText },
larkBody: { messageId, msgText, chatId, mentions, rawMsgText, openId },
} = ctx
const cardGender = larkCard.child("groupAgent")
// 回复一个loading的卡片
@ -28,12 +28,14 @@ const agent = async (ctx: Context.Data) => {
// 更新卡片
updateCard(cardGender.genPendingCard("正在爬楼中,请稍等..."))
// 获取聊天记录
const chatHistory = await getChatHistory(ctx, {
chatId,
startTime,
endTime,
mentions,
})
const { messages: chatHistory, mentions: historyMentions } =
await getChatHistory(ctx, {
chatId,
startTime,
endTime,
mentions,
senderOpenId: openId,
})
// 如果没有聊天记录,返回错误信息
if (chatHistory.length === 0) {
logger.info("No chat history found")
@ -51,6 +53,10 @@ const agent = async (ctx: Context.Data) => {
}
}
// 获取发送者的名字
const userName = historyMentions.get(openId || "") ?? "用户"
userInput = `${userName}${userInput}`
// 调用大模型
try {
await updateCard(cardGender.genPendingCard("LLM激情输出中请稍等..."))

View File

@ -48,14 +48,19 @@ const getChatHistory = async (
chatId,
startTime,
endTime,
senderOpenId,
mentions: targetUsers,
}: {
chatId: string
startTime: string
endTime: string
senderOpenId?: string
mentions?: LarkEvent.Mention[]
}
): Promise<Message[]> => {
): Promise<{
messages: Message[]
mentions: Map<string, string>
}> => {
// 获取服务器的时区偏移量(以分钟为单位)
const serverTimezoneOffset = new Date().getTimezoneOffset()
// 上海时区的偏移量UTC+8以分钟为单位
@ -76,7 +81,12 @@ const getChatHistory = async (
String(startTimeTimestamp),
String(endTimeTimestamp)
)
if (chatHistory.length === 0) return []
if (chatHistory.length === 0)
return {
messages: [],
mentions: new Map(),
}
const targetUsersSet = new Set(
targetUsers
@ -88,6 +98,8 @@ const getChatHistory = async (
// 取出所有的被AT的人以及发送者
const mentions: Map<string, string> = new Map()
const senders: Set<string> = new Set()
// 先把提问者加进去
if (senderOpenId) senders.add(senderOpenId)
// 过滤出文本和post消息
const allowedMsgTypes = ["text", "post"]
const filteredMsg: typeof chatHistory = []
@ -194,7 +206,10 @@ const getChatHistory = async (
})
}
return messages
return {
messages,
mentions,
}
}
export default getChatHistory

View File

@ -49,7 +49,7 @@ const genReport = async (
const processStart = Date.now()
// 获取聊天记录
const chatHistory = await getChatHistory(
const { messages: chatHistory } = await getChatHistory(
{ larkService, logger } as Context.Data,
{
chatId,

View File

@ -38,7 +38,7 @@
},
"dependencies": {
"@egg/hooks": "^1.2.0",
"@egg/lark-msg-tool": "^1.17.0",
"@egg/lark-msg-tool": "^1.18.0",
"@egg/logger": "^1.6.0",
"@egg/net-tool": "^1.19.0",
"@egg/path-tool": "^1.4.1",