@capybara-hq/plugin-sdk 0.1.0-rc.84 → 0.1.0-rc.85

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.
@@ -0,0 +1,17 @@
1
+ # Authoring: extension point trigger-source
2
+
3
+ <!-- generated:extension-point -->
4
+ - Name: `trigger-source` (a ref is `trigger-source@<major>`)
5
+ - Declares the events an extension emits and which people may see their subjects.
6
+ - Providers: several
7
+ - Enabled per org: yes
8
+ - Brokered by the platform: yes (extensions use it only through `ctx`)
9
+ <!-- /generated -->
10
+
11
+ Contract: TriggerSource (`trigger-source.ts`), with `ctx.triggers` (`triggers.ts`)
12
+
13
+ Declare the event types the extension emits in `permissions.triggers.emit` (lower-case dotted names such as `item.created`, at most 64) and implement `TriggerSource.events()` with one `TriggerEventDeclaration` per type: a name, whether the event is directed at one agent, and its facts (a type, a name and whether automations may filter on it). Emit with `ctx.triggers.emit(event)`; the result says whether the `dedupeKey` was already seen (`duplicate`). An event carries the org, the subject (kind, id, title, optional url), scalar or string-list facts, a summary of at most 4096 UTF-8 bytes for the agent, and optionally the cause (the tool call or the account) and an audience for directed events. `emit` throws `TriggerError`: `type_not_declared`, `invalid_event` or `org_disabled`.
14
+
15
+ `visible` answers which of the given accounts may see a subject; the kernel only wakes agents of accounts it lists, and a throw or a missing answer means nobody. `subjectLink` optionally gives people a link to the subject. Automation filters are conjunctions over facts: `eq` compares scalars, `in` matches a scalar fact against a list, `contains` matches a list fact against a scalar, and a missing fact never matches.
16
+
17
+ See `extensions` for the rules every extension follows.
@@ -128,6 +128,7 @@ look like `channel@1`: name and major version. The registered points:
128
128
  | `file-preview` | multi | yes | Renders previews of files of given types: conversation files, shared files and skill files. |
129
129
  | `web-search` | multi | yes | Searches the web for the WebSearch tool; each org picks a default provider. |
130
130
  | `context` | multi | yes | Adds text to the model's context at run start in conversations of orgs that enable it. |
131
+ | `trigger-source` | multi | yes | Declares the events an extension emits and which people may see their subjects. |
131
132
  | `web-fetch` | multi | yes | Fetches and extracts web pages for the WebFetch tool; each org picks a default provider. |
132
133
  | `push-provider` | multi | no | Delivers push notifications to people's mobile apps through one channel (APNs, FCM, an aggregator). |
133
134
  <!-- /generated -->
package/dist/ctx.d.ts CHANGED
@@ -9,6 +9,7 @@ import type { ExtensionNet } from "./net.js";
9
9
  import type { SetupCheckDefinition } from "./setup-check.js";
10
10
  import type { ObjectHead, PresignedUpload, PresignGetOptions } from "./storage.js";
11
11
  import type { ToolCallContext, ToolHandler, ToolSpec, ToolWatcher } from "./tool.js";
12
+ import type { TriggersCtx } from "./triggers.js";
12
13
  import type { EmbedClaims, UiActionArgs, UiActionResult, UiRenderArgs, UiView } from "./ui.js";
13
14
  /** Logger interface. */
14
15
  export interface Logger {
@@ -426,6 +427,11 @@ export interface ExtensionContext {
426
427
  * see {@link DeviceAccessCtx}).
427
428
  */
428
429
  deviceAccess: DeviceAccessCtx;
430
+ /**
431
+ * Emits events automations can react to (requires `permissions.triggers`, see
432
+ * {@link TriggersCtx}).
433
+ */
434
+ triggers: TriggersCtx;
429
435
  provide(extensionPoint: ExtensionPointRef, impl: unknown): void;
430
436
  /**
431
437
  * The lifecycle signal of this plugin instance: aborted when the instance is revoked (disabled,
@@ -101,7 +101,8 @@ export declare const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE"
101
101
  * {@link DEVICE_SERVICE_INSTALL_SEAL} in it; from its next start on the directory is read only
102
102
  * for the service, so nothing the service later runs (a compromised browser included) can
103
103
  * replace what was installed. The provider empties it at every start that does not find it
104
- * sealed under the service's current files (unsealed, or a plugin upgrade since), so a service
104
+ * sealed under the service's current files (unsealed, or a plugin upgrade since; under its
105
+ * {@link DEVICE_SERVICE_INSTALL_KEY_FILE} where the service ships one), so a service
105
106
  * installs afresh while unsealed and never trusts what it finds there. The process that seals
106
107
  * keeps its write access until it exits, so a service runs no untrusted input (no web page) while
107
108
  * unsealed, seals only a complete install and then exits; the next start, reviewed again, runs it
@@ -110,6 +111,24 @@ export declare const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE"
110
111
  export declare const DEVICE_SERVICE_INSTALL_ENV = "CAPYBARA_SERVICE_INSTALL";
111
112
  /** The file that seals a service's install directory ({@link DEVICE_SERVICE_INSTALL_ENV}). */
112
113
  export declare const DEVICE_SERVICE_INSTALL_SEAL = ".sealed";
114
+ /**
115
+ * A service file naming what its install directory holds ({@link DEVICE_SERVICE_INSTALL_ENV}): one
116
+ * line matching {@link DEVICE_SERVICE_INSTALL_KEY_RE}, such as the browser revision. capybara-device
117
+ * (the device-local provider's machine program, capybara-hq/plugins#311) keeps such installs in a
118
+ * machine-wide cache of the OS user and keys them by it instead of by the service's files, so a
119
+ * plugin upgrade that keeps the key reuses the sealed install, and so do the other devices of the
120
+ * same deployment (its URL's origin and path) on that machine. Services of other deployments,
121
+ * platforms or launch environments never see it. The provider still hands an unsealed install out
122
+ * empty and private to one start, publishes it only once the service exits sealed (every link
123
+ * inside it, no hard link, no special file) and verifies it before every reuse. It assumes the
124
+ * unsealed install is the service's own code running no untrusted input, as the seal does: a
125
+ * child that left its process group could still write through an open file after the digest
126
+ * (the next reuse's verification catches it). Absent (or malformed): the install is keyed by the
127
+ * files.
128
+ */
129
+ export declare const DEVICE_SERVICE_INSTALL_KEY_FILE = "capybara-install-key";
130
+ /** What {@link DEVICE_SERVICE_INSTALL_KEY_FILE} holds: lower case, so it is one name on every file system. */
131
+ export declare const DEVICE_SERVICE_INSTALL_KEY_RE: RegExp;
113
132
  /**
114
133
  * Names the Mach service allowances the provider's sandbox gives the service (macOS: device-local
115
134
  * sets it, a spec cannot). Its value {@link DEVICE_SERVICE_MACH_CHROMIUM} means the full Chromium
@@ -119,7 +119,8 @@ export const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE";
119
119
  * {@link DEVICE_SERVICE_INSTALL_SEAL} in it; from its next start on the directory is read only
120
120
  * for the service, so nothing the service later runs (a compromised browser included) can
121
121
  * replace what was installed. The provider empties it at every start that does not find it
122
- * sealed under the service's current files (unsealed, or a plugin upgrade since), so a service
122
+ * sealed under the service's current files (unsealed, or a plugin upgrade since; under its
123
+ * {@link DEVICE_SERVICE_INSTALL_KEY_FILE} where the service ships one), so a service
123
124
  * installs afresh while unsealed and never trusts what it finds there. The process that seals
124
125
  * keeps its write access until it exits, so a service runs no untrusted input (no web page) while
125
126
  * unsealed, seals only a complete install and then exits; the next start, reviewed again, runs it
@@ -128,6 +129,24 @@ export const DEVICE_SERVICE_WORKSPACE_ENV = "CAPYBARA_SERVICE_WORKSPACE";
128
129
  export const DEVICE_SERVICE_INSTALL_ENV = "CAPYBARA_SERVICE_INSTALL";
129
130
  /** The file that seals a service's install directory ({@link DEVICE_SERVICE_INSTALL_ENV}). */
130
131
  export const DEVICE_SERVICE_INSTALL_SEAL = ".sealed";
132
+ /**
133
+ * A service file naming what its install directory holds ({@link DEVICE_SERVICE_INSTALL_ENV}): one
134
+ * line matching {@link DEVICE_SERVICE_INSTALL_KEY_RE}, such as the browser revision. capybara-device
135
+ * (the device-local provider's machine program, capybara-hq/plugins#311) keeps such installs in a
136
+ * machine-wide cache of the OS user and keys them by it instead of by the service's files, so a
137
+ * plugin upgrade that keeps the key reuses the sealed install, and so do the other devices of the
138
+ * same deployment (its URL's origin and path) on that machine. Services of other deployments,
139
+ * platforms or launch environments never see it. The provider still hands an unsealed install out
140
+ * empty and private to one start, publishes it only once the service exits sealed (every link
141
+ * inside it, no hard link, no special file) and verifies it before every reuse. It assumes the
142
+ * unsealed install is the service's own code running no untrusted input, as the seal does: a
143
+ * child that left its process group could still write through an open file after the digest
144
+ * (the next reuse's verification catches it). Absent (or malformed): the install is keyed by the
145
+ * files.
146
+ */
147
+ export const DEVICE_SERVICE_INSTALL_KEY_FILE = "capybara-install-key";
148
+ /** What {@link DEVICE_SERVICE_INSTALL_KEY_FILE} holds: lower case, so it is one name on every file system. */
149
+ export const DEVICE_SERVICE_INSTALL_KEY_RE = /^[a-z0-9][a-z0-9._+-]{0,127}$/;
131
150
  /**
132
151
  * Names the Mach service allowances the provider's sandbox gives the service (macOS: device-local
133
152
  * sets it, a spec cannot). Its value {@link DEVICE_SERVICE_MACH_CHROMIUM} means the full Chromium
@@ -21,6 +21,7 @@ export const EXTENSION_POINTS = {
21
21
  "file-preview": info([1], "Renders previews of files of given types: conversation files, shared files and skill files.", "multi", false, true),
22
22
  "web-search": info([1], "Searches the web for the WebSearch tool; each org picks a default provider.", "multi", true, true, "org-default"),
23
23
  context: info([1], "Adds text to the model's context at run start in conversations of orgs that enable it.", "multi", true, true),
24
+ "trigger-source": info([1], "Declares the events an extension emits and which people may see their subjects.", "multi", true, true),
24
25
  "web-fetch": info([1], "Fetches and extracts web pages for the WebFetch tool; each org picks a default provider.", "multi", true, true, "org-default"),
25
26
  "push-provider": info([1], "Delivers push notifications to people's mobile apps through one channel (APNs, FCM, an aggregator).", "multi", true),
26
27
  };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.1.0-rc.84";
1
+ export declare const SDK_VERSION = "0.1.0-rc.85";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./avatar-presets.js";
4
4
  export * from "./bootstrap.js";
@@ -54,6 +54,8 @@ export * from "./theme-readability.js";
54
54
  export * from "./theme-tokens.generated.js";
55
55
  export * from "./tool.js";
56
56
  export * from "./tool-key.js";
57
+ export * from "./trigger-source.js";
58
+ export * from "./triggers.js";
57
59
  export * from "./ui.js";
58
60
  export * from "./ui-parts.js";
59
61
  export * from "./ui-resolve.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export const SDK_VERSION = "0.1.0-rc.84";
1
+ export const SDK_VERSION = "0.1.0-rc.85";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./avatar-presets.js";
4
4
  export * from "./bootstrap.js";
@@ -54,6 +54,8 @@ export * from "./theme-readability.js";
54
54
  export * from "./theme-tokens.generated.js";
55
55
  export * from "./tool.js";
56
56
  export * from "./tool-key.js";
57
+ export * from "./trigger-source.js";
58
+ export * from "./triggers.js";
57
59
  export * from "./ui.js";
58
60
  export * from "./ui-parts.js";
59
61
  export * from "./ui-resolve.js";
@@ -76,6 +76,13 @@ export interface ExtensionPermissions {
76
76
  deviceAccess?: {
77
77
  services: string[];
78
78
  };
79
+ /**
80
+ * `ctx.triggers`: the event types this extension may emit (at most {@link TRIGGER_MAX_TYPES},
81
+ * each matching {@link TRIGGER_TYPE_RE}). An emit of another type is refused.
82
+ */
83
+ triggers?: {
84
+ emit: string[];
85
+ };
79
86
  }
80
87
  /** The most services `permissions.deviceAccess.services` may list. */
81
88
  export declare const DEVICE_ACCESS_MAX_SERVICES = 4;
@@ -3,6 +3,7 @@ import { DIAL_IN_PROTOCOL_RE } from "./dial-in.js";
3
3
  import { POLICY_HOOKS } from "./hooks.js";
4
4
  import { HTTP_ACCESS } from "./http-access.js";
5
5
  import { SETUP_CHECK_ID_RE, SETUP_CHECK_LIMITS } from "./setup-check.js";
6
+ import { TRIGGER_MAX_TYPES, TRIGGER_TYPE_RE } from "./triggers.js";
6
7
  import { UI_ICONS } from "./ui.js";
7
8
  import { isCompactForm, parseSlotRef, uiSlot } from "./ui-slots.js";
8
9
  /** The most services `permissions.deviceAccess.services` may list. */
@@ -50,6 +51,7 @@ const KEYS = new Set([
50
51
  "setupChecks",
51
52
  "dialIn",
52
53
  "deviceAccess",
54
+ "triggers",
53
55
  ]);
54
56
  const POLICY = new Set(POLICY_HOOKS);
55
57
  const METHODS = new Set(["GET", "POST", "PUT", "DELETE"]);
@@ -235,6 +237,20 @@ export function validatePermissions(plugin, p, provides = []) {
235
237
  }
236
238
  }
237
239
  }
240
+ if (p.triggers !== undefined) {
241
+ if (!isObj(p.triggers) || Object.keys(p.triggers).some((k) => k !== "emit")) {
242
+ errs.push("permissions.triggers must be { emit: string[] }");
243
+ }
244
+ else {
245
+ const types = strList("permissions.triggers.emit", p.triggers.emit, errs);
246
+ if (types.length > TRIGGER_MAX_TYPES)
247
+ errs.push(`permissions.triggers.emit lists at most ${TRIGGER_MAX_TYPES} types`);
248
+ for (const t of types) {
249
+ if (!TRIGGER_TYPE_RE.test(t))
250
+ errs.push(`permissions.triggers.emit entry "${t}" must match ${TRIGGER_TYPE_RE}`);
251
+ }
252
+ }
253
+ }
238
254
  return errs;
239
255
  }
240
256
  const UI_ID_RE = /^[a-z][a-z0-9-]{0,39}$/;
@@ -0,0 +1,35 @@
1
+ import type { ExtensionPointRef } from "./extension-point.js";
2
+ import type { LocalizedName } from "./plugin-package.js";
3
+ import type { TriggerSubject } from "./triggers.js";
4
+ /** One fact of an event type: its value type and whether automations may filter on it. */
5
+ export interface TriggerFactDeclaration {
6
+ type: "string" | "number" | "boolean" | "string[]";
7
+ name: LocalizedName;
8
+ filterable: boolean;
9
+ }
10
+ /** One event type an extension emits, shown when an org builds an automation. */
11
+ export interface TriggerEventDeclaration {
12
+ type: string;
13
+ name: LocalizedName;
14
+ /** Directed events wake one agent (`TriggerEvent.audience`) instead of every subscriber. */
15
+ directed: boolean;
16
+ facts: Record<string, TriggerFactDeclaration>;
17
+ }
18
+ /** What a `trigger-source@1` provider implements. */
19
+ export interface TriggerSource {
20
+ events(): TriggerEventDeclaration[];
21
+ /** Of `accountIds`, the accounts that may see `subject`. */
22
+ visible(args: {
23
+ orgId: string;
24
+ accountIds: string[];
25
+ subject: TriggerSubject;
26
+ config?: unknown;
27
+ }): Promise<string[]>;
28
+ /** A link a person can open for the subject, or null. */
29
+ subjectLink?(args: {
30
+ orgId: string;
31
+ subject: TriggerSubject;
32
+ }): Promise<string | null>;
33
+ }
34
+ /** Trigger source extension point ref. */
35
+ export declare const TRIGGER_SOURCE_EXTENSION_POINT: ExtensionPointRef;
@@ -0,0 +1,2 @@
1
+ /** Trigger source extension point ref. */
2
+ export const TRIGGER_SOURCE_EXTENSION_POINT = "trigger-source@1";
@@ -0,0 +1,72 @@
1
+ import type { ToolCallContext } from "./tool.js";
2
+ /** The value of one fact on a trigger event: filters compare facts, never free text. */
3
+ export type TriggerFactValue = string | number | boolean | string[];
4
+ /** What an event is about (a task, a document, ...). */
5
+ export interface TriggerSubject {
6
+ kind: string;
7
+ id: string;
8
+ title: string;
9
+ url?: string;
10
+ }
11
+ /** Who caused an event: a tool call of the extension, or an account acting outside a call. */
12
+ export type TriggerCause = {
13
+ call: ToolCallContext;
14
+ } | {
15
+ accountId: string;
16
+ };
17
+ /** An event an extension emits through `ctx.triggers.emit`. */
18
+ export interface TriggerEvent {
19
+ orgId: string;
20
+ /** One of the types the extension declares in `permissions.triggers.emit`. */
21
+ type: string;
22
+ subject: TriggerSubject;
23
+ facts: Record<string, TriggerFactValue>;
24
+ /** Short text for the agent; at most {@link TRIGGER_SUMMARY_MAX_BYTES} UTF-8 bytes. */
25
+ summary: string;
26
+ /** Idempotency key: the same key for the same type and org emits once. */
27
+ dedupeKey: string;
28
+ cause?: TriggerCause;
29
+ /** A directed event: only this agent of this account is woken. */
30
+ audience?: {
31
+ accountId: string;
32
+ agentId: string;
33
+ };
34
+ }
35
+ export interface EventFilterClause {
36
+ fact: string;
37
+ op: "eq" | "in" | "contains";
38
+ value: TriggerFactValue;
39
+ }
40
+ /** A conjunction: every clause must match. An empty filter matches every event. */
41
+ export type EventFilter = EventFilterClause[];
42
+ /** `ctx.triggers` (requires `permissions.triggers`). */
43
+ export interface TriggersCtx {
44
+ emit(event: TriggerEvent): Promise<{
45
+ eventId: string;
46
+ duplicate: boolean;
47
+ }>;
48
+ }
49
+ export type TriggerErrorCode = "type_not_declared" | "invalid_event" | "org_disabled";
50
+ /** A {@link TriggersCtx} call was refused. */
51
+ export declare class TriggerError extends Error {
52
+ readonly code: TriggerErrorCode;
53
+ constructor(code: TriggerErrorCode, message: string, opts?: {
54
+ cause?: unknown;
55
+ });
56
+ }
57
+ export declare const TRIGGER_SUMMARY_MAX_BYTES = 4096;
58
+ /** The shape of a trigger event type name (also the entries of `permissions.triggers.emit`). */
59
+ export declare const TRIGGER_TYPE_RE: RegExp;
60
+ /** Most event types one extension may declare. */
61
+ export declare const TRIGGER_MAX_TYPES = 64;
62
+ /**
63
+ * Checks an event before it is stored; returns error codes (empty = valid): `type_not_declared`,
64
+ * `summary_too_long` (UTF-8 bytes), and `missing_<field>` for each empty required field.
65
+ */
66
+ export declare function validateTriggerEvent(e: TriggerEvent, declared: readonly string[]): string[];
67
+ /**
68
+ * Whether the facts satisfy every clause. A missing fact never matches. `eq` compares scalars (an
69
+ * array fact never matches); `in` takes an array value and matches a scalar fact contained in it;
70
+ * `contains` matches an array fact containing the scalar value.
71
+ */
72
+ export declare function matchesFilter(filter: EventFilter, facts: Record<string, TriggerFactValue>): boolean;
@@ -0,0 +1,71 @@
1
+ /** A {@link TriggersCtx} call was refused. */
2
+ export class TriggerError extends Error {
3
+ code;
4
+ constructor(code, message, opts) {
5
+ super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
6
+ this.name = "TriggerError";
7
+ this.code = code;
8
+ }
9
+ }
10
+ export const TRIGGER_SUMMARY_MAX_BYTES = 4096;
11
+ /** The shape of a trigger event type name (also the entries of `permissions.triggers.emit`). */
12
+ export const TRIGGER_TYPE_RE = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/;
13
+ /** Most event types one extension may declare. */
14
+ export const TRIGGER_MAX_TYPES = 64;
15
+ /**
16
+ * Checks an event before it is stored; returns error codes (empty = valid): `type_not_declared`,
17
+ * `summary_too_long` (UTF-8 bytes), and `missing_<field>` for each empty required field.
18
+ */
19
+ export function validateTriggerEvent(e, declared) {
20
+ const errs = [];
21
+ const blank = (v) => typeof v !== "string" || v === "";
22
+ if (blank(e.orgId))
23
+ errs.push("missing_org_id");
24
+ if (blank(e.type))
25
+ errs.push("missing_type");
26
+ else if (!declared.includes(e.type))
27
+ errs.push("type_not_declared");
28
+ if (blank(e.subject?.kind))
29
+ errs.push("missing_subject_kind");
30
+ if (blank(e.subject?.id))
31
+ errs.push("missing_subject_id");
32
+ if (blank(e.subject?.title))
33
+ errs.push("missing_subject_title");
34
+ if (blank(e.dedupeKey))
35
+ errs.push("missing_dedupe_key");
36
+ if (typeof e.summary !== "string")
37
+ errs.push("missing_summary");
38
+ else if (new TextEncoder().encode(e.summary).length > TRIGGER_SUMMARY_MAX_BYTES)
39
+ errs.push("summary_too_long");
40
+ return errs;
41
+ }
42
+ /**
43
+ * Whether the facts satisfy every clause. A missing fact never matches. `eq` compares scalars (an
44
+ * array fact never matches); `in` takes an array value and matches a scalar fact contained in it;
45
+ * `contains` matches an array fact containing the scalar value.
46
+ */
47
+ export function matchesFilter(filter, facts) {
48
+ return filter.every((c) => {
49
+ if (!Object.hasOwn(facts, c.fact))
50
+ return false;
51
+ const f = facts[c.fact];
52
+ if (f === undefined)
53
+ return false;
54
+ switch (c.op) {
55
+ case "eq":
56
+ return !Array.isArray(f) && !Array.isArray(c.value) && f === c.value;
57
+ case "in":
58
+ return (Array.isArray(c.value) &&
59
+ !Array.isArray(f) &&
60
+ typeof f === "string" &&
61
+ c.value.includes(f));
62
+ case "contains":
63
+ return (Array.isArray(f) &&
64
+ !Array.isArray(c.value) &&
65
+ typeof c.value === "string" &&
66
+ f.includes(c.value));
67
+ default:
68
+ return false;
69
+ }
70
+ });
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.84",
3
+ "version": "0.1.0-rc.85",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {