File size: 13,118 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
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
"""
Pydantic data contracts for the interactive service.

One model per concept β€” read-side rows returned by the repo and
write-side create/update payloads used by the router. Field names
map 1:1 to the column names in ``store.py`` so downstream
conversions stay mechanical.

All models allow extra fields on read (``model_config`` below) so
future batches can add columns without breaking existing clients.
"""
from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional

from pydantic import BaseModel, ConfigDict, Field


# Shared config β€” be permissive on input/output so adding fields
# later never 422s a caller.
_PERMISSIVE = ConfigDict(extra="allow", populate_by_name=True)


# ─────────────────────────────────────────────────────────────────
# Enumerations (stable string literals)
# ─────────────────────────────────────────────────────────────────

ExperienceMode = Literal[
    "sfw_general",
    "sfw_education",
    "language_learning",
    "enterprise_training",
    "social_romantic",
    "mature_gated",
]

ExperienceStatus = Literal[
    "draft", "planning", "building", "ready", "published", "archived",
]

NodeKind = Literal["scene", "decision", "merge", "ending", "assessment", "remediation"]

EdgeTriggerKind = Literal["choice", "hotspot", "timer", "auto", "fallback", "intent"]

TurnRole = Literal["user", "assistant", "system"]

EventKind = Literal[
    "start", "enter_node", "choose", "hotspot", "drop_off",
    "complete", "policy_block", "error",
]

ProgressionScheme = Literal[
    "xp_level", "mastery", "cefr", "affinity_tier", "certification",
]

# PublishChannel spans every channel any interactive publisher can
# emit today. Kept as a broad union (rather than an enum) so new
# delivery targets land as additive string additions β€” the publisher
# + frontend picker enforce which subset is actively wired at runtime.
PublishChannel = Literal[
    "web_embed", "studio_preview", "export",
    "lms", "kiosk", "api",
]


# ─────────────────────────────────────────────────────────────────
# Experience
# ─────────────────────────────────────────────────────────────────

class ExperienceCreate(BaseModel):
    model_config = _PERMISSIVE
    title: str
    description: str = ""
    objective: str = ""
    experience_mode: ExperienceMode = "sfw_general"
    policy_profile_id: str = "sfw_general"
    audience_profile: Dict[str, Any] = Field(default_factory=dict)
    project_type: str = "standard"
    studio_video_id: Optional[str] = None
    tags: List[str] = Field(default_factory=list)


class ExperienceUpdate(BaseModel):
    model_config = _PERMISSIVE
    title: Optional[str] = None
    description: Optional[str] = None
    objective: Optional[str] = None
    experience_mode: Optional[ExperienceMode] = None
    policy_profile_id: Optional[str] = None
    audience_profile: Optional[Dict[str, Any]] = None
    project_type: Optional[str] = None
    status: Optional[ExperienceStatus] = None
    tags: Optional[List[str]] = None


class Experience(BaseModel):
    model_config = _PERMISSIVE
    id: str
    user_id: str
    studio_video_id: str = ""
    title: str
    description: str = ""
    objective: str = ""
    experience_mode: ExperienceMode = "sfw_general"
    policy_profile_id: str = "sfw_general"
    audience_profile: Dict[str, Any] = Field(default_factory=dict)
    project_type: str = "standard"
    branch_count: int = 0
    max_depth: int = 0
    status: ExperienceStatus = "draft"
    tags: List[str] = Field(default_factory=list)
    created_at: Optional[str] = None
    updated_at: Optional[str] = None


# ─────────────────────────────────────────────────────────────────
# Graph β€” nodes + edges
# ─────────────────────────────────────────────────────────────────

class NodeCreate(BaseModel):
    model_config = _PERMISSIVE
    kind: NodeKind = "scene"
    title: str = ""
    narration: str = ""
    image_prompt: str = ""
    video_prompt: str = ""
    duration_sec: int = 5
    storyboard: Dict[str, Any] = Field(default_factory=dict)
    interaction_layout: Dict[str, Any] = Field(default_factory=dict)
    asset_ids: List[str] = Field(default_factory=list)


class NodeUpdate(BaseModel):
    model_config = _PERMISSIVE
    kind: Optional[NodeKind] = None
    title: Optional[str] = None
    narration: Optional[str] = None
    image_prompt: Optional[str] = None
    video_prompt: Optional[str] = None
    duration_sec: Optional[int] = None
    storyboard: Optional[Dict[str, Any]] = None
    interaction_layout: Optional[Dict[str, Any]] = None
    asset_ids: Optional[List[str]] = None


class Node(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    kind: NodeKind
    title: str = ""
    narration: str = ""
    image_prompt: str = ""
    video_prompt: str = ""
    duration_sec: int = 5
    storyboard: Dict[str, Any] = Field(default_factory=dict)
    interaction_layout: Dict[str, Any] = Field(default_factory=dict)
    asset_ids: List[str] = Field(default_factory=list)
    created_at: Optional[str] = None
    updated_at: Optional[str] = None


class EdgeCreate(BaseModel):
    model_config = _PERMISSIVE
    from_node_id: str
    to_node_id: str
    trigger_kind: EdgeTriggerKind
    trigger_payload: Dict[str, Any] = Field(default_factory=dict)
    ordinal: int = 0


class Edge(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    from_node_id: str
    to_node_id: str
    trigger_kind: EdgeTriggerKind
    trigger_payload: Dict[str, Any] = Field(default_factory=dict)
    ordinal: int = 0
    created_at: Optional[str] = None


class NodeVariant(BaseModel):
    model_config = _PERMISSIVE
    id: str
    node_id: str
    language: str
    narration: str = ""
    subtitles: str = ""
    audio_asset_id: str = ""
    video_asset_id: str = ""


# ─────────────────────────────────────────────────────────────────
# Runtime β€” sessions / turns / events
# ─────────────────────────────────────────────────────────────────

class Session(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    viewer_ref: str = ""
    current_node_id: str = ""
    language: str = "en"
    personalization: Dict[str, Any] = Field(default_factory=dict)
    consent_version: str = ""
    started_at: Optional[str] = None
    last_event_at: Optional[str] = None
    completed_at: Optional[str] = None


class SessionTurn(BaseModel):
    model_config = _PERMISSIVE
    id: str
    session_id: str
    turn_role: TurnRole
    text: str
    action_id: str = ""
    node_id: str = ""
    created_at: Optional[str] = None


class SessionEvent(BaseModel):
    model_config = _PERMISSIVE
    id: str
    session_id: str
    ts: Optional[str] = None
    event_kind: EventKind
    node_id: str = ""
    edge_id: str = ""
    action_id: str = ""
    payload: Dict[str, Any] = Field(default_factory=dict)


# ─────────────────────────────────────────────────────────────────
# Character + assets
# ─────────────────────────────────────────────────────────────────

class CharacterState(BaseModel):
    model_config = _PERMISSIVE
    id: str
    session_id: str
    persona_id: str = ""
    mood: str = "neutral"
    affinity_score: float = 0.5
    outfit_state: Dict[str, Any] = Field(default_factory=dict)
    recent_flags: List[str] = Field(default_factory=list)
    language: str = "en"
    updated_at: Optional[str] = None


class CharacterAsset(BaseModel):
    model_config = _PERMISSIVE
    id: str
    persona_id: str
    asset_id: str
    kind: str
    mood_tags: List[str] = Field(default_factory=list)
    action_tags: List[str] = Field(default_factory=list)
    language: str = ""
    outfit_tags: List[str] = Field(default_factory=list)
    duration_sec: float = 0.0
    intensity: float = 0.5
    created_at: Optional[str] = None


# ─────────────────────────────────────────────────────────────────
# Actions + progression
# ─────────────────────────────────────────────────────────────────

class ActionCreate(BaseModel):
    model_config = _PERMISSIVE
    label: str
    intent_code: str = ""
    required_level: int = 1
    required_scheme: str = "xp_level"
    required_metric_key: str = "level"
    policy_scope: List[str] = Field(default_factory=list)
    cooldown_sec: int = 0
    mood_delta: Dict[str, Any] = Field(default_factory=dict)
    xp_award: int = 0
    max_uses_per_session: int = 0
    repeat_penalty: float = 0.0
    requires_consent: str = ""
    applicable_modes: List[str] = Field(default_factory=list)
    category: str = "expression"
    edit_recipe: Dict[str, Any] = Field(default_factory=dict)
    ordinal: int = 0


class Action(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    label: str
    intent_code: str = ""
    required_level: int = 1
    required_scheme: str = "xp_level"
    required_metric_key: str = "level"
    policy_scope: List[str] = Field(default_factory=list)
    cooldown_sec: int = 0
    mood_delta: Dict[str, Any] = Field(default_factory=dict)
    xp_award: int = 0
    max_uses_per_session: int = 0
    repeat_penalty: float = 0.0
    requires_consent: str = ""
    applicable_modes: List[str] = Field(default_factory=list)
    category: str = "expression"
    edit_recipe: Dict[str, Any] = Field(default_factory=dict)
    ordinal: int = 0
    created_at: Optional[str] = None


class ProgressMetric(BaseModel):
    model_config = _PERMISSIVE
    id: str
    session_id: str
    scheme: ProgressionScheme
    metric_key: str
    metric_value: float
    updated_at: Optional[str] = None


# ─────────────────────────────────────────────────────────────────
# Personalization + intent map
# ─────────────────────────────────────────────────────────────────

class PersonalizationRule(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    name: str
    condition: Dict[str, Any] = Field(default_factory=dict)
    action: Dict[str, Any] = Field(default_factory=dict)
    priority: int = 100
    enabled: bool = True
    created_at: Optional[str] = None


class IntentMapEntry(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    intent_code: str
    action_id: str = ""
    fallback_node_id: str = ""
    priority: int = 100
    applicable_modes: List[str] = Field(default_factory=list)


# ─────────────────────────────────────────────────────────────────
# Publishing + QA
# ─────────────────────────────────────────────────────────────────

class Publication(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    channel: PublishChannel
    manifest_url: str = ""
    version: int = 1
    metadata: Dict[str, Any] = Field(default_factory=dict)
    published_at: Optional[str] = None


class QAReport(BaseModel):
    model_config = _PERMISSIVE
    id: str
    experience_id: str
    kind: str  # 'walk' | 'lint' | 'content_check'
    summary: Dict[str, Any] = Field(default_factory=dict)
    issues: List[Dict[str, Any]] = Field(default_factory=list)
    created_at: Optional[str] = None