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,248 @@
|
|
|
1
|
+
"""Command action: run an existing tool as one step of an approved action (issue #48).
|
|
2
|
+
|
|
3
|
+
Some things an approved proposal should do already have a working tool outside
|
|
4
|
+
this repo: a posting script that knows a network's escaping rules, link cards
|
|
5
|
+
and token expiry. Reimplementing that as a plugin gives two code paths for one
|
|
6
|
+
job, so this step runs the tool instead:
|
|
7
|
+
|
|
8
|
+
{"type": "command",
|
|
9
|
+
"argv": ["/usr/local/bin/some-post-tool", "--text", "{blurb} {url}"],
|
|
10
|
+
"result_format": "json"}
|
|
11
|
+
|
|
12
|
+
Config keys:
|
|
13
|
+
|
|
14
|
+
argv list of strings, required. `argv[0]` is the program and must
|
|
15
|
+
be a literal: a `{` or `}` in it is a config error. Every
|
|
16
|
+
later element is rendered with
|
|
17
|
+
`framework.templating.render_template` against the payload
|
|
18
|
+
(the proposal's content plus earlier steps' results).
|
|
19
|
+
cwd working directory for the tool, optional. Not templated.
|
|
20
|
+
timeout seconds, default 120. A tool still running then is killed
|
|
21
|
+
and the step fails.
|
|
22
|
+
result_pattern regex with named groups, searched (`re.search`) in stdout;
|
|
23
|
+
the groups become the step's results. Use `(?m)` for `^`/`$`
|
|
24
|
+
to anchor per line.
|
|
25
|
+
result_format "json": the last non-empty stdout line is parsed as a JSON
|
|
26
|
+
object and its keys become the step's results.
|
|
27
|
+
|
|
28
|
+
At most one of `result_pattern` / `result_format` may be set; with neither, the
|
|
29
|
+
step returns no results.
|
|
30
|
+
|
|
31
|
+
The program comes from config and nowhere else. The payload is the proposal's
|
|
32
|
+
content, which can trace back to text an upstream source picked up, so a
|
|
33
|
+
placeholder in `argv[0]` would let that text choose what runs. `build()`
|
|
34
|
+
refuses one outright rather than trusting every config author to notice.
|
|
35
|
+
|
|
36
|
+
The tool is run from an argv list, never through a shell. Post text is exactly
|
|
37
|
+
the kind of value that carries quotes, `$(...)` and backticks, and every one of
|
|
38
|
+
those has to reach the tool as the literal characters of one argument. Templating
|
|
39
|
+
each element separately and handing the list to `subprocess.run` keeps an element
|
|
40
|
+
one argument no matter what it renders to; there is no command line to split or
|
|
41
|
+
quote.
|
|
42
|
+
|
|
43
|
+
The environment is the agent's own, passed explicitly (`env={**os.environ}`):
|
|
44
|
+
a tool reads its credentials and `HOME` from there, and a hand-built env dict
|
|
45
|
+
would silently drop them. Under launchd `PATH` is minimal, so give `argv[0]` as
|
|
46
|
+
an absolute path.
|
|
47
|
+
|
|
48
|
+
Output is never buffered in memory. stdout and stderr go to anonymous temporary
|
|
49
|
+
files (unlinked on creation, closed on every path out of `execute`), and only a
|
|
50
|
+
bounded part is read back: the last 64 KiB of stderr for the error tail, and at
|
|
51
|
+
most 1 MiB of stdout for result parsing. A tool that writes more than 1 MiB to
|
|
52
|
+
stdout while a result mode is set fails the step as too large, since the result
|
|
53
|
+
line may be in the part that was not read. With no result mode stdout is never
|
|
54
|
+
read.
|
|
55
|
+
|
|
56
|
+
Failure is loud in every direction, because the approvals runner stops the
|
|
57
|
+
sequence at a failed step and posts its message to the approvals channel:
|
|
58
|
+
|
|
59
|
+
- A nonzero exit fails the step with the exit code and the last 20 lines of the
|
|
60
|
+
tool's stderr, so a tool's own explanation ("re-mint needed: ...") reaches the
|
|
61
|
+
human verbatim.
|
|
62
|
+
- A timeout, or a tool that cannot be started, fails the step.
|
|
63
|
+
- Exit 0 whose stdout does not parse as configured also fails: no match for
|
|
64
|
+
`result_pattern`, a named group that did not take part in the match, a last
|
|
65
|
+
line that is not a JSON object, stdout that is not UTF-8, or stdout over the
|
|
66
|
+
1 MiB limit. A later step templating `{url}` from a result that never arrived
|
|
67
|
+
is the silent failure this avoids.
|
|
68
|
+
|
|
69
|
+
Only `argv[0]` ever appears in a message this step produces. The other elements
|
|
70
|
+
are rendered post text and may hold anything the proposal did; stdout is not
|
|
71
|
+
quoted either. `subprocess.TimeoutExpired` renders the whole argv in its
|
|
72
|
+
message, so it is replaced rather than chained. Nothing here logs.
|
|
73
|
+
|
|
74
|
+
Contract: see docs/PLUGIN-CONTRACT.md.
|
|
75
|
+
"""
|
|
76
|
+
from __future__ import annotations
|
|
77
|
+
|
|
78
|
+
import json
|
|
79
|
+
import os
|
|
80
|
+
import re
|
|
81
|
+
import subprocess
|
|
82
|
+
import tempfile
|
|
83
|
+
from typing import IO, Any
|
|
84
|
+
|
|
85
|
+
from framework.config import ConfigError
|
|
86
|
+
from framework.interfaces import Action
|
|
87
|
+
from framework.registry import register_action
|
|
88
|
+
from framework.templating import render_template
|
|
89
|
+
|
|
90
|
+
STDERR_TAIL_LINES = 20
|
|
91
|
+
STDERR_TAIL_BYTES = 64 * 1024
|
|
92
|
+
STDOUT_MAX_BYTES = 1024 * 1024
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _stderr_tail(err: IO[bytes]) -> str:
|
|
96
|
+
"""The last `STDERR_TAIL_LINES` lines within the last 64 KiB of stderr."""
|
|
97
|
+
size = err.seek(0, os.SEEK_END)
|
|
98
|
+
err.seek(max(0, size - STDERR_TAIL_BYTES))
|
|
99
|
+
text = err.read().decode("utf-8", errors="replace")
|
|
100
|
+
lines = text.rstrip().splitlines()[-STDERR_TAIL_LINES:]
|
|
101
|
+
retval = "\n".join(lines)
|
|
102
|
+
return retval
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class CommandAction:
|
|
106
|
+
def __init__(
|
|
107
|
+
self,
|
|
108
|
+
argv: list[str],
|
|
109
|
+
cwd: str | None,
|
|
110
|
+
timeout: int,
|
|
111
|
+
pattern: re.Pattern | None,
|
|
112
|
+
result_format: str | None,
|
|
113
|
+
):
|
|
114
|
+
self._argv = argv
|
|
115
|
+
self._cwd = cwd
|
|
116
|
+
self._timeout = timeout
|
|
117
|
+
self._pattern = pattern
|
|
118
|
+
self._result_format = result_format
|
|
119
|
+
|
|
120
|
+
def _stdout(self, exe: str, out: IO[bytes]) -> str:
|
|
121
|
+
size = out.seek(0, os.SEEK_END)
|
|
122
|
+
if size > STDOUT_MAX_BYTES:
|
|
123
|
+
raise RuntimeError(
|
|
124
|
+
f"command {exe} exited 0 but wrote {size} bytes to stdout, over the "
|
|
125
|
+
f"{STDOUT_MAX_BYTES}-byte limit for reading its result; output too large"
|
|
126
|
+
)
|
|
127
|
+
out.seek(0)
|
|
128
|
+
try:
|
|
129
|
+
retval = out.read().decode("utf-8")
|
|
130
|
+
except UnicodeDecodeError as exc:
|
|
131
|
+
raise RuntimeError(
|
|
132
|
+
f"command {exe} exited 0 but its stdout is not UTF-8 "
|
|
133
|
+
f"({exc.reason} at byte {exc.start})"
|
|
134
|
+
) from None
|
|
135
|
+
return retval
|
|
136
|
+
|
|
137
|
+
def _results(self, exe: str, out: IO[bytes]) -> dict[str, Any]:
|
|
138
|
+
retval: dict[str, Any] = {}
|
|
139
|
+
if self._pattern is not None:
|
|
140
|
+
stdout = self._stdout(exe, out)
|
|
141
|
+
match = self._pattern.search(stdout)
|
|
142
|
+
if match is None:
|
|
143
|
+
raise RuntimeError(
|
|
144
|
+
f"command {exe} exited 0 but its stdout does not match result_pattern "
|
|
145
|
+
f"{self._pattern.pattern!r}"
|
|
146
|
+
)
|
|
147
|
+
retval = match.groupdict()
|
|
148
|
+
unset = sorted(name for name, value in retval.items() if value is None)
|
|
149
|
+
if unset:
|
|
150
|
+
raise RuntimeError(
|
|
151
|
+
f"command {exe} exited 0 but result_pattern group(s) {', '.join(unset)} "
|
|
152
|
+
"did not match anything in its stdout"
|
|
153
|
+
)
|
|
154
|
+
elif self._result_format == "json":
|
|
155
|
+
stdout = self._stdout(exe, out)
|
|
156
|
+
lines = [line for line in stdout.splitlines() if line.strip()]
|
|
157
|
+
if not lines:
|
|
158
|
+
raise RuntimeError(
|
|
159
|
+
f"command {exe} exited 0 with no stdout; result_format json needs a JSON "
|
|
160
|
+
"object on the last line"
|
|
161
|
+
)
|
|
162
|
+
try:
|
|
163
|
+
parsed = json.loads(lines[-1])
|
|
164
|
+
except json.JSONDecodeError as exc:
|
|
165
|
+
raise RuntimeError(
|
|
166
|
+
f"command {exe} exited 0 but the last line of its stdout is not JSON: "
|
|
167
|
+
f"{exc.msg} at column {exc.colno}"
|
|
168
|
+
) from None
|
|
169
|
+
if not isinstance(parsed, dict):
|
|
170
|
+
raise RuntimeError(
|
|
171
|
+
f"command {exe} exited 0 but the last line of its stdout is a JSON "
|
|
172
|
+
f"{type(parsed).__name__}, not an object"
|
|
173
|
+
)
|
|
174
|
+
retval = parsed
|
|
175
|
+
return retval
|
|
176
|
+
|
|
177
|
+
def execute(self, payload: dict[str, Any], context: dict[str, Any]) -> dict[str, Any]:
|
|
178
|
+
exe = self._argv[0]
|
|
179
|
+
argv = [exe, *(render_template(arg, payload) for arg in self._argv[1:])]
|
|
180
|
+
with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err:
|
|
181
|
+
try:
|
|
182
|
+
proc = subprocess.run(
|
|
183
|
+
argv,
|
|
184
|
+
stdout=out,
|
|
185
|
+
stderr=err,
|
|
186
|
+
timeout=self._timeout,
|
|
187
|
+
cwd=self._cwd,
|
|
188
|
+
env={**os.environ},
|
|
189
|
+
)
|
|
190
|
+
except subprocess.TimeoutExpired:
|
|
191
|
+
raise RuntimeError(
|
|
192
|
+
f"command {exe} did not finish within {self._timeout}s and was killed"
|
|
193
|
+
) from None
|
|
194
|
+
except OSError as exc:
|
|
195
|
+
raise RuntimeError(f"command {exe} could not be started: {exc}") from exc
|
|
196
|
+
if proc.returncode != 0:
|
|
197
|
+
tail = _stderr_tail(err)
|
|
198
|
+
detail = f"; last stderr lines:\n{tail}" if tail else " with no stderr"
|
|
199
|
+
raise RuntimeError(f"command {exe} failed with exit code {proc.returncode}{detail}")
|
|
200
|
+
retval = self._results(exe, out)
|
|
201
|
+
return retval
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@register_action("command")
|
|
205
|
+
def build(cfg: dict) -> Action:
|
|
206
|
+
argv = cfg.get("argv")
|
|
207
|
+
if not isinstance(argv, list) or not argv:
|
|
208
|
+
raise ValueError(
|
|
209
|
+
"command action needs 'argv', a non-empty list of strings (it is never run "
|
|
210
|
+
"through a shell)"
|
|
211
|
+
)
|
|
212
|
+
if not all(isinstance(arg, str) for arg in argv):
|
|
213
|
+
raise ValueError("command action: every 'argv' element must be a string")
|
|
214
|
+
if "{" in argv[0] or "}" in argv[0]:
|
|
215
|
+
raise ConfigError(
|
|
216
|
+
f"command action: argv[0] {argv[0]!r} contains a template brace; the program "
|
|
217
|
+
"must be a literal path in config and must not come from proposal data"
|
|
218
|
+
)
|
|
219
|
+
cwd = cfg.get("cwd")
|
|
220
|
+
if cwd is not None and not isinstance(cwd, str):
|
|
221
|
+
raise ValueError("command action: 'cwd' must be a string")
|
|
222
|
+
pattern_src = cfg.get("result_pattern")
|
|
223
|
+
result_format = cfg.get("result_format")
|
|
224
|
+
if pattern_src is not None and result_format is not None:
|
|
225
|
+
raise ValueError("command action: set at most one of 'result_pattern' and 'result_format'")
|
|
226
|
+
if result_format is not None and result_format != "json":
|
|
227
|
+
raise ValueError(
|
|
228
|
+
f"command action: result_format {result_format!r} is not supported; use \"json\""
|
|
229
|
+
)
|
|
230
|
+
pattern = None
|
|
231
|
+
if pattern_src is not None:
|
|
232
|
+
try:
|
|
233
|
+
pattern = re.compile(pattern_src)
|
|
234
|
+
except (re.error, TypeError) as exc:
|
|
235
|
+
raise ValueError(f"command action: result_pattern does not compile: {exc}") from exc
|
|
236
|
+
if not pattern.groupindex:
|
|
237
|
+
raise ValueError(
|
|
238
|
+
"command action: result_pattern has no named groups (?P<name>...), so it "
|
|
239
|
+
"would produce no results"
|
|
240
|
+
)
|
|
241
|
+
retval = CommandAction(
|
|
242
|
+
argv=argv,
|
|
243
|
+
cwd=cwd,
|
|
244
|
+
timeout=int(cfg.get("timeout", 120)),
|
|
245
|
+
pattern=pattern,
|
|
246
|
+
result_format=result_format,
|
|
247
|
+
)
|
|
248
|
+
return retval
|