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,210 @@
|
|
|
1
|
+
"""Twitter (X) post action (issue #48).
|
|
2
|
+
|
|
3
|
+
One step of an approval-gated sequence: after a post has been published, announce
|
|
4
|
+
it with a tweet. Configured as a `steps` entry of an action:
|
|
5
|
+
|
|
6
|
+
{"type": "twitter-post", "text": "{blurb_twitter} {url}"}
|
|
7
|
+
|
|
8
|
+
`text` is rendered with `framework.templating.render_template` against the
|
|
9
|
+
step's payload, so values produced by earlier steps (`{url}` from the publishing
|
|
10
|
+
step, say) and the proposal's own fields are available by name. Only plain
|
|
11
|
+
names are accepted: a field that reaches into a value (`{url.__class__}`,
|
|
12
|
+
`{result[0]}`) is refused before anything is sent, so a template cannot pull an
|
|
13
|
+
object's internals into a public tweet. A template that names a key the
|
|
14
|
+
payload does not carry is an error naming that key, not an empty substitution:
|
|
15
|
+
a tweet announcing "None" is worse than no tweet.
|
|
16
|
+
|
|
17
|
+
The rendered text is checked against the 280-character limit *before* any
|
|
18
|
+
request goes out, and a longer text is an error, not a truncation. Truncating
|
|
19
|
+
would post something the human did not approve.
|
|
20
|
+
|
|
21
|
+
The check counts URLs the way X does. Every link, whatever its length, is
|
|
22
|
+
wrapped to t.co and counts as 23 characters, so a plain `len()` over-counts a
|
|
23
|
+
text carrying a long URL and refuses tweets X would have accepted. That is not
|
|
24
|
+
hypothetical: a release-notes announcement with a 208-character blurb and a
|
|
25
|
+
95-character permalink measured 304 by `len()` and was refused, where X would
|
|
26
|
+
have counted 232 and posted it. A false refusal is worse than it looks here,
|
|
27
|
+
because this runs as a step of an approved action, after a human has read the
|
|
28
|
+
text and after earlier steps have already published.
|
|
29
|
+
|
|
30
|
+
What is still not reproduced is X's *other* weighting rule: some scripts
|
|
31
|
+
(CJK and similar) count as 2 characters each. A text that passes this check and
|
|
32
|
+
still exceeds X's count is rejected by the API with a 403, which is just as
|
|
33
|
+
loud. The asymmetry is deliberate -- this check now errs toward letting the API
|
|
34
|
+
have the final word, rather than toward refusing locally.
|
|
35
|
+
|
|
36
|
+
Posting goes through the X API v2 (`POST /2/tweets`) with OAuth 1.0a user
|
|
37
|
+
context: the tweet appears on the account that owns the access token. The
|
|
38
|
+
signing is done by `requests-oauthlib`, an optional dependency (`pip install
|
|
39
|
+
'.[twitter]'`). It is imported at module scope on purpose: the plugin loader
|
|
40
|
+
(issue #40) tolerates a missing external package here, keeps every other plugin
|
|
41
|
+
working, and turns a config that names `twitter-post` on a venv without the
|
|
42
|
+
package into a startup error quoting the missing module.
|
|
43
|
+
|
|
44
|
+
Credentials are read from the environment only. The config names the variables;
|
|
45
|
+
the defaults are the four an X developer app hands out:
|
|
46
|
+
|
|
47
|
+
api_key_env: TWITTER_API_KEY (consumer key)
|
|
48
|
+
api_secret_env: TWITTER_API_SECRET (consumer secret)
|
|
49
|
+
access_token_env: TWITTER_ACCESS_TOKEN
|
|
50
|
+
access_secret_env: TWITTER_ACCESS_SECRET
|
|
51
|
+
|
|
52
|
+
They are read in `execute`, not at construction, so a sequence whose earlier
|
|
53
|
+
steps do not need Twitter is built and started regardless, and the approval
|
|
54
|
+
runner reports *this* step as the one waiting on credentials. A missing variable
|
|
55
|
+
raises naming it. Nothing in this module logs a header, a request body or a
|
|
56
|
+
token at any level. `timeout` (seconds, default 30) bounds the request.
|
|
57
|
+
|
|
58
|
+
`execute` returns `{"tweet_url": "https://x.com/i/status/<id>"}`. The `/i/status/`
|
|
59
|
+
form resolves without knowing the account's handle, which the create response
|
|
60
|
+
does not include.
|
|
61
|
+
|
|
62
|
+
A 2xx from X means the tweet exists, so nothing after one fails the step. A
|
|
63
|
+
2xx body that cannot be read or carries no tweet id still returns, with
|
|
64
|
+
`{"tweet_url": ""}` and one notice on stderr: failing there would report an
|
|
65
|
+
announcement that went out as one that did not, and the approvals re-run
|
|
66
|
+
procedure (set the proposal back to `pending`) would post it a second time.
|
|
67
|
+
|
|
68
|
+
- **`POST /2/tweets` is not idempotent, and nothing here retries.** A timeout
|
|
69
|
+
or dropped connection after X accepted the request can leave a tweet that
|
|
70
|
+
this action reports as failed, so check the account before re-running by
|
|
71
|
+
hand.
|
|
72
|
+
"""
|
|
73
|
+
from __future__ import annotations
|
|
74
|
+
|
|
75
|
+
import re
|
|
76
|
+
|
|
77
|
+
import os
|
|
78
|
+
import sys
|
|
79
|
+
from typing import Any
|
|
80
|
+
|
|
81
|
+
import requests
|
|
82
|
+
from requests_oauthlib import OAuth1
|
|
83
|
+
|
|
84
|
+
from framework.interfaces import Action
|
|
85
|
+
from framework.registry import register_action
|
|
86
|
+
from framework.templating import render_template
|
|
87
|
+
|
|
88
|
+
TWEETS_URL = "https://api.x.com/2/tweets"
|
|
89
|
+
TWEET_MAX_CHARS = 280
|
|
90
|
+
# Every URL counts as this, whatever its real length: X wraps links to t.co.
|
|
91
|
+
# https://developer.x.com/en/docs/counting-characters
|
|
92
|
+
TCO_URL_CHARS = 23
|
|
93
|
+
_URL_RE = re.compile(r"https?://\S+")
|
|
94
|
+
|
|
95
|
+
_DEFAULT_ENV = {
|
|
96
|
+
"api_key_env": "TWITTER_API_KEY",
|
|
97
|
+
"api_secret_env": "TWITTER_API_SECRET",
|
|
98
|
+
"access_token_env": "TWITTER_ACCESS_TOKEN",
|
|
99
|
+
"access_secret_env": "TWITTER_ACCESS_SECRET",
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _require_env(name: str) -> str:
|
|
104
|
+
value = os.environ.get(name)
|
|
105
|
+
if not value:
|
|
106
|
+
raise RuntimeError(f"twitter-post needs a credential in ${name}")
|
|
107
|
+
return value
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _error_detail(resp: requests.Response) -> str:
|
|
111
|
+
"""The `title` / `detail` pair X puts in an error body, or nothing.
|
|
112
|
+
|
|
113
|
+
Only those two keys are surfaced, never the body as a whole: the message is
|
|
114
|
+
for the operator reading a failed step, and two short strings are what X
|
|
115
|
+
uses to say *why* (duplicate content, over the limit, wrong credentials).
|
|
116
|
+
"""
|
|
117
|
+
retval = ""
|
|
118
|
+
try:
|
|
119
|
+
body = resp.json()
|
|
120
|
+
except ValueError:
|
|
121
|
+
body = None
|
|
122
|
+
if isinstance(body, dict):
|
|
123
|
+
parts = [str(body[k]) for k in ("title", "detail") if body.get(k)]
|
|
124
|
+
retval = ": " + " - ".join(parts) if parts else ""
|
|
125
|
+
return retval
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _tweet_id(resp: requests.Response) -> str:
|
|
129
|
+
"""`data.id` from a create response, or "" if the body has no readable id."""
|
|
130
|
+
retval = ""
|
|
131
|
+
try:
|
|
132
|
+
body = resp.json()
|
|
133
|
+
except ValueError:
|
|
134
|
+
body = None
|
|
135
|
+
data = body.get("data") if isinstance(body, dict) else None
|
|
136
|
+
if isinstance(data, dict) and data.get("id"):
|
|
137
|
+
retval = str(data["id"])
|
|
138
|
+
return retval
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def weighted_len(text: str) -> int:
|
|
142
|
+
"""X's character count, for the URL rule only.
|
|
143
|
+
|
|
144
|
+
Each URL counts as `TCO_URL_CHARS` however long it is, because X rewrites
|
|
145
|
+
every link to a fixed-width t.co address. Nothing else is weighted here;
|
|
146
|
+
see the module docstring for what that leaves to the API.
|
|
147
|
+
"""
|
|
148
|
+
retval = len(_URL_RE.sub("u" * TCO_URL_CHARS, text))
|
|
149
|
+
return retval
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class TwitterPostAction:
|
|
153
|
+
def __init__(self, text: str, env_names: dict[str, str], timeout: int = 30):
|
|
154
|
+
self._text = text
|
|
155
|
+
self._env_names = env_names
|
|
156
|
+
self._timeout = timeout
|
|
157
|
+
|
|
158
|
+
def _auth(self) -> OAuth1:
|
|
159
|
+
retval = OAuth1(
|
|
160
|
+
_require_env(self._env_names["api_key_env"]),
|
|
161
|
+
_require_env(self._env_names["api_secret_env"]),
|
|
162
|
+
_require_env(self._env_names["access_token_env"]),
|
|
163
|
+
_require_env(self._env_names["access_secret_env"]),
|
|
164
|
+
)
|
|
165
|
+
return retval
|
|
166
|
+
|
|
167
|
+
def execute(self, payload: dict[str, Any], context: dict[str, Any]) -> dict[str, Any]:
|
|
168
|
+
text = render_template(self._text, payload)
|
|
169
|
+
weighted = weighted_len(text)
|
|
170
|
+
if weighted > TWEET_MAX_CHARS:
|
|
171
|
+
raise ValueError(
|
|
172
|
+
f"twitter-post: rendered text counts as {weighted} characters "
|
|
173
|
+
f"(X counts each URL as {TCO_URL_CHARS}), over the "
|
|
174
|
+
f"{TWEET_MAX_CHARS} limit; nothing was posted"
|
|
175
|
+
)
|
|
176
|
+
resp = requests.post(
|
|
177
|
+
TWEETS_URL,
|
|
178
|
+
json={"text": text},
|
|
179
|
+
auth=self._auth(),
|
|
180
|
+
timeout=self._timeout,
|
|
181
|
+
)
|
|
182
|
+
if not 200 <= resp.status_code < 300:
|
|
183
|
+
raise RuntimeError(
|
|
184
|
+
f"twitter-post: POST /2/tweets returned {resp.status_code}{_error_detail(resp)}"
|
|
185
|
+
)
|
|
186
|
+
tweet_id = _tweet_id(resp)
|
|
187
|
+
if tweet_id:
|
|
188
|
+
retval = {"tweet_url": f"https://x.com/i/status/{tweet_id}"}
|
|
189
|
+
else:
|
|
190
|
+
print(
|
|
191
|
+
f"agent: twitter-post: POST /2/tweets returned {resp.status_code}, so the tweet "
|
|
192
|
+
"was posted, but the response carried no tweet id; tweet_url is empty",
|
|
193
|
+
file=sys.stderr,
|
|
194
|
+
)
|
|
195
|
+
retval = {"tweet_url": ""}
|
|
196
|
+
return retval
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@register_action("twitter-post")
|
|
200
|
+
def build(cfg: dict) -> Action:
|
|
201
|
+
text = cfg.get("text")
|
|
202
|
+
if not isinstance(text, str) or not text:
|
|
203
|
+
raise ValueError("twitter-post: 'text' (a template string) is required")
|
|
204
|
+
env_names = {key: cfg.get(key, default) for key, default in _DEFAULT_ENV.items()}
|
|
205
|
+
retval = TwitterPostAction(
|
|
206
|
+
text=text,
|
|
207
|
+
env_names=env_names,
|
|
208
|
+
timeout=int(cfg.get("timeout", 30)),
|
|
209
|
+
)
|
|
210
|
+
return retval
|