@capybara-hq/plugin-sdk 0.1.0-rc.86 → 0.1.0-rc.88

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,116 @@
1
+ /**
2
+ * Read-only org facts an extension needs to build people-facing pages (collaboration spec): the
3
+ * agents a member may use, the conversations that serve one of the extension's own subjects, and
4
+ * the members' names and avatars. Each member of `ctx` sits behind its own permission key
5
+ * (`permissions.agents`, `permissions.conversations`, `permissions.people`); every call names the
6
+ * org and runs in its scope, and the extension must be enabled there.
7
+ */
8
+ import type { BuiltinAvatarPresetId } from "./avatar-presets.js";
9
+ /**
10
+ * An avatar a page can draw, structurally the same as the design system's `AvatarValue` (what
11
+ * `Avatar` and `PrincipalPicker` take; the type is copied because the SDK imports no design
12
+ * package): a built-in preset, an image (`src` is a path the web app serves, such as
13
+ * `/api/v1/accounts/<id>/avatar?v=…`, or an absolute URL), or none (the initial is drawn).
14
+ */
15
+ export type AvatarValue = {
16
+ kind: "preset";
17
+ id: BuiltinAvatarPresetId;
18
+ } | {
19
+ kind: "image";
20
+ src: string;
21
+ } | null;
22
+ /** The most account ids one {@link PeopleCtx.get} call takes. */
23
+ export declare const PEOPLE_GET_MAX = 200;
24
+ /** The most results one {@link PeopleCtx.search} call returns (a larger `limit` is clamped). */
25
+ export declare const PEOPLE_SEARCH_MAX = 50;
26
+ /** The most conversations one {@link ConversationsCtx.bySubject} call returns (newest first). */
27
+ export declare const CONVERSATIONS_BY_SUBJECT_MAX = 200;
28
+ /** An agent a member may use. */
29
+ export interface UsableAgent {
30
+ id: string;
31
+ name: string;
32
+ /** A preset stays a preset; an upload, an external image or a theme preset is an image. */
33
+ avatar: AvatarValue;
34
+ }
35
+ /**
36
+ * `ctx.agents` (requires `permissions.agents: { read: true }`).
37
+ */
38
+ export interface AgentsCtx {
39
+ /**
40
+ * The agents `accountId` may use in the org (their own, the org's and those shared with them).
41
+ * An account that is not an active member of the org gets `[]`.
42
+ */
43
+ usableBy(args: {
44
+ orgId: string;
45
+ accountId: string;
46
+ }): Promise<UsableAgent[]>;
47
+ }
48
+ /** What a conversation of a subject is doing now. */
49
+ export type SubjectConversationState = "idle" | "running" | "waiting";
50
+ /** A conversation serving a subject: metadata only, never its content. */
51
+ export interface SubjectConversation {
52
+ id: string;
53
+ ownerAccountId: string;
54
+ agentId: string;
55
+ /** From the latest run: queued or running is `running`, waiting on something is `waiting`. */
56
+ state: SubjectConversationState;
57
+ /** ISO time of the conversation's last activity. */
58
+ lastActivityAt: string;
59
+ }
60
+ /**
61
+ * `ctx.conversations` (requires `permissions.conversations: { subjects: true }`).
62
+ */
63
+ export interface ConversationsCtx {
64
+ /**
65
+ * The live conversations of the org whose subject is `{ source: this extension, kind, id }`
66
+ * (set by `subject`-mode event automations), newest activity first, at most
67
+ * {@link CONVERSATIONS_BY_SUBJECT_MAX}. Another extension's subjects are never returned.
68
+ */
69
+ bySubject(args: {
70
+ orgId: string;
71
+ subject: {
72
+ kind: string;
73
+ id: string;
74
+ };
75
+ }): Promise<SubjectConversation[]>;
76
+ }
77
+ /** A member as {@link PeopleCtx.get} returns it: no email or other profile field. */
78
+ export interface PersonView {
79
+ id: string;
80
+ name: string;
81
+ avatar: AvatarValue;
82
+ /** False once the account is disabled (it is still a member until removed). */
83
+ active: boolean;
84
+ }
85
+ /**
86
+ * `ctx.people` (requires `permissions.people: { read: true }`): members of the org only.
87
+ */
88
+ export interface PeopleCtx {
89
+ /**
90
+ * The given accounts that are members of the org (others are dropped), at most
91
+ * {@link PEOPLE_GET_MAX} ids.
92
+ */
93
+ get(args: {
94
+ orgId: string;
95
+ accountIds: string[];
96
+ }): Promise<PersonView[]>;
97
+ /**
98
+ * Active members whose display name contains `query` (case-insensitive), by name; an empty
99
+ * `query` returns active members up to the limit. `limit` defaults to 20 and is clamped to
100
+ * {@link PEOPLE_SEARCH_MAX}.
101
+ */
102
+ search(args: {
103
+ orgId: string;
104
+ query: string;
105
+ limit?: number;
106
+ }): Promise<Pick<PersonView, "id" | "name" | "avatar">[]>;
107
+ }
108
+ /** Why a read of `ctx.agents`, `ctx.conversations` or `ctx.people` was refused. */
109
+ export type CtxReadErrorCode = "invalid_args" | "org_disabled";
110
+ /** A call of `ctx.agents` / `ctx.conversations` / `ctx.people` was refused. */
111
+ export declare class CtxReadError extends Error {
112
+ readonly code: CtxReadErrorCode;
113
+ constructor(code: CtxReadErrorCode, message: string, opts?: {
114
+ cause?: unknown;
115
+ });
116
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Read-only org facts an extension needs to build people-facing pages (collaboration spec): the
3
+ * agents a member may use, the conversations that serve one of the extension's own subjects, and
4
+ * the members' names and avatars. Each member of `ctx` sits behind its own permission key
5
+ * (`permissions.agents`, `permissions.conversations`, `permissions.people`); every call names the
6
+ * org and runs in its scope, and the extension must be enabled there.
7
+ */
8
+ /** The most account ids one {@link PeopleCtx.get} call takes. */
9
+ export const PEOPLE_GET_MAX = 200;
10
+ /** The most results one {@link PeopleCtx.search} call returns (a larger `limit` is clamped). */
11
+ export const PEOPLE_SEARCH_MAX = 50;
12
+ /** The most conversations one {@link ConversationsCtx.bySubject} call returns (newest first). */
13
+ export const CONVERSATIONS_BY_SUBJECT_MAX = 200;
14
+ /** A call of `ctx.agents` / `ctx.conversations` / `ctx.people` was refused. */
15
+ export class CtxReadError extends Error {
16
+ code;
17
+ constructor(code, message, opts) {
18
+ super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
19
+ this.name = "CtxReadError";
20
+ this.code = code;
21
+ }
22
+ }
package/dist/ctx.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import type { Kysely } from "kysely";
2
+ import type { AgentsCtx, ConversationsCtx, PeopleCtx } from "./agents-ctx.js";
2
3
  import type { DeviceServiceSpec, DeviceServiceStatus, ServiceBlockedHost } from "./device-provider.js";
3
4
  import type { DevicesCtx, DialInChannel } from "./dial-in.js";
4
5
  import type { ExtensionPointRef } from "./extension-point.js";
5
6
  import type { AvailableHook, HookHandler, HookOptions } from "./hooks.js";
6
7
  import type { RouteCaller } from "./http-access.js";
8
+ import type { InboxCtx } from "./inbox.js";
7
9
  import type { ImageAspectRatio, ImageMediaType, ImageSize, ModelEmbeddingInfo, ModelImageGenerationInfo, ModelKind } from "./model.js";
8
10
  import type { ExtensionNet } from "./net.js";
9
11
  import type { SetupCheckDefinition } from "./setup-check.js";
@@ -444,6 +446,20 @@ export interface ExtensionContext {
444
446
  * {@link TriggersCtx}).
445
447
  */
446
448
  triggers: TriggersCtx;
449
+ /**
450
+ * Posts personal inbox items and resolves them (requires `permissions.inbox`, see
451
+ * {@link InboxCtx}).
452
+ */
453
+ inbox: InboxCtx;
454
+ /** The agents a member may use (requires `permissions.agents`, see {@link AgentsCtx}). */
455
+ agents: AgentsCtx;
456
+ /**
457
+ * The conversations serving the extension's own subjects (requires `permissions.conversations`,
458
+ * see {@link ConversationsCtx}).
459
+ */
460
+ conversations: ConversationsCtx;
461
+ /** Names and avatars of an org's members (requires `permissions.people`, see {@link PeopleCtx}). */
462
+ people: PeopleCtx;
447
463
  provide(extensionPoint: ExtensionPointRef, impl: unknown): void;
448
464
  /**
449
465
  * The lifecycle signal of this plugin instance: aborted when the instance is revoked (disabled,
package/dist/hooks.d.ts CHANGED
@@ -4,7 +4,7 @@ import type { ContentBlock, ImageSize, ModelRequest, ModelUsage } from "./model.
4
4
  * `HOOK_MOUNT_POINTS` (`session.*`, `agent.preStep`) stay reserved: they validate in a manifest but
5
5
  * cannot be registered yet.
6
6
  */
7
- export declare const HOOKS_AVAILABLE: readonly ["model.request", "model.response", "tool.preExecute", "tool.postExecute", "usage.report", "org.member_removed", "agent.deleted", "org.deleted"];
7
+ export declare const HOOKS_AVAILABLE: readonly ["model.request", "model.response", "tool.preExecute", "tool.postExecute", "usage.report", "org.member_removed", "agent.deleted", "org.deleted", "run.lifecycle"];
8
8
  /** Lifecycle mount points: observe-only notices that something was removed. */
9
9
  export declare const LIFECYCLE_HOOKS: readonly ["org.member_removed", "agent.deleted", "org.deleted"];
10
10
  /** A mount point that `ctx.hooks.on` accepts. */
@@ -302,6 +302,60 @@ export interface AgentDeletedPayload {
302
302
  export interface OrgDeletedPayload {
303
303
  orgId: string;
304
304
  }
305
+ /**
306
+ * `run.lifecycle`: what happened to a run of a conversation that has a subject, or to directed work
307
+ * the kernel did not run (collaboration spec §4.8). Observe-only, delivered durably through a
308
+ * queue, at least once, after the change committed, and only to the plugin named by the
309
+ * conversation's subject `source` (for `offer_declined`, the plugin that emitted the event), when
310
+ * it declares `run.lifecycle` in `permissions.hooks` and the org has it enabled (checked when the
311
+ * change commits and again at delivery); `call.config` is the extension's effective config for the
312
+ * org. A handler that fails never affects the run. Handlers must be idempotent. The payload carries
313
+ * ids only. Compact runs (context compaction) are never reported.
314
+ *
315
+ * - `started`: a run of the conversation was created (queued).
316
+ * - `waiting`: the run suspended on a person (`waitingOn`: an `approval`, or `input`: a question
317
+ * or a connect card).
318
+ * - `resumed`: the person's wait cleared (answered, decided or timed out) and the run continues;
319
+ * one per wait, like `waiting`.
320
+ * - `finished`: the run completed. `failed`: it failed. `aborted`: a person or the kernel stopped
321
+ * it.
322
+ * - `offer_declined`: directed work was not run (`reason`): the person's rule is `off`, the person
323
+ * `declined` the offer, their acceptance automation is `paused`, the offer `expired` unanswered
324
+ * (seven days), or the event stayed `undelivered` (the kernel gave up routing it). No run and no
325
+ * conversation: `accountId` and `agentId` are the event's audience; `eventId` and `dedupeKey`
326
+ * name the event the decline is for, so a plugin can tell a stale decline (an older event of
327
+ * the subject) from one for its latest.
328
+ */
329
+ export interface RunLifecyclePayload {
330
+ orgId: string;
331
+ /** The run's trigger account; for `offer_declined` the event's audience person. */
332
+ accountId: string;
333
+ agentId: string;
334
+ /** Absent for `offer_declined`. */
335
+ agentVersion?: number;
336
+ /** Absent for `offer_declined`. */
337
+ conversationId?: string;
338
+ /** Absent for `offer_declined`. */
339
+ runId?: string;
340
+ phase: "started" | "waiting" | "resumed" | "finished" | "failed" | "aborted" | "offer_declined";
341
+ /** The conversation's subject (or the directed event's subject). */
342
+ subject: {
343
+ kind: string;
344
+ id: string;
345
+ };
346
+ /** Set for `waiting` and `resumed`. */
347
+ waitingOn?: "approval" | "input";
348
+ /** Set for `offer_declined`. */
349
+ reason?: RunLifecycleDeclineReason;
350
+ /** Set for `offer_declined`: the kernel's id of the directed event that was not run. */
351
+ eventId?: string;
352
+ /**
353
+ * Set for `offer_declined`: the `dedupeKey` the plugin gave that event in `ctx.triggers.emit`.
354
+ */
355
+ dedupeKey?: string;
356
+ }
357
+ /** Why directed work was not run (`run.lifecycle` `offer_declined`). */
358
+ export type RunLifecycleDeclineReason = "declined" | "off" | "paused" | "expired" | "undelivered";
305
359
  /** Payload type of each mount point. */
306
360
  export interface HookPayloads {
307
361
  "model.request": ModelRequestHookPayload;
@@ -312,6 +366,7 @@ export interface HookPayloads {
312
366
  "org.member_removed": OrgMemberRemovedPayload;
313
367
  "agent.deleted": AgentDeletedPayload;
314
368
  "org.deleted": OrgDeletedPayload;
369
+ "run.lifecycle": RunLifecyclePayload;
315
370
  }
316
371
  /**
317
372
  * What a hook returns: nothing, or `{ deny: reason }`. A deny only takes effect for a `policy` hook
package/dist/hooks.js CHANGED
@@ -12,6 +12,7 @@ export const HOOKS_AVAILABLE = [
12
12
  "org.member_removed",
13
13
  "agent.deleted",
14
14
  "org.deleted",
15
+ "run.lifecycle",
15
16
  ];
16
17
  /** Lifecycle mount points: observe-only notices that something was removed. */
17
18
  export const LIFECYCLE_HOOKS = ["org.member_removed", "agent.deleted", "org.deleted"];
@@ -0,0 +1,95 @@
1
+ import type { LocalizedName } from "./plugin-package.js";
2
+ /**
3
+ * One button on an inbox item: clicking it calls `uiAction.actionId` of the extension's UI
4
+ * contribution `uiAction.contribution` (registered with `ctx.ui.action`) as the person who
5
+ * clicked, with `payload`, then resolves the item. The contribution must be one of an org-wide
6
+ * slot (such as `org.page`): the call carries no conversation or agent. The action must be
7
+ * idempotent: delivery is at least once (a click whose result was lost may be pressed again).
8
+ */
9
+ export interface InboxAction {
10
+ /** Unique within the item ({@link INBOX_ID_RE}). */
11
+ id: string;
12
+ label: LocalizedName;
13
+ style?: "primary" | "danger";
14
+ uiAction: {
15
+ contribution: string;
16
+ actionId: string;
17
+ payload?: unknown;
18
+ };
19
+ }
20
+ /** A personal inbox item an extension posts through `ctx.inbox.post`. */
21
+ export interface InboxPost {
22
+ orgId: string;
23
+ /** The one person the item is for: an active member of `orgId`. */
24
+ accountId: string;
25
+ /** What kind of item it is, for the extension ({@link INBOX_KIND_RE}). */
26
+ kind: string;
27
+ /** At most {@link INBOX_LIMITS}.titleChars characters. */
28
+ title: string;
29
+ /** At most {@link INBOX_LIMITS}.bodyChars characters. */
30
+ body?: string;
31
+ /**
32
+ * An in-app path opened from the item: an extension's item may only link to its own pages,
33
+ * `/p/<extension name>/...` (at most {@link INBOX_LIMITS}.linkChars characters).
34
+ */
35
+ link?: string;
36
+ /** At most {@link INBOX_ACTIONS_MAX}. */
37
+ actions?: InboxAction[];
38
+ /**
39
+ * Seconds until the item expires and leaves the inbox (default
40
+ * {@link INBOX_LIMITS}.defaultExpirySeconds, at most {@link INBOX_LIMITS}.maxExpirySeconds).
41
+ */
42
+ expiresInSeconds?: number;
43
+ /**
44
+ * Idempotency key: while an item of this extension with the same key is open for the same
45
+ * person in the same org, a post returns that item instead of adding another.
46
+ */
47
+ dedupeKey?: string;
48
+ }
49
+ /** `ctx.inbox` (requires `permissions.inbox`): personal inbox items. */
50
+ export interface InboxCtx {
51
+ /** Posts an item for one person (see {@link InboxPost}); the person's push settings apply. */
52
+ post(p: InboxPost): Promise<{
53
+ itemId: string;
54
+ }>;
55
+ /** Resolves an open item this extension posted (an item already closed is left as it is). */
56
+ resolve(args: {
57
+ orgId: string;
58
+ itemId: string;
59
+ }): Promise<void>;
60
+ }
61
+ export type InboxErrorCode = "invalid_item" | "org_disabled" | "not_found";
62
+ /** A {@link InboxCtx} call was refused. */
63
+ export declare class InboxError extends Error {
64
+ readonly code: InboxErrorCode;
65
+ constructor(code: InboxErrorCode, message: string, opts?: {
66
+ cause?: unknown;
67
+ });
68
+ }
69
+ /** The most actions one inbox item carries. */
70
+ export declare const INBOX_ACTIONS_MAX = 3;
71
+ /** Limits of an inbox item. */
72
+ export declare const INBOX_LIMITS: {
73
+ readonly titleChars: 200;
74
+ readonly bodyChars: 2000;
75
+ readonly linkChars: 512;
76
+ readonly kindChars: 64;
77
+ readonly labelChars: 40;
78
+ readonly idChars: 64;
79
+ readonly dedupeKeyChars: 200;
80
+ /** UTF-8 bytes of one action's `payload` as JSON (as a UI button's). */
81
+ readonly payloadBytes: 2000;
82
+ readonly defaultExpirySeconds: number;
83
+ readonly maxExpirySeconds: number;
84
+ };
85
+ /** The shape of an item's `kind`: dotted lower-case words. */
86
+ export declare const INBOX_KIND_RE: RegExp;
87
+ /** The shape of an action id, a contribution id and a UI action id. */
88
+ export declare const INBOX_ID_RE: RegExp;
89
+ /**
90
+ * Checks a post before it is stored; returns error codes (empty = valid): `missing_<field>`,
91
+ * `<field>_too_long`, `kind_invalid`, `link_invalid` (not a path of the extension's own pages
92
+ * `/p/<plugin>/`), `too_many_actions`, `action_invalid`, `action_duplicate`, `payload_too_large`,
93
+ * `expiry_invalid`. `plugin` is the posting extension's name.
94
+ */
95
+ export declare function validateInboxPost(p: InboxPost, plugin: string): string[];
package/dist/inbox.js ADDED
@@ -0,0 +1,152 @@
1
+ import { UI_LIMITS } from "./ui.js";
2
+ /** A {@link InboxCtx} call was refused. */
3
+ export class InboxError extends Error {
4
+ code;
5
+ constructor(code, message, opts) {
6
+ super(message, opts && "cause" in opts ? { cause: opts.cause } : undefined);
7
+ this.name = "InboxError";
8
+ this.code = code;
9
+ }
10
+ }
11
+ /** The most actions one inbox item carries. */
12
+ export const INBOX_ACTIONS_MAX = 3;
13
+ /** Limits of an inbox item. */
14
+ export const INBOX_LIMITS = {
15
+ titleChars: 200,
16
+ bodyChars: 2000,
17
+ linkChars: 512,
18
+ kindChars: 64,
19
+ labelChars: 40,
20
+ idChars: 64,
21
+ dedupeKeyChars: 200,
22
+ /** UTF-8 bytes of one action's `payload` as JSON (as a UI button's). */
23
+ payloadBytes: UI_LIMITS.payloadBytes,
24
+ defaultExpirySeconds: 30 * 24 * 3600,
25
+ maxExpirySeconds: 90 * 24 * 3600,
26
+ };
27
+ /** The shape of an item's `kind`: dotted lower-case words. */
28
+ export const INBOX_KIND_RE = /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/;
29
+ /** The shape of an action id, a contribution id and a UI action id. */
30
+ export const INBOX_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
31
+ /**
32
+ * Checks a post before it is stored; returns error codes (empty = valid): `missing_<field>`,
33
+ * `<field>_too_long`, `kind_invalid`, `link_invalid` (not a path of the extension's own pages
34
+ * `/p/<plugin>/`), `too_many_actions`, `action_invalid`, `action_duplicate`, `payload_too_large`,
35
+ * `expiry_invalid`. `plugin` is the posting extension's name.
36
+ */
37
+ export function validateInboxPost(p, plugin) {
38
+ const errs = [];
39
+ const str = (v) => typeof v === "string";
40
+ const blank = (v) => !str(v) || v.trim() === "";
41
+ if (blank(p.orgId))
42
+ errs.push("missing_org_id");
43
+ if (blank(p.accountId))
44
+ errs.push("missing_account_id");
45
+ if (blank(p.kind))
46
+ errs.push("missing_kind");
47
+ else if (p.kind.length > INBOX_LIMITS.kindChars || !INBOX_KIND_RE.test(p.kind))
48
+ errs.push("kind_invalid");
49
+ if (blank(p.title))
50
+ errs.push("missing_title");
51
+ else if (p.title.length > INBOX_LIMITS.titleChars)
52
+ errs.push("title_too_long");
53
+ if (p.body !== undefined) {
54
+ if (!str(p.body))
55
+ errs.push("body_invalid");
56
+ else if (p.body.length > INBOX_LIMITS.bodyChars)
57
+ errs.push("body_too_long");
58
+ }
59
+ if (p.link !== undefined) {
60
+ if (!str(p.link) || p.link.length > INBOX_LIMITS.linkChars || !ownPageLink(p.link, plugin))
61
+ errs.push("link_invalid");
62
+ }
63
+ if (p.dedupeKey !== undefined) {
64
+ if (blank(p.dedupeKey) || p.dedupeKey.length > INBOX_LIMITS.dedupeKeyChars)
65
+ errs.push("dedupe_key_invalid");
66
+ }
67
+ if (p.expiresInSeconds !== undefined) {
68
+ const s = p.expiresInSeconds;
69
+ if (typeof s !== "number" || !Number.isInteger(s) || s < 1 || s > INBOX_LIMITS.maxExpirySeconds)
70
+ errs.push("expiry_invalid");
71
+ }
72
+ if (p.actions !== undefined) {
73
+ if (!Array.isArray(p.actions))
74
+ errs.push("action_invalid");
75
+ else {
76
+ if (p.actions.length > INBOX_ACTIONS_MAX)
77
+ errs.push("too_many_actions");
78
+ const ids = new Set();
79
+ for (const a of p.actions) {
80
+ const problem = actionProblem(a);
81
+ if (problem !== null) {
82
+ errs.push(problem);
83
+ continue;
84
+ }
85
+ if (ids.has(a.id))
86
+ errs.push("action_duplicate");
87
+ ids.add(a.id);
88
+ }
89
+ }
90
+ }
91
+ return [...new Set(errs)];
92
+ }
93
+ const idOk = (v) => typeof v === "string" && v.length <= INBOX_LIMITS.idChars && INBOX_ID_RE.test(v);
94
+ const labelOk = (v) => typeof v === "string" && v.trim() !== "" && v.length <= INBOX_LIMITS.labelChars;
95
+ function actionProblem(a) {
96
+ if (typeof a !== "object" || a === null)
97
+ return "action_invalid";
98
+ const x = a;
99
+ if (!idOk(x.id))
100
+ return "action_invalid";
101
+ if (typeof x.label !== "object" || x.label === null)
102
+ return "action_invalid";
103
+ if (!labelOk(x.label.zh) || !labelOk(x.label.en))
104
+ return "action_invalid";
105
+ if (x.style !== undefined && x.style !== "primary" && x.style !== "danger")
106
+ return "action_invalid";
107
+ const u = x.uiAction;
108
+ if (typeof u !== "object" || u === null)
109
+ return "action_invalid";
110
+ if (!idOk(u.contribution) || !idOk(u.actionId))
111
+ return "action_invalid";
112
+ if (u.payload !== undefined) {
113
+ let json;
114
+ try {
115
+ json = JSON.stringify(u.payload);
116
+ }
117
+ catch {
118
+ return "action_invalid";
119
+ }
120
+ if (json === undefined)
121
+ return "action_invalid";
122
+ if (new TextEncoder().encode(json).length > INBOX_LIMITS.payloadBytes)
123
+ return "payload_too_large";
124
+ }
125
+ return null;
126
+ }
127
+ /**
128
+ * Whether `link` is a path of the extension's own pages: `/p/<plugin>/` followed by at least one
129
+ * character, with no backslash or control character, no dot segment (`.`, `..`, also spelled with
130
+ * `%2e` in any case), and a path that still starts with `/p/<plugin>/` once a URL parser resolved
131
+ * it.
132
+ */
133
+ function ownPageLink(link, plugin) {
134
+ const prefix = `/p/${plugin}/`;
135
+ if (!link.startsWith(prefix) || link.length === prefix.length)
136
+ return false;
137
+ if (/[\\\p{Cc}]/u.test(link))
138
+ return false;
139
+ const path = link.split(/[?#]/, 1)[0] ?? "";
140
+ const dot = (s) => {
141
+ const d = s.replace(/%2e/gi, ".");
142
+ return d === "." || d === "..";
143
+ };
144
+ if (path.split("/").some(dot))
145
+ return false;
146
+ try {
147
+ return new URL(link, "http://x").pathname.startsWith(prefix);
148
+ }
149
+ catch {
150
+ return false;
151
+ }
152
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export declare const SDK_VERSION = "0.1.0-rc.86";
1
+ export declare const SDK_VERSION = "0.1.0-rc.88";
2
2
  export * from "./agent-template.js";
3
+ export * from "./agents-ctx.js";
3
4
  export * from "./avatar-presets.js";
4
5
  export * from "./bootstrap.js";
5
6
  export * from "./cards.js";
@@ -25,6 +26,7 @@ export * from "./git-hosts.js";
25
26
  export * from "./hooks.js";
26
27
  export * from "./http-access.js";
27
28
  export * from "./identity.js";
29
+ export * from "./inbox.js";
28
30
  export * from "./marketplace.js";
29
31
  export * from "./model.js";
30
32
  export * from "./model-params.js";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- export const SDK_VERSION = "0.1.0-rc.86";
1
+ export const SDK_VERSION = "0.1.0-rc.88";
2
2
  export * from "./agent-template.js";
3
+ export * from "./agents-ctx.js";
3
4
  export * from "./avatar-presets.js";
4
5
  export * from "./bootstrap.js";
5
6
  export * from "./cards.js";
@@ -25,6 +26,7 @@ export * from "./git-hosts.js";
25
26
  export * from "./hooks.js";
26
27
  export * from "./http-access.js";
27
28
  export * from "./identity.js";
29
+ export * from "./inbox.js";
28
30
  export * from "./marketplace.js";
29
31
  export * from "./model.js";
30
32
  export * from "./model-params.js";
@@ -83,11 +83,32 @@ export interface ExtensionPermissions {
83
83
  triggers?: {
84
84
  emit: string[];
85
85
  };
86
+ /**
87
+ * `ctx.inbox`: personal inbox items for the members of orgs that enabled the extension, each
88
+ * linking only to the extension's own pages, with buttons that call its UI actions as the
89
+ * person who clicks.
90
+ */
91
+ inbox?: boolean;
92
+ /** `ctx.agents`: which agents a member of an org that enabled the extension may use. */
93
+ agents?: {
94
+ read: true;
95
+ };
96
+ /**
97
+ * `ctx.conversations`: the conversations serving the extension's own subjects (metadata only,
98
+ * never content).
99
+ */
100
+ conversations?: {
101
+ subjects: true;
102
+ };
103
+ /** `ctx.people`: names and avatars of an org's members (never email or other profile fields). */
104
+ people?: {
105
+ read: true;
106
+ };
86
107
  }
87
108
  /** The most services `permissions.deviceAccess.services` may list. */
88
109
  export declare const DEVICE_ACCESS_MAX_SERVICES = 4;
89
110
  /** Hook mount points (a closed list, see the overall design 2.4). */
90
- export declare const HOOK_MOUNT_POINTS: readonly ["session.start", "session.end", "agent.preStep", "model.request", "model.response", "tool.preExecute", "tool.postExecute", "usage.report", "org.member_removed", "agent.deleted", "org.deleted"];
111
+ export declare const HOOK_MOUNT_POINTS: readonly ["session.start", "session.end", "agent.preStep", "model.request", "model.response", "tool.preExecute", "tool.postExecute", "usage.report", "org.member_removed", "agent.deleted", "org.deleted", "run.lifecycle"];
91
112
  /** A hook mount point name. */
92
113
  export type HookMountPoint = (typeof HOOK_MOUNT_POINTS)[number];
93
114
  /** A platform API not declared in the manifest was used. */
@@ -21,6 +21,7 @@ export const HOOK_MOUNT_POINTS = [
21
21
  "org.member_removed",
22
22
  "agent.deleted",
23
23
  "org.deleted",
24
+ "run.lifecycle",
24
25
  ];
25
26
  const HOOKS = new Set(HOOK_MOUNT_POINTS);
26
27
  /** A platform API not declared in the manifest was used. */
@@ -52,6 +53,10 @@ const KEYS = new Set([
52
53
  "dialIn",
53
54
  "deviceAccess",
54
55
  "triggers",
56
+ "inbox",
57
+ "agents",
58
+ "conversations",
59
+ "people",
55
60
  ]);
56
61
  const POLICY = new Set(POLICY_HOOKS);
57
62
  const METHODS = new Set(["GET", "POST", "PUT", "DELETE"]);
@@ -87,11 +92,21 @@ export function validatePermissions(plugin, p, provides = []) {
87
92
  if (!KEYS.has(k))
88
93
  errs.push(`unknown permissions key "${k}"`);
89
94
  }
90
- for (const k of ["secrets", "storage", "client"]) {
95
+ for (const k of ["secrets", "storage", "client", "inbox"]) {
91
96
  if (p[k] !== undefined && typeof p[k] !== "boolean") {
92
97
  errs.push(`permissions.${k} must be a boolean`);
93
98
  }
94
99
  }
100
+ for (const [k, flag] of [
101
+ ["agents", "read"],
102
+ ["conversations", "subjects"],
103
+ ["people", "read"],
104
+ ]) {
105
+ const v = p[k];
106
+ if (v !== undefined &&
107
+ (!isObj(v) || Object.keys(v).some((x) => x !== flag) || v[flag] !== true))
108
+ errs.push(`permissions.${k} must be { ${flag}: true }`);
109
+ }
95
110
  if (p.tools !== undefined)
96
111
  strList("permissions.tools", p.tools, errs);
97
112
  if (p.hooks !== undefined) {
@@ -8,9 +8,15 @@ export interface TriggerSubject {
8
8
  title: string;
9
9
  url?: string;
10
10
  }
11
- /** Who caused an event: a tool call of the extension, or an account acting outside a call. */
11
+ /**
12
+ * Who caused an event: a tool call of the extension (`call`, the context the kernel passed to the
13
+ * handler), a handle the kernel made from such a call ({@link TriggersCtx.causeOf}; for an event
14
+ * emitted later, from a queue job), or an account acting outside a call.
15
+ */
12
16
  export type TriggerCause = {
13
17
  call: ToolCallContext;
18
+ } | {
19
+ handle: string;
14
20
  } | {
15
21
  accountId: string;
16
22
  };
@@ -45,6 +51,16 @@ export interface TriggersCtx {
45
51
  eventId: string;
46
52
  duplicate: boolean;
47
53
  }>;
54
+ /**
55
+ * A serializable cause for an event emitted after the tool call returned: an opaque handle for
56
+ * `call` (the context the kernel passed to the handler) that the kernel binds to the call's org
57
+ * and run and to this extension, valid for 24 hours. Pass it in the `ctx.queue` job's payload
58
+ * and emit with `cause: { handle }`: the event keeps the call's chain depth and self-trigger
59
+ * check. One handle may be used for several events of that org. Throws `TriggerError`
60
+ * (`invalid_event`) for a context the kernel did not make; `emit` refuses an expired handle, or
61
+ * one of another org or extension, the same way.
62
+ */
63
+ causeOf(call: ToolCallContext): Promise<string>;
48
64
  }
49
65
  export type TriggerErrorCode = "type_not_declared" | "invalid_event" | "org_disabled";
50
66
  /** A {@link TriggersCtx} call was refused. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.86",
3
+ "version": "0.1.0-rc.88",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "files": [
30
30
  "dist",
31
+ "api",
31
32
  "schemas",
32
33
  "agent-docs",
33
34
  "README.md",