Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
474fd80a95 | ||
|
|
bf5ee88610 | ||
|
|
db76289660 | ||
|
|
702780c61b | ||
|
|
eceb33052d | ||
|
|
32e89cca73 | ||
|
|
878fc3f8ca | ||
|
|
954e066859 | ||
|
|
ea7e349fc2 | ||
|
|
557fc77a52 | ||
|
|
d788ca39ec | ||
|
|
00f7334580 | ||
|
|
c5c0fb6b03 | ||
|
|
cc17b501cc | ||
|
|
1c8d429f81 | ||
|
|
7d447940b3 | ||
|
|
09341e2486 | ||
|
|
4cb41cdeca | ||
|
|
f189adb5d6 | ||
|
|
7acc61978d | ||
|
|
f322df540d |
@@ -20,6 +20,8 @@ When a character or NPC is created, its saved voice identity is masculine, femin
|
|||||||
|
|
||||||
System voice commands require the prefix `röstkommando`: `upprepa`, `avbryt`, `hjälp`, `alternativ`, or `paus`. Without the prefix, the same words remain ordinary dialogue. Speech recognition and synthesis are browser-dependent; release acceptance must record which real iPhone/Safari/VoiceOver and Android/Chrome/TalkBack combinations were actually checked.
|
System voice commands require the prefix `röstkommando`: `upprepa`, `avbryt`, `hjälp`, `alternativ`, or `paus`. Without the prefix, the same words remain ordinary dialogue. Speech recognition and synthesis are browser-dependent; release acceptance must record which real iPhone/Safari/VoiceOver and Android/Chrome/TalkBack combinations were actually checked.
|
||||||
|
|
||||||
|
On iPhone and iPad, Safari may require a direct tap or key press before it permits speech output. The frontend primes speech silently on the first player gesture and then reads game replies automatically. The status reports speech as in progress only after the browser's `onstart` event. Voice settings include **Testa ljudet**; if a game reply does not start, a **Läs senaste repliken** button appears in the dialogue. When Safari reports speech started but the player hears nothing, the status suggests checking device volume and audio output. The introduction is only marked as heard after the browser reports that speech actually started. The speech queue avoids an immediate `cancel()` followed by `speak()`, which older WebKit releases can drop.
|
||||||
|
|
||||||
The browser and API stay on the same public origin, `https://ai-spel.k3s.k0.nu`. The ingress routes `/` to this frontend service and routes `/api`, `/version`, and `/health` to the Rust service. That keeps the current server-side session cookie same-origin and avoids browser CORS configuration.
|
The browser and API stay on the same public origin, `https://ai-spel.k3s.k0.nu`. The ingress routes `/` to this frontend service and routes `/api`, `/version`, and `/health` to the Rust service. That keeps the current server-side session cookie same-origin and avoids browser CORS configuration.
|
||||||
|
|
||||||
## Release flow
|
## Release flow
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ apiVersion: v2
|
|||||||
name: ai-spel-frontend
|
name: ai-spel-frontend
|
||||||
description: Static web frontend for the Swedish life simulator
|
description: Static web frontend for the Swedish life simulator
|
||||||
type: application
|
type: application
|
||||||
version: 0.1.33
|
version: 0.1.43
|
||||||
appVersion: "0.1.33"
|
appVersion: "0.1.43"
|
||||||
+198
-12
@@ -37,7 +37,8 @@
|
|||||||
<section id="auth" aria-labelledby="auth-heading">
|
<section id="auth" aria-labelledby="auth-heading">
|
||||||
<h1 id="auth-heading">Svensk livssimulator</h1>
|
<h1 id="auth-heading">Svensk livssimulator</h1>
|
||||||
<p id="auth-intro">Välkommen till en svensk, dialogbaserad livssimulator utan bilder. Världen beskrivs med ljud, dofter, temperatur, beröring och riktning. Skapa konto med ett användarnamn och ett lösenord på minst tolv tecken. Berätta sedan fritt om en vuxen person. Du får redigera och godkänna personens biografi och startplats. Spela genom att skriva eller tala vad du gör och frågar. Personer du möter kan minnas tidigare samtal. Spelets repliker läses automatiskt upp på svenska och finns kvar som text. Har du redan konto väljer du Logga in.</p>
|
<p id="auth-intro">Välkommen till en svensk, dialogbaserad livssimulator utan bilder. Världen beskrivs med ljud, dofter, temperatur, beröring och riktning. Skapa konto med ett användarnamn och ett lösenord på minst tolv tecken. Berätta sedan fritt om en vuxen person. Du får redigera och godkänna personens biografi och startplats. Spela genom att skriva eller tala vad du gör och frågar. Personer du möter kan minnas tidigare samtal. Spelets repliker läses automatiskt upp på svenska och finns kvar som text. Har du redan konto väljer du Logga in.</p>
|
||||||
<button type="button" id="auth-repeat-intro">Läs introduktionen igen</button>
|
<button type="button" id="auth-repeat-intro">Läs introduktionen</button>
|
||||||
|
<p id="auth-voice-status" role="status" aria-live="polite">På iPhone och iPad kan du behöva trycka på Läs introduktionen igen en gång för att aktivera uppläsningen.</p>
|
||||||
<p id="auth-status" role="status" aria-live="polite" hidden></p>
|
<p id="auth-status" role="status" aria-live="polite" hidden></p>
|
||||||
<form id="auth-form">
|
<form id="auth-form">
|
||||||
<label>Användarnamn <input name="username" required minlength="3" maxlength="40" autocomplete="username"></label>
|
<label>Användarnamn <input name="username" required minlength="3" maxlength="40" autocomplete="username"></label>
|
||||||
@@ -71,7 +72,7 @@
|
|||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
<footer id="version-footer">Frontend <span id="frontend-version">__FRONTEND_VERSION__</span> · API <span id="api-version">hämtar version…</span></footer>
|
<footer id="version-footer">Frontend <span id="frontend-version">__FRONTEND_VERSION__</span> · API <span id="api-version">hämtar version…</span></footer>
|
||||||
<script src="/voice.js"></script>
|
<script src="/voice.js?v=__FRONTEND_VERSION__"></script>
|
||||||
<script>
|
<script>
|
||||||
const auth = document.querySelector('#auth');
|
const auth = document.querySelector('#auth');
|
||||||
const play = document.querySelector('#play');
|
const play = document.querySelector('#play');
|
||||||
@@ -88,6 +89,7 @@
|
|||||||
let pendingDialogue = null;
|
let pendingDialogue = null;
|
||||||
let latestAssistantText = '';
|
let latestAssistantText = '';
|
||||||
let latestAssistantVoice = {role: 'narrator', voiceIdentity: 'neutral'};
|
let latestAssistantVoice = {role: 'narrator', voiceIdentity: 'neutral'};
|
||||||
|
let speechRecoveryElement = null;
|
||||||
const voiceSettingsKey = 'ai-spel.voice-settings.v2';
|
const voiceSettingsKey = 'ai-spel.voice-settings.v2';
|
||||||
let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey, 'ai-spel.voice-settings.v1');
|
let voiceSettings = window.GameVoice.loadSettings(localStorage, voiceSettingsKey, 'ai-spel.voice-settings.v1');
|
||||||
let voiceController;
|
let voiceController;
|
||||||
@@ -95,6 +97,9 @@
|
|||||||
let voiceDisclosureAcknowledged = false;
|
let voiceDisclosureAcknowledged = false;
|
||||||
let adminOffset = 0;
|
let adminOffset = 0;
|
||||||
const adminPageSize = 50;
|
const adminPageSize = 50;
|
||||||
|
let presenceTimer = null;
|
||||||
|
let playerMessagesPrimed = false;
|
||||||
|
const seenPlayerMessageIds = new Set();
|
||||||
|
|
||||||
async function request(path, options = {}) {
|
async function request(path, options = {}) {
|
||||||
const response = await fetch(path, {credentials: 'same-origin', ...options});
|
const response = await fetch(path, {credentials: 'same-origin', ...options});
|
||||||
@@ -112,11 +117,29 @@
|
|||||||
latestAssistantText = content;
|
latestAssistantText = content;
|
||||||
latestAssistantVoice = options.voice || {role: 'narrator', voiceIdentity: 'neutral'};
|
latestAssistantVoice = options.voice || {role: 'narrator', voiceIdentity: 'neutral'};
|
||||||
if (options.speak !== false && location.pathname !== '/admin') {
|
if (options.speak !== false && location.pathname !== '/admin') {
|
||||||
voiceController?.speak(content, latestAssistantVoice);
|
voiceController?.speak(content, {...latestAssistantVoice, onStart: clearSpeechRecovery});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
function biographyContextText(context) {
|
||||||
|
if (!context || typeof context !== 'object') return '';
|
||||||
|
const details = [];
|
||||||
|
if (typeof context.current_situation === 'string' && context.current_situation.trim()) {
|
||||||
|
details.push(`Nuvarande situation: ${context.current_situation.trim()}.`);
|
||||||
|
}
|
||||||
|
for (const [key, label] of [
|
||||||
|
['skills', 'Färdigheter'],
|
||||||
|
['possessions', 'Ägodelar som nämnts'],
|
||||||
|
['obligations', 'Åtaganden'],
|
||||||
|
['past_events', 'Tidigare händelser'],
|
||||||
|
['known_people', 'Kända personer'],
|
||||||
|
]) {
|
||||||
|
const values = context[key];
|
||||||
|
if (Array.isArray(values) && values.length) details.push(`${label}: ${values.join(', ')}.`);
|
||||||
|
}
|
||||||
|
return details.length ? `\n\nStartuppgifter att granska: ${details.join(' ')}` : '';
|
||||||
|
}
|
||||||
function explainDialogueError(error) {
|
function explainDialogueError(error) {
|
||||||
const code = String(error?.message || '');
|
const code = String(error?.message || '');
|
||||||
const known = {
|
const known = {
|
||||||
@@ -128,7 +151,38 @@
|
|||||||
? 'Jag kunde inte behandla det just nu. Försök igen om en liten stund.'
|
? 'Jag kunde inte behandla det just nu. Försök igen om en liten stund.'
|
||||||
: code || 'Ett tillfälligt fel uppstod. Försök igen om en liten stund.');
|
: code || 'Ett tillfälligt fel uppstod. Försök igen om en liten stund.');
|
||||||
}
|
}
|
||||||
function setVoiceStatus(message = '') { document.querySelector('#voice-status').textContent = message; }
|
function setVoiceStatus(message = '') {
|
||||||
|
document.querySelector('#voice-status').textContent = message;
|
||||||
|
const authVoiceStatus = document.querySelector('#auth-voice-status');
|
||||||
|
if (authVoiceStatus && !auth.hidden && message) authVoiceStatus.textContent = message;
|
||||||
|
}
|
||||||
|
function clearSpeechRecovery() {
|
||||||
|
speechRecoveryElement?.remove();
|
||||||
|
speechRecoveryElement = null;
|
||||||
|
const status = document.querySelector('#voice-status');
|
||||||
|
if (status?.textContent.startsWith('Uppläsningen startade inte')) setVoiceStatus('Läser upp dialog.');
|
||||||
|
}
|
||||||
|
function showSpeechRecovery(message) {
|
||||||
|
clearSpeechRecovery();
|
||||||
|
const item = document.createElement('li');
|
||||||
|
item.dataset.role = 'voice-controls';
|
||||||
|
item.setAttribute('aria-label', 'Uppläsning kunde inte starta');
|
||||||
|
const explanation = document.createElement('p');
|
||||||
|
explanation.textContent = message;
|
||||||
|
const retry = document.createElement('button');
|
||||||
|
retry.type = 'button';
|
||||||
|
retry.textContent = 'Läs senaste repliken';
|
||||||
|
retry.addEventListener('click', () => {
|
||||||
|
voiceController.activateFromUserGesture();
|
||||||
|
if (voiceController.speak(latestAssistantText, {...latestAssistantVoice, onStart: clearSpeechRecovery})) {
|
||||||
|
clearSpeechRecovery();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
item.append(explanation, retry);
|
||||||
|
transcript.append(item);
|
||||||
|
item.scrollIntoView({block: 'end'});
|
||||||
|
speechRecoveryElement = item;
|
||||||
|
}
|
||||||
function refreshMicrophone() {
|
function refreshMicrophone() {
|
||||||
const button = document.querySelector('#microphone');
|
const button = document.querySelector('#microphone');
|
||||||
if (button) button.disabled = play.hidden || messageField.disabled || submitBusy;
|
if (button) button.disabled = play.hidden || messageField.disabled || submitBusy;
|
||||||
@@ -164,12 +218,20 @@
|
|||||||
microphone.textContent = listening ? 'Avsluta mikrofon' : 'Starta mikrofon';
|
microphone.textContent = listening ? 'Avsluta mikrofon' : 'Starta mikrofon';
|
||||||
refreshMicrophone();
|
refreshMicrophone();
|
||||||
if (message) setVoiceStatus(message);
|
if (message) setVoiceStatus(message);
|
||||||
|
else if (state === 'IDLE') setVoiceStatus('');
|
||||||
|
if (/Läs senaste repliken/.test(message)) showSpeechRecovery(message);
|
||||||
playVoiceSignal(state === 'IDLE' && /Mikrofonen är avstängd/.test(message) ? 'STOPPED' : state);
|
playVoiceSignal(state === 'IDLE' && /Mikrofonen är avstängd/.test(message) ? 'STOPPED' : state);
|
||||||
},
|
},
|
||||||
onTranscript(text) {
|
onTranscript(text) {
|
||||||
return submitMessage(text, 'voice');
|
return submitMessage(text, 'voice');
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
function primeSpeechFromGesture(event) {
|
||||||
|
if (!event.isTrusted) return;
|
||||||
|
voiceController.activateFromUserGesture();
|
||||||
|
}
|
||||||
|
document.addEventListener('pointerdown', primeSpeechFromGesture, true);
|
||||||
|
document.addEventListener('keydown', primeSpeechFromGesture, true);
|
||||||
persistVoiceSettings();
|
persistVoiceSettings();
|
||||||
|
|
||||||
function addVoiceButton(parent, label, handler) {
|
function addVoiceButton(parent, label, handler) {
|
||||||
@@ -205,8 +267,16 @@
|
|||||||
signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel);
|
signalLabel.append(signals, document.createTextNode(' Ljudsignal vid röstinmatning')); fieldset.append(signalLabel);
|
||||||
item.append(fieldset);
|
item.append(fieldset);
|
||||||
const speakLatest = () => {
|
const speakLatest = () => {
|
||||||
if (!voiceController.speak(latestAssistantText, latestAssistantVoice)) setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.');
|
voiceController.activateFromUserGesture();
|
||||||
|
if (!voiceController.speak(latestAssistantText, {...latestAssistantVoice, onStart: clearSpeechRecovery})) setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.');
|
||||||
};
|
};
|
||||||
|
const testSpeech = () => {
|
||||||
|
voiceController.activateFromUserGesture();
|
||||||
|
if (!voiceController.speak('Det här är ett test av uppläsningen.', {role: 'narrator', voiceIdentity: 'neutral', onStart: clearSpeechRecovery})) {
|
||||||
|
setVoiceStatus('Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
addVoiceButton(item, 'Testa ljudet', testSpeech);
|
||||||
addVoiceButton(item, 'Läs senaste repliken', speakLatest);
|
addVoiceButton(item, 'Läs senaste repliken', speakLatest);
|
||||||
addVoiceButton(item, 'Upprepa', speakLatest);
|
addVoiceButton(item, 'Upprepa', speakLatest);
|
||||||
addVoiceButton(item, 'Avbryt uppläsning', () => voiceController.cancelSpeech());
|
addVoiceButton(item, 'Avbryt uppläsning', () => voiceController.cancelSpeech());
|
||||||
@@ -285,29 +355,44 @@
|
|||||||
auth.hidden = true;
|
auth.hidden = true;
|
||||||
adminView.hidden = true;
|
adminView.hidden = true;
|
||||||
play.hidden = false;
|
play.hidden = false;
|
||||||
document.querySelector('#version-footer').hidden = true;
|
|
||||||
refreshMicrophone();
|
refreshMicrophone();
|
||||||
messageField.focus();
|
messageField.focus();
|
||||||
}
|
}
|
||||||
function showAdmin() {
|
function showAdmin() {
|
||||||
|
if (presenceTimer) clearInterval(presenceTimer);
|
||||||
|
presenceTimer = null;
|
||||||
voiceController.stopAll();
|
voiceController.stopAll();
|
||||||
auth.hidden = true;
|
auth.hidden = true;
|
||||||
play.hidden = true;
|
play.hidden = true;
|
||||||
adminView.hidden = false;
|
adminView.hidden = false;
|
||||||
document.querySelector('#version-footer').hidden = true;
|
document.querySelector('#version-footer').hidden = true;
|
||||||
}
|
}
|
||||||
const introductionKey = 'ai-spel.introduction-heard.v1';
|
const introductionKey = 'ai-spel.introduction-heard.v2';
|
||||||
function playFirstVisitIntroduction() {
|
function playFirstVisitIntroduction() {
|
||||||
let alreadyHeard = false;
|
let alreadyHeard = false;
|
||||||
try { alreadyHeard = localStorage.getItem(introductionKey) === 'true'; } catch (_) { /* Keep the intro available for this visit. */ }
|
try { alreadyHeard = localStorage.getItem(introductionKey) === 'true'; } catch (_) { /* Keep the intro available for this visit. */ }
|
||||||
if (alreadyHeard) return;
|
if (alreadyHeard) return;
|
||||||
const introduction = document.querySelector('#auth-intro').textContent;
|
const isAppleMobile = /iPad|iPhone|iPod/i.test(navigator.userAgent)
|
||||||
if (voiceController.speak(introduction, {role: 'narrator', voiceIdentity: 'neutral'})) {
|
|| (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
|
||||||
try { localStorage.setItem(introductionKey, 'true'); } catch (_) { /* The page text remains available. */ }
|
if (isAppleMobile) {
|
||||||
|
document.querySelector('#auth-repeat-intro').textContent = 'Läs introduktionen';
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
const introduction = document.querySelector('#auth-intro').textContent;
|
||||||
|
voiceController.speak(introduction, {
|
||||||
|
role: 'narrator', voiceIdentity: 'neutral',
|
||||||
|
onStart() { try { localStorage.setItem(introductionKey, 'true'); } catch (_) { /* The page text remains available. */ } },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
document.querySelector('#auth-repeat-intro').addEventListener('click', () => {
|
document.querySelector('#auth-repeat-intro').addEventListener('click', () => {
|
||||||
if (location.pathname !== '/admin') voiceController.speak(document.querySelector('#auth-intro').textContent, {role: 'narrator', voiceIdentity: 'neutral'});
|
if (location.pathname !== '/admin') {
|
||||||
|
voiceController.activateFromUserGesture();
|
||||||
|
const introduction = document.querySelector('#auth-intro').textContent;
|
||||||
|
voiceController.speak(introduction, {
|
||||||
|
role: 'narrator', voiceIdentity: 'neutral',
|
||||||
|
onStart() { try { localStorage.setItem(introductionKey, 'true'); } catch (_) { /* The page text remains available. */ } },
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
function explainAuthError(error, intent) {
|
function explainAuthError(error, intent) {
|
||||||
if (intent === 'register' && error?.message === 'username_unavailable') return 'Det användarnamnet används redan. Välj ett annat och försök igen.';
|
if (intent === 'register' && error?.message === 'username_unavailable') return 'Det användarnamnet används redan. Välj ett annat och försök igen.';
|
||||||
@@ -361,6 +446,7 @@
|
|||||||
const trace = await request(`/api/admin/players/${encodeURIComponent(accountId)}/trace?limit=200`);
|
const trace = await request(`/api/admin/players/${encodeURIComponent(accountId)}/trace?limit=200`);
|
||||||
const entries = [
|
const entries = [
|
||||||
...trace.events.map(event => ({kind: 'Världshändelse', at: event.recorded_at, order: event.commit_sequence, data: event})),
|
...trace.events.map(event => ({kind: 'Världshändelse', at: event.recorded_at, order: event.commit_sequence, data: event})),
|
||||||
|
...trace.player_messages.map(message => ({kind: 'Privat spelarmeddelande', at: message.recorded_at, order: message.sequence, data: message})),
|
||||||
...trace.ai_interactions.map(interaction => ({kind: `AI ${interaction.prompt_id} · ${interaction.outcome}`, at: interaction.requested_at, order: interaction.correlation_id, data: interaction}))
|
...trace.ai_interactions.map(interaction => ({kind: `AI ${interaction.prompt_id} · ${interaction.outcome}`, at: interaction.requested_at, order: interaction.correlation_id, data: interaction}))
|
||||||
].sort((a, b) => String(a.at).localeCompare(String(b.at)));
|
].sort((a, b) => String(a.at).localeCompare(String(b.at)));
|
||||||
adminElement('h2', 'Kronologiskt spelarspår');
|
adminElement('h2', 'Kronologiskt spelarspår');
|
||||||
@@ -501,8 +587,22 @@
|
|||||||
}
|
}
|
||||||
await loadSavedDialogue();
|
await loadSavedDialogue();
|
||||||
setStage('game');
|
setStage('game');
|
||||||
|
startPlayerPresence();
|
||||||
|
announcePlayerChatHelp();
|
||||||
|
}
|
||||||
|
function announcePlayerChatHelp() {
|
||||||
|
const key = 'ai-spel.player-chat-help.v1';
|
||||||
|
try {
|
||||||
|
if (localStorage.getItem(key) === 'true') return;
|
||||||
|
localStorage.setItem(key, 'true');
|
||||||
|
} catch (_) { /* The help remains available through dialogue questions. */ }
|
||||||
|
appendLine('assistant', 'Du kan fråga vilka andra spelare som är här, säga “Säg till Robin: hej” för att skriva direkt till en spelare eller be mig visa meddelanden från spelare.');
|
||||||
}
|
}
|
||||||
async function handleLogout() {
|
async function handleLogout() {
|
||||||
|
if (presenceTimer) clearInterval(presenceTimer);
|
||||||
|
presenceTimer = null;
|
||||||
|
playerMessagesPrimed = false;
|
||||||
|
seenPlayerMessageIds.clear();
|
||||||
await request('/api/logout', {method: 'POST'});
|
await request('/api/logout', {method: 'POST'});
|
||||||
if (location.pathname !== '/admin') appendLine('assistant', 'Du har loggat ut. Logga in igen för att fortsätta spela.');
|
if (location.pathname !== '/admin') appendLine('assistant', 'Du har loggat ut. Logga in igen för att fortsätta spela.');
|
||||||
characterDraft = null;
|
characterDraft = null;
|
||||||
@@ -542,7 +642,7 @@
|
|||||||
body: JSON.stringify({input: message, current_draft: characterDraft}),
|
body: JSON.stringify({input: message, current_draft: characterDraft}),
|
||||||
});
|
});
|
||||||
characterDraft = proposal.draft;
|
characterDraft = proposal.draft;
|
||||||
appendLine('assistant', `${proposal.message}\n\nFörslag: ${characterDraft.name}, ${characterDraft.age} år.\n${characterDraft.biography}\n\nSkriv ändringar eller “godkänn”.`);
|
appendLine('assistant', `${proposal.message}\n\nFörslag: ${characterDraft.name}, ${characterDraft.age} år.\n${characterDraft.biography}${biographyContextText(characterDraft.life_context)}\n\nSkriv ändringar eller “godkänn”.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
appendLine('assistant', `Jag kunde inte ta fram ett biografiförslag: ${error.message}. Försök igen när tjänsten svarar.`);
|
appendLine('assistant', `Jag kunde inte ta fram ett biografiförslag: ${error.message}. Försök igen när tjänsten svarar.`);
|
||||||
}
|
}
|
||||||
@@ -569,6 +669,7 @@
|
|||||||
else await proposeWorld(message);
|
else await proposeWorld(message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (await handlePlayerMessageCommand(message)) return;
|
||||||
const idempotencyKey = pendingDialogue?.message === message
|
const idempotencyKey = pendingDialogue?.message === message
|
||||||
? pendingDialogue.idempotencyKey
|
? pendingDialogue.idempotencyKey
|
||||||
: crypto.randomUUID();
|
: crypto.randomUUID();
|
||||||
@@ -637,6 +738,10 @@
|
|||||||
refreshMicrophone();
|
refreshMicrophone();
|
||||||
voiceController.stopListening();
|
voiceController.stopListening();
|
||||||
voiceController.cancelSpeech();
|
voiceController.cancelSpeech();
|
||||||
|
// cancel() above may also have discarded Safari's silent speech-channel
|
||||||
|
// primer. Restore it synchronously inside the submit gesture, before
|
||||||
|
// waiting for the API response that will be read aloud.
|
||||||
|
voiceController.activateFromUserGesture();
|
||||||
messageField.value = '';
|
messageField.value = '';
|
||||||
try {
|
try {
|
||||||
if (stage === 'biography') await handleBiography(message);
|
if (stage === 'biography') await handleBiography(message);
|
||||||
@@ -649,6 +754,87 @@
|
|||||||
if (!play.hidden && !messageField.disabled) messageField.focus();
|
if (!play.hidden && !messageField.disabled) messageField.focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async function handlePlayerMessageCommand(message) {
|
||||||
|
const listPlayers = /^(?:vilka (?:andra )?spelare är här|finns det andra spelare här)\??$/i.test(message.trim());
|
||||||
|
const sendMatch = /^(?:säg|skriv|skicka) till ([^:]{1,80}):\s*(.{1,4000})$/is.exec(message.trim());
|
||||||
|
const readMessages = /^(?:visa|läs upp) (?:de senaste )?meddelanden från spelare\.?$/i.test(message.trim());
|
||||||
|
if (!listPlayers && !sendMatch && !readMessages) return false;
|
||||||
|
appendLine('user', message);
|
||||||
|
try {
|
||||||
|
if (listPlayers) {
|
||||||
|
const players = await request('/api/players/nearby');
|
||||||
|
appendLine('assistant', players.length
|
||||||
|
? `Här finns ${players.map(player => player.name).join(', ')} att prata med.`
|
||||||
|
: 'Just nu finns inga andra inloggade spelare på platsen.');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (readMessages) {
|
||||||
|
const messages = await request('/api/player/messages');
|
||||||
|
const recent = messages.slice(-10);
|
||||||
|
if (recent.length === 0) appendLine('assistant', 'Du har inga sparade meddelanden från andra spelare.');
|
||||||
|
else for (const entry of recent) {
|
||||||
|
seenPlayerMessageIds.add(entry.id);
|
||||||
|
const isIncoming = entry.direction === 'incoming';
|
||||||
|
appendLine(isIncoming ? 'assistant' : 'user', entry.message, isIncoming
|
||||||
|
? {speaker: entry.sender_name, voice: {characterId: entry.sender_character_id, voiceIdentity: entry.sender_voice_identity}}
|
||||||
|
: {speaker: 'Du', speak: false});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const players = await request('/api/players/nearby');
|
||||||
|
const targetName = sendMatch[1].trim();
|
||||||
|
const matches = players.filter(player => player.name.localeCompare(targetName, 'sv', {sensitivity: 'base'}) === 0);
|
||||||
|
if (matches.length === 0) {
|
||||||
|
appendLine('assistant', `Jag hittar ingen inloggad spelare som heter ${targetName} på platsen. Fråga vilka andra spelare som är här.`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (matches.length > 1) {
|
||||||
|
appendLine('assistant', `Flera spelare heter ${targetName}. Säg vilket namn de använder i spelet.`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const key = pendingPlayerMessage?.text === sendMatch[2] && pendingPlayerMessage?.target === matches[0].character_id
|
||||||
|
? pendingPlayerMessage.key
|
||||||
|
: crypto.randomUUID();
|
||||||
|
pendingPlayerMessage = {text: sendMatch[2], target: matches[0].character_id, key};
|
||||||
|
const result = await request(`/api/players/${encodeURIComponent(matches[0].character_id)}/messages`, {
|
||||||
|
method: 'POST', headers: {'content-type': 'application/json'},
|
||||||
|
body: JSON.stringify({message: sendMatch[2], idempotency_key: key}),
|
||||||
|
});
|
||||||
|
pendingPlayerMessage = null;
|
||||||
|
appendLine('assistant', `Ditt meddelande skickades till ${matches[0].name}.`);
|
||||||
|
return Boolean(result.message_id);
|
||||||
|
} catch (error) {
|
||||||
|
messageField.value = message;
|
||||||
|
appendLine('assistant', `Spelarmeddelandet kunde inte hanteras: ${explainDialogueError(error)}`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let pendingPlayerMessage = null;
|
||||||
|
async function startPlayerPresence() {
|
||||||
|
if (presenceTimer) clearInterval(presenceTimer);
|
||||||
|
const refresh = async (announceNewMessages) => {
|
||||||
|
if (play.hidden || stage !== 'game') return;
|
||||||
|
try {
|
||||||
|
await request('/api/presence/heartbeat', {method: 'POST'});
|
||||||
|
const messages = await request('/api/player/messages');
|
||||||
|
for (const entry of messages) {
|
||||||
|
if (seenPlayerMessageIds.has(entry.id)) continue;
|
||||||
|
if (announceNewMessages && entry.direction === 'incoming'
|
||||||
|
&& (submitBusy || ['LISTENING', 'PROCESSING', 'SPEAKING'].includes(voiceController.getState()))) continue;
|
||||||
|
seenPlayerMessageIds.add(entry.id);
|
||||||
|
if (announceNewMessages && entry.direction === 'incoming') {
|
||||||
|
appendLine('assistant', entry.message, {
|
||||||
|
speaker: entry.sender_name,
|
||||||
|
voice: {characterId: entry.sender_character_id, voiceIdentity: entry.sender_voice_identity},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
playerMessagesPrimed = true;
|
||||||
|
} catch (_) { /* Presence is a hint; the text game remains usable if it is unavailable. */ }
|
||||||
|
};
|
||||||
|
await refresh(playerMessagesPrimed);
|
||||||
|
presenceTimer = setInterval(() => refresh(true), 30_000);
|
||||||
|
}
|
||||||
document.querySelector('#message-form').addEventListener('submit', async (event) => {
|
document.querySelector('#message-form').addEventListener('submit', async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await submitMessage(messageField.value);
|
await submitMessage(messageField.value);
|
||||||
|
|||||||
+3
-1
@@ -10,7 +10,9 @@ server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
add_header Cache-Control "no-cache" always;
|
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
|
||||||
|
add_header Pragma "no-cache" always;
|
||||||
|
add_header Expires "0" always;
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ set -euo pipefail
|
|||||||
test -s index.html
|
test -s index.html
|
||||||
grep -q '<html lang="sv">' index.html
|
grep -q '<html lang="sv">' index.html
|
||||||
grep -q 'id="frontend-version">__FRONTEND_VERSION__' index.html
|
grep -q 'id="frontend-version">__FRONTEND_VERSION__' index.html
|
||||||
|
grep -q '<script src="/voice.js?v=__FRONTEND_VERSION__"></script>' index.html
|
||||||
|
grep -q 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0' nginx/default.conf
|
||||||
grep -q "request('/version')" index.html
|
grep -q "request('/version')" index.html
|
||||||
grep -Fq "credentials: 'same-origin'" index.html
|
grep -Fq "credentials: 'same-origin'" index.html
|
||||||
grep -q 'id="dialogue" aria-label="Dialoghistorik" role="log" aria-live="polite"' index.html
|
grep -q 'id="dialogue" aria-label="Dialoghistorik" role="log" aria-live="polite"' index.html
|
||||||
@@ -12,14 +14,22 @@ grep -q 'id="auth-intro"' index.html
|
|||||||
grep -q 'Skapa konto med ett användarnamn och ett lösenord på minst tolv tecken' index.html
|
grep -q 'Skapa konto med ett användarnamn och ett lösenord på minst tolv tecken' index.html
|
||||||
grep -q 'id="auth-repeat-intro"' index.html
|
grep -q 'id="auth-repeat-intro"' index.html
|
||||||
grep -q 'function playFirstVisitIntroduction' index.html
|
grep -q 'function playFirstVisitIntroduction' index.html
|
||||||
|
grep -q 'id="auth-voice-status" role="status" aria-live="polite"' index.html
|
||||||
|
grep -q 'activateFromUserGesture' index.html
|
||||||
|
grep -q 'function showSpeechRecovery' index.html
|
||||||
|
grep -q "retry.textContent = 'Läs senaste repliken'" index.html
|
||||||
grep -q "location.pathname !== '/admin'" index.html
|
grep -q "location.pathname !== '/admin'" index.html
|
||||||
grep -q "if (options.speak !== false && location.pathname !== '/admin')" index.html
|
grep -q "if (options.speak !== false && location.pathname !== '/admin')" index.html
|
||||||
grep -q 'id="microphone" aria-pressed="false"' index.html
|
grep -q 'id="microphone" aria-pressed="false"' index.html
|
||||||
grep -q 'id="voice-status" role="status" aria-live="polite"' index.html
|
grep -q 'id="voice-status" role="status" aria-live="polite"' index.html
|
||||||
grep -q "window.GameVoice.createVoiceController" index.html
|
grep -q "window.GameVoice.createVoiceController" index.html
|
||||||
|
grep -q "addVoiceButton(item, 'Testa ljudet'" index.html
|
||||||
grep -q 'Jag förstår – starta mikrofonen' index.html
|
grep -q 'Jag förstår – starta mikrofonen' index.html
|
||||||
grep -q 'supportsRecognition' voice.js
|
grep -q 'supportsRecognition' voice.js
|
||||||
grep -q 'characterVoiceProfile' voice.js
|
grep -q 'characterVoiceProfile' voice.js
|
||||||
|
grep -q 'utterance.volume = 1' voice.js
|
||||||
|
grep -q 'Webbläsaren har startat talsyntesen' voice.js
|
||||||
|
grep -q 'WebKit versions before Safari 27' voice.js
|
||||||
grep -q "voiceIdentity: speakingNpc.voice_identity" index.html
|
grep -q "voiceIdentity: speakingNpc.voice_identity" index.html
|
||||||
grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html
|
grep -q "role: 'narrator', voiceIdentity: 'neutral'" index.html
|
||||||
grep -q 'COPY voice.js /usr/share/nginx/html/voice.js' Dockerfile
|
grep -q 'COPY voice.js /usr/share/nginx/html/voice.js' Dockerfile
|
||||||
@@ -30,6 +40,10 @@ grep -q "event.key === 'Enter' && !event.shiftKey" index.html
|
|||||||
grep -q "request('/api/character/draft'" index.html
|
grep -q "request('/api/character/draft'" index.html
|
||||||
grep -q "request('/api/world/draft'" index.html
|
grep -q "request('/api/world/draft'" index.html
|
||||||
grep -q "request('/api/dialogue'" index.html
|
grep -q "request('/api/dialogue'" index.html
|
||||||
|
grep -q "request('/api/presence/heartbeat'" index.html
|
||||||
|
grep -q "request('/api/players/nearby'" index.html
|
||||||
|
grep -q "request('/api/player/messages'" index.html
|
||||||
|
grep -q "Säg till Robin" index.html
|
||||||
grep -q "result.intent === 'move' || result.intent === 'meet'" index.html
|
grep -q "result.intent === 'move' || result.intent === 'meet'" index.html
|
||||||
grep -q 'dialogue_temporarily_unavailable:' index.html
|
grep -q 'dialogue_temporarily_unavailable:' index.html
|
||||||
grep -q 'id="admin-view"' index.html
|
grep -q 'id="admin-view"' index.html
|
||||||
|
|||||||
+88
-5
@@ -14,6 +14,7 @@ function makeHarness({supported = true, tts = true, onTranscript = () => Promise
|
|||||||
const utterances = [];
|
const utterances = [];
|
||||||
const states = [];
|
const states = [];
|
||||||
const timers = new Map();
|
const timers = new Map();
|
||||||
|
const timerDelays = new Map();
|
||||||
let timerId = 0;
|
let timerId = 0;
|
||||||
class Recognition {
|
class Recognition {
|
||||||
constructor() { recognitions.push(this); }
|
constructor() { recognitions.push(this); }
|
||||||
@@ -31,8 +32,8 @@ function makeHarness({supported = true, tts = true, onTranscript = () => Promise
|
|||||||
cancel() { this.cancelled += 1; },
|
cancel() { this.cancelled += 1; },
|
||||||
} : undefined;
|
} : undefined;
|
||||||
const window = {
|
const window = {
|
||||||
setTimeout(callback) { const id = ++timerId; timers.set(id, callback); return id; },
|
setTimeout(callback, delay = 0) { const id = ++timerId; timers.set(id, callback); timerDelays.set(id, delay); return id; },
|
||||||
clearTimeout(id) { timers.delete(id); },
|
clearTimeout(id) { timers.delete(id); timerDelays.delete(id); },
|
||||||
speechSynthesis: synthesis,
|
speechSynthesis: synthesis,
|
||||||
SpeechSynthesisUtterance: tts ? Utterance : undefined,
|
SpeechSynthesisUtterance: tts ? Utterance : undefined,
|
||||||
SpeechRecognition: supported ? Recognition : undefined,
|
SpeechRecognition: supported ? Recognition : undefined,
|
||||||
@@ -43,7 +44,14 @@ function makeHarness({supported = true, tts = true, onTranscript = () => Promise
|
|||||||
onState: (state, message) => states.push({state, message}),
|
onState: (state, message) => states.push({state, message}),
|
||||||
onTranscript,
|
onTranscript,
|
||||||
});
|
});
|
||||||
return {controller, recognitions, utterances, synthesis, states, timers};
|
return {
|
||||||
|
controller, recognitions, utterances, synthesis, states, timers,
|
||||||
|
runTimer(delay) {
|
||||||
|
const match = [...timerDelays.entries()].find(([, value]) => value === delay);
|
||||||
|
if (!match) throw new Error(`No timer found with delay ${delay}`);
|
||||||
|
timers.get(match[0])();
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
test('recognition is single-shot Swedish and sends only final transcript once', async () => {
|
test('recognition is single-shot Swedish and sends only final transcript once', async () => {
|
||||||
@@ -109,17 +117,89 @@ test('recognition watchdog closes a session that never ends and permits retry',
|
|||||||
assert.equal(h.recognitions.length, 2);
|
assert.equal(h.recognitions.length, 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('TTS selects Swedish fallback, applies rate, and cancellation invalidates callbacks', () => {
|
test('TTS uses the system default voice unless a named Swedish voice is selected', () => {
|
||||||
const h = makeHarness();
|
const h = makeHarness();
|
||||||
h.controller.update({mode: 'game-voice', rate: 1.4});
|
h.controller.update({mode: 'game-voice', rate: 1.4});
|
||||||
assert.equal(h.controller.speak('Hej.'), true);
|
assert.equal(h.controller.speak('Hej.'), true);
|
||||||
const first = h.utterances[0];
|
const first = h.utterances[0];
|
||||||
assert.equal(first.lang, 'sv-SE');
|
assert.equal(first.lang, 'sv-SE');
|
||||||
assert.equal(first.rate, 1.4);
|
assert.equal(first.rate, 1.4);
|
||||||
assert.equal(first.voice.name, 'Svensk');
|
assert.equal(first.voice, null);
|
||||||
h.controller.cancelSpeech();
|
h.controller.cancelSpeech();
|
||||||
first.onend();
|
first.onend();
|
||||||
assert.equal(h.controller.getState(), 'IDLE');
|
assert.equal(h.controller.getState(), 'IDLE');
|
||||||
|
assert.equal(h.controller.speak('Namngiven röst.', {voiceName: 'Svensk'}), true);
|
||||||
|
assert.equal(h.utterances.at(-1).voice.name, 'Svensk');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a player gesture restores the iOS speech primer after cancelling a queued reply', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
assert.equal(h.controller.activateFromUserGesture(), true);
|
||||||
|
assert.equal(h.synthesis.spoken[0].text, ' ');
|
||||||
|
h.controller.speak('Tidigare replik');
|
||||||
|
h.controller.cancelSpeech();
|
||||||
|
const countAfterCancel = h.synthesis.spoken.length;
|
||||||
|
assert.equal(h.controller.activateFromUserGesture(), true);
|
||||||
|
assert.equal(h.synthesis.spoken.length, countAfterCancel + 1);
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, ' ');
|
||||||
|
h.controller.speak('Nytt svar');
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, 'Nytt svar');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('speech is reported as in progress only after the native onstart event', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
h.controller.speak('Hej.');
|
||||||
|
assert.equal(h.controller.getState(), 'QUEUED');
|
||||||
|
assert.match(h.states.at(-1).message, /Startar uppläsningen/);
|
||||||
|
h.utterances[0].onstart();
|
||||||
|
assert.equal(h.controller.getState(), 'SPEAKING');
|
||||||
|
assert.match(h.states.at(-1).message, /Webbläsaren har startat talsyntesen/);
|
||||||
|
assert.match(h.states.at(-1).message, /Läs senaste repliken/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('iOS user gesture primes speech silently and idle speech is not cancelled before speak', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
assert.equal(h.controller.activateFromUserGesture(), true);
|
||||||
|
assert.equal(h.synthesis.spoken[0].text, ' ');
|
||||||
|
assert.equal(h.synthesis.spoken[0].volume, 0);
|
||||||
|
const cancelledBeforeReply = h.synthesis.cancelled;
|
||||||
|
assert.equal(h.controller.speak('Ett svar efter nätverksanropet'), true);
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, 'Ett svar efter nätverksanropet');
|
||||||
|
assert.equal(h.synthesis.cancelled, cancelledBeforeReply);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('replacing active speech waits one event-loop turn after WebKit cancel', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
h.controller.speak('Första repliken');
|
||||||
|
const cancelledBeforeReplace = h.synthesis.cancelled;
|
||||||
|
h.controller.speak('Nästa replik');
|
||||||
|
assert.equal(h.synthesis.cancelled, cancelledBeforeReplace + 1);
|
||||||
|
assert.notEqual(h.synthesis.spoken.at(-1).text, 'Nästa replik');
|
||||||
|
h.runTimer(100);
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, 'Nästa replik');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('speech start timeout tells the player how to recover when mobile playback is blocked', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
h.controller.speak('En replik');
|
||||||
|
const cancelledBeforeTimeout = h.synthesis.cancelled;
|
||||||
|
h.runTimer(4000);
|
||||||
|
assert.match(h.states.at(-1).message, /Tryck på Läs senaste repliken/);
|
||||||
|
assert.equal(h.synthesis.cancelled, cancelledBeforeTimeout + 1);
|
||||||
|
const cancelledBeforeRetry = h.synthesis.cancelled;
|
||||||
|
h.controller.speak('Manuell återläsning');
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, 'Manuell återläsning');
|
||||||
|
assert.equal(h.synthesis.cancelled, cancelledBeforeRetry);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('iOS not-allowed speech error offers an immediate gesture-based retry', () => {
|
||||||
|
const h = makeHarness();
|
||||||
|
h.controller.speak('En replik');
|
||||||
|
h.utterances[0].onerror({error: 'not-allowed'});
|
||||||
|
assert.match(h.states.at(-1).message, /Safari blockerade uppläsningen/);
|
||||||
|
assert.match(h.states.at(-1).message, /Läs senaste repliken/);
|
||||||
|
assert.equal(h.controller.speak('Försök igen'), true);
|
||||||
|
assert.equal(h.synthesis.spoken.at(-1).text, 'Försök igen');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('character voice identity gives stable distinct masculine, feminine, and neutral timbres', () => {
|
test('character voice identity gives stable distinct masculine, feminine, and neutral timbres', () => {
|
||||||
@@ -138,6 +218,7 @@ test('narrator uses neutral pitch while character replies use their saved voice
|
|||||||
h.controller.update({mode: 'game-voice'});
|
h.controller.update({mode: 'game-voice'});
|
||||||
h.controller.speak('Berättarröst', {role: 'narrator', voiceIdentity: 'feminine', characterId: 'narrator'});
|
h.controller.speak('Berättarröst', {role: 'narrator', voiceIdentity: 'feminine', characterId: 'narrator'});
|
||||||
h.controller.speak('NPC-replik', {voiceIdentity: 'masculine', characterId: 'npc-1'});
|
h.controller.speak('NPC-replik', {voiceIdentity: 'masculine', characterId: 'npc-1'});
|
||||||
|
h.runTimer(100);
|
||||||
assert.equal(h.utterances[0].pitch, 1);
|
assert.equal(h.utterances[0].pitch, 1);
|
||||||
assert.ok(h.utterances[1].pitch < 1);
|
assert.ok(h.utterances[1].pitch < 1);
|
||||||
});
|
});
|
||||||
@@ -148,6 +229,8 @@ test('TTS and microphone are mutually exclusive', () => {
|
|||||||
h.controller.startListening();
|
h.controller.startListening();
|
||||||
h.controller.speak('Svar');
|
h.controller.speak('Svar');
|
||||||
assert.equal(h.recognitions[0].aborted, true);
|
assert.equal(h.recognitions[0].aborted, true);
|
||||||
|
assert.equal(h.controller.getState(), 'QUEUED');
|
||||||
|
h.utterances.at(-1).onstart();
|
||||||
assert.equal(h.controller.getState(), 'SPEAKING');
|
assert.equal(h.controller.getState(), 'SPEAKING');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,10 @@
|
|||||||
let generation = 0;
|
let generation = 0;
|
||||||
let recognition = null;
|
let recognition = null;
|
||||||
let speechTimer = null;
|
let speechTimer = null;
|
||||||
|
let speechStartTimer = null;
|
||||||
let listeningTimer = null;
|
let listeningTimer = null;
|
||||||
|
let currentUtterance = null;
|
||||||
|
let speechPrimed = false;
|
||||||
|
|
||||||
function setState(next, message = '') {
|
function setState(next, message = '') {
|
||||||
state = next;
|
state = next;
|
||||||
@@ -88,11 +91,37 @@
|
|||||||
function cancelSpeech() {
|
function cancelSpeech() {
|
||||||
generation += 1;
|
generation += 1;
|
||||||
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||||
|
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||||
speechTimer = null;
|
speechTimer = null;
|
||||||
|
speechStartTimer = null;
|
||||||
|
const wasQueued = currentUtterance !== null;
|
||||||
|
currentUtterance = null;
|
||||||
|
// WebKit versions before Safari 27 can discard a speak() queued in the
|
||||||
|
// same turn as cancel(). Do not clear an already-empty native queue.
|
||||||
|
if (wasQueued) {
|
||||||
try { win.speechSynthesis?.cancel(); } catch (_) { /* Best effort. */ }
|
try { win.speechSynthesis?.cancel(); } catch (_) { /* Best effort. */ }
|
||||||
|
// cancel() also removes the silent user-gesture primer from the native
|
||||||
|
// queue. A later reply must prime again from the next player gesture.
|
||||||
|
speechPrimed = false;
|
||||||
|
}
|
||||||
if (!recognition) setState('IDLE');
|
if (!recognition) setState('IDLE');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function activateFromUserGesture() {
|
||||||
|
if (!win.speechSynthesis || !win.SpeechSynthesisUtterance) return false;
|
||||||
|
if (speechPrimed) return true;
|
||||||
|
// iOS Safari requires speech output to be initiated by a user gesture.
|
||||||
|
// Prime its speech channel silently on the first trusted tap/key press so
|
||||||
|
// later game replies can be spoken after the API response arrives.
|
||||||
|
try {
|
||||||
|
const primer = new win.SpeechSynthesisUtterance(' ');
|
||||||
|
primer.volume = 0;
|
||||||
|
win.speechSynthesis.speak(primer);
|
||||||
|
speechPrimed = true;
|
||||||
|
return true;
|
||||||
|
} catch (_) { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
function finishRecognition(token, message = '') {
|
function finishRecognition(token, message = '') {
|
||||||
if (token !== generation) return;
|
if (token !== generation) return;
|
||||||
if (listeningTimer !== null) win.clearTimeout(listeningTimer);
|
if (listeningTimer !== null) win.clearTimeout(listeningTimer);
|
||||||
@@ -123,6 +152,10 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
cancelSpeech();
|
cancelSpeech();
|
||||||
|
// This runs synchronously from the microphone button gesture. If
|
||||||
|
// cancelSpeech() removed the earlier primer, restore it before starting
|
||||||
|
// recognition so a spoken reply can follow the transcript.
|
||||||
|
activateFromUserGesture();
|
||||||
const token = ++generation;
|
const token = ++generation;
|
||||||
let instance;
|
let instance;
|
||||||
try { instance = new Recognition(); }
|
try { instance = new Recognition(); }
|
||||||
@@ -179,14 +212,19 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function speak(text, voiceOptions = {}) {
|
function speak(text, voiceOptions = {}) {
|
||||||
if (!text || !win.speechSynthesis || !win.SpeechSynthesisUtterance) return false;
|
if (!text || !win.speechSynthesis || !win.SpeechSynthesisUtterance) {
|
||||||
|
setState('IDLE', 'Talsyntes stöds inte i den här webbläsaren. Dialogen finns kvar som text.');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
stopListening();
|
stopListening();
|
||||||
cancelSpeech();
|
const hadQueuedUtterance = currentUtterance !== null;
|
||||||
const token = generation;
|
if (hadQueuedUtterance) cancelSpeech();
|
||||||
|
const token = ++generation;
|
||||||
let utterance;
|
let utterance;
|
||||||
try { utterance = new win.SpeechSynthesisUtterance(text); }
|
try { utterance = new win.SpeechSynthesisUtterance(text); }
|
||||||
catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
|
catch (_) { setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
|
||||||
utterance.lang = settings.language || 'sv-SE';
|
utterance.lang = settings.language || 'sv-SE';
|
||||||
|
utterance.volume = 1;
|
||||||
const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {};
|
const profile = voiceOptions.characterId ? settings.speakerProfiles?.[voiceOptions.characterId] || {} : {};
|
||||||
const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId);
|
const identityProfile = characterVoiceProfile(voiceOptions.voiceIdentity, voiceOptions.characterId);
|
||||||
const isNarrator = voiceOptions.role === 'narrator';
|
const isNarrator = voiceOptions.role === 'narrator';
|
||||||
@@ -195,31 +233,86 @@
|
|||||||
utterance.pitch = isNarrator ? 1 : Math.min(2, Math.max(0, Number(voiceOptions.pitch ?? profile.pitch ?? identityProfile.pitch) || 1));
|
utterance.pitch = isNarrator ? 1 : Math.min(2, Math.max(0, Number(voiceOptions.pitch ?? profile.pitch ?? identityProfile.pitch) || 1));
|
||||||
const requestedName = voiceOptions.voiceName || profile.voiceName || settings.voiceName;
|
const requestedName = voiceOptions.voiceName || profile.voiceName || settings.voiceName;
|
||||||
const voices = availableVoices();
|
const voices = availableVoices();
|
||||||
utterance.voice = voices.find(voice => voice.name === requestedName)
|
// An empty selection means the operating system should choose its
|
||||||
|
// default voice for the requested language. Forcing the first voice
|
||||||
|
// returned by WebKit can select a voice that is listed but cannot speak.
|
||||||
|
utterance.voice = requestedName
|
||||||
|
? voices.find(voice => voice.name === requestedName)
|
||||||
|| voices.find(voice => /^sv(-|$)/i.test(voice.lang))
|
|| voices.find(voice => /^sv(-|$)/i.test(voice.lang))
|
||||||
|| null;
|
|| null
|
||||||
|
: null;
|
||||||
|
utterance.onstart = () => {
|
||||||
|
if (token !== generation) return;
|
||||||
|
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||||
|
speechStartTimer = null;
|
||||||
|
if (typeof voiceOptions.onStart === 'function') voiceOptions.onStart();
|
||||||
|
// onstart confirms the browser speech engine accepted the utterance;
|
||||||
|
// it cannot confirm that sound reached the device's speakers.
|
||||||
|
setState('SPEAKING', 'Webbläsaren har startat talsyntesen. Om du inte hör ljud, tryck på Läs senaste repliken.');
|
||||||
|
};
|
||||||
utterance.onend = () => {
|
utterance.onend = () => {
|
||||||
if (token !== generation) return;
|
if (token !== generation) return;
|
||||||
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||||
|
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||||
speechTimer = null;
|
speechTimer = null;
|
||||||
|
speechStartTimer = null;
|
||||||
|
currentUtterance = null;
|
||||||
setState('IDLE');
|
setState('IDLE');
|
||||||
};
|
};
|
||||||
utterance.onerror = () => {
|
utterance.onerror = event => {
|
||||||
if (token !== generation) return;
|
if (token !== generation) return;
|
||||||
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||||
|
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||||
speechTimer = null;
|
speechTimer = null;
|
||||||
|
speechStartTimer = null;
|
||||||
|
currentUtterance = null;
|
||||||
generation += 1;
|
generation += 1;
|
||||||
setState('IDLE', 'Uppläsningen avbröts. Dialogen finns kvar som text.');
|
const blocked = event?.error === 'not-allowed' || event?.error === 'service-not-allowed';
|
||||||
|
const message = blocked
|
||||||
|
? 'Safari blockerade uppläsningen. Tryck på Läs senaste repliken för att starta ljudet.'
|
||||||
|
: 'Uppläsningen avbröts. Tryck på Läs senaste repliken för att försöka igen. Dialogen finns kvar som text.';
|
||||||
|
setState('IDLE', message);
|
||||||
};
|
};
|
||||||
setState('SPEAKING', 'Läser upp dialog.');
|
setState('QUEUED', 'Startar uppläsningen.');
|
||||||
|
currentUtterance = utterance;
|
||||||
|
speechStartTimer = win.setTimeout(() => {
|
||||||
|
if (token !== generation || currentUtterance !== utterance) return;
|
||||||
|
speechStartTimer = null;
|
||||||
|
generation += 1;
|
||||||
|
currentUtterance = null;
|
||||||
|
if (speechTimer !== null) win.clearTimeout(speechTimer);
|
||||||
|
speechTimer = null;
|
||||||
|
// Drop a blocked native utterance now. A player retry can then call
|
||||||
|
// speak() directly from its button gesture instead of following a
|
||||||
|
// same-turn cancel() that older WebKit versions may discard.
|
||||||
|
try { win.speechSynthesis.cancel(); } catch (_) { /* Best effort. */ }
|
||||||
|
setState('IDLE', 'Uppläsningen startade inte. Tryck på Läs senaste repliken för att starta ljudet.');
|
||||||
|
}, 4000);
|
||||||
speechTimer = win.setTimeout(() => {
|
speechTimer = win.setTimeout(() => {
|
||||||
if (token !== generation) return;
|
if (token !== generation) return;
|
||||||
generation += 1;
|
generation += 1;
|
||||||
try { win.speechSynthesis.cancel(); } catch (_) { /* Best effort. */ }
|
try { win.speechSynthesis.cancel(); } catch (_) { /* Best effort. */ }
|
||||||
|
currentUtterance = null;
|
||||||
|
if (speechStartTimer !== null) win.clearTimeout(speechStartTimer);
|
||||||
|
speechStartTimer = null;
|
||||||
speechTimer = null;
|
speechTimer = null;
|
||||||
setState('IDLE', 'Uppläsningen avslutades. Dialogen finns kvar som text.');
|
setState('IDLE', 'Uppläsningen avslutades. Dialogen finns kvar som text.');
|
||||||
}, Math.max(30000, Math.min(180000, String(text).length * 180)));
|
}, Math.max(30000, Math.min(180000, String(text).length * 180)));
|
||||||
|
const enqueue = () => {
|
||||||
|
if (token !== generation) return;
|
||||||
try { win.speechSynthesis.speak(utterance); }
|
try { win.speechSynthesis.speak(utterance); }
|
||||||
|
catch (_) {
|
||||||
|
currentUtterance = null;
|
||||||
|
cancelSpeech();
|
||||||
|
setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
// Give WebKit one event-loop turn to finish cancel() before replacing
|
||||||
|
// speech that was actively playing. Normal replies enqueue immediately.
|
||||||
|
if (hadQueuedUtterance) win.setTimeout(enqueue, 100);
|
||||||
|
else enqueue();
|
||||||
|
}
|
||||||
catch (_) { cancelSpeech(); setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
|
catch (_) { cancelSpeech(); setState('IDLE', 'Talsyntesen kunde inte startas. Dialogen finns kvar som text.'); return false; }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -230,7 +323,7 @@
|
|||||||
function getSettings() { return {...settings}; }
|
function getSettings() { return {...settings}; }
|
||||||
function getVoices() { return availableVoices(); }
|
function getVoices() { return availableVoices(); }
|
||||||
function supportsRecognition() { return Boolean(win.SpeechRecognition || win.webkitSpeechRecognition); }
|
function supportsRecognition() { return Boolean(win.SpeechRecognition || win.webkitSpeechRecognition); }
|
||||||
return {startListening, stopListening, speak, cancelSpeech, stopAll, update, getState, getSettings, getVoices, supportsRecognition};
|
return {startListening, stopListening, speak, cancelSpeech, stopAll, activateFromUserGesture, update, getState, getSettings, getVoices, supportsRecognition};
|
||||||
}
|
}
|
||||||
|
|
||||||
root.GameVoice = {createVoiceController, DEFAULT_SETTINGS, parseVoiceCommand, loadSettings, saveSettings, characterVoiceProfile};
|
root.GameVoice = {createVoiceController, DEFAULT_SETTINGS, parseVoiceCommand, loadSettings, saveSettings, characterVoiceProfile};
|
||||||
|
|||||||
Reference in New Issue
Block a user