File size: 12,582 Bytes
921d377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Interaction router β€” resolves one viewer action into a Transition.

This is the hot path of the runtime. One call per viewer click /
message / hotspot. Orchestrates:

  1. Build runtime state snapshot (state.build_runtime_state).
  2. If input is free text β†’ classify + free-input decision.
  3. Personalization rule evaluation β†’ RouterHint.
  4. Pick next node: RouterHint override OR outbound edge match.
  5. Apply progression rewards.
  6. Update character state (mood, affinity).

Return: a ``ResolvedTurn`` dataclass that the HTTP layer
serializes to the response contract defined in the design doc.

Keep this module pure on the Python side β€” I/O (event append, turn
append) is done by the caller (the HTTP router in batch 7).
"""
from __future__ import annotations

import time
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional

from .. import repo
from ..config import InteractiveConfig
from ..models import Action, Experience, Session
from ..personalize import PersonalizationProfile, RouterHint, evaluate as personalize_eval
from ..personalize.rules import Rule, RuleCondition
from ..policy import Decision, check_action_execution, check_free_input
from ..policy.classifier import classify_intent
from ..progression import apply_rewards, describe_level, is_action_unlocked
from .cooldown import check_cooldown, mark_used
from .state import RuntimeState, build_runtime_state, upsert_character_state, upsert_progress
from .types import Transition, TransitionKind


@dataclass(frozen=True)
class ActionPayload:
    """Input to the router. Either action_id or free_text is set."""

    action_id: str = ""
    free_text: str = ""
    viewer_region: str = ""
    client_ts_ms: int = 0


@dataclass(frozen=True)
class ResolvedTurn:
    """Router output. Fully self-contained response struct."""

    session_id: str
    transition: Transition
    decision: Decision
    intent_code: str
    reward_deltas: Dict[str, float] = field(default_factory=dict)
    level_description_display: str = ""
    level_description_level: int = 0
    mood: str = "neutral"
    affinity_score: float = 0.5
    matched_rule_id: Optional[str] = None


# ─────────────────────────────────────────────────────────────────
# Helpers
# ─────────────────────────────────────────────────────────────────

def _pick_next_from_hint_or_edges(
    hint: RouterHint, state: RuntimeState, action_intent: Optional[str],
) -> Transition:
    """Route to hint.route_to_node if set; otherwise pick the
    outbound edge whose label matches the action's intent_code (or
    the first outbound edge as fallback)."""
    if hint.route_to_node:
        return Transition(
            to_node_id=hint.route_to_node,
            kind=TransitionKind.AUTO,
            label="personalization",
            payload={"rule_id": hint.matched_rule_id},
            rule_id=hint.matched_rule_id,
        )

    outbound = [e for e in repo.list_edges(state.experience_id) if e.from_node_id == state.current_node_id]
    if not outbound:
        # Dead end β€” stay on current node.
        return Transition(
            to_node_id=state.current_node_id,
            kind=TransitionKind.FALLBACK,
            label="no_outbound",
            payload={},
        )

    # Prefer edges whose trigger_payload.label matches action intent.
    if action_intent:
        for e in outbound:
            payload = e.trigger_payload or {}
            if payload.get("label") == action_intent or e.trigger_kind == "intent":
                return Transition(
                    to_node_id=e.to_node_id,
                    kind=e.trigger_kind,
                    label=str(payload.get("label", "")),
                    payload=dict(payload),
                )

    # Default: lowest-ordinal outbound.
    ordered = sorted(outbound, key=lambda e: (e.ordinal, 0))
    e = ordered[0]
    payload = e.trigger_payload or {}
    return Transition(
        to_node_id=e.to_node_id,
        kind=e.trigger_kind,
        label=str(payload.get("label", "")),
        payload=dict(payload),
    )


def _now_ms() -> int:
    return int(time.time() * 1000)


def _load_rules_for_experience(experience_id: str) -> List[Rule]:
    """Load enabled personalization rules from DB into typed Rule list."""
    from .. import store
    with store._conn() as con:
        rows = con.execute(
            "SELECT * FROM ix_personalization_rules "
            "WHERE experience_id = ? AND enabled = 1",
            (experience_id,),
        ).fetchall()
    out: List[Rule] = []
    for r in rows:
        d = store.row_to_dict(r, json_fields=("condition", "action"))
        out.append(Rule(
            id=str(d["id"]),
            name=str(d.get("name", "")),
            condition=RuleCondition(raw=d.get("condition") or {}),
            action=dict(d.get("action") or {}),
            priority=int(d.get("priority") or 100),
            enabled=bool(d.get("enabled", True)),
        ))
    return out


# ─────────────────────────────────────────────────────────────────
# Public entry
# ─────────────────────────────────────────────────────────────────

def resolve_next(
    cfg: InteractiveConfig,
    experience: Experience,
    session: Session,
    payload: ActionPayload,
    *,
    action: Optional[Action] = None,
    profile: Optional[PersonalizationProfile] = None,
) -> ResolvedTurn:
    """Resolve one viewer turn. Returns a ``ResolvedTurn``.

    The caller supplies ``action`` when ``payload.action_id`` is
    set; when ``payload.free_text`` is set instead, the function
    classifies the intent and makes a policy decision directly.
    """
    state = build_runtime_state(session.id)
    if state is None:
        return ResolvedTurn(
            session_id=session.id,
            transition=Transition(
                to_node_id=session.current_node_id,
                kind=TransitionKind.FALLBACK,
                label="session_missing",
                payload={},
            ),
            decision=Decision(decision="block", reason_code="session_missing"),
            intent_code="",
        )

    now_ms = payload.client_ts_ms or _now_ms()
    personalization = profile or PersonalizationProfile(
        language=state.language,
        raw=state.personalization,
    )

    # ── Free-text path ──────────────────────────────────────────
    if payload.free_text and not payload.action_id:
        decision = check_free_input(
            cfg, payload.free_text, experience, session,
            viewer_region=payload.viewer_region,
        )
        intent_code = classify_intent(payload.free_text).intent_code
        if not decision.is_allow():
            return ResolvedTurn(
                session_id=session.id,
                transition=Transition(
                    to_node_id=state.current_node_id,
                    kind=TransitionKind.FALLBACK,
                    label="policy",
                    payload={"reason_code": decision.reason_code},
                ),
                decision=decision,
                intent_code=intent_code,
                mood=state.character_mood,
                affinity_score=state.affinity_score,
            )
        rules = _load_rules_for_experience(experience.id)
        hint = personalize_eval(rules, personalization, state)
        transition = _pick_next_from_hint_or_edges(hint, state, intent_code)
        return ResolvedTurn(
            session_id=session.id,
            transition=transition,
            decision=decision,
            intent_code=intent_code,
            mood=state.character_mood,
            affinity_score=state.affinity_score,
            matched_rule_id=hint.matched_rule_id,
        )

    # ── Action path ──────────────────────────────────────────────
    if not action:
        return ResolvedTurn(
            session_id=session.id,
            transition=Transition(
                to_node_id=state.current_node_id,
                kind=TransitionKind.FALLBACK,
                label="invalid",
                payload={},
            ),
            decision=Decision(decision="block", reason_code="no_action_provided"),
            intent_code="",
        )

    # Unlock / level gate.
    unlocked, lock_reason = is_action_unlocked(action, state.progress)
    if not unlocked:
        return ResolvedTurn(
            session_id=session.id,
            transition=Transition(
                to_node_id=state.current_node_id,
                kind=TransitionKind.FALLBACK,
                label="locked",
                payload={"reason": lock_reason},
            ),
            decision=Decision(decision="block", reason_code=lock_reason),
            intent_code=action.intent_code,
            mood=state.character_mood,
            affinity_score=state.affinity_score,
        )

    # Cooldown.
    remaining = check_cooldown(session.id, action.id, action.cooldown_sec, now_ms=now_ms)
    if remaining > 0:
        return ResolvedTurn(
            session_id=session.id,
            transition=Transition(
                to_node_id=state.current_node_id,
                kind=TransitionKind.FALLBACK,
                label="cooldown",
                payload={"remaining_sec": remaining},
            ),
            decision=Decision(
                decision="block", reason_code="cooldown",
                message=f"Cooldown: wait {remaining:.1f}s.",
            ),
            intent_code=action.intent_code,
            mood=state.character_mood,
            affinity_score=state.affinity_score,
        )

    # Policy re-check at execution time.
    uses_so_far = state.uses_by_action.get(action.id, 0)
    decision = check_action_execution(
        cfg, action, experience, session,
        viewer_region=payload.viewer_region,
        last_used_at_ms=0,  # cooldown already handled above
        uses_this_session=uses_so_far,
        now_ms=now_ms,
    )
    if not decision.is_allow():
        return ResolvedTurn(
            session_id=session.id,
            transition=Transition(
                to_node_id=state.current_node_id,
                kind=TransitionKind.FALLBACK,
                label="policy",
                payload={"reason_code": decision.reason_code},
            ),
            decision=decision,
            intent_code=action.intent_code,
            mood=state.character_mood,
            affinity_score=state.affinity_score,
        )

    # Apply rewards.
    reward = apply_rewards(action, state.progress, uses_before_this=uses_so_far)
    for metric_key, value in reward.new_progress.items():
        upsert_progress(session.id, reward.scheme, metric_key, value)

    # Apply character state updates from action.mood_delta + personalization bump.
    md = action.mood_delta or {}
    new_mood = str(md.get("mood") or state.character_mood)
    new_aff = state.affinity_score + float(md.get("affinity") or 0.0)

    rules = _load_rules_for_experience(experience.id)
    hint = personalize_eval(rules, personalization, state)
    new_aff += float(hint.bump_affinity or 0.0)
    new_aff = max(0.0, min(1.0, new_aff))

    upsert_character_state(
        session.id, persona_id="",
        mood=new_mood, affinity_score=new_aff,
    )

    # Cooldown mark.
    mark_used(session.id, action.id, now_ms=now_ms)

    # Pick next node.
    transition = _pick_next_from_hint_or_edges(hint, state, action.intent_code)

    # Level descriptor.
    level_desc = describe_level(reward.scheme, reward.new_progress)

    return ResolvedTurn(
        session_id=session.id,
        transition=transition,
        decision=decision,
        intent_code=action.intent_code,
        reward_deltas=dict(reward.deltas),
        level_description_display=level_desc.display,
        level_description_level=level_desc.level,
        mood=new_mood,
        affinity_score=new_aff,
        matched_rule_id=hint.matched_rule_id,
    )