File size: 7,206 Bytes
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, { useMemo, useRef, useState } from 'react';
import { Activity, Mic, MicOff, Volume2 } from 'lucide-react';
import { motion } from 'framer-motion';
import { supabase } from '../services/supabase';

type AppTab = 'dashboard' | 'marketplace' | 'debate' | 'new-project' | 'settings';

interface VoiceControlProps {
  onNavigate: (tab: AppTab) => void;
}

type SpeechRecognitionConstructor = new () => SpeechRecognition;

interface SpeechRecognitionEvent extends Event {
  results: SpeechRecognitionResultList;
}

interface SpeechRecognitionErrorEvent extends Event {
  error: string;
}

interface SpeechRecognition extends EventTarget {
  continuous: boolean;
  interimResults: boolean;
  lang: string;
  onresult: ((event: SpeechRecognitionEvent) => void) | null;
  onerror: ((event: SpeechRecognitionErrorEvent) => void) | null;
  onend: (() => void) | null;
  start: () => void;
  stop: () => void;
}

declare global {
  interface Window {
    SpeechRecognition?: SpeechRecognitionConstructor;
    webkitSpeechRecognition?: SpeechRecognitionConstructor;
  }
}

const VoiceControl: React.FC<VoiceControlProps> = ({ onNavigate }) => {
  const [listening, setListening] = useState(false);
  const [transcript, setTranscript] = useState('');
  const [status, setStatus] = useState('Voice assistant ready');
  const recognitionRef = useRef<SpeechRecognition | null>(null);

  const recognitionAvailable = useMemo(
    () => Boolean(window.SpeechRecognition || window.webkitSpeechRecognition),
    []
  );

  const speak = (message: string) => {
    setStatus(message);
    if (!window.speechSynthesis) return;

    window.speechSynthesis.cancel();
    const utterance = new SpeechSynthesisUtterance(message);
    utterance.rate = 0.95;
    utterance.pitch = 1;
    window.speechSynthesis.speak(utterance);
  };

  const getSystemSummary = async () => {
    const [{ data: projects }, { data: tasks }] = await Promise.all([
      supabase.from('projects').select('id,status'),
      supabase.from('tasks').select('id,status')
    ]);

    const taskCount = tasks?.length ?? 0;
    const projectCount = projects?.length ?? 0;
    const inProgress = tasks?.filter((task) => task.status === 'in_progress').length ?? 0;
    const awaiting = tasks?.filter((task) => task.status === 'awaiting_approval').length ?? 0;
    const failed = tasks?.filter((task) => task.status === 'failed').length ?? 0;

    return `${projectCount} projects, ${taskCount} tasks, ${inProgress} running, ${awaiting} awaiting approval, and ${failed} failed.`;
  };

  const handleCommand = async (command: string) => {
    const normalized = command.toLowerCase();
    setTranscript(command);

    if (normalized.includes('dashboard') || normalized.includes('panel')) {
      onNavigate('dashboard');
      speak('Opening dashboard.');
      return;
    }

    if (normalized.includes('marketplace') || normalized.includes('market')) {
      onNavigate('marketplace');
      speak('Opening agent marketplace.');
      return;
    }

    if (normalized.includes('debate')) {
      onNavigate('debate');
      speak('Opening multi agent debate.');
      return;
    }

    if (normalized.includes('settings') || normalized.includes('config')) {
      onNavigate('settings');
      speak('Opening settings.');
      return;
    }

    if (normalized.includes('new project') || normalized.includes('nuevo proyecto')) {
      onNavigate('new-project');
      speak('Opening new project.');
      return;
    }

    if (normalized.includes('status') || normalized.includes('estado') || normalized.includes('summary')) {
      try {
        const summary = await getSystemSummary();
        speak(summary);
      } catch {
        speak('I could not read the current project status.');
      }
      return;
    }

    speak('Command not recognized. Try dashboard, marketplace, debate, settings, or status.');
  };

  const startListening = () => {
    const Recognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    if (!Recognition) {
      speak('Voice recognition is not supported in this browser.');
      return;
    }

    const recognition = new Recognition();
    recognition.continuous = false;
    recognition.interimResults = false;
    recognition.lang = 'en-US';
    recognition.onresult = (event) => {
      const command = event.results[0]?.[0]?.transcript ?? '';
      if (command) void handleCommand(command);
    };
    recognition.onerror = (event) => {
      setListening(false);
      speak(`Voice recognition error: ${event.error}`);
    };
    recognition.onend = () => setListening(false);

    recognitionRef.current = recognition;
    setListening(true);
    setStatus('Listening...');
    recognition.start();
  };

  const stopListening = () => {
    recognitionRef.current?.stop();
    setListening(false);
    setStatus('Voice assistant paused');
  };

  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      className="glass-panel form-panel form-panel-wide"
    >
      <div className="panel-heading panel-heading-split">
        <div style={{ display: 'flex', alignItems: 'center', gap: 'var(--space-md)' }}>
          <Volume2 size={32} color="var(--accent)" />
          <div>
            <h2 style={{ fontSize: '1.5rem' }}>Voice Control</h2>
            <p style={{ color: 'var(--text-dim)', fontSize: '0.9rem' }}>Navigate and request project status by voice.</p>
          </div>
        </div>
        <div style={{ color: recognitionAvailable ? 'var(--success)' : 'var(--warning)', display: 'flex', alignItems: 'center', gap: 'var(--space-xs)', fontSize: '0.85rem' }}>
          <Activity size={16} />
          {recognitionAvailable ? 'Available' : 'Unsupported'}
        </div>
      </div>

      <div style={{ display: 'grid', gap: 'var(--space-md)' }}>
        <div style={{ padding: 'var(--space-md)', background: 'rgba(255,255,255,0.05)', borderRadius: 'var(--radius-md)', border: '1px solid var(--glass-border)' }}>
          <div style={{ color: 'var(--text-dim)', fontSize: '0.75rem', textTransform: 'uppercase', fontWeight: 700, marginBottom: 'var(--space-xs)' }}>Last command</div>
          <div style={{ minHeight: '1.5rem' }}>{transcript || 'No command captured yet.'}</div>
        </div>

        <div style={{ padding: 'var(--space-md)', background: 'rgba(255,255,255,0.05)', borderRadius: 'var(--radius-md)', border: '1px solid var(--glass-border)' }}>
          <div style={{ color: 'var(--text-dim)', fontSize: '0.75rem', textTransform: 'uppercase', fontWeight: 700, marginBottom: 'var(--space-xs)' }}>Assistant</div>
          <div>{status}</div>
        </div>

        <div className="button-row">
          <button className="btn btn-primary" onClick={listening ? stopListening : startListening}>
            {listening ? <MicOff size={18} /> : <Mic size={18} />}
            {listening ? 'Stop Listening' : 'Start Listening'}
          </button>
          <button className="btn btn-glass" onClick={() => void handleCommand('status')}>
            <Volume2 size={18} />
            Read Status
          </button>
        </div>
      </div>
    </motion.div>
  );
};

export default VoiceControl;