Spaces:
Sleeping
Sleeping
SP2: AI showrunner — mood-matched songs, smart pacing, spoken through-line
Browse filesFiles changed (4)
47 unmodified lines484950515253545547 unmodified lines"tests/test_proxy.py::test_segment_dedication_memory_aware","tests/test_proxy.py::test_segment_dedication_no_memory_is_templated","tests/test_proxy.py::test_segment_fallback_is_never_failing_and_well_shaped","tests/test_proxy.py::test_song_intro_template_includes_handle","tests/test_proxy.py::test_song_intro_template_omits_handle_when_absent","tests/test_proxy.py::test_speakable_normalizes_nightwave_for_tts","tests/test_proxy.py::test_speakable_titlecases_nightwave","tests/test_proxy.py::test_templated_caller_intro_nonempty",47 unmodified lines4849505152535455565747 unmodified lines"tests/test_proxy.py::test_segment_dedication_memory_aware","tests/test_proxy.py::test_segment_dedication_no_memory_is_templated","tests/test_proxy.py::test_segment_fallback_is_never_failing_and_well_shaped","tests/test_proxy.py::test_song_intro_no_segue_when_absent","tests/test_proxy.py::test_song_intro_template_includes_handle","tests/test_proxy.py::test_song_intro_template_omits_handle_when_absent","tests/test_proxy.py::test_song_intro_weaves_segue","tests/test_proxy.py::test_speakable_normalizes_nightwave_for_tts","tests/test_proxy.py::test_speakable_titlecases_nightwave","tests/test_proxy.py::test_templated_caller_intro_nonempty",
533 unmodified lines534535536537538539533 unmodified linesctx.get("artist", "a friend of the show"),ctx.get("vibe", "late-night")))rb = ctx.get("recommended_by")if rb:user += (533 unmodified lines534535536537538539540541542533 unmodified linesctx.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 += (
680 unmodified lines681682683684685686481 unmodified lines11681169117011711172117335 unmodified lines120912101211121212131214121512161217121812194 unmodified lines1224122512261227122812291230123112321233123412 unmodified lines124712481249125012511252125312 unmodified lines1266126712681269127012711272480 unmodified lines175317541755175617571758275 unmodified lines203420352036203720382039680 unmodified linesconnecting:false, // caller-intro is playing before the mic openssessionMemory:null, // SP1: most recent caller {caller_name,place,topic,mood}dedicationQueue:[], // SP1: pending caller dedications (cap 2)warming:false // power-on warm-up ritual is in progress (visual only)};481 unmodified lineslet s; do { s = bank[Math.floor(Math.random() * bank.length)]; } while (bank.length > 1 && s === State.song);return s;}function showNowPlaying(track){if (!els.nowPlaying || !track) return;els.nowPlaying.classList.add("playing");35 unmodified linesreturn picks; // song_intro is appended in prepareBreak (always last)}// Buffer a whole break: DJ segments + the upcoming track's intro. -> {segs, track}async function prepareBreak(track){const segs = [];// A queued caller dedication takes precedence over a generic one this break.const callerDed = State.dedicationQueue.length ? State.dedicationQueue.shift() : null;let kinds = chooseBreakKinds();if (callerDed) kinds = kinds.filter(k => k !== "dedication");for (const k of kinds){const ctx = (k === "local_weather") ? State.locale : null;4 unmodified linesif (d) segs.push(d);}}const intro = await fetchSegment("song_intro",{ title:track.title, artist:track.artist, vibe:track.vibe, recommended_by:track.recommended_by });segs.push(intro || { kind:"song_intro", mood:"warm", arc_cue:"none",text:"Coming up next, " + track.title + " by " + track.artist + ".",audio_b64: SILENT_WAV, words:[], wtimes:[] });return { segs, track };}async function playDJ(s){12 unmodified lines// ensure a buffered break (only the opening one blocks, under the bed)if (!State.pendingBreak){showCaption("… NIGHTWAVE is coming on the air …", "status");State.pendingBreak = await prepareBreak(pickSong());}const brk = State.pendingBreak; State.pendingBreak = null;if (!State.powered || State.recording) continue;12 unmodified linesconst player = renderSong(track);State.songPlayer = player;// 3) dead-air gating: buffer the NEXT break while this song playsprepareBreak(pickSong()).then(b => { State.pendingBreak = b; }).catch(()=>{});if (player.setLevel) player.setLevel(0.28, 0.6); // duck the record under the DJ introawait playDJ(intro); // rides over the song's fade-inif (player.setLevel) player.setLevel(0.5, 1.2); // bring the record back up480 unmodified linesif (data && typeof data.meter_delta === "number") addMeter(data.meter_delta);if (data && data.memory_patch) State.sessionMemory = data.memory_patch;if (data && data.queue_dedication) queueDedication(data.memory_patch);await playSegment(data, data && data.caller_text);}275 unmodified lines// Hidden judge demo trigger: ?demo=1 + Shift+D queues a caller dedication for the// next break, so the callback is reliably demoable. Not visible product UI.if (/[?&]demo=1\b/.test(location.search)){window.addEventListener("keydown", e=>{if (e.shiftKey && (e.key === "D" || e.key === "d")){e.preventDefault();680 unmodified lines681682683684685686687688689690691481 unmodified lines11731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123235 unmodified lines12681269127012711272127312741275127612771278127912804 unmodified lines128512861287128812891290129112921293129412951296129712981299130012 unmodified lines131313141315131613171318131912 unmodified lines1332133313341335133613371338480 unmodified lines1819182018211822182318241825275 unmodified lines2101210221032104210521062107680 unmodified linesconnecting:false, // caller-intro is playing before the mic openssessionMemory:null, // SP1: most recent caller {caller_name,place,topic,mood}dedicationQueue:[], // SP1: pending caller dedications (cap 2)recentKinds:[], // SP2: rolling log of content kinds (cap 8) for pacingbreaksSinceCaller:99, // SP2: breaks since last call-in (high = none yet)lastSegueBreak:-99, // SP2: break index of last voiced through-linebreakIndex:0, // SP2: monotonic break counterdemo:false, // SP2: ?demo=1 logs showrunner pickswarming:false // power-on warm-up ritual is in progress (visual only)};481 unmodified lineslet s; do { s = bank[Math.floor(Math.random() * bank.length)]; } while (bank.length > 1 && s === State.song);return s;}// ---- SP2: AI showrunner (context-aware policy, deterministic, never blocks) ----const MOOD_VIBES = {warm: ["warm","cozy","hopeful","nostalgic","breezy","gentle"],nostalgic: ["nostalgic","wistful","bittersweet","melancholy","jazzy","romantic"],tender: ["tender","gentle","lonely","romantic","wistful","melancholy"],searching: ["pensive","mysterious","hypnotic","dreamy","eerie","ambient"],hollow: ["lonely","melancholy","eerie","ambient","bittersweet","pensive"],uneasy: ["eerie","mysterious","hypnotic","pensive","ambient","lonely"]};const MOOD_SEGUE = {warm:"the night's warm and easy", nostalgic:"we're feeling a little nostalgic",tender:"the hour's gone tender", searching:"a thoughtful stretch, this",hollow:"for the quiet, lonely hours", uneasy:"there's something restless in the air"};function pickSongForMood(mood, avoid){const bank = (State.songs && State.songs.length) ? State.songs : CLIENT_SONGS;const vibes = MOOD_VIBES[mood] || MOOD_VIBES.warm;const avoidVibe = avoid && avoid.vibe;const cand = bank.filter(s => vibes.indexOf(s.vibe) >= 0 && s !== avoid && s.vibe !== avoidVibe);if (!cand.length) return pickSong();const weighted = [];cand.forEach(s => { const w = vibes.length - vibes.indexOf(s.vibe); for (let i=0;i<w;i++) weighted.push(s); });return weighted[Math.floor(Math.random()*weighted.length)];}function runShowrunner(){try{const mood = (State.sessionMemory && State.breaksSinceCaller <= 3)? (State.sessionMemory.mood || "warm") : "warm";const song = pickSongForMood(mood, State.song);const weatherKind = (State.locale && State.locale.resolved) ? "local_weather" : "weather";const recent2 = State.recentKinds.slice(-2);let pool = ["thought", weatherKind, "dedication"].filter(k => recent2.indexOf(k) < 0);if (!pool.length) pool = ["thought", weatherKind, "dedication"];const kinds = ["rejoin"];const extra = (Math.random() < 0.5) ? 1 : 2;for (let i=0;i<extra;i++){ // distinct content kinds this breakconst avail = pool.filter(k => kinds.indexOf(k) < 0);const src = avail.length ? avail : pool;kinds.push(src[Math.floor(Math.random()*src.length)]);}if (State.breakIndex % 3 === 0) kinds.push("station_id");let segueHint = null;const sinceSegue = State.breakIndex - State.lastSegueBreak;if ((State.breaksSinceCaller === 1 || sinceSegue >= 5) && sinceSegue >= 2){segueHint = (State.breaksSinceCaller === 1) ? "right after that caller": (MOOD_SEGUE[mood] || MOOD_SEGUE.warm);State.lastSegueBreak = State.breakIndex;}if (State.demo) try{ console.log("showrunner:", mood, "->", song.title, "("+song.vibe+")", segueHint||""); }catch(_){}return { kinds, song, tone:mood, segueHint };}catch(_){return { kinds: chooseBreakKinds(), song: pickSong(), tone:"warm", segueHint:null };}}function showNowPlaying(track){if (!els.nowPlaying || !track) return;els.nowPlaying.classList.add("playing");35 unmodified linesreturn picks; // song_intro is appended in prepareBreak (always last)}// Buffer a whole break: DJ segments + the upcoming track's intro. -> {segs, track}async function prepareBreak(){const plan = runShowrunner();const track = plan.song;const segs = [];// A queued caller dedication takes precedence over a generic one this break.const callerDed = State.dedicationQueue.length ? State.dedicationQueue.shift() : null;let kinds = plan.kinds.slice();if (callerDed) kinds = kinds.filter(k => k !== "dedication");for (const k of kinds){const ctx = (k === "local_weather") ? State.locale : null;4 unmodified linesif (d) segs.push(d);}}const introCtx = { title:track.title, artist:track.artist, vibe:track.vibe, recommended_by:track.recommended_by };if (plan.segueHint) introCtx.segue = plan.segueHint;const intro = await fetchSegment("song_intro", introCtx);segs.push(intro || { kind:"song_intro", mood:"warm", arc_cue:"none",text:"Coming up next, " + track.title + " by " + track.artist + ".",audio_b64: SILENT_WAV, words:[], wtimes:[] });State.breakIndex++;State.breaksSinceCaller++;for (const k of kinds){ if (k !== "rejoin" && k !== "station_id") State.recentKinds.push(k); }while (State.recentKinds.length > 8) State.recentKinds.shift();return { segs, track };}async function playDJ(s){12 unmodified lines// ensure a buffered break (only the opening one blocks, under the bed)if (!State.pendingBreak){showCaption("… NIGHTWAVE is coming on the air …", "status");State.pendingBreak = await prepareBreak();}const brk = State.pendingBreak; State.pendingBreak = null;if (!State.powered || State.recording) continue;12 unmodified linesconst player = renderSong(track);State.songPlayer = player;// 3) dead-air gating: buffer the NEXT break while this song playsprepareBreak().then(b => { State.pendingBreak = b; }).catch(()=>{});if (player.setLevel) player.setLevel(0.28, 0.6); // duck the record under the DJ introawait playDJ(intro); // rides over the song's fade-inif (player.setLevel) player.setLevel(0.5, 1.2); // bring the record back up480 unmodified linesif (data && typeof data.meter_delta === "number") addMeter(data.meter_delta);if (data && data.memory_patch) State.sessionMemory = data.memory_patch;if (data && data.queue_dedication) queueDedication(data.memory_patch);State.breaksSinceCaller = 0; // SP2: next break reflects the caller's moodawait playSegment(data, data && data.caller_text);}275 unmodified lines// Hidden judge demo trigger: ?demo=1 + Shift+D queues a caller dedication for the// next break, so the callback is reliably demoable. Not visible product UI.if (/[?&]demo=1\b/.test(location.search)){State.demo = true;window.addEventListener("keydown", e=>{if (e.shiftKey && (e.key === "D" || e.key === "d")){e.preventDefault();
283 unmodified lines284285286283 unmodified lineslambda t, voice=arc.VOICE: {"audio_b64": "", "words": [], "wtimes": [], "wdurations": []})out = proxy.segment_turn("dedication", {"topic": "waiting up", "caller_name": "Sam"})assert "Sam" in out["text"] # _dedication_fallback weaves the name283 unmodified lines284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315283 unmodified lineslambda t, voice=arc.VOICE: {"audio_b64": "", "words": [], "wtimes": [], "wdurations": []})out = proxy.segment_turn("dedication", {"topic": "waiting up", "caller_name": "Sam"})assert "Sam" in out["text"] # _dedication_fallback weaves the namedef test_song_intro_weaves_segue(monkeypatch):captured = {}def _brain(system, messages):captured["user"] = messages[-1]["content"]return {"text": "After that caller, a soft one -- 'Neon Rain'.", "mood": "tender", "arc_cue": "none"}monkeypatch.setattr(proxy, "call_brain", _brain)monkeypatch.setattr(proxy, "call_speak",lambda t, voice=arc.VOICE: {"audio_b64": "", "words": [], "wtimes": [], "wdurations": []})proxy.segment_turn("song_intro", {"title": "Neon Rain", "artist": "The Tuesday Ghosts","vibe": "melancholy", "segue": "right after that caller"})assert "right after that caller" in captured["user"]def test_song_intro_no_segue_when_absent(monkeypatch):captured = {}def _brain(system, messages):captured["user"] = messages[-1]["content"]return {"text": "Coming up, 'Neon Rain'.", "mood": "warm", "arc_cue": "none"}monkeypatch.setattr(proxy, "call_brain", _brain)monkeypatch.setattr(proxy, "call_speak",lambda t, voice=arc.VOICE: {"audio_b64": "", "words": [], "wtimes": [], "wdurations": []})proxy.segment_turn("song_intro", {"title": "Neon Rain", "artist": "The Tuesday Ghosts", "vibe": "melancholy"})assert "through-line" not in captured["user"]
.pytest_cache/v/cache/nodeids+2
proxy.py+3
radio.html+74-6
tests/test_proxy.py+29