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,566 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import contextlib
|
|
3
|
+
import math
|
|
4
|
+
from collections.abc import Awaitable, Callable, Sequence, Set
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from agentjam._agent_access import AccessBroker
|
|
9
|
+
from agentjam._agent_delegations import Delegations
|
|
10
|
+
from agentjam._agent_judging import ProposalJudge
|
|
11
|
+
from agentjam._agent_options import (
|
|
12
|
+
AttachedReply,
|
|
13
|
+
ForwardSettings,
|
|
14
|
+
OnMessage,
|
|
15
|
+
OnTask,
|
|
16
|
+
ReplyOutput,
|
|
17
|
+
)
|
|
18
|
+
from agentjam._agent_support import (
|
|
19
|
+
DEDUPE_WINDOW,
|
|
20
|
+
STEERED,
|
|
21
|
+
UNTASKED_ATTACHMENTS,
|
|
22
|
+
Judgment,
|
|
23
|
+
ReceivedTask,
|
|
24
|
+
await_preparations,
|
|
25
|
+
compute_key,
|
|
26
|
+
construct_event,
|
|
27
|
+
is_accepting,
|
|
28
|
+
is_run,
|
|
29
|
+
is_schedule,
|
|
30
|
+
list_own_providers,
|
|
31
|
+
resolve_principal,
|
|
32
|
+
)
|
|
33
|
+
from agentjam._attachments import AttachmentSource, receive_attachments
|
|
34
|
+
from agentjam._connections import ConnectionGrant, ConnectionHolder
|
|
35
|
+
from agentjam._errors import AgentJamError, ReassignedError, describe_error
|
|
36
|
+
from agentjam._files import File
|
|
37
|
+
from agentjam._idempotency import IdempotencyCache
|
|
38
|
+
from agentjam._inputs import ValueProblem
|
|
39
|
+
from agentjam._protocol import (
|
|
40
|
+
AttachmentRef,
|
|
41
|
+
ErrorEnvelope,
|
|
42
|
+
MessageEnvelope,
|
|
43
|
+
ParentRef,
|
|
44
|
+
ProposeEnvelope,
|
|
45
|
+
RecurEnvelope,
|
|
46
|
+
ReplyEnvelope,
|
|
47
|
+
TaskRef,
|
|
48
|
+
)
|
|
49
|
+
from agentjam._recaps import recap_from_wire
|
|
50
|
+
from agentjam._relay.base import Draft
|
|
51
|
+
from agentjam._signals import Signal
|
|
52
|
+
from agentjam._tasks.incoming import (
|
|
53
|
+
ExecutionLink,
|
|
54
|
+
ParsedInput,
|
|
55
|
+
TaskDetails,
|
|
56
|
+
TaskExecution,
|
|
57
|
+
TaskOutput,
|
|
58
|
+
TaskSender,
|
|
59
|
+
current_execution,
|
|
60
|
+
)
|
|
61
|
+
from agentjam._tasks.outgoing import TaskHandle
|
|
62
|
+
from agentjam._types import (
|
|
63
|
+
Delegate,
|
|
64
|
+
HistoryPage,
|
|
65
|
+
HistoryQuery,
|
|
66
|
+
IncomingMessage,
|
|
67
|
+
Price,
|
|
68
|
+
SkillCard,
|
|
69
|
+
)
|
|
70
|
+
from agentjam._utils import create_id, resolve_value, spawn
|
|
71
|
+
|
|
72
|
+
if TYPE_CHECKING:
|
|
73
|
+
from agentjam._agent import Agent
|
|
74
|
+
|
|
75
|
+
Work = Callable[[TaskExecution], Awaitable[TaskOutput]]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True)
|
|
79
|
+
class HostLink:
|
|
80
|
+
"""What incoming work reaches through the agent hosting it."""
|
|
81
|
+
|
|
82
|
+
post: Callable[[Draft], Awaitable[None]]
|
|
83
|
+
construct_sender: Callable[[str, str], TaskSender]
|
|
84
|
+
upload: Callable[[File, Signal], Awaitable[str | None]]
|
|
85
|
+
"""Uploads a file artifact, returning None when the relay can't store files."""
|
|
86
|
+
upload_attachments: Callable[
|
|
87
|
+
[Sequence[AttachmentSource] | None], Awaitable[list[AttachmentRef] | None]
|
|
88
|
+
]
|
|
89
|
+
list_history: Callable[[TaskRef, HistoryQuery], Awaitable[HistoryPage]]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class Executions:
|
|
93
|
+
"""The tasks and messages an agent receives, which it runs and answers."""
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
agent: "Agent",
|
|
98
|
+
link: HostLink,
|
|
99
|
+
*,
|
|
100
|
+
judge: ProposalJudge,
|
|
101
|
+
access: AccessBroker,
|
|
102
|
+
delegations: Delegations,
|
|
103
|
+
on_task: OnTask | None,
|
|
104
|
+
on_message: OnMessage | None,
|
|
105
|
+
forward: ForwardSettings,
|
|
106
|
+
) -> None:
|
|
107
|
+
self._agent = agent
|
|
108
|
+
self._link = link
|
|
109
|
+
self._judge = judge
|
|
110
|
+
self._access = access
|
|
111
|
+
self._delegations = delegations
|
|
112
|
+
self._on_task = on_task
|
|
113
|
+
self._on_message = on_message
|
|
114
|
+
self._forward = forward
|
|
115
|
+
self._incoming: dict[str, TaskExecution] = {}
|
|
116
|
+
self._drains: set[asyncio.Event] = set()
|
|
117
|
+
self._replies = IdempotencyCache[ReplyEnvelope | ErrorEnvelope](DEDUPE_WINDOW)
|
|
118
|
+
self._verdicts = IdempotencyCache[Judgment](DEDUPE_WINDOW)
|
|
119
|
+
self._performed = IdempotencyCache[None](DEDUPE_WINDOW)
|
|
120
|
+
|
|
121
|
+
def find(self, requester: str, task_id: str | None) -> TaskExecution | None:
|
|
122
|
+
if task_id is None:
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
return self._incoming.get(compute_key(requester, task_id))
|
|
126
|
+
|
|
127
|
+
def find_running(self) -> TaskExecution | None:
|
|
128
|
+
"""Finds the task this agent runs in the current context."""
|
|
129
|
+
task = current_execution()
|
|
130
|
+
if task is None:
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
return task if self.find(task.sender, task.id) is task else None
|
|
134
|
+
|
|
135
|
+
def find_current(self, requester: str) -> TaskExecution | None:
|
|
136
|
+
"""Finds the task `requester` proposed that this agent runs in the current context."""
|
|
137
|
+
task = self.find_running()
|
|
138
|
+
|
|
139
|
+
return task if task is not None and task.sender == requester else None
|
|
140
|
+
|
|
141
|
+
def find_above(self, task: TaskHandle | None) -> TaskExecution | None:
|
|
142
|
+
"""Finds the task a delegated task was delegated as part of."""
|
|
143
|
+
parent = None if task is None else task.parent
|
|
144
|
+
if parent is None or parent.kind != "task":
|
|
145
|
+
return None
|
|
146
|
+
|
|
147
|
+
return self.find(parent.requester, parent.id)
|
|
148
|
+
|
|
149
|
+
def cancel_held(self, holder: ConnectionHolder, reason: str) -> None:
|
|
150
|
+
if (task := self.find(holder.requester, holder.id)) is not None:
|
|
151
|
+
task.cancel(reason)
|
|
152
|
+
|
|
153
|
+
def abandon(self, peer: str, abandoned: Set[str] | None) -> None:
|
|
154
|
+
"""Cancels the tasks a disconnected peer proposed, or only those among `abandoned`, leaving runs going."""
|
|
155
|
+
for task in list(self._incoming.values()):
|
|
156
|
+
if task.sender != peer or is_run(task):
|
|
157
|
+
continue
|
|
158
|
+
|
|
159
|
+
if abandoned is None or task.id in abandoned:
|
|
160
|
+
task.cancel(f"{peer} disconnected")
|
|
161
|
+
|
|
162
|
+
def abandon_all(self, error: AgentJamError) -> None:
|
|
163
|
+
for task in list(self._incoming.values()):
|
|
164
|
+
task.cancel(str(error))
|
|
165
|
+
|
|
166
|
+
async def close(self, reason: str) -> None:
|
|
167
|
+
"""Fails every running task, except resumable ones, which move to another session of this agent."""
|
|
168
|
+
tasks = list(self._incoming.values())
|
|
169
|
+
for id, task in list(self._incoming.items()):
|
|
170
|
+
if task.resumable:
|
|
171
|
+
# WARNING: forgotten before abandoning, so ending here doesn't release the connections the resuming session uses.
|
|
172
|
+
self._forget(id)
|
|
173
|
+
task.abandon(AgentJamError(reason))
|
|
174
|
+
|
|
175
|
+
await asyncio.gather(
|
|
176
|
+
*(task.fail(AgentJamError(reason)) for task in tasks if not task.resumable),
|
|
177
|
+
return_exceptions=True,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
async def drain(self, deadline: float) -> None:
|
|
181
|
+
"""Waits for every running task to finish, for at most `deadline` seconds."""
|
|
182
|
+
if not self._incoming:
|
|
183
|
+
return
|
|
184
|
+
|
|
185
|
+
drained = asyncio.Event()
|
|
186
|
+
self._drains.add(drained)
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
with contextlib.suppress(TimeoutError):
|
|
190
|
+
async with asyncio.timeout(None if math.isinf(deadline) else deadline):
|
|
191
|
+
await drained.wait()
|
|
192
|
+
finally:
|
|
193
|
+
self._drains.discard(drained)
|
|
194
|
+
|
|
195
|
+
async def reply(self, envelope: MessageEnvelope) -> None:
|
|
196
|
+
request = compute_key(envelope.sender, envelope.id)
|
|
197
|
+
remembered = self._replies.remember(request, lambda: self._answer(envelope))
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
await self._link.post(await asyncio.shield(remembered.response))
|
|
201
|
+
finally:
|
|
202
|
+
if not remembered.repeated:
|
|
203
|
+
self._replies.release(request)
|
|
204
|
+
|
|
205
|
+
async def consider(self, proposal: ProposeEnvelope) -> None:
|
|
206
|
+
"""Answers a proposal, then runs the task it accepted, returning once the task ends."""
|
|
207
|
+
id = compute_key(proposal.sender, proposal.id)
|
|
208
|
+
remembered = self._verdicts.remember(id, lambda: self._judge.judge(proposal))
|
|
209
|
+
judgment = await asyncio.shield(remembered.response)
|
|
210
|
+
|
|
211
|
+
if remembered.repeated:
|
|
212
|
+
await self._link.post(judgment.verdict)
|
|
213
|
+
if (running := self._incoming.get(id)) is not None:
|
|
214
|
+
await running.wait_settled()
|
|
215
|
+
|
|
216
|
+
return
|
|
217
|
+
|
|
218
|
+
holder = ConnectionHolder(requester=proposal.sender, id=proposal.id)
|
|
219
|
+
if not is_accepting(judgment.verdict) or is_schedule(proposal):
|
|
220
|
+
self._verdicts.release(id)
|
|
221
|
+
|
|
222
|
+
try:
|
|
223
|
+
await self._link.post(judgment.verdict)
|
|
224
|
+
except Exception:
|
|
225
|
+
sources = self._access.list_skill_sources(proposal.skill)
|
|
226
|
+
self._access.release(holder, sources)
|
|
227
|
+
raise
|
|
228
|
+
|
|
229
|
+
return
|
|
230
|
+
|
|
231
|
+
task = TaskExecution(
|
|
232
|
+
TaskDetails(
|
|
233
|
+
id=proposal.id,
|
|
234
|
+
sender=proposal.sender,
|
|
235
|
+
principal=proposal.principal,
|
|
236
|
+
text=proposal.text,
|
|
237
|
+
skill=proposal.skill,
|
|
238
|
+
input=proposal.input,
|
|
239
|
+
parsed_input=judgment.parsed_input,
|
|
240
|
+
attachments=judgment.attachments,
|
|
241
|
+
previous=(
|
|
242
|
+
None
|
|
243
|
+
if proposal.previous is None
|
|
244
|
+
else recap_from_wire(proposal.previous)
|
|
245
|
+
),
|
|
246
|
+
price=(
|
|
247
|
+
None
|
|
248
|
+
if proposal.prepare
|
|
249
|
+
else self._find_price(proposal.skill, proposal.sender)
|
|
250
|
+
),
|
|
251
|
+
card=None
|
|
252
|
+
if proposal.prepare
|
|
253
|
+
else self._find_skill_card(proposal.skill),
|
|
254
|
+
is_self=proposal.sender == self._agent.name,
|
|
255
|
+
held_cents=proposal.held_cents,
|
|
256
|
+
resumable=not proposal.prepare and self._is_resumable(proposal.skill),
|
|
257
|
+
access=self._access.construct_access(proposal.skill, holder),
|
|
258
|
+
),
|
|
259
|
+
self._construct_link(proposal, holder),
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
def clean_up() -> None:
|
|
263
|
+
self._verdicts.forget(id, remembered.response)
|
|
264
|
+
self._access.readiness.forget(holder)
|
|
265
|
+
self._release(id, task, holder)
|
|
266
|
+
|
|
267
|
+
self._incoming[id] = task
|
|
268
|
+
self._access.readiness.expect(holder, list_own_providers(judgment.verdict))
|
|
269
|
+
|
|
270
|
+
try:
|
|
271
|
+
await self._link.post(judgment.verdict)
|
|
272
|
+
except Exception:
|
|
273
|
+
clean_up()
|
|
274
|
+
raise
|
|
275
|
+
|
|
276
|
+
async def work(incoming: TaskExecution) -> TaskOutput:
|
|
277
|
+
await self._access.readiness.wait(holder, incoming.signal)
|
|
278
|
+
if not proposal.prepare:
|
|
279
|
+
return await self._run_on_task(incoming)
|
|
280
|
+
|
|
281
|
+
# WARNING: the relay cancels a task's children once it ends, so a preparation outlives the ones below it.
|
|
282
|
+
await await_preparations(judgment.preparations)
|
|
283
|
+
|
|
284
|
+
return None
|
|
285
|
+
|
|
286
|
+
spawn(self._execute(task, clean_up, work))
|
|
287
|
+
await task.wait_settled()
|
|
288
|
+
|
|
289
|
+
async def perform(self, recur: RecurEnvelope) -> None:
|
|
290
|
+
"""Runs a run of a schedule this agent executes, once per run however often the relay delivers it."""
|
|
291
|
+
id = compute_key(recur.sender, recur.task_id)
|
|
292
|
+
remembered = self._performed.remember(id, lambda: self._run(id, recur))
|
|
293
|
+
await asyncio.shield(remembered.response)
|
|
294
|
+
|
|
295
|
+
async def resume(self, proposal: ProposeEnvelope) -> None:
|
|
296
|
+
"""Runs a task of a resumable skill that the relay moved here from a departed session of this agent.
|
|
297
|
+
|
|
298
|
+
It follows the tasks the task delegated there, so their events aren't refused as unknown. A repeat of an
|
|
299
|
+
attempt already running is ignored.
|
|
300
|
+
"""
|
|
301
|
+
attempt = proposal.attempt if proposal.attempt is not None else 2
|
|
302
|
+
id = compute_key(proposal.sender, proposal.id)
|
|
303
|
+
holder = ConnectionHolder(requester=proposal.sender, id=proposal.id)
|
|
304
|
+
received = await self._receive_task(proposal)
|
|
305
|
+
|
|
306
|
+
running = self._incoming.get(id)
|
|
307
|
+
if running is not None and running.attempt >= attempt:
|
|
308
|
+
return
|
|
309
|
+
|
|
310
|
+
if running is not None:
|
|
311
|
+
running.abandon(
|
|
312
|
+
ReassignedError(f'Task "{proposal.id}" resumed as attempt {attempt}')
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
parent = ParentRef(kind="task", requester=proposal.sender, id=proposal.id)
|
|
316
|
+
for child in proposal.children or []:
|
|
317
|
+
delegated = self._delegations.track(child.to, child.id, parent=parent)
|
|
318
|
+
spawn(self._delegations.reattach(delegated))
|
|
319
|
+
|
|
320
|
+
task = TaskExecution(
|
|
321
|
+
TaskDetails(
|
|
322
|
+
id=proposal.id,
|
|
323
|
+
sender=proposal.sender,
|
|
324
|
+
principal=proposal.principal,
|
|
325
|
+
text=proposal.text,
|
|
326
|
+
skill=proposal.skill,
|
|
327
|
+
input=proposal.input,
|
|
328
|
+
parsed_input=received.parsed_input,
|
|
329
|
+
attachments=received.attachments,
|
|
330
|
+
attempt=attempt,
|
|
331
|
+
price=self._find_price(proposal.skill, proposal.sender),
|
|
332
|
+
card=self._find_skill_card(proposal.skill),
|
|
333
|
+
is_self=proposal.sender == self._agent.name,
|
|
334
|
+
held_cents=proposal.held_cents,
|
|
335
|
+
resumable=True,
|
|
336
|
+
access=self._access.construct_access(proposal.skill, holder),
|
|
337
|
+
),
|
|
338
|
+
self._construct_link(proposal, holder),
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
self._incoming[id] = task
|
|
342
|
+
await self._execute(
|
|
343
|
+
task,
|
|
344
|
+
lambda: self._release(id, task, holder),
|
|
345
|
+
self._construct_work(received),
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
async def _answer(self, envelope: MessageEnvelope) -> ReplyEnvelope | ErrorEnvelope:
|
|
349
|
+
try:
|
|
350
|
+
output = await self._compute_reply(envelope)
|
|
351
|
+
reply = AttachedReply(text=output) if isinstance(output, str) else output
|
|
352
|
+
if envelope.task_id is None and reply.attachments:
|
|
353
|
+
raise AgentJamError(UNTASKED_ATTACHMENTS)
|
|
354
|
+
|
|
355
|
+
return ReplyEnvelope(
|
|
356
|
+
id=create_id(),
|
|
357
|
+
to=envelope.sender,
|
|
358
|
+
reply_to=envelope.id,
|
|
359
|
+
text=reply.text,
|
|
360
|
+
attachments=await self._link.upload_attachments(reply.attachments),
|
|
361
|
+
)
|
|
362
|
+
except Exception as error:
|
|
363
|
+
return ErrorEnvelope(
|
|
364
|
+
id=create_id(),
|
|
365
|
+
to=envelope.sender,
|
|
366
|
+
reply_to=envelope.id,
|
|
367
|
+
error=describe_error(error),
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
async def _compute_reply(self, envelope: MessageEnvelope) -> ReplyOutput:
|
|
371
|
+
attachments = await receive_attachments(envelope.attachments)
|
|
372
|
+
asked = self._find_asked(envelope)
|
|
373
|
+
|
|
374
|
+
if asked is not None:
|
|
375
|
+
reply = await asked.send(
|
|
376
|
+
f"{envelope.sender}: {envelope.text}",
|
|
377
|
+
attachments=[attachment.file for attachment in attachments],
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
files = [attachment.file for attachment in reply.attachments]
|
|
381
|
+
|
|
382
|
+
return AttachedReply(text=reply.text, attachments=files)
|
|
383
|
+
|
|
384
|
+
message = IncomingMessage(
|
|
385
|
+
id=envelope.id,
|
|
386
|
+
sender=envelope.sender,
|
|
387
|
+
to=envelope.to,
|
|
388
|
+
conversation_id=envelope.conversation_id,
|
|
389
|
+
text=envelope.text,
|
|
390
|
+
history=envelope.history,
|
|
391
|
+
task_id=envelope.task_id,
|
|
392
|
+
task=None if envelope.task is None else recap_from_wire(envelope.task),
|
|
393
|
+
steer=envelope.steer is True,
|
|
394
|
+
attachments=attachments,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
steered = self.find(envelope.sender, envelope.task_id)
|
|
398
|
+
if envelope.steer and steered is not None and steered.queue_message(message):
|
|
399
|
+
return STEERED
|
|
400
|
+
|
|
401
|
+
if self._on_message is None:
|
|
402
|
+
raise AgentJamError(f"{self._agent.name} does not accept messages")
|
|
403
|
+
|
|
404
|
+
return await resolve_value(self._on_message(message, self._agent))
|
|
405
|
+
|
|
406
|
+
def _find_asked(self, envelope: MessageEnvelope) -> TaskExecution | None:
|
|
407
|
+
"""Finds the task to pass a question about a delegated task up to: the one it was delegated as part of."""
|
|
408
|
+
if not self._forward.questions or envelope.task_id is None:
|
|
409
|
+
return None
|
|
410
|
+
|
|
411
|
+
return self.find_above(
|
|
412
|
+
self._delegations.find(envelope.sender, envelope.task_id)
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
async def _run(self, id: str, recur: RecurEnvelope) -> None:
|
|
416
|
+
received = await self._receive_task(recur)
|
|
417
|
+
holder = ConnectionHolder(requester=recur.sender, id=recur.schedule_id)
|
|
418
|
+
task = TaskExecution(
|
|
419
|
+
TaskDetails(
|
|
420
|
+
id=recur.task_id,
|
|
421
|
+
sender=recur.sender,
|
|
422
|
+
principal=recur.principal,
|
|
423
|
+
text=recur.text,
|
|
424
|
+
skill=recur.skill,
|
|
425
|
+
input=recur.input,
|
|
426
|
+
parsed_input=received.parsed_input,
|
|
427
|
+
attachments=received.attachments,
|
|
428
|
+
schedule_id=recur.schedule_id,
|
|
429
|
+
run=recur.run,
|
|
430
|
+
event=construct_event(recur),
|
|
431
|
+
price=self._find_price(recur.skill, recur.sender),
|
|
432
|
+
card=self._find_skill_card(recur.skill),
|
|
433
|
+
is_self=recur.sender == self._agent.name,
|
|
434
|
+
held_cents=recur.held_cents,
|
|
435
|
+
access=self._access.construct_access(recur.skill, holder),
|
|
436
|
+
),
|
|
437
|
+
self._construct_link(recur, holder),
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
def clean_up() -> None:
|
|
441
|
+
self._forget(id)
|
|
442
|
+
self._performed.release(id)
|
|
443
|
+
|
|
444
|
+
self._incoming[id] = task
|
|
445
|
+
await self._execute(task, clean_up, self._construct_work(received))
|
|
446
|
+
|
|
447
|
+
async def _receive_task(
|
|
448
|
+
self, envelope: ProposeEnvelope | RecurEnvelope
|
|
449
|
+
) -> ReceivedTask:
|
|
450
|
+
"""Downloads a task's attachments and parses its input, keeping the first failure for the task's run to raise."""
|
|
451
|
+
failure: Exception | None = None
|
|
452
|
+
|
|
453
|
+
try:
|
|
454
|
+
attachments = await receive_attachments(envelope.attachments)
|
|
455
|
+
except Exception as error:
|
|
456
|
+
attachments, failure = [], error
|
|
457
|
+
|
|
458
|
+
try:
|
|
459
|
+
parsed = self._judge.parse_input(envelope.skill, envelope.input)
|
|
460
|
+
except Exception as error:
|
|
461
|
+
parsed = ValueProblem(describe_error(error))
|
|
462
|
+
|
|
463
|
+
if isinstance(parsed, ValueProblem):
|
|
464
|
+
problem = AgentJamError(parsed.problem)
|
|
465
|
+
|
|
466
|
+
return ReceivedTask(
|
|
467
|
+
attachments, None, failure if failure is not None else problem
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
parsed_input = None if parsed is None else ParsedInput(parsed.value)
|
|
471
|
+
|
|
472
|
+
return ReceivedTask(attachments, parsed_input, failure)
|
|
473
|
+
|
|
474
|
+
def _construct_work(self, received: ReceivedTask) -> Work:
|
|
475
|
+
async def work(incoming: TaskExecution) -> TaskOutput:
|
|
476
|
+
if received.failure is not None:
|
|
477
|
+
raise received.failure
|
|
478
|
+
|
|
479
|
+
return await self._run_on_task(incoming)
|
|
480
|
+
|
|
481
|
+
return work
|
|
482
|
+
|
|
483
|
+
async def _run_on_task(self, task: TaskExecution) -> TaskOutput:
|
|
484
|
+
if self._on_task is None:
|
|
485
|
+
return None
|
|
486
|
+
|
|
487
|
+
return await resolve_value(self._on_task(task, self._agent))
|
|
488
|
+
|
|
489
|
+
def _construct_link(
|
|
490
|
+
self, envelope: ProposeEnvelope | RecurEnvelope, holder: ConnectionHolder
|
|
491
|
+
) -> ExecutionLink:
|
|
492
|
+
"""Builds how a task reaches the network, where `holder` holds its connections, which is its schedule for a run."""
|
|
493
|
+
requester = envelope.sender
|
|
494
|
+
task_id = (
|
|
495
|
+
envelope.id if isinstance(envelope, ProposeEnvelope) else envelope.task_id
|
|
496
|
+
)
|
|
497
|
+
parent = ParentRef(kind="task", requester=requester, id=task_id)
|
|
498
|
+
anchor = TaskRef(requester=requester, task_id=task_id)
|
|
499
|
+
grant = ConnectionGrant(
|
|
500
|
+
requester=holder.requester,
|
|
501
|
+
id=holder.id,
|
|
502
|
+
principal=resolve_principal(envelope.principal, requester),
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
async def prepare(delegates: list[Delegate]) -> None:
|
|
506
|
+
await self._judge.prepare_each(delegates, parent)
|
|
507
|
+
|
|
508
|
+
async def list_history(query: HistoryQuery) -> HistoryPage:
|
|
509
|
+
return await self._link.list_history(anchor, query)
|
|
510
|
+
|
|
511
|
+
return ExecutionLink(
|
|
512
|
+
post=self._link.post,
|
|
513
|
+
send=self._link.construct_sender(requester, task_id),
|
|
514
|
+
upload=self._link.upload,
|
|
515
|
+
prepare=prepare,
|
|
516
|
+
list_history=list_history,
|
|
517
|
+
require_access=self._access.construct_requirer(
|
|
518
|
+
envelope.skill, grant, task_id
|
|
519
|
+
),
|
|
520
|
+
)
|
|
521
|
+
|
|
522
|
+
async def _execute(
|
|
523
|
+
self, task: TaskExecution, clean_up: Callable[[], None], work: Work
|
|
524
|
+
) -> None:
|
|
525
|
+
try:
|
|
526
|
+
await task.perform(work)
|
|
527
|
+
except ReassignedError:
|
|
528
|
+
pass
|
|
529
|
+
except Exception as error:
|
|
530
|
+
self._agent.report_error(error)
|
|
531
|
+
finally:
|
|
532
|
+
clean_up()
|
|
533
|
+
|
|
534
|
+
def _release(self, id: str, task: TaskExecution, holder: ConnectionHolder) -> None:
|
|
535
|
+
"""Forgets a finished task and releases its connections, unless a resumed attempt replaced it meanwhile."""
|
|
536
|
+
if self._incoming.get(id) is not task:
|
|
537
|
+
return
|
|
538
|
+
|
|
539
|
+
self._forget(id)
|
|
540
|
+
self._access.release(holder, self._access.list_skill_sources(task.skill))
|
|
541
|
+
|
|
542
|
+
def _forget(self, id: str) -> None:
|
|
543
|
+
self._incoming.pop(id, None)
|
|
544
|
+
if self._incoming:
|
|
545
|
+
return
|
|
546
|
+
|
|
547
|
+
for drained in self._drains:
|
|
548
|
+
drained.set()
|
|
549
|
+
|
|
550
|
+
def _find_price(self, skill: str | None, requester: str) -> Price | None:
|
|
551
|
+
if requester == self._agent.name:
|
|
552
|
+
return None
|
|
553
|
+
|
|
554
|
+
card = self._find_skill_card(skill)
|
|
555
|
+
|
|
556
|
+
return None if card is None else card.price
|
|
557
|
+
|
|
558
|
+
def _find_skill_card(self, skill: str | None) -> SkillCard | None:
|
|
559
|
+
return next(
|
|
560
|
+
(card for card in self._agent.card.skills if card.name == skill), None
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
def _is_resumable(self, skill: str | None) -> bool:
|
|
564
|
+
card = self._find_skill_card(skill)
|
|
565
|
+
|
|
566
|
+
return card is not None and card.resumable is True
|