use-segment.ts 6.0 KB

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