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

@@ -18,6 +18,31 @@ import {
const MODEL = process.env.ANTHROPIC_MODEL || 'claude-haiku-4-5'
const MAX_TOKENS = parseInt(process.env.VOICE_MAX_TOKENS || '300', 10)
const MAX_TOOL_ROUNDS = 4
const RATE_LIMIT_PER_MINUTE = parseInt(process.env.VOICE_RATE_LIMIT || '20', 10)
// In-memory rate-limit per IP / cookie (host один — Docker контейнер).
// Защита от случайного бесконечного цикла или утечки PIN: даже если
// auth_token утечёт, вызов /api/voice/chat будет ограничен.
const rateBuckets = new Map<string, { count: number; resetAt: number }>()
function rateLimit(key: string): boolean {
const now = Date.now()
const b = rateBuckets.get(key)
if (!b || b.resetAt <= now) {
rateBuckets.set(key, { count: 1, resetAt: now + 60_000 })
return true
}
if (b.count >= RATE_LIMIT_PER_MINUTE) return false
b.count++
return true
}
// Гигиена: чистим старые бакеты периодически (раз в 5 минут максимум).
let lastSweep = 0
function sweep() {
const now = Date.now()
if (now - lastSweep < 5 * 60_000) return
lastSweep = now
for (const [k, v] of rateBuckets) if (v.resetAt <= now) rateBuckets.delete(k)
}
let _client: Anthropic | null = null
function client(): Anthropic {
@@ -44,11 +69,23 @@ function emitVoice(event: string, agent: 'cosmo' | 'lusya', text?: string) {
type AgentId = 'cosmo' | 'lusya'
export async function POST(req: Request) {
// Rate-limit по auth_token (или x-voice-internal — для loopback'а от tools).
// Идентифицируем клиента: cookie auth_token > x-voice-internal > IP > 'anon'.
const cookie = req.headers.get('cookie') || ''
const tokenMatch = cookie.match(/auth_token=([a-f0-9]{32,})/i)
const internal = req.headers.get('x-voice-internal') || ''
const fwd = req.headers.get('x-forwarded-for') || ''
const ratekey = tokenMatch?.[1] || (internal ? 'internal' : '') || fwd.split(',')[0].trim() || 'anon'
sweep()
if (!rateLimit(ratekey)) {
return NextResponse.json({ error: 'rate_limited' }, { status: 429 })
}
const body = await req.json().catch(() => null)
if (!body || typeof body.text !== 'string' || !body.text.trim()) {
return NextResponse.json({ error: 'text required' }, { status: 400 })
}
const userText: string = body.text.trim()
const userText: string = body.text.trim().slice(0, 4000) // защита от gigantic prompts
const agent: AgentId = body.agent === 'lusya' ? 'lusya' : 'cosmo'
// Echo command в орб
@@ -78,6 +115,9 @@ export async function POST(req: Request) {
let finalText = ''
const initialUserIdx = history.length - 1
// Защита от tool-cycling: запоминаем последний (name, args) — если LLM
// дважды подряд просит одно и то же, прерываем цикл.
let lastToolSig = ''
try {
const c = client()
@@ -109,6 +149,18 @@ export async function POST(req: Request) {
apiMessages.push({ role: 'assistant', content: resp.content as any })
if (resp.stop_reason === 'tool_use' && toolUses.length) {
// Сигнатура текущего раунда — для loop-guard.
const sig = toolUses
.map((t) => `${t.name}:${JSON.stringify(t.input)}`)
.sort()
.join('|')
if (sig === lastToolSig) {
console.warn('[voice/chat] tool cycle detected, breaking loop')
finalText += '\nНе получилось выполнить запрос.'
break
}
lastToolSig = sig
const toolResults: Anthropic.ToolResultBlockParam[] = []
for (const tu of toolUses) {
console.log(`[voice/chat] tool ${tu.name}(${JSON.stringify(tu.input).slice(0, 200)})`)

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 {

35
lib/audio-wav.ts Normal file
View File

@@ -0,0 +1,35 @@
/**
* Float32 [-1, 1] PCM → WAV blob (mono, 16-bit, заданная частота).
* Используется браузером для отправки записанной фразы в STT.
*/
export 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) // fmt chunk size
view.setUint16(20, 1, true) // PCM
view.setUint16(22, 1, true) // channels
view.setUint32(24, sampleRate, true)
view.setUint32(28, sampleRate * 2, true) // byte rate
view.setUint16(32, 2, true) // block align
view.setUint16(34, 16, true) // bits per sample
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): void {
for (let i = 0; i < s.length; i++) view.setUint8(offset + i, s.charCodeAt(i))
}

36
lib/debug.ts Normal file
View File

@@ -0,0 +1,36 @@
/**
* Гейт для браузерных дебаг-логов голосового стека.
*
* Production: логи молчат. Чтобы включить — выставить
* `NEXT_PUBLIC_VOICE_DEBUG=1` в env (на этапе билда). На клиенте
* также можно временно включить `localStorage.setItem('voice-debug', '1')`.
*
* Серверные логи (`/api/voice/chat`, `/api/voice/stt`) НЕ пропускаются
* через эту функцию — они полезны в Docker logs и не утекают в браузер.
*/
const ENV_ENABLED = process.env.NEXT_PUBLIC_VOICE_DEBUG === '1'
function runtimeEnabled(): boolean {
if (ENV_ENABLED) return true
if (typeof window === 'undefined') return false
try {
return window.localStorage?.getItem('voice-debug') === '1'
} catch {
return false
}
}
export function vlog(...args: any[]): void {
if (runtimeEnabled()) console.log(...args)
}
export function vwarn(...args: any[]): void {
// Warnings оставляем всегда — это анонимные ошибки, не PII.
console.warn(...args)
}
export function verror(...args: any[]): void {
// Errors всегда — нам нужно знать о реальных падениях.
console.error(...args)
}

View File

@@ -1,11 +1,19 @@
/**
* История диалога per-agent per-day. Файлы в /data/voice-history/{agent}-{date}.json.
* /data — это volume контейнера (на хосте /opt/digital-home/smart-home-tablet-data/).
*
* Fallback: если /data не существует (локальная разработка) — пишем в /tmp/voice-history.
*/
import { promises as fs } from 'node:fs'
import { existsSync } from 'node:fs'
import path from 'node:path'
const DATA_DIR = process.env.VOICE_HISTORY_DIR || '/data/voice-history'
const PRIMARY_DIR = process.env.VOICE_HISTORY_DIR || '/data/voice-history'
const DATA_DIR = (() => {
// Проверяем существование родителя (/data) — без него запись упадёт ENOENT.
const parent = path.dirname(PRIMARY_DIR)
return existsSync(parent) ? PRIMARY_DIR : '/tmp/voice-history'
})()
const MAX_HISTORY = parseInt(process.env.VOICE_MAX_HISTORY || '40', 10)
export type HistoryMessage = {

View File

@@ -1,3 +1,5 @@
import { vlog, verror } from './debug'
/**
* openWakeWord pipeline в браузере.
*
@@ -88,6 +90,7 @@ export class WakeWordDetector {
private embBuf: Float32Array[] = [] // массив 96-D векторов
private cooldownChunks = 0
private running = false
private processing = false // ONNX inference в полёте — drop-on-busy
constructor(options: WakeWordOptions) {
this.opts = {
@@ -102,10 +105,10 @@ export class WakeWordDetector {
async start(externalStream?: MediaStream): Promise<void> {
if (this.running) return
console.log('[wake] start: loading ort + models')
vlog('[wake] start: loading ort + models')
const t0 = performance.now()
const ort = await getOrt()
console.log(`[wake] ort ready in ${(performance.now() - t0).toFixed(0)}ms`)
vlog(`[wake] ort ready in ${(performance.now() - t0).toFixed(0)}ms`)
// 1. Загружаем модели параллельно (до user gesture, чтобы AudioContext не висел)
const [mel, emb, cls] = await Promise.all([
@@ -119,18 +122,14 @@ export class WakeWordDetector {
this.melInName = mel.inputNames[0]; this.melOutName = mel.outputNames[0]
this.embInName = emb.inputNames[0]; this.embOutName = emb.outputNames[0]
this.clsInName = cls.inputNames[0]; this.clsOutName = cls.outputNames[0]
console.log(`[wake] models loaded in ${(performance.now() - t0).toFixed(0)}ms`,
{ mel: { in: this.melInName, out: this.melOutName },
emb: { in: this.embInName, out: this.embOutName },
cls: { in: this.clsInName, out: this.clsOutName } })
vlog(`[wake] models loaded in ${(performance.now() - t0).toFixed(0)}ms`)
// 2. Audio context @ 16kHz (если браузер не уважит — обработаем на стороне)
this.ctx = new AudioContext({ sampleRate: 16000 })
if (this.ctx.state === 'suspended') await this.ctx.resume()
console.log(`[wake] AudioContext sampleRate=${this.ctx.sampleRate} state=${this.ctx.state}`)
vlog(`[wake] AudioContext sampleRate=${this.ctx.sampleRate} state=${this.ctx.state}`)
if (this.ctx.sampleRate !== 16000) {
console.warn(`[wake] AudioContext sampleRate=${this.ctx.sampleRate}, ожидается 16000 — wake-word скорее всего не сработает`)
this.opts.onError?.(new Error(`AudioContext sampleRate=${this.ctx.sampleRate}`))
this.opts.onError?.(new Error(`AudioContext sampleRate=${this.ctx.sampleRate}, нужен 16000`))
}
// 3. Mic stream
@@ -144,7 +143,7 @@ export class WakeWordDetector {
this.worklet = new AudioWorkletNode(this.ctx, 'wake-capture')
let chunkCount = 0
this.worklet.port.onmessage = (e) => {
if (chunkCount === 0) console.log('[wake] first audio chunk received')
if (chunkCount === 0) vlog('[wake] first audio chunk received')
chunkCount++
this.onChunk(e.data as Float32Array)
}
@@ -152,7 +151,7 @@ export class WakeWordDetector {
// Worklet не подключается к destination → не звучит в колонках.
this.running = true
console.log('[wake] running')
vlog('[wake] running')
}
async stop(): Promise<void> {
@@ -180,6 +179,10 @@ export class WakeWordDetector {
private async onChunk(chunk: Float32Array) {
if (!this.running || !this.mel || !this.emb || !this.cls) return
if (this.cooldownChunks > 0) { this.cooldownChunks--; return }
// Drop-on-busy: если предыдущий chunk ещё считается, пропускаем новый.
// Иначе на медленных устройствах backlog растёт и реакция запаздывает.
if (this.processing) return
this.processing = true
const ort = await getOrt()
@@ -246,8 +249,10 @@ export class WakeWordDetector {
}
}
} catch (e) {
console.error('[wake-word] chunk error:', e)
verror('[wake-word] chunk error:', e)
this.opts.onError?.(e as Error)
} finally {
this.processing = false
}
}
}