chore(voice): security, cleanup, resilience
All checks were successful
Deploy / deploy (push) Successful in 1m47s

Безопасность:
- Rate-limit на /api/voice/chat (20/мин per cookie/IP, env VOICE_RATE_LIMIT).
  Защищает от случайных циклов и утечки PIN.
- Усечение user prompt'а до 4000 символов в /api/voice/chat.
- Tool-loop защита от циклов: если LLM дважды просит тот же tool с теми же
  args — прерываем (раньше мог уйти в бесконечный цикл при tool error'ах).

Чистка кода:
- lib/debug.ts — vlog/vwarn/verror гейтят браузерные логи за
  NEXT_PUBLIC_VOICE_DEBUG=1 (или localStorage 'voice-debug=1').
  Серверные console.log оставлены — полезны в Docker logs.
- lib/audio-wav.ts — вынесена дублированная floatToWav из VoiceController.
- Удалены orphan компоненты FocusCard.tsx и CountdownCard.tsx
  (не подключены, отвергнуты по UX-фидбеку).

Resilience:
- WakeWordDetector: drop-on-busy в onChunk — на медленных устройствах
  (Android, бюджетный CPU) backlog inference больше не копится.
- voice-history fallback на /tmp/voice-history если /data не примонтирован
  (локальная разработка / нестандартная конфигурация).
This commit is contained in:
Cosmo
2026-04-27 12:44:18 +00:00
parent 3211d62198
commit 05b300d472
9 changed files with 169 additions and 683 deletions

View File

@@ -1,198 +0,0 @@
'use client'
import { useEffect, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { Sparkles } from 'lucide-react'
interface Countdown {
id: string
label: string
date: string
emoji?: string
color?: string
note?: string
}
interface Computed {
id: string
label: string
emoji?: string
days: number
accent: string
dateStr: string
}
const ROTATE_MS = 8_000
function resolveColor(c: Countdown): string {
if (!c.color) return 'var(--data-violet)'
if (c.color.startsWith('#')) return c.color
// токен вида "data-rose" → "var(--data-rose)"
return `var(--${c.color})`
}
function formatDate(iso: string): string {
try {
return new Date(iso + 'T00:00:00').toLocaleDateString('ru-RU', {
day: 'numeric', month: 'long',
})
} catch {
return iso
}
}
function daysFromNow(iso: string): number {
const target = new Date(iso + 'T00:00:00').getTime()
const today = new Date()
today.setHours(0, 0, 0, 0)
return Math.ceil((target - today.getTime()) / 86_400_000)
}
function pluralizeDays(n: number): string {
const a = Math.abs(n)
if (a % 10 === 1 && a % 100 !== 11) return 'день'
if ([2, 3, 4].includes(a % 10) && ![12, 13, 14].includes(a % 100)) return 'дня'
return 'дней'
}
export default function CountdownCard() {
const [items, setItems] = useState<Computed[]>([])
const [idx, setIdx] = useState(0)
const [loading, setLoading] = useState(true)
useEffect(() => {
let cancelled = false
fetch('/api/countdowns')
.then(r => r.json())
.then(d => {
if (cancelled) return
const list: Countdown[] = d.countdowns || []
const computed = list
.map(c => ({
id: c.id,
label: c.label,
emoji: c.emoji,
days: daysFromNow(c.date),
accent: resolveColor(c),
dateStr: formatDate(c.date),
}))
.filter(c => c.days >= 0) // прошедшие скрываем
.sort((a, b) => a.days - b.days)
setItems(computed)
})
.catch(() => {})
.finally(() => { if (!cancelled) setLoading(false) })
return () => { cancelled = true }
}, [])
useEffect(() => {
if (items.length <= 1) return
const t = setInterval(() => setIdx(i => (i + 1) % items.length), ROTATE_MS)
return () => clearInterval(t)
}, [items.length])
if (loading) {
return (
<div className="card" style={{
padding: '16px 18px', minHeight: 128,
display: 'flex', alignItems: 'center', justifyContent: 'center',
color: 'var(--text-tertiary)', fontSize: 12,
}}>
</div>
)
}
if (items.length === 0) {
return (
<div className="card" style={{
padding: '18px 20px', minHeight: 128,
display: 'flex', flexDirection: 'column', gap: 10,
color: 'var(--text-tertiary)',
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Sparkles size={14} />
<span className="eyebrow">Отсчёт</span>
</div>
<div style={{ fontSize: 13, lineHeight: 1.5 }}>
Добавь в настройках отпуск, др, дедлайн.
</div>
</div>
)
}
const current = items[Math.min(idx, items.length - 1)]
const imminent = current.days <= 3
const soon = current.days <= 7
return (
<div
className="card grain"
style={{
padding: '18px 20px',
minHeight: 128,
display: 'flex', flexDirection: 'column', gap: 10,
position: 'relative', overflow: 'hidden',
background: `linear-gradient(135deg, color-mix(in srgb, ${current.accent} 12%, var(--surface-1)), var(--surface-1))`,
border: `1px solid color-mix(in srgb, ${current.accent} 22%, var(--border-subtle))`,
transition: 'background 0.6s ease, border-color 0.6s ease',
}}
>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Sparkles size={14} color={current.accent} />
<span className="eyebrow" style={{ color: current.accent }}>Отсчёт</span>
{items.length > 1 && (
<div style={{ marginLeft: 'auto', display: 'flex', gap: 4 }}>
{items.map((_, i) => (
<div key={i} style={{
width: i === idx ? 14 : 5, height: 5, borderRadius: 3,
background: i === idx ? current.accent : 'var(--surface-3)',
transition: 'all 0.4s ease',
}} />
))}
</div>
)}
</div>
<AnimatePresence mode="wait">
<motion.div
key={current.id}
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -4 }}
transition={{ duration: 0.4 }}
style={{ display: 'flex', flexDirection: 'column', gap: 4 }}
>
<div style={{ display: 'flex', alignItems: 'baseline', gap: 10 }}>
<div className="num-display" style={{
fontSize: current.days === 0 ? 36 : 48,
color: imminent ? current.accent : 'var(--text-primary)',
letterSpacing: '-0.04em',
}}>
{current.days === 0 ? 'сегодня' : current.days}
</div>
{current.days > 0 && (
<div style={{
fontSize: 15, color: 'var(--text-secondary)', fontWeight: 600,
}}>
{pluralizeDays(current.days)}
</div>
)}
</div>
<div style={{
fontSize: 14, fontWeight: 700, color: 'var(--text-primary)',
display: 'flex', alignItems: 'center', gap: 8,
}}>
{current.emoji && <span style={{ fontSize: 16 }}>{current.emoji}</span>}
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
{current.label}
</span>
</div>
<div style={{ fontSize: 11, color: 'var(--text-tertiary)' }}>
{current.dateStr}
</div>
</motion.div>
</AnimatePresence>
</div>
)
}

View File

@@ -1,426 +0,0 @@
'use client'
import { useEffect, useMemo, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import {
Umbrella, Wind, ThermometerSun, TramFront, Calendar as CalendarIcon,
Receipt, Sparkles, Moon, Sun,
} from 'lucide-react'
// ——————————————————————————————
// Types
// ——————————————————————————————
export interface FocusWeather {
temp: string
desc: string
feelsLike?: string
}
export interface FocusTram {
route: string
minutes: number
direction: string
}
export interface FocusEvent {
id: string
title: string
start: string
allDay?: boolean
ownerName?: string
color?: string
}
export interface FocusCountdown {
label: string
date: string // ISO YYYY-MM-DD
}
export interface FocusBill {
title: string
amount: string
daysLeft: number
}
interface Props {
weather: FocusWeather | null
tramNext?: FocusTram | null
nextEvent?: FocusEvent | null
countdowns?: FocusCountdown[]
bills?: FocusBill[]
}
// ——————————————————————————————
// Focus state machine
// ——————————————————————————————
type FocusKind =
| { kind: 'morning-outfit'; tempNow: string; feels: string; advice: string; Icon: any; accent: string }
| { kind: 'tram-imminent'; route: string; minutes: number; direction: string }
| { kind: 'event-upcoming'; title: string; inMinutes: number; owner?: string; color?: string }
| { kind: 'countdown'; label: string; days: number }
| { kind: 'bill-due'; title: string; amount: string; daysLeft: number }
| { kind: 'night'; hour: number }
| { kind: 'quiet'; greeting: string }
function pickFocus(p: Props, hour: number): FocusKind {
// 1. Bill due today / tomorrow — всегда приоритет
const bill = p.bills?.find(b => b.daysLeft <= 1)
if (bill) return { kind: 'bill-due', ...bill }
// 2. Ближайшее событие ≤30 минут
if (p.nextEvent && !p.nextEvent.allDay) {
const start = new Date(p.nextEvent.start).getTime()
const inMin = Math.round((start - Date.now()) / 60_000)
if (inMin >= -5 && inMin <= 30) {
return {
kind: 'event-upcoming',
title: p.nextEvent.title,
inMinutes: inMin,
owner: p.nextEvent.ownerName,
color: p.nextEvent.color,
}
}
}
// 3. Трамвай в рабочий час, ≤3 мин
const rushHour = (hour >= 7 && hour <= 10) || (hour >= 17 && hour <= 20)
if (rushHour && p.tramNext && p.tramNext.minutes >= 0 && p.tramNext.minutes <= 3) {
return { kind: 'tram-imminent', ...p.tramNext }
}
// 4. Утро (7-10) → одевалка
if (hour >= 7 && hour < 11 && p.weather) {
const t = parseInt(p.weather.temp, 10)
const descLower = p.weather.desc?.toLowerCase() || ''
const rain = /дожд|ливен|грозa|морос/.test(descLower)
const snow = /снег|метел/.test(descLower)
const advice =
rain ? 'возьми зонт' :
snow ? 'шапка и зимняя обувь' :
t <= -10 ? 'пуховик, шапка, перчатки' :
t < 0 ? 'шапка и перчатки' :
t < 7 ? 'тёплая куртка' :
t < 15 ? 'лёгкая куртка' :
t < 22 ? 'свитер или рубашка' : 'футболка'
const Icon = rain ? Umbrella : snow ? Wind : t < 0 ? Wind : ThermometerSun
const accent =
rain ? 'var(--data-cool)' :
snow ? 'var(--data-info)' :
t < 0 ? 'var(--data-info)' :
t >= 22 ? 'var(--data-warm)' : 'var(--data-warm)'
return {
kind: 'morning-outfit',
tempNow: p.weather.temp,
feels: p.weather.feelsLike || '',
advice, Icon, accent,
}
}
// 5. Ближайший countdown (≤14 дней)
const cd = (p.countdowns || [])
.map(c => {
const target = new Date(c.date + 'T00:00:00').getTime()
const days = Math.ceil((target - Date.now()) / 86_400_000)
return { label: c.label, days }
})
.filter(c => c.days >= 0 && c.days <= 14)
.sort((a, b) => a.days - b.days)[0]
if (cd) return { kind: 'countdown', ...cd }
// 6. Ночь
if (hour >= 22 || hour < 5) return { kind: 'night', hour }
// 7. Тихо — приветствие
const greeting =
hour >= 5 && hour < 12 ? 'Доброе утро' :
hour >= 12 && hour < 17 ? 'Добрый день' :
hour >= 17 && hour < 22 ? 'Добрый вечер' : 'Доброй ночи'
return { kind: 'quiet', greeting }
}
// ——————————————————————————————
// Presentations
// ——————————————————————————————
function Eyebrow({ children }: { children: React.ReactNode }) {
return (
<div style={{
fontSize: 11, fontWeight: 700, letterSpacing: '0.14em',
textTransform: 'uppercase', color: 'var(--text-tertiary)',
}}>
{children}
</div>
)
}
function FocusShell({
eyebrow,
accent,
children,
}: {
eyebrow: React.ReactNode
accent: string
children: React.ReactNode
}) {
return (
<div
className="focus-card grain"
style={{
padding: '26px 28px',
display: 'flex', flexDirection: 'column',
minHeight: 220,
background: `linear-gradient(180deg, color-mix(in srgb, ${accent} 10%, var(--surface-1)), var(--surface-1))`,
border: `1px solid color-mix(in srgb, ${accent} 22%, var(--border-subtle))`,
}}
>
{/* Top eyebrow */}
<div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 14 }}>
{eyebrow}
</div>
{children}
</div>
)
}
// ——————————————————————————————
// Individual states
// ——————————————————————————————
function MorningOutfit(f: Extract<FocusKind, { kind: 'morning-outfit' }>) {
return (
<FocusShell
accent={f.accent}
eyebrow={<>
<f.Icon size={16} color={f.accent} strokeWidth={2} />
<Eyebrow>Собираясь на улицу</Eyebrow>
</>}
>
<div style={{ display: 'flex', alignItems: 'baseline', gap: 14, marginBottom: 16 }}>
<div className="num-display" style={{ fontSize: 96, color: 'var(--text-primary)' }}>
{f.tempNow}°
</div>
{f.feels && (
<div style={{ fontSize: 15, color: 'var(--text-secondary)' }}>
ощущается <span className="num" style={{ fontWeight: 700, color: 'var(--text-primary)' }}>{f.feels}°</span>
</div>
)}
</div>
<div style={{
fontSize: 22, fontWeight: 700, color: 'var(--text-primary)',
letterSpacing: '-0.3px', lineHeight: 1.3,
}}>
{f.advice}
</div>
</FocusShell>
)
}
function TramImminent(f: Extract<FocusKind, { kind: 'tram-imminent' }>) {
const accent = 'var(--data-hot)'
return (
<FocusShell
accent={accent}
eyebrow={<>
<TramFront size={16} color={accent} strokeWidth={2} />
<Eyebrow>Трамвай подходит</Eyebrow>
</>}
>
<div style={{ display: 'flex', alignItems: 'baseline', gap: 12, marginBottom: 14 }}>
<div className="num-display" style={{ fontSize: 96, color: accent }}>
{f.minutes <= 0 ? 'сейчас' : f.minutes}
</div>
{f.minutes > 0 && (
<div style={{ fontSize: 24, fontWeight: 600, color: 'var(--text-secondary)' }}>мин</div>
)}
</div>
<div style={{ fontSize: 18, fontWeight: 700, color: 'var(--text-primary)' }}>
Маршрут {f.route} · {f.direction}
</div>
</FocusShell>
)
}
function EventUpcoming(f: Extract<FocusKind, { kind: 'event-upcoming' }>) {
const accent = f.color || 'var(--accent)'
const timeWord = f.inMinutes < 0 ? 'сейчас' : f.inMinutes === 0 ? 'сейчас' : `через ${f.inMinutes}м`
return (
<FocusShell
accent={accent}
eyebrow={<>
<CalendarIcon size={16} color={accent} strokeWidth={2} />
<Eyebrow>{timeWord}</Eyebrow>
</>}
>
<div style={{
fontSize: 32, fontWeight: 800, color: 'var(--text-primary)',
letterSpacing: '-0.8px', lineHeight: 1.15, marginBottom: 14,
}}>
{f.title}
</div>
{f.owner && (
<div style={{
display: 'inline-flex', alignItems: 'center', gap: 8,
padding: '6px 12px', borderRadius: 14,
background: `color-mix(in srgb, ${accent} 16%, var(--surface-2))`,
border: `1px solid color-mix(in srgb, ${accent} 28%, var(--border-subtle))`,
color: accent, fontSize: 13, fontWeight: 700, alignSelf: 'flex-start',
}}>
{f.owner}
</div>
)}
</FocusShell>
)
}
function CountdownView(f: Extract<FocusKind, { kind: 'countdown' }>) {
const accent = f.days <= 3 ? 'var(--data-hot)' : f.days <= 7 ? 'var(--data-warm)' : 'var(--data-violet)'
const word =
f.days === 0 ? 'сегодня' :
f.days === 1 ? 'завтра' :
f.days < 5 ? `${f.days} дня` : `${f.days} дней`
return (
<FocusShell
accent={accent}
eyebrow={<>
<Sparkles size={16} color={accent} strokeWidth={2} />
<Eyebrow>До события</Eyebrow>
</>}
>
<div className="num-display" style={{
fontSize: f.days === 0 ? 56 : 96,
color: 'var(--text-primary)', marginBottom: 12,
}}>
{f.days === 0 ? 'сегодня' : f.days}
{f.days > 0 && <span style={{ fontSize: 24, fontWeight: 600, color: 'var(--text-secondary)', marginLeft: 10, letterSpacing: 0 }}>
{f.days < 5 ? 'дня' : 'дней'}
</span>}
</div>
<div style={{ fontSize: 20, fontWeight: 700, color: 'var(--text-primary)' }}>
{f.label}
</div>
</FocusShell>
)
}
function BillDue(f: Extract<FocusKind, { kind: 'bill-due' }>) {
const accent = 'var(--data-danger)'
const word = f.daysLeft === 0 ? 'сегодня' : f.daysLeft === 1 ? 'завтра' : `через ${f.daysLeft}д`
return (
<FocusShell
accent={accent}
eyebrow={<>
<Receipt size={16} color={accent} strokeWidth={2} />
<Eyebrow>К оплате {word}</Eyebrow>
</>}
>
<div className="num-display" style={{
fontSize: 72, color: accent, marginBottom: 8, letterSpacing: '-0.03em',
}}>
{f.amount}
</div>
<div style={{ fontSize: 20, fontWeight: 700, color: 'var(--text-primary)' }}>
{f.title}
</div>
</FocusShell>
)
}
function NightView() {
const accent = 'var(--data-violet)'
return (
<FocusShell
accent={accent}
eyebrow={<>
<Moon size={16} color={accent} strokeWidth={2} />
<Eyebrow>Тихое время</Eyebrow>
</>}
>
<div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center', flex: 1, gap: 8 }}>
<div style={{ fontSize: 28, fontWeight: 800, color: 'var(--text-primary)', letterSpacing: '-0.5px' }}>
Спокойной ночи
</div>
<div style={{ fontSize: 14, color: 'var(--text-secondary)', lineHeight: 1.5 }}>
Дом в режиме ожидания. Касание разблокировать.
</div>
</div>
</FocusShell>
)
}
function QuietView({ greeting, weather }: { greeting: string; weather: FocusWeather | null }) {
const accent = 'var(--accent)'
const [now, setNow] = useState(() => new Date())
useEffect(() => {
const t = setInterval(() => setNow(new Date()), 30_000)
return () => clearInterval(t)
}, [])
return (
<FocusShell
accent={accent}
eyebrow={<>
<Sun size={16} color={accent} strokeWidth={2} />
<Eyebrow>{now.toLocaleDateString('ru-RU', { weekday: 'long', day: 'numeric', month: 'long' })}</Eyebrow>
</>}
>
<div style={{ fontSize: 32, fontWeight: 800, color: 'var(--text-primary)', letterSpacing: '-0.5px', marginBottom: 12 }}>
{greeting}
</div>
{weather && (
<div style={{ display: 'flex', alignItems: 'baseline', gap: 12 }}>
<div className="num-display" style={{ fontSize: 56, color: 'var(--text-primary)' }}>
{weather.temp}°
</div>
<div style={{ fontSize: 15, color: 'var(--text-secondary)', fontWeight: 500 }}>
{weather.desc}
</div>
</div>
)}
</FocusShell>
)
}
// ——————————————————————————————
// Root
// ——————————————————————————————
export default function FocusCard(props: Props) {
const [hour, setHour] = useState(() => new Date().getHours())
useEffect(() => {
const t = setInterval(() => setHour(new Date().getHours()), 60_000)
return () => clearInterval(t)
}, [])
const focus = useMemo(() => pickFocus(props, hour), [props, hour])
const key = focus.kind + (
focus.kind === 'morning-outfit' ? focus.advice :
focus.kind === 'tram-imminent' ? `${focus.route}-${focus.minutes}` :
focus.kind === 'event-upcoming' ? focus.title :
focus.kind === 'countdown' ? focus.label :
focus.kind === 'bill-due' ? focus.title :
focus.kind === 'quiet' ? focus.greeting : ''
)
return (
<AnimatePresence mode="wait">
<motion.div
key={key}
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -8 }}
transition={{ duration: 0.35, ease: [0.4, 0, 0.2, 1] }}
style={{ display: 'flex', flexDirection: 'column', height: '100%' }}
>
{focus.kind === 'morning-outfit' && <MorningOutfit {...focus} />}
{focus.kind === 'tram-imminent' && <TramImminent {...focus} />}
{focus.kind === 'event-upcoming' && <EventUpcoming {...focus} />}
{focus.kind === 'countdown' && <CountdownView {...focus} />}
{focus.kind === 'bill-due' && <BillDue {...focus} />}
{focus.kind === 'night' && <NightView />}
{focus.kind === 'quiet' && <QuietView greeting={focus.greeting} weather={props.weather} />}
</motion.div>
</AnimatePresence>
)
}

View File

@@ -16,6 +16,8 @@
import { useEffect, useRef, useState } from 'react'
import { Mic, MicOff } from 'lucide-react'
import { WakeWordDetector } from '@/lib/wake-word'
import { floatToWav } from '@/lib/audio-wav'
import { vlog, vwarn, verror } from '@/lib/debug'
type Agent = 'cosmo' | 'lusya'
type ControllerState = 'idle' | 'loading' | 'listening' | 'recording' | 'busy' | 'error'
@@ -31,35 +33,6 @@ function emitLocal(event: string, agent: Agent, text?: string) {
)
}
function floatToWav(audio: Float32Array, sampleRate = 16000): Blob {
const numSamples = audio.length
const buffer = new ArrayBuffer(44 + numSamples * 2)
const view = new DataView(buffer)
writeStr(view, 0, 'RIFF')
view.setUint32(4, 36 + numSamples * 2, true)
writeStr(view, 8, 'WAVE')
writeStr(view, 12, 'fmt ')
view.setUint32(16, 16, true)
view.setUint16(20, 1, true)
view.setUint16(22, 1, true)
view.setUint32(24, sampleRate, true)
view.setUint32(28, sampleRate * 2, true)
view.setUint16(32, 2, true)
view.setUint16(34, 16, true)
writeStr(view, 36, 'data')
view.setUint32(40, numSamples * 2, true)
let offset = 44
for (let i = 0; i < numSamples; i++, offset += 2) {
const s = Math.max(-1, Math.min(1, audio[i]))
view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true)
}
return new Blob([buffer], { type: 'audio/wav' })
}
function writeStr(view: DataView, offset: number, s: string) {
for (let i = 0; i < s.length; i++) view.setUint8(offset + i, s.charCodeAt(i))
}
export default function VoiceController() {
const [state, setState] = useState<ControllerState>('idle')
const wakeRef = useRef<WakeWordDetector | null>(null)
@@ -67,12 +40,12 @@ export default function VoiceController() {
const busyRef = useRef(false)
useEffect(() => {
console.log('[VoiceController] mounted, state=idle, ждём тап на микрофон')
vlog('[VoiceController] mounted, state=idle, ждём тап на микрофон')
// Кнопка X в overlay шлёт voice-cancel → ставим VAD на паузу
// (НЕ destroy — иначе следующий wake снова будет ждать 1-2с на инициализацию).
const onCancel = () => {
console.log('[voice] cancel — пауза VAD')
vlog('[voice] cancel — пауза VAD')
try { vadRef.current?.pause?.() } catch {}
busyRef.current = false
try { wakeRef.current?.resume?.() } catch {}
@@ -119,7 +92,7 @@ export default function VoiceController() {
})
if (!chatResp.ok) throw new Error(`chat ${chatResp.status}`)
} catch (e) {
console.error('[voice] pipeline error:', e)
verror('[voice] pipeline error:', e)
emitLocal('error', AGENT, 'Не получилось')
} finally {
busyRef.current = false
@@ -154,16 +127,16 @@ export default function VoiceController() {
})
vadRef.current = vad
// Не вызываем start — ждём пока wake-word триггернёт.
console.log('[voice] VAD preloaded (paused)')
vlog('[voice] VAD preloaded (paused)')
} catch (e: any) {
console.error('[voice] VAD init failed:', e?.name, e?.message, e)
verror('[voice] VAD init failed:', e?.name, e?.message, e)
// Не вырубаем wake — может на ручной trigger ещё попробуем
emitLocal('error', AGENT, `VAD: ${e?.message?.slice(0, 60) || 'init'}`)
}
}
const onWakeDetected = async (score: number) => {
console.log(`[wake] cosmo score=${score.toFixed(3)}`)
vlog(`[wake] cosmo score=${score.toFixed(3)}`)
if (busyRef.current) return
// Пауза wake чтобы VAD-инициализация и команда не триггерили wake снова на эхе.
try { wakeRef.current?.pause?.() } catch {}
@@ -190,9 +163,9 @@ export default function VoiceController() {
}
const ctx: AudioContext | undefined = w.__voicePlaybackCtx
if (ctx && ctx.state === 'suspended') await ctx.resume()
console.log('[voice] playback AudioContext state=', ctx?.state)
vlog('[voice] playback AudioContext state=', ctx?.state)
} catch (e: any) {
console.warn('[voice] AudioContext init failed:', e?.message)
vwarn('[voice] AudioContext init failed:', e?.message)
}
// 1. Запрос разрешения на микрофон отдельно
@@ -200,7 +173,7 @@ export default function VoiceController() {
const probe = await navigator.mediaDevices.getUserMedia({ audio: true })
probe.getTracks().forEach((t) => t.stop())
} catch (e: any) {
console.error('[voice] mic permission failed:', e?.name, e?.message)
verror('[voice] mic permission failed:', e?.name, e?.message)
setState('error')
emitLocal('error', AGENT, e?.name === 'NotAllowedError' ? 'Нет доступа к микрофону' : 'Микрофон не открылся')
return
@@ -220,12 +193,12 @@ export default function VoiceController() {
if (s > maxScore) maxScore = s
scoreCount++
if (scoreCount % 25 === 0) {
console.log(`[wake] alive · max score за окно=${maxScore.toFixed(3)} · scoreCount=${scoreCount}`)
vlog(`[wake] alive · max score за окно=${maxScore.toFixed(3)} · scoreCount=${scoreCount}`)
maxScore = 0
}
if (s > 0.15) console.log(`[wake] score=${s.toFixed(3)}`)
if (s > 0.15) vlog(`[wake] score=${s.toFixed(3)}`)
},
onError: (e) => console.warn('[wake] error', e),
onError: (e) => vwarn('[wake] error', e),
})
await wake.start()
wakeRef.current = wake
@@ -233,7 +206,7 @@ export default function VoiceController() {
// VAD НЕ прелоадим — его второй getUserMedia мешает wake-word audio.
// Грузится при первом wake (~1-2с), но дальше переиспользуется (см. handleSpeechEnd).
} catch (e: any) {
console.error('[wake] init failed:', e)
verror('[wake] init failed:', e)
setState('error')
emitLocal('error', AGENT, `Wake: ${e?.message?.slice(0, 60) || 'init'}`)
}
@@ -252,7 +225,7 @@ export default function VoiceController() {
// Долгий тап = ручной триггер (как раньше push-to-talk). Короткий — toggle вкл/выкл.
// Для простоты сейчас: короткий тап в idle = активация; короткий тап в active = выкл.
const onTap = async () => {
console.log(`[VoiceController] tap! state=${state}`)
vlog(`[VoiceController] tap! state=${state}`)
if (state === 'idle' || state === 'error') {
await start()
} else if (state === 'listening') {

View File

@@ -2,6 +2,7 @@
import { useEffect, useRef, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { X } from 'lucide-react'
import { vwarn } from '@/lib/debug'
type VoiceState = 'idle' | 'wake' | 'listening' | 'command' | 'response' | 'error'
type Agent = 'cosmo' | 'lusya'
@@ -112,7 +113,7 @@ export default function VoiceOverlay() {
source.start()
return
} catch (e: any) {
console.warn('[voice] AudioContext playback failed, fallback to <audio>:', e?.message || e)
vwarn('[voice] AudioContext playback failed, fallback to <audio>:', e?.message || e)
}
}
@@ -133,7 +134,7 @@ export default function VoiceOverlay() {
try {
await audio.play()
} catch (e: any) {
console.warn('[voice] audio.play() rejected:', e?.name || e?.message || e)
vwarn('[voice] audio.play() rejected:', e?.name || e?.message || e)
finish()
}
} catch {