|
|
|
|
| import { Utils } from '../utils.js';
|
| import { StateManager } from './state-manager.js';
|
|
|
| export const ApiService = {
|
| |
| |
| |
| |
| |
|
|
| 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;
|
| },
|
|
|
| |
| |
| |
| |
|
|
| 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;
|
| }
|
| },
|
|
|
| |
| |
| |
| |
|
|
| 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;
|
| }
|
| },
|
|
|
| |
| |
| |
| |
|
|
| 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
|
| };
|
| }
|
| },
|
|
|
| |
| |
| |
| |
|
|
| 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
|
| };
|
| }
|
| }
|
| }; |