-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintel.py
More file actions
282 lines (230 loc) · 8.56 KB
/
intel.py
File metadata and controls
282 lines (230 loc) · 8.56 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
#!/usr/bin/env python3.12
"""ULTRON Intelligence Engine - strategic signal crawling and synthesis."""
import argparse
import json
import logging
import os
from datetime import datetime, timezone
from pathlib import Path
import requests
import yaml
from sources import Signal
from sources import arxiv, github_trending, hackernews, rss_feeds
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [intel] %(levelname)s %(message)s",
)
log = logging.getLogger(__name__)
BASE_DIR = Path(__file__).resolve().parent
SOURCE_CRAWLERS = {
"arxiv": arxiv.crawl,
"github": github_trending.crawl,
"hn": hackernews.crawl,
"rss": rss_feeds.crawl,
}
def load_config():
with open(BASE_DIR / "config.yaml") as f:
return yaml.safe_load(f)
def ask_ollama(cfg, prompt):
url = cfg["ollama"]["url"]
for model in [cfg["ollama"]["model"], cfg["ollama"]["fallback_model"]]:
try:
resp = requests.post(url, json={
"model": model,
"prompt": prompt,
"stream": False,
"options": {"temperature": 0.6},
}, timeout=180)
resp.raise_for_status()
return resp.json().get("response", "")
except Exception as e:
log.warning("Ollama failed (model=%s): %s", model, e)
return None
def crawl_all() -> list[Signal]:
"""Crawl all sources and return combined signals."""
all_signals = []
for name, crawl_fn in SOURCE_CRAWLERS.items():
log.info("Crawling %s...", name)
try:
signals = crawl_fn()
all_signals.extend(signals)
except Exception as e:
log.warning("Source %s failed: %s", name, e)
log.info("Total raw signals: %d", len(all_signals))
return all_signals
def dedupe_and_rank(signals: list[Signal]) -> list[Signal]:
"""Remove duplicates by title similarity and sort by score."""
seen_titles = set()
unique = []
for s in signals:
key = s.title.lower().strip()[:60]
if key not in seen_titles:
seen_titles.add(key)
unique.append(s)
unique.sort(key=lambda s: s.score, reverse=True)
return unique
def format_signals_for_prompt(signals: list[Signal], max_signals=40) -> str:
"""Format signals as text for Ollama prompt."""
lines = []
for s in signals[:max_signals]:
line = f"[{s.source.upper()}] {s.title}"
if s.summary:
line += f" — {s.summary[:100]}"
lines.append(line)
return "\n".join(lines)
def synthesize(cfg, signals: list[Signal]) -> str:
"""Use Ollama to synthesize intelligence brief from signals."""
signal_text = format_signals_for_prompt(signals)
prompt = f"""You are a strategic intelligence analyst. Given these signals from the past 24 hours, create a concise intelligence brief.
SIGNALS:
{signal_text}
Create a brief with these sections (use the exact headers):
TOP 5 SIGNALS: Most important developments with source tags
EMERGING THEMES: Patterns you see across multiple signals
GAP ANALYSIS: What everyone is ignoring that matters
CROSS-POLLINATION: Ideas from one domain that could transform another
CONTRARIAN TAKE: Where consensus might be wrong
6-MONTH PREDICTION: What will be obvious then that isn't now
RECOMMENDED ACTIONS: 3 specific things to do this week
Be specific. Name names. Make bold predictions. Keep total under 2000 words."""
return ask_ollama(cfg, prompt)
def format_telegram_brief(brief: str, signal_count: int, source_counts: dict) -> str:
"""Format brief for Telegram delivery."""
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
sources_str = ", ".join(f"{k}:{v}" for k, v in source_counts.items())
header = f"ULTRON INTELLIGENCE BRIEF\n{today} | {signal_count} signals ({sources_str})\n\n"
full = header + brief
# Telegram limit is 4096 chars
if len(full) > 4000:
full = full[:3997] + "..."
return full
def store_signals_aoms(cfg, signals: list[Signal]):
"""Store top signals in AOMS semantic tier."""
stored = 0
for s in signals[:15]: # top 15 only
body = {
"type": "fact",
"payload": {
"subject": s.title,
"predicate": "signals from",
"object": s.source,
"context": s.summary,
},
"tags": ["intel", s.source],
"weight": round(s.score * 3 + 0.5, 1), # scale 0.5-3.5
}
try:
requests.post(cfg["aoms"]["url"] + "/memory/semantic", json=body, timeout=5)
stored += 1
except Exception:
pass
log.info("Stored %d signals in AOMS", stored)
def store_brief_aoms(cfg, brief: str):
"""Store daily brief in AOMS episodic tier."""
body = {
"type": "experience",
"payload": {
"title": f"Intelligence Brief {datetime.now(timezone.utc).strftime('%Y-%m-%d')}",
"summary": brief[:500],
"outcome": "success",
},
"tags": ["intel_brief"],
}
try:
requests.post(cfg["aoms"]["url"] + "/memory/episodic", json=body, timeout=10)
except Exception as e:
log.warning("AOMS brief store failed: %s", e)
def send_telegram(text):
token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
chat_id = os.environ.get("TELEGRAM_CHAT_ID", "")
if not token or not chat_id:
log.info("Telegram not configured, skipping")
return False
try:
resp = requests.post(
f"https://api.telegram.org/bot{token}/sendMessage",
json={"chat_id": chat_id, "text": text, "parse_mode": "Markdown",
"disable_web_page_preview": True},
timeout=15,
)
resp.raise_for_status()
return True
except Exception as e:
log.warning("Telegram failed: %s", e)
return False
def save_raw_signals(signals: list[Signal]):
"""Save raw signals to daily log for later analysis."""
log_dir = BASE_DIR / "logs"
log_dir.mkdir(exist_ok=True)
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
log_file = log_dir / f"intel-{today}.json"
data = {
"date": today,
"signal_count": len(signals),
"signals": [s.to_dict() for s in signals],
}
log_file.write_text(json.dumps(data, indent=2))
log.info("Raw signals saved to %s", log_file)
def cmd_sources():
"""List configured sources."""
print("\nConfigured Intel Sources:")
print("-" * 40)
for name in SOURCE_CRAWLERS:
print(f" - {name}")
print(f"\nTotal: {len(SOURCE_CRAWLERS)} sources")
def main():
parser = argparse.ArgumentParser(description="ULTRON Intelligence Engine")
parser.add_argument("--dry-run", action="store_true", help="Crawl + synthesize, print to stdout")
parser.add_argument("--sources", action="store_true", help="List configured sources")
parser.add_argument("--crawl-only", action="store_true", help="Just crawl, no synthesis")
args = parser.parse_args()
if args.sources:
cmd_sources()
return
cfg = load_config()
# 1. Crawl
raw_signals = crawl_all()
if not raw_signals:
log.warning("No signals collected. Check network/sources.")
return
# 2. Dedupe and rank
signals = dedupe_and_rank(raw_signals)
log.info("After dedup: %d signals", len(signals))
# Count by source
source_counts = {}
for s in signals:
source_counts[s.source] = source_counts.get(s.source, 0) + 1
# Save raw
save_raw_signals(signals)
if args.crawl_only:
print(f"\nCrawled {len(signals)} signals:")
for s in signals[:20]:
print(f" [{s.source:6s}] {s.score:.1f} | {s.title[:70]}")
return
# 3. Synthesize
log.info("Synthesizing intelligence brief...")
brief = synthesize(cfg, signals)
if not brief:
log.error("Synthesis failed (Ollama unreachable)")
return
# 4. Store
if not args.dry_run:
store_signals_aoms(cfg, signals)
store_brief_aoms(cfg, brief)
# 5. Format and deliver
telegram_brief = format_telegram_brief(brief, len(signals), source_counts)
if args.dry_run:
print("\n" + "=" * 50)
print("INTELLIGENCE BRIEF (dry run)")
print("=" * 50)
print(telegram_brief)
print("=" * 50)
print(f"\n[{len(telegram_brief)} chars]")
return
if send_telegram(telegram_brief):
log.info("Brief delivered via Telegram")
else:
log.warning("Telegram delivery failed, brief saved in logs")
log.info("Intel cycle complete. %d signals processed.", len(signals))
if __name__ == "__main__":
main()