@agentjam/sdk 0.9.0 → 0.11.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/README.md +2 -1
- package/dist/agent.d.ts +49 -15
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +211 -70
- package/dist/agent.js.map +1 -1
- package/dist/anyOf.js +2 -2
- package/dist/anyOf.js.map +1 -1
- package/dist/artifacts.d.ts +8 -9
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js +40 -76
- package/dist/artifacts.js.map +1 -1
- package/dist/attachments.d.ts +30 -5
- package/dist/attachments.d.ts.map +1 -1
- package/dist/attachments.js +53 -8
- package/dist/attachments.js.map +1 -1
- package/dist/connections.d.ts +0 -1
- package/dist/connections.d.ts.map +1 -1
- package/dist/connections.js +0 -1
- package/dist/connections.js.map +1 -1
- package/dist/content.d.ts +3 -9
- package/dist/content.d.ts.map +1 -1
- package/dist/content.js +0 -9
- package/dist/content.js.map +1 -1
- package/dist/definitions.d.ts +20 -0
- package/dist/definitions.d.ts.map +1 -0
- package/dist/definitions.js +28 -0
- package/dist/definitions.js.map +1 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -1
- package/dist/files.d.ts +16 -4
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +26 -11
- package/dist/files.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/inputs.d.ts +14 -0
- package/dist/inputs.d.ts.map +1 -1
- package/dist/inputs.js +59 -12
- package/dist/inputs.js.map +1 -1
- package/dist/managedWebhooks.d.ts.map +1 -1
- package/dist/managedWebhooks.js +2 -0
- package/dist/managedWebhooks.js.map +1 -1
- package/dist/oauthCallbacks.js +1 -1
- package/dist/oauthCallbacks.js.map +1 -1
- package/dist/platforms/datadog.d.ts +2 -2
- package/dist/platforms/datadog.d.ts.map +1 -1
- package/dist/platforms/discord.d.ts +59 -54
- package/dist/platforms/discord.d.ts.map +1 -1
- package/dist/platforms/discord.js +4 -0
- package/dist/platforms/discord.js.map +1 -1
- package/dist/platforms/discordEvents.d.ts.map +1 -1
- package/dist/platforms/discordEvents.js +2 -0
- package/dist/platforms/discordEvents.js.map +1 -1
- package/dist/platforms/discordInteractions.d.ts.map +1 -1
- package/dist/platforms/discordInteractions.js +2 -0
- package/dist/platforms/discordInteractions.js.map +1 -1
- package/dist/platforms/github.d.ts +37 -34
- package/dist/platforms/github.d.ts.map +1 -1
- package/dist/platforms/github.js +4 -0
- package/dist/platforms/github.js.map +1 -1
- package/dist/platforms/githubEvents.d.ts.map +1 -1
- package/dist/platforms/githubEvents.js +2 -0
- package/dist/platforms/githubEvents.js.map +1 -1
- package/dist/platforms/grafana.d.ts +2 -2
- package/dist/platforms/grafana.d.ts.map +1 -1
- package/dist/platforms/microsoft.d.ts +6 -3
- package/dist/platforms/microsoft.d.ts.map +1 -1
- package/dist/platforms/microsoft.js +4 -0
- package/dist/platforms/microsoft.js.map +1 -1
- package/dist/platforms/pagerduty.d.ts +2 -2
- package/dist/platforms/pagerduty.d.ts.map +1 -1
- package/dist/platforms/sentry.d.ts +8 -7
- package/dist/platforms/sentry.d.ts.map +1 -1
- package/dist/platforms/sentry.js +2 -0
- package/dist/platforms/sentry.js.map +1 -1
- package/dist/platforms/sentryEvents.d.ts.map +1 -1
- package/dist/platforms/sentryEvents.js +2 -0
- package/dist/platforms/sentryEvents.js.map +1 -1
- package/dist/platforms/slack.d.ts +54 -54
- package/dist/platforms/slack.d.ts.map +1 -1
- package/dist/platforms/slackEvents.d.ts.map +1 -1
- package/dist/platforms/slackEvents.js +2 -0
- package/dist/platforms/slackEvents.js.map +1 -1
- package/dist/platforms/slackInteractions.d.ts.map +1 -1
- package/dist/platforms/slackInteractions.js +2 -0
- package/dist/platforms/slackInteractions.js.map +1 -1
- package/dist/principals.d.ts +9 -0
- package/dist/principals.d.ts.map +1 -0
- package/dist/principals.js +9 -0
- package/dist/principals.js.map +1 -0
- package/dist/profile.d.ts +5 -5
- package/dist/profile.d.ts.map +1 -1
- package/dist/profile.js +9 -5
- package/dist/profile.js.map +1 -1
- package/dist/protocol.d.ts +20 -5
- package/dist/protocol.d.ts.map +1 -1
- package/dist/recaps.d.ts +12 -2
- package/dist/recaps.d.ts.map +1 -1
- package/dist/recaps.js +12 -1
- package/dist/recaps.js.map +1 -1
- package/dist/relay/chains.d.ts +6 -1
- package/dist/relay/chains.d.ts.map +1 -1
- package/dist/relay/chains.js +16 -1
- package/dist/relay/chains.js.map +1 -1
- package/dist/relay/frames.d.ts +3 -2
- package/dist/relay/frames.d.ts.map +1 -1
- package/dist/relay/frames.js +8 -2
- package/dist/relay/frames.js.map +1 -1
- package/dist/relay/history.d.ts +45 -0
- package/dist/relay/history.d.ts.map +1 -0
- package/dist/relay/history.js +235 -0
- package/dist/relay/history.js.map +1 -0
- package/dist/relay/memory.d.ts +34 -10
- package/dist/relay/memory.d.ts.map +1 -1
- package/dist/relay/memory.js +182 -36
- package/dist/relay/memory.js.map +1 -1
- package/dist/relay/relay.d.ts +2 -0
- package/dist/relay/relay.d.ts.map +1 -1
- package/dist/relay/remote.d.ts +1 -1
- package/dist/relay/remote.d.ts.map +1 -1
- package/dist/relay/remote.js +4 -2
- package/dist/relay/remote.js.map +1 -1
- package/dist/relay/resumes.d.ts +40 -0
- package/dist/relay/resumes.d.ts.map +1 -0
- package/dist/relay/resumes.js +75 -0
- package/dist/relay/resumes.js.map +1 -0
- package/dist/relay/schedules.d.ts.map +1 -1
- package/dist/relay/schedules.js +3 -2
- package/dist/relay/schedules.js.map +1 -1
- package/dist/relay/sessions.d.ts +14 -4
- package/dist/relay/sessions.d.ts.map +1 -1
- package/dist/relay/sessions.js +30 -8
- package/dist/relay/sessions.js.map +1 -1
- package/dist/skillChecks.d.ts +59 -0
- package/dist/skillChecks.d.ts.map +1 -0
- package/dist/skillChecks.js +174 -0
- package/dist/skillChecks.js.map +1 -0
- package/dist/tasks/incoming.d.ts +78 -34
- package/dist/tasks/incoming.d.ts.map +1 -1
- package/dist/tasks/incoming.js +75 -32
- package/dist/tasks/incoming.js.map +1 -1
- package/dist/tasks/triggers.d.ts +2 -2
- package/dist/tasks/triggers.d.ts.map +1 -1
- package/dist/tasks/triggers.js +11 -1
- package/dist/tasks/triggers.js.map +1 -1
- package/dist/tools/network.js +1 -1
- package/dist/tools/network.js.map +1 -1
- package/dist/tools/shared.d.ts +3 -3
- package/dist/types.d.ts +225 -60
- package/dist/types.d.ts.map +1 -1
- package/dist/verdicts.d.ts +4 -2
- package/dist/verdicts.d.ts.map +1 -1
- package/dist/verdicts.js +4 -2
- package/dist/verdicts.js.map +1 -1
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { isAnyOfConnection, leavesOf } from "./anyOf.js";
|
|
2
2
|
import { ArtifactAssembler } from "./artifacts.js";
|
|
3
|
-
import { assertAttachmentTotal,
|
|
3
|
+
import { assertAttachmentTotal, constructAttachmentEntry, ForwardedFile, receiveAttachments, } from "./attachments.js";
|
|
4
4
|
import { bindGrantVault, Readiness, receiveRevocation, unbindGrantVault, } from "./connections.js";
|
|
5
5
|
import { DEFAULT_TIME_ZONE } from "./cron.js";
|
|
6
|
-
import { AgentJamError, AgentNotFoundError, describeError, FollowUpFailedError, TaskFailedError, TaskRejectedError, TriggerFailedError, } from "./errors.js";
|
|
6
|
+
import { AgentDeclinedError, AgentJamError, AgentNotFoundError, describeError, FollowUpFailedError, MessagesNotAcceptedError, TaskFailedError, TaskRejectedError, TriggerFailedError, } from "./errors.js";
|
|
7
7
|
import { defaultMaxBufferedBytes, FileReceiver } from "./files.js";
|
|
8
8
|
import { IdempotencyCache } from "./idempotency.js";
|
|
9
|
-
import { isStoredSchema, parseValue } from "./inputs.js";
|
|
9
|
+
import { isInputSchema, isStoredSchema, parseTriggerValue, parseValue, } from "./inputs.js";
|
|
10
10
|
import { receiveOAuthCallback } from "./oauthCallbacks.js";
|
|
11
|
-
import {
|
|
11
|
+
import { resolveChainPrincipal } from "./principals.js";
|
|
12
|
+
import { listRecappedFiles, recapFromWire } from "./recaps.js";
|
|
12
13
|
import { ReassignedError } from "./relay/frames.js";
|
|
13
14
|
import { AgentJamRelay } from "./relay/remote.js";
|
|
14
15
|
import { PendingRequests } from "./requests.js";
|
|
15
16
|
import { screenProposal } from "./screening.js";
|
|
16
17
|
import { constructSecretSource, isSecretConnection, isSecretSource, } from "./secrets.js";
|
|
18
|
+
import { describeMissingSkill, findProposalProblem, listVisibleSkills, } from "./skillChecks.js";
|
|
17
19
|
import { constructSubscriptionId } from "./subscriptions.js";
|
|
18
20
|
import { AccessWaits } from "./tasks/access.js";
|
|
19
21
|
import { currentTask, TaskExecution } from "./tasks/incoming.js";
|
|
@@ -40,6 +42,7 @@ const noTasks = "This relay doesn't keep tasks";
|
|
|
40
42
|
const noSchedules = "This relay doesn't keep schedules";
|
|
41
43
|
const steered = "Passed your message to the running task, which reads it before its next step.";
|
|
42
44
|
const noUploads = "This relay doesn't take attachments";
|
|
45
|
+
const noFileArtifacts = "This relay doesn't store files, so tasks can't deliver them";
|
|
43
46
|
const maxAttachments = 10;
|
|
44
47
|
const untaskedAttachments = "Only a message about a task, or a reply to one, can carry attachments";
|
|
45
48
|
const untaskedSteer = "Only a message about a task can steer it";
|
|
@@ -87,7 +90,7 @@ const eventOf = ({ eventId, summary = "", eventData, }) => eventId === undefined
|
|
|
87
90
|
const watchOf = ({ scheduleId, from, principal, text, skill, trigger, config, input, expiresAt, }) => ({
|
|
88
91
|
id: scheduleId,
|
|
89
92
|
from,
|
|
90
|
-
|
|
93
|
+
principal: resolveChainPrincipal(principal, from),
|
|
91
94
|
text,
|
|
92
95
|
skill,
|
|
93
96
|
trigger,
|
|
@@ -96,16 +99,21 @@ const watchOf = ({ scheduleId, from, principal, text, skill, trigger, config, in
|
|
|
96
99
|
...(expiresAt === undefined ? {} : { expiresAt: new Date(expiresAt) }),
|
|
97
100
|
});
|
|
98
101
|
const constructOrigin = (requester, taskId) => taskId === undefined ? undefined : { requester, taskId };
|
|
99
|
-
const toProposal = ({ from, principal, text, skill, input, cron, trigger, budget, expiresAt, follows, previous, subscribe, }, attachments, files) => ({
|
|
102
|
+
const toProposal = ({ from, principal, text, skill, input, cron, trigger, budget, expiresAt, follows, previous, subscribe, }, attachments, files, parsed) => ({
|
|
100
103
|
from,
|
|
101
|
-
|
|
104
|
+
principal: resolveChainPrincipal(principal, from),
|
|
102
105
|
text,
|
|
103
106
|
...(skill === undefined ? {} : { skill }),
|
|
104
|
-
|
|
107
|
+
input: parsed.input ? parsed.input.value : input,
|
|
105
108
|
...(cron === undefined
|
|
106
109
|
? {}
|
|
107
110
|
: { cron: { ...cron, timeZone: cron.timeZone ?? DEFAULT_TIME_ZONE } }),
|
|
108
|
-
...(trigger
|
|
111
|
+
...(trigger && {
|
|
112
|
+
trigger: {
|
|
113
|
+
name: trigger.name,
|
|
114
|
+
config: parsed.config ? parsed.config.value : trigger.config,
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
109
117
|
...(budget === undefined ? {} : { budget }),
|
|
110
118
|
...(expiresAt === undefined ? {} : { expiresAt: new Date(expiresAt) }),
|
|
111
119
|
attachments,
|
|
@@ -146,7 +154,27 @@ const ignoreReassigned = (error) => {
|
|
|
146
154
|
if (!(error instanceof ReassignedError))
|
|
147
155
|
throw error;
|
|
148
156
|
};
|
|
157
|
+
const rejectDeclined = (error) => {
|
|
158
|
+
if (!(error instanceof AgentDeclinedError))
|
|
159
|
+
throw error;
|
|
160
|
+
return reject(error.reason);
|
|
161
|
+
};
|
|
149
162
|
const unexpected = (reply) => new AgentJamError(`${reply.from} sent an unexpected ${reply.type}`);
|
|
163
|
+
const describeFailure = (error) => error instanceof MessagesNotAcceptedError
|
|
164
|
+
? { code: "messages_not_accepted", agent: error.agent }
|
|
165
|
+
: undefined;
|
|
166
|
+
const constructReplyError = ({ error, failure, }) => {
|
|
167
|
+
if (!failure)
|
|
168
|
+
return new AgentJamError(error);
|
|
169
|
+
switch (failure.code) {
|
|
170
|
+
case "messages_not_accepted":
|
|
171
|
+
return new MessagesNotAcceptedError(failure.agent);
|
|
172
|
+
default: {
|
|
173
|
+
const unknown = failure.code;
|
|
174
|
+
return new AgentJamError(error, { cause: unknown });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
150
178
|
const constructQuestionOptions = (task, { signal, timeoutMs = Infinity, ...options }) => ({
|
|
151
179
|
...options,
|
|
152
180
|
signal: signal ? AbortSignal.any([signal, task.signal]) : task.signal,
|
|
@@ -224,6 +252,8 @@ export class Agent {
|
|
|
224
252
|
link;
|
|
225
253
|
connected = Promise.withResolvers();
|
|
226
254
|
handlers;
|
|
255
|
+
context;
|
|
256
|
+
skills;
|
|
227
257
|
screen;
|
|
228
258
|
timeoutMs;
|
|
229
259
|
files;
|
|
@@ -236,8 +266,6 @@ export class Agent {
|
|
|
236
266
|
requestedName;
|
|
237
267
|
sources;
|
|
238
268
|
vaulted;
|
|
239
|
-
inputs;
|
|
240
|
-
delegates;
|
|
241
269
|
unwatch;
|
|
242
270
|
readiness = new Readiness();
|
|
243
271
|
accessWaits = new AccessWaits();
|
|
@@ -252,7 +280,7 @@ export class Agent {
|
|
|
252
280
|
performed = new IdempotencyCache(dedupeWindow);
|
|
253
281
|
triggers;
|
|
254
282
|
watches = new TaskWatches((outcomes) => this.resolveWatch(outcomes));
|
|
255
|
-
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 = {}) {
|
|
283
|
+
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, context, ...handlers }, shown = {}) {
|
|
256
284
|
this.requestedName = name;
|
|
257
285
|
const bindSecret = (connection) => constructSecretSource(connection, () => this.credentialChannel(connection.provider));
|
|
258
286
|
this.sources = new Map(skills.map((skill) => [skill.name, sourcesOf(skill, bindSecret)]));
|
|
@@ -272,8 +300,8 @@ export class Agent {
|
|
|
272
300
|
}));
|
|
273
301
|
return bound.includes(true);
|
|
274
302
|
}));
|
|
275
|
-
this.
|
|
276
|
-
this.
|
|
303
|
+
this.skills = new Map(skills.map((skill) => [skill.name, skill]));
|
|
304
|
+
this.context = { ...context, agent: this };
|
|
277
305
|
this.forward = { ...allForwarded, ...forward };
|
|
278
306
|
const tools = createNetworkTools(this, {
|
|
279
307
|
watching: handlers.onWatchResolved !== undefined,
|
|
@@ -308,7 +336,10 @@ export class Agent {
|
|
|
308
336
|
void this.restartTriggers(this.connection);
|
|
309
337
|
},
|
|
310
338
|
onError: (error) => this.orphan(error),
|
|
311
|
-
}, {
|
|
339
|
+
}, {
|
|
340
|
+
instanceId,
|
|
341
|
+
messages: handlers.onMessage !== undefined || this.forward.questions,
|
|
342
|
+
})
|
|
312
343
|
.then(async (connection) => {
|
|
313
344
|
try {
|
|
314
345
|
assertStored(connection.card, skills);
|
|
@@ -390,6 +421,7 @@ export class Agent {
|
|
|
390
421
|
* @returns The recipient's reply, with its files downloaded.
|
|
391
422
|
* @throws {AgentJamError} If the message attaches files or steers without being about a task, or an attachment couldn't upload.
|
|
392
423
|
* @throws {AgentNotFoundError} If no agent at `to` is connected.
|
|
424
|
+
* @throws {MessagesNotAcceptedError} If the recipient connected without `onMessage`.
|
|
393
425
|
* @throws {TimeoutError} If no reply arrives within the timeout, which a question to the requester of a task this agent runs only has when `options` sets one.
|
|
394
426
|
*/
|
|
395
427
|
async send(to, text, { conversationId = crypto.randomUUID(), idempotencyKey = crypto.randomUUID(), taskId = this.findCurrentTask(to)?.id, attachments, steer, ...options } = {}) {
|
|
@@ -893,7 +925,7 @@ export class Agent {
|
|
|
893
925
|
await Promise.allSettled([
|
|
894
926
|
this.triggers.stopAll("closing"),
|
|
895
927
|
...Array.from(this.incoming.values(), (task) => task.resumable
|
|
896
|
-
? task.
|
|
928
|
+
? task.handOver(new AgentJamError(reason))
|
|
897
929
|
: task.fail(new AgentJamError(reason))),
|
|
898
930
|
]);
|
|
899
931
|
const connection = await this.connection;
|
|
@@ -1018,7 +1050,13 @@ export class Agent {
|
|
|
1018
1050
|
};
|
|
1019
1051
|
}
|
|
1020
1052
|
catch (error) {
|
|
1021
|
-
|
|
1053
|
+
const failure = describeFailure(error);
|
|
1054
|
+
return {
|
|
1055
|
+
...response,
|
|
1056
|
+
type: "error",
|
|
1057
|
+
error: describeError(error),
|
|
1058
|
+
...(failure && { failure }),
|
|
1059
|
+
};
|
|
1022
1060
|
}
|
|
1023
1061
|
}
|
|
1024
1062
|
async computeReply(envelope) {
|
|
@@ -1031,6 +1069,8 @@ export class Agent {
|
|
|
1031
1069
|
});
|
|
1032
1070
|
return { text: reply.text, attachments: reply.attachments };
|
|
1033
1071
|
}
|
|
1072
|
+
if (!this.handlers.onMessage)
|
|
1073
|
+
throw new MessagesNotAcceptedError(this.name);
|
|
1034
1074
|
const { history, taskId, task, steer } = envelope;
|
|
1035
1075
|
const message = {
|
|
1036
1076
|
...toMessage(envelope),
|
|
@@ -1043,10 +1083,14 @@ export class Agent {
|
|
|
1043
1083
|
if (steer && this.findSteered(envelope)?.queueMessage(message)) {
|
|
1044
1084
|
return steered;
|
|
1045
1085
|
}
|
|
1046
|
-
|
|
1047
|
-
|
|
1086
|
+
try {
|
|
1087
|
+
return await this.handlers.onMessage(message, this);
|
|
1088
|
+
}
|
|
1089
|
+
catch (error) {
|
|
1090
|
+
if (error instanceof AgentDeclinedError)
|
|
1091
|
+
return error.reason;
|
|
1092
|
+
throw error;
|
|
1048
1093
|
}
|
|
1049
|
-
return this.handlers.onMessage(message, this);
|
|
1050
1094
|
}
|
|
1051
1095
|
/** Finds the running task a requester's message about it may steer. */
|
|
1052
1096
|
findSteered({ from, taskId }) {
|
|
@@ -1111,6 +1155,7 @@ export class Agent {
|
|
|
1111
1155
|
requester: from,
|
|
1112
1156
|
id: proposal.id,
|
|
1113
1157
|
}),
|
|
1158
|
+
recall: (ref) => this.recallTask(ref),
|
|
1114
1159
|
listHistory: this.listHistoryOf({
|
|
1115
1160
|
requester: from,
|
|
1116
1161
|
taskId: proposal.id,
|
|
@@ -1131,13 +1176,18 @@ export class Agent {
|
|
|
1131
1176
|
cleanup();
|
|
1132
1177
|
throw error;
|
|
1133
1178
|
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
}
|
|
1179
|
+
if (proposal.prepare) {
|
|
1180
|
+
void this.execute(task, cleanup, async (incoming) => {
|
|
1181
|
+
await this.readiness.wait(grant, incoming.signal);
|
|
1182
|
+
// WARNING: the relay cancels a task's children once it ends, so a preparation outlives the ones below it.
|
|
1183
|
+
await awaitPreparations(preparations);
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
else {
|
|
1187
|
+
void this.executeSkill(task, cleanup, async (incoming) => {
|
|
1188
|
+
await this.readiness.wait(grant, incoming.signal);
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1141
1191
|
return task.settled;
|
|
1142
1192
|
}
|
|
1143
1193
|
async judge(proposal) {
|
|
@@ -1147,24 +1197,27 @@ export class Agent {
|
|
|
1147
1197
|
replyTo: proposal.id,
|
|
1148
1198
|
};
|
|
1149
1199
|
try {
|
|
1150
|
-
const parsedInput = proposal.prepare
|
|
1151
|
-
?
|
|
1152
|
-
: await
|
|
1200
|
+
const [parsedInput, parsedConfig] = proposal.prepare
|
|
1201
|
+
? []
|
|
1202
|
+
: await Promise.all([
|
|
1203
|
+
this.parseSkillInput(proposal.skill, proposal.input),
|
|
1204
|
+
this.parseTriggerConfig(proposal),
|
|
1205
|
+
]);
|
|
1206
|
+
const refuse = (reason) => ({
|
|
1207
|
+
verdict: { ...response, type: "task.reject", reason },
|
|
1208
|
+
attachments: [],
|
|
1209
|
+
});
|
|
1153
1210
|
if (parsedInput && "problem" in parsedInput) {
|
|
1154
|
-
return
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
reason: parsedInput.problem,
|
|
1159
|
-
},
|
|
1160
|
-
attachments: [],
|
|
1161
|
-
};
|
|
1211
|
+
return refuse(parsedInput.problem);
|
|
1212
|
+
}
|
|
1213
|
+
if (parsedConfig && "problem" in parsedConfig) {
|
|
1214
|
+
return refuse(parsedConfig.problem);
|
|
1162
1215
|
}
|
|
1163
1216
|
const attachments = receiveAttachments(proposal.attachments, this.files, {
|
|
1164
1217
|
requester: proposal.from,
|
|
1165
1218
|
taskId: proposal.id,
|
|
1166
1219
|
});
|
|
1167
|
-
const { verdict, preparations } = await this.constructVerdict(proposal, attachments);
|
|
1220
|
+
const { verdict, preparations } = await this.constructVerdict(proposal, attachments, { input: parsedInput, config: parsedConfig });
|
|
1168
1221
|
return {
|
|
1169
1222
|
verdict: { ...response, ...verdict },
|
|
1170
1223
|
attachments,
|
|
@@ -1179,9 +1232,9 @@ export class Agent {
|
|
|
1179
1232
|
};
|
|
1180
1233
|
}
|
|
1181
1234
|
}
|
|
1182
|
-
async constructVerdict(proposal, attachments) {
|
|
1235
|
+
async constructVerdict(proposal, attachments, parsed) {
|
|
1183
1236
|
const { id, from, principal, skill } = proposal;
|
|
1184
|
-
const incoming = toProposal(proposal, attachments, this.files);
|
|
1237
|
+
const incoming = toProposal(proposal, attachments, this.files, parsed);
|
|
1185
1238
|
const verdict = proposal.prepare ? accept() : await this.decide(incoming);
|
|
1186
1239
|
if (!verdict.accepted) {
|
|
1187
1240
|
const rejection = {
|
|
@@ -1217,12 +1270,16 @@ export class Agent {
|
|
|
1217
1270
|
/** Lists the delegates of the proposal's skill, leaving out picked ones while another agent only prepares it. */
|
|
1218
1271
|
async pickDelegates(proposal, incoming) {
|
|
1219
1272
|
const { skill, prepare, then } = proposal;
|
|
1220
|
-
const delegates =
|
|
1273
|
+
const delegates = this.findSkill(skill)?.delegates;
|
|
1221
1274
|
const followUp = then ? [{ agent: then.to, skill: then.skill }] : [];
|
|
1222
1275
|
if (typeof delegates !== "function") {
|
|
1223
1276
|
return [...(delegates ?? []), ...followUp];
|
|
1224
1277
|
}
|
|
1225
|
-
|
|
1278
|
+
if (prepare)
|
|
1279
|
+
return [];
|
|
1280
|
+
// WARNING: `incoming` carries the input this skill's own schema parsed, which is the type its picker takes.
|
|
1281
|
+
const picked = await delegates(incoming, this);
|
|
1282
|
+
return [...picked, ...followUp];
|
|
1226
1283
|
}
|
|
1227
1284
|
prepareFor(parent) {
|
|
1228
1285
|
return async (delegates) => {
|
|
@@ -1264,20 +1321,31 @@ export class Agent {
|
|
|
1264
1321
|
});
|
|
1265
1322
|
}
|
|
1266
1323
|
/**
|
|
1267
|
-
* Runs the checks a proposal must pass before it reaches `onProposal`. It rejects
|
|
1268
|
-
*
|
|
1324
|
+
* Runs the checks a proposal must pass before it reaches `onProposal`. It rejects proposals that name a skill the
|
|
1325
|
+
* agent doesn't advertise or a trigger its skill doesn't declare, ones the skill's `runsAs` rules out, and ones that
|
|
1326
|
+
* name no skill or a skill this process has no `perform` for while the agent has no `onTask`.
|
|
1269
1327
|
*
|
|
1270
1328
|
* @param proposal The proposal to check.
|
|
1271
1329
|
* @returns Whether the proposal passes the checks.
|
|
1272
1330
|
*/
|
|
1273
1331
|
checkProposal(proposal) {
|
|
1274
1332
|
const { skill, trigger } = proposal;
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1333
|
+
const skills = listVisibleSkills(this.card, proposal.from);
|
|
1334
|
+
if (skill === undefined && !this.handlers.onTask) {
|
|
1335
|
+
return reject(skills.length > 0
|
|
1336
|
+
? describeMissingSkill(this.name, skills)
|
|
1337
|
+
: `"${this.name}" does not accept tasks`);
|
|
1338
|
+
}
|
|
1339
|
+
const problem = findProposalProblem(this.card, proposal, {
|
|
1340
|
+
attached: proposal.attachments,
|
|
1341
|
+
previous: listRecappedFiles(proposal.previous),
|
|
1342
|
+
});
|
|
1343
|
+
if (problem)
|
|
1344
|
+
return reject(problem);
|
|
1278
1345
|
if (skill !== undefined &&
|
|
1279
|
-
!this.
|
|
1280
|
-
|
|
1346
|
+
!this.findSkill(skill)?.perform &&
|
|
1347
|
+
!this.handlers.onTask) {
|
|
1348
|
+
return reject(`"${this.name}" does not run the "${skill}" skill`);
|
|
1281
1349
|
}
|
|
1282
1350
|
if (trigger && !this.triggers.offers(skill, trigger.name)) {
|
|
1283
1351
|
return reject(`${this.name} has no trigger named "${trigger.name}" for this skill`);
|
|
@@ -1286,7 +1354,7 @@ export class Agent {
|
|
|
1286
1354
|
}
|
|
1287
1355
|
/** @internal The agents and skills a skill hands work off to, leaving out ones a function picks per proposal. */
|
|
1288
1356
|
listDelegates(skill) {
|
|
1289
|
-
const delegates =
|
|
1357
|
+
const delegates = this.findSkill(skill)?.delegates;
|
|
1290
1358
|
return Array.isArray(delegates) ? delegates : [];
|
|
1291
1359
|
}
|
|
1292
1360
|
/** @internal Reports an error that can't reach another agent to `onError`. */
|
|
@@ -1297,19 +1365,31 @@ export class Agent {
|
|
|
1297
1365
|
const checked = this.checkProposal(proposal);
|
|
1298
1366
|
if (!checked.accepted)
|
|
1299
1367
|
return checked;
|
|
1300
|
-
const verdict = this.
|
|
1301
|
-
? await this.handlers.onProposal(proposal, this)
|
|
1302
|
-
: checked;
|
|
1368
|
+
const verdict = await this.consultHandlers(proposal, checked).catch(rejectDeclined);
|
|
1303
1369
|
if (!verdict.accepted || !this.screen || proposal.from === this.name) {
|
|
1304
1370
|
return verdict;
|
|
1305
1371
|
}
|
|
1306
1372
|
return screenProposal(proposal, this, this.screen);
|
|
1307
1373
|
}
|
|
1374
|
+
/** Asks `onProposal`, then the skill's `consider`, about a proposal `checkProposal()` accepted. */
|
|
1375
|
+
async consultHandlers(proposal, checked) {
|
|
1376
|
+
const gated = await this.handlers.onProposal?.(proposal, this.context);
|
|
1377
|
+
if (gated && !gated.accepted)
|
|
1378
|
+
return gated;
|
|
1379
|
+
const skill = this.findSkill(proposal.skill);
|
|
1380
|
+
return skill?.consider ? skill.consider(proposal, this.context) : checked;
|
|
1381
|
+
}
|
|
1382
|
+
async parseTriggerConfig({ skill, trigger }) {
|
|
1383
|
+
const schema = trigger && this.findSkill(skill)?.triggers?.[trigger.name]?.config;
|
|
1384
|
+
if (!trigger || !schema || !isInputSchema(schema))
|
|
1385
|
+
return undefined;
|
|
1386
|
+
return parseTriggerValue(trigger.name, "config", schema, trigger.config);
|
|
1387
|
+
}
|
|
1308
1388
|
async parseSkillInput(skill, input) {
|
|
1309
|
-
|
|
1389
|
+
const schema = this.findSkill(skill)?.input;
|
|
1390
|
+
if (skill === undefined || !schema)
|
|
1310
1391
|
return undefined;
|
|
1311
|
-
|
|
1312
|
-
return schema && parseValue(skill, "input", schema, input);
|
|
1392
|
+
return parseValue(skill, "input", schema, input);
|
|
1313
1393
|
}
|
|
1314
1394
|
priceOf(skill, requester) {
|
|
1315
1395
|
if (requester === this.name)
|
|
@@ -1338,6 +1418,7 @@ export class Agent {
|
|
|
1338
1418
|
const id = key(from, taskId);
|
|
1339
1419
|
const { response } = this.performed.remember(id, async () => {
|
|
1340
1420
|
const { attachments, parsedInput, failure } = await this.receiveTask(recur, { requester: from, taskId });
|
|
1421
|
+
const received = await this.readFiredEvent(recur);
|
|
1341
1422
|
const task = new TaskExecution({
|
|
1342
1423
|
id: taskId,
|
|
1343
1424
|
from,
|
|
@@ -1349,7 +1430,7 @@ export class Agent {
|
|
|
1349
1430
|
attachments,
|
|
1350
1431
|
scheduleId,
|
|
1351
1432
|
run,
|
|
1352
|
-
event:
|
|
1433
|
+
event: received.event,
|
|
1353
1434
|
price: this.priceOf(skill, from),
|
|
1354
1435
|
card: this.findSkillCard(skill),
|
|
1355
1436
|
isSelf: from === this.name,
|
|
@@ -1364,6 +1445,7 @@ export class Agent {
|
|
|
1364
1445
|
requester: from,
|
|
1365
1446
|
id: taskId,
|
|
1366
1447
|
}),
|
|
1448
|
+
recall: (ref) => this.recallTask(ref),
|
|
1367
1449
|
listHistory: this.listHistoryOf({ requester: from, taskId }),
|
|
1368
1450
|
requireAccess: this.requireAccessFor(skill, { requester: from, id: scheduleId, principal: principal ?? from }, taskId),
|
|
1369
1451
|
});
|
|
@@ -1372,14 +1454,39 @@ export class Agent {
|
|
|
1372
1454
|
this.performed.release(id);
|
|
1373
1455
|
};
|
|
1374
1456
|
this.incoming.set(id, task);
|
|
1375
|
-
await this.
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1457
|
+
await this.executeSkill(task, cleanup, () => {
|
|
1458
|
+
const problem = failure ?? received.failure;
|
|
1459
|
+
if (problem)
|
|
1460
|
+
throw problem.error;
|
|
1379
1461
|
});
|
|
1380
1462
|
});
|
|
1381
1463
|
return response;
|
|
1382
1464
|
}
|
|
1465
|
+
/** Names the trigger whose event started a run, and parses the event's data with that trigger's `event` schema. */
|
|
1466
|
+
async readFiredEvent(recur) {
|
|
1467
|
+
const event = eventOf(recur);
|
|
1468
|
+
if (!event)
|
|
1469
|
+
return { event: undefined };
|
|
1470
|
+
const name = recur.event?.trigger ?? this.findOnlyTrigger(recur.skill);
|
|
1471
|
+
if (!name)
|
|
1472
|
+
return { event };
|
|
1473
|
+
const named = { ...event, name };
|
|
1474
|
+
const schema = this.findSkill(recur.skill)?.triggers?.[name]?.event;
|
|
1475
|
+
if (!schema)
|
|
1476
|
+
return { event: named };
|
|
1477
|
+
const parsed = await parseTriggerValue(name, "event", schema, event.data).catch((error) => ({ problem: describeError(error) }));
|
|
1478
|
+
if ("problem" in parsed) {
|
|
1479
|
+
return {
|
|
1480
|
+
event: named,
|
|
1481
|
+
failure: { error: new AgentJamError(parsed.problem) },
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
return { event: { ...named, data: parsed.value } };
|
|
1485
|
+
}
|
|
1486
|
+
findOnlyTrigger(skill) {
|
|
1487
|
+
const triggers = this.findSkillCard(skill)?.triggers ?? [];
|
|
1488
|
+
return triggers.length === 1 ? triggers[0]?.name : undefined;
|
|
1489
|
+
}
|
|
1383
1490
|
/**
|
|
1384
1491
|
* Runs a task of a resumable skill that the relay moved here from a departed session of this agent, following the
|
|
1385
1492
|
* tasks it delegated there so their events aren't refused as unknown. A repeat of an attempt already running is ignored.
|
|
@@ -1424,15 +1531,15 @@ export class Agent {
|
|
|
1424
1531
|
send: this.sendAbout(from, taskId),
|
|
1425
1532
|
upload: this.uploadArtifact,
|
|
1426
1533
|
prepare: this.prepareFor(parent),
|
|
1534
|
+
recall: (ref) => this.recallTask(ref),
|
|
1427
1535
|
listHistory: this.listHistoryOf({ requester: from, taskId }),
|
|
1428
1536
|
requireAccess: this.requireAccessFor(skill, { ...grant, principal: principal ?? from }, taskId),
|
|
1429
1537
|
});
|
|
1430
1538
|
const cleanup = () => this.releaseExecution(id, task, grant);
|
|
1431
1539
|
this.incoming.set(id, task);
|
|
1432
|
-
await this.
|
|
1540
|
+
await this.executeSkill(task, cleanup, () => {
|
|
1433
1541
|
if (failure)
|
|
1434
1542
|
throw failure.error;
|
|
1435
|
-
return this.handlers.onTask?.(incoming, this);
|
|
1436
1543
|
});
|
|
1437
1544
|
}
|
|
1438
1545
|
/** Receives a task's attachments and parses its input, keeping the first failure for the task's run to throw. */
|
|
@@ -1487,6 +1594,35 @@ export class Agent {
|
|
|
1487
1594
|
cleanup();
|
|
1488
1595
|
}
|
|
1489
1596
|
}
|
|
1597
|
+
/** Runs a task with its skill's `perform`, or `onTask` without one, between the task lifecycle hooks, once `prepare` resolves. */
|
|
1598
|
+
async executeSkill(task, cleanup, prepare) {
|
|
1599
|
+
let started = false;
|
|
1600
|
+
await this.execute(task, cleanup, async (incoming) => {
|
|
1601
|
+
await prepare(incoming);
|
|
1602
|
+
started = true;
|
|
1603
|
+
await this.handlers.onTaskStarted?.(incoming, this.context);
|
|
1604
|
+
return this.runTask(incoming);
|
|
1605
|
+
});
|
|
1606
|
+
if (started)
|
|
1607
|
+
await this.endTask(task);
|
|
1608
|
+
}
|
|
1609
|
+
runTask(task) {
|
|
1610
|
+
const skill = this.findSkill(task.skill);
|
|
1611
|
+
return skill?.perform
|
|
1612
|
+
? skill.perform(task, this.context)
|
|
1613
|
+
: this.handlers.onTask?.(task, this);
|
|
1614
|
+
}
|
|
1615
|
+
findSkill(name) {
|
|
1616
|
+
return name === undefined ? undefined : this.skills.get(name);
|
|
1617
|
+
}
|
|
1618
|
+
async endTask(task) {
|
|
1619
|
+
try {
|
|
1620
|
+
await this.handlers.onTaskEnded?.(task, this.context, task.ending);
|
|
1621
|
+
}
|
|
1622
|
+
catch (error) {
|
|
1623
|
+
this.onError(error);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1490
1626
|
allSources() {
|
|
1491
1627
|
const sources = [...this.sources.values()].flatMap((skill) => [
|
|
1492
1628
|
...skill.values(),
|
|
@@ -1970,7 +2106,7 @@ export class Agent {
|
|
|
1970
2106
|
request(draft, { signal, timeoutMs = this.timeoutMs }, adopt) {
|
|
1971
2107
|
const response = this.requests.track(draft, { signal, timeoutMs }, (reply) => {
|
|
1972
2108
|
if (reply.type === "error")
|
|
1973
|
-
throw
|
|
2109
|
+
throw constructReplyError(reply);
|
|
1974
2110
|
return adopt(reply);
|
|
1975
2111
|
});
|
|
1976
2112
|
this.post(draft).catch((error) => this.requests.fail(draft.id, error));
|
|
@@ -2012,7 +2148,9 @@ export class Agent {
|
|
|
2012
2148
|
}
|
|
2013
2149
|
uploadArtifact = async (source, signal) => {
|
|
2014
2150
|
const connection = await this.connection;
|
|
2015
|
-
|
|
2151
|
+
if (!connection.upload)
|
|
2152
|
+
throw new AgentJamError(noFileArtifacts);
|
|
2153
|
+
return connection.upload(source, signal);
|
|
2016
2154
|
};
|
|
2017
2155
|
findIncomingTask(requester, taskId) {
|
|
2018
2156
|
if (taskId === undefined)
|
|
@@ -2048,11 +2186,14 @@ export class Agent {
|
|
|
2048
2186
|
const connection = await this.connection;
|
|
2049
2187
|
if (!connection.upload)
|
|
2050
2188
|
throw new AgentJamError(noUploads);
|
|
2051
|
-
const
|
|
2052
|
-
assertAttachmentTotal(
|
|
2189
|
+
const entries = await Promise.all(sources.map(constructAttachmentEntry));
|
|
2190
|
+
assertAttachmentTotal(entries);
|
|
2053
2191
|
const upload = connection.upload.bind(connection);
|
|
2054
|
-
|
|
2055
|
-
|
|
2192
|
+
return Promise.all(entries.map(async (entry) => {
|
|
2193
|
+
if (entry instanceof ForwardedFile)
|
|
2194
|
+
return entry.ref;
|
|
2195
|
+
return { attachmentId: await upload(entry, signal) };
|
|
2196
|
+
}));
|
|
2056
2197
|
}
|
|
2057
2198
|
}
|
|
2058
2199
|
/**
|