documentApi.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { API_BASE_URL, getAgentHeaders } from "@/lib/config";
  2. // 文档摘要信息
  3. export interface Document {
  4. id: number;
  5. knowledge_base_id: number;
  6. title: string;
  7. content: string;
  8. summary: string;
  9. type: string;
  10. status: string;
  11. embedding_status: string;
  12. created_at: string;
  13. updated_at: string;
  14. }
  15. // 创建文档请求
  16. export interface CreateDocumentRequest {
  17. knowledge_base_id: number;
  18. title: string;
  19. content: string;
  20. summary?: string;
  21. type?: string;
  22. status?: string;
  23. }
  24. // 更新文档请求
  25. export interface UpdateDocumentRequest {
  26. title?: string;
  27. content?: string;
  28. summary?: string;
  29. type?: string;
  30. status?: string;
  31. }
  32. // 文档列表结果
  33. export interface DocumentListResult {
  34. documents: Document[];
  35. total: number;
  36. page: number;
  37. page_size: number;
  38. total_page: number;
  39. }
  40. // 获取文档列表
  41. export async function fetchDocuments(
  42. knowledgeBaseId?: number,
  43. page: number = 1,
  44. pageSize: number = 20,
  45. keyword?: string,
  46. status?: string
  47. ): Promise<DocumentListResult> {
  48. let url = `${API_BASE_URL}/documents?page=${page}&page_size=${pageSize}`;
  49. if (knowledgeBaseId) {
  50. url += `&knowledge_base_id=${knowledgeBaseId}`;
  51. }
  52. if (keyword) {
  53. url += `&keyword=${encodeURIComponent(keyword)}`;
  54. }
  55. if (status) {
  56. url += `&status=${encodeURIComponent(status)}`;
  57. }
  58. const res = await fetch(url, {
  59. cache: "no-store",
  60. headers: getAgentHeaders(),
  61. });
  62. if (!res.ok) {
  63. throw new Error("获取文档列表失败");
  64. }
  65. return res.json();
  66. }
  67. // 获取文档详情
  68. export async function fetchDocument(id: number): Promise<Document> {
  69. const res = await fetch(`${API_BASE_URL}/documents/${id}`, {
  70. cache: "no-store",
  71. headers: getAgentHeaders(),
  72. });
  73. if (!res.ok) {
  74. if (res.status === 404) {
  75. throw new Error("文档不存在");
  76. }
  77. throw new Error("获取文档详情失败");
  78. }
  79. return res.json();
  80. }
  81. // 创建文档
  82. export async function createDocument(data: CreateDocumentRequest): Promise<Document> {
  83. const res = await fetch(`${API_BASE_URL}/documents`, {
  84. method: "POST",
  85. headers: { "Content-Type": "application/json" },
  86. body: JSON.stringify(data),
  87. });
  88. if (!res.ok) {
  89. const error = await res.json().catch(() => ({}));
  90. throw new Error(error.error || "创建文档失败");
  91. }
  92. return res.json();
  93. }
  94. // 更新文档
  95. export async function updateDocument(
  96. id: number,
  97. data: UpdateDocumentRequest
  98. ): Promise<Document> {
  99. const res = await fetch(`${API_BASE_URL}/documents/${id}`, {
  100. method: "PUT",
  101. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  102. body: JSON.stringify(data),
  103. });
  104. if (!res.ok) {
  105. const error = await res.json().catch(() => ({}));
  106. if (res.status === 404) {
  107. throw new Error("文档不存在");
  108. }
  109. throw new Error(error.error || "更新文档失败");
  110. }
  111. return res.json();
  112. }
  113. // 删除文档
  114. export async function deleteDocument(id: number): Promise<void> {
  115. const res = await fetch(`${API_BASE_URL}/documents/${id}`, {
  116. method: "DELETE",
  117. headers: getAgentHeaders(),
  118. });
  119. if (!res.ok) {
  120. const error = await res.json().catch(() => ({}));
  121. if (res.status === 404) {
  122. throw new Error("文档不存在");
  123. }
  124. throw new Error(error.error || "删除文档失败");
  125. }
  126. }
  127. // 更新文档状态
  128. export async function updateDocumentStatus(id: number, status: string): Promise<void> {
  129. const res = await fetch(`${API_BASE_URL}/documents/${id}/status`, {
  130. method: "PUT",
  131. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  132. body: JSON.stringify({ status }),
  133. });
  134. if (!res.ok) {
  135. const error = await res.json().catch(() => ({}));
  136. throw new Error(error.error || "更新文档状态失败");
  137. }
  138. }
  139. // 发布文档
  140. export async function publishDocument(id: number): Promise<void> {
  141. const res = await fetch(`${API_BASE_URL}/documents/${id}/publish`, {
  142. method: "POST",
  143. headers: getAgentHeaders(),
  144. });
  145. if (!res.ok) {
  146. const error = await res.json().catch(() => ({}));
  147. throw new Error(error.error || "发布文档失败");
  148. }
  149. }
  150. // 取消发布文档
  151. export async function unpublishDocument(id: number): Promise<void> {
  152. const res = await fetch(`${API_BASE_URL}/documents/${id}/unpublish`, {
  153. method: "POST",
  154. headers: getAgentHeaders(),
  155. });
  156. if (!res.ok) {
  157. const error = await res.json().catch(() => ({}));
  158. throw new Error(error.error || "取消发布文档失败");
  159. }
  160. }
  161. // 搜索文档(向量检索)
  162. export async function searchDocuments(
  163. query: string,
  164. topK: number = 5,
  165. knowledgeBaseId?: number
  166. ): Promise<Document[]> {
  167. let url = `${API_BASE_URL}/documents/search?query=${encodeURIComponent(query)}&top_k=${topK}`;
  168. if (knowledgeBaseId) {
  169. url += `&knowledge_base_id=${knowledgeBaseId}`;
  170. }
  171. const res = await fetch(url, {
  172. cache: "no-store",
  173. headers: getAgentHeaders(),
  174. });
  175. if (!res.ok) {
  176. const error = await res.json().catch(() => ({}));
  177. throw new Error(error.error || "搜索文档失败");
  178. }
  179. const data = await res.json();
  180. return data.documents || [];
  181. }