zhaoyingbo 9545670a51
All checks were successful
Egg Server CI/CD / build-image (push) Successful in 45s
Egg Server CI/CD / refresh-image (push) Successful in 12s
Egg Server CI/CD / fast-deploy (push) Successful in 2s
feat: 支持清洗历史数据
2024-10-15 01:20:21 +00:00

182 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { genCardOptions } from "@egg/lark-msg-tool"
import { Context } from "../../../types"
import llm from "../../../utils/llm"
import getChatHistory from "./chatHistory"
/**
* 发送群组选择器
* @param ctx - 上下文数据包含body和larkService
*/
const sendGroupSelector = async ({
larkService,
logger,
larkCard,
larkBody: { chatId },
}: Context.Data) => {
const cardGender = larkCard.child("groupAgent")
const { data: innerList } = await larkService.chat.getInnerList()
logger.info(`Inner list: ${JSON.stringify(innerList)}`)
// 组织群组数据
const groups = innerList.map((v) => ({
text: v.name,
value: `${v.chat_id}|${v.name}`,
}))
larkService.message.sendCard2Chat(
chatId,
cardGender.genTempCard("groupSelector", { groups }) as string
)
}
/**
* 发送功能选择器
* @param ctx - 上下文数据
*/
const sendFunctionSelector = async ({
logger,
larkCard,
larkBody: { actionOption },
}: Context.Data) => {
const cardGender = larkCard.child("groupAgent", false)
logger.debug(`Action option: ${JSON.stringify(actionOption)}`)
const [chatId, chatName] = (actionOption ?? "").split("|")
if (!chatId || !chatName) {
logger.error(
`Invalid targetChatId or targetChatName: ${JSON.stringify(actionOption)}`
)
return cardGender.genErrorCard("Invalid targetChatId or targetChatName")
}
return cardGender.genCard("functionSelector", {
functions: genCardOptions({
: "summary-qwen-72b-instruct-int4|总结消息",
}),
chatId,
chatName,
})
}
/**
* 发送时间范围选择器
* @param ctx - 上下文数据包含body和larkService
*/
const sendTimeScopeSelector = async ({
logger,
larkCard,
larkBody: { actionOption, actionValue },
}: Context.Data) => {
const cardGender = larkCard.child("groupAgent", false)
logger.debug(`Action option: ${JSON.stringify(actionOption)}`)
const [functionId, functionName] = (actionOption ?? "").split("|")
if (!functionId || !functionName) {
logger.error(
`Invalid functionId or functionName: ${JSON.stringify(actionOption)}`
)
return cardGender.genErrorCard("Invalid functionId or functionName")
}
logger.debug(`Action value: ${JSON.stringify(actionValue)}`)
const { chatId, chatName } = actionValue
if (!chatName || !chatId) {
logger.error(`Invalid chatName or chatId: ${JSON.stringify(actionValue)}`)
return cardGender.genErrorCard("Invalid chatName or chatId")
}
return cardGender.genCard("timeScopeSelector", {
chatId,
chatName,
functionId,
functionName,
})
}
const sendGroupReport = async (ctx: Context.Data) => {
const {
larkService,
logger,
requestId,
larkCard,
larkBody: { actionValue, messageId },
} = ctx
const cardGender = larkCard.child("groupAgent")
const { chatName, functionId, functionName, timeScope } = actionValue
// 记录发送loading消息后的时间戳
const startTime = Date.now()
// 获取聊天记录
const chatHistory = await getChatHistory(ctx)
// 如果没有历史记录则返回错误消息
if (chatHistory.length === 0) {
logger.error("Chat history is empty")
await larkService.message.update(
messageId,
cardGender.genErrorCard("未找到聊天记录")
)
return
}
logger.debug(`Chat history: ${JSON.stringify(chatHistory)}`)
try {
const llmRes = await llm.invoke(functionId, {
chatHistory: JSON.stringify(chatHistory),
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
})
// 记录大模型返回结果后的时间戳
const endTime = Date.now()
// 计算时间差并存储在processingTime变量中以秒为单位
const processingTime = ((endTime - startTime) / 1000).toFixed(2)
logger.info(`LLM takes time: ${processingTime}s, result: ${llmRes}`)
// 更新消息卡片
await larkService.message.update(
messageId,
cardGender.genCard("resultReport", {
chatName,
functionName,
llmRes,
timeScope,
requestId,
processingTime,
})
)
} catch (error: any) {
logger.error(`LLM error: ${error.message}`)
await larkService.message.update(
messageId,
cardGender.genErrorCard("LLM调用失败: " + error.message)
)
}
}
/**
* 发送结果报告
* @param ctx - 上下文数据包含body和larkService
*/
const manageGroupMsg = async (ctx: Context.Data) => {
const {
logger,
larkCard,
larkBody: { actionValue },
} = ctx
const cardGender = larkCard.child("groupAgent", false)
logger.debug(`Action value: ${JSON.stringify(actionValue)}`)
const { chatId, chatName, functionId, functionName, timeScope } = actionValue
if (!chatId || !chatName || !functionId || !functionName || !timeScope) {
logger.error(`Invalid value: ${JSON.stringify(actionValue)}`)
return cardGender.genErrorCard("Invalid value")
}
// 处理群组消息
sendGroupReport(ctx)
// 发送一个loading的消息
return cardGender.genPendingCard("正在爬楼中,请稍等...")
}
const groupAgent = {
sendGroupSelector,
sendFunctionSelector,
sendTimeScopeSelector,
manageGroupMsg,
}
export default groupAgent