zhaoyingbo b992ee0b21
Some checks failed
Egg Server MIflow / build-image (push) Failing after 5m7s
feat(group-agent): 新增支持群组问答
2024-09-25 09:14:10 +00:00

142 lines
3.5 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 { LarkServer } from "../../types"
import LarkBaseService from "./base"
class LarkDriveService extends LarkBaseService {
/**
* 批量获取文档元数据。
*
* @param docTokens - 文档令牌数组。
* @param docType - 文档类型,默认为 "doc"。
* @param userIdType - 用户ID类型默认为 "user_id"。
* @returns 包含元数据和失败列表的响应对象。
*/
async batchGetMeta(
docTokens: string[],
docType = "doc",
userIdType = "user_id"
) {
const path = "/drive/v1/metas/batch_query"
// 如果docTokens长度超出150需要分批请求
const docTokensLen = docTokens.length
const maxLen = 150
const requestMap = Array.from(
{ length: Math.ceil(docTokensLen / maxLen) },
(_, index) => {
const start = index * maxLen
const docTokensSlice = docTokens.slice(start, start + maxLen)
const data = {
request_docs: docTokensSlice.map((id) => ({
doc_token: id,
doc_type: docType,
})),
}
return this.post<LarkServer.BatchDocMetaRes>(path, data, {
user_id_type: userIdType,
})
}
)
const responses = await Promise.all(requestMap)
const metas = responses.flatMap((res) => {
return res.data?.metas || []
})
const failedList = responses.flatMap((res) => {
return res.data?.failed_list || []
})
return {
code: 0,
data: {
metas,
failedList,
},
message: "success",
}
}
/**
* 获取文件列表。
*
* @param folderToken - 文件夹令牌。
* @returns 包含文件列表的响应对象。
*/
async listFiles(folderToken: string) {
const path = "/drive/v1/files"
return this.get<LarkServer.BaseRes>(path, {
folder_token: folderToken,
})
}
/**
* 创建文件。
*
* @param folderToken - 文件夹令牌。
* @param fileName - 文件名。
* @param fileType - 文件类型。
* @returns 包含响应数据的Promise。
*/
async createFile(
folderToken: string,
fileName: string,
fileType: "doc" | "sheet" | "bitable"
) {
const path = `/drive/explorer/v2/file/${folderToken}`
return this.post<LarkServer.BaseRes>(path, {
title: fileName,
type: fileType,
})
}
/**
* 复制文件。
*
* @param folderToken - 文件夹令牌。
* @param fileToken - 文件令牌。
* @param fileName - 文件名。
* @param fileType - 文件类型。
* @returns 包含响应数据的Promise。
*/
async copyFile(
folderToken: string,
fileToken: string,
fileName: string,
fileType: LarkServer.FileType
) {
const path = `/drive/v1/files/${fileToken}/copy`
return this.post<LarkServer.BaseRes<LarkServer.CopyFileData>>(path, {
type: fileType,
folder_token: folderToken,
name: fileName,
})
}
/**
* 获取文件元数据。
*
* @param fileToken - 文件令牌。
* @returns 包含文件元数据的Promise。
*/
async addCollaborator(
fileToken: string,
fileType: LarkServer.FileType,
memberType: "userid" | "openchat",
memberId: string,
perm: "view" | "edit" | "full_access"
) {
const path = `/drive/v1/permissions/${fileToken}/members`
return this.post<LarkServer.BaseRes>(
path,
{
member_type: memberType,
member_id: memberId,
perm,
},
{
type: fileType,
}
)
}
}
export default LarkDriveService