mirror of
https://github.com/langgenius/dify.git
synced 2026-03-04 07:16:20 +08:00
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in>
224 lines
6.1 KiB
TypeScript
224 lines
6.1 KiB
TypeScript
import groupBy from 'lodash-es/groupBy'
|
|
import type { MutationOptions } from '@tanstack/react-query'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { createDocument, createFirstDocument, fetchDefaultProcessRule, fetchFileIndexingEstimate } from '../datasets'
|
|
import type { IndexingType } from '@/app/components/datasets/create/step-two'
|
|
import type { ChunkingMode, CrawlOptions, CrawlResultItem, CreateDocumentReq, CustomFile, DataSourceType, FileIndexingEstimateResponse, IndexingEstimateParams, NotionInfo, ProcessRule, ProcessRuleResponse, createDocumentResponse } from '@/models/datasets'
|
|
import type { DataSourceProvider, NotionPage } from '@/models/common'
|
|
|
|
export const getNotionInfo = (
|
|
notionPages: NotionPage[],
|
|
) => {
|
|
const workspacesMap = groupBy(notionPages, 'workspace_id')
|
|
const workspaces = Object.keys(workspacesMap).map((workspaceId) => {
|
|
return {
|
|
workspaceId,
|
|
pages: workspacesMap[workspaceId],
|
|
}
|
|
})
|
|
return workspaces.map((workspace) => {
|
|
return {
|
|
workspace_id: workspace.workspaceId,
|
|
pages: workspace.pages.map((page) => {
|
|
const { page_id, page_name, page_icon, type } = page
|
|
return {
|
|
page_id,
|
|
page_name,
|
|
page_icon,
|
|
type,
|
|
}
|
|
}),
|
|
}
|
|
}) as NotionInfo[]
|
|
}
|
|
|
|
export const getWebsiteInfo = (
|
|
opts: {
|
|
websiteCrawlProvider: DataSourceProvider
|
|
websiteCrawlJobId: string
|
|
websitePages: CrawlResultItem[]
|
|
crawlOptions?: CrawlOptions
|
|
},
|
|
) => {
|
|
const { websiteCrawlProvider, websiteCrawlJobId, websitePages, crawlOptions } = opts
|
|
return {
|
|
provider: websiteCrawlProvider,
|
|
job_id: websiteCrawlJobId,
|
|
urls: websitePages.map(page => page.source_url),
|
|
only_main_content: crawlOptions?.only_main_content,
|
|
}
|
|
}
|
|
|
|
type GetFileIndexingEstimateParamsOptionBase = {
|
|
docForm: ChunkingMode
|
|
docLanguage: string
|
|
indexingTechnique: IndexingType
|
|
processRule: ProcessRule
|
|
dataset_id: string
|
|
}
|
|
|
|
type GetFileIndexingEstimateParamsOptionFile = GetFileIndexingEstimateParamsOptionBase & {
|
|
dataSourceType: DataSourceType.FILE
|
|
files: CustomFile[]
|
|
}
|
|
|
|
const getFileIndexingEstimateParamsForFile = ({
|
|
docForm,
|
|
docLanguage,
|
|
dataSourceType,
|
|
files,
|
|
indexingTechnique,
|
|
processRule,
|
|
dataset_id,
|
|
}: GetFileIndexingEstimateParamsOptionFile): IndexingEstimateParams => {
|
|
return {
|
|
info_list: {
|
|
data_source_type: dataSourceType,
|
|
file_info_list: {
|
|
file_ids: files.map(file => file.id) as string[],
|
|
},
|
|
},
|
|
indexing_technique: indexingTechnique,
|
|
process_rule: processRule,
|
|
doc_form: docForm,
|
|
doc_language: docLanguage,
|
|
dataset_id,
|
|
}
|
|
}
|
|
|
|
export const useFetchFileIndexingEstimateForFile = (
|
|
options: GetFileIndexingEstimateParamsOptionFile,
|
|
mutationOptions: MutationOptions<FileIndexingEstimateResponse> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForFile(options))
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|
|
|
|
type GetFileIndexingEstimateParamsOptionNotion = GetFileIndexingEstimateParamsOptionBase & {
|
|
dataSourceType: DataSourceType.NOTION
|
|
notionPages: NotionPage[]
|
|
}
|
|
|
|
const getFileIndexingEstimateParamsForNotion = ({
|
|
docForm,
|
|
docLanguage,
|
|
dataSourceType,
|
|
notionPages,
|
|
indexingTechnique,
|
|
processRule,
|
|
dataset_id,
|
|
}: GetFileIndexingEstimateParamsOptionNotion): IndexingEstimateParams => {
|
|
return {
|
|
info_list: {
|
|
data_source_type: dataSourceType,
|
|
notion_info_list: getNotionInfo(notionPages),
|
|
},
|
|
indexing_technique: indexingTechnique,
|
|
process_rule: processRule,
|
|
doc_form: docForm,
|
|
doc_language: docLanguage,
|
|
dataset_id,
|
|
}
|
|
}
|
|
|
|
export const useFetchFileIndexingEstimateForNotion = (
|
|
options: GetFileIndexingEstimateParamsOptionNotion,
|
|
mutationOptions: MutationOptions<FileIndexingEstimateResponse> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForNotion(options))
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|
|
|
|
type GetFileIndexingEstimateParamsOptionWeb = GetFileIndexingEstimateParamsOptionBase & {
|
|
dataSourceType: DataSourceType.WEB
|
|
websitePages: CrawlResultItem[]
|
|
crawlOptions?: CrawlOptions
|
|
websiteCrawlProvider: DataSourceProvider
|
|
websiteCrawlJobId: string
|
|
}
|
|
|
|
const getFileIndexingEstimateParamsForWeb = ({
|
|
docForm,
|
|
docLanguage,
|
|
dataSourceType,
|
|
websitePages,
|
|
crawlOptions,
|
|
websiteCrawlProvider,
|
|
websiteCrawlJobId,
|
|
indexingTechnique,
|
|
processRule,
|
|
dataset_id,
|
|
}: GetFileIndexingEstimateParamsOptionWeb): IndexingEstimateParams => {
|
|
return {
|
|
info_list: {
|
|
data_source_type: dataSourceType,
|
|
website_info_list: getWebsiteInfo({
|
|
websiteCrawlProvider,
|
|
websiteCrawlJobId,
|
|
websitePages,
|
|
crawlOptions,
|
|
}),
|
|
},
|
|
indexing_technique: indexingTechnique,
|
|
process_rule: processRule,
|
|
doc_form: docForm,
|
|
doc_language: docLanguage,
|
|
dataset_id,
|
|
}
|
|
}
|
|
|
|
export const useFetchFileIndexingEstimateForWeb = (
|
|
options: GetFileIndexingEstimateParamsOptionWeb,
|
|
mutationOptions: MutationOptions<FileIndexingEstimateResponse> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
return fetchFileIndexingEstimate(getFileIndexingEstimateParamsForWeb(options))
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|
|
|
|
export const useCreateFirstDocument = (
|
|
mutationOptions: MutationOptions<createDocumentResponse, Error, CreateDocumentReq> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async (createDocumentReq: CreateDocumentReq,
|
|
) => {
|
|
return createFirstDocument({ body: createDocumentReq })
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|
|
|
|
export const useCreateDocument = (
|
|
datasetId: string,
|
|
mutationOptions: MutationOptions<createDocumentResponse, Error, CreateDocumentReq> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async (req: CreateDocumentReq) => {
|
|
return createDocument({ datasetId, body: req })
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|
|
|
|
export const useFetchDefaultProcessRule = (
|
|
mutationOptions: MutationOptions<ProcessRuleResponse, Error, string> = {},
|
|
) => {
|
|
return useMutation({
|
|
mutationFn: async (url: string) => {
|
|
return fetchDefaultProcessRule({ url })
|
|
},
|
|
...mutationOptions,
|
|
})
|
|
}
|