modelConfigService.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { apiClient } from './apiClient';
  2. import { ModelConfig } from '../types'; // Frontend ModelConfig interface
  3. interface ModelConfigResponse extends Omit<ModelConfig, 'apiKey'> {
  4. // Backend response might omit apiKey for security
  5. id: string; // Ensure id is always present
  6. createdAt: Date;
  7. updatedAt: Date;
  8. }
  9. export const modelConfigService = {
  10. async getAll(token: string): Promise<ModelConfigResponse[]> {
  11. const response = await apiClient.request('/models', {
  12. method: 'GET',
  13. headers: {
  14. 'Content-Type': 'application/json',
  15. },
  16. });
  17. if (!response.ok) {
  18. const errorData = await response.json();
  19. throw new Error(errorData.message || 'Failed to fetch model configs');
  20. }
  21. return response.json();
  22. },
  23. async create(token: string, modelConfig: Omit<ModelConfig, 'id'>): Promise<ModelConfigResponse> {
  24. const response = await apiClient.request('/models', {
  25. method: 'POST',
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. },
  29. body: JSON.stringify(modelConfig),
  30. });
  31. if (!response.ok) {
  32. const errorData = await response.json();
  33. throw new Error(errorData.message || 'Failed to create model config');
  34. }
  35. return response.json();
  36. },
  37. async update(token: string, id: string, modelConfig: Partial<Omit<ModelConfig, 'id'>>): Promise<ModelConfigResponse> {
  38. const response = await apiClient.request(`/models/${id}`, {
  39. method: 'PUT',
  40. headers: {
  41. 'Content-Type': 'application/json',
  42. },
  43. body: JSON.stringify(modelConfig),
  44. });
  45. if (!response.ok) {
  46. const errorData = await response.json();
  47. throw new Error(errorData.message || 'Failed to update model config');
  48. }
  49. return response.json();
  50. },
  51. async remove(token: string, id: string): Promise<void> {
  52. const response = await apiClient.request(`/models/${id}`, {
  53. method: 'DELETE',
  54. headers: {
  55. 'Content-Type': 'application/json',
  56. },
  57. });
  58. if (!response.ok) {
  59. const errorData = await response.json();
  60. throw new Error(errorData.message || 'Failed to delete model config');
  61. }
  62. },
  63. async setDefault(token: string, id: string): Promise<ModelConfigResponse> {
  64. const response = await apiClient.request(`/models/${id}/set-default`, {
  65. method: 'PATCH',
  66. headers: {
  67. 'Content-Type': 'application/json',
  68. },
  69. });
  70. if (!response.ok) {
  71. const errorData = await response.json();
  72. throw new Error(errorData.message || 'Failed to set default model');
  73. }
  74. return response.json();
  75. },
  76. };