aiConfigApi.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { apiUrl } 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. });
  43. if (!res.ok) {
  44. throw new Error("获取 AI 配置失败");
  45. }
  46. return res.json();
  47. }
  48. // 获取单个 AI 配置
  49. export async function fetchAIConfig(
  50. userId: number,
  51. configId: number
  52. ): Promise<AIConfig> {
  53. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  54. cache: "no-store",
  55. });
  56. if (!res.ok) {
  57. throw new Error("获取 AI 配置失败");
  58. }
  59. return res.json();
  60. }
  61. // 创建 AI 配置
  62. export async function createAIConfig(
  63. userId: number,
  64. data: CreateAIConfigRequest
  65. ): Promise<AIConfig> {
  66. const res = await fetch(apiUrl(`/agent/ai-config/${userId}`), {
  67. method: "POST",
  68. headers: { "Content-Type": "application/json" },
  69. body: JSON.stringify(data),
  70. });
  71. if (!res.ok) {
  72. const error = await res.json();
  73. throw new Error(error.error || "创建 AI 配置失败");
  74. }
  75. return res.json();
  76. }
  77. // 更新 AI 配置
  78. export async function updateAIConfig(
  79. userId: number,
  80. configId: number,
  81. data: UpdateAIConfigRequest
  82. ): Promise<AIConfig> {
  83. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  84. method: "PUT",
  85. headers: { "Content-Type": "application/json" },
  86. body: JSON.stringify(data),
  87. });
  88. if (!res.ok) {
  89. const error = await res.json();
  90. throw new Error(error.error || "更新 AI 配置失败");
  91. }
  92. return res.json();
  93. }
  94. // 删除 AI 配置
  95. export async function deleteAIConfig(
  96. userId: number,
  97. configId: number
  98. ): Promise<void> {
  99. const res = await fetch(apiUrl(`/agent/ai-config/${userId}/${configId}`), {
  100. method: "DELETE",
  101. });
  102. if (!res.ok) {
  103. throw new Error("删除 AI 配置失败");
  104. }
  105. }
  106. // 获取开放的模型列表(供访客选择)
  107. export async function fetchPublicAIModels(
  108. modelType: string = "text"
  109. ): Promise<AIConfig[]> {
  110. const res = await fetch(
  111. `${apiUrl("/conversations/ai-models")}?model_type=${modelType}`,
  112. {
  113. cache: "no-store",
  114. }
  115. );
  116. if (!res.ok) {
  117. throw new Error("获取模型列表失败");
  118. }
  119. const data = await res.json();
  120. return data.models || [];
  121. }