@capybara-hq/plugin-sdk 0.1.0-rc.6 → 0.1.0-rc.60

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 (136) hide show
  1. package/README.md +1 -1
  2. package/agent-docs/agent-templates.md +14 -2
  3. package/agent-docs/concepts.md +14 -5
  4. package/agent-docs/connector-templates.md +7 -1
  5. package/agent-docs/extension-points/channel.md +1 -1
  6. package/agent-docs/extension-points/context.md +15 -0
  7. package/agent-docs/extension-points/device-provider.md +15 -0
  8. package/agent-docs/extension-points/file-preview.md +2 -0
  9. package/agent-docs/extension-points/model-provider.md +3 -1
  10. package/agent-docs/extension-points/object-store.md +2 -0
  11. package/agent-docs/extension-points/push-provider.md +17 -0
  12. package/agent-docs/extensions.md +29 -3
  13. package/agent-docs/plugin-format.md +52 -2
  14. package/agent-docs/setup.md +54 -0
  15. package/dist/agent-template.d.ts +15 -6
  16. package/dist/agent-template.js +32 -13
  17. package/dist/avatar-presets.d.ts +8 -0
  18. package/dist/avatar-presets.js +17 -0
  19. package/dist/bootstrap.d.ts +109 -1
  20. package/dist/bootstrap.js +185 -2
  21. package/dist/cards.d.ts +67 -13
  22. package/dist/cards.js +91 -21
  23. package/dist/channel.d.ts +148 -48
  24. package/dist/channel.js +10 -5
  25. package/dist/client-css.d.ts +36 -0
  26. package/dist/client-css.js +326 -0
  27. package/dist/client-section.d.ts +35 -0
  28. package/dist/client-section.js +133 -0
  29. package/dist/config-ui.d.ts +22 -0
  30. package/dist/config-ui.js +64 -4
  31. package/dist/connector-auth.d.ts +15 -2
  32. package/dist/connector-auth.js +97 -8
  33. package/dist/connector.d.ts +74 -32
  34. package/dist/context.d.ts +40 -0
  35. package/dist/context.js +2 -0
  36. package/dist/ctx.d.ts +132 -29
  37. package/dist/define-extension.d.ts +26 -12
  38. package/dist/define-extension.js +12 -5
  39. package/dist/device-denied-paths.d.ts +18 -0
  40. package/dist/device-denied-paths.js +84 -0
  41. package/dist/device-operation.d.ts +333 -0
  42. package/dist/device-operation.js +312 -0
  43. package/dist/device-protocol.d.ts +285 -50
  44. package/dist/device-protocol.js +755 -90
  45. package/dist/device-provider.d.ts +500 -0
  46. package/dist/device-provider.js +302 -0
  47. package/dist/dial-in.d.ts +176 -0
  48. package/dist/dial-in.js +115 -0
  49. package/dist/extension-point.d.ts +2 -0
  50. package/dist/extension-point.js +6 -2
  51. package/dist/file-preview.d.ts +23 -6
  52. package/dist/git-hosts.d.ts +53 -0
  53. package/dist/git-hosts.js +98 -0
  54. package/dist/hooks.d.ts +125 -20
  55. package/dist/hooks.js +5 -0
  56. package/dist/http-access.d.ts +62 -0
  57. package/dist/http-access.js +12 -0
  58. package/dist/identity.d.ts +38 -9
  59. package/dist/identity.js +21 -1
  60. package/dist/index.d.ts +29 -3
  61. package/dist/index.js +29 -3
  62. package/dist/marketplace.d.ts +26 -0
  63. package/dist/marketplace.js +43 -1
  64. package/dist/model-params.d.ts +41 -22
  65. package/dist/model-params.js +28 -15
  66. package/dist/model.d.ts +206 -71
  67. package/dist/model.js +152 -19
  68. package/dist/net.d.ts +52 -0
  69. package/dist/net.js +34 -0
  70. package/dist/network-classes.d.ts +98 -0
  71. package/dist/network-classes.js +291 -0
  72. package/dist/permissions.d.ts +54 -22
  73. package/dist/permissions.js +119 -43
  74. package/dist/plugin-manifest.d.ts +45 -5
  75. package/dist/plugin-manifest.js +174 -6
  76. package/dist/plugin-package.d.ts +82 -38
  77. package/dist/plugin-package.js +61 -29
  78. package/dist/push.d.ts +75 -0
  79. package/dist/push.js +87 -0
  80. package/dist/sandbox-linux-runtime-source.d.ts +1 -0
  81. package/dist/sandbox-linux-runtime-source.js +2318 -0
  82. package/dist/sandbox-native-runtime-source.d.ts +10 -0
  83. package/dist/sandbox-native-runtime-source.js +476 -0
  84. package/dist/sandbox-runtime-source.d.ts +19 -0
  85. package/dist/sandbox-runtime-source.js +28 -0
  86. package/dist/sandbox-write-denies.d.ts +139 -0
  87. package/dist/sandbox-write-denies.js +869 -0
  88. package/dist/secret.d.ts +4 -1
  89. package/dist/secret.js +4 -1
  90. package/dist/setup-check.d.ts +128 -0
  91. package/dist/setup-check.js +142 -0
  92. package/dist/setup-guide-parse.d.ts +121 -0
  93. package/dist/setup-guide-parse.js +356 -0
  94. package/dist/setup-guide-render.d.ts +45 -0
  95. package/dist/setup-guide-render.js +351 -0
  96. package/dist/setup-guide.d.ts +108 -0
  97. package/dist/setup-guide.js +367 -0
  98. package/dist/shell-capabilities.d.ts +211 -0
  99. package/dist/shell-capabilities.js +44 -0
  100. package/dist/shell-environment.d.ts +57 -0
  101. package/dist/shell-environment.js +21 -0
  102. package/dist/skill.d.ts +27 -17
  103. package/dist/skill.js +40 -26
  104. package/dist/storage.d.ts +52 -25
  105. package/dist/svg-viewbox.d.ts +10 -0
  106. package/dist/svg-viewbox.js +24 -0
  107. package/dist/theme-assets.d.ts +55 -0
  108. package/dist/theme-assets.js +489 -0
  109. package/dist/theme-readability.d.ts +37 -0
  110. package/dist/theme-readability.js +145 -0
  111. package/dist/theme-tokens.generated.d.ts +299 -0
  112. package/dist/theme-tokens.generated.js +184 -0
  113. package/dist/theme.d.ts +190 -0
  114. package/dist/theme.js +471 -0
  115. package/dist/tool.d.ts +99 -26
  116. package/dist/tool.js +29 -9
  117. package/dist/ui-parts.d.ts +23 -0
  118. package/dist/ui-parts.js +385 -0
  119. package/dist/ui-resolve.d.ts +35 -0
  120. package/dist/ui-resolve.js +42 -0
  121. package/dist/ui-slots.d.ts +65 -0
  122. package/dist/ui-slots.js +261 -0
  123. package/dist/ui.d.ts +212 -76
  124. package/dist/ui.js +101 -27
  125. package/dist/url.d.ts +4 -1
  126. package/dist/url.js +4 -1
  127. package/package.json +13 -1
  128. package/schemas/bootstrap-extra.v1.schema.json +186 -0
  129. package/schemas/bootstrap.v1.schema.json +20 -0
  130. package/schemas/connector-template.v1.schema.json +35 -0
  131. package/schemas/marketplace.v1.schema.json +14 -0
  132. package/schemas/plugin.v1.schema.json +122 -3
  133. package/schemas/theme.v1.schema.json +302 -0
  134. package/agent-docs/extension-points/sandbox.md +0 -15
  135. package/dist/sandbox.d.ts +0 -254
  136. package/dist/sandbox.js +0 -153
@@ -0,0 +1,326 @@
1
+ /**
2
+ * The client stylesheet rule (client extensions spec §4.1, §7.2): one function both sides run, so
3
+ * a stylesheet that packs cleanly imports cleanly. `capybara-plugin pack` wraps every file in
4
+ * `@layer extensions { … }` ({@link wrapClientCss}) and checks the wrapped text; the kernel checks
5
+ * the stored bytes again at import, because a tgz can be written by hand. A file must be one
6
+ * `@layer extensions` block, may not `@import`, and a URL may name only an asset of the plugin
7
+ * (`../assets/<path>`, a file of `client.assets`), a `data:` URL or a `#fragment`. Any other URL,
8
+ * relative ones included, is refused: the stylesheet is served from
9
+ * `/api/v1/client/<plugin>/<build>/styles/`, so `../../../../…` would reach the kernel's own
10
+ * routes with the viewer's cookies.
11
+ *
12
+ * Two passes, both must pass:
13
+ * - a plain-text pass over the escape-decoded file (`cssRemoteProblem`);
14
+ * - a tokenizer that follows the CSS Syntax tokenization (comments, strings, escapes, url tokens)
15
+ * closely enough that every brace it skips is one a browser skips too: the file must start with
16
+ * `@layer extensions {` and the block that opens must close at the file's last character, so no
17
+ * rule lands outside the layer. Every `url(…)` (token or string) is checked after decoding
18
+ * escapes, and an `@import` at-keyword is refused however it is spelled.
19
+ *
20
+ * Two rules close the spellings a plain string check would miss:
21
+ * - URLs are trimmed with CSS whitespace only (space, tab, LF, CR, FF), and a file holding any other
22
+ * whitespace, a zero-width character (U+200B-U+200D, U+2060) or U+FEFF anywhere, escaped or not,
23
+ * is refused: JavaScript's `trim()` strips an NBSP that CSS and the URL parser keep, so
24
+ * `url("\a0 data:/../../v1")` would pass as `data:` and resolve to `/api/v1`;
25
+ * - functions that turn a plain string (or a `var()`) into a URL or reach outside the stylesheet are
26
+ * refused outright: `image-set(var(--u) 1x)` takes its URL from a custom property no string check
27
+ * can see. Plugins write `url(../assets/x)` directly.
28
+ */
29
+ /** A stylesheet as the packer stores it: wrapped in `@layer extensions { … }` (spec §4.1). */
30
+ export function wrapClientCss(css) {
31
+ return `@layer extensions {\n${css.trimEnd()}\n}\n`;
32
+ }
33
+ /** CSS whitespace (CSS Syntax §4.2): only these are trimmed from a URL. */
34
+ const CSS_SPACE = "[ \\t\\n\\r\\f]";
35
+ const CSS_TRIM = new RegExp(`^${CSS_SPACE}+|${CSS_SPACE}+$`, "g");
36
+ const cssTrim = (s) => s.replace(CSS_TRIM, "");
37
+ /** Whitespace other than CSS's, zero-width characters and U+FEFF: refused anywhere in a file. */
38
+ const INVISIBLE_RE = /[\u0085\u00a0\u1680\u180e\u2000-\u200d\u2028\u2029\u202f\u205f\u2060\u3000\ufeff]/;
39
+ const INVISIBLE_REFUSED = "the stylesheet contains non-ASCII whitespace, a zero-width character or U+FEFF; use plain spaces";
40
+ /**
41
+ * Functions refused in a client stylesheet (case-insensitive, escapes decoded): they take URLs from
42
+ * plain strings or `var()` (`image-set`, `cross-fade`, `src`, `image`), or reach outside it
43
+ * (`element`, `paint`).
44
+ */
45
+ const REFUSED_FUNCTIONS = new Set([
46
+ "image-set",
47
+ "-webkit-image-set",
48
+ "cross-fade",
49
+ "-webkit-cross-fade",
50
+ "element",
51
+ "-moz-element",
52
+ "paint",
53
+ "src",
54
+ "image",
55
+ ]);
56
+ const REFUSED_FUNCTION_RE = /(?<![A-Za-z0-9_-])(-webkit-image-set|image-set|-webkit-cross-fade|cross-fade|-moz-element|element|paint|src|image)\(/i;
57
+ const functionRefused = (name) => `${name}() is not allowed in a client stylesheet; use url(../assets/<file>) directly`;
58
+ /**
59
+ * The plain-text pass, escapes decoded first: no invisible characters (see the file header), no {@link REFUSED_FUNCTIONS}, no
60
+ * `@import`, and every `url()` is {@link cssUrlProblem}-free. `assets` are the plugin's asset paths
61
+ * relative to `assets/` (`a.png`, `fx/boot.ogg`). Returns the problem, or undefined.
62
+ */
63
+ function cssRemoteProblem(css, assets) {
64
+ const bare = css
65
+ .replace(/\/\*[\s\S]*?\*\//g, "")
66
+ .replace(/\\([0-9a-f]{1,6})[ \t\n]?|\\([^\n])/gi, (_m, hex, ch) => {
67
+ if (hex === undefined)
68
+ return ch ?? "";
69
+ const cp = Number.parseInt(hex, 16);
70
+ return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : "�";
71
+ });
72
+ if (INVISIBLE_RE.test(css) || INVISIBLE_RE.test(bare))
73
+ return INVISIBLE_REFUSED;
74
+ const fn = REFUSED_FUNCTION_RE.exec(bare);
75
+ if (fn !== null)
76
+ return functionRefused(fn[1].toLowerCase());
77
+ if (/@import\b/i.test(bare))
78
+ return IMPORT_REFUSED;
79
+ for (const m of bare.matchAll(/url\([ \t\n\r\f]*(["']?)([^)"']*)\1[ \t\n\r\f]*\)/gi)) {
80
+ const problem = cssUrlProblem(cssTrim(m[2] ?? ""), assets);
81
+ if (problem !== undefined)
82
+ return problem;
83
+ }
84
+ return undefined;
85
+ }
86
+ const IMPORT_REFUSED = "@import is not allowed (a remote stylesheet is not integrity-pinned); inline the rules";
87
+ /** How a stylesheet names one of the plugin's assets (it is served from `styles/`). */
88
+ const ASSETS_URL = "../assets/";
89
+ /**
90
+ * The problem of one URL of a stylesheet (escapes decoded, CSS whitespace trimmed), or undefined
91
+ * when it is a `#fragment`, a `data:` URL or `../assets/<path>` naming one of `assets` exactly.
92
+ */
93
+ function cssUrlProblem(u, assets) {
94
+ if (u.startsWith("#") || /^data:/i.test(u))
95
+ return undefined;
96
+ if (u.startsWith(ASSETS_URL) && assets.has(u.slice(ASSETS_URL.length)))
97
+ return undefined;
98
+ return `url(${u}) is not allowed; use ../assets/<a file of client/assets>, a data: URL or a #fragment`;
99
+ }
100
+ const WRAP_RE = /^@layer extensions \{/;
101
+ const isNewline = (c) => c === "\n" || c === "\r" || c === "\f";
102
+ const isWhitespace = (c) => c === " " || c === "\t" || isNewline(c);
103
+ const isHex = (c) => c !== undefined && /^[0-9a-f]$/i.test(c);
104
+ /** A name code point (CSS Syntax §4.2): letters, digits, `-`, `_`, anything non-ASCII. */
105
+ const isNameChar = (c) => c !== undefined && (/^[A-Za-z0-9_-]$/.test(c) || c.codePointAt(0) >= 0x80);
106
+ class CssProblem extends Error {
107
+ }
108
+ /** Tokenizes `css` from `start`; see the file header. */
109
+ class Scanner {
110
+ css;
111
+ assets;
112
+ i;
113
+ /** Open `{` blocks; the layer block is the first. */
114
+ depth = 1;
115
+ /** Open function / parenthesis names ("" for a plain `(` or `[`), innermost last. */
116
+ fns = [];
117
+ constructor(css, start, assets) {
118
+ this.css = css;
119
+ this.assets = assets;
120
+ this.i = start;
121
+ }
122
+ run() {
123
+ const s = this.css;
124
+ while (this.i < s.length) {
125
+ const c = s[this.i];
126
+ if (c === "/" && s[this.i + 1] === "*") {
127
+ const end = s.indexOf("*/", this.i + 2);
128
+ if (end < 0)
129
+ throw new CssProblem("a comment is not closed");
130
+ this.i = end + 2;
131
+ }
132
+ else if (c === '"' || c === "'") {
133
+ const value = this.string(c);
134
+ if (this.fns[this.fns.length - 1] === "url") {
135
+ const problem = cssUrlProblem(cssTrim(value), this.assets);
136
+ if (problem !== undefined)
137
+ throw new CssProblem(problem);
138
+ }
139
+ }
140
+ else if (c === "@") {
141
+ this.i++;
142
+ if (this.startsName()) {
143
+ if (this.name().toLowerCase() === "import")
144
+ throw new CssProblem(IMPORT_REFUSED);
145
+ }
146
+ }
147
+ else if (c === "#") {
148
+ this.i++;
149
+ if (this.startsName())
150
+ this.name();
151
+ }
152
+ else if (this.startsName()) {
153
+ const name = this.name().toLowerCase();
154
+ if (s[this.i] === "(") {
155
+ this.i++;
156
+ if (REFUSED_FUNCTIONS.has(name))
157
+ throw new CssProblem(functionRefused(name));
158
+ if (name === "url" && !this.quoteAfterWhitespace())
159
+ this.urlToken();
160
+ else
161
+ this.fns.push(name);
162
+ }
163
+ }
164
+ else if (c === "(" || c === "[") {
165
+ this.fns.push("");
166
+ this.i++;
167
+ }
168
+ else if (c === ")" || c === "]") {
169
+ this.fns.pop();
170
+ this.i++;
171
+ }
172
+ else if (c === "{") {
173
+ this.depth++;
174
+ this.i++;
175
+ }
176
+ else if (c === "}") {
177
+ this.depth--;
178
+ this.i++;
179
+ if (this.depth === 0) {
180
+ if (cssTrim(s.slice(this.i)) !== "") {
181
+ throw new CssProblem("a rule is outside @layer extensions; the whole file must be one @layer extensions block");
182
+ }
183
+ return;
184
+ }
185
+ }
186
+ else {
187
+ this.i++;
188
+ }
189
+ }
190
+ throw new CssProblem("the @layer extensions block is not closed");
191
+ }
192
+ /** Whether a name starts here: a name code point or a valid escape. */
193
+ startsName() {
194
+ const s = this.css;
195
+ const c = s[this.i];
196
+ return isNameChar(c) || (c === "\\" && this.i + 1 < s.length && !isNewline(s[this.i + 1]));
197
+ }
198
+ /** A valid escape at `this.i` (the backslash); returns the decoded character. */
199
+ escape() {
200
+ const s = this.css;
201
+ this.i++; // the backslash
202
+ if (isHex(s[this.i])) {
203
+ let hex = "";
204
+ while (hex.length < 6 && isHex(s[this.i]))
205
+ hex += s[this.i++];
206
+ if (s[this.i] === "\r" && s[this.i + 1] === "\n")
207
+ this.i += 2;
208
+ else if (isWhitespace(s[this.i]))
209
+ this.i++;
210
+ const cp = Number.parseInt(hex, 16);
211
+ return cp > 0 && cp <= 0x10ffff && (cp < 0xd800 || cp > 0xdfff)
212
+ ? String.fromCodePoint(cp)
213
+ : "�";
214
+ }
215
+ const cp = s.codePointAt(this.i);
216
+ if (cp === undefined)
217
+ return "�";
218
+ const ch = String.fromCodePoint(cp);
219
+ this.i += ch.length;
220
+ return ch;
221
+ }
222
+ /** Consumes a name (name code points and valid escapes), decoded. */
223
+ name() {
224
+ const s = this.css;
225
+ let out = "";
226
+ while (this.i < s.length) {
227
+ const c = s[this.i];
228
+ if (isNameChar(c)) {
229
+ out += c;
230
+ this.i++;
231
+ }
232
+ else if (c === "\\" && this.i + 1 < s.length && !isNewline(s[this.i + 1])) {
233
+ out += this.escape();
234
+ }
235
+ else
236
+ break;
237
+ }
238
+ return out;
239
+ }
240
+ /** A string from its opening quote; an unescaped newline or the end of the file refuses. */
241
+ string(quote) {
242
+ const s = this.css;
243
+ this.i++;
244
+ let out = "";
245
+ while (this.i < s.length) {
246
+ const c = s[this.i];
247
+ if (c === quote) {
248
+ this.i++;
249
+ return out;
250
+ }
251
+ if (isNewline(c))
252
+ throw new CssProblem("a string is not closed on its line");
253
+ if (c === "\\") {
254
+ const next = s[this.i + 1];
255
+ if (next === undefined)
256
+ break;
257
+ if (isNewline(next)) {
258
+ this.i += next === "\r" && s[this.i + 2] === "\n" ? 3 : 2;
259
+ }
260
+ else
261
+ out += this.escape();
262
+ }
263
+ else {
264
+ out += c;
265
+ this.i++;
266
+ }
267
+ }
268
+ throw new CssProblem("a string is not closed");
269
+ }
270
+ /** After `url(`: whether the argument is a string (then `url(` is an ordinary function). */
271
+ quoteAfterWhitespace() {
272
+ const s = this.css;
273
+ let j = this.i;
274
+ while (isWhitespace(s[j]))
275
+ j++;
276
+ return s[j] === '"' || s[j] === "'";
277
+ }
278
+ /**
279
+ * An unquoted url token after `url(`: everything up to the first unescaped `)` (a bad url, with a
280
+ * quote or `(` inside, ends there too), escapes decoded; the URL is checked.
281
+ */
282
+ urlToken() {
283
+ const s = this.css;
284
+ let out = "";
285
+ while (this.i < s.length) {
286
+ const c = s[this.i];
287
+ if (c === ")") {
288
+ this.i++;
289
+ const problem = cssUrlProblem(cssTrim(out), this.assets);
290
+ if (problem !== undefined)
291
+ throw new CssProblem(problem);
292
+ return;
293
+ }
294
+ if (c === "\\" && this.i + 1 < s.length && !isNewline(s[this.i + 1]))
295
+ out += this.escape();
296
+ else {
297
+ out += c;
298
+ this.i++;
299
+ }
300
+ }
301
+ throw new CssProblem("a url( is not closed");
302
+ }
303
+ }
304
+ /**
305
+ * The problem of a client stylesheet as stored (see the file header), or undefined when it is one
306
+ * `@layer extensions` block with no `@import` and every URL a `#fragment`, a `data:` URL or one of
307
+ * the plugin's `assets` (paths relative to `assets/`).
308
+ */
309
+ export function clientCssProblem(css, assets) {
310
+ const remote = cssRemoteProblem(css, assets);
311
+ if (remote !== undefined)
312
+ return remote;
313
+ const wrap = WRAP_RE.exec(css);
314
+ if (wrap === null) {
315
+ return "the file must start with @layer extensions { (capybara-plugin pack wraps every stylesheet)";
316
+ }
317
+ try {
318
+ new Scanner(css, wrap[0].length, assets).run();
319
+ }
320
+ catch (e) {
321
+ if (e instanceof CssProblem)
322
+ return e.message;
323
+ throw e;
324
+ }
325
+ return undefined;
326
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The client section of a plugin's extension (client extensions spec §4.1): the browser bundle, CSS
3
+ * files and assets under `./dist/client/`, each pinned by a sha384 integrity. Written by the packer.
4
+ */
5
+ export interface PluginClientSection {
6
+ /** `./dist/client/client.mjs`; absent for CSS-only decoration. */
7
+ entry?: string;
8
+ /** `./dist/client/styles/<name>.css`, in injection order. */
9
+ styles?: string[];
10
+ /** `./dist/client/assets/<path>`. */
11
+ assets?: string[];
12
+ /** sha384 of every file above, keyed by its path: `sha384-<base64>`. */
13
+ integrity: Record<string, string>;
14
+ /** {@link CLIENT_API_VERSION} the bundle was built against. */
15
+ apiVersion: number;
16
+ }
17
+ /** The client API version the host provides (`@capybara-hq/plugin-client` carries the same number). */
18
+ export declare const CLIENT_API_VERSION = 1;
19
+ /** Limits of client code (client extensions spec §4.1, §10). */
20
+ export declare const CLIENT_LIMITS: {
21
+ readonly entryBytes: 1048576;
22
+ readonly styleFiles: 8;
23
+ readonly styleBytes: 262144;
24
+ readonly assetFiles: 64;
25
+ readonly assetBytes: 2097152;
26
+ readonly totalBytes: 8388608;
27
+ readonly loadTimeoutMs: 10000;
28
+ readonly pendingMs: 1500;
29
+ readonly crashBudget: 3;
30
+ };
31
+ /** Allowed asset extensions and the content type each is served with. */
32
+ export declare const CLIENT_ASSET_TYPES: Readonly<Record<string, string>>;
33
+ export declare const CLIENT_ENTRY_PATH = "./dist/client/client.mjs";
34
+ /** Checks a client section (`parsePluginManifest` and the packer use it); returns the problems. */
35
+ export declare function validateClientSection(s: unknown): string[];
@@ -0,0 +1,133 @@
1
+ import { isSafePackagePath } from "./plugin-package.js";
2
+ /** The client API version the host provides (`@capybara-hq/plugin-client` carries the same number). */
3
+ export const CLIENT_API_VERSION = 1;
4
+ /** Limits of client code (client extensions spec §4.1, §10). */
5
+ export const CLIENT_LIMITS = {
6
+ entryBytes: 1048576,
7
+ styleFiles: 8,
8
+ styleBytes: 262144,
9
+ assetFiles: 64,
10
+ assetBytes: 2097152,
11
+ totalBytes: 8388608,
12
+ loadTimeoutMs: 10000,
13
+ pendingMs: 1500,
14
+ crashBudget: 3,
15
+ };
16
+ /** Allowed asset extensions and the content type each is served with. */
17
+ export const CLIENT_ASSET_TYPES = {
18
+ ".png": "image/png",
19
+ ".webp": "image/webp",
20
+ ".svg": "image/svg+xml",
21
+ ".woff2": "font/woff2",
22
+ ".mp3": "audio/mpeg",
23
+ ".ogg": "audio/ogg",
24
+ ".wav": "audio/wav",
25
+ };
26
+ export const CLIENT_ENTRY_PATH = "./dist/client/client.mjs";
27
+ const STYLE_RE = /^\.\/dist\/client\/styles\/[A-Za-z0-9][A-Za-z0-9._-]*\.css$/;
28
+ const ASSET_PREFIX = "./dist/client/assets/";
29
+ /** One segment of an asset path: URL-safe characters only (the kernel's delivery key charset). */
30
+ const ASSET_SEGMENT_RE = /^[A-Za-z0-9._-]+$/;
31
+ const INTEGRITY_RE = /^sha384-[A-Za-z0-9+/]{64}$/;
32
+ const isStringArray = (v) => Array.isArray(v) && v.every((x) => typeof x === "string");
33
+ /** Checks a client section (`parsePluginManifest` and the packer use it); returns the problems. */
34
+ export function validateClientSection(s) {
35
+ if (s === null || typeof s !== "object" || Array.isArray(s))
36
+ return ["client must be an object"];
37
+ const o = s;
38
+ const errs = [];
39
+ for (const k of Object.keys(o)) {
40
+ if (!["entry", "styles", "assets", "integrity", "apiVersion"].includes(k)) {
41
+ errs.push(`client.${k} is not a known field`);
42
+ }
43
+ }
44
+ const listed = [];
45
+ if (o.entry !== undefined) {
46
+ if (o.entry !== CLIENT_ENTRY_PATH)
47
+ errs.push(`client.entry must be ${CLIENT_ENTRY_PATH}`);
48
+ else
49
+ listed.push(o.entry);
50
+ }
51
+ let styleCount = 0;
52
+ if (o.styles !== undefined) {
53
+ if (!isStringArray(o.styles))
54
+ errs.push("client.styles must be an array of strings");
55
+ else {
56
+ styleCount = o.styles.length;
57
+ if (styleCount > CLIENT_LIMITS.styleFiles) {
58
+ errs.push(`client.styles has ${styleCount} files; the limit is ${CLIENT_LIMITS.styleFiles}`);
59
+ }
60
+ for (const p of o.styles) {
61
+ if (!STYLE_RE.test(p)) {
62
+ errs.push(`client.styles: ${p} must be ./dist/client/styles/<name>.css`);
63
+ }
64
+ else if (listed.includes(p))
65
+ errs.push(`client.styles: duplicate ${p}`);
66
+ else
67
+ listed.push(p);
68
+ }
69
+ }
70
+ }
71
+ if (o.assets !== undefined) {
72
+ if (!isStringArray(o.assets))
73
+ errs.push("client.assets must be an array of strings");
74
+ else {
75
+ if (o.assets.length > CLIENT_LIMITS.assetFiles) {
76
+ errs.push(`client.assets has ${o.assets.length} files; the limit is ${CLIENT_LIMITS.assetFiles}`);
77
+ }
78
+ for (const p of o.assets) {
79
+ const rest = p.startsWith(ASSET_PREFIX) ? p.slice(ASSET_PREFIX.length) : null;
80
+ if (rest === null || !isSafePackagePath(rest)) {
81
+ errs.push(`client.assets: ${p} must be a safe path under ${ASSET_PREFIX}`);
82
+ continue;
83
+ }
84
+ // the kernel looks a file up by its exact key, never decoding the URL (`%2e%2e` names no
85
+ // file there), so every segment uses the URL-safe characters only
86
+ if (!rest.split("/").every((seg) => ASSET_SEGMENT_RE.test(seg))) {
87
+ errs.push(`client.assets: ${p} must use letters, digits, . _ - in each path segment`);
88
+ continue;
89
+ }
90
+ const name = rest.slice(rest.lastIndexOf("/") + 1);
91
+ const dot = name.lastIndexOf(".");
92
+ if (dot === 0) {
93
+ errs.push(`client.assets: ${p} needs a file name before the extension`);
94
+ continue;
95
+ }
96
+ const ext = dot < 0 ? "" : name.slice(dot).toLowerCase();
97
+ if (!Object.hasOwn(CLIENT_ASSET_TYPES, ext)) {
98
+ errs.push(`client.assets: ${p} has an unsupported type; allowed: ${Object.keys(CLIENT_ASSET_TYPES).join(" ")}`);
99
+ }
100
+ else if (listed.includes(p))
101
+ errs.push(`client.assets: duplicate ${p}`);
102
+ else
103
+ listed.push(p);
104
+ }
105
+ }
106
+ }
107
+ if (o.entry === undefined && styleCount === 0) {
108
+ errs.push("client needs an entry or at least one style file");
109
+ }
110
+ const integ = o.integrity;
111
+ if (integ === null || typeof integ !== "object" || Array.isArray(integ)) {
112
+ errs.push("client.integrity must be an object");
113
+ }
114
+ else {
115
+ const map = integ;
116
+ for (const p of listed) {
117
+ const v = map[p];
118
+ if (v === undefined)
119
+ errs.push(`client.integrity: missing entry for ${p}`);
120
+ else if (typeof v !== "string" || !INTEGRITY_RE.test(v)) {
121
+ errs.push(`client.integrity: ${p} must be sha384-<base64>`);
122
+ }
123
+ }
124
+ for (const p of Object.keys(map)) {
125
+ if (!listed.includes(p))
126
+ errs.push(`client.integrity: ${p} is not a listed file`);
127
+ }
128
+ }
129
+ if (typeof o.apiVersion !== "number" || !Number.isInteger(o.apiVersion) || o.apiVersion < 1) {
130
+ errs.push("client.apiVersion must be a positive integer");
131
+ }
132
+ return errs;
133
+ }
@@ -10,6 +10,10 @@ import type { LocalizedName } from "./plugin-package.js";
10
10
  export type JsonSchema = Record<string, unknown>;
11
11
  /** The input widget of a field. */
12
12
  export type ConfigWidget = "text" | "textarea" | "url" | "number" | "select" | "radio" | "switch" | "checkbox" | "tristate" | "tags" | "json";
13
+ /** How a channel connection receives events: a long connection (default) or webhooks. */
14
+ export type ChannelInboundMode = "long_connection" | "webhook";
15
+ /** Every channel inbound mode, in the order forms list them. */
16
+ export declare const CHANNEL_INBOUND_MODES: readonly ChannelInboundMode[];
13
17
  /** A labelled value of an enum (or boolean) field. */
14
18
  export interface ConfigUiOption {
15
19
  value: string | number | boolean;
@@ -73,6 +77,13 @@ export interface ConfigUiHints {
73
77
  sections?: ConfigUiSection[];
74
78
  /** Root only: localized messages for custom issues (`params: { ui: key }`). */
75
79
  messages?: Record<string, LocalizedName>;
80
+ /**
81
+ * Channel connection config only, top-level fields only: the inbound modes the field belongs to.
82
+ * Forms draw it only in those modes and require it there; the kernel refuses a connection whose
83
+ * mode requires it without a value (channel inbound mode fields spec §2, §3). The zod schema keeps
84
+ * the field optional, since the other modes do not use it.
85
+ */
86
+ inboundModes?: ChannelInboundMode[];
76
87
  }
77
88
  /** Limits of config UI hints and of the JSON Schemas a plugin package carries. */
78
89
  export declare const CONFIG_UI_LIMITS: {
@@ -129,3 +140,14 @@ export interface ConfigUiProblem {
129
140
  * problem (empty = valid); never throws. Problem paths are dot paths of the node holding the hint.
130
141
  */
131
142
  export declare function validateConfigUi(schema: JsonSchema, level: "deployment" | "org" | "connection"): ConfigUiProblem[];
143
+ /**
144
+ * The top-level fields of a connection config's JSON Schema ({@link configJsonSchema}) that carry
145
+ * an `inboundModes` hint, with their modes (unknown modes dropped). Pure; fields without the hint
146
+ * belong to every mode and are not listed.
147
+ */
148
+ export declare function inboundModeFields(schema: JsonSchema): Record<string, ChannelInboundMode[]>;
149
+ /**
150
+ * The fields `mode` requires ({@link inboundModeFields}) for which `has(field)` is false, in schema
151
+ * order: what the kernel refuses on a channel connection create or config / mode change.
152
+ */
153
+ export declare function missingInboundModeFields(schema: JsonSchema, mode: ChannelInboundMode, has: (field: string) => boolean): string[];
package/dist/config-ui.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { z } from "zod";
2
+ /** Every channel inbound mode, in the order forms list them. */
3
+ export const CHANNEL_INBOUND_MODES = ["long_connection", "webhook"];
2
4
  /** Limits of config UI hints and of the JSON Schemas a plugin package carries. */
3
5
  export const CONFIG_UI_LIMITS = {
4
6
  /** Characters per language of a `label` (also `unit`, `item.addLabel`). */
@@ -163,9 +165,18 @@ const HINT_KEYS = new Set([
163
165
  "secrets",
164
166
  "sections",
165
167
  "messages",
168
+ "inboundModes",
166
169
  ]);
167
170
  /** The only hints a secret node may carry. */
168
- const SECRET_HINT_KEYS = new Set(["label", "help", "placeholder", "order", "section", "advanced"]);
171
+ const SECRET_HINT_KEYS = new Set([
172
+ "label",
173
+ "help",
174
+ "placeholder",
175
+ "order",
176
+ "section",
177
+ "advanced",
178
+ "inboundModes",
179
+ ]);
169
180
  const ITEM_KEYS = new Set(["title", "subtitle", "addLabel", "reorder"]);
170
181
  const utf8Bytes = (s) => new TextEncoder().encode(s).length;
171
182
  /** The JSON types a node accepts (`type`, a type list, or the branches of a nullable `anyOf`). */
@@ -326,7 +337,7 @@ export function validateConfigUi(schema, level) {
326
337
  if (!isObj(raw))
327
338
  report("x-ui must be an object");
328
339
  else
329
- checkHints(raw, node, isRoot, report, text);
340
+ checkHints(raw, node, isRoot, depth, report, text);
330
341
  }
331
342
  const shape = shapeOf(node);
332
343
  const props = shape.properties;
@@ -343,7 +354,7 @@ export function validateConfigUi(schema, level) {
343
354
  visit(shape.additionalProperties, path === "" ? "additionalProperties" : `${path}.additionalProperties`, false, depth + 1);
344
355
  }
345
356
  };
346
- const checkHints = (h, node, isRoot, report, text) => {
357
+ const checkHints = (h, node, isRoot, depth, report, text) => {
347
358
  const shape = shapeOf(node);
348
359
  const types = typesOf(node);
349
360
  const isArray = types.has("array");
@@ -356,7 +367,7 @@ export function validateConfigUi(schema, level) {
356
367
  if (node["x-secret"] === true) {
357
368
  const bad = Object.keys(h).filter((k) => HINT_KEYS.has(k) && !SECRET_HINT_KEYS.has(k));
358
369
  if (bad.length > 0) {
359
- report(`a secret field allows only label, help, placeholder, order, section and advanced hints, not ${bad.join(", ")}`);
370
+ report(`a secret field allows only label, help, placeholder, order, section, advanced and inboundModes hints, not ${bad.join(", ")}`);
360
371
  }
361
372
  }
362
373
  text(h.label, "label", L.labelChars);
@@ -369,6 +380,28 @@ export function validateConfigUi(schema, level) {
369
380
  if (h.advanced !== undefined && typeof h.advanced !== "boolean") {
370
381
  report("advanced must be a boolean");
371
382
  }
383
+ if (h.inboundModes !== undefined) {
384
+ const modes = h.inboundModes;
385
+ if (level !== "connection") {
386
+ report(`inboundModes is only allowed at the connection level, not ${level}`);
387
+ }
388
+ else if (depth !== 1) {
389
+ report("inboundModes is only allowed on a top-level field of the config");
390
+ }
391
+ else if (!Array.isArray(modes) || modes.length === 0) {
392
+ report("inboundModes must be a non-empty array");
393
+ }
394
+ else {
395
+ modes.forEach((m, i) => {
396
+ if (!CHANNEL_INBOUND_MODES.includes(m)) {
397
+ report(`inboundModes[${i}] must be one of ${CHANNEL_INBOUND_MODES.join(", ")}`);
398
+ }
399
+ else if (modes.indexOf(m) !== i) {
400
+ report(`inboundModes[${i}] "${String(m)}" is listed twice`);
401
+ }
402
+ });
403
+ }
404
+ }
372
405
  if (h.section !== undefined) {
373
406
  if (typeof h.section !== "string" || !sectionIds.has(h.section)) {
374
407
  report(`section "${String(h.section)}" is not an id of the root's sections`);
@@ -600,3 +633,30 @@ export function validateConfigUi(schema, level) {
600
633
  }
601
634
  return problems;
602
635
  }
636
+ /**
637
+ * The top-level fields of a connection config's JSON Schema ({@link configJsonSchema}) that carry
638
+ * an `inboundModes` hint, with their modes (unknown modes dropped). Pure; fields without the hint
639
+ * belong to every mode and are not listed.
640
+ */
641
+ export function inboundModeFields(schema) {
642
+ const out = {};
643
+ for (const [key, node] of Object.entries(properties(schema))) {
644
+ const hints = isObj(node) ? node["x-ui"] : undefined;
645
+ const modes = isObj(hints) ? hints.inboundModes : undefined;
646
+ if (!Array.isArray(modes))
647
+ continue;
648
+ const known = CHANNEL_INBOUND_MODES.filter((m) => modes.includes(m));
649
+ if (known.length > 0)
650
+ out[key] = known;
651
+ }
652
+ return out;
653
+ }
654
+ /**
655
+ * The fields `mode` requires ({@link inboundModeFields}) for which `has(field)` is false, in schema
656
+ * order: what the kernel refuses on a channel connection create or config / mode change.
657
+ */
658
+ export function missingInboundModeFields(schema, mode, has) {
659
+ return Object.entries(inboundModeFields(schema))
660
+ .filter(([key, modes]) => modes.includes(mode) && !has(key))
661
+ .map(([key]) => key);
662
+ }