140 lines
3.6 KiB
TypeScript
140 lines
3.6 KiB
TypeScript
import type { LarkEvent } from "@egg/lark-msg-tool"
|
|
import { NetToolBase } from "@egg/net-tool"
|
|
|
|
import { APP_CONFIG } from "../../constant/config"
|
|
import { LarkServer } from "../../types"
|
|
|
|
interface Chat2SoupParams {
|
|
user_query: string
|
|
soup_id: string
|
|
history: string
|
|
}
|
|
|
|
interface Chat2SoupResp {
|
|
type: "GAME" | "END" | "OTHER"
|
|
content: string
|
|
}
|
|
|
|
interface Soup {
|
|
/**
|
|
* 海龟汤ID
|
|
*/
|
|
title: string
|
|
/**
|
|
* 海龟汤内容
|
|
*/
|
|
query: string
|
|
/**
|
|
* 海龟汤答案
|
|
*/
|
|
answer: string
|
|
}
|
|
|
|
class AttachService extends NetToolBase {
|
|
protected hostMap: Record<string, string> = {
|
|
dev: "https://lark-egg-preview.ai.xiaomi.com",
|
|
preview: "https://lark-egg-preview.ai.xiaomi.com",
|
|
production: "https://lark-egg.ai.xiaomi.com",
|
|
}
|
|
|
|
/**
|
|
* 监控CI状态
|
|
* @param {string} chat_id - 聊天ID。
|
|
* @returns {Promise<string>} 返回CI监控结果。
|
|
*/
|
|
async ciMonitor(chat_id: string) {
|
|
const URL = `https://lark-egg.ai.xiaomi.com/gitlab_monitor/ci?chat_id=${chat_id}`
|
|
return this.get(URL).catch(() => "")
|
|
}
|
|
|
|
/**
|
|
* 收集报告数据
|
|
* @param {LarkEvent.Data} body - 报告数据。
|
|
* @returns {Promise<string>} 返回报告收集结果。
|
|
*/
|
|
async reportCollector(body: LarkEvent.Data) {
|
|
const URL = "https://report.imoaix.cn/report"
|
|
return this.post(URL, body).catch(() => "")
|
|
}
|
|
|
|
/**
|
|
* 代理MiChat事件
|
|
* @param {LarkEvent.Data} body - 事件数据。
|
|
* @returns {Promise<void>} 返回空。
|
|
*/
|
|
async proxyMiChatEvent(body: LarkEvent.Data) {
|
|
const hostList = ["michat-staging.ai.srv", "michat.ai.srv"]
|
|
const path = "/api/v1/bypass/robot_event_callback"
|
|
for (const host of hostList) {
|
|
const URL = `http://${host}${path}`
|
|
await this.post(URL, body).catch(() => "")
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 开始海龟汤
|
|
*/
|
|
async startSoup() {
|
|
const URL = "https://lark-egg.ai.xiaomi.com/soup"
|
|
return this.post<Soup>(URL, {}).catch(() => null)
|
|
}
|
|
|
|
/**
|
|
* 和海龟汤聊天
|
|
* @param {Chat2SoupParams} body - 聊天参数
|
|
*/
|
|
async chat2Soup(body: Chat2SoupParams) {
|
|
const URL = "https://lark-egg.ai.xiaomi.com/soup/chat"
|
|
return this.post<Chat2SoupResp>(URL, body).catch(() => null)
|
|
}
|
|
|
|
/**
|
|
* 抓取网页内容
|
|
* @param {string} url - 网页URL。
|
|
* @returns {Promise<string>} 返回抓取的网页内容。
|
|
*/
|
|
async crawlWeb(url: string) {
|
|
const URL = "https://lark-egg.ai.xiaomi.com/tools/web/crawler"
|
|
return this.get<LarkServer.BaseRes<string>>(URL, { url }).catch(() => null)
|
|
}
|
|
|
|
/**
|
|
* 使用mify爬虫抓取网页内容
|
|
* @param {string} link - 网页链接
|
|
* @param {string} userDescription - 用户描述
|
|
* @returns {Promise<any>} 返回爬取结果
|
|
*/
|
|
async mifyCrawler(link: string, userDescription: string) {
|
|
const URL = "https://mify-be.pt.xiaomi.com/api/v1/workflows/run"
|
|
return this.post(
|
|
URL,
|
|
{
|
|
inputs: {
|
|
link,
|
|
userDescription,
|
|
},
|
|
response_mode: "blocking",
|
|
user: "egg-server",
|
|
},
|
|
{},
|
|
{
|
|
Authorization: `Bearer ${APP_CONFIG.MIFY_CRAWLER_TOKEN}`,
|
|
}
|
|
)
|
|
.then((res) => {
|
|
const llmRes = res.data.outputs.content
|
|
if (!llmRes) throw new Error("模型总结失败")
|
|
if (llmRes === "crawlerErr") throw new Error("网页抓取失败")
|
|
return llmRes as string
|
|
})
|
|
.catch((error) => {
|
|
if (["网页抓取失败", "模型总结失败"].includes(error.message)) {
|
|
throw error
|
|
}
|
|
throw new Error("MIFY爬虫请求失败")
|
|
})
|
|
}
|
|
}
|
|
|
|
export default AttachService
|