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
|
"""Data schemas for last30days skill."""
from dataclasses import dataclass, field
from datetime import UTC, datetime
from typing import Any
@dataclass
class Engagement:
"""Engagement metrics."""
# Reddit fields
score: int | None = None
num_comments: int | None = None
upvote_ratio: float | None = None
# X fields
likes: int | None = None
reposts: int | None = None
replies: int | None = None
quotes: int | None = None
def to_dict(self) -> dict[str, Any]:
d = {}
if self.score is not None:
d["score"] = self.score
if self.num_comments is not None:
d["num_comments"] = self.num_comments
if self.upvote_ratio is not None:
d["upvote_ratio"] = self.upvote_ratio
if self.likes is not None:
d["likes"] = self.likes
if self.reposts is not None:
d["reposts"] = self.reposts
if self.replies is not None:
d["replies"] = self.replies
if self.quotes is not None:
d["quotes"] = self.quotes
return d if d else None
@dataclass
class Comment:
"""Reddit comment."""
score: int
date: str | None
author: str
excerpt: str
url: str
def to_dict(self) -> dict[str, Any]:
return {
"score": self.score,
"date": self.date,
"author": self.author,
"excerpt": self.excerpt,
"url": self.url,
}
@dataclass
class SubScores:
"""Component scores."""
relevance: int = 0
recency: int = 0
engagement: int = 0
def to_dict(self) -> dict[str, int]:
return {
"relevance": self.relevance,
"recency": self.recency,
"engagement": self.engagement,
}
@dataclass
class RedditItem:
"""Normalized Reddit item."""
id: str
title: str
url: str
subreddit: str
date: str | None = None
date_confidence: str = "low"
engagement: Engagement | None = None
top_comments: list[Comment] = field(default_factory=list)
comment_insights: list[str] = field(default_factory=list)
relevance: float = 0.5
why_relevant: str = ""
subs: SubScores = field(default_factory=SubScores)
score: int = 0
def to_dict(self) -> dict[str, Any]:
return {
"id": self.id,
"title": self.title,
"url": self.url,
"subreddit": self.subreddit,
"date": self.date,
"date_confidence": self.date_confidence,
"engagement": self.engagement.to_dict() if self.engagement else None,
"top_comments": [c.to_dict() for c in self.top_comments],
"comment_insights": self.comment_insights,
"relevance": self.relevance,
"why_relevant": self.why_relevant,
"subs": self.subs.to_dict(),
"score": self.score,
}
@dataclass
class XItem:
"""Normalized X item."""
id: str
text: str
url: str
author_handle: str
date: str | None = None
date_confidence: str = "low"
engagement: Engagement | None = None
relevance: float = 0.5
why_relevant: str = ""
subs: SubScores = field(default_factory=SubScores)
score: int = 0
def to_dict(self) -> dict[str, Any]:
return {
"id": self.id,
"text": self.text,
"url": self.url,
"author_handle": self.author_handle,
"date": self.date,
"date_confidence": self.date_confidence,
"engagement": self.engagement.to_dict() if self.engagement else None,
"relevance": self.relevance,
"why_relevant": self.why_relevant,
"subs": self.subs.to_dict(),
"score": self.score,
}
@dataclass
class WebSearchItem:
"""Normalized web search item (no engagement metrics)."""
id: str
title: str
url: str
source_domain: str # e.g., "medium.com", "github.com"
snippet: str
date: str | None = None
date_confidence: str = "low"
relevance: float = 0.5
why_relevant: str = ""
subs: SubScores = field(default_factory=SubScores)
score: int = 0
def to_dict(self) -> dict[str, Any]:
return {
"id": self.id,
"title": self.title,
"url": self.url,
"source_domain": self.source_domain,
"snippet": self.snippet,
"date": self.date,
"date_confidence": self.date_confidence,
"relevance": self.relevance,
"why_relevant": self.why_relevant,
"subs": self.subs.to_dict(),
"score": self.score,
}
@dataclass
class Report:
"""Full research report."""
topic: str
range_from: str
range_to: str
generated_at: str
mode: str # 'reddit-only', 'x-only', 'both', 'web-only', etc.
openai_model_used: str | None = None
xai_model_used: str | None = None
reddit: list[RedditItem] = field(default_factory=list)
x: list[XItem] = field(default_factory=list)
web: list[WebSearchItem] = field(default_factory=list)
best_practices: list[str] = field(default_factory=list)
prompt_pack: list[str] = field(default_factory=list)
context_snippet_md: str = ""
# Status tracking
reddit_error: str | None = None
x_error: str | None = None
web_error: str | None = None
# Cache info
from_cache: bool = False
cache_age_hours: float | None = None
def to_dict(self) -> dict[str, Any]:
d = {
"topic": self.topic,
"range": {
"from": self.range_from,
"to": self.range_to,
},
"generated_at": self.generated_at,
"mode": self.mode,
"openai_model_used": self.openai_model_used,
"xai_model_used": self.xai_model_used,
"reddit": [r.to_dict() for r in self.reddit],
"x": [x.to_dict() for x in self.x],
"web": [w.to_dict() for w in self.web],
"best_practices": self.best_practices,
"prompt_pack": self.prompt_pack,
"context_snippet_md": self.context_snippet_md,
}
if self.reddit_error:
d["reddit_error"] = self.reddit_error
if self.x_error:
d["x_error"] = self.x_error
if self.web_error:
d["web_error"] = self.web_error
if self.from_cache:
d["from_cache"] = self.from_cache
if self.cache_age_hours is not None:
d["cache_age_hours"] = self.cache_age_hours
return d
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Report":
"""Create Report from serialized dict (handles cache format)."""
# Handle range field conversion
range_data = data.get("range", {})
range_from = range_data.get("from", data.get("range_from", ""))
range_to = range_data.get("to", data.get("range_to", ""))
# Reconstruct Reddit items
reddit_items = []
for r in data.get("reddit", []):
eng = None
if r.get("engagement"):
eng = Engagement(**r["engagement"])
comments = [Comment(**c) for c in r.get("top_comments", [])]
subs = SubScores(**r.get("subs", {})) if r.get("subs") else SubScores()
reddit_items.append(
RedditItem(
id=r["id"],
title=r["title"],
url=r["url"],
subreddit=r["subreddit"],
date=r.get("date"),
date_confidence=r.get("date_confidence", "low"),
engagement=eng,
top_comments=comments,
comment_insights=r.get("comment_insights", []),
relevance=r.get("relevance", 0.5),
why_relevant=r.get("why_relevant", ""),
subs=subs,
score=r.get("score", 0),
)
)
# Reconstruct X items
x_items = []
for x in data.get("x", []):
eng = None
if x.get("engagement"):
eng = Engagement(**x["engagement"])
subs = SubScores(**x.get("subs", {})) if x.get("subs") else SubScores()
x_items.append(
XItem(
id=x["id"],
text=x["text"],
url=x["url"],
author_handle=x["author_handle"],
date=x.get("date"),
date_confidence=x.get("date_confidence", "low"),
engagement=eng,
relevance=x.get("relevance", 0.5),
why_relevant=x.get("why_relevant", ""),
subs=subs,
score=x.get("score", 0),
)
)
# Reconstruct Web items
web_items = []
for w in data.get("web", []):
subs = SubScores(**w.get("subs", {})) if w.get("subs") else SubScores()
web_items.append(
WebSearchItem(
id=w["id"],
title=w["title"],
url=w["url"],
source_domain=w.get("source_domain", ""),
snippet=w.get("snippet", ""),
date=w.get("date"),
date_confidence=w.get("date_confidence", "low"),
relevance=w.get("relevance", 0.5),
why_relevant=w.get("why_relevant", ""),
subs=subs,
score=w.get("score", 0),
)
)
return cls(
topic=data["topic"],
range_from=range_from,
range_to=range_to,
generated_at=data["generated_at"],
mode=data["mode"],
openai_model_used=data.get("openai_model_used"),
xai_model_used=data.get("xai_model_used"),
reddit=reddit_items,
x=x_items,
web=web_items,
best_practices=data.get("best_practices", []),
prompt_pack=data.get("prompt_pack", []),
context_snippet_md=data.get("context_snippet_md", ""),
reddit_error=data.get("reddit_error"),
x_error=data.get("x_error"),
web_error=data.get("web_error"),
from_cache=data.get("from_cache", False),
cache_age_hours=data.get("cache_age_hours"),
)
def create_report(
topic: str,
from_date: str,
to_date: str,
mode: str,
openai_model: str | None = None,
xai_model: str | None = None,
) -> Report:
"""Create a new report with metadata."""
return Report(
topic=topic,
range_from=from_date,
range_to=to_date,
generated_at=datetime.now(UTC).isoformat(),
mode=mode,
openai_model_used=openai_model,
xai_model_used=xai_model,
)
|