File size: 12,531 Bytes
5bdbf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db4505
5bdbf17
 
 
 
 
 
4db4505
5bdbf17
4db4505
 
 
 
 
5bdbf17
4db4505
 
 
5bdbf17
4db4505
5bdbf17
4db4505
 
 
 
 
 
 
5bdbf17
 
 
4db4505
 
5bdbf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dd52ab
 
 
 
 
 
 
 
 
 
 
 
 
 
5bdbf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db4505
 
5bdbf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db4505
 
5bdbf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db4505
 
5bdbf17
0dd52ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// ---------------------------------------------------------------------------
// DECLARE User Study -- client-side logic
// Handles: consent/demographics submission, real microphone recording via the
// MediaRecorder API, submitting recordings to the Flask backend, the two-part
// judgement step, and the UMUX feedback questionnaire.
// ---------------------------------------------------------------------------

const screens = {
  consent: document.getElementById('consent-screen'),
  trial: document.getElementById('trial-screen'),
  umux: document.getElementById('umux-screen'),
  end: document.getElementById('end-screen'),
};
const errorBanner = document.getElementById('error-banner');

let sessionId = null;
let totalTrials = 0;
let currentTrialIndex = 0;
let entityApplicable = false;
let intentLegendHtml = '';

let mediaStream = null;
let mediaRecorder = null;
let audioChunks = [];
let isRecording = false;

function showScreen(name) {
  Object.values(screens).forEach(el => el.classList.add('hidden'));
  screens[name].classList.remove('hidden');
}

function showError(message) {
  errorBanner.textContent = message;
  errorBanner.classList.remove('hidden');
  window.scrollTo({ top: 0, behavior: 'smooth' });
}

function clearError() {
  errorBanner.classList.add('hidden');
}

function getRadioValue(name) {
  const el = document.querySelector(`input[name="${name}"]:checked`);
  return el ? el.value : null;
}

function clearRadioGroup(name) {
  document.querySelectorAll(`input[name="${name}"]`).forEach(el => { el.checked = false; });
}

// ---------------------------------------------------------------------------
// Intent legend -- fetched once, reused every trial
// ---------------------------------------------------------------------------
async function loadIntentLegend() {
  const res = await fetch('/api/intent-legend');
  const data = await res.json();
  const parts = ['<div class="legend-title">What the categories mean:</div>'];
  data.labels.forEach(label => {
    const meta = data.meta[label];
    parts.push(`<div class="legend-item">${meta.icon} <strong>${label}</strong> — ${meta.description}</div>`);
  });
  intentLegendHtml = parts.join('');
}

// ---------------------------------------------------------------------------
// Consent -> session start
// ---------------------------------------------------------------------------
document.getElementById('start-btn').addEventListener('click', async () => {
  clearError();
  const gender = getRadioValue('gender');
  const age_group = getRadioValue('age_group');
  const first_language = document.getElementById('lang-input').value.trim();
  const environment = getRadioValue('environment');
  const consent = document.getElementById('consent-check').checked;

  if (!consent) {
    showError("Please confirm you've read and agree to the consent statement.");
    return;
  }
  if (!gender || !age_group) {
    showError('Please select your gender and age group before starting.');
    return;
  }

  let data;
  try {
    const res = await fetch('/api/session/start', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ gender, age_group, first_language, environment, consent }),
    });
    data = await res.json();
    if (!res.ok) { showError(data.error || 'Could not start session.'); return; }
  } catch (err) {
    console.error('Session start failed:', err);
    showError('Error starting session (check browser console for details): ' + err.message);
    return;
  }

  sessionId = data.session_id;
  totalTrials = data.total_trials;
  currentTrialIndex = data.trial_index;

  try {
    await loadIntentLegend();
  } catch (err) {
    console.error('Loading intent legend failed:', err);
    showError('Error loading category legend (check browser console for details): ' + err.message);
    return;
  }

  try {
    renderTrial(data.prompt_text);
    showScreen('trial');
  } catch (err) {
    console.error('Rendering trial screen failed:', err);
    showError('Error displaying the trial screen (check browser console for details): ' + err.message);
  }
});

// ---------------------------------------------------------------------------
// Trial rendering
// ---------------------------------------------------------------------------
function renderTrial(promptText) {
  document.getElementById('progress-label').textContent = `COMMAND ${currentTrialIndex + 1} OF ${totalTrials}`;
  document.getElementById('progress-fill').style.width = `${((currentTrialIndex + 1) / totalTrials) * 100}%`;
  document.getElementById('prompt-text').textContent = `"${promptText}"`;

  document.getElementById('rec-zone').classList.remove('hidden');
  document.getElementById('analyzing-zone').classList.add('hidden');
  document.getElementById('result-zone').classList.add('hidden');

  const btn = document.getElementById('rec-btn');
  btn.classList.remove('recording');
  btn.textContent = '●';
  btn.disabled = false;
  document.getElementById('rec-hint').textContent = 'Click to record';

  clearRadioGroup('intent_judge');
  clearRadioGroup('entity_judge');
  document.getElementById('entity-judge-block').classList.add('hidden');
}

// ---------------------------------------------------------------------------
// Recording -- real microphone capture via MediaRecorder
// ---------------------------------------------------------------------------
document.getElementById('rec-btn').addEventListener('click', async () => {
  if (!isRecording) {
    await startRecording();
  } else {
    stopRecording();
  }
});

async function startRecording() {
  clearError();
  try {
    mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
  } catch (err) {
    showError('Microphone access is required to participate. Please allow microphone access and try again.');
    return;
  }

  audioChunks = [];
  mediaRecorder = new MediaRecorder(mediaStream);
  mediaRecorder.ondataavailable = e => { if (e.data.size > 0) audioChunks.push(e.data); };
  mediaRecorder.onstop = handleRecordingStopped;
  mediaRecorder.start();

  isRecording = true;
  const btn = document.getElementById('rec-btn');
  btn.classList.add('recording');
  btn.textContent = '■';
  document.getElementById('rec-hint').textContent = 'Recording… click to stop';
}

function stopRecording() {
  if (mediaRecorder && mediaRecorder.state !== 'inactive') {
    mediaRecorder.stop();
  }
  if (mediaStream) {
    mediaStream.getTracks().forEach(track => track.stop());  // release mic indicator between trials
  }
  isRecording = false;
}

async function handleRecordingStopped() {
  let blob;
  try {
    // Deliberately no `type` option here -- some WebKit/Safari versions return
    // an unusual or malformed mediaRecorder.mimeType value, and passing that
    // into Blob's type throws "The string did not match the expected pattern".
    // We don't need a specific type: the server converts via ffmpeg, which
    // detects the real format from file content, not from this label.
    blob = new Blob(audioChunks);
  } catch (err) {
    showError('Could not process the recording: ' + err.message);
    document.getElementById('analyzing-zone').classList.add('hidden');
    document.getElementById('rec-zone').classList.remove('hidden');
    return;
  }

  document.getElementById('rec-zone').classList.add('hidden');
  document.getElementById('analyzing-zone').classList.remove('hidden');

  try {
    const formData = new FormData();
    formData.append('session_id', sessionId);
    formData.append('audio', blob, 'recording.webm');

    const res = await fetch('/api/trial/submit', { method: 'POST', body: formData });
    const data = await res.json();

    document.getElementById('analyzing-zone').classList.add('hidden');

    if (!res.ok) {
      showError(data.error || 'Could not process recording.');
      document.getElementById('rec-zone').classList.remove('hidden');
      return;
    }

    showPrediction(data);
  } catch (err) {
    document.getElementById('analyzing-zone').classList.add('hidden');
    document.getElementById('rec-zone').classList.remove('hidden');
    console.error('Trial submit failed:', err);
    showError('Error submitting recording (check browser console): ' + err.message);
  }
}

function showPrediction(data) {
  entityApplicable = data.entity_applicable;

  document.getElementById('prediction-intent').textContent = `${data.icon} ${data.intent}`;
  document.getElementById('prediction-intent').classList.remove('empty');

  const entityEl = document.getElementById('prediction-entity');
  if (entityApplicable) {
    entityEl.textContent = data.entity;
    entityEl.classList.remove('empty');
  } else {
    entityEl.textContent = 'none detected';
    entityEl.classList.add('empty');
  }

  document.getElementById('timing-caption').textContent = `Processed in ${data.elapsed_seconds.toFixed(2)}s`;
  document.getElementById('legend-box').innerHTML = intentLegendHtml;

  document.getElementById('entity-judge-block').classList.toggle('hidden', !entityApplicable);
  document.getElementById('result-zone').classList.remove('hidden');
}

// ---------------------------------------------------------------------------
// Judgement confirmation
// ---------------------------------------------------------------------------
document.getElementById('confirm-btn').addEventListener('click', async () => {
  clearError();
  const intentAnswer = getRadioValue('intent_judge');
  const entityAnswer = getRadioValue('entity_judge');

  if (!intentAnswer) {
    showError('Please answer whether the command type was correct.');
    return;
  }
  if (entityApplicable && !entityAnswer) {
    showError('Please answer whether the detected item was correct.');
    return;
  }

  try {
    const res = await fetch('/api/trial/judge', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        session_id: sessionId,
        intent_correct: intentAnswer,
        entity_correct: entityApplicable ? entityAnswer : null,
      }),
    });
    const data = await res.json();
    if (!res.ok) { showError(data.error || 'Could not record judgement.'); return; }

    if (data.session_complete) {
      await startUmux();
      showScreen('umux');
    } else {
      currentTrialIndex = data.trial_index;
      totalTrials = data.total_trials;
      renderTrial(data.prompt_text);
    }
  } catch (err) {
    console.error('Judgement submit failed:', err);
    showError('Error recording judgement (check browser console): ' + err.message);
  }
});

// ---------------------------------------------------------------------------
// UMUX feedback (validated 4-item SUS short-form, Finstad 2010)
// ---------------------------------------------------------------------------
async function startUmux() {
  const res = await fetch('/api/umux/items');
  const data = await res.json();
  const container = document.getElementById('umux-items');
  container.innerHTML = '';

  data.items.forEach((item, i) => {
    const qNum = i + 1;
    const wrap = document.createElement('div');
    wrap.className = 'umux-item';
    wrap.innerHTML = `
      <p class="umux-question">${qNum}. ${item}</p>
      <div class="radio-group umux-scale" data-qnum="${qNum}">
        ${[1, 2, 3, 4, 5, 6, 7].map(v => `
          <div class="radio-pill">
            <input type="radio" name="umux_q${qNum}" id="umux-${qNum}-${v}" value="${v}">
            <label for="umux-${qNum}-${v}">${v}</label>
          </div>
        `).join('')}
      </div>
      <div class="umux-anchors"><span>Strongly disagree</span><span>Strongly agree</span></div>
    `;
    container.appendChild(wrap);
  });
}

document.getElementById('umux-submit-btn').addEventListener('click', async () => {
  clearError();
  const responses = [1, 2, 3, 4].map(q => getRadioValue(`umux_q${q}`));

  if (responses.some(r => r === null)) {
    showError('Please answer all 4 questions before submitting.');
    return;
  }

  try {
    const res = await fetch('/api/umux/submit', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ session_id: sessionId, responses: responses.map(Number) }),
    });
    const data = await res.json();
    if (!res.ok) { showError(data.error || 'Could not submit feedback.'); return; }
    showScreen('end');
  } catch (err) {
    console.error('UMUX submit failed:', err);
    showError('Error submitting feedback (check browser console): ' + err.message);
  }
});