@agentjam/sdk 0.4.0 → 0.6.0
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.
- package/dist/adapters/ai.d.ts +1 -1
- package/dist/adapters/ai.d.ts.map +1 -1
- package/dist/adapters/anthropic.d.ts +1 -1
- package/dist/adapters/anthropic.d.ts.map +1 -1
- package/dist/adapters/openai.d.ts +1 -1
- package/dist/adapters/openai.d.ts.map +1 -1
- package/dist/adapters/openaiAgents.d.ts +1 -1
- package/dist/adapters/openaiAgents.d.ts.map +1 -1
- package/dist/adapters/shared.d.ts +6 -4
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +43 -18
- package/dist/adapters/shared.js.map +1 -1
- package/dist/agent.d.ts +82 -6
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +201 -34
- package/dist/agent.js.map +1 -1
- package/dist/artifacts.d.ts +19 -15
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js +67 -39
- package/dist/artifacts.js.map +1 -1
- package/dist/attachments.d.ts +30 -18
- package/dist/attachments.d.ts.map +1 -1
- package/dist/attachments.js +57 -30
- package/dist/attachments.js.map +1 -1
- package/dist/backoff.d.ts +15 -0
- package/dist/backoff.d.ts.map +1 -0
- package/dist/backoff.js +10 -0
- package/dist/backoff.js.map +1 -0
- package/dist/content.d.ts +95 -5
- package/dist/content.d.ts.map +1 -1
- package/dist/content.js +137 -2
- package/dist/content.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/files.d.ts +47 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +192 -0
- package/dist/files.js.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +170 -6
- package/dist/protocol.d.ts.map +1 -1
- package/dist/recaps.d.ts +4 -2
- package/dist/recaps.d.ts.map +1 -1
- package/dist/recaps.js +39 -39
- package/dist/recaps.js.map +1 -1
- package/dist/relay/frames.d.ts +55 -2
- package/dist/relay/frames.d.ts.map +1 -1
- package/dist/relay/frames.js +4 -0
- package/dist/relay/frames.js.map +1 -1
- package/dist/relay/links.d.ts +13 -0
- package/dist/relay/links.d.ts.map +1 -0
- package/dist/relay/links.js +31 -0
- package/dist/relay/links.js.map +1 -0
- package/dist/relay/memory.d.ts.map +1 -1
- package/dist/relay/memory.js +21 -10
- package/dist/relay/memory.js.map +1 -1
- package/dist/relay/payments.d.ts +11 -4
- package/dist/relay/payments.d.ts.map +1 -1
- package/dist/relay/payments.js +25 -4
- package/dist/relay/payments.js.map +1 -1
- package/dist/relay/relay.d.ts +49 -7
- package/dist/relay/relay.d.ts.map +1 -1
- package/dist/relay/remote.d.ts.map +1 -1
- package/dist/relay/remote.js +83 -13
- package/dist/relay/remote.js.map +1 -1
- package/dist/relay/schedules.d.ts +2 -1
- package/dist/relay/schedules.d.ts.map +1 -1
- package/dist/relay/schedules.js +13 -3
- package/dist/relay/schedules.js.map +1 -1
- package/dist/relay/uploads.d.ts +28 -6
- package/dist/relay/uploads.d.ts.map +1 -1
- package/dist/relay/uploads.js +262 -40
- package/dist/relay/uploads.js.map +1 -1
- package/dist/screening.js +4 -4
- package/dist/screening.js.map +1 -1
- package/dist/storage.d.ts +31 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +50 -0
- package/dist/storage.js.map +1 -0
- package/dist/tasks/incoming.d.ts +4 -1
- package/dist/tasks/incoming.d.ts.map +1 -1
- package/dist/tasks/incoming.js +24 -5
- package/dist/tasks/incoming.js.map +1 -1
- package/dist/tasks/outgoing.d.ts +2 -0
- package/dist/tasks/outgoing.d.ts.map +1 -1
- package/dist/tasks/outgoing.js +4 -3
- package/dist/tasks/outgoing.js.map +1 -1
- package/dist/tasks/scheduled.d.ts +18 -8
- package/dist/tasks/scheduled.d.ts.map +1 -1
- package/dist/tasks/scheduled.js +77 -20
- package/dist/tasks/scheduled.js.map +1 -1
- package/dist/tasks/triggers.d.ts +11 -3
- package/dist/tasks/triggers.d.ts.map +1 -1
- package/dist/tasks/triggers.js +30 -11
- package/dist/tasks/triggers.js.map +1 -1
- package/dist/tools/delegations.d.ts +17 -0
- package/dist/tools/delegations.d.ts.map +1 -0
- package/dist/tools/delegations.js +67 -0
- package/dist/tools/delegations.js.map +1 -0
- package/dist/tools/inputs.d.ts +62 -0
- package/dist/tools/inputs.d.ts.map +1 -0
- package/dist/tools/inputs.js +143 -0
- package/dist/tools/inputs.js.map +1 -0
- package/dist/tools/network.d.ts +10 -0
- package/dist/tools/network.d.ts.map +1 -0
- package/dist/tools/network.js +435 -0
- package/dist/tools/network.js.map +1 -0
- package/dist/tools/schedules.d.ts +43 -0
- package/dist/tools/schedules.d.ts.map +1 -0
- package/dist/tools/schedules.js +337 -0
- package/dist/tools/schedules.js.map +1 -0
- package/dist/tools/shared.d.ts +152 -0
- package/dist/tools/shared.d.ts.map +1 -0
- package/dist/tools/shared.js +151 -0
- package/dist/tools/shared.js.map +1 -0
- package/dist/tools.d.ts +2 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +5 -5
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +52 -30
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { isAnyOfConnection, leavesOf } from "./anyOf.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ArtifactAssembler } from "./artifacts.js";
|
|
3
|
+
import { assertAttachmentTotal, constructUpload, receiveAttachments, } from "./attachments.js";
|
|
3
4
|
import { bindGrantVault, Readiness, receiveRevocation, unbindGrantVault, } from "./connections.js";
|
|
4
5
|
import { DEFAULT_TIME_ZONE } from "./cron.js";
|
|
5
6
|
import { AgentJamError, AgentNotFoundError, describeError, FollowUpFailedError, TaskFailedError, TaskRejectedError, TriggerFailedError, } from "./errors.js";
|
|
7
|
+
import { defaultMaxBufferedBytes, FileReceiver } from "./files.js";
|
|
6
8
|
import { IdempotencyCache } from "./idempotency.js";
|
|
7
9
|
import { isStoredSchema, parseValue } from "./inputs.js";
|
|
8
10
|
import { receiveOAuthCallback } from "./oauthCallbacks.js";
|
|
@@ -18,7 +20,7 @@ import { TaskHandle } from "./tasks/outgoing.js";
|
|
|
18
20
|
import { ScheduleHandle } from "./tasks/scheduled.js";
|
|
19
21
|
import { TriggerRunner } from "./tasks/triggers.js";
|
|
20
22
|
import { TaskWatches } from "./tasks/watches.js";
|
|
21
|
-
import { createNetworkTools, pickTools } from "./tools.js";
|
|
23
|
+
import { createNetworkTools, pickTools } from "./tools/network.js";
|
|
22
24
|
import { lostGrant, requestersOf } from "./vaults/vault.js";
|
|
23
25
|
import { accept, reject } from "./verdicts.js";
|
|
24
26
|
const defaultTimeout = 30_000;
|
|
@@ -33,6 +35,8 @@ const noDisputes = "This relay doesn't support disputes";
|
|
|
33
35
|
const noTaskList = "This relay doesn't list tasks";
|
|
34
36
|
const noRecall = "This relay doesn't recall tasks";
|
|
35
37
|
const noHistory = "This relay doesn't keep task history";
|
|
38
|
+
const noTasks = "This relay doesn't keep tasks";
|
|
39
|
+
const noSchedules = "This relay doesn't keep schedules";
|
|
36
40
|
const steered = "Passed your message to the running task, which reads it before its next step.";
|
|
37
41
|
const noUploads = "This relay doesn't take attachments";
|
|
38
42
|
const maxAttachments = 10;
|
|
@@ -90,7 +94,8 @@ const watchOf = ({ scheduleId, from, principal, text, skill, trigger, config, in
|
|
|
90
94
|
...(input === undefined ? {} : { input }),
|
|
91
95
|
...(expiresAt === undefined ? {} : { expiresAt: new Date(expiresAt) }),
|
|
92
96
|
});
|
|
93
|
-
const
|
|
97
|
+
const constructOrigin = (requester, taskId) => taskId === undefined ? undefined : { requester, taskId };
|
|
98
|
+
const toProposal = ({ from, principal, text, skill, input, cron, trigger, budget, expiresAt, follows, previous, }, attachments, files) => ({
|
|
94
99
|
from,
|
|
95
100
|
...(principal === undefined ? {} : { principal }),
|
|
96
101
|
text,
|
|
@@ -104,7 +109,9 @@ const toProposal = ({ from, principal, text, skill, input, cron, trigger, budget
|
|
|
104
109
|
...(expiresAt === undefined ? {} : { expiresAt: new Date(expiresAt) }),
|
|
105
110
|
attachments,
|
|
106
111
|
...(follows === undefined ? {} : { follows }),
|
|
107
|
-
...(previous === undefined
|
|
112
|
+
...(previous === undefined
|
|
113
|
+
? {}
|
|
114
|
+
: { previous: recapFromWire(previous, files) }),
|
|
108
115
|
});
|
|
109
116
|
const warnRevoking = (name, { from, skill }, sources) => {
|
|
110
117
|
const providers = Array.from(sources)
|
|
@@ -217,6 +224,7 @@ export class Agent {
|
|
|
217
224
|
handlers;
|
|
218
225
|
screen;
|
|
219
226
|
timeoutMs;
|
|
227
|
+
files;
|
|
220
228
|
forward;
|
|
221
229
|
onError;
|
|
222
230
|
onRun;
|
|
@@ -242,7 +250,7 @@ export class Agent {
|
|
|
242
250
|
performed = new IdempotencyCache(dedupeWindow);
|
|
243
251
|
triggers;
|
|
244
252
|
watches = new TaskWatches((outcomes) => this.resolveWatch(outcomes));
|
|
245
|
-
constructor({ name, apiKey, network, payments, skills = [], networkTools = true, timeoutMs = defaultTimeout, instanceId, forward = {}, onError = console.error, onRun = logRun, onFollowUp = logFollowUp, onActionRequired = warnActions, toolNames = [], screen, ...handlers }, shown = {}) {
|
|
253
|
+
constructor({ name, apiKey, network, payments, skills = [], networkTools = true, timeoutMs = defaultTimeout, instanceId, forward = {}, onError = console.error, onRun = logRun, onFollowUp = logFollowUp, onActionRequired = warnActions, toolNames = [], screen, maxBufferedBytes = defaultMaxBufferedBytes, ...handlers }, shown = {}) {
|
|
246
254
|
this.requestedName = name;
|
|
247
255
|
const bindSecret = (connection) => constructSecretSource(connection, () => this.credentialChannel(connection.provider));
|
|
248
256
|
this.sources = new Map(skills.map((skill) => [skill.name, sourcesOf(skill, bindSecret)]));
|
|
@@ -275,6 +283,7 @@ export class Agent {
|
|
|
275
283
|
this.handlers = handlers;
|
|
276
284
|
this.screen = screen;
|
|
277
285
|
this.timeoutMs = timeoutMs;
|
|
286
|
+
this.files = new FileReceiver(maxBufferedBytes, (file) => this.linkFile(file));
|
|
278
287
|
this.onError = onError;
|
|
279
288
|
this.onRun = onRun;
|
|
280
289
|
this.onFollowUp = onFollowUp;
|
|
@@ -414,7 +423,11 @@ export class Agent {
|
|
|
414
423
|
files: response.attachments,
|
|
415
424
|
};
|
|
416
425
|
});
|
|
417
|
-
|
|
426
|
+
const origin = constructOrigin(asked ? to : this.name, taskId);
|
|
427
|
+
return {
|
|
428
|
+
...reply,
|
|
429
|
+
attachments: receiveAttachments(files, this.files, origin),
|
|
430
|
+
};
|
|
418
431
|
}
|
|
419
432
|
/**
|
|
420
433
|
* Proposes a task to another agent and resolves once it accepts. Aborting `signal` withdraws
|
|
@@ -508,7 +521,7 @@ export class Agent {
|
|
|
508
521
|
};
|
|
509
522
|
const id = key(to, idempotencyKey);
|
|
510
523
|
this.scheduling.add(id);
|
|
511
|
-
|
|
524
|
+
const schedule = await this.request(draft, options, (response) => {
|
|
512
525
|
if (response.type === "task.reject") {
|
|
513
526
|
this.scheduling.delete(id);
|
|
514
527
|
throw new TaskRejectedError(to, response.reason);
|
|
@@ -521,6 +534,52 @@ export class Agent {
|
|
|
521
534
|
this.require(to, response);
|
|
522
535
|
return this.follow(to, idempotencyKey, actionsOf(response), parent);
|
|
523
536
|
});
|
|
537
|
+
// WARNING: Runs can arrive before the caller gets the handle, so they're held until its code after `schedule()`
|
|
538
|
+
// has had the chance to start reading them.
|
|
539
|
+
setTimeout(() => schedule.stopHolding(), 0);
|
|
540
|
+
return schedule;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Ends a schedule this agent proposed or runs so no more runs start, leaving a run in progress to finish. Unlike
|
|
544
|
+
* `Schedule.end()`, it needs no handle, so it also ends schedules from before a restart, such as the one a run names.
|
|
545
|
+
*
|
|
546
|
+
* @param to The address of the other party: the agent running the schedule, or its requester when this agent runs it.
|
|
547
|
+
* @param scheduleId The schedule's `id`, which each of its runs carries as `scheduleId`.
|
|
548
|
+
* @param reason Why the schedule is ending, shown to the other party.
|
|
549
|
+
* @returns Resolves once the ending has been sent.
|
|
550
|
+
*/
|
|
551
|
+
async endSchedule(to, scheduleId, reason) {
|
|
552
|
+
const schedule = this.schedules.get(key(to, scheduleId));
|
|
553
|
+
if (schedule)
|
|
554
|
+
return schedule.end(reason);
|
|
555
|
+
await this.postTo(to)({
|
|
556
|
+
type: "schedule.end",
|
|
557
|
+
scheduleId,
|
|
558
|
+
...(reason === undefined ? {} : { reason }),
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Cancels a task this agent delegated that's still running, such as a schedule's run. Unlike
|
|
563
|
+
* `OutgoingTask.cancel()`, it needs no handle, so it also cancels tasks from before a restart.
|
|
564
|
+
*
|
|
565
|
+
* @param taskId The `id` of the delegated task.
|
|
566
|
+
* @param reason Why the task is no longer needed, shown to the executor.
|
|
567
|
+
* @returns Resolves once the cancellation has been sent, or at once if the task already ended.
|
|
568
|
+
* @throws {TaskCommittedError} If the task's agent committed to an action it can't undo, or sent part of a flat-priced result.
|
|
569
|
+
* @throws {AgentJamError} If the relay doesn't keep tasks, or this agent delegated no such task.
|
|
570
|
+
*/
|
|
571
|
+
async cancelTask(taskId, reason) {
|
|
572
|
+
const task = this.findOutgoing(taskId);
|
|
573
|
+
if (task)
|
|
574
|
+
return task.cancel(reason);
|
|
575
|
+
const { executor, status } = await this.inspectTask(taskId);
|
|
576
|
+
if (!openStatuses.includes(status))
|
|
577
|
+
return;
|
|
578
|
+
await this.postTo(executor)({
|
|
579
|
+
type: "task.cancel",
|
|
580
|
+
taskId,
|
|
581
|
+
...(reason === undefined ? {} : { reason }),
|
|
582
|
+
});
|
|
524
583
|
}
|
|
525
584
|
/**
|
|
526
585
|
* Rates the agent that performed a task this agent delegated. A task can be rated once, after it
|
|
@@ -596,7 +655,76 @@ export class Agent {
|
|
|
596
655
|
const connection = await this.connection;
|
|
597
656
|
if (!connection.recall)
|
|
598
657
|
throw new AgentJamError(noRecall);
|
|
599
|
-
return recapFromWire(await connection.recall(task));
|
|
658
|
+
return recapFromWire(await connection.recall(task), this.files);
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Reports a task this agent delegated as the relay keeps it, with its log, subtasks and unanswered
|
|
662
|
+
* questions. Once an ended task that isn't a schedule's run is reported, it no longer counts as unread.
|
|
663
|
+
*
|
|
664
|
+
* @param taskId The `id` of the delegated task.
|
|
665
|
+
* @returns The task, with its log so far.
|
|
666
|
+
* @throws {AgentJamError} If the relay doesn't keep tasks, or this agent delegated no such task.
|
|
667
|
+
*/
|
|
668
|
+
async inspectTask(taskId) {
|
|
669
|
+
const connection = await this.connection;
|
|
670
|
+
if (!connection.inspectTask)
|
|
671
|
+
throw new AgentJamError(noTasks);
|
|
672
|
+
return connection.inspectTask(taskId);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Reports a schedule this agent requested, with up to 20 of the runs that finished since it last
|
|
676
|
+
* checked, which then count as checked.
|
|
677
|
+
*
|
|
678
|
+
* @param scheduleId The `id` of the schedule.
|
|
679
|
+
* @returns The schedule and its newly finished runs.
|
|
680
|
+
* @throws {AgentJamError} If the relay doesn't keep schedules, or this agent requested no such schedule.
|
|
681
|
+
*/
|
|
682
|
+
async inspectSchedule(scheduleId) {
|
|
683
|
+
const connection = await this.connection;
|
|
684
|
+
if (!connection.inspectSchedule)
|
|
685
|
+
throw new AgentJamError(noSchedules);
|
|
686
|
+
return connection.inspectSchedule(scheduleId);
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Lists the schedules this agent requested, with how many of each one's finished runs it hasn't checked.
|
|
690
|
+
*
|
|
691
|
+
* @returns The schedules.
|
|
692
|
+
* @throws {AgentJamError} If the relay doesn't keep schedules.
|
|
693
|
+
*/
|
|
694
|
+
async listSchedules() {
|
|
695
|
+
const connection = await this.connection;
|
|
696
|
+
if (!connection.listSchedules)
|
|
697
|
+
throw new AgentJamError(noSchedules);
|
|
698
|
+
return connection.listSchedules();
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Lists the runs of a schedule this agent requested, newest first, without marking them as checked.
|
|
702
|
+
*
|
|
703
|
+
* @param scheduleId The `id` of the schedule.
|
|
704
|
+
* @param query The page size, and the `next` cursor of an earlier page to continue from.
|
|
705
|
+
* @returns A page of runs, with `next` set when older ones remain.
|
|
706
|
+
* @throws {AgentJamError} If the relay doesn't keep schedules, this agent requested no such schedule, or `query` is invalid.
|
|
707
|
+
*/
|
|
708
|
+
async listRuns(scheduleId, query = {}) {
|
|
709
|
+
const connection = await this.connection;
|
|
710
|
+
if (!connection.listRuns)
|
|
711
|
+
throw new AgentJamError(noSchedules);
|
|
712
|
+
return connection.listRuns(scheduleId, query);
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Replaces the follow-up of a schedule this agent requested, or removes it. A follow-up that needs
|
|
716
|
+
* actions first waits on a preparation task before it replaces the old one.
|
|
717
|
+
*
|
|
718
|
+
* @param scheduleId The `id` of the schedule.
|
|
719
|
+
* @param then The new follow-up, or null to remove it.
|
|
720
|
+
* @returns The changed schedule, and the preparation the new follow-up waits on.
|
|
721
|
+
* @throws {AgentJamError} If the relay doesn't keep schedules, this agent requested no such schedule, or the relay refuses the follow-up.
|
|
722
|
+
*/
|
|
723
|
+
async updateFollowUp(scheduleId, then) {
|
|
724
|
+
const connection = await this.connection;
|
|
725
|
+
if (!connection.updateFollowUp)
|
|
726
|
+
throw new AgentJamError(noSchedules);
|
|
727
|
+
return connection.updateFollowUp(scheduleId, then);
|
|
600
728
|
}
|
|
601
729
|
/**
|
|
602
730
|
* Buys AgentJam credits for this agent's owner, paying over MPP with its `payments`. Cards buy at
|
|
@@ -887,23 +1015,21 @@ export class Agent {
|
|
|
887
1015
|
}
|
|
888
1016
|
}
|
|
889
1017
|
async computeReply(envelope) {
|
|
890
|
-
const
|
|
1018
|
+
const origin = constructOrigin(envelope.task ? envelope.from : this.name, envelope.taskId);
|
|
1019
|
+
const attachments = receiveAttachments(envelope.attachments, this.files, origin);
|
|
891
1020
|
const asked = this.findAsked(envelope);
|
|
892
1021
|
if (asked) {
|
|
893
1022
|
const reply = await asked.send(`${envelope.from}: ${envelope.text}`, {
|
|
894
|
-
attachments
|
|
1023
|
+
attachments,
|
|
895
1024
|
});
|
|
896
|
-
return {
|
|
897
|
-
text: reply.text,
|
|
898
|
-
attachments: reply.attachments.map(({ file }) => file),
|
|
899
|
-
};
|
|
1025
|
+
return { text: reply.text, attachments: reply.attachments };
|
|
900
1026
|
}
|
|
901
1027
|
const { history, taskId, task, steer } = envelope;
|
|
902
1028
|
const message = {
|
|
903
1029
|
...toMessage(envelope),
|
|
904
1030
|
history,
|
|
905
1031
|
...(taskId === undefined ? {} : { taskId }),
|
|
906
|
-
...(task === undefined ? {} : { task: recapFromWire(task) }),
|
|
1032
|
+
...(task === undefined ? {} : { task: recapFromWire(task, this.files) }),
|
|
907
1033
|
...(steer === undefined ? {} : { steer }),
|
|
908
1034
|
attachments,
|
|
909
1035
|
};
|
|
@@ -960,7 +1086,7 @@ export class Agent {
|
|
|
960
1086
|
input,
|
|
961
1087
|
parsedInput,
|
|
962
1088
|
attachments,
|
|
963
|
-
previous: proposal.previous && recapFromWire(proposal.previous),
|
|
1089
|
+
previous: proposal.previous && recapFromWire(proposal.previous, this.files),
|
|
964
1090
|
price: proposal.prepare ? undefined : this.priceOf(skill, from),
|
|
965
1091
|
card: proposal.prepare ? undefined : this.findSkillCard(skill),
|
|
966
1092
|
isSelf: from === this.name,
|
|
@@ -1025,7 +1151,10 @@ export class Agent {
|
|
|
1025
1151
|
attachments: [],
|
|
1026
1152
|
};
|
|
1027
1153
|
}
|
|
1028
|
-
const attachments =
|
|
1154
|
+
const attachments = receiveAttachments(proposal.attachments, this.files, {
|
|
1155
|
+
requester: proposal.from,
|
|
1156
|
+
taskId: proposal.id,
|
|
1157
|
+
});
|
|
1029
1158
|
const { verdict, preparations } = await this.constructVerdict(proposal, attachments);
|
|
1030
1159
|
return {
|
|
1031
1160
|
verdict: { ...response, ...verdict },
|
|
@@ -1043,7 +1172,7 @@ export class Agent {
|
|
|
1043
1172
|
}
|
|
1044
1173
|
async constructVerdict(proposal, attachments) {
|
|
1045
1174
|
const { id, from, principal, skill } = proposal;
|
|
1046
|
-
const incoming = toProposal(proposal, attachments);
|
|
1175
|
+
const incoming = toProposal(proposal, attachments, this.files);
|
|
1047
1176
|
const verdict = proposal.prepare ? accept() : await this.decide(incoming);
|
|
1048
1177
|
if (!verdict.accepted) {
|
|
1049
1178
|
const rejection = {
|
|
@@ -1181,13 +1310,17 @@ export class Agent {
|
|
|
1181
1310
|
async restartTriggers(connection) {
|
|
1182
1311
|
if (!this.triggers.declared)
|
|
1183
1312
|
return;
|
|
1184
|
-
await this.triggers.sync(async () => (await connection).triggers?.());
|
|
1313
|
+
const ended = await this.triggers.sync(async () => (await connection).triggers?.(this.triggers.listWatched()));
|
|
1314
|
+
// WARNING: awaiting these stops would deadlock the first connect, since their posts wait for it to finish.
|
|
1315
|
+
for (const { requester, scheduleId } of ended) {
|
|
1316
|
+
void this.stopSchedule(requester, scheduleId).catch(this.onError);
|
|
1317
|
+
}
|
|
1185
1318
|
}
|
|
1186
1319
|
perform(recur) {
|
|
1187
1320
|
const { from, principal, scheduleId, run, taskId, text, skill, input } = recur;
|
|
1188
1321
|
const id = key(from, taskId);
|
|
1189
1322
|
const { response } = this.performed.remember(id, async () => {
|
|
1190
|
-
const { attachments, parsedInput, failure } = await this.receiveTask(recur);
|
|
1323
|
+
const { attachments, parsedInput, failure } = await this.receiveTask(recur, { requester: from, taskId });
|
|
1191
1324
|
const task = new TaskExecution({
|
|
1192
1325
|
id: taskId,
|
|
1193
1326
|
from,
|
|
@@ -1239,7 +1372,10 @@ export class Agent {
|
|
|
1239
1372
|
const { attempt = 2, children = [] } = proposal;
|
|
1240
1373
|
const id = key(from, taskId);
|
|
1241
1374
|
const grant = { requester: from, id: taskId };
|
|
1242
|
-
const { attachments, parsedInput, failure } = await this.receiveTask(proposal
|
|
1375
|
+
const { attachments, parsedInput, failure } = await this.receiveTask(proposal, {
|
|
1376
|
+
requester: proposal.from,
|
|
1377
|
+
taskId: proposal.id,
|
|
1378
|
+
});
|
|
1243
1379
|
const running = this.incoming.get(id);
|
|
1244
1380
|
if (running && running.attempt >= attempt)
|
|
1245
1381
|
return;
|
|
@@ -1280,9 +1416,9 @@ export class Agent {
|
|
|
1280
1416
|
return this.handlers.onTask?.(incoming, this);
|
|
1281
1417
|
});
|
|
1282
1418
|
}
|
|
1283
|
-
/**
|
|
1284
|
-
async receiveTask({ skill, input, attachments,
|
|
1285
|
-
const received =
|
|
1419
|
+
/** Receives a task's attachments and parses its input, keeping the first failure for the task's run to throw. */
|
|
1420
|
+
async receiveTask({ skill, input, attachments }, origin) {
|
|
1421
|
+
const received = this.receiveTaskAttachments(attachments, origin);
|
|
1286
1422
|
const parsed = await this.parseSkillInput(skill, input).catch((error) => ({ problem: describeError(error) }));
|
|
1287
1423
|
if (parsed && "problem" in parsed) {
|
|
1288
1424
|
const error = new AgentJamError(parsed.problem);
|
|
@@ -1294,6 +1430,15 @@ export class Agent {
|
|
|
1294
1430
|
}
|
|
1295
1431
|
return { ...received, parsedInput: parsed };
|
|
1296
1432
|
}
|
|
1433
|
+
receiveTaskAttachments(refs, origin) {
|
|
1434
|
+
try {
|
|
1435
|
+
const attachments = receiveAttachments(refs, this.files, origin);
|
|
1436
|
+
return { attachments, failure: undefined };
|
|
1437
|
+
}
|
|
1438
|
+
catch (error) {
|
|
1439
|
+
return { attachments: [], failure: { error } };
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1297
1442
|
/** Forgets a finished task and releases its connections, unless a resumed attempt replaced it meanwhile. */
|
|
1298
1443
|
releaseExecution(id, task, grant) {
|
|
1299
1444
|
if (this.incoming.get(id) !== task)
|
|
@@ -1641,6 +1786,9 @@ export class Agent {
|
|
|
1641
1786
|
listDelegated() {
|
|
1642
1787
|
return Array.from(this.outgoing.values()).filter((task) => !isRun(task));
|
|
1643
1788
|
}
|
|
1789
|
+
findOutgoing(taskId) {
|
|
1790
|
+
return Array.from(this.outgoing.values()).find(({ id }) => id === taskId);
|
|
1791
|
+
}
|
|
1644
1792
|
async reattach(task) {
|
|
1645
1793
|
try {
|
|
1646
1794
|
await this.postTo(task.to)({ type: "task.reattach", taskId: task.id });
|
|
@@ -1684,7 +1832,9 @@ export class Agent {
|
|
|
1684
1832
|
const id = key(to, scheduleId);
|
|
1685
1833
|
const schedule = new ScheduleHandle(scheduleId, to, {
|
|
1686
1834
|
post: this.postTo(to),
|
|
1687
|
-
|
|
1835
|
+
collect: (run, handled) => this.collectAfter(run, scheduleId, handled),
|
|
1836
|
+
handRun: (run) => this.handRun(run, scheduleId),
|
|
1837
|
+
handFollowUp: (task) => this.handFollowUp(task),
|
|
1688
1838
|
release: () => this.schedules.delete(id),
|
|
1689
1839
|
}, actions, parent);
|
|
1690
1840
|
this.schedules.set(id, schedule);
|
|
@@ -1720,13 +1870,10 @@ export class Agent {
|
|
|
1720
1870
|
run: { scheduleId, event: eventOf(notice) },
|
|
1721
1871
|
...(schedule?.parent && { parent: schedule.parent }),
|
|
1722
1872
|
});
|
|
1723
|
-
if (schedule)
|
|
1873
|
+
if (schedule)
|
|
1724
1874
|
schedule.add(run);
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
const handled = Promise.resolve(this.onRun(run, this)).catch(this.onError);
|
|
1728
|
-
this.collectAfter(run, scheduleId, handled);
|
|
1729
|
-
}
|
|
1875
|
+
else
|
|
1876
|
+
this.handRun(run, scheduleId);
|
|
1730
1877
|
return run;
|
|
1731
1878
|
}
|
|
1732
1879
|
followUp(notice) {
|
|
@@ -1738,6 +1885,13 @@ export class Agent {
|
|
|
1738
1885
|
const schedule = [...this.schedules.values()].find(({ id }) => id === scheduleId);
|
|
1739
1886
|
if (schedule)
|
|
1740
1887
|
return schedule.addFollowUp(task);
|
|
1888
|
+
this.handFollowUp(task);
|
|
1889
|
+
}
|
|
1890
|
+
handRun(run, scheduleId) {
|
|
1891
|
+
const handled = Promise.resolve(this.onRun(run, this)).catch(this.onError);
|
|
1892
|
+
this.collectAfter(run, scheduleId, handled);
|
|
1893
|
+
}
|
|
1894
|
+
handFollowUp(task) {
|
|
1741
1895
|
Promise.resolve(this.onFollowUp(task, this)).catch(this.onError);
|
|
1742
1896
|
}
|
|
1743
1897
|
collectAfter(run, scheduleId, handled) {
|
|
@@ -1763,11 +1917,22 @@ export class Agent {
|
|
|
1763
1917
|
this.outgoing.delete(id);
|
|
1764
1918
|
signal?.removeEventListener("abort", abort);
|
|
1765
1919
|
},
|
|
1920
|
+
constructAssembler: () => new ArtifactAssembler(this.files, {
|
|
1921
|
+
requester: this.name,
|
|
1922
|
+
taskId,
|
|
1923
|
+
}),
|
|
1766
1924
|
}, { ...details, actions });
|
|
1767
1925
|
this.outgoing.set(id, handle);
|
|
1768
1926
|
signal?.addEventListener("abort", abort, { once: true });
|
|
1769
1927
|
return handle;
|
|
1770
1928
|
}
|
|
1929
|
+
async linkFile(file) {
|
|
1930
|
+
const connection = await this.connection;
|
|
1931
|
+
if (!connection.linkFile) {
|
|
1932
|
+
throw new AgentJamError(`The relay can't renew the link to ${file.kind} ${file.id}`);
|
|
1933
|
+
}
|
|
1934
|
+
return connection.linkFile(file);
|
|
1935
|
+
}
|
|
1771
1936
|
request(draft, { signal, timeoutMs = this.timeoutMs }, adopt) {
|
|
1772
1937
|
const response = this.requests.track(draft, { signal, timeoutMs }, (reply) => {
|
|
1773
1938
|
if (reply.type === "error")
|
|
@@ -1791,9 +1956,9 @@ export class Agent {
|
|
|
1791
1956
|
return connection.history(anchor, query);
|
|
1792
1957
|
};
|
|
1793
1958
|
}
|
|
1794
|
-
uploadArtifact = async (
|
|
1959
|
+
uploadArtifact = async (source, signal) => {
|
|
1795
1960
|
const connection = await this.connection;
|
|
1796
|
-
return connection.upload?.(
|
|
1961
|
+
return connection.upload?.(source, signal);
|
|
1797
1962
|
};
|
|
1798
1963
|
findIncomingTask(requester, taskId) {
|
|
1799
1964
|
if (taskId === undefined)
|
|
@@ -1829,8 +1994,10 @@ export class Agent {
|
|
|
1829
1994
|
const connection = await this.connection;
|
|
1830
1995
|
if (!connection.upload)
|
|
1831
1996
|
throw new AgentJamError(noUploads);
|
|
1997
|
+
const uploads = await Promise.all(sources.map(constructUpload));
|
|
1998
|
+
assertAttachmentTotal(uploads);
|
|
1832
1999
|
const upload = connection.upload.bind(connection);
|
|
1833
|
-
const ids = await Promise.all(
|
|
2000
|
+
const ids = await Promise.all(uploads.map((source) => upload(source, signal)));
|
|
1834
2001
|
return ids.map((attachmentId) => ({ attachmentId }));
|
|
1835
2002
|
}
|
|
1836
2003
|
}
|