-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathmain.py
More file actions
367 lines (314 loc) · 14.1 KB
/
main.py
File metadata and controls
367 lines (314 loc) · 14.1 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
import json
import os
import requests
from src.agent.capability import MatchingCapability
from src.agent.capability_worker import CapabilityWorker
from src.main import AgentWorker
# ── Default location (New York) — user can change via voice ──────────
DEFAULT_LAT = 40.71
DEFAULT_LON = -74.01
DEFAULT_CITY = "New York"
# ── 3rd-party API keys (all free, no keys required) ─────────────────
# Uncomment and set these if you switch to a key-based provider:
# WEATHER_API_KEY = "your-weather-api-key-here"
# QUOTE_API_KEY = "your-quote-api-key-here"
# FACT_API_KEY = "your-fact-api-key-here"
# ── Free API endpoints (no keys needed) ──────────────────────────────
WEATHER_URL = "https://api.open-meteo.com/v1/forecast"
QUOTE_URL = "https://zenquotes.io/api/today"
FACT_URL = "https://uselessfacts.jsph.pl/api/v2/facts/today"
# ── WMO weather codes → plain English ────────────────────────────────
WEATHER_DESCRIPTIONS = {
0: "clear skies",
1: "mostly clear",
2: "partly cloudy",
3: "overcast",
45: "foggy",
48: "foggy",
51: "light drizzle",
53: "drizzle",
55: "heavy drizzle",
61: "light rain",
63: "rain",
65: "heavy rain",
71: "light snow",
73: "snow",
75: "heavy snow",
80: "rain showers",
81: "rain showers",
82: "heavy rain showers",
85: "snow showers",
86: "heavy snow showers",
95: "thunderstorms",
96: "thunderstorms with hail",
99: "thunderstorms with heavy hail",
}
# ── Well-known cities for quick location matching ────────────────────
CITY_COORDS = {
"new york": (40.71, -74.01),
"los angeles": (34.05, -118.24),
"chicago": (41.88, -87.63),
"houston": (29.76, -95.37),
"phoenix": (33.45, -112.07),
"san francisco": (37.77, -122.42),
"seattle": (47.61, -122.33),
"miami": (25.76, -80.19),
"boston": (42.36, -71.06),
"denver": (39.74, -104.98),
"austin": (30.27, -97.74),
"dallas": (32.78, -96.80),
"atlanta": (33.75, -84.39),
"london": (51.51, -0.13),
"paris": (48.86, 2.35),
"tokyo": (35.68, 139.69),
"sydney": (-33.87, 151.21),
"toronto": (43.65, -79.38),
"dubai": (25.20, 55.27),
"singapore": (1.35, 103.82),
"berlin": (52.52, 13.41),
"mumbai": (19.08, 72.88),
"cairo": (30.04, 31.24),
"rome": (41.90, 12.50),
"istanbul": (41.01, 28.98),
"lahore": (31.55, 74.35),
"karachi": (24.86, 67.01),
"islamabad": (33.69, 73.04),
}
# ── Briefing LLM prompt ─────────────────────────────────────────────
BRIEFING_PROMPT = (
"You are a friendly morning radio host. Weave the following data into a "
"natural, conversational 3-sentence morning briefing. Be warm and brief. "
"Do NOT use bullet points or labels. Just talk like a person giving a "
"quick morning update.\n\n"
"Weather: {weather}\n"
"Quote of the day: \"{quote}\" — {author}\n"
"Fun fact: {fact}\n\n"
"Start with 'Good morning!' and keep the whole thing under 50 words."
)
DETAIL_PROMPT = (
"The user wants to know more about: {topic}. "
"Here is the raw data:\n{data}\n\n"
"Give a 2-sentence conversational expansion. Keep it voice-friendly."
)
class DailyBriefingCapability(MatchingCapability):
model_config = {"extra": "allow", "arbitrary_types_allowed": True}
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
#{{register_capability}}
# ── API helpers ──────────────────────────────────────────────────
def fetch_weather(self, lat, lon):
try:
resp = requests.get(
WEATHER_URL,
params={
"latitude": lat,
"longitude": lon,
"current": "temperature_2m,weathercode",
"temperature_unit": "fahrenheit",
"timezone": "auto",
},
timeout=10,
)
if resp.status_code == 200:
data = resp.json()
current = data.get("current", {})
temp = current.get("temperature_2m")
code = current.get("weathercode", 0)
desc = WEATHER_DESCRIPTIONS.get(code, "mixed conditions")
return {
"temp": temp,
"description": desc,
"raw": f"{temp} degrees Fahrenheit with {desc}",
}
except Exception as e:
self.worker.editor_logging_handler.error(f"Weather API error: {e}")
return None
def fetch_quote(self):
try:
resp = requests.get(QUOTE_URL, timeout=10)
if resp.status_code == 200:
data = resp.json()
if data and len(data) > 0:
return {
"quote": data[0].get("q", ""),
"author": data[0].get("a", "Unknown"),
}
except Exception as e:
self.worker.editor_logging_handler.error(f"Quote API error: {e}")
return None
def fetch_fun_fact(self):
try:
resp = requests.get(FACT_URL, timeout=10)
if resp.status_code == 200:
data = resp.json()
return {"fact": data.get("text", "")}
except Exception as e:
self.worker.editor_logging_handler.error(f"Fact API error: {e}")
return None
# ── Location helper ──────────────────────────────────────────────
def resolve_city(self, user_input):
text = user_input.lower().strip()
for city, (lat, lon) in CITY_COORDS.items():
if city in text:
return city.title(), lat, lon
# LLM fallback to extract city name
try:
prompt = (
f"The user said: '{user_input}'. "
"Extract ONLY the city name. Return ONLY the city name, nothing else. "
"If no city is mentioned, return 'none'."
)
result = self.capability_worker.text_to_text_response(prompt).strip().lower()
if result and result != "none":
for city, (lat, lon) in CITY_COORDS.items():
if city in result or result in city:
return city.title(), lat, lon
except Exception:
pass
return None, None, None
# ── Command detection ────────────────────────────────────────────
def detect_command(self, user_input):
text = user_input.lower().strip()
if any(w in text for w in ("stop", "exit", "quit", "done", "bye", "no", "leave")):
return "exit"
if any(w in text for w in ("weather", "temperature", "forecast", "outside")):
return "weather"
if any(w in text for w in ("quote", "motivation", "inspire", "wisdom")):
return "quote"
if any(w in text for w in ("fact", "trivia", "history", "random")):
return "fact"
if any(w in text for w in ("change", "city", "location", "switch", "set")):
return "location"
if any(w in text for w in ("again", "repeat", "briefing", "morning", "full", "summary")):
return "repeat"
return "unknown"
# ── Build and deliver the briefing ───────────────────────────────
async def deliver_briefing(self, lat, lon, city_name):
await self.capability_worker.speak("One moment, getting your morning update.")
weather = self.fetch_weather(lat, lon)
quote = self.fetch_quote()
fact = self.fetch_fun_fact()
# Store for "tell me more" follow-ups
self.last_weather = weather
self.last_quote = quote
self.last_fact = fact
weather_str = weather["raw"] if weather else "weather data unavailable"
quote_str = quote["quote"] if quote else "Stay positive and keep going"
author_str = quote["author"] if quote else "Anonymous"
fact_str = fact["fact"] if fact else "No fun fact available right now"
prompt = BRIEFING_PROMPT.format(
weather=f"{weather_str} in {city_name}",
quote=quote_str,
author=author_str,
fact=fact_str,
)
try:
briefing = self.capability_worker.text_to_text_response(prompt)
await self.capability_worker.speak(briefing)
except Exception as e:
self.worker.editor_logging_handler.error(f"LLM briefing error: {e}")
# Fallback: speak raw data directly
await self.capability_worker.speak(
f"Good morning! It's {weather_str} in {city_name}. "
f"Here's your quote: {quote_str}, by {author_str}. "
f"Fun fact: {fact_str}"
)
# ── Detail follow-ups ────────────────────────────────────────────
async def expand_topic(self, topic, data):
try:
prompt = DETAIL_PROMPT.format(topic=topic, data=json.dumps(data))
detail = self.capability_worker.text_to_text_response(prompt)
await self.capability_worker.speak(detail)
except Exception as e:
self.worker.editor_logging_handler.error(f"Detail expand error: {e}")
await self.capability_worker.speak("Sorry, I couldn't get more details on that.")
# ── Main flow ────────────────────────────────────────────────────
async def run_daily_briefing(self):
lat = DEFAULT_LAT
lon = DEFAULT_LON
city_name = DEFAULT_CITY
self.last_weather = None
self.last_quote = None
self.last_fact = None
# Check if trigger phrase mentions a city
if self.initial_request:
found_city, found_lat, found_lon = self.resolve_city(self.initial_request)
if found_city:
city_name = found_city
lat = found_lat
lon = found_lon
# Deliver the main briefing
await self.deliver_briefing(lat, lon, city_name)
# Interaction loop — let user ask follow-ups or exit
max_turns = 10
for _ in range(max_turns):
try:
response = await self.capability_worker.run_io_loop(
"Want details on weather, quote, or fact? Or say done."
)
if not response:
continue
command = self.detect_command(response)
if command == "exit":
await self.capability_worker.speak("Have a great day!")
break
elif command == "weather":
if self.last_weather:
await self.expand_topic("weather", self.last_weather)
else:
await self.capability_worker.speak("Weather data wasn't available.")
elif command == "quote":
if self.last_quote:
await self.expand_topic("quote", self.last_quote)
else:
await self.capability_worker.speak("Quote data wasn't available.")
elif command == "fact":
if self.last_fact:
await self.expand_topic("fun fact", self.last_fact)
else:
await self.capability_worker.speak("Fun fact wasn't available.")
elif command == "location":
city_response = await self.capability_worker.run_io_loop(
"Which city would you like the briefing for?"
)
found_city, found_lat, found_lon = self.resolve_city(city_response)
if found_city:
city_name = found_city
lat = found_lat
lon = found_lon
await self.deliver_briefing(lat, lon, city_name)
else:
await self.capability_worker.speak(
"I don't recognize that city yet. Try a major city name."
)
elif command == "repeat":
await self.deliver_briefing(lat, lon, city_name)
else:
await self.capability_worker.speak(
"You can say weather, quote, fact, change city, or done."
)
except Exception as e:
self.worker.editor_logging_handler.error(f"Loop error: {e}")
break
self.capability_worker.resume_normal_flow()
# ── Entry point ──────────────────────────────────────────────────
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
# Grab the triggering transcription
self.initial_request = None
try:
self.initial_request = worker.transcription
except (AttributeError, Exception):
pass
if not self.initial_request:
try:
self.initial_request = worker.last_transcription
except (AttributeError, Exception):
pass
if not self.initial_request:
try:
self.initial_request = worker.current_transcription
except (AttributeError, Exception):
pass
self.worker.session_tasks.create(self.run_daily_briefing())