diff --git a/controller/groupAgent/report.ts b/controller/groupAgent/report.ts index 4827aa9..be45992 100644 --- a/controller/groupAgent/report.ts +++ b/controller/groupAgent/report.ts @@ -1,7 +1,7 @@ import { RespMessage } from "../../constant/message" import db from "../../db" import { Context } from "../../types" -import genContext from "../../utils/genContext" +import { genContextManually } from "../../utils/genContext" import llm from "../../utils/llm" import { getTimeRange } from "../../utils/time" import getChatHistory from "./chatHistory" @@ -24,13 +24,14 @@ const genSummary = async ( let loadingMessageId = "" try { // 手动发送先发一个Loading卡片 - const { - data: { message_id: messageId }, - } = await larkService.message.replyCard( + const res = await larkService.message.replyCard( larkBody.messageId, cardGender.genPendingCard("总结中,请稍等...") ) - loadingMessageId = messageId + + if ("data" in res) { + loadingMessageId = res.data.message_id + } // 获取群聊信息 const chat = await db.chat.getAndCreate(ctx) @@ -91,8 +92,8 @@ const genSummary = async ( }) // 发送卡片消息,手动触发时回复原消息 - if (trigger === "manual") { - await larkService.message.update(messageId, cardContent) + if (loadingMessageId) { + await larkService.message.update(loadingMessageId, cardContent) } else { await larkService.message.sendCard2Chat(chatId, cardContent) } @@ -128,9 +129,7 @@ const genSummary = async ( * @returns {Promise} */ const genAllReport = async (timeScope: "daily" | "weekly" = "daily") => { - const ctx = await genContext( - new Request("https://lark-egg-preview.ai.xiaomi.com") - ) + const ctx = await genContextManually() const { logger } = ctx logger.info(`genAllReport ${timeScope}`) try { @@ -143,9 +142,7 @@ const genAllReport = async (timeScope: "daily" | "weekly" = "daily") => { } // 总结 for (const chat of chatList) { - const newCtx = await genContext( - new Request("https://lark-egg-preview.ai.xiaomi.com") - ) + const newCtx = await genContextManually() newCtx.larkBody.chatId = chat.chatId let scope = "daily" as "daily" | "weekly" if (timeScope === "weekly" && chat.weeklySummary) { diff --git a/utils/genContext.ts b/utils/genContext.ts index 36b018e..f7cebb2 100644 --- a/utils/genContext.ts +++ b/utils/genContext.ts @@ -31,7 +31,7 @@ const getPreRequestId = (larkBody: LarkBody) => { * @param {Request} req - 请求对象。 * @returns {Promise} 返回包含请求上下文的对象。 */ -const genContext = async (req: Request) => { +const genContext = async (req: Request, rId?: string) => { let body: any = null let text: string = "" try { @@ -44,7 +44,7 @@ const genContext = async (req: Request) => { const searchParams = new URL(req.url).searchParams const app = searchParams.get("app") || "egg" const appInfo = APP_MAP[app] - const requestId = getPreRequestId(larkBody) || uuid() + const requestId = rId || getPreRequestId(larkBody) || uuid() const logger = loggerIns.child({ requestId }) const genResp = new NetTool({ requestId }) const larkService = genLarkService("egg", requestId) @@ -83,4 +83,10 @@ const genContext = async (req: Request) => { } as Context } +export const genContextManually = (rId?: string, app = "egg") => + genContext( + new Request("https://lark-egg-preview.ai.xiaomi.com?app=" + app), + rId + ) + export default genContext