-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodec_scheduler.py
More file actions
308 lines (261 loc) · 10.5 KB
/
codec_scheduler.py
File metadata and controls
308 lines (261 loc) · 10.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
"""CODEC Scheduler — Cron-like scheduling for agent crews and commands."""
import json
import os
import time
import logging
import sys
from datetime import datetime
import requests
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [SCHEDULER] %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger("scheduler")
SCHEDULE_PATH = os.path.expanduser("~/.codec/schedules.json")
DASHBOARD_URL = "http://localhost:8090"
os.makedirs(os.path.expanduser("~/.codec"), exist_ok=True)
# ── Storage ─────────────────────────────────────────────────────────────────
def load_schedules() -> list:
try:
with open(SCHEDULE_PATH) as f:
return json.load(f)
except Exception:
return []
def save_schedules(schedules: list):
with open(SCHEDULE_PATH, "w") as f:
json.dump(schedules, f, indent=2)
# ── Management ──────────────────────────────────────────────────────────────
def add_schedule(
crew_name: str,
topic: str = "",
cron_hour: int = 8,
cron_minute: int = 0,
days: list | None = None,
) -> dict:
"""Add a scheduled agent crew run. days: 0=Mon … 6=Sun, default every day."""
schedules = load_schedules()
schedule = {
"id": f"sched_{int(time.time())}",
"crew": crew_name,
"topic": topic,
"hour": cron_hour,
"minute": cron_minute,
"days": days if days is not None else [0, 1, 2, 3, 4, 5, 6],
"enabled": True,
"last_run": None,
"created": datetime.now().isoformat(),
}
schedules.append(schedule)
save_schedules(schedules)
log.info(f"Schedule added: {crew_name} at {cron_hour:02d}:{cron_minute:02d}")
return schedule
def remove_schedule(sched_id: str) -> bool:
schedules = load_schedules()
before = len(schedules)
schedules = [s for s in schedules if s["id"] != sched_id]
save_schedules(schedules)
return len(schedules) < before
def toggle_schedule(sched_id: str, enabled: bool) -> bool:
schedules = load_schedules()
for s in schedules:
if s["id"] == sched_id:
s["enabled"] = enabled
save_schedules(schedules)
return True
return False
# ── Execution ────────────────────────────────────────────────────────────────
def _run_crew(sched: dict):
"""Fire off a crew via the background job endpoint and optionally poll."""
payload: dict = {"crew": sched["crew"]}
if sched.get("topic"):
payload["topic"] = sched["topic"]
try:
r = requests.post(
f"{DASHBOARD_URL}/api/agents/run",
json=payload,
timeout=30,
)
if r.status_code == 200:
data = r.json()
job_id = data.get("job_id")
if job_id:
log.info(f" Job started: {job_id} — polling for result…")
# Poll up to 10 min
for _ in range(120):
time.sleep(5)
sr = requests.get(
f"{DASHBOARD_URL}/api/agents/status/{job_id}",
timeout=10,
)
if sr.status_code == 200:
st = sr.json().get("status")
if st not in ("running", "pending"):
log.info(f" ✅ {sched['crew']} finished: {st}")
return True
else:
log.info(f" ✅ {sched['crew']} completed synchronously")
return True
else:
log.warning(f" ⚠️ /api/agents/run returned {r.status_code}")
except Exception as e:
log.error(f" ❌ Crew run failed: {e}")
return False
def check_and_run():
"""Check every minute whether any schedules should fire right now."""
schedules = load_schedules()
now = datetime.now()
changed = False
for sched in schedules:
if not sched.get("enabled"):
continue
if now.hour != sched["hour"] or now.minute != sched["minute"]:
continue
if now.weekday() not in sched.get("days", list(range(7))):
continue
last_run = sched.get("last_run")
if last_run and last_run[:10] == now.strftime("%Y-%m-%d"):
continue
log.info(f"🚀 Scheduled run: {sched['crew']} — {sched.get('topic', '')}")
success = _run_crew(sched)
if success:
sched["last_run"] = now.isoformat()
changed = True
if changed:
save_schedules(schedules)
def run_daemon(check_interval: int = 60):
"""Run check_and_run every minute."""
schedules = load_schedules()
log.info(f"Scheduler daemon starting — {len(schedules)} schedule(s) loaded")
while True:
try:
check_and_run()
except Exception as e:
log.error(f"Scheduler loop error: {e}")
time.sleep(check_interval)
# ── CODEC Skill (voice control) ──────────────────────────────────────────────
SKILL_NAME = "scheduler"
SKILL_TRIGGERS = [
"schedule agent", "schedule crew", "run every morning",
"run every monday", "schedule daily", "run daily briefing",
"set up schedule", "every morning at", "every monday",
"schedule competitor analysis", "run briefing at",
]
SKILL_DESCRIPTION = "Schedule CODEC agent crews to run automatically on a cron schedule"
_DAY_MAP = {
"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3,
"friday": 4, "saturday": 5, "sunday": 6,
"weekdays": [0, 1, 2, 3, 4], "weekends": [5, 6],
"every day": [0, 1, 2, 3, 4, 5, 6], "daily": [0, 1, 2, 3, 4, 5, 6],
}
_CREW_MAP = {
"daily briefing": "daily_briefing",
"briefing": "daily_briefing",
"competitor": "competitor_analysis",
"competitor analysis": "competitor_analysis",
"social media": "social_media",
"code review": "code_review",
"data analysis": "data_analysis",
}
def _parse_schedule_intent(task: str) -> dict | None:
"""Parse natural language like 'run my daily briefing every morning at 8'."""
import re
tl = task.lower()
# Detect crew
crew = "daily_briefing"
for phrase, name in _CREW_MAP.items():
if phrase in tl:
crew = name
break
# Detect hour
hour = 8
m = re.search(r"at (\d{1,2})(?::(\d{2}))?\s*(?:am|pm)?", tl)
if m:
hour = int(m.group(1))
minute_str = m.group(2)
minute = int(minute_str) if minute_str else 0
if "pm" in tl and hour < 12:
hour += 12
else:
minute = 0
# Detect days
days = [0, 1, 2, 3, 4, 5, 6]
for phrase, val in _DAY_MAP.items():
if phrase in tl:
days = val if isinstance(val, list) else [val]
break
return {"crew": crew, "hour": hour, "minute": minute, "days": days}
def run(task: str, context: str = "") -> str:
"""Voice-triggered schedule creation."""
tl = task.lower()
if "list" in tl or "show" in tl or "what schedule" in tl:
schedules = load_schedules()
if not schedules:
return "No schedules set up yet. Say 'schedule daily briefing at 8am' to create one."
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
lines = [f"{len(schedules)} schedule(s):"]
for s in schedules:
days_str = ", ".join(day_names[d] for d in s.get("days", []))
status = "✅" if s.get("enabled") else "❌"
lines.append(f" {status} {s['crew']} at {s['hour']:02d}:{s['minute']:02d} [{days_str}]")
return "\n".join(lines)
if "remove" in tl or "delete" in tl or "cancel" in tl:
schedules = load_schedules()
if schedules:
remove_schedule(schedules[-1]["id"])
return f"Removed last schedule: {schedules[-1]['crew']}"
return "No schedules to remove."
intent = _parse_schedule_intent(task)
if not intent:
return "I couldn't parse that schedule. Try: 'run daily briefing every morning at 8'"
s = add_schedule(
intent["crew"],
cron_hour=intent["hour"],
cron_minute=intent["minute"],
days=intent["days"],
)
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
days_str = ", ".join(day_names[d] for d in s["days"])
return (
f"Scheduled: {s['crew']} will run at {s['hour']:02d}:{s['minute']:02d} "
f"on {days_str}. Say 'list schedules' to see all."
)
# ── CLI ──────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
if len(sys.argv) < 2:
run_daemon()
elif sys.argv[1] == "list":
schedules = load_schedules()
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
if not schedules:
print("No schedules.")
for s in schedules:
days_str = ", ".join(day_names[d] for d in s.get("days", []))
status = "✅" if s.get("enabled") else "❌"
print(f" {status} {s['id']}: {s['crew']} at {s['hour']:02d}:{s['minute']:02d} [{days_str}]")
elif sys.argv[1] == "add" and len(sys.argv) > 2:
crew = sys.argv[2]
hour = 8
minute = 0
days = None
i = 3
while i < len(sys.argv):
if sys.argv[i] == "--hour" and i + 1 < len(sys.argv):
hour = int(sys.argv[i + 1]); i += 2
elif sys.argv[i] == "--minute" and i + 1 < len(sys.argv):
minute = int(sys.argv[i + 1]); i += 2
elif sys.argv[i] == "--days" and i + 1 < len(sys.argv):
days = [int(d) for d in sys.argv[i + 1].split(",")]; i += 2
else:
i += 1
s = add_schedule(crew, cron_hour=hour, cron_minute=minute, days=days)
print(f"Added: {s['id']} — {crew} at {hour:02d}:{minute:02d}")
elif sys.argv[1] == "remove" and len(sys.argv) > 2:
if remove_schedule(sys.argv[2]):
print(f"Removed {sys.argv[2]}")
else:
print(f"Not found: {sys.argv[2]}")
elif sys.argv[1] == "run":
check_and_run()
else:
run_daemon()