From 5c356f31f3703f1ac8ad67b4d9c5e16717fcf7c3 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Fri, 24 Jan 2025 08:51:52 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E9=80=BB=E8=BE=91=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=9B=B4=E6=96=B0=E5=9B=9E=E5=A4=8D=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=9B=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=8D=A1=E7=89=87=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bun.lockb | Bin 166163 -> 166163 bytes controller/groupAgent/agent.ts | 23 +++++++++-------------- controller/groupAgent/report.ts | 23 ++++++----------------- controller/soupAgent/index.ts | 8 ++++---- package.json | 2 +- 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/bun.lockb b/bun.lockb index 2fa7c0438869b7def276aab9112c16ee111067f2..fe556d2de7961d780ca45fe3a0d03a8357732720 100755 GIT binary patch delta 215 zcmV;|04V>Hk_wZO3Xm=!%MB17c&E-)`{Wppic RZ*OcZF)lJUxAPHk_wZO3Xm=!o*TaDnI<224Qg17c$E-)`{Wppic RZ*OcZF)lJSxAPQ!PniG! diff --git a/controller/groupAgent/agent.ts b/controller/groupAgent/agent.ts index 17883da..262eed4 100644 --- a/controller/groupAgent/agent.ts +++ b/controller/groupAgent/agent.ts @@ -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)) } } diff --git a/controller/groupAgent/report.ts b/controller/groupAgent/report.ts index be45992..2461fde 100644 --- a/controller/groupAgent/report.ts +++ b/controller/groupAgent/report.ts @@ -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 { diff --git a/controller/soupAgent/index.ts b/controller/soupAgent/index.ts index 357438c..2c82d21 100644 --- a/controller/soupAgent/index.ts +++ b/controller/soupAgent/index.ts @@ -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) } /** diff --git a/package.json b/package.json index 6c9294f..4c0db4d 100644 --- a/package.json +++ b/package.json @@ -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",