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,548 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from datetime import UTC, datetime
|
|
4
|
+
from typing import assert_never
|
|
5
|
+
|
|
6
|
+
from agentjam._errors import AgentJamError
|
|
7
|
+
from agentjam._protocol import (
|
|
8
|
+
AcceptEnvelope,
|
|
9
|
+
ActionsEnvelope,
|
|
10
|
+
ArtifactEnvelope,
|
|
11
|
+
BudgetEnvelope,
|
|
12
|
+
CancelEnvelope,
|
|
13
|
+
CommitEnvelope,
|
|
14
|
+
CompleteEnvelope,
|
|
15
|
+
ConnectionReadyEnvelope,
|
|
16
|
+
ConnectionRequiredEnvelope,
|
|
17
|
+
ConnectionRevokedEnvelope,
|
|
18
|
+
ConnectionStoredEnvelope,
|
|
19
|
+
DeliveredEnvelope,
|
|
20
|
+
DisconnectEnvelope,
|
|
21
|
+
ErrorEnvelope,
|
|
22
|
+
FailEnvelope,
|
|
23
|
+
FollowUpEnvelope,
|
|
24
|
+
MessageEnvelope,
|
|
25
|
+
ProposeEnvelope,
|
|
26
|
+
RecappedArtifactRef,
|
|
27
|
+
RecurEnvelope,
|
|
28
|
+
RejectEnvelope,
|
|
29
|
+
ReplyEnvelope,
|
|
30
|
+
RequireEnvelope,
|
|
31
|
+
ScheduleEndEnvelope,
|
|
32
|
+
ScheduleHandoverEnvelope,
|
|
33
|
+
ScheduleStartEnvelope,
|
|
34
|
+
TaskRecapRef,
|
|
35
|
+
TaskRef,
|
|
36
|
+
TriggerFailedEnvelope,
|
|
37
|
+
UpdateEnvelope,
|
|
38
|
+
)
|
|
39
|
+
from agentjam._relay.sessions import request_key
|
|
40
|
+
from agentjam._relay.uploads import DEFAULT_MIME_TYPE
|
|
41
|
+
from agentjam._types import (
|
|
42
|
+
FollowUp,
|
|
43
|
+
HistoryPage,
|
|
44
|
+
HistoryQuery,
|
|
45
|
+
Message,
|
|
46
|
+
PastTask,
|
|
47
|
+
SkillCard,
|
|
48
|
+
TaskStatus,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
MAX_UPDATES = 20
|
|
52
|
+
MAX_ARTIFACTS = 20
|
|
53
|
+
MAX_INLINE_TEXT = 20_000
|
|
54
|
+
MAX_HISTORY = 50
|
|
55
|
+
DEFAULT_HISTORY_PAGE = 20
|
|
56
|
+
MAX_HISTORY_PAGE = 100
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(eq=False)
|
|
60
|
+
class RecordedTask:
|
|
61
|
+
executor: str
|
|
62
|
+
principal: str | None
|
|
63
|
+
schedule_id: str | None
|
|
64
|
+
run: int | None
|
|
65
|
+
recap: TaskRecapRef
|
|
66
|
+
partials: dict[str, list[str]] = field(default_factory=dict)
|
|
67
|
+
"""The chunks of each artifact still streaming, by artifact id."""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class Question:
|
|
72
|
+
message: Message
|
|
73
|
+
task: str
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_now() -> str:
|
|
77
|
+
return datetime.now(UTC).isoformat()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def is_scheduled(proposal: ProposeEnvelope) -> bool:
|
|
81
|
+
return proposal.cron is not None or proposal.trigger is not None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def is_open(task: RecordedTask) -> bool:
|
|
85
|
+
return task.recap.ended_at is None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def construct_task_key(task: RecordedTask) -> str:
|
|
89
|
+
return request_key(task.recap.requester, task.recap.task_id)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def is_past_of(anchor: RecordedTask, task: RecordedTask) -> bool:
|
|
93
|
+
if is_open(task) or task.recap.status == "rejected":
|
|
94
|
+
return False
|
|
95
|
+
|
|
96
|
+
if anchor.schedule_id is not None:
|
|
97
|
+
return (
|
|
98
|
+
task.schedule_id == anchor.schedule_id
|
|
99
|
+
and task.recap.requester == anchor.recap.requester
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
task.schedule_id is None
|
|
104
|
+
and task.executor == anchor.executor
|
|
105
|
+
and task.principal == anchor.principal
|
|
106
|
+
and task.recap.skill == anchor.recap.skill
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def summarize_past(task: RecordedTask) -> PastTask:
|
|
111
|
+
recap = task.recap
|
|
112
|
+
|
|
113
|
+
return PastTask(
|
|
114
|
+
requester=recap.requester,
|
|
115
|
+
id=recap.task_id,
|
|
116
|
+
run=task.run,
|
|
117
|
+
skill=recap.skill,
|
|
118
|
+
status=recap.status,
|
|
119
|
+
has_result=bool(recap.artifacts),
|
|
120
|
+
created_at=datetime.fromisoformat(recap.created_at),
|
|
121
|
+
ended_at=None
|
|
122
|
+
if recap.ended_at is None
|
|
123
|
+
else datetime.fromisoformat(recap.ended_at),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def assert_history_limit(limit: int) -> None:
|
|
128
|
+
if 1 <= limit <= MAX_HISTORY_PAGE:
|
|
129
|
+
return
|
|
130
|
+
|
|
131
|
+
raise AgentJamError(
|
|
132
|
+
f"A history page holds 1 to {MAX_HISTORY_PAGE} tasks, not {limit}"
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def page_history(past: list[RecordedTask], query: HistoryQuery) -> HistoryPage:
|
|
137
|
+
"""Pages through past tasks, newest first, continuing after the task a cursor names."""
|
|
138
|
+
limit = DEFAULT_HISTORY_PAGE if query.limit is None else query.limit
|
|
139
|
+
assert_history_limit(limit)
|
|
140
|
+
keys = [construct_task_key(task) for task in past]
|
|
141
|
+
if query.cursor is not None and query.cursor not in keys:
|
|
142
|
+
raise AgentJamError(f'The cursor "{query.cursor}" isn\'t from this history')
|
|
143
|
+
|
|
144
|
+
start = 0 if query.cursor is None else keys.index(query.cursor) + 1
|
|
145
|
+
page = past[start : start + limit]
|
|
146
|
+
has_more = start + limit < len(past)
|
|
147
|
+
|
|
148
|
+
return HistoryPage(
|
|
149
|
+
tasks=[summarize_past(task) for task in page],
|
|
150
|
+
next=construct_task_key(page[-1]) if has_more and page else None,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def finish_text(envelope: ArtifactEnvelope, chunks: list[str]) -> RecappedArtifactRef:
|
|
155
|
+
text = "".join(chunks)
|
|
156
|
+
is_truncated = len(text) > MAX_INLINE_TEXT
|
|
157
|
+
|
|
158
|
+
return RecappedArtifactRef(
|
|
159
|
+
artifact_id=envelope.artifact_id,
|
|
160
|
+
kind=envelope.kind,
|
|
161
|
+
name=envelope.name,
|
|
162
|
+
mime_type=envelope.mime_type,
|
|
163
|
+
key=envelope.key,
|
|
164
|
+
text=text[:MAX_INLINE_TEXT],
|
|
165
|
+
truncated=True if is_truncated else None,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def finish_file(envelope: ArtifactEnvelope, chunks: list[str]) -> RecappedArtifactRef:
|
|
170
|
+
data = b"".join(base64.b64decode(chunk) for chunk in chunks)
|
|
171
|
+
mime_type = DEFAULT_MIME_TYPE if envelope.mime_type is None else envelope.mime_type
|
|
172
|
+
encoded = base64.b64encode(data).decode()
|
|
173
|
+
is_linked = envelope.url is not None
|
|
174
|
+
|
|
175
|
+
return RecappedArtifactRef(
|
|
176
|
+
artifact_id=envelope.artifact_id,
|
|
177
|
+
kind=envelope.kind,
|
|
178
|
+
name=envelope.name,
|
|
179
|
+
mime_type=envelope.mime_type,
|
|
180
|
+
key=envelope.key,
|
|
181
|
+
url=envelope.url if is_linked else f"data:{mime_type};base64,{encoded}",
|
|
182
|
+
size=None if is_linked else len(data),
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def finish_artifact(
|
|
187
|
+
envelope: ArtifactEnvelope, chunks: list[str]
|
|
188
|
+
) -> RecappedArtifactRef:
|
|
189
|
+
if envelope.kind == "text":
|
|
190
|
+
return finish_text(envelope, chunks)
|
|
191
|
+
|
|
192
|
+
return finish_file(envelope, chunks)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def construct_recap(
|
|
196
|
+
requester: str,
|
|
197
|
+
id: str,
|
|
198
|
+
source: ProposeEnvelope | RecurEnvelope,
|
|
199
|
+
status: TaskStatus,
|
|
200
|
+
follow_up: FollowUp | None = None,
|
|
201
|
+
) -> TaskRecapRef:
|
|
202
|
+
return TaskRecapRef(
|
|
203
|
+
task_id=id,
|
|
204
|
+
requester=requester,
|
|
205
|
+
text=source.text,
|
|
206
|
+
skill=source.skill,
|
|
207
|
+
input=source.input,
|
|
208
|
+
attachments=source.attachments,
|
|
209
|
+
status=status,
|
|
210
|
+
updates=[],
|
|
211
|
+
artifacts=[],
|
|
212
|
+
history=[],
|
|
213
|
+
follow_up=follow_up,
|
|
214
|
+
created_at=get_now(),
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def construct_follow_up(previous: TaskRecapRef | None) -> FollowUp | None:
|
|
219
|
+
if previous is None:
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
chained = previous.follow_up
|
|
223
|
+
|
|
224
|
+
return FollowUp(
|
|
225
|
+
previous=previous.task_id,
|
|
226
|
+
root=previous.task_id if chained is None else chained.root,
|
|
227
|
+
round=1 if chained is None else chained.round + 1,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class FollowUpLedger:
|
|
232
|
+
"""Keeps what each task and run routed through a `MemoryRelay` asked for and produced.
|
|
233
|
+
|
|
234
|
+
A follow-up's executor and messages about a task carry a recap of it, and an executor can list its past tasks.
|
|
235
|
+
Follow-ups chain like the network relay's, but every task still runs for free, and every task is kept for the
|
|
236
|
+
relay's life.
|
|
237
|
+
"""
|
|
238
|
+
|
|
239
|
+
def __init__(self) -> None:
|
|
240
|
+
self._tasks: dict[str, RecordedTask] = {}
|
|
241
|
+
self._questions: dict[str, Question] = {}
|
|
242
|
+
|
|
243
|
+
def follow(
|
|
244
|
+
self, proposal: ProposeEnvelope, skill: SkillCard | None
|
|
245
|
+
) -> ProposeEnvelope:
|
|
246
|
+
"""Resolves the ended task a proposal follows up on, reusing its input for the same skill when the proposal leaves its own out.
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
The proposal with a recap of that task as `previous`, or as is when it follows none.
|
|
250
|
+
Raises:
|
|
251
|
+
AgentJamError: If the proposal isn't a one-off, or the task isn't an ended one its sender requested from the same executor.
|
|
252
|
+
"""
|
|
253
|
+
follows = proposal.follows
|
|
254
|
+
if follows is None:
|
|
255
|
+
return proposal
|
|
256
|
+
|
|
257
|
+
if is_scheduled(proposal) or proposal.prepare or follows == proposal.id:
|
|
258
|
+
raise AgentJamError("Only a one-off task can follow up on an earlier task")
|
|
259
|
+
|
|
260
|
+
previous = self._tasks.get(request_key(proposal.sender, follows))
|
|
261
|
+
if previous is None or previous.executor != proposal.to:
|
|
262
|
+
raise AgentJamError(
|
|
263
|
+
f'"{proposal.sender}" has no task "{follows}" that "{proposal.to}" worked on, so it can\'t follow up on it'
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
if is_open(previous):
|
|
267
|
+
raise AgentJamError(
|
|
268
|
+
f'Task "{follows}" is still open, so it can\'t be followed up on until it ends'
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
is_inherited = (
|
|
272
|
+
proposal.input is None
|
|
273
|
+
and skill is not None
|
|
274
|
+
and skill.input_schema is not None
|
|
275
|
+
and previous.recap.skill == skill.name
|
|
276
|
+
)
|
|
277
|
+
recap = self._recap(previous)
|
|
278
|
+
inherited = recap.input if is_inherited else proposal.input
|
|
279
|
+
|
|
280
|
+
return proposal.model_copy(update={"input": inherited, "previous": recap})
|
|
281
|
+
|
|
282
|
+
def observe(self, envelope: DeliveredEnvelope) -> None:
|
|
283
|
+
"""Records what each one-off task's envelopes say about it, and the conversation turns about it."""
|
|
284
|
+
match envelope:
|
|
285
|
+
case ProposeEnvelope():
|
|
286
|
+
self._record(envelope)
|
|
287
|
+
|
|
288
|
+
case AcceptEnvelope():
|
|
289
|
+
self._set_status(envelope.to, envelope.reply_to, "working")
|
|
290
|
+
|
|
291
|
+
case RequireEnvelope():
|
|
292
|
+
self._set_status(envelope.to, envelope.reply_to, "requires_action")
|
|
293
|
+
|
|
294
|
+
case ConnectionRequiredEnvelope():
|
|
295
|
+
self._set_status(envelope.to, envelope.proposal_id, "requires_action")
|
|
296
|
+
|
|
297
|
+
case RejectEnvelope():
|
|
298
|
+
self._end(envelope.to, envelope.reply_to, "rejected")
|
|
299
|
+
|
|
300
|
+
case ErrorEnvelope():
|
|
301
|
+
self._questions.pop(request_key(envelope.to, envelope.reply_to), None)
|
|
302
|
+
self._end(envelope.to, envelope.reply_to, "failed", envelope.error)
|
|
303
|
+
|
|
304
|
+
case UpdateEnvelope():
|
|
305
|
+
self._add_update(envelope.to, envelope.task_id, envelope.text)
|
|
306
|
+
|
|
307
|
+
case ArtifactEnvelope():
|
|
308
|
+
self._add_chunk(envelope)
|
|
309
|
+
|
|
310
|
+
case CompleteEnvelope():
|
|
311
|
+
self._end(envelope.to, envelope.task_id, "completed")
|
|
312
|
+
|
|
313
|
+
case FailEnvelope():
|
|
314
|
+
self._end(envelope.to, envelope.task_id, "failed", envelope.error)
|
|
315
|
+
|
|
316
|
+
case CancelEnvelope():
|
|
317
|
+
self._end(envelope.sender, envelope.task_id, "canceled")
|
|
318
|
+
|
|
319
|
+
case MessageEnvelope():
|
|
320
|
+
self._ask(envelope)
|
|
321
|
+
|
|
322
|
+
case ReplyEnvelope():
|
|
323
|
+
self._answer(envelope)
|
|
324
|
+
|
|
325
|
+
case (
|
|
326
|
+
CommitEnvelope()
|
|
327
|
+
| BudgetEnvelope()
|
|
328
|
+
| ActionsEnvelope()
|
|
329
|
+
| RecurEnvelope()
|
|
330
|
+
| DisconnectEnvelope()
|
|
331
|
+
| ScheduleStartEnvelope()
|
|
332
|
+
| ScheduleEndEnvelope()
|
|
333
|
+
| ScheduleHandoverEnvelope()
|
|
334
|
+
| FollowUpEnvelope()
|
|
335
|
+
| TriggerFailedEnvelope()
|
|
336
|
+
| ConnectionReadyEnvelope()
|
|
337
|
+
| ConnectionStoredEnvelope()
|
|
338
|
+
| ConnectionRevokedEnvelope()
|
|
339
|
+
):
|
|
340
|
+
return
|
|
341
|
+
|
|
342
|
+
case _:
|
|
343
|
+
assert_never(envelope)
|
|
344
|
+
|
|
345
|
+
def check_steering(self, message: MessageEnvelope) -> None:
|
|
346
|
+
"""Refuses a steering message the network relay would, from the executor of its task or about a task that ended.
|
|
347
|
+
|
|
348
|
+
Raises:
|
|
349
|
+
AgentJamError: If the message steers a task its sender doesn't request, or one that ended.
|
|
350
|
+
"""
|
|
351
|
+
task_id = message.task_id
|
|
352
|
+
if not message.steer or task_id is None:
|
|
353
|
+
return
|
|
354
|
+
|
|
355
|
+
executed = self._tasks.get(request_key(message.to, task_id))
|
|
356
|
+
if executed is not None and executed.executor == message.sender:
|
|
357
|
+
raise AgentJamError(f'Only the requester of task "{task_id}" can steer it')
|
|
358
|
+
|
|
359
|
+
requested = self._tasks.get(request_key(message.sender, task_id))
|
|
360
|
+
if (
|
|
361
|
+
requested is not None
|
|
362
|
+
and requested.executor == message.to
|
|
363
|
+
and not is_open(requested)
|
|
364
|
+
):
|
|
365
|
+
raise AgentJamError(
|
|
366
|
+
f'Task "{task_id}" already ended, so it can\'t be steered. Follow up on it instead'
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
def recap_messaged(self, message: MessageEnvelope) -> TaskRecapRef | None:
|
|
370
|
+
"""Recaps the task a message is about for its recipient, when the recipient executes it."""
|
|
371
|
+
if message.task_id is None:
|
|
372
|
+
return None
|
|
373
|
+
|
|
374
|
+
task = self._tasks.get(request_key(message.sender, message.task_id))
|
|
375
|
+
if task is None or task.executor != message.to:
|
|
376
|
+
return None
|
|
377
|
+
|
|
378
|
+
return self._recap(task)
|
|
379
|
+
|
|
380
|
+
def recall(self, executor: str, ref: TaskRef) -> TaskRecapRef:
|
|
381
|
+
"""Recaps a task for its executor.
|
|
382
|
+
|
|
383
|
+
Raises:
|
|
384
|
+
AgentJamError: If `executor` didn't work on the task.
|
|
385
|
+
"""
|
|
386
|
+
return self._recap(self._find_executed(executor, ref))
|
|
387
|
+
|
|
388
|
+
def recur(self, envelope: RecurEnvelope) -> None:
|
|
389
|
+
"""Records a schedule's run as it's delivered, once for both of its parties."""
|
|
390
|
+
key = request_key(envelope.sender, envelope.task_id)
|
|
391
|
+
if key in self._tasks:
|
|
392
|
+
return
|
|
393
|
+
|
|
394
|
+
self._tasks[key] = RecordedTask(
|
|
395
|
+
executor=envelope.to,
|
|
396
|
+
principal=envelope.principal,
|
|
397
|
+
schedule_id=envelope.schedule_id,
|
|
398
|
+
run=envelope.run,
|
|
399
|
+
recap=construct_recap(
|
|
400
|
+
envelope.sender, envelope.task_id, envelope, "working"
|
|
401
|
+
),
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
def list_history(
|
|
405
|
+
self, executor: str, anchor: TaskRef, query: HistoryQuery
|
|
406
|
+
) -> HistoryPage:
|
|
407
|
+
"""Lists the ended tasks `executor` ran before `anchor` that it may recall, newest first, like the network relay.
|
|
408
|
+
|
|
409
|
+
Those are earlier runs of a run's schedule, or earlier one-offs of a one-off's skill for the same human.
|
|
410
|
+
|
|
411
|
+
Raises:
|
|
412
|
+
AgentJamError: If `executor` didn't work on `anchor`, or `query` is invalid.
|
|
413
|
+
"""
|
|
414
|
+
task = self._find_executed(executor, anchor)
|
|
415
|
+
recorded = list(self._tasks.values())
|
|
416
|
+
earlier = recorded[: recorded.index(task)]
|
|
417
|
+
past = [
|
|
418
|
+
candidate for candidate in reversed(earlier) if is_past_of(task, candidate)
|
|
419
|
+
]
|
|
420
|
+
|
|
421
|
+
return page_history(past, query)
|
|
422
|
+
|
|
423
|
+
def _find_executed(self, executor: str, ref: TaskRef) -> RecordedTask:
|
|
424
|
+
task = self._tasks.get(request_key(ref.requester, ref.task_id))
|
|
425
|
+
if task is None or task.executor != executor:
|
|
426
|
+
raise AgentJamError(
|
|
427
|
+
f'"{executor}" didn\'t work on task "{ref.task_id}" of "{ref.requester}"'
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
return task
|
|
431
|
+
|
|
432
|
+
def _recap(self, task: RecordedTask) -> TaskRecapRef:
|
|
433
|
+
recap = task.recap
|
|
434
|
+
|
|
435
|
+
return recap.model_copy(
|
|
436
|
+
update={
|
|
437
|
+
"updates": recap.updates[-MAX_UPDATES:],
|
|
438
|
+
"artifacts": recap.artifacts[:MAX_ARTIFACTS],
|
|
439
|
+
"history": recap.history[-MAX_HISTORY:],
|
|
440
|
+
},
|
|
441
|
+
deep=True,
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
def _record(self, proposal: ProposeEnvelope) -> None:
|
|
445
|
+
if is_scheduled(proposal) or proposal.prepare:
|
|
446
|
+
return
|
|
447
|
+
|
|
448
|
+
follow_up = construct_follow_up(proposal.previous)
|
|
449
|
+
self._tasks[request_key(proposal.sender, proposal.id)] = RecordedTask(
|
|
450
|
+
executor=proposal.to,
|
|
451
|
+
principal=proposal.principal,
|
|
452
|
+
schedule_id=None,
|
|
453
|
+
run=None,
|
|
454
|
+
recap=construct_recap(
|
|
455
|
+
proposal.sender, proposal.id, proposal, "proposed", follow_up
|
|
456
|
+
),
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
def _find_open(self, requester: str, id: str) -> RecordedTask | None:
|
|
460
|
+
task = self._tasks.get(request_key(requester, id))
|
|
461
|
+
|
|
462
|
+
return task if task is not None and is_open(task) else None
|
|
463
|
+
|
|
464
|
+
def _set_status(self, requester: str, id: str, status: TaskStatus) -> None:
|
|
465
|
+
if task := self._find_open(requester, id):
|
|
466
|
+
task.recap = task.recap.model_copy(update={"status": status})
|
|
467
|
+
|
|
468
|
+
def _end(
|
|
469
|
+
self, requester: str, id: str, status: TaskStatus, error: str | None = None
|
|
470
|
+
) -> None:
|
|
471
|
+
task = self._find_open(requester, id)
|
|
472
|
+
if task is None:
|
|
473
|
+
return
|
|
474
|
+
|
|
475
|
+
ending = {"status": status, "ended_at": get_now()}
|
|
476
|
+
task.recap = task.recap.model_copy(
|
|
477
|
+
update=ending if error is None else {**ending, "error": error}
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
def _add_update(self, requester: str, id: str, text: str) -> None:
|
|
481
|
+
if task := self._tasks.get(request_key(requester, id)):
|
|
482
|
+
task.recap = task.recap.model_copy(
|
|
483
|
+
update={"updates": [*task.recap.updates, text]}
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
def _add_chunk(self, envelope: ArtifactEnvelope) -> None:
|
|
487
|
+
task = self._tasks.get(request_key(envelope.to, envelope.task_id))
|
|
488
|
+
if task is None:
|
|
489
|
+
return
|
|
490
|
+
|
|
491
|
+
chunks = [*task.partials.get(envelope.artifact_id, []), envelope.chunk]
|
|
492
|
+
task.partials[envelope.artifact_id] = chunks
|
|
493
|
+
if not envelope.last:
|
|
494
|
+
return
|
|
495
|
+
|
|
496
|
+
del task.partials[envelope.artifact_id]
|
|
497
|
+
kept = [
|
|
498
|
+
artifact
|
|
499
|
+
for artifact in task.recap.artifacts
|
|
500
|
+
if envelope.key is None or artifact.key != envelope.key
|
|
501
|
+
]
|
|
502
|
+
artifacts = [finish_artifact(envelope, chunks), *kept]
|
|
503
|
+
task.recap = task.recap.model_copy(update={"artifacts": artifacts})
|
|
504
|
+
|
|
505
|
+
def _ask(self, message: MessageEnvelope) -> None:
|
|
506
|
+
if message.task_id is None:
|
|
507
|
+
return
|
|
508
|
+
|
|
509
|
+
task = self._find_shared(message.sender, message.to, message.task_id)
|
|
510
|
+
if task is None:
|
|
511
|
+
return
|
|
512
|
+
|
|
513
|
+
asked = Message(
|
|
514
|
+
id=message.id,
|
|
515
|
+
sender=message.sender,
|
|
516
|
+
to=message.to,
|
|
517
|
+
conversation_id=message.conversation_id,
|
|
518
|
+
text=message.text,
|
|
519
|
+
)
|
|
520
|
+
self._questions[request_key(message.sender, message.id)] = Question(asked, task)
|
|
521
|
+
|
|
522
|
+
def _find_shared(self, first: str, second: str, task_id: str) -> str | None:
|
|
523
|
+
"""Finds the task `task_id` that one of the two agents requested from the other, by its key."""
|
|
524
|
+
for requester, executor in ((first, second), (second, first)):
|
|
525
|
+
task = self._tasks.get(request_key(requester, task_id))
|
|
526
|
+
if task is not None and task.executor == executor:
|
|
527
|
+
return request_key(requester, task_id)
|
|
528
|
+
|
|
529
|
+
return None
|
|
530
|
+
|
|
531
|
+
def _answer(self, reply: ReplyEnvelope) -> None:
|
|
532
|
+
question = self._questions.pop(request_key(reply.to, reply.reply_to), None)
|
|
533
|
+
if question is None:
|
|
534
|
+
return
|
|
535
|
+
|
|
536
|
+
task = self._tasks.get(question.task)
|
|
537
|
+
if task is None:
|
|
538
|
+
return
|
|
539
|
+
|
|
540
|
+
answer = Message(
|
|
541
|
+
id=reply.id,
|
|
542
|
+
sender=reply.sender,
|
|
543
|
+
to=reply.to,
|
|
544
|
+
conversation_id=question.message.conversation_id,
|
|
545
|
+
text=reply.text,
|
|
546
|
+
)
|
|
547
|
+
history = [*task.recap.history, question.message, answer]
|
|
548
|
+
task.recap = task.recap.model_copy(update={"history": history})
|