feat: 接入egg_chat_agent

This commit is contained in:
zhaoyingbo 2024-11-27 07:13:39 +00:00
parent f0d481629b
commit a8dd0194b5
6 changed files with 70 additions and 17 deletions

View File

@ -27,7 +27,6 @@
"ChakrounAnas.turbo-console-log",
"streetsidesoftware.code-spell-checker",
"MS-CEINTL.vscode-language-pack-zh-hans",
"Prisma.prisma",
"humao.rest-client",
"GitHub.copilot",
"GitHub.copilot-chat",

View File

@ -11,3 +11,5 @@ LANGFUSE_BASE_URL=
# Map of apps such as '{"michat": { "app_id": "123", "app_secret": "456", "app_name": "Mi Chat" }}'
LARK_APP_MAP=
ATTACH_APP_SECRET=

BIN
bun.lockb

Binary file not shown.

View File

@ -22,29 +22,29 @@
"@eslint/js": "^9.15.0",
"@types/node-schedule": "^2.1.7",
"@types/uuid": "^10.0.0",
"bun-types": "^1.1.36",
"bun-types": "^1.1.37",
"eslint": "^9.15.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-unused-imports": "^4.1.4",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"oxlint": "^0.13.1",
"prettier": "^3.3.3",
"typescript-eslint": "^8.15.0"
"oxlint": "^0.13.2",
"prettier": "^3.4.1",
"typescript-eslint": "^8.16.0"
},
"peerDependencies": {
"typescript": "^5.5.4"
},
"dependencies": {
"@egg/hooks": "^1.2.0",
"@egg/lark-msg-tool": "^1.14.0",
"@egg/logger": "^1.4.4",
"@egg/net-tool": "^1.14.2",
"@egg/lark-msg-tool": "^1.15.0",
"@egg/logger": "^1.5.0",
"@egg/net-tool": "^1.16.0",
"@egg/path-tool": "^1.4.1",
"@langchain/core": "^0.3.18",
"@langchain/core": "^0.3.19",
"@langchain/openai": "^0.3.14",
"joi": "^17.13.3",
"langfuse-langchain": "^3.30.3",
"langfuse-langchain": "^3.31.0",
"node-schedule": "^2.1.1",
"p-limit": "^6.1.0",
"pocketbase": "^0.21.5",

View File

@ -1,4 +1,4 @@
import { type LarkBody } from "@egg/lark-msg-tool"
import { LarkBody } from "@egg/lark-msg-tool"
import tempMap from "../../constant/template"
import { Context } from "../../types"
@ -114,12 +114,12 @@ const manageCMDMsg = (ctx: Context.Data) => {
logger,
larkService,
attachService,
larkBody: { msgText, chatId },
larkBody: { msgText, chatId, chatType },
} = ctx
logger.info(`bot req text: ${msgText}`)
// 处理命令消息
if (msgText.trim() === "/id") {
if (msgText === "/id") {
logger.info(`bot command is /id, chatId: ${chatId}`)
manageIdMsg(ctx)
return
@ -127,6 +127,33 @@ const manageCMDMsg = (ctx: Context.Data) => {
// michat专属功能
if (app === "michat") {
// 仅限群组功能
if (chatType === "group") {
// 注册群组
if (msgText === "开启日报、周报") {
logger.info(`bot command is register, chatId: ${chatId}`)
attachService.groupAgent(app, body, "register")
return
}
// 注销群组
if (msgText === "关闭日报、周报") {
logger.info(`bot command is unregister, chatId: ${chatId}`)
attachService.groupAgent(app, body, "unregister")
return
}
// 立即发送日简报
if (msgText === "发送日报") {
logger.info(`bot command is summary, chatId: ${chatId}`)
attachService.groupAgent(app, body, "summary", "daily")
return
}
// 立即发送周简报
if (msgText === "发送周报") {
logger.info(`bot command is summary, chatId: ${chatId}`)
attachService.groupAgent(app, body, "summary", "weekly")
return
}
}
// 帮助
logger.info(`bot command is /help, chatId: ${chatId}`)
manageHelpMsg(ctx, "miChatGuide")
@ -136,7 +163,7 @@ const manageCMDMsg = (ctx: Context.Data) => {
// 小煎蛋专属功能
if (app === "egg") {
// CI监控
if (msgText.trim() === "/ci") {
if (msgText === "/ci") {
logger.info(`bot command is /ci, chatId: ${chatId}`)
attachService.ciMonitor(chatId)
return
@ -156,21 +183,21 @@ const manageCMDMsg = (ctx: Context.Data) => {
return
}
// 创建Sheet DB
if (msgText.trim() === "/gen db") {
if (msgText === "/gen db") {
logger.info(`bot command is /gen db, chatId: ${chatId}`)
createKVTemp.createFromEvent(ctx)
return
}
// 选择群组信息
if (msgText.trim().startsWith("/g")) {
if (msgText.startsWith("/g")) {
logger.info(`bot command is /groupchat, chatId: ${chatId}`)
groupAgent(ctx)
return
}
// 帮助
if (msgText.trim() === "/help") {
if (msgText === "/help") {
logger.info(`bot command is /help, chatId: ${chatId}`)
manageHelpMsg(ctx, "eggGuide")
return

View File

@ -2,6 +2,12 @@ import type { LarkEvent } from "@egg/lark-msg-tool"
import { NetToolBase } from "@egg/net-tool"
class AttachService extends NetToolBase {
protected hostMap: Record<string, string> = {
dev: "lark-egg-preview.ai.xiaomi.com",
preview: "lark-egg-preview.ai.xiaomi.com",
production: "lark-egg.ai.xiaomi.com",
}
/**
* CI状态
* @param {string} chat_id - ID
@ -35,6 +41,25 @@ class AttachService extends NetToolBase {
await this.post(URL, body).catch(() => "")
}
}
/**
* MiChat事件
* @param {LarkEvent.Data} body -
* @returns {Promise<void>}
*/
async groupAgent(
app: string,
body: LarkEvent.Data,
func: "register" | "unregister" | "summary",
timeScope?: "daily" | "weekly"
) {
const URL = `${this.hostMap[Bun.env.NODE_ENV!]}/chat_agent/${func}`
return this.post(
URL,
{ body },
{ timeScope, app, secret: Bun.env.ATTACH_APP_SECRET }
).catch(() => "")
}
}
export default AttachService