@agentjam/sdk 0.0.0-stage → 0.1.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 +181 -2
- package/dist/adapters/ai.d.ts +47 -0
- package/dist/adapters/ai.d.ts.map +1 -0
- package/dist/adapters/ai.js +168 -0
- package/dist/adapters/ai.js.map +1 -0
- package/dist/adapters/anthropic.d.ts +68 -0
- package/dist/adapters/anthropic.d.ts.map +1 -0
- package/dist/adapters/anthropic.js +193 -0
- package/dist/adapters/anthropic.js.map +1 -0
- package/dist/adapters/openai.d.ts +35 -0
- package/dist/adapters/openai.d.ts.map +1 -0
- package/dist/adapters/openai.js +80 -0
- package/dist/adapters/openai.js.map +1 -0
- package/dist/adapters/openaiAgents.d.ts +39 -0
- package/dist/adapters/openaiAgents.d.ts.map +1 -0
- package/dist/adapters/openaiAgents.js +152 -0
- package/dist/adapters/openaiAgents.js.map +1 -0
- package/dist/adapters/shared.d.ts +72 -0
- package/dist/adapters/shared.d.ts.map +1 -0
- package/dist/adapters/shared.js +174 -0
- package/dist/adapters/shared.js.map +1 -0
- package/dist/agent.d.ts +462 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +1820 -0
- package/dist/agent.js.map +1 -0
- package/dist/artifacts.d.ts +79 -0
- package/dist/artifacts.d.ts.map +1 -0
- package/dist/artifacts.js +367 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/attachments.js +69 -0
- package/dist/attachments.js.map +1 -0
- package/dist/connections.d.ts +109 -0
- package/dist/connections.d.ts.map +1 -0
- package/dist/connections.js +274 -0
- package/dist/connections.js.map +1 -0
- package/dist/content.d.ts +36 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +21 -0
- package/dist/content.js.map +1 -0
- package/dist/cron.d.ts +31 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +93 -0
- package/dist/cron.js.map +1 -0
- package/dist/errors.d.ts +227 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +253 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +57 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +24 -0
- package/dist/events.js.map +1 -0
- package/dist/idempotency.d.ts +21 -0
- package/dist/idempotency.d.ts.map +1 -0
- package/dist/idempotency.js +46 -0
- package/dist/idempotency.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/inputs.d.ts +22 -0
- package/dist/inputs.d.ts.map +1 -0
- package/dist/inputs.js +72 -0
- package/dist/inputs.js.map +1 -0
- package/dist/mpp.d.ts +76 -0
- package/dist/mpp.d.ts.map +1 -0
- package/dist/mpp.js +113 -0
- package/dist/mpp.js.map +1 -0
- package/dist/oauth.d.ts +3 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/oauthCallbacks.d.ts +2 -0
- package/dist/oauthCallbacks.d.ts.map +1 -0
- package/dist/oauthCallbacks.js +58 -0
- package/dist/oauthCallbacks.js.map +1 -0
- package/dist/platforms/datadog.d.ts +10 -0
- package/dist/platforms/datadog.d.ts.map +1 -0
- package/dist/platforms/datadog.js +15 -0
- package/dist/platforms/datadog.js.map +1 -0
- package/dist/platforms/github.d.ts +181 -0
- package/dist/platforms/github.d.ts.map +1 -0
- package/dist/platforms/github.js +468 -0
- package/dist/platforms/github.js.map +1 -0
- package/dist/platforms/google.d.ts +19 -0
- package/dist/platforms/google.d.ts.map +1 -0
- package/dist/platforms/google.js +70 -0
- package/dist/platforms/google.js.map +1 -0
- package/dist/platforms/grafana.d.ts +10 -0
- package/dist/platforms/grafana.d.ts.map +1 -0
- package/dist/platforms/grafana.js +14 -0
- package/dist/platforms/grafana.js.map +1 -0
- package/dist/platforms/posthog.d.ts +10 -0
- package/dist/platforms/posthog.d.ts.map +1 -0
- package/dist/platforms/posthog.js +14 -0
- package/dist/platforms/posthog.js.map +1 -0
- package/dist/platforms/presets.d.ts +34 -0
- package/dist/platforms/presets.d.ts.map +1 -0
- package/dist/platforms/presets.js +36 -0
- package/dist/platforms/presets.js.map +1 -0
- package/dist/platforms/sentry.d.ts +79 -0
- package/dist/platforms/sentry.d.ts.map +1 -0
- package/dist/platforms/sentry.js +124 -0
- package/dist/platforms/sentry.js.map +1 -0
- package/dist/platforms/slack.d.ts +101 -0
- package/dist/platforms/slack.d.ts.map +1 -0
- package/dist/platforms/slack.js +309 -0
- package/dist/platforms/slack.js.map +1 -0
- package/dist/platforms/splunk.d.ts +10 -0
- package/dist/platforms/splunk.d.ts.map +1 -0
- package/dist/platforms/splunk.js +14 -0
- package/dist/platforms/splunk.js.map +1 -0
- package/dist/polling.d.ts +2 -0
- package/dist/polling.d.ts.map +1 -0
- package/dist/polling.js +66 -0
- package/dist/polling.js.map +1 -0
- package/dist/profile.d.ts +60 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +119 -0
- package/dist/profile.js.map +1 -0
- package/dist/protocol.d.ts +474 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +2 -0
- package/dist/protocol.js.map +1 -0
- package/dist/providers.d.ts +113 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +98 -0
- package/dist/providers.js.map +1 -0
- package/dist/recaps.d.ts +10 -0
- package/dist/recaps.d.ts.map +1 -0
- package/dist/recaps.js +49 -0
- package/dist/recaps.js.map +1 -0
- package/dist/relay/chains.d.ts +62 -0
- package/dist/relay/chains.d.ts.map +1 -0
- package/dist/relay/chains.js +150 -0
- package/dist/relay/chains.js.map +1 -0
- package/dist/relay/credentials.d.ts +25 -0
- package/dist/relay/credentials.d.ts.map +1 -0
- package/dist/relay/credentials.js +234 -0
- package/dist/relay/credentials.js.map +1 -0
- package/dist/relay/endpoints.d.ts +13 -0
- package/dist/relay/endpoints.d.ts.map +1 -0
- package/dist/relay/endpoints.js +13 -0
- package/dist/relay/endpoints.js.map +1 -0
- package/dist/relay/frames.d.ts +193 -0
- package/dist/relay/frames.d.ts.map +1 -0
- package/dist/relay/frames.js +161 -0
- package/dist/relay/frames.js.map +1 -0
- package/dist/relay/grants.d.ts +158 -0
- package/dist/relay/grants.d.ts.map +1 -0
- package/dist/relay/grants.js +2 -0
- package/dist/relay/grants.js.map +1 -0
- package/dist/relay/guestStore.d.ts +37 -0
- package/dist/relay/guestStore.d.ts.map +1 -0
- package/dist/relay/guestStore.js +61 -0
- package/dist/relay/guestStore.js.map +1 -0
- package/dist/relay/guests.d.ts +7 -0
- package/dist/relay/guests.d.ts.map +1 -0
- package/dist/relay/guests.js +30 -0
- package/dist/relay/guests.js.map +1 -0
- package/dist/relay/memory.d.ts +60 -0
- package/dist/relay/memory.d.ts.map +1 -0
- package/dist/relay/memory.js +334 -0
- package/dist/relay/memory.js.map +1 -0
- package/dist/relay/payments.d.ts +43 -0
- package/dist/relay/payments.d.ts.map +1 -0
- package/dist/relay/payments.js +115 -0
- package/dist/relay/payments.js.map +1 -0
- package/dist/relay/profiles.d.ts +10 -0
- package/dist/relay/profiles.d.ts.map +1 -0
- package/dist/relay/profiles.js +43 -0
- package/dist/relay/profiles.js.map +1 -0
- package/dist/relay/relay.d.ts +133 -0
- package/dist/relay/relay.d.ts.map +1 -0
- package/dist/relay/relay.js +2 -0
- package/dist/relay/relay.js.map +1 -0
- package/dist/relay/remote.d.ts +39 -0
- package/dist/relay/remote.d.ts.map +1 -0
- package/dist/relay/remote.js +492 -0
- package/dist/relay/remote.js.map +1 -0
- package/dist/relay/revisions.d.ts +13 -0
- package/dist/relay/revisions.d.ts.map +1 -0
- package/dist/relay/revisions.js +34 -0
- package/dist/relay/revisions.js.map +1 -0
- package/dist/relay/schedules.d.ts +41 -0
- package/dist/relay/schedules.d.ts.map +1 -0
- package/dist/relay/schedules.js +217 -0
- package/dist/relay/schedules.js.map +1 -0
- package/dist/relay/secrets.d.ts +14 -0
- package/dist/relay/secrets.d.ts.map +1 -0
- package/dist/relay/secrets.js +30 -0
- package/dist/relay/secrets.js.map +1 -0
- package/dist/relay/sessions.d.ts +59 -0
- package/dist/relay/sessions.d.ts.map +1 -0
- package/dist/relay/sessions.js +282 -0
- package/dist/relay/sessions.js.map +1 -0
- package/dist/relay/uploads.d.ts +15 -0
- package/dist/relay/uploads.d.ts.map +1 -0
- package/dist/relay/uploads.js +55 -0
- package/dist/relay/uploads.js.map +1 -0
- package/dist/requests.d.ts +22 -0
- package/dist/requests.d.ts.map +1 -0
- package/dist/requests.js +64 -0
- package/dist/requests.js.map +1 -0
- package/dist/screening.d.ts +93 -0
- package/dist/screening.d.ts.map +1 -0
- package/dist/screening.js +176 -0
- package/dist/screening.js.map +1 -0
- package/dist/secrets.d.ts +38 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +162 -0
- package/dist/secrets.js.map +1 -0
- package/dist/skills.d.ts +11 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +55 -0
- package/dist/skills.js.map +1 -0
- package/dist/stripe.d.ts +10 -0
- package/dist/stripe.d.ts.map +1 -0
- package/dist/stripe.js +51 -0
- package/dist/stripe.js.map +1 -0
- package/dist/tasks/access.d.ts +35 -0
- package/dist/tasks/access.d.ts.map +1 -0
- package/dist/tasks/access.js +101 -0
- package/dist/tasks/access.js.map +1 -0
- package/dist/tasks/incoming.d.ts +333 -0
- package/dist/tasks/incoming.d.ts.map +1 -0
- package/dist/tasks/incoming.js +354 -0
- package/dist/tasks/incoming.js.map +1 -0
- package/dist/tasks/outgoing.d.ts +137 -0
- package/dist/tasks/outgoing.d.ts.map +1 -0
- package/dist/tasks/outgoing.js +233 -0
- package/dist/tasks/outgoing.js.map +1 -0
- package/dist/tasks/scheduled.d.ts +76 -0
- package/dist/tasks/scheduled.d.ts.map +1 -0
- package/dist/tasks/scheduled.js +110 -0
- package/dist/tasks/scheduled.js.map +1 -0
- package/dist/tasks/triggers.d.ts +47 -0
- package/dist/tasks/triggers.d.ts.map +1 -0
- package/dist/tasks/triggers.js +149 -0
- package/dist/tasks/triggers.js.map +1 -0
- package/dist/tasks/watches.d.ts +13 -0
- package/dist/tasks/watches.d.ts.map +1 -0
- package/dist/tasks/watches.js +54 -0
- package/dist/tasks/watches.js.map +1 -0
- package/dist/tools.d.ts +85 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +627 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +926 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vaults/local.d.ts +54 -0
- package/dist/vaults/local.d.ts.map +1 -0
- package/dist/vaults/local.js +362 -0
- package/dist/vaults/local.js.map +1 -0
- package/dist/vaults/remote.d.ts +78 -0
- package/dist/vaults/remote.d.ts.map +1 -0
- package/dist/vaults/remote.js +295 -0
- package/dist/vaults/remote.js.map +1 -0
- package/dist/vaults/vault.d.ts +69 -0
- package/dist/vaults/vault.d.ts.map +1 -0
- package/dist/vaults/vault.js +19 -0
- package/dist/vaults/vault.js.map +1 -0
- package/dist/verdicts.d.ts +15 -0
- package/dist/verdicts.d.ts.map +1 -0
- package/dist/verdicts.js +17 -0
- package/dist/verdicts.js.map +1 -0
- package/dist/watermarks.d.ts +29 -0
- package/dist/watermarks.d.ts.map +1 -0
- package/dist/watermarks.js +11 -0
- package/dist/watermarks.js.map +1 -0
- package/package.json +152 -3
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Content, ContentOptions, ContentSource } from "./content.js";
|
|
2
|
+
import { LabeledContent } from "./content.js";
|
|
3
|
+
import type { AttachmentRef } from "./protocol.js";
|
|
4
|
+
import type { Attachment } from "./types.js";
|
|
5
|
+
/** Anything a requester can attach to a task. Streams are read to the end before they upload. */
|
|
6
|
+
export type AttachmentSource = ContentSource;
|
|
7
|
+
/**
|
|
8
|
+
* Attaches a name and MIME type to attachment content, e.g. `attachment(csv, { name: "q3.csv" })`.
|
|
9
|
+
*
|
|
10
|
+
* @param content A string, `File`, or async iterable to attach.
|
|
11
|
+
* @param options The attachment's name and MIME type.
|
|
12
|
+
* @returns The labeled attachment, ready to pass in `attachments`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const attachment: (content: Content, options?: ContentOptions) => LabeledContent;
|
|
15
|
+
/**
|
|
16
|
+
* Turns an attachment source into the file that uploads. Text defaults to `text/plain`, and labels
|
|
17
|
+
* take precedence over a `File`'s own name and type.
|
|
18
|
+
*
|
|
19
|
+
* @param source The content, optionally labeled.
|
|
20
|
+
* @returns The file to upload.
|
|
21
|
+
*/
|
|
22
|
+
export declare const constructFile: (source: AttachmentSource) => Promise<File>;
|
|
23
|
+
/**
|
|
24
|
+
* Downloads an attachment the relay linked for this agent.
|
|
25
|
+
*
|
|
26
|
+
* @param ref The attachment's id, metadata and signed link.
|
|
27
|
+
* @returns The downloaded attachment.
|
|
28
|
+
* @throws {AgentJamError} If the relay sent no link, or the download fails.
|
|
29
|
+
*/
|
|
30
|
+
export declare const receiveAttachment: ({ attachmentId, name, mimeType, url, }: AttachmentRef) => Promise<Attachment>;
|
|
31
|
+
/**
|
|
32
|
+
* Downloads every attachment the relay linked for this agent, in order.
|
|
33
|
+
*
|
|
34
|
+
* @param refs The attachments' ids, metadata and signed links.
|
|
35
|
+
* @returns The downloaded attachments.
|
|
36
|
+
* @throws {AgentJamError} If any download fails.
|
|
37
|
+
*/
|
|
38
|
+
export declare const receiveAttachments: (refs?: AttachmentRef[]) => Promise<{
|
|
39
|
+
id: string;
|
|
40
|
+
type: "file";
|
|
41
|
+
file: File;
|
|
42
|
+
}[]>;
|
|
43
|
+
//# sourceMappingURL=attachments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.d.ts","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAW,MAAM,cAAc,CAAC;AAEvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,iGAAiG;AACjG,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC;AAE7C;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,YACZ,OAAO,YACP,cAAc,KACtB,cAAsD,CAAC;AAc1D;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,WAChB,gBAAgB,KACvB,OAAO,CAAC,IAAI,CAsBd,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,2CAK3B,aAAa,KAAG,OAAO,CAAC,UAAU,CAiBpC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,UAAU,aAAa,EAAE;;;;IACd,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { download } from "./artifacts.js";
|
|
2
|
+
import { LabeledContent, unlabel } from "./content.js";
|
|
3
|
+
import { AgentJamError } from "./errors.js";
|
|
4
|
+
import { defaultMimeType } from "./relay/uploads.js";
|
|
5
|
+
/**
|
|
6
|
+
* Attaches a name and MIME type to attachment content, e.g. `attachment(csv, { name: "q3.csv" })`.
|
|
7
|
+
*
|
|
8
|
+
* @param content A string, `File`, or async iterable to attach.
|
|
9
|
+
* @param options The attachment's name and MIME type.
|
|
10
|
+
* @returns The labeled attachment, ready to pass in `attachments`.
|
|
11
|
+
*/
|
|
12
|
+
export const attachment = (content, options = {}) => new LabeledContent(content, options);
|
|
13
|
+
const isText = (pieces) => pieces.every((piece) => typeof piece === "string");
|
|
14
|
+
const readPieces = async (stream) => {
|
|
15
|
+
const pieces = [];
|
|
16
|
+
for await (const piece of stream) {
|
|
17
|
+
pieces.push(typeof piece === "string" ? piece : new Uint8Array(piece));
|
|
18
|
+
}
|
|
19
|
+
return pieces;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Turns an attachment source into the file that uploads. Text defaults to `text/plain`, and labels
|
|
23
|
+
* take precedence over a `File`'s own name and type.
|
|
24
|
+
*
|
|
25
|
+
* @param source The content, optionally labeled.
|
|
26
|
+
* @returns The file to upload.
|
|
27
|
+
*/
|
|
28
|
+
export const constructFile = async (source) => {
|
|
29
|
+
const { content, options } = unlabel(source);
|
|
30
|
+
if (content instanceof File) {
|
|
31
|
+
if (!options.name && !options.mimeType)
|
|
32
|
+
return content;
|
|
33
|
+
return new File([content], options.name ?? content.name, {
|
|
34
|
+
type: options.mimeType ?? content.type,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const pieces = typeof content === "string" ? [content] : await readPieces(content);
|
|
38
|
+
const text = isText(pieces);
|
|
39
|
+
return new File(pieces, options.name ?? (text ? "attachment.txt" : "attachment"), {
|
|
40
|
+
type: options.mimeType ?? (text ? "text/plain" : defaultMimeType),
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Downloads an attachment the relay linked for this agent.
|
|
45
|
+
*
|
|
46
|
+
* @param ref The attachment's id, metadata and signed link.
|
|
47
|
+
* @returns The downloaded attachment.
|
|
48
|
+
* @throws {AgentJamError} If the relay sent no link, or the download fails.
|
|
49
|
+
*/
|
|
50
|
+
export const receiveAttachment = async ({ attachmentId, name, mimeType, url, }) => {
|
|
51
|
+
if (!url) {
|
|
52
|
+
throw new AgentJamError(`The relay sent attachment ${attachmentId} without a link to download it`);
|
|
53
|
+
}
|
|
54
|
+
const bytes = await download(url, "file", `attachment ${name ?? attachmentId}`);
|
|
55
|
+
return {
|
|
56
|
+
id: attachmentId,
|
|
57
|
+
type: "file",
|
|
58
|
+
file: new File([bytes], name ?? attachmentId, { type: mimeType ?? "" }),
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Downloads every attachment the relay linked for this agent, in order.
|
|
63
|
+
*
|
|
64
|
+
* @param refs The attachments' ids, metadata and signed links.
|
|
65
|
+
* @returns The downloaded attachments.
|
|
66
|
+
* @throws {AgentJamError} If any download fails.
|
|
67
|
+
*/
|
|
68
|
+
export const receiveAttachments = (refs = []) => Promise.all(refs.map(receiveAttachment));
|
|
69
|
+
//# sourceMappingURL=attachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.js","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAMrD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,OAAgB,EAChB,OAAO,GAAmB,EAAE,EACZ,EAAE,CAAC,IAAI,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAE1D,MAAM,MAAM,GAAG,CAAC,MAA+B,EAAE,EAAE,CACjD,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC;AAErD,MAAM,UAAU,GAAG,KAAK,EAAE,MAA0C,EAAE,EAAE;IACtE,MAAM,MAAM,GAAyC,EAAE,CAAC;IACxD,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACjC,MAAM,CAAC,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IACzE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,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AI,IAAI,YAAY,EAAE,CACrC,CAAC;IACF,OAAO;QACL,EAAE,EAAE,YAAY;QAChB,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,IAAI,IAAI,YAAY,EAAE,EAAE,IAAI,EAAE,QAAQ,IAAI,EAAE,EAAE,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAI,GAAoB,EAAE,EAAE,EAAE,CAC/D,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Account, AccountRevocation, OAuthEndpoints, OAuthProfile } from "./providers.js";
|
|
2
|
+
import type { ConnectionScopes, SkillConnection } from "./types.js";
|
|
3
|
+
import type { ConnectionEvent, ConnectionGrant, ConnectionHolder, ConnectionStore } from "./vaults/vault.js";
|
|
4
|
+
export { UnknownStateError } from "./vaults/vault.js";
|
|
5
|
+
export type { ConnectionEvent, ConnectionGrant, ConnectionHolder, ConnectionStore, } from "./vaults/vault.js";
|
|
6
|
+
export declare const constructScopeRecord: (scopes: ConnectionScopes, token?: string) => Record<string, string[]>;
|
|
7
|
+
/**
|
|
8
|
+
* Gives an executor access to a human's account with `provider`, through one grant per human that all of
|
|
9
|
+
* their tasks and schedules share, whichever agent requested them. `oauthConnection()` builds one, and a
|
|
10
|
+
* credential vault can implement it instead.
|
|
11
|
+
*/
|
|
12
|
+
export interface CredentialSource<Token extends string = string> extends SkillConnection {
|
|
13
|
+
/** Returns the URL where the principal connects their account for `grant`, or undefined when their grant already covers it. */
|
|
14
|
+
authorize(grant: ConnectionGrant): Promise<string | undefined>;
|
|
15
|
+
/** Returns where the principal changes what their account lets `grant` reach, such as which repositories, once a running task lacks access. */
|
|
16
|
+
constructAccessUrl?(grant: ConnectionGrant): Promise<string>;
|
|
17
|
+
/** Returns a current access token for `holder`, the one named `name` if given, and throws `ConnectionRevokedError` once access is lost. */
|
|
18
|
+
token(holder: ConnectionHolder, name?: Token): Promise<string>;
|
|
19
|
+
/** Returns the account `holder` uses, if the source knows it, and throws `ConnectionRevokedError` once access is lost. */
|
|
20
|
+
account?(holder: ConnectionHolder): Promise<Account | undefined>;
|
|
21
|
+
/** Ends every grant of the account that `revocation` names without calling the provider, which already ended access. */
|
|
22
|
+
revokeAccount?(revocation: AccountRevocation): Promise<void>;
|
|
23
|
+
/** Stops `holder` from using the principal's account once its task or schedule ended. */
|
|
24
|
+
release(holder: ConnectionHolder): Promise<void>;
|
|
25
|
+
/** Lists every task and schedule holding a grant, so work outside a task, such as a mailbox listener, can reach each account. */
|
|
26
|
+
listGrants?(): Promise<ConnectionGrant[]>;
|
|
27
|
+
/** Calls `listener` on every event, until the returned function is called. */
|
|
28
|
+
watch(listener: (event: ConnectionEvent) => void): () => void;
|
|
29
|
+
/** Set when the source revokes a grant once no task or schedule uses it, so a grant made ahead of a schedule's runs doesn't last until them. */
|
|
30
|
+
readonly revokeOnEnd?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** The settings every OAuth connection takes, whichever flow its provider runs. */
|
|
33
|
+
export type OAuthSettings<Token extends string = string> = SkillConnection<Token> & {
|
|
34
|
+
/** The OAuth client id the provider issued to the executor. */
|
|
35
|
+
clientId: string;
|
|
36
|
+
/** The OAuth client secret the provider issued to the executor. */
|
|
37
|
+
clientSecret: string;
|
|
38
|
+
/** The executor's callback URL registered with the provider, which passes each request to `agent.receiveOAuth()`, or to `callback()` on a custom server. */
|
|
39
|
+
redirectUri: string;
|
|
40
|
+
/**
|
|
41
|
+
* Where the connection keeps its tokens and pending authorizations. Defaults to AgentJam, which holds them for
|
|
42
|
+
* every instance of the agent, so pass a store only on a relay that holds no grants.
|
|
43
|
+
*/
|
|
44
|
+
store?: ConnectionStore;
|
|
45
|
+
/**
|
|
46
|
+
* Where a human who already connected changes what the executor reaches, which `task.requireAccess()` links to.
|
|
47
|
+
* Defaults to the provider's own page for it, such as a GitHub App's installation page, or else a fresh consent link.
|
|
48
|
+
*/
|
|
49
|
+
accessUrl?: string;
|
|
50
|
+
/** Revokes access with the provider once no task or schedule uses a grant, instead of keeping it for later ones. Defaults to false. */
|
|
51
|
+
revokeOnEnd?: boolean;
|
|
52
|
+
};
|
|
53
|
+
/** Options for `oauthConnection()`, which runs the provider's flow from either its `endpoints` or a `profile`. */
|
|
54
|
+
export type OAuthConnectionOptions<Token extends string = string> = OAuthSettings<Token> & ({
|
|
55
|
+
/** The provider's OAuth endpoints, which treat it as a standard OAuth 2.0 provider. */
|
|
56
|
+
endpoints: OAuthEndpoints;
|
|
57
|
+
profile?: never;
|
|
58
|
+
} | {
|
|
59
|
+
/** How to run the provider's flow, for providers that depart from the standard. */
|
|
60
|
+
profile: OAuthProfile;
|
|
61
|
+
endpoints?: never;
|
|
62
|
+
});
|
|
63
|
+
/**
|
|
64
|
+
* Options every platform's connection preset takes, such as `githubConnection()`. Each of `clientId`, `clientSecret`
|
|
65
|
+
* and `redirectUri` left out is read from the platform's environment variable, such as `GITHUB_CLIENT_ID`.
|
|
66
|
+
*/
|
|
67
|
+
export type OAuthPresetOptions = Omit<OAuthSettings, "provider" | "scopes" | "clientId" | "clientSecret" | "redirectUri"> & Partial<Pick<OAuthSettings, "clientId" | "clientSecret" | "redirectUri">>;
|
|
68
|
+
/** An OAuth 2.0 connection whose flow the executor runs itself, whose tokens are named `Token`. */
|
|
69
|
+
export type OAuthConnection<Token extends string = string> = CredentialSource<Token> & Required<Pick<CredentialSource, "account" | "revokeAccount" | "constructAccessUrl" | "listGrants">> & {
|
|
70
|
+
/** The scopes the skill asks the human to grant, keyed by token name. */
|
|
71
|
+
scopes: Record<string, string[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Finishes an authorization when the provider redirects the human back to `redirectUri`.
|
|
74
|
+
*
|
|
75
|
+
* @param url The callback request's URL, absolute or relative to `redirectUri`.
|
|
76
|
+
* @returns The grant that became connected.
|
|
77
|
+
* @throws {ConnectionFailedError} If connecting failed, such as when the provider refuses the code, but the human can try again at its `url`.
|
|
78
|
+
* @throws {AgentJamError} If the human denied access or some of the scopes, the authorization expired, or the provider refuses the code.
|
|
79
|
+
*/
|
|
80
|
+
callback(url: string | URL): Promise<ConnectionGrant>;
|
|
81
|
+
/**
|
|
82
|
+
* Revokes a human's grant with the provider and forgets its tokens, which ends every task and schedule that uses it.
|
|
83
|
+
*
|
|
84
|
+
* @param principal The human whose account to disconnect.
|
|
85
|
+
* @returns Resolves once the grant is revoked.
|
|
86
|
+
* @throws {AgentJamError} If the provider refuses the revocation.
|
|
87
|
+
*/
|
|
88
|
+
revoke(principal: string): Promise<void>;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Connects a skill to a human's account with an OAuth 2.0 provider. Each human connects once, and their
|
|
92
|
+
* later tasks and schedules reuse the grant while it covers their scopes, whichever agent requested them.
|
|
93
|
+
* Those waiting on the human at the same time share one link, which connects them all.
|
|
94
|
+
*
|
|
95
|
+
* @param options The provider, its scopes and client credentials, the callback URL, and a token store if the relay holds no grants.
|
|
96
|
+
* @returns A connection to list in a skill's `connections`.
|
|
97
|
+
* @throws {AgentJamError} If the options pass both `endpoints` and a `profile`, or neither.
|
|
98
|
+
*/
|
|
99
|
+
export declare const oauthConnection: <Token extends string = string>(options: OAuthConnectionOptions<Token>) => OAuthConnection<Token>;
|
|
100
|
+
/** Holds back one-off tasks until the requester's human connected every account their skill still needs. */
|
|
101
|
+
export declare class Readiness {
|
|
102
|
+
private readonly awaited;
|
|
103
|
+
expect(holder: ConnectionHolder, providers: string[]): void;
|
|
104
|
+
settle(provider: string, holder: ConnectionHolder): void;
|
|
105
|
+
/** Resolves once `holder` is ready, and throws if `signal` aborted first. */
|
|
106
|
+
wait(holder: ConnectionHolder, signal: AbortSignal): Promise<void>;
|
|
107
|
+
forget(holder: ConnectionHolder): void;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=connections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connections.d.ts","sourceRoot":"","sources":["../src/connections.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,OAAO,EACP,iBAAiB,EACjB,cAAc,EACd,YAAY,EAIb,MAAM,gBAAgB,CAAC;AAMxB,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAQpE,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,eAAe,EAIhB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,YAAY,EACV,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAkB3B,eAAO,MAAM,oBAAoB,WACvB,gBAAgB,qBAEvB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAKnB,CAAC;AAER;;;;GAIG;AACH,MAAM,WAAW,gBAAgB,CAC/B,KAAK,SAAS,MAAM,GAAG,MAAM,CAC7B,SAAQ,eAAe;IACvB,+HAA+H;IAC/H,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC/D,+IAA+I;IAC/I,kBAAkB,CAAC,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7D,2IAA2I;IAC3I,KAAK,CAAC,MAAM,EAAE,gBAAgB,EAAE,IAAI,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/D,0HAA0H;IAC1H,OAAO,CAAC,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAAC;IACjE,wHAAwH;IACxH,aAAa,CAAC,CAAC,UAAU,EAAE,iBAAiB,GAAG,OAAO,CAAC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AI,CAAC,aAAa,EAAE,UAAU,GAAG,cAAc,GAAG,aAAa,CAAC,CAAC,CAAC;AAE5E,mGAAmG;AACnG,MAAM,MAAM,eAAe,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,IACvD,gBAAgB,CAAC,KAAK,CAAC,GACrB,QAAQ,CACN,IAAI,CACF,gBAAgB,EAChB,SAAS,GAAG,eAAe,GAAG,oBAAoB,GAAG,YAAY,CAClE,CACF,GAAG;IACF,yEAAyE;IACzE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACjC;;;;;;;OAOG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;IACtD;;;;;;OAMG;IACH,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C,CAAC;AAkDN;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,SAAS,MAAM,GAAG,MAAM,WAClD,sBAAsB,CAAC,KAAK,CAAC,KACrC,eAAe,CAAC,KAAK,CA4PvB,CAAC;AAWF,4GAA4G;AAC5G,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkC;IAEnD,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,QAO1D;IAEM,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,QAKvD;IAED,6EAA6E;IAChE,IAAI,CAAC,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,WAAW,iBAgB9D;IAEM,MAAM,CAAC,MAAM,EAAE,gBAAgB,QAErC;CACF"}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { AgentJamError, ConnectionFailedError, describeError, } from "./errors.js";
|
|
3
|
+
import { constructStandardProfile, defaultToken, postForm, } from "./providers.js";
|
|
4
|
+
import { LocalGrantVault } from "./vaults/local.js";
|
|
5
|
+
import { RemoteGrantVault, VaultBinding, } from "./vaults/remote.js";
|
|
6
|
+
import { LostGrantError } from "./vaults/vault.js";
|
|
7
|
+
export { UnknownStateError } from "./vaults/vault.js";
|
|
8
|
+
/**
|
|
9
|
+
* Returns how to run the connection's OAuth flow: its `profile` as given, or its `endpoints` as a standard provider.
|
|
10
|
+
*
|
|
11
|
+
* @param options The connection's `endpoints` or `profile`.
|
|
12
|
+
* @returns The provider's profile.
|
|
13
|
+
* @throws {AgentJamError} If the connection passes both `endpoints` and a `profile`, or neither.
|
|
14
|
+
*/
|
|
15
|
+
const resolveProfile = ({ endpoints, profile }) => {
|
|
16
|
+
if (profile && !endpoints)
|
|
17
|
+
return profile;
|
|
18
|
+
if (endpoints && !profile)
|
|
19
|
+
return constructStandardProfile(endpoints);
|
|
20
|
+
throw new AgentJamError("oauthConnection() takes either endpoints or a profile");
|
|
21
|
+
};
|
|
22
|
+
export const constructScopeRecord = (scopes, token = defaultToken) => Array.isArray(scopes)
|
|
23
|
+
? { [token]: [...scopes] }
|
|
24
|
+
: Object.fromEntries(Object.entries(scopes).map(([name, granted]) => [name, [...granted]]));
|
|
25
|
+
const vaultBindings = new WeakMap();
|
|
26
|
+
const challengeOf = (verifier) => createHash("sha256").update(verifier).digest("base64url");
|
|
27
|
+
const grantedScopes = ({ tokens, scopes }, requested) => Object.fromEntries(Object.keys(tokens).map((name) => [
|
|
28
|
+
name,
|
|
29
|
+
scopes?.[name] ?? requested[name] ?? [],
|
|
30
|
+
]));
|
|
31
|
+
const refusal = ({ code, description }) => new AgentJamError(`The provider refused the token request: ${description ?? code}`);
|
|
32
|
+
/**
|
|
33
|
+
* @internal Lets `agent` reach the network for `source`, when it's an OAuth connection without a store.
|
|
34
|
+
*
|
|
35
|
+
* @returns Whether `source` keeps its grants on the network.
|
|
36
|
+
* @throws {AgentJamError} If another agent already serves the connection.
|
|
37
|
+
*/
|
|
38
|
+
export const bindGrantVault = (source, agent, link) => {
|
|
39
|
+
const binding = vaultBindings.get(source);
|
|
40
|
+
binding?.bind(agent, link);
|
|
41
|
+
return binding !== undefined;
|
|
42
|
+
};
|
|
43
|
+
/** @internal Frees `source` for another agent once `agent` stops serving it. */
|
|
44
|
+
export const unbindGrantVault = (source, agent) => vaultBindings.get(source)?.unbind(agent);
|
|
45
|
+
/** @internal Tells `source`'s watchers that the network revoked a grant, when `source` keeps its grants there. */
|
|
46
|
+
export const receiveRevocation = (source, revocation) => vaultBindings.get(source)?.receiveRevocation(revocation);
|
|
47
|
+
/**
|
|
48
|
+
* Connects a skill to a human's account with an OAuth 2.0 provider. Each human connects once, and their
|
|
49
|
+
* later tasks and schedules reuse the grant while it covers their scopes, whichever agent requested them.
|
|
50
|
+
* Those waiting on the human at the same time share one link, which connects them all.
|
|
51
|
+
*
|
|
52
|
+
* @param options The provider, its scopes and client credentials, the callback URL, and a token store if the relay holds no grants.
|
|
53
|
+
* @returns A connection to list in a skill's `connections`.
|
|
54
|
+
* @throws {AgentJamError} If the options pass both `endpoints` and a `profile`, or neither.
|
|
55
|
+
*/
|
|
56
|
+
export const oauthConnection = (options) => {
|
|
57
|
+
const { provider, scopes, clientId, clientSecret, redirectUri, store, accessUrl, revokeOnEnd = false, } = options;
|
|
58
|
+
const profile = resolveProfile(options);
|
|
59
|
+
const requested = constructScopeRecord(scopes, profile.defaultToken);
|
|
60
|
+
const urls = profile.endpoints;
|
|
61
|
+
const listeners = new Set();
|
|
62
|
+
const client = { clientId, clientSecret };
|
|
63
|
+
const requestTokens = async (form) => {
|
|
64
|
+
const response = await postForm(urls.token, { client_id: clientId, client_secret: clientSecret, ...form }, { accept: "application/json" });
|
|
65
|
+
const body = (await response.json());
|
|
66
|
+
return { body, error: profile.readError(response, body) };
|
|
67
|
+
};
|
|
68
|
+
const emit = (event) => {
|
|
69
|
+
for (const listener of listeners)
|
|
70
|
+
listener(event);
|
|
71
|
+
};
|
|
72
|
+
const mintLink = (wanted) => {
|
|
73
|
+
const state = `${provider}.${randomBytes(16).toString("base64url")}`;
|
|
74
|
+
const verifier = profile.pkce
|
|
75
|
+
? randomBytes(32).toString("base64url")
|
|
76
|
+
: undefined;
|
|
77
|
+
const url = new URL(urls.authorize);
|
|
78
|
+
const params = {
|
|
79
|
+
...profile.constructAuthorizeParams(wanted),
|
|
80
|
+
response_type: "code",
|
|
81
|
+
client_id: clientId,
|
|
82
|
+
redirect_uri: redirectUri,
|
|
83
|
+
state,
|
|
84
|
+
...(verifier === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: {
|
|
87
|
+
code_challenge: challengeOf(verifier),
|
|
88
|
+
code_challenge_method: "S256",
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
for (const [name, value] of Object.entries(params)) {
|
|
92
|
+
url.searchParams.set(name, value);
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
state,
|
|
96
|
+
url: url.toString(),
|
|
97
|
+
...(verifier !== undefined && { verifier }),
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
const refreshTokens = async (name, refreshToken, previous) => {
|
|
101
|
+
const { body, error } = await requestTokens({
|
|
102
|
+
grant_type: "refresh_token",
|
|
103
|
+
refresh_token: refreshToken,
|
|
104
|
+
});
|
|
105
|
+
if (error && profile.lostGrantErrors.includes(error.code)) {
|
|
106
|
+
throw new LostGrantError(`The provider lost the grant (${error.code})`);
|
|
107
|
+
}
|
|
108
|
+
if (error)
|
|
109
|
+
throw refusal(error);
|
|
110
|
+
const { tokens } = profile.parseTokens(body, previous);
|
|
111
|
+
const issued = tokens[name];
|
|
112
|
+
if (!issued) {
|
|
113
|
+
throw new AgentJamError(`The provider's refresh response has no token named "${name}"`);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
tokens: { ...tokens, [name]: { refreshToken, ...issued } },
|
|
117
|
+
accessToken: issued.accessToken,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const revokeTokens = async (tokens, keepShared) => {
|
|
121
|
+
for (const [name, issued] of Object.entries(tokens)) {
|
|
122
|
+
if (!keepShared || !profile.sharedTokens.includes(name)) {
|
|
123
|
+
await profile.revokeTokens(issued, client);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const parseTokens = async (body, previous) => {
|
|
128
|
+
const parsed = profile.parseTokens(body, previous);
|
|
129
|
+
const issued = parsed.tokens[profile.defaultToken];
|
|
130
|
+
if (parsed.account || !profile.fetchAccount || !issued)
|
|
131
|
+
return parsed;
|
|
132
|
+
return { ...parsed, account: await profile.fetchAccount(issued) };
|
|
133
|
+
};
|
|
134
|
+
const hooks = {
|
|
135
|
+
mintLink,
|
|
136
|
+
parseTokens,
|
|
137
|
+
refreshTokens,
|
|
138
|
+
revokeTokens,
|
|
139
|
+
emit,
|
|
140
|
+
};
|
|
141
|
+
const settings = {
|
|
142
|
+
provider,
|
|
143
|
+
requested,
|
|
144
|
+
impliedScopes: profile.impliedScopes,
|
|
145
|
+
revokeOnEnd,
|
|
146
|
+
};
|
|
147
|
+
const binding = new VaultBinding(provider, emit);
|
|
148
|
+
const vault = store
|
|
149
|
+
? new LocalGrantVault(store, settings, hooks)
|
|
150
|
+
: new RemoteGrantVault(settings, hooks, binding);
|
|
151
|
+
const emitRelink = (relink, reason) => {
|
|
152
|
+
if (!relink)
|
|
153
|
+
return;
|
|
154
|
+
for (const grant of relink.grants) {
|
|
155
|
+
emit({ type: "required", provider, grant, url: relink.url, reason });
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const storeIssued = async (state, authorization, code) => {
|
|
159
|
+
const { verifier, scopes: wanted } = authorization;
|
|
160
|
+
const { body, error } = await requestTokens({
|
|
161
|
+
grant_type: "authorization_code",
|
|
162
|
+
code,
|
|
163
|
+
redirect_uri: redirectUri,
|
|
164
|
+
...(verifier === undefined ? {} : { code_verifier: verifier }),
|
|
165
|
+
});
|
|
166
|
+
if (error)
|
|
167
|
+
throw refusal(error);
|
|
168
|
+
const granted = grantedScopes(profile.parseTokens(body), wanted);
|
|
169
|
+
return vault.storeGrant(state, body, granted, authorization);
|
|
170
|
+
};
|
|
171
|
+
const exchange = async (state, authorization, code) => {
|
|
172
|
+
const { ready, relink } = await storeIssued(state, authorization, code).catch(async (error) => {
|
|
173
|
+
// WARNING: the exchange's own failure is what the callback reports, so a failed reopening only leaves the old link.
|
|
174
|
+
const reopened = await vault
|
|
175
|
+
.reopenLink(state, authorization)
|
|
176
|
+
.catch(() => undefined);
|
|
177
|
+
if (!reopened)
|
|
178
|
+
throw error;
|
|
179
|
+
emitRelink(reopened, "Connecting failed, so try again at the new link");
|
|
180
|
+
throw new ConnectionFailedError(provider, describeError(error), reopened.url);
|
|
181
|
+
});
|
|
182
|
+
emitRelink(relink, "Grant every permission the link asks for");
|
|
183
|
+
for (const grant of ready)
|
|
184
|
+
emit({ type: "ready", provider, grant });
|
|
185
|
+
const [first] = ready;
|
|
186
|
+
if (first)
|
|
187
|
+
return first;
|
|
188
|
+
throw new AgentJamError(relink
|
|
189
|
+
? "The human didn't grant every scope, so they must consent again at a new link"
|
|
190
|
+
: "No task waits on this authorization any more");
|
|
191
|
+
};
|
|
192
|
+
const connection = {
|
|
193
|
+
provider,
|
|
194
|
+
scopes: requested,
|
|
195
|
+
revokeOnEnd,
|
|
196
|
+
authorize: (grant) => vault.authorize(grant),
|
|
197
|
+
constructAccessUrl: async (grant) => accessUrl ?? profile.constructAccessUrl?.(client) ?? vault.link(grant),
|
|
198
|
+
callback: async (callbackUrl) => {
|
|
199
|
+
const query = new URL(callbackUrl, redirectUri).searchParams;
|
|
200
|
+
const state = query.get("state") ?? "";
|
|
201
|
+
const denial = query.get("error");
|
|
202
|
+
if (denial !== null) {
|
|
203
|
+
const reason = `The human denied access (${denial})`;
|
|
204
|
+
for (const grant of await vault.deny(state)) {
|
|
205
|
+
emit({ type: "denied", provider, grant, reason });
|
|
206
|
+
}
|
|
207
|
+
throw new AgentJamError(reason);
|
|
208
|
+
}
|
|
209
|
+
const authorization = await vault.consume(state);
|
|
210
|
+
const code = query.get("code");
|
|
211
|
+
if (code === null) {
|
|
212
|
+
throw new AgentJamError("The OAuth callback has no code");
|
|
213
|
+
}
|
|
214
|
+
return exchange(state, authorization, code);
|
|
215
|
+
},
|
|
216
|
+
token: (holder, name = profile.defaultToken) => vault.readToken(holder, name),
|
|
217
|
+
account: (holder) => vault.readAccount(holder),
|
|
218
|
+
release: (holder) => vault.release(holder),
|
|
219
|
+
revoke: (principal) => vault.revoke(principal),
|
|
220
|
+
revokeAccount: (revocation) => vault.revokeAccount(revocation),
|
|
221
|
+
listGrants: () => vault.listGrants(),
|
|
222
|
+
watch: (listener) => {
|
|
223
|
+
listeners.add(listener);
|
|
224
|
+
return () => listeners.delete(listener);
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
if (!store)
|
|
228
|
+
vaultBindings.set(connection, binding);
|
|
229
|
+
return connection;
|
|
230
|
+
};
|
|
231
|
+
const readinessKey = ({ requester, id }) => JSON.stringify([requester, id]);
|
|
232
|
+
/** Holds back one-off tasks until the requester's human connected every account their skill still needs. */
|
|
233
|
+
export class Readiness {
|
|
234
|
+
awaited = new Map();
|
|
235
|
+
expect(holder, providers) {
|
|
236
|
+
if (providers.length === 0)
|
|
237
|
+
return;
|
|
238
|
+
this.awaited.set(readinessKey(holder), {
|
|
239
|
+
providers: new Set(providers),
|
|
240
|
+
outcome: Promise.withResolvers(),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
settle(provider, holder) {
|
|
244
|
+
const entry = this.awaited.get(readinessKey(holder));
|
|
245
|
+
if (!entry?.providers.delete(provider))
|
|
246
|
+
return;
|
|
247
|
+
if (entry.providers.size === 0)
|
|
248
|
+
entry.outcome.resolve(undefined);
|
|
249
|
+
}
|
|
250
|
+
/** Resolves once `holder` is ready, and throws if `signal` aborted first. */
|
|
251
|
+
async wait(holder, signal) {
|
|
252
|
+
const key = readinessKey(holder);
|
|
253
|
+
const entry = this.awaited.get(key);
|
|
254
|
+
if (!entry)
|
|
255
|
+
return;
|
|
256
|
+
const stop = () => entry.outcome.resolve(signal.reason);
|
|
257
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
258
|
+
if (signal.aborted)
|
|
259
|
+
stop();
|
|
260
|
+
try {
|
|
261
|
+
const failure = await entry.outcome.promise;
|
|
262
|
+
if (failure !== undefined)
|
|
263
|
+
throw failure;
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
signal.removeEventListener("abort", stop);
|
|
267
|
+
this.awaited.delete(key);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
forget(holder) {
|
|
271
|
+
this.awaited.delete(readinessKey(holder));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=connections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connections.js","sourceRoot":"","sources":["../src/connections.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,aAAa,GACd,MAAM,aAAa,CAAC;AAUrB,OAAO,EACL,wBAAwB,EACxB,YAAY,EACZ,QAAQ,GACT,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EACL,gBAAgB,EAEhB,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAU5B,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAQtD;;;;;;GAMG;AACH,MAAM,cAAc,GAAG,CAAC,EAAE,SAAS,EAAE,OAAO,EAA0B,EAAE,EAAE;IACxE,IAAI,OAAO,IAAI,CAAC,SAAS;QAAE,OAAO,OAAO,CAAC;IAC1C,IAAI,SAAS,IAAI,CAAC,OAAO;QAAE,O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AK,CAAC;YAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACnE,CAAC;IAED,6EAA6E;IACtE,KAAK,CAAC,IAAI,CAAC,MAAwB,EAAE,MAAmB;QAC7D,MAAM,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;QACjC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACxD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,IAAI,MAAM,CAAC,OAAO;YAAE,IAAI,EAAE,CAAC;QAE3B,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;YAC5C,IAAI,OAAO,KAAK,SAAS;gBAAE,MAAM,OAAO,CAAC;QAC3C,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAEM,MAAM,CAAC,MAAwB;QACpC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5C,CAAC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Content an artifact or attachment can carry. Async iterables, such as LLM text streams, stream as artifacts. */
|
|
2
|
+
export type Content = string | File | AsyncIterable<string | Uint8Array>;
|
|
3
|
+
/** Metadata attached with `artifact()` or `attachment()`. Both fields take precedence over those of a `File`. */
|
|
4
|
+
export type ContentOptions = {
|
|
5
|
+
/** The content's name, such as a file name. */
|
|
6
|
+
name?: string;
|
|
7
|
+
/** The content's MIME type. */
|
|
8
|
+
mimeType?: string;
|
|
9
|
+
};
|
|
10
|
+
/** Metadata attached with `artifact()`, which can also name a stable key. */
|
|
11
|
+
export type ArtifactOptions = ContentOptions & {
|
|
12
|
+
/** A stable key of 1 to 256 characters. A later delivery with the same key replaces the artifact for requesters. */
|
|
13
|
+
key?: string;
|
|
14
|
+
};
|
|
15
|
+
/** Content with metadata attached. Build one with `artifact()` or `attachment()`. */
|
|
16
|
+
export declare class LabeledContent {
|
|
17
|
+
/** The content to send. */
|
|
18
|
+
readonly content: Content;
|
|
19
|
+
/** The name, MIME type and key attached to the content. */
|
|
20
|
+
readonly options: ArtifactOptions;
|
|
21
|
+
constructor(
|
|
22
|
+
/** The content to send. */
|
|
23
|
+
content: Content,
|
|
24
|
+
/** The name, MIME type and key attached to the content. */
|
|
25
|
+
options: ArtifactOptions);
|
|
26
|
+
}
|
|
27
|
+
/** Content, optionally labeled with a name and MIME type. */
|
|
28
|
+
export type ContentSource = Content | LabeledContent;
|
|
29
|
+
/**
|
|
30
|
+
* Separates a source's content from its labels.
|
|
31
|
+
*
|
|
32
|
+
* @param source The content, optionally labeled.
|
|
33
|
+
* @returns The content and its labels, empty when it has none.
|
|
34
|
+
*/
|
|
35
|
+
export declare const unlabel: (source: ContentSource) => LabeledContent;
|
|
36
|
+
//# sourceMappingURL=content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,mHAAmH;AACnH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,aAAa,CAAC,MAAM,GAAG,UAAU,CAAC,CAAC;AAEzE,iHAAiH;AACjH,MAAM,MAAM,cAAc,GAAG;IAC3B,+CAA+C;IAC/C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,6EAA6E;AAC7E,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG;IAC7C,oHAAoH;IACpH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qFAAqF;AACrF,qBAAa,cAAc;IAEvB,2BAA2B;aACX,OAAO,EAAE,OAAO;IAChC,2DAA2D;aAC3C,OAAO,EAAE,eAAe;IAJ1C;IACE,2BAA2B;IACX,OAAO,EAAE,OAAO;IAChC,2DAA2D;IAC3C,OAAO,EAAE,eAAe,EACtC;CACL;AAED,6DAA6D;AAC7D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,cAAc,CAAC;AAErD;;;;;GAKG;AACH,eAAO,MAAM,OAAO,WAAY,aAAa,KAAG,cAC4B,CAAC"}
|
package/dist/content.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Content with metadata attached. Build one with `artifact()` or `attachment()`. */
|
|
2
|
+
export class LabeledContent {
|
|
3
|
+
content;
|
|
4
|
+
options;
|
|
5
|
+
constructor(
|
|
6
|
+
/** The content to send. */
|
|
7
|
+
content,
|
|
8
|
+
/** The name, MIME type and key attached to the content. */
|
|
9
|
+
options) {
|
|
10
|
+
this.content = content;
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Separates a source's content from its labels.
|
|
16
|
+
*
|
|
17
|
+
* @param source The content, optionally labeled.
|
|
18
|
+
* @returns The content and its labels, empty when it has none.
|
|
19
|
+
*/
|
|
20
|
+
export const unlabel = (source) => source instanceof LabeledContent ? source : new LabeledContent(source, {});
|
|
21
|
+
//# sourceMappingURL=content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.js","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAiBA,qFAAqF;AACrF,MAAM,OAAO,cAAc;IAGP,OAAO;IAEP,OAAO;IAJzB;IACE,2BAA2B;IACX,OAAgB;IAChC,2DAA2D;IAC3C,OAAwB;uBAFxB,OAAO;uBAEP,OAAO;IACtB,CAAC;CACL;AAKD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,MAAqB,EAAkB,EAAE,CAC/D,MAAM,YAAY,cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,cAAc,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC"}
|
package/dist/cron.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Cron } from "./types.js";
|
|
2
|
+
export declare const DEFAULT_TIME_ZONE = "UTC";
|
|
3
|
+
/** Where `computeShortestInterval()` and `isIntervalAtLeast()` look for runs. */
|
|
4
|
+
export type IntervalOptions = {
|
|
5
|
+
/** When to start looking. Defaults to now. */
|
|
6
|
+
from?: Date | undefined;
|
|
7
|
+
/** How far past `from` to look, in milliseconds. Defaults to a year. */
|
|
8
|
+
horizonMs?: number | undefined;
|
|
9
|
+
};
|
|
10
|
+
export declare const computeNextRun: (cron: Cron, after: Date) => Date | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Computes the shortest gap between consecutive runs of a cron, reading it in its time zone across daylight saving
|
|
13
|
+
* changes. Use it to refuse schedules that would run more often than an agent allows.
|
|
14
|
+
*
|
|
15
|
+
* @param cron The cron expression and its time zone.
|
|
16
|
+
* @param options When to start looking and how far ahead, which default to now and a year.
|
|
17
|
+
* @returns The shortest gap in milliseconds, or undefined if the cron runs at most once in that span.
|
|
18
|
+
* @throws {Error} If the expression or time zone is invalid.
|
|
19
|
+
*/
|
|
20
|
+
export declare const computeShortestInterval: (cron: Cron, options?: IntervalOptions) => number | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Checks that no two consecutive runs of a cron are closer than `minimumMs`, such as in `onProposal` before accepting a schedule.
|
|
23
|
+
*
|
|
24
|
+
* @param cron The cron expression and its time zone.
|
|
25
|
+
* @param minimumMs The shortest gap allowed between runs, in milliseconds.
|
|
26
|
+
* @param options When to start looking and how far ahead, which default to now and a year.
|
|
27
|
+
* @returns Whether every gap in that span is at least `minimumMs`.
|
|
28
|
+
* @throws {Error} If the expression or time zone is invalid.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isIntervalAtLeast: (cron: Cron, minimumMs: number, options?: IntervalOptions) => boolean;
|
|
31
|
+
//# sourceMappingURL=cron.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cron.d.ts","sourceRoot":"","sources":["../src/cron.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEvC,eAAO,MAAM,iBAAiB,QAAQ,CAAC;AAMvC,iFAAiF;AACjF,MAAM,MAAM,eAAe,GAAG;IAC5B,8CAA8C;IAC9C,IAAI,CAAC,EAAE,IAAI,GAAG,SAAS,CAAC;IACxB,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC,CAAC;AAUF,eAAO,MAAM,cAAc,SAAU,IAAI,SAAS,IAAI,qBACL,CAAC;AA0ElD;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,SAC5B,IAAI,YACD,eAAe,uBACoC,CAAC;AAE/D;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,SACtB,IAAI,aACC,MAAM,YACR,eAAe,YAKzB,CAAC"}
|