File size: 10,856 Bytes
990895d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c23c1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
990895d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c23c1f
 
 
 
990895d
 
 
 
 
4c23c1f
 
990895d
4c23c1f
 
 
 
990895d
 
 
 
4c23c1f
 
 
 
 
 
 
 
 
 
 
 
 
990895d
 
 
 
 
 
 
 
 
 
4c23c1f
 
 
 
 
 
 
990895d
 
 
 
 
4c23c1f
990895d
 
 
 
4c23c1f
 
 
 
990895d
 
 
 
 
 
 
 
 
 
 
 
 
4c23c1f
990895d
 
 
4c23c1f
 
 
 
 
 
 
 
 
 
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
"""Hardcoded coach backup rules ordered by priority.

Selects the highest-priority matching rule; fills SERVER_PICK when allowed.
"""

from __future__ import annotations

from typing import Any

RULES: list[dict[str, Any]] = [
    {
        "id": "crisis",
        "priority": 100,
        "keywords": ["suicide", "kill myself", "self-harm", "self harm", "end it"],
        "tags": ["crisis"],
        "template": (
            "LOOP: crisis\n"
            "FEELING: overwhelm\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: Contact local emergency / crisis resources now\n"
            "NEXT_BRICK: S\n"
            "DO_NOT: Isolate or escalate alone\n"
            "LINE: Get help before anything else\n"
            "NOTE: This is not therapy; seek real support\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "high_intensity_fse",
        "priority": 95,
        "keywords": [
            "shame",
            "freeze",
            "rerun",
            "fantasy",
            "daydream",
            "movie in my head",
            "stuck indoors",
        ],
        "tags": [
            "shame",
            "freeze",
            "fantasy",
            "indoors",
        ],
        "template": (
            "LOOP: high_fse\n"
            "FEELING: spike\n"
            "INTENSITY_GUESS: high\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: B\n"
            "DO_NOT: Label more or feed the movie with cinematic headphones\n"
            "LINE: Leave the room; one proof step\n"
            "NOTE: Interrupt walk without movie music, or one admin line\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "court",
        "priority": 90,
        "keywords": ["court", "argue with myself", "verdict"],
        "tags": ["court"],
        "template": (
            "LOOP: court\n"
            "FEELING: judgment\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: E\n"
            "DO_NOT: Reopen the case tonight\n"
            "LINE: Close court; one brick only\n"
            "NOTE: Silence after one boundary line\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "rerun",
        "priority": 80,
        "keywords": ["rerun", "replay", "looping the scene"],
        "tags": ["rerun"],
        "template": (
            "LOOP: rerun\n"
            "FEELING: stuck\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: S\n"
            "DO_NOT: Replay the scene again\n"
            "LINE: Interrupt and move body once\n"
            "NOTE: Water or short walk\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "rage_movie",
        "priority": 70,
        "keywords": ["rage", "movie", "fantasy fight"],
        "tags": ["rage", "movie"],
        "template": (
            "LOOP: rage_movie\n"
            "FEELING: heat\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: B\n"
            "DO_NOT: Feed the movie\n"
            "LINE: Name it and stop the reel\n"
            "NOTE: One hygiene or environment act\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "daydream_fc",
        "priority": 60,
        "keywords": ["daydream", "fantasy", "fc"],
        "tags": ["daydream", "fc"],
        "template": (
            "LOOP: daydream\n"
            "FEELING: drift\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: A\n"
            "DO_NOT: Skip today's brick\n"
            "LINE: Brick first, then optional rest\n"
            "NOTE: Daydream only after brick\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "urge_corn",
        "priority": 55,
        "keywords": ["urge", "corn", "delay"],
        "tags": ["urge", "corn"],
        "template": (
            "LOOP: urge\n"
            "FEELING: pull\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: C\n"
            "DO_NOT: Stack sessions\n"
            "LINE: Delay, then one brick\n"
            "NOTE: Ceiling is one session\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "bully",
        "priority": 50,
        "keywords": ["bully", "harsh voice", "attack myself"],
        "tags": ["bully"],
        "template": (
            "LOOP: bully\n"
            "FEELING: harsh\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: E\n"
            "DO_NOT: Argue with the voice\n"
            "LINE: One boundary line then silence\n"
            "NOTE: No pep talk\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "shame",
        "priority": 40,
        "keywords": ["shame", "embarrassed", "humiliated"],
        "tags": ["shame"],
        "template": (
            "LOOP: shame\n"
            "FEELING: shame\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: B\n"
            "DO_NOT: Hide and ruminate\n"
            "LINE: Small body reset, then brick\n"
            "NOTE: Keep it practical\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "spain_admin",
        "priority": 35,
        "keywords": ["admin", "paperwork", "travel", "deadline"],
        "tags": ["admin", "travel"],
        "template": (
            "LOOP: admin\n"
            "FEELING: pressure\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: A\n"
            "DO_NOT: Cancel committed process from fear\n"
            "LINE: One admin checklist item\n"
            "NOTE: Fear is not a cancel signal\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "build_earn",
        "priority": 30,
        "keywords": ["build", "earn", "ship", "work"],
        "tags": ["build", "earn"],
        "template": (
            "LOOP: build\n"
            "FEELING: drive\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: C\n"
            "DO_NOT: Open five fronts\n"
            "LINE: Ship one small unit\n"
            "NOTE: One earn/build act\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "home",
        "priority": 20,
        "keywords": ["home", "mess", "environment"],
        "tags": ["home"],
        "template": (
            "LOOP: home\n"
            "FEELING: clutter\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: B\n"
            "DO_NOT: Redesign everything\n"
            "LINE: One hygiene or tidy act\n"
            "NOTE: Environment first\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "loneliness",
        "priority": 10,
        "keywords": ["lonely", "alone", "isolation"],
        "tags": ["lonely"],
        "template": (
            "LOOP: loneliness\n"
            "FEELING: lonely\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: D\n"
            "DO_NOT: Spiral into absence stories\n"
            "LINE: One logistics line, then brick\n"
            "NOTE: Keep contact practical\n"
            "DATA_THIN: {data_thin}"
        ),
    },
    {
        "id": "default",
        "priority": 0,
        "keywords": [],
        "tags": [],
        "template": (
            "LOOP: default\n"
            "FEELING: mixed\n"
            "INTENSITY_GUESS: n/a\n"
            "REMEDY: {{server_pick}}\n"
            "NEXT_BRICK: A\n"
            "DO_NOT: Overthink the next hour\n"
            "LINE: Do the next small brick\n"
            "NOTE: Checklist over story\n"
            "DATA_THIN: {data_thin}"
        ),
    },
]


DEFAULT_PICK = "Do one listed brick: water, admin item, or tidy act"
INTERRUPT_PICK = (
    "Leave the room, cold water on face, or a 10-min interrupt walk "
    "without cinematic headphones — then one proof line (admin/message)"
)


def select_backup_rule(
    text: str,
    tags: list[str],
    *,
    intensity: int | None = None,
) -> dict[str, Any]:
    """Return the highest-priority rule matching keywords or tags.

    Intensity ≥ 7 forces the high_intensity_fse rule (unless crisis matches).
    """

    haystack = (text or "").lower()
    tag_set = {t.strip().lower() for t in tags if t and t.strip()}
    ordered = sorted(RULES, key=lambda rule: rule["priority"], reverse=True)

    crisis = next((r for r in ordered if r["id"] == "crisis"), None)
    if crisis:
        keywords = crisis.get("keywords") or []
        rule_tags = {t.lower() for t in (crisis.get("tags") or [])}
        if any(keyword in haystack for keyword in keywords) or (tag_set & rule_tags):
            return crisis

    if intensity is not None and intensity >= 7:
        high = next((r for r in ordered if r["id"] == "high_intensity_fse"), None)
        if high:
            return high

    for rule in ordered:
        keywords = rule.get("keywords") or []
        rule_tags = {t.lower() for t in (rule.get("tags") or [])}
        if any(keyword in haystack for keyword in keywords):
            return rule
        if tag_set & rule_tags:
            return rule
    return ordered[-1]


def _sanitize_pick(pick: str, *, force_interrupt: bool) -> str:
    lowered = (pick or "").strip().lower()
    if force_interrupt or "label" in lowered:
        return INTERRUPT_PICK
    return pick or DEFAULT_PICK


def render_backup(
    rule: dict[str, Any],
    *,
    server_picks: list[dict[str, Any]],
    data_thin: bool,
    intensity: int | None = None,
) -> str:
    """Fill the selected rule template with pick and DATA_THIN."""

    pick = server_picks[0]["remedy_key"] if server_picks else DEFAULT_PICK
    force_interrupt = rule["id"] == "high_intensity_fse" or (
        intensity is not None and intensity >= 7
    )
    pick = _sanitize_pick(pick, force_interrupt=force_interrupt)
    if rule["id"] == "crisis":
        text = rule["template"]
    else:
        text = rule["template"].replace("{{server_pick}}", pick)
    return text.format(data_thin=str(data_thin).lower())


def backup_reply(
    text: str,
    tags: list[str],
    server_picks: list[dict[str, Any]],
    *,
    data_thin: bool,
    intensity: int | None = None,
) -> tuple[str, str]:
    """Select and render a backup reply; return (text, rule_id)."""

    rule = select_backup_rule(text, tags, intensity=intensity)
    return (
        render_backup(
            rule,
            server_picks=server_picks,
            data_thin=data_thin,
            intensity=intensity,
        ),
        rule["id"],
    )