monorepo-ai-medical/web/src/services/modelRegistryApi.js

108 lines
4.5 KiB
JavaScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import axiosClient from './axiosClient'; // Axios Client ที่มี JWT Interceptor
const STALE_TIME = 60 * 1000; // 1 นาที
// ----------------------------------------------------
// Model Registry Queries (GET)
// ----------------------------------------------------
/**
* Hook สำหรับดึงรายการ AI Model ทั้งหมด
* Endpoint: GET /api/v1/models/ (Protected by IsAuthenticated)
*/
export const useModelList = () => {
return useQuery({
queryKey: ['modelList'],
queryFn: async () => {
const response = await axiosClient.get('/api/v1/models/');
return response.data; // คาดหวัง Array ของ Model Objects
},
staleTime: STALE_TIME,
});
};
// ----------------------------------------------------
// Model Control Mutations (POST, PATCH, DELETE)
// ----------------------------------------------------
/**
* Hook สำหรับลงทะเบียน Model ใหม่ (POST)
* Endpoint: POST /api/v1/models/ (Protected by IsAdminOrManager)
*/
export const useCreateModel = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (modelData) => {
// modelData: { name, model_version, base_url, inference_path, ... }
const response = await axiosClient.post('/api/v1/models/', modelData);
return response.data;
},
onSuccess: () => {
alert('Model ถูกลงทะเบียนสำเร็จแล้ว!');
queryClient.invalidateQueries({ queryKey: ['modelList'] });
},
onError: (error) => {
alert(`การลงทะเบียนล้มเหลว: ${error.response?.data?.detail || 'โปรดตรวจสอบข้อมูล'}`);
}
});
};
/**
* Hook สำหรับลบ Model (DELETE)
* Endpoint: DELETE /api/v1/models/{id}/ (Protected by IsAdminOrManager)
*/
export const useDeleteModel = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (modelId) => {
const response = await axiosClient.delete(`/api/v1/models/${modelId}/`);
return response.data;
},
onSuccess: () => {
alert('Model ถูกลบสำเร็จแล้ว!');
queryClient.invalidateQueries({ queryKey: ['modelList'] });
},
onError: (error) => {
alert(`การลบล้มเหลว: ${error.response?.data?.detail || 'คุณอาจไม่มีสิทธิ์'}`);
}
});
};
/**
* Hook สำหรับทดสอบการเชื่อมต่อ (POST /test-connection/)
* Endpoint: POST /api/v1/models/{id}/test-connection/ (Protected by IsAuthenticated)
*/
export const useTestConnection = () => {
return useMutation({
mutationFn: async (modelId) => {
// Note: Endpoints นี้ต้องการ Body แต่ถ้า Backend ไม่ได้ใช้ เราส่ง Body เปล่า
const response = await axiosClient.post(`/api/v1/models/${modelId}/test-connection/`, {});
return response.data;
},
});
};
/**
* Hook สำหรับแก้ไข Model ที่มีอยู่ (PUT/PATCH)
* Endpoint: PATCH /api/v1/models/{id}/ (Protected by IsAdminOrManager)
*/
export const useUpdateModel = () => {
const queryClient = useQueryClient();
return useMutation({
// mutationFn รับ object ที่มี { id: number, data: object }
mutationFn: async ({ id, data }) => {
// เราใช้ PATCH เพื่อส่งเฉพาะฟิลด์ที่มีการเปลี่ยนแปลง
const response = await axiosClient.patch(`/api/v1/models/${id}/`, data);
return response.data;
},
onSuccess: () => {
alert('Model ถูกแก้ไขสำเร็จแล้ว!');
// Invalidate query list เพื่อบังคับให้ตารางอัปเดตข้อมูล
queryClient.invalidateQueries({ queryKey: ['modelList'] });
},
onError: (error) => {
alert(`การแก้ไขล้มเหลว: ${error.response?.data?.detail || 'โปรดตรวจสอบข้อมูล'}`);
}
});
};