Spaces:
Sleeping
Sleeping
File size: 22,129 Bytes
fd52f31 |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import React, { useState, useEffect } from 'react';
import { FaPlay, FaSave, FaTimes, FaChevronDown, FaVolumeUp } from 'react-icons/fa';
import './AgentModal.css';
import { BsRobot, BsToggleOff, BsToggleOn } from "react-icons/bs";
import Toast from './Toast';
type Voice = {
id: string;
name: string;
description: string;
};
type FormData = {
name: string;
voice: Voice | null;
speed: number;
pitch: number;
volume: number;
outputFormat: 'mp3' | 'wav';
testInput: string;
personality: string;
showPersonality: boolean;
};
const VOICE_OPTIONS: Voice[] = [
{ id: 'alloy', name: 'Alloy', description: 'Versatile, well-rounded voice' },
{ id: 'ash', name: 'Ash', description: 'Direct and clear articulation' },
{ id: 'coral', name: 'Coral', description: 'Warm and inviting tone' },
{ id: 'echo', name: 'Echo', description: 'Balanced and measured delivery' },
{ id: 'fable', name: 'Fable', description: 'Expressive storytelling voice' },
{ id: 'onyx', name: 'Onyx', description: 'Authoritative and professional' },
{ id: 'nova', name: 'Nova', description: 'Energetic and engaging' },
{ id: 'sage', name: 'Sage', description: 'Calm and thoughtful delivery' },
{ id: 'shimmer', name: 'Shimmer', description: 'Bright and optimistic tone' }
];
interface AgentModalProps {
isOpen: boolean;
onClose: () => void;
editAgent?: {
id: string;
name: string;
voice_id: string;
speed: number;
pitch: number;
volume: number;
output_format: string;
personality: string;
} | null;
}
const AgentModal: React.FC<AgentModalProps> = ({ isOpen, onClose, editAgent }) => {
const [formData, setFormData] = useState<FormData>({
name: '',
voice: null,
speed: 1,
pitch: 1,
volume: 1,
outputFormat: 'mp3',
testInput: '',
personality: '',
showPersonality: false
});
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [toast, setToast] = useState<{ message: string; type: 'success' | 'error' | 'info' } | null>(null);
const [audioPlayer, setAudioPlayer] = useState<HTMLAudioElement | null>(null);
const [isTestingVoice, setIsTestingVoice] = useState(false);
// Initialize form data when editing an agent
useEffect(() => {
if (editAgent) {
// Find the matching voice from VOICE_OPTIONS
const matchingVoice = VOICE_OPTIONS.find(voice => voice.id === editAgent.voice_id) || VOICE_OPTIONS[0];
// Ensure output_format is either 'mp3' or 'wav'
const validOutputFormat = editAgent.output_format === 'wav' ? 'wav' : 'mp3';
setFormData({
name: editAgent.name,
voice: matchingVoice,
speed: editAgent.speed || 1,
pitch: editAgent.pitch || 1,
volume: editAgent.volume || 1,
outputFormat: validOutputFormat,
testInput: '',
personality: editAgent.personality || '',
showPersonality: !!editAgent.personality
});
} else {
// Reset form when not editing
setFormData({
name: '',
voice: VOICE_OPTIONS[0],
speed: 1,
pitch: 1,
volume: 1,
outputFormat: 'mp3',
testInput: '',
personality: '',
showPersonality: false
});
}
}, [editAgent]);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value
}));
};
const handleVoiceSelect = (voice: Voice) => {
setFormData(prev => ({
...prev,
voice
}));
setIsDropdownOpen(false);
};
const handleSliderChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
const numericValue = parseFloat(value);
if (!isNaN(numericValue)) {
setFormData(prev => ({
...prev,
[name]: numericValue
}));
}
};
const handleTestVoice = async () => {
if (!formData.testInput.trim()) {
setToast({ message: 'Please enter some text to test', type: 'error' });
return;
}
try {
setIsTestingVoice(true);
const token = localStorage.getItem('token');
if (!token) {
setToast({ message: 'Authentication token not found', type: 'error' });
setIsTestingVoice(false);
return;
}
// Stop any currently playing audio
if (audioPlayer) {
audioPlayer.pause();
audioPlayer.src = '';
setAudioPlayer(null);
}
// Prepare test data
const testData = {
text: formData.testInput.trim(),
voice_id: formData.voice?.id || 'alloy',
emotion: 'neutral', // Default emotion
speed: formData.speed
};
console.log('Sending test data:', JSON.stringify(testData, null, 2));
// Make API request
const response = await fetch('http://localhost:8000/agents/test-voice', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(testData)
});
console.log('Response status:', response.status);
const data = await response.json();
console.log('Response data:', JSON.stringify(data, null, 2));
if (!response.ok) {
throw new Error(data.detail || 'Failed to test voice');
}
if (!data.audio_url) {
throw new Error('No audio URL returned from server');
}
setToast({ message: 'Creating audio player...', type: 'info' });
// Create and configure new audio player
const newPlayer = new Audio();
// Set up event handlers before setting the source
newPlayer.onerror = (e) => {
console.error('Audio loading error:', newPlayer.error, e);
setToast({
message: `Failed to load audio file: ${newPlayer.error?.message || 'Unknown error'}`,
type: 'error'
});
setIsTestingVoice(false);
};
newPlayer.oncanplaythrough = () => {
console.log('Audio can play through, starting playback');
newPlayer.play()
.then(() => {
setToast({ message: 'Playing test audio', type: 'success' });
})
.catch((error) => {
console.error('Playback error:', error);
setToast({
message: `Failed to play audio: ${error.message}`,
type: 'error'
});
setIsTestingVoice(false);
});
};
newPlayer.onended = () => {
console.log('Audio playback ended');
setIsTestingVoice(false);
};
// Log the audio URL we're trying to play
console.log('Setting audio source to:', data.audio_url);
// Set the source and start loading
newPlayer.src = data.audio_url;
setAudioPlayer(newPlayer);
// Try to load the audio
try {
await newPlayer.load();
console.log('Audio loaded successfully');
} catch (loadError) {
console.error('Error loading audio:', loadError);
setToast({
message: `Error loading audio: ${loadError instanceof Error ? loadError.message : 'Unknown error'}`,
type: 'error'
});
setIsTestingVoice(false);
}
} catch (error) {
console.error('Error testing voice:', error);
setToast({
message: error instanceof Error ? error.message : 'Failed to test voice',
type: 'error'
});
setIsTestingVoice(false);
}
};
// Cleanup audio player on modal close
React.useEffect(() => {
return () => {
if (audioPlayer) {
audioPlayer.pause();
audioPlayer.src = '';
}
};
}, [audioPlayer]);
const toggleInputType = () => {
setFormData(prev => ({
...prev,
showPersonality: !prev.showPersonality
}));
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!formData.voice) {
setToast({ message: 'Please select a voice', type: 'error' });
return;
}
try {
const token = localStorage.getItem('token');
if (!token) {
setToast({ message: 'Authentication token not found', type: 'error' });
return;
}
const requestData = {
name: formData.name,
voice_id: formData.voice.id,
voice_name: formData.voice.name,
voice_description: formData.voice.description,
speed: formData.speed,
pitch: formData.pitch,
volume: formData.volume,
output_format: formData.outputFormat, // Use snake_case to match backend
personality: formData.showPersonality ? formData.personality : null
};
console.log('Request data:', JSON.stringify(requestData, null, 2));
const url = editAgent
? `http://localhost:8000/agents/${editAgent.id}`
: 'http://localhost:8000/agents/create';
const response = await fetch(url, {
method: editAgent ? 'PUT' : 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(requestData)
});
const responseData = await response.json();
console.log('Response data:', JSON.stringify(responseData, null, 2));
if (!response.ok) {
throw new Error(JSON.stringify(responseData, null, 2));
}
setToast({ message: `Agent ${editAgent ? 'updated' : 'created'} successfully`, type: 'success' });
onClose();
} catch (error) {
console.error('Error saving agent:', error);
if (error instanceof Error) {
console.error('Error details:', error.message);
try {
const errorDetails = JSON.parse(error.message);
setToast({
message: errorDetails.detail?.[0]?.msg || 'Failed to save agent',
type: 'error'
});
} catch {
setToast({ message: error.message, type: 'error' });
}
} else {
setToast({ message: 'An unexpected error occurred', type: 'error' });
}
}
};
if (!isOpen) return null;
return (
<div className="agent-modal-overlay" style={{ display: isOpen ? 'flex' : 'none' }}>
<div className="agent-modal-content">
<div className="agent-modal-header">
<h2>{editAgent ? 'Edit Agent' : 'Create New Agent'}</h2>
<button className="close-button" onClick={onClose}>×</button>
</div>
<form className="agent-form" onSubmit={handleSubmit}>
<div className="form-group">
<label htmlFor="name">Agent Name</label>
<input
type="text"
id="name"
name="name"
value={formData.name}
onChange={handleInputChange}
placeholder="Enter agent name"
required
/>
</div>
<div className="form-group">
<label>Voice</label>
<div className="custom-dropdown">
<div
className="dropdown-header"
onClick={() => setIsDropdownOpen(!isDropdownOpen)}
>
<div className="selected-voice">
<FaVolumeUp />
<div className="voice-info">
<span>{formData.voice?.name}</span>
<small>{formData.voice?.description}</small>
</div>
</div>
<FaChevronDown style={{
transform: isDropdownOpen ? 'rotate(180deg)' : 'none',
transition: 'transform 0.3s ease'
}} />
</div>
{isDropdownOpen && (
<div className="dropdown-options">
{VOICE_OPTIONS.map(voice => (
<div
key={voice.id}
className="dropdown-option"
onClick={() => handleVoiceSelect(voice)}
>
<FaVolumeUp />
<div className="voice-info">
<span>{voice.name}</span>
<small>{voice.description}</small>
</div>
</div>
))}
</div>
)}
</div>
</div>
<div className="form-group">
<label htmlFor="speed">Speed</label>
<div className="slider-container">
<input
type="range"
id="speed"
name="speed"
min="0.5"
max="2"
step="0.1"
value={formData.speed}
onChange={handleSliderChange}
/>
<span className="slider-value">{formData.speed}x</span>
</div>
</div>
<div className="form-group">
<label htmlFor="pitch">Pitch</label>
<div className="slider-container">
<input
type="range"
id="pitch"
name="pitch"
min="0.5"
max="2"
step="0.1"
value={formData.pitch}
onChange={handleSliderChange}
/>
<span className="slider-value">{formData.pitch}x</span>
</div>
</div>
<div className="form-group">
<label htmlFor="volume">Volume</label>
<div className="slider-container">
<input
type="range"
id="volume"
name="volume"
min="0"
max="2"
step="0.1"
value={formData.volume}
onChange={handleSliderChange}
/>
<span className="slider-value">{formData.volume}x</span>
</div>
</div>
<div className="form-group">
<label>Output Format</label>
<div className="radio-group">
<label className="radio-label">
<input
type="radio"
name="outputFormat"
value="mp3"
checked={formData.outputFormat === 'mp3'}
onChange={handleInputChange}
/>
<span>MP3</span>
</label>
<label className="radio-label">
<input
type="radio"
name="outputFormat"
value="wav"
checked={formData.outputFormat === 'wav'}
onChange={handleInputChange}
/>
<span>WAV</span>
</label>
</div>
</div>
<div className="form-group toggle-group">
<label>Input Type</label>
<div className="toggle-container" onClick={toggleInputType}>
<span className={!formData.showPersonality ? 'active' : ''}>Test Input</span>
{formData.showPersonality ?
<BsToggleOn className="toggle-icon" /> :
<BsToggleOff className="toggle-icon" />
}
<span className={formData.showPersonality ? 'active' : ''}>Agent Personality</span>
</div>
</div>
{formData.showPersonality ? (
<div className="form-group">
<label htmlFor="personality">Agent Personality</label>
<textarea
id="personality"
name="personality"
value={formData.personality}
onChange={handleInputChange}
placeholder="Describe the personality and characteristics of this agent..."
rows={4}
/>
<small className="help-text">This personality description will be used to guide the agent's responses in workflows.</small>
</div>
) : (
<div className="form-group">
<label htmlFor="testInput">Test Input</label>
<textarea
id="testInput"
name="testInput"
value={formData.testInput}
onChange={handleInputChange}
placeholder="Enter text to test the voice"
rows={4}
/>
</div>
)}
<div className="modal-actions">
{!formData.showPersonality && (
<button
type="button"
className="test-voice-btn"
onClick={handleTestVoice}
disabled={!formData.testInput || isTestingVoice}
>
<FaPlay /> {isTestingVoice ? 'Testing...' : 'Test Voice'}
</button>
)}
<div className="right-actions">
<button type="button" className="cancel-btn" onClick={onClose}>
Cancel
</button>
<button
type="submit"
className="save-btn"
disabled={isLoading}
>
<FaSave /> {isLoading ? 'Saving...' : 'Save Agent'}
</button>
</div>
</div>
</form>
{toast && (
<Toast
message={toast.message}
type={toast.type}
onClose={() => setToast(null)}
/>
)}
</div>
</div>
);
};
export default AgentModal; |