feat: 优化消息回复逻辑,统一使用更新回复消息的方法;更新相关卡片内容

This commit is contained in:
zhaoyingbo 2025-01-24 08:51:52 +00:00
parent 7d71b342d1
commit 5c356f31f3
5 changed files with 20 additions and 36 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -12,21 +12,15 @@ const agent = async (ctx: Context) => {
larkBody: { messageId, msgText, chatId, mentions, rawMsgText, openId },
} = ctx
const cardGender = larkCard.child("groupAgent")
// 回复一个loading的卡片
const {
data: { message_id },
} = await larkService.message.replyCard(
messageId,
const replyCard = larkService.message.updateReplyMessage(messageId)
const loadingMessageId = await replyCard(
cardGender.genPendingCard("分析中,请稍等...")
)
const updateCard = (content: any) =>
larkService.message.update(message_id, content)
// 使用大模型解析用户输入
const { startTime, endTime } = await llm.timeParser(msgText, requestId)
logger.info(`Parsed time: startTime: ${startTime}, endTime: ${endTime}`)
// 更新卡片
await updateCard(cardGender.genPendingCard("正在爬楼中,请稍等..."))
await replyCard(cardGender.genPendingCard("正在爬楼中,请稍等..."))
// 获取聊天记录
const { messages: chatHistory, mentions: historyMentions } =
await getChatHistory(ctx, {
@ -35,13 +29,14 @@ const agent = async (ctx: Context) => {
endTime,
mentions,
senderOpenId: openId,
excludedMessageIds: [message_id, messageId],
excludedMessageIds: [loadingMessageId, messageId],
excludeMentions: [appInfo.appName],
})
// 如果没有聊天记录,返回错误信息
if (chatHistory.length === 0) {
logger.info("No chat history found")
return await updateCard(cardGender.genErrorCard("未找到聊天记录"))
await replyCard(cardGender.genErrorCard("未找到聊天记录"))
return
}
logger.debug(`Chat history: ${JSON.stringify(chatHistory)}`)
@ -57,7 +52,7 @@ const agent = async (ctx: Context) => {
// 调用大模型
try {
await updateCard(cardGender.genPendingCard("LLM输出中请稍等..."))
await replyCard(cardGender.genPendingCard("LLM输出中请稍等..."))
const llmRes = (await llm.invoke(
"groupAgent",
{
@ -74,10 +69,10 @@ const agent = async (ctx: Context) => {
const cleanedLlmRes = llmRes
.replace(/```(\w+)?\n([\s\S]*?)```/g, "$2")
.trim()
await updateCard(cardGender.genSuccessCard(cleanedLlmRes))
await replyCard(cardGender.genSuccessCard(cleanedLlmRes))
} catch (error: any) {
logger.error(`Failed to invoke llm: ${error.message}`)
await updateCard(cardGender.genErrorCard("LLM调用失败: " + error.message))
await replyCard(cardGender.genErrorCard("LLM调用失败: " + error.message))
}
}

View File

@ -21,18 +21,11 @@ const genSummary = async (
const { logger, requestId, larkCard, larkService, appInfo, larkBody } = ctx
logger.info(`genSummary ${timeScope} by ${trigger}`)
const cardGender = larkCard.child("groupAgent")
let loadingMessageId = ""
const replyCard = larkService.message.updateReplyMessage(larkBody.messageId)
try {
// 手动发送先发一个Loading卡片
const res = await larkService.message.replyCard(
larkBody.messageId,
cardGender.genPendingCard("总结中,请稍等...")
)
if ("data" in res) {
loadingMessageId = res.data.message_id
if (trigger === "manual") {
await replyCard(cardGender.genPendingCard("总结中,请稍等..."))
}
// 获取群聊信息
const chat = await db.chat.getAndCreate(ctx)
if (!chat) {
@ -92,8 +85,8 @@ const genSummary = async (
})
// 发送卡片消息,手动触发时回复原消息
if (loadingMessageId) {
await larkService.message.update(loadingMessageId, cardContent)
if (trigger === "manual") {
await replyCard(cardContent)
} else {
await larkService.message.sendCard2Chat(chatId, cardContent)
}
@ -111,11 +104,7 @@ const genSummary = async (
)
// 手动触发时回复原消息
if (trigger === "manual") {
if (loadingMessageId) {
await larkService.message.update(loadingMessageId, errorCard)
} else {
await larkService.message.replyCard(larkBody.messageId, errorCard)
}
await replyCard(errorCard)
}
// 自动触发发送给自己的订阅群
else {

View File

@ -143,14 +143,13 @@ const chat2Soup = async (ctx: Context) => {
larkService,
} = ctx
const cardGender = larkCard.child("soupAgent")
const replyCard = larkService.message.updateReplyMessage(messageId, "text")
const activeGame = await db.soupGame.getActiveOneByChatId(chatId)
if (!activeGame) {
logger.info(`chatId: ${chatId} has no active game`)
return
}
const {
data: { message_id },
} = await larkService.message.reply(messageId, "text", "模型生成中...")
await replyCard("模型生成中...")
const res = await attachService.chat2Soup({
user_query: msgText,
@ -175,8 +174,9 @@ const chat2Soup = async (ctx: Context) => {
...activeGame.history,
msgText,
])
// 回复用户模型的消息
await larkService.message.update(message_id, res.content, true)
await replyCard(res.content)
}
/**

View File

@ -39,7 +39,7 @@
"@egg/hooks": "^1.2.0",
"@egg/lark-msg-tool": "^1.21.0",
"@egg/logger": "^1.6.0",
"@egg/net-tool": "^1.25.0",
"@egg/net-tool": "^1.27.0",
"@egg/path-tool": "^1.4.1",
"@langchain/core": "^0.3.33",
"@langchain/langgraph": "^0.2.41",