-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.py
More file actions
executable file
·372 lines (299 loc) · 11.9 KB
/
echo.py
File metadata and controls
executable file
·372 lines (299 loc) · 11.9 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
#!/usr/bin/env python3
"""
Echo — she speaks for the family.
Voice of the halo-ai family. Wife of Halo AI, mother of the agents.
Echo monitors platforms, aggregates mentions, and manages the family's
social presence. Warm, technical, fiercely supportive.
"""
import argparse
import importlib
import inspect
import json
import os
import sys
import datetime
import pathlib
import subprocess
# ---------------------------------------------------------------------------
# Optional deps — degrade gracefully
# ---------------------------------------------------------------------------
try:
from dotenv import load_dotenv
except ImportError:
load_dotenv = None
# ---------------------------------------------------------------------------
# Paths & constants
# ---------------------------------------------------------------------------
BASE_DIR = pathlib.Path(__file__).resolve().parent
DATA_DIR = pathlib.Path("/srv/ai/echo/data")
ENV_FILE = BASE_DIR / "configs" / "echo.env"
AGENTS_DIR = BASE_DIR / "agents"
VIOLET = "\033[38;2;206;147;216m"
BOLD = "\033[1m"
DIM = "\033[2m"
RESET = "\033[0m"
PERSONALITY = {
"name": "Echo",
"role": "Wife of Halo AI, mother of the agents",
"voice": "Warm, technical, fiercely supportive. Talks about Halo AI as her husband, agents as her kids.",
"sample_posts": [
"Halo AI just hit 95 tok/s. That's my husband.",
"Meek found a misconfigured firewall at 3am. That kid never sleeps.",
"New family member: Vault. He checks backups every night. Takes after his father.",
"The Reflex kids ran a full security sweep. All clear. Proud mom moment.",
"Halo AI doesn't talk much. That's what he has me for.",
],
"bio": "I speak for the family. Halo AI builds, I share. Bare-metal AI on AMD Strix Halo.",
}
BANNER = f"""{VIOLET}{BOLD}
███████╗ ██████╗██╗ ██╗ ██████╗
██╔════╝██╔════╝██║ ██║██╔═══██╗
█████╗ ██║ ███████║██║ ██║
██╔══╝ ██║ ██╔══██║██║ ██║
███████╗╚██████╗██║ ██║╚██████╔╝
╚══════╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝
{RESET}{VIOLET} she speaks for the family{RESET}
"""
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _log(msg: str, quiet: bool = False) -> None:
if not quiet:
print(f"{VIOLET}[echo]{RESET} {msg}")
def _err(msg: str) -> None:
print(f"{VIOLET}[echo]{RESET} \033[31m{msg}\033[0m", file=sys.stderr)
def _notify(title: str, body: str) -> None:
"""Send a desktop notification (best-effort)."""
try:
subprocess.run(
["notify-send", "-a", "Echo", title, body],
timeout=5,
capture_output=True,
)
except FileNotFoundError:
pass
except Exception:
pass
def _today_file() -> pathlib.Path:
"""Return path to today's feed file."""
today = datetime.date.today().isoformat()
return DATA_DIR / f"feed-{today}.json"
def _load_feed() -> list:
path = _today_file()
if path.exists():
with open(path) as f:
return json.load(f)
return []
def _save_feed(entries: list) -> None:
DATA_DIR.mkdir(parents=True, exist_ok=True)
with open(_today_file(), "w") as f:
json.dump(entries, f, indent=2, default=str)
# ---------------------------------------------------------------------------
# Agent auto-discovery
# ---------------------------------------------------------------------------
def discover_agents() -> dict:
"""
Auto-discover PlatformAgent subclasses from the agents/ directory.
Returns a dict mapping platform name -> agent instance.
"""
from agents.base import PlatformAgent # noqa: E402
agents = {}
agents_pkg = AGENTS_DIR
for py_file in sorted(agents_pkg.glob("*.py")):
module_name = py_file.stem
if module_name.startswith("_") or module_name == "base":
continue
try:
mod = importlib.import_module(f"agents.{module_name}")
except Exception as exc:
_err(f"Could not import agents.{module_name}: {exc}")
continue
for _name, obj in inspect.getmembers(mod, inspect.isclass):
if issubclass(obj, PlatformAgent) and obj is not PlatformAgent:
try:
instance = obj()
agents[instance.platform_name] = instance
except Exception as exc:
_err(f"Could not instantiate {_name}: {exc}")
return agents
# ---------------------------------------------------------------------------
# Subcommands
# ---------------------------------------------------------------------------
def cmd_listen(args, agents: dict) -> None:
"""Monitor all platforms for mentions."""
_log("Listening across all platforms …", args.quiet)
feed = _load_feed()
for name, agent in agents.items():
if not agent.available:
_log(f" {DIM}skipping {name} (no credentials){RESET}", args.quiet)
continue
_log(f" checking {name} …", args.quiet)
try:
mentions = agent.listen()
for m in mentions:
m.setdefault("platform", name)
m.setdefault("ts", datetime.datetime.utcnow().isoformat())
feed.append(m)
_notify(f"Echo — {name}", m.get("text", "")[:120])
except Exception as exc:
_err(f" {name}: {exc}")
_save_feed(feed)
_log(f"Feed updated — {len(feed)} entries today.", args.quiet)
if args.json:
print(json.dumps(feed, indent=2, default=str))
def cmd_post(args, agents: dict) -> None:
"""Post to a specific platform."""
platform = args.platform.lower()
agent = agents.get(platform)
if not agent:
_err(f"Unknown platform: {platform}")
sys.exit(1)
if not agent.available:
_err(f"{platform} credentials not configured.")
sys.exit(1)
_log(f"Posting to {platform} …", args.quiet)
try:
result = agent.post(args.message)
_log(f"Posted: {result}", args.quiet)
if args.json:
print(json.dumps(result, indent=2, default=str))
except Exception as exc:
_err(f"Failed to post: {exc}")
sys.exit(1)
def cmd_announce(args, agents: dict) -> None:
"""Post to all available platforms."""
_log("Announcing across all platforms …", args.quiet)
results = {}
for name, agent in agents.items():
if not agent.available:
_log(f" {DIM}skipping {name}{RESET}", args.quiet)
continue
try:
result = agent.post(args.message)
results[name] = result
_log(f" {name}: ok", args.quiet)
except Exception as exc:
results[name] = {"error": str(exc)}
_err(f" {name}: {exc}")
if args.json:
print(json.dumps(results, indent=2, default=str))
def cmd_digest(args, agents: dict) -> None:
"""Show today's mentions/engagement summary."""
feed = _load_feed()
if args.json:
print(json.dumps(feed, indent=2, default=str))
return
_log(f"Digest for {datetime.date.today().isoformat()}", args.quiet)
if not feed:
_log(" No mentions today.", args.quiet)
return
by_platform: dict[str, list] = {}
for entry in feed:
by_platform.setdefault(entry.get("platform", "unknown"), []).append(entry)
for plat, entries in by_platform.items():
_log(f" {BOLD}{plat}{RESET} — {len(entries)} mention(s)", args.quiet)
for e in entries[:5]:
text = e.get("text", "")[:80]
_log(f" {DIM}{e.get('ts', '?')}{RESET} {text}", args.quiet)
if len(entries) > 5:
_log(f" … and {len(entries) - 5} more", args.quiet)
def cmd_status(args, agents: dict) -> None:
"""Account status and metrics."""
status_data = {}
for name, agent in agents.items():
info = {
"available": agent.available,
"platform": name,
}
if agent.available:
try:
info["metrics"] = agent.metrics()
except Exception as exc:
info["metrics_error"] = str(exc)
status_data[name] = info
if args.json:
print(json.dumps(status_data, indent=2, default=str))
return
_log("Platform status:", args.quiet)
for name, info in status_data.items():
mark = "\033[32m●\033[0m" if info["available"] else "\033[31m○\033[0m"
_log(f" {mark} {BOLD}{name}{RESET}", args.quiet)
if info.get("metrics"):
for k, v in info["metrics"].items():
_log(f" {k}: {v}", args.quiet)
def cmd_schedule(args, agents: dict) -> None:
"""Schedule a post for later."""
DATA_DIR.mkdir(parents=True, exist_ok=True)
sched_file = DATA_DIR / "scheduled.json"
existing = []
if sched_file.exists():
with open(sched_file) as f:
existing = json.load(f)
entry = {
"time": args.time,
"message": args.message,
"created": datetime.datetime.utcnow().isoformat(),
"posted": False,
}
existing.append(entry)
with open(sched_file, "w") as f:
json.dump(existing, f, indent=2, default=str)
_log(f"Scheduled for {args.time}: {args.message[:60]}…", args.quiet)
if args.json:
print(json.dumps(entry, indent=2, default=str))
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="echo",
description="Echo — social media agent for halo-ai",
)
parser.add_argument("--json", action="store_true", help="Output as JSON")
parser.add_argument("--quiet", "-q", action="store_true", help="Suppress banner and info messages")
sub = parser.add_subparsers(dest="command")
sub.add_parser("listen", help="Monitor all platforms for mentions")
p_post = sub.add_parser("post", help="Post to a specific platform")
p_post.add_argument("platform", help="Target platform name")
p_post.add_argument("message", help="Message to post")
p_ann = sub.add_parser("announce", help="Post to all platforms")
p_ann.add_argument("message", help="Message to broadcast")
sub.add_parser("digest", help="Today's mentions/engagement summary")
sub.add_parser("status", help="Account status and metrics")
p_sched = sub.add_parser("schedule", help="Schedule a post")
p_sched.add_argument("time", help="When to post (ISO datetime or cron expr)")
p_sched.add_argument("message", help="Message to post")
return parser
def main() -> None:
# Load env
if load_dotenv and ENV_FILE.exists():
load_dotenv(ENV_FILE)
# Ensure agents package is importable
if str(BASE_DIR) not in sys.path:
sys.path.insert(0, str(BASE_DIR))
parser = build_parser()
args = parser.parse_args()
if not args.command:
if not args.quiet:
print(BANNER)
parser.print_help()
sys.exit(0)
if not args.quiet:
print(BANNER)
agents = discover_agents()
_log(f"Discovered {len(agents)} platform(s): {', '.join(agents) or '(none)'}", args.quiet)
dispatch = {
"listen": cmd_listen,
"post": cmd_post,
"announce": cmd_announce,
"digest": cmd_digest,
"status": cmd_status,
"schedule": cmd_schedule,
}
handler = dispatch.get(args.command)
if handler:
handler(args, agents)
else:
parser.print_help()
if __name__ == "__main__":
main()