forked from openhome-dev/abilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (81 loc) · 3.25 KB
/
main.py
File metadata and controls
95 lines (81 loc) · 3.25 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
import json
import os
import requests
from src.agent.capability import MatchingCapability
from src.main import AgentWorker
from src.agent.capability_worker import CapabilityWorker
# =============================================================================
# API TEMPLATE
# For Abilities that call an external API.
# Pattern: Speak → Collect input → Call API → Speak result → Exit
#
# Replace API_URL, API_HEADERS, and the fetch_data() logic with your own.
# =============================================================================
# --- CONFIGURATION ---
# Replace with your actual API endpoint and headers
API_URL = "https://api.example.com/data"
API_HEADERS = {
"Authorization": "Bearer YOUR_API_KEY_HERE",
"Content-Type": "application/json",
}
class ApiTemplateCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
@classmethod
def register_capability(cls) -> "MatchingCapability":
with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
) as file:
data = json.load(file)
return cls(
unique_name=data["unique_name"],
matching_hotwords=data["matching_hotwords"],
)
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
self.worker.session_tasks.create(self.run())
async def fetch_data(self, query: str) -> str | None:
"""
Call your external API here.
Returns the result as a string, or None on failure.
"""
try:
response = requests.get(
API_URL,
headers=API_HEADERS,
params={"q": query},
)
if response.status_code == 200:
data = response.json()
# --- Parse your API response here ---
return str(data)
else:
self.worker.editor_logging_handler.error(
f"[ApiTemplate] API returned {response.status_code}: {response.text}"
)
return None
except Exception as e:
self.worker.editor_logging_handler.error(f"[ApiTemplate] Error: {e}")
return None
async def run(self):
# Step 1: Ask what they need
await self.capability_worker.speak("Sure! What would you like me to look up?")
# Step 2: Get user input
user_input = await self.capability_worker.user_response()
# Step 3: Call the API
await self.capability_worker.speak("Let me check on that.")
result = await self.fetch_data(user_input)
# Step 4: Respond
if result:
# Use LLM to turn raw data into a natural spoken response
response = self.capability_worker.text_to_text_response(
f"Summarize this data in one short sentence for a voice response: {result}"
)
await self.capability_worker.speak(response)
else:
await self.capability_worker.speak(
"Sorry, I couldn't get that information right now. Try again later."
)
# Step 5: ALWAYS resume normal flow
self.capability_worker.resume_normal_flow()