diff --git a/bun.lockb b/bun.lockb index 2fa7c04..fe556d2 100755 Binary files a/bun.lockb and b/bun.lockb differ 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",