-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathmain.py
More file actions
413 lines (351 loc) · 14.5 KB
/
main.py
File metadata and controls
413 lines (351 loc) · 14.5 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
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
import json
import os
import re
from typing import Any
from src.agent.capability import MatchingCapability
from src.agent.capability_worker import CapabilityWorker
from src.main import AgentWorker
DEFAULT_NUM_QUESTIONS = 3
MAX_NUM_QUESTIONS = 10
EXIT_WORDS = {"stop", "exit", "quit", "cancel", "done", "bye", "goodbye"}
BEST_SCORE_FILE = "vibe_trivia_best_score.json"
INTRO = (
"Welcome to Vibe Trivia. Pick a category like movies, science, or history. "
"Or say random."
)
ASK_NUM_QUESTIONS = (
f"How many questions would you like? You can say a number from 1 to {MAX_NUM_QUESTIONS}."
)
CONFIRM_START = "Great. We'll do {num} questions on {cat}. Ready to start?"
GENERATE_QUESTIONS_PROMPT = (
"Generate {num} multiple-choice trivia questions about '{cat}'. "
"Difficulty: medium. "
"Return ONLY valid JSON (no markdown). "
"Return a JSON array of objects. Each object MUST have:\n"
"- question: string\n"
"- choices: array of 4 strings (A, B, C, D choices, but do NOT prefix with 'A)' etc)\n"
"- correct_answer: one of 'A','B','C','D'\n"
)
ANSWER_JUDGE_PROMPT = (
"You are grading a trivia answer.\n"
"Question: {question}\n"
"Choices:\n"
"A: {a}\n"
"B: {b}\n"
"C: {c}\n"
"D: {d}\n"
"Correct letter: {correct_letter}\n"
"User answer: {user_answer}\n\n"
"Is the user's answer correct? Respond with ONLY 'yes' or 'no'."
)
class VibeTriviaCapability(MatchingCapability):
model_config = {"extra": "allow", "arbitrary_types_allowed": True}
worker: AgentWorker | None = None
capability_worker: CapabilityWorker | None = None
initial_request: str | None = None
hotwords: list[str] = ["start vibe trivia", "vibe trivia", "trivia time", "quiz me", "play trivia", "start a quiz"]
#{{register_capability}}
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
if self.matching_hotwords:
self.hotwords = list(self.matching_hotwords)
self.initial_request = None
try:
self.initial_request = worker.transcription
except Exception:
pass
if not self.initial_request:
try:
self.initial_request = worker.last_transcription
except Exception:
pass
if not self.initial_request:
try:
self.initial_request = worker.current_transcription
except Exception:
pass
self.worker.session_tasks.create(self.run())
def _log_info(self, msg: str):
if self.worker:
self.worker.editor_logging_handler.info(msg)
def _log_error(self, msg: str):
if self.worker:
self.worker.editor_logging_handler.error(msg)
def _is_exit(self, text: str | None) -> bool:
if not text:
return False
lowered = text.lower()
return any(w in lowered for w in EXIT_WORDS)
def _extract_first_int(self, text: str) -> int | None:
m = re.search(r"\b(\d+)\b", text)
if not m:
return None
try:
return int(m.group(1))
except Exception:
return None
def _clean_json(self, raw: str) -> str:
cleaned = raw.strip()
cleaned = cleaned.replace("```json", "").replace("```", "").strip()
start = cleaned.find("[")
end = cleaned.rfind("]")
if start != -1 and end != -1 and end > start:
return cleaned[start:end + 1]
return cleaned
def _validate_questions(self, data: Any) -> list[dict]:
if not isinstance(data, list):
return []
validated: list[dict] = []
for item in data:
if not isinstance(item, dict):
continue
q = item.get("question")
choices = item.get("choices")
correct = item.get("correct_answer")
if not isinstance(q, str) or not q.strip():
continue
if not isinstance(choices, list) or len(choices) != 4:
continue
if not all(isinstance(c, str) and c.strip() for c in choices):
continue
if not isinstance(correct, str):
continue
correct_letter = correct.strip().upper()
if correct_letter not in {"A", "B", "C", "D"}:
continue
validated.append(
{
"question": q.strip(),
"choices": [c.strip() for c in choices],
"correct_answer": correct_letter,
}
)
return validated
def _extract_letter(self, text: str) -> str | None:
m = re.search(r"\b([ABCD])\b", text.upper())
if m:
return m.group(1)
return None
def _normalize_for_match(self, text: str) -> str:
lowered = text.lower()
lowered = re.sub(r"[^a-z0-9\s]", " ", lowered)
lowered = re.sub(r"\s+", " ", lowered).strip()
return lowered
def _guess_choice_by_text(self, user_answer: str, choices: list[str]) -> str | None:
ua = self._normalize_for_match(user_answer)
if not ua:
return None
normalized_choices = [self._normalize_for_match(c) for c in choices]
for idx, c in enumerate(normalized_choices):
if not c:
continue
if ua == c or ua in c or c in ua:
return "ABCD"[idx]
return None
def _looks_like_trigger_echo(self, text: str) -> bool:
lowered = text.lower().strip()
if not lowered:
return False
if self.initial_request and lowered == self.initial_request.lower().strip():
return True
if any(hw and hw in lowered for hw in self.hotwords):
return True
return False
async def _listen_nonempty(
self, prompt: str, retries: int = 2, exit_ok: bool = True
) -> str | None:
assert self.capability_worker is not None
if self.worker:
await self.worker.session_tasks.sleep(0.2)
current_prompt = prompt
for attempt in range(retries + 1):
text = await self.capability_worker.run_io_loop(current_prompt)
if exit_ok and self._is_exit(text):
return None
if text and text.strip() and not self._looks_like_trigger_echo(text):
return text.strip()
if text and text.strip() and self._looks_like_trigger_echo(text):
self._log_info("[VibeTrivia] Ignoring trigger-echo transcription")
self._log_info(
f"[VibeTrivia] Empty/invalid response (attempt {attempt + 1}/{retries + 1})"
)
if attempt < retries:
current_prompt = "I didn't catch that. Please say it again."
else:
return None
async def _read_best(self) -> tuple[int, int] | None:
if not self.capability_worker:
return None
try:
exists = await self.capability_worker.check_if_file_exists(
BEST_SCORE_FILE, False
)
if not exists:
return None
raw = await self.capability_worker.read_file(BEST_SCORE_FILE, False)
data = json.loads(raw) if raw else {}
best_correct = int(data.get("best_correct", 0))
best_total = int(data.get("best_total", 0))
if best_total <= 0:
return None
return best_correct, best_total
except Exception as e:
self._log_error(f"[VibeTrivia] Failed to read best score: {e}")
return None
async def _write_best(self, best_correct: int, best_total: int):
if not self.capability_worker:
return
try:
payload = json.dumps(
{"best_correct": best_correct, "best_total": best_total}, indent=2
)
await self.capability_worker.write_file(BEST_SCORE_FILE, payload, False)
except Exception as e:
self._log_error(f"[VibeTrivia] Failed to write best score: {e}")
async def _ask_num_questions(self) -> int | None:
assert self.capability_worker is not None
user_input = await self._listen_nonempty(ASK_NUM_QUESTIONS, retries=1)
if user_input is None:
await self.capability_worker.speak("Okay, exiting trivia.")
return None
n = self._extract_first_int(user_input or "")
if n is None:
await self.capability_worker.speak(
f"No worries. We'll do {DEFAULT_NUM_QUESTIONS}."
)
return DEFAULT_NUM_QUESTIONS
n = max(1, min(MAX_NUM_QUESTIONS, n))
return n
async def _generate_questions(self, num: int, category: str) -> list[dict] | None:
assert self.capability_worker is not None
last_raw: str | None = None
for attempt in range(1, 4):
try:
prompt = GENERATE_QUESTIONS_PROMPT.format(num=num, cat=category)
raw = self.capability_worker.text_to_text_response(prompt)
last_raw = raw
cleaned = self._clean_json(raw)
parsed = json.loads(cleaned)
questions = self._validate_questions(parsed)
if len(questions) >= num:
return questions[:num]
raise ValueError("Not enough validated questions")
except Exception as e:
self._log_error(
f"[VibeTrivia] Question generation attempt {attempt} failed: {e}"
)
if attempt < 3:
await self.capability_worker.speak(
"Hang on—I had trouble generating questions. Let me try again."
)
else:
if last_raw:
self._log_error(
f"[VibeTrivia] Last raw generation output: {last_raw[:500]}"
)
return None
async def _ask_one(self, idx: int, total: int, q: dict) -> bool | None:
assert self.capability_worker is not None
question = q["question"]
choices: list[str] = q["choices"]
correct_letter: str = q["correct_answer"]
prompt = (
f"Question {idx} of {total}. {question}. "
f"A: {choices[0]}. "
f"B: {choices[1]}. "
f"C: {choices[2]}. "
f"D: {choices[3]}."
)
user_answer = await self._listen_nonempty(prompt, retries=1)
if user_answer is None:
return None
letter = self._extract_letter(user_answer or "")
if not letter:
letter = self._guess_choice_by_text(user_answer or "", choices)
if letter:
return letter == correct_letter
try:
judge_prompt = ANSWER_JUDGE_PROMPT.format(
question=question,
a=choices[0],
b=choices[1],
c=choices[2],
d=choices[3],
correct_letter=correct_letter,
user_answer=user_answer,
)
result = self.capability_worker.text_to_text_response(judge_prompt)
return "yes" in (result or "").lower()
except Exception as e:
self._log_error(f"[VibeTrivia] Judge failed: {e}")
return False
async def run(self):
try:
if not self.capability_worker:
return
self._log_info("[VibeTrivia] Ability started")
await self.capability_worker.speak("Vibe Trivia activated.")
category_input = await self._listen_nonempty(INTRO, retries=2)
if category_input is None:
await self.capability_worker.speak("Okay, exiting trivia.")
return
category = (category_input or "").strip()
if not category:
category = "random"
if category.lower() == "random":
category = "general knowledge"
num = await self._ask_num_questions()
if num is None:
return
confirmed = await self.capability_worker.run_confirmation_loop(
CONFIRM_START.format(num=num, cat=category)
)
if not confirmed:
await self.capability_worker.speak("No problem. Come back anytime.")
return
await self.capability_worker.speak("Awesome. Here we go.")
questions = await self._generate_questions(num=num, category=category)
if not questions:
await self.capability_worker.speak(
"Sorry, I couldn't generate a quiz right now. Try again in a bit."
)
return
score = 0
for i, q in enumerate(questions, start=1):
is_correct = await self._ask_one(i, num, q)
if is_correct is None:
await self.capability_worker.speak("All good. Ending the quiz.")
return
if is_correct:
score += 1
await self.capability_worker.speak("Correct.")
else:
await self.capability_worker.speak("Not quite.")
await self.capability_worker.speak(
f"Final score: {score} out of {num}. Thanks for playing!"
)
previous = await self._read_best()
if previous is None:
await self._write_best(score, num)
await self.capability_worker.speak("That's your first recorded score. Nice.")
return
prev_correct, prev_total = previous
prev_pct = prev_correct / prev_total if prev_total else 0.0
pct = score / num if num else 0.0
if (pct > prev_pct) or (pct == prev_pct and score > prev_correct):
await self._write_best(score, num)
await self.capability_worker.speak("New best score!")
else:
await self.capability_worker.speak(
f"Your best so far is {prev_correct} out of {prev_total}."
)
except Exception as e:
self._log_error(f"[VibeTrivia] Unexpected error: {e}")
if self.capability_worker:
await self.capability_worker.speak(
"Sorry—something went wrong. Exiting trivia."
)
finally:
if self.capability_worker:
self.capability_worker.resume_normal_flow()