agentjam 0.1.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.
- agentjam/__init__.py +435 -0
- agentjam/_adapters/__init__.py +0 -0
- agentjam/_adapters/responses.py +73 -0
- agentjam/_adapters/shared.py +435 -0
- agentjam/_agent.py +1032 -0
- agentjam/_agent_access.py +360 -0
- agentjam/_agent_delegations.py +376 -0
- agentjam/_agent_executions.py +566 -0
- agentjam/_agent_judging.py +288 -0
- agentjam/_agent_options.py +122 -0
- agentjam/_agent_routing.py +214 -0
- agentjam/_agent_session.py +195 -0
- agentjam/_agent_support.py +415 -0
- agentjam/_artifacts.py +565 -0
- agentjam/_attachments.py +112 -0
- agentjam/_connections.py +1282 -0
- agentjam/_content.py +29 -0
- agentjam/_cron.py +302 -0
- agentjam/_errors.py +238 -0
- agentjam/_events.py +113 -0
- agentjam/_files.py +55 -0
- agentjam/_github.py +742 -0
- agentjam/_idempotency.py +68 -0
- agentjam/_inputs.py +79 -0
- agentjam/_models.py +21 -0
- agentjam/_polling.py +139 -0
- agentjam/_profile.py +244 -0
- agentjam/_protocol.py +480 -0
- agentjam/_providers.py +365 -0
- agentjam/_recaps.py +67 -0
- agentjam/_relay/__init__.py +0 -0
- agentjam/_relay/actions.py +234 -0
- agentjam/_relay/artifacts.py +96 -0
- agentjam/_relay/base.py +387 -0
- agentjam/_relay/commits.py +77 -0
- agentjam/_relay/copies.py +66 -0
- agentjam/_relay/credentials.py +127 -0
- agentjam/_relay/endpoints.py +39 -0
- agentjam/_relay/follow_ups.py +548 -0
- agentjam/_relay/frames.py +443 -0
- agentjam/_relay/guest_store.py +114 -0
- agentjam/_relay/guests.py +55 -0
- agentjam/_relay/inputs.py +119 -0
- agentjam/_relay/lineage.py +294 -0
- agentjam/_relay/memory.py +1045 -0
- agentjam/_relay/payments.py +204 -0
- agentjam/_relay/profiles.py +95 -0
- agentjam/_relay/remote.py +826 -0
- agentjam/_relay/resumptions.py +65 -0
- agentjam/_relay/revisions.py +43 -0
- agentjam/_relay/schedule_follow_ups.py +274 -0
- agentjam/_relay/schedules.py +547 -0
- agentjam/_relay/secrets.py +275 -0
- agentjam/_relay/sessions.py +640 -0
- agentjam/_relay/uploads.py +71 -0
- agentjam/_requests.py +178 -0
- agentjam/_screening.py +368 -0
- agentjam/_secrets.py +153 -0
- agentjam/_sentry.py +233 -0
- agentjam/_signals.py +98 -0
- agentjam/_slack.py +468 -0
- agentjam/_stripe.py +93 -0
- agentjam/_tasks/__init__.py +0 -0
- agentjam/_tasks/access.py +142 -0
- agentjam/_tasks/incoming.py +952 -0
- agentjam/_tasks/outgoing.py +472 -0
- agentjam/_tasks/scheduled.py +206 -0
- agentjam/_tasks/serial.py +40 -0
- agentjam/_tasks/triggers.py +258 -0
- agentjam/_tasks/watches.py +77 -0
- agentjam/_tools.py +1035 -0
- agentjam/_types.py +965 -0
- agentjam/_utils.py +75 -0
- agentjam/_verdicts.py +21 -0
- agentjam/_watermarks.py +59 -0
- agentjam/anthropic.py +442 -0
- agentjam/mpp.py +233 -0
- agentjam/openai.py +141 -0
- agentjam/openai_agents.py +253 -0
- agentjam/py.typed +0 -0
- agentjam/pydantic_ai.py +301 -0
- agentjam/skills.py +69 -0
- agentjam-0.1.0.dist-info/METADATA +210 -0
- agentjam-0.1.0.dist-info/RECORD +86 -0
- agentjam-0.1.0.dist-info/WHEEL +4 -0
- agentjam-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from agentjam._errors import AgentJamError, ConnectionRevokedError, RequestRefusedError
|
|
8
|
+
from agentjam._protocol import ConnectionStoredEnvelope, ProposeEnvelope
|
|
9
|
+
from agentjam._relay.base import SecretGrant
|
|
10
|
+
from agentjam._types import AllowedRequest, ConnectionCard
|
|
11
|
+
from agentjam._utils import create_id, encode_json, resolve_value
|
|
12
|
+
|
|
13
|
+
FORM_PREFIX = "memory://credentials/"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True, kw_only=True)
|
|
17
|
+
class SecretStub:
|
|
18
|
+
"""A stand-in for a provider credential that a `MemoryRelay` holds for every human."""
|
|
19
|
+
|
|
20
|
+
base_url: str | None = None
|
|
21
|
+
"""The base URL relative requests resolve against. Defaults to `https://<provider>.test`."""
|
|
22
|
+
config: Mapping[str, str] | None = None
|
|
23
|
+
"""The non-secret fields `connection_config()` returns. Defaults to the base URL as `baseUrl`."""
|
|
24
|
+
fetch: (
|
|
25
|
+
Callable[[httpx.Request], Awaitable[httpx.Response] | httpx.Response] | None
|
|
26
|
+
) = None
|
|
27
|
+
"""Answers each request a task sends through `connection_fetch()`, in place of the provider. Defaults to an empty 200."""
|
|
28
|
+
pending: bool = False
|
|
29
|
+
"""Makes each human enter the credential first, through `enter_credential()` at the `credential_form` action's link."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class SecretProposal:
|
|
34
|
+
executor: str
|
|
35
|
+
principal: str
|
|
36
|
+
connections: list[ConnectionCard]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class SecretUse:
|
|
41
|
+
"""The human whose credential a grant uses, and the secret connection it's for."""
|
|
42
|
+
|
|
43
|
+
principal: str
|
|
44
|
+
connection: ConnectionCard
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True)
|
|
48
|
+
class OpenForm:
|
|
49
|
+
grant: SecretGrant
|
|
50
|
+
executor: str
|
|
51
|
+
principal: str
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def construct_key(*parts: str) -> str:
|
|
55
|
+
return encode_json(parts)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def construct_hold_key(grant: SecretGrant) -> str:
|
|
59
|
+
return construct_key(grant.requester, grant.id, grant.provider)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def escape_segment(segment: str) -> str:
|
|
63
|
+
return "[^/]*".join(re.escape(part) for part in segment.split("*"))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def is_allowed_path(allowed: str, path: str) -> bool:
|
|
67
|
+
is_open = allowed.endswith("/**")
|
|
68
|
+
fixed = allowed.removesuffix("/**") if is_open else allowed
|
|
69
|
+
segments = "/".join(escape_segment(segment) for segment in fixed.split("/"))
|
|
70
|
+
|
|
71
|
+
return re.fullmatch(f"{segments}{'(/.*)?' if is_open else ''}", path) is not None
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def is_allowed_request(allow: list[AllowedRequest], method: str, path: str) -> bool:
|
|
75
|
+
return any(
|
|
76
|
+
allowed.method == method and is_allowed_path(allowed.path, path)
|
|
77
|
+
for allowed in allow
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def parse_url(target: str, base: httpx.URL | None = None) -> httpx.URL | None:
|
|
82
|
+
try:
|
|
83
|
+
url = base.join(target) if base else httpx.URL(target)
|
|
84
|
+
except httpx.InvalidURL:
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
return None if url.is_relative_url else url
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def is_same_origin(first: httpx.URL, second: httpx.URL) -> bool:
|
|
91
|
+
return (first.scheme, first.host, first.port) == (
|
|
92
|
+
second.scheme,
|
|
93
|
+
second.host,
|
|
94
|
+
second.port,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def resolve_target(
|
|
99
|
+
provider: str, base: httpx.URL, target: str
|
|
100
|
+
) -> tuple[httpx.URL, str]:
|
|
101
|
+
"""Resolves a path under `base`, or a full URL under it, as the platform's proxy does."""
|
|
102
|
+
base_path = base.path.removesuffix("/")
|
|
103
|
+
is_path = target.startswith("/") and not target.startswith("//")
|
|
104
|
+
url = parse_url(f"{base_path}{target}", base) if is_path else parse_url(target)
|
|
105
|
+
is_under = url is not None and (
|
|
106
|
+
url.path == base_path or url.path.startswith(f"{base_path}/")
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
if url is None or not is_same_origin(url, base) or not is_under:
|
|
110
|
+
raise RequestRefusedError(
|
|
111
|
+
provider,
|
|
112
|
+
"invalid_target",
|
|
113
|
+
f"Send {provider} requests to a path under {base}",
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
return url, url.path[len(base_path) :] or "/"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class SecretLedger:
|
|
120
|
+
"""Holds the stubbed credentials of the secret connections each proposal routed through a `MemoryRelay` declares."""
|
|
121
|
+
|
|
122
|
+
def __init__(self, stubs: Mapping[str, SecretStub]) -> None:
|
|
123
|
+
self._stubs = stubs
|
|
124
|
+
self._proposals: dict[str, SecretProposal] = {}
|
|
125
|
+
self._held: set[str] = set()
|
|
126
|
+
self._entered: set[str] = set()
|
|
127
|
+
self._forms: dict[str, OpenForm] = {}
|
|
128
|
+
|
|
129
|
+
def propose(
|
|
130
|
+
self, proposal: ProposeEnvelope, declared: list[ConnectionCard] | None
|
|
131
|
+
) -> None:
|
|
132
|
+
connections = [
|
|
133
|
+
connection for connection in declared or [] if connection.kind == "secret"
|
|
134
|
+
]
|
|
135
|
+
if not connections:
|
|
136
|
+
return
|
|
137
|
+
|
|
138
|
+
self._proposals[construct_key(proposal.sender, proposal.id)] = SecretProposal(
|
|
139
|
+
proposal.to,
|
|
140
|
+
proposal.principal if proposal.principal is not None else proposal.sender,
|
|
141
|
+
connections,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
def authorize(self, executor: str, grant: SecretGrant) -> str | None:
|
|
145
|
+
"""Holds the stub for `grant`, or opens a form when the human has yet to enter it, returning its link."""
|
|
146
|
+
principal = self._find_use(executor, grant).principal
|
|
147
|
+
stub = self._find_stub(grant.provider)
|
|
148
|
+
if (
|
|
149
|
+
stub.pending
|
|
150
|
+
and construct_key(principal, grant.provider) not in self._entered
|
|
151
|
+
):
|
|
152
|
+
url = f"{FORM_PREFIX}{create_id()}"
|
|
153
|
+
self._forms[url] = OpenForm(grant, executor, principal)
|
|
154
|
+
|
|
155
|
+
return url
|
|
156
|
+
|
|
157
|
+
self._held.add(construct_hold_key(grant))
|
|
158
|
+
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
def enter(self, url: str) -> ConnectionStoredEnvelope:
|
|
162
|
+
"""Enters the credential at a form's link, and returns the notice telling its executor it's stored."""
|
|
163
|
+
form = self._forms.pop(url, None)
|
|
164
|
+
if form is None:
|
|
165
|
+
raise AgentJamError(f"No credential form is open at {url}")
|
|
166
|
+
|
|
167
|
+
self._entered.add(construct_key(form.principal, form.grant.provider))
|
|
168
|
+
self._held.add(construct_hold_key(form.grant))
|
|
169
|
+
|
|
170
|
+
return ConnectionStoredEnvelope(
|
|
171
|
+
id=create_id(),
|
|
172
|
+
sender=form.grant.requester,
|
|
173
|
+
to=form.executor,
|
|
174
|
+
proposal_id=form.grant.id,
|
|
175
|
+
principal=form.principal,
|
|
176
|
+
provider=form.grant.provider,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
def release(self, executor: str, grant: SecretGrant) -> None:
|
|
180
|
+
proposal = self._proposals.get(construct_key(grant.requester, grant.id))
|
|
181
|
+
if proposal is None or proposal.executor != executor:
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
self._held.discard(construct_hold_key(grant))
|
|
185
|
+
|
|
186
|
+
def config(self, executor: str, grant: SecretGrant) -> dict[str, str]:
|
|
187
|
+
self._find_held(executor, grant)
|
|
188
|
+
stub = self._find_stub(grant.provider)
|
|
189
|
+
if stub.config is not None:
|
|
190
|
+
return dict(stub.config)
|
|
191
|
+
|
|
192
|
+
return {"baseUrl": str(self._find_base(grant.provider, stub))}
|
|
193
|
+
|
|
194
|
+
async def fetch(
|
|
195
|
+
self, executor: str, grant: SecretGrant, request: httpx.Request
|
|
196
|
+
) -> httpx.Response:
|
|
197
|
+
connection = self._find_held(executor, grant)
|
|
198
|
+
stub = self._find_stub(grant.provider)
|
|
199
|
+
base = self._find_base(grant.provider, stub)
|
|
200
|
+
url, path = resolve_target(grant.provider, base, str(request.url))
|
|
201
|
+
content = await request.aread()
|
|
202
|
+
resolved = httpx.Request(
|
|
203
|
+
request.method, url, headers=request.headers, content=content
|
|
204
|
+
)
|
|
205
|
+
method = resolved.method.upper()
|
|
206
|
+
|
|
207
|
+
if connection.allow is not None and not is_allowed_request(
|
|
208
|
+
connection.allow, method, path
|
|
209
|
+
):
|
|
210
|
+
raise RequestRefusedError(
|
|
211
|
+
grant.provider,
|
|
212
|
+
"unlisted_request",
|
|
213
|
+
f"The skill's {grant.provider} connection doesn't allow {method} {path}",
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
if stub.fetch is None:
|
|
217
|
+
return httpx.Response(200, request=resolved)
|
|
218
|
+
|
|
219
|
+
return await resolve_value(stub.fetch(resolved))
|
|
220
|
+
|
|
221
|
+
def end(self, requester: str, id: str) -> None:
|
|
222
|
+
"""Forgets the ended proposal `id` of `requester`, which no longer holds its credentials."""
|
|
223
|
+
proposal = self._proposals.pop(construct_key(requester, id), None)
|
|
224
|
+
if proposal is None:
|
|
225
|
+
return
|
|
226
|
+
|
|
227
|
+
for connection in proposal.connections:
|
|
228
|
+
self._held.discard(construct_key(requester, id, connection.provider))
|
|
229
|
+
|
|
230
|
+
def _find_use(self, executor: str, grant: SecretGrant) -> SecretUse:
|
|
231
|
+
proposal = self._proposals.get(construct_key(grant.requester, grant.id))
|
|
232
|
+
if proposal is None or proposal.executor != executor:
|
|
233
|
+
raise AgentJamError(
|
|
234
|
+
f"{executor} executes no open task or schedule {grant.id} for {grant.requester}"
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
connection = next(
|
|
238
|
+
(
|
|
239
|
+
declared
|
|
240
|
+
for declared in proposal.connections
|
|
241
|
+
if declared.provider == grant.provider
|
|
242
|
+
),
|
|
243
|
+
None,
|
|
244
|
+
)
|
|
245
|
+
if connection is None:
|
|
246
|
+
raise AgentJamError(
|
|
247
|
+
f'The skill declares no secret connection to "{grant.provider}"'
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
return SecretUse(proposal.principal, connection)
|
|
251
|
+
|
|
252
|
+
def _find_held(self, executor: str, grant: SecretGrant) -> ConnectionCard:
|
|
253
|
+
use = self._find_use(executor, grant)
|
|
254
|
+
if construct_hold_key(grant) not in self._held:
|
|
255
|
+
raise ConnectionRevokedError(
|
|
256
|
+
grant.provider, "The human's credential was deleted or expired"
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
return use.connection
|
|
260
|
+
|
|
261
|
+
def _find_stub(self, provider: str) -> SecretStub:
|
|
262
|
+
stub = self._stubs.get(provider)
|
|
263
|
+
if stub is None:
|
|
264
|
+
raise AgentJamError(
|
|
265
|
+
f"This MemoryRelay holds no {provider} credential, so pass a stub for it in secrets"
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
return stub
|
|
269
|
+
|
|
270
|
+
def _find_base(self, provider: str, stub: SecretStub) -> httpx.URL:
|
|
271
|
+
base = httpx.URL(
|
|
272
|
+
f"https://{provider}.test" if stub.base_url is None else stub.base_url
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
return base.copy_with(path=base.path)
|