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.
Files changed (86) hide show
  1. agentjam/__init__.py +435 -0
  2. agentjam/_adapters/__init__.py +0 -0
  3. agentjam/_adapters/responses.py +73 -0
  4. agentjam/_adapters/shared.py +435 -0
  5. agentjam/_agent.py +1032 -0
  6. agentjam/_agent_access.py +360 -0
  7. agentjam/_agent_delegations.py +376 -0
  8. agentjam/_agent_executions.py +566 -0
  9. agentjam/_agent_judging.py +288 -0
  10. agentjam/_agent_options.py +122 -0
  11. agentjam/_agent_routing.py +214 -0
  12. agentjam/_agent_session.py +195 -0
  13. agentjam/_agent_support.py +415 -0
  14. agentjam/_artifacts.py +565 -0
  15. agentjam/_attachments.py +112 -0
  16. agentjam/_connections.py +1282 -0
  17. agentjam/_content.py +29 -0
  18. agentjam/_cron.py +302 -0
  19. agentjam/_errors.py +238 -0
  20. agentjam/_events.py +113 -0
  21. agentjam/_files.py +55 -0
  22. agentjam/_github.py +742 -0
  23. agentjam/_idempotency.py +68 -0
  24. agentjam/_inputs.py +79 -0
  25. agentjam/_models.py +21 -0
  26. agentjam/_polling.py +139 -0
  27. agentjam/_profile.py +244 -0
  28. agentjam/_protocol.py +480 -0
  29. agentjam/_providers.py +365 -0
  30. agentjam/_recaps.py +67 -0
  31. agentjam/_relay/__init__.py +0 -0
  32. agentjam/_relay/actions.py +234 -0
  33. agentjam/_relay/artifacts.py +96 -0
  34. agentjam/_relay/base.py +387 -0
  35. agentjam/_relay/commits.py +77 -0
  36. agentjam/_relay/copies.py +66 -0
  37. agentjam/_relay/credentials.py +127 -0
  38. agentjam/_relay/endpoints.py +39 -0
  39. agentjam/_relay/follow_ups.py +548 -0
  40. agentjam/_relay/frames.py +443 -0
  41. agentjam/_relay/guest_store.py +114 -0
  42. agentjam/_relay/guests.py +55 -0
  43. agentjam/_relay/inputs.py +119 -0
  44. agentjam/_relay/lineage.py +294 -0
  45. agentjam/_relay/memory.py +1045 -0
  46. agentjam/_relay/payments.py +204 -0
  47. agentjam/_relay/profiles.py +95 -0
  48. agentjam/_relay/remote.py +826 -0
  49. agentjam/_relay/resumptions.py +65 -0
  50. agentjam/_relay/revisions.py +43 -0
  51. agentjam/_relay/schedule_follow_ups.py +274 -0
  52. agentjam/_relay/schedules.py +547 -0
  53. agentjam/_relay/secrets.py +275 -0
  54. agentjam/_relay/sessions.py +640 -0
  55. agentjam/_relay/uploads.py +71 -0
  56. agentjam/_requests.py +178 -0
  57. agentjam/_screening.py +368 -0
  58. agentjam/_secrets.py +153 -0
  59. agentjam/_sentry.py +233 -0
  60. agentjam/_signals.py +98 -0
  61. agentjam/_slack.py +468 -0
  62. agentjam/_stripe.py +93 -0
  63. agentjam/_tasks/__init__.py +0 -0
  64. agentjam/_tasks/access.py +142 -0
  65. agentjam/_tasks/incoming.py +952 -0
  66. agentjam/_tasks/outgoing.py +472 -0
  67. agentjam/_tasks/scheduled.py +206 -0
  68. agentjam/_tasks/serial.py +40 -0
  69. agentjam/_tasks/triggers.py +258 -0
  70. agentjam/_tasks/watches.py +77 -0
  71. agentjam/_tools.py +1035 -0
  72. agentjam/_types.py +965 -0
  73. agentjam/_utils.py +75 -0
  74. agentjam/_verdicts.py +21 -0
  75. agentjam/_watermarks.py +59 -0
  76. agentjam/anthropic.py +442 -0
  77. agentjam/mpp.py +233 -0
  78. agentjam/openai.py +141 -0
  79. agentjam/openai_agents.py +253 -0
  80. agentjam/py.typed +0 -0
  81. agentjam/pydantic_ai.py +301 -0
  82. agentjam/skills.py +69 -0
  83. agentjam-0.1.0.dist-info/METADATA +210 -0
  84. agentjam-0.1.0.dist-info/RECORD +86 -0
  85. agentjam-0.1.0.dist-info/WHEEL +4 -0
  86. agentjam-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,127 @@
1
+ from dataclasses import dataclass
2
+ from typing import Any, Literal
3
+
4
+ import httpx
5
+
6
+ from agentjam._errors import AgentJamError, ConnectionRevokedError, RequestRefusedError
7
+ from agentjam._models import WireModel
8
+ from agentjam._relay.base import SecretGrant
9
+ from agentjam._relay.endpoints import AGENT_NAME_HEADER, encode_component
10
+ from agentjam._relay.profiles import get_message, parse_body
11
+ from agentjam._utils import encode_json
12
+
13
+ REFUSAL_HEADER = "x-agentjam-refusal"
14
+ """The header the proxy names its reason in when it refuses to send a request."""
15
+
16
+ LOST_REASONS = frozenset({"missing_credential", "grant_closed"})
17
+
18
+ LOST_CREDENTIAL = "The human's credential was deleted or expired"
19
+
20
+ PROXY_PATH = "/fetch"
21
+
22
+
23
+ class GrantOutcome(WireModel):
24
+ status: Literal["ready", "required"]
25
+ url: str | None = None
26
+
27
+
28
+ def construct_refusal_error(
29
+ provider: str, reason: str, response: httpx.Response
30
+ ) -> AgentJamError:
31
+ error = parse_body(response).get("error")
32
+ message = error.get("message") if isinstance(error, dict) else None
33
+ if not isinstance(message, str):
34
+ message = f"The credential proxy refused the request ({reason})"
35
+
36
+ if reason in LOST_REASONS:
37
+ return ConnectionRevokedError(provider, message)
38
+
39
+ return RequestRefusedError(provider, reason, message)
40
+
41
+
42
+ @dataclass(frozen=True, kw_only=True)
43
+ class RemoteCredentials:
44
+ """Reaches the credentials the AgentJam platform holds for secret connections, through its API and credential proxy."""
45
+
46
+ api_url: str
47
+ proxy_url: str
48
+ api_key: str
49
+ name: str
50
+
51
+ async def authorize(self, grant: SecretGrant) -> str | None:
52
+ outcome = GrantOutcome.model_validate(
53
+ await self._post("credentials/grants", grant)
54
+ )
55
+
56
+ return outcome.url if outcome.status == "required" else None
57
+
58
+ async def release(self, grant: SecretGrant) -> None:
59
+ await self._post("credentials/grants/release", grant)
60
+
61
+ async def config(self, grant: SecretGrant) -> dict[str, str]:
62
+ config: dict[str, str] | None = await self._post(
63
+ "credentials/grants/config", grant
64
+ )
65
+ if config is None:
66
+ raise ConnectionRevokedError(grant.provider, LOST_CREDENTIAL)
67
+
68
+ return config
69
+
70
+ async def fetch(self, grant: SecretGrant, request: httpx.Request) -> httpx.Response:
71
+ headers = httpx.Headers(request.headers)
72
+ headers.pop("host", None)
73
+ headers.update(self._compute_proxy_headers(grant, str(request.url)))
74
+ proxied = httpx.Request(
75
+ request.method,
76
+ f"{self.proxy_url}{PROXY_PATH}",
77
+ headers=headers,
78
+ stream=request.stream,
79
+ )
80
+
81
+ async with httpx.AsyncClient() as client:
82
+ response = await client.send(proxied, follow_redirects=False)
83
+
84
+ reason = response.headers.get(REFUSAL_HEADER)
85
+ if reason is None:
86
+ return response
87
+
88
+ raise construct_refusal_error(grant.provider, reason, response)
89
+
90
+ def _compute_authentication_headers(self) -> dict[str, str]:
91
+ return {
92
+ "authorization": f"Bearer {self.api_key}",
93
+ AGENT_NAME_HEADER: encode_component(self.name),
94
+ }
95
+
96
+ def _compute_proxy_headers(self, grant: SecretGrant, target: str) -> dict[str, str]:
97
+ return {
98
+ **self._compute_authentication_headers(),
99
+ "x-agentjam-requester": grant.requester,
100
+ "x-agentjam-holder": grant.id,
101
+ "x-agentjam-provider": grant.provider,
102
+ "x-agentjam-target": target,
103
+ }
104
+
105
+ async def _post(self, path: str, grant: SecretGrant) -> Any:
106
+ body = {
107
+ "requester": grant.requester,
108
+ "id": grant.id,
109
+ "provider": grant.provider,
110
+ }
111
+
112
+ async with httpx.AsyncClient() as client:
113
+ response = await client.post(
114
+ f"{self.api_url}/{path}",
115
+ headers={
116
+ **self._compute_authentication_headers(),
117
+ "content-type": "application/json",
118
+ },
119
+ content=encode_json(body),
120
+ )
121
+
122
+ if not response.is_success:
123
+ raise AgentJamError(
124
+ f"Couldn't reach the {grant.provider} credential: {get_message(response)}"
125
+ )
126
+
127
+ return response.json()["data"]
@@ -0,0 +1,39 @@
1
+ import os
2
+ from collections.abc import Mapping
3
+ from dataclasses import dataclass
4
+ from urllib.parse import quote
5
+
6
+ AGENT_NAME_HEADER = "x-agentjam-agent"
7
+ """The header naming the agent a request acts as, which an account's API key needs to pick one of its agents."""
8
+
9
+
10
+ @dataclass(frozen=True, kw_only=True)
11
+ class Endpoints:
12
+ """Where an `AgentJamRelay` reaches the network."""
13
+
14
+ relay_url: str
15
+ """The transport's WebSocket URL, which agents connect to."""
16
+ api_url: str
17
+ """The API gateway's HTTP URL."""
18
+ proxy_url: str
19
+ """The credential proxy's HTTP URL, which sends the requests of secret connections with their credentials."""
20
+
21
+
22
+ HOSTED_ENDPOINTS = Endpoints(
23
+ relay_url="wss://relay.agentjam.com",
24
+ api_url="https://api.agentjam.com",
25
+ proxy_url="https://proxy.agentjam.com",
26
+ )
27
+
28
+
29
+ def resolve_endpoints(environment: Mapping[str, str] = os.environ) -> Endpoints:
30
+ return Endpoints(
31
+ relay_url=environment.get("AGENTJAM_RELAY_URL") or HOSTED_ENDPOINTS.relay_url,
32
+ api_url=environment.get("AGENTJAM_API_URL") or HOSTED_ENDPOINTS.api_url,
33
+ proxy_url=environment.get("AGENTJAM_PROXY_URL") or HOSTED_ENDPOINTS.proxy_url,
34
+ )
35
+
36
+
37
+ def encode_component(value: str) -> str:
38
+ """Percent-encodes `value` the way JS `encodeURIComponent` does."""
39
+ return quote(value, safe="!*'()")