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,65 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from agentjam._protocol import DelegatedTask, ProposeEnvelope
|
|
4
|
+
from agentjam._relay.sessions import request_key
|
|
5
|
+
from agentjam._types import SkillCard
|
|
6
|
+
|
|
7
|
+
MAX_RESUMPTIONS = 3
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Invariant: a task is kept only while it's open, and resumptions counts the times it moved to another session.
|
|
11
|
+
@dataclass
|
|
12
|
+
class ResumableTask:
|
|
13
|
+
proposal: ProposeEnvelope
|
|
14
|
+
accepted: bool = False
|
|
15
|
+
resumptions: int = 0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ResumptionLedger:
|
|
19
|
+
"""Tracks the open tasks routed through a `MemoryRelay` that another session of their executor can resume."""
|
|
20
|
+
|
|
21
|
+
def __init__(self) -> None:
|
|
22
|
+
self._tasks: dict[str, ResumableTask] = {}
|
|
23
|
+
|
|
24
|
+
def propose(self, proposal: ProposeEnvelope, skill: SkillCard | None) -> None:
|
|
25
|
+
"""Keeps a one-off task of a resumable skill, leaving out schedules and preparations."""
|
|
26
|
+
is_scheduled = proposal.cron is not None or proposal.trigger is not None
|
|
27
|
+
if not skill or not skill.resumable or is_scheduled or proposal.prepare:
|
|
28
|
+
return
|
|
29
|
+
|
|
30
|
+
self._tasks[request_key(proposal.sender, proposal.id)] = ResumableTask(proposal)
|
|
31
|
+
|
|
32
|
+
def accept(self, requester: str, id: str) -> None:
|
|
33
|
+
"""Marks a task its executor started working on, once it accepted it and its requester's human took every action."""
|
|
34
|
+
if task := self._tasks.get(request_key(requester, id)):
|
|
35
|
+
task.accepted = True
|
|
36
|
+
|
|
37
|
+
def is_resumable(self, requester: str, id: str) -> bool:
|
|
38
|
+
task = self._tasks.get(request_key(requester, id))
|
|
39
|
+
|
|
40
|
+
return task is not None and task.accepted and task.resumptions < MAX_RESUMPTIONS
|
|
41
|
+
|
|
42
|
+
def resume(
|
|
43
|
+
self, requester: str, id: str, children: list[DelegatedTask]
|
|
44
|
+
) -> ProposeEnvelope | None:
|
|
45
|
+
"""Counts another resumption of a task, and constructs the proposal that redelivers it to the session taking it over.
|
|
46
|
+
|
|
47
|
+
The session taking it over follows the open tasks it delegated, `children`, from now on. Returns None for
|
|
48
|
+
a task that isn't resumable.
|
|
49
|
+
"""
|
|
50
|
+
task = self._tasks.get(request_key(requester, id))
|
|
51
|
+
if task is None:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
task.resumptions += 1
|
|
55
|
+
|
|
56
|
+
return task.proposal.model_copy(
|
|
57
|
+
update={
|
|
58
|
+
"resumed": True,
|
|
59
|
+
"attempt": task.resumptions + 1,
|
|
60
|
+
"children": children or None,
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def end(self, requester: str, id: str) -> None:
|
|
65
|
+
self._tasks.pop(request_key(requester, id), None)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from agentjam._protocol import ArtifactEnvelope, DeliveredEnvelope
|
|
4
|
+
from agentjam._relay.sessions import request_key
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class TaskRevisions:
|
|
9
|
+
counts: dict[str, int] = field(default_factory=dict)
|
|
10
|
+
"""How many deliveries each key has started."""
|
|
11
|
+
revisions: dict[str, int] = field(default_factory=dict)
|
|
12
|
+
"""The revision of each keyed artifact that has started."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RevisionLedger:
|
|
16
|
+
"""Numbers the revisions of keyed artifacts routed through a `MemoryRelay`.
|
|
17
|
+
|
|
18
|
+
Revisions count per key within each task in the order their first chunks arrive, and each chunk is stamped
|
|
19
|
+
with its artifact's revision.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self) -> None:
|
|
23
|
+
self._tasks: dict[str, TaskRevisions] = {}
|
|
24
|
+
|
|
25
|
+
def revise(self, envelope: DeliveredEnvelope) -> DeliveredEnvelope:
|
|
26
|
+
if not isinstance(envelope, ArtifactEnvelope) or envelope.key is None:
|
|
27
|
+
return envelope
|
|
28
|
+
|
|
29
|
+
task = self._find_task(envelope.to, envelope.task_id)
|
|
30
|
+
revision = task.revisions.get(envelope.artifact_id)
|
|
31
|
+
if revision is None:
|
|
32
|
+
revision = task.counts.get(envelope.key, 0) + 1
|
|
33
|
+
task.counts[envelope.key] = revision
|
|
34
|
+
task.revisions[envelope.artifact_id] = revision
|
|
35
|
+
|
|
36
|
+
return envelope.model_copy(update={"revision": revision})
|
|
37
|
+
|
|
38
|
+
def end(self, requester: str, id: str) -> None:
|
|
39
|
+
"""Forgets a task's revisions, so a restarted attempt numbers its own from 1."""
|
|
40
|
+
self._tasks.pop(request_key(requester, id), None)
|
|
41
|
+
|
|
42
|
+
def _find_task(self, requester: str, id: str) -> TaskRevisions:
|
|
43
|
+
return self._tasks.setdefault(request_key(requester, id), TaskRevisions())
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
from collections.abc import Awaitable, Callable
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Literal, TypeAlias
|
|
4
|
+
|
|
5
|
+
from agentjam._errors import ADDRESS_MISTAKE, AgentJamError
|
|
6
|
+
from agentjam._protocol import FollowUpEnvelope
|
|
7
|
+
from agentjam._relay.artifacts import FinishedArtifact
|
|
8
|
+
from agentjam._types import AgentCard, JsonObject, ScheduleFollowUp, SkillCard
|
|
9
|
+
from agentjam._utils import encode_json
|
|
10
|
+
|
|
11
|
+
FOLLOW_UP_SUFFIX = ":then"
|
|
12
|
+
|
|
13
|
+
MISSING = object()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True, kw_only=True)
|
|
17
|
+
class FollowUpCheck:
|
|
18
|
+
"""What a `MemoryRelay`'s `judge_follow_up` reads to decide whether a completed run meets its follow-up's `condition`."""
|
|
19
|
+
|
|
20
|
+
condition: str
|
|
21
|
+
"""The follow-up's yes or no question about the run's result."""
|
|
22
|
+
schedule_id: str
|
|
23
|
+
run_id: str
|
|
24
|
+
text: str
|
|
25
|
+
"""The run's text artifacts, joined by blank lines."""
|
|
26
|
+
files: list[str]
|
|
27
|
+
"""The names of the run's file artifacts."""
|
|
28
|
+
output: JsonObject | None = None
|
|
29
|
+
"""The run's structured output, set when its skill has an output schema."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
FollowUpJudge: TypeAlias = Callable[[FollowUpCheck], Awaitable[bool] | bool]
|
|
33
|
+
"""Answers whether a completed run meets its follow-up's `condition`, as the AgentJam network's model does."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, kw_only=True)
|
|
37
|
+
class FollowedRun:
|
|
38
|
+
"""A completed run of a schedule with `then`, with what the relay needs to follow it up."""
|
|
39
|
+
|
|
40
|
+
schedule_id: str
|
|
41
|
+
requester: str
|
|
42
|
+
executor: str
|
|
43
|
+
run_id: str
|
|
44
|
+
text: str
|
|
45
|
+
then: ScheduleFollowUp
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class MatchMismatch:
|
|
50
|
+
kind: Literal["unknown", "unmatchable"]
|
|
51
|
+
field: str
|
|
52
|
+
pattern: object = None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def refuse_judgment(check: FollowUpCheck) -> bool:
|
|
56
|
+
"""Refuses every condition, so a test passes `judge_follow_up` before it follows up on one."""
|
|
57
|
+
raise AgentJamError(
|
|
58
|
+
f'This MemoryRelay can\'t check the condition "{check.condition}", so pass judge_follow_up to MemoryRelay()'
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def is_json_equal(first: object, second: object) -> bool:
|
|
63
|
+
"""Compares JSON values strictly like Node's `isDeepStrictEqual`, so booleans never equal numbers."""
|
|
64
|
+
if isinstance(first, bool) or isinstance(second, bool):
|
|
65
|
+
return type(first) is type(second) and first == second
|
|
66
|
+
|
|
67
|
+
if isinstance(first, dict) and isinstance(second, dict):
|
|
68
|
+
return first.keys() == second.keys() and all(
|
|
69
|
+
is_json_equal(value, second[key]) for key, value in first.items()
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
if isinstance(first, list) and isinstance(second, list):
|
|
73
|
+
return len(first) == len(second) and all(map(is_json_equal, first, second))
|
|
74
|
+
|
|
75
|
+
if isinstance(first, dict | list) or isinstance(second, dict | list):
|
|
76
|
+
return False
|
|
77
|
+
|
|
78
|
+
return first == second
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def list_elements(pattern: object) -> list[object]:
|
|
82
|
+
return pattern if isinstance(pattern, list) else [pattern]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def find_json_type(value: object) -> str:
|
|
86
|
+
match value:
|
|
87
|
+
case None:
|
|
88
|
+
return "null"
|
|
89
|
+
|
|
90
|
+
case bool():
|
|
91
|
+
return "boolean"
|
|
92
|
+
|
|
93
|
+
case int():
|
|
94
|
+
return "integer"
|
|
95
|
+
|
|
96
|
+
case float():
|
|
97
|
+
return "integer" if value.is_integer() else "number"
|
|
98
|
+
|
|
99
|
+
case str():
|
|
100
|
+
return "string"
|
|
101
|
+
|
|
102
|
+
case list():
|
|
103
|
+
return "array"
|
|
104
|
+
|
|
105
|
+
case _:
|
|
106
|
+
return "object"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def list_schema_types(schema: dict[str, object]) -> list[object] | None:
|
|
110
|
+
types = schema.get("type")
|
|
111
|
+
if isinstance(types, str):
|
|
112
|
+
return [types]
|
|
113
|
+
|
|
114
|
+
return types if isinstance(types, list) else None
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def is_type_allowed(types: list[object] | None, type: str) -> bool:
|
|
118
|
+
return types is None or type in types or (type == "integer" and "number" in types)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def is_matching(value: object, pattern: object) -> bool:
|
|
122
|
+
"""Checks that `value` holds `pattern`, as the AgentJam relay does.
|
|
123
|
+
|
|
124
|
+
An object holds every field of the pattern's, an array holds each element of an array pattern or the pattern
|
|
125
|
+
itself, and anything else equals it exactly.
|
|
126
|
+
"""
|
|
127
|
+
if isinstance(value, list):
|
|
128
|
+
return all(
|
|
129
|
+
any(is_matching(element, expected) for element in value)
|
|
130
|
+
for expected in list_elements(pattern)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if not isinstance(pattern, dict):
|
|
134
|
+
return is_json_equal(value, pattern)
|
|
135
|
+
|
|
136
|
+
return isinstance(value, dict) and all(
|
|
137
|
+
is_matching(value.get(field, MISSING), expected)
|
|
138
|
+
for field, expected in pattern.items()
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def find_field_mismatch(
|
|
143
|
+
pattern: dict[str, object], schema: dict[str, object], parent: str | None = None
|
|
144
|
+
) -> MatchMismatch | None:
|
|
145
|
+
properties = schema.get("properties")
|
|
146
|
+
if not isinstance(properties, dict):
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
for key, expected in pattern.items():
|
|
150
|
+
field = key if parent is None else f"{parent}.{key}"
|
|
151
|
+
if key not in properties:
|
|
152
|
+
return MatchMismatch("unknown", field)
|
|
153
|
+
|
|
154
|
+
if mismatch := find_mismatch(expected, properties[key], field):
|
|
155
|
+
return mismatch
|
|
156
|
+
|
|
157
|
+
return None
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def find_own_mismatch(
|
|
161
|
+
pattern: object, schema: dict[str, object], field: str
|
|
162
|
+
) -> MatchMismatch | None:
|
|
163
|
+
unmatchable = MatchMismatch("unmatchable", field, pattern)
|
|
164
|
+
if not is_type_allowed(list_schema_types(schema), find_json_type(pattern)):
|
|
165
|
+
return unmatchable
|
|
166
|
+
|
|
167
|
+
if isinstance(pattern, dict):
|
|
168
|
+
return find_field_mismatch(pattern, schema, field)
|
|
169
|
+
|
|
170
|
+
options = schema.get("enum")
|
|
171
|
+
if isinstance(options, list) and not any(
|
|
172
|
+
is_json_equal(option, pattern) for option in options
|
|
173
|
+
):
|
|
174
|
+
return unmatchable
|
|
175
|
+
|
|
176
|
+
if "const" in schema and not is_json_equal(schema["const"], pattern):
|
|
177
|
+
return unmatchable
|
|
178
|
+
|
|
179
|
+
return None
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def find_mismatch(pattern: object, schema: object, field: str) -> MatchMismatch | None:
|
|
183
|
+
"""Finds the first field of a match that a value fitting `schema` could never hold. A schema without a `type` accepts every pattern."""
|
|
184
|
+
if not isinstance(schema, dict):
|
|
185
|
+
return None
|
|
186
|
+
|
|
187
|
+
if "array" not in (list_schema_types(schema) or []):
|
|
188
|
+
return find_own_mismatch(pattern, schema, field)
|
|
189
|
+
|
|
190
|
+
if not isinstance(pattern, list) and not find_own_mismatch(pattern, schema, field):
|
|
191
|
+
return None
|
|
192
|
+
|
|
193
|
+
mismatches = (
|
|
194
|
+
find_mismatch(element, schema.get("items"), field)
|
|
195
|
+
for element in list_elements(pattern)
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
return next(filter(None, mismatches), None)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def follow_up_problem(
|
|
202
|
+
then: ScheduleFollowUp, requester: str, skill: SkillCard | None
|
|
203
|
+
) -> str | None:
|
|
204
|
+
"""Describes why a schedule's follow-up can't be admitted, the way the AgentJam relay does."""
|
|
205
|
+
if then.to == requester:
|
|
206
|
+
return "A schedule follows up with another agent, not its own requester"
|
|
207
|
+
|
|
208
|
+
if then.when.match is None:
|
|
209
|
+
return None
|
|
210
|
+
|
|
211
|
+
if skill is None or skill.output_schema is None:
|
|
212
|
+
return "Only a schedule whose skill has an output schema can match its runs' output, so describe a condition instead"
|
|
213
|
+
|
|
214
|
+
mismatch = find_field_mismatch(then.when.match, skill.output_schema)
|
|
215
|
+
if mismatch is None:
|
|
216
|
+
return None
|
|
217
|
+
|
|
218
|
+
if mismatch.kind == "unknown":
|
|
219
|
+
return f'The "{skill.name}" skill\'s output has no "{mismatch.field}" field to match'
|
|
220
|
+
|
|
221
|
+
return f'The "{skill.name}" skill\'s output field "{mismatch.field}" never matches {encode_json(mismatch.pattern)}'
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def find_target_skill(then: ScheduleFollowUp, card: AgentCard | None) -> SkillCard:
|
|
225
|
+
"""Finds the skill a follow-up calls for, raising the AgentJam relay's errors when its agent or skill is missing."""
|
|
226
|
+
if card is None:
|
|
227
|
+
raise AgentJamError(
|
|
228
|
+
f'No agent named "{then.to}" is on the network. {ADDRESS_MISTAKE}'
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
offered = next((skill for skill in card.skills if skill.name == then.skill), None)
|
|
232
|
+
if offered is None:
|
|
233
|
+
raise AgentJamError(f'"{then.to}" has no "{then.skill}" skill')
|
|
234
|
+
|
|
235
|
+
return offered
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def construct_check(
|
|
239
|
+
run: FollowedRun,
|
|
240
|
+
condition: str,
|
|
241
|
+
artifacts: list[FinishedArtifact],
|
|
242
|
+
output: JsonObject | None,
|
|
243
|
+
) -> FollowUpCheck:
|
|
244
|
+
return FollowUpCheck(
|
|
245
|
+
condition=condition,
|
|
246
|
+
schedule_id=run.schedule_id,
|
|
247
|
+
run_id=run.run_id,
|
|
248
|
+
text="\n\n".join(
|
|
249
|
+
artifact.text for artifact in artifacts if artifact.text is not None
|
|
250
|
+
),
|
|
251
|
+
files=[
|
|
252
|
+
artifact.attachment.attachment_id
|
|
253
|
+
if artifact.attachment.name is None
|
|
254
|
+
else artifact.attachment.name
|
|
255
|
+
for artifact in artifacts
|
|
256
|
+
if artifact.text is None
|
|
257
|
+
],
|
|
258
|
+
output=output,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def construct_notice(
|
|
263
|
+
run: FollowedRun, *, task_id: str | None = None, error: str | None = None
|
|
264
|
+
) -> FollowUpEnvelope:
|
|
265
|
+
return FollowUpEnvelope(
|
|
266
|
+
id=f"{run.run_id}{FOLLOW_UP_SUFFIX}:notice",
|
|
267
|
+
sender=run.then.to,
|
|
268
|
+
to=run.requester,
|
|
269
|
+
schedule_id=run.schedule_id,
|
|
270
|
+
run_id=run.run_id,
|
|
271
|
+
skill=run.then.skill,
|
|
272
|
+
task_id=task_id,
|
|
273
|
+
error=error,
|
|
274
|
+
)
|