Initial commit, basic functionality in place.
This commit is contained in:
329
renderer/index.html
Normal file
329
renderer/index.html
Normal file
@@ -0,0 +1,329 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Kokoro Widget</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg-primary: #0d1220;
|
||||
--bg-secondary: #141c2c;
|
||||
--bg-tertiary: #1c2638;
|
||||
--text-primary: #f0f2f5;
|
||||
--text-secondary: #c0c8d4;
|
||||
--text-muted: #8a919d;
|
||||
--cyan: #12c2e9;
|
||||
--purple: #c471ed;
|
||||
--pink: #ff6b9d;
|
||||
--teal: #2dd4bf;
|
||||
--orange: #F39C12;
|
||||
--border: rgba(255, 255, 255, 0.08);
|
||||
}
|
||||
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-primary);
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 1rem;
|
||||
-webkit-app-region: no-drag;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
/* Make window draggable by the panel header area */
|
||||
.voice-panel {
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 16px;
|
||||
padding: 1.5rem 2rem;
|
||||
width: 300px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.panel-header {
|
||||
-webkit-app-region: drag;
|
||||
cursor: move;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.voice-panel h1 {
|
||||
font-size: 1.2rem;
|
||||
font-weight: 700;
|
||||
color: var(--cyan);
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.voice-panel .subtitle {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
/* Status indicator */
|
||||
.status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.status-dot {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 50%;
|
||||
background: var(--text-muted);
|
||||
transition: background 0.3s, box-shadow 0.3s;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.status-dot.connected {
|
||||
background: var(--teal);
|
||||
box-shadow: 0 0 8px rgba(45, 212, 191, 0.4);
|
||||
}
|
||||
|
||||
.status-dot.connecting {
|
||||
background: var(--orange);
|
||||
animation: pulse 1.5s infinite;
|
||||
}
|
||||
|
||||
.status-dot.error {
|
||||
background: var(--pink);
|
||||
}
|
||||
|
||||
.status-text {
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.4; }
|
||||
}
|
||||
|
||||
/* Speaking indicator */
|
||||
.speaking-indicator {
|
||||
height: 36px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 4px;
|
||||
margin-bottom: 0.75rem;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s;
|
||||
}
|
||||
|
||||
.speaking-indicator.active {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.speaking-bar {
|
||||
width: 4px;
|
||||
height: 8px;
|
||||
background: var(--cyan);
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.speaking-indicator.active .speaking-bar {
|
||||
animation: bars 0.8s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.speaking-bar:nth-child(1) { animation-delay: 0s; }
|
||||
.speaking-bar:nth-child(2) { animation-delay: 0.1s; }
|
||||
.speaking-bar:nth-child(3) { animation-delay: 0.2s; }
|
||||
.speaking-bar:nth-child(4) { animation-delay: 0.3s; }
|
||||
.speaking-bar:nth-child(5) { animation-delay: 0.2s; }
|
||||
.speaking-bar:nth-child(6) { animation-delay: 0.1s; }
|
||||
.speaking-bar:nth-child(7) { animation-delay: 0s; }
|
||||
|
||||
@keyframes bars {
|
||||
0%, 100% { height: 8px; }
|
||||
50% { height: 26px; }
|
||||
}
|
||||
|
||||
/* Last message */
|
||||
.last-message {
|
||||
font-size: 0.78rem;
|
||||
color: var(--text-muted);
|
||||
min-height: 1.4em;
|
||||
margin-bottom: 1rem;
|
||||
font-style: italic;
|
||||
line-height: 1.4;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
/* Divider */
|
||||
.divider {
|
||||
border: none;
|
||||
border-top: 1px solid var(--border);
|
||||
margin: 0 0 1rem 0;
|
||||
}
|
||||
|
||||
/* Volume row */
|
||||
.volume-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.volume-row label {
|
||||
font-size: 0.7rem;
|
||||
color: var(--text-muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
white-space: nowrap;
|
||||
min-width: 3rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.volume-row input[type="range"] {
|
||||
flex: 1;
|
||||
accent-color: var(--cyan);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* Settings section */
|
||||
.settings-section {
|
||||
border-top: 1px solid var(--border);
|
||||
padding-top: 1rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.settings-section h3 {
|
||||
font-size: 0.65rem;
|
||||
color: var(--text-muted);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.settings-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.settings-row label {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
min-width: 2.5rem;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.settings-row input {
|
||||
flex: 1;
|
||||
background: var(--bg-tertiary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
padding: 0.35rem 0.5rem;
|
||||
color: var(--text-primary);
|
||||
font-size: 0.8rem;
|
||||
font-family: inherit;
|
||||
outline: none;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.settings-row input:focus {
|
||||
border-color: rgba(18, 194, 233, 0.5);
|
||||
}
|
||||
|
||||
.settings-row input[type="number"]::-webkit-outer-spin-button,
|
||||
.settings-row input[type="number"]::-webkit-inner-spin-button {
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.save-row {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
|
||||
#saveBtn {
|
||||
background: var(--cyan);
|
||||
color: #0d1220;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
padding: 0.4rem 1.1rem;
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
font-family: inherit;
|
||||
cursor: pointer;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
#saveBtn:hover {
|
||||
opacity: 0.85;
|
||||
}
|
||||
|
||||
#saveBtn:active {
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
#saveBtn.saved {
|
||||
background: var(--teal);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="voice-panel">
|
||||
<div class="panel-header">
|
||||
<h1>PAI Voice</h1>
|
||||
<p class="subtitle">Voice notifications from your AI assistant</p>
|
||||
</div>
|
||||
|
||||
<div class="status">
|
||||
<span class="status-dot connecting" id="statusDot"></span>
|
||||
<span class="status-text" id="statusText">Connecting...</span>
|
||||
</div>
|
||||
|
||||
<div class="speaking-indicator" id="speakingIndicator">
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
<div class="speaking-bar"></div>
|
||||
</div>
|
||||
|
||||
<div class="last-message" id="lastMessage">Waiting for notifications...</div>
|
||||
|
||||
<hr class="divider">
|
||||
|
||||
<div class="volume-row">
|
||||
<label for="volume">Volume</label>
|
||||
<input type="range" id="volume" min="0" max="100" value="80">
|
||||
</div>
|
||||
|
||||
<div class="settings-section">
|
||||
<h3>Settings</h3>
|
||||
|
||||
<div class="settings-row">
|
||||
<label for="hostInput">Host</label>
|
||||
<input type="text" id="hostInput" placeholder="localhost" value="localhost">
|
||||
</div>
|
||||
|
||||
<div class="settings-row">
|
||||
<label for="portInput">Port</label>
|
||||
<input type="number" id="portInput" placeholder="8888" value="8888" min="1" max="65535">
|
||||
</div>
|
||||
|
||||
<div class="save-row">
|
||||
<button id="saveBtn">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="renderer.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
263
renderer/renderer.js
Normal file
263
renderer/renderer.js
Normal file
@@ -0,0 +1,263 @@
|
||||
'use strict';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// State
|
||||
// ---------------------------------------------------------------------------
|
||||
let ws = null;
|
||||
let config = {}; // populated from IPC in init() — no hardcoded defaults here
|
||||
let everConnected = false; // true after first successful onopen
|
||||
let reconnectTimer = null;
|
||||
const audioQueue = [];
|
||||
const AUDIO_QUEUE_MAX = 25;
|
||||
let isPlaying = false;
|
||||
let consecutiveAudioErrors = 0;
|
||||
const AUDIO_ERROR_LIMIT = 5;
|
||||
let volumeSaveTimer = null;
|
||||
|
||||
// DOM refs — populated after DOMContentLoaded
|
||||
let statusDot, statusText, speakingIndicator, lastMessage;
|
||||
let volumeSlider, hostInput, portInput, saveBtn;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebSocket URL
|
||||
// ---------------------------------------------------------------------------
|
||||
function getWsUrl() {
|
||||
return `ws://${config.host}:${config.port}/stream`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Status display
|
||||
// ---------------------------------------------------------------------------
|
||||
function setStatus(state, text) {
|
||||
statusDot.className = 'status-dot ' + state;
|
||||
statusText.textContent = text;
|
||||
}
|
||||
|
||||
function setSpeaking(active) {
|
||||
if (active) {
|
||||
speakingIndicator.classList.add('active');
|
||||
} else {
|
||||
speakingIndicator.classList.remove('active');
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebSocket connection
|
||||
// ---------------------------------------------------------------------------
|
||||
function connect() {
|
||||
// Clean up any existing connection
|
||||
if (ws) {
|
||||
ws.onopen = null;
|
||||
ws.onmessage = null;
|
||||
ws.onclose = null;
|
||||
ws.onerror = null;
|
||||
ws.close();
|
||||
ws = null;
|
||||
}
|
||||
|
||||
clearReconnectTimer();
|
||||
setStatus('connecting', 'Connecting...');
|
||||
|
||||
try {
|
||||
ws = new WebSocket(getWsUrl());
|
||||
ws.binaryType = 'arraybuffer';
|
||||
|
||||
ws.onopen = () => {
|
||||
everConnected = true;
|
||||
// Socket open but no data yet — show a distinct waiting state
|
||||
setStatus('connected', 'Connected — waiting for audio');
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
if (typeof event.data === 'string') {
|
||||
try {
|
||||
const msg = JSON.parse(event.data);
|
||||
if (msg.type === 'notification' && msg.message) {
|
||||
lastMessage.textContent = msg.message;
|
||||
setStatus('connected', 'Connected');
|
||||
}
|
||||
} catch {
|
||||
// Ignore malformed JSON
|
||||
}
|
||||
} else if (event.data instanceof ArrayBuffer) {
|
||||
// First real data confirms the server is live
|
||||
setStatus('connected', 'Connected');
|
||||
if (audioQueue.length < AUDIO_QUEUE_MAX) {
|
||||
audioQueue.push(event.data);
|
||||
if (!isPlaying) playNext();
|
||||
} else {
|
||||
console.warn('Audio queue full — dropping incoming frame');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
ws = null;
|
||||
if (everConnected) {
|
||||
// Was previously connected — show that we lost it
|
||||
setStatus('error', 'Disconnected — retrying...');
|
||||
} else {
|
||||
// Never connected yet — stay in the neutral "Connecting..." state
|
||||
// so there is no blink during the retry loop
|
||||
setStatus('connecting', 'Connecting...');
|
||||
}
|
||||
scheduleReconnect();
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
// onclose always fires after onerror — let onclose handle the status
|
||||
// update so there is no intermediate blink from this handler.
|
||||
if (ws) ws._hadError = true;
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('WebSocket connection failed:', e);
|
||||
setStatus('error', 'Connection error');
|
||||
scheduleReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleReconnect() {
|
||||
clearReconnectTimer();
|
||||
reconnectTimer = setTimeout(() => {
|
||||
if (ws === null) connect();
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function clearReconnectTimer() {
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Audio playback queue
|
||||
// ---------------------------------------------------------------------------
|
||||
async function playNext() {
|
||||
if (audioQueue.length === 0) {
|
||||
isPlaying = false;
|
||||
setSpeaking(false);
|
||||
return;
|
||||
}
|
||||
|
||||
isPlaying = true;
|
||||
setSpeaking(true);
|
||||
const buffer = audioQueue.shift();
|
||||
let url = null;
|
||||
|
||||
try {
|
||||
const blob = new Blob([buffer], { type: 'audio/mpeg' });
|
||||
url = URL.createObjectURL(blob);
|
||||
const audio = new Audio(url);
|
||||
audio.volume = Math.min(1, Math.max(0, config.volume / 100));
|
||||
|
||||
audio.onended = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
consecutiveAudioErrors = 0;
|
||||
playNext();
|
||||
};
|
||||
|
||||
audio.onerror = (e) => {
|
||||
console.error('Audio error:', e);
|
||||
URL.revokeObjectURL(url);
|
||||
consecutiveAudioErrors++;
|
||||
if (consecutiveAudioErrors >= AUDIO_ERROR_LIMIT) {
|
||||
console.error(`Audio failed ${AUDIO_ERROR_LIMIT} times in a row — stopping playback`);
|
||||
isPlaying = false;
|
||||
setSpeaking(false);
|
||||
setStatus('error', 'Audio error');
|
||||
audioQueue.length = 0;
|
||||
return;
|
||||
}
|
||||
playNext();
|
||||
};
|
||||
|
||||
await audio.play();
|
||||
consecutiveAudioErrors = 0;
|
||||
} catch (e) {
|
||||
console.error('Audio playback failed:', e);
|
||||
if (url) URL.revokeObjectURL(url); // prevent leak on play() rejection
|
||||
consecutiveAudioErrors++;
|
||||
if (consecutiveAudioErrors >= AUDIO_ERROR_LIMIT) {
|
||||
console.error(`Audio failed ${AUDIO_ERROR_LIMIT} times in a row — stopping playback`);
|
||||
isPlaying = false;
|
||||
setSpeaking(false);
|
||||
setStatus('error', 'Audio error');
|
||||
audioQueue.length = 0;
|
||||
return;
|
||||
}
|
||||
playNext();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Initialization
|
||||
// ---------------------------------------------------------------------------
|
||||
async function init() {
|
||||
// Grab DOM refs
|
||||
statusDot = document.getElementById('statusDot');
|
||||
statusText = document.getElementById('statusText');
|
||||
speakingIndicator = document.getElementById('speakingIndicator');
|
||||
lastMessage = document.getElementById('lastMessage');
|
||||
volumeSlider = document.getElementById('volume');
|
||||
hostInput = document.getElementById('hostInput');
|
||||
portInput = document.getElementById('portInput');
|
||||
saveBtn = document.getElementById('saveBtn');
|
||||
|
||||
// Load config from main process (merged saved + CLI args)
|
||||
try {
|
||||
config = await window.kokoro.getConfig();
|
||||
} catch (e) {
|
||||
console.error('Failed to load config:', e);
|
||||
}
|
||||
|
||||
// Populate UI with loaded values
|
||||
volumeSlider.value = config.volume;
|
||||
hostInput.value = config.host;
|
||||
portInput.value = config.port;
|
||||
|
||||
// Volume slider: live update + debounce save
|
||||
volumeSlider.addEventListener('input', () => {
|
||||
config.volume = parseInt(volumeSlider.value, 10);
|
||||
clearTimeout(volumeSaveTimer);
|
||||
volumeSaveTimer = setTimeout(async () => {
|
||||
try {
|
||||
await window.kokoro.saveConfig({ volume: config.volume });
|
||||
} catch (e) {
|
||||
console.error('Failed to save volume:', e);
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
|
||||
// Save button: update host/port, save, reconnect
|
||||
saveBtn.addEventListener('click', async () => {
|
||||
const newHost = hostInput.value.trim() || 'localhost';
|
||||
const newPort = parseInt(portInput.value, 10);
|
||||
const validPort = (!isNaN(newPort) && newPort > 0 && newPort <= 65535) ? newPort : 8888;
|
||||
|
||||
hostInput.value = newHost;
|
||||
portInput.value = validPort;
|
||||
|
||||
try {
|
||||
config = await window.kokoro.saveConfig({ host: newHost, port: validPort });
|
||||
|
||||
// Visual feedback
|
||||
saveBtn.textContent = 'Saved!';
|
||||
saveBtn.classList.add('saved');
|
||||
setTimeout(() => {
|
||||
saveBtn.textContent = 'Save';
|
||||
saveBtn.classList.remove('saved');
|
||||
}, 1500);
|
||||
|
||||
// Reconnect with new settings
|
||||
connect();
|
||||
} catch (e) {
|
||||
console.error('Failed to save config:', e);
|
||||
}
|
||||
});
|
||||
|
||||
// Connect to WebSocket
|
||||
connect();
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
Reference in New Issue
Block a user