File size: 9,229 Bytes
f8ba6bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DungeonMaster AI - Voice Integration Models

Pydantic models for voice profiles, synthesis results, and status tracking.
"""

from __future__ import annotations

from datetime import datetime
from enum import Enum

from pydantic import BaseModel, Field


class VoiceType(str, Enum):
    """Voice profile types for different speakers."""

    DM = "dm"
    NPC_MALE_GRUFF = "npc_male_gruff"
    NPC_FEMALE_GENTLE = "npc_female_gentle"
    NPC_MYSTERIOUS = "npc_mysterious"
    MONSTER = "monster"


class VoiceCircuitState(str, Enum):
    """Circuit breaker states for voice service."""

    CLOSED = "closed"  # Normal operation, requests allowed
    OPEN = "open"  # Too many failures, requests rejected
    HALF_OPEN = "half_open"  # Testing if service recovered


class VoiceServiceState(str, Enum):
    """Overall voice service availability state."""

    AVAILABLE = "available"  # Fully functional
    DEGRADED = "degraded"  # Working but experiencing issues
    UNAVAILABLE = "unavailable"  # Not available (auth error, quota, etc.)


class VoiceModelType(str, Enum):
    """ElevenLabs model types for synthesis."""

    TURBO_V2 = "eleven_turbo_v2"
    TURBO_V2_5 = "eleven_turbo_v2_5"
    MULTILINGUAL_V2 = "eleven_multilingual_v2"


# =============================================================================
# Voice Configuration Models
# =============================================================================


class VoiceSynthesisSettings(BaseModel):
    """Settings for voice synthesis quality and style."""

    stability: float = Field(
        default=0.5,
        ge=0.0,
        le=1.0,
        description="Voice stability (0.0-1.0). Lower = more variation.",
    )
    similarity_boost: float = Field(
        default=0.75,
        ge=0.0,
        le=1.0,
        description="How closely to match the original voice (0.0-1.0).",
    )
    style: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description="Style exaggeration (0.0-1.0). Higher = more expressive.",
    )
    use_speaker_boost: bool = Field(
        default=True,
        description="Boost voice clarity and reduce background noise.",
    )


class VoiceProfile(BaseModel):
    """Complete voice profile definition."""

    name: str = Field(description="Profile name identifier")
    voice_id: str = Field(description="ElevenLabs voice ID")
    description: str = Field(default="", description="Human-readable description")
    voice_type: VoiceType = Field(description="Type of voice profile")
    settings: VoiceSynthesisSettings = Field(
        default_factory=VoiceSynthesisSettings,
        description="Synthesis settings for this voice",
    )


# =============================================================================
# Synthesis Request/Result Models
# =============================================================================


class SynthesisRequest(BaseModel):
    """Request for voice synthesis."""

    text: str = Field(description="Text to synthesize")
    voice_type: VoiceType = Field(
        default=VoiceType.DM,
        description="Voice profile type to use",
    )
    voice_profile_override: str | None = Field(
        default=None,
        description="Override voice profile name (ignores voice_type)",
    )
    stream: bool = Field(
        default=True,
        description="Stream audio chunks for real-time playback",
    )
    model: VoiceModelType = Field(
        default=VoiceModelType.TURBO_V2,
        description="ElevenLabs model to use",
    )
    output_format: str = Field(
        default="mp3_22050_32",
        description="Audio output format",
    )


class SynthesisResult(BaseModel):
    """Result of voice synthesis."""

    success: bool = Field(description="Whether synthesis succeeded")
    audio_bytes: bytes | None = Field(
        default=None,
        description="Synthesized audio data",
    )
    duration_ms: int | None = Field(
        default=None,
        description="Audio duration in milliseconds",
    )
    voice_type: VoiceType = Field(
        default=VoiceType.DM,
        description="Voice type used",
    )
    voice_id: str = Field(
        default="",
        description="ElevenLabs voice ID used",
    )
    text_length: int = Field(
        default=0,
        description="Length of synthesized text",
    )
    model_used: str = Field(
        default="",
        description="ElevenLabs model used",
    )
    from_cache: bool = Field(
        default=False,
        description="Whether result came from cache",
    )
    error_message: str | None = Field(
        default=None,
        description="Error message if synthesis failed",
    )


# =============================================================================
# Text Processing Models
# =============================================================================


class TextSegment(BaseModel):
    """A segment of text with assigned voice."""

    text: str = Field(description="Text content of this segment")
    voice_type: VoiceType = Field(
        default=VoiceType.DM,
        description="Voice type to use for this segment",
    )
    is_dialogue: bool = Field(
        default=False,
        description="Whether this is quoted dialogue",
    )
    speaker_name: str | None = Field(
        default=None,
        description="Name of the speaker if known",
    )
    pause_before_ms: int = Field(
        default=0,
        description="Pause duration before this segment in ms",
    )
    pause_after_ms: int = Field(
        default=0,
        description="Pause duration after this segment in ms",
    )


class ProcessedNarration(BaseModel):
    """Fully processed narration ready for synthesis."""

    segments: list[TextSegment] = Field(
        default_factory=list,
        description="List of text segments with voice assignments",
    )
    total_text: str = Field(
        default="",
        description="Complete processed text",
    )
    primary_voice: VoiceType = Field(
        default=VoiceType.DM,
        description="Primary voice type used",
    )
    has_dialogue: bool = Field(
        default=False,
        description="Whether narration contains dialogue",
    )
    estimated_duration_ms: int = Field(
        default=0,
        description="Estimated audio duration in ms",
    )


# =============================================================================
# Service Status Models
# =============================================================================


class VoiceServiceStatus(BaseModel):
    """Status information for voice service."""

    state: VoiceServiceState = Field(
        default=VoiceServiceState.UNAVAILABLE,
        description="Overall service state",
    )
    circuit_state: VoiceCircuitState = Field(
        default=VoiceCircuitState.CLOSED,
        description="Circuit breaker state",
    )
    is_available: bool = Field(
        default=False,
        description="Whether voice service is available for use",
    )
    is_initialized: bool = Field(
        default=False,
        description="Whether client has been initialized",
    )
    last_successful_call: datetime | None = Field(
        default=None,
        description="When the last successful synthesis occurred",
    )
    consecutive_failures: int = Field(
        default=0,
        description="Number of consecutive synthesis failures",
    )
    cache_size: int = Field(
        default=0,
        description="Number of cached audio entries",
    )
    cache_hit_rate: float = Field(
        default=0.0,
        description="Cache hit rate (0.0-1.0)",
    )
    error_message: str | None = Field(
        default=None,
        description="Last error message if any",
    )


# =============================================================================
# Narration Result Model (for VoiceNarratorAgent in Phase 3)
# =============================================================================


class NarrationResult(BaseModel):
    """Result from voice narration including audio and metadata."""

    success: bool = Field(description="Whether narration succeeded")
    audio: bytes | None = Field(
        default=None,
        description="Synthesized audio data",
    )
    format: str = Field(
        default="mp3",
        description="Audio format",
    )
    voice_used: str = Field(
        default="dm",
        description="Voice profile name used",
    )
    voice_type: VoiceType = Field(
        default=VoiceType.DM,
        description="Voice type used",
    )
    text_narrated: str = Field(
        default="",
        description="Original text that was narrated",
    )
    text_processed: str = Field(
        default="",
        description="Processed text after TTS preprocessing",
    )
    duration_ms: int = Field(
        default=0,
        description="Audio duration in milliseconds",
    )
    is_streaming: bool = Field(
        default=False,
        description="Whether this is a streaming result",
    )
    from_cache: bool = Field(
        default=False,
        description="Whether audio came from cache",
    )
    error_message: str | None = Field(
        default=None,
        description="Error message if narration failed",
    )