File size: 28,215 Bytes
021c59c
 
 
 
 
 
 
 
 
 
 
 
 
cc0745b
021c59c
 
37974e4
b9183e9
021c59c
cc0745b
021c59c
 
 
 
 
 
37974e4
021c59c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a13f875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ee0711
a13f875
7ee0711
a13f875
7ee0711
a13f875
7ee0711
a13f875
7ee0711
a13f875
7ee0711
 
a13f875
7ee0711
 
 
a13f875
 
 
021c59c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cb7e78
 
 
 
 
 
 
 
 
021c59c
 
 
 
 
 
 
0cb7e78
021c59c
 
 
 
 
b9183e9
 
 
 
 
 
 
7ee0711
 
 
 
 
 
 
b9183e9
 
 
 
 
 
7ee0711
b9183e9
 
 
 
 
7ee0711
 
 
 
 
 
 
 
 
 
b9183e9
 
 
 
 
 
eddd90b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9183e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
021c59c
 
 
 
 
 
 
 
 
 
 
7ee0711
 
 
 
 
 
 
 
 
 
021c59c
 
 
7ee0711
 
021c59c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a13f875
0cb7e78
 
 
 
 
 
 
 
7ee0711
 
 
 
 
0cb7e78
 
 
 
7ee0711
0cb7e78
eddd90b
021c59c
 
 
a13f875
 
021c59c
 
 
 
 
eddd90b
 
021c59c
37974e4
 
 
 
 
 
 
 
 
 
 
 
 
a13f875
 
37974e4
 
 
 
 
 
a13f875
37974e4
a13f875
 
 
 
 
 
 
 
37974e4
 
1743612
 
37974e4
 
 
2e8c86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1743612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eddd90b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37974e4
 
 
 
eddd90b
1743612
 
 
 
 
eddd90b
 
 
37974e4
7ee0711
 
 
 
37974e4
 
 
 
b852032
 
 
a13f875
 
 
7ee0711
a13f875
 
2e8c86f
 
 
a13f875
 
37974e4
 
 
 
eddd90b
 
 
37974e4
 
eddd90b
37974e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
"""Server-side proxy to the Modal NIGHTWAVE engine.

Python 3.9 compatible. The browser NEVER calls Modal directly -- it calls the
Space's same-origin /api/* routes (see server.py), which call into these
functions, which in turn call Modal with the proxy-auth headers held in Space
Secrets.

Mock mode (NIGHTWAVE_MOCK=="1" or MODAL_URL unset) returns canned,
stage-appropriate data plus a short silent WAV, so the whole UI runs with no
Modal backend at all -- ideal for local dev and CI.
"""

import base64
import datetime
import io
import os
import random
import re
import struct
import threading
import wave
from typing import Any, Dict, List, Optional

import httpx

import arc
import content

# Reuse one client across calls (connection pooling). 60s timeout per the
# contract; Modal cold starts can be slow.
_TIMEOUT = 60.0
_client: Optional[httpx.Client] = None


def _get_client() -> httpx.Client:
    global _client
    if _client is None:
        _client = httpx.Client(timeout=_TIMEOUT)
    return _client


# ---------------------------------------------------------------------------
# Environment / auth
# ---------------------------------------------------------------------------
def _modal_url() -> str:
    return (os.environ.get("MODAL_URL") or "").rstrip("/")


def _modal_headers() -> Dict[str, str]:
    """Modal proxy-auth headers from Space Secrets."""
    return {
        "Modal-Key": os.environ.get("MODAL_KEY", ""),
        "Modal-Secret": os.environ.get("MODAL_SECRET", ""),
        "Content-Type": "application/json",
    }


def is_mock() -> bool:
    """True when we should serve canned data instead of hitting Modal."""
    if os.environ.get("NIGHTWAVE_MOCK") == "1":
        return True
    return not os.environ.get("MODAL_URL")


# ---------------------------------------------------------------------------
# Mock helpers
# ---------------------------------------------------------------------------
def _silent_wav_b64(seconds: float = 0.4, rate: int = 24000) -> str:
    """Build a short silent 24kHz mono PCM16 WAV in pure Python.

    Returns base64 of the WAV bytes with NO data: prefix (per the contract).
    """
    n_frames = int(seconds * rate)
    buf = io.BytesIO()
    wf = wave.open(buf, "wb")
    try:
        wf.setnchannels(1)
        wf.setsampwidth(2)  # 16-bit PCM
        wf.setframerate(rate)
        # All-zero (silent) samples.
        silence = struct.pack("<%dh" % n_frames, *([0] * n_frames))
        wf.writeframes(silence)
    finally:
        wf.close()
    return base64.b64encode(buf.getvalue()).decode("ascii")


_MOCK_BRAIN_LINE = (
    "Stay right where you are, friend -- the dial's warm and the night is long. "
    "This one's for everyone still awake out there."
)


def _mock_brain() -> Dict[str, Any]:
    return {"text": _MOCK_BRAIN_LINE, "mood": "warm", "arc_cue": "none"}


def _mock_asr() -> Dict[str, Any]:
    return {"text": "is anybody really out there tonight?"}


def _mock_speak() -> Dict[str, Any]:
    return {
        "audio_b64": _silent_wav_b64(),
        "words": [],
        "wtimes": [],
        "wdurations": [],
    }


# ---------------------------------------------------------------------------
# Real local weather/location resolution (server-side; never logs raw coords).
# Open-Meteo (current weather + sunrise + local time) and BigDataCloud (city).
# Both are free, key-less, CORS-irrelevant (server-side). All failures degrade
# to the fictional-town weather, so the show never breaks.
# ---------------------------------------------------------------------------
_WMO = {
    0: "clear skies", 1: "mostly clear", 2: "partly cloudy", 3: "overcast",
    45: "fog", 48: "freezing fog", 51: "light drizzle", 53: "drizzle",
    61: "light rain", 63: "steady rain", 65: "heavy rain", 71: "light snow",
    73: "snow", 80: "passing showers", 81: "showers", 95: "a thunderstorm",
}


def _wmo_phrase(code) -> str:
    try:
        return _WMO.get(int(code), "quiet skies")
    except (TypeError, ValueError):
        return "quiet skies"


def _fmt_clock(iso: Optional[str]) -> Optional[str]:
    """'2026-06-15T02:14' -> '2:14 AM'. None/garbage -> None."""
    if not iso:
        return None
    try:
        t = datetime.datetime.fromisoformat(iso)
        h = t.hour % 12 or 12
        ap = "AM" if t.hour < 12 else "PM"
        return "%d:%02d %s" % (h, t.minute, ap)
    except (ValueError, TypeError):
        return None


def _reverse_city(lat: float, lon: float) -> Optional[str]:
    try:
        r = _get_client().get(
            "https://api.bigdatacloud.net/data/reverse-geocode-client",
            params={"latitude": lat, "longitude": lon, "localityLanguage": "en"},
        )
        r.raise_for_status()
        j = r.json()
        return j.get("city") or j.get("locality") or j.get("principalSubdivision") or None
    except Exception:
        return None


def resolve_locale(lat: float, lon: float) -> Dict[str, Any]:
    """Real weather/time/city for a lat/lon -> facts dict, or {'resolved': False}.

    NEVER logs raw coordinates (privacy). In mock mode returns canned facts so the
    whole flow runs offline.
    """
    if is_mock():
        return {"resolved": True, "city": "Old Ferris", "temp_f": 59,
                "sky": "clear skies", "local_time": "2:14 AM", "sunrise": "6:48 AM"}
    try:
        wx = _get_client().get(
            "https://api.open-meteo.com/v1/forecast",
            params={
                "latitude": lat, "longitude": lon,
                "current": "temperature_2m,weather_code",
                "daily": "sunrise,sunset", "timezone": "auto",
                "temperature_unit": "fahrenheit", "forecast_days": 1,
            },
        )
        wx.raise_for_status()
        w = wx.json()
        cur = w.get("current", {}) or {}
        daily = w.get("daily", {}) or {}
        sunrise_list = daily.get("sunrise") or [None]
        return {
            "resolved": True,
            "temp_f": cur.get("temperature_2m"),
            "sky": _wmo_phrase(cur.get("weather_code")),
            "local_time": _fmt_clock(cur.get("time")),
            "sunrise": _fmt_clock(sunrise_list[0]),
            "city": _reverse_city(lat, lon),
        }
    except Exception:
        return {"resolved": False}


def _local_weather_user(ctx: Optional[Dict[str, Any]] = None) -> str:
    """Build the USER-turn fact string for a local_weather segment."""
    ctx = ctx or {}
    parts: List[str] = []
    if ctx.get("city"):
        parts.append("out %s way" % ctx["city"])
    if ctx.get("temp_f") is not None:
        parts.append("it is about %d degrees" % int(ctx["temp_f"]))
    if ctx.get("sky"):
        parts.append("the sky is %s" % ctx["sky"])
    if ctx.get("local_time"):
        parts.append("the clock just read %s" % ctx["local_time"])
    if ctx.get("sunrise"):
        parts.append("the sun comes up around %s" % ctx["sunrise"])
    facts = ", and ".join(parts) if parts else "it is a still, clear night out their way"
    return (
        "Tell the listener about their own sky tonight, gently. Here is what is true "
        "right now: " + facts + ". Weave it into one or two warm spoken sentences. Do "
        'not read it as a forecast, do not list it, and never write anything as "label: value".'
    )


# ---------------------------------------------------------------------------
# Raw Modal calls
# ---------------------------------------------------------------------------
def call_brain(system: str, messages: List[Dict[str, str]]) -> Dict[str, Any]:
    """POST /brain -> {"text", "mood", "arc_cue"}."""
    if is_mock():
        return _mock_brain()
    resp = _get_client().post(
        _modal_url() + "/brain",
        headers=_modal_headers(),
        json={"system": system, "messages": messages},
    )
    resp.raise_for_status()
    return resp.json()


def call_asr(audio_b64: str) -> Dict[str, Any]:
    """POST /asr -> {"text"}."""
    if is_mock():
        return _mock_asr()
    resp = _get_client().post(
        _modal_url() + "/asr",
        headers=_modal_headers(),
        json={"audio_b64": audio_b64},
    )
    resp.raise_for_status()
    return resp.json()


# All-caps "NIGHTWAVE" makes the TTS spell it out letter-by-letter; normalize to
# title case for SPEECH only (captions/UI keep the branded all-caps original).
_SPEAK_FIX = re.compile(r"\bNIGHT\s*WAVE\b", re.IGNORECASE)


def _speakable(text: Optional[str]) -> str:
    return _SPEAK_FIX.sub("Nightwave", text or "")


def call_speak(text: str, voice: str = arc.VOICE) -> Dict[str, Any]:
    """POST /speak -> {"audio_b64", "words", "wtimes", "wdurations"}."""
    if is_mock():
        return _mock_speak()
    resp = _get_client().post(
        _modal_url() + "/speak",
        headers=_modal_headers(),
        json={"text": _speakable(text), "voice": voice},
    )
    resp.raise_for_status()
    return resp.json()


# ---------------------------------------------------------------------------
# DJ-text sanitizer: a small 1B model sometimes leaks the JSON labels into the
# spoken text ("...mood: warm") or emits placeholders ("<sentence 1>"). We never
# want the DJ to SAY those, so we scrub the text before TTS and, if what's left
# is degenerate, fall back to a clean stage-appropriate canned line.
# ---------------------------------------------------------------------------
_LABEL_RE = re.compile(
    r"[\(\[]?\s*\b(?:mood|arc[_ ]?cue|title|artist|song|track|vibe|genre|by|city|town|"
    r"temp(?:erature)?|sky|weather|conditions?|forecast|time|local[_ ]?time|sunrise|sunset|"
    r"handle|requested[_ ]?by|recommended[_ ]?by|dedication|name)\b\s*[:=]\s*[^\n,;]*",
    re.IGNORECASE,
)
_PLACEHOLDER_RE = re.compile(
    r"<[^>]{0,40}>|\[[^\]]{0,40}\]|\{[^}]{0,40}\}|\bsentence\s*\d+\b", re.IGNORECASE
)


def _clean_dj_text(text: Optional[str]) -> Optional[str]:
    """Strip label leakage / markdown; return cleaned spoken text or None if junk."""
    t = text or ""
    t = _LABEL_RE.sub("", t)          # drop "mood: warm" / "title: X" / "artist: Y" runs
    t = re.sub(r"[*#`]+", "", t)       # drop markdown emphasis/headings
    t = t.strip()
    if len(t) >= 2 and t[0] in "\"'" and t[-1] in "\"'":
        t = t[1:-1].strip()            # unwrap a fully-quoted line
    t = re.sub(r"\s+", " ", t).strip()  # it's spoken: collapse whitespace
    # Degenerate? empty / placeholder / bracketed / structured key:value / echo.
    if not t or _PLACEHOLDER_RE.search(t):
        return None
    if any(c in t for c in "<>[]{}|"):
        return None
    if re.search(r"\b(?:title|artist|mood|arc_cue|temp|sky|sunrise|vibe)\b\s*[:=]", t, re.IGNORECASE):
        return None
    if t.count(":") >= 2:
        return None
    if re.search(r"\b(one or two sentences|do not repeat|in your own words|spoken sentences|stage direction)\b", t, re.IGNORECASE):
        return None
    if len(re.sub(r"[^a-zA-Z]", "", t)) < 6:
        return None
    return t


# ---------------------------------------------------------------------------
# Caller session memory (SP1): deterministic extraction from the ASR text.
# No model call, no Modal engine change -- mood is reused from the answer brain
# call; topic is the caller's gist; name/place are best-effort (often None).
# ---------------------------------------------------------------------------
_NAME_RE = re.compile(
    r"\b(?i:i'?m|i am|this is|it'?s|my name is|name'?s)\s+([A-Z][a-z]{1,18})\b"
)
_PLACE_RE = re.compile(r"\b(?i:in|from|out in|over in)\s+([A-Z][a-z]+(?:\s[A-Z][a-z]+)?)\b")


def _caller_gist(caller_text: Optional[str]) -> Optional[str]:
    t = re.sub(r"\s+", " ", (caller_text or "")).strip()
    if len(t.split()) < 3:
        return None
    return t[:140].strip()


def _extract_name(caller_text: Optional[str]) -> Optional[str]:
    m = _NAME_RE.search(caller_text or "")
    return m.group(1).strip()[:40] if m else None


def _extract_place(caller_text: Optional[str]) -> Optional[str]:
    m = _PLACE_RE.search(caller_text or "")
    return m.group(1).strip()[:40] if m else None


def _build_memory_patch(caller_text: Optional[str], mood: str) -> Optional[Dict[str, Any]]:
    topic = _caller_gist(caller_text)
    if not topic:
        return None
    return {
        "caller_name": _extract_name(caller_text),
        "place": _extract_place(caller_text),
        "topic": topic,
        "mood": mood if mood in arc.MOODS else "warm",
    }


# ---------------------------------------------------------------------------
# Pre-cached "stall" lines: instant filler audio played the moment a caller stops
# talking, while the real reply generates underneath -- so the call never has
# dead air. Synthesized once (per container) via Modal /speak, then cached.
# ---------------------------------------------------------------------------
_STALL_LINES = [
    "Mm. Good question, friend. Let me sit with that a second.",
    "That's a thing to ask at this hour. Hold on now, let me find the words.",
    "Hm. Give me a breath, caller. The night makes me slow.",
    "Let me think on that one. Stay right there with me.",
]
_stall_cache: Optional[List[str]] = None
_stall_lock = threading.Lock()


def get_stalls() -> List[str]:
    """Return a list of base64 WAV stall clips (cached after first synthesis)."""
    global _stall_cache
    if _stall_cache is not None:
        return _stall_cache
    with _stall_lock:
        if _stall_cache is not None:
            return _stall_cache
        clips: List[str] = []
        for line in _STALL_LINES:
            try:
                sp = call_speak(line)
                if sp.get("audio_b64"):
                    clips.append(sp["audio_b64"])
            except Exception:
                pass
        _stall_cache = clips
        return clips


# ---------------------------------------------------------------------------
# High-level turns (what server.py routes call)
# ---------------------------------------------------------------------------
def broadcast_turn(
    stage: str, meter: int, topic: Optional[str] = None
) -> Dict[str, Any]:
    """Produce one solo on-air broadcast turn.

    -> /api/broadcast response shape:
       {text, mood, arc_cue, audio_b64, words, wtimes, wdurations}
    """
    system = arc.build_host_prompt("on_air", {"topic": topic})
    user = (topic.strip() if (topic and topic.strip())
            else "Go on the air now with a short stretch of warm late-night patter.")
    try:
        brain = call_brain(system, [{"role": "user", "content": user}])
        text = _clean_dj_text(brain.get("text", "")) or _templated_text("thought")
        mood = brain.get("mood", "warm")
        arc_cue = brain.get("arc_cue", "none")
    except Exception:
        text, mood, arc_cue = _templated_text("thought"), "warm", "none"
    speak = call_speak(text)
    return {
        "text": text,
        "mood": mood,
        "arc_cue": arc_cue,
        "audio_b64": speak.get("audio_b64", ""),
        "words": speak.get("words", []),
        "wtimes": speak.get("wtimes", []),
        "wdurations": speak.get("wdurations", []),
    }


def call_turn(stage: str, meter: int, audio_b64: str) -> Dict[str, Any]:
    """Handle one live call-in turn.

    -> /api/call response shape:
       {caller_text, text, mood, arc_cue, audio_b64, words, wtimes,
        wdurations, meter_delta}
    """
    asr = call_asr(audio_b64)
    caller_text = asr.get("text", "")
    meter_delta = arc.detect_triggers(caller_text)

    # Straight station: the host always answers the caller in his own voice. The
    # caller's words are delivered as an ANSWER-DIRECTIVE in the user turn (NOT
    # embedded verbatim) because a 1B model under the JSON grammar tends to echo a
    # question that appears verbatim in its context.
    system = arc.build_host_prompt("caller", {"caller_text": caller_text})
    if caller_text and caller_text.strip():
        user_msg = (
            "The caller is asking about this: " + caller_text.strip()
            + "\nRespond by speaking ONLY your warm reply in your own words -- a fresh "
            "sentence that answers them. Begin your reply with a word other than the "
            'caller\'s. Do not quote or restate their words, do not write "X: Y", and do '
            "not repeat this instruction."
        )
    else:
        user_msg = "(the caller's line is too crackly to make out)"
    brain = call_brain(system, [{"role": "user", "content": user_msg}])
    text = _clean_dj_text(brain.get("text", "")) or _pick(content.CALLER_FALLBACKS)
    speak = call_speak(text)
    memory_patch = _build_memory_patch(caller_text, brain.get("mood", "warm"))
    return {
        "caller_text": caller_text,
        "text": text,
        "mood": brain.get("mood", arc.stage_default_mood(stage)),
        "arc_cue": brain.get("arc_cue", "none"),
        "audio_b64": speak.get("audio_b64", ""),
        "words": speak.get("words", []),
        "wtimes": speak.get("wtimes", []),
        "wdurations": speak.get("wdurations", []),
        "meter_delta": meter_delta,
        "memory_patch": memory_patch,
        "queue_dedication": bool(memory_patch),
    }


# ---------------------------------------------------------------------------
# Autonomous show segments (the straight station).
# LLM kinds (thought, song_intro) -> host persona + brain + sanitizer.
# Templated kinds (station_id, weather, dedication) -> content banks. All spoken.
# ---------------------------------------------------------------------------
def _pick(seq):
    return random.choice(seq) if seq else ""


def _templated_text(kind: str, ctx: Optional[Dict[str, Any]] = None) -> str:
    ctx = ctx or {}
    if kind in ("weather", "local_weather"):
        # Fictional-town weather is also the graceful fallback for local_weather.
        return _pick(content.WEATHER)
    if kind == "dedication":
        d = _pick(content.DEDICATIONS) or {"name": "someone out there",
                                           "message": "you're not as alone as it feels"}
        return "This next one's for %s -- %s." % (d["name"], d["message"])
    if kind == "song_intro":
        base = "Coming up next, '%s' by %s." % (
            ctx.get("title", "this next one"), ctx.get("artist", "a friend of the show"))
        rb = ctx.get("recommended_by")
        if rb:
            base = base[:-1] + " -- sent in by %s." % rb
        return base
    if kind == "rejoin":
        return _pick(content.REJOINS)
    if kind == "caller_intro":
        return _pick(content.CALLER_INTROS)
    if kind == "thought":
        return _pick(content.THOUGHTS)
    if kind == "fragment":
        return _pick(content.FRAGMENTS)
    return _pick(content.STATION_IDS)  # station_id (default)


# ---------------------------------------------------------------------------
# Generative song cards (SP3): model writes title+artist; server picks the vibe
# and assigns ALWAYS-VALID musical params so a card can never break the engine.
# ---------------------------------------------------------------------------
_VIBE_MUSIC = {
    "melancholy": ("minor", "rhodes", 70, ("A", "E", "D")),
    "lonely": ("minor", "music_box", 64, ("A", "E", "Bb")),
    "tender": ("major", "music_box", 68, ("C", "F", "Bb")),
    "warm": ("major", "rhodes", 78, ("C", "D", "G")),
    "cozy": ("major", "rhodes", 80, ("F", "C", "Bb")),
    "hopeful": ("lydian", "soft_saw", 80, ("D", "G", "C")),
    "nostalgic": ("major", "triangle_pluck", 74, ("C", "Bb", "G")),
    "wistful": ("dorian", "sine_pad", 72, ("C", "D", "F")),
    "bittersweet": ("minor", "rhodes", 70, ("Bb", "A", "E")),
    "jazzy": ("dorian", "rhodes", 84, ("D", "F", "G")),
    "dreamy": ("lydian", "sine_pad", 64, ("F", "D", "Eb")),
    "pensive": ("dorian", "sine_pad", 76, ("C", "E", "G")),
    "hypnotic": ("pentatonic_minor", "soft_saw", 70, ("G", "A", "D")),
    "mysterious": ("lydian", "sine_pad", 68, ("Eb", "A", "F")),
    "eerie": ("pentatonic_minor", "sine_pad", 60, ("A", "Eb", "G")),
    "ambient": ("lydian", "sine_pad", 64, ("F", "D", "C")),
    "romantic": ("minor", "rhodes", 66, ("Ab", "D", "A")),
    "gentle": ("major", "triangle_pluck", 75, ("C", "G", "D")),
    "breezy": ("major", "triangle_pluck", 88, ("G", "D", "C")),
}
_DEFAULT_MUSIC = ("major", "rhodes", 74, ("C", "D", "G"))
_MOOD_TO_VIBE = {
    "warm": "warm", "nostalgic": "nostalgic", "tender": "tender",
    "searching": "pensive", "hollow": "lonely", "uneasy": "eerie",
}


def _parse_title_artist(text):
    t = re.sub(r"\s+", " ", text or "").strip().strip("\"'")
    t = re.sub(r"^(?:title|song)\s*[:=]\s*", "", t, flags=re.IGNORECASE)
    parts = re.split(r"\s+by\s+", t, maxsplit=1, flags=re.IGNORECASE)
    if len(parts) != 2:
        return None, None
    title = parts[0].strip().strip("\"'")[:42]
    # Drop trailing model chatter ("... by The Velvet Sundays. Hope you enjoy!")
    # at the first real sentence break -- a period after >=4 letters then space --
    # so abbreviations like "St. Vincent" survive.
    artist = re.split(r"(?<=[A-Za-z]{4})\.\s", parts[1].strip().strip("\"'"), maxsplit=1)[0].strip()[:42]
    if 2 <= len(title) <= 42 and 2 <= len(artist) <= 42 and not any(c in t for c in "<>{}[]") and ":" not in title:
        return title, artist
    return None, None


def _procedural_title():
    return "%s %s" % (random.choice(content.CARD_TITLE_A), random.choice(content.CARD_TITLE_B))


def _procedural_artist():
    return "%s %s" % (random.choice(content.CARD_ARTIST_A), random.choice(content.CARD_ARTIST_B))


def _generate_title_artist(ctx, vibe):
    try:
        flavor = ""
        if ctx.get("topic"):
            flavor = " inspired by someone who said: %s" % str(ctx["topic"])[:80]
        elif ctx.get("city"):
            flavor = " for a night in %s" % ctx["city"]
        user = ("Invent a fictional late-night record with a %s feeling%s. Reply with ONLY "
                "the song title and the artist in the form: <title> by <artist>." % (vibe, flavor))
        brain = call_brain(arc.build_song_card_prompt(), [{"role": "user", "content": user}])
        title, artist = _parse_title_artist(brain.get("text", ""))
        if title and artist:
            return title, artist
    except Exception:
        pass
    return _procedural_title(), _procedural_artist()


def make_song_card(ctx=None):
    """Invent ONE fictional record. Musical params are always valid engine enums."""
    ctx = ctx or {}
    vibe = _MOOD_TO_VIBE.get(ctx.get("mood")) or random.choice(list(_VIBE_MUSIC.keys()))
    scale, timbre, tempo, keys = _VIBE_MUSIC.get(vibe, _DEFAULT_MUSIC)
    title, artist = _generate_title_artist(ctx, vibe)
    return {
        "title": title, "artist": artist, "vibe": vibe,
        "key": random.choice(keys), "scale": scale, "tempo": tempo, "timbre": timbre,
        "recommended_by": None, "generated": True,
    }


_FRAGMENT_FLAVORS = ["dedication", "numbers_weather", "station_id", "dream", "song_title"]
_FRAGMENT_HINT = {
    "dedication": "a half-heard dedication to someone you will never name",
    "numbers_weather": "a numbers-station weather reading for a place that may not exist",
    "station_id": "a fictional station identifier from a frequency that should be empty",
    "dream": "a single stray line, like a dream someone else is having out loud",
    "song_title": "a garbled song title and artist that dissolve into static",
}


def _fragment_user(ctx: Optional[Dict[str, Any]] = None) -> str:
    ctx = ctx or {}
    flavor = random.choice(_FRAGMENT_FLAVORS)
    hint = _FRAGMENT_HINT[flavor]
    extra = ""
    if flavor == "numbers_weather" and ctx.get("city"):
        extra = " somewhere near %s" % ctx["city"]
    if flavor == "dedication" and ctx.get("topic"):
        extra = " about %s" % ctx["topic"]
    return ("Speak ONE short fragment bleeding in from another station on the dial: " + hint
            + extra + ". At most a dozen words, eerie and distant, half-lost in static. "
            "Plain spoken words only, no labels.")


def _dedication_user(ctx: Optional[Dict[str, Any]] = None) -> str:
    ctx = ctx or {}
    msg = ("Read a short, warm dedication on the air for a caller. Here's what they "
           "shared: %s." % ctx.get("topic", "they're up late tonight"))
    if ctx.get("caller_name"):
        msg += " Their name is %s." % ctx["caller_name"]
    if ctx.get("place"):
        msg += " They're in %s." % ctx["place"]
    msg += (" Send the next record out to them in one or two warm spoken sentences. "
            "Do not quote them, do not label anything, and do not repeat this instruction.")
    return msg


def _dedication_fallback(ctx: Optional[Dict[str, Any]] = None) -> str:
    ctx = ctx or {}
    name = ctx.get("caller_name")
    if name:
        return "This next one's for %s -- you're not as alone as it feels tonight." % name
    return _templated_text("dedication")


def segment_turn(kind: str, ctx: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """Produce one show segment with audio."""
    ctx = ctx or {}
    mood, arc_cue = "warm", "none"
    memory_dedication = (kind == "dedication" and bool(ctx.get("topic")))
    if kind in ("thought", "song_intro", "local_weather", "fragment") or memory_dedication:
        system = arc.build_fragment_prompt() if kind == "fragment" else arc.build_host_prompt(kind, ctx)
        if kind == "fragment":
            user = _fragment_user(ctx)
        elif kind == "dedication":
            user = _dedication_user(ctx)
            mood = ctx.get("mood", "warm")
        elif kind == "song_intro":
            user = (
                "Bring in the next song on the air now. It is called %s and it is by %s "
                "-- it has a %s feel. Welcome it in with one or two warm spoken sentences "
                'in your own voice. Do not use the words "title" or "artist", do not label '
                'anything, do not write "X: Y", and do not repeat this instruction.'
                % (ctx.get("title", "this next one"),
                   ctx.get("artist", "a friend of the show"),
                   ctx.get("vibe", "late-night"))
            )
            if ctx.get("segue"):
                user = ("Open with a brief, warm half-sentence through-line -- %s -- and then "
                        % ctx["segue"]) + user[0].lower() + user[1:]
            rb = ctx.get("recommended_by")
            if rb:
                user += (
                    " A listener, %s, sent this one in -- give them a warm little shout "
                    "as you bring it in." % rb
                )
            if ctx.get("fresh"):
                user += (" Mention warmly that this record is brand new -- pressed just "
                         "tonight, never heard before.")
        elif kind == "local_weather":
            user = _local_weather_user(ctx)
        else:
            user = "Share one short late-night thought for the listeners right now."
        try:
            brain = call_brain(system, [{"role": "user", "content": user}])
            fb = _dedication_fallback(ctx) if kind == "dedication" else _templated_text(kind, ctx)
            text = _clean_dj_text(brain.get("text", "")) or fb
            mood = brain.get("mood", mood)
            arc_cue = brain.get("arc_cue", "none")
        except Exception:
            text = _dedication_fallback(ctx) if kind == "dedication" else _templated_text(kind, ctx)
    else:
        text = _templated_text(kind, ctx)
        if kind == "weather":
            mood = "nostalgic"

    speak = call_speak(text)
    return {
        "kind": kind,
        "text": text,
        "mood": mood,
        "arc_cue": arc_cue,
        "audio_b64": speak.get("audio_b64", ""),
        "words": speak.get("words", []),
        "wtimes": speak.get("wtimes", []),
        "wdurations": speak.get("wdurations", []),
    }


def segment_fallback(kind: str, ctx: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """Never-fail segment: templated text + a short silent WAV (bed covers it)."""
    return {
        "kind": kind,
        "text": _templated_text(kind, ctx),
        "mood": "warm",
        "arc_cue": "none",
        "audio_b64": _silent_wav_b64(),
        "words": [],
        "wtimes": [],
        "wdurations": [],
    }