feat: 修改上下文生成逻辑,支持手动请求ID;更新相关调用以使用新方法

This commit is contained in:
zhaoyingbo 2025-01-22 15:22:22 +00:00
parent d7bd5a9199
commit 4990ffb3a4
2 changed files with 18 additions and 15 deletions

View File

@ -1,7 +1,7 @@
import { RespMessage } from "../../constant/message" import { RespMessage } from "../../constant/message"
import db from "../../db" import db from "../../db"
import { Context } from "../../types" import { Context } from "../../types"
import genContext from "../../utils/genContext" import { genContextManually } from "../../utils/genContext"
import llm from "../../utils/llm" import llm from "../../utils/llm"
import { getTimeRange } from "../../utils/time" import { getTimeRange } from "../../utils/time"
import getChatHistory from "./chatHistory" import getChatHistory from "./chatHistory"
@ -24,13 +24,14 @@ const genSummary = async (
let loadingMessageId = "" let loadingMessageId = ""
try { try {
// 手动发送先发一个Loading卡片 // 手动发送先发一个Loading卡片
const { const res = await larkService.message.replyCard(
data: { message_id: messageId },
} = await larkService.message.replyCard(
larkBody.messageId, larkBody.messageId,
cardGender.genPendingCard("总结中,请稍等...") cardGender.genPendingCard("总结中,请稍等...")
) )
loadingMessageId = messageId
if ("data" in res) {
loadingMessageId = res.data.message_id
}
// 获取群聊信息 // 获取群聊信息
const chat = await db.chat.getAndCreate(ctx) const chat = await db.chat.getAndCreate(ctx)
@ -91,8 +92,8 @@ const genSummary = async (
}) })
// 发送卡片消息,手动触发时回复原消息 // 发送卡片消息,手动触发时回复原消息
if (trigger === "manual") { if (loadingMessageId) {
await larkService.message.update(messageId, cardContent) await larkService.message.update(loadingMessageId, cardContent)
} else { } else {
await larkService.message.sendCard2Chat(chatId, cardContent) await larkService.message.sendCard2Chat(chatId, cardContent)
} }
@ -128,9 +129,7 @@ const genSummary = async (
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
const genAllReport = async (timeScope: "daily" | "weekly" = "daily") => { const genAllReport = async (timeScope: "daily" | "weekly" = "daily") => {
const ctx = await genContext( const ctx = await genContextManually()
new Request("https://lark-egg-preview.ai.xiaomi.com")
)
const { logger } = ctx const { logger } = ctx
logger.info(`genAllReport ${timeScope}`) logger.info(`genAllReport ${timeScope}`)
try { try {
@ -143,9 +142,7 @@ const genAllReport = async (timeScope: "daily" | "weekly" = "daily") => {
} }
// 总结 // 总结
for (const chat of chatList) { for (const chat of chatList) {
const newCtx = await genContext( const newCtx = await genContextManually()
new Request("https://lark-egg-preview.ai.xiaomi.com")
)
newCtx.larkBody.chatId = chat.chatId newCtx.larkBody.chatId = chat.chatId
let scope = "daily" as "daily" | "weekly" let scope = "daily" as "daily" | "weekly"
if (timeScope === "weekly" && chat.weeklySummary) { if (timeScope === "weekly" && chat.weeklySummary) {

View File

@ -31,7 +31,7 @@ const getPreRequestId = (larkBody: LarkBody) => {
* @param {Request} req - * @param {Request} req -
* @returns {Promise<Context>} * @returns {Promise<Context>}
*/ */
const genContext = async (req: Request) => { const genContext = async (req: Request, rId?: string) => {
let body: any = null let body: any = null
let text: string = "" let text: string = ""
try { try {
@ -44,7 +44,7 @@ const genContext = async (req: Request) => {
const searchParams = new URL(req.url).searchParams const searchParams = new URL(req.url).searchParams
const app = searchParams.get("app") || "egg" const app = searchParams.get("app") || "egg"
const appInfo = APP_MAP[app] const appInfo = APP_MAP[app]
const requestId = getPreRequestId(larkBody) || uuid() const requestId = rId || getPreRequestId(larkBody) || uuid()
const logger = loggerIns.child({ requestId }) const logger = loggerIns.child({ requestId })
const genResp = new NetTool({ requestId }) const genResp = new NetTool({ requestId })
const larkService = genLarkService("egg", requestId) const larkService = genLarkService("egg", requestId)
@ -83,4 +83,10 @@ const genContext = async (req: Request) => {
} as Context } 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 export default genContext