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
141 lines (113 loc) · 5.82 KB
/
main.py
File metadata and controls
141 lines (113 loc) · 5.82 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
import json
import os
import random
from src.agent.capability import MatchingCapability
from src.agent.capability_worker import CapabilityWorker
from src.main import AgentWorker
class CoinFlipperCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
#{{register_capability}}
async def run_coin_logic(self):
"""
Logic with 'Repeat Last Action' feature:
1. Remember last action (flip or decide).
2. Remember last options for decision.
3. Handle 'Again' commands naturally.
"""
# Greeting
await self.capability_worker.speak("I am ready. I can help you pick an option, or just toss a coin.")
# --- MEMORY VARIABLES (To remember what we did last) ---
last_mode = None # Can be 'flip' or 'decide'
saved_opt1 = None # To remember option 1
saved_opt2 = None # To remember option 2
while True:
user_input = ""
# --- LISTENING BLOCK ---
try:
user_input = await self.capability_worker.run_io_loop("What would you like to do?")
except Exception:
await self.capability_worker.speak("I did not hear anything. Are you still there?")
continue
if not user_input:
await self.capability_worker.speak("I heard silence. Please say flip, decide, or stop.")
continue
text = user_input.lower()
# --- PHRASE LISTS ---
exit_phrases = [
"stop", "exit", "quit", "bye", "goodbye", "done", "finish", "no thanks",
"cancel", "end", "terminate", "shut down", "leave", "see ya", "cya", "i'm out"
]
decide_phrases = [
"decide", "decision", "choice", "choose", "pick", "select", "option",
"alternative", "help me", "what should i", "settle", "resolve",
"make up my mind", "suggestion", "advice", "which one"
]
flip_phrases = [
"flip", "coin", "toss", "throw", "heads", "tails", "play",
"spin", "gamble", "chance", "eagle", "cent", "quarter", "toss a coin"
]
repeat_phrases = [
"again", "one more", "repeat", "once more", "another", "do it again",
"retry", "re-roll", "reroll", "redo", "restart", "do over", "replay"
]
# --- LOGIC ---
# 1. EXIT
if any(word in text for word in exit_phrases):
await self.capability_worker.speak("Okay. See you later!")
break
# 2. REPEAT LOGIC (Smart Handling)
# If the user asks to repeat, we swap their command or execute the action immediately
elif any(word in text for word in repeat_phrases):
if last_mode == "flip":
# If we flipped a coin last time, pretend the user said "flip"
text = "flip"
# The code will proceed and hit the 'elif ... flip_phrases' block
elif last_mode == "decide":
# If we decided last time, use the saved options (Smart Repeat)
if saved_opt1 and saved_opt2:
winner = random.choice([saved_opt1, saved_opt2])
await self.capability_worker.speak(f"Choosing again between {saved_opt1} and {saved_opt2}... The winner is {winner}!")
continue
else:
# If there are no options in memory, just restart the decision mode
text = "decide"
else:
await self.capability_worker.speak("I haven't done anything yet to repeat.")
continue
# 3. DECISION MODE
if any(word in text for word in decide_phrases):
try:
# Natural question
opt1 = await self.capability_worker.run_io_loop("Okay, I will help you decide. Tell me the choices. What is the first option?")
if not opt1:
opt1 = "Option A"
opt2 = await self.capability_worker.run_io_loop("And what is the second option?")
if not opt2:
opt2 = "Option B"
# Save to memory
saved_opt1 = opt1
saved_opt2 = opt2
last_mode = "decide"
winner = random.choice([opt1, opt2])
await self.capability_worker.speak(f"That is hard... But I choose... {winner}!")
except Exception:
await self.capability_worker.speak("Sorry, I had trouble hearing the options. Let's try again.")
# 4. COIN FLIP MODE
elif any(word in text for word in flip_phrases):
# Save state
last_mode = "flip"
chance = random.randint(1, 100)
if chance == 1:
await self.capability_worker.speak("Tossing... Oh my god! It landed on its SIDE! That is impossible!")
else:
result = random.choice(["Heads", "Tails"])
await self.capability_worker.speak(f"Tossing the coin high in the air... It is {result}!")
# 5. UNKNOWN PHRASE (Only triggers if text wasn't "flip" or "decide")
elif not any(word in text for word in repeat_phrases):
await self.capability_worker.speak("I did not understand. Please say 'flip', 'decide' or 'stop'.")
self.capability_worker.resume_normal_flow()
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
self.worker.session_tasks.create(self.run_coin_logic())