embeddingConfigApi.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { apiUrl, getAgentHeaders } 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. headers: getAgentHeaders(),
  35. });
  36. if (!res.ok) {
  37. throw new Error("获取知识库向量配置失败");
  38. }
  39. return res.json();
  40. }
  41. /** 更新知识库向量配置(仅管理员);修改后需重启后端生效 */
  42. export async function updateEmbeddingConfig(
  43. userId: number,
  44. data: UpdateEmbeddingConfigRequest
  45. ): Promise<EmbeddingConfig> {
  46. const res = await fetch(apiUrl("/agent/embedding-config"), {
  47. method: "PUT",
  48. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  49. body: JSON.stringify({ user_id: userId, ...data }),
  50. });
  51. if (!res.ok) {
  52. const err = await res.json();
  53. throw new Error(err.error || "更新知识库向量配置失败");
  54. }
  55. return res.json();
  56. }
  57. /** 获取访客小窗配置(联网设置等,无需登录,供访客端调用) */
  58. export async function fetchVisitorWidgetConfig(): Promise<VisitorWidgetConfig> {
  59. const res = await fetch(apiUrl("/visitor/widget-config"), { cache: "no-store" });
  60. if (!res.ok) throw new Error("获取小窗配置失败");
  61. return res.json();
  62. }