agents-framework 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agents_framework-0.3.0.dist-info/METADATA +1364 -0
- agents_framework-0.3.0.dist-info/RECORD +50 -0
- agents_framework-0.3.0.dist-info/WHEEL +5 -0
- agents_framework-0.3.0.dist-info/entry_points.txt +2 -0
- agents_framework-0.3.0.dist-info/licenses/LICENSE +661 -0
- agents_framework-0.3.0.dist-info/top_level.txt +1 -0
- framework/__init__.py +0 -0
- framework/__main__.py +9 -0
- framework/action_page.py +1102 -0
- framework/approval_buttons.py +775 -0
- framework/approvals.py +678 -0
- framework/calendar_card.py +276 -0
- framework/cli.py +273 -0
- framework/codex_llm.py +401 -0
- framework/config.py +126 -0
- framework/drafter.py +263 -0
- framework/interfaces.py +96 -0
- framework/llm.py +744 -0
- framework/plugins/__init__.py +147 -0
- framework/plugins/agentmail_source.py +355 -0
- framework/plugins/approval_destination.py +478 -0
- framework/plugins/command_action.py +248 -0
- framework/plugins/digest_destination.py +546 -0
- framework/plugins/email_destination.py +248 -0
- framework/plugins/fanout_destination.py +160 -0
- framework/plugins/github_org_source.py +226 -0
- framework/plugins/github_releases_source.py +420 -0
- framework/plugins/ics_source.py +594 -0
- framework/plugins/llm_scorer.py +411 -0
- framework/plugins/multi_source.py +241 -0
- framework/plugins/reddit_source.py +625 -0
- framework/plugins/regex_scorer.py +93 -0
- framework/plugins/rss_source.py +346 -0
- framework/plugins/signal_destination.py +133 -0
- framework/plugins/slack_destination.py +461 -0
- framework/plugins/slack_draft_action.py +137 -0
- framework/plugins/slack_post_action.py +65 -0
- framework/plugins/slack_workspace_source.py +992 -0
- framework/plugins/twitter_action.py +210 -0
- framework/plugins/websearch_source.py +761 -0
- framework/plugins/wordpress_action.py +413 -0
- framework/proposals.py +246 -0
- framework/registry.py +166 -0
- framework/runlog.py +125 -0
- framework/runner.py +243 -0
- framework/schedule.py +571 -0
- framework/signal_messenger.py +1418 -0
- framework/slack.py +801 -0
- framework/state.py +39 -0
- framework/templating.py +76 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Regex/keyword scorer — the v1 reference Scorer implementation (issue #4).
|
|
2
|
+
|
|
3
|
+
Config-driven: a list of {pattern, points} rules; every rule whose regex matches
|
|
4
|
+
the signal text contributes its points. Negative points express spam/vendor
|
|
5
|
+
penalties. Multiplier rules apply a factor when they match, which is how the
|
|
6
|
+
industry-relevance boost is expressed.
|
|
7
|
+
|
|
8
|
+
Behaviour is deliberately equivalent to hq/leads/signals/signal_processor.py's
|
|
9
|
+
`score_signal()`, which is the reference for v1:
|
|
10
|
+
|
|
11
|
+
- matching is **presence-based**, not occurrence-counted: a keyword appearing
|
|
12
|
+
five times scores the same as appearing once (`if keyword in text`);
|
|
13
|
+
- spam indicators carry negative points and are summed with the rest;
|
|
14
|
+
- a single industry match multiplies the running total by 1.5, truncated to
|
|
15
|
+
int, and matching several industry terms still multiplies only once;
|
|
16
|
+
- the final score is floored at 0.
|
|
17
|
+
|
|
18
|
+
`match: count` is offered for agents that genuinely want "more mentions means a
|
|
19
|
+
stronger signal", but it is not the reference behaviour and is not the default.
|
|
20
|
+
|
|
21
|
+
An LLM-based scorer is the tracked future replacement (issue #9).
|
|
22
|
+
|
|
23
|
+
Contract: see docs/PLUGIN-CONTRACT.md.
|
|
24
|
+
"""
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import re
|
|
28
|
+
|
|
29
|
+
from framework.interfaces import ScoredSignal, Scorer, Signal
|
|
30
|
+
from framework.registry import register_scorer
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class RegexScorer:
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
rules: list[dict],
|
|
37
|
+
ignore_case: bool = True,
|
|
38
|
+
match: str = "presence",
|
|
39
|
+
multipliers: list[dict] | None = None,
|
|
40
|
+
floor: int | None = 0,
|
|
41
|
+
):
|
|
42
|
+
if match not in ("presence", "count"):
|
|
43
|
+
raise ValueError(f"regex scorer: match must be 'presence' or 'count', got {match!r}")
|
|
44
|
+
flags = re.IGNORECASE if ignore_case else 0
|
|
45
|
+
self._match = match
|
|
46
|
+
self._floor = floor
|
|
47
|
+
self._rules = [
|
|
48
|
+
(re.compile(r["pattern"], flags), int(r.get("points", 1))) for r in rules
|
|
49
|
+
]
|
|
50
|
+
self._multipliers = [
|
|
51
|
+
(re.compile(m["pattern"], flags), float(m.get("factor", 1.0)))
|
|
52
|
+
for m in (multipliers or [])
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
def score(self, signal: Signal) -> ScoredSignal:
|
|
56
|
+
total = 0
|
|
57
|
+
reasons: list[str] = []
|
|
58
|
+
|
|
59
|
+
for pattern, points in self._rules:
|
|
60
|
+
if self._match == "presence":
|
|
61
|
+
if pattern.search(signal.text):
|
|
62
|
+
total += points
|
|
63
|
+
reasons.append(f"{pattern.pattern} (+{points})")
|
|
64
|
+
else:
|
|
65
|
+
hits = len(pattern.findall(signal.text))
|
|
66
|
+
if hits:
|
|
67
|
+
total += hits * points
|
|
68
|
+
reasons.append(f"{pattern.pattern} x{hits} (+{hits * points})")
|
|
69
|
+
|
|
70
|
+
# Multipliers apply once each, after the additive pass — matching the
|
|
71
|
+
# reference, where any industry hit boosts the whole score by 1.5x.
|
|
72
|
+
for pattern, factor in self._multipliers:
|
|
73
|
+
if pattern.search(signal.text):
|
|
74
|
+
total = int(total * factor)
|
|
75
|
+
reasons.append(f"{pattern.pattern} (x{factor})")
|
|
76
|
+
|
|
77
|
+
if self._floor is not None:
|
|
78
|
+
total = max(self._floor, total)
|
|
79
|
+
|
|
80
|
+
retval = ScoredSignal(signal=signal, score=total, reasons=reasons)
|
|
81
|
+
return retval
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@register_scorer("regex")
|
|
85
|
+
def build(cfg: dict) -> Scorer:
|
|
86
|
+
retval = RegexScorer(
|
|
87
|
+
rules=cfg.get("rules", []),
|
|
88
|
+
ignore_case=cfg.get("ignore_case", True),
|
|
89
|
+
match=cfg.get("match", "presence"),
|
|
90
|
+
multipliers=cfg.get("multipliers"),
|
|
91
|
+
floor=cfg.get("floor", 0),
|
|
92
|
+
)
|
|
93
|
+
return retval
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"""RSS source: a feed as a source (issue #149).
|
|
2
|
+
|
|
3
|
+
Every other source in this repo pulls. The reddit source fetches eight feeds a
|
|
4
|
+
run and pays in rate limits; the websearch source pays per query in credits.
|
|
5
|
+
A feed is the opposite shape: something else watches continuously and publishes
|
|
6
|
+
what it found, so a run costs one conditional GET, has no quota and no terms
|
|
7
|
+
question, and catches things that happened between runs rather than sampling.
|
|
8
|
+
|
|
9
|
+
Google Alerts is the case this was written for — Google watches a phrase and
|
|
10
|
+
publishes each hit to a per-alert feed — but nothing here is Alerts-specific
|
|
11
|
+
beyond one documented unwrapping, and an ordinary blog or forum feed works the
|
|
12
|
+
same way.
|
|
13
|
+
|
|
14
|
+
"source": {
|
|
15
|
+
"type": "rss",
|
|
16
|
+
"feeds": ["https://www.google.com/alerts/feeds/<alert-id>/<feed-id>"],
|
|
17
|
+
"limit_per_feed": 25
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
## The two things that are not obvious
|
|
21
|
+
|
|
22
|
+
**A Google Alerts link is a redirector.** Every entry links to
|
|
23
|
+
`google.com/url?...&url=<the real page>&...`, so the page a lead actually lives
|
|
24
|
+
on is a query parameter. Left wrapped, two alerts that both matched one page
|
|
25
|
+
produce two different urls that no destination can show as the same thing, and
|
|
26
|
+
the url that reaches a reader is a Google redirect rather than the page. So a
|
|
27
|
+
`google.com/url` link is unwrapped to its `url` parameter, and that is the only
|
|
28
|
+
host this module names.
|
|
29
|
+
|
|
30
|
+
**A Google Alerts guid is per-hit, not per-page.** The entry id is a
|
|
31
|
+
`tag:google.com,2013:googlealerts/feed:<n>` serial, unique to that alert's
|
|
32
|
+
delivery of that page. Two alerts matching one page therefore carry two guids,
|
|
33
|
+
and keying dedupe on the guid delivers the page twice. For those entries the
|
|
34
|
+
unwrapped url is the id instead. Every other feed keeps its guid, which is what
|
|
35
|
+
a guid is for: a feed that rewrites its links must not re-deliver its backlog.
|
|
36
|
+
|
|
37
|
+
## A feed URL is a credential, so it never reaches a log
|
|
38
|
+
|
|
39
|
+
A Google Alerts feed URL grants whoever holds it the contents of that alert, and
|
|
40
|
+
scheduled runs persist stderr. So nothing here prints a feed URL: feeds are
|
|
41
|
+
identified in every message, and in `meta`, by their configured `label`, or by
|
|
42
|
+
their position when no label is given. The same rule covers exception text,
|
|
43
|
+
which is why a request error is reported by its exception type rather than
|
|
44
|
+
echoed whole — `requests` puts the URL in the message.
|
|
45
|
+
|
|
46
|
+
That is also why `feeds` accepts an object form:
|
|
47
|
+
|
|
48
|
+
"feeds": [
|
|
49
|
+
{"url": "https://www.google.com/alerts/feeds/<id>/<id>", "label": "invoices"},
|
|
50
|
+
"https://forum.example/feed.xml"
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
## Dedupe is namespaced per feed
|
|
54
|
+
|
|
55
|
+
An RSS `guid` is unique only within its own feed — `<guid>123</guid>` is
|
|
56
|
+
perfectly legal in two unrelated publishers — and the runner's seen store is
|
|
57
|
+
shared across the whole agent. So an ordinary feed's signal id is its guid
|
|
58
|
+
prefixed with a stable key derived from the feed URL, and two feeds using the
|
|
59
|
+
same local id no longer suppress each other.
|
|
60
|
+
|
|
61
|
+
Google Alerts entries are the deliberate exception: their id is the unwrapped
|
|
62
|
+
page URL with no prefix, so that two *different* alerts matching one page
|
|
63
|
+
collapse to a single lead. Cross-feed collapsing is the goal there and a bug
|
|
64
|
+
everywhere else.
|
|
65
|
+
|
|
66
|
+
Changing a feed's URL changes its key and therefore re-delivers that feed's
|
|
67
|
+
current entries once. Renaming its label does not.
|
|
68
|
+
|
|
69
|
+
## Reporting
|
|
70
|
+
|
|
71
|
+
Per-feed counts on stderr, every failed feed named, and a raise when every feed
|
|
72
|
+
fails — the shape #135 settled for the reddit source. A feed that has gone
|
|
73
|
+
quiet and a feed that has started refusing look identical in a signal count of
|
|
74
|
+
zero, and this repo has shipped that defect once already.
|
|
75
|
+
|
|
76
|
+
Contract: see docs/PLUGIN-CONTRACT.md.
|
|
77
|
+
"""
|
|
78
|
+
from __future__ import annotations
|
|
79
|
+
|
|
80
|
+
import hashlib
|
|
81
|
+
import html
|
|
82
|
+
import re
|
|
83
|
+
import sys
|
|
84
|
+
import time
|
|
85
|
+
from urllib.parse import parse_qs, urlsplit
|
|
86
|
+
|
|
87
|
+
import feedparser
|
|
88
|
+
import requests
|
|
89
|
+
|
|
90
|
+
from framework.interfaces import Signal, Source
|
|
91
|
+
from framework.registry import register_source
|
|
92
|
+
|
|
93
|
+
_DEFAULT_USER_AGENT = "voitta-agents:rss-source/1.0 (+https://github.com/voitta-ai/agents)"
|
|
94
|
+
_DEFAULT_LABEL = "rss"
|
|
95
|
+
|
|
96
|
+
# The one host this module names, and why: see the module docstring.
|
|
97
|
+
_REDIRECT_HOSTS = ("google.com", "www.google.com")
|
|
98
|
+
_ALERTS_GUID_PREFIX = "tag:google.com,2013:googlealerts"
|
|
99
|
+
|
|
100
|
+
_TAG = re.compile(r"<[^>]+>")
|
|
101
|
+
_WS = re.compile(r"\s+")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _plain_text(raw: str) -> str:
|
|
105
|
+
"""Feed text as prose: tags dropped, entities resolved, whitespace collapsed.
|
|
106
|
+
|
|
107
|
+
Feed summaries are HTML written for a browser — Google Alerts wraps the
|
|
108
|
+
matched phrase in `<b>`, and a forum feed will send a whole styled excerpt.
|
|
109
|
+
Left alone it reaches a scorer as markup to match against and a reader as
|
|
110
|
+
literal angle brackets.
|
|
111
|
+
|
|
112
|
+
Entities are resolved BEFORE tags are stripped, which is the opposite order
|
|
113
|
+
to the reddit source's cleaner and is load-bearing. Feed markup arrives
|
|
114
|
+
escaped a varying number of times: an Atom `content type="html"` body is
|
|
115
|
+
escaped once by XML, feedparser resolves that, and a feed that escaped its
|
|
116
|
+
own HTML before publishing leaves a second layer behind. Strip first and
|
|
117
|
+
`<b>` survives as a literal `<b>` in the text a scorer matches
|
|
118
|
+
against and a reader reads. Unescape first and both layers come off.
|
|
119
|
+
|
|
120
|
+
The cost of that order is that text deliberately displaying `<b>` to
|
|
121
|
+
a reader loses it. In a feed of leads that is a rounding error; markup
|
|
122
|
+
reaching the scorer is the failure actually observed.
|
|
123
|
+
|
|
124
|
+
Deliberately simpler than the reddit source's `_clean_html`, which also
|
|
125
|
+
strips Reddit's own SC_OFF markers and its "submitted by" footer. Those are
|
|
126
|
+
artefacts of that transport, not of feeds in general. If a third consumer
|
|
127
|
+
turns up, the two belong in one shared module; two is not yet a pattern.
|
|
128
|
+
"""
|
|
129
|
+
if not raw:
|
|
130
|
+
return ""
|
|
131
|
+
text = html.unescape(raw)
|
|
132
|
+
text = _TAG.sub(" ", text)
|
|
133
|
+
# One more pass: an entity that was escaped twice is now a bare entity.
|
|
134
|
+
text = html.unescape(text)
|
|
135
|
+
retval = _WS.sub(" ", text).strip()
|
|
136
|
+
return retval
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _unwrap(url: str) -> str:
|
|
140
|
+
"""A redirector's target, or the url unchanged.
|
|
141
|
+
|
|
142
|
+
Only `google.com/url?...&url=<target>` is unwrapped, and only when the
|
|
143
|
+
parameter is there to take. An unrecognised link is returned as it came:
|
|
144
|
+
guessing at a redirector's shape would silently rewrite links this module
|
|
145
|
+
does not understand.
|
|
146
|
+
"""
|
|
147
|
+
if not url:
|
|
148
|
+
return ""
|
|
149
|
+
parts = urlsplit(url)
|
|
150
|
+
if parts.netloc.lower() not in _REDIRECT_HOSTS or parts.path != "/url":
|
|
151
|
+
return url
|
|
152
|
+
target = parse_qs(parts.query).get("url") or []
|
|
153
|
+
retval = target[0].strip() if target and target[0].strip() else url
|
|
154
|
+
return retval
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _feed_key(url: str) -> str:
|
|
158
|
+
"""A stable, non-secret per-feed prefix for dedupe ids.
|
|
159
|
+
|
|
160
|
+
Derived from the URL rather than the label so that renaming a feed does not
|
|
161
|
+
re-deliver it, and hashed rather than used raw so that a signal id -- which
|
|
162
|
+
is written to the seen store and shown in logs -- cannot carry a credential.
|
|
163
|
+
"""
|
|
164
|
+
retval = hashlib.sha1(url.encode("utf-8")).hexdigest()[:8]
|
|
165
|
+
return retval
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class RSSSource:
|
|
169
|
+
def __init__(
|
|
170
|
+
self,
|
|
171
|
+
feeds: list[tuple[str, str]],
|
|
172
|
+
user_agent: str = _DEFAULT_USER_AGENT,
|
|
173
|
+
limit_per_feed: int = 25,
|
|
174
|
+
delay_seconds: float = 1.0,
|
|
175
|
+
timeout: int = 20,
|
|
176
|
+
label: str = _DEFAULT_LABEL,
|
|
177
|
+
):
|
|
178
|
+
# (url, label) pairs. The label is what any message may name.
|
|
179
|
+
self.feeds = feeds
|
|
180
|
+
self.user_agent = user_agent
|
|
181
|
+
self.limit_per_feed = limit_per_feed
|
|
182
|
+
self.delay_seconds = delay_seconds
|
|
183
|
+
self.timeout = timeout
|
|
184
|
+
self.label = label
|
|
185
|
+
# Keyed by LABEL, never by url: these lists end up on stderr.
|
|
186
|
+
self.feed_errors: list[tuple[str, str]] = []
|
|
187
|
+
self.feed_counts: list[tuple[str, int]] = []
|
|
188
|
+
|
|
189
|
+
def _fetch_feed(self, url: str, label: str) -> list[dict]:
|
|
190
|
+
"""One feed's entries as plain dicts, or [] with the reason recorded."""
|
|
191
|
+
try:
|
|
192
|
+
resp = requests.get(
|
|
193
|
+
url, headers={"User-Agent": self.user_agent}, timeout=self.timeout
|
|
194
|
+
)
|
|
195
|
+
except requests.RequestException as exc:
|
|
196
|
+
# The exception's own text is NOT included: requests puts the full
|
|
197
|
+
# URL in it, and a feed URL is a credential. The type is enough to
|
|
198
|
+
# tell a timeout from a DNS failure from a refused connection.
|
|
199
|
+
self.feed_errors.append((label, type(exc).__name__))
|
|
200
|
+
return []
|
|
201
|
+
if not resp.ok:
|
|
202
|
+
self.feed_errors.append((label, f"{resp.status_code} {resp.reason}"))
|
|
203
|
+
return []
|
|
204
|
+
|
|
205
|
+
feed = feedparser.parse(resp.content)
|
|
206
|
+
# A feed that parsed to nothing AND reported a parse error is broken,
|
|
207
|
+
# not empty: feedparser sets `bozo` for malformed XML but still returns
|
|
208
|
+
# whatever it salvaged, so entries are worth more than the flag.
|
|
209
|
+
if not feed.entries and getattr(feed, "bozo", 0):
|
|
210
|
+
# The exception type only, for the reason above: feedparser's
|
|
211
|
+
# message can quote the document and its location.
|
|
212
|
+
why = type(getattr(feed, "bozo_exception", None)).__name__
|
|
213
|
+
self.feed_errors.append((label, f"unparseable: {why}"))
|
|
214
|
+
return []
|
|
215
|
+
|
|
216
|
+
entries = []
|
|
217
|
+
for entry in feed.entries[: self.limit_per_feed]:
|
|
218
|
+
link = _unwrap(str(entry.get("link") or "").strip())
|
|
219
|
+
guid = str(entry.get("id") or "").strip()
|
|
220
|
+
entries.append(
|
|
221
|
+
{
|
|
222
|
+
"guid": guid,
|
|
223
|
+
"link": link,
|
|
224
|
+
"title": _plain_text(str(entry.get("title") or "")),
|
|
225
|
+
"summary": _plain_text(str(entry.get("summary") or "")),
|
|
226
|
+
"author": str(entry.get("author") or ""),
|
|
227
|
+
"published": str(entry.get("published") or ""),
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
return entries
|
|
231
|
+
|
|
232
|
+
def _to_signal(self, entry: dict, label: str, key: str) -> Signal:
|
|
233
|
+
guid = entry["guid"]
|
|
234
|
+
link = entry["link"]
|
|
235
|
+
# See the module docstring: an Alerts guid identifies the delivery, the
|
|
236
|
+
# url identifies the page, and it is the page we must not see twice.
|
|
237
|
+
# That id stays unprefixed on purpose, so two alerts matching one page
|
|
238
|
+
# collapse; every other feed's guid is namespaced, because a guid is
|
|
239
|
+
# unique only within the feed that issued it.
|
|
240
|
+
if not guid or guid.startswith(_ALERTS_GUID_PREFIX):
|
|
241
|
+
signal_id = link or (f"{key}:{guid}" if guid else "")
|
|
242
|
+
else:
|
|
243
|
+
signal_id = f"{key}:{guid}"
|
|
244
|
+
title = entry["title"]
|
|
245
|
+
summary = entry["summary"]
|
|
246
|
+
retval = Signal(
|
|
247
|
+
id=signal_id,
|
|
248
|
+
text=f"{title}\n\n{summary}".strip(),
|
|
249
|
+
url=link,
|
|
250
|
+
source=self.label,
|
|
251
|
+
meta={
|
|
252
|
+
"title": title,
|
|
253
|
+
"author": entry["author"],
|
|
254
|
+
"published": entry["published"],
|
|
255
|
+
# The label, not the url: meta reaches destinations and state.
|
|
256
|
+
"feed": label,
|
|
257
|
+
},
|
|
258
|
+
)
|
|
259
|
+
return retval
|
|
260
|
+
|
|
261
|
+
def fetch(self) -> list[Signal]:
|
|
262
|
+
self.feed_errors = []
|
|
263
|
+
self.feed_counts = []
|
|
264
|
+
signals: list[Signal] = []
|
|
265
|
+
seen_ids: set[str] = set()
|
|
266
|
+
|
|
267
|
+
for index, (feed_url, label) in enumerate(self.feeds):
|
|
268
|
+
if index and self.delay_seconds:
|
|
269
|
+
time.sleep(self.delay_seconds)
|
|
270
|
+
key = _feed_key(feed_url)
|
|
271
|
+
failures_before = len(self.feed_errors)
|
|
272
|
+
entries = self._fetch_feed(feed_url, label)
|
|
273
|
+
# [] means both "empty feed" and "failed feed"; the error list
|
|
274
|
+
# growing across the call is what separates them.
|
|
275
|
+
if len(self.feed_errors) == failures_before:
|
|
276
|
+
self.feed_counts.append((label, len(entries)))
|
|
277
|
+
for entry in entries:
|
|
278
|
+
signal = self._to_signal(entry, label, key)
|
|
279
|
+
# Two alerts on one page, or one page in two feeds, is one lead.
|
|
280
|
+
if not signal.id or signal.id in seen_ids:
|
|
281
|
+
continue
|
|
282
|
+
seen_ids.add(signal.id)
|
|
283
|
+
signals.append(signal)
|
|
284
|
+
|
|
285
|
+
if self.feeds:
|
|
286
|
+
got = sum(n for _, n in self.feed_counts)
|
|
287
|
+
print(
|
|
288
|
+
f"rss source: {len(self.feed_counts)}/{len(self.feeds)} feed(s) answered "
|
|
289
|
+
f"with {got} entry/entries, {len(signals)} after dedupe",
|
|
290
|
+
file=sys.stderr,
|
|
291
|
+
)
|
|
292
|
+
if self.feed_errors:
|
|
293
|
+
print(
|
|
294
|
+
f"rss source: {len(self.feed_errors)} of {len(self.feeds)} feed(s) failed "
|
|
295
|
+
f"and their entries are missing from this run — "
|
|
296
|
+
f"{'; '.join(f'{name}: {why}' for name, why in self.feed_errors)}",
|
|
297
|
+
file=sys.stderr,
|
|
298
|
+
)
|
|
299
|
+
# A wholly failed run must not read as a quiet one.
|
|
300
|
+
if self.feeds and len(self.feed_errors) == len(self.feeds):
|
|
301
|
+
detail = "; ".join(f"{name}: {why}" for name, why in self.feed_errors[:3])
|
|
302
|
+
raise RuntimeError(
|
|
303
|
+
f"rss source: all {len(self.feeds)} feed(s) failed — {detail}"
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
retval = signals
|
|
307
|
+
return retval
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _normalise_feeds(declared: object) -> list[tuple[str, str]]:
|
|
311
|
+
"""(url, label) per configured feed. Accepts a URL string or {url, label}.
|
|
312
|
+
|
|
313
|
+
Errors name the feed's POSITION, never its URL, for the reason in the
|
|
314
|
+
module docstring: these messages are read from logs.
|
|
315
|
+
"""
|
|
316
|
+
if not isinstance(declared, list) or not declared:
|
|
317
|
+
raise RuntimeError("rss source needs a non-empty 'feeds' list")
|
|
318
|
+
retval: list[tuple[str, str]] = []
|
|
319
|
+
for index, entry in enumerate(declared, start=1):
|
|
320
|
+
if isinstance(entry, str):
|
|
321
|
+
url, label = entry.strip(), ""
|
|
322
|
+
elif isinstance(entry, dict):
|
|
323
|
+
url = str(entry.get("url") or "").strip()
|
|
324
|
+
label = str(entry.get("label") or "").strip()
|
|
325
|
+
else:
|
|
326
|
+
raise RuntimeError(
|
|
327
|
+
f"rss source: feed {index} must be a URL string or an object with 'url'"
|
|
328
|
+
)
|
|
329
|
+
if not url:
|
|
330
|
+
raise RuntimeError(f"rss source: feed {index} has no 'url'")
|
|
331
|
+
retval.append((url, label or f"feed {index}"))
|
|
332
|
+
return retval
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
@register_source("rss")
|
|
336
|
+
def build(cfg: dict) -> Source:
|
|
337
|
+
feeds = _normalise_feeds(cfg.get("feeds"))
|
|
338
|
+
retval = RSSSource(
|
|
339
|
+
feeds=feeds,
|
|
340
|
+
user_agent=cfg.get("user_agent", _DEFAULT_USER_AGENT),
|
|
341
|
+
limit_per_feed=int(cfg.get("limit_per_feed", 25)),
|
|
342
|
+
delay_seconds=float(cfg.get("delay_seconds", 1.0)),
|
|
343
|
+
timeout=int(cfg.get("timeout", 20)),
|
|
344
|
+
label=cfg.get("label", _DEFAULT_LABEL),
|
|
345
|
+
)
|
|
346
|
+
return retval
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Signal destination — one scored signal, one Signal message (issue #120).
|
|
2
|
+
|
|
3
|
+
```json
|
|
4
|
+
"destination": {
|
|
5
|
+
"type": "signal-messenger",
|
|
6
|
+
"account_env": "AGENTS_SIGNAL_ACCOUNT",
|
|
7
|
+
"group_id_env": "AGENTS_SIGNAL_GROUP_LEADS",
|
|
8
|
+
"min_score": 10
|
|
9
|
+
}
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The type is `signal-messenger`, not `signal`. The `approval` destination
|
|
13
|
+
already has a `mode: "signal"` meaning one proposal per `Signal` — the
|
|
14
|
+
dataclass every source emits — and a destination called `signal` would read as
|
|
15
|
+
that one, in configs where both appear together.
|
|
16
|
+
|
|
17
|
+
Deliberately narrow next to the Slack destination:
|
|
18
|
+
|
|
19
|
+
- **One target, no routes.** Routes exist for the hot/firehose split, which is
|
|
20
|
+
a Slack channel pattern. A Signal group is somebody's phone.
|
|
21
|
+
- **Streaming only.** No batch mode, because the digest destination is the
|
|
22
|
+
thing that summarises a run, and sending Signal a digest is a separate piece
|
|
23
|
+
of work — `digest` picks its transport at construction, in Slack's shape.
|
|
24
|
+
- **No drafter.** The drafted block belongs to the agent that needs it; this
|
|
25
|
+
renders what it is given.
|
|
26
|
+
|
|
27
|
+
What it does carry over is `accepts()` and `render_signal()`, so the `approval`
|
|
28
|
+
destination's signal mode can wrap it and a lead looks the same whether it is
|
|
29
|
+
posted plainly or proposed for approval.
|
|
30
|
+
"""
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
from framework.interfaces import Destination, ScoredSignal
|
|
34
|
+
from framework.registry import register_destination
|
|
35
|
+
from framework.signal_messenger import (
|
|
36
|
+
Transport,
|
|
37
|
+
check_footer,
|
|
38
|
+
post_message,
|
|
39
|
+
transport_from_config,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
_LABEL = "signal-messenger destination"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class SignalDestination:
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
transport: Transport,
|
|
49
|
+
min_score: int = 0,
|
|
50
|
+
footer: str = "",
|
|
51
|
+
show_reasons: bool = True,
|
|
52
|
+
):
|
|
53
|
+
self.transport = transport
|
|
54
|
+
self.min_score = min_score
|
|
55
|
+
self.footer = footer
|
|
56
|
+
self.show_reasons = show_reasons
|
|
57
|
+
|
|
58
|
+
def _format(self, scored: ScoredSignal) -> str:
|
|
59
|
+
"""One signal as Markdown, which the transport renders for Signal.
|
|
60
|
+
|
|
61
|
+
The same shape the Slack destination streams, for the same reason: the
|
|
62
|
+
score first because it is what a reader triages on, then the signal's
|
|
63
|
+
own words, then its link, then — set apart and in italics — the
|
|
64
|
+
scorer's reasons, which are ours about them rather than theirs.
|
|
65
|
+
"""
|
|
66
|
+
sig = scored.signal
|
|
67
|
+
text = f"**[{scored.score}]** {sig.text}"
|
|
68
|
+
if sig.url:
|
|
69
|
+
text += f"\n{sig.url}"
|
|
70
|
+
if self.show_reasons and scored.reasons:
|
|
71
|
+
text += "\n\n" + "\n".join(f"- _{reason}_" for reason in scored.reasons)
|
|
72
|
+
retval = text
|
|
73
|
+
return retval
|
|
74
|
+
|
|
75
|
+
def accepts(self, scored: ScoredSignal) -> bool:
|
|
76
|
+
"""Whether this signal clears the gate. No send, no model call."""
|
|
77
|
+
retval = scored.score >= self.min_score
|
|
78
|
+
return retval
|
|
79
|
+
|
|
80
|
+
def render_signal(self, scored: ScoredSignal) -> dict | None:
|
|
81
|
+
"""The message this would send, without sending it (issue #65).
|
|
82
|
+
|
|
83
|
+
The footer is kept apart because the `approval` destination posts
|
|
84
|
+
through its own transport and appends it itself.
|
|
85
|
+
"""
|
|
86
|
+
if not self.accepts(scored):
|
|
87
|
+
return None
|
|
88
|
+
retval = {"text": self._format(scored), "footer": self.footer}
|
|
89
|
+
return retval
|
|
90
|
+
|
|
91
|
+
def send(self, scored: ScoredSignal) -> None:
|
|
92
|
+
if not self.accepts(scored):
|
|
93
|
+
return
|
|
94
|
+
# The signal's own id as the seed, not the rendered text: a retry
|
|
95
|
+
# re-scores, and `llm_scorer` renders the waterfall rung it landed on
|
|
96
|
+
# into the reasons, so the text moves between runs while the id does
|
|
97
|
+
# not. See `GatewayTransport._key`.
|
|
98
|
+
post_message(
|
|
99
|
+
self.transport,
|
|
100
|
+
self._format(scored),
|
|
101
|
+
footer=self.footer,
|
|
102
|
+
seed=scored.signal.id,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@register_destination("signal-messenger")
|
|
107
|
+
def build(cfg: dict) -> Destination:
|
|
108
|
+
timeout = int(cfg.get("timeout", 120))
|
|
109
|
+
transport = transport_from_config(cfg, _LABEL, timeout)
|
|
110
|
+
|
|
111
|
+
show_reasons = cfg.get("show_reasons", True)
|
|
112
|
+
if not isinstance(show_reasons, bool):
|
|
113
|
+
raise RuntimeError(f"{_LABEL}: 'show_reasons' must be true or false")
|
|
114
|
+
|
|
115
|
+
footer = cfg.get("footer", "")
|
|
116
|
+
if not isinstance(footer, str):
|
|
117
|
+
raise RuntimeError(f"{_LABEL}: 'footer' must be a string")
|
|
118
|
+
# Here rather than only at send time: a footer too long to leave room for a
|
|
119
|
+
# message is knowable before the run fetches, scores or asks a model
|
|
120
|
+
# anything, and left unchecked it turns one scored signal into thousands of
|
|
121
|
+
# one-character Signal messages. The same guard `slack_destination` makes.
|
|
122
|
+
# Against the transport's own limit, not Signal's: a gateway that puts a
|
|
123
|
+
# label in front of every message has fewer bytes to give the footer, and
|
|
124
|
+
# a check against the constant would pass a footer the run then cannot fit.
|
|
125
|
+
check_footer(footer, f"{_LABEL}: 'footer'", limit=transport.limit)
|
|
126
|
+
|
|
127
|
+
retval = SignalDestination(
|
|
128
|
+
transport=transport,
|
|
129
|
+
min_score=int(cfg.get("min_score", 0)),
|
|
130
|
+
footer=footer,
|
|
131
|
+
show_reasons=show_reasons,
|
|
132
|
+
)
|
|
133
|
+
return retval
|