knowledgeBaseApi.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { apiUrl, getAgentHeaders } from "@/lib/config";
  2. // 知识库摘要信息
  3. export interface KnowledgeBase {
  4. id: number;
  5. name: string;
  6. description: string;
  7. document_count: number;
  8. rag_enabled?: boolean; // 是否参与 RAG(对 AI 开放),默认 true
  9. created_at: string;
  10. updated_at: string;
  11. }
  12. // 创建知识库请求
  13. export interface CreateKnowledgeBaseRequest {
  14. name: string;
  15. description?: string;
  16. }
  17. // 更新知识库请求
  18. export interface UpdateKnowledgeBaseRequest {
  19. name?: string;
  20. description?: string;
  21. rag_enabled?: boolean;
  22. }
  23. // 获取知识库列表
  24. export async function fetchKnowledgeBases(): Promise<KnowledgeBase[]> {
  25. const res = await fetch(apiUrl("/knowledge-bases"), {
  26. cache: "no-store",
  27. headers: getAgentHeaders(),
  28. });
  29. if (!res.ok) {
  30. throw new Error("获取知识库列表失败");
  31. }
  32. const data = await res.json();
  33. return data.knowledge_bases || [];
  34. }
  35. // 获取知识库详情
  36. export async function fetchKnowledgeBase(id: number): Promise<KnowledgeBase> {
  37. const res = await fetch(apiUrl(`/knowledge-bases/${id}`), {
  38. cache: "no-store",
  39. headers: getAgentHeaders(),
  40. });
  41. if (!res.ok) {
  42. if (res.status === 404) {
  43. throw new Error("知识库不存在");
  44. }
  45. throw new Error("获取知识库详情失败");
  46. }
  47. return res.json();
  48. }
  49. // 创建知识库
  50. export async function createKnowledgeBase(data: CreateKnowledgeBaseRequest): Promise<KnowledgeBase> {
  51. const res = await fetch(apiUrl("/knowledge-bases"), {
  52. method: "POST",
  53. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  54. body: JSON.stringify(data),
  55. });
  56. if (!res.ok) {
  57. const error = await res.json().catch(() => ({}));
  58. throw new Error(error.error || "创建知识库失败");
  59. }
  60. return res.json();
  61. }
  62. // 更新知识库「参与 RAG」开关
  63. export async function updateKnowledgeBaseRAGEnabled(
  64. id: number,
  65. ragEnabled: boolean
  66. ): Promise<KnowledgeBase> {
  67. const res = await fetch(apiUrl(`/knowledge-bases/${id}/rag-enabled`), {
  68. method: "PATCH",
  69. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  70. body: JSON.stringify({ rag_enabled: ragEnabled }),
  71. });
  72. if (!res.ok) {
  73. const error = await res.json().catch(() => ({}));
  74. throw new Error((error as { error?: string }).error || "更新失败");
  75. }
  76. return res.json();
  77. }
  78. // 更新知识库
  79. export async function updateKnowledgeBase(
  80. id: number,
  81. data: UpdateKnowledgeBaseRequest
  82. ): Promise<KnowledgeBase> {
  83. const res = await fetch(apiUrl(`/knowledge-bases/${id}`), {
  84. method: "PUT",
  85. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  86. body: JSON.stringify(data),
  87. });
  88. if (!res.ok) {
  89. const error = await res.json().catch(() => ({}));
  90. if (res.status === 404) {
  91. throw new Error("知识库不存在");
  92. }
  93. throw new Error(error.error || "更新知识库失败");
  94. }
  95. return res.json();
  96. }
  97. // 删除知识库
  98. export async function deleteKnowledgeBase(id: number): Promise<void> {
  99. const res = await fetch(apiUrl(`/knowledge-bases/${id}`), {
  100. method: "DELETE",
  101. });
  102. if (!res.ok) {
  103. const error = await res.json().catch(() => ({}));
  104. if (res.status === 404) {
  105. throw new Error("知识库不存在");
  106. }
  107. throw new Error(error.error || "删除知识库失败");
  108. }
  109. }
  110. // 获取知识库的文档列表
  111. export async function fetchDocumentsByKnowledgeBase(
  112. knowledgeBaseId: number,
  113. page: number = 1,
  114. pageSize: number = 20,
  115. keyword?: string,
  116. status?: string
  117. ): Promise<any> {
  118. let url = `${apiUrl("/documents")}?knowledge_base_id=${knowledgeBaseId}&page=${page}&page_size=${pageSize}`;
  119. if (keyword) {
  120. url += `&keyword=${encodeURIComponent(keyword)}`;
  121. }
  122. if (status) {
  123. url += `&status=${encodeURIComponent(status)}`;
  124. }
  125. const res = await fetch(url, {
  126. cache: "no-store",
  127. headers: getAgentHeaders(),
  128. });
  129. if (!res.ok) {
  130. throw new Error("获取文档列表失败");
  131. }
  132. return res.json();
  133. }