mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Chat sits next to the mic in the command dock. Replies are silent and never request the microphone; the first message connects with the selected provider and runs the same map actions as voice through each provider's text API (OpenAI OPENAI_CHAT_MODEL, Gemini GEMINI_CHAT_MODEL via generateContent, which never starts Live). Only one mode runs at a time. - Chat panel rendered by the voice control presenter: conversation, recent and quick-command suggestions, Up/Down history, attachments (map-layer references or small UTF-8 text files, sent only with the message) - Chat and voice panels sit above the dock or dock into the right rail's column; minimized bars sit centred above the dock, and height animates on swaps - A centred panel steps down to its bar for the Location and Visual Presets trays without moving focus, rests above an open tray, and docks right while expanded over one; it returns to the centre once the trays close - Minimized voice previews the last sentence from each side; the empty Voice Preview suggests things to say - Settings opened from chat keep chat's frame and show the shared AI provider and quick commands; voice Settings show Back to voice - Closing chat ends a typed session; the voice panel's close turns voice off - The dock widens its voice control for the chat button and restyles its wings, which show icons only at 900px and below; assistant panels share the left panels' glass, and Cyber keeps its own surfaces - Server text-chat handlers with body, time and per-IP limits (GEV_RATELIMIT_*_CHAT_PER_MIN), documented in SECURITY.md; tests and all eleven locales Co-authored-by: Darren Dignam <[email protected]>
401 lines
13 KiB
JavaScript
401 lines
13 KiB
JavaScript
import { nextTick } from 'vue';
|
|
import { onLocaleChange, t } from '../i18n/index.js';
|
|
import { useVoiceStore } from '../ui/stores/voice.js';
|
|
import { applicationPinia } from '../ui/vueRuntime.js';
|
|
|
|
// The assistant's name on its card. i18n-ignore: product name
|
|
export const ASSISTANT_NAME = 'GEV';
|
|
|
|
/** Bars in the dock waveform. */
|
|
export const VOICE_VISUALIZER_BARS = 15;
|
|
|
|
/**
|
|
* Short state label for the narrow voice heading. The `voice` context lets
|
|
* languages use shorter words; unknown states show upper-cased.
|
|
* @param {string} status machine state (dataset.status)
|
|
* @returns {string}
|
|
*/
|
|
export function voiceStatusLabel(status) {
|
|
switch (status) {
|
|
case 'idle':
|
|
return t('OFF', { context: 'voice' });
|
|
case 'connecting':
|
|
return t('CONNECTING', { context: 'voice' });
|
|
case 'listening':
|
|
return t('LISTENING', { context: 'voice' });
|
|
case 'executing':
|
|
return t('EXECUTING', { context: 'voice' });
|
|
case 'error':
|
|
return t('ERROR', { context: 'voice' });
|
|
default:
|
|
return String(status || '').toUpperCase();
|
|
}
|
|
}
|
|
|
|
/** The card before its first render: hidden, with its default labels. */
|
|
function initialCard() {
|
|
return {
|
|
visible: false,
|
|
busy: null,
|
|
phase: '',
|
|
user: { visible: false, text: '', label: t('YOU') },
|
|
pointer: { visible: false, kind: t('THIS'), text: '' },
|
|
assistant: { visible: false, text: '' },
|
|
steps: [],
|
|
result: {
|
|
visible: false,
|
|
title: '',
|
|
chips: [],
|
|
lines: [],
|
|
referents: [],
|
|
notes: [],
|
|
},
|
|
announce: '',
|
|
};
|
|
}
|
|
|
|
/** The control as it first renders, before any owner writes to it. */
|
|
function initialState(panel) {
|
|
return {
|
|
panel,
|
|
root: {
|
|
status: 'idle',
|
|
speaker: 'idle',
|
|
provider: null,
|
|
providerChoice: null,
|
|
pushToTalk: null,
|
|
microphone: null,
|
|
rowGrace: false,
|
|
errorDismissed: false,
|
|
// Which assistant the dock shows as selected: idle, voice or chat.
|
|
assistantMode: 'idle',
|
|
chatOpen: false,
|
|
chatBusy: false,
|
|
// Set once either side has spoken this voice session.
|
|
voiceExchange: false,
|
|
},
|
|
// The provider field and its select.
|
|
provider: {
|
|
hidden: true,
|
|
options: [],
|
|
value: '',
|
|
title: () => t('Changing provider reconnects voice if it is on.'),
|
|
},
|
|
tier: {
|
|
hidden: false,
|
|
text: () => t('STD'),
|
|
pressed: false,
|
|
title: () => t('Voice model tier — applies next session'),
|
|
},
|
|
cost: {
|
|
hidden: false,
|
|
text: '~$0.00',
|
|
level: 'ok',
|
|
title: () => t('Estimated session cost'),
|
|
},
|
|
costWarning: { hidden: true, text: '', level: null, title: null },
|
|
button: {
|
|
label: () =>
|
|
t('Voice control — activate to toggle voice; hold Space to speak'),
|
|
pressed: null,
|
|
},
|
|
mic: { text: () => t('ON/OFF') },
|
|
status: { text: () => voiceStatusLabel('idle') },
|
|
detail: { text: () => t('VOICE STANDBY'), title: null },
|
|
help: {
|
|
text: () =>
|
|
t('Hold Space to speak · tap Space to activate focused controls'),
|
|
},
|
|
// A typed chat error reads as chat's, without the microphone hint.
|
|
error: { text: '', typed: false },
|
|
// Connection and turn state beside the selected mode's indicator.
|
|
phase: { text: '' },
|
|
voicePanel: {
|
|
hidden: true,
|
|
view: null,
|
|
previewHidden: false,
|
|
settingsHidden: true,
|
|
settingsExpanded: false,
|
|
selected: 'preview',
|
|
minimized: false,
|
|
// 'chat' while Settings were opened from chat.
|
|
context: null,
|
|
// Chat's frame, so Settings opened from chat keep its size.
|
|
frameWidth: null,
|
|
frameHeight: null,
|
|
},
|
|
// The typed chat panel; its messages publish apart (setChatMessages).
|
|
chat: {
|
|
open: false,
|
|
minimized: false,
|
|
busy: false,
|
|
status: () => t('Silent chat · microphone off'),
|
|
empty: true,
|
|
quickChecked: true,
|
|
// The latest reply, shown above the minimized bar.
|
|
miniReply: '',
|
|
suggestions: [],
|
|
attachments: [],
|
|
attachMenuOpen: false,
|
|
layers: [],
|
|
locked: false,
|
|
sendDisabled: true,
|
|
// The input's text; bumped revision means the owner wrote it.
|
|
draft: '',
|
|
draftRevision: 0,
|
|
rows: 2,
|
|
backVisible: false,
|
|
},
|
|
// The minimized voice bar's last sentence from each side.
|
|
mini: { user: '', assistant: '' },
|
|
// Above the dock, or docked into the right rail's lane.
|
|
// `yielded` names the panel minimized to make room for a dock tray;
|
|
// `trayDocked` marks a dock taken only while a tray was open.
|
|
layout: { docked: false, yielded: null, trayDocked: false },
|
|
previewEmpty: {
|
|
hidden: false,
|
|
text: () => t('Voice replies appear here.'),
|
|
},
|
|
preferences: {
|
|
previewChecked: true,
|
|
previewNote: '',
|
|
timeoutChoice: '5',
|
|
timeoutMinutes: '5',
|
|
timeoutInputHidden: false,
|
|
timeoutNote: () => t('These preferences stay in this browser.'),
|
|
timeoutStatus: '',
|
|
// Bumped by every owner write of the fields, so the view restores a
|
|
// refused edit but leaves a draft alone across other renders.
|
|
revision: 0,
|
|
},
|
|
card: initialCard(),
|
|
};
|
|
}
|
|
|
|
// The rendered elements the presenter focuses or measures, by name.
|
|
const ELEMENT_IDS = Object.freeze({
|
|
root: 'gev-voice-control',
|
|
button: 'gev-voice-button',
|
|
provider: 'gev-voice-provider',
|
|
voicePanel: 'gev-voice-panel',
|
|
previewPanel: 'gev-voice-preview-panel',
|
|
settingsPanel: 'gev-voice-settings-panel',
|
|
previewTab: 'gev-voice-preview-tab',
|
|
settingsTab: 'gev-voice-settings-tab',
|
|
settingsButton: 'gev-voice-settings-button',
|
|
settingsClose: 'gev-voice-settings-close',
|
|
card: 'gev-voice-card',
|
|
chatButton: 'gev-chat-button',
|
|
chatPanel: 'gev-chat-panel',
|
|
chatInput: 'gev-chat-input',
|
|
chatLayer: 'gev-chat-layer',
|
|
chatFile: 'gev-chat-file',
|
|
chatBack: 'gev-chat-back',
|
|
voiceBack: 'gev-voice-back',
|
|
chatMiniReply: 'gev-chat-mini-reply',
|
|
});
|
|
|
|
let controlCount = 0;
|
|
|
|
const sameBars = (a, b) =>
|
|
a === b ||
|
|
(Array.isArray(a) &&
|
|
Array.isArray(b) &&
|
|
a.length === b.length &&
|
|
a.every(
|
|
(bar, index) =>
|
|
bar.level === b[index].level && bar.opacity === b[index].opacity,
|
|
));
|
|
|
|
/**
|
|
* The voice control, independent of its connection backend. Session owners
|
|
* write its state by part (`update('detail', {text})`) and listen for what
|
|
* the person does (`on('button', fn)`); the command dock's VoiceControl view
|
|
* renders it from the voice store. A newer control replaces this one there.
|
|
* @param {object} [options]
|
|
* @param {boolean} [options.panel] Show the Voice Settings panel and preview.
|
|
*/
|
|
export function createVoiceControl({
|
|
panel = false,
|
|
store = useVoiceStore(applicationPinia()),
|
|
} = {}) {
|
|
const id = ++controlCount;
|
|
const listeners = new Map();
|
|
// The fields given as functions, by part: they are called again when the
|
|
// locale changes, so the control's words follow the language.
|
|
const wording = new Map();
|
|
const word = (part, patch) => {
|
|
let fields = wording.get(part);
|
|
let worded = patch;
|
|
for (const [field, value] of Object.entries(patch)) {
|
|
if (typeof value !== 'function') {
|
|
fields?.delete(field);
|
|
continue;
|
|
}
|
|
if (!fields) wording.set(part, (fields = new Map()));
|
|
fields.set(field, value);
|
|
if (worded === patch) worded = { ...patch };
|
|
worded[field] = value();
|
|
}
|
|
return worded;
|
|
};
|
|
let state = initialState(Boolean(panel));
|
|
for (const [part, value] of Object.entries(state))
|
|
if (part !== 'card' && value && typeof value === 'object')
|
|
state[part] = word(part, value);
|
|
// The default card's labels follow the locale until a card replaces it.
|
|
let defaultCard = true;
|
|
// Each bar's `--audio-level` and `--audio-opacity`, or null at rest.
|
|
let bars = null;
|
|
let chatMessages = [];
|
|
let removed = false;
|
|
const owns = () => !removed && store.controls === ui;
|
|
const publish = () => {
|
|
if (owns()) store.publish({ ...state, id });
|
|
};
|
|
const element = (name) =>
|
|
owns()
|
|
? (globalThis.document?.getElementById(ELEMENT_IDS[name]) ?? null)
|
|
: null;
|
|
const shown = (node) =>
|
|
Boolean(
|
|
node?.checkVisibility?.({
|
|
opacityProperty: true,
|
|
visibilityProperty: true,
|
|
}) ?? node,
|
|
);
|
|
const ui = {
|
|
get panel() {
|
|
return state.panel;
|
|
},
|
|
/** The current state; read-only, replaced on every update. */
|
|
get state() {
|
|
return state;
|
|
},
|
|
/**
|
|
* Merge `patch` into one part of the control. A field given as a
|
|
* function is shown as what it returns, and called again when the
|
|
* locale changes; give translated words that way.
|
|
*/
|
|
update(part, patch) {
|
|
if (removed) return;
|
|
state = { ...state, [part]: { ...state[part], ...word(part, patch) } };
|
|
publish();
|
|
},
|
|
/** Replace one part of the control. */
|
|
replace(part, value) {
|
|
if (removed) return;
|
|
wording.delete(part);
|
|
if (part === 'card') defaultCard = false;
|
|
state = { ...state, [part]: value };
|
|
publish();
|
|
},
|
|
/**
|
|
* Listen for an interaction the view reports: `button`, `tier`,
|
|
* `provider-change`, `settings-button`, `settings-close`, `tab`,
|
|
* `tab-keydown`, `panel-keydown`, `pointerenter`, `pointerleave`,
|
|
* `preview-change`, `timeout-select`, `timeout-input`, `card-dismiss`,
|
|
* `voice-back`, `voice-minimize`, `layout-dock`; the chat's
|
|
* `chat-button`, `chat-close`, `chat-minimize`, `chat-restore`,
|
|
* `chat-back`, `chat-input`, `chat-keydown`, `chat-submit`, `chat-stop`,
|
|
* `chat-prompt`, `chat-suggestion`, `chat-quick-change`,
|
|
* `chat-attach-toggle`, `chat-layer`, `chat-file-button`, `chat-files`,
|
|
* `chat-attachment-remove`; and `locale` after a locale change, for an
|
|
* owner's own words.
|
|
* @returns {() => void} Removes the listener.
|
|
*/
|
|
on(name, listener) {
|
|
if (!listeners.has(name)) listeners.set(name, new Set());
|
|
listeners.get(name).add(listener);
|
|
return () => listeners.get(name)?.delete(listener);
|
|
},
|
|
/** The view reports an interaction; listeners run in the order added. */
|
|
emit(name, ...args) {
|
|
if (removed) return;
|
|
for (const listener of [...(listeners.get(name) ?? [])])
|
|
listener(...args);
|
|
},
|
|
/** The waveform's bars, or null at rest. */
|
|
get bars() {
|
|
return bars;
|
|
},
|
|
/** The chat conversation, as last published. */
|
|
get chatMessages() {
|
|
return chatMessages;
|
|
},
|
|
/**
|
|
* Show the chat conversation. Published apart from the rest of the
|
|
* control: a typed reply reveals frame by frame.
|
|
* @param {Array<{id: number, role: string, text: string}>} next
|
|
*/
|
|
setChatMessages(next) {
|
|
if (removed) return;
|
|
chatMessages = next;
|
|
if (owns()) store.publishChat(chatMessages);
|
|
},
|
|
/**
|
|
* Show the waveform's bars (null rests it). Published apart from the
|
|
* rest of the control, and only when they change.
|
|
* @param {?Array<{level: string, opacity: string}>} next
|
|
*/
|
|
setBars(next) {
|
|
if (removed || sameBars(bars, next)) return;
|
|
bars = next;
|
|
if (owns()) store.publishBars(bars);
|
|
},
|
|
/** The error tray's DISMISS. */
|
|
dismissError() {
|
|
ui.update('root', { errorDismissed: true });
|
|
},
|
|
/** The rendered element by name, or null before it renders. */
|
|
element,
|
|
/** Whether the named element is rendered and visible on the page. */
|
|
visible: (name) => shown(element(name)),
|
|
/**
|
|
* Once the view has rendered the latest state, focus the first of the
|
|
* named elements that is visible, or with `anyVisibility`, that is
|
|
* rendered (focus may be what reveals it).
|
|
* @param {string[]} names
|
|
* @param {{anyVisibility?: boolean}} [options]
|
|
*/
|
|
focus(names, { anyVisibility = false } = {}) {
|
|
return nextTick(() => {
|
|
const target = names
|
|
.map(element)
|
|
.find((node) => node && (anyVisibility || shown(node)));
|
|
target?.focus?.();
|
|
return target ?? null;
|
|
});
|
|
},
|
|
/** Remove the control from the dock, unless a newer one replaced it. */
|
|
remove() {
|
|
if (removed) return;
|
|
if (store.controls === ui) {
|
|
store.connect(null);
|
|
store.publish(null);
|
|
store.publishBars(null);
|
|
store.publishChat([]);
|
|
}
|
|
removed = true;
|
|
offLocale();
|
|
listeners.clear();
|
|
},
|
|
};
|
|
const offLocale = onLocaleChange(() => {
|
|
if (removed) return;
|
|
for (const [part, fields] of wording) {
|
|
const patch = {};
|
|
for (const [field, value] of fields) patch[field] = value();
|
|
state = { ...state, [part]: { ...state[part], ...patch } };
|
|
}
|
|
if (defaultCard) state = { ...state, card: initialCard() };
|
|
publish();
|
|
ui.emit('locale');
|
|
});
|
|
store.connect(ui);
|
|
store.publishBars(null);
|
|
store.publishChat(chatMessages);
|
|
publish();
|
|
return ui;
|
|
}
|