File size: 21,589 Bytes
45d92cc
 
 
 
 
efe8555
 
45d92cc
 
 
 
 
 
 
 
 
 
4d426ef
45d92cc
a07ee41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d92cc
efe8555
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
efe8555
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d426ef
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a07ee41
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efe8555
 
 
 
 
 
 
 
 
a07ee41
efe8555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518c021
 
 
 
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efe8555
 
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
efe8555
 
 
 
 
 
45d92cc
 
 
 
 
 
0a82bbc
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d426ef
45d92cc
 
 
 
 
 
 
 
 
 
 
 
0a82bbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d92cc
 
 
 
 
 
 
4d426ef
 
 
 
 
 
 
 
 
 
 
 
 
a07ee41
4d426ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d92cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import os
import re
import urllib.error
import urllib.request
from pathlib import Path
from typing import Any, Literal, Protocol

from huggingface_hub import InferenceClient
from pydantic import BaseModel, Field

from studio.adapters.router import route_id_for_model
from studio.registry import ModelRegistry, load_registry

PROMPT_PATH = Path(__file__).resolve().parents[1] / "prompts" / "producer.md"
MASTER_PROMPT_PATH = Path(__file__).resolve().parents[1] / "prompts" / "mastering.md"

_FALLBACK_PRODUCER = (
    "Du bist der Musikproducer-Agent von 2B-Studio. Antworte ausschließlich als JSON "
    "für fill_create_fields. Spiegele die Sprache des Users. Keine freien Generierungen starten."
)
_FALLBACK_MASTER = (
    "Du bist der Mastering-Agent von 2B-Studio. Antworte ausschließlich als JSON "
    "für fill_master_fields. Spiegele die Sprache des Users. Kein Mastering selbst starten."
)

def _read_prompt(env_key: str, path: Path, fallback: str) -> str:
    env = (os.environ.get(env_key) or "").strip()
    if env and not env.startswith("# STUB"):
        return env
    if path.is_file():
        text = path.read_text(encoding="utf-8").strip()
        if text and not text.startswith("# STUB"):
            return text
    return fallback



def gemini_api_key() -> str | None:
    return os.environ.get("GEMINI_API_KEY", "").strip() or None


class FieldProposal(BaseModel):
    model_id: str | None = None
    genre_style_instruments: str
    lyrics: str
    negative: str
    rationale: str
    duration_s: int = Field(default=20, ge=5, le=180)
    idea: str = ""
    studio: dict[str, str] = Field(default_factory=dict)
    renderings: dict[str, str] = Field(default_factory=dict)
    preview: str = ""
    ask_user: str | None = None
    warnings: list[str] = Field(default_factory=list)
    source: Literal["gemini", "hf-inference", "fallback"] = "fallback"


class ProducerAction(BaseModel):
    type: Literal["fill_create_fields"] = "fill_create_fields"
    payload: FieldProposal
    preview: str
    ask_user: str | None = None


class MasterProposal(BaseModel):
    profile: Literal[
        "streaming_safe",
        "streaming_loud",
        "club",
        "dynamic_hifi",
        "premium_clarity",
        "audiophile_preserve",
    ] = "streaming_safe"
    purity_mode: Literal["soft_keep_air", "strict", "off"] = "soft_keep_air"
    keep_air_db: float = Field(default=-20.0, ge=-48, le=0)
    margin: float = Field(default=2.0, ge=0.5, le=2.9)
    intensity: int = Field(default=50, ge=0, le=100)
    analyze_only: bool = False
    rationale: str
    preview: str
    requires_strict_approval: bool = False
    warnings: list[str] = Field(default_factory=list)


class MasterAction(BaseModel):
    type: Literal["fill_master_params"] = "fill_master_params"
    payload: MasterProposal
    preview: str


class ProducerProvider(Protocol):
    def propose(self, message: str, current_fields: dict[str, Any]) -> dict[str, Any]: ...


class HFInferenceProducerProvider:
    def __init__(self) -> None:
        token = os.environ.get("HF_TOKEN", "")
        if not token:
            raise ValueError("HF_TOKEN ist nicht gesetzt.")
        self.model_id = os.environ.get(
            "AGENT_MODEL_ID", "meta-llama/Llama-3.2-3B-Instruct"
        )
        self.client = InferenceClient(token=token)
        self.system_prompt = _read_prompt("PRODUCER_SYSTEM_PROMPT", PROMPT_PATH, _FALLBACK_PRODUCER)

    def propose(self, message: str, current_fields: dict[str, Any]) -> dict[str, Any]:
        response = self.client.chat.completions.create(
            model=self.model_id,
            messages=[
                {"role": "system", "content": self.system_prompt},
                {
                    "role": "user",
                    "content": json.dumps(
                        {"idea": message, "current_fields": current_fields},
                        ensure_ascii=False,
                    ),
                },
            ],
            temperature=0.2,
            max_tokens=1400,
        )
        content = response.choices[0].message.content or "{}"
        cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", content.strip())
        parsed = json.loads(cleaned)
        if not isinstance(parsed, dict):
            raise ValueError("Der Agent lieferte kein JSON-Objekt.")
        payload = parsed.get("payload", parsed)
        if not isinstance(payload, dict):
            raise ValueError("Der Agent lieferte kein gültiges Payload-Objekt.")
        return payload


class GeminiProducerProvider:
    """Gemini via REST API (keine Zusatz-Dependency, Free-Tier-tauglich)."""

    def __init__(self) -> None:
        key = gemini_api_key()
        if not key:
            raise ValueError("GEMINI_API_KEY ist nicht gesetzt.")
        self.api_key = key
        self.model_id = os.environ.get("GEMINI_MODEL", "gemini-3.5-flash")
        self.system_prompt = _read_prompt("PRODUCER_SYSTEM_PROMPT", PROMPT_PATH, _FALLBACK_PRODUCER)
        self.base_url = "https://generativelanguage.googleapis.com/v1beta"

    def propose(self, message: str, current_fields: dict[str, Any]) -> dict[str, Any]:
        url = (
            f"{self.base_url}/models/{self.model_id}:generateContent"
            f"?key={self.api_key}"
        )
        body = {
            "system_instruction": {"parts": [{"text": self.system_prompt}]},
            "contents": [
                {
                    "parts": [
                        {
                            "text": json.dumps(
                                {"idea": message, "current_fields": current_fields},
                                ensure_ascii=False,
                            )
                        }
                    ]
                }
            ],
            "generationConfig": {
                "temperature": 0.2,
                "maxOutputTokens": 4096,
                "responseMimeType": "application/json",
            },
        }
        request = urllib.request.Request(
            url,
            data=json.dumps(body).encode("utf-8"),
            headers={"Content-Type": "application/json"},
            method="POST",
        )
        try:
            with urllib.request.urlopen(request, timeout=60) as response:
                payload = json.loads(response.read().decode("utf-8"))
        except urllib.error.HTTPError as error:
            detail = error.read().decode("utf-8", errors="replace")[:300]
            raise ValueError(f"Gemini HTTP {error.code}: {detail}") from error
        except urllib.error.URLError as error:
            raise ValueError(f"Gemini nicht erreichbar: {error.reason}") from error

        candidates = payload.get("candidates") or []
        if not candidates:
            block = payload.get("promptFeedback", {}).get("blockReason")
            raise ValueError(f"Gemini lieferte keine Antwort (blockReason={block}).")
        text = candidates[0].get("content", {}).get("parts", [{}])[0].get("text", "")
        cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", text.strip())
        parsed = json.loads(cleaned)
        if not isinstance(parsed, dict):
            raise ValueError("Gemini lieferte kein JSON-Objekt.")
        payload_out = parsed.get("payload", parsed)
        if not isinstance(payload_out, dict):
            raise ValueError("Gemini lieferte kein gültiges Payload-Objekt.")
        return payload_out


def _duration_from_message(message: str) -> int:
    match = re.search(
        r"\b(?P<value>\d{1,3})\s*(?P<unit>sekunden?|seconds?|secs?|s|minuten?|minutes?|min)\b",
        message.casefold(),
    )
    if not match:
        return 20
    value = int(match.group("value"))
    if match.group("unit").startswith(("min", "minute")):
        value *= 60
    return max(5, min(180, value))


def _fallback_proposal(message: str) -> FieldProposal:
    normalized = message.casefold()
    duration_s = _duration_from_message(message)
    vocals_requested = any(
        term in normalized
        for term in (
            "vocal",
            "gesang",
            "sänger",
            "sängerin",
            "männlich",
            "weiblich",
            "lyrics",
            "stimme",
        )
    ) and "instrumental" not in normalized and not any(
        neg in normalized
        for neg in (
            "ohne gesang", "kein gesang", "keinen gesang", "ohne vocals", "keine vocals", "ohne stimme",
            "ohne text", "keine lyrics", "ohne lyrics", "no vocals", "without vocals", "no lyrics",
        )
    )

    if any(term in normalized for term in ("riddim", "reggae", "dub")):
        style = (
            "dark roots riddim, 140 BPM, analog sub bass, tight live drums, "
            "warm organ skanks, evolving dub transitions"
        )
    elif any(term in normalized for term in ("techno", "club", "rave")):
        style = (
            "hypnotic techno, 132 BPM, rolling sub bass, crisp percussion, "
            "warehouse depth, tension-building arrangement"
        )
    elif any(term in normalized for term in ("lofi", "chill", "entspannt")):
        style = (
            "lo-fi soul, 82 BPM, dusty drums, mellow Rhodes piano, warm tape texture, "
            "human timing, intimate dynamics"
        )
    elif any(term in normalized for term in ("film", "cinematic", "episch")):
        style = (
            "cinematic electronica, 96 BPM, evolving strings, deep percussion, "
            "wide synth layers, clear emotional arc"
        )
    elif any(term in normalized for term in ("rock", "gitarre", "guitar")):
        style = (
            "modern alternative rock, 118 BPM, expressive electric guitars, live drums, "
            "driving bass, dynamic verse-to-chorus lift"
        )
    else:
        style = (
            "modern electronic soul, 108 BPM, punchy drums, warm bass, memorable hook, "
            "intentional transitions, polished stereo depth"
        )

    if vocals_requested:
        style += ", expressive lead vocal with clear diction"
        lyrics = (
            "[Verse]\nWe turn the quiet into light\nEvery step is drawing near\n\n"
            "[Chorus]\nWe hold the line tonight\nMake the hidden signal clear"
        )
        model_id = "minimax-music3"
    else:
        lyrics = "[Instrumental]"
        model_id = "2b-musicgen-medium" if duration_s > 20 else "2b-musicgen-small"

    short_caption = style[:800]
    return FieldProposal(
        model_id=model_id,
        genre_style_instruments=style,
        lyrics=lyrics,
        negative=(
            "muddy low end, harsh sibilance, uncontrolled clipping, phase cancellation, "
            "static arrangement, generic AI artifacts"
        ),
        duration_s=duration_s,
        idea=message.strip(),
        studio={
            "global_metadata": f"Basic Attributes: {style}",
            "vocal_details": (
                "Vocal Gender & Timbre: expressive lead, clear diction"
                if vocals_requested
                else "Vocal Direction: instrumental, no lead vocals"
            ),
            "arrangement": "Instrument Lifecycle: focused opening, developed middle, resolved ending",
        },
        renderings={
            "musicgen_prompt": style,
            "short_caption": short_caption,
        },
        preview=f"{duration_s}s · {'Vocal' if vocals_requested else 'Instrumental'} · {model_id}",
        rationale=(
            "MiniMax passt zur strukturierten Vocal-Anforderung."
            if vocals_requested
            else "Der live angebundene 2B-Audio-Worker passt zum instrumentalen Entwurf."
        ),
        source="fallback",
    )


class ProducerAgent:
    def __init__(
        self,
        registry: ModelRegistry,
        provider: ProducerProvider | None = None,
    ) -> None:
        self.registry = registry
        self.provider = provider
        if self.provider is None:
            if gemini_api_key():
                self.provider = GeminiProducerProvider()
            elif os.environ.get("HF_TOKEN"):
                self.provider = HFInferenceProducerProvider()

    def propose(
        self,
        message: str,
        current_fields: dict[str, Any] | None = None,
    ) -> ProducerAction:
        warnings: list[str] = []
        if self.provider is not None:
            try:
                raw = self.provider.propose(message, current_fields or {})
                source = (
                    "gemini"
                    if isinstance(self.provider, GeminiProducerProvider)
                    else "hf-inference"
                )
                proposal = FieldProposal.model_validate({**raw, "source": source})
            except Exception as error:
                proposal = _fallback_proposal(message)
                warnings.append(f"LLM-Fallback aktiv: {type(error).__name__}")
        else:
            proposal = _fallback_proposal(message)

        # Deterministische Absicherung: vage Eingabe -> immer eine Rückfrage
        if proposal.ask_user is None and _is_vague_input(message):
            proposal.ask_user = _default_ask(message)

        try:
            model = self.registry.get(proposal.model_id or "")
        except KeyError:
            model = self.registry.get("2b-musicgen-small")
            proposal.model_id = model.id
            warnings.append("Unbekanntes Agentenmodell wurde auf 2B Audio Small zurückgesetzt.")

        if proposal.duration_s > model.max_duration_s:
            warnings.append(
                f"Dauer wurde an {model.label} angepasst: maximal {model.max_duration_s} s."
            )
            proposal.duration_s = model.max_duration_s
        if proposal.lyrics.strip().casefold() != "[instrumental]" and not model.supports_vocals:
            warnings.append(f"{model.label} unterstützt Gesang nur eingeschränkt.")
        if not route_id_for_model(model).endswith("-real"):
            warnings.append(
                f"{model.label} ist gerade nicht erreichbar; der Worker antwortet nicht."
            )

        proposal.warnings = [*proposal.warnings, *warnings]
        if not proposal.preview:
            proposal.preview = f"{proposal.duration_s}s · {model.label}"
        return ProducerAction(
            payload=proposal,
            preview=proposal.preview,
            ask_user=proposal.ask_user,
        )


def _is_vague_input(message: str) -> bool:
    """True, wenn die Eingabe keine entscheidenden Produktions-Infos enthält."""
    normalized = message.casefold()
    genre_terms = (
        "riddim", "reggae", "dub", "techno", "club", "rave", "lofi", "lo-fi", "chill",
        "film", "cinematic", "episch", "rock", "gitarre", "guitar", "pop", "hiphop",
        "hip-hop", "house", "drum and bass", "dnb", "ambient", "jazz", "soul", "trap",
        "edm", "trance", "metal", "punk", "blues", "funk", "disco", "country", "folk",
    )
    mood_terms = (
        "dunkel", "dark", "euphorisch", "euphoric", "entspannt", "relaxed", "traurig",
        "sad", "wütend", "angry", "aggressiv", "aggressive", "melancholisch", "melancholic",
        "fröhlich", "happy", "warm", "kalt", "cold", "mysteriös", "mysterious", "episch",
        "cinematic", "energetisch", "energetic", "ruhig", "calm", "böse", "evil", "düster",
    )
    instrument_terms = (
        "bass", "drums", "drum", "gitarre", "guitar", "piano", "keys", "synth", "synthesizer",
        "strings", "streicher", "violine", "saxophon", "sax", "horn", "flöte", "flute",
        "percussion", "schlagzeug", "kick", "snare", "hi-hat", "pad", "lead", "vocal", "gesang",
        "stimme", "choir", "chor", "organ", "orgel", "brass",
    )
    structure_terms = (
        "instrumental", "vocal", "gesang", "lyrics", "text", "sänger", "sängerin",
        "sekunden", "seconds", "minuten", "minutes", "bpm", "tempo", "dauer", "länge",
        "intro", "verse", "chorus", "bridge", "drop", "hook",
    )
    has_info = any(
        term in normalized
        for term in (*genre_terms, *mood_terms, *instrument_terms, *structure_terms)
    )
    return not has_info


def _default_ask(message: str) -> str:
    """Deterministische Rückfrage, wenn die Eingabe zu wenig Info enthält."""
    normalized = message.casefold()
    if "instrumental" not in normalized and not any(
        term in normalized for term in ("vocal", "gesang", "stimme", "lyrics", "text")
    ):
        return "Soll es ein Instrumental sein oder mit Gesang?"
    if not any(
        term in normalized
        for term in (
            "bpm", "tempo", "sekunden", "seconds", "minuten", "minutes", "dauer", "länge",
        )
    ):
        return "Wie lang soll der Track sein (5–180 Sekunden)?"
    return "Welche Stimmung soll der Track vermitteln (z. B. dunkel, euphorisch, entspannt)?"


def propose_fields(message: str) -> FieldProposal:
    """Compatibility helper using the validated local fallback/provider chain."""

    return ProducerAgent(load_registry()).propose(message).payload


def propose_master(message: str) -> MasterAction:
    if gemini_api_key():
        try:
            return _propose_master_llm(message)
        except Exception:
            pass  # Fallback auf deterministische Heuristik
    return _propose_master_fallback(message)


def _propose_master_llm(message: str) -> MasterAction:
    """Master-Vorschlag via Gemini; nutzt prompts/mastering.md als System-Prompt."""
    api_key = gemini_api_key()
    assert api_key is not None
    model_id = os.environ.get("GEMINI_MODEL", "gemini-3.5-flash")
    system_prompt = _read_prompt("MASTERING_SYSTEM_PROMPT", MASTER_PROMPT_PATH, _FALLBACK_MASTER)
    base_url = "https://generativelanguage.googleapis.com/v1beta"
    url = f"{base_url}/models/{model_id}:generateContent?key={api_key}"
    body = {
        "system_instruction": {"parts": [{"text": system_prompt}]},
        "contents": [{"parts": [{"text": json.dumps({"idea": message}, ensure_ascii=False)}]}],
        "generationConfig": {
            "temperature": 0.2,
            "maxOutputTokens": 2048,
            "responseMimeType": "application/json",
        },
    }
    request = urllib.request.Request(
        url,
        data=json.dumps(body).encode("utf-8"),
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    try:
        with urllib.request.urlopen(request, timeout=60) as response:
            payload = json.loads(response.read().decode("utf-8"))
    except urllib.error.HTTPError as error:
        detail = error.read().decode("utf-8", errors="replace")[:300]
        raise ValueError(f"Gemini HTTP {error.code}: {detail}") from error
    except urllib.error.URLError as error:
        raise ValueError(f"Gemini nicht erreichbar: {error.reason}") from error

    candidates = payload.get("candidates") or []
    if not candidates:
        block = payload.get("promptFeedback", {}).get("blockReason")
        raise ValueError(f"Gemini lieferte keine Antwort (blockReason={block}).")
    text = candidates[0].get("content", {}).get("parts", [{}])[0].get("text", "")
    cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", text.strip())
    parsed = json.loads(cleaned)
    if not isinstance(parsed, dict):
        raise ValueError("Gemini lieferte kein JSON-Objekt.")
    payload_out = parsed.get("payload", parsed)
    if not isinstance(payload_out, dict):
        raise ValueError("Gemini lieferte kein gültiges Payload-Objekt.")
    proposal = MasterProposal.model_validate(payload_out)
    if proposal.purity_mode == "strict":
        proposal.requires_strict_approval = True
    return MasterAction(payload=proposal, preview=proposal.preview)


def _propose_master_fallback(message: str) -> MasterAction:
    normalized = message.casefold()
    analyze_only = any(
        term in normalized for term in ("nur messen", "nur analyse", "only analyze", "analyse only")
    )
    if any(term in normalized for term in ("club", "dj", "dancefloor")):
        profile = "club"
        intensity = 72
    elif any(term in normalized for term in ("lauter", "loud", "druck")):
        profile = "streaming_loud"
        intensity = 65
    elif any(term in normalized for term in ("hifi", "dynamisch", "audiophil")):
        profile = "dynamic_hifi"
        intensity = 42
    else:
        profile = "streaming_safe"
        intensity = 50

    strict = "strict" in normalized
    purity_off = any(
        term in normalized for term in ("ohne purity", "kein purity", "nur master", "skip purity")
    )
    purity_mode = "strict" if strict else "off" if purity_off else "soft_keep_air"
    preserve_air = any(
        term in normalized for term in ("luft", "air", "wärme", "hall", "nicht tot")
    )
    keep_air_db = -24.0 if preserve_air else -20.0
    margin = 1.5 if preserve_air else 2.0
    rationale = (
        "Strict wurde nur als Vorschlag markiert und benötigt eine separate Bestätigung."
        if strict
        else "soft_keep_air schützt Air, Hall und Wärme; das Original bleibt unverändert."
    )
    if analyze_only:
        rationale = "Es wird ausschließlich gemessen; keine Audiodatei wird verändert oder erzeugt."

    proposal = MasterProposal(
        profile=profile,
        purity_mode=purity_mode,
        keep_air_db=keep_air_db,
        margin=margin,
        intensity=intensity,
        analyze_only=analyze_only,
        rationale=rationale,
        preview=f"{profile} · {purity_mode}{' · nur Analyse' if analyze_only else ''}",
        requires_strict_approval=strict,
    )
    return MasterAction(payload=proposal, preview=proposal.preview)