embeddingConfigApi.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { apiUrl } from "@/lib/config";
  2. // 知识库向量配置(API 返回,不含明文 API Key)
  3. export interface EmbeddingConfig {
  4. id?: number;
  5. embedding_type: string;
  6. api_url: string;
  7. api_key_masked?: string;
  8. model: string;
  9. customer_can_use_kb: boolean;
  10. visitor_web_search_enabled?: boolean;
  11. /** 联网方式:vendor=厂商内置 web_search,custom=自建 Serper */
  12. web_search_source?: "vendor" | "custom";
  13. updated_at?: string;
  14. }
  15. // 访客小窗配置(联网设置,供访客端拉取)
  16. export interface VisitorWidgetConfig {
  17. web_search_enabled: boolean;
  18. }
  19. // 更新入参(api_key 可选,不传则保留原密钥)
  20. export interface UpdateEmbeddingConfigRequest {
  21. embedding_type?: string;
  22. api_url?: string;
  23. api_key?: string;
  24. model?: string;
  25. customer_can_use_kb?: boolean;
  26. visitor_web_search_enabled?: boolean;
  27. /** 联网方式:vendor=厂商内置,custom=自建(Serper) */
  28. web_search_source?: "vendor" | "custom";
  29. }
  30. /** 获取当前知识库向量配置(需传 user_id 以通过代理) */
  31. export async function fetchEmbeddingConfig(userId: number): Promise<EmbeddingConfig> {
  32. const res = await fetch(`${apiUrl("/agent/embedding-config")}?user_id=${userId}`, {
  33. cache: "no-store",
  34. });
  35. if (!res.ok) {
  36. throw new Error("获取知识库向量配置失败");
  37. }
  38. return res.json();
  39. }
  40. /** 更新知识库向量配置(仅管理员);修改后需重启后端生效 */
  41. export async function updateEmbeddingConfig(
  42. userId: number,
  43. data: UpdateEmbeddingConfigRequest
  44. ): Promise<EmbeddingConfig> {
  45. const res = await fetch(apiUrl("/agent/embedding-config"), {
  46. method: "PUT",
  47. headers: { "Content-Type": "application/json" },
  48. body: JSON.stringify({ user_id: userId, ...data }),
  49. });
  50. if (!res.ok) {
  51. const err = await res.json();
  52. throw new Error(err.error || "更新知识库向量配置失败");
  53. }
  54. return res.json();
  55. }
  56. /** 获取访客小窗配置(联网设置等,无需登录,供访客端调用) */
  57. export async function fetchVisitorWidgetConfig(): Promise<VisitorWidgetConfig> {
  58. const res = await fetch(apiUrl("/visitor/widget-config"), { cache: "no-store" });
  59. if (!res.ok) throw new Error("获取小窗配置失败");
  60. return res.json();
  61. }