chunkApi.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { apiUrl, getAgentHeaders } from "@/lib/config";
  2. // 文档分段
  3. export interface DocumentChunk {
  4. id: number;
  5. document_id: number;
  6. knowledge_base_id: number;
  7. chunk_index: number;
  8. content: string;
  9. embedding_status: string;
  10. created_at: string;
  11. updated_at: string;
  12. }
  13. // 分段请求参数
  14. export interface ChunkRequest {
  15. method: "char_count" | "separator";
  16. chunk_size?: number;
  17. separator?: string;
  18. }
  19. // 分段列表响应
  20. export interface ChunkListResponse {
  21. chunks: DocumentChunk[];
  22. total: number;
  23. page: number;
  24. page_size: number;
  25. total_page: number;
  26. }
  27. // 分段执行响应
  28. export interface ChunkExecuteResponse {
  29. message: string;
  30. chunk_count: number;
  31. chunks: DocumentChunk[];
  32. }
  33. // 执行分段
  34. export async function executeChunking(
  35. documentId: number,
  36. req: ChunkRequest
  37. ): Promise<ChunkExecuteResponse> {
  38. const res = await fetch(apiUrl(`/documents/${documentId}/chunks`), {
  39. method: "POST",
  40. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  41. body: JSON.stringify(req),
  42. });
  43. if (!res.ok) {
  44. const error = await res.json().catch(() => ({}));
  45. throw new Error(error.error || "执行分段失败");
  46. }
  47. return res.json();
  48. }
  49. // 获取分段列表
  50. export async function fetchChunks(
  51. documentId: number,
  52. page: number = 1,
  53. pageSize: number = 10
  54. ): Promise<ChunkListResponse> {
  55. const params = new URLSearchParams({
  56. page: String(page),
  57. page_size: String(pageSize),
  58. });
  59. const res = await fetch(
  60. apiUrl(`/documents/${documentId}/chunks?${params}`),
  61. {
  62. cache: "no-store",
  63. headers: getAgentHeaders(),
  64. }
  65. );
  66. if (!res.ok) {
  67. const error = await res.json().catch(() => ({}));
  68. throw new Error(error.error || "获取分段列表失败");
  69. }
  70. return res.json();
  71. }
  72. // 更新单个分段
  73. export async function updateChunk(
  74. documentId: number,
  75. chunkId: number,
  76. content: string
  77. ): Promise<DocumentChunk> {
  78. const res = await fetch(
  79. apiUrl(`/documents/${documentId}/chunks/${chunkId}`),
  80. {
  81. method: "PUT",
  82. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  83. body: JSON.stringify({ content }),
  84. }
  85. );
  86. if (!res.ok) {
  87. const error = await res.json().catch(() => ({}));
  88. throw new Error(error.error || "更新分段失败");
  89. }
  90. return res.json();
  91. }
  92. // 删除所有分段
  93. export async function deleteChunks(documentId: number): Promise<void> {
  94. const res = await fetch(apiUrl(`/documents/${documentId}/chunks`), {
  95. method: "DELETE",
  96. headers: getAgentHeaders(),
  97. });
  98. if (!res.ok) {
  99. const error = await res.json().catch(() => ({}));
  100. throw new Error(error.error || "删除分段失败");
  101. }
  102. }