use-segment.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { useMutation, useQuery } from '@tanstack/react-query'
  2. import { del, get, patch, post } from '../base'
  3. import type { CommonResponse } from '@/models/common'
  4. import type {
  5. BatchImportResponse,
  6. ChildChunkDetail,
  7. ChildSegmentsResponse,
  8. ChunkingMode,
  9. SegmentDetailModel,
  10. SegmentUpdater,
  11. SegmentsResponse,
  12. } from '@/models/datasets'
  13. const NAME_SPACE = 'segment'
  14. export const useSegmentListKey = [NAME_SPACE, 'chunkList']
  15. export const useChunkListEnabledKey = [NAME_SPACE, 'chunkList', { enabled: true }]
  16. export const useChunkListDisabledKey = [NAME_SPACE, 'chunkList', { enabled: false }]
  17. export const useChunkListAllKey = [NAME_SPACE, 'chunkList', { enabled: 'all' }]
  18. export const useSegmentList = (
  19. payload: {
  20. datasetId: string
  21. documentId: string
  22. params: {
  23. page: number
  24. limit: number
  25. keyword: string
  26. enabled: boolean | 'all' | ''
  27. }
  28. },
  29. disable?: boolean,
  30. ) => {
  31. const { datasetId, documentId, params } = payload
  32. const { page, limit, keyword, enabled } = params
  33. return useQuery<SegmentsResponse>({
  34. queryKey: [...useSegmentListKey, { datasetId, documentId, page, limit, keyword, enabled }],
  35. queryFn: () => {
  36. return get<SegmentsResponse>(`/datasets/${datasetId}/documents/${documentId}/segments`, { params })
  37. },
  38. enabled: !disable,
  39. })
  40. }
  41. export const useUpdateSegment = () => {
  42. return useMutation({
  43. mutationKey: [NAME_SPACE, 'update'],
  44. mutationFn: (payload: { datasetId: string; documentId: string; segmentId: string; body: SegmentUpdater }) => {
  45. const { datasetId, documentId, segmentId, body } = payload
  46. return patch<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`, { body })
  47. },
  48. })
  49. }
  50. export const useAddSegment = () => {
  51. return useMutation({
  52. mutationKey: [NAME_SPACE, 'add'],
  53. mutationFn: (payload: { datasetId: string; documentId: string; body: SegmentUpdater }) => {
  54. const { datasetId, documentId, body } = payload
  55. return post<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(`/datasets/${datasetId}/documents/${documentId}/segment`, { body })
  56. },
  57. })
  58. }
  59. export const useEnableSegment = () => {
  60. return useMutation({
  61. mutationKey: [NAME_SPACE, 'enable'],
  62. mutationFn: (payload: { datasetId: string; documentId: string; segmentIds: string[] }) => {
  63. const { datasetId, documentId, segmentIds } = payload
  64. const query = segmentIds.map(id => `segment_id=${id}`).join('&')
  65. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/segment/enable?${query}`)
  66. },
  67. })
  68. }
  69. export const useDisableSegment = () => {
  70. return useMutation({
  71. mutationKey: [NAME_SPACE, 'disable'],
  72. mutationFn: (payload: { datasetId: string; documentId: string; segmentIds: string[] }) => {
  73. const { datasetId, documentId, segmentIds } = payload
  74. const query = segmentIds.map(id => `segment_id=${id}`).join('&')
  75. return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/segment/disable?${query}`)
  76. },
  77. })
  78. }
  79. export const useDeleteSegment = () => {
  80. return useMutation({
  81. mutationKey: [NAME_SPACE, 'delete'],
  82. mutationFn: (payload: { datasetId: string; documentId: string; segmentIds: string[] }) => {
  83. const { datasetId, documentId, segmentIds } = payload
  84. const query = segmentIds.map(id => `segment_id=${id}`).join('&')
  85. return del<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/segments?${query}`)
  86. },
  87. })
  88. }
  89. export const useChildSegmentListKey = [NAME_SPACE, 'childChunkList']
  90. export const useChildSegmentList = (
  91. payload: {
  92. datasetId: string
  93. documentId: string
  94. segmentId: string
  95. params: {
  96. page: number
  97. limit: number
  98. keyword: string
  99. }
  100. },
  101. disable?: boolean,
  102. ) => {
  103. const { datasetId, documentId, segmentId, params } = payload
  104. const { page, limit, keyword } = params
  105. return useQuery({
  106. queryKey: [...useChildSegmentListKey, { datasetId, documentId, segmentId, page, limit, keyword }],
  107. queryFn: () => {
  108. return get<ChildSegmentsResponse>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`, { params })
  109. },
  110. enabled: !disable,
  111. })
  112. }
  113. export const useDeleteChildSegment = () => {
  114. return useMutation({
  115. mutationKey: [NAME_SPACE, 'childChunk', 'delete'],
  116. mutationFn: (payload: { datasetId: string; documentId: string; segmentId: string; childChunkId: string }) => {
  117. const { datasetId, documentId, segmentId, childChunkId } = payload
  118. return del<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`)
  119. },
  120. })
  121. }
  122. export const useAddChildSegment = () => {
  123. return useMutation({
  124. mutationKey: [NAME_SPACE, 'childChunk', 'add'],
  125. mutationFn: (payload: { datasetId: string; documentId: string; segmentId: string; body: { content: string } }) => {
  126. const { datasetId, documentId, segmentId, body } = payload
  127. return post<{ data: ChildChunkDetail }>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`, { body })
  128. },
  129. })
  130. }
  131. export const useUpdateChildSegment = () => {
  132. return useMutation({
  133. mutationKey: [NAME_SPACE, 'childChunk', 'update'],
  134. mutationFn: (payload: { datasetId: string; documentId: string; segmentId: string; childChunkId: string; body: { content: string } }) => {
  135. const { datasetId, documentId, segmentId, childChunkId, body } = payload
  136. return patch<{ data: ChildChunkDetail }>(`/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`, { body })
  137. },
  138. })
  139. }
  140. export const useSegmentBatchImport = () => {
  141. return useMutation({
  142. mutationKey: [NAME_SPACE, 'batchImport'],
  143. mutationFn: (payload: { url: string; body: FormData }) => {
  144. const { url, body } = payload
  145. return post<BatchImportResponse>(url, { body }, { bodyStringify: false, deleteContentType: true })
  146. },
  147. })
  148. }
  149. export const useCheckSegmentBatchImportProgress = () => {
  150. return useMutation({
  151. mutationKey: [NAME_SPACE, 'batchImport', 'checkProgress'],
  152. mutationFn: (payload: { jobID: string }) => {
  153. const { jobID } = payload
  154. return get<BatchImportResponse>(`/datasets/batch_import_status/${jobID}`)
  155. },
  156. })
  157. }