@capybara-hq/plugin-sdk 0.1.0-rc.1 → 0.1.0-rc.11

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.
Files changed (97) hide show
  1. package/README.md +1 -1
  2. package/agent-docs/agent-templates.md +47 -0
  3. package/agent-docs/concepts.md +63 -0
  4. package/agent-docs/connector-templates.md +48 -0
  5. package/agent-docs/extension-points/channel.md +15 -0
  6. package/agent-docs/extension-points/config-source.md +15 -0
  7. package/agent-docs/extension-points/connector.md +15 -0
  8. package/agent-docs/extension-points/counter.md +15 -0
  9. package/agent-docs/extension-points/directory.md +15 -0
  10. package/agent-docs/extension-points/file-preview.md +15 -0
  11. package/agent-docs/extension-points/identity-source.md +15 -0
  12. package/agent-docs/extension-points/model-provider.md +15 -0
  13. package/agent-docs/extension-points/object-store.md +15 -0
  14. package/agent-docs/extension-points/sandbox.md +15 -0
  15. package/agent-docs/extension-points/secret-store.md +15 -0
  16. package/agent-docs/extension-points/web-fetch.md +15 -0
  17. package/agent-docs/extension-points/web-search.md +15 -0
  18. package/agent-docs/extensions.md +87 -0
  19. package/agent-docs/plugin-format.md +100 -0
  20. package/agent-docs/setup.md +46 -0
  21. package/agent-docs/skills.md +44 -0
  22. package/agent-docs/tool-naming.md +21 -0
  23. package/dist/agent-template.d.ts +85 -0
  24. package/dist/agent-template.js +361 -2
  25. package/dist/cards.d.ts +7 -0
  26. package/dist/cards.js +4 -4
  27. package/dist/channel.d.ts +20 -1
  28. package/dist/client-css.d.ts +36 -0
  29. package/dist/client-css.js +326 -0
  30. package/dist/client-section.d.ts +35 -0
  31. package/dist/client-section.js +133 -0
  32. package/dist/config-ui.d.ts +131 -0
  33. package/dist/config-ui.js +602 -0
  34. package/dist/connector-auth.d.ts +2 -0
  35. package/dist/connector-auth.js +15 -0
  36. package/dist/ctx.d.ts +86 -4
  37. package/dist/define-extension.d.ts +5 -1
  38. package/dist/define-extension.js +1 -0
  39. package/dist/device-protocol.d.ts +118 -0
  40. package/dist/device-protocol.js +276 -0
  41. package/dist/directory.d.ts +11 -2
  42. package/dist/extension-point.d.ts +9 -0
  43. package/dist/extension-point.js +17 -12
  44. package/dist/file-preview.d.ts +9 -1
  45. package/dist/hooks.d.ts +76 -16
  46. package/dist/index.d.ts +18 -1
  47. package/dist/index.js +18 -1
  48. package/dist/marketplace.d.ts +55 -2
  49. package/dist/marketplace.js +73 -3
  50. package/dist/model.d.ts +60 -2
  51. package/dist/model.js +22 -0
  52. package/dist/org-access.d.ts +27 -0
  53. package/dist/org-access.js +35 -0
  54. package/dist/permissions.d.ts +32 -7
  55. package/dist/permissions.js +86 -22
  56. package/dist/plugin-manifest.d.ts +70 -5
  57. package/dist/plugin-manifest.js +186 -6
  58. package/dist/sandbox.d.ts +164 -62
  59. package/dist/sandbox.js +71 -14
  60. package/dist/secret.d.ts +4 -1
  61. package/dist/secret.js +5 -2
  62. package/dist/setup-check.d.ts +115 -0
  63. package/dist/setup-check.js +142 -0
  64. package/dist/setup-guide-parse.d.ts +121 -0
  65. package/dist/setup-guide-parse.js +350 -0
  66. package/dist/setup-guide-render.d.ts +45 -0
  67. package/dist/setup-guide-render.js +351 -0
  68. package/dist/setup-guide.d.ts +94 -0
  69. package/dist/setup-guide.js +336 -0
  70. package/dist/skill-invocation.d.ts +9 -5
  71. package/dist/skill-invocation.js +18 -8
  72. package/dist/skill.d.ts +23 -3
  73. package/dist/skill.js +31 -6
  74. package/dist/theme-assets.d.ts +39 -0
  75. package/dist/theme-assets.js +449 -0
  76. package/dist/theme-readability.d.ts +37 -0
  77. package/dist/theme-readability.js +145 -0
  78. package/dist/theme-tokens.generated.d.ts +275 -0
  79. package/dist/theme-tokens.generated.js +167 -0
  80. package/dist/theme.d.ts +152 -0
  81. package/dist/theme.js +392 -0
  82. package/dist/tool.d.ts +11 -3
  83. package/dist/ui-parts.d.ts +23 -0
  84. package/dist/ui-parts.js +238 -0
  85. package/dist/ui-resolve.d.ts +40 -0
  86. package/dist/ui-resolve.js +54 -0
  87. package/dist/ui-slots.d.ts +55 -0
  88. package/dist/ui-slots.js +247 -0
  89. package/dist/ui.d.ts +143 -37
  90. package/dist/ui.js +65 -5
  91. package/dist/web.d.ts +105 -0
  92. package/dist/web.js +135 -0
  93. package/package.json +10 -2
  94. package/schemas/connector-template.v1.schema.json +3 -0
  95. package/schemas/marketplace.v1.schema.json +50 -0
  96. package/schemas/plugin.v1.schema.json +164 -4
  97. package/schemas/theme.v1.schema.json +225 -0
package/dist/ui.d.ts CHANGED
@@ -1,18 +1,13 @@
1
1
  import type { Locale } from "./cards.js";
2
2
  import { type FormAnswer, type InputForm } from "./tool.js";
3
+ import type { UiSlotId, UiSlotScope } from "./ui-slots.js";
3
4
  /**
4
- * Where a ui contribution goes (UI refresh spec §5.3):
5
- * - `panel`: a right panel of the conversation page, opened from the extension's icon in the title
6
- * bar (one at a time);
7
- * - `section`: a section of the conversation's properties panel;
8
- * - `inline`: an inline card in the conversation, hanging off a tool result (`ToolResult.card`);
9
- * - `page`: an org page under "Extensions";
10
- * - `file-preview`: an embed previewer of `file-preview@1` (embed only, see `FilePreviewer.embed`);
11
- * - `conversation`: the old right-side tab; it means `panel` (see {@link normalizePlace}).
5
+ * The places of the first UI design, kept as a deprecated input (UI slots spec §4, step A): each maps
6
+ * to a slot through `PLACE_SLOTS` (`panel` and `conversation` → `conversation.panel`, `section` →
7
+ * `conversation.properties.section`, `inline` → `conversation.tool-result.card`, `page` →
8
+ * `org.page`, `file-preview` → `file.preview`).
12
9
  */
13
10
  export type UiPlace = "conversation" | "panel" | "section" | "inline" | "page" | "file-preview";
14
- /** A place with the old `conversation` mapped to `panel`. */
15
- export declare function normalizePlace(place: UiPlace): Exclude<UiPlace, "conversation">;
16
11
  /**
17
12
  * Icons a contribution may use (design system icon names; `puzzle` is the default and is drawn as
18
13
  * the design `extension` icon). The title-bar button of a panel shows it.
@@ -20,18 +15,25 @@ export declare function normalizePlace(place: UiPlace): Exclude<UiPlace, "conver
20
15
  export declare const UI_ICONS: readonly ["list-checks", "layout-grid", "file", "chart", "calendar", "users", "bot", "book", "message-square", "puzzle"];
21
16
  export type UiIcon = (typeof UI_ICONS)[number];
22
17
  /**
23
- * An "open" target: a `panel` contribution of the same extension, scrolled to the block or item
24
- * whose `id` equals `item` (the panel's render receives it as {@link UiRenderArgs.item}).
18
+ * An "open" target: a `conversation.panel` contribution of the same extension, scrolled to the
19
+ * block or item whose `id` equals `item` (the panel's render receives it as
20
+ * {@link UiRenderArgs.item}).
25
21
  */
26
22
  export interface UiOpen {
27
23
  contribution: string;
28
24
  item?: string;
29
25
  }
30
- /** 界面贡献的种类:`view` = 渲染 schema(由我们的组件画);`embed` = 插件自己的页面放进 iframe。 */
31
- export type UiKind = "view" | "embed";
32
26
  /**
33
- * 插件清单 `ui` 里的一项界面贡献。`id` 必须同时出现在 `permissions.ui.views`(kind view)
34
- * 或 `permissions.ui.embeds`(kind embed)里;`embed` 的页面由插件路由提供。
27
+ * The kind of a UI contribution: `view` = a render schema drawn by the host's components; `embed` =
28
+ * the extension's own page in an iframe; `component` = a React component from the extension's
29
+ * client code (client extensions spec §5).
30
+ */
31
+ export type UiKind = "view" | "embed" | "component";
32
+ /**
33
+ * One UI contribution of the extension manifest's `ui`. Its `id` must be listed in the list of its
34
+ * kind: `permissions.ui.views` (view), `permissions.ui.embeds` (embed) or
35
+ * `permissions.ui.components` (component, which also needs `permissions.client: true`); an embed's
36
+ * page is served by an extension route.
35
37
  */
36
38
  export interface UiContribution {
37
39
  id: string;
@@ -39,9 +41,21 @@ export interface UiContribution {
39
41
  zh: string;
40
42
  en: string;
41
43
  };
42
- place: UiPlace;
44
+ /**
45
+ * The slot it goes in: `"<id>"` (version 1) or `"<id>@<n>"`, an id of `UI_SLOTS`. Exactly one of
46
+ * `slot` and `place`. An unknown slot or version does not fail the extension: the contribution is
47
+ * dropped (see `resolveUiSlots`).
48
+ */
49
+ slot?: string;
50
+ /** @deprecated use slot (UI slots spec §4, step A) */
51
+ place?: UiPlace;
52
+ /**
53
+ * One of the slot's `anchors` to be placed after; omitted (or not an anchor of the slot) = the
54
+ * slot's first anchor.
55
+ */
56
+ after?: string;
43
57
  kind: UiKind;
44
- /** 仅 embed:插件路由下 `/ui/` 开头的路径,须在 `permissions.http` 中声明为 GET。 */
58
+ /** Embed only: a path under `/ui/` of the extension's routes, declared in `permissions.http` as GET. */
45
59
  path?: string;
46
60
  /** Icon from {@link UI_ICONS}; default `puzzle`. */
47
61
  icon?: UiIcon;
@@ -121,12 +135,22 @@ export type UiBlock = {
121
135
  actionId: string;
122
136
  submitLabel: string;
123
137
  };
138
+ /** A JSON value: what survives `JSON.stringify` and `JSON.parse` unchanged. */
139
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {
140
+ [key: string]: JsonValue;
141
+ };
124
142
  /** 插件视图:有上限的块列表;`badge` 显示在标签上(如「2/4」)。 */
125
143
  export interface UiView {
126
144
  badge?: string;
127
145
  /** The ⤢ of an inline card's title row and a properties section's header. */
128
146
  open?: UiOpen;
147
+ /** A component's view may have no blocks (`[]`). */
129
148
  blocks: UiBlock[];
149
+ /**
150
+ * Only from a component's render (`useView` hands it to the component): at most
151
+ * `UI_LIMITS.componentDataBytes` bytes of JSON. Refused in the views of the other kinds.
152
+ */
153
+ data?: JsonValue;
130
154
  }
131
155
  /**
132
156
  * 视图上限({@link validateUiView} 统一检查):
@@ -135,6 +159,7 @@ export interface UiView {
135
159
  * - `checklistItems`:清单项数;
136
160
  * - `textChars`:整个视图所有文本(含表单 JSON)的字符总数;
137
161
  * - `payloadBytes`:单个按钮 `payload` 的 JSON 序列化后 UTF-8 字节数。
162
+ * - `componentDataBytes`: the UTF-8 bytes of a component view's `data` as JSON.
138
163
  */
139
164
  export declare const UI_LIMITS: {
140
165
  readonly blocks: 50;
@@ -142,28 +167,55 @@ export declare const UI_LIMITS: {
142
167
  readonly checklistItems: 100;
143
168
  readonly textChars: 20000;
144
169
  readonly payloadBytes: 2000;
170
+ readonly componentDataBytes: 32768;
145
171
  };
146
172
  /**
147
173
  * 校验插件返回的视图,返回问题列表(空 = 合法)。链接与图片只允许 http(s) 绝对地址;
148
174
  * Markdown 的检查是纵深防御,网页渲染时仍须只认 http(s) 链接、不渲染原生 HTML。
149
175
  * 按钮与表单的 `actionId` 在整个视图内唯一(动作回调靠它区分来源)。
176
+ * `data` is accepted only with `opts.component` (the view of a component contribution): JSON as is
177
+ * (nothing JSON would drop or rewrite, see `jsonProblem`), up to `UI_LIMITS.componentDataBytes`
178
+ * bytes of UTF-8.
150
179
  */
151
- export declare function validateUiView(v: unknown): string[];
180
+ export declare function validateUiView(v: unknown, opts?: {
181
+ component?: boolean;
182
+ }): string[];
152
183
  /**
153
- * 调用者在组织内的角色(由高到低 owner / admin / member / guest)。`place: "page"` 的贡献只给 member 以上
154
- * (内核对 guest 返回 404,插件收不到 guest 的组织页面请求);对话位置的贡献给能访问该对话的任何角色。
184
+ * The caller's role in the org (owner / admin / member / guest, highest first). A slot's `minRole`
185
+ * decides who may use its contributions, then its scope's rule (the kernel answers 404 to anyone
186
+ * else, so the extension never sees such a request): `org.page` member and up,
187
+ * `org.settings.section` admin and up; conversation slots any role that can read the
188
+ * conversation; `agent.detail.section` any role that can read the agent (read-only viewers
189
+ * included: tighten on `role` if you must).
155
190
  */
156
191
  export type UiRole = "owner" | "admin" | "member" | "guest";
157
- /** 内核调用插件 `render` 时传入的参数(身份由内核鉴权后给出,插件拿不到用户的会话凭据)。 */
158
- export interface UiRenderArgs {
159
- orgId: string;
192
+ /** The render args every scope shares (identity comes from the kernel's auth, never the caller's credentials). */
193
+ export interface UiRenderArgsBase {
160
194
  accountId: string;
195
+ /** The catalog slot the contribution resolved to. */
196
+ slot: UiSlotId;
197
+ locale: Locale;
198
+ signal: AbortSignal;
199
+ /**
200
+ * The extension's effective deployment config: for an org, the platform's deployment config
201
+ * with the org's copy applied to the overridable fields (`orgAccess(…, "override")`), or the
202
+ * platform's config (equal to `ctx.config`) while the org follows it; for the platform scope,
203
+ * the platform's config. Use it, not `ctx.config`, for work done for this org. Optional in the
204
+ * type so older extensions compile; the kernel always passes it.
205
+ */
206
+ config?: unknown;
207
+ }
208
+ /** 内核调用插件 `render` 时传入的参数,组织内的位置(会话、智能体、组织、文件)。 */
209
+ export interface UiOrgRenderArgs extends UiRenderArgsBase {
210
+ /** The scope of {@link slot}: which of the context ids below are set. */
211
+ scope: Exclude<UiSlotScope, "platform">;
212
+ orgId: string;
161
213
  /** 调用者此刻在 `orgId` 的角色(内核按成员关系填,插件可据此收紧按钮等,见 {@link UiRole})。 */
162
214
  role: UiRole;
163
215
  /** 仅对话视图。 */
164
216
  conversationId?: string;
165
- locale: Locale;
166
- signal: AbortSignal;
217
+ /** Only an agent view (`agent.detail.section`): an agent of `orgId` the caller can read. */
218
+ agentId?: string;
167
219
  /** A panel opened from an open target: the block or item `id` to scroll to. */
168
220
  item?: string;
169
221
  /**
@@ -176,12 +228,35 @@ export interface UiRenderArgs {
176
228
  key?: string;
177
229
  };
178
230
  }
179
- /** 视图里的按钮或表单提交:`actionId` 来自块;表单提交时带 `answer`。 */
180
- export interface UiActionArgs extends UiRenderArgs {
231
+ /**
232
+ * The render args of a `platform.page` contribution: rendered for a super admin in the admin
233
+ * console, with no org (and so no org role, conversation or agent).
234
+ */
235
+ export interface UiPlatformRenderArgs extends UiRenderArgsBase {
236
+ scope: "platform";
237
+ orgId?: undefined;
238
+ role: "superAdmin";
239
+ conversationId?: undefined;
240
+ agentId?: undefined;
241
+ item?: undefined;
242
+ card?: undefined;
243
+ }
244
+ /** 内核调用插件 `render` 时传入的参数:narrow on `scope` (`"platform"` has no `orgId`). */
245
+ export type UiRenderArgs = UiOrgRenderArgs | UiPlatformRenderArgs;
246
+ /** What a button or form submit adds to the render args. */
247
+ export interface UiActionInput {
181
248
  actionId: string;
182
249
  payload?: unknown;
183
250
  answer?: FormAnswer;
184
251
  }
252
+ /** An action of an org-scope view. */
253
+ export interface UiOrgActionArgs extends UiOrgRenderArgs, UiActionInput {
254
+ }
255
+ /** An action of a `platform.page` view. */
256
+ export interface UiPlatformActionArgs extends UiPlatformRenderArgs, UiActionInput {
257
+ }
258
+ /** 视图里的按钮或表单提交:`actionId` 来自块;表单提交时带 `answer`。 */
259
+ export type UiActionArgs = UiOrgActionArgs | UiPlatformActionArgs;
185
260
  /** 动作结果:一条提示,或一份新的视图。 */
186
261
  export type UiActionResult = {
187
262
  toast?: {
@@ -191,21 +266,55 @@ export type UiActionResult = {
191
266
  } | {
192
267
  view: UiView;
193
268
  };
194
- /** 嵌入令牌里的声明(经 `ctx.ui.verifyEmbedToken` 校验后可信)。`exp` 为 Unix 秒。 */
195
- export interface EmbedClaims {
269
+ /** The embed claims both scopes share. `exp` 为 Unix 秒。 */
270
+ export interface EmbedClaimsBase {
196
271
  plugin: string;
197
- orgId: string;
198
272
  accountId: string;
273
+ contribution: string;
274
+ exp: number;
275
+ }
276
+ /** An org-scope embed token's claims (every slot but `platform.page`). */
277
+ export interface OrgEmbedClaims extends EmbedClaimsBase {
278
+ scope: "org";
279
+ orgId: string;
199
280
  /** 调用者的角色:签发时写入;`ctx.ui.verifyEmbedToken` 返回校验时刻的当前角色。 */
200
281
  role: UiRole;
201
282
  conversationId?: string;
283
+ /** Only an agent embed's token (`agent.detail.section`): the agent it was issued for. */
284
+ agentId?: string;
202
285
  /** Only a `file-preview` embed's token: the one file (of `conversationId`) it previews. */
203
286
  fileId?: string;
204
- contribution: string;
205
- exp: number;
206
287
  }
288
+ /** A `platform.page` embed token's claims: a super admin's, with no org, conversation, agent or file. */
289
+ export interface PlatformEmbedClaims extends EmbedClaimsBase {
290
+ scope: "platform";
291
+ orgId?: undefined;
292
+ role: "superAdmin";
293
+ conversationId?: undefined;
294
+ agentId?: undefined;
295
+ fileId?: undefined;
296
+ }
297
+ /**
298
+ * 嵌入令牌里的声明(经 `ctx.ui.verifyEmbedToken` 校验后可信): narrow on `scope` (`"platform"` has no
299
+ * `orgId` and `role` `"superAdmin"`).
300
+ */
301
+ export type EmbedClaims = OrgEmbedClaims | PlatformEmbedClaims;
207
302
  /** 外层页面与嵌入 iframe 之间 `postMessage` 协议的版本标识。 */
208
303
  export declare const EMBED_PROTOCOL = "capybara-embed/1";
304
+ /**
305
+ * What an embed's `init` says about where it is shown, narrowed on `scope` like {@link EmbedClaims}:
306
+ * an org's page, panel, section or previewer (`orgId`, and the conversation when there is one), or
307
+ * a platform page of the admin console (no org).
308
+ */
309
+ export type EmbedInitContext = {
310
+ scope: "org";
311
+ orgId: string;
312
+ conversationId?: string;
313
+ } | {
314
+ scope: "platform";
315
+ orgId?: undefined;
316
+ conversationId?: undefined;
317
+ };
209
318
  /** 外层页面发给 iframe 的消息。 */
210
319
  export type EmbedParentMessage = {
211
320
  protocol: typeof EMBED_PROTOCOL;
@@ -214,10 +323,7 @@ export type EmbedParentMessage = {
214
323
  locale: Locale;
215
324
  theme: "dark" | "light";
216
325
  tokens: Record<string, string>;
217
- context: {
218
- orgId: string;
219
- conversationId?: string;
220
- };
326
+ context: EmbedInitContext;
221
327
  } | {
222
328
  protocol: typeof EMBED_PROTOCOL;
223
329
  type: "token";
package/dist/ui.js CHANGED
@@ -1,9 +1,5 @@
1
1
  import { validateInputForm } from "./tool.js";
2
2
  import { isHttpUrl } from "./url.js";
3
- /** A place with the old `conversation` mapped to `panel`. */
4
- export function normalizePlace(place) {
5
- return place === "conversation" ? "panel" : place;
6
- }
7
3
  /**
8
4
  * Icons a contribution may use (design system icon names; `puzzle` is the default and is drawn as
9
5
  * the design `extension` icon). The title-bar button of a panel shows it.
@@ -27,6 +23,7 @@ export const UI_ICONS = [
27
23
  * - `checklistItems`:清单项数;
28
24
  * - `textChars`:整个视图所有文本(含表单 JSON)的字符总数;
29
25
  * - `payloadBytes`:单个按钮 `payload` 的 JSON 序列化后 UTF-8 字节数。
26
+ * - `componentDataBytes`: the UTF-8 bytes of a component view's `data` as JSON.
30
27
  */
31
28
  export const UI_LIMITS = {
32
29
  blocks: 50,
@@ -34,6 +31,7 @@ export const UI_LIMITS = {
34
31
  checklistItems: 100,
35
32
  textChars: 20000,
36
33
  payloadBytes: 2000,
34
+ componentDataBytes: 32768,
37
35
  };
38
36
  const BADGE_MAX = 32;
39
37
  /** Longest item / block `id` and open target `item`. */
@@ -65,15 +63,77 @@ function jsonBytes(v) {
65
63
  return undefined;
66
64
  }
67
65
  }
66
+ const JSON_KINDS = "null, booleans, finite numbers, strings, arrays, plain objects";
67
+ /**
68
+ * The first part of `v` (`at` is its path) that JSON would not carry as is, or undefined: a value
69
+ * `JSON.stringify` drops, rewrites or throws on (a function, symbol or bigint, NaN or ±Infinity,
70
+ * `undefined` in an array, an object that is not plain such as a Map or a Date, a cycle). A plain
71
+ * object's `undefined` property is accepted: JSON drops it, which reads as an absent optional field.
72
+ */
73
+ function jsonProblem(v, at, seen = new Set()) {
74
+ if (v === null || typeof v === "boolean" || typeof v === "string")
75
+ return undefined;
76
+ if (typeof v === "number")
77
+ return Number.isFinite(v) ? undefined : `${at} is ${v}`;
78
+ if (v === undefined)
79
+ return `${at} is undefined`;
80
+ if (typeof v !== "object")
81
+ return `${at} is a ${typeof v}`;
82
+ if (seen.has(v))
83
+ return `${at} is a cycle`;
84
+ seen.add(v);
85
+ try {
86
+ if (Array.isArray(v)) {
87
+ for (let i = 0; i < v.length; i++) {
88
+ const p = jsonProblem(v[i], `${at}[${i}]`, seen);
89
+ if (p !== undefined)
90
+ return p;
91
+ }
92
+ return undefined;
93
+ }
94
+ const proto = Object.getPrototypeOf(v);
95
+ if (proto !== Object.prototype && proto !== null) {
96
+ return `${at} is a ${proto.constructor?.name || "non-plain object"}`;
97
+ }
98
+ for (const [k, x] of Object.entries(v)) {
99
+ if (x === undefined)
100
+ continue;
101
+ const p = jsonProblem(x, `${at}.${k}`, seen);
102
+ if (p !== undefined)
103
+ return p;
104
+ }
105
+ return undefined;
106
+ }
107
+ finally {
108
+ seen.delete(v);
109
+ }
110
+ }
68
111
  /**
69
112
  * 校验插件返回的视图,返回问题列表(空 = 合法)。链接与图片只允许 http(s) 绝对地址;
70
113
  * Markdown 的检查是纵深防御,网页渲染时仍须只认 http(s) 链接、不渲染原生 HTML。
71
114
  * 按钮与表单的 `actionId` 在整个视图内唯一(动作回调靠它区分来源)。
115
+ * `data` is accepted only with `opts.component` (the view of a component contribution): JSON as is
116
+ * (nothing JSON would drop or rewrite, see `jsonProblem`), up to `UI_LIMITS.componentDataBytes`
117
+ * bytes of UTF-8.
72
118
  */
73
- export function validateUiView(v) {
119
+ export function validateUiView(v, opts = {}) {
74
120
  if (!isObj(v))
75
121
  return ["view must be an object"];
76
122
  const out = [];
123
+ if (v.data !== undefined) {
124
+ if (opts.component !== true)
125
+ out.push("view data is only allowed for component views");
126
+ else {
127
+ const problem = jsonProblem(v.data, "data");
128
+ if (problem !== undefined)
129
+ out.push(`view data must be JSON (${JSON_KINDS}): ${problem}`);
130
+ else {
131
+ const n = jsonBytes(v.data) ?? 0;
132
+ if (n > UI_LIMITS.componentDataBytes)
133
+ out.push(`view data is ${n} bytes; the limit is ${UI_LIMITS.componentDataBytes}`);
134
+ }
135
+ }
136
+ }
77
137
  let chars = 0;
78
138
  const text = (at, s, nonEmpty = false) => {
79
139
  if (typeof s !== "string" || (nonEmpty && s === "")) {
package/dist/web.d.ts ADDED
@@ -0,0 +1,105 @@
1
+ /** Web search extension point ref (`web-search@1`). */
2
+ export declare const WEB_SEARCH_EXTENSION_POINT = "web-search@1";
3
+ /** Web fetch extension point ref (`web-fetch@1`). */
4
+ export declare const WEB_FETCH_EXTENSION_POINT = "web-fetch@1";
5
+ /** One search hit. */
6
+ export interface WebSearchResult {
7
+ title: string;
8
+ /** Absolute http / https URL. */
9
+ url: string;
10
+ /** Plain text or markdown, trimmed by the provider. */
11
+ snippet?: string;
12
+ /** ISO 8601 date or datetime, when known. */
13
+ publishedAt?: string;
14
+ }
15
+ /**
16
+ * A web search provider (`web-search@1`). The kernel passes the org's decrypted `orgConfig` and the
17
+ * extension's effective deployment config for the org (`config`).
18
+ */
19
+ export interface WebSearchProvider {
20
+ /**
21
+ * False when this org's config lacks what the provider needs. `config` is the effective
22
+ * deployment config for the org (see `search`'s `config`).
23
+ */
24
+ configured?(orgConfig: unknown, config?: unknown): boolean;
25
+ search(args: {
26
+ query: string;
27
+ /** Hints only: the kernel filters the results again. */
28
+ allowedDomains?: string[];
29
+ blockedDomains?: string[];
30
+ maxResults: number;
31
+ orgConfig: unknown;
32
+ /**
33
+ * The extension's effective deployment config for this org: the platform's deployment config
34
+ * with the org's copy applied to the overridable fields (`orgAccess(…, "override")`), or the
35
+ * platform's config (equal to `ctx.config`) while the org follows it. Use it, not `ctx.config`,
36
+ * for work done for this org. Optional in the type so older extensions compile; the kernel
37
+ * always passes it.
38
+ */
39
+ config?: unknown;
40
+ signal: AbortSignal;
41
+ }): Promise<{
42
+ results: WebSearchResult[];
43
+ }>;
44
+ }
45
+ /** A fetched page, converted to markdown. */
46
+ export interface WebFetchPage {
47
+ /** After redirects; equal to the requested URL when there were none. */
48
+ finalUrl: string;
49
+ title?: string;
50
+ markdown: string;
51
+ contentType?: string;
52
+ }
53
+ /**
54
+ * A web fetch provider (`web-fetch@1`).
55
+ *
56
+ * The kernel checks the URL it passes and the returned `finalUrl` without DNS: it refuses IP
57
+ * literals in private, loopback, link-local or reserved ranges and single-label or `localhost`
58
+ * names. A provider that fetches from inside the deployment (rather than through a remote
59
+ * service) must also refuse non-public addresses after DNS resolution, for the URL and for every
60
+ * redirect it follows. It may reuse the kernel's rules: {@link isPublicHostname} on
61
+ * `new URL(url).hostname` (never a hand-parsed host), and on each resolved address it connects
62
+ * to (an IPv6 address in brackets, as in a URL).
63
+ */
64
+ export interface WebFetchProvider {
65
+ /**
66
+ * False when this org's config lacks what the provider needs. `config` is the effective
67
+ * deployment config for the org (see `fetch`'s `config`).
68
+ */
69
+ configured?(orgConfig: unknown, config?: unknown): boolean;
70
+ fetch(args: {
71
+ /** Already validated and upgraded by the kernel. */
72
+ url: string;
73
+ orgConfig: unknown;
74
+ /**
75
+ * The extension's effective deployment config for this org: the platform's deployment config
76
+ * with the org's copy applied to the overridable fields (`orgAccess(…, "override")`), or the
77
+ * platform's config (equal to `ctx.config`) while the org follows it. Use it, not `ctx.config`,
78
+ * for work done for this org. Optional in the type so older extensions compile; the kernel
79
+ * always passes it.
80
+ */
81
+ config?: unknown;
82
+ signal: AbortSignal;
83
+ }): Promise<WebFetchPage>;
84
+ }
85
+ /**
86
+ * Why a web provider call failed: `not_found` (404 / no content), `blocked` (the site refused the
87
+ * provider), `auth` (bad credentials), `rate_limited`, `unavailable` (provider down, 5xx, timeout),
88
+ * `invalid` (the provider rejected the query or URL).
89
+ */
90
+ export type WebProviderErrorKind = "not_found" | "blocked" | "auth" | "rate_limited" | "unavailable" | "invalid";
91
+ /** Thrown by web providers; the kernel maps `kind` to fixed, model-visible error text. */
92
+ export declare class WebProviderError extends Error {
93
+ readonly kind: WebProviderErrorKind;
94
+ constructor(message: string, opts: {
95
+ kind: WebProviderErrorKind;
96
+ });
97
+ }
98
+ /**
99
+ * Whether a URL hostname can name a public internet host. Pure, no DNS: false for `localhost`,
100
+ * single-label names, `*.localhost` / `*.local` / `*.internal`, and IP literals (IPv6 in brackets,
101
+ * as in a URL) in private, loopback, link-local, CGNAT, multicast, unspecified, documentation and
102
+ * reserved ranges, including IPv4-mapped IPv6. A name that resolves to a private address is not
103
+ * detected here.
104
+ */
105
+ export declare function isPublicHostname(host: string): boolean;
package/dist/web.js ADDED
@@ -0,0 +1,135 @@
1
+ /** Web search extension point ref (`web-search@1`). */
2
+ export const WEB_SEARCH_EXTENSION_POINT = "web-search@1";
3
+ /** Web fetch extension point ref (`web-fetch@1`). */
4
+ export const WEB_FETCH_EXTENSION_POINT = "web-fetch@1";
5
+ /** Thrown by web providers; the kernel maps `kind` to fixed, model-visible error text. */
6
+ export class WebProviderError extends Error {
7
+ kind;
8
+ constructor(message, opts) {
9
+ super(message);
10
+ this.name = "WebProviderError";
11
+ this.kind = opts.kind;
12
+ }
13
+ }
14
+ function ipv4Octets(host) {
15
+ const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
16
+ if (!m)
17
+ return undefined;
18
+ const o = m.slice(1).map(Number);
19
+ return o.every((n) => n <= 255) ? o : undefined;
20
+ }
21
+ function isPublicIpv4(o) {
22
+ const [a, b, c] = o;
23
+ if (a === 0 || a === 10 || a === 127)
24
+ return false;
25
+ if (a === 100 && b >= 64 && b <= 127)
26
+ return false; // CGNAT
27
+ if (a === 169 && b === 254)
28
+ return false;
29
+ if (a === 172 && b >= 16 && b <= 31)
30
+ return false;
31
+ if (a === 192 && b === 168)
32
+ return false;
33
+ if (a === 192 && b === 0 && (c === 0 || c === 2))
34
+ return false; // IETF protocol, TEST-NET-1
35
+ if (a === 192 && b === 88 && c === 99)
36
+ return false; // 6to4 relay
37
+ if (a === 198 && (b === 18 || b === 19))
38
+ return false; // benchmarking
39
+ if (a === 198 && b === 51 && c === 100)
40
+ return false; // TEST-NET-2
41
+ if (a === 203 && b === 0 && c === 113)
42
+ return false; // TEST-NET-3
43
+ if (a >= 224)
44
+ return false; // multicast, reserved, broadcast
45
+ return true;
46
+ }
47
+ /** Parses an IPv6 literal (no brackets) into eight 16-bit groups, or undefined. */
48
+ function ipv6Groups(host) {
49
+ let s = host;
50
+ const zone = s.indexOf("%");
51
+ if (zone >= 0)
52
+ s = s.slice(0, zone);
53
+ const tail = /(\d+\.\d+\.\d+\.\d+)$/.exec(s);
54
+ if (tail) {
55
+ const o = ipv4Octets(tail[1]);
56
+ if (!o)
57
+ return undefined;
58
+ s = `${s.slice(0, tail.index)}${((o[0] << 8) | o[1]).toString(16)}:${((o[2] << 8) | o[3]).toString(16)}`;
59
+ }
60
+ if (!/^[0-9a-f:]+$/i.test(s) || s.split("::").length > 2)
61
+ return undefined;
62
+ const [head = "", rest] = s.split("::");
63
+ const h = head === "" ? [] : head.split(":");
64
+ const t = rest === undefined || rest === "" ? [] : rest.split(":");
65
+ const parts = [...h, ...t];
66
+ if (!parts.every((p) => /^[0-9a-f]{1,4}$/i.test(p)))
67
+ return undefined;
68
+ let groups;
69
+ if (rest === undefined) {
70
+ if (h.length !== 8)
71
+ return undefined;
72
+ groups = h;
73
+ }
74
+ else {
75
+ if (h.length + t.length > 7)
76
+ return undefined;
77
+ groups = [...h, ...new Array(8 - h.length - t.length).fill("0"), ...t];
78
+ }
79
+ return groups.map((g) => Number.parseInt(g, 16));
80
+ }
81
+ function isPublicIpv6(g) {
82
+ const [g0, g1, g2, g3, g4, g5, g6, g7] = g;
83
+ const zeros5 = g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0 && g4 === 0;
84
+ if (zeros5 && g5 === 0 && g6 === 0 && (g7 === 0 || g7 === 1))
85
+ return false; // :: and ::1
86
+ // IPv4-mapped (::ffff:a.b.c.d) and IPv4-compatible (::a.b.c.d): judge the embedded address.
87
+ if (zeros5 && (g5 === 0xffff || g5 === 0)) {
88
+ return isPublicIpv4([g6 >> 8, g6 & 255, g7 >> 8, g7 & 255]);
89
+ }
90
+ if ((g0 & 0xfe00) === 0xfc00)
91
+ return false; // unique local fc00::/7
92
+ if ((g0 & 0xffc0) === 0xfe80)
93
+ return false; // link-local fe80::/10
94
+ if ((g0 & 0xffc0) === 0xfec0)
95
+ return false; // site-local (deprecated)
96
+ if ((g0 & 0xff00) === 0xff00)
97
+ return false; // multicast
98
+ if (g0 === 0x2001 && g1 === 0x0db8)
99
+ return false; // documentation
100
+ if (g0 === 0x0064 && g1 === 0xff9b)
101
+ return isPublicIpv4([g6 >> 8, g6 & 255, g7 >> 8, g7 & 255]); // NAT64
102
+ if (g0 === 0x2002)
103
+ return isPublicIpv4([g1 >> 8, g1 & 255, g2 >> 8, g2 & 255]); // 6to4
104
+ return true;
105
+ }
106
+ /**
107
+ * Whether a URL hostname can name a public internet host. Pure, no DNS: false for `localhost`,
108
+ * single-label names, `*.localhost` / `*.local` / `*.internal`, and IP literals (IPv6 in brackets,
109
+ * as in a URL) in private, loopback, link-local, CGNAT, multicast, unspecified, documentation and
110
+ * reserved ranges, including IPv4-mapped IPv6. A name that resolves to a private address is not
111
+ * detected here.
112
+ */
113
+ export function isPublicHostname(host) {
114
+ let h = host.trim().toLowerCase();
115
+ if (h.endsWith("."))
116
+ h = h.slice(0, -1);
117
+ if (h === "")
118
+ return false;
119
+ if (h.startsWith("[")) {
120
+ if (!h.endsWith("]"))
121
+ return false;
122
+ const g = ipv6Groups(h.slice(1, -1));
123
+ return g !== undefined && isPublicIpv6(g);
124
+ }
125
+ const o = ipv4Octets(h);
126
+ if (o)
127
+ return isPublicIpv4(o);
128
+ if (/^[\d.]+$/.test(h))
129
+ return false; // malformed or shorthand numeric host
130
+ if (!h.includes("."))
131
+ return false;
132
+ if (h === "localhost" || /\.(localhost|local|internal)$/.test(h))
133
+ return false;
134
+ return true;
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.1",
3
+ "version": "0.1.0-rc.11",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -17,16 +17,24 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "import": "./dist/index.js",
19
19
  "default": "./dist/index.js"
20
- }
20
+ },
21
+ "./package.json": "./package.json"
21
22
  },
22
23
  "files": [
23
24
  "dist",
24
25
  "schemas",
26
+ "agent-docs",
25
27
  "README.md",
26
28
  "NOTICE"
27
29
  ],
28
30
  "dependencies": {
31
+ "@types/mdast": "4.0.4",
29
32
  "kysely": "^0.29.6",
33
+ "mdast-util-from-markdown": "2.1.0",
34
+ "mdast-util-gfm": "3.1.0",
35
+ "mdast-util-to-markdown": "2.2.0",
36
+ "micromark-extension-gfm": "3.0.0",
37
+ "saxes": "6.0.0",
30
38
  "semver": "^7.8.5",
31
39
  "yaml": "^2.9.1",
32
40
  "zod": "^4.6.7"
@@ -36,6 +36,9 @@
36
36
  "type": "string",
37
37
  "maxLength": 512
38
38
  },
39
+ "defaultEnabled": {
40
+ "type": "boolean"
41
+ },
39
42
  "template": {
40
43
  "type": "object",
41
44
  "properties": {