knowledgeBaseService.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { apiClient } from './apiClient';
  2. import { KnowledgeFile } from '../types';
  3. export const knowledgeBaseService = {
  4. async getAll(
  5. authToken: string,
  6. options: {
  7. page?: number;
  8. limit?: number;
  9. name?: string;
  10. status?: string;
  11. groupId?: string;
  12. } = {}
  13. ): Promise<{ items: KnowledgeFile[]; total: number; page: number; limit: number }> {
  14. const queryParams = new URLSearchParams();
  15. if (options.page) queryParams.append('page', options.page.toString());
  16. if (options.limit) queryParams.append('limit', options.limit.toString());
  17. if (options.name) queryParams.append('name', options.name);
  18. if (options.status) queryParams.append('status', options.status);
  19. if (options.groupId) queryParams.append('groupId', options.groupId);
  20. const queryString = queryParams.toString();
  21. const url = `/knowledge-bases${queryString ? `?${queryString}` : ''}`;
  22. const response = await apiClient.request(url, {});
  23. if (!response.ok) {
  24. throw new Error('Failed to fetch knowledge base files');
  25. }
  26. const data = await response.json();
  27. console.log('Knowledge base API response:', data);
  28. const items = Array.isArray(data) ? data : (data.items || []);
  29. const total = Array.isArray(data) ? data.length : (data.total || 0);
  30. const page = Array.isArray(data) ? 1 : (data.page || 1);
  31. const limit = Array.isArray(data) ? items.length : (data.limit || 12);
  32. return {
  33. items: items.map((item: any) => ({
  34. id: item.id,
  35. name: item.originalName,
  36. originalName: item.originalName,
  37. type: item.mimetype,
  38. size: item.size,
  39. status: item.status,
  40. groups: item.groups || [],
  41. createdAt: item.createdAt,
  42. updatedAt: item.updatedAt,
  43. })),
  44. total,
  45. page,
  46. limit,
  47. };
  48. },
  49. async getStatuses(ids: string[], authToken: string): Promise<{ id: string, status: KnowledgeFile['status'], updatedAt: string }[]> {
  50. const response = await apiClient.request('/knowledge-bases/statuses', {
  51. method: 'POST',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. },
  55. body: JSON.stringify({ ids }),
  56. });
  57. if (!response.ok) {
  58. throw new Error('Failed to fetch knowledge base statuses');
  59. }
  60. return response.json();
  61. },
  62. async getStats(authToken: string): Promise<{ total: number, uncategorized: number }> {
  63. const response = await apiClient.request('/knowledge-bases/stats', {
  64. method: 'GET'
  65. });
  66. if (!response.ok) {
  67. throw new Error('Failed to fetch knowledge base stats');
  68. }
  69. return response.json();
  70. },
  71. async clearAll(authToken: string): Promise<void> {
  72. const response = await apiClient.request('/knowledge-bases/clear', {
  73. method: 'DELETE',
  74. });
  75. if (!response.ok) {
  76. throw new Error('Failed to clear knowledge base');
  77. }
  78. },
  79. async search(query: string, topK: number = 5, authToken: string): Promise<any> {
  80. const response = await apiClient.request('/knowledge-bases/search', {
  81. method: 'POST',
  82. headers: {
  83. 'Content-Type': 'application/json',
  84. },
  85. body: JSON.stringify({ query, topK }),
  86. });
  87. if (!response.ok) {
  88. throw new Error('Failed to search knowledge base');
  89. }
  90. return response.json();
  91. },
  92. async deleteFile(fileId: string, authToken: string): Promise<void> {
  93. const response = await apiClient.request(`/knowledge-bases/${fileId}`, {
  94. method: 'DELETE',
  95. });
  96. if (!response.ok) {
  97. throw new Error('Failed to delete file');
  98. }
  99. },
  100. async retryFile(fileId: string, authToken: string): Promise<KnowledgeFile> {
  101. const response = await apiClient.request(`/knowledge-bases/${fileId}/retry`, {
  102. method: 'POST',
  103. });
  104. if (!response.ok) {
  105. throw new Error('Failed to retry file');
  106. }
  107. const item = await response.json();
  108. return {
  109. id: item.id,
  110. name: item.originalName,
  111. originalName: item.originalName,
  112. type: item.mimetype,
  113. size: item.size,
  114. status: item.status,
  115. groups: item.groups || [],
  116. createdAt: item.createdAt,
  117. updatedAt: item.updatedAt,
  118. };
  119. },
  120. async getFileChunks(fileId: string, authToken: string) {
  121. const response = await apiClient.request(`/knowledge-bases/${fileId}/chunks`, {});
  122. if (!response.ok) {
  123. throw new Error('Failed to get file chunks');
  124. }
  125. return response.json();
  126. },
  127. getPageImageUrl(fileId: string, pageIndex: number): string {
  128. return `/api/knowledge-bases/${fileId}/page/${pageIndex}`;
  129. },
  130. };