-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetective_client.py
More file actions
364 lines (289 loc) · 13.2 KB
/
Copy pathdetective_client.py
File metadata and controls
364 lines (289 loc) · 13.2 KB
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
"""
Case Closed — Python client library.
Load your team token from .env and go:
from detective_client import DetectiveClient
client = DetectiveClient() # reads DETECTIVE_TOKEN from .env
session = client.start_session("S001")
obs = client.move("loc_office")
obs = client.search()
obs = client.interview("char_finn")
obs = client.present("char_mara", ref_id="item_ledger")
result = client.commit(
culprit_id="char_silas",
means_id="means_blade",
evidence_notes=[
{"clue_id": "clue_04", "note": "Silas signed the midnight delivery himself, placing him at the warehouse."},
{"clue_id": "clue_S2", "note": "He confessed when confronted with the ledger and the knife."},
],
process_explanation="Silas arranged the cargo run to lure Marsh, then stabbed him when Marsh threatened to expose the operation.",
)
"""
from __future__ import annotations
import os
import requests
from pydantic import BaseModel, Field
class GameError(Exception):
"""Raised when a game action is rejected (wrong location, bad ref_id, etc.)."""
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass # python-dotenv optional; set env vars manually if not installed
# ── Shared leaf models ────────────────────────────────────────────────────────
class ClueText(BaseModel):
"""A single clue ID paired with its text."""
clue_id: str
text: str
# ── Session start ─────────────────────────────────────────────────────────────
class LocationInfo(BaseModel):
id: str
name: str
class CastMember(BaseModel):
id: str
name: str
class MeansOption(BaseModel):
id: str
name: str
class TimelineStepBrief(BaseModel):
step_id: str
claim: str
class Briefing(BaseModel):
synopsis: str
locations: list[LocationInfo]
cast: list[CastMember]
means_options: list[MeansOption]
timeline_steps: list[TimelineStepBrief] = []
class LocationObservation(BaseModel):
location_id: str
name: str
description: str
characters_present: list[str]
exits: list[str]
searchable: bool
class SessionInfo(BaseModel):
seed_id: str
briefing: Briefing
start_location: str
action_budget: int
actions_remaining: int
session_id: str
observation: LocationObservation
# ── State ──────────────────────────────────────────────────────────────────────
class GameState(BaseModel):
current_location: str
actions_remaining: int
# ── Action observations ────────────────────────────────────────────────────────
class MoveObservation(BaseModel):
ok: bool = True
location_id: str
name: str
description: str
characters_present: list[str]
exits: list[str]
searchable: bool
actions_remaining: int
class EvidenceFound(BaseModel):
evidence_id: str
name: str
yields: list[ClueText]
is_item: bool
item_id: str | None = None
class SearchObservation(BaseModel):
ok: bool = True
found: list[EvidenceFound]
actions_remaining: int
class InterviewObservation(BaseModel):
ok: bool = True
character_id: str
name: str
said: list[ClueText]
actions_remaining: int
class PresentObservation(BaseModel):
ok: bool = True
accepted: bool
revealed: list[ClueText]
actions_remaining: int
# ── Commit ─────────────────────────────────────────────────────────────────────
class CommitResult(BaseModel):
ok: bool = True
submission_id: str
actions_remaining: int
process_explanation: str
accepted: bool = False
# Practice seeds return all fields; scored seeds return only accepted=True until reveal:
score: float | None = None
culprit_correct: bool | None = None
means_correct: bool | None = None
notes_score: float | None = None
explanation_score: float | None = None
notes_correct: int | None = None
notes_wrong: int | None = None
notes_missing: int | None = None
efficiency_score: float | None = None
actions_used: int | None = None
# ── Seeds ──────────────────────────────────────────────────────────────────────
class SeedInfo(BaseModel):
seed_id: str
visibility: str
action_budget: int
class SeedsResponse(BaseModel):
seeds: list[SeedInfo]
# ── Client ─────────────────────────────────────────────────────────────────────
class DetectiveClient:
"""Typed HTTP client for the Case Closed detective API.
Args:
token: Bearer token for your team. If omitted, reads DETECTIVE_TOKEN from env.
base_url: API base URL. If omitted, reads DETECTIVE_BASE_URL from env or
defaults to http://localhost:8000.
"""
def __init__(
self,
token: str | None = None,
base_url: str | None = None,
) -> None:
self.token = token or os.environ["DETECTIVE_TOKEN"]
self.base_url = (base_url or os.environ.get("DETECTIVE_BASE_URL", "https://railtracks-case-closed.azurewebsites.net/")).rstrip("/")
self.session_id: str | None = None
# Client-side knowledge tracking — updated from each action response
self.clues_seen: set[str] = set()
self.inventory: set[str] = set()
self.actions_remaining: int = 0
self.current_location: str = ""
# ── Internal HTTP ──────────────────────────────────────────────────────────
def _headers(self) -> dict[str, str]:
return {"Authorization": f"Bearer {self.token}"}
def _get(self, path: str) -> dict:
r = requests.get(f"{self.base_url}{path}", headers=self._headers())
self._raise(r)
return r.json()
def _post(self, path: str, body: dict) -> dict:
r = requests.post(f"{self.base_url}{path}", json=body, headers=self._headers())
self._raise(r)
return r.json()
@staticmethod
def _raise(r: requests.Response) -> None:
if r.ok:
return
try:
detail = r.json().get("detail", r.text)
except Exception:
detail = r.text
raise requests.exceptions.HTTPError(
f"HTTP {r.status_code}: {detail}", response=r
)
def _act(self, verb: str, args: dict) -> dict:
if not self.session_id:
raise RuntimeError("No active session — call start_session() first")
r = requests.post(
f"{self.base_url}/sessions/{self.session_id}/act",
json={"verb": verb, "args": args},
headers=self._headers(),
)
if r.status_code == 400:
try:
detail = r.json().get("detail", r.text)
except Exception:
detail = r.text
raise GameError(detail)
self._raise(r)
return r.json()
# ── Session lifecycle ──────────────────────────────────────────────────────
def start_session(self, seed_id: str) -> SessionInfo:
"""Start a new investigation session. Resets all client-side tracking."""
data = self._post("/sessions", {"seed_id": seed_id})
result = SessionInfo.model_validate(data)
self.session_id = result.session_id
self.actions_remaining = result.actions_remaining
self.current_location = result.start_location
self.clues_seen = set()
self.inventory = set()
return result
def get_state(self) -> GameState:
"""Fetch authoritative state from the server (location + actions remaining)."""
if not self.session_id:
raise RuntimeError("No active session")
data = self._get(f"/sessions/{self.session_id}/state")
gs = GameState.model_validate(data)
self.actions_remaining = gs.actions_remaining
self.current_location = gs.current_location
return gs
# ── Actions ────────────────────────────────────────────────────────────────
def move(self, location_id: str) -> MoveObservation:
"""Move to an adjacent location. Costs 1 action."""
data = self._act("move", {"location_id": location_id})
result = MoveObservation.model_validate(data)
self.actions_remaining = result.actions_remaining
self.current_location = result.location_id
return result
def search(self) -> SearchObservation:
"""Search the current location for evidence and items. Costs 1 action."""
data = self._act("search", {})
result = SearchObservation.model_validate(data)
self.actions_remaining = result.actions_remaining
for ev in result.found:
for clue in ev.yields:
self.clues_seen.add(clue.clue_id)
if ev.item_id:
self.inventory.add(ev.item_id)
return result
def interview(self, character_id: str) -> InterviewObservation:
"""Talk to a character at the current location. Returns their freely offered clues.
Repeating costs an action but gives the same response. Costs 1 action."""
data = self._act("interview", {"character_id": character_id})
result = InterviewObservation.model_validate(data)
self.actions_remaining = result.actions_remaining
for clue in result.said:
self.clues_seen.add(clue.clue_id)
return result
def present(self, character_id: str, ref_id: str) -> PresentObservation:
"""Present an item or clue to a character to unlock their guarded testimony.
ref_id is either an item_id (from inventory) or a clue_id (from clues_seen).
The server determines which type based on the character's unlock conditions.
Costs 1 action.
"""
data = self._act("present", {"character_id": character_id, "ref_id": ref_id})
result = PresentObservation.model_validate(data)
self.actions_remaining = result.actions_remaining
for clue in result.revealed:
self.clues_seen.add(clue.clue_id)
return result
def commit(
self,
culprit_id: str,
means_id: str,
evidence_notes: list[dict],
process_explanation: str,
) -> CommitResult:
"""Submit your final answer. Irreversible on hidden seeds.
Args:
culprit_id: Exact character ID of the killer (e.g. 'char_silas').
means_id: Exact means ID (e.g. 'means_blade').
evidence_notes: One note per clue you found. Each element must be a dict
with exactly two keys: 'clue_id' and 'note'. The note should explain
what the clue proves in context (~120 words). Submit notes for every
clue collected — non-key clues are silently ignored; notes on clue IDs
you never found, or duplicates, count as wrong and are penalised.
process_explanation: Short holistic narrative — who did it, how, and why.
No clue IDs. Target ~120 words.
"""
args = {
"culprit_id": culprit_id,
"means_id": means_id,
"evidence_notes": evidence_notes,
"process_explanation": process_explanation,
}
data = self._act("commit", args)
return CommitResult.model_validate(data)
# ── Meta ───────────────────────────────────────────────────────────────────
def list_seeds(self) -> SeedsResponse:
"""List all available seeds and their visibility."""
return SeedsResponse.model_validate(self._get("/seeds"))
# ── Submission helpers ─────────────────────────────────────────────────────
def knowledge_summary(self) -> str:
"""Return a human-readable summary of current investigation state."""
return (
f"Location : {self.current_location}\n"
f"Actions remaining : {self.actions_remaining}\n"
f"Clues collected : {sorted(self.clues_seen) or 'none'}\n"
f"Items in inventory: {sorted(self.inventory) or 'none'}"
)