Spaces:
Paused
Paused
| // services/api-service.js - All API interactions | |
| import { Utils } from '../utils.js'; | |
| import { StateManager } from './state-manager.js'; | |
| export const ApiService = { | |
| /** | |
| * Send a chat message to the server | |
| * @param {string} text - User message text | |
| * @param {string} modelType - Model type to use | |
| * @returns {Promise<Object>} Response data | |
| */ | |
| async sendChatMessage(text, modelType) { | |
| const payload = { | |
| user_id: Utils.getMachineId(), | |
| session_id: StateManager.sessionId, | |
| conversation_id: StateManager.getConversationId(modelType), | |
| human_message: text, | |
| model_type: modelType, | |
| consent: StateManager.consentGranted, | |
| age_group: StateManager.profile.ageGroup, | |
| gender: StateManager.profile.gender, | |
| roles: StateManager.profile.roles, | |
| participant_id: StateManager.profile.participantId, | |
| lang: StateManager.currentLang | |
| }; | |
| const res = await fetch('/chat', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(payload), | |
| }); | |
| if (!res.ok) { | |
| throw new Error(`HTTP ${res.status}`); | |
| } | |
| return res; | |
| }, | |
| /** | |
| * Upload a file to the server | |
| * @param {File} file - File to upload | |
| * @returns {Promise<boolean>} Success status | |
| */ | |
| async uploadFile(file) { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| formData.append('session_id', StateManager.sessionId); | |
| try { | |
| const res = await fetch('/file', { | |
| method: 'PUT', | |
| body: formData, | |
| }); | |
| if (!res.ok) { | |
| if (res.status === 413) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_file_too_large"], 'error'); | |
| } else if (res.status === 400) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_malformed_file"], 'error'); | |
| } else if (res.status === 415) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_unsupported_mime_type"], 'error'); | |
| } else if (res.status === 419) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_exceed_session_size"], 'error'); | |
| } else if (res.status === 500) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_server_error"], 'error'); | |
| } else { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_unknown_error"], 'error'); | |
| } | |
| return false; | |
| } | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_success"], 'success'); | |
| return true; | |
| } catch (err) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_network_error"], 'error'); | |
| return false; | |
| } | |
| }, | |
| /** | |
| * Delete a file from the server | |
| * @param {File} file - File to delete | |
| * @returns {Promise<boolean>} Success status | |
| */ | |
| async deleteFile(file) { | |
| const payload = { | |
| file_name: file.name, | |
| user_id: Utils.getMachineId(), | |
| session_id: StateManager.sessionId, | |
| consent: StateManager.consentGranted, | |
| age_group: StateManager.profile.ageGroup, | |
| gender: StateManager.profile.gender, | |
| roles: StateManager.profile.roles, | |
| participant_id: StateManager.profile.participantId | |
| }; | |
| try { | |
| const res = await fetch('/file', { | |
| method: 'DELETE', | |
| body: JSON.stringify(payload), | |
| headers: { 'Content-Type': 'application/json' }, | |
| }); | |
| if (!res.ok) { | |
| showSnackbar(translations[StateManager.currentLang]["file_upload_failed_server_error"], 'error'); | |
| return false; | |
| } | |
| showSnackbar(translations[StateManager.currentLang]["file_delete_success"], 'success'); | |
| return true; | |
| } catch (err) { | |
| showSnackbar(translations[StateManager.currentLang]["file_delete_failed_network_error"], 'error'); | |
| return false; | |
| } | |
| }, | |
| /** | |
| * Send a comment to the server | |
| * @param {string} comment - Comment text | |
| * @returns {Promise<Object>} Response object with status | |
| */ | |
| async sendComment(comment) { | |
| const payload = { | |
| user_id: Utils.getMachineId(), | |
| session_id: StateManager.sessionId, | |
| comment, | |
| consent: StateManager.consentGranted, | |
| age_group: StateManager.profile.ageGroup, | |
| gender: StateManager.profile.gender, | |
| roles: StateManager.profile.roles, | |
| participant_id: StateManager.profile.participantId | |
| }; | |
| try { | |
| const res = await fetch('/comment', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(payload), | |
| }); | |
| if (!res.ok) { | |
| return { | |
| success: false, | |
| status: res.status | |
| }; | |
| } | |
| return { | |
| success: true | |
| }; | |
| } catch (err) { | |
| return { | |
| success: false, | |
| error: err | |
| }; | |
| } | |
| }, | |
| /** | |
| * Submit message feedback to the server | |
| * @param {Object} feedbackData - Feedback data object | |
| * @returns {Promise<Object>} Response object with status | |
| */ | |
| async submitFeedback(feedbackData) { | |
| const payload = { | |
| ...feedbackData, | |
| consent: StateManager.consentGranted, | |
| age_group: StateManager.profile.ageGroup, | |
| gender: StateManager.profile.gender, | |
| roles: StateManager.profile.roles, | |
| participant_id: StateManager.profile.participantId, | |
| lang: StateManager.currentLang | |
| }; | |
| try { | |
| const res = await fetch('/feedback', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(payload), | |
| }); | |
| if (!res.ok) { | |
| return { | |
| success: false, | |
| status: res.status | |
| }; | |
| } | |
| return { | |
| success: true | |
| }; | |
| } catch (err) { | |
| return { | |
| success: false, | |
| error: err | |
| }; | |
| } | |
| } | |
| }; |