aiConfigApi.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { apiUrl, getAgentHeaders } from "@/lib/config";
  2. // AI 配置类型定义
  3. export interface AIConfig {
  4. id: number;
  5. user_id: number;
  6. provider: string;
  7. api_url: string;
  8. model: string;
  9. model_type: string;
  10. is_active: boolean;
  11. is_public: boolean;
  12. description: string;
  13. created_at: string;
  14. updated_at: string;
  15. }
  16. // 创建 AI 配置请求
  17. export interface CreateAIConfigRequest {
  18. provider: string;
  19. api_url: string;
  20. api_key: string;
  21. model: string;
  22. model_type?: string;
  23. is_active?: boolean;
  24. is_public?: boolean;
  25. description?: string;
  26. }
  27. // 更新 AI 配置请求
  28. export interface UpdateAIConfigRequest {
  29. provider?: string;
  30. api_url?: string;
  31. api_key?: string;
  32. model?: string;
  33. model_type?: string;
  34. is_active?: boolean;
  35. is_public?: boolean;
  36. description?: string;
  37. }
  38. // 获取用户的所有 AI 配置
  39. export async function fetchAIConfigs(userId: number): Promise<AIConfig[]> {
  40. const res = await fetch(apiUrl(`/agent/ai-config/${userId}`), {
  41. cache: "no-store",
  42. headers: getAgentHeaders(),
  43. });
  44. if (!res.ok) {
  45. throw new Error("获取 AI 配置失败");
  46. }
  47. return res.json();
  48. }
  49. // 获取单个 AI 配置
  50. export async function fetchAIConfig(
  51. userId: number,
  52. configId: number
  53. ): Promise<AIConfig> {
  54. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  55. cache: "no-store",
  56. headers: getAgentHeaders(),
  57. });
  58. if (!res.ok) {
  59. throw new Error("获取 AI 配置失败");
  60. }
  61. return res.json();
  62. }
  63. // 创建 AI 配置
  64. export async function createAIConfig(
  65. userId: number,
  66. data: CreateAIConfigRequest
  67. ): Promise<AIConfig> {
  68. const res = await fetch(apiUrl(`/agent/ai-config/${userId}`), {
  69. method: "POST",
  70. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  71. body: JSON.stringify(data),
  72. });
  73. if (!res.ok) {
  74. const error = await res.json();
  75. throw new Error(error.error || "创建 AI 配置失败");
  76. }
  77. return res.json();
  78. }
  79. // 更新 AI 配置
  80. export async function updateAIConfig(
  81. userId: number,
  82. configId: number,
  83. data: UpdateAIConfigRequest
  84. ): Promise<AIConfig> {
  85. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  86. method: "PUT",
  87. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  88. body: JSON.stringify(data),
  89. });
  90. if (!res.ok) {
  91. const error = await res.json();
  92. throw new Error(error.error || "更新 AI 配置失败");
  93. }
  94. return res.json();
  95. }
  96. // 删除 AI 配置
  97. export async function deleteAIConfig(
  98. userId: number,
  99. configId: number
  100. ): Promise<void> {
  101. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  102. method: "DELETE",
  103. headers: getAgentHeaders(),
  104. });
  105. if (!res.ok) {
  106. throw new Error("删除 AI 配置失败");
  107. }
  108. }
  109. // 获取开放的模型列表(供访客选择)
  110. export async function fetchPublicAIModels(
  111. modelType: string = "text"
  112. ): Promise<AIConfig[]> {
  113. const res = await fetch(
  114. `${apiUrl("/conversations/ai-models")}?model_type=${modelType}`,
  115. {
  116. cache: "no-store",
  117. }
  118. );
  119. if (!res.ok) {
  120. throw new Error("获取模型列表失败");
  121. }
  122. const data = await res.json();
  123. return data.models || [];
  124. }